C++ code to emulate openGL old direct mode drawing

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

 [  compact version ] | [  modular version ]

Remember the old days when you were able to simply draw a few primitives with GL_POINTS, GL_LINES or GL_QUADS within a pair of good old begin() end(). Well I'm providing a C++ class which will enable you to do this again under OpenGL 3.1 or higher.

The so called direct mode or immediate mode with OpenGL 2.1 was very convenient to debug by drawig a few GL_TRIANGLES or GL_LINES. You would simply write this in your display loop:

    glColor3f(1.f, 0.f, 0.f);
    glVertex3f(0.f, 0.f, 0.f);
    glVertex3f(1.f, 0.f, 0.f);
    glVertex3f(0.f, 1.f, 0.f);
    glVertex3f(1.f, 1.f, 0.f);

There were also display lists with higher performances. You could draw a primitive within glBegin() glEnd() and draw it as much as you wanted without redefining it:

GLuint index = glGenLists(1); // create a single display list

glNewList(index, GL_COMPILE); // fill the display
glEndList(); // end filling

// draw the display list with a single function call

// delete it
glDeleteLists(index, 1);

Even though Kronos group had good reason to remove them (as well as quads primitives)  I still feel it was a great debugging tool. So here is a preview on how to use my class GlDirect_draw which emulates direct mode:

    GlDirect_draw prim;
    prim.color3f(1.f, 0.f, 0.f);
    prim.vertex3f(0.f, 0.f, 0.f);
    prim.vertex3f(0.f, 10.f, 0.f);
    prim.vertex3f(0.f, 0.f, 10.f);

    // Draw the triangle
    prim.set_matrix(model_view_mat, projection_mat);
    // release CPU GPU memory
    // Draw nothing

The class has a simple internal shader which will enable you to lit or not the mesh with an headlight. You can even automatically generate flat normals for flat shading. There is a lot more explanations and examples in the code header. If you don't need performances and don't want to waste time on VBO/VAO/Shaders setup; this is what you want. My advice is to use it as a display list to get maximal performances and for debugging purposes.

I've made two archives a [  compact version ] with every classes concatenated in a single .cpp and a [  modular version ].

Enjoy, and don't hesitate to report bugs (don't suggest new features though. I won't have time for that).

Note: I have found similar code to do immediate mode under OpenGL 3.1 here or there if you're not pleased with mine.

One comment

Interesting concept. Thanks for sharing.

Robinson - 04/09/2017 -- 18:57
(optional field, I won't disclose or spam but it's necessary to notify you if I respond to your comment)
All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.
Anti-spam question: