views:

34

answers:

1

I have a simple rectangle i have drawn on screen in opengl.

My target is to draw this rectangle in 3D so the left side of it is deeper (z-axis) than the right side.

Look at this pic so you can see what i mean: http://www.battleteam.net/tech/fis/docs/images/metroid_hud1.png

This is the code i use to draw a rectangle which uses different colors and i moved both points on left side up a bit.

glColor4f( 1.0, 1.0, 1.0, 1.0 );
glVertex3f( 0,            -20,           z_left );

glColor4f( 0.0, 1.0, 1.0, 1.0 );
glVertex3f( SQUARE_WIDTH, 0,             0 );

glColor4f( 1.0, 0.0, 1.0, 1.0 );
glVertex3f( SQUARE_WIDTH, SQUARE_HEIGHT, 0 );

glColor4f( 1.0, 1.0, 0.0, 1.0 );
glVertex3f( 0,            SQUARE_HEIGHT-20, z_left );

I use the z_left variable to dynamically change the z-value for the both points on the left side to move these points on the z-axis. But what happens is that the rectangle gets cut off from the left side. This happens when the z_left value reaches the zFar or zNear Variable defined via the "glOrtho" function call.

My glOrtho looks like this: glOrtho( 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -100, 100 );

So if the z_left gets higher than 100 or less than -100 then that strange cutting off begins. I dont know why. I expected to get the left side of the rectangle to be moved on z-axis, means moving it deeper (away from eye) or closer.

Can somebody tell me whats wrong here? The rest of the code is pretty simple and standard. A simple rectangle in a 3D environment being changed a bit to have a "3d panel" like rectangle.

My OpenGL init looks like this.

glClearColor( 0, 0, 0, 0 );
glMatrixMode( GL_PROJECTION );
glLoadIdentity(); glOrtho( 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -100, 100 ); 
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

Thanks for any help.

+4  A: 

Ortographic projections have that property, you can move them in the Z axis, but the object looks the same. Switch to a perspective projection, on which objects get smaller with distance to the camera.

About the culling, you're drawing outside the viewing cube (when Z < -100 or Z > 100). The projection will cull away anything outside it's view.

Matias Valdenegro
Ok so what does that mean? Is my init wrong? I thought i'am already in a perspective projection. The only thing i do is the init above, and then start drawing with glVertex3f. Can you explain me what to change?Second block of your answer makes sense.Thanks for help.
NovumCoder
You're using glOrtho, that makes a ortographic projection. Use gluPerspective to get a perspective projection.
Matias Valdenegro
Ah great. Just googled for "opengl perspective projection". I see two different here. glFrustum and gluPerspective. As iam using SDL+OpenGL i guess "glFrustum" is my choice.Many thanks.
NovumCoder