Skip to content

Commit f1bd4a2

Browse files
committed
Added Makefile
1 parent b5fef65 commit f1bd4a2

File tree

3 files changed

+113
-11
lines changed

3 files changed

+113
-11
lines changed

Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
all: start
2+
3+
start: main.o glad.o shader.o stb_image.o
4+
clang++ -o x64/main x64/main.o x64/glad.o x64/shader.o x64/stb_image.o -ldl -lglfw
5+
6+
main.o: src/main.cpp
7+
clang++ -c src/main.cpp -o x64/main.o
8+
9+
glad.o: src/glad.c
10+
clang++ -c src/glad.c -o x64/glad.o
11+
12+
shader.o: src/shader.cpp src/shader.h
13+
clang++ -c src/shader.cpp -o x64/shader.o
14+
15+
stb_image.o: src/stb_image.cpp src/stb_image.h
16+
clang++ -c src/stb_image.cpp -o x64/stb_image.o

a.out

-219 KB
Binary file not shown.

src/main.cpp

Lines changed: 97 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,41 @@
44
#include <glm/ext/matrix_clip_space.hpp>
55
#include <glm/ext/matrix_float4x4.hpp>
66
#include <glm/ext/matrix_transform.hpp>
7+
#include <glm/ext/quaternion_geometric.hpp>
78
#include <glm/ext/vector_float3.hpp>
89
#include <glm/fwd.hpp>
910
#include <glm/glm.hpp>
1011
#include <glm/gtc/matrix_transform.hpp>
1112
#include <glm/gtc/type_ptr.hpp>
1213

1314
#include <iostream>
15+
#include <math.h>
1416

1517
#include "stb_image.h"
1618
#include "shader.h"
1719

1820
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
1921
void processInput(GLFWwindow *window);
22+
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
23+
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
2024

2125
// settings
2226
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;
2442

2543
int main()
2644
{
@@ -46,6 +64,9 @@ int main()
4664
}
4765
glfwMakeContextCurrent(window);
4866
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
67+
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
68+
glfwSetCursorPosCallback(window, mouse_callback);
69+
glfwSetScrollCallback(window, scroll_callback);
4970

5071
// glad: load all OpenGL function pointers
5172
// ---------------------------------------
@@ -202,6 +223,12 @@ int main()
202223

203224
glEnable(GL_DEPTH_TEST);
204225

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+
205232
// render loop
206233
// -----------
207234
while (!glfwWindowShouldClose(window))
@@ -210,6 +237,10 @@ int main()
210237
// -----
211238
processInput(window);
212239

240+
float currentFrame = glfwGetTime();
241+
deltaTime = currentFrame - lastFrame;
242+
lastFrame = currentFrame;
243+
213244
// render
214245
// ------
215246
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
@@ -220,18 +251,18 @@ int main()
220251
glBindTexture(GL_TEXTURE_2D, texture1);
221252
glActiveTexture(GL_TEXTURE1);
222253
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+
230255
int modelLoc = glGetUniformLocation(ourShader.ID, "model");
231256

257+
glm::mat4 view;
258+
view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
259+
232260
int viewLoc = glGetUniformLocation(ourShader.ID, "view");
233261
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
234262

263+
glm::mat4 projection;
264+
projection = glm::perspective(glm::radians(fov), 4.0f / 3.0f, 0.1f, 100.0f);
265+
235266
int projectionLoc = glGetUniformLocation(ourShader.ID, "projection");
236267
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
237268

@@ -250,9 +281,6 @@ int main()
250281
glm::mat4 model = glm::mat4(1.0f);
251282
model = glm::translate(model, cubePositions[i]);
252283

253-
if (i % 3 == 0)
254-
model = glm::rotate(model, (float)glfwGetTime(), glm::vec3(0.0f, 1.0f, 1.0f));
255-
256284
float angle = 20.0f * i;
257285
model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
258286
glUniformMatrix4fv(glGetUniformLocation(ourShader.ID, "model"), 1, GL_FALSE, glm::value_ptr(model));
@@ -283,6 +311,20 @@ void processInput(GLFWwindow *window)
283311
{
284312
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
285313
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;
286328
}
287329
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
288330
// ---------------------------------------------------------------------------------------------
@@ -292,3 +334,47 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height)
292334
// height will be significantly larger than specified on retina displays.
293335
glViewport(0, 0, width, height);
294336
}
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

Comments
 (0)