tags:

views:

109

answers:

4

I'm trying to understand OpenGL coordinate system. Everywhere I see it's described as right-handed one, but it doesn't correspond with my experience. I tried to draw some shapes and 3-d objects and I see that apparently z-axis points "into the screen" (while x points to right, and y up - which is description of left-handed coordinate system). What am I missing?

edit: For example:
http://img576.imageshack.us/img576/9234/triangles.jpg
If +Z points viewer, why green triangle, which z-coordinate is larger, is behind red one?

initializeGL:

qglClearColor(Qt::darkGray);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc( GL_LEQUAL );    
glEnable(GL_COLOR);  

resizeGL:

glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0f, 50.0f, -50.0f, 50.0f, 50.0, -50.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

paintGL:

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef (-30.0, 1.0, 0.0, 0.0);
glRotatef (20.0, 0.0, 1.0, 0.0);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex3f(45.0, 0.0, 0.0);
glVertex3f(-45.0, 0.0, 0.0);
glVertex3f(0.0, 45.0,  0.0);
glVertex3f(0.0, -45.0,  0.0);
glVertex3f( 0.0, 0.0, 45.0);
glVertex3f( 0.0, 0.0, -45.0);
glEnd();
glBegin(GL_TRIANGLES);
//Red triangle:
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 20.0, 1.0);
glVertex3f(20.0, 0.0, 1.0);
//Green triangle:    
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-15.0, 0.0, 20.0);
glVertex3f(0.0, 25.0, 20.0);
glVertex3f(15.0, 0.0, 20.0);
glEnd();
+2  A: 

Okay, the thing that stands out for me is the glOrtho function. You have the near and far clipping planes only 100 units apart. That seems very small. Do you really want it that way? It's represented by the last 2 parameters in that function.

Also your objects are very big in relation to this small frustum. Objects could be sitting directly in front of your camera and it would block other objects from being seen.

I would enlarge all the parameters in the glOrtho call. HTH


In a right-handed system (OpenGL) negative Z (-Z) should point into the screen. Positive Y should be "up," and positive X should be to the right.

This seems to be what you described, and that is a right-handed coordinate system.

Perhaps this can help. http://www.geometrictools.com/Documentation/LeftHandedToRightHanded.pdf

JustBoo
By z-axis "into the screen" I meant positive Z (the larger point's Z-coordinate is, the farer away it is). I don't use glScale with negative values or anything like this, so it's a bit mysteriuos for me.
majaen
@majaen: Maybe you set up modelview matrix differently. The larger the z the closer objects are to you.
SigTerm
Are you sure that, somewhere in the code, one of the axis is not being "flipped"? I remember (vaguely) once I flipped certain axes around and even made origin at the top-left to accommodate a system in Windows. It appears something in code somewhere is causing weirdness.
JustBoo
I edited my question adding a simple example, maybe it can help with finding out which of my assumptions are wrong.
majaen
See new verbiage in my post above.
JustBoo
+1  A: 

It depends how you draw.

an Identity for modelview and a regular gluPerspective for projection will expect an input coordinate system that is right-handed (as JustBoo mentioned, -z is into the screen).

if you load a different set of matrices, though, nothing stops you from flipping one of the coordinates in the matrix, resulting in a left-handed system. (or not flip. gluPerspective/glFrustum actually flips z coordinates). There are some practical things to keep in mind when doing this, like the winding order for culling.

Bahbar
+1  A: 

What am I missing?

In (the viewport of) left-handed system x points right, y points up, z points forward (away from viewer).

In right-handed x points right, y points up, z points backward (towards the viewer).

Picture

Picture is taken from wikipedia.

x is thumb, y is index finger and z is middle finger.

SigTerm
@SigTerm thanks for the link, it's a lot simpler to explain with this image :)
Calvin1602
+1  A: 

Last 2 parameters glOrtho reference:

nearVal, farVal
Specify the distances to the nearer and farther depth clipping planes. These values are negative if the plane is to be behind the viewer.

Your nearer clipping plane is in front of the viewer and farther, behind. It'll flip Z axis.

Ivan Baldin
That's essentially what I said 12 hours before this answer.
JustBoo
Yes, z-axis was indeed accidentally flipped but then I had a problem with locating exact line of code responsible for that
majaen