Sign in to follow this  
beebs1

Perspective Projection?

Recommended Posts

** Fixed **


Hiya,

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:
[code]
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);
[/code]

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

[code]
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.identity();

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);
[/code]

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:

[img]http://www.jamesbb.co.uk/screen.png[/img]

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 [i]too [/i]trivial to be incorrect. :/

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this