forked from lquatrin/cpp_volume_rendering
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathrenderingmanager.cpp
More file actions
1278 lines (1087 loc) · 39 KB
/
renderingmanager.cpp
File metadata and controls
1278 lines (1087 loc) · 39 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 "renderingmanager.h"
#include "defines.h"
#include <glm/gtc/type_ptr.hpp>
#include "volrenderbase.h"
#include <gl_utils/framebufferobject.h>
#include <volvis_utils/transferfunction1d.h>
#include <volvis_utils/utils.h>
#define USING_IM_EXT
#ifdef USING_IM_EXT
#include <im/im.h>
#include <im/im_image.h>
#endif
#include <math_utils/utils.h>
#include <vis_utils/colorutils.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/quaternion.hpp>
#include <iostream>
#include <iomanip>
#include <chrono>
#include <filesystem>
#include <GL/wglew.h>
#define ALWAYS_OUTDATE_THE_CURRENT_VR_RENDERER
#define RENDERING_MANAGER_MIN_VIEWPORT_SIZE 32
#define RENDERING_MANAGER_MAX_VIEWPORT_SIZE 8192
#define RENDERING_MANAGER_TIME_PER_FPS_COUNT_MS 5000.0
#define WHITE_BACKGROUND
//#define GET_NVIDIA_MEMORY
#ifdef GET_NVIDIA_MEMORY
#define GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX 0x9048
#define GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX 0x9049
#endif
double GetCurrentRenderTime()
{
#ifdef USING_FREEGLUT
return glutGet(GLUT_ELAPSED_TIME);
#elif USING_GLFW
return glfwGetTime();
#endif
}
RenderingManager *RenderingManager::crr_instance = 0;
RenderingManager* RenderingManager::Instance ()
{
if (!crr_instance)
crr_instance = new RenderingManager();
return crr_instance;
}
bool RenderingManager::Exists ()
{
return (crr_instance != NULL);
}
void RenderingManager::DestroyInstance ()
{
if (!RenderingManager::Exists())
return;
if (crr_instance)
{
delete crr_instance;
crr_instance = NULL;
}
}
// Init glew + camera + curr vol renderer
void RenderingManager::InitGL()
{
#ifdef USING_FREEGLUT
glEnable(GL_TEXTURE_2D);
glEnable(GL_TEXTURE_3D);
#else
#ifdef USING_GLFW
// Deprecated on Opengl 4x, does not work with glfw
//glEnable(GL_TEXTURE_2D);
//glEnable(GL_TEXTURE_3D);
#endif
#endif
glEnable(GL_DEPTH_TEST);
gl::ExitOnGLError("RenderingManager: Could not enable depth test...");
glEnable(GL_CULL_FACE);
gl::ExitOnGLError("RenderingManager: Could not enable cull face...");
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_ONE);
gl::ExitOnGLError("RenderingManager: Could not enable blend...");
#ifdef WHITE_BACKGROUND
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
#else
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
#endif
int max;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
//std::cout << max << std::endl;
}
void RenderingManager::AddVolumeRenderer (BaseVolumeRenderer* bvolrend)
{
m_vtr_vr_methods.push_back(bvolrend);
m_vtr_vr_ui_names.push_back(bvolrend->GetName());
}
// Init glew + camera + curr vol renderer
void RenderingManager::InitData ()
{
// Read Datasets and Transfer Functions from Data Manager
m_data_mgr.SetPathToData(MAKE_STR(CMAKE_PATH_TO_DATA_FOLDER));
m_data_mgr.ReadData();
// Read camera states and set first camera data
std::string path_data_folder1(MAKE_STR(CMAKE_PATH_TO_DATA_FOLDER));
m_camera_state_list.ReadCameraStates(path_data_folder1 + "#list_camera_states");
m_std_cam_state_names.clear();
for (int i = 0; i < m_camera_state_list.NumberOfCameraStates(); i++)
m_std_cam_state_names.push_back(m_camera_state_list.GetCameraState(i)->cam_setup_name);
m_current_camera_state_id = 0;
curr_rdr_parameters.GetCamera()->SetData(m_camera_state_list.GetCameraState(m_current_camera_state_id));
// Read light source lists
std::string path_data_folder2(MAKE_STR(CMAKE_PATH_TO_DATA_FOLDER));
m_light_source_list.ReadLightSourceLists(path_data_folder2 + "#list_light_sources");
m_std_lsource_names.clear();
for (int i = 0; i < m_light_source_list.NumberOfLists(); i++)
m_std_lsource_names.push_back(m_light_source_list.GetList(i)->l_name);
m_current_lightsource_data_id = glm::clamp(m_current_lightsource_data_id, 0, m_light_source_list.NumberOfLists() - 1);
curr_rdr_parameters.EraseAllLightSources();
for (int i = 0; i < m_light_source_list.GetList(m_current_lightsource_data_id)->m_lightsources.size(); i++)
curr_rdr_parameters.CreateNewLightSource(m_light_source_list.GetList(i)->m_lightsources[i]);
if (m_vtr_vr_methods.empty())
{
std::cout << "RenderingManager: No VR method added." << std::endl;
exit(1);
}
// Set auxiliar data parameter classes for each rendering mode
for (int i = 0; i < m_vtr_vr_methods.size(); i++)
m_vtr_vr_methods[i]->SetExternalResources(&m_data_mgr, &curr_rdr_parameters);
// Current rendering mode id (Null)
m_current_vr_method_id = 0;
// Update rendering mode
SetCurrentVolumeRenderer();
UpdateLightSourceCameraVectors();
// Reshape
Reshape(curr_rdr_parameters.GetScreenWidth(), curr_rdr_parameters.GetScreenHeight());
}
void RenderingManager::Display ()
{
//We always redraw during evaluation
if (m_eval_running)
{
curr_vol_renderer->SetOutdated();
}
else
{
//The frame rate display (console and ImGui) is only updated when not evaluating.
//Evaluation computes the frame rate after a given fixed number of frames.
UpdateFrameRate();
}
if (curr_rdr_parameters.GetCamera()->UpdatePositionAndRotations())
{
curr_vol_renderer->SetOutdated();
}
// Build ImgGui interface
if (m_imgui_render_ui) SetImGuiInterface();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// Render Function
if (curr_vol_renderer && curr_vol_renderer->IsBuilt())
{
curr_vol_renderer->PrepareRender(curr_rdr_parameters.GetCamera());
#ifdef MULTISAMPLE_AVAILABLE
f_render[curr_vol_renderer->GetCurrentMultiScalingMode()](this);
#else
curr_vol_renderer->Redraw();
#endif
}
// If we must capture a screenshot
if (curr_rdr_parameters.TakeScreenshot())
SaveScreenshot();
// Render the current ImGui interface
if (m_imgui_render_ui) DrawImGuiInterface();
#ifdef USING_FREEGLUT
// Swap buffer
f_swapbuffer(d_swapbuffer);
#else
#ifdef USING_GLFW
f_swapbuffer(d_swapbuffer);
#endif
#endif
// If camera is rotating
if (animate_camera_rotation)
{
//if (updating_world_light_pos)
//{
// curr_rdr_parameters.SetBlinnPhongLightingPosition(curr_rdr_parameters.GetCamera()->GetEye());
// UpdateLightSourceCameraVectors();
// curr_vol_renderer->SetOutdated();
//}
#ifdef USING_FREEGLUT
double endframetime = glutGet(GLUT_ELAPSED_TIME);
#else
#ifdef USING_GLFW
double endframetime = glfwGetTime() * 1000.0;
#endif
#endif
double lwindowms = endframetime - m_ts_last_time;
vis::CameraData sdata;
float radius = curr_rdr_parameters.GetCamera()->GetRadius();
sdata.center = glm::vec3(0.0f);
sdata.up = curr_rdr_parameters.GetCamera()->GetUp();
glm::vec3 cdir = -curr_rdr_parameters.GetCamera()->GetDir();
glm::vec3 cright = glm::normalize(glm::cross(curr_rdr_parameters.GetCamera()->GetDir(),
curr_rdr_parameters.GetCamera()->GetUp()));
cdir = RodriguesRotation(cdir, 0.0008f * ((float)lwindowms), sdata.up);
sdata.eye = glm::vec3(cdir.x, cdir.y, cdir.z) * radius;
curr_rdr_parameters.GetCamera()->SetData(&sdata);
curr_vol_renderer->SetOutdated();
}
if (m_eval_running)
{
//We shoot a number of frames for each evaluation sample
m_eval_currframe++; //go to the next frame
if (m_eval_currframe >= m_eval_numframes)
{
//Compute the rendering speed for that sample point
const double currenttime = GetCurrentRenderTime();
const double time_per_frame = (currenttime - m_eval_lasttime) / m_eval_numframes;
const double frames_per_second = 1000.0 / time_per_frame;
//Save the last rendered image
std::string imagefilename = std::to_string(m_eval_currsample);
size_t n_zero = 4;
imagefilename = std::string(n_zero - std::min(n_zero, imagefilename.length()), '0') + imagefilename + ".png";
SaveScreenshot(m_eval_imgdirectory + "/" + imagefilename);
//Store evaluation results in csv file
for(int i=0;i<m_eval_paramspace.GetNumDimensions();i++)
{
m_eval_csvfile << m_eval_paramspace.GetDimensionValue(i) << ",";
}
m_eval_csvfile << std::to_string(time_per_frame) << ","
<< std::to_string(frames_per_second) << ","
<< "\"" << imagefilename << "\"\n";
//We go to the next sample point in the parameter space.
if (m_eval_paramspace.IncrEvaluation())
{
//The ID of the new sample point
m_eval_currsample++;
//... and we shoot as many frames there as for the other samples.
m_eval_currframe = 0;
//Restart time taking
m_eval_lasttime = GetCurrentRenderTime();
}
else
{
//We reached the end of the evaluation.
m_eval_paramspace.EndEvaluation();
m_eval_running = false;
m_eval_csvfile.close();
//Enable or disable vsync according to user prefs
if (m_vsync)
{
wglSwapIntervalEXT(1);
}
else
{
wglSwapIntervalEXT(0);
}
//Restore UI
m_imgui_render_ui = true;
}
}
}
}
void RenderingManager::Reshape (int w, int h)
{
glViewport(0, 0, w, h);
curr_rdr_parameters.SetScreenSize(w, h);
curr_rdr_parameters.GetCamera()->UpdateAspectRatio(float(w), float(h));
if (curr_vol_renderer->IsBuilt())
{
curr_vol_renderer->Reshape(w,h);
curr_vol_renderer->SetOutdated();
}
PostRedisplay();
}
void RenderingManager::Keyboard (unsigned char key, int x, int y)
{
curr_rdr_parameters.GetCamera()->KeyboardDown(key, x, y);
PostRedisplay();
}
void RenderingManager::KeyboardUp(unsigned char key, int x, int y)
{
curr_rdr_parameters.GetCamera()->KeyboardUp(key, x, y);
if (key == 'i')
m_imgui_render_ui = !m_imgui_render_ui;
PostRedisplay();
}
void RenderingManager::MouseButton (int bt, int st, int x, int y)
{
curr_rdr_parameters.GetCamera()->MouseButton(bt, st, x, y);
PostRedisplay();
}
void RenderingManager::MouseMotion (int x, int y)
{
if (curr_rdr_parameters.GetCamera()->MouseMotion(x, y) == 1)
{
curr_vol_renderer->SetOutdated();
}
PostRedisplay();
}
void RenderingManager::MouseWheel(int wheel, int direction, int x, int y)
{
curr_rdr_parameters.GetCamera()->MouseWheel(wheel, direction, x, y);
PostRedisplay();
}
void RenderingManager::CloseFunc ()
{
gl::PipelineShader::Unbind();
gl::ArrayObject::Unbind();
for (int i = m_vtr_vr_methods.size() - 1; i >= 0; i--) delete m_vtr_vr_methods[i];
m_vtr_vr_methods.clear();
}
void RenderingManager::IdleFunc ()
{
if (m_idle_rendering)
{
#ifdef ALWAYS_OUTDATE_THE_CURRENT_VR_RENDERER
curr_vol_renderer->SetOutdated();
#endif
PostRedisplay();
}
}
void RenderingManager::PostRedisplay ()
{
#ifdef USING_FREEGLUT
glutPostRedisplay();
#else
#ifdef USING_GLFW
Display();
#endif
#endif
}
// Update the volume renderer with the current volume and transfer function
void RenderingManager::UpdateDataAndResetCurrentVRMode ()
{
curr_vol_renderer->Init(curr_rdr_parameters.GetScreenWidth(), curr_rdr_parameters.GetScreenHeight());
}
void RenderingManager::SaveScreenshot (std::string filename)
{
// Get pixel data without alpha
GLubyte* rgb_data = GetFrontBufferPixelData(false);
if (filename.empty())
{
filename = AddAbreviationName(curr_rdr_parameters.GetDefaultScreenshotName());
}
GenerateImgFile(filename, curr_rdr_parameters.GetScreenWidth(), curr_rdr_parameters.GetScreenHeight(), rgb_data, false);
delete[] rgb_data;
}
void RenderingManager::UpdateLightSourceCameraVectors ()
{
glm::vec3 cforward, cup, cright;
curr_rdr_parameters.GetCamera()->GetCameraVectors(&cforward, &cup, &cright);
curr_rdr_parameters.SetBlinnPhongLightSourceCameraVectors(cforward, cup, cright);
}
void RenderingManager::ResetGLStateConfig ()
{
#ifdef USING_FREEGLUT
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_ONE);
#endif
}
std::string RenderingManager::AddAbreviationName (std::string filename, std::string extension)
{
std::string str = filename;
std::size_t pos = str.find(".");
std::string out_str = str.substr(0, pos);
out_str = out_str + "_" + curr_vol_renderer->GetAbbreviationName() + extension;
return out_str;
}
bool RenderingManager::GenerateImgFile (std::string out_str, int w, int h, unsigned char *gl_data, bool alpha, std::string image_type)
{
int error;
#ifdef USING_IM_EXT
//Deal with absolute and relative paths
if (std::filesystem::path(out_str).is_relative())
{
std::string path_to_data = CPPVOLREND_DATA_DIR;
out_str = path_to_data + out_str;
}
//Create the file and save it
imFile* ifile = imFileNew(out_str.c_str(), image_type.c_str(), &error);
int user_color_mode = alpha ? IM_RGB | IM_ALPHA | IM_PACKED : IM_RGB | IM_PACKED;
error = imFileWriteImageInfo(ifile, w, h, user_color_mode, IM_BYTE);
error = imFileWriteImageData(ifile, gl_data);
imFileClose(ifile);
#else
error = -1;
#endif
return error == 0;
}
GLubyte* RenderingManager::GetFrontBufferPixelData (bool alpha)
{
GLubyte *gl_img_data = new GLubyte[(alpha ? 4 : 3) * curr_rdr_parameters.GetScreenWidth() * curr_rdr_parameters.GetScreenHeight()];
glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
glPushAttrib(GL_PIXEL_MODE_BIT);
glReadBuffer(GL_BACK);
glFlush();
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, curr_rdr_parameters.GetScreenWidth(), curr_rdr_parameters.GetScreenHeight(), (alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, gl_img_data);
glPopAttrib();
glPopClientAttrib();
return gl_img_data;
}
// Go to previous renderer
bool RenderingManager::PreviousRenderer ()
{
if (m_current_vr_method_id > 0)
{
ResetGLStateConfig();
m_current_vr_method_id -= 1;
SetCurrentVolumeRenderer();
return true;
}
return false;
}
// Go to next renderer
bool RenderingManager::NextRenderer ()
{
if (m_current_vr_method_id + 1 < m_vtr_vr_methods.size())
{
ResetGLStateConfig();
m_current_vr_method_id += 1;
SetCurrentVolumeRenderer();
return true;
}
return false;
}
// Set current volume renderer
void RenderingManager::SetCurrentVolumeRenderer ()
{
if (curr_vol_renderer) curr_vol_renderer->Clean();
curr_vol_renderer = m_vtr_vr_methods[m_current_vr_method_id];
if (curr_vol_renderer->GetDataTypeSupport() == m_data_mgr.GetInputVolumeDataType())
{
UpdateDataAndResetCurrentVRMode();
}
else
{
// Null Renderer
m_current_vr_method_id = 0;
curr_vol_renderer = m_vtr_vr_methods[m_current_vr_method_id];
UpdateDataAndResetCurrentVRMode();
}
//Let the volume renderer define some parameters for evaluation
curr_vol_renderer->FillParameterSpace(m_eval_paramspace);
}
void RenderingManager::SetImGuiInterface ()
{
#ifdef USING_IMGUI
// Start the Dear ImGui frame
#ifdef USING_FREEGLUT
ImGui_ImplOpenGL2_NewFrame();
ImGui_ImplGLUT_NewFrame();
#else
#ifdef USING_GLFW
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
#endif
#endif
ImGui::BeginMainMenuBar();
if (ImGui::BeginMenu("File###FileMenu"))
{
if (ImGui::MenuItem("Exit###FileMenuExit"))
{
CloseFunc();
exit(EXIT_SUCCESS);
}
ImGui::EndMenu();
}
if (ImGui::BeginMenu("View###ViewMenu"))
{
if (ImGui::MenuItem("Rendering Manager###ViewMenuRenderingManager"))
{
m_imgui_render_manager = true;
}
ImGui::Separator();
if (ImGui::MenuItem("Data Manager###ViewMenuDataManager"))
{
m_imgui_data_window = true;
}
ImGui::Separator();
if (ImGui::MenuItem("Render Modes###ViewMenuRendererManager"))
{
m_imgui_renderer_window = true;
}
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
// https://eliasdaler.github.io/using-imgui-with-sfml-pt2/#using-imgui-with-stl
static auto vector_getter = [](void* vec, int idx, const char** out_text)
{
auto& vector = *static_cast<std::vector<std::string>*>(vec);
if (idx < 0 || idx >= static_cast<int>(vector.size())) { return false; }
*out_text = vector.at(idx).c_str();
return true;
};
if (m_imgui_render_manager)
{
ImGui::Begin("Rendering Manager", &m_imgui_render_manager);
ImGui::BulletText("OpenGL Version %s", glGetString(GL_VERSION));
if (ImGui::Checkbox("VSync", &m_vsync))
{
if (m_vsync)
{
wglSwapIntervalEXT(1);
}
else
{
wglSwapIntervalEXT(0);
}
};
if (ImGui::Checkbox("Idle Redraw", &m_idle_rendering));
ImGui::SameLine();
ImGui::Text("- %.3f ms/frame (%.1f FPS)", m_ts_window_ms, m_ts_window_fps);
if (ImGui::Checkbox("Rotate Camera", &animate_camera_rotation))
{
if (animate_camera_rotation) m_idle_rendering = true;
}
if (ImGui::Button("Save Screenshot"))
{
curr_rdr_parameters.SetDefaultScreenshotName("output.png");
curr_rdr_parameters.RequestSaveScreenshot();
}
ImGui::Separator();
if (ImGui::Button("Reference Image###BTNstorerefimage"))
{
s_ref_image.clear();
glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT);
int o_w = curr_rdr_parameters.GetScreenWidth(),
o_h = curr_rdr_parameters.GetScreenHeight();
s_ref_image.resize(o_w * o_h);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, curr_vol_renderer->GetScreenTextureID());
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, &s_ref_image[0]);
glBindTexture(GL_TEXTURE_2D, 0);
glPopAttrib();
}
ImGui::SameLine();
if (ImGui::Button("Generate Diff###BTNgeneratediffimage"))
{
glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT);
printf("Generate diff image\n");
int o_w = curr_rdr_parameters.GetScreenWidth(),
o_h = curr_rdr_parameters.GetScreenHeight();
vis::TransferFunction1D tf1d;
tf1d.AddAlphaControlPoint(vis::TransferControlPoint(1.0, 0));
tf1d.AddAlphaControlPoint(vis::TransferControlPoint(1.0, 255));
//tf1d.AddRGBControlPoint(vis::TransferControlPoint(1.0, 1.0, 1.0, 0.0));
//tf1d.AddRGBControlPoint(vis::TransferControlPoint(1.0, 0.0, 0.0, (0.10 * 255.0)));
//tf1d.AddRGBControlPoint(vis::TransferControlPoint(0.0, 1.0, 0.0, (0.55 * 255.0)));
//tf1d.AddRGBControlPoint(vis::TransferControlPoint(0.0, 0.0, 1.0, (1.00 * 255.0)));
tf1d.AddRGBControlPoint(vis::TransferControlPoint(1.0, 1.0, 1.0, 0.0));
tf1d.AddRGBControlPoint(vis::TransferControlPoint(1.0, 0.0, 0.0, (0.40 * 255.0)));
tf1d.AddRGBControlPoint(vis::TransferControlPoint(1.0, 0.0, 0.0, (1.00 * 255.0)));
tf1d.Build();
//if (s_tex_diff_ref_screen)
{
double max_c_alpha = 0.0;
//GLfloat* refe_rgba_data = new GLfloat[o_w * o_h * 4];
glActiveTexture(GL_TEXTURE0);
//glBindTexture(GL_TEXTURE_2D, s_tex_diff_ref_screen->GetTextureID());
//glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, refe_rgba_data);
//glBindTexture(GL_TEXTURE_2D, 0);
GLfloat* curr_rgba_data = new GLfloat[o_w * o_h * 4];
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, curr_vol_renderer->GetScreenTextureID());
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, curr_rgba_data);
glBindTexture(GL_TEXTURE_2D, 0);
GLubyte* ext_rgb_data = new GLubyte[o_w * o_h * 3];
for (int i = 0; i < o_w * o_h; i++)
{
double rgb_ref[3];
rgb_ref[0] = s_ref_image[i].x * 255.0;
rgb_ref[1] = s_ref_image[i].y * 255.0;
rgb_ref[2] = s_ref_image[i].z * 255.0;
double rgb_cur[3];
rgb_cur[0] = curr_rgba_data[(i * 4) + 0] * 255.0;
rgb_cur[1] = curr_rgba_data[(i * 4) + 1] * 255.0;
rgb_cur[2] = curr_rgba_data[(i * 4) + 2] * 255.0;
double diff_lab = Cie2000Comparison(rgb_ref, rgb_cur);
//double c_alpha_r = glm::clamp((Cie2000Comparison(rgb_ref, rgb_cur) / 100.0) / s_diff_max_c_alpha, 0.0, 1.0);
double c_alpha_r = glm::clamp((diff_lab / 100.0), 0.0, 1.0);
glm::vec4 clr = tf1d.Get(c_alpha_r * 255.0);
ext_rgb_data[(i * 3) + 0] = clr.x * 255.0;
ext_rgb_data[(i * 3) + 1] = clr.y * 255.0;
ext_rgb_data[(i * 3) + 2] = clr.z * 255.0;
}
std::string fl = "";
fl.append("Image");
fl.append("_diff");
fl.append(".png");
GenerateImgFile(fl, o_w, o_h, ext_rgb_data, false, "PNG");
//delete[] refe_rgba_data;
delete[] ext_rgb_data;
delete[] curr_rgba_data;
}
glPopAttrib();
}
ImGui::Separator();
if (ImGui::Button("Export LAB Diff Transfer Function Image###BTNexportimagetfdiff"))
{
int img_w = 256, img_h = 40;
GLubyte* tf_rgb_img = new GLubyte[img_w * img_h * 3];
vis::TransferFunction1D tf1d;
tf1d.AddAlphaControlPoint(vis::TransferControlPoint(1.0, 0));
tf1d.AddAlphaControlPoint(vis::TransferControlPoint(1.0, 255));
tf1d.AddRGBControlPoint(vis::TransferControlPoint(1.0, 1.0, 1.0, 0.0));
tf1d.AddRGBControlPoint(vis::TransferControlPoint(1.0, 0.0, 0.0, (0.40 * 255.0)));
tf1d.AddRGBControlPoint(vis::TransferControlPoint(1.0, 0.0, 0.0, (1.00 * 255.0)));
tf1d.Build();
for (int i = 0; i < img_w; i++)
{
glm::vec4 clr = tf1d.Get((double(i) / double(img_w)) * 255.0);
for (int j = 0; j < img_h; j++)
{
int idx = (i + (j * img_w)) * 3;
tf_rgb_img[idx + 0] = clr.r * 255.0;
tf_rgb_img[idx + 1] = clr.g * 255.0;
tf_rgb_img[idx + 2] = clr.b * 255.0;
}
}
{
std::string out_tf_str = "transfer_function_diff.png";
GenerateImgFile(
out_tf_str,
img_w, img_h,
tf_rgb_img,
false
);
}
delete[] tf_rgb_img;
}
if (ImGui::Button("Export Transfer Function Image###BTNexportimagetf"))
{
int img_w = 256, img_h = 40;
GLubyte* tf_rgb_img = new GLubyte[img_w * img_h * 3];
for (int i = 0; i < img_w; i++)
{
glm::vec4 clr = m_data_mgr.GetCurrentTransferFunction()->Get((double(i) / double(img_w)) * 255.0);
for (int j = 0; j < img_h; j++)
{
int idx = (i + (j * img_w)) * 3;
tf_rgb_img[idx + 0] = clr.r * 255.0;
tf_rgb_img[idx + 1] = clr.g * 255.0;
tf_rgb_img[idx + 2] = clr.b * 255.0;
}
}
{
std::string out_tf_str = "transfer_function.png";
GenerateImgFile(
out_tf_str,
img_w, img_h,
tf_rgb_img,
false
);
}
delete[] tf_rgb_img;
}
ImGui::PushID("Viewport Data");
ImGui::Text("- Viewport:");
int v_size[2] = { curr_rdr_parameters.GetScreenWidth(), curr_rdr_parameters.GetScreenHeight() };
if (ImGui::DragInt2("###WINDOWVIEWPORT", v_size, 1, RENDERING_MANAGER_MIN_VIEWPORT_SIZE, RENDERING_MANAGER_MAX_VIEWPORT_SIZE))
{
#ifdef USING_FREEGLUT
glutReshapeWindow(v_size[0], v_size[1]);
#else
#ifdef USING_GLFW
#endif
#endif
curr_vol_renderer->SetOutdated();
}
ImGui::PopID();
if (ImGui::CollapsingHeader("Evaluation###EvaluationHeader"))
{
const int numsamples = m_eval_paramspace.GetNumSamplePoints();
ImGui::Text("%d parameters with %d total samples", m_eval_paramspace.GetNumDimensions(), numsamples);
ImGui::PushItemWidth(100);
ImGui::InputInt("Eval Frames per Sample", &m_eval_numframes, 1, 10);
ImGui::PopItemWidth();
m_eval_numframes = std::max(std::min(m_eval_numframes, 500), 1);
const double neededtime = ceil(m_ts_window_ms * numsamples * m_eval_numframes / 1000.);
ImGui::Text("approx. %.0f seconds for evaluation at current FPS", neededtime);
if (ImGui::Button("Start Evaluation"))
{
//Name of a directory containing all the evaluation files - naming is datetime-based
auto t = std::time(nullptr);
auto tm = *std::localtime(&t);
std::ostringstream oss;
oss << std::put_time(&tm, "eval_%d-%m-%Y_%H-%M-%S");
const std::string path_to_data = CPPVOLREND_DATA_DIR;
m_eval_basedirectory = path_to_data + oss.str();
// - and an image subsirectory
m_eval_imgdirectory = m_eval_basedirectory + "/img";
//Create the new directories
std::filesystem::create_directory(m_eval_basedirectory);
std::filesystem::create_directory(m_eval_imgdirectory);
//Open a CSV file to record the results of the evaluation.
if (m_eval_csvfile.is_open()) m_eval_csvfile.close(); //Should really not happen, but better be save.
m_eval_csvfile.open(m_eval_basedirectory + "/eval.csv", std::ios_base::out);
//Start the evaluation if everything is fine
if (m_eval_csvfile.is_open())
{
//Reset parameter space to the beginning
m_eval_paramspace.StartEvaluation();
//Initialize the csv file
for(int i=0;i<m_eval_paramspace.GetNumDimensions();i++)
{
m_eval_csvfile << m_eval_paramspace.GetDimensionName(i) << ",";
}
m_eval_csvfile << "TimePerFrame (ms),FramesPerSecond,ImageFile\n";
//Set a bool to trigger evaluation action in Display().
m_eval_running = true;
m_eval_currframe = 0;
m_eval_currsample = 0;
m_eval_lasttime = GetCurrentRenderTime();
curr_vol_renderer->SetOutdated();
// Careful: Not rendering the ImGui may have unintended consequences,
// namely if they Gui code changes parameters based on the parameters
// that we are setting during the eval.
// On the other hand, we want to measure the speed of the volume renderer and not of ImGui.
m_imgui_render_ui = false;
//Disable VSync for full speed
wglSwapIntervalEXT(0);
}
}
}
//int u_cam_beha = m_camera.GetCameraBehaviour();
if(ImGui::CollapsingHeader("Camera###CameraSettingsHeader"))
{
// std::vector<std::string> vec_cam_states;
ImGui::Text("- Load Camera State: ");
if(ImGui::Combo("###ImArrayLoadCurrentCameraState", &m_current_camera_state_id, vector_getter,
static_cast<void*>(&m_std_cam_state_names), m_std_cam_state_names.size()))
{
curr_rdr_parameters.GetCamera()->SetData(m_camera_state_list.GetCameraState(m_current_camera_state_id));
curr_vol_renderer->SetOutdated();
}
// ImGui::Text("- Behaviour: ");
// static const char* items_camera_behaviours[]{
// "Flight",
// "ArcBall",
// };
// if(ImGui::Combo("###CurrentCameraBehaviour", &u_cam_beha,
// items_camera_behaviours, IM_ARRAYSIZE(items_camera_behaviours)))
// {
// m_camera.SetCameraBehaviour((vis::Camera::CAMERA_BEHAVIOUR)u_cam_beha);
// }
}
if (ImGui::CollapsingHeader("Light Sources###LightSourceManagerHeader"))
{
ImGui::Text("- Light Source List:");
if (ImGui::Combo("###CurrentLightSourcePositionData", &m_current_lightsource_data_id, vector_getter,
static_cast<void*>(&m_std_lsource_names), m_std_lsource_names.size()))
{
//updating_world_light_pos = false;
curr_rdr_parameters.EraseAllLightSources();
for (int i = 0; i < m_light_source_list.GetList(m_current_lightsource_data_id)->m_lightsources.size(); i++)
{
vis::LightSourceData lsd;
lsd.position = m_light_source_list.GetList(m_current_lightsource_data_id)->m_lightsources[i].position;
lsd.z_axis = m_light_source_list.GetList(m_current_lightsource_data_id)->m_lightsources[i].z_axis;
lsd.y_axis = m_light_source_list.GetList(m_current_lightsource_data_id)->m_lightsources[i].y_axis;
lsd.x_axis = m_light_source_list.GetList(m_current_lightsource_data_id)->m_lightsources[i].x_axis;
curr_rdr_parameters.CreateNewLightSource(lsd);
}
curr_vol_renderer->SetOutdated();
}
if (ImGui::Button("New Light Source###RMANAGERADDLIGHTSOURCE"))
{
curr_rdr_parameters.CreateNewLightSource();
curr_vol_renderer->SetOutdated();
}
int remove_light_source = -1;
for (int i = 0; i < curr_rdr_parameters.GetNumberOfLightSources(); i++)
{
ImGui::Separator();
ImGui::PushItemWidth(60.0);
vis::LightSourceData* lsd = curr_rdr_parameters.GetLightSourceData(i);
std::string lsourcepath = std::to_string(i + 1);
ImGui::Text(lsourcepath.c_str());
ImGui::SameLine();
std::string lsp_bt_cam = "Copy From Camera###RMLightSourceFromCamerabt";
lsp_bt_cam.append(lsourcepath);
if (ImGui::Button(lsp_bt_cam.c_str()))
{
lsd->position = curr_rdr_parameters.GetCamera()->GetEye();
lsd->z_axis = -curr_rdr_parameters.GetCamera()->GetDir();
lsd->y_axis = curr_rdr_parameters.GetCamera()->GetYAxis();
lsd->x_axis = curr_rdr_parameters.GetCamera()->GetXAxis();
curr_vol_renderer->SetOutdated();
}
ImGui::SameLine();
std::string lsp_bt = "x###RMLightSourceParameterbt";
lsp_bt.append(lsourcepath);
if (ImGui::Button(lsp_bt.c_str()))
{
curr_vol_renderer->SetOutdated();
remove_light_source = i;
}
{
ImGui::BulletText("Position: ");
ImGui::SameLine();
std::string lsp_v1 = "###RMLightSourceParameterv1";
lsp_v1.append(lsourcepath);
if (ImGui::DragFloat(lsp_v1.c_str(), &lsd->position.x, 0.5f, -FLT_MAX, FLT_MAX, "%.1f"))
{
curr_vol_renderer->SetOutdated();
}
ImGui::SameLine();
std::string lsp_v2 = "###RMLightSourceParameterv2";
lsp_v2.append(lsourcepath);
if (ImGui::DragFloat(lsp_v2.c_str(), &lsd->position.y, 0.5f, -FLT_MAX, FLT_MAX, "%.1f"))
{
curr_vol_renderer->SetOutdated();
}
ImGui::SameLine();
std::string lsp_v3 = "###RMLightSourceParameterv3";
lsp_v3.append(lsourcepath);
if (ImGui::DragFloat(lsp_v3.c_str(), &lsd->position.z, 0.5f, -FLT_MAX, FLT_MAX, "%.1f"))
{
curr_vol_renderer->SetOutdated();
}
}
ImGui::BulletText("Energy Density: ");
ImGui::SameLine();
std::string lsp_ed = "###RMLightSourceParameterEnergyDensity";
lsp_ed.append(lsourcepath);
if (ImGui::DragFloat(lsp_ed.c_str(), &lsd->energy_density, 0.1f, 0.0f, FLT_MAX, "%.1f"))
{
curr_vol_renderer->SetOutdated();
}
{
ImGui::BulletText("Color: ");
ImGui::SameLine();
std::string lsp_clr_v1 = "###RMLightSourceParameterColorv1";
lsp_clr_v1.append(lsourcepath);
if (ImGui::DragFloat(lsp_clr_v1.c_str(), &lsd->color.x, 0.01f, 0.0f, 1.0f, "%.2f"))
{
curr_vol_renderer->SetOutdated();