PDA

View Full Version : Changing units/dimensions in OpenGL


Jesse Aldridge
08-21-2005, 05:18 PM
I'm moving from an SDL based to an OpenGL based system for rendering. With SDL, I had a system based on pixel dimensions. In order to make this transition as easy as possible, I'd like to get OpenGL set up in the same way, so that when sprites are drawn at their normal size (dimensions on screen = dimensions on file; unscaled) the left and right side of the view are 800 units apart and the top and bottom are 600 units apart.

Currently, it seems like sprites are drawn at there normal size when z = -15. This makes the left side of the screen -9.2 (or something like that). These numbers are rather unintuitive for me to work with, but I can't figure out how to change them. glOrtho seems like it may do what I want, but I think I may want to take advantage of some 3d functionality in the future, so I'd like to work with glPerspective if I can.

Aldacron
08-21-2005, 05:37 PM
change them. glOrtho seems like it may do what I want, but I think I may want to take advantage of some 3d functionality in the future, so I'd like to work with glPerspective if I can.

No matter what, you're going to have to change the projection to get the behavior you want for sprite rendering. Normal practice is to render the 3D scene first with perspective projection and then change to an orthographic projection and render 2D elements on top (such as GUI objects).

The following call will give you a coordinate system such that (0,0) is in the upperleft corner and each point corrolates with a pixel on the screen so that you can render sprites in the traditional 2D manner:

glOrtho(0, width, height, 0, -1.0, 1.0);

You can change it to get (0,0) in the bottom left corner:

glOrtho(0, width, 0, height, -1.0, 1.0);

If you need a z coordinate in your 2D scene, yyou can adjust the last two parameters (near and far planes) to your liking. Then you will have depth, but not perspective.