Jump to content
• ### What is your GameDev Story?

• Advertisement

# OpenGL Perspective Projection Matrix Confusion!

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

Hello all!! :) All this time, I was using functions like gluPerspective() and D3DXMatrixPerspectiveFovLH() to create my Perspective projection matrix for my camera. Currently I am working on a 3D Engine that is going to support both 3D APIs (D3D10 and OpenGL, Currently only D3D10 Renderer is working). I want to create a "cross-3DAPI" camera and I want to create the perspective projection matrix manually. The thing is that I am very confused, I have looked the source code of various Open Source 3D Engines and each one calculates the Perspective projection matrix differently!
Lets say this is the matrix structure:

A       0       0       0
0       B       0       0
0       0       C       D
0       0       E       0

These is the different matrices I found:

A = cot(fovy / 2) / Aspect Ratio
B = cot(fovy / 2)
C = (zFar + zNear) / (zNear - zFar)
D = (2 * zFar * zNear) / (zNear - zFar)
E = -1

A = cot(fovy / 2)
B = cot(fovy / 2) * Aspect Ratio
C = (zFar + zNear) / (zFar - zNear)
D = -(2 * zFar * zNear) / (zFar - zNear)
E = 1

A = cot(fovy / 2) / Aspect Ratio
B = cot(fovy / 2)
C = zFar / (zFar - zNear)
D = -(zFar * zNear) / (zFar - zNear)
E = 1

If I keep searching I am sure I will find more perspective projection matrices calculations that are different from these three! Which of these is correct? What are the differences between a D3D10 perspective projection matrix and an OpenGL perspective Projection matrix? What I need to do to convert from one to the other? I am really confused! :/ Thanks for your time!!!

#### Share this post

##### Share on other sites
Advertisement
Considering that you are planning to design an API-agnostic library, there are several issues to consider. These can be the source of your confusion:

1) Matrix memory layout: Row major vs. Column major
How are matrix elements stored in memory? Are they stored row by row or column by column? You can find more information here.

The moral of the story: choose one and stick with it!

2) Order of matrix multiplications:
Are matrices pre-multiplied (i.e. the OpenGL way) or post-multiplied(i.e. the Direct3D way) by vectors/matrices. Please note that the order of matrix multiplications has nothing to do with its memory layout! It's possible to write a matrix class that stores matrices in column major order but enforces a post-multiplication ordering. For instance, Wild Magic and Ogre both store matrices in row major order but enforce a pre-multiplication ordering.

The moral of the story: choose one and stick with it!

3) Coordinate system handedness: Right handed vs. Left handed coordinate systems

The moral of the story: choose on and stick with it!

4) OpenGL expects the camera to look down the negative Z-axis. Direct3D expects the camera to look down the positive Z-axis.

The moral of the story: choose one and stick with it!

5) OpenGL maps the view frustum into a unit cube ([-1,1] x [-1,1] x [-1,1]) centered at the origin. Direct3D maps the view frustum to [-1,1]x[-1,1]x[0,1].

The moral of the story: choose one and stick with it!

The moral of this post: It's all about conventions :)

#### Share this post

##### Share on other sites
Well first of all I can only agree with Ashkan. It's all about conventions. But it's so easy to **** it up when just getting into 3D.

This webpage might help you understand projection matrices and why they look as they do. http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123/

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement
• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• ### Popular Now

• 12
• 9
• 9
• 34
• 16
• Advertisement
• ### Forum Statistics

• Total Topics
634123
• Total Posts
3015657
×

## 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!