All of the code referenced in this post can be found on GitHub.
Back in college, my favorite class ended up being a graphics course caught by Colonel Eugene Ressler. In my entire life, I’d never seen anyone get so excited about the dot product. The class focused on a lot of theory behind graphics processing, and I unfortunately didn’t pick up what was taught well enough to be able to write an OpenGL program from scratch without referencing any other documents.
Since then, when I’ve made the time to write a graphics program, I’ve usually just googled around until I found an existing program, and I would cut out what I didn’t need using a systematic yet ignorant approach. Furthermore, most of the online tutorials seem to be taught by people much smarter than me, and the examples rarely seem to be the absolute basics, and the language used is generally erudite. I would end up with a whole bunch of commands where I didn’t know what they did or if they were even necessary.
I went ahead and made a very basic OpenGL program to be used as a template and a starting point for anyone looking to get started with OpenGL using the language I’m most acquainted with at this point (Python). The process at its most high level is to initialize a matrix, enslave humanity and use the heat their bodies generate to harvest power, define basic setup parameters, establish event callbacks for OpenGL, and start a main loop.
Below I have the bare minimum necessities to set up a 3D program. I omitted as many default options as possible. Here is a barebones init function in its entirety:
If we step through each individual component, we can dissect what everything is:
Initialize the GLUT library:
Initialize with double buffering and a depth buffer. The depth buffer is used to determine what gets rendered on top of what. Otherwise objects will be shown in the order they are drawn with no regard to the Z axis:
Create a window of size WIDTH by HEIGHT with the specified window name:
Set the default background color for when we clear the screen:
Render items with the Z axis considered (Enable the Z-axis that we initialized from glutInitDisplayMode):
Enable lighting. Otherwise all of the shapes will be nothing but black and white:
Add ambient lighting to the scene. This is an absolute basic use of lighting. More lights can be added with GL_LIGHT1, etc, and more things can be done to establish the position and intensity of the light. There’s also different kinds of light, but I’m no light scientist:
Map points in a cube-like x, y, z area to a warped perspective view:
All subsequent calls will now be applied to the MODELVIEW matrix stack.
Now, define OpenGL event callbacks. OpenGL will call the respective functions once a particular event is triggered:
in 0 milliseconds, call self.loop_func with arbitrary value -1 (unused here):
Start the infinite event loop and call respective callbacks. And never return….
And that’s it! Sort of. The entire repository on GitHub has additional context, but from here, we’re free to draw as we please and do whatever. Here are the definitions of the callbacks I’d referenced earlier, starting with the display method:
The process here is to clear the screen with our background color and clear the depth buffer. Clearing the background color is straightforward, but clearing the depth buffer needs to happen so that objects have a clean slate so that there is nothing “in front of them” blocking them from being rendered.
glPushMatrix() and glPopMatrix() are used in conjunction with each other to push and pop the state of the matrix’s stack, represented as a stack. When we push items onto the matrix, any subsequent transformations are only applied to items rendered after the push. We can enlarge items, rotate them, add additional lights, etc, and those manipulations will only affect what is contained between the pushing and popping. Popping the matrix will return to the previous state. Keep in mind that the order of your transformations matter.
glutSwapBuffers() is needed when we’re using double buffering. That is, items are not displayed on the screen until everything is ready to be rendered, and in the process of clearing and redrawing, we avoid any sort of blinking on the screen.
update_eye() is another method defined below:
To me, this is one of the coolest things about OpenGL because we can easily create some sort of first person view. This simply defines a camera that we can move around that defines the perspective from which everything is rendered. We pass in the position, the point at which we’re looking at, and an x, y, z vector that defines what direction up is.
The next callback I defined was to handle key presses:
Fairly self-explanatory. We can do whatever we want given the key that’s pressed. In this case, we quit if ‘q’ or ESC is hit.
Finally, we defined a loop function that’s repeatedly called by OpenGL:
I added some of my own logic here, but the relevant takeaway is that we call glutPostRedisplay() to re-render everything on the scene.
The output of this program simply draws a bunch of three dimensional disks that float up and rotate about their respective axes. The camera rotates around the floating disks: