diff --git a/common/controls.cpp b/common/controls.cpp index fdf91b3d5..09af712c9 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,45 @@ 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 ); + } + else { + firstPass = false; + } // 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 +117,4 @@ void computeMatricesFromInputs(){ // For the next frame, the "last time" will be "now" lastTime = currentTime; -} \ No newline at end of file +}