views:

2553

answers:

1

This is for a 2D game so there are only an x and y axis. The game is in landscape mode on the iPhone. I wish to be able to set the screen x and y coordinates of where to render a texture.

+5  A: 

If you're doing a 2D game, you can set up your projection and model-view matrices so that you don't need to convert at all:

// This goes in your init code somewhere
// Set up 480x320 orthographic projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(-240.0f, 240.0f, -160.0f, 160.0f, -1.0f, 1.0f);

// Rotate into landscape mode
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(-90.0f, 0.0f, 1.0f, 0.0f);

This makes it so that world coordinates (-240, -160) maps to the top-left of the screen (bottom-left in landscape mode), (240, 160) maps to the bottom-right (top-right in landscape), etc. Since the iPhone's screen is 480x320, you won't need to convert between world and screen coordinates with your matrices set up thusly.

Of course, if you want to be able to move the camera, then you'll need to offset by the camera's location.

Adam Rosenfield
Thanks for this. I just noticed that in OpenGL ES for the iPhone there is no gluOrtho2D method. Would you know what the equivelant statement is?
Dimitris
gluOrtho2D() is just a wrapper around glOrtho(), so I've replaced it with that.
Adam Rosenfield