Perspective Woes

Started by
2 comments, last by medevilenemy 12 years ago
Hi, silly issue I'm having here, it's been around for a while but it was a low priority until now. In the past, with the perspective set up as follows:
gluPerspective(45, (double)screen_width/(double)screen_height, 0.01, 1.0);

objects would not show up square -- square objects were being drawn wider than they should have been. Fiddling with gluPerspective, I came up with:
gluPerspective(90, (double)screen_width/(double)screen_height, 0.01, 1.0);

Which draws things square, but is giving me all sorts of problems with depth (Its a 2d engine, but things can be drawn on one of a couple layers of depth, plus hud type overlays) which is giving me a particular problem with my overlays, also, things get distorted again when I move objects to different layers.

How do I set up my perspective (or if perspective isn't the correct approach, an alternative) so that I can have my depth, but without size distortion (I don't want scaling or resizing with depth), so square show up square and whatnot? It needs to use the appropriate screen aspect ratio as defined by screen_width/screen_height.

Thanks!
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.
Advertisement
I think glOrtho is the solution, but I'm not sure how to set it up properly. I want the center of the screen to be 0,0, top to be 1.0, bottom to be -1.0, with left and right appropriate scaled values relative to height, so that squares are square and whatnot.
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.
You can supply negative values into glOrtho, so if you want to go from -1 to 1, then you can call glOrtho(-1, 1, -1, 1, 0, 1); // the last two being the near/far planes.

You should also take aspect ratio into consideration, which will typically mean modifying left/right. something like:
float aspect = (float)width / (float)height
float height = 2 // going from -1 to 1!
float width = 2 * aspect
float left = width * -0.5f
float right = width * 0.5
Ah, many thanks, that seems to have done it (though I had to set near and far as 1, 0 to get the effect I wanted... bigger number = farther away). Nice for squares to actually be squares.
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.

This topic is closed to new replies.

Advertisement