From ce60561dfdb3d3cb583bcc38cbcb93b9cd1b13a6 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 26 Jan 2021 21:13:41 +0000 Subject: Basic mouse navigation --- application/main.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ gfx/gl/camera.cpp | 6 ++++++ gfx/gl/camera.h | 1 + 3 files changed, 49 insertions(+) diff --git a/application/main.cpp b/application/main.cpp index fe81b88..912fa4f 100644 --- a/application/main.cpp +++ b/application/main.cpp @@ -82,6 +82,7 @@ public: auto t_start = std::chrono::high_resolution_clock::now(); const auto framelen = std::chrono::milliseconds {1000} / 120; + bool mrb {false}, ctrl {false}; while (isRunning) { while (SDL_PollEvent(&e)) { switch (e.type) { @@ -108,9 +109,50 @@ public: case SDLK_KP_9: camera.RotateY(-0.04); break; + case SDLK_LCTRL: + case SDLK_RCTRL: + ctrl = true; + break; } shader.setView(camera.GetViewProjection()); break; + case SDL_KEYUP: + switch (e.key.keysym.sym) { + case SDLK_LCTRL: + case SDLK_RCTRL: + ctrl = false; + break; + } + break; + case SDL_MOUSEBUTTONDOWN: + switch (e.button.button) { + case SDL_BUTTON_RIGHT: + SDL_SetRelativeMouseMode(SDL_TRUE); + mrb = true; + break; + } + break; + case SDL_MOUSEBUTTONUP: + switch (e.button.button) { + case SDL_BUTTON_RIGHT: + SDL_SetRelativeMouseMode(SDL_FALSE); + mrb = false; + break; + } + break; + case SDL_MOUSEMOTION: + if (mrb) { + if (ctrl) { + camera.RotateY(-0.01F * e.motion.xrel); + camera.Pitch(-0.01F * e.motion.yrel); + } + else { + camera.MoveRight(e.motion.xrel); + camera.SlideForward(e.motion.yrel); + } + shader.setView(camera.GetViewProjection()); + } + break; } } diff --git a/gfx/gl/camera.cpp b/gfx/gl/camera.cpp index b4a76d0..2f48ca5 100644 --- a/gfx/gl/camera.cpp +++ b/gfx/gl/camera.cpp @@ -19,6 +19,12 @@ Camera::MoveForward(float amt) pos += forward * amt; } +void +Camera::SlideForward(float amt) +{ + pos += forward * amt * glm::vec3 {1, 0, 1}; +} + void Camera::MoveRight(float amt) { diff --git a/gfx/gl/camera.h b/gfx/gl/camera.h index fa4296d..90d88a3 100644 --- a/gfx/gl/camera.h +++ b/gfx/gl/camera.h @@ -10,6 +10,7 @@ public: [[nodiscard]] glm::mat4 GetViewProjection() const; void MoveForward(float amt); + void SlideForward(float amt); void MoveRight(float amt); void Pitch(float angle); void RotateY(float angle); -- cgit v1.2.3