source: network-game/graphics_library/common/controls.cpp@ ace001a

Last change on this file since ace001a was af116c0, checked in by Dmitry Portnoy <dmp1488@…>, 11 years ago

new graphics library code

  • Property mode set to 100644
File size: 3.6 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(){
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 horizontalAngle += mouseSpeed * float(1024/2 - xpos );
55 verticalAngle += mouseSpeed * float( 768/2 - ypos );
56
57 // Direction : Spherical coordinates to Cartesian coordinates conversion
58 glm::vec3 direction(
59 cos(verticalAngle) * sin(horizontalAngle),
60 sin(verticalAngle),
61 cos(verticalAngle) * cos(horizontalAngle)
62 );
63
64 // Right vector
65 glm::vec3 right = glm::vec3(
66 sin(horizontalAngle - 3.14f/2.0f),
67 0,
68 cos(horizontalAngle - 3.14f/2.0f)
69 );
70
71 // Up vector
72 glm::vec3 up = glm::cross( right, direction );
73
74 // Move forward
75 if (glfwGetKey( window, GLFW_KEY_UP ) == GLFW_PRESS){
76 position += direction * deltaTime * speed;
77 }
78 // Move backward
79 if (glfwGetKey( window, GLFW_KEY_DOWN ) == GLFW_PRESS){
80 position -= direction * deltaTime * speed;
81 }
82 // Strafe right
83 if (glfwGetKey( window, GLFW_KEY_RIGHT ) == GLFW_PRESS){
84 position += right * deltaTime * speed;
85 }
86 // Strafe left
87 if (glfwGetKey( window, GLFW_KEY_LEFT ) == GLFW_PRESS){
88 position -= right * deltaTime * speed;
89 }
90
91 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.
92
93 // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
94 ProjectionMatrix = glm::perspective(FoV, 4.0f / 3.0f, 0.1f, 100.0f);
95 // Camera matrix
96 ViewMatrix = glm::lookAt(
97 position, // Camera is here
98 position+direction, // and looks here : at the same position, plus "direction"
99 up // Head is up (set to 0,-1,0 to look upside-down)
100 );
101
102 // For the next frame, the "last time" will be "now"
103 lastTime = currentTime;
104}
Note: See TracBrowser for help on using the repository browser.