2D Camera Zoom Help Needed

Started by
3 comments, last by szecs 13 years, 12 months ago
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!
Advertisement
#Adjust thesezoom_in_out_x = 0.0zoom_in_out_y = 0.0#zoom_in_out_y/zoom_in_out_x = SCREEN_HEIGHT/SCREEN_WIDTHglMatrixMode(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

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

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()
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()
The performance cost is negligible.

This topic is closed to new replies.

Advertisement