From 87701279438dd44d758bb486da15e1e5003c872d Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 16 Feb 2021 18:19:39 +0000 Subject: Add a camera controller that follows vehicles --- gfx/followCameraController.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 gfx/followCameraController.cpp (limited to 'gfx/followCameraController.cpp') diff --git a/gfx/followCameraController.cpp b/gfx/followCameraController.cpp new file mode 100644 index 0000000..0034c3b --- /dev/null +++ b/gfx/followCameraController.cpp @@ -0,0 +1,42 @@ +#include "followCameraController.h" +#include "game/vehicles/vehicle.h" +#include "gfx/gl/transform.h" +#include +#include +#include +#include +#include +#include +#include + +FollowCameraController::FollowCameraController(VehicleWPtr t, Mode m) : target(std::move(t)), mode(m) { } + +void +FollowCameraController::updateCamera(Camera * camera, Shader * shader) const +{ + const auto [pos, rot] = [this]() { + const auto t {target.lock()}; + return std::tie(t->getLocation().GetPos(), t->getLocation().GetRot()); + }(); + + switch (mode) { + case Mode::Pan: + camera->forward = glm::normalize(pos - camera->pos); + camera->up = up; + break; + + case Mode::Ride: + camera->pos = pos + (up * 4.8F); + camera->forward = {-std::sin(rot.y), 0.F, -std::cos(rot.y)}; + camera->up = up; + break; + + case Mode::ISO: + camera->pos = pos + ((up + north + east) * 20.F); + camera->forward = -glm::normalize(up + north + east); + camera->up = glm::normalize(up - north - east); + break; + } + + shader->setView(camera->GetViewProjection()); +} -- cgit v1.2.3