summaryrefslogtreecommitdiff
path: root/gfx
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-02-16 18:19:39 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2021-02-16 18:19:39 +0000
commit87701279438dd44d758bb486da15e1e5003c872d (patch)
tree9c75ce556c6662328a99d11b176499521fb7c4e1 /gfx
parentDefine a vehicle weak ptr (diff)
downloadilt-87701279438dd44d758bb486da15e1e5003c872d.tar.bz2
ilt-87701279438dd44d758bb486da15e1e5003c872d.tar.xz
ilt-87701279438dd44d758bb486da15e1e5003c872d.zip
Add a camera controller that follows vehicles
Diffstat (limited to 'gfx')
-rw-r--r--gfx/followCameraController.cpp42
-rw-r--r--gfx/followCameraController.h24
2 files changed, 66 insertions, 0 deletions
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 <gfx/gl/camera.h>
+#include <gfx/gl/shader.h>
+#include <glm/glm.hpp>
+#include <maths.h>
+#include <memory>
+#include <tuple>
+#include <utility>
+
+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());
+}
diff --git a/gfx/followCameraController.h b/gfx/followCameraController.h
new file mode 100644
index 0000000..d57b59f
--- /dev/null
+++ b/gfx/followCameraController.h
@@ -0,0 +1,24 @@
+#ifndef FOLLOW_CAMERA_CONTROLLER_H
+#define FOLLOW_CAMERA_CONTROLLER_H
+
+#include "game/worldobject.h"
+#include <game/vehicles/vehicle.h>
+#include <gfx/camera_controller.h>
+
+class Camera;
+class Shader;
+
+class FollowCameraController : public CameraController {
+public:
+ enum class Mode { Pan, Ride, ISO };
+ explicit FollowCameraController(VehicleWPtr, Mode = Mode::Pan);
+
+ void tick(TickDuration) override { }
+
+ void updateCamera(Camera * camera, Shader * shader) const override;
+
+private:
+ VehicleWPtr target;
+ Mode mode;
+};
+#endif