Exemplo n.º 1
0
        public void OnDrawFrame(Javax.Microedition.Khronos.Opengles.IGL10 gl)
        {
            float[] scratch = new float[16];

            // Draw background color
            GLES20.GlClear(GLES20.GlColorBufferBit);

            //synchronized (this) {
            if (mUpdateST)
            {
                mSTexture.UpdateTexImage();
                mUpdateST = false;
            }
            //}

            GLES20.GlUseProgram(hProgram);

            int ph  = GLES20.GlGetAttribLocation(hProgram, "vPosition");
            int tch = GLES20.GlGetAttribLocation(hProgram, "vTexCoord");
            int th  = GLES20.GlGetUniformLocation(hProgram, "sTexture");

            GLES20.GlActiveTexture(GLES20.GlTexture0);
            GLES20.GlBindTexture(GLES11Ext.GlTextureExternalOes, hTex[0]);
            GLES20.GlUniform1i(th, 0);

            GLES20.GlVertexAttribPointer(ph, 2, GLES20.GlFloat, false, 4 * 2, pVertex);
            GLES20.GlVertexAttribPointer(tch, 2, GLES20.GlFloat, false, 4 * 2, pTexCoord);
            GLES20.GlEnableVertexAttribArray(ph);
            GLES20.GlEnableVertexAttribArray(tch);

            GLES20.GlDrawArrays(GLES20.GlTriangleStrip, 0, 4);


            // Set the camera position (View matrix)
            Android.Opengl.Matrix.SetLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

            // Calculate the projection and view transformation
            Android.Opengl.Matrix.MultiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);


            // Create a rotation for the triangle

            // Use the following code to generate constant rotation.
            // Leave this code out when using TouchEvents.
            // long time = SystemClock.uptimeMillis() % 4000L;
            // float angle = 0.090f * ((int) time);

            Android.Opengl.Matrix.SetRotateM(mRotationMatrix, 0, mAngle, 0, 0, 1.0f);

            // Combine the rotation matrix with the projection and camera view
            // Note that the mMVPMatrix factor *must be first* in order
            // for the matrix multiplication product to be correct.
            Android.Opengl.Matrix.MultiplyMM(scratch, 0, mMVPMatrix, 0, mRotationMatrix, 0);

            // Draw triangle
            mTriangle.draw(scratch);
        }
Exemplo n.º 2
0
        public void OnDrawFrame(IGL10 gl)
        {
            GLES20.GlClear(GLES20.GlColorBufferBit | GLES20.GlDepthBufferBit);

            // Draw the triangle facing straight on.
            angleInDegrees += 0.2f;
            //Prepare model transformation matrix
            float[] mModelMatrix = new float[16];
            Matrix.SetIdentityM(mModelMatrix, 0);
            Matrix.RotateM(mModelMatrix, 0, angleInDegrees, 1.0f, 0.0f, 0.0f);

            //Draw with VBO
            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[0]);
            GLES20.GlEnableVertexAttribArray(mPositionHandle);
            GLES20.GlVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GlFloat, false, 0, 0);

            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[1]);
            GLES20.GlEnableVertexAttribArray(mColorHandle);
            GLES20.GlVertexAttribPointer(mColorHandle, mColorDataSize, GLES20.GlFloat, false, 0, 0);

            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[2]);
            GLES20.GlEnableVertexAttribArray(mTextureCoordHandle);
            GLES20.GlVertexAttribPointer(mTextureCoordHandle, 2, GLES20.GlFloat, false, 0, 0);

            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[3]);
            GLES20.GlEnableVertexAttribArray(mNormalHandle);
            GLES20.GlVertexAttribPointer(mNormalHandle, 3, GLES20.GlFloat, false, 0, 0);

            GLES20.GlActiveTexture(GLES20.GlTexture0);
            GLES20.GlBindTexture(GLES20.GlTexture2d, textureHandle[0]);
            GLES20.GlUniform1i(mTextureHandle, 0);

            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, 0);
            //END OF Draw with VBO

            //light position
            // GLES20.GlUniform3f(mLightPos, 0.0f, 0.0f, angleInDegrees);
            GLES20.GlUniform3f(mLightPos, 0.0f, 0.0f, 0.0f);

            // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix
            // (which currently contains model * view).
            // Allocate storage for the final combined matrix. This will be passed into the shader program. */
            float[] mMVPMatrix = new float[16];
            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).
            // THIS IS NOT WORK AT C# Matrix class -> Matrix.MultiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
            float[] _mMVPMatrix = new float[16];
            Matrix.MultiplyMM(_mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);

            GLES20.GlUniformMatrix4fv(mMVPMatrixHandle, 1, false, _mMVPMatrix, 0);

            GLES20.GlDrawArrays(GLES20.GlTriangles, 0, 3 * 12); //Cube has 12 triagle faces each face has 3 coord
        }
Exemplo n.º 3
0
            public void OnSurfaceCreated(Javax.Microedition.Khronos.Opengles.IGL10 gl, Javax.Microedition.Khronos.Egl.EGLConfig config)
            {
                _glProgram = CreateProgram(mVertexShader, mFragmentShader);

                var extensions = " " + GLES20.GlGetString(GLES20.GlExtensions) + " ";
                var ok         = extensions.IndexOf("GL_OES_framebuffer_object") >= 0;
                var ab         = extensions;

                if (_glProgram == 0)
                {
                    return;
                }

                GLES20.GlUseProgram(0);
                GLES20.GlUseProgram(_glProgram);

                _aPositionHandle     = ActOnError(GLES20.GlGetAttribLocation(_glProgram, "aPosition"), "aPosition");
                _aTextureCoord       = ActOnError(GLES20.GlGetAttribLocation(_glProgram, "aTextureCoord"), "aTextureCoord");
                _uMVPMatrixHandle    = ActOnError(GLES20.GlGetUniformLocation(_glProgram, "uMVPMatrix"), "uMVPMatrix");
                _uSTMatrixHandle     = ActOnError(GLES20.GlGetUniformLocation(_glProgram, "uSTMatrix"), "uSTMatrix");
                _OESTextureUniform   = ActOnError(GLES20.GlGetUniformLocation(_glProgram, textureName), textureName);
                _otherTextureUniform = ActOnError(GLES20.GlGetUniformLocation(_glProgram, "overlay"), "overlay");

                int[] textures = new int[1];
                GLES20.GlGenTextures(1, textures, 0);

                _OESTextureId = textures[0];
                GLES20.GlUseProgram(0);
                GLES20.GlUseProgram(_glProgram);
                GLES20.GlActiveTexture(GLES20.GlTexture1);
                GLES20.GlBindTexture(GL_TEXTURE_EXTERNAL_OES, _OESTextureId);
                GLES20.GlUniform1i(_OESTextureUniform, 1);
                GLES20.GlUseProgram(0);
                GLES20.GlUseProgram(_glProgram);

                GLES20.GlTexParameterf(GL_TEXTURE_EXTERNAL_OES, GLES20.GlTextureMinFilter,
                                       GLES20.GlNearest);
                GLES20.GlTexParameterf(GL_TEXTURE_EXTERNAL_OES, GLES20.GlTextureMagFilter,
                                       GLES20.GlLinear);

                _surfaceTexture = new SurfaceTexture(_OESTextureId);
                _surfaceTexture.FrameAvailable += _surfaceTexture_FrameAvailable;
                Surface surface = new Surface(_surfaceTexture);

                _mediaPlayer.SetSurface(surface);
                surface.Release();

                _otherTextureId = CreateTargetTexture();
                GLES20.GlActiveTexture(GLES20.GlTexture2);
                GLES20.GlBindTexture(GLES20.GlTexture2d, _otherTextureId);
                GLES20.GlUniform1i(_otherTextureUniform, 2);

                _updateSurface = false;
                _mediaPlayer.Start();
            }
        /// <summary>
        /// Draw face geometrical features. This method is called on each frame.
        /// </summary>
        private void DrawFaceGeometry()
        {
            ShaderUtil.CheckGlError(TAG, "Before draw.");
            Log.Debug(TAG, "Draw face geometry: mPointsNum: " + mPointsNum + " mTrianglesNum: " + mTrianglesNum);

            GLES20.GlActiveTexture(GLES20.GlTexture0);
            GLES20.GlBindTexture(GLES20.GlTexture2d, mTextureName);
            GLES20.GlUniform1i(mTextureUniform, 0);
            ShaderUtil.CheckGlError(TAG, "Init texture.");

            GLES20.GlEnable(GLES20.GlDepthTest);
            GLES20.GlEnable(GL_CULL_FACE_CONSTANT);

            // Draw point.
            GLES20.GlUseProgram(mProgram);
            ShaderUtil.CheckGlError(TAG, "Draw point.");
            GLES20.GlEnableVertexAttribArray(mPositionAttribute);
            GLES20.GlEnableVertexAttribArray(mTextureCoordAttribute);
            GLES20.GlEnableVertexAttribArray(mColorUniform);
            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, mVerticeId);
            GLES20.GlVertexAttribPointer(mPositionAttribute, POSITION_COMPONENTS_NUMBER, GLES20.GlFloat, false,
                                         BYTES_PER_POINT, 0);
            GLES20.GlVertexAttribPointer(mTextureCoordAttribute, TEXCOORD_COMPONENTS_NUMBER, GLES20.GlFloat, false,
                                         BYTES_PER_COORD, 0);
            GLES20.GlUniform4f(mColorUniform, 1.0f, 0.0f, 0.0f, 1.0f);
            GLES20.GlUniformMatrix4fv(mModelViewProjectionUniform, 1, false, mModelViewProjections, 0);
            GLES20.GlUniform1f(mPointSizeUniform, 5.0f); // Set the size of Point to 5.
            GLES20.GlDrawArrays(GLES20.GlPoints, 0, mPointsNum);
            GLES20.GlDisableVertexAttribArray(mColorUniform);
            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, 0);
            ShaderUtil.CheckGlError(TAG, "Draw point.");

            // Draw triangles.
            GLES20.GlEnableVertexAttribArray(mColorUniform);

            // Clear the color and use the texture color to draw triangles.
            GLES20.GlUniform4f(mColorUniform, 0.0f, 0.0f, 0.0f, 0.0f);
            GLES20.GlBindBuffer(GLES20.GlElementArrayBuffer, mTriangleId);

            // The number of input triangle points
            GLES20.GlDrawElements(GLES20.GlTriangles, mTrianglesNum * 3, GLES20.GlUnsignedInt, 0);
            GLES20.GlBindBuffer(GLES20.GlElementArrayBuffer, 0);
            GLES20.GlDisableVertexAttribArray(mColorUniform);
            ShaderUtil.CheckGlError(TAG, "Draw triangles.");

            GLES20.GlDisableVertexAttribArray(mTextureCoordAttribute);
            GLES20.GlDisableVertexAttribArray(mPositionAttribute);
            GLES20.GlBindTexture(GLES20.GlTexture2d, 0);

            GLES20.GlDisable(GLES20.GlDepthTest);
            GLES20.GlDisable(GL_CULL_FACE_CONSTANT);
            ShaderUtil.CheckGlError(TAG, "Draw after.");
        }
