public void onSurfaceCreated(GL10 glUnused, javax.microedition.khronos.egl.EGLConfig config)
            {
                // and now we still need redux?

                // Set the background clear color to gray.
                gl.clearColor(0.5f, 0.5f, 0.5f, 0.5f);

                // Position the eye behind the origin.
                const float eyeX = 0.0f;
                const float eyeY = 0.0f;
                const float eyeZ = 1.5f;

                // We are looking toward the distance
                const float lookX = 0.0f;
                const float lookY = 0.0f;
                const float lookZ = -5.0f;

                // Set our up vector. This is where our head would be pointing were we holding the camera.
                const float upX = 0.0f;
                const float upY = 1.0f;
                const float upZ = 0.0f;

                // Set the view matrix. This matrix can be said to represent the camera position.
                // NOTE: In OpenGL 1, a ModelView matrix is used, which is a combination of a model and
                // view matrix. In OpenGL 2, we can keep track of these matrices separately if we choose.
                Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);



                // Create a program object and store the handle to it.


                var programHandle = gl.createProgram(
                    new Shaders.TriangleVertexShader(),
                    new Shaders.TriangleFragmentShader()
                    );

                gl.bindAttribLocation(programHandle, 0, "a_Position");
                gl.bindAttribLocation(programHandle, 1, "a_Color");

                gl.linkProgram(programHandle);

                // Set program handles. These will later be used to pass in values to the program.
                mMVPMatrixHandle = gl.getUniformLocation(programHandle, "u_MVPMatrix");
                mPositionHandle  = gl.getAttribLocation(programHandle, "a_Position");
                mColorHandle     = gl.getAttribLocation(programHandle, "a_Color");

                // Tell OpenGL to use this program when rendering.
                gl.useProgram(programHandle);
            }
