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 SDL on Windows


In this section, we'll discuss how to set up OpenGL on a Windows machine using SDL and GLEW. SDL stands for Simple DirectMedia Layer, and it allows us to create a render window and provides access to input devices through OpenGL. SDL is prominently used to code games and other media applications that run on various operating systems. It's a cross-platform multimedia library written in the C language. GLEW (OpenGL Extension Wrangler), as seen in the previous sections, allows us to easily use extensions and non-core OpenGL functionality.

Downloading the SDL library

We'll begin the setup by downloading the essential libraries. Let's first download the SDL library by following these steps:

  1. Visit http://libsdl.org/index.php, go to Download, and click on the latest version; at the time of writing this book, SDL 2.0 was the latest version.
  2. Once you have clicked on the latest version, you may either want to download the development libraries or the runtime libraries. For this project, it is recommended that you download Development Libraries.
  3. We'll go for the Visual C++ one, which is SDL2-devel-2.0.8-VC.zip. Click on the filename and download it.
  4. Once you've downloaded the file, unzip it and put it in the SDL folder inside the OpenGL folder that we created in previous sections.
  5. After downloading the SDL library, we move on to downloading the GLEW libraries, but as we've already downloaded them in the previous sections, you can just refer to that.

Note

If you want a quick review on downloading GLEW, you can refer to the Downloading the essential libraries section at the start of the chapter.

Setting up OpenGL using SDL and GLEW with absolute linking

Follow these steps to set up the environment in Visual Studio for OpenGL using SDL and GLEW with absolute linking:

  1. Open up Visual Studio and click on Create new Project... in the home page window.
  2. Go to Visual C++ | Windows Desktop | Windows Console Application, name your project SDLOpenGL, and then click OK.
  3. Next, right-click on the project in the Solution Explorer window. Click on Properties.
  4. 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.
  5. Click on the New button, and then click on the three dots. And now, you want to go to SDL in the OpenGL folder. Select include and then click on the Select Folder button. Repeat the same process for including GLEW files. Once both the files have been included, click on the OK button.
  6. 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.
  7. In the window, click on the New button, then click on the three dots, and go to the SDL folder. Open the lib folder, go to x86 (which is a 32-bit file, actually), and then click on the Select Folder button.
  8. Repeat the same process for including GLEW libraries. Open the lib folder, then double-click on the Release folder, select Win32, and then click on the Select Folder button. Once you have added both the libraries, click on the OK button.
  9. Next, we'll go to Linker|Input, and then go to Additional Dependencies. Click on the dropdown, then click on Edit, and type opengl32.lib. Then, we'll type glew32s.lib. If you don't want to statically link the library, you can just remove the s. Next, we'll type SDL2.lib and SDL2main.lib, and then click on OK.
  10. Then, click on the Apply button.

Setting up OpenGL using SDL and GLEW with relative linking

In this section, we'll take a look at how to set up OpenGL using SDL and GLEW as the provider for creating a render window with relative linking. Follow these steps:

  1. Click on Create new project... and go to Visual C++. Select Windows Console Application and name it something like SDLApp.
  2. Then, in the New Project window, click on the Browse... button. Go to the OpenGL folder that you created on the desktop and placed the downloaded libraries into External Libraries. Just select the folder and then click OK.
  3. Now, we'll 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 entry point; let's name it main.cpp and then click on the Add button.
  4. Next, again right-click on the project in the Solution Explorer window. Click on Properties.
  5. A Property Pages window will pop up. Click on C/C++ General and then go to Additional Include Directories. Click on the dropdown, and then click on Edit.
  6. Then, click on the New button and type $(SolutionDir) in the textbox. This command refers to the folder that contains our .sln file. So if we were to specify a folder in the path, and whenever we do something new in the project, it'd be relatively linked to wherever that file is located.
  1. To link up the include files, add the paths, as shown in the following screenshot:

  1. Next, we'll link up the libraries. So, go to Linker | General, and then go to Additional Library Directories. Click on the dropdown and then click on Edit. Click on New and add the paths, as shown in the following screenshot, then click OK, and click Apply:
  1. Next, we'll link up the .lib files. So, go to the dropdown and click Edit. Now, just type in opengl32.lib. Then, we'll type glew32s.lib. Next, we'll type SDL2.lib and SDL2main.lib, and then click on OK.
  2. Then, click on the Apply button.

Adding a DLL file to the project

As we saw in the previous sections, before completing the setup, we'll have to copy the dynamic link library into our project. Follow these steps to do that:

  1. Go to C:\OpenGL\SDL\lib\x86 and copy the SDL2.dll dynamic link library, as seen in the following screenshot:

SDL2.dll dynamic link library

  1. Now, go to the location in your system where the main.cpp file of your project is located and paste the dynamic link library there. We'll also have to copy and paste the glew32.dll file here from the bin folder of the GLEW folder.