Exemplo n.º 5
0
        /**
         * \brief Draw this mesh (in OpenGL).
         * @param modelViewProjection this mesh model-view-projection matrix.
         */
        public void DrawMesh(float[] modelViewProjection)
        {
            //set up gl state
            GLES20.GlEnable(GLES20.GlDepthTest);
            //GLES20.GlDisable(GLES20.GlCullFaceMode);
            GLES20.GlCullFace(GLES20.GlBack);
            GLES20.GlFrontFace(GLES20.GlCw);

            //set shader program to use
            GLES20.GlUseProgram(mProgram_GL_ID);
            RenderUtils.CheckGLError("DrawMesh:glUseProgram");

            //find attrib and unifroms in shader program
            int vertexHandle = GLES20.GlGetAttribLocation(mProgram_GL_ID, "vertexPosition");
            //int normalHandle = GLES20.GlGetAttribLocation(Program_GL_ID, "vertexNormal");
            int textureCoordHandle = GLES20.GlGetAttribLocation(mProgram_GL_ID, "vertexTexCoord");
            int mvpMatrixHandle    = GLES20.GlGetUniformLocation(mProgram_GL_ID, "modelViewProjectionMatrix");
            int texSampler2DHandle = GLES20.GlGetUniformLocation(mProgram_GL_ID, "texSampler2D");

            RenderUtils.CheckGLError("DrawMesh:get attribs and uniforms");

            //upload mesh data to OpenGL attribs
            GLES20.GlVertexAttribPointer(vertexHandle, 3, GLES20.GlFloat, false, 0, mVertices_Buffer);
            //GLES20.GlVertexAttribPointer(normalHandle, 3, GLES20.GlFloat, false, 0, Normals_Buffer);
            GLES20.GlVertexAttribPointer(textureCoordHandle, 2, GLES20.GlFloat, false, 0, mTexCoords_Buffer);
            RenderUtils.CheckGLError("DrawMesh:put attrib pointers");

            //enable gl attribs to use
            GLES20.GlEnableVertexAttribArray(vertexHandle);
            //GLES20.GlEnableVertexAttribArray(normalHandle);
            GLES20.GlEnableVertexAttribArray(textureCoordHandle);
            RenderUtils.CheckGLError("DrawMesh:enable attrib arrays");

            // activate texture 0, bind it, and pass to shader
            GLES20.GlActiveTexture(GLES20.GlTexture0);
            GLES20.GlBindTexture(GLES20.GlTexture2d, mTexture_GL_ID);
            GLES20.GlUniform1i(texSampler2DHandle, 0);
            RenderUtils.CheckGLError("DrawMesh:activate texturing");

            // pass the model view matrix to the shader
            GLES20.GlUniformMatrix4fv(mvpMatrixHandle, 1, false, modelViewProjection, 0);
            RenderUtils.CheckGLError("DrawMesh:upload matrix");

            // finally draw the teapot
            GLES20.GlDrawElements(GLES20.GlTriangles, mIndices_Number, GLES20.GlUnsignedShort, mIndex_Buffer);
            RenderUtils.CheckGLError("DrawMesh:draw elements");

            // disable the enabled arrays
            GLES20.GlDisableVertexAttribArray(vertexHandle);
            //GLES20.GlDisableVertexAttribArray(normalHandle);
            GLES20.GlDisableVertexAttribArray(textureCoordHandle);
            RenderUtils.CheckGLError("DrawMesh:disable attrib arrays");
        }
        /// <summary>
        /// Initialize the OpenGL ES rendering related to face geometry,
        /// including creating the shader program.
        /// This method is called when FaceRenderManager's OnSurfaceCreated method calling.
        /// </summary>
        /// <param name="context">Context.</param>
        public void Init(Context context)
        {
            ShaderUtil.CheckGlError(TAG, "Init start.");
            int[] texNames = new int[1];
            GLES20.GlActiveTexture(GLES20.GlTexture0);
            GLES20.GlGenTextures(1, texNames, 0);
            mTextureName = texNames[0];

            int[] buffers = new int[BUFFER_OBJECT_NUMBER];
            GLES20.GlGenBuffers(BUFFER_OBJECT_NUMBER, buffers, 0);
            mVerticeId  = buffers[0];
            mTriangleId = buffers[1];

            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, mVerticeId);
            GLES20.GlBufferData(GLES20.GlArrayBuffer, mVerticeBufferSize * BYTES_PER_POINT, null, GLES20.GlDynamicDraw);
            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, 0);

            GLES20.GlBindBuffer(GLES20.GlElementArrayBuffer, mTriangleId);

            // Each floating-point number occupies 4 bytes.
            GLES20.GlBufferData(GLES20.GlElementArrayBuffer, mTriangleBufferSize * 4, null,
                                GLES20.GlDynamicDraw);
            GLES20.GlBindBuffer(GLES20.GlElementArrayBuffer, 0);
            GLES20.GlBindTexture(GLES20.GlTexture2d, mTextureName);

            CreateProgram();

            //Add texture to facegeometry.
            Bitmap       textureBitmap = null;
            AssetManager assets        = context.Assets;

            try
            {
                Stream sr = assets.Open("face_geometry.png");
                textureBitmap = BitmapFactory.DecodeStream(sr);
            }
            catch (Exception e)
            {
                Log.Debug(TAG, " Open bitmap error!");
            }


            GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapS, GLES20.GlClampToEdge);
            GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapT, GLES20.GlClampToEdge);
            GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMinFilter, GLES20.GlLinearMipmapLinear);
            GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMagFilter, GLES20.GlLinear);
            GLUtils.TexImage2D(GLES20.GlTexture2d, 0, textureBitmap, 0);
            GLES20.GlGenerateMipmap(GLES20.GlTexture2d);
            GLES20.GlBindTexture(GLES20.GlTexture2d, 0);
            ShaderUtil.CheckGlError(TAG, "Init end.");
        }
Exemplo n.º 7
0
 // Upload the YUV planes from |frame| to |textures|.
 private void texImage2D(VideoRenderer.I420Frame frame, int[] textures)
 {
     for (int i = 0; i < 3; ++i)
     {
         ByteBuffer plane = frame.YuvPlanes[i];
         GLES20.GlActiveTexture(GLES20.GlTexture0 + i);
         GLES20.GlBindTexture(GLES20.GlTexture2d, textures[i]);
         int w = i == 0 ? frame.Width : frame.Width / 2;
         int h = i == 0 ? frame.Height : frame.Height / 2;
         abortUnless(w == frame.YuvStrides[i], frame.YuvStrides[i] + "!=" + w);
         GLES20.GlTexImage2D(GLES20.GlTexture2d, 0, GLES20.GlLuminance, w, h, 0, GLES20.GlLuminance, GLES20.GlUnsignedByte, plane);
     }
     checkNoGLES2Error();
 }
Exemplo n.º 8
0
        // Draw |textures| using |vertices| (X,Y coordinates).
        private void drawRectangle(int[] textures, FloatBuffer vertices)
        {
            for (int i = 0; i < 3; ++i)
            {
                GLES20.GlActiveTexture(GLES20.GlTexture0 + i);
                GLES20.GlBindTexture(GLES20.GlTexture2d, textures[i]);
            }

            GLES20.GlVertexAttribPointer(posLocation, 2, GLES20.GlFloat, false, 0, vertices);
            GLES20.GlEnableVertexAttribArray(posLocation);

            GLES20.GlDrawArrays(GLES20.GlTriangleStrip, 0, 4);
            checkNoGLES2Error();
        }
        /// <summary>
        /// Draw a virtual object at a specific location on a specified plane.
        /// This method is called when WorldRenderManager's OnDrawFrame.
        /// </summary>
        /// <param name="cameraView">The viewMatrix is a 4 * 4 matrix.</param>
        /// <param name="cameraProjection">The ProjectionMatrix is a 4 * 4 matrix.</param>
        /// <param name="lightIntensity">The lighting intensity.</param>
        /// <param name="obj">The virtual object.</param>
        public void OnDrawFrame(float[] cameraView, float[] cameraProjection, float lightIntensity, VirtualObject obj)
        {
            ShaderUtil.CheckGlError(TAG, "onDrawFrame start.");
            mModelMatrixs = obj.GetModelAnchorMatrix();
            Matrix.MultiplyMM(mModelViewMatrixs, 0, cameraView, 0, mModelMatrixs, 0);
            Matrix.MultiplyMM(mModelViewProjectionMatrixs, 0, cameraProjection, 0, mModelViewMatrixs, 0);
            GLES20.GlUseProgram(mGlProgram);
            Matrix.MultiplyMV(mViewLightDirections, 0, mModelViewMatrixs, 0, LIGHT_DIRECTIONS, 0);
            MatrixUtil.NormalizeVec3(mViewLightDirections);

            // Light direction.
            GLES20.GlUniform4f(mLightingParametersUniform,
                               mViewLightDirections[0], mViewLightDirections[1], mViewLightDirections[2], lightIntensity);
            float[] objColors = obj.GetColor();

            GLES20.GlUniform4fv(mColorUniform, 1, objColors, 0);
            GLES20.GlActiveTexture(GLES20.GlTexture0);
            GLES20.GlBindTexture(GLES20.GlTexture2d, mTextures[0]);
            GLES20.GlUniform1i(mTextureUniform, 0);
            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, mVertexBufferId);

            // The coordinate dimension of the read virtual object is 3.
            GLES20.GlVertexAttribPointer(
                mPositionAttribute, 3, GLES20.GlFloat, false, 0, 0);

            // The dimension of the normal vector is 3.
            GLES20.GlVertexAttribPointer(
                mNormalAttribute, 3, GLES20.GlFloat, false, 0, mNormalsBaseAddress);

            // The dimension of the texture coordinate is 2.
            GLES20.GlVertexAttribPointer(
                mTexCoordAttribute, 2, GLES20.GlFloat, false, 0, mTexCoordsBaseAddress);
            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, 0);
            GLES20.GlUniformMatrix4fv(
                mModelViewUniform, 1, false, mModelViewMatrixs, 0);
            GLES20.GlUniformMatrix4fv(
                mModelViewProjectionUniform, 1, false, mModelViewProjectionMatrixs, 0);
            GLES20.GlEnableVertexAttribArray(mPositionAttribute);
            GLES20.GlEnableVertexAttribArray(mNormalAttribute);
            GLES20.GlEnableVertexAttribArray(mTexCoordAttribute);
            GLES20.GlBindBuffer(GLES20.GlElementArrayBuffer, mIndexBufferId);
            GLES20.GlDrawElements(GLES20.GlTriangles, mIndexCount, GLES20.GlUnsignedShort, 0);
            GLES20.GlBindBuffer(GLES20.GlElementArrayBuffer, 0);
            GLES20.GlDisableVertexAttribArray(mPositionAttribute);
            GLES20.GlDisableVertexAttribArray(mNormalAttribute);
            GLES20.GlDisableVertexAttribArray(mTexCoordAttribute);
            GLES20.GlBindTexture(GLES20.GlTexture2d, 0);
            ShaderUtil.CheckGlError(TAG, "onDrawFrame end.");
        }
