• Introduction
  • Prerequisites
  • Forget Everything
  • Building the tutorials
    • Edifice on Windows
    • Edifice on Linux
    • Building on Mac
    • Note for Lawmaking::Blocks
  • Running the tutorials
  • How to follow these tutorials
  • Opening a window

Introduction

Welcome to the first tutorial !

Before jumping into OpenGL, you will first learn how to build the code that goes with each tutorial, how to run it, and most chiefly, how to play with the code yourself.

Prerequisites

No special prerequisite is needed to follow these tutorials. Experience with any programming langage ( C, Coffee, Lisp, Javascript, any ) is meliorate to fully understand the code, but not needed; it will but be more than complicated to acquire 2 things at the same time.

All tutorials are written in "Easy C++" : Lots of attempt has been fabricated to make the code equally simple as possible. No templates, no classes, no pointers. This way, y'all will be able to understand everything even if you lot only know Coffee.

Forget Everything

Y'all don't have to know anything, but you have to forget everything you know near OpenGL. If y'all know about something that looks like glBegin(), forget it. Here you will learn mod OpenGL (OpenGL 3 and 4) , and many online tutorials teach "sometime" OpenGL (OpenGL 1 and 2). And so forget everything you might know before your brain melts from the mix.

Building the tutorials

All tutorials tin be built on Windows, Linux and Mac. For all these platforms, the procedure is roughly the aforementioned :

  • Update your drivers !! doooo information technology. You lot've been warned.
  • Download a compiler, if y'all don't already have one.
  • Install CMake
  • Download the source code of the tutorials
  • Generate a projection using CMake
  • Build the project using your compiler
  • Play with the samples !

Detailed procedures will now exist given for each platform. Adaptations may be required. If unsure, read the education for Windows and try to adapt them.

Building on Windows

  • Updating your drivers should be easy. Simply go to NVIDIA's or AMD's website and download the drivers. If unsure nigh your GPU model : Command Panel -> Organisation and Security -> System -> Device Managing director -> Display adapter. If you take an integrated Intel GPU, drivers are usually provided by your OEM (Dell, HP, …).
  • We suggest using Visual Studio 2017 Express for Desktop every bit a compiler. You can download it for gratuitous here. MAKE Certain YOU Cull CUSTOM INSTALLATION AND CHECK C++. If you prefer using MinGW, we recommend using Qt Creator. Install whichever you want. Subsequent steps volition be explained with Visual Studio, simply should be like with any other IDE.
  • Download CMake from hither and install it
  • Download the source lawmaking and unzip information technology, for example in C:\Users\XYZ\Projects\OpenGLTutorials\ .
  • Launch CMake. In the first line, navigate to the unzipped binder. If unsure, choose the folder that contains the CMakeLists.txt file. In the second line, enter where you lot want all the compiler's stuff to live. For instance, you tin can cull C:\Users\XYZ\Projects\OpenGLTutorials-build-Visual2017-64bits\, or C:\Users\XYZ\Projects\OpenGLTutorials\build\Visual2017-32bits. Find that it tin exist anywhere, not necessarily in the aforementioned folder.

  • Click on the Configure push. Since this is the starting time time you configure the project, CMake will ask you which compiler you would like to employ. Choose wisely depending on step ane. If yous have a 64 bit Windows, you lot can cull 64 bits; if you lot don't know, choose 32 $.25.
  • Click on Configure until all red lines disappear. Click on Generate. Your Visual Studio project is now created. You lot tin at present forget nigh CMake.
  • Open C:\Users\XYZ\Projects\OpenGLTutorials-build-Visual2010-32bits. Y'all will see a Tutorials.sln file : open up it with Visual Studio.

In the Build menu, click Build All. Every tutorial and dependency will exist compiled. Each executable will likewise exist copied back into C:\Users\XYZ\Projects\OpenGLTutorials\ . Hopefuly no mistake occurs.

  • Open C:\Users\XYZ\Projects\OpenGLTutorials\playground, and launch playground.exe. A black window should appear.

You lot can also launch any tutorial from inside Visual Studio. Right-click on Playground once, "Choose as startup projection". You can now debug the code past pressing F5.

Building on Linux

