matrix.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #ifndef MATRIX_H
  2. #define MATRIX_H
  3. #include <array>
  4. #include "vector3D.h"
  5. //Data struct holding all of the data you need to make a transform matrix
  6. struct TransformParameters{
  7. TransformParameters() :scaling(Vector3f(1,1,1)) {};
  8. Vector3f translation;
  9. Vector3f rotation;
  10. Vector3f scaling;
  11. };
  12. //Matrices are stored in memory in row major order, but operations are done as if it was
  13. //Column major.
  14. class Matrix4{
  15. public:
  16. //Operators
  17. float& operator()(size_t y, size_t x){
  18. return mMatrix[y*4 + x];
  19. }
  20. Matrix4 operator* (Matrix4 &rhs);
  21. Vector3f matMultVec(const Vector3f &vec);
  22. Vector3f matMultDir(const Vector3f &vec);
  23. Matrix4 transpose();
  24. Matrix4 inverse();
  25. //Named constructor idiom to build the basic matrices we need for
  26. //transformation.
  27. Matrix4 static makeTestMat();
  28. Matrix4 static unitMatrix();
  29. //Uses ZYX convention for rotation
  30. Matrix4 static fullRotMat(float alpha, float beta, float gamma);
  31. Matrix4 static scaleMat(float scaleX, float scaleY, float scaleZ);
  32. Matrix4 static translateMat(float dx, float dy, float dz);
  33. Matrix4(){};
  34. //Builds a matrix that scales, rotates and translates all at once
  35. Matrix4 static transformMatrix(TransformParameters transform);
  36. //Inverse Camera transformation matrix (the world from the camera's eyes)
  37. Matrix4 static lookAt(Vector3f& position, Vector3f& target, Vector3f& temp);
  38. //3D projection matrix. When applied results in the camera frustrum area being
  39. //defined as X[-1,1] Y[-1,1] Z[1,0]
  40. Matrix4 static projectionMatrix(float fov, float AR, float near, float far);
  41. //Debug stuff
  42. void print();
  43. private:
  44. std::array<float, 16> mMatrix{};
  45. };
  46. #endif