From 5792db239efe7e97abf931598c5d5183a18a6b84 Mon Sep 17 00:00:00 2001
From: Joseph Walton-Rivers <joseph@walton-rivers.uk>
Date: Sat, 11 Sep 2021 21:09:22 +0100
Subject: [PATCH] use NDC for mouse axis

---
 demo/main.cpp       | 10 +++++-----
 fggl/gfx/window.cpp | 12 +++++++++---
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/demo/main.cpp b/demo/main.cpp
index 1186476..70aa8b2 100644
--- a/demo/main.cpp
+++ b/demo/main.cpp
@@ -55,7 +55,7 @@ camera_type cam_mode = cam_free;
 using namespace fggl::input;
 using InputManager = std::shared_ptr<fggl::input::Input>;
 
-void process_arcball(fggl::gfx::Window& window, fggl::ecs::ECS& ecs, InputManager input, fggl::ecs::entity_t cam) {
+void process_arcball(fggl::ecs::ECS& ecs, InputManager input, fggl::ecs::entity_t cam) {
 	// see https://asliceofrendering.com/camera/2019/11/30/ArcballCamera/
 	auto camTransform = ecs.getComponent<fggl::math::Transform>(cam);
 	auto camComp = ecs.getComponent<fggl::gfx::Camera>(cam);
@@ -67,8 +67,8 @@ void process_arcball(fggl::gfx::Window& window, fggl::ecs::ECS& ecs, InputManage
 	glm::vec3 viewDir = -glm::transpose(view)[2];
 	glm::vec3 rightDir = glm::transpose(view)[0];
 
-	float deltaAngleX = ( 2 * M_PI / window.width() ); 
-	float deltaAngleY = ( M_PI / window.height() );
+	float deltaAngleX = ( 2 * M_PI ); 
+	float deltaAngleY = ( M_PI );
 	float xAngle = ( -mouse.axisDelta(fggl::input::MouseAxis::X) ) * deltaAngleX;
 	float yAngle = ( -mouse.axisDelta(fggl::input::MouseAxis::Y) ) * deltaAngleY;
 
@@ -170,14 +170,14 @@ void process_camera(fggl::gfx::Window& window, fggl::ecs::ECS& ecs, InputManager
 
 	// scroll wheel
 	glm::vec3 motion(0.0f);
-	float delta = input->mouse.axisDelta( fggl::input::MouseAxis::SCROLL_Y );
+	float delta = input->mouse.axis( fggl::input::MouseAxis::SCROLL_Y );
 	if ( (glm::length( dir ) < 25.0f && delta < 0.0f) || (glm::length( dir ) > 2.5f && delta > 0.0f) )
 		motion -= (forward * delta);
 
 	camTransform->origin( camTransform->origin() + motion );
 
 	if ( cam_mode == cam_arcball || input->mouse.button( fggl::input::MouseButton::MIDDLE ) ) { 
-		process_arcball(window, ecs, input, cam);
+		process_arcball(ecs, input, cam);
 	} else if ( cam_mode == cam_free ) {
 		process_freecam(ecs, input, cam);
 	}
diff --git a/fggl/gfx/window.cpp b/fggl/gfx/window.cpp
index 78ab5f9..898a3b1 100644
--- a/fggl/gfx/window.cpp
+++ b/fggl/gfx/window.cpp
@@ -1,4 +1,3 @@
-
 #include "window.hpp"
 #include "window_input.hpp"
 
@@ -6,8 +5,6 @@
 #include <string>
 #include <stdexcept>
 
-#include <GLFW/glfw3.h>
-
 using namespace fggl::gfx;
 
 static void glfw_error(int code, const char* description) {
@@ -24,6 +21,15 @@ static void framebuffer_resize(GLFWwindow* window, int width, int height) {
 
 static void fggl_input_cursor(GLFWwindow* window, double x, double y) {
 	auto& input = GlfwInputManager::instance();
+	auto fgglWin = (Window*)glfwGetWindowUserPointer(window);
+
+	#ifndef FGGL_INPUT_SCREEN_COORDS
+		// convert to nice ranges...
+		x = (x / fgglWin->width() * 2) - 1.0; // [-1, 1]
+		y = (y / fgglWin->height() * 2) - 1.0; // [-1, 1]
+	#endif
+
+	// inform the input system
 	input.onMouseMove(x, y);
 }
 
-- 
GitLab