source: opengl-game/common/controls-new.cpp@ 4762301

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