They are so many Linux variants out there that it's impossible to list every possible platform. Adjust if required, and don't hesitate to read your distribution's documentation.

  • Install the latest drivers. Nosotros highly recommend the closed-source binary drivers. It'due south not GNU or whatever, but they piece of work. If your distribution doesn't provide an automated install, attempt Ubuntu's guide.
  • Install all needed compilers, tools & libs. Complete list is : cmake make g++ libx11-dev libxi-dev libgl1-mesa-dev libglu1-mesa-dev libxrandr-dev libxext-dev libxcursor-dev libxinerama-dev libxi-dev . Employ sudo apt-get install ***** or su && yum install ******.
  • Download the source code and unzip it, for case in ~/Projects/OpenGLTutorials/
  • cd in ~/Projects/OpenGLTutorials/ and enter the following commands :

  • mkdir build
  • cd build
  • cmake ..

  • A makefile has been created in the build/ directory.
  • type "make all". Every tutorial and dependency will exist compiled. Each executable volition besides be copied dorsum into ~/Projects/OpenGLTutorials/ . Hopefuly no fault occurs.
  • Open ~/Projects/OpenGLTutorials/playground, and launch ./playground. A black window should appear.

Annotation that you really should use an IDE like Qt Creator. In item, this ane has born back up for CMake, and it volition provide a much nicer experience when debugging. Here are the instructions for QtCreator :

  • In QtCreator, go to File->Tools->Options->Compile&Execute->CMake
  • Prepare the path to CMake. This is most probably /usr/bin/cmake
  • File->Open Project; Select tutorials/CMakeLists.txt
  • Select a build directory, preferably outside the tutorials folder
  • Optionally set up -DCMAKE_BUILD_TYPE=Debug in the parameters box. Validate.
  • Click on the hammer on the bottom. The tutorials can now be launched from the tutorials/ binder.
  • To run the tutorials from QtCreator, click on Projects->Execution parameters->Working Directory, and select the directory where the shaders, textures & models live. Example for tutorial 2 : ~/opengl-tutorial/tutorial02_red_triangle/

Building on Mac

The procedure is very similar to Windows' (Makefiles are too supported, but won't be explained here) :

  • Install XCode from the Mac App Store
  • Download CMake, and install the .dmg . You don't need to install the command-line tools.
  • Download the source code and unzip it, for instance in ~/Projects/OpenGLTutorials/ .
  • Launch CMake (Applications->CMake). In the first line, navigate to the unzipped binder. If unsure, choose the binder that contains the CMakeLists.txt file. In the 2nd line, enter where you want all the compiler'south stuff to live. For case, you can choose ~/Projects/OpenGLTutorials_bin_XCode/. Observe that it can exist anywhere, not necessarily in the same folder.
  • Click on the Configure button. Since this is the first time you configure the project, CMake will ask you which compiler you would like to use. Choose Xcode.
  • Click on Configure until all red lines disappear. Click on Generate. Your Xcode project is now created. Y'all can forget almost CMake.
  • Open ~/Projects/OpenGLTutorials_bin_XCode/ . You will encounter a Tutorials.xcodeproj file : open it.
  • Select the desired tutorial to run in Xcode's Scheme console, and use the Run button to compile & run :

Note for Code::Blocks

Due to 2 bugs (one in C::B, i in CMake), yous have to edit the command-line in Project->Build Options->Brand commands, as follows :

