Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | None | 0 0
  1. // Include GLFW
  2. #include <GLFW/glfw3.h>
  3. 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.
  4.  
  5. // Include GLM
  6. #include <glm/glm.hpp>
  7. #include <glm/gtc/matrix_transform.hpp>
  8. using namespace glm;
  9.  
  10. #include "controls.hpp"
  11.  
  12. glm::mat4 ViewMatrix;
  13. glm::mat4 ProjectionMatrix;
  14.  
  15. glm::mat4 getViewMatrix() {
  16.     return ViewMatrix;
  17. }
  18. glm::mat4 getProjectionMatrix() {
  19.     return ProjectionMatrix;
  20. }
  21.  
  22.  
  23. // Initial position : on +Z
  24. glm::vec3 position = glm::vec3(6, 6, 5);
  25. // Initial horizontal angle : toward -Z
  26. float horizontalAngle = -1.0f;
  27. // Initial vertical angle : none
  28. float verticalAngle = -1.9f;
  29. // Initial Field of View
  30. float initialFoV = 60.0f;
  31.  
  32. float speed = 6.0f; // 6u/sec
  33. float mouseSpeed = 0.005f;
  34.  
  35.  
  36. void computeMatricesFromInputs() {
  37.     // glfwGetTime is called only once, the first time this function is called
  38.     static double lastTime = glfwGetTime();
  39.  
  40.     // Compute time difference between current and last frame
  41.     double currentTime = glfwGetTime();
  42.     float deltaTime = float(currentTime - lastTime);
  43.  
  44.     // Get mouse position
  45.     double xpos, ypos;
  46.     glfwGetCursorPos(window, &xpos, &ypos);
  47.  
  48.     // Reset mouse position for next frame
  49.     glfwSetCursorPos(window, 1024 / 2, 768 / 2);
  50.  
  51.     // Compute new orientation
  52.     horizontalAngle += mouseSpeed * float(1024 / 2 - xpos);
  53.     verticalAngle += mouseSpeed * float(768 / 2 - ypos);
  54.  
  55.     // Direction : Spherical coordinates to Cartesian coordinates conversion
  56.     glm::vec3 direction(
  57.         cos(verticalAngle) * sin(horizontalAngle),
  58.         sin(verticalAngle),
  59.         cos(verticalAngle) * cos(horizontalAngle)
  60.     );
  61.  
  62.     // Right vector
  63.     glm::vec3 right = glm::vec3(
  64.         sin(horizontalAngle - 3.14f / 2.0f),
  65.         0,
  66.         cos(horizontalAngle - 3.14f / 2.0f)
  67.     );
  68.  
  69.     // Up vector
  70.     glm::vec3 up = glm::cross(right, direction);
  71.  
  72.     // Move forward
  73.     if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
  74.         position += direction * deltaTime * speed;
  75.     }
  76.     // Move backward
  77.     if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
  78.         position -= direction * deltaTime * speed;
  79.     }
  80.     // Strafe right
  81.     if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
  82.         position += right * deltaTime * speed;
  83.     }
  84.     // Strafe left
  85.     if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
  86.         position -= right * deltaTime * speed;
  87.     }
  88.     // Quit
  89.     if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
  90.         glfwSetWindowShouldClose(window, GLFW_TRUE);
  91.     }
  92.  
  93.     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.
  94.  
  95.     // Projection matrix : 45� Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
  96.     ProjectionMatrix = glm::perspective(glm::radians(FoV), 4.0f / 3.0f, 0.1f, 100.0f);
  97.     // Camera matrix
  98.     ViewMatrix = glm::lookAt(
  99.         position,           // Camera is here
  100.         position + direction, // and looks here : at the same position, plus "direction"
  101.         up                  // Head is up (set to 0,-1,0 to look upside-down)
  102.     );
  103.  
  104.     // For the next frame, the "last time" will be "now"
  105.     lastTime = currentTime;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement