From f138311d2d2e0cc9ba0496d523bb46f2c1c9fb73 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Wed, 20 Sep 2023 23:58:58 -0700 Subject: Copy from the PlasticSCM repo, replace vendored glm wtih conan --- source/30-game/Camera.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 source/30-game/Camera.cpp (limited to 'source/30-game/Camera.cpp') diff --git a/source/30-game/Camera.cpp b/source/30-game/Camera.cpp new file mode 100644 index 0000000..39f0369 --- /dev/null +++ b/source/30-game/Camera.cpp @@ -0,0 +1,46 @@ +#include "Camera.hpp" + +#include "AppConfig.hpp" + +#include + +Camera::Camera() + : eye(0.0f, 0.0f, 0.0f) + , target(0.0, 0.0f, -2.0f) + , pixelsPerMeter{ 50.0f } // Basic default + , fov{ M_PI / 4 } // 45deg is the convention + , perspective{ false } // +{ +} + +void Camera::SetEyePos(glm::vec3 pos) { + auto lookVector = this->target - /*Old pos*/ this->eye; + this->eye = pos; + this->target = pos + lookVector; +} + +void Camera::SetTargetPos(glm::vec3 pos) { + this->target = pos; +} + +void Camera::SetTargetDirection(glm::vec3 lookVector) { + this->target = this->eye + lookVector; +} + +void Camera::SetHasPerspective(bool perspective) { + this->perspective = perspective; +} + +glm::mat4 Camera::CalcViewMatrix() const { + return glm::lookAt(eye, target, glm::vec3(0, 1, 0)); +} + +glm::mat4 Camera::CalcProjectionMatrix() const { + if (perspective) { + return glm::perspective(fov, AppConfig::mainWindowAspectRatio, 0.1f, 1000.0f); + } else { + float widthMeters = AppConfig::mainWindowWidth / pixelsPerMeter; + float heightMeters = AppConfig::mainWindowHeight / pixelsPerMeter; + return glm::ortho(-widthMeters / 2, +widthMeters / 2, -heightMeters / 2, +heightMeters / 2); + } +} -- cgit v1.2.3-70-g09d2