Exemplo n.º 10
0
            public void OnDrawFrame(Javax.Microedition.Khronos.Opengles.IGL10 glUnused)
            {
                if (_updateSurface)
                {
                    _surfaceTexture.UpdateTexImage();
                    _surfaceTexture.GetTransformMatrix(_STMatrix);
                    _updateSurface = false;
                }

                GLES20.GlUseProgram(0);
                GLES20.GlUseProgram(_glProgram);
                GLES20.GlActiveTexture(GLES20.GlTexture2);
                var tWidth  = _width;
                var tHeight = _height;

                funnyGhostEffectBuffer = ByteBuffer.AllocateDirect(tWidth * tHeight * 4);
                funnyGhostEffectBuffer.Order(ByteOrder.NativeOrder());
                funnyGhostEffectBuffer.Position(0);

                // Note that it is read in GlReadPixels in a different pixel order than top-left to lower-right, so it adds a reversed+mirror effect
                // when passed to TexImage2D to convert to texture.
                GLES20.GlReadPixels(0, 0, tWidth - 1, tHeight - 1, GLES20.GlRgba, GLES20.GlUnsignedByte, funnyGhostEffectBuffer);
                updateTargetTexture(tWidth, tHeight);
                GLES20.GlBindTexture(GLES20.GlTexture2d, _otherTextureId);
                GLES20.GlUniform1i(_otherTextureUniform, 2);

                GLES20.GlUseProgram(0);
                GLES20.GlUseProgram(_glProgram);
                GLES20.GlActiveTexture(GLES20.GlTexture1);
                GLES20.GlBindTexture(GLES11Ext.GlTextureExternalOes, _OESTextureId);
                GLES20.GlUniform1i(_OESTextureUniform, 1);

                _triangleVertices.Position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
                GLES20.GlVertexAttribPointer(_aPositionHandle, 3, GLES20.GlFloat, false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES, _triangleVertices);
                GLES20.GlEnableVertexAttribArray(_aPositionHandle);

                _textureVertices.Position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
                GLES20.GlVertexAttribPointer(_aTextureCoord, 2, GLES20.GlFloat, false, TEXTURE_VERTICES_DATA_STRIDE_BYTES, _textureVertices);
                GLES20.GlEnableVertexAttribArray(_aTextureCoord);

                Android.Opengl.Matrix.SetIdentityM(_MVPMatrix, 0);
                GLES20.GlUniformMatrix4fv(_uMVPMatrixHandle, 1, false, _MVPMatrix, 0);
                GLES20.GlUniformMatrix4fv(_uSTMatrixHandle, 1, false, _STMatrix, 0);

                GLES20.GlDrawArrays(GLES20.GlTriangleStrip, 0, 4);

                GLES20.GlFinish();
            }
Exemplo n.º 11
0
        /// <summary>
        /// Allocates and initializes OpenGL resources needed by the plane renderer.  Must be
        /// called on the OpenGL thread, typically in
        /// <see cref="GLSurfaceView.IRenderer.OnSurfaceCreated(IGL10, Javax.Microedition.Khronos.Egl.EGLConfig)"/>
        /// </summary>
        /// <param name="context">Needed to access shader source and texture PNG.</param>
        /// <param name="gridDistanceTextureName">Name of the PNG file containing the grid texture.</param>
        public void CreateOnGlThread(Context context, String gridDistanceTextureName)
        {
            int vertexShader = ShaderUtil.LoadGLShader(TAG, context,
                                                       GLES20.GlVertexShader, Resource.Raw.plane_vertex);
            int passthroughShader = ShaderUtil.LoadGLShader(TAG, context,
                                                            GLES20.GlFragmentShader, Resource.Raw.plane_fragment);

            mPlaneProgram = GLES20.GlCreateProgram();
            GLES20.GlAttachShader(mPlaneProgram, vertexShader);
            GLES20.GlAttachShader(mPlaneProgram, passthroughShader);
            GLES20.GlLinkProgram(mPlaneProgram);
            GLES20.GlUseProgram(mPlaneProgram);

            ShaderUtil.CheckGLError(TAG, "Program creation");

            // Read the texture.
            var textureBitmap = Android.Graphics.BitmapFactory.DecodeStream(
                context.Assets.Open(gridDistanceTextureName));

            GLES20.GlActiveTexture(GLES20.GlTexture0);
            GLES20.GlGenTextures(mTextures.Length, mTextures, 0);
            GLES20.GlBindTexture(GLES20.GlTexture2d, mTextures[0]);

            GLES20.GlTexParameteri(GLES20.GlTexture2d,
                                   GLES20.GlTextureMinFilter, GLES20.GlLinearMipmapLinear);
            GLES20.GlTexParameteri(GLES20.GlTexture2d,
                                   GLES20.GlTextureMagFilter, GLES20.GlLinear);
            GLUtils.TexImage2D(GLES20.GlTexture2d, 0, textureBitmap, 0);
            GLES20.GlGenerateMipmap(GLES20.GlTexture2d);
            GLES20.GlBindTexture(GLES20.GlTexture2d, 0);

            ShaderUtil.CheckGLError(TAG, "Texture loading");

            mPlaneXZPositionAlphaAttribute = GLES20.GlGetAttribLocation(mPlaneProgram,
                                                                        "a_XZPositionAlpha");

            mPlaneModelUniform = GLES20.GlGetUniformLocation(mPlaneProgram, "u_Model");
            mPlaneModelViewProjectionUniform =
                GLES20.GlGetUniformLocation(mPlaneProgram, "u_ModelViewProjection");
            mTextureUniform       = GLES20.GlGetUniformLocation(mPlaneProgram, "u_Texture");
            mLineColorUniform     = GLES20.GlGetUniformLocation(mPlaneProgram, "u_lineColor");
            mDotColorUniform      = GLES20.GlGetUniformLocation(mPlaneProgram, "u_dotColor");
            mGridControlUniform   = GLES20.GlGetUniformLocation(mPlaneProgram, "u_gridControl");
            mPlaneUvMatrixUniform = GLES20.GlGetUniformLocation(mPlaneProgram, "u_PlaneUvMatrix");

            ShaderUtil.CheckGLError(TAG, "Program parameters");
        }
