Compiling and running your first OpenGL application in Mac OS X or Linux
Setting up a Linux or Mac machine is made much simpler with the command-line interface. We assume that you have all the components that were discussed earlier ready, and all default paths are used as recommended.
Getting ready
We will start by compiling the sample code described previously. You can download the complete code package from the official website of Packt Publishing https://www.packtpub.com. We assume that all files are saved to a top-level directory called code
and the main.cpp
file is saved inside the /code/Tutorial1
subdirectory.
How to do it...
- Open a terminal or an equivalent command-line interface.
- Change the current directory to the working directory:
cd ~/code
- Enter the following command to compile the program:
gcc -Wall `pkg-config --cflags glfw3` -o main Tutorial1/main.cpp `pkg-config --static --libs glfw3`
- Run the program:
./main
Here is your first OpenGL application that runs natively on your graphics hardware and displays a rotating triangle. Although we have defined the color of only three vertices to be red, green, and blue, the graphics engine interpolates the intermediate results and all calculations are performed using the graphics hardware.
To further simplify the process, we have provided a compile script in the sample code. You can execute the script by simply typing the following commands in a terminal:
chmod +x compile.sh ./compile.sh
You may notice that the OpenGL code is platform-independent. One of the most powerful features of the GLFW library is that it handles the windows management and other platform-dependent functions behind the scene. Therefore, the same source code (main.cpp
) can be shared and compiled on multiple platforms without the need for any changes.