Sign in to follow this  

2D Camera Zoom Help Needed

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

The problem I need to solve is how to implement a zoom feature in my camera for the 2D game I am working on. Here is my initGL function, which I call only once at startup:
bool initGL()
{
    //Set clear color
    glClearColor( 0, 0, 0, 0 );

    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    
    //Set projection
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho( 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1500, 1500 );

    //Initialize modelview matrix
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    //If there was any errors
    if( glGetError() != GL_NO_ERROR )
    {
        return false;
    }

    //If everything initialized
    return true;
}
Before I render, I do a
glTranslatef(-xCam, -yCam, 0 ); 
where xCam and yCam is the camera position, which lets me draw on screen where I intend to draw even though I move my camera around. So all is well there. But how do I take this camera and ZOOM in on what's on screen? I've seen a few suggestions around involving glOrtho and glScalef, but I'm not sure how to implement them. Help!

Share this post


Link to post
Share on other sites
#Adjust these
zoom_in_out_x = 0.0
zoom_in_out_y = 0.0
#zoom_in_out_y/zoom_in_out_x = SCREEN_HEIGHT/SCREEN_WIDTH

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0+zoom_in_out_x,SCREEN_WIDTH-zoom_in_out_x,
0+zoom_in_out_y,SCREEN_HEIGHT-zoom_in_out_y,
-1,1)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
-G

Share this post


Link to post
Share on other sites
G, as far as I know, my code will only run that glOrtho once. Will changing those values down the stretch (the zoomin/zoomout values) still somehow affect glOrtho, or will I need to make some sort of call back and reinitialize my screen?

I'm trying to do this in real time.

Should I do this every time before I draw?:

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0+zoom_in_out_x,SCREEN_WIDTH-zoom_in_out_x,
0+zoom_in_out_y,SCREEN_HEIGHT-zoom_in_out_y,
-1,1)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()

Share this post


Link to post
Share on other sites
Ok, I've actually got this working. I understand what you want me to do there. A few values in your pseudo code were flipped (image was rendering upside down), but I tweaked it and it's good.

Is this an expensive call though? Or is that just the price of zooming?


glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0+zoom_in_out_x,SCREEN_WIDTH-zoom_in_out_x,
0+zoom_in_out_y,SCREEN_HEIGHT-zoom_in_out_y,
-1,1)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this