PDA

View Full Version : Converting over from Ortho to Frustum



Fezziwig
03-18-2012, 07:40 AM
I have always used Ortho projection for my OpenGL programs. But half-way through my current project I have realised that I should instead be using Perspective(Frustrum) projection.

My current reshape function is as follows:

void Reshape(void)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, WIN_WIDTH, WIN_HEIGHT);
glOrtho(0, WIN_WIDTH, WIN_HEIGHT, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

I tried swapping the word Ortho to Frustum and a few other ways but none work.

What do I need to do to change it to Perspective projection. Is it just a matter of chopping and changing a few lines or do I have to change the way I draw object?

Thanks,
Rowan.

shawn619
03-18-2012, 07:42 AM
Replace glOrtho() w/ "gluPerspective(30.0, WIN_WIDTH / WIN_HEIGHT, 1.0, 2000.0);"

Fezziwig
03-18-2012, 11:00 AM
I just get a black screen :(. Do i have to draw objects differently. At the moment they are drawn like this for example



glPushMatrix();
glDisable( GL_TEXTURE_2D );
glBegin(GL_LINES);
glColor4f(0.7, 0.7, 0.5, 1);
glVertex3d(x,y, 0);
glColor4f(0,0,0,0);
glVertex3d(prevx,prevy, 0);
glColor4f(1, 1, 1, 1);
glEnd();
glPopMatrix();

Alfonse Reinheart
03-18-2012, 11:32 AM
Why do you think you should be using a perspective projection, when you're clearly doing 2D line drawing?

Fezziwig
03-18-2012, 11:35 AM
this is just part of the code that would be used for part of the UI overlay. I guess that was a bad example. Because I'm making a top down shooter, I need the camera to follow the player and the UI overlay to remain on the same position on the screen.
Would I not use perspective projection for this?

Alfonse Reinheart
03-18-2012, 02:02 PM
Would I not use perspective projection for this?

No.

A perspective projection is for providing, well, perspective (http://en.wikipedia.org/wiki/Perspective_%28graphical%29). What you want is a camera, which is a different thing.

For a top-down 2D game, all you want is to provide a simple translation offset (possibly a rotation if you want to rotate the view). Just stick that in the MODELVIEW matrix before putting any other transforms into it.

Fezziwig
03-18-2012, 02:16 PM
Thanks, Alfonse.

As you can probobly tell I'm quite new to OpenGL and programming so advice like this really helps