3D Game Programming

(C. Jardin) #1
We could have used 1 for each of the numbers and the effect would be the
same. The sun would still be shining down from the same direction in the
sky (to the right, the front, and up). We used 250 not so that the “sun” will be
far away, but rather because moving the position of the light far away moves
the light’s shadow box.

In addition to describing the location of the directional light in the sky, the
position of a directional light provides a starting point for the shadow box. It
would be too much work for a computer to try to draw shadows everywhere
in a scene. The shadow box is the area within a scene where shadows are
drawn. The box starts from the directional light’s position.

The remaining properties in the addSunlight() function describe the shadow box
for this game. The shadowCameraNear property specifies how far away from the
light we want shadows to appear. In this case, we don’t need shadows until
the light is 200 away from the camera. By setting shadowCameraFar to 600 , we’re
telling the camera that it can stop drawing shadows after a distance of 600.
The thickness of the box is then 400 , which helps the computer do less work
making shadows so that it can spend more time on more-important tasks.

The shadowCameraLeft, shadowCameraRight, shadowCameraTop, and shadowCameraBottom
properties describe how wide and long the shadow box should be. All of these
were chosen by experimentation. Feel free to come back to play with them
yourself after you’ve finished coding the game.

The last two numbers, shadowMapWidth and shadowMapHeight, describe how much
detail we want in the shadows. Larger numbers mean more details (and more
work for the computer, so don’t make them too big). The normal value of 512 ,
which would have been used if we didn’t set these properties at all, is too low
for our purposes. The shadows would have been too hard to see. The values
of 4096 were found through art, or just random experimentation.

With light added to our river-rafting game, let’s add another important thing:
the scoreboard to track our scores.

Keeping Score


We’ll use a similar scoreboard to those in Project: The Purple Fruit Monster
Game and Project: Cave Puzzle. We start by adding a second line to the code
outline:

addSunlight(scene);
varscoreboard = addScoreboard();

Then add the following function definition below the addSunlight() function:


Chapter 20. Project: River Rafting • 188


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf