0
\$\begingroup\$

I want to render a simple OpenGL scene as usual, but then I want to superimpose a small image of my own (such as from a bitmap file) on top of the render, such that this image always shows. For example, this could be thought of as showing a logo in the corner of the screen for a 3D game, where the logo is always displayed on top of the rendered scene.

Please could somebody start me off in the right direction? What should I be looking into? I am rather a novice at OpenGL...

Let us suppose that I have the following code:

#include <GL/glut.h>

void renderScene(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBegin(GL_TRIANGLES);
        glVertex3f(-0.5,-0.5,0.0);
        glVertex3f(0.5,0.0,0.0);
        glVertex3f(0.0,0.5,0.0);
    glEnd();

    glutSwapBuffers();
}

int main(int argc, char **argv)
{
    // init GLUT and create Window
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(320,320);
    glutCreateWindow("GLUT Triangles");

    // register callbacks
    glutDisplayFunc(renderScene);

    // enter GLUT event processing cycle
    glutMainLoop();

    return 1;
}

This renders a triangle on the screen. How, for example, would I now render a 10-by-10 bitmap from file, at location (100, 100) on the screen? If the viewpoint was static, I could just calculate its 3D location and render it. However, I want the bitmap image to always be displayed in this location, even when the viewpoint changes.

Thanks :)

\$\endgroup\$
3
  • \$\begingroup\$ This is a little unclear. Do you already have the code and knowledge needed to render the scene? \$\endgroup\$ – Jon May 5 '15 at 18:56
  • 3
    \$\begingroup\$ such as superimposing a small image on the screen, on top of the rendered image Easiest way is to just draw a quad with needed image after other objects. \$\endgroup\$ – HolyBlackCat May 5 '15 at 19:55
  • 1
    \$\begingroup\$ Is this question just "how do I render a textured quad in OpenGL?" \$\endgroup\$ – Trevor Powell May 6 '15 at 1:23
1
\$\begingroup\$

You question is a little unclear, but if you asked how to render, for instance a GUI, I have an answer.

Basically you need to render a textured 2d quad. Note that it should be rendered last.

Heres a video tutorial: https://www.youtube.com/watch?v=vOmJ1lyiJ4A&list=PLRIWtICgwaX0u7Rf9zkZhLoLuZVfUksDP&index=24

\$\endgroup\$
1
  • \$\begingroup\$ Expand on this for +1's - For example, which BlendState should he be using for the quad, and why? \$\endgroup\$ – Jon May 5 '15 at 20:00
1
\$\begingroup\$

You need to use a textured quad with an orthographic projection matrix. Using alpha blending will make the image partially transparent allowing for the image to not appear square.

\$\endgroup\$
1
\$\begingroup\$

The title of your question is different from what you're actually asking. I assume you are asking how to render a textured quad like a part of your game's HUD.

Here's what you can do to have an object rendered on top of your other objects:

void renderScene(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Set up matrixes in order to draw your scene
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho2d( -1.0, 1.0,
               -1.0, 1.0 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    gluLookAt( 0.0f, 1.0f, 0.0f,
               0.0f, 0.0f, 0.0f,
               0.0f, 1.0f, 0.0f );

    // Render your scene/triangle
    /*(...)*/

    // Set up your matrixes in order to draw your HUD over the scene
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho2d( 0, windowWidth, 0, windowHeight );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    // Disable Depth test so that the HUD will overwrite what was rendered before
    glDisable( GL_DEPTH_TEST );

    // Render your HUD elements
    /*(...)*/
    glPushMatrix();

        glBindTexture( GL_TEXTURE_2D, &someTextureId );

        glTranslate3f( 0, windowHeight - 100, 0 );
        // For example, scale your quad/texture to 100x100 pixels
        glScalef( 100.0f, 100.0f, 0.0f );

        glBegin(GL_QUADS);
            glTexCoord2f( 0.0f, 0.0f ); 
            glVertex3f( 0.0f, 0.0f, 0.0f );

            glTexCoord2f( 1.0f, 0.0f );
            glVertex3f( 1.0f, 0.0f, 0.0f );

            glTexCoord2f( 1.0f, 1.0f );
            glVertex3f( 1.0f, 1.0f, 0.0f );

            glTexCoord2f( 0.0f, 1.0f );
            glVertex3f( 0.0f, 1.0f, 0.0f );
        glEnd();

        // Remove texture
        glBindTexture( GL_TEXTURE_2D, 0 );

   glPopMatrix();

   glEnable( GL_DEPTH_TEST );

   glutSwapBuffers();
}

I've only indented the glPushMatrix and glPopMatrix to make it easier to read, it's not absolutely necessary.

If there's something you don't understand in the code sample feel free to ask.


Here's a series of tutorials that I recommend to get you started in OpenGL: http://lazyfoo.net/tutorials/OpenGL/index.php

There's also how to load and use textures.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.