Simple Beginner OpenGL programs

This entry is part 2 of 4 in the series Learning OpenGL

I’m currently at the 21st YouTube episode from Jamie King’s Playlist ‘3D Computer Graphics using OpenGL’. So far, I have created 3 little programs which are based on each other. All of them are available on Github.

The best way to learn a new language is by actually using it and that’s why I write little programs based on what I’m learning from the YouTube episodes.

My 2nd beginner program

The 2nd OpenGL program consists of a fixed pipeline (no use of shaders). It sets the background blue and then draws two white triangles.

Setting the background color is done via the glClearColor function.

For the drawing of the triangles, there are a few more steps required. First, you need an array with your vertices. Then you need to create a buffer OpenGL can use, which is done via the glGenBuffer function. This buffer then needs to be bound to GL_ARRAY_ELEMENT (since we have an array of vertices which form two triangles if you connect each of them) via glBindBuffer.

This OpenGL buffer object now still needs the actual vertice coordinates which we provide via the glBufferData function. Now all that’s left is to tell OpenGL that it can use that buffer via glEnableVertexAttribArray and we also still need to tell it how the buffer looks like (how many floats for a vertice, etc.) which is done via glVertexAttribPointer.

The buffer object is now ready to be used by OpenGL. We only have to tell OpenGL anymore about the size of the window to render which we do with glViewport and call the actual drawing function glDrawArrays.

My 3rd beginner program

The 3rd program improves memory usage of the previous program (though admittingly for that tiny array, it doesn’t really matter at all) and uses a simple vertex and fragment shader to actually color those two triangles created.

When creating an array of vertices, it is likely that you have a lot of duplication. E.g. one vertice of the first triangle could be the starting point of the second triangle. To avoid unnecessary duplication, you can use additionally to the GL_ARRAY_BUFFER also the GL_ELEMENT_ARRAY_BUFFER. That means I remove all duplicated vertices in the one array and create a second array with the indices. For that array, I generate again an OpenGL buffer and this time bind it to GL_ELEMENT_ARRAY_BUFFER. Last but not least, I have to give the new buffer object the indices array.

The drawing function glDrawElements is now a different one, since the indices array tells OpenGL which vertice comes next.

For the coloring of the triangles, we need to add the RGB floats to each vertice in the first buffer array. Then we tell OpenGL that the buffer array contains those color vertices as well via glEnableVertexAttribArray and glVertexAttribPointer.

Last but not least, we need to write simple vertex and fragment shader code (minimal.vert and minimal.frag in my example program) and then tell OpenGL to compile, link and use the shaders.

Pretty unspectacular what I’ve produced so far, but I’m looking forward to learning more and more.

Do. Or do not! There is no try. – Yoda

Series Navigation<< OpenGL with MFC ‘Hello world’ tutorialOpenGL glBufferSubData >>

Leave a Reply

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