Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Lucas

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Multiply a point by a transformation matrix

// Multiply a point by a transformation matrix.

   1  
   2  /*!
   3   * Multiply a point by a transformation matrix.
   4   *
   5   * Applies the given transformation matrix to the given point.  With some
   6   * transformation matrices, a vector may also be transformed.
   7   *
   8   * \param c Result. (just a float[3])
   9   * \param m Transformation matrix. (just a float[4][4])
  10   * \param a Input point. (just a float[3])
  11   */
  12  void
  13  lib3ds_vector_transform(Lib3dsVector c, Lib3dsMatrix m, Lib3dsVector a) {
  14      c[0] = m[0][0] * a[0] + m[1][0] * a[1] + m[2][0] * a[2] + m[3][0];
  15      c[1] = m[0][1] * a[0] + m[1][1] * a[1] + m[2][1] * a[2] + m[3][1];
  16      c[2] = m[0][2] * a[0] + m[1][2] * a[1] + m[2][2] * a[2] + m[3][2];
  17  }

Multiply two matrices

// Multiply two matrices

   1  
   2  /*!
   3  * Multiplies a matrix by a second one (m = m * n).
   4  */
   5  void lib3ds_matrix_mult(Lib3dsMatrix m, Lib3dsMatrix n) {
   6      Lib3dsMatrix tmp;
   7      int i, j, k;
   8      float ab;
   9  
  10      memcpy(tmp, m, sizeof(Lib3dsMatrix));
  11      for (j = 0; j < 4; j++) {
  12          for (i = 0; i < 4; i++) {
  13              ab = 0.0f;
  14              for (k = 0; k < 4; k++)
  15                  ab += tmp[k][i] * n[j][k];
  16              m[j][i] = ab;
  17          }
  18      }
  19  }

Apply a rotation about an arbitrary axis to a matrix.

// Apply a rotation about an arbitrary axis to a matrix.

   1  
   2  /*!
   3  * Apply a rotation about an arbitrary axis to a matrix.
   4  * m is just a float[4][4], q is just a float[4]
   5  */
   6  void lib3ds_matrix_rotate(Lib3dsMatrix m, Lib3dsQuat q) {
   7      float s, xs, ys, zs, wx, wy, wz, xx, xy, xz, yy, yz, zz, l;
   8      Lib3dsMatrix R;
   9  
  10      l = q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3];
  11      s = 2.0f / l;
  12  
  13      xs = q[0] * s;
  14      ys = q[1] * s;
  15      zs = q[2] * s;
  16      wx = q[3] * xs;
  17      wy = q[3] * ys;
  18      wz = q[3] * zs;
  19      xx = q[0] * xs;
  20      xy = q[0] * ys;
  21      xz = q[0] * zs;
  22      yy = q[1] * ys;
  23      yz = q[1] * zs;
  24      zz = q[2] * zs;
  25  
  26      R[0][0] = 1.0f - (yy + zz);
  27      R[1][0] = xy - wz;
  28      R[2][0] = xz + wy;
  29      R[0][1] = xy + wz;
  30      R[1][1] = 1.0f - (xx + zz);
  31      R[2][1] = yz - wx;
  32      R[0][2] = xz - wy;
  33      R[1][2] = yz + wx;
  34      R[2][2] = 1.0f - (xx + yy);
  35      R[3][0] = R[3][1] = R[3][2] = R[0][3] = R[1][3] = R[2][3] = 0.0f;
  36      R[3][3] = 1.0f;
  37  
  38      lib3ds_matrix_mult(m, R);
  39  }

Build a quaternion from a rotation about an arbitrary axis

// Build a quaternion from a rotation about an arbitrary axis

   1  
   2  /*!
   3  * Compute a quaternion from axis and angle.
   4  *
   5  * \param c Computed quaternion (this is just a float[4])
   6  * \param axis Rotation axis (this is just a float[3])
   7  * \param angle Angle of rotation, radians.
   8  */
   9  void lib3ds_quat_axis_angle(Lib3dsQuat c, Lib3dsVector axis, float angle)
  10  {
  11      double omega, s;
  12      double l = sqrt(axis[0] * axis[0] + axis[1] * axis[1] + axis[2] * axis[2]);
  13  
  14      omega = -0.5 * angle;
  15      s = sin(omega) / l;
  16  
  17      c[0] = (float)s * axis[0];
  18      c[1] = (float)s * axis[1];
  19      c[2] = (float)s * axis[2];
  20      c[3] = (float)cos(omega);
  21  }
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS