From d1ad0f94d8d26b9f6788424cc1d7186e363c9fdd Mon Sep 17 00:00:00 2001 From: William Emerison Six Date: Fri, 31 Jan 2020 21:12:03 -0500 Subject: [PATCH 1/2] the camera was terrible on Linux. Fixed that --- common/controls.cpp | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/common/controls.cpp b/common/controls.cpp index fdf91b3d5..a694ab623 100644 --- a/common/controls.cpp +++ b/common/controls.cpp @@ -21,7 +21,7 @@ glm::mat4 getProjectionMatrix(){ // Initial position : on +Z -glm::vec3 position = glm::vec3( 0, 0, 5 ); +glm::vec3 position = glm::vec3( 0, 0, 5 ); // Initial horizontal angle : toward -Z float horizontalAngle = 3.14f; // Initial vertical angle : none @@ -45,29 +45,42 @@ void computeMatricesFromInputs(){ // Get mouse position double xpos, ypos; - glfwGetCursorPos(window, &xpos, &ypos); + + // for some reason, on linux, the camera ends up in a random location + // because of glfwGetCursorPos glfwSetCursorPos, whereas on Windows + // it works just fine. + // To make the camera work consistently across platforms, set + // the cursor location on first rendering, and only compute the + // offset on subsequent renderings. + static bool firstPass = true; + if(!firstPass){ + glfwGetCursorPos(window, &xpos, &ypos); + } // Reset mouse position for next frame glfwSetCursorPos(window, 1024/2, 768/2); - // Compute new orientation - horizontalAngle += mouseSpeed * float(1024/2 - xpos ); - verticalAngle += mouseSpeed * float( 768/2 - ypos ); + // Compute new orientation after the first frame + if(!firstPass){ + // Compute new orientation + horizontalAngle += mouseSpeed * float(1024/2 - xpos ); + verticalAngle += mouseSpeed * float( 768/2 - ypos ); + } // Direction : Spherical coordinates to Cartesian coordinates conversion glm::vec3 direction( - cos(verticalAngle) * sin(horizontalAngle), + cos(verticalAngle) * sin(horizontalAngle), sin(verticalAngle), cos(verticalAngle) * cos(horizontalAngle) ); - + // Right vector glm::vec3 right = glm::vec3( - sin(horizontalAngle - 3.14f/2.0f), + sin(horizontalAngle - 3.14f/2.0f), 0, cos(horizontalAngle - 3.14f/2.0f) ); - + // Up vector glm::vec3 up = glm::cross( right, direction ); @@ -101,4 +114,4 @@ void computeMatricesFromInputs(){ // For the next frame, the "last time" will be "now" lastTime = currentTime; -} \ No newline at end of file +} From d90ac3c0fdc8073794c5986aff7355c6df6a1977 Mon Sep 17 00:00:00 2001 From: William Emerison Six Date: Sat, 1 Feb 2020 03:15:32 -0500 Subject: [PATCH 2/2] In the previous commit, although the camera was in the correct place, I broke mouse controls. The commit fixes that --- common/controls.cpp | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/common/controls.cpp b/common/controls.cpp index a694ab623..09af712c9 100644 --- a/common/controls.cpp +++ b/common/controls.cpp @@ -46,26 +46,29 @@ void computeMatricesFromInputs(){ // Get mouse position double xpos, ypos; - // for some reason, on linux, the camera ends up in a random location - // because of glfwGetCursorPos glfwSetCursorPos, whereas on Windows - // it works just fine. - // To make the camera work consistently across platforms, set - // the cursor location on first rendering, and only compute the - // offset on subsequent renderings. - static bool firstPass = true; - if(!firstPass){ - glfwGetCursorPos(window, &xpos, &ypos); - } + // for some reason, on linux, the camera ends up in a random location + // because of glfwGetCursorPos glfwSetCursorPos, whereas on Windows + // it works just fine. + // To make the camera work consistently across platforms, set + // the cursor location on first rendering, and only compute the + // offset on subsequent renderings. + static bool firstPass = true; + if(!firstPass){ + glfwGetCursorPos(window, &xpos, &ypos); + } // Reset mouse position for next frame glfwSetCursorPos(window, 1024/2, 768/2); - // Compute new orientation after the first frame - if(!firstPass){ - // Compute new orientation - horizontalAngle += mouseSpeed * float(1024/2 - xpos ); - verticalAngle += mouseSpeed * float( 768/2 - ypos ); - } + // Compute new orientation after the first frame + if(!firstPass){ + // Compute new orientation + horizontalAngle += mouseSpeed * float(1024/2 - xpos ); + verticalAngle += mouseSpeed * float( 768/2 - ypos ); + } + else { + firstPass = false; + } // Direction : Spherical coordinates to Cartesian coordinates conversion glm::vec3 direction(