Jump to content
• Advertisement

Public Group

# OpenGL Projection Matrix

This topic is 3027 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. I'm trying to create my own projection matrix to avoid using gluPerspective() but, when I apply my matrix instead, my scene appears smaller. Is this normal behavior or should my matrix produce identical results? Here is my solution based on a number of reliable resources.
void buildProjectionMatrix(double fov, double aspectRatio, double zNear, double zFar, double* projectionMatrix) {
double xMax = zNear * tan(fov * PI / 360.0);
double xMin = -xMax;

double yMax = xMax / aspectRatio;
double yMin = xMin / aspectRatio;

for (int i = 0; i < 16; i++) {
projectionMatrix = 0.0;
}

projectionMatrix[0] = (2.0 * zNear) / (xMax - xMin);
projectionMatrix[5] = (2.0 * zNear) / (yMax - yMin);
projectionMatrix[8] = (xMax + xMin) / (xMax - xMin);
projectionMatrix[9] = (yMax + yMin) / (yMax - yMin);
projectionMatrix[10] = -1.0 * (zFar + zNear) / (zFar - zNear);
projectionMatrix[11] = -1.0;
projectionMatrix[14] = (-2.0 * zFar * zNear) / (zFar - zNear);
}


Results:
//gluPerspective's matrix
1.81066    0.0        0.0         0.0
0.0        2.41421    0.0         0.0
0.0        0.0        -1.00196    -1.0
0.0        0.0        -2.00196    0.0

//my matrix
2.41421    0.0        0.0         0.0
0.0        3.21895    0.0         0.0
0.0        0.0        -1.00196    -1.0
0.0        0.0        -2.00196    0.0


Thanks for reading. [Edited by - X Abstract X on April 3, 2010 2:15:29 AM]

#### Share this post

##### Share on other sites
Advertisement
It should look the same, I guess that you probably have an equation error.

Idea for a quick test:

Setup projection matrix with gluPerspective, then download the matrix with glGetFloatv(GL_PROJECTION_MATRIX, &matrix).

Compare these floats versus the matrix you calculate. Maybe you are only off by one number, or maybe they are all wrong, but should help your debugging.

#### Share this post

##### Share on other sites
Thanks, I never thought of comparing the matrices like that! I can't seem to figure out why but 2 of the indices in my matrix don't match up. If anyone has any suggestions, please throw them here.

//gluPerspective's matrix1.81066    0.0        0.0         0.00.0        2.41421    0.0         0.00.0        0.0        -1.00196    -1.00.0        0.0        -2.00196    0.0//my matrix2.41421    0.0        0.0         0.00.0        3.21895    0.0         0.00.0        0.0        -1.00196    -1.00.0        0.0        -2.00196    0.0

#### Share this post

##### Share on other sites
Have you considered just using the same equations as gluPerspective? They are described in the manuals gluPerspective

Just as a guess why you're getting different results, are you describing FOV in the x direction? gluPerspective takes an argument fovy which is fov in the Y direction. If you're supplying the same arguments to both that would explain the difference.

#### Share this post

##### Share on other sites
I'm using fov in Y-axis and I'm using the formula prescribed on PyOpenGL site, Microsoft's site and the OpenGL Red Book. I don't know what I'm doing wrong though.

#### Share this post

##### Share on other sites
If your fov is in the y direction, than why are you setting X = Z*tan(fov). Shouldn't it by Y = Z*tan(fov)?

xMax = zNear * tan(fov * PI / 360.0);

#### Share this post

##### Share on other sites
Quote:
 Original post by karwostsIf your fov is in the y direction, than why are you setting X = Z*tan(fov). Shouldn't it by Y = Z*tan(fov)?xMax = zNear * tan(fov * PI / 360.0);

Yes, you're right. Thanks for all your help.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
21
3. 3
JoeJ
18
4. 4
5. 5
• Advertisement

• 14
• 39
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631719
• Total Posts
3001883
×

## Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!