Example #1
0
        private static void SetupCameraMatrices(MyRenderMessageSetCameraViewMatrix message)
        {
            var viewMatrixAt0 = message.ViewMatrix;

            viewMatrixAt0.M14 = 0;
            viewMatrixAt0.M24 = 0;
            viewMatrixAt0.M34 = 0;
            viewMatrixAt0.M41 = 0;
            viewMatrixAt0.M42 = 0;
            viewMatrixAt0.M43 = 0;
            viewMatrixAt0.M44 = 1;

            var originalProjection = message.ProjectionMatrix;
            //var invOriginalProjection = Matrix.CreatePerspectiveFovRhInverse(message.FOV, MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y, message.NearPlane, message.FarPlane);
            var renderProjection = Matrix.CreatePerspectiveFieldOfView(message.FOV, MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y, message.FarPlane, message.NearPlane);
            var invProj          = Matrix.CreatePerspectiveFovRhInverse(message.FOV, MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y, message.FarPlane, message.NearPlane);

            renderProjection = Matrix.CreatePerspectiveFovRhInfiniteComplementary(message.FOV, MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y, message.NearPlane);
            invProj          = Matrix.CreatePerspectiveFovRhInfiniteComplementaryInverse(message.FOV, MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y, message.NearPlane);

            var invView = Matrix.Transpose(viewMatrixAt0);

            invView.M41 = (float)message.CameraPosition.X;
            invView.M42 = (float)message.CameraPosition.Y;
            invView.M43 = (float)message.CameraPosition.Z;

            MyEnvironment.ViewAt0              = viewMatrixAt0;
            MyEnvironment.InvViewAt0           = Matrix.Transpose(viewMatrixAt0);
            MyEnvironment.ViewProjectionAt0    = viewMatrixAt0 * renderProjection;
            MyEnvironment.InvViewProjectionAt0 = invProj * Matrix.Transpose(viewMatrixAt0);
            message.CameraPosition.AssertIsValid();
            MyEnvironment.CameraPosition      = message.CameraPosition;
            MyEnvironment.View                = message.ViewMatrix;
            MyEnvironment.ViewD               = message.ViewMatrix;
            MyEnvironment.OriginalProjectionD = originalProjection;
            MyEnvironment.InvView             = invView;
            MyEnvironment.ViewProjection      = message.ViewMatrix * renderProjection;
            MyEnvironment.InvViewProjection   = invProj * invView;
            MyEnvironment.Projection          = renderProjection;
            MyEnvironment.InvProjection       = invProj;

            MyEnvironment.ViewProjectionD = MyEnvironment.ViewD * (MatrixD)renderProjection;

            MyEnvironment.NearClipping             = message.NearPlane;
            MyEnvironment.FarClipping              = message.FarPlane;
            MyEnvironment.LargeDistanceFarClipping = message.FarPlane * 500.0f;
            MyEnvironment.FovY = message.FOV;

            MyUtils.Init(ref MyEnvironment.ViewFrustumD);
            MyEnvironment.ViewFrustumD.Matrix = MyEnvironment.ViewProjectionD;

            MyUtils.Init(ref MyEnvironment.ViewFrustumClippedD);
            MyEnvironment.ViewFrustumClippedD.Matrix = MyEnvironment.ViewD * MyEnvironment.OriginalProjectionD;
        }
Example #2
0
        private static void SetupCameraMatricesInternal(MyRenderMessageSetCameraViewMatrix message, MyEnvironmentMatrices envMatrices, MyStereoRegion typeofEnv)
        {//uses m_leftEye to handle HMD images
            var viewMatrix     = message.ViewMatrix;
            var cameraPosition = message.CameraPosition;

            if (MyStereoRender.Enable)
            {
                if (MyOpenVR.Static != null && message.LastMomentUpdateIndex != 0)
                {
                    MatrixD origin = MatrixD.Identity;
                    MyOpenVR.LMUMatrixGetOrigin(ref origin, message.LastMomentUpdateIndex);
                    viewMatrix = MatrixD.Invert(origin);
                }
            }

            var viewMatrixAt0 = viewMatrix;

            viewMatrixAt0.M14 = 0;
            viewMatrixAt0.M24 = 0;
            viewMatrixAt0.M34 = 0;
            viewMatrixAt0.M41 = 0;
            viewMatrixAt0.M42 = 0;
            viewMatrixAt0.M43 = 0;
            viewMatrixAt0.M44 = 1;

            if (MyStereoRender.Enable)
            {
                if (MyOpenVR.Static != null)
                {
                    if (message.LastMomentUpdateIndex != 0)
                    {
                        var tViewMatrix = Matrix.Transpose(viewMatrix);
                        var viewHMDat0  = MyOpenVR.ViewHMD;
                        viewHMDat0.M14 = 0;
                        viewHMDat0.M24 = 0;
                        viewHMDat0.M34 = 0;
                        viewHMDat0.M41 = 0;
                        viewHMDat0.M42 = 0;
                        viewHMDat0.M43 = 0;
                        viewHMDat0.M44 = 1;

                        //cameraPosition += tViewMatrix.Up * MyOpenVR.ViewHMD.Translation.Y;
                        //cameraPosition += tViewMatrix.Backward * MyOpenVR.ViewHMD.Translation.X;
                        //cameraPosition += tViewMatrix.Right * MyOpenVR.ViewHMD.Translation.Z;

                        viewMatrixAt0 = viewMatrixAt0 * viewHMDat0;
                        viewMatrix    = viewMatrix * viewHMDat0;

                        if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.LEFT)
                        {
                            viewMatrixAt0 = GetMatrixEyeTranslation(true, viewMatrixAt0) * viewMatrixAt0;
                            viewMatrix    = GetMatrixEyeTranslation(true, viewMatrix) * viewMatrix;
                        }
                        else if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.RIGHT)
                        {
                            viewMatrixAt0 = GetMatrixEyeTranslation(false, viewMatrixAt0) * viewMatrixAt0;
                            viewMatrix    = GetMatrixEyeTranslation(false, viewMatrix) * viewMatrix;
                        }
                    }
                }
                else
                {
                    if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.LEFT)
                    {
                        viewMatrixAt0 = GetMatrixEyeTranslation(true, viewMatrixAt0) * viewMatrixAt0;
                        viewMatrix    = GetMatrixEyeTranslation(true, viewMatrix) * viewMatrix;
                    }
                    else if (!MyOpenVR.Debug2DImage && typeofEnv == MyStereoRegion.RIGHT)
                    {
                        viewMatrixAt0 = GetMatrixEyeTranslation(false, viewMatrixAt0) * viewMatrixAt0;
                        viewMatrix    = GetMatrixEyeTranslation(false, viewMatrix) * viewMatrix;
                    }
                }
            }

            var originalProjection = message.ProjectionMatrix;
            //var invOriginalProjection = Matrix.CreatePerspectiveFovRhInverse(message.FOV, MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y, message.NearPlane, message.FarPlane);

            float aspectRatio = MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y;

            if (typeofEnv != MyStereoRegion.FULLSCREEN)
            {
                aspectRatio /= 2;
            }
            var renderProjection = Matrix.CreatePerspectiveFieldOfView(message.FOV, aspectRatio, message.FarPlane, message.NearPlane);
            var invProj          = Matrix.CreatePerspectiveFovRhInverse(message.FOV, aspectRatio, message.FarPlane, message.NearPlane);

            renderProjection = Matrix.CreatePerspectiveFovRhInfiniteComplementary(message.FOV, aspectRatio, message.NearPlane);
            invProj          = Matrix.CreatePerspectiveFovRhInfiniteComplementaryInverse(message.FOV, aspectRatio, message.NearPlane);

            var invView = Matrix.Transpose(viewMatrixAt0);

            invView.M41 = (float)cameraPosition.X;
            invView.M42 = (float)cameraPosition.Y;
            invView.M43 = (float)cameraPosition.Z;

            envMatrices.ViewAt0              = viewMatrixAt0;
            envMatrices.InvViewAt0           = Matrix.Transpose(viewMatrixAt0);
            envMatrices.ViewProjectionAt0    = viewMatrixAt0 * renderProjection;
            envMatrices.InvViewProjectionAt0 = invProj * Matrix.Transpose(viewMatrixAt0);
            cameraPosition.AssertIsValid();
            envMatrices.CameraPosition      = cameraPosition;
            envMatrices.View                = viewMatrix;
            envMatrices.ViewD               = viewMatrix;
            envMatrices.OriginalProjectionD = originalProjection;
            envMatrices.InvView             = invView;
            envMatrices.ViewProjection      = viewMatrix * renderProjection;
            envMatrices.InvViewProjection   = invProj * invView;
            envMatrices.Projection          = renderProjection;
            envMatrices.InvProjection       = invProj;

            envMatrices.ViewProjectionD = envMatrices.ViewD * (MatrixD)renderProjection;

            envMatrices.NearClipping             = message.NearPlane;
            envMatrices.FarClipping              = message.FarPlane;
            envMatrices.LargeDistanceFarClipping = message.FarPlane * 500.0f;
            envMatrices.FovY = message.FOV;

            MyUtils.Init(ref envMatrices.ViewFrustumD);
            envMatrices.ViewFrustumD.Matrix = envMatrices.ViewProjectionD;

            MyUtils.Init(ref envMatrices.ViewFrustumClippedD);
            envMatrices.ViewFrustumClippedD.Matrix = envMatrices.ViewD * envMatrices.OriginalProjectionD;
        }