   How to scroll a camera around a 2D scene
  I really appreciate all the help you guys have given me so far.  My coding is coming along quite nicely.Right now I need some direction on how to move a camera around a 2D scene.  I have a window drawing a 2048x2048 image onto a 2048x2048 quad.  This is my background image.void renderBackground(){    // now render    glLoadIdentity();    glBindTexture(GL_TEXTURE_2D, texture[2]);   glBegin( GL_QUADS );    glTexCoord2i(0, 0);  glVertex3f( 0, 0, 0 ); glTexCoord2i(1, 0);  glVertex3f( LEVEL_WIDTH, 0, 0 ); glTexCoord2i(1, 1);  glVertex3f( LEVEL_WIDTH, LEVEL_HEIGHT, 0 ); glTexCoord2i(0, 1);  glVertex3f( 0, LEVEL_HEIGHT, 0 );  glEnd();    glLoadIdentity();}My Window right now is only 1024x768.  I would like to be able to scroll the camera around the scene.  Input handling is not an issue.  What I need to know is how to move the view. Also relevant is my glInit():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, -10, 10 );    //Initialize modelview matrix    glMatrixMode( GL_MODELVIEW );    glLoadIdentity();    //If there was any errors    if( glGetError() != GL_NO_ERROR )    {   return false;    }    //If everything initialized    return true;}So how do I move around in my little world?   
  You can use the model-view matrix as a "camera", which moves which part of the scene you are looking at:    glMatrixMode( GL_MODELVIEW );    glLoadIdentity(); glTranslatef( -camPos.x, -camPos.y, 0 );DrawObjects();We use the negative position of the camera because we're actually moving the world, not moving the camera. e.g. if the camera is at X=10, then we need to shift the whole world by -10 so that the camera is at the origin.   ;Quote:Original post by HodgmanYou can use the model-view matrix as a "camera", which moves which part of the scene you are looking at:    glMatrixMode( GL_MODELVIEW );    glLoadIdentity(); glTranslatef( -camPos.x, -camPos.y, 0 );DrawObjects();We use the negative position of the camera because we're actually moving the world, not moving the camera. e.g. if the camera is at X=10, then we need to shift the whole world by -10 so that the camera is at the origin.I have gathered similar information from looking around.  How does this fit into the structure of a program?Right now, I have a main loop that I show you here that makes a few different drawing calls.int main( int argc, char* args[] ){    //Quit flag    bool quit = false;    //The frame rate regulator    Timer fps;    //Initialize    if( init() == false )    {   return 1;    }    //Load the core files    if( load_files() == false )    {   return 1;    }    // gonna need 2 textures here, one for each ship    // will need to make this dynamic later    glGenTextures(3, &texture[0]);    loadBackground("bg2.jpg");   Ship myShip;    myShip.load_player_ships();    Ship enemyShip;    enemyShip.load_enemy_ships();   //While the user hasn't quit    while( quit == false )    {   //Start the frame timer   fps.start();   //While there's events to handle   while( SDL_PollEvent( &event ) )   {  //Handle events for the dot  myShip.handle_input();  //If the user has Xed out the window  if( event.type == SDL_QUIT )  { //Quit the program quit = true;  }   }   // update ship movement   myShip.move();//Clear the screenglClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );   //Show the stuff on the screen   renderBackground();   enemyShip.show(1);   myShip.show(0);   // update screen    SDL_GL_SwapBuffers();   //Cap the frame rate   if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )   {  SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );   }    }    //Clean up    clean_up();    return 0;}I call the draw functions with:    renderBackground();enemyShip.show(1);myShip.show(0);When in the program flow do I move the modelview matrix?  Do I do it once, and then draw everything?  Or do I do it once per draw?  Would I do the transform right before the renderBackground() call?   ;Quote:Original post by HodgmanYou can use the model-view matrix as a "camera", which moves which part of the scene you are looking at:    glMatrixMode( GL_MODELVIEW );    glLoadIdentity(); glTranslatef( -camPos.x, -camPos.y, 0 );DrawObjects();We use the negative position of the camera because we're actually moving the world, not moving the camera. e.g. if the camera is at X=10, then we need to shift the whole world by -10 so that the camera is at the origin.I just can't quite figure this out.I am able to move the camera, but then the positions are off for the rest of the objects I draw.  I want to draw items to an absolute x, y coordinate system.So before I draw, I call glLoadIdentity() and this should reset the coordinate system to the ORIGINAL matrix, right?  Or is that wrong.Argh.  The strange thing is that I am able to draw the background quad and render to it just fine.  I can navigate around the space using the functions you gave me.  But then all subsequent renders are being drawn in my relative coordinate system and not the absolute system.   ;  Assuming you want the background to stay still when the camera moves, and only the objects are affected by the camera, then:glMatrixMode( GL_MODELVIEW );glLoadIdentity();renderBackground();glTranslatef( -camPos.x, -camPos.y, 0 );enemyShip.show(1);myShip.show(0);Note that if you're setting up the view matrix in your main function like this, then you can remove the calls to glLoadIdentity from inside renderBackground, as they're no longer needed.If you want to remove the camera's tranlsation, you call:glLoadIdentity();//reset GL_MODELVIEW To set the camera's translation you callglLoadIdentity();//reset GL_MODELVIEW ...glTranslatef( -camPos.x, -camPos.y, 0 );//...and then add an offset to it;Quote:Original post by HodgmanAssuming you want the background to stay still when the camera moves, and only the objects are affected by the camera, then:*** Source Snippet Removed ***Note that if you're setting up the view matrix in your main function like this, then you can remove the calls to glLoadIdentity from inside renderBackground, as they're no longer needed.If you want to remove the camera's tranlsation, you call:*** Source Snippet Removed ***To set the camera's translation you call*** Source Snippet Removed ***Thank you.  I finally realized that glTransformf calls are cumulative and that saved the day.  Now on to Parallax backgrounds!   