// Include GLFW #include extern GLFWwindow* window; // The "extern" keyword here is to access the variable "window" declared in tutorialXXX.cpp. This is a hack to keep the tutorials simple. Please avoid this. #include using namespace std; // Include GLM #include #include using namespace glm; #include "controls.hpp" glm::mat4 ViewMatrix; glm::mat4 ProjectionMatrix; glm::mat4 getViewMatrix(){ return ViewMatrix; } glm::mat4 getProjectionMatrix(){ return ProjectionMatrix; } // Initial position : on +Z glm::vec3 position = glm::vec3(4,6,-3); //glm::vec3( 0, 0, 5 ); // Initial horizontal angle : toward -Z float horizontalAngleBase = 3.14f * 3.0f / 2.0f; // 3.14f; // Initial vertical angle : none float verticalAngle = 0.0f; // Initial Field of View float initialFoV = 45.0f; float speed = 3.0f; // 3 units / second float mouseSpeed = 0.005f; int centeredCount = 0; void computeMatricesFromInputs(int windowWidth, int windowHeight) { // glfwGetTime is called only once, the first time this function is called static double lastTime = glfwGetTime(); // Compute time difference between current and last frame double currentTime = glfwGetTime(); float deltaTime = float(currentTime - lastTime); // Get mouse position double xpos, ypos; glfwGetCursorPos(window, &xpos, &ypos); // Stupid hack to set the cursor position correctly // The call has no effect the first several times it's called if (centeredCount < 100) { glfwSetCursorPos(window, windowWidth/2, windowHeight/2); centeredCount++; } // Compute new orientation /* STOP ROTATION FOR NOW */ float horizontalAngle = horizontalAngleBase + mouseSpeed * float(windowWidth/2 - xpos ); // verticalAngle += mouseSpeed * float( windowHeight/2 - ypos ); // Direction : Spherical coordinates to Cartesian coordinates conversion glm::vec3 direction( cos(verticalAngle) * sin(horizontalAngle), sin(verticalAngle), cos(verticalAngle) * cos(horizontalAngle) ); glm::vec3 lookDirection( 0.0f, -3.0f, 0.0f ); lookDirection = lookDirection + 3.0f * direction; // Right vector glm::vec3 right = glm::vec3( sin(horizontalAngle - 3.14f/2.0f), 0, cos(horizontalAngle - 3.14f/2.0f) ); // Up vector // glm::vec3 up = glm::cross( right, direction ); // Move forward if (glfwGetKey( window, GLFW_KEY_UP ) == GLFW_PRESS){ position += direction * deltaTime * speed; } // Move backward if (glfwGetKey( window, GLFW_KEY_DOWN ) == GLFW_PRESS){ position -= direction * deltaTime * speed; } // Strafe right if (glfwGetKey( window, GLFW_KEY_RIGHT ) == GLFW_PRESS){ position += right * deltaTime * speed; } // Strafe left if (glfwGetKey( window, GLFW_KEY_LEFT ) == GLFW_PRESS){ position -= right * 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. // Projection matrix : 45� Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units ProjectionMatrix = glm::perspective(FoV, 4.0f / 3.0f, 0.1f, 100.0f); // Camera matrix ViewMatrix = glm::lookAt( position, // Camera is here //position+direction, // and looks here : at the same position, plus "direction" // position+glm::vec3(-4,0,0), // position+glm::vec3(-4,-3,3), position+lookDirection, glm::vec3(0,1,0) //up // Head is up (set to 0,-1,0 to look upside-down) ); // For the next frame, the "last time" will be "now" lastTime = currentTime; }