Reader small image

You're reading from  C++ Game Animation Programming - Second Edition

Product typeBook
Published inDec 2023
Reading LevelN/a
PublisherPackt
ISBN-139781803246529
Edition2nd Edition
Languages
Tools
Concepts
Right arrow
Authors (2):
Michael Dunsky
Michael Dunsky
author image
Michael Dunsky

Michael Dunsky is an educated electronics technician, game developer, and console porting programmer with more than 20 years of programming experience. He started at the age of 14 with BASIC, adding on his way Assembly language, C, C++, Java, Python, VHDL, OpenGL, GLSL, and Vulkan to his portfolio. During his career, he also gained extensive knowledge in virtual machines, server operation, infrastructure automation, and other DevOps topics. Michael holds a Master of Science degree in Computer Science from the FernUniversität in Hagen, focused on computer graphics, parallel programming and software systems.
Read more about Michael Dunsky

Gabor Szauer
Gabor Szauer
author image
Gabor Szauer

Gabor Szauer has been making games since 2010. He graduated from Full Sail University in 2010 with a bachelor's degree in game development. Gabor maintains an active Twitter presence, and maintains a programming-oriented game development blog. Gabor's previously published books are Game Physics Programming Cookbook and Lua Quick Start Guide, both published by Packt.
Read more about Gabor Szauer

View More author details
Right arrow

2

Building an OpenGL 4 Renderer

Welcome to Chapter 2! In the previous chapter, you learned how to open an application window, including the OpenGL context, and how to perform a very basic operation: clearing the screen in different colors. More actions were not possible due to the limited OpenGL support included in GLFW.

In this chapter, you will learn how to get access to the OpenGL function calls and extensions using a “loader” helper, which is a small piece of code that maps the OpenGL functions to the entry points of the installed system library. We could also do this mapping in our own code, but this would require a lot of extra work. The OpenGL renderer will be enhanced during the book – as the first step, we will only display a textured quad on the screen, consisting of two triangles.

In this chapter, we will cover the following topics:

  • The rendering pipeline of OpenGL 4
  • Basic elements of our OpenGL 4 renderer
  • Buffer types for the...

Technical requirements

For this chapter, you will need the following:

  • Main.cpp and the OpenGL window code from Chapter 1
  • Glad, the OpenGL loader generator
  • stb_image, a single-header loader for image files
  • The OpenGL Mathematics (GLM) library (installed with the Vulkan SDK)

The rendering pipeline of OpenGL 4

OpenGL is one of the most used graphics libraries to render objects in 3D, and also 2D, to the screen. It is not just the world, buildings, trees, or characters that are drawn using OpenGL; other elements (such as the user interface or a 2D map) are brought to the screen with the help of OpenGL draw calls.

The library has faced several evolutionary steps since its initial release in 1992, with each version giving the developer more and more control of the underlying graphics hardware. While the rendering pipeline in OpenGL had only limited features and fixed operations, the latest version (4.6) offers high flexibility for all components. All green components are programmable in the later versions:

Figure 2.1: The OpenGL graphics pipeline

Figure 2.1: The OpenGL graphics pipeline

Figure 2.1 can be understood as follows:

  1. The characters we will draw are made of triangles, and the Vertex Data of these triangles is sent from our application to the graphics...

Basic elements of the OpenGL 4 renderer

To be able to use OpenGL in our code, we need access to its functions and extensions. Unfortunately, the graphics card vendors do not create these functions in an easy-to-use way. They are stored as function pointers, which will be hard to use.

To translate the function pointers back to more human-readable function names, several helper libraries exist. We will use the Glad tool for this translation.

The OpenGL loader generator Glad

Glad is a free and open source loader generator; all parts to make OpenGL fully available for us are included. Glad supports all OpenGL versions, even back to the first version (1.0), plus the mobile variant OpenGL for Embedded Systems (ES) and the platform-specific APIs for Microsoft Windows and the X Window System of Unix.

You can access the web service at https://glad.dav1d.de, which should open this screen:

Figure 2.2: The Glad web service, version selection

Figure 2.2: The Glad web service, version selection

Select OpenGL...

Loading and compiling shaders

A shader is a small program running on the graphics card, which has special computing units for them. Modern GPUs have thousands of shader units to be able to run the shaders in a massively parallel fashion, which is one of the reasons for the high-speed drawing of pictures of 3D worlds.

The OpenGL rendering pipeline uses several shader types, as seen in Figure 2.1, but we will use only two of the types here: vertex shaders and fragment shaders, the first and last steps in the pipeline. There are more shader types, such as geometry or tessellation shaders, and also shaders outside the normal pipeline such as compute shaders, which are used for simple but fast computation in the shader units.

Let’s take a closer look at the two shader types we will use in the OpenGL renderer to draw the objects to the screen: the vertex and fragment shaders.

Vertex and fragment shaders

A vertex shader uses the uploaded vertex data as input and transforms...

Summary

In this chapter, we created a quite simple OpenGL renderer, consisting of the renderer itself, plus helper classes for a framebuffer, vertex array objects, textures, and shaders. This renderer enables us to draw triangles on the screen, and the data is taken from a Model class. The current minimalistic model will be extended in later chapters when we will take care of model loading and animations.

In the next chapter, we will take a look at the Vulkan API and create a renderer to show the same textured two triangles with it. You will learn about the similarities and differences between OpenGL and Vulkan, and we will use helper libraries to lower the amount of code.

Practical sessions

There are some additions you could make to the code:

  • Add log lines with the Logger class to all the methods we implemented. This will help a lot if you need to debug problems, as you can also output values used in the methods.
  • Read the failure logs during shader compilation and linking. This is a bit tricky because you need to get the length of the log first, allocate a dynamic buffer (that is, by using std::vector), and get the log contents into this buffer. You will get a detailed error log and see the faulty line and operation or data type.
  • Add support for different file formats in the Texture class. Right now, there’s only support for PNGs in the RGBA component order. Try to also add other formats, such as JPG, or even more exotic variants such as ARGB or the reversed BGRA.

Additional resources

For further reading, please check these links:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
C++ Game Animation Programming - Second Edition
Published in: Dec 2023Publisher: PacktISBN-13: 9781803246529
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.
undefined
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 $15.99/month. Cancel anytime

Authors (2)

author image
Michael Dunsky

Michael Dunsky is an educated electronics technician, game developer, and console porting programmer with more than 20 years of programming experience. He started at the age of 14 with BASIC, adding on his way Assembly language, C, C++, Java, Python, VHDL, OpenGL, GLSL, and Vulkan to his portfolio. During his career, he also gained extensive knowledge in virtual machines, server operation, infrastructure automation, and other DevOps topics. Michael holds a Master of Science degree in Computer Science from the FernUniversität in Hagen, focused on computer graphics, parallel programming and software systems.
Read more about Michael Dunsky

author image
Gabor Szauer

Gabor Szauer has been making games since 2010. He graduated from Full Sail University in 2010 with a bachelor's degree in game development. Gabor maintains an active Twitter presence, and maintains a programming-oriented game development blog. Gabor's previously published books are Game Physics Programming Cookbook and Lua Quick Start Guide, both published by Packt.
Read more about Gabor Szauer