glTranslate future problems

I’m making a scroller learning from thebennybox tutorials. In his way of scrolling he does this each loop.

public void render()
    {
       // glPushMatrix();
        {
            // draw to center (exact in this case) of screen first instead 
            // of bottom left
            glTranslatef(
              Display.getWidth() / 2 - Player.SIZE / 2, 
              Display.getHeight() / 2 - Player.SIZE / 2, 
              0); 
            sprite.render();
            // negative to scroll in opposite direction
            glTranslatef(-x, -y, 0);
        }
      //  glPopMatrix();
    }

He also calls

glLoadeIdentity();

each frame. I plan on rotating sprites later on but pushing and poping this doesn’t let it move. How can I fix this?

glPushMatrix() adds another matrix on the stack with the value of the previous one. glLoadIdentity() replaces the current matrix with the identity matrix, meaning no transformation takes place, which is typically how you want to start your rendering. However, if you are pushing and popping all your matrices at the beginning and end of each render, you shouldn’t need to use it.

Thanks for the info. I’ve spent nearly two weeks on this and still can’t work rotation with parallax scrolling. :emo: How do top down shooters do it >:(

You may want to break the problem down, moving a background is no different to moving a player. Handle the background, and then the player. Something simple like this should work:


glPushMatrix();
    glTranslatef(playerPosition.x, playerPosition.y); // Translate background in opposite direction of player
    drawBackground();
glPopMatrix();
glPushMatrix();
    glTranslatef(/* centre player */);
    drawPlayer();
glPopMatrix();

You can scale the amount the background moves as you wish by multiplying playerPosition. The main problem will be handling the case when your background sprite starts moving off the screen. You will need a way to draw the background again at the other side.

Why oh why do i over complicate things :o