Exemplo n.º 12
0
        public void DrawCube()
        {
            // This is not the floor!
            GLES20.GlUniform1f(isFloorParam, 0f);

            // Set the Model in the shader, used to calculate lighting
            GLES20.GlUniformMatrix4fv(modelParam, 1, false, modelCube, 0);

            // Set the ModelView in the shader, used to calculate lighting
            GLES20.GlUniformMatrix4fv(modelViewParam, 1, false, modelView, 0);

            // Set the position of the cube
            GLES20.GlVertexAttribPointer(positionParam, CoordsPerVertex, GLES20.GlFloat,
                                         false, 0, cubeVertices);

            // Set the ModelViewProjection matrix in the shader.
            GLES20.GlUniformMatrix4fv(modelViewProjectionParam, 1, false, modelViewProjection, 0);

            // Set the normal positions of the cube, again for shading
            GLES20.GlVertexAttribPointer(normalParam, 3, GLES20.GlFloat,
                                         false, 0, cubeNormals);

            // Set the texture coordinates
            GLES20.GlVertexAttribPointer(texCoordParam, 2, GLES20.GlFloat, false, 0, cubeTextureCoords);

            GLES20.GlActiveTexture(GLES20.GlTexture0);

            if (IsLookingAtObject)
            {
                GLES20.GlVertexAttribPointer(colorParam, 4, GLES20.GlFloat, false,
                                             0, cubeFoundColors);
                GLES20.GlBindTexture(GLES20.GlTexture2d, monkeyFound);
            }
            else
            {
                GLES20.GlVertexAttribPointer(colorParam, 4, GLES20.GlFloat, false,
                                             0, cubeColors);
                GLES20.GlBindTexture(GLES20.GlTexture2d, monkeyNotFound);
            }

            GLES20.GlUniform1i(texture, 0);

            GLES20.GlDrawArrays(GLES20.GlTriangles, 0, 36);
            CheckGlError("Drawing cube");
        }
        /// <summary>
        /// Create a shader program to read the data of the virtual object.
        /// This method is called when WorldRenderManager's OnSurfaceCreated.
        /// </summary>
        /// <param name="context">Context.</param>
        public void Init(Context context)
        {
            ShaderUtil.CheckGlError(TAG, "Init start.");
            CreateProgram();

            // Coordinate and index.
            int[] buffers = new int[2];
            GLES20.GlGenBuffers(2, buffers, 0);
            mVertexBufferId = buffers[0];
            mIndexBufferId  = buffers[1];
            GLES20.GlActiveTexture(GLES20.GlTexture0);
            GLES20.GlGenTextures(mTextures.Length, mTextures, 0);
            GLES20.GlBindTexture(GLES20.GlTexture2d, mTextures[0]);
            GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMinFilter, GLES20.GlLinearMipmapLinear);
            InitGlTextureData(context);
            InitializeGlObjectData(context);
            ShaderUtil.CheckGlError(TAG, "Init end.");
        }
        private void SetupImage()
        {
            // Create our UV coordinates.
            uvs = new float[] {
                0.0f, 0.0f,
                0.0f, 1.0f,
                1.0f, 1.0f,
                1.0f, 0.0f
            };

            // The texture buffer
            ByteBuffer byteBuffer = ByteBuffer.AllocateDirect(uvs.Length * 4);

            byteBuffer.Order(ByteOrder.NativeOrder());
            uvBuffer = byteBuffer.AsFloatBuffer();
            uvBuffer.Put(uvs);
            uvBuffer.Position(0);

            // Generate Textures, if more needed, alter these numbers.
            int[] textureIds = new int[1];
            GLES20.GlGenTextures(1, textureIds, 0);

            // Retrieve our image from resources.
            Bitmap bitmap = BitmapFactory.DecodeResource(context.Resources, Resource.Drawable.plant07);

            // Bind texture to texturename
            GLES20.GlActiveTexture(GLES20.GlTexture0);
            GLES20.GlBindTexture(GLES20.GlTexture2d, textureIds[0]);

            // Set filtering
            GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMinFilter, GLES20.GlLinear);
            GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMagFilter, GLES20.GlLinear);

            // Set wrapping mode
            GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapS, GLES20.GlClampToEdge);
            GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapT, GLES20.GlClampToEdge);

            // Load the bitmap into the bound texture.
            GLUtils.TexImage2D(GLES20.GlTexture2d, 0, bitmap, 0);

            // We are done using the bitmap so we should recycle it.
            bitmap.Recycle();
            bitmap.Dispose();
        }
        private void DrawSortedPlans(List <ARPlane> sortedPlanes, float[] cameraViews, float[] cameraProjection)
        {
            ShaderUtil.CheckGlError(TAG, "Draw sorted plans start.");

            GLES20.GlDepthMask(false);
            GLES20.GlEnable(GLES20.GlBlend);
            GLES20.GlBlendFuncSeparate(
                GLES20.GlDstAlpha, GLES20.GlOne, GLES20.GlZero, GLES20.GlOneMinusSrcAlpha);
            GLES20.GlUseProgram(mProgram);
            GLES20.GlEnableVertexAttribArray(glPositionParameter);

            foreach (ARPlane plane in sortedPlanes)
            {
                float[] planeMatrix = new float[MATRIX_SIZE];
                plane.CenterPose.ToMatrix(planeMatrix, 0);

                Array.Copy(planeMatrix, modelMatrix, MATRIX_SIZE);
                float scaleU = 1.0f / LABEL_WIDTH;

                // Set the value of the plane angle uv matrix.
                planeAngleUvMatrix[0] = scaleU;
                planeAngleUvMatrix[1] = 0.0f;
                planeAngleUvMatrix[2] = 0.0f;
                float scaleV = 1.0f / LABEL_HEIGHT;
                planeAngleUvMatrix[3] = scaleV;

                int idx = plane.Label.Ordinal();
                Log.Debug(TAG, "Plane getLabel:" + idx);
                idx = Java.Lang.Math.Abs(idx);
                GLES20.GlActiveTexture(GLES20.GlTexture0 + idx);
                GLES20.GlBindTexture(GLES20.GlTexture2d, textures[idx]);
                GLES20.GlUniform1i(glTexture, idx);
                GLES20.GlUniformMatrix2fv(glPlaneUvMatrix, 1, false, planeAngleUvMatrix, 0);

                DrawLabel(cameraViews, cameraProjection);
            }

            GLES20.GlDisableVertexAttribArray(glPositionParameter);
            GLES20.GlBindTexture(GLES20.GlTexture2d, 0);
            GLES20.GlDisable(GLES20.GlBlend);
            GLES20.GlDepthMask(true);
            ShaderUtil.CheckGlError(TAG, "Draw sorted plans end.");
        }
Exemplo n.º 16
0
            /**
             * Draws the external texture in SurfaceTexture onto the current EGL surface.
             */
            public void drawFrame(SurfaceTexture st, bool invert)
            {
                checkGlError("onDrawFrame start");
                st.GetTransformMatrix(mSTMatrix);
                if (invert)
                {
                    mSTMatrix[5]  = -mSTMatrix[5];
                    mSTMatrix[13] = 1.0f - mSTMatrix[13];
                }

                // (optional) clear to green so we can see if we're failing to set pixels
                GLES20.GlClearColor(0.0f, 1.0f, 0.0f, 1.0f);
                GLES20.GlClear(GLES20.GlColorBufferBit);

                GLES20.GlUseProgram(mProgram);
                checkGlError("glUseProgram");

                GLES20.GlActiveTexture(GLES20.GlTexture0);
                GLES20.GlBindTexture(GLES11Ext.GlTextureExternalOes, mTextureID);

                mTriangleVertices.Position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
                GLES20.GlVertexAttribPointer(maPositionHandle, 3, GLES20.GlFloat, false,
                                             TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices);
                checkGlError("glVertexAttribPointer maPosition");
                GLES20.GlEnableVertexAttribArray(maPositionHandle);
                checkGlError("glEnableVertexAttribArray maPositionHandle");

                mTriangleVertices.Position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
                GLES20.GlVertexAttribPointer(maTextureHandle, 2, GLES20.GlFloat, false,
                                             TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices);
                checkGlError("glVertexAttribPointer maTextureHandle");
                GLES20.GlEnableVertexAttribArray(maTextureHandle);
                checkGlError("glEnableVertexAttribArray maTextureHandle");

                Android.Opengl.Matrix.SetIdentityM(mMVPMatrix, 0);
                GLES20.GlUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
                GLES20.GlUniformMatrix4fv(muSTMatrixHandle, 1, false, mSTMatrix, 0);

                GLES20.GlDrawArrays(GLES20.GlTriangleStrip, 0, 4);
                checkGlError("glDrawArrays");

                GLES20.GlBindTexture(GLES11Ext.GlTextureExternalOes, 0);
            }
        public void RenderTexture(int texId)
        {
            try
            {
                // Bind default FBO
                GLES20.GlBindFramebuffer(GLES20.GlFramebuffer, 0);

                // Use our shader program
                GLES20.GlUseProgram(MProgram);
                GlToolbox.CheckGlError("glUseProgram");

                // Set viewport
                GLES20.GlViewport(0, 0, MViewWidth, MViewHeight);
                GlToolbox.CheckGlError("glViewport");

                // Disable blending
                GLES20.GlDisable(GLES20.GlBlend);

                // Set the vertex attributes
                GLES20.GlVertexAttribPointer(MTexCoordHandle, 2, GLES20.GlFloat, false, 0, MTexVertices);
                GLES20.GlEnableVertexAttribArray(MTexCoordHandle);
                GLES20.GlVertexAttribPointer(MPosCoordHandle, 2, GLES20.GlFloat, false, 0, MPosVertices);
                GLES20.GlEnableVertexAttribArray(MPosCoordHandle);
                GlToolbox.CheckGlError("vertex attribute setup");

                // Set the input texture
                GLES20.GlActiveTexture(GLES20.GlTexture0);
                GlToolbox.CheckGlError("glActiveTexture");
                GLES20.GlBindTexture(GLES20.GlTexture2d, texId);
                GlToolbox.CheckGlError("glBindTexture");
                GLES20.GlUniform1i(MTexSamplerHandle, 0);

                // Draw
                GLES20.GlClearColor(0.0f, 0.0f, 0.0f, 1.0f);
                GLES20.GlClear(GLES20.GlColorBufferBit);
                GLES20.GlDrawArrays(GLES20.GlTriangleStrip, 0, 4);
            }
            catch (Exception e)
            {
                Methods.DisplayReportResultTrack(e);
            }
        }
Exemplo n.º 18
0
 /// <summary>
 /// Inform this View of the dimensions of frames coming from |stream|. </summary>
 public virtual void setSize(Endpoint stream, int width, int height)
 {
     // Generate 3 texture ids for Y/U/V and place them into |textures|,
     // allocating enough storage for |width|x|height| pixels.
     int[] textures = yuvTextures[stream == Endpoint.LOCAL ? 0 : 1];
     GLES20.GlGenTextures(3, textures, 0);
     for (int i = 0; i < 3; ++i)
     {
         int w = i == 0 ? width : width / 2;
         int h = i == 0 ? height : height / 2;
         GLES20.GlActiveTexture(GLES20.GlTexture0 + i);
         GLES20.GlBindTexture(GLES20.GlTexture2d, textures[i]);
         GLES20.GlTexImage2D(GLES20.GlTexture2d, 0, GLES20.GlLuminance, w, h, 0, GLES20.GlLuminance, GLES20.GlUnsignedByte, null);
         GLES20.GlTexParameterf(GLES20.GlTexture2d, GLES20.GlTextureMinFilter, GLES20.GlLinear);
         GLES20.GlTexParameterf(GLES20.GlTexture2d, GLES20.GlTextureMagFilter, GLES20.GlLinear);
         GLES20.GlTexParameterf(GLES20.GlTexture2d, GLES20.GlTextureWrapS, GLES20.GlClampToEdge);
         GLES20.GlTexParameterf(GLES20.GlTexture2d, GLES20.GlTextureWrapT, GLES20.GlClampToEdge);
     }
     checkNoGLES2Error();
 }
