matrix.cxx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. //
  2. // matrix.cxx
  3. // graphics
  4. //
  5. // Created by Sam Jaffe on 5/24/19.
  6. // Copyright © 2019 Sam Jaffe. All rights reserved.
  7. //
  8. #include "matrix.hpp"
  9. #include "game/math/angle.hpp"
  10. #include "matrix/matrix.hpp"
  11. namespace graphics {
  12. math::matr4 orthogonal_view(float left, float right, float bottom, float top,
  13. float near, float far) {
  14. math::matr4 matrix;
  15. matrix(0, 0) = 2.f / (right - left);
  16. matrix(1, 1) = 2.f / (top - bottom);
  17. matrix(2, 2) = -2.f / (far - near);
  18. matrix(0, 3) = -(right + left) / (right - left);
  19. matrix(1, 3) = -(top + bottom) / (top - bottom);
  20. matrix(2, 3) = -(far + near) / (far - near);
  21. matrix(3, 3) = 1.f;
  22. return matrix;
  23. }
  24. math::matr4 frustum_view(float left, float right, float bottom, float top,
  25. float near, float far) {
  26. math::matr4 matrix;
  27. matrix(0, 0) = 2.f * near / (right - left);
  28. matrix(1, 1) = 2.f * near / (top - bottom);
  29. matrix(2, 0) = (right + left) / (right - left);
  30. matrix(2, 1) = (top + bottom) / (top - bottom);
  31. matrix(2, 2) = -(far + near) / (far - near);
  32. matrix(2, 3) = -1.f;
  33. matrix(3, 2) = -(2.f * far * near) / (far - near);
  34. matrix(3, 3) = 0.f;
  35. return matrix;
  36. }
  37. math::matr4 perspective(math::degree fovY, float aspect, float front,
  38. float back) {
  39. float tangent = tan(fovY);
  40. float height = front * tangent;
  41. float width = height * aspect;
  42. return frustum_view(-width, width, -height, height, front, back);
  43. }
  44. }