Book Image

Learn OpenGL

By : Frahaan Hussain
Book Image

Learn OpenGL

By: Frahaan Hussain

Overview of this book

Learn OpenGL is your one-stop reference guide to get started with OpenGL and C++ for game development. From setting up the development environment to getting started with basics of drawing and shaders, along with concepts such as lighting, model loading, and cube mapping, this book will get you up to speed with the fundamentals. You begin by setting up your development environment to use OpenGL on Windows and macOS. With GLFW and GLEW set up using absolute and relative linking done, you are ready to setup SDL and SFML for both the operating systems. Now that your development environment is set up, you'll learn to draw using simple shaders as well as make the shader more adaptable and reusable. Then we move on to more advanced topics like texturing your objects with images and transforming your objects using translate, rotate and scale. With these concepts covered, we'll move on to topics like lighting to enable you to incorporate amazing dynamic lights in your game world. By the end of the book, you'll learn about model loading, right from setting up ASSIMP to learning about the model class and loading a model in your game environment. We will conclude by understanding cube mapping to bring advance worlds to your game.
Table of Contents (13 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

Setting up OpenGL using GLFW on a Mac


Here's what we've discussed so far, we discussed about how to set up our project to use GLFW library on Windows. In this section, we'll discuss how to set up OpenGL on a Mac system. So, let's get started.

Downloading the GLFW and GLEW libraries for a Mac

To download and install the essential libraries onto your Mac system, we'll have to install a package manager for Mac known as Homebrew. Homebrew will help us in installing all the necessary packages and libraries to run our OpenGL code.

To install Homebrew, go to https://brew.sh/, copy the path highlighted in the following screenshot, paste it into your Terminal, and then hit Enter. The prompt will download and install Homebrew on your system:

Path on Homebrew homepage

Once we've installed Homebrew, we'll download the GLFW and GLEW libraries onto our system. Let's install GLFW first. To do that, we need to type the following command in the Terminal window:

brew install glfw3

In the preceding command, you must have observed we've included the number 3; the reason for that is that if you just type glfw, it installs an older version, which we don't want, so inserting glfw3 will install the latest version. Hit Enter and the libraries will be downloaded onto your system.

Now, we're going to do the same process for GLEW; type the following command in the Terminal:

brew install glew 

We don't need to put any version for this; just press Enter and the necessary files will be downloaded. That's it for the libraries to download onto our system.

Note

Make a note that, since we're installing the libraries on the system itself and not in our project, whenever you move your project to a different system, you will have to install these libraries onto that particular system.

Once we've downloaded and installed all the essential libraries with the help of Homebrew, we'll now move on to setting up Xcode for OpenGL.

Note

Make sure Xcode is installed on to your system. If not, please follow these instructions and install it on your system.

Setting up Xcode for OpenGL

In this section, we'll discuss how to set up Xcode to run our OpenGL code. Follow these steps and carry out the setup process:

  1. Open up Xcode and click on the Create a new Xcode project option.
  2. Go to OS X | Application, select Command Line Tool, and then click Next.
  3. You will get the following window; fill in the necessary details, as highlighted in the following screenshot:

Basic details for a project

  1. In the preceding screenshot, make sure that the Language option is always set to C++, and then click Next. The Organization Name and Organization identifier properties, you can set to whatever you want.
  2. Next, set the location you would like to store and save the project to. Then, click on the Create button. Next, we have just a plain C++ project ready. Before we begin with our code, we need to follow a few more steps necessary to set up our project.
  3. First of all, in Xcode, click on your project and go to Build Settings. In Build Settings, go to the Search Paths section and click on Header Search Paths.Then, click on + and type /usr/local/include. This will allow us to #include GLEW and GLFW in our main.cpp file.
  4. Now go to Build Phases, then click on Link Binary With Libraries, and click the + button. Type opengl in the search bar, select OpenGL.framework, and then click on the Add button.
  1. Again click on the + button, then click on Add Other.... Now, press Cmd + Shift + G, and it will open up a go-to folder search bar. In it, type /usr/local. Then click on Cellar, go to the glew | lib folder, select libGLEW.1.12.0.dylib without the little arrow, and then click Open.

Note

The arrow is just a shortcut, an alias, and we don't want that. We also don't want the MX version, just the regular .dy non-alias lib.

  1. Click + again, then click Add Other... , press Cmd + Shift + G, and type /usr/local. Now go to Cellar, and go to glfw | lib. Select the non-alias libglfw3.3.1.dylib and click Open.

With all the steps executed, our project is now set up to use GLEW and GLFW with OpenGL on Mac. We can now go to the main.cpp file on Xcode and start writing our code for creating the OpenGL rendering window.