Exemplo n.º 1
0
        private void drawNonAlpha(ShellSurface surface, GLSL glsl)
        {
            var mats = surface.RenderLists;

            int max = mats.Count;

            // draw non-alpha material
//			GLES20.GlEnable(GLES20.GlCullFace);
            GLES20.GlEnable(2884);
            GLES20.GlCullFace(GLES20.GlBack);
            GLES20.GlDisable(GLES20.GlBlend);
            for (int r = 0; r < max; r++)
            {
                var     mat = mats[r];
                TexInfo tb  = null;
                if (mat.material.texture != null)
                {
                    tb = TextureFile.FetchTexInfo(mat.material.texture);
                }
                if (mat.material.diffuse_color[3] >= 1.0 && (tb == null || !tb.has_alpha))
                {
                    drawOneMaterial(glsl, surface, mat);
                }
            }
        }
Exemplo n.º 2
0
        public override void DrawFrame()
        {
            //Glass code with enable blend
            GLES20.GlEnable(GL10.GlBlend);
            GLES20.GlBlendFunc(GL10.GlSrcAlpha, GL10.GlOneMinusSrcAlpha);

            if (brocken)
            {
                lx  = 0.9f;
                ly  = 0.0f;
                lz  = 0.0f;
                lw += 0.01f;
                if (lw > 0.5)
                {
                    lw = 0;
                }
            }
            else
            {
                lw = 0;
            }

            base.DrawFrame();


            GLES20.GlDisable(GL10.GlBlend);
        }
Exemplo n.º 3
0
        public override void OnSurfaceCreated(IGL10 gl, Javax.Microedition.Khronos.Egl.EGLConfig config)
        {
            base.OnSurfaceCreated(gl, config);

            //SETUP OpenGL ES
            GLES20.GlClearColor(0.9f, 0.9f, 0.9f, 1.0f);

            GLES20.GlEnable(GLES20.GlDepthTest); //uncoment if needs enabled dpeth test

            //GLES20.GlEnable(2884); // GlCullFace == 2884 see OpenGL documentation to this constant value  GLES20.GlDisable(2884);
            //GLES20.GlFrontFace(GLES20.GlCcw);
            //GLES20.GlCullFace(GLES20.GlFront);

            GLES20.GlEnable(GL10.GlBlend);
            GLES20.GlBlendFunc(GL10.GlSrcAlpha, GL10.GlOneMinusSrcAlpha);

            //  GLES20.GlDisable(GL10.GlLighting);
            // GLES20.GlEnable(2884);
            //  GLES20.GlDisable(GL10.GlDepthBufferBit);
            //  GLES20.GlEnable(GL10.GlDither);
            //   GLES20.GlEnable(GL10.GlBlend);
            // GLES20.GlBlendFunc(GL10.GlSrcAlpha, GL10.GlOne);


            //ENDSETUP OpenGL ES

            //Loading objects
            glObjects.Add(new GLObject(this, "vertex_shader", "fragment_shader", "oldhouse_objvertex", "oldhouse_objnormal", "oldhouse_objtexture", "body"));

            //Ask android to run RAM garbage cleaner
            System.GC.Collect();
        }
Exemplo n.º 4
0
        public void Initialize()
        {
            GLES20.GlClearColor(0.1f, 0.1f, 0.1f, 0.5f);             // Dark background so text shows up well

            cubeVertices      = PrepareBuffer(WorldLayoutData.CubeCoords);
            cubeNormals       = PrepareBuffer(WorldLayoutData.CubeNormals);
            cubeTextureCoords = PrepareBuffer(WorldLayoutData.CubeTexCoords);

            floorVertices = PrepareBuffer(WorldLayoutData.FloorCoords);
            floorNormals  = PrepareBuffer(WorldLayoutData.FloorNormals);
            floorColors   = PrepareBuffer(WorldLayoutData.FloorColors);

            monkeyFound    = DrawingUtils.LoadGlTexture(resources, Resource.Drawable.texture2);
            monkeyNotFound = DrawingUtils.LoadGlTexture(resources, Resource.Drawable.texture1);

            int vertexShader = DrawingUtils.LoadGlShader(GLES20.GlVertexShader, resources, Resource.Raw.vertex);
            int gridShader   = DrawingUtils.LoadGlShader(GLES20.GlFragmentShader, resources, Resource.Raw.fragment);

            glProgram = GLES20.GlCreateProgram();
            GLES20.GlAttachShader(glProgram, vertexShader);
            GLES20.GlAttachShader(glProgram, gridShader);
            GLES20.GlLinkProgram(glProgram);

            GLES20.GlEnable(GLES20.GlDepthTest);

            // Object first appears directly in front of user
            Matrix.SetIdentityM(modelCube, 0);
            Matrix.TranslateM(modelCube, 0, 0, 0, -mObjectDistance);

            Matrix.SetIdentityM(modelFloor, 0);
            Matrix.TranslateM(modelFloor, 0, 0, -mFloorDepth, 0);             // Floor appears below user

            CheckGlError("onSurfaceCreated");
        }
Exemplo n.º 5
0
        /**
         * \brief Draw the video keyframe (in OpenGL).
         * @param mvpMatrix the model-view-projection matrix.
         */
        private void DrawKeyFrame(float[] mvpMatrix)
        {
            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);
            GLES20.GlBindTexture(GLES20.GlTexture2d, mKeyframeTexture_GL_ID);
            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.º 6
0
        public void Draw(Frame frame)
        {
            if (frame.HasDisplayGeometryChanged)//.IsDisplayRotationChanged)
            {
                frame.TransformDisplayUvCoords(mQuadTexCoord, mQuadTexCoordTransformed);
            }

            GLES20.GlDisable(GLES20.GlDepthTest);
            GLES20.GlDepthMask(false);

            GLES20.GlBindTexture(GLES11Ext.GlTextureExternalOes, TextureId);

            GLES20.GlUseProgram(mQuadProgram);

            GLES20.GlVertexAttribPointer(
                mQuadPositionParam, COORDS_PER_VERTEX, GLES20.GlFloat, false, 0, mQuadVertices);

            GLES20.GlVertexAttribPointer(mQuadTexCoordParam, TEXCOORDS_PER_VERTEX,
                                         GLES20.GlFloat, false, 0, mQuadTexCoordTransformed);

            GLES20.GlEnableVertexAttribArray(mQuadPositionParam);
            GLES20.GlEnableVertexAttribArray(mQuadTexCoordParam);

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

            // Disable vertex arrays
            GLES20.GlDisableVertexAttribArray(mQuadPositionParam);
            GLES20.GlDisableVertexAttribArray(mQuadTexCoordParam);

            // Restore the depth state for further drawing.
            GLES20.GlDepthMask(true);
            GLES20.GlEnable(GLES20.GlDepthTest);

            ShaderUtil.CheckGLError(TAG, "Draw");
        }
        /**
         * Draws a frame for an eye.
         *
         * @param eye The eye to render. Includes all required transformations.
         */
        public void OnDrawEye(Eye eye)
        {
            GLES20.GlEnable(GLES20.GlDepthTest);
            GLES20.GlClear(GLES20.GlColorBufferBit | GLES20.GlDepthBufferBit);

            CheckGLError("colorParam");

            // Apply the eye transformation to the camera.
            Matrix.MultiplyMM(view, 0, eye.GetEyeView(), 0, camera, 0);

            // Set the position of the light
            Matrix.MultiplyMV(lightPosInEyeSpace, 0, view, 0, LIGHT_POS_IN_WORLD_SPACE, 0);

            // Build the ModelView and ModelViewProjection matrices
            // for calculating cube position and light.
            float [] perspective = eye.GetPerspective(Z_NEAR, Z_FAR);
            Matrix.MultiplyMM(modelView, 0, view, 0, modelCube, 0);
            Matrix.MultiplyMM(modelViewProjection, 0, perspective, 0, modelView, 0);

            DrawCube();

            // Set modelView for the floor, so we draw floor in the correct location
            Matrix.MultiplyMM(modelView, 0, view, 0, modelFloor, 0);
            Matrix.MultiplyMM(modelViewProjection, 0, perspective, 0, modelView, 0);

            DrawFloor();
        }