Exemplo n.º 19
0
        private void renderDistortionMesh(DistortionMesh mesh)
        {
            GLES20.GlBindBuffer(34962, mesh.mArrayBufferId);
            /*mesh.getClass(); mesh.getClass();*/ GLES20.GlVertexAttribPointer(mProgramHolder.aPosition, 3, 5126, false, 20, 0 * 4);

            GLES20.GlEnableVertexAttribArray(mProgramHolder.aPosition);

            /*mesh.getClass(); mesh.getClass();*/ GLES20.GlVertexAttribPointer(mProgramHolder.aVignette, 1, 5126, false, 20, 2 * 4);

            GLES20.GlEnableVertexAttribArray(mProgramHolder.aVignette);

            /*mesh.getClass(); mesh.getClass();*/ GLES20.GlVertexAttribPointer(mProgramHolder.aTextureCoord, 2, 5126, false, 20, 3 * 4);

            GLES20.GlEnableVertexAttribArray(mProgramHolder.aTextureCoord);

            GLES20.GlActiveTexture(33984);
            GLES20.GlBindTexture(3553, mTextureId);
            GLES20.GlUniform1i(mProgramHolder.uTextureSampler, 0);
            GLES20.GlUniform1f(mProgramHolder.uTextureCoordScale, mResolutionScale);

            GLES20.GlBindBuffer(34963, mesh.mElementBufferId);
            GLES20.GlDrawElements(5, mesh.nIndices, 5125, 0);
        }
Exemplo n.º 20
0
	public void drawFrame() {	
		checkGlError("onDrawFrame start");
		mSurfaceTexture.GetTransformMatrix(mSTMatrix);

		//GLES20.glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
		//GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);

		GLES20.GlUseProgram(mProgram);
		checkGlError("glUseProgram");

		GLES20.GlActiveTexture(GLES20.GlTexture0);
		GLES20.GlBindTexture(GLES11Ext.GlTextureExternalOes, 0);
		GLES20.GlBindTexture(GLES11Ext.GlTextureExternalOes, mTextureID);

		mTriangleVertices.Position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
		GLES20.GlVertexAttribPointer(maPositionHandle, 3, GLES20.GlFloat, false,
				TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices);
		checkGlError("glVertexAttribPointer maPosition");
		GLES20.GlEnableVertexAttribArray(maPositionHandle);
		checkGlError("glEnableVertexAttribArray maPositionHandle");

		mTriangleVertices.Position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
		GLES20.GlVertexAttribPointer(maTextureHandle, 2, GLES20.GlFloat, false,
				TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices);
		checkGlError("glVertexAttribPointer maTextureHandle");
		GLES20.GlEnableVertexAttribArray(maTextureHandle);
		checkGlError("glEnableVertexAttribArray maTextureHandle");

		Android.Opengl.Matrix.SetIdentityM(mMVPMatrix, 0);
		GLES20.GlUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
		GLES20.GlUniformMatrix4fv(muSTMatrixHandle, 1, false, mSTMatrix, 0);

		GLES20.GlDrawArrays(GLES20.GlTriangleStrip, 0, 4);
		checkGlError("glDrawArrays");
		GLES20.GlFinish();
	}
Exemplo n.º 21
0
        /**
         * Draws the collection of tracked planes, with closer planes hiding more distant ones.
         *
         * @param allPlanes The collection of planes to draw.
         * @param cameraPose The pose of the camera, as returned by {@link Frame#getPose()}
         * @param cameraPerspective The projection matrix, as returned by
         *     {@link Session#getProjectionMatrix(float[], int, float, float)}
         */
        public void DrawPlanes(IEnumerable <Plane> allPlanes, Pose cameraPose, float[] cameraPerspective)
        {
            // Planes must be sorted by distance from camera so that we draw closer planes first, and
            // they occlude the farther planes.
            List <SortablePlane> sortedPlanes = new List <SortablePlane>();

            float[] normal  = new float[3];
            float   cameraX = cameraPose.Tx();
            float   cameraY = cameraPose.Ty();
            float   cameraZ = cameraPose.Tz();

            foreach (var plane in allPlanes)
            {
                if (plane.GetType() != Plane.Type.HorizontalUpwardFacing ||
                    plane.GetTrackingState() != Plane.TrackingState.Tracking)
                {
                    continue;
                }

                var center = plane.CenterPose;
                // Get transformed Y axis of plane's coordinate system.
                center.GetTransformedAxis(1, 1.0f, normal, 0);
                // Compute dot product of plane's normal with vector from camera to plane center.
                float distance = (cameraX - center.Tx()) * normal[0] +
                                 (cameraY - center.Ty()) * normal[1] + (cameraZ - center.Tz()) * normal[2];
                if (distance < 0)
                {  // Plane is back-facing.
                    continue;
                }
                sortedPlanes.Add(new SortablePlane(distance, plane));
            }

            sortedPlanes.Sort((x, y) => x.Distance.CompareTo(y.Distance));


            var cameraView = new float[16];

            cameraPose.Inverse().ToMatrix(cameraView, 0);

            // Planes are drawn with additive blending, masked by the alpha channel for occlusion.

            // Start by clearing the alpha channel of the color buffer to 1.0.
            GLES20.GlClearColor(1, 1, 1, 1);
            GLES20.GlColorMask(false, false, false, true);
            GLES20.GlClear(GLES20.GlColorBufferBit);
            GLES20.GlColorMask(true, true, true, true);

            // Disable depth write.
            GLES20.GlDepthMask(false);

            // Additive blending, masked by alpha chanel, clearing alpha channel.
            GLES20.GlEnable(GLES20.GlBlend);
            GLES20.GlBlendFuncSeparate(
                GLES20.GlDstAlpha, GLES20.GlOne,            // RGB (src, dest)
                GLES20.GlZero, GLES20.GlOneMinusSrcAlpha);  // ALPHA (src, dest)

            // Set up the shader.
            GLES20.GlUseProgram(mPlaneProgram);

            // Attach the texture.
            GLES20.GlActiveTexture(GLES20.GlTexture0);
            GLES20.GlBindTexture(GLES20.GlTexture2d, mTextures[0]);
            GLES20.GlUniform1i(mTextureUniform, 0);

            // Shared fragment uniforms.
            GLES20.GlUniform4fv(mGridControlUniform, 1, GRID_CONTROL, 0);

            // Enable vertex arrays
            GLES20.GlEnableVertexAttribArray(mPlaneXZPositionAlphaAttribute);

            ShaderUtil.CheckGLError(TAG, "Setting up to draw planes");

            foreach (var sortedPlane in sortedPlanes)
            {
                var     plane       = sortedPlane.Plane;
                float[] planeMatrix = new float[16];
                plane.CenterPose.ToMatrix(planeMatrix, 0);


                updatePlaneParameters(planeMatrix, plane.ExtentX,
                                      plane.ExtentZ, plane.PlanePolygon);

                // Get plane index. Keep a map to assign same indices to same planes.

                int planeIndex = -1;
                if (!mPlaneIndexMap.TryGetValue(plane, out planeIndex))
                {
                    planeIndex = Java.Lang.Integer.ValueOf(mPlaneIndexMap.Count).IntValue();
                    mPlaneIndexMap.Add(plane, planeIndex);
                }

                // Set plane color. Computed deterministically from the Plane index.
                int colorIndex = planeIndex % PLANE_COLORS_RGBA.Length;

                colorRgbaToFloat(mPlaneColor, PLANE_COLORS_RGBA[colorIndex]);
                GLES20.GlUniform4fv(mLineColorUniform, 1, mPlaneColor, 0);
                GLES20.GlUniform4fv(mDotColorUniform, 1, mPlaneColor, 0);

                // Each plane will have its own angle offset from others, to make them easier to
                // distinguish. Compute a 2x2 rotation matrix from the angle.
                float angleRadians = planeIndex * 0.144f;
                float uScale       = DOTS_PER_METER;
                float vScale       = DOTS_PER_METER * EQUILATERAL_TRIANGLE_SCALE;
                mPlaneAngleUvMatrix[0] = +(float)Math.Cos(angleRadians) * uScale;
                mPlaneAngleUvMatrix[1] = -(float)Math.Sin(angleRadians) * uScale;
                mPlaneAngleUvMatrix[2] = +(float)Math.Sin(angleRadians) * vScale;
                mPlaneAngleUvMatrix[3] = +(float)Math.Cos(angleRadians) * vScale;
                GLES20.GlUniformMatrix2fv(mPlaneUvMatrixUniform, 1, false, mPlaneAngleUvMatrix, 0);


                Draw(cameraView, cameraPerspective);
            }

            // Clean up the state we set
            GLES20.GlDisableVertexAttribArray(mPlaneXZPositionAlphaAttribute);
            GLES20.GlBindTexture(GLES20.GlTexture2d, 0);
            GLES20.GlDisable(GLES20.GlBlend);
            GLES20.GlDepthMask(true);

            ShaderUtil.CheckGLError(TAG, "Cleaning up after drawing planes");
        }
Exemplo n.º 22
0
 public static void Activate(int index)
 {
     GLES20.GlActiveTexture(GLES20.GlTexture0 + index);
 }
Exemplo n.º 23
0
        /// <summary>
        /// This method is called from Renderer when OpenGL ready to draw scene
        /// The code of this method show "standard" OpenGL object drawing routine with using transformation matrix
        /// </summary>
        /// <param name="gl">IGL10 access object pointer from orign OpenGL.OnDraw(IGL10 gl)</param>
        /// <param name="mViewMatrix">Camere View matrix</param>
        /// <param name="mProjectionMatrix">Camera Projection matrix</param>
        public virtual void DrawFrame()
        {
            float[] mModelMatrix = new float[16];
            Matrix.SetIdentityM(mModelMatrix, 0);
            Matrix.ScaleM(mModelMatrix, 0, scale, scale, scale);
            Matrix.RotateM(mModelMatrix, 0, angleX, 1, ay, az);
            Matrix.RotateM(mModelMatrix, 0, angleY, ax, 1, az);
            Matrix.TranslateM(mModelMatrix, 0, x, y, z);

            // Tell OpenGL to use this program when rendering.

            GLES20.GlUseProgram(shader.programHandle);

            //Draw with VBO
            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, vertexVBO.handle);
            GLES20.GlEnableVertexAttribArray(shader.mPositionHandle);
            GLES20.GlVertexAttribPointer(shader.mPositionHandle, mPositionDataSize, GLES20.GlFloat, false, 0, 0);

            if (textureVBO.handle != -1)
            {
                GLES20.GlBindBuffer(GLES20.GlArrayBuffer, textureVBO.handle);
                GLES20.GlEnableVertexAttribArray(shader.mTextureCoordHandle);
                GLES20.GlVertexAttribPointer(shader.mTextureCoordHandle, 2, GLES20.GlFloat, false, 0, 0);
            }

            if (normalVBO.handle != -1)
            {
                GLES20.GlBindBuffer(GLES20.GlArrayBuffer, normalVBO.handle);
                GLES20.GlEnableVertexAttribArray(shader.mNormalHandle);
                GLES20.GlVertexAttribPointer(shader.mNormalHandle, 3, GLES20.GlFloat, false, 0, 0);
            }

            if (texture.handle != -1)
            {
                GLES20.GlActiveTexture(GLES20.GlTexture0);
                GLES20.GlBindTexture(GLES20.GlTexture2d, texture.handle);
                GLES20.GlUniform1i(shader.mTextureHandle, 0);
            }

            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, 0);
            //END OF Draw with VBO

            //light position
            GLES20.GlUniform4f(shader.mLightPos, lx, ly, lz, lw);

            // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix
            // (which currently contains model * view).
            // Allocate storage for the final combined matrix. This will be passed into the shader program.

            float[] mMVPMatrix = new float[16];
            Matrix.MultiplyMM(mMVPMatrix, 0, renderer.camera.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).
            // THIS IS NOT WORK AT C# Matrix class -> Matrix.MultiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
            float[] _mMVPMatrix = new float[16];
            Matrix.MultiplyMM(_mMVPMatrix, 0, renderer.camera.mProjectionMatrix, 0, mMVPMatrix, 0);

            GLES20.GlUniformMatrix4fv(shader.mMVPMatrixHandle, 1, false, _mMVPMatrix, 0);
            GLES20.GlDrawArrays(GLES20.GlTriangles, 0, vertexVBO.objectSize); //Cube has 12 triagle faces each face has 3 coord


            GLES20.GlUseProgram(0);
        }
Exemplo n.º 24
0
        /**
         * \brief Draw the video icon (in OpenGL).
         * @param mvpMatrix the model-view-projection matrix.
         * @param status the video state.
         */
        private void DrawIcon(float[] mvpMatrix, PikkartVideoPlayer.VideoSate.VIDEO_STATE status)
        {
            GLES20.GlEnable(GLES20.GlBlend);
            GLES20.GlBlendFunc(GLES20.GlSrcAlpha, GLES20.GlOneMinusSrcAlpha);

            GLES20.GlUseProgram(mKeyframe_Program_GL_ID);

            int vertexHandle       = GLES20.GlGetAttribLocation(mKeyframe_Program_GL_ID, "vertexPosition");
            int textureCoordHandle = GLES20.GlGetAttribLocation(mKeyframe_Program_GL_ID, "vertexTexCoord");
            int mvpMatrixHandle    = GLES20.GlGetUniformLocation(mKeyframe_Program_GL_ID, "modelViewProjectionMatrix");
            int texSampler2DHandle = GLES20.GlGetUniformLocation(mKeyframe_Program_GL_ID, "texSampler2D");

            GLES20.GlVertexAttribPointer(vertexHandle, 3, GLES20.GlFloat, false, 0, mVertices_Buffer);
            GLES20.GlVertexAttribPointer(textureCoordHandle, 2, GLES20.GlFloat, false, 0, mTexCoords_Buffer);

            GLES20.GlEnableVertexAttribArray(vertexHandle);
            GLES20.GlEnableVertexAttribArray(textureCoordHandle);

            GLES20.GlActiveTexture(GLES20.GlTexture0);
            switch ((int)status)
            {
            case 0:    //end
                GLES20.GlBindTexture(GLES20.GlTexture2d, mIconPlayTexture_GL_ID);
                break;

            case 1:    //pasued
                GLES20.GlBindTexture(GLES20.GlTexture2d, mIconPlayTexture_GL_ID);
                break;

            case 2:    //stopped
                GLES20.GlBindTexture(GLES20.GlTexture2d, mIconPlayTexture_GL_ID);
                break;

            case 3:    //playing
                GLES20.GlBindTexture(GLES20.GlTexture2d, mIconPlayTexture_GL_ID);
                break;

            case 4:    //ready
                GLES20.GlBindTexture(GLES20.GlTexture2d, mIconPlayTexture_GL_ID);
                break;

            case 5:    //not ready
                GLES20.GlBindTexture(GLES20.GlTexture2d, mIconBusyTexture_GL_ID);
                break;

            case 6:    //buffering
                GLES20.GlBindTexture(GLES20.GlTexture2d, mIconBusyTexture_GL_ID);
                break;

            case 7:    //error
                GLES20.GlBindTexture(GLES20.GlTexture2d, mIconErrorTexture_GL_ID);
                break;

            default:
                GLES20.GlBindTexture(GLES20.GlTexture2d, mIconBusyTexture_GL_ID);
                break;
            }
            GLES20.GlUniform1i(texSampler2DHandle, 0);

            GLES20.GlUniformMatrix4fv(mvpMatrixHandle, 1, false, mvpMatrix, 0);


            GLES20.GlDrawElements(GLES20.GlTriangles, mIndices_Number, GLES20.GlUnsignedShort, mIndex_Buffer);

            GLES20.GlDisableVertexAttribArray(vertexHandle);
            GLES20.GlDisableVertexAttribArray(textureCoordHandle);

            GLES20.GlUseProgram(0);
            GLES20.GlDisable(GLES20.GlBlend);
        }
Exemplo n.º 25
0
        private void drawOneMaterial(GLSL glsl, ShellSurface surface, RenderList mat)
        {
            // set motion
            if (surface.Animation)
            {
                if (mat.bone_inv_map == null)
                {
                    for (int j = 0; j < surface.RenderBones.Count; j++)
                    {
                        var b = surface.RenderBones[j];
                        if (b != null)
                        {
                            Array.Copy(b.matrix, 0, mBoneMatrix, j * 16, 16);
                        }
                    }
                }
                else
                {
                    for (int j = 0; j < mat.bone_inv_map.Length; j++)
                    {
                        int inv = mat.bone_inv_map[j];
                        if (inv >= 0)
                        {
                            var b = surface.RenderBones[inv];
                            Array.Copy(b.matrix, 0, mBoneMatrix, j * 16, 16);
                        }
                    }
                }
                GLES20.GlUniformMatrix4fv(glsl.muMBone, mat.bone_num, false, mBoneMatrix, 0);

                GLES20.GlEnableVertexAttribArray(glsl.maBlendHandle);
                GLES20.GlVertexAttribPointer(glsl.maBlendHandle, 3, GLES20.GlUnsignedByte, false, 0, mat.weight);
            }

            // initialize color
            for (int i = 0; i < mDifAmb.Count(); i++)
            {
                mDifAmb[i] = 1.0f;
            }

            // diffusion and ambient
            float wi = 0.6f;                    // light color = (0.6, 0.6, 0.6)

            for (int i = 0; i < 3; i++)
            {
                mDifAmb[i] *= mat.material.diffuse_color[i] * wi + mat.material.emmisive_color[i];
            }
            mDifAmb[3] *= mat.material.diffuse_color[3];
            Vector.min(mDifAmb, 1.0f);
            GLES20.GlUniform4fv(glsl.muDif, 1, mDifAmb, 0);

            // speculation
            if (glsl.muPow >= 0)
            {
                GLES20.GlUniform4f(glsl.muSpec, mat.material.specular_color[0], mat.material.specular_color[1], mat.material.specular_color[2], 0);
                GLES20.GlUniform1f(glsl.muPow, mat.material.power);
            }

            // toon
            GLES20.GlUniform1i(glsl.msToonSampler, 0);
            GLES20.GlActiveTexture(GLES20.GlTexture0);
            GLES20.GlBindTexture(GLES20.GlTexture2d, TextureFile.FetchTexInfo(surface.toon_name[mat.material.toon_index]).tex);

            // texture
            GLES20.GlUniform1i(glsl.msTextureSampler, 1);
            GLES20.GlActiveTexture(GLES20.GlTexture1);
            if (mat.material.texture != null)
            {
                TexInfo tb = TextureFile.FetchTexInfo(mat.material.texture);
                if (tb != null)
                {
                    GLES20.GlBindTexture(GLES20.GlTexture2d, tb.tex);
                }
                else                                                                                              // avoid crash
                {
                    GLES20.GlBindTexture(GLES20.GlTexture2d, TextureFile.FetchTexInfo(surface.toon_name[0]).tex); // white texture using toon0.bmp
                    for (int i = 0; i < 3; i++)                                                                   // for emulate premultiplied alpha
                    {
                        mDifAmb[i] *= mat.material.diffuse_color[3];
                    }
                }
            }
            else
            {
                GLES20.GlBindTexture(GLES20.GlTexture2d, TextureFile.FetchTexInfo(surface.toon_name[0]).tex); // white texture using toon0.bmp
                for (int i = 0; i < 3; i++)                                                                   // for emulate premultiplied alpha
                {
                    mDifAmb[i] *= mat.material.diffuse_color[3];
                }
            }

            // sphere(sph)
            GLES20.GlUniform1i(glsl.msSphSampler, 2);
            GLES20.GlActiveTexture(GLES20.GlTexture2);
            if (mat.material.sph != null)
            {
                TexInfo tb = TextureFile.FetchTexInfo(mat.material.sph);
                if (tb != null)
                {
                    GLES20.GlBindTexture(GLES20.GlTexture2d, tb.tex);
                }
                else                                                                                               // avoid crash
                {
                    GLES20.GlBindTexture(GLES20.GlTexture2d, TextureFile.FetchTexInfo(surface.toon_name [0]).tex); // white texture using toon0.bmp
                }
            }
            else
            {
                GLES20.GlBindTexture(GLES20.GlTexture2d, TextureFile.FetchTexInfo(surface.toon_name[0]).tex);                   // white texture using toon0.bmp
            }

            // sphere(spa)
            GLES20.GlUniform1i(glsl.msSpaSampler, 3);
            GLES20.GlActiveTexture(GLES20.GlTexture3);
            if (mat.material.spa != null)
            {
                TexInfo tb = TextureFile.FetchTexInfo(mat.material.spa);
                if (tb != null)
                {
                    GLES20.GlBindTexture(GLES20.GlTexture2d, tb.tex);
                }
            }

            // draw
            surface.IndexBuffer.Position(mat.face_vert_offset);
            GLES20.GlDrawElements(GLES20.GlTriangles, mat.face_vert_count, GLES20.GlUnsignedShort, surface.IndexBuffer);
            checkGlError("glDrawElements");
        }
