Camera.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "Camera.h"
  23. #include "Device.h"
  24. #include "Types.h"
  25. #include "Renderer.h"
  26. namespace crown
  27. {
  28. Camera::Camera(const Vec3& position, bool visible, float fov, float aspect, bool active) :
  29. mPosition(position),
  30. mLookAt(0, 0, -1),
  31. mUp(0, 1, 0),
  32. mFOV(fov),
  33. mAspect(aspect),
  34. mNear(0.1f),
  35. mFar(1000.0f),
  36. mActive(active),
  37. mAutoAspect(true)
  38. {
  39. UpdateProjectionMatrix();
  40. UpdateViewMatrix();
  41. UpdateFrustum();
  42. }
  43. Camera::~Camera()
  44. {
  45. }
  46. void Camera::SetPosition(const Vec3& position)
  47. {
  48. mPosition = position;
  49. UpdateViewMatrix();
  50. }
  51. const Vec3& Camera::GetLookAt() const
  52. {
  53. return mLookAt;
  54. }
  55. void Camera::SetLookAt(const Vec3& lookat)
  56. {
  57. mLookAt = lookat;
  58. UpdateViewMatrix();
  59. }
  60. const Vec3& Camera::GetUpVector() const
  61. {
  62. return mUp;
  63. }
  64. bool Camera::IsActive() const
  65. {
  66. return mActive;
  67. }
  68. void Camera::SetActive(bool active)
  69. {
  70. mActive = active;
  71. }
  72. float Camera::GetFOV() const
  73. {
  74. return mFOV;
  75. }
  76. void Camera::SetFOV(float fov)
  77. {
  78. mFOV = fov;
  79. UpdateProjectionMatrix();
  80. }
  81. bool Camera::GetAutoAspect() const
  82. {
  83. return mAutoAspect;
  84. }
  85. void Camera::SetAutoAspect(bool autoAspect)
  86. {
  87. mAutoAspect = autoAspect;
  88. }
  89. float Camera::GetAspect() const
  90. {
  91. return mAspect;
  92. }
  93. void Camera::SetAspect(float aspect)
  94. {
  95. mAspect = aspect;
  96. UpdateProjectionMatrix();
  97. }
  98. float Camera::GetNearClipDistance() const
  99. {
  100. return mNear;
  101. }
  102. void Camera::SetNearClipDistance(float near)
  103. {
  104. mNear = near;
  105. UpdateProjectionMatrix();
  106. }
  107. float Camera::GetFarClipDistance() const
  108. {
  109. return mFar;
  110. }
  111. void Camera::SetFarClipDistance(float far)
  112. {
  113. mFar = far;
  114. UpdateProjectionMatrix();
  115. }
  116. const Mat4& Camera::GetProjectionMatrix() const
  117. {
  118. return mProjection;
  119. }
  120. const Mat4& Camera::GetViewMatrix() const
  121. {
  122. return mView;
  123. }
  124. const Frustum& Camera::GetFrustum() const
  125. {
  126. return mFrustum;
  127. }
  128. void Camera::Render()
  129. {
  130. GetDevice()->renderer()->set_matrix(MT_PROJECTION, mProjection);
  131. GetDevice()->renderer()->set_matrix(MT_VIEW, mView);
  132. }
  133. void Camera::UpdateProjectionMatrix()
  134. {
  135. mProjection.build_projection_perspective_rh(mFOV, mAspect, mNear, mFar);
  136. }
  137. void Camera::UpdateViewMatrix()
  138. {
  139. mView.build_look_at_rh(mPosition, mPosition + mLookAt, mUp);
  140. }
  141. void Camera::UpdateFrustum()
  142. {
  143. mFrustum.from_matrix(mProjection * mView);
  144. }
  145. } // namespace crown