Hello Guest

Camera using glOrtho

  • 1 Replies
  • 7564 Views
Camera using glOrtho
« on: October 11, 2011, 02:50:54 »
I made a very simple 2D game, and I had the camera working and everything. But then I found out that for 2D games, I should be using glOrtho (I was using gluPerspective). So I swapped over, and everything works but the camera.
I used this camera code, but modified it a bit for a 2D game: http://www.lloydgoodall.com/tutorials/first-person-camera-control-with-lwjgl/

My lookThrough() function looks like this:

Code: [Select]
public void lookThrough()
{
        GL11.glTranslatef(position.x, position.y, 0);
        //System.out.println(position.x + " : " + position.y); //debugging
}

And that worked with gluPerspective but it doesn't work with glOrtho. I initialize OpenGL like this:

Code: [Select]
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, width, height, 0, -1, 1);

and my render code is like this:

Code: [Select]
GL11.glLoadIdentity();
handleKeyboard();
camera.lookThrough();
renderManager.RenderAll();
Display.update();

renderManager.RenderAll(); is this:

Code: [Select]
public void RenderAll()
{
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();

RenderTerrain();
}

RenderTerrain just renders all my terrain tiles. Their render functions are this:

Code: [Select]
public void render()
{
{
GL11.glPushMatrix();
                {
                GL11.glTranslatef(pos.x, pos.y, 0);
        GL11.glBegin(GL11.GL_QUADS);     
        {           
                 GL11.glTexCoord2f(uvCoords[0][0], uvCoords[0][1]); GL11.glVertex3f(-size, size, 0.0f);               
                 GL11.glTexCoord2f(uvCoords[1][0], uvCoords[1][1]); GL11.glVertex3f( size, size, 0.0f);       
                 GL11.glTexCoord2f(uvCoords[2][0], uvCoords[2][1]); GL11.glVertex3f( size,-size, 0.0f);       
                 GL11.glTexCoord2f(uvCoords[3][0], uvCoords[3][1]); GL11.glVertex3f(-size,-size, 0.0f); 
        }
        GL11.glEnd();
        }
        GL11.glPopMatrix();
}
}

So, I have no idea why it doesn't work. I've moved around things, added new things, but with no result.
Any help is greatly appreciated!

Re: Camera using glOrtho
« Reply #1 on: October 11, 2011, 03:18:42 »
Nevermind, I should have known I'd find it after I already asked for help.

I just had to use
Code: [Select]
GL11.glOrtho(position.x, position.x + Display.getDisplayMode().getWidth()/60, position.y + Display.getDisplayMode().getHeight()/60, position.y, -1, 1);
I have this post to thank:
http://gamedev.stackexchange.com/questions/14563/making-a-camera-in-a-2d-game-glortho