-
Notifications
You must be signed in to change notification settings - Fork 0
/
Window.cpp
475 lines (410 loc) · 12.5 KB
/
Window.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
////////////////////////////////////////
// Window.cpp
////////////////////////////////////////
#include "Window.h"
#define M_PI 3.14159265
////////////////////////////////////////////////////////////////////////////////
// Window Properties
int Window::width;
int Window::height;
const char* Window::windowTitle = "CSE 169: Particle system";
GLFWwindow* Window::window;
ImVec2 Window::imguiMin;
ImVec2 Window::imguiMax;
bool Window::initGui;
bool Window::culling;
std::chrono::high_resolution_clock::time_point Window::start;
std::chrono::steady_clock::time_point Window::currTime;
bool Window::started;
bool Window::paused;
int Window::numIncr = 10; // particle system steps numIncr times per
// draw loop to avoid 'batches' of new particles
// Objects
ParticleSystem * Window::ps;
Cube * Window::ground;
// Physics constants
float Window::density = 1.225f;
float Window::drag = 0.1f;
float Window::mass = 0.1f;
float Window::restitution = 0.05f;
float Window::friction = 0.3f;
float Window::groundLevel = -2.0f;
glm::vec3 Window::gravity = glm::vec3(0.0,-9.8f,0.0);
// Camera Properties
Camera* Cam;
// Interaction Variables
bool LeftDown, RightDown;
int MouseX, MouseY;
// The shader program id
GLuint Window::shaderProgram;
////////////////////////////////////////////////////////////////////////////////
// Constructors and desctructors
bool Window::initializeProgram() {
// Create a shader program with a vertex shader and a fragment shader.
shaderProgram = LoadShaders("shaders/shader.vert", "shaders/shader.frag");
// Check the shader program.
if (!shaderProgram)
{
std::cerr << "Failed to initialize shader program" << std::endl;
return false;
}
return true;
}
bool Window::initializeObjects()
{
started = false;
culling = true;
initGui = false;
ps = new ParticleSystem();
ground = new Cube(glm::vec3(-100, -100, -100), glm::vec3(100, groundLevel-0.1, 100));
return true;
}
void Window::cleanUp()
{
// Deallocate the objects.
delete ps;
delete ground;
// Delete the shader program.
glDeleteProgram(shaderProgram);
}
////////////////////////////////////////////////////////////////////////////////
// for the Window
GLFWwindow* Window::createWindow(int width, int height)
{
// Initialize GLFW.
if (!glfwInit())
{
std::cerr << "Failed to initialize GLFW" << std::endl;
return NULL;
}
// 4x antialiasing.
glfwWindowHint(GLFW_SAMPLES, 4);
#ifdef __APPLE__
// Apple implements its own version of OpenGL and requires special treatments
// to make it uses modern OpenGL.
// Ensure that minimum OpenGL version is 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// Enable forward compatibility and allow a modern OpenGL context
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// Create the GLFW window.
//GLFWwindow* window = glfwCreateWindow(width, height, windowTitle, NULL, NULL);
window = glfwCreateWindow(width, height, windowTitle, NULL, NULL);
// Check if the window could not be created.
if (!window)
{
std::cerr << "Failed to open GLFW window." << std::endl;
glfwTerminate();
return NULL;
}
// Make the context of the window.
glfwMakeContextCurrent(window);
#ifndef __APPLE__
// On Windows and Linux, we need GLEW to provide modern OpenGL functionality.
// Initialize GLEW.
if (glewInit())
{
std::cerr << "Failed to initialize GLEW" << std::endl;
return NULL;
}
#endif
// Set swap interval to 1.
glfwSwapInterval(0);
// set up the camera
Cam = new Camera();
Cam->SetAspect(float(width) / float(height));
// initialize the interaction variables
LeftDown = RightDown = false;
MouseX = MouseY = 0;
// Call the resize callback to make sure things get drawn immediately.
Window::resizeCallback(window, width, height);
return window;
}
void Window::resizeCallback(GLFWwindow* window, int width, int height)
{
#ifdef __APPLE__
glfwGetFramebufferSize(window, &width, &height);
#endif
Window::width = width;
Window::height = height;
// Set the viewport size.
glViewport(0, 0, width, height);
Cam->SetAspect(float(width) / float(height));
}
////////////////////////////////////////////////////////////////////////////////
// update and draw functions
void Window::idleCallback()
{
if (paused) {
return;
}
// Perform any updates as necessary.
Cam->Update();
}
void Window::displayCallback(GLFWwindow* window)
{
if (!started) {
currTime = std::chrono::high_resolution_clock::now();
start= std::chrono::high_resolution_clock::now();
started = true;
}
else {
auto prevTime = currTime;
currTime = std::chrono::high_resolution_clock::now();
fsec changeFsec = currTime - prevTime;
float change = changeFsec.count();
float delta = change/numIncr;
//std::cout << delta*100 << std::endl;
for (int i = 0; i < numIncr; i++) {
ps->ApplyForces(gravity, density, drag);
ps->Step(delta);
ps->ApplyConstraints(groundLevel, restitution, friction);
}
ps->UpdateVertices();
ps->BindBuffers();
ps->Update(Cam->GetViewProjectMtx(), Cam->GetViewMtx());
}
// Clear the color and depth buffers.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Render the object.
ground->draw(Cam->GetViewProjectMtx(), Window::shaderProgram);
ps->Draw(Window::shaderProgram);
drawImgui();
// Gets events, including input such as keyboard and mouse or window resizing.
glfwPollEvents();
// Swap buffers.
glfwSwapBuffers(window);
}
// helper to set correct imgui window position min and max
void Window::updateImguiPos() {
// hack to include borders and scrollbar in desired rect
int smallPadding = 10;
int scrollPadding = 30;
int titlePadding = 40;
imguiMin = ImGui::GetWindowContentRegionMin();
imguiMax = ImGui::GetWindowContentRegionMax();
imguiMin.x += ImGui::GetWindowPos().x - smallPadding;
imguiMin.y += ImGui::GetWindowPos().y + ImGui::GetScrollY() - titlePadding;
imguiMax.x += ImGui::GetWindowPos().x + scrollPadding;
imguiMax.y += ImGui::GetWindowPos().y + ImGui::GetScrollY() + smallPadding;
// rectangle for debugging
//ImGui::GetForegroundDrawList()->AddRect(imguiMin, imguiMax, IM_COL32(255, 255, 0, 255));
}
void Window::drawImgui() {
// start new frame
ImGuiIO& io = ImGui::GetIO();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
//ImGui::SetWindowFontScale(10.0f);
ImGui::SetNextWindowPos(ImVec2(0, 0));
ImGui::SetNextWindowSize(ImVec2(Window::width / 3, Window::height));
ImGui::Begin("Particle system simulation");
ImGui::SetWindowFontScale(2.0f);
if (ImGui::Button("Reset defaults"))
{
density = 1.225f;
drag = 0.1f;
mass = 0.0f;
restitution = 0.05f;
friction = 0.5f;
gravity = glm::vec3(0.0, -9.8f, 0.0);
ps->startPos = glm::vec3(0.0f, 1.0f, 0.0f);
ps->startVel = glm::vec3(0.0f, 1.0f, 0.0f);
ps->lifeSpan = 3.0f;
ps->posVar = 1.0f;
ps->velVar = 1.0f;
ps->lifeVar = 2.0f;
ps->pps = 50;
ps->radius = 0.1f;
}
// Create slider hierarchy
std::string slider_name;
std::string axes[3] = { "x", "y", "z" };
if (!initGui) ImGui::SetNextTreeNodeOpen(true);
if (ImGui::TreeNode("Particle controls")) {
//if (!initGui) ImGui::SetNextTreeNodeOpen(true);
if (ImGui::TreeNode("position")) {
for (int i = 0; i < 3; i++) {
std::string slider_name = axes[i] + "##pos";
if (i == 1) {
ImGui::SliderFloat(slider_name.c_str(), &ps->startPos[i], groundLevel+0.001, 5.0f);
}
else {
ImGui::SliderFloat(slider_name.c_str(), &ps->startPos[i], -5.0f, 5.0f);
}
}
slider_name = "var##pos";
ImGui::SliderFloat(slider_name.c_str(), &ps->posVar, 0.0f, 5.0f);
ImGui::TreePop();
}
//if (!initGui) ImGui::SetNextTreeNodeOpen(true);
if (ImGui::TreeNode("velocity")) {
for (int i = 0; i < 3; i++) {
std::string slider_name = axes[i] + "##rot";
ImGui::SliderFloat(slider_name.c_str(), &ps->startVel[i], -10.0f, 10.0f);
}
slider_name = "var##vel";
ImGui::SliderFloat(slider_name.c_str(), &ps->velVar, 0.0f, 10.0f);
ImGui::TreePop();
}
if (!initGui) ImGui::SetNextTreeNodeOpen(true);
if (ImGui::TreeNode("radius")) {
slider_name = "##radius";
ImGui::SliderFloat(slider_name.c_str(), &ps->radius, 0.01f, 2.0f);
ImGui::TreePop();
}
if (!initGui) ImGui::SetNextTreeNodeOpen(true);
if (ImGui::TreeNode("life span")) {
slider_name = "span##life";
ImGui::SliderFloat(slider_name.c_str(), &ps->lifeSpan, 0.001f, 10.0f);
slider_name = "var##life";
ImGui::SliderFloat(slider_name.c_str(), &ps->lifeVar, 0.0f, 10.0f);
ImGui::TreePop();
}
if (!initGui) ImGui::SetNextTreeNodeOpen(true);
if (ImGui::TreeNode("particles per second")) {
slider_name = "##pps";
ImGui::SliderFloat(slider_name.c_str(), &ps->pps, 1.0f, 100.0f);
ImGui::TreePop();
}
//if (!initGui) ImGui::SetNextTreeNodeOpen(true);
if (ImGui::TreeNode("color")) {
for (int i = 0; i < 3; i++) {
std::string slider_name = axes[i] + "##color";
ImGui::SliderFloat(slider_name.c_str(), &ps->color[i], 0.0f, 1.0f);
}
ImGui::TreePop();
}
ImGui::TreePop();
}
if (!initGui) ImGui::SetNextTreeNodeOpen(true);
if (ImGui::TreeNode("Environment controls")) {
if (!initGui) ImGui::SetNextTreeNodeOpen(true);
if (ImGui::TreeNode("gravity")) {
slider_name = "##gravity";
ImGui::SliderFloat(slider_name.c_str(), &gravity.y, 0.0f, -20.0f);
ImGui::TreePop();
}
if (!initGui) ImGui::SetNextTreeNodeOpen(true);
if (ImGui::TreeNode("drag coefficient")) {
slider_name = "##drag";
ImGui::SliderFloat(slider_name.c_str(), &drag, 0.0f, 5.0f);
ImGui::TreePop();
}
if (!initGui) ImGui::SetNextTreeNodeOpen(true);
if (ImGui::TreeNode("density")) {
slider_name = "##density";
ImGui::SliderFloat(slider_name.c_str(), &density, 0.0f, 5.0f);
ImGui::TreePop();
}
if (!initGui) ImGui::SetNextTreeNodeOpen(true);
if (ImGui::TreeNode("collision restitution")) {
slider_name = "##restitution";
ImGui::SliderFloat(slider_name.c_str(), &restitution, 0.001f, 1.0f);
ImGui::TreePop();
}
if (!initGui) ImGui::SetNextTreeNodeOpen(true);
if (ImGui::TreeNode("collision friction")) {
slider_name = "##friction";
ImGui::SliderFloat(slider_name.c_str(), &friction, 0.0f, 1.0f);
ImGui::TreePop();
}
ImGui::TreePop();
}
initGui = true;
updateImguiPos();
ImGui::End();
// Render imgui onto screen
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
////////////////////////////////////////////////////////////////////////////////
// helper to reset the camera
void Window::resetCamera()
{
Cam->Reset();
Cam->SetAspect(float(Window::width) / float(Window::height));
}
////////////////////////////////////////////////////////////////////////////////
// callbacks - for Interaction
void Window::keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
// Check for a key press.
if (action == GLFW_PRESS)
{
switch (key)
{
case GLFW_KEY_ESCAPE:
// Close the window. This causes the program to also terminate.
glfwSetWindowShouldClose(window, GL_TRUE);
break;
case GLFW_KEY_R:
resetCamera();
break;
case GLFW_KEY_C:
if (culling){
glDisable(GL_CULL_FACE);
culling = false;
}
else {
glEnable(GL_CULL_FACE);
culling = true;
}
break;
case GLFW_KEY_P:
if (!paused) {
paused = true;
}
else if (paused) {
paused = false;
}
break;
default:
break;
}
}
}
void Window::mouse_callback(GLFWwindow* window, int button, int action, int mods)
{
if (button == GLFW_MOUSE_BUTTON_LEFT) {
LeftDown = (action == GLFW_PRESS);
}
if (button == GLFW_MOUSE_BUTTON_RIGHT) {
RightDown = (action == GLFW_PRESS);
}
}
void Window::cursor_callback(GLFWwindow* window, double currX, double currY) {
int maxDelta = 100;
int dx = glm::clamp((int)currX - MouseX, -maxDelta, maxDelta);
int dy = glm::clamp(-((int)currY - MouseY), -maxDelta, maxDelta);
MouseX = (int)currX;
MouseY = (int)currY;
// don't update rotation if mouse inside Imgui window
if (MouseX >= imguiMin.x && MouseX <= imguiMax.x &&
MouseY >= imguiMin.y && MouseY <= imguiMax.y )
{
return;
}
// don't update rotation if mouse outside window
if (MouseX < 0 || MouseX > Window::width ||
MouseY < 0 || MouseY > Window::height)
{
return;
}
// Move camera
// NOTE: this should really be part of Camera::Update()
if (LeftDown) {
const float rate = 1.0f;
Cam->SetAzimuth(Cam->GetAzimuth() + dx * rate);
Cam->SetIncline(glm::clamp(Cam->GetIncline() - dy * rate, -90.0f, 90.0f));
}
if (RightDown) {
const float rate = 0.005f;
float dist = glm::clamp(Cam->GetDistance() * (1.0f - dx * rate), 0.01f, 1000.0f);
Cam->SetDistance(dist);
}
}
////////////////////////////////////////////////////////////////////////////////