matrix.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #ifndef MATRIX_H
  2. #define MATRIX_H
  3. #include <array>
  4. #include <vector3.h>
  5. //Data struct holding all of the data you need to make a transform matrix
  6. struct TransformParameters{
  7. TransformParameters() :scaling(Vector3(1,1,1)) {};
  8. Vector3 translation;
  9. Vector3 rotation;
  10. Vector3 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. Vector3 matMultVec(Vector3 &vec);
  22. //Named constructor idiom to build the basic matrices we need for
  23. //transformation.
  24. Matrix4 static makeTestMat();
  25. Matrix4 static unitMatrix();
  26. //Uses ZYX convention for rotation
  27. Matrix4 static fullRotMat(float alpha, float beta, float gamma);
  28. Matrix4 static scaleMat(float scaleX, float scaleY, float scaleZ);
  29. Matrix4 static translateMat(float dx, float dy, float dz);
  30. Matrix4(){};
  31. //Builds a matrix that scales, rotates and translates all at once
  32. Matrix4 static transformMatrix(TransformParameters transform);
  33. //Inverse Camera transformation matrix (the world from the camera's eyes)
  34. Matrix4 static lookAt(Vector3& position, Vector3& target, Vector3& temp);
  35. //3D projection matrix. When applied results in the camera frustrum area being
  36. //defined as X[-1,1] Y[-1,1] Z[1,0]
  37. Matrix4 static projectionMatrix(float fov, float AR, float near, float far);
  38. //Debug stuff
  39. void print();
  40. private:
  41. std::array<float, 16> mMatrix{};
  42. };
  43. #endif