generated from libigl/libigl-example-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·117 lines (97 loc) · 2.76 KB
/
main.cpp
File metadata and controls
executable file
·117 lines (97 loc) · 2.76 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
#include <igl/opengl/glfw/Viewer.h>
#include <igl/opengl/glfw/imgui/ImGuiMenu.h>
#include <igl/opengl/glfw/imgui/ImGuiHelpers.h>
#include <Eigen/StdVector>
#include <thread>
#include <iostream>
#include <algorithm>
#include "PhysicsHook.h"
#include "MpmHook.h"
#include <omp.h>
using namespace Eigen;
static PhysicsHook *hook = NULL;
void toggleSimulation()
{
if (!hook)
return;
if (hook->isPaused())
hook->run();
else
hook->pause();
}
void resetSimulation()
{
if (!hook)
return;
hook->reset();
}
bool drawCallback(igl::opengl::glfw::Viewer &viewer)
{
if (!hook)
return false;
hook->render(viewer);
return false;
}
bool keyCallback(igl::opengl::glfw::Viewer &viewer, unsigned int key, int modifiers)
{
if (key == ' ') {
toggleSimulation();
return true;
} else if (key == 'r') {
resetSimulation();
return true;
}
return false;
}
bool mouseDownCallback(igl::opengl::glfw::Viewer &viewer, int button, int modifier) {
if (!hook)
return false;
return hook->mouseClicked(viewer, button);
}
bool mouseUpCallback(igl::opengl::glfw::Viewer &viewer, int button, int modifier) {
if (!hook)
return false;
return hook->mouseReleased(viewer, button);
}
bool mouseMoveCallback(igl::opengl::glfw::Viewer &viewer, int button, int modifier) {
if (!hook)
return false;
return hook->mouseMoved(viewer, button);
}
bool drawGUI(igl::opengl::glfw::imgui::ImGuiMenu &menu) {
if (ImGui::CollapsingHeader("Simulation Control", ImGuiTreeNodeFlags_DefaultOpen)) {
if (ImGui::Button("Run/Pause Sim", ImVec2(-1, 0))) {
toggleSimulation();
}
if (ImGui::Button("Reset Sim", ImVec2(-1, 0))) {
resetSimulation();
}
}
hook->drawGUI(menu);
return false;
}
int main(int argc, char *argv[]) {
igl::opengl::glfw::Viewer viewer;
omp_set_num_threads(12);
hook = new MpmHook();
hook->reset();
viewer.core().is_animating = true;
viewer.core().animation_max_fps = 60;
// Center camera
Matrix3d bnd;
bnd << 0.0, 0.0, 0.0,
0.5, 0.5, 0.0,
1.0, 1.0, 0.0;
viewer.core().align_camera_center(bnd);
// Render points
viewer.callback_mouse_down = mouseDownCallback;
viewer.callback_mouse_up = mouseUpCallback;
viewer.callback_mouse_move = mouseMoveCallback;
viewer.callback_key_pressed = keyCallback;
viewer.callback_pre_draw = drawCallback;
// Add UI
igl::opengl::glfw::imgui::ImGuiMenu menu;
viewer.plugins.push_back(&menu);
menu.callback_draw_viewer_menu = [&]() {drawGUI(menu); };
viewer.launch();
}