Jump to content
  • Advertisement
Sign in to follow this  

Perspective Projection?

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

** Fixed **


I'm trying to draw a simple square in OpenGL and it's coming out a little skewed, but I've not been able figure out what's wrong.

The square vertices are simply:

Vertex1P vdata[6];
vdata[0].Position = Vec3(-0.5f, -0.5f, -2.5f); // Triangle 1.
vdata[1].Position = Vec3(-0.5f, 0.5f, -2.5f);
vdata[2].Position = Vec3( 0.5f, 0.5f, -2.5f);
vdata[3].Position = Vec3( 0.5f, -0.5f, -2.5f); // Triangle 2.
vdata[4].Position = Vec3(-0.5f, -0.5f, -2.5f);
vdata[5].Position = Vec3( 0.5f, 0.5f, -2.5f);

The view and model matrices are set to the identity, and I'm using the following to create a perspective projection matrix:

Mat4 perspective(float fov, float aspect, float zNear, float zFar)
float d = 1.0f / tanf(fov * PI / 180.0f / 2.0f);
float recip = 1.0f / (zNear - zFar);

Mat4 proj;

proj(0, 0) = d / aspect;
proj(1, 1) = d;
proj(2, 2) = (zNear + zFar) * recip;
proj(2, 3) = (2.0f * zNear * zFar) * recip;
proj(3, 2) = -1.0f;
proj(3, 3) = 0.0f;

return proj;

float aspect = 800.0f / 800.0f;
Mat4 proj = perspective(60.0f, aspect, 1.0f, 100.0f);

I'm expecting the square to be dead center in the view, but I seem to have got something wrong. Here's an image of what I'm seeing:


I think it must be to do with the projection matrix, but as far as I can tell it's correct. Can anyone see a problem with it?

Many thanks for any suggestions!

Cheers. Edited by Telios

Share this post

Link to post
Share on other sites
Fixed - I overlooked a silly problem in operator(), which looked like it was too trivial to be incorrect. :/

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.

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!