Advertisement
xerpi

matrix

Aug 25th, 2013
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.06 KB | None | 0 0
  1. #ifndef _MATRIX_H_
  2. #define _MATRIX_H_
  3.  
  4. #include <math.h>
  5. #include <string.h>
  6.  
  7. class Matrix4x4
  8. {
  9.     public:
  10.         Matrix4x4()
  11.         {
  12.             identity();
  13.         }
  14.  
  15.         void identity()
  16.         {
  17.             float *p = (float *)matrix;
  18.             p[0]  =  p[5] =  p[10] = p[15] = 1.0;
  19.             p[1]  =  p[2] =  p[3]  =  p[4] = 0.0;
  20.             p[6]  =  p[7] =  p[8]  =  p[9] = 0.0;
  21.             p[11] = p[12] = p[13]  = p[14] = 0.0;
  22.         }
  23.  
  24.         void copy(Matrix4x4 &dst)
  25.         {
  26.             memcpy(dst.getMatrixPointer(), getMatrixPointer(), sizeof(float) * 16);
  27.         }
  28.         void copy(Matrix4x4 *dst)
  29.         {
  30.             memcpy(dst->getMatrixPointer(), getMatrixPointer(), sizeof(float) * 16);
  31.         }
  32.  
  33.  
  34.         float *getMatrixPointer()
  35.         {
  36.             return (float *)matrix;
  37.         }
  38.  
  39.         void multiply(Matrix4x4 &mtx, Matrix4x4 &dst)
  40.         {
  41.             float (*dest)[4] = (float (*)[4])dst.getMatrixPointer();
  42.             float (*m1)[4]   = (float (*)[4])getMatrixPointer();
  43.             float (*m2)[4]   = (float (*)[4])mtx.getMatrixPointer();
  44.             dest[0][0] = m1[0][0] * m2[0][0] + m1[0][1] * m2[1][0] + m1[0][2] * m2[2][0] + m1[0][3] * m2[3][0];
  45.             dest[0][1] = m1[0][0] * m2[0][1] + m1[0][1] * m2[1][1] + m1[0][2] * m2[2][1] + m1[0][3] * m2[3][1];
  46.             dest[0][2] = m1[0][0] * m2[0][2] + m1[0][1] * m2[1][2] + m1[0][2] * m2[2][2] + m1[0][3] * m2[3][2];
  47.             dest[0][3] = m1[0][0] * m2[0][3] + m1[0][1] * m2[1][3] + m1[0][2] * m2[2][3] + m1[0][3] * m2[3][3];
  48.  
  49.             dest[1][0] = m1[1][0] * m2[0][0] + m1[1][1] * m2[1][0] + m1[1][2] * m2[2][0] + m1[1][3] * m2[3][0];
  50.             dest[1][1] = m1[1][0] * m2[0][1] + m1[1][1] * m2[1][1] + m1[1][2] * m2[2][1] + m1[1][3] * m2[3][1];
  51.             dest[1][2] = m1[1][0] * m2[0][2] + m1[1][1] * m2[1][2] + m1[1][2] * m2[2][2] + m1[1][3] * m2[3][2];
  52.             dest[1][3] = m1[1][0] * m2[0][3] + m1[1][1] * m2[1][3] + m1[1][2] * m2[2][3] + m1[1][3] * m2[3][3];
  53.  
  54.             dest[2][0] = m1[2][0] * m2[0][0] + m1[2][1] * m2[1][0] + m1[2][2] * m2[2][0] + m1[2][3] * m2[3][0];
  55.             dest[2][1] = m1[2][0] * m2[0][1] + m1[2][1] * m2[1][1] + m1[2][2] * m2[2][1] + m1[2][3] * m2[3][1];
  56.             dest[2][2] = m1[2][0] * m2[0][2] + m1[2][1] * m2[1][2] + m1[2][2] * m2[2][2] + m1[2][3] * m2[3][2];
  57.             dest[2][3] = m1[2][0] * m2[0][3] + m1[2][1] * m2[1][3] + m1[2][2] * m2[2][3] + m1[2][3] * m2[3][3];
  58.  
  59.             dest[3][0] = m1[3][0] * m2[0][0] + m1[3][1] * m2[1][0] + m1[3][2] * m2[2][0] + m1[3][3] * m2[3][0];
  60.             dest[3][1] = m1[3][0] * m2[0][1] + m1[3][1] * m2[1][1] + m1[3][2] * m2[2][1] + m1[3][3] * m2[3][1];
  61.             dest[3][2] = m1[3][0] * m2[0][2] + m1[3][1] * m2[1][2] + m1[3][2] * m2[2][2] + m1[3][3] * m2[3][2];
  62.             dest[3][3] = m1[3][0] * m2[0][3] + m1[3][1] * m2[1][3] + m1[3][2] * m2[2][3] + m1[3][3] * m2[3][3];
  63.         }
  64.  
  65.         //Set matrix rotation X
  66.             void setRotationX(float angle)
  67.             {
  68.                 float s = sinf(angle);
  69.                 float c = cosf(angle);
  70.  
  71.                 matrix[1][1] = c;
  72.                 matrix[1][2] = s;
  73.  
  74.                 matrix[2][1] = -s;
  75.                 matrix[2][2] = c;
  76.             }
  77.  
  78.         //Set matrix rotation Y
  79.             void setRotationY(float angle)
  80.             {
  81.                 float s = sinf(angle);
  82.                 float c = cosf(angle);
  83.  
  84.                 matrix[0][0] = c;
  85.                 matrix[0][2] = -s;
  86.  
  87.                 matrix[2][0] = s;
  88.                 matrix[2][2] = c;
  89.             }
  90.  
  91.         //Set matrix rotation Z
  92.             void setRotationZ(float angle)
  93.             {
  94.                 float s = sinf(angle);
  95.                 float c = cosf(angle);
  96.  
  97.                 matrix[0][0] = c;
  98.                 matrix[0][1] = -s;
  99.  
  100.                 matrix[1][0] = s;
  101.                 matrix[1][1] = c;
  102.             }
  103.  
  104.         void translate(float x, float y, float z)
  105.         {
  106.             matrix[0][3] = matrix[0][0]*x + matrix[0][1]*y + matrix[0][2]*z;
  107.             matrix[1][3] = matrix[1][0]*x + matrix[1][1]*y + matrix[1][2]*z;
  108.             matrix[2][3] = matrix[2][0]*x + matrix[2][1]*y + matrix[2][2]*z;
  109.         }
  110.  
  111.      //Rotate this matrix around X axis
  112.         void rotateX(float angle)
  113.         {
  114.             Matrix4x4 rotated, tmp;
  115.             rotated.setRotationX(angle);
  116.             multiply(rotated, tmp);  // tmp = this * rotated
  117.             tmp.copy(this);  //this = tmp
  118.         }
  119.  
  120.     //Rotate this matrix around Y axis
  121.         void rotateY(float angle)
  122.         {
  123.             Matrix4x4 rotated, tmp;
  124.             rotated.setRotationY(angle);
  125.             multiply(rotated, tmp);  // tmp = this * rotated
  126.             tmp.copy(this);  //this = tmp
  127.         }
  128.  
  129.     //Rotate this matrix around Z axis
  130.         void rotateZ(float angle)
  131.         {
  132.             Matrix4x4 rotated, tmp;
  133.             rotated.setRotationZ(angle);
  134.             multiply(rotated, tmp); // tmp = this * rotated
  135.             tmp.copy(this); //this = tmp
  136.         }
  137.  
  138.     private:
  139.         float matrix[4][4];
  140. };
  141.  
  142. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement