开发者

Moving some points in z-axis in OpenGL doesnt work

开发者 https://www.devze.com 2023-01-08 14:59 出处:网络
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.

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.


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.

0

精彩评论

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