The pain of learning OpenGL

When I started learning computer graphics, my knowledge on OpenGL was non-existent. If you are frustated with OpenGL, trust me, I know how you feel. But hang on there. Learning OpenGL is an uphill battle, but once you get to the top, everything will make sense.

I remember when I first started with OpenGL. I had no idea I needed to initialize an OpenGL context. Once I did that, I then learned that I needed to import a math library. Linear Algebra operations are crucial in computer graphics. So you need a math library with vector and matrix operations.

My next question was, How the heck do I render with OpenGL?. It so happens that OpenGL needs the vertex-positions of the object you want to render. I didn't want to complicate my life. I just wanted to render a cube. So, I set up an array with eight vertices. A vertex for each corner of the cube. Ok, so now I have an OpenGL Context, I have a math library and I have the vertex positions of my cube. now what?

Long hours reading at Starbucks led me to the discovery of OpenGL Buffers. Not getting too technical, buffers are responsible for taking data to the GPU. I loaded my vertices into the OpenGL Buffers. Expecting to see my cube rendered on the screen, I clicked on run. To my surprise, nothing showed up on the screen. Did I provided the wrong vertices? Did I make a mistake with the buffers? No, these were not the problems. The problem was that OpenGL requires shaders. Shaders are small programs that operate on the GPU. Shaders receives data from the buffer through Vertex Attributes and render the data. Ok, great. I set up a simple shader pair. You need to set up a Vertex and a Fragmet Shader. The Vertex Shader will control the position of the object, the Fragment will add color to it. Perfect! I ran my program and guess what? Nothing happened.

I found out that you need to compile, attach and link your shaders to a program-object which OpenGL can then call. By this time, I didn't care anymore. I just wanted to see a damn cube rendered on the screen. I didn't read much on it. I was tired and didn't care. I just copy the code to compile-attach-link shaders from a website online. Honestly, I just wanted to see something on the screen. But aside from being tired, I was lost. I was aware that there were so many variables that could go wrong. I didn't know where to look if something went wrong. But I kept going.

So finally, I got the shaders to compile and link. There were some errors in the Vertex & Fragment Shaders which were simple syntax errors. I don't want to make this story too long. But unfortunately, my cube refused to show up on the screen. It turns out that I needed to supply some matrix transformations to the shaders as Uniforms. Uniforms are like vertex attributes. They take data to shaders. The difference is this: vertex attributes are inputs to your vertex shaders. A fragment shader can't receive data from the CPU unless it is coming from the Vertex Shader. But you can send data to any shader through Uniforms. Most of the time, you use uniforms to send matrix transformations.

Finally, I was able to see my cube on the screen. I spent my whole weekend trying to get my first OpenGL program to work. And all that I had to show for was an ugly cube. It had no lighting, no shading. It was just a simple piece of nothingness. If this is currently you, trust me, I know how you feel. I have spent long, long, long hours at starbucks trying to learn OpenGL. It has been a slow and frustated process. But hang in there. Keep at it. Soon you will fall in love deeper with computer graphics. You have no idea how great it feels to bring pixels to life. To move them, to collide them, to simulate physics on them, etc. If you are thinking of giving up, don't. Instead go and take a break. Now I'm developing a game engine. Yes, the same guy at starbucks who was unable to render a cube for a whole weekend is developing a game engine! So If I can do this, you can also do this. You can be an expert on OpenGL and computer graphics. Just hang on in there. Don't let go. Because soon, you will be the master of those stubborn pixels.

Harold Serrano

Computer Graphics Enthusiast. Currently developing a 3D Game Engine.