Esempio n. 1
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);
        }
Esempio n. 2
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);
                }
            }
        }
Esempio n. 3
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);
        }
        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");
        }
        /// <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.");
        }
Esempio n. 6
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.GL_BACK);
            //GLES20.glFrontFace(GLES20.GL_CCW);

            //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");
        }
Esempio n. 7
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");
        }
        /// <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.");
        }
        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.");
        }
        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);
            }
        }
        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));
        }
        /**
         * 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);
        }
        /**
         * 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");
        }
Esempio n. 14
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");
        }
Esempio n. 15
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);
                        }
                    }
                }
            }
        }
Esempio n. 16
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);
        }
        /**
         * Prepares EGL.  We want a GLES 2.0 context and a surface that supports recording.
         */
        private void eglSetup()
        {
            mEGLDisplay = EGL14.EglGetDisplay(EGL14.EglDefaultDisplay);
            if (mEGLDisplay == EGL14.EglNoDisplay)
            {
                throw new Java.Lang.RuntimeException("unable to get EGL14 display");
            }
            int[] version = new int[2];
            if (!EGL14.EglInitialize(mEGLDisplay, version, 0, version, 1))
            {
                throw new RuntimeException("unable to initialize EGL14");
            }

            // Configure EGL for recording and OpenGL ES 2.0.
            int[] attribList;
            if (mEGLSharedContext == null)
            {
                attribList = new int[] {
                    EGL14.EglRedSize, 8,
                    EGL14.EglGreenSize, 8,
                    EGL14.EglBlueSize, 8,
                    EGL14.EglRenderableType, EGL14.EglOpenglEs2Bit,
                    EGL14.EglNone
                };
            }
            else
            {
                attribList = new int[] {
                    EGL14.EglRedSize, 8,
                    EGL14.EglGreenSize, 8,
                    EGL14.EglBlueSize, 8,
                    EGL14.EglRenderableType, EGL14.EglOpenglEs2Bit,
                    EGL_RECORDABLE_ANDROID, 1,
                    EGL14.EglNone
                };
            }
            EGLConfig[] configs    = new EGLConfig[1];
            int[]       numConfigs = new int[1];
            EGL14.EglChooseConfig(mEGLDisplay, attribList, 0, configs, 0, configs.Length,
                                  numConfigs, 0);
            checkEglError("eglCreateContext RGB888+recordable ES2");

            // Configure context for OpenGL ES 2.0.
            int[] attrib_list =
            {
                EGL14.EglContextClientVersion, 2,
                EGL14.EglNone
            };

            if (mEGLSharedContext == null)
            {
                mEGLContext = EGL14.EglCreateContext(mEGLDisplay, configs[0], EGL14.EglNoContext, attrib_list, 0);
            }
            else
            {
                mEGLContext = EGL14.EglCreateContext(mEGLDisplay, configs[0], mEGLSharedContext, attrib_list, 0);
            }
            checkEglError("eglCreateContext");

            // Create a window surface, and attach it to the Surface we received.
            int[] surfaceAttribs =
            {
                EGL14.EglNone
            };
            mEGLSurface = EGL14.EglCreateWindowSurface(mEGLDisplay, configs[0], mSurface,
                                                       surfaceAttribs, 0);
            checkEglError("eglCreateWindowSurface");

            GLES20.GlDisable(GLES20.GlDepthTest);
            GLES20.GlDisable(GLES20.GlCullFaceMode);
        }