-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.cpp
executable file
·388 lines (324 loc) · 9.22 KB
/
App.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
#include "App.h"
#include <memory>
#include <nanogui/label.h>
#include <nanogui/layout.h>
#include "scenarios/ClothScenario.h"
const double gFPS = 60;
const double gMaxWindStrength = 50;
cApp::cApp(int w, int h, const std::string& title) : nanogui::Screen(Eigen::Vector2i(w, h), title)
{
mGUIWindow = nullptr;
mPlayButton = nullptr;
mWindSlider = nullptr;
mPrevTime = 0;
mEnableAnimation = true;
}
cApp::~cApp()
{
mScenario.reset();
}
void cApp::Init()
{
cDrawUtil::InitDrawUtil();
BuildScenario();
BuildGUI();
}
bool cApp::keyboardEvent(int key, int scancode, int action, int modifiers) {
if (Screen::keyboardEvent(key, scancode, action, modifiers))
return true;
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
setVisible(false);
return true;
}
return false;
}
bool cApp::mouseButtonEvent(const Eigen::Vector2i &p, int button, bool down, int modifiers)
{
if (nanogui::Screen::mouseButtonEvent(p, button, down, modifiers))
{
return true;
}
if (mScenario != nullptr)
{
return mScenario->MouseButtonEvent(p, button, down, modifiers);
}
return false;
}
bool cApp::mouseMotionEvent(const Eigen::Vector2i &p, const Eigen::Vector2i &rel, int button, int modifiers)
{
if (nanogui::Screen::mouseMotionEvent(p, rel, button, modifiers))
{
return true;
}
if (mScenario != nullptr)
{
return mScenario->MouseMotionEvent(p, rel, button, modifiers);
}
return false;
}
bool cApp::scrollEvent(const Eigen::Vector2i &p, const Eigen::Vector2f &rel)
{
if (nanogui::Screen::scrollEvent(p, rel))
{
return true;
}
if (mScenario != nullptr)
{
return mScenario->ScrollEvent(p, rel);
}
return false;
}
void cApp::draw(NVGcontext *ctx)
{
//Draw the user interface
Screen::draw(ctx);
}
void cApp::drawContents()
{
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
Update();
DrawScenario();
}
bool cApp::resizeEvent(const Eigen::Vector2i& size)
{
bool val = nanogui::Screen::resizeEvent(size);
if (mScenario != nullptr)
{
mScenario->Resize(size);
}
return val;
}
double cApp::GetFPS() const
{
return gFPS;
}
void cApp::BuildScenario()
{
mScenario = std::unique_ptr<cScenario>(new cClothScenario());
mScenario->Resize(mSize);
mScenario->Init();
}
void cApp::UpdateScenario(double time)
{
double time_elapsed = time - mPrevTime;
double time_step = 1 / GetFPS();
time_elapsed = std::min(time_elapsed, time_step);
StepScenario(time_elapsed);
mPrevTime = time;
}
void cApp::StepScenario(double time_elapsed)
{
if (mScenario != nullptr)
{
mScenario->Update(time_elapsed);
}
}
cClothScenario* cApp::GetClothScene()
{
cClothScenario* cloth_scene = reinterpret_cast<cClothScenario*>(mScenario.get());
return cloth_scene;
}
void cApp::Update()
{
if (EnableAnimation())
{
// Advances animation
UpdateScenario(glfwGetTime());
}
UpdateGUI();
}
void cApp::DrawScenario()
{
if (mScenario != nullptr)
{
mScenario->Draw();
}
}
bool cApp::EnableAnimation() const
{
bool enable = mEnableAnimation;
if (mPlayButton != nullptr)
{
enable &= mPlayButton->pushed();
}
return enable;
}
void cApp::ClearGUI()
{
if (mGUIWindow != nullptr)
{
removeChild(mGUIWindow);
}
}
void cApp::BuildGUI()
{
ClearGUI();
auto cloth_scene = GetClothScene();
// Build GUI panel that will contain the interface
mGUIWindow = new nanogui::Window(this, "Scene Control");
mGUIWindow->setPosition(nanogui::Vector2i(0, 0));
mGUIWindow->setLayout(new nanogui::GroupLayout());
// FPS textbox
mFPSLabel = new nanogui::Label(mGUIWindow, "FPS: 0", "sans-bold");
mFPSLabel->setFontSize(18);
// Add combo box for interators
auto lab = new nanogui::Label(mGUIWindow, "Integrator", "sans-bold");
mIntegratorCombo = new nanogui::ComboBox(mGUIWindow, { "Explicit", "Midpoint", "Trapezoid", "Implicit" });
tComboCallback int_combo_callback = std::bind(&cApp::IntegratorComboCallback, this, std::placeholders::_1);
mIntegratorCombo->setCallback(int_combo_callback);
mIntegratorCombo->setSelectedIndex(cloth_scene->GetIntegrator());
// Add panel for stepsize control
new nanogui::Label(mGUIWindow, "Stepsize", "sans-bold");
Widget* stepsize_panel = new Widget(mGUIWindow);
stepsize_panel->setLayout(new nanogui::BoxLayout(nanogui::Orientation::Horizontal,
nanogui::Alignment::Middle, 0, 6));
// Decrement
auto stepsize_dec = new nanogui::Button(stepsize_panel, "", ENTYPO_ICON_DOWN);
stepsize_dec->setCallback(std::bind(&cApp::DecSimStepsize, this));
mStepSizeTextBox = new nanogui::TextBox(stepsize_panel);
mStepSizeTextBox->setFixedSize(Eigen::Vector2i(70, 30));
SetSimStepsize(cloth_scene->GetStepsize());
// Increment
auto stepsize_inc = new nanogui::Button(stepsize_panel, "", ENTYPO_ICON_UP);
stepsize_inc->setCallback(std::bind(&cApp::IncSimStepsize, this));
// Add panel for stiffness control
new nanogui::Label(mGUIWindow, "Stiffness", "sans-bold");
Widget* stiffness_panel = new Widget(mGUIWindow);
stiffness_panel->setLayout(new nanogui::BoxLayout(nanogui::Orientation::Horizontal,
nanogui::Alignment::Middle, 0, 6));
// Decrement
auto stiffness_dec = new nanogui::Button(stiffness_panel, "", ENTYPO_ICON_DOWN);
stiffness_dec->setCallback(std::bind(&cApp::DecClothStiffness, this));
mStiffnessTextBox = new nanogui::TextBox(stiffness_panel);
mStiffnessTextBox->setFixedSize(Eigen::Vector2i(70, 30));
SetClothStiffness(cloth_scene->GetStiffness());
// Increment
auto stiffness_inc = new nanogui::Button(stiffness_panel, "", ENTYPO_ICON_UP);
stiffness_inc->setCallback(std::bind(&cApp::IncClothStiffness, this));
// Slider to control the strength of the wind
new nanogui::Label(mGUIWindow, "Wind Strength", "sans-bold");
mWindSlider = new nanogui::Slider(mGUIWindow);
mWindSlider->setCallback(std::bind(&cApp::WindSliderCallback, this, std::placeholders::_1));
double wind_strength = cloth_scene->GetWindStrength();
mWindSlider->setValue(static_cast<float>(wind_strength / gMaxWindStrength));
// Add panel for playback control
new nanogui::Label(mGUIWindow, "Playback", "sans-bold");
Widget* playback = new Widget(mGUIWindow);
playback->setLayout(new nanogui::BoxLayout(nanogui::Orientation::Horizontal,
nanogui::Alignment::Middle, 0, 6));
// Reload scene button
auto reload = new nanogui::Button(playback, "", ENTYPO_ICON_CCW);
reload->setCallback(std::bind(&cApp::Reload, this));
// Play/Pause button
mPlayButton = new nanogui::Button(playback, "", ENTYPO_ICON_PAUS);
mPlayButton->setFlags(nanogui::Button::ToggleButton);
mPlayButton->setPushed(true);
mPlayButton->setChangeCallback(std::bind(&cApp::TogglePlayCallback, this, std::placeholders::_1));
// Step forward button
auto step_forward = new nanogui::Button(playback, "", ENTYPO_ICON_FF);
step_forward->setCallback(std::bind(&cApp::StepForwardCallback, this));
// After all GUI has been built, call refresh to reorganize everything
RefreshGUI();
}
void cApp::UpdateGUI()
{
char str_buffer[32];
auto cloth_scene = GetClothScene();
sprintf(str_buffer, "FPS: %.3f", cloth_scene->GetFPS());
mFPSLabel->setCaption(std::string(str_buffer));
}
void cApp::RefreshGUI()
{
performLayout();
}
void cApp::StepForwardCallback()
{
mPlayButton->setPushed(false);
TogglePlayCallback(false);
StepScenario(1 / GetFPS());
}
void cApp::TogglePlayCallback(bool pushed)
{
if (pushed)
{
mPlayButton->setIcon(ENTYPO_ICON_PAUS);
}
else
{
mPlayButton->setIcon(ENTYPO_ICON_PLAY);
}
}
void cApp::WindSliderCallback(double val)
{
auto cloth_scene = GetClothScene();
cloth_scene->SetWindStrength(val * gMaxWindStrength);
}
void cApp::IntegratorComboCallback(int i)
{
auto cloth_scene = GetClothScene();
cloth_scene->SetIntegrator(static_cast<cCloth::eIntegrator>(i));
cloth_scene->Reset();
}
void cApp::DecSimStepsize()
{
auto cloth_scene = GetClothScene();
SetSimStepsize(cloth_scene->GetStepsize() - 0.001);
}
void cApp::IncSimStepsize()
{
auto cloth_scene = GetClothScene();
SetSimStepsize(cloth_scene->GetStepsize() + 0.001);
}
void cApp::SetSimStepsize(double stepsize)
{
auto cloth_scene = GetClothScene();
cloth_scene->SetStepsize(std::max(0.0, stepsize));
char str_buffer[32];
sprintf(str_buffer, "%.3f", cloth_scene->GetStepsize());
mStepSizeTextBox->setValue(std::string(str_buffer));
}
void cApp::DecClothStiffness()
{
auto cloth_scene = GetClothScene();
SetClothStiffness(cloth_scene->GetStiffness() - 1);
}
void cApp::IncClothStiffness()
{
auto cloth_scene = GetClothScene();
SetClothStiffness(cloth_scene->GetStiffness() + 1);
}
void cApp::SetClothStiffness(double stiffness)
{
auto cloth_scene = GetClothScene();
cloth_scene->SetStiffness(std::max(0.0, stiffness));
char str_buffer[32];
sprintf(str_buffer, "%.0f", cloth_scene->GetStiffness());
mStiffnessTextBox->setValue(std::string(str_buffer));
}
void cApp::Reload()
{
mScenario->Reset();
}
void cApp::BuildShortFileNames(const std::vector<std::string>& files, std::vector<std::string>& out_names) const
{
// shorten the filepaths for display in the GUI
out_names.clear();
std::vector<std::string> short_names;
for (size_t f = 0; f < files.size(); ++f)
{
const std::string& curr_file = files[f];
int idx = 0;
for (int i = static_cast<int>(curr_file.size()) - 1; i >= 0; --i)
{
char curr_char = curr_file[i];
if (curr_char == '\\' || curr_char == '/')
{
idx = i + 1;
break;
}
}
std::string filename = curr_file.substr(idx, curr_file.size() - idx);
out_names.push_back(filename);
}
}