summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-02-28 23:29:37 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2021-02-28 23:29:37 +0000
commitdd236a39906de5855fdd1252b17a7337f0c7b052 (patch)
treedbd19e1a278feca0f7e00409aab2cc9d2125a51a
parentGlobal definition of quarter_pi (diff)
downloadilt-dd236a39906de5855fdd1252b17a7337f0c7b052.tar.bz2
ilt-dd236a39906de5855fdd1252b17a7337f0c7b052.tar.xz
ilt-dd236a39906de5855fdd1252b17a7337f0c7b052.zip
Vastly improved manual camera controller
-rw-r--r--application/main.cpp3
-rw-r--r--gfx/manualCameraController.cpp30
-rw-r--r--gfx/manualCameraController.h9
3 files changed, 27 insertions, 15 deletions
diff --git a/application/main.cpp b/application/main.cpp
index 2f37c59..8257a79 100644
--- a/application/main.cpp
+++ b/application/main.cpp
@@ -98,7 +98,8 @@ public:
const auto framelen = std::chrono::milliseconds {1000} / 120;
inputStack.objects.push_back(shared_from_this());
- inputStack.objects.insert(inputStack.objects.begin(), world.create<ManualCameraController>());
+ inputStack.objects.insert(
+ inputStack.objects.begin(), world.create<ManualCameraController>(glm::vec2 {-1150, -1150}));
while (isRunning) {
processInputs();
diff --git a/gfx/manualCameraController.cpp b/gfx/manualCameraController.cpp
index 01787c6..62c48c8 100644
--- a/gfx/manualCameraController.cpp
+++ b/gfx/manualCameraController.cpp
@@ -1,5 +1,8 @@
#include "manualCameraController.h"
+#include <algorithm>
+#include <cmath>
#include <gfx/gl/camera.h>
+#include <maths.h>
bool
ManualCameraController::handleInput(SDL_Event & e)
@@ -39,9 +42,19 @@ ManualCameraController::handleInput(SDL_Event & e)
break;
case SDL_MOUSEMOTION:
if (mrb) {
- motion = e.motion;
+ if (ctrl) {
+ direction -= 0.01F * e.motion.xrel;
+ pitch = std::clamp(pitch - 0.01F * e.motion.yrel, 0.1F, half_pi);
+ }
+ else {
+ focus.x += cos(direction) * e.motion.xrel + sin(direction) * e.motion.yrel;
+ focus.y += cos(direction) * e.motion.yrel - sin(direction) * e.motion.xrel;
+ }
}
return true;
+ case SDL_MOUSEWHEEL:
+ dist = std::clamp(dist - e.wheel.y * 4.F, 5.F, 200.F);
+ break;
}
return false;
}
@@ -49,15 +62,8 @@ ManualCameraController::handleInput(SDL_Event & e)
void
ManualCameraController::updateCamera(Camera * camera) const
{
- if (motion) {
- if (ctrl) {
- camera->RotateY(-0.01F * motion->xrel);
- camera->Pitch(-0.01F * motion->yrel);
- }
- else {
- camera->MoveRight(motion->xrel);
- camera->SlideForward(motion->yrel);
- }
- motion.reset();
- }
+ const auto rel {glm::normalize(glm::vec3 {sin(direction), -sin(pitch), cos(direction)})};
+ camera->pos = !focus + up * 3.F - (rel * std::pow(dist, 1.3F));
+ camera->forward = glm::normalize(rel);
+ camera->up = up;
}
diff --git a/gfx/manualCameraController.h b/gfx/manualCameraController.h
index 465f31f..f2141a4 100644
--- a/gfx/manualCameraController.h
+++ b/gfx/manualCameraController.h
@@ -5,12 +5,15 @@
#include "inputHandler.h"
#include <SDL2/SDL.h>
#include <gfx/camera_controller.h>
-#include <optional>
+#include <glm/glm.hpp>
+#include <maths.h>
class Camera;
class ManualCameraController : public CameraController, public InputHandler {
public:
+ explicit ManualCameraController(glm::vec2 f) : focus {f} { }
+
bool handleInput(SDL_Event & e) override;
void tick(TickDuration) override { }
@@ -19,6 +22,8 @@ public:
private:
bool ctrl {false}, mrb {false};
- mutable std::optional<SDL_MouseMotionEvent> motion;
+ glm::vec2 focus;
+ float direction {quarter_pi};
+ float dist {40}, pitch {quarter_pi};
};
#endif