- I setup the 2D mode using glOrtho( 0, 640, 0 480, -1, 1 );
- I then draw 4 colored triangles:
As you can see, one is off the viewport of 640x480, and one is out of the ZDepth of -1x1.
glBegin( GL_TRIANGLES ); glColor3ub( 0, 0, 255 ); glVertex2d( 50, 0 ); glColor3ub( 255, 0, 0 ); glVertex2d( 0, 50 ); glColor3ub( 0, 255, 0 ); glVertex2d( 100,50 ); glEnd(); glBegin( GL_TRIANGLES ); glColor3ub( 0, 0, 255 ); glVertex2d( 150, 100 ); glColor3ub( 255, 0, 0 ); glVertex2d( 100, 150 ); glColor3ub( 0, 255, 0 ); glVertex2d( 200, 150 ); glEnd(); glBegin( GL_TRIANGLES ); glColor3ub( 0, 0, 255 ); glVertex2d( 850, 800 ); glColor3ub( 255, 0, 0 ); glVertex2d( 800, 850 ); glColor3ub( 0, 255, 0 ); glVertex2d( 900, 850 ); glEnd(); glBegin( GL_TRIANGLES ); glColor3ub( 255, 0, 255 ); glVertex3d( 350, 300, -5 ); glColor3ub( 255, 255, 0 ); glVertex3d( 300, 350, -5 ); glColor3ub( 0, 255, 255 ); glVertex3d( 400, 350, -5 ); glEnd();
- Now to move the view I use this code:
The whole point of the ZN/ZF is for different layer viewing.
void glEnable2D( GLdouble Left, GLdouble Right, GLdouble Top, GLdouble Bottom, GLdouble ZNear = -1, GLdouble ZFar = 1 ) { glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glOrtho( Left, Right, Bottom, Top, ZNear, ZFar ); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); } ... if( inputMgr.IsKeyDown( SDLK_LEFT ) ) { X--; glEnable2D( X, 640 + X, Y, 480 + Y, ZN, ZF ); } if( inputMgr.IsKeyDown( SDLK_RIGHT ) ) { X++; glEnable2D( X, 640 + X, Y, 480 + Y, ZN, ZF ); } if( inputMgr.IsKeyDown( SDLK_UP ) ) { Y--; glEnable2D( X, 640 + X, Y, 480 + Y, ZN, ZF ); } if( inputMgr.IsKeyDown( SDLK_DOWN ) ) { Y++; glEnable2D( X, 640 + X, Y, 480 + Y, ZN, ZF ); } if( inputMgr.IsKeyDown( SDLK_a ) ) { ZN--; glEnable2D( X, 640 + X, Y, 480 + Y, ZN, ZF ); } if( inputMgr.IsKeyDown( SDLK_z ) ) { ZN++; glEnable2D( X, 640 + X, Y, 480 + Y, ZN, ZF ); } if( inputMgr.IsKeyDown( SDLK_s ) ) { ZF--; glEnable2D( X, 640 + X, Y, 480 + Y, ZN, ZF ); } if( inputMgr.IsKeyDown( SDLK_x ) ) { ZF++; glEnable2D( X, 640 + X, Y, 480 + Y, ZN, ZF ); }
Is this a good idea? (Constantly changing view with glOrtho)
Ok right now I am just experimenting with OpenGL with 2D stuff. (Using OpenGL though SDL). Right now I have a little test system that works as follows:
The values already in the Z buffer will not change when you change the znear/zfar values in glOrtho. If you only do this once per frame, and clear the z buffer at the beginning of the frame, then that's not a problem.
Quote:Original post by hplus0603
The values already in the Z buffer will not change when you change the znear/zfar values in glOrtho. If you only do this once per frame, and clear the z buffer at the beginning of the frame, then that's not a problem.
Ok cool, I think I understand what you are saying, but to make sure, I am currently clearing the z buffer with glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); at the beginning of each frame, is that correct? (Or are the depth and Z buffers different things)
Quote:Original post by Drew_Benton
I am currently clearing the z buffer with glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); at the beginning of each frame, is that correct?
Why not? If you need to clear both color and depth...[smile]
Quote:Or are the depth and Z buffers different things ?
The same thing. In theory the proper term is depth buffer (aka Z buffer)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement