Example #1
0
        internal static Matrix PrepareLocalEnvironmentMatrix(Vector3 pos, Vector2I resolution, int faceId, float farPlane)
        {
            var    projection = Matrix.CreatePerspectiveFieldOfView((float)Math.PI * 0.5f, 1, 0.5f, farPlane);
            Matrix viewMatrix = CubeFaceViewMatrix(pos, faceId);

            return(viewMatrix * projection);
        }
Example #2
0
 /// <summary>
 /// Changes FOV for ForwardCamera (updates projection matrix)
 /// SetViewMatrix overwrites this changes
 /// </summary>
 /// <param name="fov"></param>
 public static void ChangeFov(float fov)
 {
     //  Projection matrix according to zoom level
     ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(fov, AspectRatio,
                                                            GetSafeNear(),
                                                            MyRenderCamera.FAR_PLANE_DISTANCE);
 }
Example #3
0
 /// <summary>
 /// Changes FOV for ForwardCamera (updates projection matrix)
 /// SetViewMatrix overwrites this changes
 /// </summary>
 /// <param name="fov"></param>
 public void ChangeFov(float fov)
 {
     //  Projection matrix according to zoom level
     ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(fov, ForwardAspectRatio,
                                                            GetSafeNear(),
                                                            FarPlaneDistance);
 }
Example #4
0
 public static void ChangeClipPlanes(float near, float far, bool applyNow = false)
 {
     Debug.Assert(!m_backupMatrix.HasValue, "Reset clip planes before changing clip planes again");
     m_backupMatrix   = ProjectionMatrix;
     ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(FieldOfView, AspectRatio, near, far);
     if (applyNow)
     {
         UpdateCamera();
     }
 }
Example #5
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 #6
0
        public static void UpdateCamera()
        {
            ViewMatrix           = m_viewMatrix;
            ViewProjectionMatrix = ViewMatrix * ProjectionMatrix;

            UpdateVectors();
            UpdateBoundingFrustum();

            ViewMatrixAtZero = Matrix.CreateLookAt(Vector3.Zero, ForwardVector, UpVector);

            ViewProjectionMatrixAtZero = ViewMatrixAtZero * ProjectionMatrix;

            CornerFrustum = CalculateCornerFrustum();

            ProjectionMatrixForFarObjects = Matrix.CreatePerspectiveFieldOfView(FieldOfView, AspectRatio, NEAR_PLANE_FOR_BACKGROUND, FAR_PLANE_FOR_BACKGROUND);
        }
Example #7
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.CreatePerspectiveFovInv(message.FOV, MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y, message.NearPlane, message.FarPlane);
            var complementaryProjection = Matrix.CreatePerspectiveFieldOfView(message.FOV, MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y, message.FarPlane, message.NearPlane);
            var invProj = Matrix.CreatePerspectiveFovInv(message.FOV, MyRender11.ResolutionF.X / MyRender11.ResolutionF.Y, message.FarPlane, 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 * complementaryProjection;
            MyEnvironment.InvViewProjectionAt0 = invProj * Matrix.Transpose(viewMatrixAt0);
            message.CameraPosition.AssertIsValid();
            MyEnvironment.CameraPosition    = message.CameraPosition;
            MyEnvironment.View              = message.ViewMatrix;
            MyEnvironment.InvView           = invView;
            MyEnvironment.ViewProjection    = message.ViewMatrix * complementaryProjection;
            MyEnvironment.InvViewProjection = invProj * invView;
            MyEnvironment.Projection        = complementaryProjection;
            MyEnvironment.InvProjection     = invProj;

            MyEnvironment.NearClipping = message.NearPlane;
            MyEnvironment.FarClipping  = message.FarPlane;
            MyEnvironment.FovY         = message.FOV;
            MyEnvironment.ViewFrustum  = new BoundingFrustum(MyEnvironment.ViewProjection);
        }
Example #8
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;
        }