Camera.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <glm/glm.hpp>
  2. #include <glm/gtc/matrix_transform.hpp>
  3. #include "Camera.h"
  4. using namespace WallpaperEngine;
  5. using namespace WallpaperEngine::Render;
  6. Camera::Camera (Wallpapers::CScene& scene, const SceneData::Camera& camera) :
  7. m_width (0), m_height (0), m_camera (camera), m_scene (scene) {
  8. // TODO: ensure this is only used for non-orthographic cameras, it throws off points otherwise
  9. this->m_lookat = glm::lookAt (this->getEye (), this->getCenter (), this->getUp ());
  10. }
  11. Camera::~Camera () = default;
  12. const glm::vec3& Camera::getCenter () const { return this->m_camera.configuration.center; }
  13. const glm::vec3& Camera::getEye () const { return this->m_camera.configuration.eye; }
  14. const glm::vec3& Camera::getUp () const { return this->m_camera.configuration.up; }
  15. const glm::mat4& Camera::getProjection () const { return this->m_projection; }
  16. const glm::mat4& Camera::getLookAt () const { return this->m_lookat; }
  17. bool Camera::isOrthogonal () const { return this->m_isOrthogonal; }
  18. Wallpapers::CScene& Camera::getScene () const { return this->m_scene; }
  19. float Camera::getWidth () const { return this->m_width; }
  20. float Camera::getHeight () const { return this->m_height; }
  21. float Camera::getCanvasWidth () const { return this->m_canvasWidth; }
  22. float Camera::getCanvasHeight () const { return this->m_canvasHeight; }
  23. float Camera::getFov () const { return this->m_camera.projection.fov->value->getFloat (); }
  24. float Camera::getNearZ () const { return this->m_camera.projection.nearz->value->getFloat (); }
  25. float Camera::getFarZ () const { return this->m_camera.projection.farz->value->getFloat (); }
  26. void Camera::setOrthogonalProjection (
  27. const float width, const float height, const float canvasWidth, const float canvasHeight
  28. ) {
  29. this->m_width = width;
  30. this->m_height = height;
  31. this->m_canvasWidth = canvasWidth > 0.0f ? canvasWidth : width;
  32. this->m_canvasHeight = canvasHeight > 0.0f ? canvasHeight : height;
  33. float nearz = this->m_camera.projection.nearz->value->getFloat ();
  34. float farz = this->m_camera.projection.farz->value->getFloat ();
  35. this->m_projection = glm::ortho<float> (
  36. -this->m_canvasWidth / 2.0, this->m_canvasWidth / 2.0, -this->m_canvasHeight / 2.0, this->m_canvasHeight / 2.0,
  37. nearz, farz
  38. );
  39. this->m_projection = glm::translate (this->m_projection, this->getEye ());
  40. this->m_isOrthogonal = true;
  41. }