-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGLFWApp.cpp
532 lines (440 loc) · 13.2 KB
/
GLFWApp.cpp
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#include "GLFWApp.h"
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <glm/gtc/matrix_transform.hpp>
#include "imgui/imgui_impl_glfw_gl3.h"
#ifdef _WIN32
#undef APIENTRY
#define GLFW_EXPOSE_NATIVE_WIN32
#define GLFW_EXPOSE_NATIVE_WGL
#include <GLFW/glfw3native.h>
#include <chrono>
#endif
#include "GameObject.h"
#include "Plane.h"
void Error_callback(int error, const char* description);
void Key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
void Mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
void Mouse_cursor_callback(GLFWwindow* window, double xpos, double ypos);
void Mouse_scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void Frame_Status_GUI();
void Object_Viewer_GUI();
void Mouse_Position_GUI();
GLFWApp* GLFWApp::appInstance;
GLFWApp::GLFWApp() :
m_previousTime(0),
m_currentTime(0),
m_app_status(true),
m_resource_manager(nullptr),
m_gui_manager(nullptr),
m_mainCamera(nullptr),
m_renderer(nullptr)
{
}
GLFWApp::~GLFWApp()
{
m_resource_manager.reset();
m_mainCamera.reset();
m_renderer.reset();
}
bool GLFWApp::Initialize(int width , int height , const std::string &title)
{
const float f_width = static_cast<float>(width);
const float f_height = static_cast<float>(height);
srand(time(NULL));
if (!glfwInit())
{
return false;
}
glfwSetErrorCallback(Error_callback);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_SAMPLES, 4);
/* Setting GLFW window */
m_window = glfwCreateWindow(width, height, title.c_str() , NULL , NULL);
if (!m_window)
{
std::cout << "Window Creation Failed!" << std::endl;
return false;
}
glfwSetWindowPos(m_window, 100, 100);
glfwMakeContextCurrent(m_window);
glfwSetKeyCallback(m_window, Key_callback);
glfwSetMouseButtonCallback(m_window, Mouse_button_callback);
glfwSetCursorPosCallback(m_window, Mouse_cursor_callback);
glfwSetScrollCallback(m_window, Mouse_scroll_callback);
glfwSwapInterval(0);
// Initialize glew
glewExperimental = true;
GLenum status = glewInit();
if (status != GLEW_OK)
{
std::cout << "GLEW INIT Failed! " << std::endl;
return false;
}
glEnable(GL_DEPTH_TEST);
glEnable(GL_MULTISAMPLE);
glEnable(GL_BLEND);
glEnable(GL_PROGRAM_POINT_SIZE);
glEnable(GL_POINT_SPRITE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
/* ResourceManager Creation */
m_resource_manager = std::make_shared<ResourceManager>();
// Initialize asset importer
m_importer = std::make_shared<AssetImporter>();
// Initialize particle system
m_particle_system = std::make_shared<ParticleSystem>();
/* Simulator creation */
m_simulator = std::make_shared<Simulation>();
#ifdef _DEBUG
assert(m_resource_manager != nullptr &&
m_importer != nullptr &&
m_particle_system != nullptr
&& m_simulator != nullptr);
#endif
// ShowdowMapping shader settings
std::shared_ptr<Shader> shadow_mapping_shader = std::make_shared<Shader>("ShadowMapping");
shadow_mapping_shader->SetupShader("resources/shader/shadow_mapping_vs_adv.glsl",
"resources/shader/shadow_mapping_fs.glsl");
std::shared_ptr<Shader> point_shader = std::make_shared<Shader>("PointSprite");
point_shader->SetupShader("resources/shader/point_sprite_vs.glsl",
"resources/shader/point_sprite_fs.glsl");
auto mat_uniform = glGetUniformBlockIndex(shadow_mapping_shader->getProgram(), "Matrices");
GLuint ubo;
glGenBuffers(1, &ubo);
glBindBuffer(GL_UNIFORM_BUFFER, ubo);
glBufferData(GL_UNIFORM_BUFFER, 3 * sizeof(glm::mat4), NULL, GL_STATIC_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
// define the range of the buffer that links to a uniform binding point
glBindBufferRange(GL_UNIFORM_BUFFER, 0, ubo, 0, 3 * sizeof(glm::mat4));
// Camera Setting
{
CameraDesc camera_desc;
camera_desc.fov = 45.0f;
camera_desc.screen_width = f_width;
camera_desc.screen_height = f_height;
camera_desc.near_plane = 0.001f;
camera_desc.far_plane = 1000.0f;
camera_desc.position = glm::vec3(0.0f, 0.0f, 5.0f);
camera_desc.target_position = glm::vec3(0, 0, 0);
camera_desc.ubo = ubo;
//Main Camera setting
m_mainCamera = std::make_shared<Camera>(camera_desc);
m_resource_manager->SetMainCamera(m_mainCamera);
//Renderer setting
m_renderer = std::make_shared<Renderer>();
m_renderer->Initialize(m_resource_manager, m_particle_system, m_mainCamera, width, height);
}
// Load Meshes
//std::shared_ptr<GameObject> obj = std::make_shared<GameObject>();
{
//default mesh
auto load_status = IMPORT_STATUS::IMPORT_FAILED;
auto mesh = m_importer->LoadMesh("resources/models/monkey.obj", shadow_mapping_shader, load_status);
mesh->Initialize(shadow_mapping_shader);
if (load_status == IMPORT_STATUS::IMPORT_FAILED)
{
std::cout << "Load Failed\n";
SignalFail();
}
}
// Terrain Initilization
{
std::shared_ptr<Plane> plane_terrain = std::make_shared<Plane>();
plane_terrain->Initialize(glm::vec3(0, 0, 0), shadow_mapping_shader);
m_resource_manager->AddGameObject(static_pointer_cast<GameObject>(plane_terrain));
m_simulator->AddCollider(plane_terrain->getCollider());
auto collider = new PlaneCollider(glm::vec3(1, 0, 0), -10);
m_simulator->AddCollider(collider);
collider = new PlaneCollider(glm::vec3(-1, 0, 0), -10);
m_simulator->AddCollider(collider);
collider = new PlaneCollider(glm::vec3(0, 0, 1), -15 );
m_simulator->AddCollider(collider);
collider = new PlaneCollider(glm::vec3(0, 0, -1), 1);
m_simulator->AddCollider(collider);
}
//GenerateRadomParticles();
//GenerateFluidParticles();
/*
* End of resource settings
*/
/*
* UI settings
*/
/* GUI Initialize */
m_gui_manager = std::make_shared<GUIManager>();
m_gui_manager->Initialize(m_window);
m_gui_manager->AddGUIFunction(std::bind(Frame_Status_GUI));
//m_gui_manager->AddGUIFunction(std::bind(Mouse_Position_GUI));
//m_gui_manager->AddGUIFunction(std::bind(Object_Viewer_GUI));
//m_gui_manager->AddGUIFunction(std::bind(Animated_Character_GUI));
/* Managers initialization */
m_resource_manager->ArrangeStaticObjects();
m_simulator->Initialize(PBD_MODE::XPBD, m_particle_system);
m_simulator->SetSolverIteration(2);
return true;
}
void GLFWApp::Run()
{
while (!glfwWindowShouldClose(m_window))
{
t0 = std::chrono::high_resolution_clock::now();
ImGui_ImplGlfwGL3_NewFrame();
//Frame_Status_GUI();
//Object_Viewer_GUI();
t1 = std::chrono::high_resolution_clock::now();
Update();
t2 = std::chrono::high_resolution_clock::now();
Render();
t3 = std::chrono::high_resolution_clock::now();
glfwSwapBuffers(m_window);
glfwPollEvents();
t4 = std::chrono::high_resolution_clock::now();;
}
ImGui_ImplGlfwGL3_Shutdown();
ImGui::DestroyContext();
glfwTerminate();
}
void GLFWApp::ReleaseResources()
{
m_resource_manager->ShutDown();
m_particle_system->Release();
}
void GLFWApp::SignalFail()
{
m_app_status = false;
}
float GLFWApp::getElapsedTime()
{
return static_cast<float>(glfwGetTime());
}
void Error_callback(int error, const char* description)
{
fputs(description, stderr);
}
void Key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
static GLFWApp* const instance = GLFWApp::getInstance();
static auto res_manager = instance->getResourceManager();
static auto camera = res_manager->getMainCamera();
if (action == GLFW_RELEASE)
{
switch (key)
{
case GLFW_KEY_ESCAPE:
{
glfwSetWindowShouldClose(window, GL_TRUE);
break;
}
case GLFW_KEY_SPACE:
{
auto simulator = instance->getSimulator();
simulator->Pause();
break;
}
}
}
else if (action == GLFW_REPEAT)
{
auto camera = instance->getMainCamera();
switch (key)
{
case GLFW_KEY_UP:
{
camera->Rotate(0.0f, 0.03f);
break;
}
case GLFW_KEY_DOWN:
{
camera->Rotate(0.0f, -0.03f);
break;
}
case GLFW_KEY_LEFT:
{
camera->Rotate(-0.03f, 0.0f);
break;
}
case GLFW_KEY_RIGHT:
{
camera->Rotate(0.03f, 0.0f);
break;
}
}
}
}
void Mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
static GLFWApp* const instance = GLFWApp::getInstance();
double x_pos, y_pos;
glfwGetCursorPos(window, &x_pos, &y_pos);
/* record last mouse position when press mouse right button */
if (button == GLFW_MOUSE_BUTTON_RIGHT)
{
if (action == GLFW_PRESS)
{
instance->m_mouse_last_x = static_cast<float>(x_pos);
instance->m_mouse_last_y = static_cast<float>(y_pos);
instance->m_mouse_pressed = true;
}
else if (action == GLFW_RELEASE)
{
instance->m_mouse_pressed = false;
}
}
}
void Mouse_cursor_callback(GLFWwindow* window, double xpos, double ypos)
{
auto instance = GLFWApp::getInstance();
if (instance->m_mouse_pressed)
{
auto camera = instance->getMainCamera();
float x_change = -0.0005f * static_cast<float>(xpos - instance->m_mouse_last_x);
float y_change = 0.0005f * static_cast<float>(ypos - instance->m_mouse_last_y);
camera->Rotate(x_change, y_change);
instance->m_mouse_last_x = xpos;
instance->m_mouse_last_y = ypos;
}
}
void Mouse_scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
auto camera = GLFWApp::getInstance()->getMainCamera();
camera->Zoom(0.1f * yoffset);
}
void GLFWApp::Render()
{
m_renderer->Render();
//GUI rendering
m_gui_manager->Render();
}
void GLFWApp::Update()
{
auto anim_characters = m_resource_manager->getAnimCharacters();
auto jellies = m_resource_manager->getJellies();
m_previousTime = m_currentTime;
m_currentTime = static_cast<float>(glfwGetTime());
float dt = m_currentTime - m_previousTime;
const float time_step = 0.0005f;
#ifdef _USE_CUDA_
m_simulator->StepCUDA(time_step);
#else
m_simulator->Step(time_step);
#endif
for (auto it = m_resource_manager->getObjects().begin();
it != m_resource_manager->getObjects().end(); ++it)
{
(*it)->Update();
}
m_mainCamera->Update();
// GUI update
m_gui_manager->Update();
}
void GLFWApp::SetUpImGui()
{
//Set up imgui binding
ImGui::CreateContext();
ImGuiIO &io = ImGui::GetIO(); (void)io;
ImGui_ImplGlfwGL3_Init(m_window, false);
ImGui::StyleColorsDark();
}
void GLFWApp::GenerateRadomParticles()
{
float x, y, z;
std::vector<Particle_Ptr> particles;
for (int i = 0; i < 1000; ++i)
{
x = static_cast<float>(rand() % 1000) / 50.f - 10.f;
y = static_cast<float>(rand() % 1000) / 100.f;
z = static_cast<float>(rand() % 1000) / -20.f;
auto particle = std::make_shared<Particle>(glm::vec3(x, y, z), 0.1f);
particles.push_back(particle);
}
m_particle_system->Update();
}
void GLFWApp::GenerateFluidParticles()
{
float x, y, z;
ParticleSet* particles = m_particle_system->AllocateParticles(10000, 0.1f);
// set positions
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 100; ++j)
{
for (int k = 0; k < 10; ++k)
{
int idx = k + j * 10 + i * 1000;
x = 0.f + 0.5f * static_cast<float>(i);
y = 0.5f + 0.5f * static_cast<float>(j);
z = -10.f + 0.5f * static_cast<float>(k);
glm::vec3 pos(x, y, z);
particles->m_positions[idx] = pos;
particles->m_new_positions[idx] = pos;
particles->m_predict_positions[idx] = pos;
}
}
}
#ifdef _USE_CUDA_
m_particle_system->InitializeCUDA();
#else
m_particle_system->Initialize();
#endif
}
/*
* GUI functions
*/
void Frame_Status_GUI()
{
auto resource_manager = GLFWApp::getInstance()->getResourceManager();
auto camera = resource_manager->getMainCamera();
// If we don't call ImGui::Begin()/ImGui::End() the widgets automatically appears in a window called "Debug".
ImGui::Begin("Frame Status");
static float f = 0.0f;
static int counter = 0;
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::NewLine();
ImGui::Text("Object Array Size: %u", resource_manager->getObjects().size());
ImGui::Text("Mesh Array Size: %u", resource_manager->getMeshes().size());
ImGui::Text("Shader Array Size: %u", resource_manager->getShaders().size());
//ImGui::Text("IMGUI time: %.2lf ms", (t0-t4).count()/1000000.0);
//ImGui::Text("Update time: %.2lf ms", (t2-t1).count()/1000000.0);
//ImGui::Text("Render time: %.2lf ms", (t3-t2).count()/1000000.0);
//ImGui::Text("SWAP & PollEvent time: %.2lf ms", (t4-t3).count()/1000000.0);
ImGui::Text("Camera Position: %.2f, %.2f, %.2f", camera->m_position.x, camera->m_position.y, camera->m_position.z);
ImGui::End();
}
void Object_Viewer_GUI()
{
ImGui::Begin("Object List");
auto resource_manager = GLFWApp::getInstance()->getResourceManager();
static int update_count = 0;
static std::string name_list = "";
if (update_count % 60 == 0)
{
update_count = 0;
name_list.clear();
for (auto it = resource_manager->getObjects().begin();
it != resource_manager->getObjects().end(); ++it)
{
name_list += (*it)->getName() + "\n";
}
}
ImGui::Text("%s", name_list.data());
ImGui::NewLine();
update_count++;
ImGui::End();
}
void Mouse_Position_GUI()
{
auto instance = GLFWApp::getInstance();
double xpos, ypos;
glfwGetCursorPos(instance->getGLFWwindow(), &xpos, &ypos);
{
ImGui::Begin("Mouse");
ImGui::Text("Last: %f, %f", instance->m_mouse_last_x, instance->m_mouse_last_y);
ImGui::Text("Current: %f, %f", static_cast<float>(xpos), static_cast<float>(ypos));
ImGui::End();
}
}