matrix.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #ifndef MATRIX_H
  2. #define MATRIX_H
  3. // ===============================
  4. // AUTHOR : Angel Ortiz (angelo12 AT vt DOT edu)
  5. // CREATE DATE : 2018-07-04
  6. // PURPOSE : Perform matrix operations and linear algebra related operations.
  7. // Also to have constructors to build the most common matrices used in
  8. // rendering.
  9. // ===============================
  10. // SPECIAL NOTES: All matrices are described as 4x4 matrices even if thery are supposed
  11. // to be only 3x3. This is because there is a special matmulDir function that performs
  12. // the equivalent multiplication that a 3x3 matrix would. Matrices are stored in memory
  13. // in row major order, but operations are done as if it was Column major. Exactly how
  14. // Opengl does.
  15. // ===============================
  16. //Headers
  17. #include <array>
  18. #include "vector3D.h"
  19. //Data struct holding all of the data you need to make a transform matrix
  20. struct TransformParameters{
  21. TransformParameters() :scaling(Vector3f(1,1,1)) {};
  22. Vector3f translation;
  23. Vector3f rotation;
  24. Vector3f scaling;
  25. };
  26. class Matrix4{
  27. public:
  28. Matrix4(){};
  29. //Operators
  30. float& operator()(size_t y, size_t x){
  31. return mMatrix[y*4 + x];
  32. }
  33. Matrix4 operator* (Matrix4 &rhs);
  34. Vector3f matMultVec(const Vector3f &vec);
  35. Vector3f matMultDir(const Vector3f &vec);
  36. Matrix4 transpose();
  37. Matrix4 inverse();
  38. //Named constructor idiom to build the basic matrices we need for
  39. //transformation.
  40. Matrix4 static makeTestMat();
  41. Matrix4 static unitMatrix();
  42. //Uses ZYX convention for rotation
  43. Matrix4 static fullRotMat(float alpha, float beta, float gamma);
  44. Matrix4 static scaleMat(float scaleX, float scaleY, float scaleZ);
  45. Matrix4 static translateMat(float dx, float dy, float dz);
  46. //Builds a matrix that scales, rotates and translates all at once
  47. Matrix4 static transformMatrix(TransformParameters transform);
  48. //Inverse Camera transformation matrix (the world from the camera's eyes)
  49. Matrix4 static lookAt(Vector3f& position, Vector3f& target, Vector3f& temp);
  50. //3D projection matrix. When applied results in the camera frustrum area being
  51. //defined as X[-1,1] Y[-1,1] Z[1,0]
  52. Matrix4 static projectionMatrix(float fov, float AR, float near, float far);
  53. //Tangent bitangent normal matrix used to get from world space to tangent space
  54. Matrix4 static TBNMatrix(const Vector3f &tangent, const Vector3f &biTangent, const Vector3f &normal);
  55. //Debug stuff
  56. void print();
  57. private:
  58. std::array<float, 16> mMatrix{};
  59. };
  60. #endif