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

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

Implement movement in a basic 3D scene and add some examples from the new OpenGL tutorials site

  • Property mode set to 100644
File size: 3.5 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(4,3,-3); //glm::vec3( 0, 0, 5 );
25// Initial horizontal angle : toward -Z
26float horizontalAngleBase = 3.14f * 3.0f / 2.0f; // 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;
34int centeredCount = 0;
35
36
37
38void computeMatricesFromInputs(){
39
40 // glfwGetTime is called only once, the first time this function is called
41 static double lastTime = glfwGetTime();
42
43 // Compute time difference between current and last frame
44 double currentTime = glfwGetTime();
45 float deltaTime = float(currentTime - lastTime);
46
47 // Get mouse position
48 double xpos, ypos;
49 glfwGetCursorPos(window, &xpos, &ypos);
50
51 // Stupid hack to set the cursor position correctly
52 // The call has no effect the first several times it's called
53 if (centeredCount < 100) {
54 glfwSetCursorPos(window, 1024/2, 768/2);
55 centeredCount++;
56 }
57
58 // Compute new orientation
59 /* STOP ROTATION FOR NOW */
60 float horizontalAngle = horizontalAngleBase + mouseSpeed * float(1024/2 - xpos );
61 // verticalAngle += mouseSpeed * float( 768/2 - ypos );
62
63 // Direction : Spherical coordinates to Cartesian coordinates conversion
64 glm::vec3 direction(
65 cos(verticalAngle) * sin(horizontalAngle),
66 sin(verticalAngle),
67 cos(verticalAngle) * cos(horizontalAngle)
68 );
69
70 // Right vector
71 glm::vec3 right = glm::vec3(
72 sin(horizontalAngle - 3.14f/2.0f),
73 0,
74 cos(horizontalAngle - 3.14f/2.0f)
75 );
76
77 // Up vector
78 // glm::vec3 up = glm::cross( right, direction );
79
80 // Move forward
81 if (glfwGetKey( window, GLFW_KEY_UP ) == GLFW_PRESS){
82 position += direction * deltaTime * speed;
83 }
84 // Move backward
85 if (glfwGetKey( window, GLFW_KEY_DOWN ) == GLFW_PRESS){
86 position -= direction * deltaTime * speed;
87 }
88 // Strafe right
89 if (glfwGetKey( window, GLFW_KEY_RIGHT ) == GLFW_PRESS){
90 position += right * deltaTime * speed;
91 }
92 // Strafe left
93 if (glfwGetKey( window, GLFW_KEY_LEFT ) == GLFW_PRESS){
94 position -= right * deltaTime * speed;
95 }
96
97 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.
98
99 // Projection matrix : 45� Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
100 ProjectionMatrix = glm::perspective(FoV, 4.0f / 3.0f, 0.1f, 100.0f);
101 // Camera matrix
102 ViewMatrix = glm::lookAt(
103 position, // Camera is here
104 //position+direction,
105 // and looks here : at the same position, plus "direction"
106 // position+glm::vec3(-4,0,0), // position+glm::vec3(-4,-3,3),
107 position+direction,
108 glm::vec3(0,1,0) //up // Head is up (set to 0,-1,0 to look upside-down)
109 );
110
111 // For the next frame, the "last time" will be "now"
112 lastTime = currentTime;
113}
Note: See TracBrowser for help on using the repository browser.