Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit aa396b96 authored by Jack Palevich's avatar Jack Palevich
Browse files

Unhide the Android OpenGL ES 2.0 API

Add a Matrix.setLookAtM method for computing a look-at viewing transform.

Change GLU.lookAt to use Matrix.setLook.
parent 0dce2dd2
Loading
Loading
Loading
Loading
+6601 −16

File changed.

Preview size limit exceeded, changes collapsed.

+14 −14
Original line number Diff line number Diff line
@@ -19,8 +19,8 @@

package android.opengl;

/** OpenGL ES 2.0
 * @hide
/** OpenGL ES 2.0. This class exposes the core OpenGL ES 2.0 APIs.
 * All the methods are static.
 */
public class GLES20 {
    public static final int GL_ACTIVE_TEXTURE                          = 0x84E0;
@@ -49,7 +49,7 @@ public class GLES20 {
    public static final int GL_SRC_ALPHA_SATURATE                      = 0x0308;
    public static final int GL_FUNC_ADD                                = 0x8006;
    public static final int GL_BLEND_EQUATION                          = 0x8009;
    public static final int GL_BLEND_EQUATION_RGB                      = 0x8009;   /* same as BLEND_EQUATION */
    public static final int GL_BLEND_EQUATION_RGB                      = 0x8009;
    public static final int GL_BLEND_EQUATION_ALPHA                    = 0x883D;
    public static final int GL_FUNC_SUBTRACT                           = 0x800A;
    public static final int GL_FUNC_REVERSE_SUBTRACT                   = 0x800B;
+0 −1
Original line number Diff line number Diff line
@@ -398,7 +398,6 @@ public class GLSurfaceView extends SurfaceView implements SurfaceHolder.Callback
     * {@link #setEGLConfigChooser(EGLConfigChooser)} has been called, then the supplied
     * EGLConfigChooser is responsible for choosing an OpenGL ES 2.0-compatible config.
     * @param version The EGLContext client version to choose. Use 2 for OpenGL ES 2.0
     * @hide
     */
    public void setEGLContextClientVersion(int version) {
        checkRenderThreadState();
+2 −50
Original line number Diff line number Diff line
@@ -72,60 +72,12 @@ public class GLU {
            float centerX, float centerY, float centerZ, float upX, float upY,
            float upZ) {

        // See the OpenGL GLUT documentation for gluLookAt for a description
        // of the algorithm. We implement it in a straightforward way:

        float fx = centerX - eyeX;
        float fy = centerY - eyeY;
        float fz = centerZ - eyeZ;

        // Normalize f
        float rlf = 1.0f / Matrix.length(fx, fy, fz);
        fx *= rlf;
        fy *= rlf;
        fz *= rlf;

        // compute s = f x up (x means "cross product")
        float sx = fy * upZ - fz * upY;
        float sy = fz * upX - fx * upZ;
        float sz = fx * upY - fy * upX;

        // and normalize s
        float rls = 1.0f / Matrix.length(sx, sy, sz);
        sx *= rls;
        sy *= rls;
        sz *= rls;

        // compute u = s x f
        float ux = sy * fz - sz * fy;
        float uy = sz * fx - sx * fz;
        float uz = sx * fy - sy * fx;

        float[] scratch = sScratch;
        synchronized(scratch) {
            scratch[0] = sx;
            scratch[1] = ux;
            scratch[2] = -fx;
            scratch[3] = 0.0f;

            scratch[4] = sy;
            scratch[5] = uy;
            scratch[6] = -fy;
            scratch[7] = 0.0f;

            scratch[8] = sz;
            scratch[9] = uz;
            scratch[10] = -fz;
            scratch[11] = 0.0f;

            scratch[12] = 0.0f;
            scratch[13] = 0.0f;
            scratch[14] = 0.0f;
            scratch[15] = 1.0f;

            Matrix.setLookAtM(scratch, 0, eyeX, eyeY, eyeZ, centerX, centerY, centerZ,
                    upX, upY, upZ);
            gl.glMultMatrixf(scratch, 0);
        }
        gl.glTranslatef(-eyeX, -eyeY, -eyeZ);
    }

    /**
+75 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package android.opengl;

import javax.microedition.khronos.opengles.GL10;

/**
 * Matrix math utilities. These methods operate on OpenGL ES format
 * matrices and vectors stored in float arrays.
@@ -582,4 +584,77 @@ public class Matrix {
        rm[rmOffset + 14] =  0.0f;
        rm[rmOffset + 15] =  1.0f;
    }

    /**
     * Define a viewing transformation in terms of an eye point, a center of
     * view, and an up vector.
     *
     * @param rm returns the result
     * @param rmOffset index into rm where the result matrix starts
     * @param eyeX eye point X
     * @param eyeY eye point Y
     * @param eyeZ eye point Z
     * @param centerX center of view X
     * @param centerY center of view Y
     * @param centerZ center of view Z
     * @param upX up vector X
     * @param upY up vector Y
     * @param upZ up vector Z
     */
    public static void setLookAtM(float[] rm, int rmOffset,
            float eyeX, float eyeY, float eyeZ,
            float centerX, float centerY, float centerZ, float upX, float upY,
            float upZ) {

        // See the OpenGL GLUT documentation for gluLookAt for a description
        // of the algorithm. We implement it in a straightforward way:

        float fx = centerX - eyeX;
        float fy = centerY - eyeY;
        float fz = centerZ - eyeZ;

        // Normalize f
        float rlf = 1.0f / Matrix.length(fx, fy, fz);
        fx *= rlf;
        fy *= rlf;
        fz *= rlf;

        // compute s = f x up (x means "cross product")
        float sx = fy * upZ - fz * upY;
        float sy = fz * upX - fx * upZ;
        float sz = fx * upY - fy * upX;

        // and normalize s
        float rls = 1.0f / Matrix.length(sx, sy, sz);
        sx *= rls;
        sy *= rls;
        sz *= rls;

        // compute u = s x f
        float ux = sy * fz - sz * fy;
        float uy = sz * fx - sx * fz;
        float uz = sx * fy - sy * fx;

        rm[rmOffset + 0] = sx;
        rm[rmOffset + 1] = ux;
        rm[rmOffset + 2] = -fx;
        rm[rmOffset + 3] = 0.0f;

        rm[rmOffset + 4] = sy;
        rm[rmOffset + 5] = uy;
        rm[rmOffset + 6] = -fy;
        rm[rmOffset + 7] = 0.0f;

        rm[rmOffset + 8] = sz;
        rm[rmOffset + 9] = uz;
        rm[rmOffset + 10] = -fz;
        rm[rmOffset + 11] = 0.0f;

        rm[rmOffset + 12] = 0.0f;
        rm[rmOffset + 13] = 0.0f;
        rm[rmOffset + 14] = 0.0f;
        rm[rmOffset + 15] = 1.0f;

        translateM(rm, rmOffset, -eyeX, -eyeY, -eyeZ);
    }
}