WebGL Implementation

Now, let's take a look at how we can implement vertex and normal transformations in WebGL. The following diagram shows the theory we have learned so far, along with the relationships between the steps in the theory and the implementation in WebGL:

In WebGL, the five transformations that we apply to object coordinates to obtain viewport coordinates are grouped into three matrices and one WebGL method:

  • The Model-View matrix that groups the model and view transform in one single matrix. When we multiply our vertices by this matrix, we end up in view coordinates.
  • The Normal matrix is obtained by inverting and transposing the Model-View matrix. This matrix is applied to normal vectors to ensure that they continue to be perpendicular to the surface. This is very important in cases such as lighting.
  • The Projection matrix groups the projection transformation and the perspective division, and as a result, we end up in normalized device coordinates.

Finally, we use the gl.viewport operation to map NDCs to viewport coordinates:

gl.viewport(minX, minY, width, height);

The viewport coordinates originate in the lower-left corner of the HTML5 canvas.

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

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