Exemplo n.º 8
0
        public void OnSurfaceCreated(IGL10 gl, EGLConfig config)
        {
            GLES20.GlEnable(GL10.GlBlend);
            // For premultiplied alpha:
            // GLES20.GlBlendFunc( GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA );
            GLES20.GlBlendFunc(GL10.GlSrcAlpha, GL10.GlOneMinusSrcAlpha);

            GLES20.GlEnable(GL10.GlScissorTest);

            TextureCache.Reload();
        }
Exemplo n.º 9
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>
        /// 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.º 11
0
        void GLSurfaceView.IRenderer.OnSurfaceCreated(Javax.Microedition.Khronos.Opengles.IGL10 gl, Javax.Microedition.Khronos.Egl.EGLConfig config)

        {
            GLES20.GlClearColor(1f, 1f, 1f, 1.0f);
            GLES20.GlEnable(GLES20.GlDepthTest);
            mPointCloud           = new PointCloud(mMaxDepthPoints);
            mGrid                 = new Grid();
            mCameraFrustumAndAxis = new CameraFrustumAndAxis();
            Matrix.SetIdentityM(mViewMatrix, 0);
            Matrix.SetLookAtM(mViewMatrix, 0, 5f, 5f, 5f, 0f, 0f, 0f, 0f, 1f, 0f);
            mCameraFrustumAndAxis.ModelMatrix = ModelMatCalculator.ModelMatrix;
        }
Exemplo n.º 12
0
        public override void OnSurfaceCreated(IGL10 gl, Javax.Microedition.Khronos.Egl.EGLConfig config)
        {
            base.OnSurfaceCreated(gl, config);

            //SETUP OpenGL ES
            GLES20.GlClearColor(0.9f, 0.9f, 0.9f, 0.9f);
            GLES20.GlEnable(GLES20.GlDepthTest); //uncoment if needs enabled dpeth test
            //ENDSETUP OpenGL ES

            //Loading objects
            glObjects.Add(new GLObject(this, "vertex_shader", "fragment_shader", "oldhouse_objvertex", "oldhouse_objnormal", "oldhouse_objtexture", "body"));

            //Ask android to run RAM garbage cleaner
            System.GC.Collect();
        }
Exemplo n.º 13
0
 public void OnSurfaceCreated(IGL10 gl, Javax.Microedition.Khronos.Egl.EGLConfig config)
 {
     // Set background color and enable depth testing
     GLES20.GlClearColor(1f, 1f, 1f, 1.0f);
     GLES20.GlEnable(GLES20.GlDepthTest);
     resetModelMatCalculator();
     mCameraFrustum        = new CameraFrustum();
     mFloorGrid            = new Grid();
     mCameraFrustumAndAxis = new CameraFrustumAndAxis();
     mTrajectory           = new Trajectory(3);
     // Construct the initial view matrix
     Matrix.SetIdentityM(mViewMatrix, 0);
     Matrix.SetLookAtM(mViewMatrix, 0, 5f, 5f, 5f, 0f, 0f, 0f, 0f, 1f, 0f);
     mCameraFrustumAndAxis.ModelMatrix = ModelMatCalculator.ModelMatrix;
 }
Exemplo n.º 14
0
        /**
         * Draws the AR background image.  The image will be drawn such that virtual content rendered
         * with the matrices provided by {@link Frame#getViewMatrix(float[], int)} and
         * {@link Session#getProjectionMatrix(float[], int, float, float)} will accurately follow
         * static physical objects.  This must be called <b>before</b> drawing virtual content.
         *
         * @param frame The last {@code Frame} returned by {@link Session#update()}.
         */
        public void Draw(Frame frame)
        {
            // If display rotation changed (also includes view size change), we need to re-query the uv
            // coordinates for the screen rect, as they may have changed as well.
            if (frame.HasDisplayGeometryChanged)
            {
                frame.TransformDisplayUvCoords(mQuadTexCoord, mQuadTexCoordTransformed);
            }

            // No need to test or write depth, the screen quad has arbitrary depth, and is expected
            // to be drawn first.
            GLES20.GlDisable(GLES20.GlDepthTest);
            GLES20.GlDepthMask(false);

            GLES20.GlBindTexture(mTextureTarget, TextureId);
            GLES20.GlTexParameteri(mTextureTarget, GLES20.GlTextureWrapS, GLES20.GlClampToEdge);
            GLES20.GlTexParameteri(mTextureTarget, GLES20.GlTextureWrapT, GLES20.GlClampToEdge);
            GLES20.GlTexParameteri(mTextureTarget, GLES20.GlTextureMinFilter, GLES20.GlNearest);
            GLES20.GlTexParameteri(mTextureTarget, GLES20.GlTextureMagFilter, GLES20.GlNearest);

            GLES20.GlUseProgram(mQuadProgram);

            // Set the vertex positions.
            GLES20.GlVertexAttribPointer(
                mQuadPositionParam, COORDS_PER_VERTEX, GLES20.GlFloat, false, 0, mQuadVertices);

            // Set the texture coordinates.
            GLES20.GlVertexAttribPointer(mQuadTexCoordParam, TEXCOORDS_PER_VERTEX,
                                         GLES20.GlFloat, false, 0, mQuadTexCoordTransformed);

            // Enable vertex arrays
            GLES20.GlEnableVertexAttribArray(mQuadPositionParam);
            GLES20.GlEnableVertexAttribArray(mQuadTexCoordParam);

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

            // Disable vertex arrays
            GLES20.GlDisableVertexAttribArray(mQuadPositionParam);
            GLES20.GlDisableVertexAttribArray(mQuadTexCoordParam);

            // Restore the depth state for further drawing.
            GLES20.GlDepthMask(true);
            GLES20.GlEnable(GLES20.GlDepthTest);



            ShaderUtil.CheckGLError(TAG, "Draw");
        }
Exemplo n.º 15
0
        public void OnSurfaceCreated(IGL10 gl, Javax.Microedition.Khronos.Egl.EGLConfig config)
        {
            if (RenderManager.externalPaused)
            {
                RenderManager.initShaders();
            }
            else
            {
                RenderManager.createObjects();
            }

            GLES20.GlClearColor(0.1f, 0.1f, 0.9f, 0.0f);
            GLES20.GlEnable(GLES20.GlDepthTest);
            GLES20.GlEnable(GLES20.GlCullFaceMode);
            GLES20.GlCullFace(GLES20.GlBack);
        }
        public void OnSurfaceCreated(IGL10 gl, Javax.Microedition.Khronos.Egl.EGLConfig config)
        {
            // Create the triangle
            SetupTriangle();

            // Create the image information
            SetupImage();

            // Set the clear color to yellow
            GLES20.GlClearColor(1.0f, 1.0f, 0f, 1);

            GLES20.GlEnable(GLES20.GlBlend);
            GLES20.GlBlendFunc(GLES20.GlOne, GLES20.GlOneMinusSrcAlpha);

            #region Solid colors mode

            /*
             * // Create the shaders, solid color
             * int vertexShader = ShaderHelper.LoadShader(GLES20.GlVertexShader, ShaderHelper.VsSolidColor);
             * int fragmentShader = ShaderHelper.LoadShader(GLES20.GlFragmentShader, ShaderHelper.FsSolidColor);
             *
             * ShaderHelper.SpSolidColor = GLES20.GlCreateProgram();             // create empty OpenGL ES Program
             * GLES20.GlAttachShader(ShaderHelper.SpSolidColor, vertexShader);   // add the vertex shader to program
             * GLES20.GlAttachShader(ShaderHelper.SpSolidColor, fragmentShader); // add the fragment shader to program
             * GLES20.GlLinkProgram(ShaderHelper.SpSolidColor);                  // creates OpenGL ES program executables
             *
             * // Set our shader programm
             * GLES20.GlUseProgram(ShaderHelper.SpSolidColor);
             */
            #endregion

            #region Texture mode
            // Create the shaders, images
            int vertexShader   = ShaderHelper.LoadShader(GLES20.GlVertexShader, ShaderHelper.VsImage);
            int fragmentShader = ShaderHelper.LoadShader(GLES20.GlFragmentShader, ShaderHelper.FsImage);

            ShaderHelper.SpImage = GLES20.GlCreateProgram();
            GLES20.GlAttachShader(ShaderHelper.SpImage, vertexShader);
            GLES20.GlAttachShader(ShaderHelper.SpImage, fragmentShader);
            GLES20.GlLinkProgram(ShaderHelper.SpImage);

            // Set our shader programm
            GLES20.GlUseProgram(ShaderHelper.SpImage);
            #endregion
        }
Exemplo n.º 17
0
            public void OnDrawFrame(HeadTransform head, EyeParams leftEye, EyeParams rightEye)
            {
                mStereoRenderer.OnNewFrame(head);
                GLES20.GlEnable(GLES20.GlScissorTest);

                leftEye.getViewport().setGLViewport();
                leftEye.getViewport().setGLScissor();
                mStereoRenderer.OnDrawEye(leftEye.getTransform());

                if (rightEye == null)
                {
                    return;
                }

                rightEye.getViewport().setGLViewport();
                rightEye.getViewport().setGLScissor();
                mStereoRenderer.OnDrawEye(rightEye.getTransform());
            }
Exemplo n.º 18
0
        public void OnSurfaceCreated(IGL10 gl, EGLConfig config)
        {
            // GL configurations
            int bonenum = 48;

            GLES20.GlGetIntegerv(GLES20.GlMaxTextureSize, mTexSize, 0);
            ShellViewModel.MaxBone = bonenum;
            mNpot = hasExt("GL_OES_texture_npot");

            // initialize
            GLES20.GlClearColor(0, 0, 0, 1);
            GLES20.GlEnable(GLES20.GlDepthTest);
            GLES20.GlEnable(GLES20.GlBlend);
            checkGlError("GlEnable");

            // GLUtils.texImage2D generates premultiplied-alpha texture. so we use GL_ONE instead of GL_ALPHA
            GLES20.GlBlendFunc(GL10.GlOne, GL10.GlOneMinusSrcAlpha);
            GLES20.GlDepthFunc(GLES20.GlLequal);
            GLES20.GlFrontFace(GLES20.GlCw);
            checkGlError("GlMiscSettings");

            // shader programs
            mGLSL = new Dictionary <String, GLSL>();

            mGLSL.Add("builtin:default",
                      new GLSL(String.Format(Util.ReadAssetString("shader/vs.vsh"), ShellViewModel.MaxBone),
                               Util.ReadAssetString("shader/fs.fsh")));

            mGLSL.Add("builtin:default_alpha",
                      new GLSL(String.Format(Util.ReadAssetString("shader/vs.vsh"), ShellViewModel.MaxBone),
                               Util.ReadAssetString("shader/fs_alpha.fsh")));

            mGLSL.Add("builtin:nomotion",
                      new GLSL(Util.ReadAssetString("shader/vs_nm.vsh"), Util.ReadAssetString("shader/fs.fsh")));

            mGLSL.Add("builtin:nomotion_alpha",
                      new GLSL(Util.ReadAssetString("shader/vs_nm.vsh"), Util.ReadAssetString("shader/fs_alpha.fsh")));

            // render targets
            mRT = new Dictionary <String, RenderTarget>();
            mRT.Add("screen", new RenderTarget());

            GLES20.GlBindFramebuffer(GLES20.GlFramebuffer, 0);
        }
        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.");
        }
        /// <summary>
        /// Render each frame.
        /// This method is called when Android.Opengl.GLSurfaceView.IRenderer's OnDrawFrame.
        /// </summary>
        /// <param name="frame">ARFrame</param>
        public void OnDrawFrame(ARFrame frame)
        {
            ShaderUtil.CheckGlError(TAG, "On draw frame start.");
            if (frame == null)
            {
                return;
            }
            if (frame.HasDisplayGeometryChanged)
            {
                frame.TransformDisplayUvCoords(mTexBuffer, mTexTransformedBuffer);
            }
            Clear();

            GLES20.GlDisable(GLES20.GlDepthTest);
            GLES20.GlDepthMask(false);

            GLES20.GlUseProgram(mProgram);

            // Set the texture ID.
            GLES20.GlBindTexture(GLES11Ext.GlTextureExternalOes, mExternalTextureId);

            // Set the projection matrix.
            GLES20.GlUniformMatrix4fv(mMatrix, 1, false, mProjectionMatrix, 0);

            GLES20.GlUniformMatrix4fv(mCoordMatrix, 1, false, coordMatrixs, 0);

            // Set the vertex.
            GLES20.GlEnableVertexAttribArray(mPosition);
            GLES20.GlVertexAttribPointer(mPosition, 2, GLES20.GlFloat, false, 0, mVerBuffer);

            // Set the texture coordinates.
            GLES20.GlEnableVertexAttribArray(mCoord);
            GLES20.GlVertexAttribPointer(mCoord, 2, GLES20.GlFloat, false, 0, mTexTransformedBuffer);

            // Number of vertices.
            GLES20.GlDrawArrays(GLES20.GlTriangleStrip, 0, 4);
            GLES20.GlDisableVertexAttribArray(mPosition);
            GLES20.GlDisableVertexAttribArray(mCoord);

            GLES20.GlDepthMask(true);
            GLES20.GlEnable(GLES20.GlDepthTest);
            ShaderUtil.CheckGlError(TAG, "On draw frame end.");
        }
