# OpenGL Is this a good idea? (Constantly changing view with glOrtho)

This topic is 4881 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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:
• I setup the 2D mode using glOrtho( 0, 640, 0 480, -1, 1 );
• I then draw 4 colored triangles:
                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();


As you can see, one is off the viewport of 640x480, and one is out of the ZDepth of -1x1.
• Now to move the view I use this code:
void glEnable2D( GLdouble Left, GLdouble Right, GLdouble Top, GLdouble Bottom, GLdouble ZNear = -1, GLdouble ZFar = 1 )
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glOrtho( Left, Right, Bottom, Top, ZNear, ZFar );
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
}
...

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 );
}


The whole point of the ZN/ZF is for different layer viewing.
Now my question for you OpenGL experts is if there is anything wrong with this approach? Is it suitable for a "2D camera" in what I am doing? I know I shouldn't be drawing what's not on the screen, but I will take care of that later only if this method works fine. Thanks for any insight/comments on this! PS as for where I am going with this, just experimenting in 2D, so probabally a tile-based system or side scroller.

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by hplus0603The 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)

##### Share on other sites
Quote:
 Original post by Drew_BentonI 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)

##### Share on other sites
Awesome, thanks for clarifying that! [smile]

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 10
• 9
• 34
• 16
• ### Forum Statistics

• Total Topics
634125
• Total Posts
3015666
×