I have modest experience of developing games with sdl , libgdx , unity etc. But never got into learning any low level API. So I thought about learning OpenGL and got started with tutorials provided by lazyfoo.net.(Yes I know he is using the legacy OpenGL but he migrates to modern OpenGL as he progresses through the tutorials).
On the fourth chapter he shows the use of glPushMatrix() and glPopMatrix (). Up until then I had the understanding that whenever I translate the modelview matrix that translates the whole gameworld. Now this process of saving the current matrix on stack has put me into some confusions.
They are:1.If I push the current matrix on the matrix stack and make change to current state, then pop that saved state again; how are these transformations combined into one matrix? 2.In the tutorial he shows how to do scrolling background with four quads in the background.He translates the matrix to position the camera in the proper place based on user input. Then he pops the matrix from the stack in the render method and translates it to four different positions to draw four quads. My question is why don't we see the quads moving with the camera(which means not moving at all relative to the camera) since they are drawn relative to the camera.
In key input function:
//Take saved matrix off the stack and reset it
glMatrixMode( GL_MODELVIEW );
glPopMatrix();
glLoadIdentity();
//Move camera to position
glTranslatef( -gCameraX, -gCameraY, 0.f );
//Save default matrix again with camera translation
glPushMatrix();
in render function:
//Pop default matrix onto current matrix
glMatrixMode( GL_MODELVIEW );
glPopMatrix();
//Save default matrix again
glPushMatrix();
//Move to center of the screen
glTranslatef( SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f, 0.f );
//Red quad
glBegin( GL_QUADS );
glColor3f( 1.f, 0.f, 0.f );
glVertex2f( -SCREEN_WIDTH / 4.f, -SCREEN_HEIGHT / 4.f );
glVertex2f( SCREEN_WIDTH / 4.f, -SCREEN_HEIGHT / 4.f );
glVertex2f( SCREEN_WIDTH / 4.f, SCREEN_HEIGHT / 4.f );
glVertex2f( -SCREEN_WIDTH / 4.f, SCREEN_HEIGHT / 4.f );
glEnd();
These all might be very silly questions and I believe they are. I think there is a problem in my understanding of how matrices work in OpenGL. It would be very nice of you to enlighten me on this matter :)