-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
459 lines (397 loc) · 15.9 KB
/
main.cpp
File metadata and controls
459 lines (397 loc) · 15.9 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#include <array>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <camera/camera.hpp>
#include <cmath>
#include <cstddef>
#include <cstdio>
#include <glad/glad.h>
#include <glm/ext/matrix_transform.hpp>
#include <glm/fwd.hpp>
#include <glm/geometric.hpp>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/trigonometric.hpp>
#include <iostream>
#include <memory>
#include <shader/shader.hpp>
#include <string>
#define STB_IMAGE_IMPLEMENTATION
#include <Lights.cpp>
#include <Model/Model.hpp>
#include <format>
#include <imgui/backends/imgui_impl_glfw.h>
#include <imgui/backends/imgui_impl_opengl3.h>
#include <imgui/imgui.h>
#include <stb_image/stb_image.h>
using std::cout;
using std::endl;
using std::string;
// Globals
#ifndef GLOBALS
#define GLOBALS
float f32_zero = -10.0f;
float f32_one = 10.0f;
bool show_demo_window = true;
int WIDTH = 1000;
int HEIGHT = 1000;
const char *glsl_version = "#version 150";
float fov = 45.0;
float lastTime = 0.0f;
float deltaTime = 1.0f;
bool firstMouse = true;
glm::vec3 lightPos(1.2f, 1.0f, 2.0f);
uint32_t uvTexture;
bool enableControl = true;
bool displayCursor = false;
bool displayingCursor = false;
extern std::unique_ptr<Camera> activeCamera;
enum ShaderType {
vertexShader = GL_VERTEX_SHADER,
fragShader = GL_FRAGMENT_SHADER,
};
glm::vec3 direction;
#endif // !GLOBALS
// Function defintions
glm::vec3 cubePositions[] = {
glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(2.0f, 5.0f, -15.0f),
glm::vec3(-1.5f, -2.2f, -2.5f), glm::vec3(-3.8f, -2.0f, -12.3f),
glm::vec3(2.4f, -0.4f, -3.5f), glm::vec3(-1.7f, 3.0f, -7.5f),
glm::vec3(1.3f, -2.0f, -2.5f), glm::vec3(1.5f, 2.0f, -2.5f),
glm::vec3(1.5f, 0.2f, -1.5f), glm::vec3(-1.3f, 1.0f, -1.5f)};
std::vector<float> pointLightPositions[] = {
std::vector<float>{0.7f, 0.2f, 2.0f},
std::vector<float>{2.3f, -3.3f, -4.0f},
std::vector<float>{-4.0f, 2.0f, -12.0f},
std::vector<float>{0.0f, 0.0f, -3.0f}};
void frameBufferSizeCallback(GLFWwindow *window, int width, int height);
void processInput(GLFWwindow *window);
void makeCube(unsigned int *vao);
GLFWwindow *setupWindow();
unsigned int setupShader(GLenum shaderType, const char *shaderSource);
unsigned int setupShaderProgram(unsigned int vertexShader,
unsigned int fragmentShader);
bool getShaderSource(std::string *vertexShaderSource, const char *filename);
unsigned int loadAndSetupImage(const char *imageName, bool containsAlpha);
void makeCube(unsigned int *vao);
Shader *outlineShader;
int main() {
// setup window
GLFWwindow *window = setupWindow();
if (!window) {
return 1;
}
activeCamera = std::make_unique<Camera>(window);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// setup shaders
Shader simpleShader("vertexShader.glsl", "whiteFrag.glsl");
Shader lightingShader("vertexShader.glsl", "simpleLight.glsl");
Shader textureShader("vertexShader.glsl", "textureShader.glsl");
Shader oShader("outlineVertex.glsl", "whiteFrag.glsl");
outlineShader = &oShader;
Camera::InitCallbacks(window);
cout << "Before UV Load: " << uvTexture << " ";
uvTexture = loadAndSetupImage("textures/uvmap.png", false);
cout << "=> " << uvTexture << endl;
// Imgui Context init:
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO &io = ImGui::GetIO();
(void)io;
io.ConfigFlags |=
ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |=
ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
io.ConfigFlags &=
!ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport /
// Setup Platform/Renderer backends
ImGui_ImplGlfw_InitForOpenGL(window, true);
#ifdef __EMSCRIPTEN__
ImGui_ImplGlfw_InstallEmscriptenCallbacks(window, "#canvas");
#endif
ImGui_ImplOpenGL3_Init(glsl_version);
// RenderLoop:
bool pointLightsOn = true;
bool prevPointLightsOn = false;
Model backpackModel("models/Sponza/Sponza.obj");
backpackModel.scale = {0.02, 0.02, 0.02};
backpackModel.shader = &lightingShader;
float background[3] = {0.2f, 0.2f, 0.2f};
// ambient, diffuse, specular direction
DirectionalLight dirLight{
{-0.2f, -1.0f, -0.3f},
{0.2f, 0.2f, 0.2f},
{0.5f, 0.5f, 0.5f},
{1.0f, 1.0f, 1.0f},
false,
};
dirLight.UpdateShaderValues(&lightingShader);
backpackModel.UpdateShaderTransforms(activeCamera.get());
lightingShader.setFloat("material.shininess", 32.0f);
glEnable(GL_DEPTH_TEST);
uint32_t tinyCubeVAO;
makeCube(&tinyCubeVAO);
uint32_t referenceCube;
makeCube(&referenceCube);
PointLight pointLights[4];
for (int i = 0; i < 4; i++) {
// ambient, diffuse, specular, position, constant, linear, quadratic, count,
// isStatic
vector<float> p = pointLightPositions[i];
pointLights[i] = PointLight{{0.2f, 0.2f, 0.2f},
{0.5f, 0.5f, 0.5f},
{1.0f, 1.0f, 1.0f},
{p[0], p[1], p[2]},
1.0f,
0.09f,
0.032f,
i,
false};
pointLights[i].UpdateShaderValues(&lightingShader);
}
lightingShader.setInt("usePointLight", 1);
while (!glfwWindowShouldClose(window)) {
// Handle any polled user inputs:
processInput(window);
// Imgui Init for while loop
{
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
if (show_demo_window)
ImGui::ShowDemoWindow(&show_demo_window);
}
bool showWidnow = true;
activeCamera->DisplayCameraProperties();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glClearColor(background[0], background[1], background[2], 1);
dirLight.DisplayWindow();
backpackModel.DisplayWindow();
// Update time and delta time
float timeValue = glfwGetTime();
deltaTime = timeValue - lastTime;
lastTime = timeValue;
// Update camera matrices
activeCamera->UpdateCamera();
std::vector<float> cameraPos = {activeCamera->cameraPos.x,
activeCamera->cameraPos.y,
activeCamera->cameraPos.z};
dirLight.UpdateShaderValues(&lightingShader);
backpackModel.UpdateShaderTransforms(activeCamera.get());
backpackModel.Draw(lightingShader, activeCamera.get());
glm::mat4 genericModel = glm::mat4(1.0f);
// // Reference Cube:
// glBindVertexArray(referenceCube);
// textureShader.use();
// textureShader.setMatrix("view", activeCamera->view);
// textureShader.setMatrix("projection", activeCamera->projection);
// glm::mat4 upOffset = glm::translate(genericModel, glm::vec3(0, 0.5, 0));
// textureShader.setMatrix("model", upOffset);
// glDrawArrays(GL_TRIANGLES, 0, 36);
// Draw the white cube : Update camera information, and model matrix
simpleShader.use();
glBindVertexArray(tinyCubeVAO);
simpleShader.setMatrix("view", activeCamera->view);
simpleShader.setMatrix("projection", activeCamera->projection);
for (int i = 0; i < 4; i++) {
glm::mat4 modelLight =
glm::translate(genericModel, glm::vec3(pointLightPositions[i][0],
pointLightPositions[i][1],
pointLightPositions[i][2]));
modelLight = glm::scale(modelLight, glm::vec3(0.2f));
simpleShader.setMatrix("model", modelLight);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
// Render Imgui window
// Swap color buffer
// poll events
{
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
// Update and Render additional Platform Windows
// (Platform functions may change the current OpenGL context, so we
// The way I understand it is if the window is weird, this will handle it
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) {
GLFWwindow *backup_current_context = glfwGetCurrentContext();
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
glfwMakeContextCurrent(backup_current_context);
}
// Swap the color buffer with the current buffer being displayed.
glfwSwapBuffers(window);
glfwPollEvents();
}
}
// Imgui cleanup
// glfw cleanup
{
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwTerminate();
}
return 0;
}
// This function loads the image using the stb library, and binds it to a newly
// created texture object. Then, it uses GL to generate mipmap after setting up
// the GL-parameters
unsigned int loadAndSetupImage(const char *imageName, bool containsAlpha) {
unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
int width, height, nrChannel;
// nr channel: color channel length
// 0: R Channel I think?
stbi_set_flip_vertically_on_load(true);
unsigned char *data = stbi_load(imageName, &width, &height, &nrChannel, 0);
if (!data) {
std::cout << "Error loading up the image!";
return 0;
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
containsAlpha ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
// set the texture wrapping/filtering options (on the currently bound texture
// object)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
stbi_image_free(data);
return texture;
}
GLFWwindow *setupWindow() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *window =
glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL) {
std::cout << "Failed to create GLFW window\n";
glfwTerminate();
return NULL;
}
glfwMakeContextCurrent(window);
// Initialize GLAD to allow it to manage Os specific functions that implement
// the OpenGL specifications
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize GLAD!\n";
glfwTerminate();
return NULL;
}
/**
* Set the size of the renderring window so OpenGL knows what area within our
* sized window it should render in. For our case, we render in all of it.
*/
int fwidth, fheight;
glfwGetFramebufferSize(window, &fwidth, &fheight);
glViewport(0, 0, fwidth, fheight);
glfwSetFramebufferSizeCallback(window, frameBufferSizeCallback);
return window;
}
// Handles keyboard input each frame. Keys 7/8 toggle cursor visibility,
// keys 9/0 toggle camera control, ESC closes the window.
void processInput(GLFWwindow *window) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, 1);
if (!enableControl && glfwGetKey(window, GLFW_KEY_0) == GLFW_PRESS)
enableControl = true;
if (enableControl && glfwGetKey(window, GLFW_KEY_9) == GLFW_PRESS)
enableControl = false;
if (!displayCursor && glfwGetKey(window, GLFW_KEY_8) == GLFW_PRESS)
displayCursor = true;
if (displayCursor && glfwGetKey(window, GLFW_KEY_7) == GLFW_PRESS)
displayCursor = false;
if (displayCursor != displayingCursor) {
glfwSetInputMode(window, GLFW_CURSOR,
(displayCursor) ? GLFW_CURSOR_NORMAL
: GLFW_CURSOR_DISABLED);
displayingCursor = displayCursor;
}
if (!enableControl)
return;
// make our camera process the rest of the inputs
activeCamera->KeyInput(window);
}
void frameBufferSizeCallback(GLFWwindow *window, int width, int height) {
int fwidth, fheight;
glfwGetFramebufferSize(window, &fwidth, &fheight);
glViewport(0, 0, fwidth, fheight);
WIDTH = fwidth;
HEIGHT = fheight;
}
void makeCube(unsigned int *vao) {
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
float vertices[] = {
// positions // normals // texture coords
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.5f, -0.5f,
-0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, 0.5f, 0.5f, -0.5f, 0.0f,
0.0f, -1.0f, 1.0f, 1.0f, 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
1.0f, 1.0f, -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0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 0.0f,
1.0f, 0.0f, -0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.5f, 0.5f,
-0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.5f, 0.5f, 0.5f, 0.0f,
1.0f, 0.0f, 1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
1.0f, 0.0f, -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f};
unsigned VBO;
glGenVertexArrays(1, vao);
glBindVertexArray(*vao);
// Generate a buffer and assign the ID to VBO: Vertex Buffer Object.
glGenBuffers(1, &VBO);
// Bind the GL_ARRAY_BUFFER with the VBO Buffer.
glBindBuffer(GL_ARRAY_BUFFER, VBO);
// Copies vertex data to buffer we have just bound
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Setup the vertex objects for our VBOs
// first_arg: Position we are assigning. We set it to 0 since we are assigning
// the position attribute which is the 0th attribute. second_arg: How many
// components per vertex attribute.
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0);
glEnableVertexAttribArray(0);
// Similarly, set the attributes of the second index, which are the colors,
// and enable them.
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
(void *)(3 * sizeof(float)));
// Lastly, set the attributes of the third index, which are the texture
// coordinates.
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
(void *)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
}
/*
Model lifecycle:
- Model meshes and textures configured
- Set Shader
- For each loop iteration:
- Use()
- Set transforms for the shader (Internal)
- Set camera information (Send camera) for the shader
- Set Directional light information (Send lighting information through main)
- Draw()
*/