C++ code to emulate openGL old direct mode drawing

glBegin(GL_TRIANGLES);
glVertex3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 1.0f);
glEnd();

 [  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:

{
    glBegin(GL_QUAD_STRIP)
    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);
    glEnd();
}

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
    glBegin(GL_TRIANGLES);
    glVertex3fv(v0);
    glVertex3fv(v1);
    glVertex3fv(v2);
    glEnd();
glEndList(); // end filling

// draw the display list with a single function call
glCallList(index);

// 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.begin(GL_TRIANGLES);
    prim.vertex3f(0.f, 0.f, 0.f);
    prim.vertex3f(0.f, 10.f, 0.f);
    prim.vertex3f(0.f, 0.f, 10.f);
    prim.end();

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

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: