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

Customizing the Render Pipeline

In Chapter 5, Let’s Light It Up!, we discussed adding color, textures, and lights to a scene. In our project, we colored vertices, applied textures, and turned on lights with single OpenGL calls.

Now in this chapter we will investigate how the color of all pixels is calculated through the use of shaders. When working with shaders, all the mathematics is revealed. With a few modifications to your project, by the end of this chapter you will be using shaders for the following purposes:

  • Coloring and texturing mesh faces
  • Turning on the lights

We will begin by grabbing the UV values that come with the OBJ model file and passing these values and a texture to a vertex and fragment shader for processing. This will allow us to color a model using an external image. Following this, because a plain image on a model will look rather flat, we’ll examine the fundamental lighting models that have been traditionally used to emulate...

Technical requirements

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

Coloring and texturing mesh faces

Thus far, in our OpenGL project, we have implemented simple white mesh rendering. Now, it’s time to add the functionality of coloring and texturing polygons and pixels. The logic is similar to that of vertex coloring and UV mapping, as discussed in Chapter 5, Let’s Light It Up! Though now, when using shaders to do the rendering, the colors are added vertex by vertex and pixel by pixel.

A color is allocated to a vertex via the vertex shader and then passed to the fragment shader. The color for each vertex will specify the color of the pixel representing the vertex, but not the colors in between. Take, for example, the close-up of our teapot model in Figure 18.1:

Figure 18.1: Polygons colored using vertex colors

The colors for each vector are indicated by (a), (b), and (c). This means the fragment shader must interpret what the value for each pixel between the vertices will be. As you can see from Figure 18.1...

Turning on the lights

When it comes to lighting in shaders, there’s a lot of hands-on mathematics. No longer can we rely on nice, neat OpenGL function calls in our main code. Instead, we must calculate the color of a pixel and consider the lighting in the fragment shader.

In the previous section, when you completed the exercise and loaded the granny model with the texture, it was unlit. It displayed the colors as they appear in the PNG texture file. To include lighting effects, we must determine how the light will influence the original color of the pixel taken from the texture. Over the years, a gradual improvement has been made to lighting models, which is evident if you take a look at the quality of computer graphics from 20 years ago up until now.

In this section, we will examine some popular lighting models and apply the mathematics to our fragment shader to light up the model.

Ambient lighting

Ambient lighting is lighting in a scene that has no apparent source...

Summary

In this chapter, we changed over the functionality of our project, which was using older OpenGL methods, and replaced the rendering functions with our own vertex and fragment shaders. The shader code we write gets compiled into a program for the graphics processor. You will now have an understanding of how an external script or program written in Python interacts with the shader program. The majority of graphics and games programs are interactive, so being able to process transformations, projections, and camera movements in one program, and send commands to the graphics card for rendering, is a necessary skill for any graphics programmer.

The lighting models we examined here were first created by computer scientists to light scenes with 3D models to create more believable results. Though they are effective and still used today, they don’t quite address the physical nature of the way light interacts in real life.

Nowadays, the more common way to light a scene...

Answers

Exercise A:

The code only differs from loading the teapot since it loads the granny model:

quat_granny = Object(“Granny”)
quat_granny.add_component(Transform())
mat = Material(“shaders/texturedvert.vs”, 
               “shaders/texturedfrag.vs”)
quat_granny.add_component(LoadMesh(quat_granny.vao_ref,
    mat,GL_TRIANGLES, “models/granny.obj”, 
    “images/granny.png”))
quat_granny.add_component(mat)
quat_trans: Transform =
    quat_granny.get_component(Transform)
quat_trans.update_position(pygame.Vector3(0, -100, -200))

In the fragment shader, remove the normal from the setup of frag_color (if you aren’t on Windows and haven’t commented out all the error-producing code), like this:

frag_color = vec4(1,1,1,1)
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