开发者

Where points OpenGL z-axis?

开发者 https://www.devze.com 2023-01-10 10:41 出处:网络
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'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();
g开发者_运维问答lBegin(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();


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


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).

Where points OpenGL z-axis?

Picture is taken from wikipedia.

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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号