diff options
Diffstat (limited to 'src/camera')
| -rw-r--r-- | src/camera/camera.cpp | 72 | ||||
| -rw-r--r-- | src/camera/camera.h | 49 |
2 files changed, 121 insertions, 0 deletions
diff --git a/src/camera/camera.cpp b/src/camera/camera.cpp new file mode 100644 index 0000000..62e8021 --- /dev/null +++ b/src/camera/camera.cpp @@ -0,0 +1,72 @@ +#include <stdexcept> +#include "camera.h" + +Camera::Camera(SceneCameraData cameraData) : + m_pos(cameraData.pos), + m_heightAngle(cameraData.heightAngle), + m_focalLength(cameraData.focalLength), + m_aperture(cameraData.aperture) +{ + // VIEW MATRIX INTIALIZATION + // cast to 3 for dots & cross + glm::vec3 look3{cameraData.look.x, cameraData.look.y, cameraData.look.z}; + glm::vec3 up3{cameraData.up.x, cameraData.up.y, cameraData.up.z}; + + // calculate new basis + glm::vec3 e0 = -glm::normalize(look3); + glm::vec3 e1 = glm::normalize(up3 - glm::dot(up3, e0) * e0); + glm::vec3 e2 = glm::cross(e1, e0); + + glm::mat4 alignment + { + e2.x, e1.x, e0.x, 0.f, + e2.y, e1.y, e0.y, 0.f, + e2.z, e1.z, e0.z, 0.f, + 0.f, 0.f, 0.f, 1.f + }; + glm::mat4 translation + { + 1.f, 0.f, 0.f, 0.f, + 0.f, 1.f, 0.f, 0.f, + 0.f, 0.f, 1.f, 0.f, + -cameraData.pos.x, -cameraData.pos.y, -cameraData.pos.z, 1.f + }; + + m_viewMatrix = alignment * translation; + m_inverse = glm::inverse(m_viewMatrix); +} + + +glm::mat4 Camera::getViewMatrix() const { + // Optional TODO: implement the getter or make your own design + return m_viewMatrix; +} + +glm::mat4 Camera::getInverseViewMatrix() const { + // Optional TODO: implement the getter or make your own design + return m_inverse; +} + +float Camera::getAspectRatio() const { + // Optional TODO: implement the getter or make your own design + throw std::runtime_error("not implemented"); +} + +float Camera::getHeightAngle() const { + // Optional TODO: implement the getter or make your own design + return m_heightAngle; +} + +float Camera::getFocalLength() const { + // Optional TODO: implement the getter or make your own design + return m_focalLength; +} + +float Camera::getAperture() const { + // Optional TODO: implement the getter or make your own design + return m_aperture; +} + +glm::vec3 Camera::getPos() const { + return m_pos; +} diff --git a/src/camera/camera.h b/src/camera/camera.h new file mode 100644 index 0000000..e0cd013 --- /dev/null +++ b/src/camera/camera.h @@ -0,0 +1,49 @@ +#pragma once + +#include "utils/scenedata.h" +#include <glm/glm.hpp> + +// A class representing a virtual camera. + +// Feel free to make your own design choices for Camera class, the functions below are all optional / for your convenience. +// You can either implement and use these getters, or make your own design. +// If you decide to make your own design, feel free to delete these as TAs won't rely on them to grade your assignments. + +class Camera { +public: + Camera(SceneCameraData cameraData); + // Returns the view matrix for the current camera settings. + // You might also want to define another function that return the inverse of the view matrix. + glm::mat4 getViewMatrix() const; + glm::mat4 getInverseViewMatrix() const; + + // Returns the aspect ratio of the camera. + float getAspectRatio() const; + + // Returns the height angle of the camera in RADIANS. + float getHeightAngle() const; + + // Returns the focal length of this camera. + // This is for the depth of field extra-credit feature only; + // You can ignore if you are not attempting to implement depth of field. + float getFocalLength() const; + + // Returns the focal length of this camera. + // This is for the depth of field extra-credit feature only; + // You can ignore if you are not attempting to implement depth of field. + float getAperture() const; + + glm::vec3 getPos() const; + + float cameraDepth = -1.f; + +private: + glm::mat4 m_viewMatrix; + glm::mat4 m_inverse; + float m_heightAngle; + glm::vec3 m_pos; + + float m_focalLength; + float m_aperture; +}; + |