Exemplo n.º 26
0
        /**
         * Draws the model.
         *
         * @param cameraView  A 4x4 view matrix, in column-major order.
         * @param cameraPerspective  A 4x4 projection matrix, in column-major order.
         * @param lightIntensity  Illumination intensity.  Combined with diffuse and specular material
         *     properties.
         * @see #setBlendMode(BlendMode)
         * @see #updateModelMatrix(float[], float)
         * @see #setMaterialProperties(float, float, float, float)
         * @see android.opengl.Matrix
         */
        public void Draw(float[] cameraView, float[] cameraPerspective, float lightIntensity)
        {
            ShaderUtil.CheckGLError(TAG, "Before draw");

            // Build the ModelView and ModelViewProjection matrices
            // for calculating object position and light.
            Android.Opengl.Matrix.MultiplyMM(mModelViewMatrix, 0, cameraView, 0, mModelMatrix, 0);
            Android.Opengl.Matrix.MultiplyMM(mModelViewProjectionMatrix, 0, cameraPerspective, 0, mModelViewMatrix, 0);

            GLES20.GlUseProgram(mProgram);

            // Set the lighting environment properties.
            Android.Opengl.Matrix.MultiplyMV(mViewLightDirection, 0, mModelViewMatrix, 0, LIGHT_DIRECTION, 0);
            normalizeVec3(mViewLightDirection);
            GLES20.GlUniform4f(mLightingParametersUniform,
                               mViewLightDirection[0], mViewLightDirection[1], mViewLightDirection[2], lightIntensity);

            // Set the object material properties.
            GLES20.GlUniform4f(mMaterialParametersUniform, mAmbient, mDiffuse, mSpecular,
                               mSpecularPower);

            // Attach the object texture.
            GLES20.GlActiveTexture(GLES20.GlTexture0);
            GLES20.GlBindTexture(GLES20.GlTexture2d, mTextures[0]);
            GLES20.GlUniform1i(mTextureUniform, 0);

            // Set the vertex attributes.
            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, mVertexBufferId);

            GLES20.GlVertexAttribPointer(
                mPositionAttribute, COORDS_PER_VERTEX, GLES20.GlFloat, false, 0, mVerticesBaseAddress);
            GLES20.GlVertexAttribPointer(
                mNormalAttribute, 3, GLES20.GlFloat, false, 0, mNormalsBaseAddress);
            GLES20.GlVertexAttribPointer(
                mTexCoordAttribute, 2, GLES20.GlFloat, false, 0, mTexCoordsBaseAddress);

            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, 0);

            // Set the ModelViewProjection matrix in the shader.
            GLES20.GlUniformMatrix4fv(
                mModelViewUniform, 1, false, mModelViewMatrix, 0);
            GLES20.GlUniformMatrix4fv(
                mModelViewProjectionUniform, 1, false, mModelViewProjectionMatrix, 0);

            // Enable vertex arrays
            GLES20.GlEnableVertexAttribArray(mPositionAttribute);
            GLES20.GlEnableVertexAttribArray(mNormalAttribute);
            GLES20.GlEnableVertexAttribArray(mTexCoordAttribute);

            if (mBlendMode != BlendMode.Null)
            {
                GLES20.GlDepthMask(false);
                GLES20.GlEnable(GLES20.GlBlend);
                switch (mBlendMode)
                {
                case BlendMode.Shadow:
                    // Multiplicative blending function for Shadow.
                    GLES20.GlBlendFunc(GLES20.GlZero, GLES20.GlOneMinusSrcAlpha);
                    break;

                case BlendMode.Grid:
                    // Grid, additive blending function.
                    GLES20.GlBlendFunc(GLES20.GlSrcAlpha, GLES20.GlOneMinusSrcAlpha);
                    break;
                }
            }

            GLES20.GlBindBuffer(GLES20.GlElementArrayBuffer, mIndexBufferId);
            GLES20.GlDrawElements(GLES20.GlTriangles, mIndexCount, GLES20.GlUnsignedShort, 0);
            GLES20.GlBindBuffer(GLES20.GlElementArrayBuffer, 0);

            if (mBlendMode != BlendMode.Null)
            {
                GLES20.GlDisable(GLES20.GlBlend);
                GLES20.GlDepthMask(true);
            }

            // Disable vertex arrays
            GLES20.GlDisableVertexAttribArray(mPositionAttribute);
            GLES20.GlDisableVertexAttribArray(mNormalAttribute);
            GLES20.GlDisableVertexAttribArray(mTexCoordAttribute);

            GLES20.GlBindTexture(GLES20.GlTexture2d, 0);

            ShaderUtil.CheckGLError(TAG, "After draw");
        }
        public void OnDrawFrame(IGL10 unused)
        {
            // Clear view.
            GLES20.GlClearColor(0.5f, 0.5f, 0.5f, 1.0f);
            GLES20.GlClear(GLES20.GlColorBufferBit);

            // If we have new preview texture.
            if (_surfaceTextureUpdate)
            {
                // Update surface texture.
                _surfaceTexture.UpdateTexImage();
                // Update texture transform matrix.
                _surfaceTexture.GetTransformMatrix(_transformM);
                _surfaceTextureUpdate = false;

                // Bind offscreen texture into use.
                _fboOffscreen.Bind();
                _fboOffscreen.BindTexture(0);

                // Take copy shader into use.
                _shaderCopyOes.UseProgram();

                // Uniform variables.
                var uOrientationM = _shaderCopyOes.GetHandle("uOrientationM");
                var uTransformM   = _shaderCopyOes.GetHandle("uTransformM");

                // We're about to transform external texture here already.
                GLES20.GlUniformMatrix4fv(uOrientationM, 1, false,
                                          _sharedData._orientationM, 0);
                GLES20.GlUniformMatrix4fv(uTransformM, 1, false, _transformM, 0);

                // We're using external OES texture as source.
                GLES20.GlActiveTexture(GLES20.GlTexture0);
                //GLES20.GlBindTexture(GLES20.GlTexture2d, mFboExternal.getTexture(0));

                // Trigger actual rendering.
                renderQuad(_shaderCopyOes.GetHandle("aPosition"));
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("OK");
            }

            // Bind screen buffer into use.
            GLES20.GlBindFramebuffer(GLES20.GlFramebuffer, 0);
            GLES20.GlViewport(0, 0, _width, _height);

            // Take filter shader into use.
            _shaderFilterDefault.UseProgram();

            // Uniform variables.
            var uBrightness   = _shaderFilterDefault.GetHandle("uBrightness");
            var uContrast     = _shaderFilterDefault.GetHandle("uContrast");
            var uSaturation   = _shaderFilterDefault.GetHandle("uSaturation");
            var uCornerRadius = _shaderFilterDefault.GetHandle("uCornerRadius");

            var uAspectRatio        = _shaderFilterDefault.GetHandle("uAspectRatio");
            var uAspectRatioPreview = _shaderFilterDefault.GetHandle("uAspectRatioPreview");

            // Store uniform variables into use.
            GLES20.GlUniform1f(uBrightness, _sharedData._brightness);
            GLES20.GlUniform1f(uContrast, _sharedData._contrast);
            GLES20.GlUniform1f(uSaturation, _sharedData._saturation);
            GLES20.GlUniform1f(uCornerRadius, _sharedData._cornerRadius);

            GLES20.GlUniform2fv(uAspectRatio, 1, _aspectRatio, 0);
            GLES20.GlUniform2fv(uAspectRatioPreview, 1,
                                _sharedData._aspectRatioPreview, 0);

            // Use offscreen texture as source.
            GLES20.GlActiveTexture(GLES20.GlTexture1);
            GLES20.GlBindTexture(GLES20.GlTexture2d, _fboOffscreen.GetTexture(0));

            // Trigger actual rendering.
            renderQuad(_shaderCopyOes.GetHandle("aPosition"));
        }
