In this section, we'll learn how to set up OpenGL using GLFW and GLEW on your Windows system, by using Visual Studio. But, first let's understand what GLFW and GLEW are. GLFW is an OpenGL framework. It's a very lightweight framework that allows us to detect events such as keyboard input, mouse input, and other sort of inputs, but more importantly, it allows you to create a render window in which you can render your code, as OpenGL doesn't have a way of creating a rendering window, and it needs something like GLFW to create it.
GLEW is the OpenGL Extension Wrangler, which basically allows you to use OpenGL functions that are new, or more precisely, non-core. It provides systematic mechanisms during runtime to determine which OpenGL extensions are supported on the target platform. For any new functions, you essentially require something like OpenGL Extension Wrangler to initialize extensions and to write portable applications.
Note
First things first: For this book, it's really important that you have a good foundation in C++, as, in this book, you will be using OpenGL with C++. It's not the easiest thing, OpenGL. If this is your first time coding or you haven't been coding for long, it is recommended you get a good grasp of C++ and then continue with the book.
Let's begin our setup process by downloading the GLFW and GLEW libraries.
Let's get started with the setup, by performing below mentioned steps:
- First of all, we will need Visual Studio. You might have already installed it. If you did, it's fantastic. If not, go to https://visualstudio.microsoft.com/, go to
Downloads
, and then click to download theVisual Studio Community 2017 version. Then, follow the instructions and install Visual Studio onto your system. Once you've got that installed, you just want to make sure it's set up for the C++ environment.
Note
If you have the Pro version, that's fantastic, but the Community version will more than suffice.
- Next, we will download the OpenGL Extension Wrangler library. Visit http://glew.sourceforge.net/, and then click on the
Binaries
option to download the 32-bit or 64-bit version files, as per your system requirements:
Downloading binaries for GLEW
After downloading, just unzip the file and put it somewhere you find feasible to access, because for this project, and any other projects that you create, it will reference that directory. So, you don't want to be moving it around because then you will have to redo the settings for your project. For this project, it's recommended that you create a folder called OpenGL
in your C:
drive and place all the downloaded libraries in it. This will help you with easy access to the libraries while you are linking those to your project.
Note
When you extract the files, they won't be named nicely and you might find it confusing to view with all the version numbers and stuff. Therefore, to take off any versioning text, it's better that you rename the folders to something as simple as GLEW
and GLFW
. It's neat this way, you can easily know what you're doing, and it makes it a lot easier to see things.
- Once you have done that, we will move on to downloading OpenGL framework library files. Go to http://www.glfw.org/ and click on the
Download
menu. We will want to download the precompiled binaries for Windows. As seen in the following screenshot, select and click to download either the 32-bit or 64-bit version as per your system requirements:
Downloading binaries for GLFW
Note
Note: Even if you know you need to download the 64-bit version for developing on a 64-bit machine, try to stick with the 32-bit version, because unless you think your game or your application is going to be using more than 4 GB of memory, the 32- bit version will more than suffice and it will help you in maximizing compatibility.
Once you've downloaded the file, unzip it and, as mentioned before, place it in the GLFW
folder inside the OpenGL
folder.
After we have downloaded all the necessary files, we will set up the environment in Visual Studio for OpenGL using the GLFW and GLEW libraries. Follow these steps:
- Open Visual Studio, and then click on
Create new Project...:
Visual Studio start page
- Then, go to
Visual C++ | Windows Desktop | Windows Console Application
and name your projectGLFWOpenGL
, as seen in the following screenshot, and then clickOK:
Creating a new project
Note
If you don't see the Visual C++
option in the New Project
window, you might need to download Visual C++. For more information, you can visit the following link:https://docs.microsoft.com/en-us/cpp/build/vscpp-step-0-installation
- Now, right-click on the project in the
Solution Explorer
window. Go toAdd | New Item
, and you will get anAdd New Item
window. SelectC++ File
, as this will be our main file, and let's name itmain.cpp
and then click on theAdd
button.
- Next, right-click on the project in the
Solution Explorer
window. Click onProperties
. - A
Property Pages
window will pop up; click onC/C++
|General
and then go toAdditional Include Directories
. Click on the dropdown, then click on<Edit>
, and you will get a pop up window as follows:
Adding include directories
- As highlighted in the preceding screenshot, click on the new button and then click on the three dots. And now, browse to the
GLEW
folder inside theOpenGL
folder. Select theinclude
folder and then click on theSelect Folder
button. Next, we repeat the same process to add the GLFW library to our project. Once we have included both the libraries, click on theOK
button.
- Now, again in the
Property Pages
window, we'll go toLinker | General
, and then go toAdditional Library Directories
. Click on the dropdown, then click on<Edit>
, and you will get a pop-up window, as follows:
Adding libraries
- As highlighted in the preceding screenshot, click on the new button and then click on the three dots. Now, browse to the
OpenGL
folder where you have downloaded the GLEW files. Open thelib
folder inside theGLEW
folder, then double-click on theRelease
folder, selectWin32
, and then click on theSelect Folder
button. - Repeat the same process to include GLFW libraries. But for GLFW, you've got a bunch of different libraries that you could choose from. For our project, it's best that we select the
lib-vc2015
folder. Once you have added both the libraries, click on theOK
button.
Note
There are a bunch of other versions of libraries that you can choose for GLFW. So, if you have an older version of Visual Studio, you can select the library for that particular version.
- Next, we'll go to
Linker
|Input
, and then go toAdditional Dependencies
. Click on the dropdown, and then click onEdit.
What we'll do here is typeopengl32.lib
in the textbox, as seen highlighted in the following screenshot.opengl32.lib
is the library built into the operating system. Next, we'll typeglew32s.lib
. This is the static library and it will be statically linked to your project. If you don't want to statically link it, you can just remove thes
from the suffix; it is up to you. Next, we'll typeglfw3.lib
, and then click on theOK
button:
Adding additional dependencies
- Then, click on the
Apply
button.
In the preceding section, we discussed how to download the essential libraries and how to link them up to our project with absolute linking.
In the following section, we'll study how to link these libraries to our project with relative linking, and we'll also learn about how relative linking is beneficial for us. You can use either one of them to link the libraries to your project; it's your choice.
In this section, we'll take a look at how to set up OpenGL using GLFW as the provider to create a render window with relative linking. In the previous section, we discussed absolute linking, So, let's just have a quick overview of what absolute and relative linking actually are.
Absolute linking is a process where you specifically link your libraries to the project. For example, if you create a project and you are linking up libraries like GLFW and GLEW, while linking them up, you specifically put in the paths of the directory they are in. If they're in the C:
drive, you would actually put the explicit directory. But, if you move the library files to any other location, then you would have to update your Visual Studio project with the changed path.
With relative linking, the libraries are actually linked, but relative to the project. So, you don't say libraries are in the C:
drive; rather, you say those relatively link to your project from a particular folder. So even if you move your libraries, it won't affect your project. It is a great method for transporting the project from one machine to an other. This method of development is preferable when you're working on a platform that doesn't really have a good visual editor; for example, platforms, such as Unity or Unreal.
So, let's get started with relatively linking our libraries and creating an OpenGL render window. Let's open up Visual Studio and follow these steps:
- Click on
Create new project...
and go toVisual C++
|Windows Desktop
|Windows Console Application
. Name the projectGLApp
(since we are learning how to relatively link the libraries, we've created a different project). - Then, in the
New Project
window, click on theBrowse...
button. Go to theOpenGL
folder that we've created on the desktop (we are using this folder structure format to understand relative linking). Just select the folder and then clickOK
.
- One more thing you need to do before starting the project is to create a folder called
External Libraries
within theOpenGL
folder on desktop where you have saved your project. Extract the library files for GLEW and GLFW in theExternal Libraries
folder.
- Now, we'll right-click on the project in the
Solution Explorer
window. Go toAdd | New Item
. SelectC++ File
and name itmain.cpp
, and then click on theAdd
button. - Next, right-click on the project in the
Solution Explorer
window and go toProperties
. - A
Property Pages
window will pop up; click onC/C++
|General
and then go toAdditional Include Directories
. In it, click on the dropdown and then click on<Edit>
:
Adding include directories
- Then, click on the new button. As we are doing relative linking in this section, we won't click on the three dots. Clicking on them is for absolute linking only, as we have to browse to the directory where we have stored the libraries.
- In the textbox highlighted in the preceding screenshot, type
$(SolutionDir)
; this command refers to the folder that contains our.sln
file. So if we were to specify a folder in the path, whenever we do something new in the project it'd be relatively linked to wherever that file is located. - To include the files in our project, add the paths as shown in the following screenshot and then click on the
OK
button:
- Next, we'll link up the libraries. So, in the
Property Pages
window, we'll go toLinker | General
, and then go toAdditional Library Directories
. Click on the dropdown, click onEdit
, and then click onNew
. Add the paths as shown in the following screenshot, and then clickOK
and thenApply:
- Now, we've got one more thing to do, and that is to link up the
.lib
files. So, go toLinker
|Input
, and then go toAdditional Dependencies
. Click on the dropdown and then click on<Edit>
. Now, in the textbox, just typeopengl32.lib
. This library file isn't downloaded withGLFW
orGLEW
; it's built into Windows. Next, on a new line, just typeglew32s.lib
and now forGLFW lib-vc2015
, typeglfw3.lib
. Then, clickOK
and click on theApply
button.
Whichever linking process you are comfortable with, you can follow that. With either of the methods that you use to link the libraries, there's one last step that we need complete before we can begin with coding, and that is to copy and paste the dynamic link library into our project.
Let's take a look at these steps and understand how to add a dynamic link library (dll) to our project:
- Go to the
OpenGL
folder on theC:
drive; in it, go to theGLEW
folder, open it and go tobin
, double-click on it, and then go toWin32
and open it. Then, copy theglew32.dll
dynamic link library, as highlighted in the following screenshot:
glew32.dll dynamic link library
- Do as mentioned in the previous step to add the GLFW
.dll
files to your project - Now, go to the location in your system where the
main.cpp
file of your project is, and paste the copied dynamic link library file there.
With this last step, we have completed the setup for OpenGL and have also absolutely or relatively linked the libraries to our project. We are now ready to write code for the OpenGL rendering window.
In the preceding section, we discussed how to set up OpenGL on a Windows platform. But, what if any of you are working on a Mac system? Therefore, let's check out how we can download the libraries and set up OpenGL on the Mac platform.