Exemplo n.º 21
0
        public void afterDrawFrame()
        {
            GLES20.GlBindFramebuffer(36160, mOriginalFramebufferId /*.array()[0]*/.Get(0));
            GLES20.GlViewport(0, 0, mHmd.getScreen().getWidth(), mHmd.getScreen().getHeight());

            GLES20.GlGetIntegerv(2978, mViewport);
            GLES20.GlGetIntegerv(2884, mCullFaceEnabled);
            GLES20.GlGetIntegerv(3089, mScissorTestEnabled);
            GLES20.GlDisable(3089);
            GLES20.GlDisable(2884);

            GLES20.GlClearColor(0.0F, 0.0F, 0.0F, 1.0F);
            GLES20.GlClear(16640);

            GLES20.GlUseProgram(mProgramHolder.program);

            GLES20.GlEnable(3089);
            GLES20.GlScissor(0, 0, mHmd.getScreen().getWidth() / 2, mHmd.getScreen().getHeight());

            renderDistortionMesh(mLeftEyeDistortionMesh);

            GLES20.GlScissor(mHmd.getScreen().getWidth() / 2, 0, mHmd.getScreen().getWidth() / 2, mHmd.getScreen().getHeight());

            renderDistortionMesh(mRightEyeDistortionMesh);

            GLES20.GlDisableVertexAttribArray(mProgramHolder.aPosition);
            GLES20.GlDisableVertexAttribArray(mProgramHolder.aVignette);
            GLES20.GlDisableVertexAttribArray(mProgramHolder.aTextureCoord);
            GLES20.GlUseProgram(0);
            GLES20.GlBindBuffer(34962, 0);
            GLES20.GlBindBuffer(34963, 0);
            GLES20.GlDisable(3089);
            if (mCullFaceEnabled /*.array()[0]*/.Get(0) == 1)
            {
                GLES20.GlEnable(2884);
            }
            if (mScissorTestEnabled /*.array()[0]*/.Get(0) == 1)
            {
                GLES20.GlEnable(3089);
            }
            GLES20.GlViewport(mViewport /*.array()[0]*/.Get(0), mViewport /*.array()[1]*/.Get(1), mViewport /*.array()[2]*/.Get(2), mViewport /*.array()[3]*/.Get(3));
        }
Exemplo n.º 22
0
        public override void OnSurfaceCreated(IGL10 gl, Javax.Microedition.Khronos.Egl.EGLConfig config)
        {
            base.OnSurfaceCreated(gl, config);

            //SETUP OpenGL ES
            GLES20.GlClearColor(0.5f, 0.5f, 0.5f, 1.0f);
            GLES20.GlEnable(GLES20.GlDepthTest); //uncoment if needs enabled dpeth test
            //  GLES20.GlEnable(2884); // GlCullFace == 2884 see OpenGL documentation to this constant value
            //  GLES20.GlCullFace(GLES20.GlFront);
            //ENDSETUP OpenGL ES

            //Loading objects
            glObjects.Add(new GLObject(this, "den_vertex_shader", "den_fragment_shader", "den_house1_objvertex", "den_house1_objnormal", "den_house1_objtexture", "den_housetextutre2"));
            glObjects.Add(new GLObject(this, "den_vertex_shader", "den_fragment_shader", "den_house2_objvertex", "den_house2_objnormal", "den_house2_objtexture", "den_housetextutre2"));
            glObjects.Add(new GLObject(this, "den_vertex_shader", "den_fragment_shader", "den_house3_objvertex", "den_house3_objnormal", "den_house3_objtexture", "den_housetextutre2"));
            glObjects.Add(new DenGlassObject(this, "den_glass"));


            //Ask android to run RAM garbage cleaner
            System.GC.Collect();
        }
        /**
         * Generates a frame of data using GL commands.
         * <p>
         * We have an 8-frame animation sequence that wraps around. It looks like this:
         * <pre>
         * 0 1 2 3
         * 7 6 5 4
         * </pre>
         * We draw one of the eight rectangles and leave the rest set to the zero-fill color. */
        private void generateSurfaceFrame(int frameIndex)
        {
            frameIndex %= 8;
            int startX, startY;

            if (frameIndex < 4)
            {
                // (0,0) is bottom-left in GL
                startX = frameIndex * (mWidth / 4);
                startY = mHeight / 2;
            }
            else
            {
                startX = (7 - frameIndex) * (mWidth / 4);
                startY = 0;
            }
            GLES20.GlDisable(GLES20.GlScissorTest);
            GLES20.GlClearColor(TEST_R0 / 255.0f, TEST_G0 / 255.0f, TEST_B0 / 255.0f, 1.0f);
            GLES20.GlClear(GLES20.GlColorBufferBit);
            GLES20.GlEnable(GLES20.GlScissorTest);
            GLES20.GlScissor(startX, startY, mWidth / 4, mHeight / 2);
            GLES20.GlClearColor(TEST_R1 / 255.0f, TEST_G1 / 255.0f, TEST_B1 / 255.0f, 1.0f);
            GLES20.GlClear(GLES20.GlColorBufferBit);
        }
Exemplo n.º 24
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");
        }
