Have a Go: Updating Light Positions

As we've seen, by moving the camera, we're applying the inverse transformation to the world. If we do not update the light position, the light source will be located at the same static point, regardless of the final transformation applied to the world.

This is very convenient when we're moving around or exploring an object in the scene. We can always see whether the light is located on the same axis of the camera. This is the case for the exercises in this chapter. Nevertheless, we can also simulate the case when the camera movement is independent from the light source. To do so, we need to calculate the new light position whenever we move the camera.

First, we calculate the light direction. We can do this by simply calculating the difference vector between our target and our origin. Say the light source is located at [0, 2, 50]. If we want to direct our light source toward the origin, we calculate the [0, 0, 0] - [0, 2, 50] vector (target - origin). This vector has the correct orientation of the light when we target the origin. We repeat the same procedure if we have a different target that needs to be lit. In that case, we just use the coordinates of the target and from them, we subtract the location of the light.

As we are directing our light source toward the origin, we can find the direction of the light just by inverting the light position. As you may have noticed, we do this in ESSL in the vertex shader:

vec3 L = normalize(-uLightPosition);

As light is a vector, if we want to update the direction of the light, we need to use the Normal matrix, discussed earlier in this chapter, to update this vector under any world transformation. This step is optional in the vertex shader:

if (uFixedLight) {
L = vec3(uNormalMatrix * vec4(L, 0.0));
}

In the previous fragment of code, light is augmented to four components, so we can use the direct multiplication provided by ESSL. (Remember that uNormalMatrix is a 4x4 matrix and, as such, the vectors it transforms need to be four-dimensional.) Please bear in mind that, as explained at the beginning of this chapter, the Homogeneous coordinates of vectors are always set to 0, while the Homogeneous coordinates of vertices are set to 1.

  1. After the multiplication, we reduce the result to three components before assigning the result back to light.
  2. You can test the effects of updating the light position by using the Static Light Position button, provided in the ch04_05_car.html file.
  3. We connect a global variable that keeps track of the state of this button with the uUpdateLight uniform.
  4. Edit ch04_05_car.html and set the light position to a different location. To do this, edit the configure function. Go to the following position:
gl.uniform3fv(program.uLightPosition, [0, 0, 2120]);
  1. Try different light positions:
    • [2120, 0, 0]
    • [0, 2120, 0]
    • [100, 100, 100]
  2. For each option, save the file and try it with and without updating the light position (use the Static Light Position button):

  1. For a better visualization, use an Orbiting camera.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.183.1