-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
262 lines (213 loc) · 7.72 KB
/
Copy pathmain.cpp
File metadata and controls
262 lines (213 loc) · 7.72 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
#if defined(_WIN32) && !defined(WIN32)
#define WIN32
#endif
#include "window/BaseFrame.h"
#include "window/BaseWindow.h"
#include "window/SwitchableBaseWindow.h"
#include "scene/SceneManager.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "dearimgui.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb.h"
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include "controller/Controller.h"
// BaseWindow includes
#include "window/SceneBuilder.h"
#include "window/SceneControl.h"
#include "window/AssetPanel.h"
#include "window/ConsolePanel.h"
#include "window/SceneWorld.h"
#include "window/MeshViewer.h"
// Add logger
#include "logger/Logger.h"
// Add shaders
#include "shader/ShaderFactory.h"
// Add time
#include "time/Time.h"
#ifndef SRC_DIR
#define SRC_DIR "./src"
#endif
int screenWidth = 1500;
int screenHeight = 800;
std::vector<std::shared_ptr<BaseFrame>> windows;
void mouse_scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
std::shared_ptr<Entity> entity = SceneBuilder::GetActiveEntity();
SceneBuilder::OnScroll(entity, yoffset);
MeshViewer::OnScroll(yoffset);
}
void notifyClickDownHandlers(int xpos, int ypos)
{
SceneBuilder::SetDownClick(xpos, ypos);
MeshViewer::SetDownClick(xpos, ypos);
}
void notifyClickReleaseHandlers()
{
SceneBuilder::SetReleaseClick();
MeshViewer::SetReleaseClick();
}
void notifyMousePositionHandlers(int x, int y)
{
SceneBuilder::SetDownClickPosition(x, y);
MeshViewer::SetDownClickPosition(x, y);
SceneWorld::SetMousePosition(x, y);
}
static void cursor_position_callback(GLFWwindow* window, double xpos, double ypos)
{
notifyMousePositionHandlers(xpos, ypos);
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
if (button == GLFW_MOUSE_BUTTON_LEFT)
{
if (action == GLFW_PRESS)
{
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
notifyClickDownHandlers(xpos, ypos);
}
else
{
notifyClickReleaseHandlers();
}
}
if (button == GLFW_MOUSE_BUTTON_RIGHT)
{
SceneWorld::SetLookActive(action == GLFW_PRESS || action == GLFW_REPEAT);
}
}
void window_size_callback(GLFWwindow* window, int width, int height)
{
screenWidth = width;
screenHeight = height;
windows[0]->Resize(0, 0, 250, screenHeight / 2);
windows[1]->Resize(0, 0, screenWidth, screenHeight);
windows[2]->Resize(screenWidth - 250, screenHeight / 4, 250, screenHeight / 2);
windows[3]->Resize(screenWidth - 250, 0, 250, screenHeight / 2);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
MeshViewer::KeyCallback(action, key);
SceneWorld::KeyCallback(action, key);
}
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
GLFWwindow* window = glfwCreateWindow(screenWidth, screenHeight, "KubeChaser", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, key_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetScrollCallback(window, mouse_scroll_callback);
glfwSetCursorPosCallback(window, cursor_position_callback);
glfwSetWindowSizeCallback(window, window_size_callback);
glfwSetScrollCallback(window, mouse_scroll_callback);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
stbi_set_flip_vertically_on_load(true);
const GLubyte* renderer = glGetString(GL_RENDERER);
const GLubyte* version = glGetString(GL_VERSION);
std::cout << "Renderer: " << renderer << std::endl;
std::cout << "OpenGL version supported " << version << std::endl;
// Initialize Logger
std::shared_ptr<Logger> logger = std::make_shared<Logger>();
BaseWindow::AttachLogger(logger);
// Initialize Shader Factory
std::shared_ptr<ShaderFactory> factory = std::make_shared<ShaderFactory>();
BaseWindow::AttachShaderFactory(factory);
// Initialize Time
std::shared_ptr<Time> time = std::make_shared<Time>();
BaseWindow::AttachTime(time);
std::shared_ptr<SceneControl> sceneControl = std::make_shared<SceneControl>(0, 0, 250, screenHeight / 2);
windows.push_back(sceneControl);
std::shared_ptr<SceneNode> root = std::make_shared<SceneNode>();
std::shared_ptr<Mesh> rootMesh = std::make_shared<Mesh>("statefulset.obj");
rootMesh->Translate(glm::vec3(0.0f, 1.0f, 0.0f));
root->AddStaticObject(rootMesh);
std::shared_ptr<Scene> scene = std::make_shared<Scene>(root);
std::shared_ptr<Camera> worldCamera = std::make_shared<Camera>(screenWidth, screenHeight);
std::shared_ptr<SceneWorld> sceneWorld = std::make_shared<SceneWorld>(0, 0, screenWidth, screenHeight);
sceneWorld->AddCamera(worldCamera);
std::shared_ptr<SceneManager> sceneManager = std::make_shared<SceneManager>();
sceneManager->AddScene(SceneCoord{0, 0, 0}, scene);
sceneManager->SetShowGrid(true, worldCamera);
sceneManager->SetActiveRadius(40);
sceneWorld->AddSceneManager(sceneManager);
// switch
std::shared_ptr<SwitchableBaseWindow> windowSwitch = std::make_shared<SwitchableBaseWindow>(0, 0, screenWidth, screenHeight);
windowSwitch->AddWindow(std::make_shared<MeshViewer>(0, 0, screenWidth, screenHeight));
windowSwitch->SetActiveMeshViewer(0);
windowSwitch->AddWindow(sceneWorld);
windowSwitch->AttachSceneController(sceneControl);
sceneControl->AttachSwitchableBaseWindow(windowSwitch);
windows.push_back(windowSwitch);
windows.push_back(std::make_shared<AssetPanel>(screenWidth - 250, screenHeight / 4, 250, screenHeight / 2));
windows.push_back(std::make_shared<ConsolePanel>(screenWidth - 250, screenHeight / 2, 250, screenHeight / 2));
// attach BaseWindows to controllers
// Controller controller(window);
// controller.AttachWindow(scenePlayer);
const char* glsl_version = "#version 330";
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
// Setup Dear ImGui style
//ImGui::StyleColorsDark();
ImGui::StyleColorsClassic();
// Setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init(glsl_version);
bool show_demo_window = true;
bool show_another_window = false;
ImVec4 clear_color = ImVec4(0.2f, 0.2f, 0.2f, 1.00f);
bool scenePlayerOn = false;
while (!glfwWindowShouldClose(window))
{
time->GetDelta();
glfwPollEvents();
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
// clear background
glViewport(0, 0, screenWidth, screenHeight);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// render windows
for (int i = 0; i < windows.size(); i++)
{
if (!windows[i]->Hidden())
{
windows[i]->Render();
}
}
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
}
// free memory
windows.clear();
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwTerminate();
return 0;
}