Jump to content
  • Advertisement
Sign in to follow this  
doctorsixstring

Right-Hand Perspective Matrix

This topic is 4618 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

I'm trying to write a method (in Python) that returns a right-handed perspective matrix. Here is my current code:
def Perspective(fov,	# vertical field of view
	aspect,		# aspect ratio (width/height)
	near,		# distance of near Z-plane
	far):		# distance of far Z-plane

	f = 1.0 / math.tan( fov / 2)

	return [
		[f/aspect, 0.0, 0.0, 0.0],
		[0.0, f, 0.0, 0.0],
		[0.0, 0.0, far/(near-far), -1.0],
		[0.0, 0.0, (near*far)/(near-far), 0.0] ]

matrix = Perspective(3.14/4.0, 800.0/600.0, 1.0, 10.0)

Result of my code (rounded to 8 decimal points): [ 1.81168022, 0.0, 0.0, 0.0 ] [ 0.0, 2.41557363, 0.0, 0.0 ] [ 0.0, 0.0, -1.22222222, -2.22222222 ] [ 0.0, 0.0, -1.0, 0.0 ] My problem is that the this is not the same matrix created by Managed DirectX's Matrix.PerspectiveFovRH() method or with OpenGL's gluPerspective() method. Result of...     Matrix.PerspectiveFovRH(3.14f/4.0f, 800.0f/600.0f, 1.0f, 10.0f) [ 1.81168008, 0.0, 0.0, 0.0 ] [ 0.0, 2.41557336, 0.0, 0.0 ] [ 0.0, 0.0, -1.11111116, -1.0 ] [ 0.0, 0.0, -1.11111116, 0.0 ] Result of...     glMatrixMode(GL_PROJECTION)     glLoadIdentity()     gluPerspective(45.0, 800.0/600.0, 1.0, 10.0)     glGetDoublev(GL_PROJECTION_MATRIX) [ 1.81066012, 0.0, 0.0, 0.0 ] [ 0.0, 2.41421366, 0.0, 0.0 ] [ 0. 0.0, -1.22222221, -1.0 ] [ 0. 0.0, -2.22222233, 0.0 ] D3D Perspective FOV Right-Hand formula OpenGL's gluPerspective formula (according to PyOpenGL docs) As you can see, all three matrices are very similar, except for the values in the lower-right quadrant. Which matrix is correct? Why am I getting different results from each method? - Mike

Share this post


Link to post
Share on other sites
Advertisement
Yours appears to be exactly the same as the OpenGL version you posted, but transposed (swap row 1 <=> column 1 etc).

Share this post


Link to post
Share on other sites
I didn't look at your code, but I thought I'd mention that the D3D z clipping range is [0, 1], while in OpenGL it's [-1, 1]. This affects the elements of the matrix to which you're referring, and may have something to do with the results not being what you expect.

Share this post


Link to post
Share on other sites
Both the handedness and the clipping range determine what values are in those elements (the ones with the 'near' and 'far' terms). OpenGL uses a right-handed system with a clipping range of [-1, 1], and Direct3D uses a left-handed system with a clipping range of [0, 1], but for the other two combinations (right-handed with [0, 1] and left-handed with [-1, 1]) you might have to do the math yourself because I don't believe you'll find them in a standard reference. It's actually not all that bad.

Share this post


Link to post
Share on other sites
So if I swap the the values bakery2k1 mentioned, I should be good to go? Are the differences with the D3D matrix (-1.11111116, -1.11111116 vs. -1.22222221, -2.22222233) explained by the diffence in the Z-clipping ranges?

Share this post


Link to post
Share on other sites
Quote:
Original post by doctorsixstring
Are the differences with the D3D matrix (-1.11111116, -1.11111116 vs. -1.22222221, -2.22222233) explained by the diffence in the Z-clipping ranges?
Those values correspond to the z-clipping range, so yes, that is most likely the case.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!