4
4
#include < glm/ext/matrix_clip_space.hpp>
5
5
#include < glm/ext/matrix_float4x4.hpp>
6
6
#include < glm/ext/matrix_transform.hpp>
7
+ #include < glm/ext/quaternion_geometric.hpp>
7
8
#include < glm/ext/vector_float3.hpp>
8
9
#include < glm/fwd.hpp>
9
10
#include < glm/glm.hpp>
10
11
#include < glm/gtc/matrix_transform.hpp>
11
12
#include < glm/gtc/type_ptr.hpp>
12
13
13
14
#include < iostream>
15
+ #include < math.h>
14
16
15
17
#include " stb_image.h"
16
18
#include " shader.h"
17
19
18
20
void framebuffer_size_callback (GLFWwindow* window, int width, int height);
19
21
void processInput (GLFWwindow *window);
22
+ void mouse_callback (GLFWwindow* window, double xpos, double ypos);
23
+ void scroll_callback (GLFWwindow* window, double xoffset, double yoffset);
20
24
21
25
// settings
22
26
const unsigned int SCR_WIDTH = 800 ;
23
- const unsigned int SCR_HEIGHT = 800 ;
27
+ const unsigned int SCR_HEIGHT = 600 ;
28
+
29
+ // global variables
30
+ glm::vec3 cameraPos = glm::vec3(0 .0f , 0 .0f , 3 .0f );
31
+ glm::vec3 cameraFront = glm::vec3(0 .0f , 0 .0f , -1 .0f );
32
+ glm::vec3 cameraUp = glm::vec3(0 .0f , 1 .0f , 0 .0f );
33
+ float lastX = (float )SCR_WIDTH / 2 ;
34
+ float lastY = (float )SCR_HEIGHT / 2 ;
35
+ float yaw = -90 .0f ;
36
+ float pitch = 0 .0f ;
37
+ float fov = 90 .0f ;
38
+ bool firstMouse = true ;
39
+
40
+ float deltaTime = 0 .0f ;
41
+ float lastFrame = 0 .0f ;
24
42
25
43
int main ()
26
44
{
@@ -46,6 +64,9 @@ int main()
46
64
}
47
65
glfwMakeContextCurrent (window);
48
66
glfwSetFramebufferSizeCallback (window, framebuffer_size_callback);
67
+ glfwSetInputMode (window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
68
+ glfwSetCursorPosCallback (window, mouse_callback);
69
+ glfwSetScrollCallback (window, scroll_callback);
49
70
50
71
// glad: load all OpenGL function pointers
51
72
// ---------------------------------------
@@ -202,6 +223,12 @@ int main()
202
223
203
224
glEnable (GL_DEPTH_TEST);
204
225
226
+ glm::vec3 cameraTarget = glm::vec3 (0 .0f , 0 .0f , 0 .0f );
227
+ glm::vec3 cameraDirection = glm::normalize (cameraPos - cameraTarget);
228
+
229
+ glm::vec3 up = glm::vec3 (0 .0f , 1 .0f , 0 .0f );
230
+ glm::vec3 cameraRight = glm::normalize (glm::cross (up, cameraDirection));
231
+
205
232
// render loop
206
233
// -----------
207
234
while (!glfwWindowShouldClose (window))
@@ -210,6 +237,10 @@ int main()
210
237
// -----
211
238
processInput (window);
212
239
240
+ float currentFrame = glfwGetTime ();
241
+ deltaTime = currentFrame - lastFrame;
242
+ lastFrame = currentFrame;
243
+
213
244
// render
214
245
// ------
215
246
glClearColor (0 .2f , 0 .3f , 0 .3f , 1 .0f );
@@ -220,18 +251,18 @@ int main()
220
251
glBindTexture (GL_TEXTURE_2D, texture1);
221
252
glActiveTexture (GL_TEXTURE1);
222
253
glBindTexture (GL_TEXTURE_2D, texture2);
223
-
224
- glm::mat4 view = glm::mat4 (1 .0f );
225
- view = glm::translate (view, glm::vec3 (0 .0f , -0 .5f , -3 .0f ));
226
-
227
- glm::mat4 projection;
228
- projection = glm::perspective (glm::radians (45 .0f ), 800 .0f / 800 .f , 0 .1f , 100 .0f );
229
-
254
+
230
255
int modelLoc = glGetUniformLocation (ourShader.ID , " model" );
231
256
257
+ glm::mat4 view;
258
+ view = glm::lookAt (cameraPos, cameraPos + cameraFront, cameraUp);
259
+
232
260
int viewLoc = glGetUniformLocation (ourShader.ID , " view" );
233
261
glUniformMatrix4fv (viewLoc, 1 , GL_FALSE, glm::value_ptr (view));
234
262
263
+ glm::mat4 projection;
264
+ projection = glm::perspective (glm::radians (fov), 4 .0f / 3 .0f , 0 .1f , 100 .0f );
265
+
235
266
int projectionLoc = glGetUniformLocation (ourShader.ID , " projection" );
236
267
glUniformMatrix4fv (projectionLoc, 1 , GL_FALSE, glm::value_ptr (projection));
237
268
@@ -250,9 +281,6 @@ int main()
250
281
glm::mat4 model = glm::mat4 (1 .0f );
251
282
model = glm::translate (model, cubePositions[i]);
252
283
253
- if (i % 3 == 0 )
254
- model = glm::rotate (model, (float )glfwGetTime (), glm::vec3 (0 .0f , 1 .0f , 1 .0f ));
255
-
256
284
float angle = 20 .0f * i;
257
285
model = glm::rotate (model, glm::radians (angle), glm::vec3 (1 .0f , 0 .3f , 0 .5f ));
258
286
glUniformMatrix4fv (glGetUniformLocation (ourShader.ID , " model" ), 1 , GL_FALSE, glm::value_ptr (model));
@@ -283,6 +311,20 @@ void processInput(GLFWwindow *window)
283
311
{
284
312
if (glfwGetKey (window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
285
313
glfwSetWindowShouldClose (window, true );
314
+
315
+ const float cameraSpeed = 0 .05f ;
316
+ if (glfwGetKey (window, GLFW_KEY_W) == GLFW_PRESS)
317
+ cameraPos += cameraSpeed * cameraFront;
318
+ if (glfwGetKey (window, GLFW_KEY_S) == GLFW_PRESS)
319
+ cameraPos -= cameraSpeed * cameraFront;
320
+ if (glfwGetKey (window, GLFW_KEY_A) == GLFW_PRESS)
321
+ cameraPos -= glm::normalize (glm::cross (cameraFront, cameraUp)) * cameraSpeed;
322
+ if (glfwGetKey (window, GLFW_KEY_D) == GLFW_PRESS)
323
+ cameraPos += glm::normalize (glm::cross (cameraFront, cameraUp)) * cameraSpeed;
324
+ if (glfwGetKey (window, GLFW_KEY_SPACE) == GLFW_PRESS)
325
+ cameraPos += cameraSpeed * cameraUp;
326
+ if (glfwGetKey (window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
327
+ cameraPos -= cameraSpeed * cameraUp;
286
328
}
287
329
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
288
330
// ---------------------------------------------------------------------------------------------
@@ -292,3 +334,47 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height)
292
334
// height will be significantly larger than specified on retina displays.
293
335
glViewport (0 , 0 , width, height);
294
336
}
337
+
338
+ void mouse_callback (GLFWwindow* window, double xpos, double ypos)
339
+ {
340
+ if (firstMouse)
341
+ {
342
+ lastX = xpos;
343
+ lastY = ypos;
344
+ firstMouse = false ;
345
+ }
346
+
347
+ float xOffset = xpos - lastX;
348
+ float yOffset = lastY - ypos;
349
+ lastX = xpos;
350
+ lastY = ypos;
351
+
352
+ const float sensitivity = 0 .15f ;
353
+ xOffset *= sensitivity;
354
+ yOffset *= sensitivity;
355
+
356
+ yaw += xOffset;
357
+ pitch += yOffset;
358
+ if (pitch < -89 .0f )
359
+ pitch = -89 .0f ;
360
+ if (pitch > 89 .0f )
361
+ pitch = 89 .0f ;
362
+
363
+ glm::vec3 direction;
364
+ direction.x = cos (glm::radians (yaw));
365
+ direction.y = sin (glm::radians (yaw));
366
+
367
+ direction.y = sin (glm::radians (pitch));
368
+ direction.x = cos (glm::radians (yaw)) * cos (glm::radians (pitch));
369
+ direction.z = sin (glm::radians (yaw)) * cos (glm::radians (pitch));
370
+ cameraFront = glm::normalize (direction);
371
+ }
372
+
373
+ void scroll_callback (GLFWwindow* window, double xoffset, double yoffset)
374
+ {
375
+ fov -= (float )yoffset;
376
+ if (fov < 1 .0f )
377
+ fov = 1 .0f ;
378
+ if (fov > 120 .0f )
379
+ fov = 120 .0f ;
380
+ }
0 commit comments