Sign in to follow this  
RyanA

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
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!

Share this post


Link to post
Share on other sites
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.]

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this