Jump to content
  • Advertisement
Sign in to follow this  
RyanA

2d HUD over 3d Game

This topic is 4208 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
You can use glOrtho() and then draw quads in screen space. Do this after you draw your scene.

Share this post


Link to post
Share on other sites
Look at glOrtho()


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

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!