Ejemplo n.º 2
0
            public void onDrawFrame(GL10 glUnused)
            {
                gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

                // Do a complete rotation every 10 seconds.
                long  time           = SystemClock.uptimeMillis() % 10000L;
                float angleInDegrees = (360.0f / 10000.0f) * ((int)time);

                // Set our per-vertex lighting program.
                gl.useProgram(mPerVertexProgramHandle);

                // Set program handles for cube drawing.
                mMVPMatrixHandle = gl.getUniformLocation(mPerVertexProgramHandle, "u_MVPMatrix");
                mMVMatrixHandle  = gl.getUniformLocation(mPerVertexProgramHandle, "u_MVMatrix");
                mLightPosHandle  = gl.getUniformLocation(mPerVertexProgramHandle, "u_LightPos");

                mPositionHandle = gl.getAttribLocation(mPerVertexProgramHandle, "a_Position");
                mColorHandle    = gl.getAttribLocation(mPerVertexProgramHandle, "a_Color");
                mNormalHandle   = gl.getAttribLocation(mPerVertexProgramHandle, "a_Normal");

                // Calculate position of the light. Rotate and then push into the distance.
                Matrix.setIdentityM(mLightModelMatrix, 0);
                Matrix.translateM(mLightModelMatrix, 0, 0.0f, 0.0f, -5.0f);
                Matrix.rotateM(mLightModelMatrix, 0, angleInDegrees, 0.0f, 1.0f, 0.0f);
                Matrix.translateM(mLightModelMatrix, 0, 0.0f, 0.0f, 2.0f);

                Matrix.multiplyMV(mLightPosInWorldSpace, 0, mLightModelMatrix, 0, mLightPosInModelSpace, 0);
                Matrix.multiplyMV(mLightPosInEyeSpace, 0, mViewMatrix, 0, mLightPosInWorldSpace, 0);

                #region drawCube
                Action drawCube =
                    delegate
                {
                    // Pass in the position information
                    mCubePositions.position(0);

                    opengl.glVertexAttribPointer(mPositionHandle, mPositionDataSize, (int)gl.FLOAT, false,
                                                 0, mCubePositions);

                    gl.enableVertexAttribArray((uint)mPositionHandle);

                    // Pass in the color information
                    mCubeColors.position(0);
                    opengl.glVertexAttribPointer(mColorHandle, mColorDataSize, (int)gl.FLOAT, false,
                                                 0, mCubeColors);

                    gl.enableVertexAttribArray((uint)mColorHandle);

                    // Pass in the normal information
                    mCubeNormals.position(0);
                    opengl.glVertexAttribPointer(mNormalHandle, mNormalDataSize, (int)gl.FLOAT, false,
                                                 0, mCubeNormals);

                    gl.enableVertexAttribArray((uint)mNormalHandle);

                    // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix
                    // (which currently contains model * view).
                    Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);

                    // Pass in the modelview matrix.
                    gl.uniformMatrix4fv(mMVMatrixHandle, false, mMVPMatrix);

                    // This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix
                    // (which now contains model * view * projection).
                    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);

                    // Pass in the combined matrix.
                    gl.uniformMatrix4fv(mMVPMatrixHandle, false, mMVPMatrix);

                    // Pass in the light position in eye space.
                    gl.uniform3f(mLightPosHandle, mLightPosInEyeSpace[0], mLightPosInEyeSpace[1], mLightPosInEyeSpace[2]);

                    // Draw the cube.
                    gl.drawArrays(gl.TRIANGLES, 0, 36);
                };
                #endregion


                // Draw some cubes.
                Matrix.setIdentityM(mModelMatrix, 0);
                Matrix.translateM(mModelMatrix, 0, 4.0f, 0.0f, -7.0f);
                Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 1.0f, 0.0f, 0.0f);
                drawCube();

                Matrix.setIdentityM(mModelMatrix, 0);
                Matrix.translateM(mModelMatrix, 0, -4.0f, 0.0f, -7.0f);
                Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 1.0f, 0.0f);
                drawCube();

                Matrix.setIdentityM(mModelMatrix, 0);
                Matrix.translateM(mModelMatrix, 0, 0.0f, 4.0f, -7.0f);
                Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);
                drawCube();

                Matrix.setIdentityM(mModelMatrix, 0);
                Matrix.translateM(mModelMatrix, 0, 0.0f, -4.0f, -7.0f);
                drawCube();

                Matrix.setIdentityM(mModelMatrix, 0);
                Matrix.translateM(mModelMatrix, 0, 0.0f, 0.0f, -5.0f);
                Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 1.0f, 1.0f, 0.0f);
                drawCube();

                #region drawLight
                Action drawLight =
                    delegate
                {
                    var pointMVPMatrixHandle = gl.getUniformLocation(mPointProgramHandle, "u_MVPMatrix");
                    var pointPositionHandle  = gl.getAttribLocation(mPointProgramHandle, "a_Position");

                    // Pass in the position.
                    gl.vertexAttrib3f((uint)pointPositionHandle, mLightPosInModelSpace[0], mLightPosInModelSpace[1], mLightPosInModelSpace[2]);

                    // Since we are not using a buffer object, disable vertex arrays for this attribute.
                    gl.disableVertexAttribArray((uint)pointPositionHandle);

                    // Pass in the transformation matrix.
                    Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mLightModelMatrix, 0);
                    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);

                    gl.uniformMatrix4fv(pointMVPMatrixHandle, false, mMVPMatrix);

                    // Draw the point.
                    gl.drawArrays(gl.POINTS, 0, 1);
                };
                #endregion

                // Draw a point to indicate the light.
                gl.useProgram(mPointProgramHandle);
                drawLight();
            }
            public void onDrawFrame(GL10 glUnused)
            {
                if (mBlending)
                {
                    gl.clear(gl.COLOR_BUFFER_BIT);
                }
                else
                {
                    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
                }

                // Do a complete rotation every 10 seconds.
                long  time           = SystemClock.uptimeMillis() % 10000L;
                float angleInDegrees = (360.0f / 10000.0f) * ((int)time);

                // Set our program
                gl.useProgram(mProgramHandle);

                // Set program handles for cube drawing.
                mMVPMatrixHandle = gl.getUniformLocation(mProgramHandle, "u_MVPMatrix");
                mPositionHandle  = gl.getAttribLocation(mProgramHandle, "a_Position");
                mColorHandle     = gl.getAttribLocation(mProgramHandle, "a_Color");

                #region drawCube
                Action drawCube =
                    delegate
                {
                    // Pass in the position information
                    mCubePositions.position(0);


                    GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, (int)gl.FLOAT, false,
                                                 0, mCubePositions);

                    gl.enableVertexAttribArray((uint)mPositionHandle);

                    // Pass in the color information
                    mCubeColors.position(0);
                    GLES20.glVertexAttribPointer(mColorHandle, mColorDataSize, (int)gl.FLOAT, false,
                                                 0, mCubeColors);

                    gl.enableVertexAttribArray((uint)mColorHandle);

                    // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix
                    // (which currently contains model * view).
                    Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);

                    // This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix
                    // (which now contains model * view * projection).
                    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);

                    // Pass in the combined matrix.
                    gl.uniformMatrix4fv(mMVPMatrixHandle, false, mMVPMatrix);

                    // Draw the cube.
                    gl.drawArrays(gl.TRIANGLES, 0, 36);
                };
                #endregion

                // Draw some cubes.
                Matrix.setIdentityM(mModelMatrix, 0);
                Matrix.translateM(mModelMatrix, 0, 4.0f, 0.0f, -7.0f);
                Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 1.0f, 0.0f, 0.0f);
                drawCube();

                Matrix.setIdentityM(mModelMatrix, 0);
                Matrix.translateM(mModelMatrix, 0, -4.0f, 0.0f, -7.0f);
                Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 1.0f, 0.0f);
                drawCube();

                Matrix.setIdentityM(mModelMatrix, 0);
                Matrix.translateM(mModelMatrix, 0, 0.0f, 4.0f, -7.0f);
                Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);
                drawCube();

                Matrix.setIdentityM(mModelMatrix, 0);
                Matrix.translateM(mModelMatrix, 0, 0.0f, -4.0f, -7.0f);
                drawCube();

                Matrix.setIdentityM(mModelMatrix, 0);
                Matrix.translateM(mModelMatrix, 0, 0.0f, 0.0f, -5.0f);
                Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 1.0f, 1.0f, 0.0f);
                drawCube();
            }