OpenGL coordinate system

Started by
12 comments, last by Zakwayda 14 years, 1 month ago
I'm very confused about why OpenGL defines the coordinates it uses when you call glVertex as offsets from the center of the screen. It's extremely counter-intuitive for just about everything I want to do. I need a bit of help understanding how to make this work logically with a grid-based collision system (and later, some sort of tree-based subdivision structure). Currently, the objects I want to display are just points drawn using glVertex2. Their position vectors are stored as offsets from the center of the screen, which is for some reason (0, 0). However, I want their coordinates in screen space. That is, the rect defined with an upper left point of (0,0) and the bottom right being (window width, window height). Is there a way to make OpenGL vertex calls work on regular coordinates or do I have to write some sort of function to translate them every time I want to use an object's position to draw a vertex? This would also be a solution (a computationally wasteful one, however.) to the problem of only having OpenGL offset coordinates when I wanted to check which grid cell an object resides in. Thanks.
Advertisement
The coordinate system is entirely up to you to define. Look up glOrtho.
hmm, glOrtho definitely does what I want for the most part. However, what if I still want to have depth? Should I be using glVertex3f(...) instead of glVertex2f and just keep the Z component at zero?

I ask because I might want to zoom the camera back to show a larger landscape, but still only operate in 2 dimensions for everything else.

This seems to be more logical, as it's only the illusion of 2d.
Quote:Original post by kibokun
hmm, glOrtho definitely does what I want for the most part. However, what if I still want to have depth? Should I be using glVertex3f(...) instead of glVertex2f and just keep the Z component at zero?


This will just make it possible to "occlude" something because higher Z will overdraw lower Z. With glOrtho you are literally mapping XY coordinates to screen space. You could zoom out by scaling your coordinates or changing the values you pass to glOrtho.

If you want 3D then stick to perspective mode. The "center of the screen" thing is an illusion. You are specifying ModelView coordinates but the View is the identity matrix. So you start at the origin looking down the negative Z axis. Try adding some glTranslate / glRotate calls before your glVertex calls and observe how the "camera" changes.

By the way, all the glVertex etc calls are now deprecated (but it's still ok to use them if you want to).
Quote:Original post by kibokun
Is there a way to make OpenGL vertex calls work on regular coordinates ...
Why do you think that the one co-ordinate system is regular and the other is not? In fact none system is superior to another in general. Its just more convenient to do some task in the one system instead of the other.

Quote:Original post by kibokun
... or do I have to write some sort of function to translate them every time I want to use an object's position to draw a vertex? This would also be a solution (a computationally wasteful one, however.) to the problem of only having OpenGL offset coordinates when I wanted to check which grid cell an object resides in.
OpenGL does a transformation anyway, and that is much more complex that just a single 2D translation (i.e. it is a 4D matrix by vector product).

Quote:Original post by kibokun
... However, what if I still want to have depth? Should I be using glVertex3f(...) instead of glVertex2f and just keep the Z component at zero?
Read this:
Quote:Manpage of glVertex
When only x and y are specified, z defaults to 0 and w defaults to 1. When x, y, and z are specified, w defaults to 1.


Quote:Original post by kibokun
I ask because I might want to zoom the camera back to show a larger landscape, but still only operate in 2 dimensions for everything else.
Zoom has nothing to do with depth; it cannot even be faked by moving the camera because you utilize orthographic projection. Instead, adapt glOrtho's parameters accordingly.
To accomplish "zoom" in a purely orthographic projection, use scaling instead.
Amateurs practice until they do it right.Professionals practice until they never do it wrong.
I wouldn't suggest to use scale (maybe it's just me, but simply I don't like scaling). If you give greater values to glOrtho, that means more stuff can "fit" into it, thus to the screen, so there you have zooming out. Twice as big values, you zoomed out with two. It's that simple.
Thanks for the replies. Very informative. :)

I think I'm going to stay with gluPerspective because it's more natural for me. However, I'm still not understanding how I can accomplish this:

When a user clicks on a point on the screen, a particle should be created at that X and Y position. However, the X and Y positions I get from the SDL event are on the range of 0-800 for X and 0-600 for Y.

I'd like to find the corresponding X and Y positions I need to pass to glVertex to place a vertex under the mouse cursor.

Thanks!
If you use ortho mode it is fairly straight forward, if you use projection mode it's a little more fiddly as you have to transform the screen space mouse coordinates into world space but not a major ball ache with gluUnproject.
The basic idea is that you unproject the mouse coordinates twice to get a ray, then intersect that ray with whatever plane you want the particle at (let's say y=0, meaning ground plane).

This topic is closed to new replies.

Advertisement