Burkey's Blog

Random musings of a PhD geek on video games, technology and music.

  • About
  • Contact
  • Game Completion List
  • Music Blog
  • Portfolio & CV
  • August 5, 2009 @ 9:49 am

    XNA Code Snippet: Setting and Getting the Near & Far Clipping Planes from a Projection Matrix

    It may be the case in your XNA code that you’re given a 3D projection matrix from a library method but its near and far clipping planes aren’t set as you require and so you’ll need to alter them.  I had to do just such a thing yesterday and it took me a short while to figure out how to do this, so I thought I’d post the code up here for anyone who needs it.  You’ll can then alter the code as you require it.

    Setting the clipping planes:

    public Matrix SetMatrixClippingPlanes(Matrix m, float near, float far)
    {
         m.M33 = far / (near - far);
         m.M43 = near * far / (near - far);
    
         return m;
    }

    Retrieving and printing the clipping planes:

    public void PrintClippingPlanes(Matrix m)
    {
         float near = m.M43 / m.M33;
         float far = (near * m.M33) / (m.M33 + 1);
    
         Console.WriteLine("Near Clipping Plane: " + near);
         Console.WriteLine("Far Clipping Plane: " + far);
    }

    Hopefully this helps some of you out and saves a bit of time.

    Filed under Geek

    Leave a Comment

    Archives

    Categories

    Recently Played Tracks


    Twitter  Last.fm  Facebook  Flickr  LinkedIn  SlideShare