-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcube_shaded.cpp
More file actions
322 lines (283 loc) · 9.49 KB
/
cube_shaded.cpp
File metadata and controls
322 lines (283 loc) · 9.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/**
* @file cube_shaded.cpp
* @brief Rendering a cube with a point light using Blinn-Phong shading
* @date October 2023
*/
// preprocessor directives
// ==================================================
// OpenGL
#include <glad/glad.h>
#include <GLFW/glfw3.h>
// GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
// C++
#include <string>
// Custom
#include <opengl-sandbox/shader.h>
#include <opengl-sandbox/camera.h>
#include <opengl-sandbox/logger.h>
#include <opengl-sandbox/cube.h>
// function prototypes
// ==================================================
/**
* Initializes GLFW
*/
void initGLFW();
/**
* Creates a window object
* @return A pointer to the GLFW window
*/
GLFWwindow* initWindow();
/**
* Sets all window callback
* @param window - A pointer to a GLFW window
*/
void initCallbacks(GLFWwindow* window);
/**
* Initializes GLAD
*/
void initGLAD();
/**
* Stops the program
* @param code - An exit code
* @param message - A message detailing program termination
*/
void terminate(int exitCode, std::string message);
/**
* Prints program specifications to the logger
*/
void logSpecs();
/**
* Handles framebuffer resizing for a given window
* @param window - A pointer to a GLFW window
* @param width - The new framebuffer width
* @param height - The new framebuffer height
*/
void framebufferSizeCallback(GLFWwindow* window, int width, int height);
/**
* Processes per-frame time logic (change in time since previous frame)
*/
void processTime();
/**
* Processes keyboard input
* @param window - A pointer to a GLFW window
*/
void processKeyboardInput(GLFWwindow* window);
/**
* Processes mouse movement
* @param window - A pointer to a GLFW window
* @param posX - The mouse's position along the x-axis
* @param posY - The mouse's position along the y-axis
*/
void mouseMovementCallback(GLFWwindow* window, double posX, double posY);
/**
* Processes mouse scroll
* @param window - A pointer to a GLFW window
* @param offsetX - Amount in which the mouse is scrolled horizontally
* @param offsetY - Amount in which the mouse is scrolled vertically
*/
void mouseScrollCallback(GLFWwindow* window, double offsetX, double offsetY);
// settings
// ==================================================
// GLFW
const int OPENGL_VERSION_MAJOR = 3;
const int OPENGL_VERSION_MINOR = 3;
const char* WINDOW_NAME = "Cube";
GLsizei windowWidth = 800;
GLsizei windowHeight = 600;
float aspectRatio = static_cast<float>(windowWidth) / static_cast<float>(windowHeight);
// timing
float deltaTime = 0.0f;
float lastFrame = 0.0f;
// camera
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
bool resetKeyPressed = false;
bool firstMouse = true;
float lastX = windowWidth / 2.0f;
float lastY = windowHeight / 2.0f;
// logger
Logger logger(Logger::Output::CONSOLE);
// shader
const char* CUBE_VERT_SHADER = "src/cube_shaded/cube.vs";
const char* CUBE_FRAG_SHADER = "src/cube_shaded/cube.fs";
const char* LIGHT_VERT_SHADER = "src/cube_shaded/light.vs";
const char* LIGHT_FRAG_SHADER = "src/cube_shaded/light.fs";
// lighting
struct {
float scalar = 0.25f;
float movementSpeed = 4.0f;
glm::vec3 position = glm::vec3(2.0f);
glm::vec3 color = glm::vec3(1.0f);
float constant = 1.0f;
float linear = 0.09f;
float quadratic = 0.032f;
} light;
// function definitions
// ==================================================
int main(void) {
// initialize
initGLFW();
GLFWwindow* window = initWindow();
initCallbacks(window);
initGLAD();
logSpecs();
// configure OpenGL
glEnable(GL_MULTISAMPLE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
// create shader program objects
Shader cubeShader(CUBE_VERT_SHADER, CUBE_FRAG_SHADER, &logger);
Shader lightShader(LIGHT_VERT_SHADER, LIGHT_FRAG_SHADER, &logger);
// cube object
Cube cube;
// render loop
while (!glfwWindowShouldClose(window)) {
processTime();
processKeyboardInput(window);
// set color and clear buffer bits
glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// coordinate space transformations
const float FRUSTUM_NEAR = 0.01f;
const float FRUSTUM_FAR = 100.0f;
glm::mat4 model(1.0f);
glm::mat4 view = camera.getViewMatrix();
glm::mat4 projection = glm::perspective(camera.getFOV(), aspectRatio, FRUSTUM_NEAR, FRUSTUM_FAR);
// render light source
model = glm::translate(model, light.position);
model = glm::scale(model, glm::vec3(light.scalar));
lightShader.use();
lightShader.setVec3("lightColor", light.color);
lightShader.setMat4("model", model);
lightShader.setMat4("view", view);
lightShader.setMat4("projection", projection);
cube.draw(lightShader);
// render cube
model = glm::mat4(1.0f);
cubeShader.use();
cubeShader.setVec3("light.position", light.position);
cubeShader.setVec3("light.ambient", light.color);
cubeShader.setVec3("light.diffuse", light.color);
cubeShader.setVec3("light.specular", light.color);
cubeShader.setFloat("light.constant", light.constant);
cubeShader.setFloat("light.linear", light.linear);
cubeShader.setFloat("light.quadratic", light.quadratic);
cubeShader.setVec3("viewPos", camera.getPosition());
cubeShader.setMat4("model", model);
cubeShader.setMat4("view", view);
cubeShader.setMat4("projection", projection);
cube.draw(cubeShader);
glfwSwapBuffers(window);
glfwPollEvents();
}
logger.log("Program exited with status " + std::to_string(EXIT_SUCCESS));
glfwTerminate();
return EXIT_SUCCESS;
}
void initGLFW() {
if (!glfwInit())
terminate(EXIT_FAILURE, "Failed to initialize GLFW");
}
GLFWwindow* initWindow() {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, OPENGL_VERSION_MAJOR);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, OPENGL_VERSION_MINOR);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_SAMPLES, 4);
GLFWwindow* window = glfwCreateWindow(windowWidth, windowHeight, WINDOW_NAME, nullptr, nullptr);
if (!window)
terminate(EXIT_FAILURE, "Failed to create GLFW window");
glfwMakeContextCurrent(window);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
return window;
}
void initCallbacks(GLFWwindow* window) {
glfwSetFramebufferSizeCallback(window, framebufferSizeCallback);
glfwSetCursorPosCallback(window, mouseMovementCallback);
glfwSetScrollCallback(window, mouseScrollCallback);
}
void initGLAD() {
if (!gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress)))
terminate(EXIT_FAILURE, "Failed to initialize GLAD");
}
void terminate(int code, std::string message) {
logger.log(message);
logger.log("Program exited with status " + std::to_string(code));
glfwTerminate();
exit(code);
}
void logSpecs() {
logger.log("GL vendor:", false, ' ');
logger.log(glGetString(GL_VENDOR), false);
logger.log("GL renderer:", false, ' ');
logger.log(glGetString(GL_RENDERER), false);
logger.log("GL version:", false, ' ');
logger.log(glGetString(GL_VERSION), false);
logger.log("GLSL version:", false, ' ');
logger.log(glGetString(GL_SHADING_LANGUAGE_VERSION), false);
logger.log("", false);
}
void framebufferSizeCallback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
windowWidth = width;
windowHeight = height;
aspectRatio = static_cast<float>(windowWidth) / static_cast<float>(windowHeight);
}
void processTime() {
float currentFrame = static_cast<float>(glfwGetTime());
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
}
void processKeyboardInput(GLFWwindow* window) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.move(Camera::Movement::FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.move(Camera::Movement::BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.move(Camera::Movement::LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.move(Camera::Movement::RIGHT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.move(Camera::Movement::RIGHT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS && !resetKeyPressed) {
camera.reset();
resetKeyPressed = true;
logger.log("Camera reset to default orientation");
}
if (glfwGetKey(window, GLFW_KEY_R) == GLFW_RELEASE)
resetKeyPressed = false;
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS)
light.position.z -= light.movementSpeed * deltaTime;
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS)
light.position.z += light.movementSpeed * deltaTime;
if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS)
light.position.x -= light.movementSpeed * deltaTime;
if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS)
light.position.x += light.movementSpeed * deltaTime;
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
light.position.y += light.movementSpeed * deltaTime;
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
light.position.y -= light.movementSpeed * deltaTime;
}
void mouseMovementCallback(GLFWwindow* window, double posX, double posY) {
float positionX = static_cast<float>(posX);
float positionY = static_cast<float>(posY);
if (firstMouse) {
lastX = positionX;
lastY = positionY;
firstMouse = false;
}
float offsetX = positionX - lastX;
float offsetY = lastY - positionY; // reversed since y-coordinates range from top to bottom
lastX = positionX;
lastY = positionY;
camera.look(offsetX, offsetY);
}
void mouseScrollCallback(GLFWwindow* window, double offsetX, double offsetY) {
camera.adjustFOV(static_cast<float>(offsetY / 10.0f));
}