-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditorMain.cpp
More file actions
401 lines (330 loc) · 10.9 KB
/
EditorMain.cpp
File metadata and controls
401 lines (330 loc) · 10.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
#include <iostream>
#include <chrono>
#include <vector>
#include <string>
#include <cstring>
#include <float.h>
#include <filesystem>
#include <algorithm>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <GL/gl.h>
#include <stb/stb_image.h>
#include "imgui/imgui.h"
#include "imgui/imgui_impl_glfw.h"
#include "imgui/imgui_impl_opengl2.h"
#include "Texture.h"
#include "BoxCollider.h"
#include "shaderClass.h"
#include "VBO.h"
#include "EBO.h"
#include "EditorCamera.h"
#include "SoundManager.h"
#include "Model.h"
#include "modelLoader.h"
const unsigned int width = 1000;
const unsigned int height = 800;
EditorCamera* globalCamera = nullptr;
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
if (globalCamera != nullptr) {
globalCamera->width = width;
globalCamera->height = height;
globalCamera->updateMatrix(90.0f, 0.1f, 100.0f);
}
}
//actual Game
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
GLFWwindow* window = glfwCreateWindow(width, height, "Editor", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window\n";
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
gladLoadGL();
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL2_Init();
glViewport(0, 0, width, height);
Shader shaderProgram("Shaders/Default.vert", "Shaders/Default.frag");
glm::vec4 lightColor = glm::vec4(0.95f, 0.95f, 0.9f, 1.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
EditorCamera camera(width, height, glm::vec3(0.0f, 0.0f, 2.0f));
globalCamera = &camera;
ModelLoader modelLoader;
std::string currentXML = "prefabRooms/example.xml";
if (!modelLoader.loadFromXML(currentXML)) {
std::cerr << "Failed to load models from XML file" << std::endl;
} else {
std::cout << "Successfully loaded " << modelLoader.getModelCount() << " models from XML" << std::endl;
}
bool showOpenScene = false;
bool showOpenRuntime = false;
std::vector<std::string> xmlFiles;
std::string path = std::filesystem::current_path().string() + "/prefabRooms";
int selectedModelIndex = -1;
//find all prefab rooms
for (const auto& entry : std::filesystem::directory_iterator(path))
{
if (entry.is_regular_file() && entry.path().extension() == ".xml")
{
std::string file = "prefabRooms/" + entry.path().filename().string();
xmlFiles.push_back(file);
}
}
//delta time stuff
float print_time = 0;
auto lastTick = std::chrono::system_clock::now();
float deltaTime = 0;
float fps = 0;
//this is fps that is going to be displayed so it doesn't appear off the screen
float displayedFPS = 0;
float moving_Test = 0;
std::cout << "\033[2J\033[1;1H"; // Clear screen & move cursor to top-left
while (!glfwWindowShouldClose(window))
{
moving_Test += deltaTime / 2;
auto now = std::chrono::system_clock::now();
std::chrono::duration<float> elapsed = now - lastTick;
deltaTime = elapsed.count();
lastTick = now;
print_time += deltaTime;
//----FPS--//
//fps display logic is in Info
fps = 1.0f / deltaTime;
if (print_time >= 1.0f) {
displayedFPS = fps;
print_time = 0.0f;
}
//----FPS--//
glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ImGui_ImplOpenGL2_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
if (ImGui::BeginMainMenuBar()) {
if (ImGui::BeginMenu("File")) {
if (ImGui::MenuItem("Open Scene")) {
showOpenScene = true;
}
if (ImGui::MenuItem("Save Scene")) {
modelLoader.saveToXML(currentXML);
}
if (ImGui::MenuItem("Update Runtime")) {
showOpenRuntime = true;
}
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
}
ImGui::SetNextWindowSize(ImVec2(1200, 800), ImGuiCond_Once);
ImGui::SetNextWindowSizeConstraints(ImVec2(200, 200), ImVec2(FLT_MAX, FLT_MAX));
ImGui::SetNextWindowCollapsed(false, ImGuiCond_Once);
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
ImGui::Begin("Viewport");
ImGui::PopStyleVar();
ImVec2 viewportSize = ImGui::GetContentRegionAvail();
ImVec2 viewportPos = ImGui::GetWindowPos();
ImVec2 contentMin = ImGui::GetWindowContentRegionMin();
bool viewportHovered = ImGui::IsWindowHovered();
ImGuiIO& io = ImGui::GetIO();
bool isDragging = io.MouseDown[0] && ImGui::IsAnyItemActive();
int fbWidth, fbHeight;
glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
int viewportX = (int)(viewportPos.x + contentMin.x);
int viewportY = fbHeight - (int)(viewportPos.y + contentMin.y + viewportSize.y);
int viewportW = (int)viewportSize.x;
int viewportH = (int)viewportSize.y;
if (viewportW < 1) viewportW = 1;
if (viewportH < 1) viewportH = 1;
ImGui::End();
ImGui::Begin("Hierarchy");
if (ImGui::Button("Add Model")) {
std::string modelName = "Model";
if(modelLoader.alreadyExist(modelName))
{
unsigned int modelInc = 1;
while(modelLoader.alreadyExist(modelName + std::to_string(modelInc)))
{
modelInc += 1;
}
modelLoader.addModel(modelName + std::to_string(modelInc));
}
else
{
modelLoader.addModel(modelName);
}
selectedModelIndex = modelLoader.getModelCount() - 1;
}
for (int i = 0; i < modelLoader.getModelCount(); i++) {
const ModelData* modelData = modelLoader.getModelData(i);
if (modelData != nullptr) {
std::string displayName;
if (modelData->name.empty()) {
displayName = modelData->path;
} else {
displayName = modelData->name;
}
bool isSelected = (selectedModelIndex == i);
if (ImGui::Selectable(displayName.c_str(), isSelected)) {
selectedModelIndex = i;
}
}
}
ImGui::End();
ImGui::Begin("Properties");
if (selectedModelIndex >= 0) {
ModelData* modelData = modelLoader.getModelData(selectedModelIndex);
if (modelData != nullptr) {
char nameBuffer[256];
for (int i = 0; i < 256; i++) {
nameBuffer[i] = 0;
}
for (int i = 0; i < modelData->name.length() && i < 255; i++) {
nameBuffer[i] = modelData->name[i];
}
if (ImGui::InputText("Name", nameBuffer, 256)) {
modelData->name = nameBuffer;
}
char pathBuffer[512];
for (int i = 0; i < 512; i++) {
pathBuffer[i] = 0;
}
for (int i = 0; i < modelData->path.length() && i < 511; i++) {
pathBuffer[i] = modelData->path[i];
}
if (ImGui::InputText("Path", pathBuffer, 512)) {
modelData->path = pathBuffer;
modelLoader.loadModelForIndex(selectedModelIndex);
}
float posX = modelData->position.x;
float posY = modelData->position.y;
float posZ = modelData->position.z;
float posArray[3] = {posX, posY, posZ};
if (ImGui::InputFloat3("Position", posArray)) {
modelData->position.x = posArray[0];
modelData->position.y = posArray[1];
modelData->position.z = posArray[2];
}
float rotX = modelData->rotation.x;
float rotY = modelData->rotation.y;
float rotZ = modelData->rotation.z;
float rotArray[3] = {rotX, rotY, rotZ};
if (ImGui::InputFloat3("Rotation", rotArray)) {
modelData->rotation.x = rotArray[0];
modelData->rotation.y = rotArray[1];
modelData->rotation.z = rotArray[2];
}
float scaleX = modelData->scale.x;
float scaleY = modelData->scale.y;
float scaleZ = modelData->scale.z;
float scaleArray[3] = {scaleX, scaleY, scaleZ};
if (ImGui::InputFloat3("Scale", scaleArray)) {
modelData->scale.x = scaleArray[0];
modelData->scale.y = scaleArray[1];
modelData->scale.z = scaleArray[2];
}
}
}
ImGui::End();
if (showOpenScene) {
ImGui::Begin("Open Scene", &showOpenScene);
for (size_t i = 0; i < xmlFiles.size(); i++) {
if (ImGui::Selectable(xmlFiles[i].c_str())) {
currentXML = xmlFiles[i];
modelLoader.loadFromXML(currentXML);
selectedModelIndex = -1;
showOpenScene = false;
}
}
ImGui::End();
}
if (showOpenRuntime) {
ImGui::Begin("Change Runtime Scene", &showOpenRuntime);
for (size_t i = 0; i < xmlFiles.size(); i++) {
if (ImGui::Selectable(xmlFiles[i].c_str())) {
selectedModelIndex = -1;
showOpenRuntime = false;
std::ofstream newRuntime("RUNTIME_STARTUP_CONFIG");
if(newRuntime.is_open()) {
newRuntime << xmlFiles[i].c_str();
} else {
std::cout << "Error unable to create runtime config";
}
}
}
ImGui::End();
}
ImGui::Begin("Info");
ImGui::Text("FPS: %f", displayedFPS);
ImGui::End();
ImGui::Render();
ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
if (viewportW > 0 && viewportH > 0) {
GLint oldViewport[4];
GLboolean oldScissorEnabled;
GLint oldScissorBox[4];
glGetIntegerv(GL_VIEWPORT, oldViewport);
glGetBooleanv(GL_SCISSOR_TEST, &oldScissorEnabled);
if (oldScissorEnabled) {
glGetIntegerv(GL_SCISSOR_BOX, oldScissorBox);
}
glViewport(viewportX, viewportY, viewportW, viewportH);
glEnable(GL_SCISSOR_TEST);
glScissor(viewportX, viewportY, viewportW, viewportH);
camera.width = viewportW;
camera.height = viewportH;
bool mouseOverOtherUI = io.WantCaptureMouse && !viewportHovered;
if (!mouseOverOtherUI && !isDragging) {
camera.Inputs(window, deltaTime);
}
camera.updateMatrix(90.0f, 0.1f, 100.0f);
glEnable(GL_DEPTH_TEST);
glClearColor(0.549f, 0.671f, 0.631f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shaderProgram.Activate();
camera.Matrix(shaderProgram, "camMatrix");
glm::mat4 defaultModel = glm::mat4(1.0f);
glUniformMatrix4fv(glGetUniformLocation(shaderProgram.ID, "model"), 1, GL_FALSE, glm::value_ptr(defaultModel));
glm::mat3 normalMatrix = glm::transpose(glm::inverse(glm::mat3(defaultModel)));
glUniformMatrix3fv(glGetUniformLocation(shaderProgram.ID, "normalMatrix"), 1, GL_FALSE, glm::value_ptr(normalMatrix));
glUniform4f(glGetUniformLocation(shaderProgram.ID, "lightColor"), lightColor.x, lightColor.y, lightColor.z, lightColor.w);
glUniform3f(glGetUniformLocation(shaderProgram.ID, "lightPos[0]"), 0.0f, 0.2f, 0.2f);
glUniform3f(glGetUniformLocation(shaderProgram.ID, "lightPos[1]"), moving_Test, 0.2f, 0.2f);
modelLoader.drawModels(shaderProgram);
glUseProgram(0);
if (!oldScissorEnabled) {
glDisable(GL_SCISSOR_TEST);
} else {
glScissor(oldScissorBox[0], oldScissorBox[1], oldScissorBox[2], oldScissorBox[3]);
}
glViewport(oldViewport[0], oldViewport[1], oldViewport[2], oldViewport[3]);
}
glfwSwapBuffers(window);
glfwPollEvents();
}
ImGui_ImplOpenGL2_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
globalCamera = nullptr;
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}