Sign in to follow this  

C# - Sharpdx - Building Orthographic Transform

This topic is 408 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

HI All,

 

I'm currently walking through a tutorial on Richardsoftware.net about shadow mapping ( http://richardssoftware.net/Home/Post/37).

 

I've already have my depthbuffer/Shaderresourceview code working, so I know that I am writing correctly to a shader buffer.  

 

When I apply my normal Projection Matrix everything renders fine, when I try to do an orthographic projection I'm coming up 0.  Now to tell the truth, I've adapted the code and I know the theory to a degree.  But I think i'm executing incorrectly.  Below, i've attached the code for my matrix generation which I believe is incorrectly generated (fairly obvious that this is where the problem should be).  I've moved from Richards Left hand coordinate tutorial to Right hand (which is what my code is based off).

 

I'm not as familiar with the bounding sphere usage, but I do believe I should be setting the centre to the centre of the scene.

 

My updatecamera function is called only at the point of time I am pulling the matrices from the containing object.  So its called Just in time.  This works for projection fine, but as mentioned, ortho is failing.   Matrix math has always been a nightmare to me, im hoping someone can suggest some directions so I can learn more.

 

Very grateful if anyone can throw some ideas on table.

     void updateCamera()
        {
            m_changed = false;

            m_worldMatrix = Matrix.RotationYawPitchRoll(0.0f, 0.0f, 0.0f);

            m_viewMatrix = Matrix.LookAtRH(m_position, m_targetPosition, m_cameraUp);

            if (m_displayMode == ProjectionMode.PROJECTION)
            {
                m_projectionMatrix = Matrix.PerspectiveFovRH(fov, m_viewport.AspectRatio, 0.1f, c_maxDepth);
                m_wvp = m_worldMatrix * m_viewMatrix * m_projectionMatrix;
            }
            else if (m_displayMode == ProjectionMode.SHADOW)
            {
                BuildShadowTransform();
            }
            else
            { 
                m_projectionMatrix = Matrix.OrthoRH(m_viewport.Width, m_viewport.Height, 0.1f, c_maxDepth);
                m_wvp = m_projectionMatrix;
            }

            generateFrustumPlanes();       
        }

        private void BuildShadowTransform()
        {

            BoundingSphere _sceneBounds = new BoundingSphere(new Vector3(), (float) Math.Sqrt(10 * 10 + 15 * 15));
            _sceneBounds.Center = Lookat;

            var lightPos = -2.0f * _sceneBounds.Radius * Direction;
            var targetPos = _sceneBounds.Center;


            Vector3 sphereCenterLS = Vector3.TransformCoordinate(targetPos, m_viewMatrix);

            var l = sphereCenterLS.X - _sceneBounds.Radius;
            var b = sphereCenterLS.Y - _sceneBounds.Radius;
            var n = sphereCenterLS.Z - _sceneBounds.Radius;
            var r = sphereCenterLS.X + _sceneBounds.Radius;
            var t = sphereCenterLS.Y + _sceneBounds.Radius;
            var f = sphereCenterLS.Z + _sceneBounds.Radius;

            m_projectionMatrix = Matrix.OrthoOffCenterRH(l, r, b, t, n, f);
            var T = new Matrix
            {
                M11 = 0.5f,
                M22 = -0.5f,
                M33 = 1.0f,
                M41 = 0.5f,
                M42 = 0.5f,
                M44 = 1.0f
            };

            m_inverseMatrix = m_viewMatrix * m_projectionMatrix * T;
            m_wvp = m_worldMatrix * m_viewMatrix * m_projectionMatrix;
        }

Share this post


Link to post
Share on other sites

Follow up, the issue is around my frustrum culling, I will investigate but I believe the current tests are eliminating all my AABB tests.

 

I believe my frustrum being generated is incorrect for the Orthographic project I am attempting.

 

This works for Projection, but not my Orthographic.

        public void generateFrustumPlanes()
        {
            if (m_changed)
            {
                updateCamera();
            }

            if (m_disableFrustrumUpdates)
                return;

            float zMinimum, r;
            Matrix matrix;

            //m_planes.Clear();
            Matrix projectionMatrix = m_projectionMatrix;
            Matrix viewMatrix = m_viewMatrix;

            // Calculate the minimum Z distance in the frustum.
            zMinimum = -projectionMatrix.M43 / projectionMatrix.M33;
            r = c_maxDepth / (c_maxDepth - zMinimum);
            projectionMatrix.M33 = r;
            projectionMatrix.M43 = -r * zMinimum;

            // Create the frustum matrix from the view matrix and updated projection matrix.
            matrix = viewMatrix * projectionMatrix;

            m_planes[0] = new Vector4(matrix.M14 + matrix.M13,
                matrix.M24 + matrix.M23,
                matrix.M34 + matrix.M33,
                matrix.M44 + matrix.M43); // near
            m_planes[1] = new Vector4(matrix.M14 - matrix.M13,
                matrix.M24 - matrix.M23,
                matrix.M34 - matrix.M33,
                matrix.M44 - matrix.M43);

            m_planes[2] = new Vector4(matrix.M14 + matrix.M11,
                matrix.M24 + matrix.M21,
                matrix.M34 + matrix.M31,
                matrix.M44 + matrix.M41); // left
            m_planes[3] = new Vector4(matrix.M14 - matrix.M11,
                matrix.M24 - matrix.M21,
                matrix.M34 - matrix.M31,
                matrix.M44 - matrix.M41);//right

            m_planes[4] = new Vector4(matrix.M14 - matrix.M12,
                matrix.M24 - matrix.M22,
                matrix.M34 - matrix.M32,
                matrix.M44 - matrix.M42); // top
            m_planes[5]  = new Vector4(matrix.M14 + matrix.M12, matrix.M24 + matrix.M22, matrix.M34 + matrix.M32, matrix.M44 + matrix.M42); // bottom

            for (int i = 0; i < s_frustrumPlanes; i++)
            {
                m_planes[i].Normalize();
            }          
        }

Share this post


Link to post
Share on other sites

This topic is 408 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this