TL;DR Summary
Check out sushruta/starter-code-opengl repository and use it as a template for your next OpenGL project with code laid out neatly and out of the box support for GLFW, GLM and GLAD.
Background
Started writing code in OpenGL code and I quickly realized that there are no good CMake resources anywhere. There are ones with Makefile or ones that describe the steps but none of them present a CMake file that works with GLAD, GLFW3 and GLM.
In the future, I will also be writing OpenGL code that interacts with CUDA and the complexity of the build process would only increase even more. That’s why I decided to invest some time in creating a CMake project that I can use and reuse for various OpenGL applications. As I am (re-)learning CUDA and attempt to write a rasterizer in CUDA and OpenGL, I will also integrate the CUDA related changes in the CMake file.
Pre-requisities
Things that need to be installed
cmake
- I used apt-get to install it on my ubuntu machine.curl
orwget
to download things from the internetgit
to do code version control- Latest Driver for your GPU. Changes happen quite frequently and it is always good to keep your GPU driver current.
Installation of GLFW
Please download the source code of GLFW and install it using cmake. Do -
|
|
The above steps will install GLFW somewhere in /usr/local/...
. Of special interest is the .cmake
file that is also installed. We will use it later as you will see.
Installation of GLM
Get the source code for GLM and follow a similar process as above -
|
|
This will install GLM’s headers in /usr/local/...
something and the cmake file will again be available for us to use later on.
Get GLAD
Go to OpenGL loader/generator webpage and choose from language, spec, version and profile. Then click generate to get a zip that contains some code.
I choose C++
in language, OpenGL
in specification, 4.5
in version and Core
in profile.
It is convenient to add glad
directory in the project folder itself. You could also consider adding it at global level but that will involve doing different things based on the OS. This is how the glad code is laid out for me -
|
|
Get stb headers for various image and text utility functions
Just create a folder called stb
and add two folders src
and include
. Look at this directory on github to understand this more.
The Complete Code Layout
In the root project folder, we have -
src
directory holding the.cpp
files.include
directory holding the.h
and.hpp
filesglad
directory containing glad related filesresources
directory containing textures and shaders.stb
containing image related methods.
All of the above directories except resources
and include
contain their own CMakeLists.txt
file.
The Role of Different CMakeLists.txt Files
I will point out the most interesting lines in each CMakeLists file.
In the root CMakeLists file, there are lines like these -
|
|
These rely on the .cmake
files installed earlier and they get all the include and linking information from there.
|
|
allows us to fail and stop the process if the libraries are not found.
|
|
allows us to set the location where the executable sits. I want the executable to sit in the root build directory and hence made that specification here.
The CMakeLists file in stb and glad give the directions of how to generate a static lib from the files and use it while creating the top level executable.
|
|
in the CMakeLists.txt file of src tells what libraries are needed to complete the linking process.
Link to the Github Repo
The code is available for use here in github. Please take a look at it and use it if you think it works for you. It provides out of box integration with -
Feel free to create an issue there if you think some functionality is missing.