Exemplo n.º 1
0
        public bool InitMesh(string meshFile, string textureFile)
        {
            GenerateMesh();

            CGSize dims = CGSize.Empty;

            mMeshPath               = meshFile;
            mTexture_GL_ID          = RenderUtils.LoadTextureFromFileName(textureFile, out dims);
            mKeyframe_Program_GL_ID = RenderUtils.CreateProgram(MESH_VERTEX_SHADER, MESH_FRAGMENT_SHADER);
            return(true);
        }
Exemplo n.º 2
0
        /*
         * glEnable(GLenum(GL_DEPTH_TEST));
         * glDisable(GLenum(GL_CULL_FACE));
         *
         * glUseProgram(_Program_GL_ID);
         *
         * RenderUtils.checkGLError()
         *
         * let vertexHandle = glGetAttribLocation(_Program_GL_ID, "vertexPosition");
         * let textureCoordHandle = glGetAttribLocation(_Program_GL_ID, "vertexTexCoord");
         * let mvpMatrixHandle = glGetUniformLocation(_Program_GL_ID, "modelViewProjectionMatrix");
         * let texSampler2DHandle = glGetUniformLocation(_Program_GL_ID, "texSampler2D");
         *
         * RenderUtils.checkGLError()
         *
         *
         * glVertexAttribPointer(GLuint(vertexHandle), 3, GLenum(GL_FLOAT), GLboolean(GL_FALSE), 0, _vertices_buffer);
         * glVertexAttribPointer(GLuint(textureCoordHandle), 2, GLenum(GL_FLOAT), GLboolean(GL_FALSE), 0, _tex_vertices_buffer);
         *
         * glEnableVertexAttribArray(GLuint(vertexHandle));
         * glEnableVertexAttribArray(GLuint(textureCoordHandle));
         *
         * RenderUtils.checkGLError()
         *
         * // activate texture 0, bind it, and pass to shader
         * glActiveTexture(GLenum(GL_TEXTURE0));
         * glBindTexture(GLenum(GL_TEXTURE_2D), _Texture_GL_ID);
         * glUniform1i(texSampler2DHandle, 0);
         * RenderUtils.checkGLError()
         *
         * // pass the model view matrix to the shader
         * glUniformMatrix4fv(mvpMatrixHandle, 1, GLboolean(GL_FALSE), modelViewProjection);
         *
         * RenderUtils.checkGLError()
         *
         * // finally draw the monkey
         * glDrawElements(GLenum(GL_TRIANGLES), GLsizei(_indexes_number), GLenum(GL_UNSIGNED_SHORT), _indexes_buffer);
         *
         * RenderUtils.checkGLError()
         *
         * glDisableVertexAttribArray(GLuint(vertexHandle));
         * glDisableVertexAttribArray(GLuint(textureCoordHandle));
         *
         * RenderUtils.checkGLError()
         */
        public void DrawMesh(ref float[] mvpMatrix)
        {
            GL.Enable(EnableCap.DepthTest);
            GL.Disable(EnableCap.CullFace);

            GL.UseProgram(mKeyframe_Program_GL_ID);

            RenderUtils.CheckGLError();

            var vertexHandle       = GL.GetAttribLocation(mKeyframe_Program_GL_ID, "vertexPosition");
            var textureCoordHandle = GL.GetAttribLocation(mKeyframe_Program_GL_ID, "vertexTexCoord");
            var mvpMatrixHandle    = GL.GetUniformLocation(mKeyframe_Program_GL_ID, "modelViewProjectionMatrix");
            var texSampler2DHandle = GL.GetUniformLocation(mKeyframe_Program_GL_ID, "texSampler2D");

            RenderUtils.CheckGLError();

            GL.VertexAttribPointer(vertexHandle, 3, VertexAttribPointerType.Float, false, 0, mVertices);
            GL.VertexAttribPointer(textureCoordHandle, 2, VertexAttribPointerType.Float, false, 0, mTexCoords);


            GL.EnableVertexAttribArray(vertexHandle);
            GL.EnableVertexAttribArray(textureCoordHandle);

            RenderUtils.CheckGLError();

            GL.ActiveTexture(TextureUnit.Texture0);
            GL.BindTexture(TextureTarget.Texture2D, mTexture_GL_ID);
            GL.Uniform1(texSampler2DHandle, 0);

            RenderUtils.CheckGLError();

            GL.Ext.PushGroupMarker(0, "Draw Pikkart KeyFrame");

            GL.UniformMatrix4(mvpMatrixHandle, 1, false, mvpMatrix);

            GL.DrawElements(BeginMode.Triangles, mIndices_Number, DrawElementsType.UnsignedShort, mIndex);

            GL.Ext.PopGroupMarker();

            RenderUtils.CheckGLError();

            GL.DisableVertexAttribArray(vertexHandle);
            GL.DisableVertexAttribArray(textureCoordHandle);

            RenderUtils.CheckGLError();
        }
        internal static int BuildTexture(demoImage image)
        {
            int texName = 0;

            // Create a texture object to apply to model
            unsafe  {
                GL.GenTextures(1, &texName);
                GL.BindTexture(TextureTarget.Texture2D, texName);

                // Set up filter and wrap modes for this texture object
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)All.Nearest);


                // Allocate and load image data into texture
                GL.TexImage2D(TextureTarget.Texture2D, 0, (PixelInternalFormat)image.format,
                              (int)image.width, (int)image.height, 0,
                              image.format, image.type, Marshal.UnsafeAddrOfPinnedArrayElement(image.data, 0));
                RenderUtils.CheckGLError();
            }

            return(texName);
        }
        public override void DrawInRect(GLKView view, CoreGraphics.CGRect rect)
        {
            if (!isActive())
            {
                return;
            }

            UIInterfaceOrientation orientation = UIApplication.SharedApplication.StatusBarOrientation;
            CGSize size = new CGSize(ViewportWidth, ViewportHeight);

            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            if (firstGLUpdate == false)
            {
                ApplyCameraGlOrientation(orientation);
                firstGLUpdate = true;
            }

            RenderCamera(size, Angle);

            if (isTracking())
            {
                if (CurrentMarker != null)
                {
                    if (CurrentMarker.Id == "3_1836")
                    {
                        float[] mvpMatrix = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                        if (computeModelViewProjectionMatrix(ref mvpMatrix))
                        {
                            mMonkeyMesh.DrawMesh(ref mvpMatrix);
                            RenderUtils.CheckGLError();
                        }
                    }
                }
            }
            GL.Finish();
        }
        bool computeModelViewProjectionMatrix(ref float[] mvpMatrix)
        {
            float w = 640, h = 480;

            RenderUtils.MtxLoadIdentity(ref mvpMatrix);

            float ar = (float)(ViewportHeight / ViewportWidth);

            if (ViewportHeight > ViewportWidth)
            {
                ar = 1f / ar;
            }

            float h1 = h, w1 = w;

            if (ar < h / w)
            {
                h1 = (float)(w * ar);
            }
            else
            {
                w1 = (float)(h / ar);
            }

            float a = 0, b = 0;

            switch (Angle)
            {
            case 0:
                a = 1; b = 0;
                break;

            case 90:
                a = 0; b = 1;
                break;

            case 180:
                a = -1; b = 0;
                break;

            case 270:
                a = 0; b = -1;
                break;

            default: break;
            }

            float [] angleMatrix = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

            angleMatrix[0]  = a; angleMatrix[1] = b; angleMatrix[2] = 0; angleMatrix[3] = 0;
            angleMatrix[4]  = -b; angleMatrix[5] = a; angleMatrix[6] = 0; angleMatrix[7] = 0;
            angleMatrix[8]  = 0; angleMatrix[9] = 0; angleMatrix[10] = 1f; angleMatrix[11] = 0f;
            angleMatrix[12] = 0f; angleMatrix[13] = 0f; angleMatrix[14] = 0f; angleMatrix[15] = 1f;

            IntPtr tempPtr = IntPtr.Zero;

            GetCurrentProjectionMatrix(ref tempPtr);

            float[] projectionMatrix = new float[16];

            Marshal.Copy(tempPtr, projectionMatrix, 0, 16);

            projectionMatrix[5] = projectionMatrix[5] * (h / h1);

            float[] correctedProjection = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

            RenderUtils.MatrixMultiply(4, 4, ref angleMatrix, 4, 4, ref projectionMatrix, ref correctedProjection);

            if (isTracking())
            {
                IntPtr  modelviewMatrixPtr = IntPtr.Zero;
                float[] temp_mvp           = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                float[] modelviewMatrix    = new float[16];

                GetCurrentModelViewMatrix(ref modelviewMatrixPtr);

                Marshal.Copy(modelviewMatrixPtr, modelviewMatrix, 0, 16);

                RenderUtils.MatrixMultiply(4, 4, ref correctedProjection, 4, 4, ref modelviewMatrix, ref temp_mvp);
                RenderUtils.MtxTranspose(ref temp_mvp, ref mvpMatrix);

                return(true);
            }

            return(false);
        }