Jump to content
  • Advertisement
Sign in to follow this  
steffenomak

OpenGL zoom in and out

This topic is 3049 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

Hi I'm using OpengGl Qt and Box2d to make a game of sorts. The problem I'm having is that when i use glTranslate3f and at the z parameter put something there, there is no diffrense. I want to be able like zooming and stuff. I have just started learning some opengl, but then I started making box2d working in qt but whit qts paintingmethods beeing not to my liking i just made opengl render everything. The code for everything is in a mess becouse I havent starting cleaning up and making elegant solutions, i only whant something that works. The code that renders the sceene, or at least all the boxes, ill post here and if some one whant to download the source I'll put up a link
void World::renderWorld(){

    RenderShape painter;
    painter.set_render_mode( SolidRender );

    GLfloat x=0.0f;
    GLfloat y=0.0f;
    GLfloat w=0.0f;
    GLfloat h=0.0f;
    GLfloat a=0.0f;
    GLfloat z = 0.0f;

    GLfloat cameraZoom = -6.0f;
    GLfloat cameraY = 0.0f;
    GLfloat cameraX = 0.0f;

    glLoadIdentity();
    glTranslatef( 0.0f, 0.0f, cameraZoom );

    QListIterator<Body *> list( bodyList );
    int i=0;
    while( list.hasNext() ){
        Body *temp = list.next();
        if( temp->get_frozen() == false ){
            x = temp->get_x();
            y = temp->get_y();
            w = temp->get_width() / 2.0f;
            h = temp->get_height() / 2.0f;
            a = temp->get_angle_deg();

            glTranslatef( x, y, z );

            GLfloat color1[] = { 0.0f, 0.0f, 0.0f };
            GLfloat color2[] = { 0.5f, 0.5f, 0.5f };
            GLfloat color3[] = { 1.0f, 1.0f, 1.0f };

            if( temp->get_body_type() == square ){
                temp->get_dynamic() ?
                        painter.render_square( w, h, a, color1 ):
                        painter.render_square( w, h, a, color2 );
            } else if( temp->get_body_type() == circle ){
                w *= 2.0f;
                temp->get_dynamic() ?
                        painter.render_circle( w, color1 ):
                        painter.render_circle( w, color2 );
            }

            glLoadIdentity();
            glTranslatef( 0.0f, 0.0f, -6.0f );
        } else {
            delete temp;
            bodyList.removeAt( i );
        }
        i+=1;

    }

    GLfloat c[] = { 0.0f, 0.0f, 0.0f };
    painter.render_grid( -3.0f, 3.0f, 6.0f, 6.0f, 0.1f, c );

    target->render( cameraX, cameraY, cameraZoom );

    glLoadIdentity();
    glTranslatef( cameraX, cameraY, cameraZoom );
}

the source link: http://www.megaupload.com/?d=A1J5WNPY [Edited by - steffenomak on February 16, 2010 2:00:33 AM]

Share this post


Link to post
Share on other sites
Advertisement
Are you using a perspective projection, or an orthographic projection?

If it's the latter, changing the camera distance won't give you the effect you're after (more info on orthographic projections here). You can however create a zoom effect by modifying the parameters of the orthographic projection matrix. In straightforward terms, moving the sides of the orthographic frame (left, right, bottom, and top) closer together will create a 'zoom in' effect, and moving them farther apart will create a 'zoom out' effect.

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Are you using a perspective projection, or an orthographic projection?

If it's the latter, changing the camera distance won't give you the effect you're after (more info on orthographic projections here). You can however create a zoom effect by modifying the parameters of the orthographic projection matrix. In straightforward terms, moving the sides of the orthographic frame (left, right, bottom, and top) closer together will create a 'zoom in' effect, and moving them farther apart will create a 'zoom out' effect.


The resize event that does some things and i thing there it sets it to projection but i dont know


void GLRender::resizeGL( int w, int h ){
if( h == 0 ){
h = 1;
}
glViewport( 0, 0, w, h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();

gluPerspective( 45.0f, (GLfloat)w/(GLfloat)h, 0.1f, 100.0f );

glMatrixMode( GL_MODELVIEW );

glLoadIdentity();
}



But could you give an super small example is you would be so nice.

Share this post


Link to post
Share on other sites
Ok, given that you're using a perspective projection, I couldn't really say (without looking at your code more carefully, at least) why moving the camera isn't having an effect. That said, a somewhat more common way to create the effect of zooming when using a perspective projection is to increase (zoom out) or decrease (zoom in) the field of view, so you might try that and see if it gives you the results you're after.

Share this post


Link to post
Share on other sites
Original post by jyk
lol, I solved it by placing object directly deeper in to the screen and out of the screen lol. So that means I cant move the camera whit glTranslatef it just moves the focus for the glVertex starting point. Funny

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!