Exemplo n.º 28
0
        /**
         * Creates and initializes OpenGL resources needed for rendering the model.
         *
         * @param context Context for loading the shader and below-named model and texture assets.
         * @param objAssetName  Name of the OBJ file containing the model geometry.
         * @param diffuseTextureAssetName  Name of the PNG file containing the diffuse texture map.
         */
        public void CreateOnGlThread(Context context, string objAssetName, string diffuseTextureAssetName)
        {
            // Read the texture.
            var textureBitmap = BitmapFactory.DecodeStream(context.Assets.Open(diffuseTextureAssetName));

            GLES20.GlActiveTexture(GLES20.GlTexture0);
            GLES20.GlGenTextures(mTextures.Length, mTextures, 0);
            GLES20.GlBindTexture(GLES20.GlTexture2d, mTextures[0]);

            GLES20.GlTexParameteri(GLES20.GlTexture2d,
                                   GLES20.GlTextureMinFilter, GLES20.GlLinearMipmapLinear);
            GLES20.GlTexParameteri(GLES20.GlTexture2d,
                                   GLES20.GlTextureMagFilter, GLES20.GlLinear);
            GLUtils.TexImage2D(GLES20.GlTexture2d, 0, textureBitmap, 0);
            GLES20.GlGenerateMipmap(GLES20.GlTexture2d);
            GLES20.GlBindTexture(GLES20.GlTexture2d, 0);

            textureBitmap.Recycle();

            ShaderUtil.CheckGLError(TAG, "Texture loading");

            // Read the obj file.
            var objInputStream = context.Assets.Open(objAssetName);
            var obj            = ObjReader.Read(objInputStream);

            // Prepare the Obj so that its structure is suitable for
            // rendering with OpenGL:
            // 1. Triangulate it
            // 2. Make sure that texture coordinates are not ambiguous
            // 3. Make sure that normals are not ambiguous
            // 4. Convert it to single-indexed data
            obj = ObjUtils.ConvertToRenderable(obj);

            // OpenGL does not use Java arrays. ByteBuffers are used instead to provide data in a format
            // that OpenGL understands.

            // Obtain the data from the OBJ, as direct buffers:
            IntBuffer   wideIndices = ObjData.GetFaceVertexIndices(obj, 3);
            FloatBuffer vertices    = ObjData.GetVertices(obj);
            FloatBuffer texCoords   = ObjData.GetTexCoords(obj, 2);
            FloatBuffer normals     = ObjData.GetNormals(obj);

            // Convert int indices to shorts for GL ES 2.0 compatibility
            ShortBuffer indices = ByteBuffer.AllocateDirect(2 * wideIndices.Limit())
                                  .Order(ByteOrder.NativeOrder()).AsShortBuffer();

            while (wideIndices.HasRemaining)
            {
                indices.Put((short)wideIndices.Get());
            }
            indices.Rewind();

            var buffers = new int[2];

            GLES20.GlGenBuffers(2, buffers, 0);
            mVertexBufferId = buffers[0];
            mIndexBufferId  = buffers[1];

            // Load vertex buffer
            mVerticesBaseAddress  = 0;
            mTexCoordsBaseAddress = mVerticesBaseAddress + 4 * vertices.Limit();
            mNormalsBaseAddress   = mTexCoordsBaseAddress + 4 * texCoords.Limit();
            int totalBytes = mNormalsBaseAddress + 4 * normals.Limit();

            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, mVertexBufferId);
            GLES20.GlBufferData(GLES20.GlArrayBuffer, totalBytes, null, GLES20.GlStaticDraw);
            GLES20.GlBufferSubData(
                GLES20.GlArrayBuffer, mVerticesBaseAddress, 4 * vertices.Limit(), vertices);
            GLES20.GlBufferSubData(
                GLES20.GlArrayBuffer, mTexCoordsBaseAddress, 4 * texCoords.Limit(), texCoords);
            GLES20.GlBufferSubData(
                GLES20.GlArrayBuffer, mNormalsBaseAddress, 4 * normals.Limit(), normals);
            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, 0);

            // Load index buffer
            GLES20.GlBindBuffer(GLES20.GlElementArrayBuffer, mIndexBufferId);
            mIndexCount = indices.Limit();
            GLES20.GlBufferData(
                GLES20.GlElementArrayBuffer, 2 * mIndexCount, indices, GLES20.GlStaticDraw);
            GLES20.GlBindBuffer(GLES20.GlElementArrayBuffer, 0);

            ShaderUtil.CheckGLError(TAG, "OBJ buffer load");

            int vertexShader = ShaderUtil.LoadGLShader(TAG, context,
                                                       GLES20.GlVertexShader, Resource.Raw.object_vertex);
            int fragmentShader = ShaderUtil.LoadGLShader(TAG, context,
                                                         GLES20.GlFragmentShader, Resource.Raw.object_fragment);

            mProgram = GLES20.GlCreateProgram();
            GLES20.GlAttachShader(mProgram, vertexShader);
            GLES20.GlAttachShader(mProgram, fragmentShader);
            GLES20.GlLinkProgram(mProgram);
            GLES20.GlUseProgram(mProgram);

            ShaderUtil.CheckGLError(TAG, "Program creation");

            mModelViewUniform           = GLES20.GlGetUniformLocation(mProgram, "u_ModelView");
            mModelViewProjectionUniform =
                GLES20.GlGetUniformLocation(mProgram, "u_ModelViewProjection");

            mPositionAttribute = GLES20.GlGetAttribLocation(mProgram, "a_Position");
            mNormalAttribute   = GLES20.GlGetAttribLocation(mProgram, "a_Normal");
            mTexCoordAttribute = GLES20.GlGetAttribLocation(mProgram, "a_TexCoord");

            mTextureUniform = GLES20.GlGetUniformLocation(mProgram, "u_Texture");

            mLightingParametersUniform = GLES20.GlGetUniformLocation(mProgram, "u_LightingParameters");
            mMaterialParametersUniform = GLES20.GlGetUniformLocation(mProgram, "u_MaterialParameters");

            ShaderUtil.CheckGLError(TAG, "Program parameters");

            Android.Opengl.Matrix.SetIdentityM(mModelMatrix, 0);
        }
Exemplo n.º 29
0
        public void CreateOnGlThread(Context context, string objAssetName, string diffuseTextureAssetName)
        {
            // Read the texture.
            var textureBitmap = BitmapFactory.DecodeStream(context.Assets.Open(diffuseTextureAssetName));

            GLES20.GlActiveTexture(GLES20.GlTexture0);
            GLES20.GlGenTextures(mTextures.Length, mTextures, 0);
            GLES20.GlBindTexture(GLES20.GlTexture2d, mTextures[0]);

            GLES20.GlTexParameteri(GLES20.GlTexture2d,
                                   GLES20.GlTextureMinFilter, GLES20.GlLinearMipmapLinear);
            GLES20.GlTexParameteri(GLES20.GlTexture2d,
                                   GLES20.GlTextureMagFilter, GLES20.GlLinear);
            GLUtils.TexImage2D(GLES20.GlTexture2d, 0, textureBitmap, 0);
            GLES20.GlGenerateMipmap(GLES20.GlTexture2d);
            GLES20.GlBindTexture(GLES20.GlTexture2d, 0);

            textureBitmap.Recycle();

            ShaderUtil.CheckGLError(TAG, "Texture loading");

            // Read the obj file.
            var objInputStream = context.Assets.Open(objAssetName);
            var obj            = JavaGl.Obj.ObjReader.Read(objInputStream);

            obj = JavaGl.Obj.ObjUtils.ConvertToRenderable(obj);

            IntBuffer   wideIndices = JavaGl.Obj.ObjData.GetFaceVertexIndices(obj, 3);
            FloatBuffer vertices    = JavaGl.Obj.ObjData.GetVertices(obj);
            FloatBuffer texCoords   = JavaGl.Obj.ObjData.GetTexCoords(obj, 2);
            FloatBuffer normals     = JavaGl.Obj.ObjData.GetNormals(obj);

            ShortBuffer indices = ByteBuffer.AllocateDirect(2 * wideIndices.Limit())
                                  .Order(ByteOrder.NativeOrder()).AsShortBuffer();

            while (wideIndices.HasRemaining)
            {
                indices.Put((short)wideIndices.Get());
            }
            indices.Rewind();

            var buffers = new int[2];

            GLES20.GlGenBuffers(2, buffers, 0);
            mVertexBufferId = buffers[0];
            mIndexBufferId  = buffers[1];

            mVerticesBaseAddress  = 0;
            mTexCoordsBaseAddress = mVerticesBaseAddress + 4 * vertices.Limit();
            mNormalsBaseAddress   = mTexCoordsBaseAddress + 4 * texCoords.Limit();
            int totalBytes = mNormalsBaseAddress + 4 * normals.Limit();

            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, mVertexBufferId);
            GLES20.GlBufferData(GLES20.GlArrayBuffer, totalBytes, null, GLES20.GlStaticDraw);
            GLES20.GlBufferSubData(
                GLES20.GlArrayBuffer, mVerticesBaseAddress, 4 * vertices.Limit(), vertices);
            GLES20.GlBufferSubData(
                GLES20.GlArrayBuffer, mTexCoordsBaseAddress, 4 * texCoords.Limit(), texCoords);
            GLES20.GlBufferSubData(
                GLES20.GlArrayBuffer, mNormalsBaseAddress, 4 * normals.Limit(), normals);
            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, 0);

            GLES20.GlBindBuffer(GLES20.GlElementArrayBuffer, mIndexBufferId);
            mIndexCount = indices.Limit();
            GLES20.GlBufferData(
                GLES20.GlElementArrayBuffer, 2 * mIndexCount, indices, GLES20.GlStaticDraw);
            GLES20.GlBindBuffer(GLES20.GlElementArrayBuffer, 0);

            ShaderUtil.CheckGLError(TAG, "OBJ buffer load");

            int vertexShader = ShaderUtil.LoadGLShader(TAG, context,
                                                       GLES20.GlVertexShader, Resource.Raw.object_vertex);
            int fragmentShader = ShaderUtil.LoadGLShader(TAG, context,
                                                         GLES20.GlFragmentShader, Resource.Raw.object_fragment);

            mProgram = GLES20.GlCreateProgram();
            GLES20.GlAttachShader(mProgram, vertexShader);
            GLES20.GlAttachShader(mProgram, fragmentShader);
            GLES20.GlLinkProgram(mProgram);
            GLES20.GlUseProgram(mProgram);

            ShaderUtil.CheckGLError(TAG, "Program creation");

            mModelViewUniform           = GLES20.GlGetUniformLocation(mProgram, "u_ModelView");
            mModelViewProjectionUniform =
                GLES20.GlGetUniformLocation(mProgram, "u_ModelViewProjection");

            mPositionAttribute = GLES20.GlGetAttribLocation(mProgram, "a_Position");
            mNormalAttribute   = GLES20.GlGetAttribLocation(mProgram, "a_Normal");
            mTexCoordAttribute = GLES20.GlGetAttribLocation(mProgram, "a_TexCoord");

            mTextureUniform = GLES20.GlGetUniformLocation(mProgram, "u_Texture");

            mLightingParametersUniform = GLES20.GlGetUniformLocation(mProgram, "u_LightingParameters");
            mMaterialParametersUniform = GLES20.GlGetUniformLocation(mProgram, "u_MaterialParameters");

            ShaderUtil.CheckGLError(TAG, "Program parameters");

            Android.Opengl.Matrix.SetIdentityM(mModelMatrix, 0);
        }