# 2d HUD over 3d Game

## Recommended Posts

I am creating a simple 3d game and was wondering how to draw a HUD on the screen. Here is everything that relates to setting up the camera:
// This is called before the game starts
glMatrixMode(GL_PROJECTION);
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 1000.0);
glMatrixMode(GL_MODELVIEW);

// This is called every update loop
camera.roll *= 0.98;
if(y < standHeight) y += 0.05;

if(keyDown(KEY_W)) camera.walk(0.2);
if(keyDown(KEY_S)) camera.walk(-0.2);
if(keyDown(KEY_A)) camera.strafe(-0.2);
if(keyDown(KEY_D)) camera.strafe(0.2);
if(keyDown(KEY_LCTRL)) camera.crouch(0.2);

camera.setYaw((SCREENWIDTH/2-mouseX())*0.2);
camera.setPitch((SCREENHEIGHT/2-mouseY())*0.2);

// And finally this is called before I draw anything
glRotatef(-pitch, 1.0, 0.0, 0.0);
glRotatef(-roll, 0.0, 0.0, 1.0);
glRotatef(-yaw, 0.0, 1.0, 0.0);
glTranslatef(-x, -y, -z);

This works fine, I just need to know how i can get 2d things over my 3d screen. Thanks!

##### Share on other sites
You can use glOrtho() and then draw quads in screen space. Do this after you draw your scene.

##### Share on other sites
Look at glOrtho()

	glOrtho(0, ScreenWidth, ScreenHeight, 0, -1, 1);	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();

##### Share on other sites
Quote:
 Original post by RyanAThis works fine, I just need to know how i can get 2d things over my 3d screen. Thanks!
Short answer: set up a separate transform exclusively for the HUD. Typically this will involve an identity modelview transform, and an orthographic projection.

You can use glPushMatrix() and glPopMatrix() to isolate the HUD transform from other transforms, or just overwrite your previous transforms if the HUD will be the last thing rendered in the scene.

[Edit: Beaten X 2.]

##### Share on other sites
Wow, thanks for all of your quick responses. I am sadly a noob at OpenGL, so I tried following all of you guys' advice, but I still must be doing something wrong.

This is at the end of my drawing code:
// HUD - Always Last Thing!glPushMatrix();glOrtho(0, SCREENWIDTH, SCREENHEIGHT, 0, -1, 1);glMatrixMode(GL_MODELVIEW);glLoadIdentity();        glBegin(GL_QUADS);      // try drawing a quad    glVertex2f(0.0, 0.0);    glVertex2f(10.0, 0.0);    glVertex2f(10.0, 10.0);    glVertex2f(0.0, 10.0);glEnd();        glPopMatrix();

The 3d scene still shows up fine, but there is no quad to be found. I tried changing the width/height value of the quad to 1.0, 10.0, and 100.0 but still no quad appeared. Any ideas?

Thanks!

##### Share on other sites
You need to reset your projection matrix before calling glOrtho().

glOrtho(0, SCREENWIDTH, SCREENHEIGHT, 0, -1, 1);glMatrixMode(GL_MODELVIEW);glLoadIdentity();

BECOMES
glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(0, SCREENWIDTH, SCREENHEIGHT, 0, -1, 1);glMatrixMode(GL_MODELVIEW);glLoadIdentity();

##### Share on other sites
Excellent! Thanks a ton. It works perfectly nemesisgeek.

##### Share on other sites
By the way, even though I said quads, in the long run you might get better mileage out of two triangles. I'm not sure but I think some OpenGL implementations are optimized for displaying triangle primitives.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628305
• Total Posts
2981967

• 9
• 13
• 11
• 12
• 11