Exemplo n.º 25
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.º 26
0
        public void OnDrawFrame(IGL10 gl)
        {
            // Clear screen to notify driver it should not load any pixels from previous frame.
            GLES20.GlBindFramebuffer(GLES20.GlFramebuffer, 0);
            GLES20.GlViewport(0, 0, glSurfaceView.Width, glSurfaceView.Height);
            GLES20.GlClear(GLES20.GlColorBufferBit | GLES20.GlDepthBufferBit);

            if (arSession == null)
            {
                return;
            }

            // Notify ARCore session that the view size changed so that the perspective matrix and the video background
            // can be properly adjusted
            // displayRotationHelper.UpdateSessionIfNeeded(arSession);

            try
            {
                // Obtain the current frame from ARSession. When the configuration is set to
                // UpdateMode.BLOCKING (it is by default), this will throttle the rendering to the
                // camera framerate.
                Frame frame = arSession.Update();
                Google.AR.Core.Camera camera = frame.Camera;


                // Draw background.
                GLES20.GlViewport(0, 0, glSurfaceView.Width, glSurfaceView.Height);
                backgroundRenderer.Draw(frame);

                GLES20.GlBindFramebuffer(GLES20.GlFramebuffer, fboId);
                GLES20.GlViewport(0, 0, targetResolution.Width, targetResolution.Height);
                GLES20.GlClear(GLES20.GlColorBufferBit | GLES20.GlDepthBufferBit);
                backgroundRenderer.Draw(frame);
                GLES20.GlBindFramebuffer(GLES20.GlFramebuffer, 0);
                GLES20.GlViewport(0, 0, glSurfaceView.Width, glSurfaceView.Height);
                GlUtil.CheckNoGLES2Error("Switch framebuffers.");

                // Handle taps. Handling only one tap per frame, as taps are usually low frequency
                // compared to frame rate.
                MotionEvent tap = null;
                queuedSingleTaps.TryDequeue(out tap);

                if (tap != null && camera.TrackingState == TrackingState.Tracking)
                {
                    foreach (var hit in frame.HitTest(tap))
                    {
                        var trackable = hit.Trackable;

                        // Check if any plane was hit, and if it was hit inside the plane polygon.
                        if (trackable is Plane && ((Plane)trackable).IsPoseInPolygon(hit.HitPose))
                        {
                            // Cap the number of objects created. This avoids overloading both the
                            // rendering system and ARCore.
                            if (anchors.Count >= 16)
                            {
                                anchors[0].Detach();
                                anchors.RemoveAt(0);
                            }
                            // Adding an Anchor tells ARCore that it should track this position in
                            // space.  This anchor is created on the Plane to place the 3d model
                            // in the correct position relative to both the world and to the plane
                            anchors.Add(hit.CreateAnchor());

                            // Hits are sorted by depth. Consider only closest hit on a plane.
                            break;
                        }
                    }
                }

                // If not tracking, don't draw 3d objects.
                if (camera.TrackingState == TrackingState.Paused)
                {
                    return;
                }

                // Get projection matrix.
                float[] projmtx = new float[16];
                camera.GetProjectionMatrix(projmtx, 0, 0.1f, 100.0f);

                // Get camera matrix and draw.
                float[] viewmtx = new float[16];
                camera.GetViewMatrix(viewmtx, 0);

                // Compute lighting from average intensity of the image.
                var lightIntensity = frame.LightEstimate.PixelIntensity;

                // Visualize tracked points.
                var pointCloud = frame.AcquirePointCloud();
                pointCloudRenderer.Update(pointCloud);

                // App is repsonsible for releasing point cloud resources after using it
                pointCloud.Release();

                var planes = new List <Plane>();
                foreach (var p in arSession.GetAllTrackables(Java.Lang.Class.FromType(typeof(Plane))))
                {
                    var plane = (Plane)p;
                    planes.Add(plane);
                }

                // Check if we detected at least one plane. If so, hide the loading message.
                if (loadingMessageSnackbar != null)
                {
                    foreach (var plane in planes)
                    {
                        if (plane.GetType() == Plane.Type.HorizontalUpwardFacing &&
                            plane.TrackingState == TrackingState.Tracking)
                        {
                            HideLoadingMessage();
                            break;
                        }
                    }
                }

                // Draw(frame, camera, projmtx, viewmtx, lightIntensity, planes);


                GLES20.GlBindFramebuffer(GLES20.GlFramebuffer, fboId);
                GLES20.GlViewport(0, 0, targetResolution.Width, targetResolution.Height);
                // Restore the depth state for further drawing.
                GLES20.GlDepthMask(true);
                GLES20.GlEnable(GLES20.GlDepthTest);
                // Draw(frame, camera, projmtx, viewmtx, lightIntensity, planes);
                // DrawModels(projmtx, viewmtx, lightIntensity);


                if (doCaptureCameraFrame)
                {
                    var displayOrientedPose = camera.DisplayOrientedPose;
                    var pose = new VirtualStudio.Shared.DTOs.Tracking.Pose
                    {
                        Position    = new System.Numerics.Vector3(displayOrientedPose.Tx(), displayOrientedPose.Ty(), displayOrientedPose.Tz()),
                        Orientation = new System.Numerics.Vector4(displayOrientedPose.Qx(), displayOrientedPose.Qy(), displayOrientedPose.Qz(), displayOrientedPose.Qw()),
                        Projection  = new System.Numerics.Matrix4x4(
                            projmtx[0], projmtx[1], projmtx[2], projmtx[3],
                            projmtx[4], projmtx[5], projmtx[6], projmtx[7],
                            projmtx[8], projmtx[9], projmtx[10], projmtx[11],
                            projmtx[12], projmtx[13], projmtx[14], projmtx[15]
                            )
                    };
                    webRtcClient.SendMessage(pose.ToBinary());
                    counter = 0;

                    var textureBuffer = new TextureBufferImpl(targetResolution.Width, targetResolution.Height, VideoFrame.TextureBufferType.Rgb, renderTextureId, new Android.Graphics.Matrix(), null, null, null);
                    var i420Buffer    = yuvConverter.Convert(textureBuffer);
                    VideoFrameAvailable?.Invoke(this, i420Buffer);
                }
            }
            catch (System.Exception ex)
            {
                // Avoid crashing the application due to unhandled exceptions.
                Log.Error(TAG, "Exception on the OpenGL thread", ex);
            }
        }
Exemplo n.º 27
0
        /**
         * \brief Draw the video mesh /with keyframe and icons too) (in OpenGL).
         * @param modelView the model-view matrix.
         * @param projection the projection matrix.
         */
        public void DrawMesh(float[] modelView, float[] projection)
        {
            PikkartVideoPlayer.VideoSate.VIDEO_STATE currentStatus = PikkartVideoPlayer.VideoSate.VIDEO_STATE.NOT_READY;
            if (mPikkartVideoPlayer != null)
            {
                currentStatus = mPikkartVideoPlayer.getVideoStatus();
                if (!mPikkartVideoPlayer.isFullscreen())
                {
                    if (mPikkartVideoPlayer.getVideoStatus() == PikkartVideoPlayer.VideoSate.VIDEO_STATE.PLAYING)
                    {
                        mPikkartVideoPlayer.updateVideoData();
                    }
                    mPikkartVideoPlayer.getSurfaceTextureTransformMatrix(mTexCoordTransformationMatrix);
                    SetVideoDimensions(mPikkartVideoPlayer.getVideoWidth(), mPikkartVideoPlayer.getVideoHeight(), mTexCoordTransformationMatrix);
                    mVideoTexCoords_Buffer = FillBuffer(videoTextureCoordsTransformed);
                }
            }

            Marker currentMarker = RecognitionFragment.CurrentMarker;

            if (currentMarker != null)
            {
                float markerWidth  = currentMarker.Width;
                float markerHeight = currentMarker.Height;

                GLES20.GlEnable(GLES20.GlDepthTest);
                //GLES20.GlDisable(GLES20.GlCullFaceMode);
                GLES20.GlCullFace(GLES20.GlBack);
                GLES20.GlFrontFace(GLES20.GlCw);

                if ((currentStatus == PikkartVideoPlayer.VideoSate.VIDEO_STATE.READY) ||
                    (currentStatus == PikkartVideoPlayer.VideoSate.VIDEO_STATE.END) ||
                    (currentStatus == PikkartVideoPlayer.VideoSate.VIDEO_STATE.NOT_READY) ||
                    (currentStatus == PikkartVideoPlayer.VideoSate.VIDEO_STATE.ERROR))
                {
                    float[] scaleMatrix = new float[16];
                    RenderUtils.matrix44Identity(scaleMatrix);
                    scaleMatrix[0]  = markerWidth;
                    scaleMatrix[5]  = markerWidth * keyframeAspectRatio;
                    scaleMatrix[10] = markerWidth;

                    float[] temp_mv = new float[16];
                    RenderUtils.matrixMultiply(4, 4, modelView, 4, 4, scaleMatrix, temp_mv);

                    float[] temp_mvp = new float[16];
                    RenderUtils.matrixMultiply(4, 4, projection, 4, 4, temp_mv, temp_mvp);
                    float[] mvpMatrix = new float[16];
                    RenderUtils.matrix44Transpose(temp_mvp, mvpMatrix);

                    DrawKeyFrame(mvpMatrix);
                }
                else
                {
                    float[] scaleMatrix = new float[16];
                    RenderUtils.matrix44Identity(scaleMatrix);
                    scaleMatrix[0]  = markerWidth;
                    scaleMatrix[5]  = markerWidth * videoAspectRatio;
                    scaleMatrix[10] = markerWidth;

                    float[] temp_mv = new float[16];
                    RenderUtils.matrixMultiply(4, 4, modelView, 4, 4, scaleMatrix, temp_mv);

                    float[] temp_mvp = new float[16];
                    RenderUtils.matrixMultiply(4, 4, projection, 4, 4, temp_mv, temp_mvp);
                    float[] mvpMatrix = new float[16];
                    RenderUtils.matrix44Transpose(temp_mvp, mvpMatrix);

                    DrawVideo(mvpMatrix);
                }

                if ((currentStatus == PikkartVideoPlayer.VideoSate.VIDEO_STATE.READY) ||
                    (currentStatus == PikkartVideoPlayer.VideoSate.VIDEO_STATE.END) ||
                    (currentStatus == PikkartVideoPlayer.VideoSate.VIDEO_STATE.PAUSED) ||
                    (currentStatus == PikkartVideoPlayer.VideoSate.VIDEO_STATE.NOT_READY) ||
                    (currentStatus == PikkartVideoPlayer.VideoSate.VIDEO_STATE.ERROR))
                {
                    float[] translateMatrix = new float[16];
                    RenderUtils.matrix44Identity(translateMatrix);
                    //scale a bit
                    translateMatrix[0]  = 0.4f;
                    translateMatrix[5]  = 0.4f;
                    translateMatrix[10] = 0.4f;
                    //translate a bit
                    translateMatrix[3]  = 0.0f;
                    translateMatrix[7]  = 0.45f;
                    translateMatrix[11] = -0.05f;

                    float[] temp_mv = new float[16];
                    RenderUtils.matrixMultiply(4, 4, modelView, 4, 4, translateMatrix, temp_mv);

                    float[] temp_mvp = new float[16];
                    RenderUtils.matrixMultiply(4, 4, projection, 4, 4, temp_mv, temp_mvp);
                    float[] mvpMatrix = new float[16];
                    RenderUtils.matrix44Transpose(temp_mvp, mvpMatrix);

                    DrawIcon(mvpMatrix, currentStatus);
                }
                RenderUtils.CheckGLError("VideoMesh:end video renderer");
            }
        }
