Page 130 - Computer Graphics Handout
P. 130

by working with the desired matrix. Using the matrix and vector classes, we can form affine matrices for rotation, translation, and
          scaling by creating the following five functions:
          RotateX(float xangle);
          RotateY(float yangle);
          RotateZ(float zangle);
          Translate(float dx, float dy, float dz);
          Scale(float sx, float sy, float sz);













          For example, the code for Rotatez is
          mat4 Rotatez(const float theta)
          {
          float s = M_PI/180.0*theta; //convert degrees to radians
          mat4 c; // an identity matrix
          c[2][2] = c[3][3] = 1.0;
          c[0][0] = c[1][1] = cos(s);
          c[1][0] = sin(s);
          c[0][1] = -c[1][0];
          return c;
          }
          For rotation, the angles are specified in degrees and the rotations are for a fixed point at the origin. In the translation function, the
          variables are the components of the displacement vector; for scaling, the variables determine the scale factors along the coordinate
          axes and the fixed point is the origin.

          3.11.3 Rotation About a Fixed Point
          In Section 3.10, we showed that we can perform a rotation about a fixed point, other than the origin, by first moving the fixed point
          to the origin, then rotating about the origin, and finally moving the fixed point back to its original location. Using the example from
          Section 3.11, the following sequence sets the matrix mode, then forms the required matrix for a 45-degree rotation about the line
          through the origin and the point (1, 2, 3) with a fixed point of (4, 5, 6):
          mat4 R, ctm;
          float thetax, thetay;
          const float Radians_To_Degrees = 180.0/M_PI;
          thetax = Radians_To_Degrees*acos(3.0/sqrt(14.0));
          thetay = Radians_To_Degrees*acos(sqrt(13.0/14.0));
          R = RotateX(-thetax)*RotateY(-thetay)*RotateZ(-45.0)
          *RotateY(-thetax)*RotateX(thetax);
          ctm = Translate(4.0, 5.0, 6.0)*R* Translate(-4.0, -5.0, -6.0);
          Because we want to do arbitrary rotations so often, it is a good exercise (Exercise 3.31) to write functions Rotate(float theta, vec3
          d) and Rotate(float theta, float dx, float dy, float dz) that will form an arbitrary rotation matrix for a rotation of theta degrees about
          a line in the direction of the vector d = (dx, dy, dz).
                                                   23






          23
            Although it is a good exercise to write the various matrix functions yourself, we also provide them in a separate include file angel.h that can be
          downloaded from the book’sWeb site.
                                                             130
   125   126   127   128   129   130   131   132   133   134   135