bool computeProjectionMatrix(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 = w * ar;
            }
            else
            {
                w1 = 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] = 1; angleMatrix[11] = 0;
            angleMatrix[12] = 0; angleMatrix[13] = 0; angleMatrix[14] = 0; angleMatrix[15] = 1;

            IntPtr tempPtr = IntPtr.Zero;

            GetCurrentProjectionMatrix(ref tempPtr);

            float[] projectionMatrix = new float[16];

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

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


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

            return(false);
        }
        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();
                }
            }
        }
        bool computeModelViewProjectionMatrix(ref float[] mvpMatrix)
        {
            float w = 640f, h = 480f;

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