Exemplo n.º 28
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.º 29
0
        private void drawAlpha(ShellSurface surface, GLSL glsl, bool alpha_test)
        {
            int max = surface.RenderLists.Count();

            // draw alpha material
            GLES20.GlEnable(GLES20.GlBlend);
            for (int r = 0; r < max; r++)
            {
                var     mat = surface.RenderLists[r];
                TexInfo tb  = null;
                if (mat.material.texture != null)
                {
                    tb = TextureFile.FetchTexInfo(mat.material.texture);
                }
                if (alpha_test)
                {
                    if (tb != null && tb.needs_alpha_test)
                    {
                        if (mat.material.diffuse_color[3] < 1.0)
                        {
//							GLES20.GlDisable(GLES20.GL_CULL_FACE);
                            GLES20.GlDisable(2884);
                        }
                        else
                        {
//							GLES20.GlEnable(GLES20.GL_CULL_FACE);
                            GLES20.GlEnable(2884);
                        }
                        drawOneMaterial(glsl, surface, mat);
                    }
                }
                else
                {
                    if (tb != null)                      // has texture
                    {
                        if (!tb.needs_alpha_test)
                        {
                            if (tb.has_alpha)
                            {
                                if (mat.material.diffuse_color[3] < 1.0)
                                {
//									GLES20.GlDisable(GLES20.GL_CULL_FACE);
                                    GLES20.GlDisable(2884);
                                }
                                else
                                {
//									GLES20.GlEnable(GLES20.GL_CULL_FACE);
                                    GLES20.GlEnable(2884);
                                }
                                drawOneMaterial(glsl, surface, mat);
                            }
                            else if (mat.material.diffuse_color[3] < 1.0)
                            {
//								GLES20.GlDisable(GLES20.GL_CULL_FACE);
                                GLES20.GlDisable(2884);
                                drawOneMaterial(glsl, surface, mat);
                            }
                        }
                    }
                    else
                    {
                        if (mat.material.diffuse_color[3] < 1.0)
                        {
//							GLES20.GlDisable(GLES20.GL_CULL_FACE);
                            GLES20.GlDisable(2884);
                            drawOneMaterial(glsl, surface, mat);
                        }
                    }
                }
            }
        }
