Reputation: 21
triangle is not drawn when glOrtho z range between 0 and 1.
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( -1,1,-1,1,-1,1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
// Draw a triagnle in z 0.5
glBegin( GL_TRIANGLES );
glColor3f( 1, 0, 0 );
glVertex3f( -0.5, -0.5, 0.5 );
glVertex3f( 0.5, -0.5, 0.5 );
glVertex3f( 0.0, 0.5, 0.5 );
glEnd();
It displays a red triangle. Its fine for me.
But when I change near clipping plane to 0, It displays nothing. Triangle is drawing with z 0.5, and near and far is between 0 and 1. But Triangle is not drawn why ?
Following code is used to display the Triangle in z 0.5
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( -1,1,-1,1,0,1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
// Draw a triagnle in z 0.5 This triangle is not displayed.
glBegin( GL_TRIANGLES );
glColor3f( 1, 0, 0 );
glVertex3f( -0.5, -0.5, 0.5 );
glVertex3f( 0.5, -0.5, 0.5 );
glVertex3f( 0.0, 0.5, 0.5 );
glEnd();
Upvotes: 0
Views: 756
Reputation: 1235
Note that your triangle is being drawn BEHIND the camera. The positive Z direction is backward, toward the screen, and the negative Z direction is forward, toward the far plane. However, in glOrtho
, the near and far planes are only distances in front of the camera, not actual Z coordinates.
In your first example, the near plane is -1, and the far plane is 1, so the frustum has a range of -1 unit in front of the camera (1 unit behind) to 1 unit in front of the camera. (from 1 behind to 1 in front) Therefore, your object behind the camera was drawn.
In your second example, the near plane is 0 units in front of the camera, and the far plane is 1 unit in front of the camera (from origin to 1 in front), so your object behind the camera was not drawn.
To fix it, you should change your triangle to be drawn in front of the camera by drawing it at -0.5 z instead.
// Setup ortho projection from the origin.
glOrtho( -1,1,-1,1,0,1 );
...
// Draw a triangle in front of the camera, at -0.5 z
glVertex3f( -0.5, -0.5, -0.5 );
glVertex3f( 0.5, -0.5, -0.5 );
glVertex3f( 0.0, 0.5, -0.5 );
There are also other methods such as reversing the Z axis, if you prefer to use positive Z values to represent forward:
// Setup ortho projection from the origin, with the Z axis reversed.
// (the far plane is behind the near plane.)
glOrtho( -1,1,-1,1,0,-1 );
...
// Draw a triangle at 0.5 z
glVertex3f( -0.5, -0.5, 0.5 );
glVertex3f( 0.5, -0.5, 0.5 );
glVertex3f( 0.0, 0.5, 0.5 );
Upvotes: 1
Reputation: 45352
The nearVal
and farVal
values do not specify the z values of the clipping planes, but the distance from the camera. In fixed-function OpenGL, eye space is defined with camera at origin, looking at -z direction.
glOrtho()
is just defined so that positive values refer to positions in front of the viewer (so it is consistent to the behavior of glFrustum()
), and negative vaules are behind the viewer, so actually, the planes are at z=-nearVal
and z=-farVal
in eye space.
Upvotes: 0