Example #1
0
            public void OnDrawFrame(HeadTransform head, EyeParams leftEye, EyeParams rightEye)
            {
                mStereoRenderer.OnNewFrame(head);
                GLES20.GlEnable(GLES20.GlScissorTest);

                leftEye.getViewport().setGLViewport();
                leftEye.getViewport().setGLScissor();
                mStereoRenderer.OnDrawEye(leftEye.getTransform());

                if (rightEye == null)
                {
                    return;
                }

                rightEye.getViewport().setGLViewport();
                rightEye.getViewport().setGLScissor();
                mStereoRenderer.OnDrawEye(rightEye.getTransform());
            }
        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);
        }
Example #3
0
            public RendererHelper(CardboardView view, CardboardView.Renderer renderer)
            {
                mView          = view;
                mRenderer      = renderer;
                mHmd           = new HeadMountedDisplay(mView.mHmd);
                mHeadTransform = new HeadTransform();
                mMonocular     = new EyeParams(EyeParams.Eye.MONOCULAR);
                mLeftEye       = new EyeParams(EyeParams.Eye.LEFT);
                mRightEye      = new EyeParams(EyeParams.Eye.RIGHT);
                UpdateFieldOfView(mLeftEye.getFov(), mRightEye.getFov());
                mView.mDistortionRenderer = new DistortionRenderer();

                mLeftEyeTranslate  = new float[16];
                mRightEyeTranslate = new float[16];

                mVRMode = mView.mVRMode;
                mDistortionCorrectionEnabled = mView.mDistortionCorrectionEnabled;
                mDistortionCorrectionScale   = mView.mDistortionCorrectionScale;
                mZNear = mView.mZNear;
                mZFar  = mView.mZFar;

                mProjectionChanged = true;
            }
            public DistortionMesh(EyeParams eye, Distortion distortion, float screenWidthM, float screenHeightM, float xEyeOffsetMScreen, float yEyeOffsetMScreen, float textureWidthM, float textureHeightM, float xEyeOffsetMTexture, float yEyeOffsetMTexture, float viewportXMTexture, float viewportYMTexture, float viewportWidthMTexture, float viewportHeightMTexture)
            {
                float mPerUScreen  = screenWidthM;
                float mPerVScreen  = screenHeightM;
                float mPerUTexture = textureWidthM;
                float mPerVTexture = textureHeightM;

                float[] vertexData   = new float[8000];
                int     vertexOffset = 0;

                for (int row = 0; row < 40; row++)
                {
                    for (int col = 0; col < 40; col++)
                    {
                        float uTexture = col / 39.0F * (viewportWidthMTexture / textureWidthM) + viewportXMTexture / textureWidthM;

                        float vTexture = row / 39.0F * (viewportHeightMTexture / textureHeightM) + viewportYMTexture / textureHeightM;

                        float xTexture    = uTexture * mPerUTexture;
                        float yTexture    = vTexture * mPerVTexture;
                        float xTextureEye = xTexture - xEyeOffsetMTexture;
                        float yTextureEye = yTexture - yEyeOffsetMTexture;
                        float rTexture    = (float)Math.Sqrt(xTextureEye * xTextureEye + yTextureEye * yTextureEye);

                        float textureToScreen = rTexture > 0.0F ? distortion.distortInverse(rTexture) / rTexture : 1.0F;

                        float xScreen = xTextureEye * textureToScreen + xEyeOffsetMScreen;
                        float yScreen = yTextureEye * textureToScreen + yEyeOffsetMScreen;
                        float uScreen = xScreen / mPerUScreen;
                        float vScreen = yScreen / mPerVScreen;
                        float vignetteSizeMTexture = 0.002F / textureToScreen;

                        float dxTexture = xTexture - DistortionRenderer.clamp(xTexture, viewportXMTexture + vignetteSizeMTexture, viewportXMTexture + viewportWidthMTexture - vignetteSizeMTexture);

                        float dyTexture = yTexture - DistortionRenderer.clamp(yTexture, viewportYMTexture + vignetteSizeMTexture, viewportYMTexture + viewportHeightMTexture - vignetteSizeMTexture);

                        float drTexture = (float)Math.Sqrt(dxTexture * dxTexture + dyTexture * dyTexture);

                        float vignette = 1.0F - DistortionRenderer.clamp(drTexture / vignetteSizeMTexture, 0.0F, 1.0F);

                        vertexData[(vertexOffset + 0)] = (2.0F * uScreen - 1.0F);
                        vertexData[(vertexOffset + 1)] = (2.0F * vScreen - 1.0F);
                        vertexData[(vertexOffset + 2)] = vignette;
                        vertexData[(vertexOffset + 3)] = uTexture;
                        vertexData[(vertexOffset + 4)] = vTexture;

                        vertexOffset += 5;
                    }
                }

                nIndices = 3158;
                int[] indexData   = new int[nIndices];
                int   indexOffset = 0;

                vertexOffset = 0;
                for (int row = 0; row < 39; row++)
                {
                    if (row > 0)
                    {
                        indexData[indexOffset] = indexData[(indexOffset - 1)];
                        indexOffset++;
                    }
                    for (int col = 0; col < 40; col++)
                    {
                        if (col > 0)
                        {
                            if (row % 2 == 0)
                            {
                                vertexOffset++;
                            }
                            else
                            {
                                vertexOffset--;
                            }
                        }
                        indexData[(indexOffset++)] = vertexOffset;
                        indexData[(indexOffset++)] = (vertexOffset + 40);
                    }
                    vertexOffset += 40;
                }

                FloatBuffer vertexBuffer = ByteBuffer.AllocateDirect(vertexData.Length * 4).Order(ByteOrder.NativeOrder()).AsFloatBuffer();

                vertexBuffer.Put(vertexData).Position(0);

                IntBuffer indexBuffer = ByteBuffer.AllocateDirect(indexData.Length * 4).Order(ByteOrder.NativeOrder()).AsIntBuffer();

                indexBuffer.Put(indexData).Position(0);

                int[] bufferIds = new int[2];
                GLES20.GlGenBuffers(2, bufferIds, 0);
                mArrayBufferId   = bufferIds[0];
                mElementBufferId = bufferIds[1];

                GLES20.GlBindBuffer(34962, mArrayBufferId);
                GLES20.GlBufferData(34962, vertexData.Length * 4, vertexBuffer, 35044);

                GLES20.GlBindBuffer(34963, mElementBufferId);
                GLES20.GlBufferData(34963, indexData.Length * 4, indexBuffer, 35044);

                GLES20.GlBindBuffer(34962, 0);
                GLES20.GlBindBuffer(34963, 0);
            }
 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));
 }