-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisual.cpp
More file actions
1209 lines (999 loc) · 43.2 KB
/
visual.cpp
File metadata and controls
1209 lines (999 loc) · 43.2 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
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "headers/visual.h"
// GLAD & GLFW
#include <glad/glad.h>
#include <GLFW/glfw3.h>
// GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
// ImGui
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
// explosion strategy
#include "explosionaxis/explosion_axis_strategy.h"
#include "post_processor.h"
namespace MC
{
// global variables
Camera camera;
bool firstMouse = true;
float lastX, lastY;
bool showIntersections = false; // control exploded view display
bool showErrorPopup = false;
std::string errorPopupMessage = "";
std::string errorPopupTitle = "Error";
float g_explosionDistancePercent = 87.5f;
float g_explosionDistance = 35.0f;
bool showExplosionAxis = false;
float g_isoLevelPercent = 10.0f; // default 50%
float g_tempIsoLevelPercent = 10.0f;
float tempExplosionPercent = g_explosionDistancePercent;
static bool panelFirstTime = true;
// vertex shader source for mesh rendering
const char *vertexShaderSource = R"glsl(
#version 330 core
layout (location = 0) in vec3 aPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
out vec3 FragPos;
void main()
{
FragPos = vec3(model * vec4(aPos, 1.0));
gl_Position = projection * view * model * vec4(aPos, 1.0);
}
)glsl";
// fragment shader source for basic lighting
const char *fragmentShaderSource = R"glsl(
#version 330 core
out vec4 FragColor;
in vec3 FragPos;
void main()
{
vec3 normal = normalize(cross(dFdx(FragPos), dFdy(FragPos)));
vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0));
float diff = max(dot(normal, lightDir), 0.0);
vec3 diffuse = diff * vec3(0.8, 0.8, 0.8);
vec3 ambient = vec3(0.2, 0.2, 0.2);
FragColor = vec4(ambient + diffuse, 1.0);
}
)glsl";
// line shader for symmetry axis visualization
const char *lineVertexShaderSource = R"glsl(
#version 330 core
layout (location = 0) in vec3 aPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main() {
gl_Position = projection * view * model * vec4(aPos, 1.0);
}
)glsl";
const char *lineFragmentShaderSource = R"glsl(
#version 330 core
out vec4 FragColor;
void main() {
FragColor = vec4(1.0, 0.5, 0.0, 1.0); // orange color for the axis
}
)glsl";
// imgui style
void setupImGuiStyle()
{
ImGuiStyle &style = ImGui::GetStyle();
ImVec4 *colors = style.Colors;
// configure colors and style
colors[ImGuiCol_WindowBg] = ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
colors[ImGuiCol_Text] = ImVec4(0.0f, 0.0f, 0.0f, 1.0f);
colors[ImGuiCol_TitleBg] = ImVec4(0.9f, 0.9f, 0.9f, 1.0f);
colors[ImGuiCol_TitleBgActive] = ImVec4(0.85f, 0.85f, 0.85f, 1.0f);
colors[ImGuiCol_Button] = ImVec4(0.9f, 0.9f, 0.9f, 1.0f);
colors[ImGuiCol_ButtonHovered] = ImVec4(0.8f, 0.8f, 0.8f, 1.0f);
colors[ImGuiCol_ButtonActive] = ImVec4(0.7f, 0.7f, 0.7f, 1.0f);
colors[ImGuiCol_FrameBg] = ImVec4(0.95f, 0.95f, 0.95f, 1.0f);
colors[ImGuiCol_CheckMark] = ImVec4(0.3f, 0.3f, 0.3f, 1.0f);
colors[ImGuiCol_SliderGrab] = ImVec4(0.7f, 0.7f, 0.7f, 1.0f);
colors[ImGuiCol_SliderGrabActive] = ImVec4(0.5f, 0.5f, 0.5f, 1.0f);
colors[ImGuiCol_FrameBgHovered] = ImVec4(0.9f, 0.9f, 0.9f, 1.0f);
colors[ImGuiCol_FrameBgActive] = ImVec4(0.85f, 0.85f, 0.85f, 1.0f);
colors[ImGuiCol_Header] = ImVec4(0.9f, 0.9f, 0.9f, 1.0f);
colors[ImGuiCol_HeaderHovered] = ImVec4(0.8f, 0.8f, 0.8f, 1.0f);
colors[ImGuiCol_HeaderActive] = ImVec4(0.7f, 0.7f, 0.7f, 1.0f);
colors[ImGuiCol_Separator] = ImVec4(0.8f, 0.8f, 0.8f, 1.0f);
colors[ImGuiCol_PopupBg] = ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
style.WindowPadding = ImVec2(12.0f, 12.0f);
style.FramePadding = ImVec2(6.0f, 4.0f);
style.ItemSpacing = ImVec2(8.0f, 6.0f);
style.ItemInnerSpacing = ImVec2(6.0f, 6.0f);
style.TouchExtraPadding = ImVec2(0.0f, 0.0f);
style.WindowRounding = 4.0f;
style.FrameRounding = 3.0f;
style.PopupRounding = 4.0f;
style.ScrollbarRounding = 4.0f;
style.GrabRounding = 3.0f;
style.TabRounding = 4.0f;
style.WindowTitleAlign = ImVec2(0.5f, 0.5f);
}
// calculate viewport size
bool calculateViewport(GLFWwindow *window, int &viewportX, int &viewportY, int &viewportWidth, int &viewportHeight)
{
int framebufferWidth = 0;
int framebufferHeight = 0;
glfwGetFramebufferSize(window, &framebufferWidth, &framebufferHeight);
if (framebufferWidth<=0||framebufferHeight<=0)
{
viewportX = 0;
viewportY = 0;
viewportWidth = 0;
viewportHeight = 0;
return false;
}
viewportX = 0;
viewportY = 0;
viewportWidth = framebufferWidth;
viewportHeight = framebufferHeight;
return true;
}
// apply calculated viewport to OpenGL
bool updateViewport(GLFWwindow *window, int *outWidth, int *outHeight)
{
int viewportX = 0;
int viewportY = 0;
int viewportWidth = 0;
int viewportHeight = 0;
if (!calculateViewport(window, viewportX, viewportY, viewportWidth, viewportHeight))
{
return false;
}
glViewport(viewportX, viewportY, viewportWidth, viewportHeight);
if (outWidth)
{
*outWidth = viewportWidth;
}
if (outHeight)
{
*outHeight = viewportHeight;
}
return true;
}
// window resize callback
void framebuffer_size_callback(GLFWwindow *window, int width, int height)
{
if (width<=0||height<=0)
{
return;
}
updateViewport(window);
}
// mouse callback function
void mouse_callback(GLFWwindow *window, double xpos, double ypos)
{
ImGuiIO &io = ImGui::GetIO();
if (io.WantCaptureMouse)
{
return;
}
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
return;
}
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
{
float xoffset = xpos - lastX;
float yoffset = lastY - ypos;
const float sensitivity = 0.1f;
camera.rotationY += xoffset * sensitivity;
camera.rotationX += yoffset * sensitivity;
// limit pitch angle
if (camera.rotationX > 89.0f)
camera.rotationX = 89.0f;
if (camera.rotationX < -89.0f)
camera.rotationX = -89.0f;
}
lastX = xpos;
lastY = ypos;
}
// scroll callback for zooming
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset)
{
std::cout << "Scroll event: yoffset = " << yoffset << std::endl;
camera.zoom -= yoffset * 0.2f;
}
// Key callback for additional controls
void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, true);
}
// adjust camera distance with + and - keys
if (key == GLFW_KEY_EQUAL && (action == GLFW_PRESS || action == GLFW_REPEAT))
{
camera.distance *= 0.9f; //Scroll in
}
if (key == GLFW_KEY_MINUS && (action == GLFW_PRESS || action == GLFW_REPEAT))
{
camera.distance *= 1.1f; //Scroll out
}
}
// compile individual shader
unsigned int compileShader(unsigned int type, const char *src)
{
unsigned int shader = glCreateShader(type);
glShaderSource(shader, 1, &src, nullptr);
glCompileShader(shader);
int success;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success)
{
char infoLog[512];
glGetShaderInfoLog(shader, 512, nullptr, infoLog);
std::cerr << "Shader compilation failed: " << infoLog << std::endl;
}
return shader;
}
// create shader program for mesh rendering
unsigned int createShaderProgram()
{
unsigned int vertexShader = compileShader(GL_VERTEX_SHADER, vertexShaderSource);
unsigned int fragmentShader = compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
unsigned int shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
int success;
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success)
{
char infoLog[512];
glGetProgramInfoLog(shaderProgram, 512, nullptr, infoLog);
std::cerr << "Shader program linking failed: " << infoLog << std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
return shaderProgram;
}
// create shader program for line rendering
unsigned int createLineShaderProgram()
{
unsigned int vertexShader = compileShader(GL_VERTEX_SHADER, lineVertexShaderSource);
unsigned int fragmentShader = compileShader(GL_FRAGMENT_SHADER, lineFragmentShaderSource);
unsigned int shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
int success;
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success)
{
char infoLog[512];
glGetProgramInfoLog(shaderProgram, 512, nullptr, infoLog);
std::cerr << "Line shader program linking failed: " << infoLog << std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
return shaderProgram;
}
//---------------------- OpenGL ----------------------
// set mesh VAO/VBO/EBO
void setupMesh(const Mesh &mesh, unsigned int &VAO, unsigned int &VBO, unsigned int &EBO)
{
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, mesh.vertices.size() * sizeof(Vertex),
mesh.vertices.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh.indices.size() * sizeof(IndexType),
mesh.indices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
}
// update mesh
void updateMesh(const Mesh &mesh, unsigned int &VAO, unsigned int &VBO, unsigned int &EBO)
{
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, mesh.vertices.size() * sizeof(Vertex),
mesh.vertices.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh.indices.size() * sizeof(IndexType),
mesh.indices.data(), GL_STATIC_DRAW);
}
void renderExplosionAxisGUI(std::string ¤tStrategy)
{
ImGuiStyle &style = ImGui::GetStyle();
style.Colors[ImGuiCol_WindowBg] = ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
style.Colors[ImGuiCol_Text] = ImVec4(0.0f, 0.0f, 0.0f, 1.0f);
style.Colors[ImGuiCol_TitleBg] = ImVec4(0.9f, 0.9f, 0.9f, 1.0f);
style.Colors[ImGuiCol_TitleBgActive] = ImVec4(0.85f, 0.85f, 0.85f, 1.0f);
style.Colors[ImGuiCol_Button] = ImVec4(0.9f, 0.9f, 0.9f, 1.0f);
style.Colors[ImGuiCol_ButtonHovered] = ImVec4(0.8f, 0.8f, 0.8f, 1.0f);
style.Colors[ImGuiCol_ButtonActive] = ImVec4(0.7f, 0.7f, 0.7f, 1.0f);
style.Colors[ImGuiCol_FrameBg] = ImVec4(0.95f, 0.95f, 0.95f, 1.0f);
ImGuiViewport *viewport = ImGui::GetMainViewport();
ImVec2 viewportSize = viewport->Size;
float panelWidth = 300.0f;
float spacing = 10.0f;
float panelY = spacing + ImGui::GetFrameHeight() + spacing + 150.0f;
if(panelFirstTime) {
ImGui::SetNextWindowPos(ImVec2(viewportSize.x - panelWidth - spacing, panelY));
ImGui::SetNextWindowSize(ImVec2(panelWidth, 0));
}
if (ImGui::Begin("Explosion Axis Settings"))
{
MC::ExplosionAxisConfig &config = MC::getExplosionAxisConfig();
std::string currentInternalStrategy = MC::getCurrentExplosionStrategyName();
if (currentStrategy.empty())
{
currentStrategy = MC::ExplosionAxisConfig::convertToUIName(currentInternalStrategy);
}
std::vector<std::string> strategies = MC::ExplosionAxisConfig::getStrategyNames();
std::vector<const char *> strategyNames;
for (const auto &strategy : strategies)
{
strategyNames.push_back(strategy.c_str());
}
int currentIndex = 0;
for (size_t i = 0; i < strategies.size(); ++i)
{
if (strategies[i] == currentStrategy)
{
currentIndex = static_cast<int>(i);
break;
}
}
static std::string previousStrategy = currentStrategy;
ImGui::Text("Custom Explosion Axis");
if (ImGui::Checkbox("Use Custom Axis", &config.useCustomExplosionAxis))
{
if (!config.useCustomExplosionAxis)
{
*reinterpret_cast<bool *>(ImGui::GetIO().UserData) = true;
}
MC::applyExplosionAxisConfig(config);
}
if (config.useCustomExplosionAxis)
{
// custom X, Y, Z axis control
bool axisChanged = false;
axisChanged |= ImGui::DragFloat("Axis X", &config.customExplosionAxis.x, 0.01f, -1.0f, 1.0f);
axisChanged |= ImGui::DragFloat("Axis Y", &config.customExplosionAxis.y, 0.01f, -1.0f, 1.0f);
axisChanged |= ImGui::DragFloat("Axis Z", &config.customExplosionAxis.z, 0.01f, -1.0f, 1.0f);
if (axisChanged)
{
MC::applyExplosionAxisConfig(config);
}
// calculate the axis vector length
float length = std::sqrt(
config.customExplosionAxis.x * config.customExplosionAxis.x +
config.customExplosionAxis.y * config.customExplosionAxis.y +
config.customExplosionAxis.z * config.customExplosionAxis.z);
if (length > 0.0001f)
{
ImGui::Text("Normalized Axis: (%.2f, %.2f, %.2f)",
config.customExplosionAxis.x / length,
config.customExplosionAxis.y / length,
config.customExplosionAxis.z / length);
}
if (ImGui::Button("Apply Custom Axis", ImVec2(ImGui::GetWindowWidth() * 0.8f, 30)))
{
if (length > 0.0001f)
{
Vec3 normalizedAxis = {
config.customExplosionAxis.x / length,
config.customExplosionAxis.y / length,
config.customExplosionAxis.z / length};
config.customExplosionAxis = normalizedAxis;
MC::applyExplosionAxisConfig(config);
*reinterpret_cast<bool *>(ImGui::GetIO().UserData) = true;
ImGui::TextColored(ImVec4(0.0f, 0.6f, 0.0f, 1.0f), "Custom axis applied!");
}
else
{
MC::showError("Invalid Axis", "Cannot use a zero-length axis vector.\nPlease specify a valid direction.");
}
}
}
ImGui::Separator();
ImGui::Text("Strategy Selection");
bool strategyChanged = false;
if (ImGui::Combo("##Explosion Axis Strategy", ¤tIndex, strategyNames.data(), static_cast<int>(strategyNames.size())))
{
currentStrategy = strategies[currentIndex];
strategyChanged = (previousStrategy != currentStrategy);
}
if (strategyChanged)
{
ImGui::TextColored(ImVec4(0.8f, 0.4f, 0.0f, 1.0f),
"Strategy change will be applied when you click 'Recalculate'");
}
ImGui::Separator();
static bool errorShown = false;
bool shouldShowError = false;
std::string errorMessage;
if (previousStrategy == currentStrategy && !config.lastDetectionSuccessful)
{
ImGui::TextColored(ImVec4(0.8f, 0.0f, 0.0f, 1.0f),
"No symmetry detected with current strategy!");
ImGui::TextColored(ImVec4(0.8f, 0.4f, 0.0f, 1.0f),
"Using previous axis as fallback.");
ImGui::Separator();
shouldShowError = true;
errorMessage = "No symmetry detected with the current strategy!\n\n"
"Using previous axis as fallback.\n\n"
"Try adjusting the parameters or selecting a different strategy.";
}
if (currentStrategy == "Rotational Symmetry" && previousStrategy == currentStrategy)
{
ImGui::Text("Rotational Symmetry Parameters:");
if (!config.rotationalDetectionSuccessful && config.lastDetectionSuccessful)
{
ImGui::TextColored(ImVec4(0.8f, 0.4f, 0.0f, 1.0f),
"No rotational symmetry detected in last analysis.");
shouldShowError = true;
errorMessage = "No rotational symmetry detected in last analysis.\n\n"
"Try adjusting the sample count or symmetry order,\n"
"or switch to a different strategy.";
}
}
ImGui::Text("Explosion Axis Visibility");
ImGui::Checkbox("Show Explosion Axis", &showExplosionAxis);
if (shouldShowError && !errorShown)
{
MC::showError("Symmetry Detection Failed", errorMessage);
errorShown = true;
}
else if (!shouldShowError)
{
errorShown = false;
}
ImGui::Separator();
if (!config.useCustomExplosionAxis)
{
if (ImGui::Button("Recalculate Explosion Axis", ImVec2(ImGui::GetWindowWidth() * 0.8f, 30)))
{
if (previousStrategy != currentStrategy)
{
std::string newInternalStrategy = MC::ExplosionAxisConfig::convertToInternalName(currentStrategy);
config.rotationalDetectionSuccessful = true;
config.reflectiveDetectionSuccessful = true;
config.lastDetectionSuccessful = true;
config.strategyName = newInternalStrategy;
MC::setExplosionStrategy(newInternalStrategy);
MC::applyExplosionAxisConfig(config);
previousStrategy = currentStrategy;
}
*reinterpret_cast<bool *>(ImGui::GetIO().UserData) = true;
errorShown = false;
}
}
}
ImGui::End();
MC::renderErrorPopup();
}
void showError(const std::string &title, const std::string &message)
{
errorPopupTitle = title;
errorPopupMessage = message;
showErrorPopup = true;
}
// render error popup
void renderErrorPopup()
{
if (showErrorPopup)
{
ImVec2 center = ImGui::GetMainViewport()->GetCenter();
ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
ImGui::SetNextWindowSize(ImVec2(400, 0), ImGuiCond_Appearing);
if (ImGui::BeginPopupModal(errorPopupTitle.c_str(), &showErrorPopup,
ImGuiWindowFlags_AlwaysAutoResize))
{
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.0f, 0.0f, 0.0f, 1.0f)); // black text
ImGui::TextWrapped("%s", errorPopupMessage.c_str());
ImGui::PopStyleColor();
ImGui::Separator();
ImGui::SetCursorPosX((ImGui::GetWindowWidth() - 120) * 0.5f);
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.9f, 0.9f, 0.9f, 1.0f));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.8f, 0.8f, 0.8f, 1.0f));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.7f, 0.7f, 0.7f, 1.0f));
if (ImGui::Button("OK", ImVec2(120, 0)) ||
ImGui::IsKeyPressed(ImGuiKey_Escape) ||
ImGui::IsKeyPressed(ImGuiKey_Enter))
{
showErrorPopup = false;
ImGui::CloseCurrentPopup();
}
ImGui::PopStyleColor(3);
ImGui::EndPopup();
}
if (showErrorPopup)
{
ImGui::OpenPopup(errorPopupTitle.c_str());
}
}
}
void renderFrame(GLFWwindow *window, unsigned int shaderProgram, unsigned int VAO,
const Mesh &mesh, Camera &camera, float &isoLevel, float &tempIsoLevel,
const VolumeData &volumeData,
std::string ¤tExplosionStrategy, PostProcess &postProcessor,
unsigned int axisVAO,
unsigned int lineShaderProgram)
{
int width = 0;
int height = 0;
glBindFramebuffer(GL_FRAMEBUFFER, postProcessor.getFBO());
if (!updateViewport(window, &width, &height))
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return;
}
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
setupImGuiStyle();
ImGuiViewport *viewport = ImGui::GetMainViewport();
ImVec2 viewportSize = viewport->Size;
float panelWidth = 300.0f;
float panelSpacing = 10.0f;
float panel1Height = viewportSize.y * 0.2f;
float panel2Height = viewportSize.y * 0.5f;
float panel3Height = viewportSize.y * 0.2f;
// panel 1
if (panelFirstTime) {
ImGui::SetNextWindowPos(ImVec2(viewportSize.x - panelWidth - panelSpacing, panelSpacing));
ImGui::SetNextWindowSize(ImVec2(panelWidth, panel1Height));
}
ImGui::Begin("Marching Cubes Control", nullptr);
ImGui::Text("ISO level: %.1f to %.1f", volumeData.minValue, volumeData.maxValue);
if (ImGui::SliderFloat("##ISO Level (%)", &g_tempIsoLevelPercent, 0.0f, 100.0f, "%.1f%%"))
{
tempIsoLevel = volumeData.minValue + (g_tempIsoLevelPercent / 100.0f) * (volumeData.maxValue - volumeData.minValue);
}
if (ImGui::Button("Apply ISO"))
{
g_isoLevelPercent = g_tempIsoLevelPercent;
isoLevel = volumeData.minValue + (g_isoLevelPercent / 100.0f) * (volumeData.maxValue - volumeData.minValue);
}
ImGui::SameLine();
ImGui::Text("Current ISO: %.1f (%.1f%%)", isoLevel, g_isoLevelPercent);
ImGui::End();
// panel 2
if (panelFirstTime)
{
ImGui::SetNextWindowPos(ImVec2(viewportSize.x - panelWidth - panelSpacing, panelSpacing * 2 + panel1Height));
ImGui::SetNextWindowSize(ImVec2(panelWidth, panel2Height));
}
renderExplosionAxisGUI(currentExplosionStrategy);
// panel 3
if (panelFirstTime) {
ImGui::SetNextWindowPos(ImVec2(viewportSize.x - panelWidth - panelSpacing, viewportSize.y - panel3Height - panelSpacing));
ImGui::SetNextWindowSize(ImVec2(panelWidth, panel3Height));
}
ImGui::Begin("Explosion View Control", nullptr);
if (ImGui::Checkbox("Explode Model at Cutting Planes", &showIntersections))
{
std::cout << "Model explosion: " << (showIntersections ? "ON" : "OFF") << std::endl;
}
const float MAX_EXPLOSION_DISTANCE = 40.0f;
static bool firstRun = true;
if (firstRun)
{
tempExplosionPercent = g_explosionDistancePercent;
firstRun = false;
}
ImGui::Text("Explosion Distance");
if (showIntersections)
{
if (ImGui::SliderFloat("##Explosion Distance", &tempExplosionPercent, 0.0f, 100.0f, "%.1f%%"))
{
}
if (ImGui::Button("Apply Distance"))
{
g_explosionDistancePercent = tempExplosionPercent;
g_explosionDistance = (g_explosionDistancePercent / 100.0f) * MAX_EXPLOSION_DISTANCE;
}
ImGui::SameLine();
ImGui::Text("Current: %.1f%%", g_explosionDistancePercent);
}
else
{
ImGui::BeginDisabled();
ImGui::SliderFloat("##Explosion Distance", &tempExplosionPercent, 0.0f, 100.0f, "%.1f%%");
if (ImGui::Button("Apply Distance"))
{
}
ImGui::SameLine();
ImGui::Text("Current: %.1f%%", g_explosionDistancePercent);
ImGui::EndDisabled();
}
ImGui::End();
/* Duplicate
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());*/
float aspect = static_cast<float>(width) / static_cast<float>(height);
glUseProgram(shaderProgram);
glm::mat4 model(1.0f);
float scale_factor = 20.0f / mesh.max_dimension;
model = glm::scale(model, glm::vec3(scale_factor, scale_factor, scale_factor));
// center
glm::vec3 glmCenter(mesh.center.x, mesh.center.y, mesh.center.z);
model = glm::translate(model, -glmCenter);
glm::mat4 view(1.0f);
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -camera.distance * camera.zoom));
view = glm::rotate(view, glm::radians(camera.rotationX), glm::vec3(1.0f, 0.0f, 0.0f));
view = glm::rotate(view, glm::radians(camera.rotationY), glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 projection = glm::perspective(glm::radians(45.0f), aspect, 0.1f, 1000.0f);
// transform uniform
int modelLoc = glGetUniformLocation(shaderProgram, "model");
int viewLoc = glGetUniformLocation(shaderProgram, "view");
int projLoc = glGetUniformLocation(shaderProgram, "projection");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, (GLsizei)mesh.indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glUseProgram(lineShaderProgram);
// draw the axis only when showExplosionAxis is true
if (showExplosionAxis)
{
int axisModelLoc = glGetUniformLocation(lineShaderProgram, "model");
int axisViewLoc = glGetUniformLocation(lineShaderProgram, "view");
int axisProjLoc = glGetUniformLocation(lineShaderProgram, "projection");
glUniformMatrix4fv(axisModelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(axisViewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(axisProjLoc, 1, GL_FALSE, glm::value_ptr(projection));
glLineWidth(20.0f);
glBindVertexArray(axisVAO);
glDrawArrays(GL_LINES, 0, 2);
glBindVertexArray(0);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
if(!updateViewport(window, &width, &height))
{
return;
}
// post processor
postProcessor.render(width, height);
// render ImGui
ImGui::Render();
/* Duplicate
// draw the axis only when showExplosionAxis is true
if (showExplosionAxis)
{
glUseProgram(lineShaderProgram);
int axisModelLoc = glGetUniformLocation(lineShaderProgram, "model");
int axisViewLoc = glGetUniformLocation(lineShaderProgram, "view");
int axisProjLoc = glGetUniformLocation(lineShaderProgram, "projection");
glUniformMatrix4fv(axisModelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(axisViewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(axisProjLoc, 1, GL_FALSE, glm::value_ptr(projection));
glLineWidth(20.0f);
glBindVertexArray(axisVAO);
glDrawArrays(GL_LINES, 0, 2);
glBindVertexArray(0);
}*/
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
panelFirstTime = false;
}
void renderExplodedView(
GLFWwindow *window,
const ExplodedView &explodedView,
unsigned int shaderProgram,
const Mesh &mesh,
float &isoLevel,
float &tempIsoLevel,
const VolumeData &volumeData,
unsigned int axisVAO,
unsigned int lineShaderProgram,
PostProcess &postProcessor)
{
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
setupImGuiStyle();
ImGuiViewport *viewport = ImGui::GetMainViewport();
ImVec2 viewportSize = viewport->Size;
float panelWidth = 300.0f;
float panelSpacing = 10.0f;
float panel1Height = viewportSize.y * 0.2f;
float panel2Height = viewportSize.y * 0.5f;
float panel3Height = viewportSize.y * 0.2f;
// panel 1
if (panelFirstTime)
{
ImGui::SetNextWindowPos(ImVec2(viewportSize.x - panelWidth - panelSpacing, panelSpacing));
ImGui::SetNextWindowSize(ImVec2(panelWidth, panel1Height));
}
ImGui::Begin("Marching Cubes Control", nullptr);
ImGui::Text("ISO level: %.1f to %.1f", volumeData.minValue, volumeData.maxValue);
if (ImGui::SliderFloat("##ISO Level (%)", &g_tempIsoLevelPercent, 0.0f, 100.0f, "%.1f%%"))
{
tempIsoLevel = volumeData.minValue + (g_tempIsoLevelPercent / 100.0f) * (volumeData.maxValue - volumeData.minValue);
}
if (ImGui::Button("Apply ISO"))
{
g_isoLevelPercent = g_tempIsoLevelPercent;
isoLevel = volumeData.minValue + (g_isoLevelPercent / 100.0f) * (volumeData.maxValue - volumeData.minValue);
}
ImGui::SameLine();
ImGui::Text("Current ISO: %.1f (%.1f%%)", isoLevel, g_isoLevelPercent);
ImGui::End();
// panel 2
ImGui::SetNextWindowPos(ImVec2(viewportSize.x - panelWidth - panelSpacing, panelSpacing * 2 + panel1Height));
ImGui::SetNextWindowSize(ImVec2(panelWidth, panel2Height));
std::string currentStrategy = "";
renderExplosionAxisGUI(currentStrategy);
// panel 3
ImGui::SetNextWindowPos(ImVec2(viewportSize.x - panelWidth - panelSpacing, viewportSize.y - panel3Height - panelSpacing));
ImGui::SetNextWindowSize(ImVec2(panelWidth, panel3Height));
ImGui::Begin("Explosion View Control", nullptr);
if (ImGui::Checkbox("Explode Model at Cutting Planes", &showIntersections))
{
std::cout << "Model explosion: " << (showIntersections ? "ON" : "OFF") << std::endl;
}
const float MAX_EXPLOSION_DISTANCE = 40.0f;
ImGui::Text("Explosion Distance");
if (showIntersections)
{
float currentDistancePercent = (explodedView.explosionDistance / MAX_EXPLOSION_DISTANCE) * 100.0f;
static bool firstRun = true;
if (firstRun)
{
tempExplosionPercent = g_explosionDistancePercent;
firstRun = false;
}
if (ImGui::SliderFloat("##Explosion Distance", &tempExplosionPercent, 0.0f, 100.0f, "%.1f%%"))
{
}
if (ImGui::Button("Apply Distance"))
{
g_explosionDistancePercent = tempExplosionPercent;
g_explosionDistance = (tempExplosionPercent / 100.0f) * MAX_EXPLOSION_DISTANCE;
}
ImGui::SameLine();
ImGui::Text("Current: %.1f%%", currentDistancePercent);
}
else
{
ImGui::BeginDisabled();
ImGui::SliderFloat("##Explosion Distance", &tempExplosionPercent, 0.0f, 100.0f, "%.1f%%");
if (ImGui::Button("Apply Distance"))
{
}
ImGui::SameLine();
ImGui::Text("Current: %.1f%%", g_explosionDistancePercent);
ImGui::EndDisabled();
}
ImGui::End();
int width = 0;
int height = 0;
glBindFramebuffer(GL_FRAMEBUFFER, postProcessor.getFBO());
if (!updateViewport(window, &width, &height))
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return;
}
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float aspect = static_cast<float>(width) / static_cast<float>(height);
glm::mat4 model(1.0f);
float scale_factor = 20.0f / mesh.max_dimension;
model = glm::scale(model, glm::vec3(scale_factor, scale_factor, scale_factor));
// center
glm::vec3 glmCenter(mesh.center.x, mesh.center.y, mesh.center.z);
model = glm::translate(model, -glmCenter);
glm::mat4 view(1.0f);
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -camera.distance * camera.zoom));
view = glm::rotate(view, glm::radians(camera.rotationX), glm::vec3(1.0f, 0.0f, 0.0f));
view = glm::rotate(view, glm::radians(camera.rotationY), glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 projection = glm::perspective(glm::radians(45.0f), aspect, 0.1f, 1000.0f);
// render each fragment
if (explodedView.enabled)
{
for (const auto &segment : explodedView.segments)
{
if (segment.indices.empty() || segment.VAO == 0)
{
continue;
}
glUseProgram(shaderProgram);
glm::mat4 segmentModel = model;
segmentModel = glm::translate(segmentModel, glm::vec3(
segment.displacement.x,
segment.displacement.y,
segment.displacement.z));
int modelLoc = glGetUniformLocation(shaderProgram, "model");
int viewLoc = glGetUniformLocation(shaderProgram, "view");
int projLoc = glGetUniformLocation(shaderProgram, "projection");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(segmentModel));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
glBindVertexArray(segment.VAO);
glDrawElements(GL_TRIANGLES, segment.indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
}
if (showExplosionAxis)
{
glUseProgram(lineShaderProgram);
glUniformMatrix4fv(glGetUniformLocation(lineShaderProgram, "model"), 1, GL_FALSE, glm::value_ptr(model));