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

Preface

OpenGL is the most popular graphics library in the world; most mobile games use OpenGL and many other applications.  In this book, you'll get to learn about the fundamentals that make the awesome games we play and the game engines behind them. A step-by-step process is used to show everything, from setting up OpenGL to its essential modern features. You'll gain a good understanding of the following concepts: setting up on Windows and Mac using GLFW, SDL, and SFML, 2D drawing, 3D drawing, texturing, lighting, 3D rendering, Shaders/GLSL, model loading, and cube mapping. 

Who this book is for

Learn OpenGL is for 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.

What this book covers

Chapter 1, Setting Up OpenGL, In this chapter, you'll get to learn how to set up OpenGL using various libraries: GLFW, GLEW, SDL, and SFML. We'll learn how to set up our OpenGL projects on Windows and Mac. We also discuss how to link the libraries to your projects using absolute or relative linking, and eventually create the rendering windows to display OpenGL graphics.

Chapter 2, Drawing Shapes and Applying Textures, takes you through drawing various shapes using shaders. We'll begin by drawing a triangle and learn to add color to it. Then, we'll use the triangle concept to draw our quadrilateral and also learn how to add texture to it. 

Chapter 3, Transformations, Projections, and Camera,  This chapter further builds on the previous chapter. You'll get to grips with applying transformations such as rotation and translation to our shapes, and learn to draw a cube and apply a texture to it. Then, we explore the concepts of projections (perspective and orthographic) and implement those concepts in our game world.

Chapter 4, Effects of lighting, Materials, and Lightmaps, In this chapter we'll learn to apply colors to our objects and how to create a light source such as a lamp in our game world. We'll then look at the effects of light on the objects. You'll understand different types of lighting techniques: ambient, diffused, specular lighting. We'll also explore the various real worl materials and observe the effects of light on the materials. You'll also get to learn about lightmaps in this chapter. 

Chapter 5, Types of light sources and combining of lights, This chapter will discuss the different types of light sources such as directional lights, point lights, and spotlights. We'll also learn to combine the lighting effects and the light sources for our game world.      

Chapter 6, Implementing a Skybox Using a Cubemap, In this chapter, you'll generate a skybox using a cubemap. You'll learn how to apply textures to the skybox and then create a separate texture file to make loading of textures in the code easier. You'll also learn to draw the skybox and create our game world using it. 

Online Chapter, Model Loading, This is a bonus chapter available at https://www.packtpub.com/sites/default/files/downloads/ModelLoading.pdf. In this chapter, you'll learn how to setup Assimp (Open Asset Import Library) on Windows using CMake for all our Model Loading needs. We'll also cover setting up Assimp on Mac OS X and creating a cross-platform Mesh Class. Then we'll explore how to load a 3D model into our game. You'll also get to learn how to create a Model Class to handle loading of our model.  

To get the most out of this book

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.

Disclaimer 

The illustrations used in this book are for illustrative purposes only. We do not recommend you to misuse these in any way. For more information please consult the terms and conditions of the publishers mentioned here.

Nintendo : https://www.nintendo.com/terms-of-use

Download the example code files

You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packt.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packt.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Learn-OpenGL. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/LearnOpenGL_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Extract the library files for GLEW and GLFW in External Libraries folder. "

A block of code is set as follows:

   SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); 
   SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); 
   SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); 

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

   SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); 
   SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); 
   SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); 

Any command-line input or output is written as follows:

brew install glfw3
brew install glew

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Open up Xcode and click on the Create a new Xcode project option."

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packt.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packt.com.