Chapter 4. Camera

In this chapter, we will learn more about the matrices that we have seen in the source code. These matrices represent transformations that when applied to our scene, allow us to move things around. We have used them so far to set the camera to a distance that is good enough to see all the objects in our scene and also for spinning our Nissan GTS model (Animate button in ch3_Nissan.html). In general, we move the camera and the objects in the scene using matrices.

The bad news is that you will not see a camera object in the WebGL API, only matrices. The good news is that having matrices instead of a camera object gives WebGL a lot of flexibility to represent complex animations (as we will see in Chapter 5, Action). In this chapter, we will learn what these matrix transformations mean and how we can use them to define and operate a virtual camera.

In this chapter, we will:

  • Understand the transformations that the scene undergoes from a 3D world to a 2D screen
  • Learn about affine transformations
  • Map matrices to ESSL uniforms
  • Work with the Model-View matrix and the Perspective matrix
  • Appreciate the value of the Normal matrix
  • Create a camera and use it to move around a 3D scene

WebGL does not have cameras

This statement should be shocking! How is it that there are no cameras in a 3D computer graphics technology? Well, let me rephrase this in a more amicable way. WebGL does not have a camera object that you can manipulate. However, we can assume that what we see rendered in the canvas is what our camera captures. In this chapter, we are going to solve the problem of how to represent a camera in WebGL. The short answer is we need 4x4 matrices.

Every time that we move our camera around, we will need to update the objects according to the new camera position. To do this, we need to systematically process each vertex applying a transformation that produces the new viewing position. Similarly, we need to make sure that the object normals and light directions are still consistent after the camera has moved. In summary, we need to analyze two different types of transformations: vertex (points) and normal (vectors).

..................Content has been hidden....................

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