Learn OpenGL
eBook - ePub

Learn OpenGL

Beginner's guide to 3D rendering and game development with OpenGL and C++

Frahaan Hussain

Share book
  1. 208 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Learn OpenGL

Beginner's guide to 3D rendering and game development with OpenGL and C++

Frahaan Hussain

Book details
Book preview
Table of contents
Citations

About This Book

A step-by-step instructional guide to understanding the fundamentals of game development with OpenGL. Right from the setup to the important features, we'll get a better understanding of games and the engines behind them.

Key Features

  • Learn the basics of drawing along with fundamentals of shading to create amazing objects.
  • Get in-depth knowledge of lighting and materials to make realistic objects.
  • Understand the fundamentals of model loading and cube mapping.

Book Description

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.

What you will learn

  • Set up GLFW and GLEW on Windows and macOS with absolute, relative Linking
  • Set up SDL and SFML on your system using absolute and relative Linking
  • Draw using the simple shaders
  • Create a camera and learn to populate your game world with objects
  • Learn about color and lighting concepts to create an amazing game world
  • Understand model loading and cube mapping to advance your game

Who this book is for

This book is targeted towards anyone and everyone who is interested in creating games, learning how game engines work and most importantly for anyone who is interested in learning OpenGL. The ideal reader for this book would be anyone with a passion for learning game development or looking out for an OpenGL reference guide. The skills that you'll learn in this book will be applicable to all your game development needs. You'll require a strong foundation in C++ to understand and apply the concepts of this book.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Learn OpenGL an online PDF/ePUB?
Yes, you can access Learn OpenGL by Frahaan Hussain in PDF and/or ePUB format, as well as other popular books in Computer Science & Software Development. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781789340860
Edition
1

Setting Up OpenGL

Welcome to the world of modern OpenGL. Open Graphics Library (OpenGL) is an API that provides developers with various sets of functions that enable them to manipulate graphics and images. It is the core framework of most games today, whether it's mobile games for iOS or Android, or for other platforms, such as desktops and consoles as well. OpenGL just speaks for itself. Take a look at any sort of game you can think of, and it is doable in OpenGL. It's not just restricted to 3D; you can create 2D games as well. The 2D and 3D game engines are created using OpenGL, so it is more than capable of anything that you can think of. In this book, we will learn all the concepts necessary for 3D game development.
In this chapter, we'll take a look at how to set up OpenGL on Windows and Mac, using various libraries, such as GLFW, GLEW, SDL, and SFML. Our main focus for this chapter will be to understand how to download the libraries and set up OpenGL using those. While we are learning about how to set up our projects, we will also take a look at how to link these libraries to our project using absolute and relative linking.
The following topics will be covered in this chapter:
  • Downloading the essential libraries
  • Setting up a project on different platforms to use the libraries
  • Creating an OpenGL rendering window using the libraries
You can refer to all the code files for this chapter in the Chapter01 folder on GitHub. The GitHub link can be found in the preface of the book.

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 c...

Table of contents