From: Daniel Karbach Date: Thu, 12 Feb 2015 21:13:25 +0000 (+0100) Subject: mouse controlled camera pitch/yaw X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=ea1ce7b0fb7709ae56977480821ac96a231a0686;p=blank.git mouse controlled camera pitch/yaw --- diff --git a/src/app.cpp b/src/app.cpp index 038aa1a..ff5a3be 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -24,6 +24,8 @@ Application::Application() , ctx(window.CreateContext()) , init_glew() , program() +, pitch_sensitivity(-0.0025f) +, yaw_sensitivity(-0.001f) , cam() , model() , vtx_buf(0) @@ -63,6 +65,7 @@ Application::Application() glBufferData(GL_ARRAY_BUFFER, sizeof(vtx_coords), vtx_coords, GL_STATIC_DRAW); model.Position(glm::vec3(0, 0, -4)); + cam.Position(glm::vec3(0, 0, 4)); mvp_handle = program.UniformLocation("MVP"); @@ -77,6 +80,7 @@ Application::~Application() { void Application::Run() { running = true; Uint32 last = SDL_GetTicks(); + window.GrabMouse(); while (running) { Uint32 now = SDL_GetTicks(); int delta = now - last; @@ -95,6 +99,12 @@ void Application::HandleEvents() { SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { + case SDL_MOUSEMOTION: + cam.RotateYaw(event.motion.xrel * yaw_sensitivity); + cam.RotatePitch(event.motion.yrel * pitch_sensitivity); + std::cout << "x: " << event.motion.xrel << ", y: " << event.motion.yrel + << ", pitch: " << cam.Pitch() << ", yaw: " << cam.Yaw() << std::endl; + break; case SDL_QUIT: running = false; break; diff --git a/src/app.hpp b/src/app.hpp index 4e381a2..8bafb23 100644 --- a/src/app.hpp +++ b/src/app.hpp @@ -36,6 +36,9 @@ private: InitGLEW init_glew; Program program; + float pitch_sensitivity; + float yaw_sensitivity; + Camera cam; Model model; diff --git a/src/camera.cpp b/src/camera.cpp index 8309fe6..21df874 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -11,11 +11,9 @@ Camera::Camera() , aspect(1.0f) , near_clip(0.1f) , far_clip(100.0f) -, position(0, 0, 0) -, target(0, 0, -1) -, up(0, 1, 0) +, model() , projection(glm::perspective(fov, aspect, near_clip, far_clip)) -, view(glm::lookAt(position, target, up)) +, view(model.Transform()) , vp(projection * view) { } @@ -61,7 +59,7 @@ void Camera::UpdateProjection() { } void Camera::UpdateView() { - view = glm::lookAt(position, target, up); + view = glm::inverse(model.Transform()); vp = projection * view; } diff --git a/src/camera.hpp b/src/camera.hpp index 5be71e2..88da8ca 100644 --- a/src/camera.hpp +++ b/src/camera.hpp @@ -3,6 +3,8 @@ #include +#include "model.hpp" + namespace blank { @@ -25,10 +27,16 @@ public: void Aspect(float w, float h); void Clip(float near, float far); - void Position(glm::vec3 pos) { position = pos; UpdateView(); } - void Move(glm::vec3 delta) { position += delta; UpdateView(); } + void Position(glm::vec3 pos) { model.Position(pos); UpdateView(); } + void Move(glm::vec3 delta) { model.Move(delta); UpdateView(); } - void LookAt(glm::vec3 tgt) { target = tgt; UpdateView(); } + // all angles in radians (full circle = 2π) + float Pitch() const { return model.Pitch(); } + void Pitch(float p) { model.Pitch(p); UpdateView(); } + void RotatePitch(float delta) { model.RotatePitch(delta); UpdateView(); } + float Yaw() const { return model.Yaw(); } + void Yaw(float y) { model.Yaw(y); UpdateView(); } + void RotateYaw(float delta) { model.RotateYaw(delta); UpdateView(); } private: void UpdateProjection(); @@ -40,9 +48,7 @@ private: float near_clip; float far_clip; - glm::vec3 position; - glm::vec3 target; - glm::vec3 up; + Model model; glm::mat4 projection; glm::mat4 view; diff --git a/src/init.cpp b/src/init.cpp index 20f4fe4..4772898 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -83,6 +83,26 @@ Window::~Window() { SDL_DestroyWindow(handle); } +void Window::GrabInput() { + SDL_SetWindowGrab(handle, SDL_TRUE); +} + +void Window::ReleaseInput() { + SDL_SetWindowGrab(handle, SDL_FALSE); +} + +void Window::GrabMouse() { + if (SDL_SetRelativeMouseMode(SDL_TRUE) != 0) { + sdl_error("SDL_SetRelativeMouseMode"); + } +} + +void Window::ReleaseMouse() { + if (SDL_SetRelativeMouseMode(SDL_FALSE) != 0) { + sdl_error("SDL_SetRelativeMouseMode"); + } +} + GLContext Window::CreateContext() { return GLContext(handle); } diff --git a/src/init.hpp b/src/init.hpp index bf6191b..a2f2de7 100644 --- a/src/init.hpp +++ b/src/init.hpp @@ -54,6 +54,12 @@ public: Window(const Window &) = delete; Window &operator =(const Window &) = delete; + void GrabInput(); + void ReleaseInput(); + + void GrabMouse(); + void ReleaseMouse(); + GLContext CreateContext(); void Flip(); diff --git a/src/model.hpp b/src/model.hpp index c0bda41..938fd44 100644 --- a/src/model.hpp +++ b/src/model.hpp @@ -18,8 +18,10 @@ public: void Move(glm::vec3 delta) { position += delta; } // all angles in radians (full circle = 2π) + float Pitch() const { return pitch; } void Pitch(float p) { pitch = p; } void RotatePitch(float delta) { pitch += delta; } + float Yaw() const { return yaw; } void Yaw(float y) { yaw = y; } void RotateYaw(float delta) { yaw += delta; }