OrthographicCamera.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "anki/scene/OrthographicCamera.h"
  2. namespace anki {
  3. //==============================================================================
  4. // setAll =
  5. //==============================================================================
  6. void OrthographicCamera::setAll(float left_, float right_, float top_,
  7. float bottom_, float zNear_, float zFar_)
  8. {
  9. left = left_;
  10. right = right_;
  11. top = top_;
  12. bottom = bottom_;
  13. zNear = zNear_;
  14. zFar = zFar_;
  15. calcProjectionMatrix();
  16. calcLSpaceFrustumPlanes();
  17. }
  18. //==============================================================================
  19. // calcLSpaceFrustumPlanes =
  20. //==============================================================================
  21. void OrthographicCamera::calcLSpaceFrustumPlanes()
  22. {
  23. lspaceFrustumPlanes[FP_LEFT] = Plane(Vec3(1.0, 0.0, 0.0), left);
  24. lspaceFrustumPlanes[FP_RIGHT] = Plane(Vec3(-1.0, 0.0, 0.0), -right);
  25. lspaceFrustumPlanes[FP_NEAR] = Plane(Vec3(0.0, 0.0, -1.0), zNear);
  26. lspaceFrustumPlanes[FP_FAR] = Plane(Vec3(0.0, 0.0, 1.0), -zFar);
  27. lspaceFrustumPlanes[FP_TOP] = Plane(Vec3(0.0, -1.0, 0.0), -top);
  28. lspaceFrustumPlanes[FP_BOTTOM] = Plane(Vec3(0.0, 1.0, 0.0), bottom);
  29. }
  30. //==============================================================================
  31. // ortho =
  32. //==============================================================================
  33. Mat4 OrthographicCamera::ortho(float left, float right, float bottom,
  34. float top, float near, float far)
  35. {
  36. float difx = right - left;
  37. float dify = top - bottom;
  38. float difz = far - near;
  39. float tx = -(right + left) / difx;
  40. float ty = -(top + bottom) / dify;
  41. float tz = -(far + near) / difz;
  42. Mat4 m;
  43. m(0, 0) = 2.0 / difx;
  44. m(0, 1) = 0.0;
  45. m(0, 2) = 0.0;
  46. m(0, 3) = tx;
  47. m(1, 0) = 0.0;
  48. m(1, 1) = 2.0 / dify;
  49. m(1, 2) = 0.0;
  50. m(1, 3) = ty;
  51. m(2, 0) = 0.0;
  52. m(2, 1) = 0.0;
  53. m(2, 2) = -2.0 / difz;
  54. m(2, 3) = tz;
  55. m(3, 0) = 0.0;
  56. m(3, 1) = 0.0;
  57. m(3, 2) = 0.0;
  58. m(3, 3) = 1.0;
  59. return m;
  60. }
  61. //==============================================================================
  62. // calcProjectionMatrix =
  63. //==============================================================================
  64. void OrthographicCamera::calcProjectionMatrix()
  65. {
  66. projectionMat = ortho(left, right, bottom, top, zNear, zFar);
  67. invProjectionMat = projectionMat.getInverse();
  68. }
  69. } // end namespace