Opengl: 2D Hud Over 3D

Opengl: 2d HUD over 3D

Took a little while to figure it out, so just in case others have the same issues:

    ...After Drawing 3d Stuff...

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0, SCREEN_WIDTH, SCREEN_HEIGHT, 0.0, -1.0, 10.0);
glMatrixMode(GL_MODELVIEW);
//glPushMatrix(); ----Not sure if I need this
glLoadIdentity();
glDisable(GL_CULL_FACE);

glClear(GL_DEPTH_BUFFER_BIT);

glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0);
glVertex2f(0.0, 0.0);
glVertex2f(10.0, 0.0);
glVertex2f(10.0, 10.0);
glVertex2f(0.0, 10.0);
glEnd();

// Making sure we can render 3d again
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
//glPopMatrix(); ----and this?

...Then swap buffers...

:)

OpenGL 2D hud in 3D application

Once you're done rendering the HUD, you need to re-enable the depth writes

glDepthMask(GL_TRUE);

Draw 2D HUD elements over 3D rendered scene

You did screw up your GL state very badly:

void renderHUDQuad() {
if (HUDquadVAO == 0)
{
[...]
glGenVertexArrays(1, &quadVAO);

You actually use quadVAO in the rest of this function, so you overwrite your fullscreen quad by the smaller one, which means the rest of your scene will be scaled down to this quad from the next frame on...

Opengl Draw HUD over 3D game

There are two mistakes in your code that could cause the problem:

  • glm::mat4 projMatrix = _hud.getCameraMatrix();
    getCameraMatrix() does not return your projection matrix

  • glUniformMatrix4fv(mvpLocation, 1, GL_FALSE, &(projMatrix[0][0]));
    You're assigning your "projection matrix" to your model view projection matrix (MVP)

The fixed code could look like this:

glm::mat4 modelviewMatrix = _hud.getCameraMatrix(); // _hud._cameraMatrix
glm::mat4 projMatrix = _hud.getProjectionMatrix(); // _hud._orthoMatrix
glm::mat4 mvp = projMatrix * modelviewMatrix;
glUniformMatrix4fv(mvpLocation, 1, GL_FALSE, glm::value_ptr(mvp));

I also recommend you read this and this.

Drawing 2D HUD over 3D OpenGL sence with SDL

IIRC, OpenGL has functions for drawing 2D elements. You could render the 3D to a texture and then render 2D over it. If you must use SDL, you'll need a method to copy an OpenGL texture to an SDL surface/texture.



Related Topics



Leave a reply



Submit