Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learn OpenGL

You're reading from   Learn OpenGL Beginner's guide to 3D rendering and game development with OpenGL and C++

Arrow left icon
Product type Paperback
Published in Aug 2018
Publisher Packt
ISBN-13 9781789340365
Length 208 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Frahaan Hussain Frahaan Hussain
Author Profile Icon Frahaan Hussain
Frahaan Hussain
Arrow right icon
View More author details
Toc

Setting up OpenGL using GLFW and GLEW on Windows

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.

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.

Downloading the essential libraries

Let's get started with the setup, by performing below mentioned steps:

  1. 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 the Visual 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.
If you have the Pro version, that's fantastic, but the Community version will more than suffice.
  1. 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.

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.
  1. 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: 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.

Linking GLFW and GLEW libraries with absolute linking

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:

  1. Open Visual Studio, and then click on Create new Project...:
Visual Studio start page
  1. Then, go to Visual C++ | Windows Desktop | Windows Console Application and name your project GLFWOpenGL, as seen in the following screenshot, and then click OK:
Creating a new project
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
  1. Now, right-click on the project in the Solution Explorer window. Go to Add | New Item, and you will get an Add New Item window. Select C++ File, as this will be our main file, and let's name it main.cpp and then click on the Add button.
  1. Next, right-click on the project in the Solution Explorer window. Click on Properties.
  2. A Property Pages window will pop up; click on C/C++ | General and then go to Additional Include Directories. Click on the dropdown, then click on <Edit>, and you will get a pop up window as follows:
Adding include directories
  1. 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 the OpenGL folder. Select the include folder and then click on the Select 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 the OK button.
  1. Now, again in the Property Pages window, we'll go to Linker | General, and then go to Additional Library Directories. Click on the dropdown, then click on <Edit>, and you will get a pop-up window, as follows:
Adding libraries
  1. 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 the lib folder inside the GLEW folder, then double-click on the Release folder, select Win32, and then click on the Select Folder button.
  2. 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 the OK button.
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.
  1. Next, we'll go to Linker | Input, and then go to Additional Dependencies. Click on the dropdown, and then click on Edit. What we'll do here is type opengl32.lib in the textbox, as seen highlighted in the following screenshot. opengl32.lib is the library built into the operating system. Next, we'll type glew32s.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 the s from the suffix; it is up to you. Next, we'll type glfw3.lib, and then click on the OK button:
Adding additional dependencies
  1. 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.

Linking GLFW and GLEW libraries with relative linking

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:

  1. Click on Create new project... and go to Visual C++ | Windows Desktop | Windows Console Application. Name the project GLApp (since we are learning how to relatively link the libraries, we've created a different project).
  2. Then, in the New Project window, click on the Browse... button. Go to the OpenGL 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 click OK.
  1. One more thing you need to do before starting the project is to create a folder called External Libraries within the OpenGL folder on desktop where you have saved your project. Extract the library files for GLEW and GLFW in the External Libraries folder.
  1. Now, we'll right-click on the project in the Solution Explorer window. Go to Add | New Item. Select C++ File and name it main.cpp, and then click on the Add button.
  2. Next, right-click on the project in the Solution Explorer window and go to Properties.
  3. A Property Pages window will pop up; click on C/C++ | General and then go to Additional Include Directories. In it, click on the dropdown and then click on <Edit>:
Adding include directories
  1. 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.
  2. 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.
  3. To include the files in our project, add the paths as shown in the following screenshot and then click on the OK button:
  1. Next, we'll link up the libraries. So, in the Property Pages window, we'll go to Linker | General, and then go to Additional Library Directories. Click on the dropdown, click on Edit, and then click on New. Add the paths as shown in the following screenshot, and then click OK and then Apply:
  1. Now, we've got one more thing to do, and that is to link up the .lib files. So, go to Linker | Input, and then go to Additional Dependencies. Click on the dropdown and then click on <Edit>. Now, in the textbox, just type opengl32.lib. This library file isn't downloaded with GLFW or GLEW; it's built into Windows. Next, on a new line, just type glew32s.lib and now for GLFW lib-vc2015, type glfw3.lib. Then, click OK and click on the Apply 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.

Adding a dynamic link library to the project

Let's take a look at these steps and understand how to add a dynamic link library (dll) to our project:

  1. Go to the OpenGL folder on the C: drive; in it, go to the GLEW folder, open it and go to bin, double-click on it, and then go to Win32 and open it. Then, copy the glew32.dll dynamic link library, as highlighted in the following screenshot:
glew32.dll dynamic link library
  1. Do as mentioned in the previous step to add the GLFW .dll files to your project
  2. 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.

You have been reading a chapter from
Learn OpenGL
Published in: Aug 2018
Publisher: Packt
ISBN-13: 9781789340365
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime