-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
395 lines (273 loc) · 10.2 KB
/
Copy pathMain.cpp
File metadata and controls
395 lines (273 loc) · 10.2 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
#include<iostream>
#include<vector>
#include<glad/glad.h>
#include<GLFW/glfw3.h>
#include<stb/stb_image.h>
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "shaderClass.h"
#include "VAO.h"
#include "VBO.h"
#include "EBO.h"
#include "model.h"
#include "Object.h"
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
//OpenGL Course - Create 3D and 2D Graphics With C++
bool GUI = true;
float const height = 1200;
float const width = 1200;
// Camera class
Camera camera(glm::vec3(0.0f, 0.0f, -3.0f));
glm::vec3 objectColor(1.0f, 0.0f, 0.5f);
glm::vec3 lightPos(7.0f, 7.0f, 0.0f);
glm::vec3 lightCol(1.0f, 1.0f, 1.0f);
float lastX = height / 2.0f;
float lastY = width / 2.0f;
bool firstMouse = true;
bool captureMouse = true;
glm::vec3 mod2trans(0.0f, 0.0f, 0.0f);
// Key input polling loop, to be called in the main loop
void processInput(GLFWwindow* window, Camera& camera, float deltaTime) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.processKeyboard(deltaTime, GLFW_KEY_W);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.processKeyboard(deltaTime, GLFW_KEY_S);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.processKeyboard(deltaTime, GLFW_KEY_A);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.processKeyboard(deltaTime, GLFW_KEY_D);
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
camera.processKeyboard(deltaTime, GLFW_KEY_E);
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
camera.processKeyboard(deltaTime, GLFW_KEY_Q);
if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS) {
captureMouse = false;
firstMouse = true;
}
}
// Mouse callback function-pos
void mouseCallback(GLFWwindow* window, double xpos, double ypos) {
if (!captureMouse) {
return;
}
if (firstMouse) {
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top
lastX = xpos;
lastY = ypos;
camera.processMouseMovement(xoffset, yoffset);
}
// Mouse callback function-scroll
void mouseScrollCallback(GLFWwindow* window, double xoffset, double yoffset) {
// Zoom in when scrolling up (positive yoffset)
if (yoffset > 0) {
camera.fov -= 2.0f;
}
// Zoom out when scrolling down (negative yoffset)
else {
camera.fov += 2.0f;
}
// Clamp the FoV to a reasonable range
if (camera.fov < 1.0f) {
camera.fov = 1.0f;
}
if (camera.fov > 90.0f) {
camera.fov = 90.0f;
}
}
int main() {
//initialize glfw
glfwInit();
//handshake glfw with proper OpenGL version
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//Create window obj with 800x800 dimesnions
GLFWwindow* window = glfwCreateWindow(height, width, "OpenGL", NULL, NULL);
if (window == NULL) {
std::cout << "Failed to create GLFW Window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// Capture the cursor for first-person camera controls
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// Set the mouse callback function
glfwSetCursorPosCallback(window, mouseCallback);
glfwSetScrollCallback(window, mouseScrollCallback);
stbi_set_flip_vertically_on_load(true);
//load glad to config OpenGL
gladLoadGL();
glViewport(0, 0, height, width);
glEnable(GL_DEPTH_TEST);
if (GUI) {
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 330");
}
Shader shaderProgram("default.vert", "default.frag");
Shader lightShader("light.vert", "light.frag");
Shader modelShader("model.vert", "model.frag");
modelShader.Activate();
glm::mat4 model = glm::mat4(1.0f);
glm::mat4 projection = glm::mat4(1.0f);
float deltaTime = 0.0f;
float lastFrame = 0.0f;
// load models
// -----------
//Model ourModel("models/backpack/backpack.obj");
Model ourModel2("models/subaru_impreza.glb");
//Model ourModel("models/modern_luxury_wedding_arch_house_building_design.glb");
//Model ourModel("models/beautiful_city.glb");
Model ourModel("models/brutalist_interior.glb");
//Model ourModel("Aristotle.obj");
std::cout << "Model loaded with " << ourModel.meshes.size() << " meshes" << std::endl;
if (ourModel.meshes.empty()) {
std::cout << "ERROR: Failed to load model or model has no meshes!" << std::endl;
return -1;
}
std::cout << "=== MODEL DEBUG INFO ===" << std::endl;
std::cout << "Number of meshes: " << ourModel.meshes.size() << std::endl;
std::cout << "Number of textures loaded: " << ourModel.textures_loaded.size() << std::endl;
// Debug each mesh
for (size_t i = 0; i < ourModel.meshes.size(); ++i) {
auto& mesh = ourModel.meshes[i];
std::cout << "\nMesh " << i << ":" << std::endl;
std::cout << " Vertices: " << mesh.vertices.size() << std::endl;
std::cout << " Indices: " << mesh.indices.size() << std::endl;
std::cout << " Textures: " << mesh.textures.size() << std::endl;
// Debug textures for this mesh
for (size_t j = 0; j < mesh.textures.size(); ++j) {
auto& tex = mesh.textures[j];
std::cout << " Texture " << j << ": " << tex.type
<< " (ID: " << tex.id << ", Path: '" << tex.path << "')" << std::endl;
}
}
// Test if any textures were loaded at all
if (ourModel.textures_loaded.empty()) {
std::cout << "\nWARNING: No textures were loaded from GLB file!" << std::endl;
std::cout << "This could be normal for GLB files with embedded textures." << std::endl;
}
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
//ERROS HAPPENING HERE LIGHT IS UNDEFINED AND CUBES SAY INCOMPLETE TYPE NOT ALLOED
LightSrc lightSrc;
std::vector<Object> objs;
objs.emplace_back(Cube());
objs.emplace_back(Cube());
objs.emplace_back(Sphere());
glm::vec3 bkColor(0.9f, 0.9f, 0.9f);
while (!glfwWindowShouldClose(window)) {
// Calculate delta time
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
lightSrc.pos = lightPos;
if (!captureMouse) {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
// Process keyboard input
processInput(window, camera, deltaTime);
// Clear the screen'
glClearColor(bkColor.r, bkColor.g, bkColor.b, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
lightSrc.draw(lightShader, camera, lightPos, lightCol, (float) height / width);
for (Object& obj : objs) { // Use reference
obj.draw(shaderProgram, camera, lightPos, lightCol, (float)height / width);
}
//tcube.draw(shaderProgram, camera, lightPos, lightCol);
// Activate shader and set uniforms
modelShader.Activate();
glm::mat4 projection = glm::perspective(glm::radians(camera.fov), (float)width / (float)height, 0.1f, 100.0f);
glm::mat4 view = camera.getViewMatrix();
modelShader.setMat4("projection", projection);
modelShader.setMat4("view", view);
// Add lighting uniforms
modelShader.setVec3("lightPos", lightPos);
modelShader.setVec3("lightColor", lightCol); // White light
modelShader.setVec3("viewPos", camera.pos);
// render the loaded model
glm::mat4 model = glm::mat4(1.0f);
modelShader.setMat4("model", model);
ourModel.Draw(modelShader);
model = glm::mat4(1.0f);
model = glm::translate(model, ourModel2.pos);
model = glm::rotate(model, glm::radians(ourModel2.angle.x), glm::vec3(1.0f, 0.0f, 0.0f)); //x
model = glm::rotate(model, glm::radians(ourModel2.angle.y), glm::vec3(0.0f, 1.0f, 0.0f)); //y
model = glm::rotate(model, glm::radians(ourModel2.angle.z), glm::vec3(0.0f, 0.0f, 1.0f)); //z
model = glm::scale(model, glm::vec3(0.007f, 0.007f, 0.007f)); // Scale down by 100x
modelShader.setMat4("model", model);
ourModel2.Draw(modelShader);
if (GUI) {
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::Begin("Camera Debug", &GUI);
ImGui::Text("Camera Position: (%.2f, %.2f, %.2f)", camera.pos.x, camera.pos.y, camera.pos.z);
ImGui::Text("Camera Front: (%.2f, %.2f, %.2f)", camera.front.x, camera.front.y, camera.front.z);
ImGui::SliderFloat("FOV", &camera.fov, 1, 90);
if (ImGui::SliderFloat("Yaw", &camera.yaw, -180.0f, 180.0f)) {
camera.processGUIMovement(camera.yaw, 0);
}
if (ImGui::SliderFloat("Pitch", &camera.pitch, -89.0f, 89.0f)) {
camera.processGUIMovement(0, camera.pitch);
}
if (ImGui::Button("Capture Mouse"))
{
captureMouse = true;
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
}
ImGuiIO& io = ImGui::GetIO();
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
ImGui::End();
ImGui::Begin("Model", &GUI);
ImGui::DragFloat3("Model Position", &ourModel2.pos.x, 0.01f, -1000.0f, 1000.0f);
ImGui::DragFloat3("Rotation", &ourModel2.angle.x, 0.1f, -360.0f, 360.0f);
ImGui::End();
ImGui::Begin("Light Settings", &GUI);
ImGui::DragFloat3("Light Position", &lightPos.x, 0.1f, -100.0f, 100.0f);
ImGui::ColorPicker3("Light Color", &lightCol.r);
ImGui::ColorPicker3("Background Color", &bkColor.r);
ImGui::End();
for (int i = 0; i < objs.size(); ++i) {
Object& obj = objs[i];
std::string frame = "Object " + std::to_string(i + 1) + "##" + std::to_string(i);
ImGui::Begin(frame.c_str(), &GUI);
ImGui::DragFloat3("Position", &obj.pos.x, 0.1f, -1000.0f, 1000.0f);
ImGui::DragFloat("Scale", &obj.size, 0.1f, -0.01f, 1000.0f);
ImGui::DragFloat("Rotate", &obj.angle, 0.1f, -360.0f, 360.0f);
ImGui::ColorPicker3("Color", &obj.col.r);
ImGui::End();
}
// Render ImGui
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
glfwSwapBuffers(window);
glfwPollEvents();
}
//glDeleteTextures(1, &texture);
modelShader.Delete();
//
glfwDestroyWindow(window);
glfwTerminate();
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
return 0;
}