Unit Hell!

I’m having a real hard time trying to figure out how units work in this game. What defines the distance from origin? Is it based on the type of projection you’re using? (i.e. What makes 1.0 to the right of the origin look like an inch or cm on your screen?) Does it have anything to do with the size of the GL window? I’ve been hardcoding the position of my objects up to now (insert laugh). And would really like to understand how GL uses/defines coordinates. Anyways I would love any help.

Mighty

How OpenGL rasterizes verteces (coverts to 2D) does indeed depend on projection.

If your using a 3D projection, positioning things such as health bars will be nigh on impossible.

If you use glOrtho( … ) you can set up OpenGL for 2D rendering with a given scale. You can use OpenGL in 2D and 3D modes at the same time.

Note: Even in “2D” mode, OpenGL is still in 3D mode, only the projection matrix doesn’t add any perspective to the coords

First openGL units are not inches, meters or miles.

OpenGL units are relative, 1 openGL unit can be = to a inch, mile or meters, etc.

ALso openGL units are relative to how you have setup your projection.

Once you learn more about projection and how it relates to unit size, the more it will help with objects size and location.

[This message has been edited by nexusone (edited 02-04-2004).]

Your question is badly phrased, so let’s rephrase it:

When you place an object in the world, what governs where it will appear on your screen and how big it is in pixels?

Your first mistake, talking in inches or cm of screen, obviously does not take into account different sized screens of the same resolution. I will leave that one, as generally, the application doesn’t care about such things. I think you mean, transforming your object in physical space into screen pixels, and letting the user decide how big a monitor they want to buy.

Even so, there isn’t a single answer to this question.

If you use glOrtho, then the projection is parallel:

glOrtho(0,5,0,3,-100,100);

I purposely made the z range big so you can forget it.

In this projection, if you plot a line:

glBegin(GL_LINES);
glVertex3f(0,0);
glVertex4f(2.5,0);
glEnd();

It will go from the left edge to halfway across the screen at the bottom.

Next, you can set the viewport with glViewpoint, which would limit which area of the screen your transform should apply.

glViewport(0,0,100,100);
glOrtho(0,5,0,3,-100,100);

would draw exactly the same thing as before, except that the “window” is now actually a 100x100 pixel square at the lower-left of your window.

Last, if you use gluLookat, glPerspective, etc, the object will be projected using 3d perspective onto the screen, so obviously the farther away it is, the smaller it gets. Yes, for the same perspective, if you have a wider window you will get a different number, and so yes, you do have to take the aspect ratio into account. See the red book for the canonical example, or for that matter most demos use gluLookat or gluPerspective. But the transformation is mapped onto the viewport, so it does in fact depend on window size.