Exemplo n.º 30
0
        public void OnSurfaceCreated(IGL10 gl, Javax.Microedition.Khronos.Egl.EGLConfig config)
        {
            const float coord = 1.0f;

            // Cube coords
            // X, Y, Z = 1 vertex * 3 = 1 face * 12 = 1 cube
            float[] triangleVerticesData =
            {
                -coord, -coord, -coord,
                -coord, -coord, coord,
                -coord, coord,  coord,

                coord,  coord,  -coord,
                -coord, -coord, -coord,
                -coord, coord,  -coord,

                coord,  -coord, coord,
                -coord, -coord, -coord,
                coord,  -coord, -coord,

                coord,  coord,  -coord,
                coord,  -coord, -coord,
                -coord, -coord, -coord,

                -coord, -coord, -coord,
                -coord, coord,  coord,
                -coord, coord,  -coord,

                coord,  -coord, coord,
                -coord, -coord, coord,
                -coord, -coord, -coord,

                -coord, coord,  coord,
                -coord, -coord, coord,
                coord,  -coord, coord,

                coord,  coord,  coord,
                coord,  -coord, -coord,
                coord,  coord,  -coord,

                coord,  -coord, -coord,
                coord,  coord,  coord,
                coord,  -coord, coord,

                coord,  coord,  coord,
                coord,  coord,  -coord,
                -coord, coord,  -coord,

                coord,  coord,  coord,
                -coord, coord,  -coord,
                -coord, coord,  coord,

                coord,  coord,  coord,
                -coord, coord,  coord,
                coord,  -coord, coord
            };

            FloatBuffer mTriangleVertices = ByteBuffer.AllocateDirect(triangleVerticesData.Length * mBytesPerFloat).Order(ByteOrder.NativeOrder()).AsFloatBuffer();

            mTriangleVertices.Put(triangleVerticesData).Flip();

            // Cube colors
            // R, G, B, A
            float[] triangleColorsData =
            {
                1.0f, 0.0f, 0.0f, 0.5f,
                0.0f, 0.5f, 1.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,

                1.0f, 0.0f, 0.0f, 0.5f,
                0.0f, 0.5f, 1.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,

                1.0f, 0.0f, 0.0f, 0.5f,
                0.0f, 0.5f, 1.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,

                1.0f, 0.0f, 0.0f, 0.5f,
                0.0f, 0.5f, 1.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,

                1.0f, 0.0f, 0.0f, 0.5f,
                0.0f, 0.5f, 1.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,

                1.0f, 0.0f, 0.0f, 0.5f,
                0.0f, 0.5f, 1.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,

                1.0f, 0.0f, 0.0f, 0.5f,
                0.0f, 0.5f, 1.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,

                1.0f, 0.0f, 0.0f, 0.5f,
                0.0f, 0.5f, 1.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,

                1.0f, 0.0f, 0.0f, 0.5f,
                0.0f, 0.5f, 1.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,

                1.0f, 0.0f, 0.0f, 0.5f,
                0.0f, 0.5f, 1.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,

                1.0f, 0.0f, 0.0f, 0.5f,
                0.0f, 0.5f, 1.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,

                1.0f, 0.0f, 0.0f, 0.5f,
                0.0f, 0.5f, 1.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f
            };

            FloatBuffer mTriangleColors = ByteBuffer.AllocateDirect(triangleColorsData.Length * mBytesPerFloat).Order(ByteOrder.NativeOrder()).AsFloatBuffer();

            mTriangleColors.Put(triangleColorsData).Flip();

            //Cube texture UV Map
            float[] triangleTextureUVMapData =
            {
                0.0f, 0.0f,
                0.0f, 1.0f,
                1.0f, 0.0f,

                0.0f, 0.0f,
                0.0f, 1.0f,
                1.0f, 0.0f,

                0.0f, 0.0f,
                0.0f, 1.0f,
                1.0f, 0.0f,

                0.0f, 0.0f,
                0.0f, 1.0f,
                1.0f, 0.0f,

                0.0f, 0.0f,
                0.0f, 1.0f,
                1.0f, 0.0f,

                0.0f, 0.0f,
                0.0f, 1.0f,
                1.0f, 0.0f,

                0.0f, 0.0f,
                0.0f, 1.0f,
                1.0f, 0.0f,

                0.0f, 0.0f,
                0.0f, 1.0f,
                1.0f, 0.0f,

                0.0f, 0.0f,
                0.0f, 1.0f,
                1.0f, 0.0f,

                0.0f, 0.0f,
                0.0f, 1.0f,
                1.0f, 0.0f,

                0.0f, 0.0f,
                0.0f, 1.0f,
                1.0f, 0.0f,

                0.0f, 0.0f,
                0.0f, 1.0f,
                1.0f, 0.0f
            };

            FloatBuffer mTriangleTextureUVMap = ByteBuffer.AllocateDirect(triangleTextureUVMapData.Length * mBytesPerFloat).Order(ByteOrder.NativeOrder()).AsFloatBuffer();

            mTriangleTextureUVMap.Put(triangleTextureUVMapData).Flip();

            //triagles normals
            //This normal array is not right, it is spacialy DO FOR demonstrate how normals work with faces when light is calculated at shader program
            float[] triangleNormalData =
            {
                // Front face
                0.0f,   0.0f,  1.0f,
                0.0f,   0.0f,  1.0f,
                0.0f,   0.0f,  1.0f,
                1.0f,   0.0f,  0.0f,
                1.0f,   0.0f,  0.0f,
                1.0f,   0.0f,  0.0f,

                // Right face
                1.0f,   0.0f,  0.0f,
                1.0f,   0.0f,  0.0f,
                1.0f,   0.0f,  0.0f,
                1.0f,   0.0f,  0.0f,
                1.0f,   0.0f,  0.0f,
                1.0f,   0.0f,  0.0f,

                // Back face
                0.0f,   0.0f, -1.0f,
                0.0f,   0.0f, -1.0f,
                0.0f,   0.0f, -1.0f,
                0.0f,   0.0f, -1.0f,
                0.0f,   0.0f, -1.0f,
                0.0f,   0.0f, -1.0f,

                // Left face
                -1.0f,  0.0f,  0.0f,
                -1.0f,  0.0f,  0.0f,
                -1.0f,  0.0f,  0.0f,
                -1.0f,  0.0f,  0.0f,
                -1.0f,  0.0f,  0.0f,
                -1.0f,  0.0f,  0.0f,

                // Top face
                0.0f,   1.0f,  0.0f,
                0.0f,   1.0f,  0.0f,
                0.0f,   1.0f,  0.0f,
                0.0f,   1.0f,  0.0f,
                0.0f,   1.0f,  0.0f,
                0.0f,   1.0f,  0.0f,

                // Bottom face
                0.0f,  -1.0f,  0.0f,
                0.0f,  -1.0f,  0.0f,
                0.0f,  -1.0f,  0.0f,
                0.0f,  -1.0f,  0.0f,
                0.0f,  -1.0f,  0.0f,
                0.0f,  -1.0f, 0.0f
            };

            FloatBuffer mTriangleNormal = ByteBuffer.AllocateDirect(triangleNormalData.Length * mBytesPerFloat).Order(ByteOrder.NativeOrder()).AsFloatBuffer();

            mTriangleNormal.Put(triangleNormalData).Flip();

            //Data buffers to VBO
            GLES20.GlGenBuffers(4, VBOBuffers, 0); //2 buffers for vertices, texture and colors

            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[0]);
            GLES20.GlBufferData(GLES20.GlArrayBuffer, mTriangleVertices.Capacity() * mBytesPerFloat, mTriangleVertices, GLES20.GlStaticDraw);

            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[1]);
            GLES20.GlBufferData(GLES20.GlArrayBuffer, mTriangleColors.Capacity() * mBytesPerFloat, mTriangleColors, GLES20.GlStaticDraw);

            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[2]);
            GLES20.GlBufferData(GLES20.GlArrayBuffer, mTriangleTextureUVMap.Capacity() * mBytesPerFloat, mTriangleTextureUVMap, GLES20.GlStaticDraw);

            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, VBOBuffers[3]);
            GLES20.GlBufferData(GLES20.GlArrayBuffer, mTriangleNormal.Capacity() * mBytesPerFloat, mTriangleNormal, GLES20.GlStaticDraw);

            GLES20.GlBindBuffer(GLES20.GlArrayBuffer, 0);

            //Load and setup texture

            GLES20.GlGenTextures(1, textureHandle, 0); //init 1 texture storage handle
            if (textureHandle[0] != 0)
            {
                //Android.Graphics cose class Matrix exists at both Android.Graphics and Android.OpenGL and this is only sample of using
                Android.Graphics.BitmapFactory.Options options = new Android.Graphics.BitmapFactory.Options();
                options.InScaled = false; // No pre-scaling
                Android.Graphics.Bitmap bitmap = Android.Graphics.BitmapFactory.DecodeResource(context.Resources, Resource.Drawable.texture1, options);
                GLES20.GlBindTexture(GLES20.GlTexture2d, textureHandle[0]);
                GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMinFilter, GLES20.GlNearest);
                GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureMagFilter, GLES20.GlNearest);
                GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapS, GLES20.GlClampToEdge);
                GLES20.GlTexParameteri(GLES20.GlTexture2d, GLES20.GlTextureWrapT, GLES20.GlClampToEdge);
                GLUtils.TexImage2D(GLES20.GlTexture2d, 0, bitmap, 0);
                bitmap.Recycle();
            }

            //Ask android to run RAM garbage cleaner
            System.GC.Collect();

            //Setup OpenGL ES
            GLES20.GlClearColor(0.0f, 0.0f, 0.0f, 0.0f);
            // GLES20.GlEnable(GLES20.GlDepthTest); //uncoment if needs enabled dpeth test
            GLES20.GlEnable(2884); // GlCullFace == 2884 see OpenGL documentation to this constant value
            GLES20.GlCullFace(GLES20.GlBack);


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

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

            // Set our up vector. This is where our head would be pointing were we holding the camera.
            float upX = 0.0f;
            float upY = coord;
            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);

            //all "attribute" variables is "triagles" VBO (arrays) items representation
            //a_Possition[0] <=> a_Color[0] <=> a_TextureCoord[0] <=> a_Normal[0]
            //a_Possition[1] <=> a_Color[1] <=> a_TextureCoord[1] <=> a_Normal[1]
            //...
            //a_Possition[n] <=> a_Color[n] <=> a_TextureCoord[n] <=> a_Normal[n] -- where "n" is object buffers length
            //-> HOW MANY faces in your object (model) in VBO -> how many times the vertex shader will be called by OpenGL
            string vertexShader =
                "uniform mat4 u_MVPMatrix;      \n"         // A constant representing the combined model/view/projection matrix.
                + "uniform vec3 u_LightPos;       \n"       // A constant representing the light source position
                + "attribute vec4 a_Position;     \n"       // Per-vertex position information we will pass in. (it means vec4[x,y,z,w] but we put only x,y,z at this sample
                + "attribute vec4 a_Color;        \n"       // Per-vertex color information we will pass in.
                + "varying vec4 v_Color;          \n"       // This will be passed into the fragment shader.
                + "attribute vec2 a_TextureCoord; \n"       // Per-vertex texture UVMap information we will pass in.
                + "varying vec2 v_TextureCoord;   \n"       // This will be passed into the fragment shader.
                + "attribute vec3 a_Normal;       \n"       // Per-vertex normals information we will pass in.
                + "void main()                    \n"       // The entry point for our vertex shader.
                + "{                              \n"
                //light calculation section for fragment shader
                + "   vec3 modelViewVertex = vec3(u_MVPMatrix * a_Position);\n"
                + "   vec3 modelViewNormal = vec3(u_MVPMatrix * vec4(a_Normal, 0.0));\n"
                + "   float distance = length(u_LightPos - modelViewVertex);\n"
                + "   vec3 lightVector = normalize(u_LightPos - modelViewVertex);\n"
                + "   float diffuse = max(dot(modelViewNormal, lightVector), 0.1);\n"
                + "   diffuse = diffuse * (1.0 / (1.0 + (0.25 * distance * distance)));\n"
                + "   v_Color = a_Color * vec4(diffuse);\n" //Pass the color with light aspect to fragment shader
                + "   v_TextureCoord = a_TextureCoord; \n"  // Pass the texture coordinate through to the fragment shader. It will be interpolated across the triangle.
                + "   gl_Position = u_MVPMatrix   \n"       // gl_Position is a special variable used to store the final position.
                + "                 * a_Position; \n"       // Multiply the vertex by the matrix to get the final point in normalized screen coordinates.
                + "}                              \n";

            string fragmentShader =
                "precision mediump float;       \n"                                       // Set the default precision to medium. We don't need as high of a
                                                                                          // precision in the fragment shader.
                + "varying vec4 v_Color;          \n"                                     // This is the color from the vertex shader interpolated across the triangle per fragment.
                + "varying vec2 v_TextureCoord;   \n"                                     // This is the texture coordinate from the vertex shader interpolated across the triangle per fragment.
                + "uniform sampler2D u_Texture;   \n"                                     // This is the texture image handler
                + "void main()                    \n"                                     // The entry point for our fragment shader.
                + "{                              \n"
                + "   gl_FragColor = texture2D(u_Texture, v_TextureCoord) * v_Color;  \n" // Pass the color directly through the pipeline.
                + "}                              \n";

            int vertexShaderHandle = GLES20.GlCreateShader(GLES20.GlVertexShader);

            if (vertexShaderHandle != 0)
            {
                // Pass in the shader source.
                GLES20.GlShaderSource(vertexShaderHandle, vertexShader);

                // Compile the shader.
                GLES20.GlCompileShader(vertexShaderHandle);

                // Get the compilation status.
                int[] compileStatus = new int[1];
                GLES20.GlGetShaderiv(vertexShaderHandle, GLES20.GlCompileStatus, compileStatus, 0);

                // If the compilation failed, delete the shader.
                if (compileStatus[0] == 0)
                {
                    GLES20.GlDeleteShader(vertexShaderHandle);
                    vertexShaderHandle = 0;
                }
            }

            if (vertexShaderHandle == 0)
            {
                throw new Exception("Error creating vertex shader.");
            }

            // Load in the fragment shader shader.
            int fragmentShaderHandle = GLES20.GlCreateShader(GLES20.GlFragmentShader);

            if (fragmentShaderHandle != 0)
            {
                // Pass in the shader source.
                GLES20.GlShaderSource(fragmentShaderHandle, fragmentShader);

                // Compile the shader.
                GLES20.GlCompileShader(fragmentShaderHandle);

                // Get the compilation status.
                int[] compileStatus = new int[1];
                GLES20.GlGetShaderiv(fragmentShaderHandle, GLES20.GlCompileStatus, compileStatus, 0);

                // If the compilation failed, delete the shader.
                if (compileStatus[0] == 0)
                {
                    GLES20.GlDeleteShader(fragmentShaderHandle);
                    fragmentShaderHandle = 0;
                }
            }

            if (fragmentShaderHandle == 0)
            {
                throw new Exception("Error creating fragment shader.");
            }

            // Create a program object and store the handle to it.
            int programHandle = GLES20.GlCreateProgram();

            if (programHandle != 0)
            {
                // Bind the vertex shader to the program.
                GLES20.GlAttachShader(programHandle, vertexShaderHandle);

                // Bind the fragment shader to the program.
                GLES20.GlAttachShader(programHandle, fragmentShaderHandle);

                // Bind attributes
                GLES20.GlBindAttribLocation(programHandle, 0, "a_Position");
                GLES20.GlBindAttribLocation(programHandle, 1, "a_Color");
                GLES20.GlBindAttribLocation(programHandle, 2, "a_TextureCoord");
                GLES20.GlBindAttribLocation(programHandle, 3, "a_Normal");

                // Link the two shaders together into a program.
                GLES20.GlLinkProgram(programHandle);

                // Get the link status.
                int[] linkStatus = new int[1];
                GLES20.GlGetProgramiv(programHandle, GLES20.GlLinkStatus, linkStatus, 0);

                // If the link failed, delete the program.
                if (linkStatus[0] == 0)
                {
                    GLES20.GlDeleteProgram(programHandle);
                    programHandle = 0;
                }
            }

            if (programHandle == 0)
            {
                throw new Exception("Error creating program.");
            }

            // Set program handles. These will later be used to pass in values to the program.
            mMVPMatrixHandle    = GLES20.GlGetUniformLocation(programHandle, "u_MVPMatrix");
            mLightPos           = GLES20.GlGetUniformLocation(programHandle, "u_LightPos");
            mPositionHandle     = GLES20.GlGetAttribLocation(programHandle, "a_Position");
            mColorHandle        = GLES20.GlGetAttribLocation(programHandle, "a_Color");
            mTextureCoordHandle = GLES20.GlGetAttribLocation(programHandle, "a_TextureCoord");
            mNormalHandle       = GLES20.GlGetAttribLocation(programHandle, "a_Normal");
            mTextureHandle      = GLES20.GlGetUniformLocation(programHandle, "u_Texture");


            // Tell OpenGL to use this program when rendering.
            GLES20.GlUseProgram(programHandle);
        }