2d HUD over 3d Game

Started by
6 comments, last by Stani R 17 years ago
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
glLoadIdentity();
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!
Advertisement
You can use glOrtho() and then draw quads in screen space. Do this after you draw your scene.
Look at glOrtho()

	glOrtho(0, ScreenWidth, ScreenHeight, 0, -1, 1);	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();
- Iliak -
[ ArcEngine: An open source .Net gaming framework ]
[ Dungeon Eye: An open source remake of Eye of the Beholder II ]
Quote:Original post by RyanA
This 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.]
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!
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();
hackerkey://v4sw7+8CHS$hw6+8ln6pr8O$ck4ma4+9u5Lw7VX$m0l5Ri8ONotepad++/e3+8t3b8AORTen7+9a17s0r4g8OP
Excellent! Thanks a ton. It works perfectly nemesisgeek.
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.

This topic is closed to new replies.

Advertisement