• Home
  • Textbooks
  • Mathematics for Game Programming and Computer Graphics: Explore the essential mathematics for creating, rendering, and manipulating 3D virtual environments
  • Let’s Light It Up!

Mathematics for Game Programming and Computer Graphics: Explore the essential mathematics for creating, rendering, and manipulating 3D virtual environments

Penny de Byl

Chapter 5

Let’s Light It Up! - all with Video Answers

Educators


Section 1

Technical requirements Adding Lighting Effects

Problem 1

Create a new Python script called HelloLights . py and copy the exact same code from the previous rotating cube in the Chapter 4, Graphics and Game Engine Components script in HelloMesh.py.

Check back soon!

Problem 2

Edit Mesh3D.py and change the line from this:
glBegin (GL_LINE_LOOP)
Change it to this:
glBegin (GL_POLYGON)

Check back soon!

Problem 3

Run this application. You will get a black window with the white silhouette of a solid rotating cube.

Check back soon!

Problem 4

Now, modify HelloLights . py with the following lines:
done $=$ False
white $=$ pygame. $\operatorname{Color}(255,255,255)$
glMatrixMode (GL_PROJECTION)
gluPerspective(60, (screen_width / screen_height),
0.1, 100.0)
glMatrixMode(GL_MODELVIEW)
glTranslatef(0.0, 0.0, -3)
glEnable(GL_DEPTH_TEST)
mesh = Cube()

Check back soon!

Problem 5

Let's turn on some lights. Add in the code to enable them:
.
glTranslatef $(0,0,-4)$
glEnable (GL_DEPTH_TEST)
glEnable (GL_LIGHTING)
while not done:
. .
Now, when you run the application, the lights will be on, but they might not look like they are because you will see a very dull version of the rotating cube. This is a kind of ambient lighting. Just because the lights are enabled doesn't spontaneously create any lights. We have to do that manually.

Check back soon!

Problem 6

To create a light and turn it on, add the following:
.
glEnable (GL_DEPTH_TEST)
glEnable (GL_LIGHTING)
glLight (GL_LIGHT0, GL_POSITION, (5, 5, 5, 1))
glEnable (GL_LIGHT0)
while not done:
.

Check back soon!

Problem 7

To control the ambient, diffuse, and specular colors from GL_LIGHT0, add the following:
.
glLight (GL_LIGHT0, GL_POSITION, (5, 5, 5, 1))
glLightfv(GL_LIGHT0, GL_AMBIENT, ( $1,0,1,1)$ )
glLightfv(GL_LIGHT0, GL_DIFFUSE, $(1,1,0,1)$ )
glLightfv (GL_LIGHT0, GL_SPECULAR, $(0,1,0,1)$ )
glEnable (GL_LIGHTO)
. .

Check back soon!

Problem 8

Run the application to see a fully lit cube, as shown in Figure 5.3:
(Figure can't copy)
Each of the given light types is succeeded by a four-valued red, green, blue, and alpha (transparency) value. The light will be positioned in the world at $(5,5,5)$ and have a magenta ambient color, a yellow diffuse color, and a green specular color. Note that OpenGL requires color channel values to be specified between 0 and 1 .

Check back soon!