source: opengl-game/common/controls.cpp@ 92bc4fe

feature/imgui-sdl points-test
Last change on this file since 92bc4fe was 92bc4fe, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 7 years ago

Make the game window fullscreen, hide the mouse cursor, and change the view to an overhead one as the player moves around the scene

  • Property mode set to 100644
File size: 3.1 KB
Line 
1// Include GLFW
2#include <GLFW/glfw3.h>
3extern 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>
8using namespace glm;
9
10#include "controls.hpp"
11
12glm::mat4 ViewMatrix;
13glm::mat4 ProjectionMatrix;
14
15glm::mat4 getViewMatrix(){
16 return ViewMatrix;
17}
18glm::mat4 getProjectionMatrix(){
19 return ProjectionMatrix;
20}
21
22
23// Initial position : on +Z
24glm::vec3 position = glm::vec3( 0, 0, 5 );
25// Initial horizontal angle : toward -Z
26float horizontalAngle = 3.14f;
27// Initial vertical angle : none
28float verticalAngle = 0.0f;
29// Initial Field of View
30float initialFoV = 45.0f;
31
32float speed = 3.0f; // 3 units / second
33float mouseSpeed = 0.005f;
34
35
36
37void computeMatricesFromInputs(int windowWidth, int windowHeight) {
38
39 // glfwGetTime is called only once, the first time this function is called
40 static double lastTime = glfwGetTime();
41
42 // Compute time difference between current and last frame
43 double currentTime = glfwGetTime();
44 float deltaTime = float(currentTime - lastTime);
45
46 // Get mouse position
47 double xpos, ypos;
48 glfwGetCursorPos(window, &xpos, &ypos);
49
50 // Reset mouse position for next frame
51 glfwSetCursorPos(window, 1024/2, 768/2);
52
53 // Compute new orientation
54 /* STOP ROTATION FOR NOW */
55 horizontalAngle += mouseSpeed * float(1024/2 - xpos );
56 verticalAngle += mouseSpeed * float( 768/2 - ypos );
57
58 // Direction : Spherical coordinates to Cartesian coordinates conversion
59 glm::vec3 direction(
60 cos(verticalAngle) * sin(horizontalAngle),
61 sin(verticalAngle),
62 cos(verticalAngle) * cos(horizontalAngle)
63 );
64
65 // Right vector
66 glm::vec3 right = glm::vec3(
67 sin(horizontalAngle - 3.14f/2.0f),
68 0,
69 cos(horizontalAngle - 3.14f/2.0f)
70 );
71
72 // Up vector
73 glm::vec3 up = glm::cross( right, direction );
74
75 // Move forward
76 if (glfwGetKey( window, GLFW_KEY_UP ) == GLFW_PRESS){
77 position += direction * deltaTime * speed;
78 }
79 // Move backward
80 if (glfwGetKey( window, GLFW_KEY_DOWN ) == GLFW_PRESS){
81 position -= direction * deltaTime * speed;
82 }
83 // Strafe right
84 if (glfwGetKey( window, GLFW_KEY_RIGHT ) == GLFW_PRESS){
85 position += right * deltaTime * speed;
86 }
87 // Strafe left
88 if (glfwGetKey( window, GLFW_KEY_LEFT ) == GLFW_PRESS){
89 position -= right * deltaTime * speed;
90 }
91
92 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.
93
94 // Projection matrix : 45� Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
95 ProjectionMatrix = glm::perspective(FoV, 4.0f / 3.0f, 0.1f, 100.0f);
96 // Camera matrix
97 ViewMatrix = glm::lookAt(
98 position, // Camera is here
99 position+direction, // and looks here : at the same position, plus "direction"
100 up // Head is up (set to 0,-1,0 to look upside-down)
101 );
102
103 // For the next frame, the "last time" will be "now"
104 lastTime = currentTime;
105}
Note: See TracBrowser for help on using the repository browser.