Two ways. One is ortho mode described by mrbastard. The other is this:
int width = 1024;int height = 768;glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);glViewport(0, 0, width, height ); glMatrixMode(GL11.GL_PROJECTION); glLoadIdentity(); glFrustum( - width / 64.0, width / 64.0, - height / 64.0, height / 64.0, 8, 65536); glMatrixMode(GL11.GL_MODELVIEW);
To draw a 10px quad at the center of the screen you do this:
glTranslatef(0.0f, 0.0f, -256.0f); // NOTE THE -256!glBegin( GL_QUADS ); glColor3f( 1.0f, 1.0f, 1.0f ); glVertex3f( 10f, 10f, 0.0f ); glVertex3f( 10f, -10f, 0.0f ); glVertex3f( -10f, -10f, 0.0f ); glVertex3f( -10f, 10f, 0.0f );glEnd();
Take note of the -256 z value! At this depth, the lower left corner of the screen is (-width / 2, -height / 2). The upper right corner is (width / 2, height / 2). For my 2d game, I wrote a GL Utility program to automatically translate to the correct place:
private static final float OFFSET_X = width / -2.0f;private static final float OFFSET_Y = height / -2.0f;private static final float OFFSET_Z = -256.0f;public static void funkTranslate(Vector2df location){ glTranslatef(OFFSET_X + location.x, OFFSET_Y + location.y, OFFSET_Z);}