You also accept to setup the working directory yourself : Project->Properties -> Build targets -> tutorial North -> execution working dir ( it's src_dir/tutorial_N/ ).

Running the tutorials

You should run the tutorials directly from the correct directory : simply double-click on the executable. If y'all like control line best, cd to the right directory.

If you lot want to run the tutorials from the IDE, don't forget to read the instructions above to set the correct working directory.

How to follow these tutorials

Each tutorial comes with its source code and data, which tin be establish in tutorialXX/. However, yous will never change these projects : they are for reference simply. Open playground/playground.cpp, and tweak this file instead. Torture information technology in any fashion you like. If you are lost, simply cutting'north paste any tutorial in it, and everything should be back to normal.

We will provide snippets of code all along the tutorials. Don't hesitate to cut'n paste them directly in the playground while you lot're reading : experimentation is adept. Avert merely reading the finished code, y'all won't learn a lot this way. Even with simple cut'north pasting, you'll get your boatload of problems.

Opening a window

Finally ! OpenGL code ! Well, not really. Many tutorials show you the "low level" way to practice things, so that yous tin can see that no magic happens. But the "open a window" part is actually very wearisome and useless, and then we will use GLFW, an external library, to do this for us instead. If y'all actually wanted to, y'all could apply the Win32 API on Windows, the X11 API on Linux, and the Cocoa API on Mac; or apply another high-level library like SFML, FreeGLUT, SDL, … see the Links page.

Ok, let's get. Beginning, we'll have to deal with dependencies : nosotros demand some basic stuff to display letters in the console :

                          // Include standard headers                            #include <stdio.h> #include <stdlib.h>                                    

Beginning, GLEW. This one really is a trivial flake magic, simply allow's leave this for afterwards.

                          // Include GLEW. Always include it earlier gl.h and glfw3.h, since information technology's a bit magic.                            #include <GL/glew.h>                                    

We decided to allow GLFW handle the window and the keyboard, then let's include it too :

                          // Include GLFW                            #include <GLFW/glfw3.h>                                    

We don't actually need this one right now, merely this is a library for 3D mathematics. It will prove very useful soon. At that place is no magic in GLM, you tin can write your own if you want; it's merely handy. The "using namespace" is there to avoid typing "glm::vec3", simply "vec3" instead.

                          // Include GLM                            #include <glm/glm.hpp>                            using              namespace              glm              ;                      

If yous cut'n paste all these #include's in playground.cpp, the compiler volition complain that at that place is no main() function. And then let's create one :

Outset matter to do it to initialize GLFW :

                          // Initialise GLFW                            glewExperimental              =              true              ;              // Needed for core profile                            if              (              !              glfwInit              ()              )              {              fprintf              (              stderr              ,              "Failed to initialize GLFW              \n              "              );              return              -              1              ;              }                      

We tin now create our beginning OpenGL window !

                          glfwWindowHint              (              GLFW_SAMPLES              ,              4              );              // 4x antialiasing                            glfwWindowHint              (              GLFW_CONTEXT_VERSION_MAJOR              ,              3              );              // We desire OpenGL 3.3                            glfwWindowHint              (              GLFW_CONTEXT_VERSION_MINOR              ,              three              );              glfwWindowHint              (              GLFW_OPENGL_FORWARD_COMPAT              ,              GL_TRUE              );              // To brand MacOS happy; should non be needed                            glfwWindowHint              (              GLFW_OPENGL_PROFILE              ,              GLFW_OPENGL_CORE_PROFILE              );              // We don't want the former OpenGL                            // Open a window and create its OpenGL context                            GLFWwindow              *              window              ;              // (In the accompanying source lawmaking, this variable is global for simplicity)                            window              =              glfwCreateWindow              (              1024              ,              768              ,              "Tutorial 01"              ,              Nothing              ,              Nada              );              if              (              window              ==              NULL              ){              fprintf              (              stderr              ,              "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.              \n              "              );              glfwTerminate              ();              return              -              1              ;              }              glfwMakeContextCurrent              (              window              );              // Initialize GLEW                            glewExperimental              =              true              ;              // Needed in core profile                            if              (              glewInit              ()              !=              GLEW_OK              )              {              fprintf              (              stderr              ,              "Failed to initialize GLEW              \n              "              );              return              -              1              ;              }                      

Build this and run. A window should appear, and be closed right away. Of course! We demand to wait until the user hits the Escape central :

                          // Ensure we can capture the escape key being pressed beneath                            glfwSetInputMode              (              window              ,              GLFW_STICKY_KEYS              ,              GL_TRUE              );              practise              {              // Clear the screen. It'southward not mentioned before Tutorial 02, but information technology can cause flickering, and then it's there nonetheless.                            glClear              (              GL_COLOR_BUFFER_BIT              );              // Describe cypher, come across you lot in tutorial 2 !                            // Swap buffers                            glfwSwapBuffers              (              window              );              glfwPollEvents              ();              }              // Cheque if the ESC fundamental was pressed or the window was airtight                            while              (              glfwGetKey              (              window              ,              GLFW_KEY_ESCAPE              )              !=              GLFW_PRESS              &&              glfwWindowShouldClose              (              window              )              ==              0              );                      

And this concludes our start tutorial ! In Tutorial 2, y'all will learn how to actually draw a triangle.