Exemple #1
0
        public static int InitShader(ShaderType shaderType, string source)
        {
            int logLength;
            int status = 0;
            int shader = GL.CreateShader(shaderType);

            if (shader != 0)
            {
                unsafe {
                    GL.ShaderSource(shader, source);
                    GL.CompileShader(shader);
                    GL.GetShader(shader, ShaderParameter.InfoLogLength, &logLength);

                    if (logLength > 0)
                    {
                        StringBuilder log = new StringBuilder();
                        GL.GetShaderInfoLog(shader, logLength, (int *)null, log);
                        Console.WriteLine("Vtx Shader compile log: {0}\n", log);
                    }
                    GL.GetShader(shader, ShaderParameter.CompileStatus, &status);
                    if (status == 0)
                    {
                        Console.WriteLine("Failed to compile vtx shader: {0}\n", source);
                    }
                }
            }
            RenderUtils.CheckGLError();

            return(shader);
        }
        void DrawKeyFrame(ref float[] mvpMatrix)
        {
            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);

            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, mKeyframeTexture_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();

            GL.UseProgram(0);
            GL.Disable(EnableCap.Blend);

            RenderUtils.CheckGLError();
        }
Exemple #3
0
        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);
        }
        void DrawVideo(ref float[] mvpMatrix)
        {
            GL.UseProgram(mVideo_Program_GL_ID);

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

            RenderUtils.CheckGLError();

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

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

            RenderUtils.CheckGLError();

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

            RenderUtils.CheckGLError();

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

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

            RenderUtils.CheckGLError();

            GL.UseProgram(0);

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

            RenderUtils.CheckGLError();
        }
        public override void DrawInRect(GLKView view, CoreGraphics.CGRect rect)
        {
            if (!isActive())
            {
                return;
            }
            UIInterfaceOrientation orientation = UIApplication.SharedApplication.StatusBarOrientation;
            CGSize size;

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

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

            size = new CGSize(ViewportWidth, ViewportHeight);
            RenderCamera(size, Angle);

            if (isTracking())
            {
                if (CurrentMarker != null)
                {
                    if (CurrentMarker.Id == "3_543")
                    {
                        float[] mvMatrix = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                        float[] pMatrix  = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                        if (computeModelViewProjectionMatrix(ref mvMatrix, ref pMatrix))
                        {
                            _videoMesh.DrawMesh(ref mvMatrix, ref pMatrix);
                            RenderUtils.CheckGLError();
                        }
                    }
                }
            }

            GL.Finish();
        }
        public void DrawMesh(ref float [] modelView, ref float[] projection)
        {
            var viewCtrl = _parentCtrl as RecognitionViewController;

            if (viewCtrl != null)
            {
                var currentStatus = PKTVIDEO_STATE.NOT_READY;
                if (mPikkartVideoPlayer != null)
                {
                    currentStatus = mPikkartVideoPlayer.Status;
                    if (currentStatus == PKTVIDEO_STATE.PLAYING)
                    {
                        mPikkartVideoPlayer.UpdateVideoData();
                        videoAspectRatio = (float)(mPikkartVideoPlayer.Size.Height / mPikkartVideoPlayer.Size.Width);
                    }
                }

                PKTMarker currentMarker = viewCtrl.CurrentMarker;
                if (currentMarker != null)
                {
                    var markerSize = new CGSize(currentMarker.Width, currentMarker.Height);
                    GL.Enable(EnableCap.DepthTest);
                    GL.Disable(EnableCap.CullFace);

                    if ((currentStatus == PKTVIDEO_STATE.READY) ||
                        (currentStatus == PKTVIDEO_STATE.REACHED_END) ||
                        (currentStatus == PKTVIDEO_STATE.NOT_READY) ||
                        (currentStatus == PKTVIDEO_STATE.ERROR))
                    {
                        float[] scaleMatrix = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                        RenderUtils.MtxLoadIdentity(ref scaleMatrix);
                        scaleMatrix[0]  = (float)markerSize.Width;
                        scaleMatrix[5]  = (float)(markerSize.Width * keyframeAspectRatio);
                        scaleMatrix[10] = (float)markerSize.Width;

                        float[] temp_mv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                        RenderUtils.MatrixMultiply(4, 4, ref modelView, 4, 4, ref scaleMatrix, ref temp_mv);

                        float[] temp_mvp = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                        RenderUtils.MatrixMultiply(4, 4, ref projection, 4, 4, ref temp_mv, ref temp_mvp);

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

                        RenderUtils.MtxTranspose(ref temp_mvp,
                                                 ref mvpMatrix);

                        DrawKeyFrame(ref mvpMatrix);
                    }
                    else
                    {
                        float[] scaleMatrix = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                        RenderUtils.MtxLoadIdentity(ref scaleMatrix);
                        scaleMatrix[0]  = (float)(markerSize.Width);
                        scaleMatrix[5]  = (float)(markerSize.Width * videoAspectRatio);
                        scaleMatrix[10] = (float)(markerSize.Width);


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

                        RenderUtils.MatrixMultiply(4, 4, ref modelView, 4, 4, ref scaleMatrix, ref temp_mv);


                        float[] temp_mvp = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                        RenderUtils.MatrixMultiply(4, 4, ref projection, 4, 4, ref temp_mv, ref temp_mvp);

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

                        RenderUtils.MtxTranspose(ref temp_mvp,
                                                 ref mvpMatrix);

                        DrawVideo(ref mvpMatrix);
                    }

                    if ((currentStatus == PKTVIDEO_STATE.READY) ||
                        (currentStatus == PKTVIDEO_STATE.REACHED_END) ||
                        (currentStatus == PKTVIDEO_STATE.PAUSED) ||
                        (currentStatus == PKTVIDEO_STATE.NOT_READY) ||
                        (currentStatus == PKTVIDEO_STATE.ERROR))
                    {
                        float[] translateMatrix = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

                        RenderUtils.MtxLoadIdentity(ref translateMatrix);
                        //scale a bit
                        translateMatrix[0]  = 0.4f;
                        translateMatrix[5]  = 0.4f;
                        translateMatrix[10] = 0.4f;
                        //translate a bit
                        translateMatrix[3]  = 0;
                        translateMatrix[7]  = 0.45f;
                        translateMatrix[11] = -0.05f;

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

                        RenderUtils.MatrixMultiply(4, 4, ref modelView, 4, 4, ref translateMatrix, ref temp_mv);

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

                        RenderUtils.MatrixMultiply(4, 4, ref projection, 4, 4, ref temp_mv, ref temp_mvp);

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

                        RenderUtils.MtxTranspose(ref temp_mvp,
                                                 ref mvpMatrix);

                        DrawIcon(ref mvpMatrix, currentStatus);
                    }
                    RenderUtils.CheckGLError();
                }
            }
        }
        void DrawIcon(ref float[] mvpMatrix, PikkartVideoPlayerBindingLibrary.PKTVIDEO_STATE status)
        {
            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);

            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");

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

            RenderUtils.CheckGLError();

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

            RenderUtils.CheckGLError();

            GL.ActiveTexture(TextureUnit.Texture0);

            switch ((long)status)
            {
            case 0:        //end
                GL.BindTexture(TextureTarget.Texture2D, mIconPlayTexture_GL_ID);
                break;

            case 1:        //pasued
                GL.BindTexture(TextureTarget.Texture2D, mIconPlayTexture_GL_ID);
                break;

            case 2:        //stopped
                GL.BindTexture(TextureTarget.Texture2D, mIconPlayTexture_GL_ID);
                break;

            case 3:        //playing
                GL.BindTexture(TextureTarget.Texture2D, mIconPlayTexture_GL_ID);
                break;

            case 4:        //ready
                GL.BindTexture(TextureTarget.Texture2D, mIconPlayTexture_GL_ID);
                break;

            case 5:        //not ready
                GL.BindTexture(TextureTarget.Texture2D, mIconBusyTexture_GL_ID);
                break;

            case 6:        //buffering
                GL.BindTexture(TextureTarget.Texture2D, mIconBusyTexture_GL_ID);
                break;

            case 7:        //error
                GL.BindTexture(TextureTarget.Texture2D, mIconErrorTexture_GL_ID);
                break;

            default:
                GL.BindTexture(TextureTarget.Texture2D, mIconBusyTexture_GL_ID);
                break;
            }
            RenderUtils.CheckGLError();

            GL.Uniform1(texSampler2DHandle, 0);

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

            RenderUtils.CheckGLError();

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


            RenderUtils.CheckGLError();
            GL.UseProgram(0);

            GL.Disable(EnableCap.Blend);
            GL.DisableVertexAttribArray(vertexHandle);
            GL.DisableVertexAttribArray(textureCoordHandle);

            RenderUtils.CheckGLError();
        }