Read me About Demoscene? Links Contact

Follow up on scoped OpenGL

Ah, yesterdays post seemed to stir some of my readers - always nice to observe other peoples reactions to my thoughts.

The two major concerns I met were:

  1. “The macro at the bottom will impact my code - I have to fragment a whole lot more than I would like to!”
  2. “Using the scoped definitions I am no longer able to enable/disable features on a per line basis”

Ad 1) Well yes, my simple macro is limited to ONE scoped begin/enable of a given OpenGL cap per scope. This means that the following piece of reference code could never be translated directly:

void DrawComplexScene()
{
  glEnable(GL_DEPTH_TEST);
  for (int i=0; i<scene.size(); ++i) scene[i].drawPassOne();
  glDisable(GL_DEPTH_TEST);
  for (int i=0; i<scene.size(); ++i) scene[i].drawPassTwo();
  glEnable(GL_DEPTH_TEST);
  for (int i=0; i<scene.size(); ++i) scene[i].drawPassThree();
  glDisable(GL_DEPTH_TEST);
}

If you start inserting ENABLE(GL_DEPTH_TEST)-macro calls your compiler will go bezerk with duplicate definition of the variable __GL_DEPTH_TEST__. This is a natural side effect. It has the bonus of tying your hands behind your back and forcing you to fragment your code into what I believe is reasonable chunks.

Observe:

void DrawComplexScene()
{
  DrawComplexScenePassOne();
  DrawComplexScenePassTwo();
  DrawComplexScenePassThree();
}

void DrawComplexScenePassOne()
{
  ENABLE(GL_DEPTH_TEST);
  for (int i=0; i<scene.size(); ++i) scene[i].drawPassOne();
}

void DrawComplexScenePassTwo()
{
  for (int i=0; i<scene.size(); ++i) scene[i].drawPassTwo();
}

void DrawComplexScenePassThree()
{
  ENABLE(GL_DEPTH_TEST);
  for (int i=0; i<scene.size(); ++i) scene[i].drawPassThree();
}

Ad 2) I sure hope that my answer above covers this. In my opinion the structure sketched above gives the compiler and the reader the oppertunity to see which chunks are coherent and which are not. If the reference code was more realistic and had more lines and more enable/disable statements an outsider would have no clue on which parts goes with which render pass. In the fragmented version using the scoped ENABLE-macro the reader may learn the drawing a complex scene is done in three passes; if needed he may dive into each of these passes separately and study them further.

Feel free to disagree :-)