Having fun drawing triangles with OpenGL

Now that I've built my first PC with a dedicated graphics card, I want to try out some OpenGL projects to get my feet wet with graphics programming.

Having fun drawing triangles with OpenGL
Experimenting with OpenGL

Now that I've built my first PC with a dedicated graphics card, I want to try out some OpenGL projects to get my feet wet with graphics programming. I found Anton Gerdelan's tutorial on drawing your first OpenGL triangle and recommend using that as a guide as well as Joey de Vries in depth LearnOpenGL website. Both of them have covered OpenGL concepts in much, much more depth than I think I could ever hope to - especially since I am just getting started - so instead in this post I'm just going to capture some of my notes, struggles and experiments as I enter this arena.

Setup and first triangle

I honestly struggled with this for a while before getting things compiling. In full transparency I haven't really touched C++ since my college days, and that was a big contributing factor. To get started, I downloaded the 64 bit versions of the following:

  • GLEW - GL Extensions Wrangler, a library to ensure the correct version and extensions of OpenGL are loaded
  • GLFW - GL Framework(?), a helper library to get set up with the OpenGL context
  • MinGW via MSYS2 - Toolchain that allows compiling C++ on Windows

MinGW via MSYS2 setup is fairly straightforward and can be tested out by compiling a simple hello world C++ program via g++.

For GLEW and GLFW I downloaded the pre-compiled 64 bit versions from the respective websites. I then created a folder for my project OpenGL, and created an include folder inside the project folder. I then copied over the include folder contents for GLEW and GLFW into my projects include folder.

Next I copied over the GLEW and GLFW libraries into my project folders root so dynamic linking during compilation could find them.

To make it easier to understand and replicate here is what my project directory structure looked like after I was done.

│   glew32.dll
│   glew32.lib
│   glfw3.dll
│   libglfw3.a
│   libglfw3dll.a
│   main.cpp
    │       eglew.h
    │       glew.h
    │       glxew.h
    │       wglew.h

Where main.cpp contains the code from the tutorial Anton Gerdelan's tutorial. I was then able to run the following command and get an executable - hellot.exe:

g++ -o hellot.exe main.cpp libglfw3dll.a glew32.dll -I include -L./ -lglew32 -lglfw3 -lopengl32
Link libraries and compile to an executable

Which I ran using:


And generated my first triangle:



I followed the experiments on the tutorial next which were pretty straightforward:

Pull the shaders out into their own files 

The above experiments were pretty straightforward, just messing around with some variables in the VBO generation, the shader properties and properties of OpenGL. Next, another 2 straightforward ones but a little more interesting.

  • So far we have been using a single VBO, VAO and shader program to generate the two triangle/rectangle seen in the last image in the above gallery. The ask now is to use 2 different VAOs to draw two different shapes.
  • While the above is interesting implementation wise, it also would just generate the same rectangle image, with a different code implementation. To take it all the way, I combined it with the next experiment to use two different fragment shaders to generate the shapes with different colors and make it more visually interesting.

And the result is:

Next steps

Playing around with drawing was lots of fun, I'm excited to see what other things I can learn about OpenGL graphics programming. Things I'm curious about are particle simulation and ray tracing, which I've touched a little in the past but want to get a more in depth understanding of. In addition to that I want to get a deeper understanding of the concepts I've already barely scratched in making this post.

Blog code

I uploaded my experiment work to this repo if you want to check it out: https://github.com/nkorai/OpenGLHelloTriangle.

More resources