OpenGL SuperBible: first example under Xlib

I have started to read the OpenGL SuperBible 6th edition, which is apparently a good reference on the topic (I have never used OpenGL in my life, although I did some C++ programming on a Silicon Graphics Indigo in 1993…).
I tried to compile the example code, which failed, apparently because my version of GLFW is too new.
That did not discourage me for long, since I would rather avoid non-strictly necessary libraries like GLFW anyway.
Starting with the code from OpenGL’s own “Tutorial: OpenGL 3.0 Context Creation (GLX)”, I read the beginning of the book and tried to compile the first example, that is supposed to display a window full of red color.
The code from the book is:

That’s the kind of “hello word” I don’t really like, because it really is very far from a “hello world”. It assumes that one pulls in a whole header file supplied with the book, and a lot is going on that one does not control. It looks like this render() function is some kind of callback that gets called once in a while.
Instead, I tried to just copy/paste the contents of render() in my own startup code, mentioned above. That did not work at once, but I managed to figure it out.
To even start with OpenGL, one needs to understand the GLEW library concept (or some equivalent, but GLEW really seems to be the most common). The issue solved by GLEW is that quite many of today’s common OpenGL functions like glClearBufferfv(), are considered as extensions that may or may not be implemented by the GPU drivers, and that need to be resolved at runtime.
This is what GLEW does. It exposes the whole OpenGL API through a single #include <GL/glew.h>, and takes care of the rest via the GLEW library (which one needs to link – gcc‘s -lGLEW option will do that).
But it will not do its job if it is not first initialized (glewInit()) after an OpenGL context has been made current (glXMakeCurrent(display, win, ctx) in my case).
Additionally, in the setup mentioned above from “Tutorial: OpenGL 3.0 Context Creation (GLX)”, one has two buffers: a front one and a back one. It looks like the parameter 0 in glClearBufferfv(GL_COLOR, 0, red) refers to the back buffer, which is really what one wants. After it has been updated, one needs to swap the buffers with glXSwapBuffers(display, win).
When all that is in place, the code works, and one gets to see this:
Screenshot - 2014-10-24 - 21:47:28
You will find the code there. git clone it, run make and ./OpenGlTest under OpenGlTest.

Leave a Reply

Your email address will not be published. Required fields are marked *