Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.28 KB | None | 0 0
  1.     class Matrix4
  2.     {
  3. ///<Matrix4
  4.     public:
  5.         /**
  6.          * Holds the transform matrix data in array form.
  7.          */
  8.         real data[12];
  9. ///<Matrix4Intro
  10.  
  11. ///>Omit;Matrix4
  12.         // ... Other Matrix4 code as before ...
  13.        
  14. ///<Omit;Matrix4
  15.  
  16.         /**
  17.          * Creates an identity matrix.
  18.          */
  19.         Matrix4()
  20.         {
  21.             data[1] = data[2] = data[3] = data[4] = data[6] =
  22.                 data[7] = data[8] = data[9] = data[11] = 0;
  23.             data[0] = data[5] = data[10] = 1;
  24.         }
  25.  
  26.         /**
  27.          * Sets the matrix to be a diagonal matrix with the given coefficients.
  28.          */
  29.         void setDiagonal(real a, real b, real c)
  30.         {
  31.             data[0] = a;
  32.             data[5] = b;
  33.             data[10] = c;
  34.         }
  35.  
  36. ///>Matrix4MatrixMultiply
  37.         /**
  38.          * Returns a matrix which is this matrix multiplied by the given
  39.          * other matrix.
  40.          */
  41.         Matrix4 operator*(const Matrix4 &o) const
  42.         {
  43.             Matrix4 result;
  44.             result.data[0] = (o.data[0]*data[0]) + (o.data[4]*data[1]) + (o.data[8]*data[2]);
  45.             result.data[4] = (o.data[0]*data[4]) + (o.data[4]*data[5]) + (o.data[8]*data[6]);
  46.             result.data[8] = (o.data[0]*data[8]) + (o.data[4]*data[9]) + (o.data[8]*data[10]);
  47.  
  48.             result.data[1] = (o.data[1]*data[0]) + (o.data[5]*data[1]) + (o.data[9]*data[2]);
  49.             result.data[5] = (o.data[1]*data[4]) + (o.data[5]*data[5]) + (o.data[9]*data[6]);
  50.             result.data[9] = (o.data[1]*data[8]) + (o.data[5]*data[9]) + (o.data[9]*data[10]);
  51.  
  52.             result.data[2] = (o.data[2]*data[0]) + (o.data[6]*data[1]) + (o.data[10]*data[2]);
  53.             result.data[6] = (o.data[2]*data[4]) + (o.data[6]*data[5]) + (o.data[10]*data[6]);
  54.             result.data[10] = (o.data[2]*data[8]) + (o.data[6]*data[9]) + (o.data[10]*data[10]);
  55.  
  56.             result.data[3] = (o.data[3]*data[0]) + (o.data[7]*data[1]) + (o.data[11]*data[2]) + data[3];
  57.             result.data[7] = (o.data[3]*data[4]) + (o.data[7]*data[5]) + (o.data[11]*data[6]) + data[7];
  58.             result.data[11] = (o.data[3]*data[8]) + (o.data[7]*data[9]) + (o.data[11]*data[10]) + data[11];
  59.  
  60.             return result;
  61.         }
  62. ///<Matrix4MatrixMultiply
  63.  
  64. ///>Matrix4VectorMultiply
  65.         /**
  66.          * Transform the given vector by this matrix.
  67.          *
  68.          * @param vector The vector to transform.
  69.          */
  70.         Vector3 operator*(const Vector3 &vector) const
  71.         {
  72.             return Vector3(
  73.                 vector.x * data[0] +
  74.                 vector.y * data[1] +
  75.                 vector.z * data[2] + data[3],
  76.  
  77.                 vector.x * data[4] +
  78.                 vector.y * data[5] +
  79.                 vector.z * data[6] + data[7],
  80.  
  81.                 vector.x * data[8] +
  82.                 vector.y * data[9] +
  83.                 vector.z * data[10] + data[11]
  84.             );
  85.         }
  86. ///<Matrix4VectorMultiply
  87.  
  88.         /**
  89.          * Transform the given vector by this matrix.
  90.          *
  91.          * @param vector The vector to transform.
  92.          */
  93.         Vector3 transform(const Vector3 &vector) const
  94.         {
  95.             return (*this) * vector;
  96.         }
  97.  
  98. ///>Matrix4Inverse
  99.         /**
  100.          * Returns the determinant of the matrix.
  101.          */
  102.         real getDeterminant() const;
  103.  
  104.         /**
  105.          * Sets the matrix to be the inverse of the given matrix.
  106.          *
  107.          * @param m The matrix to invert and use to set this.
  108.          */
  109.         void setInverse(const Matrix4 &m);
  110.  
  111.         /** Returns a new matrix containing the inverse of this matrix. */
  112.         Matrix4 inverse() const
  113.         {
  114.             Matrix4 result;
  115.             result.setInverse(*this);
  116.             return result;
  117.         }
  118.  
  119.         /**
  120.          * Inverts the matrix.
  121.          */
  122.         void invert()
  123.         {
  124.             setInverse(*this);
  125.         }
  126. ///<Matrix4Inverse
  127.  
  128. ///>TransformDirection        
  129.         /**
  130.          * Transform the given direction vector by this matrix.
  131. ///<TransformDirection
  132.          *
  133.          * @note When a direction is converted between frames of
  134.          * reference, there is no translation required.
  135.          *
  136.          * @param vector The vector to transform.
  137. ///>TransformDirection        
  138.          */
  139.         Vector3 transformDirection(const Vector3 &vector) const
  140.         {
  141.             return Vector3(
  142.                 vector.x * data[0] +
  143.                 vector.y * data[1] +
  144.                 vector.z * data[2],
  145.  
  146.                 vector.x * data[4] +
  147.                 vector.y * data[5] +
  148.                 vector.z * data[6],
  149.  
  150.                 vector.x * data[8] +
  151.                 vector.y * data[9] +
  152.                 vector.z * data[10]
  153.             );
  154.         }
  155.  
  156.         /**
  157.          * Transform the given direction vector by the
  158.          * transformational inverse of this matrix.
  159. ///<TransformDirection
  160.          *
  161.          * @note This function relies on the fact that the inverse of
  162.          * a pure rotation matrix is its transpose. It separates the
  163.          * translational and rotation components, transposes the
  164.          * rotation, and multiplies out. If the matrix is not a
  165.          * scale and shear free transform matrix, then this function
  166.          * will not give correct results.
  167.          *
  168.          * @note When a direction is converted between frames of
  169.          * reference, there is no translation required.
  170.          *
  171.          * @param vector The vector to transform.
  172. ///>TransformDirection
  173.          */
  174.         Vector3 transformInverseDirection(const Vector3 &vector) const
  175.         {
  176.             return Vector3(
  177.                 vector.x * data[0] +
  178.                 vector.y * data[4] +
  179.                 vector.z * data[8],
  180.  
  181.                 vector.x * data[1] +
  182.                 vector.y * data[5] +
  183.                 vector.z * data[9],
  184.  
  185.                 vector.x * data[2] +
  186.                 vector.y * data[6] +
  187.                 vector.z * data[10]
  188.             );
  189.         }        
  190. ///<TransformDirection
  191.  
  192. ///>TransformInverse
  193.         /**
  194.          * Transform the given vector by the transformational inverse
  195.          * of this matrix.
  196. ///<TransformInverse        
  197.          *
  198.          * @note This function relies on the fact that the inverse of
  199.          * a pure rotation matrix is its transpose. It separates the
  200.          * translational and rotation components, transposes the
  201.          * rotation, and multiplies out. If the matrix is not a
  202.          * scale and shear free transform matrix, then this function
  203.          * will not give correct results.
  204.          *
  205.          * @param vector The vector to transform.
  206. ///>TransformInverse        
  207.          */
  208.         Vector3 transformInverse(const Vector3 &vector) const
  209.         {
  210.             Vector3 tmp = vector;
  211.             tmp.x -= data[3];
  212.             tmp.y -= data[7];
  213.             tmp.z -= data[11];
  214.             return Vector3(
  215.                 tmp.x * data[0] +
  216.                 tmp.y * data[4] +
  217.                 tmp.z * data[8],
  218.  
  219.                 tmp.x * data[1] +
  220.                 tmp.y * data[5] +
  221.                 tmp.z * data[9],
  222.  
  223.                 tmp.x * data[2] +
  224.                 tmp.y * data[6] +
  225.                 tmp.z * data[10]
  226.             );
  227.         }
  228. ///<TransformInverse        
  229.  
  230.         /**
  231.          * Gets a vector representing one axis (i.e. one column) in the matrix.
  232.          *
  233.          * @param i The row to return. Row 3 corresponds to the position
  234.          * of the transform matrix.
  235.          *
  236.          * @return The vector.
  237.          */
  238.         Vector3 getAxisVector(int i) const
  239.         {
  240.             return Vector3(data[i], data[i+4], data[i+8]);
  241.         }
  242.  
  243. ///>OrientationAndPosToMatrix4
  244.         /**
  245.          * Sets this matrix to be the rotation matrix corresponding to
  246.          * the given quaternion.
  247.          */
  248.         void setOrientationAndPos(const Quaternion &q, const Vector3 &pos)
  249.         {
  250.             data[0] = 1 - (2*q.j*q.j + 2*q.k*q.k);
  251.             data[1] = 2*q.i*q.j + 2*q.k*q.r;
  252.             data[2] = 2*q.i*q.k - 2*q.j*q.r;
  253.             data[3] = pos.x;
  254.  
  255.             data[4] = 2*q.i*q.j - 2*q.k*q.r;
  256.             data[5] = 1 - (2*q.i*q.i  + 2*q.k*q.k);
  257.             data[6] = 2*q.j*q.k + 2*q.i*q.r;
  258.             data[7] = pos.y;
  259.  
  260.             data[8] = 2*q.i*q.k + 2*q.j*q.r;
  261.             data[9] = 2*q.j*q.k - 2*q.i*q.r;
  262.             data[10] = 1 - (2*q.i*q.i  + 2*q.j*q.j);
  263.             data[11] = pos.z;
  264.         }
  265. ///<OrientationAndPosToMatrix4
  266.  
  267.         /**
  268.          * Fills the given array with this transform matrix, so it is
  269.          * usable as an open-gl transform matrix. OpenGL uses a column
  270.          * major format, so that the values are transposed as they are
  271.          * written.
  272.          */
  273.         void fillGLArray(float array[16]) const
  274.         {
  275.             array[0] = (float)data[0];
  276.             array[1] = (float)data[4];
  277.             array[2] = (float)data[8];
  278.             array[3] = (float)0;
  279.  
  280.             array[4] = (float)data[1];
  281.             array[5] = (float)data[5];
  282.             array[6] = (float)data[9];
  283.             array[7] = (float)0;
  284.  
  285.             array[8] = (float)data[2];
  286.             array[9] = (float)data[6];
  287.             array[10] = (float)data[10];
  288.             array[11] = (float)0;
  289.  
  290.             array[12] = (float)data[3];
  291.             array[13] = (float)data[7];
  292.             array[14] = (float)data[11];
  293.             array[15] = (float)1;
  294.         }
  295. ///>Matrix4;Matrix4Intro
  296.     };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement