Reader small image

You're reading from  Mathematics for Game Programming and Computer Graphics

Product typeBook
Published inNov 2022
PublisherPackt
ISBN-139781801077330
Edition1st Edition
Tools
Right arrow
Author (1)
Penny de Byl
Penny de Byl
author image
Penny de Byl

Penny de Byl is a full stack developer with an honors in graphics and Ph.D. in artificial intelligence for games. She has a passion for teaching, teaching games development and computer graphics for over 25 years in universities in Australia and Europe. Her best-selling textbooks, including Holistic Game Development with Unity, are used in over 100 institutions. She has won numerous awards for teaching, including an Australian Government Excellence in Teaching Award and the Unity Mobile Game Curriculum Competition. Her approach to teaching computer science and related fields is project-based giving you hands-on workshops you can immediately get your teeth into. The full range of her teaching interests can be found at H3D Learn.
Read more about Penny de Byl

Right arrow

Rendering Visual Realism Like a Pro

As graphics cards and the resolution of computer displays have improved over time, audiences have expected better quality rendering in animated movies, games, and other computer graphics-based media. By examining the way that objects in the real world get their color, computer scientists have been able to improve upon similar models of Lambert and Phong. Today, most 3D engines aimed at producing visual realism apply a shading technique called physically based rendering (PBR).

In this chapter, we will investigate the theory behind this technique and then put it into practice in our Pygame/OpenGL project. To this end, we will be discussing the following topics in this chapter:

  • Following where light bounces
  • Applying the Inverse Square Law
  • Calculating Bidirectional Reflectance
  • Putting it all together

By the end of this chapter, you will have a project that uses Python and OpenGL to render objects using PBR and understand...

Technical requirements

The solution files containing the code in this chapter can be found on GitHub at https://github.com/PacktPublishing/Mathematics-for-Game-Programming-and-Computer-Graphics/tree/main/Chapter19 in the Chapter19 folder.

Following where light bounces

PBR is based on the actual physics of light rather than the other relatively simple lighting model of Lambert, examined in Chapter 5, Let’s Light It Up! PBR is a concept rather than a specific algorithm and can be achieved using a variety of mathematical models. To understand how PBR works, we need to understand some key fundamentals about the visual way light works.

Light is a ray we can represent with vectors relative to the normal of the surface being hit, as shown in Figure 19.1:

Figure 19.1: An incident and reflection ray

The light coming in from the source is called the incident ray and the light being reflected from the surface is called the reflection ray. According to the law of reflection, the angle of incidence is equal to the angle of reflection. Both rays travel in a straight line, and whether the strength of the incoming ray is the same as the reflected ray depends on what happens at the point of collision...

Applying the Inverse Square Law

The way that the strength of light gets weaker with distance from the light source is described by the inverse square law. It states that the light intensity gets inversely weaker based on the square of the distance the viewer is away from the light source. Mathematically, we represent it like this:

Just how quickly the light strength falls off with distance will depend on the medium through which the light is traveling. We can calculate the strength of light at a certain distance in the same medium if we know its strength for a previously measured distance. For example, if the light intensity is 10 at a distance of 100 meters from the source, we can calculate the strength that this same light will be at 125 meters, using proportions like this:

This answer makes sense if we think about it as the same light at a further distance being less bright.

The strength of the light being emitted from the light source, as we discussed...

Calculating Bidirectional Reflectance

Besides ordinary reflectance and scattering, PBR also integrates a bidirectional reflectance distribution function (BRDF), which considers how a specular reflection will fall off or how fuzzy it appears around the edges. It is a function that considers the four factors of the incident ray, the vector to the viewer, the surface normal, and radiance (how well the surface reflects light). In fact, the Lambert (diffuse) and Phong (specular) models we considered in Chapter 18, Customizing the Render Pipeline, are examples of BRDFs. The BRDF for Phong, which calculates specular lighting that can be added to the diffuse of Lambert for a final effect, can be stated as the following:

In this formula, R is the vector of reflection of the incoming light, E is the vector from the point of contact to the viewer’s eye, and p is the specular power. All vectors involved in calculating reflections are shown in Figure 19.4:

Figure...

Putting it all together

PBR lighting models are used in many game engines, including Unity and Unreal. Walt Disney Pictures and Pixar also use PBR to light their 3D animations, and in fact, the models you’ve learned about herein are used in their graphics tools.

What distinguishes the BRDF used by PBR is that it allows for the use of parameters. These parameters allow you to customize the look of the shader and define the surface qualities of objects, using albedo for the diffuse color and values for metallicness, roughness, and ambient occlusion (AO).

Now, it’s time to put all this theory into practice, so we can see it at work in our Python/OpenGL project.

Let’s do it…

In this exercise, we will rework the project to pass the settings for albedo, metallic, roughness, and ambient occlusion through to the shaders, in addition to adding multiple lights:

  1. Make a copy of the Chapter_18 folder and rename it Chapter_19.
  2. You will need a...

Summary

As you’ve explored in this chapter, there’s a lot of mathematics involved in creating shaders, although the basics still focus on the vectors that explain the direction of a surface with respect to the position of the light and the location of the viewer. With the addition of a few extra PBR parameters of metallicness, roughness, and AO, we are now also able to define how a surface scatters light and use that to improve a final render.

Your Python/OpenGL project is now at the point that you can continue to independently research graphics and shader techniques and experiment with them in the base that you have. You will now have a firm foundation of knowledge in this area that you can apply in the future to games and other applications alike.

The domain of mathematics involved in computer games and graphics is enormous. Unfortunately, books have page limits and authors have limited writing time. To cover everything in this field would require a set of encyclopedic...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mathematics for Game Programming and Computer Graphics
Published in: Nov 2022Publisher: PacktISBN-13: 9781801077330
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

Author (1)

author image
Penny de Byl

Penny de Byl is a full stack developer with an honors in graphics and Ph.D. in artificial intelligence for games. She has a passion for teaching, teaching games development and computer graphics for over 25 years in universities in Australia and Europe. Her best-selling textbooks, including Holistic Game Development with Unity, are used in over 100 institutions. She has won numerous awards for teaching, including an Australian Government Excellence in Teaching Award and the Unity Mobile Game Curriculum Competition. Her approach to teaching computer science and related fields is project-based giving you hands-on workshops you can immediately get your teeth into. The full range of her teaching interests can be found at H3D Learn.
Read more about Penny de Byl