Guest User

Untitled

a guest
Mar 22nd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6.  
  7. class CMatrix4x4f
  8. {
  9. public: // attributes
  10.  
  11. float m_vector[16];
  12.  
  13. CMatrix4x4f()
  14. {
  15. SetIdentity();
  16. }
  17.  
  18. ~CMatrix4x4f()
  19. {
  20. }
  21.  
  22. void SetIdentity()
  23. {
  24. memset(m_vector, 0x00, 16*sizeof(float));
  25. m_vector[0] = m_vector[5] = m_vector[10] = m_vector[15]= 1.0f;
  26. }
  27.  
  28. void Rotate(float angle, float x, float y, float z)
  29. {
  30. float u[3];
  31. float norma = x*x+y*y+z*z;
  32. if (norma != 0.0f)
  33. {
  34. norma = (float)(1.0/sqrt(norma));
  35. u[0] = x*norma;
  36. u[1] = y*norma;
  37. u[2] = z*norma;
  38. }
  39. else
  40. u[0] = u[1] = u[2] = 0.0f;
  41. float seno = (float)sin(angle);
  42. float coseno = (float)cos(angle);
  43.  
  44. float aux;
  45. float sen_x_u[3];
  46. sen_x_u[0] = seno*u[0];
  47. sen_x_u[1] = seno*u[1];
  48. sen_x_u[2] = seno*u[2];
  49.  
  50. aux = u[0]*u[0]; m_vector[0] = aux + coseno * (1.0f - aux);
  51. aux = u[0]*u[1]; m_vector[1] = aux - coseno * aux - sen_x_u[2];
  52. aux = u[0]*u[2]; m_vector[2] = aux - coseno * aux + sen_x_u[1];
  53.  
  54. aux = u[1]*u[0]; m_vector[4] = aux - coseno * aux + sen_x_u[2];
  55. aux = u[1]*u[1]; m_vector[5] = aux + coseno * (1.0f - aux);
  56. aux = u[1]*u[2]; m_vector[6] = aux - coseno * aux - sen_x_u[0];
  57.  
  58. aux = u[2]*u[0]; m_vector[8] = aux - coseno * aux - sen_x_u[1];
  59. aux = u[2]*u[1]; m_vector[9] = aux - coseno * aux + sen_x_u[0];
  60. aux = u[2]*u[2]; m_vector[10]= aux + coseno * (1.0f - aux);
  61.  
  62. m_vector[3] = m_vector[7] = m_vector[11] = m_vector[12]= m_vector[13] = m_vector[14] = 0.0f;
  63. m_vector[15] = 1.0f;
  64. }
  65.  
  66. void PreMult(CMatrix4x4f & leftMatrix)
  67. {
  68. CMatrix4x4f res;
  69. for (int i=0; i<4; i++)
  70. for (int j=0; j<4; j++)
  71. {
  72. res[i][j] = 0;
  73. for (int k=0; k<4; k++)
  74. res[i][j] += m_vector[(k<<2)+j]*leftMatrix[i][k];
  75. }
  76. *this = res;
  77. }
  78.  
  79. float* operator [](int i)
  80. {
  81. return &m_vector[i<<2];
  82. }
  83.  
  84. CMatrix4x4f& Transpose()
  85. {
  86. for (int i=0; i<4; i++)
  87. for (int j=i+1; j<4; j++)
  88. {
  89. float aux = m_vector[(i<<2)+j];
  90. m_vector[(i<<2)+j] = m_vector[(j<<2)+i];
  91. m_vector[(j<<2)+i] = aux;
  92. }
  93. return *this;
  94. }
  95.  
  96. };
Add Comment
Please, Sign In to add comment