private EyeViewport initViewportForEye(EyeParams eye, float xOffsetM)
        {
            ScreenParams          screen = mHmd.getScreen();
            CardboardDeviceParams cdp    = mHmd.getCardboard();

            float eyeToScreenDistanceM = cdp.getEyeToLensDistance() + cdp.getScreenToLensDistance();
            float leftM = (float)Math.Tan(Math.ToRadians(eye.getFov().getLeft())) * eyeToScreenDistanceM;

            float rightM = (float)Math.Tan(Math.ToRadians(eye.getFov().getRight())) * eyeToScreenDistanceM;

            float bottomM = (float)Math.Tan(Math.ToRadians(eye.getFov().getBottom())) * eyeToScreenDistanceM;

            float topM = (float)Math.Tan(Math.ToRadians(eye.getFov().getTop())) * eyeToScreenDistanceM;

            EyeViewport vp = new EyeViewport();

            vp.x      = xOffsetM;
            vp.y      = 0.0F;
            vp.width  = (leftM + rightM);
            vp.height = (bottomM + topM);
            vp.eyeX   = (leftM + xOffsetM);
            vp.eyeY   = bottomM;

            float xPxPerM = screen.getWidth() / screen.getWidthMeters();
            float yPxPerM = screen.getHeight() / screen.getHeightMeters();

            eye.getViewport().x      = Math.Round(vp.x * xPxPerM);
            eye.getViewport().y      = Math.Round(vp.y * xPxPerM);
            eye.getViewport().width  = Math.Round(vp.width * xPxPerM);
            eye.getViewport().height = Math.Round(vp.height * xPxPerM);

            return(vp);
        }
        public void onProjectionChanged(HeadMountedDisplay hmd, EyeParams leftEye, EyeParams rightEye, float zNear, float zFar)
        {
            mHmd         = new HeadMountedDisplay(hmd);
            mLeftEyeFov  = new FieldOfView(leftEye.getFov());
            mRightEyeFov = new FieldOfView(rightEye.getFov());

            ScreenParams          screen = mHmd.getScreen();
            CardboardDeviceParams cdp    = mHmd.getCardboard();

            if (mProgramHolder == null)
            {
                mProgramHolder = createProgramHolder();
            }

            EyeViewport leftEyeViewport  = initViewportForEye(leftEye, 0.0F);
            EyeViewport rightEyeViewport = initViewportForEye(rightEye, leftEyeViewport.width);

            leftEye.getFov().toPerspectiveMatrix(zNear, zFar, leftEye.getTransform().GetPerspective(), 0);

            rightEye.getFov().toPerspectiveMatrix(zNear, zFar, rightEye.getTransform().GetPerspective(), 0);

            float textureWidthM   = leftEyeViewport.width + rightEyeViewport.width;
            float textureHeightM  = Math.Max(leftEyeViewport.height, rightEyeViewport.height);
            float xPxPerM         = screen.getWidth() / screen.getWidthMeters();
            float yPxPerM         = screen.getHeight() / screen.getHeightMeters();
            int   textureWidthPx  = Math.Round(textureWidthM * xPxPerM);
            int   textureHeightPx = Math.Round(textureHeightM * yPxPerM);

            float xEyeOffsetMScreen = screen.getWidthMeters() / 2.0F - cdp.getInterpupillaryDistance() / 2.0F;
            float yEyeOffsetMScreen = cdp.getVerticalDistanceToLensCenter() - screen.getBorderSizeMeters();

            mLeftEyeDistortionMesh = createDistortionMesh(leftEye, leftEyeViewport, textureWidthM, textureHeightM, xEyeOffsetMScreen, yEyeOffsetMScreen);

            xEyeOffsetMScreen       = screen.getWidthMeters() - xEyeOffsetMScreen;
            mRightEyeDistortionMesh = createDistortionMesh(rightEye, rightEyeViewport, textureWidthM, textureHeightM, xEyeOffsetMScreen, yEyeOffsetMScreen);

            setupRenderTextureAndRenderbuffer(textureWidthPx, textureHeightPx);
        }
 private DistortionMesh createDistortionMesh(EyeParams eye, EyeViewport eyeViewport, float textureWidthM, float textureHeightM, float xEyeOffsetMScreen, float yEyeOffsetMScreen)
 {
     return(new DistortionMesh(eye, mHmd.getCardboard().getDistortion(), mHmd.getScreen().getWidthMeters(), mHmd.getScreen().getHeightMeters(), xEyeOffsetMScreen, yEyeOffsetMScreen, textureWidthM, textureHeightM, eyeViewport.eyeX, eyeViewport.eyeY, eyeViewport.x, eyeViewport.y, eyeViewport.width, eyeViewport.height));
 }