diff --git a/common/controls.cpp b/common/controls.cpp index fdf91b3d5..c049977ab 100644 --- a/common/controls.cpp +++ b/common/controls.cpp @@ -19,11 +19,12 @@ glm::mat4 getProjectionMatrix(){ return ProjectionMatrix; } +const float PI = glm::pi(); // Initial position : on +Z glm::vec3 position = glm::vec3( 0, 0, 5 ); // Initial horizontal angle : toward -Z -float horizontalAngle = 3.14f; +float horizontalAngle = PI; // Initial vertical angle : none float verticalAngle = 0.0f; // Initial Field of View @@ -32,8 +33,6 @@ float initialFoV = 45.0f; float speed = 3.0f; // 3 units / second float mouseSpeed = 0.005f; - - void computeMatricesFromInputs(){ // glfwGetTime is called only once, the first time this function is called @@ -43,6 +42,14 @@ void computeMatricesFromInputs(){ double currentTime = glfwGetTime(); float deltaTime = float(currentTime - lastTime); + // First time this function is called, the cursor position is reset since + // an unintended movement will otherwise register on the first frame + static bool first_frame = true; + if (first_frame){ + glfwSetCursorPos(window, 1024/2, 768/2); + first_frame = false; + } + // Get mouse position double xpos, ypos; glfwGetCursorPos(window, &xpos, &ypos); @@ -52,7 +59,14 @@ void computeMatricesFromInputs(){ // Compute new orientation horizontalAngle += mouseSpeed * float(1024/2 - xpos ); - verticalAngle += mouseSpeed * float( 768/2 - ypos ); + verticalAngle -= mouseSpeed * float( 768/2 - ypos ); + + if (verticalAngle < PI/2+0.01) { + verticalAngle = PI/2+0.01; + } + if (verticalAngle > 3*PI/2-0.01) { + verticalAngle = 3*PI/2-0.01; + } // Direction : Spherical coordinates to Cartesian coordinates conversion glm::vec3 direction( @@ -60,32 +74,47 @@ void computeMatricesFromInputs(){ sin(verticalAngle), cos(verticalAngle) * cos(horizontalAngle) ); - - // Right vector - glm::vec3 right = glm::vec3( - sin(horizontalAngle - 3.14f/2.0f), - 0, - cos(horizontalAngle - 3.14f/2.0f) - ); - + + // Forwards vector + glm::vec3 forwards = glm::normalize(glm::vec3(direction.x,0,direction.z)); + glm::vec3 backwards = (-1.0f)*forwards; + // Up vector - glm::vec3 up = glm::cross( right, direction ); + glm::vec3 up = glm::vec3( 0, 1, 0 ); + glm::vec3 down = (-1.0f)*up; + + // Right vector + glm::vec3 right = glm::cross(forwards,up); + glm::vec3 left = (-1.0f)*right; // Move forward - if (glfwGetKey( window, GLFW_KEY_UP ) == GLFW_PRESS){ - position += direction * deltaTime * speed; + if (glfwGetKey( window, GLFW_KEY_UP ) == GLFW_PRESS + || glfwGetKey( window, GLFW_KEY_W ) == GLFW_PRESS){ + position += forwards * deltaTime * speed; } // Move backward - if (glfwGetKey( window, GLFW_KEY_DOWN ) == GLFW_PRESS){ - position -= direction * deltaTime * speed; + if (glfwGetKey( window, GLFW_KEY_DOWN ) == GLFW_PRESS + || glfwGetKey( window, GLFW_KEY_S ) == GLFW_PRESS){ + position += backwards * deltaTime * speed; } // Strafe right - if (glfwGetKey( window, GLFW_KEY_RIGHT ) == GLFW_PRESS){ + if (glfwGetKey( window, GLFW_KEY_RIGHT ) == GLFW_PRESS + || glfwGetKey( window, GLFW_KEY_D ) == GLFW_PRESS){ position += right * deltaTime * speed; } // Strafe left - if (glfwGetKey( window, GLFW_KEY_LEFT ) == GLFW_PRESS){ - position -= right * deltaTime * speed; + if (glfwGetKey( window, GLFW_KEY_LEFT ) == GLFW_PRESS + || glfwGetKey( window, GLFW_KEY_A ) == GLFW_PRESS){ + position += left * deltaTime * speed; + } + // Move up + if (glfwGetKey( window, GLFW_KEY_SPACE ) == GLFW_PRESS){ + position += up * deltaTime * speed; + } + // Move down + if (glfwGetKey( window, GLFW_KEY_LEFT_SHIFT ) == GLFW_PRESS + || glfwGetKey( window, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS){ + position += down * deltaTime * speed; } float FoV = initialFoV;// - 5 * glfwGetMouseWheel(); // Now GLFW 3 requires setting up a callback for this. It's a bit too complicated for this beginner's tutorial, so it's disabled instead. @@ -101,4 +130,4 @@ void computeMatricesFromInputs(){ // For the next frame, the "last time" will be "now" lastTime = currentTime; -} \ No newline at end of file +}