Page 108 - Computer Graphics Handout
P. 108
3.5 MATRIX AND VECTOR CLASSES
In Chapter 2, we saw how using some new data types could clarify our application code and were necessary for GLSL. Let’s expand
and formalize these notions by introducing the C++ classes that we will use in our applications. The code is in two files, mat.h and
vec.h, that can both be included in your application through the include line
#include "mat.h"
The basic types are mat2, mat3, mat4, vec2, vec3, and vec4. The matrix classes are for 2 × 2, 3 × 3, and 4 × 4 matrices whereas the
vector types are for 2-, 3-, and 4-element arrays. The arithmetic operators are overloaded, so we can write code such as
#include "mat.h"
vec4 x, y = vec4(1.0, 2.0, 3.0, 1.0); // use of constructor
x = 2.0*y;
x[2] = 5.0;
mat4 a, b = mat4(vec4(y), vec4(x), vec4(y), vec4(x)); //matrix constructor
float s = 2.5;
a[2][1] = 3.5;
b = s*a;
vec4 z = b*x;
y = x*b;
Thus, we can reference individual elements of either type and carry out the standard matrix operations, as described in Appendix
B. Note that the products b*x and x*b will, in general, yield different results. The first is the product of a 1× 4 row matrix times a 4
× 4 square matrix, whereas the second is the product of a 4 × 4 square matrix times a 4 × 1 column matrix. Both yield four elements
and so can be stored as vec4s.
In light of our previous definition of points and vectors, the use of the names vec2, vec3, and vec4 may be a bit disconcerting. GLSL
uses these types to store any quantity that has three or four elements, including vectors (directions) and points in homogeneous
coordinates, colors (either RGB or RGBA), and, as we shall see later, texture coordinates. The advantage is that we can write code
for all these types that uses the same operations. By using these same GLSL types in the classes in vec.h and mat.h, the code that
manipulates points, vectors, and transformations in our applications will look similar to GLSL code. We will see that we will often
have a choice as to where we carry out our algorithms, in the application or in one of the shaders. By having the same types available,
we will be able to transfer an algorithm easily from an application to one of the shaders. One trick that can make the application
code a little clearer is to use a typedef to assign names to types that will make the code easier to understand. Some helpful examples
we will use are
typedef vec3 color3;
typedef vec4 color4;
typedef vec3 point3;
typedef vec4 point4;
Of course we have not really created any new classes, but we prefer code such as
color3 red = color3(1.0, 0.0, 0.0);
to
vec3 red = vec3(1.0, 0.0, 0.0);
3.6 MODELING A COLORED CUBE
We now have most of the basic conceptual and practical knowledge we need to build three-dimensional graphical applications. We
will use them to produce a program that draws a rotating cube. One frame of an animation might be as shown in Figure 3.28.
However, before we can rotate the cube, we will consider how we can model it efficiently. Although three-dimensional objects can
108

