-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_volume_raycaster.cpp
1041 lines (815 loc) · 36 KB
/
my_volume_raycaster.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
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
// -----------------------------------------------------------------------------
// Copyright : (C) 2014 Andreas-C. Bernstein
// 2015 Sebastian Thiele
// License : MIT (see the file LICENSE)
// Maintainer : Sebastian Thiele <[email protected]>
// Stability : experimantal exercise
//
// scivis exercise Example
// -----------------------------------------------------------------------------
#ifdef _MSC_VER
#pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen
#endif
#define _USE_MATH_DEFINES
#include "fensterchen.hpp"
#include <string>
#include <iostream>
#include <sstream> // std::stringstream
#include <fstream>
#include <algorithm>
#include <stdexcept>
#include <cmath>
///GLM INCLUDES
#define GLM_FORCE_RADIANS
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/matrix_access.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtx/norm.hpp>
///PROJECT INCLUDES
#include <volume_loader_raw.hpp>
#include <transfer_function.hpp>
#include <utils.hpp>
#include <turntable.hpp>
#include <imgui.h>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h> // stb_image.h for PNG loading
#define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
///IMGUI INCLUDES
#include <imgui_impl_glfw_gl3.h>
//-----------------------------------------------------------------------------
// Helpers
//-----------------------------------------------------------------------------
#define IM_ARRAYSIZE(_ARR) ((int)(sizeof(_ARR)/sizeof(*_ARR)))
#undef PI
const float PI = 3.14159265358979323846f;
#ifdef INT_MAX
#define IM_INT_MAX INT_MAX
#else
#define IM_INT_MAX 2147483647
#endif
// Play it nice with Windows users. Notepad in 2014 still doesn't display text data with Unix-style \n.
#ifdef _MSC_VER
#define STR_NEWLINE "\r\n"
#else
#define STR_NEWLINE "\n"
#endif
const std::string g_file_vertex_shader("../../../source/shader/volume.vert");
const std::string g_file_fragment_shader("../../../source/shader/volume.frag");
const std::string g_GUI_file_vertex_shader("../../../source/shader/pass_through_GUI.vert");
const std::string g_GUI_file_fragment_shader("../../../source/shader/pass_through_GUI.frag");
GLuint loadShaders(std::string const& vs, std::string const& fs)
{
std::string v = readFile(vs);
std::string f = readFile(fs);
return createProgram(v, f);
}
GLuint loadShaders(
std::string const& vs,
std::string const& fs,
const int task_nbr,
const int enable_lighting,
const int enable_shadowing,
const int enable_opeacity_cor)
{
std::string v = readFile(vs);
std::string f = readFile(fs);
std::stringstream ss1;
ss1 << task_nbr;
int index = (int)f.find("#define TASK");
f.replace(index + 13, 2, ss1.str());
std::stringstream ss2;
ss2 << enable_opeacity_cor;
index = (int)f.find("#define ENABLE_OPACITY_CORRECTION");
f.replace(index + 34, 1, ss2.str());
std::stringstream ss3;
ss3 << enable_lighting;
index = (int)f.find("#define ENABLE_LIGHTNING");
f.replace(index + 25, 1, ss3.str());
std::stringstream ss4;
ss4 << enable_shadowing;
index = (int)f.find("#define ENABLE_SHADOWING");
f.replace(index + 25, 1, ss4.str());
//std::cout << f << std::endl;
return createProgram(v, f);
}
Turntable g_turntable;
///SETUP VOLUME RAYCASTER HERE
// set the volume file
std::string g_file_string = "../../../data/head_w256_h256_d225_c1_b8.raw";
// set the sampling distance for the ray traversal
float g_sampling_distance = 0.001f;
float g_sampling_distance_fact = 0.5f;
float g_sampling_distance_fact_move = 2.0f;
float g_sampling_distance_fact_ref = 1.0f;
float g_iso_value = 0.2f;
// set the light position and color for shading
// set the light position and color for shading
glm::vec3 g_light_pos = glm::vec3(10.0, 10.0, 10.0);
glm::vec3 g_ambient_light_color = glm::vec3(0.1f, 0.1f, 0.1f);
glm::vec3 g_diffuse_light_color = glm::vec3(0.2f, 0.2f, 0.2f);
glm::vec3 g_specula_light_color = glm::vec3(0.2f, 0.2f, 0.2f);
float g_ref_coef = 12.0;
// set backgorund color here
//glm::vec3 g_background_color = glm::vec3(1.0f, 1.0f, 1.0f); //white
glm::vec3 g_background_color = glm::vec3(0.08f, 0.08f, 0.08f); //grey
glm::ivec2 g_window_res = glm::ivec2(1600, 800);
Window g_win(g_window_res);
// Volume Rendering GLSL Program
GLuint g_volume_program(0);
std::string g_error_message;
bool g_reload_shader_error = false;
Transfer_function g_transfer_fun;
int g_current_tf_data_value = 0;
GLuint g_transfer_texture;
bool g_transfer_dirty = true;
bool g_redraw_tf = true;
bool g_lighting_toggle = false;
bool g_shadow_toggle = false;
bool g_opacity_correction_toggle = false;
// imgui variables
static bool g_show_gui = true;
static bool mousePressed[2] = { false, false };
bool g_show_transfer_function_in_window = false;
glm::vec2 g_transfer_function_pos = glm::vec2(0.0f);
glm::vec2 g_transfer_function_size = glm::vec2(0.0f);
//imgui values
bool g_over_gui = false;
bool g_reload_shader = false;
bool g_reload_shader_pressed = false;
bool g_show_transfer_function = false;
int g_task_chosen = 10;
int g_task_chosen_old = g_task_chosen;
bool g_pause = false;
Volume_loader_raw g_volume_loader;
volume_data_type g_volume_data;
glm::ivec3 g_vol_dimensions;
glm::vec3 g_max_volume_bounds;
unsigned g_channel_size = 0;
unsigned g_channel_count = 0;
GLuint g_volume_texture = 0;
Cube g_cube;
int g_bilinear_interpolation = true;
bool first_frame = true;
struct Manipulator
{
Manipulator()
: m_turntable()
, m_mouse_button_pressed(0, 0, 0)
, m_mouse(0.0f, 0.0f)
, m_lastMouse(0.0f, 0.0f)
{}
glm::mat4 matrix()
{
m_turntable.rotate(m_slidelastMouse, m_slideMouse);
return m_turntable.matrix();
}
glm::mat4 matrix(Window const& g_win)
{
m_mouse = g_win.mousePosition();
if (ImGui::IsMouseDown(GLFW_MOUSE_BUTTON_LEFT)) {
if (!m_mouse_button_pressed[0]) {
m_mouse_button_pressed[0] = 1;
}
m_turntable.rotate(m_lastMouse, m_mouse);
m_slideMouse = m_mouse;
m_slidelastMouse = m_lastMouse;
}
else {
m_mouse_button_pressed[0] = 0;
m_turntable.rotate(m_slidelastMouse, m_slideMouse);
//m_slideMouse *= 0.99f;
//m_slidelastMouse *= 0.99f;
}
if (ImGui::IsMouseDown(GLFW_MOUSE_BUTTON_MIDDLE)) {
if (!m_mouse_button_pressed[1]) {
m_mouse_button_pressed[1] = 1;
}
m_turntable.pan(m_lastMouse, m_mouse);
}
else {
m_mouse_button_pressed[1] = 0;
}
if (ImGui::IsMouseDown(GLFW_MOUSE_BUTTON_RIGHT)) {
if (!m_mouse_button_pressed[2]) {
m_mouse_button_pressed[2] = 1;
}
m_turntable.zoom(m_lastMouse, m_mouse);
}
else {
m_mouse_button_pressed[2] = 0;
}
m_lastMouse = m_mouse;
return m_turntable.matrix();
}
private:
Turntable m_turntable;
glm::ivec3 m_mouse_button_pressed;
glm::vec2 m_mouse;
glm::vec2 m_lastMouse;
glm::vec2 m_slideMouse;
glm::vec2 m_slidelastMouse;
};
bool read_volume(std::string& volume_string){
//init volume g_volume_loader
//Volume_loader_raw g_volume_loader;
//read volume dimensions
g_vol_dimensions = g_volume_loader.get_dimensions(g_file_string);
g_sampling_distance = 1.0f / glm::max(glm::max(g_vol_dimensions.x, g_vol_dimensions.y), g_vol_dimensions.z);
unsigned max_dim = std::max(std::max(g_vol_dimensions.x,
g_vol_dimensions.y),
g_vol_dimensions.z);
// calculating max volume bounds of volume (0.0 .. 1.0)
g_max_volume_bounds = glm::vec3(g_vol_dimensions) / glm::vec3((float)max_dim);
// loading volume file data
g_volume_data = g_volume_loader.load_volume(g_file_string);
g_channel_size = g_volume_loader.get_bit_per_channel(g_file_string) / 8;
g_channel_count = g_volume_loader.get_channel_count(g_file_string);
// setting up proxy geometry
g_cube.freeVAO();
g_cube = Cube(glm::vec3(0.0, 0.0, 0.0), g_max_volume_bounds);
glActiveTexture(GL_TEXTURE0);
g_volume_texture = createTexture3D(g_vol_dimensions.x, g_vol_dimensions.y, g_vol_dimensions.z, g_channel_size, g_channel_count, (char*)&g_volume_data[0]);
return 0 != g_volume_texture;
}
void UpdateImGui()
{
ImGuiIO& io = ImGui::GetIO();
// Setup resolution (every frame to accommodate for window resizing)
int w, h;
int display_w, display_h;
glfwGetWindowSize(g_win.getGLFWwindow(), &w, &h);
glfwGetFramebufferSize(g_win.getGLFWwindow(), &display_w, &display_h);
io.DisplaySize = ImVec2((float)display_w, (float)display_h); // Display size, in pixels. For clamping windows positions.
// Setup time step
static double time = 0.0f;
const double current_time = glfwGetTime();
io.DeltaTime = (float)(current_time - time);
time = current_time;
// Setup inputs
// (we already got mouse wheel, keyboard keys & characters from glfw callbacks polled in glfwPollEvents())
double mouse_x, mouse_y;
glfwGetCursorPos(g_win.getGLFWwindow(), &mouse_x, &mouse_y);
mouse_x *= (float)display_w / w; // Convert mouse coordinates to pixels
mouse_y *= (float)display_h / h;
io.MousePos = ImVec2((float)mouse_x, (float)mouse_y); // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.)
io.MouseDown[0] = mousePressed[0] || glfwGetMouseButton(g_win.getGLFWwindow(), GLFW_MOUSE_BUTTON_LEFT) != 0; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
io.MouseDown[1] = mousePressed[1] || glfwGetMouseButton(g_win.getGLFWwindow(), GLFW_MOUSE_BUTTON_RIGHT) != 0;
// Start the frame
//ImGui::NewFrame();
ImGui_ImplGlfwGL3_NewFrame();
}
void showGUI(){
ImGui::Begin("Volume Settings", &g_show_gui, ImVec2(300, 500));
static float f;
g_over_gui = ImGui::IsMouseHoveringAnyWindow();
// Calculate and show frame rate
static ImVector<float> ms_per_frame; if (ms_per_frame.empty()) { ms_per_frame.resize(400); memset(&ms_per_frame.front(), 0, ms_per_frame.size()*sizeof(float)); }
static int ms_per_frame_idx = 0;
static float ms_per_frame_accum = 0.0f;
if (!g_pause){
ms_per_frame_accum -= ms_per_frame[ms_per_frame_idx];
ms_per_frame[ms_per_frame_idx] = ImGui::GetIO().DeltaTime * 1000.0f;
ms_per_frame_accum += ms_per_frame[ms_per_frame_idx];
ms_per_frame_idx = (ms_per_frame_idx + 1) % ms_per_frame.size();
}
const float ms_per_frame_avg = ms_per_frame_accum / 120;
if (ImGui::CollapsingHeader("Task", 0, true, true))
{
if (ImGui::TreeNode("Introduction")){
ImGui::RadioButton("Max Intensity Projection", &g_task_chosen, 10);
ImGui::RadioButton("Average Intensity Projection", &g_task_chosen, 11);
ImGui::TreePop();
}
if (ImGui::TreeNode("Iso Surface Rendering")){
ImGui::RadioButton("Inaccurate", &g_task_chosen, 12);
ImGui::RadioButton("Binary Search", &g_task_chosen, 13);
ImGui::SliderFloat("Iso Value", &g_iso_value, 0.0f, 1.0f, "%.8f", 1.0f);
ImGui::TreePop();
}
if (ImGui::TreeNode("Direct Volume Rendering")){
ImGui::RadioButton("Compositing", &g_task_chosen, 31);
ImGui::TreePop();
}
g_reload_shader ^= ImGui::Checkbox("1", &g_lighting_toggle); ImGui::SameLine();
g_task_chosen == 31 || g_task_chosen == 12 || g_task_chosen == 13 ? ImGui::Text("Enable Lighting") : ImGui::TextColored(ImVec4(0.2f, 0.2f, 0.2f, 0.5f), "Enable Lighting");
g_reload_shader ^= ImGui::Checkbox("2", &g_shadow_toggle); ImGui::SameLine();
g_task_chosen == 31 || g_task_chosen == 12 || g_task_chosen == 13 ? ImGui::Text("Enable Shadows") : ImGui::TextColored(ImVec4(0.2f, 0.2f, 0.2f, 0.5f), "Enable Shadows");
g_reload_shader ^= ImGui::Checkbox("3", &g_opacity_correction_toggle); ImGui::SameLine();
g_task_chosen == 31 ? ImGui::Text("Opacity Correction") : ImGui::TextColored(ImVec4(0.2f, 0.2f, 0.2f, 0.5f), "Opacity Correction");
if (g_task_chosen != g_task_chosen_old){
g_reload_shader = true;
g_task_chosen_old = g_task_chosen;
}
}
if (ImGui::CollapsingHeader("Load Volumes", 0, true, false))
{
bool load_volume_1 = false;
bool load_volume_2 = false;
bool load_volume_3 = false;
ImGui::Text("Volumes");
load_volume_1 ^= ImGui::Button("Load Volume Head");
load_volume_2 ^= ImGui::Button("Load Volume Engine");
load_volume_3 ^= ImGui::Button("Load Volume Bucky");
if (load_volume_1){
g_file_string = "../../../data/head_w256_h256_d225_c1_b8.raw";
read_volume(g_file_string);
}
if (load_volume_2){
g_file_string = "../../../data/Engine_w256_h256_d256_c1_b8.raw";
read_volume(g_file_string);
}
if (load_volume_3){
g_file_string = "../../../data/Bucky_uncertainty_data_w32_h32_d32_c1_b8.raw";
read_volume(g_file_string);
}
}
if (ImGui::CollapsingHeader("Lighting Settings"))
{
ImGui::SliderFloat3("Position Light", &g_light_pos[0], -10.0f, 10.0f);
ImGui::ColorEdit3("Ambient Color", &g_ambient_light_color[0]);
ImGui::ColorEdit3("Diffuse Color", &g_diffuse_light_color[0]);
ImGui::ColorEdit3("Specular Color", &g_specula_light_color[0]);
ImGui::SliderFloat("Reflection Coefficient kd", &g_ref_coef, 0.0f, 20.0f, "%.5f", 1.0f);
}
if (ImGui::CollapsingHeader("Quality Settings"))
{
ImGui::Text("Interpolation");
ImGui::RadioButton("Nearest Neighbour", &g_bilinear_interpolation, 0);
ImGui::RadioButton("Bilinear", &g_bilinear_interpolation, 1);
ImGui::Text("Slamping Size");
ImGui::SliderFloat("sampling factor", &g_sampling_distance_fact, 0.1f, 10.0f, "%.5f", 4.0f);
ImGui::SliderFloat("sampling factor move", &g_sampling_distance_fact_move, 0.1f, 10.0f, "%.5f", 4.0f);
ImGui::SliderFloat("reference sampling factor", &g_sampling_distance_fact_ref, 0.1f, 10.0f, "%.5f", 4.0f);
}
if (ImGui::CollapsingHeader("Shader", 0, true, true))
{
static ImVec4 text_color(1.0, 1.0, 1.0, 1.0);
if (g_reload_shader_error) {
text_color = ImVec4(1.0, 0.0, 0.0, 1.0);
ImGui::TextColored(text_color, "Shader Error");
}
else
{
text_color = ImVec4(0.0, 1.0, 0.0, 1.0);
ImGui::TextColored(text_color, "Shader Ok");
}
ImGui::TextWrapped(g_error_message.c_str());
g_reload_shader ^= ImGui::Button("Reload Shader");
}
if (ImGui::CollapsingHeader("Timing"))
{
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", ms_per_frame_avg, 1000.0f / ms_per_frame_avg);
float min = *std::min_element(ms_per_frame.begin(), ms_per_frame.end());
float max = *std::max_element(ms_per_frame.begin(), ms_per_frame.end());
if (max - min < 10.0f){
float mid = (max + min) * 0.5f;
min = mid - 5.0f;
max = mid + 5.0f;
}
static size_t values_offset = 0;
char buf[50];
sprintf(buf, "avg %f", ms_per_frame_avg);
ImGui::PlotLines("Frame Times", &ms_per_frame.front(), (int)ms_per_frame.size(), (int)values_offset, buf, min - max * 0.1f, max * 1.1f, ImVec2(0, 70));
ImGui::SameLine(); ImGui::Checkbox("pause", &g_pause);
}
if (ImGui::CollapsingHeader("Window options"))
{
if (ImGui::TreeNode("Window Size")){
const char* items[] = { "640x480", "720x576", "1280x720", "1920x1080", "1920x1200", "2048x1536" };
static int item2 = -1;
bool press = ImGui::Combo("Window Size", &item2, items, IM_ARRAYSIZE(items));
if (press){
glm::ivec2 win_re_size = glm::ivec2(640, 480);
switch (item2){
case 0:
win_re_size = glm::ivec2(640, 480);
break;
case 1:
win_re_size = glm::ivec2(720, 576);
break;
case 2:
win_re_size = glm::ivec2(1280, 720);
break;
case 3:
win_re_size = glm::ivec2(1920, 1080);
break;
case 4:
win_re_size = glm::ivec2(1920, 1200);
break;
case 5:
win_re_size = glm::ivec2(1920, 1536);
break;
default:
break;
}
g_win.resize(win_re_size);
}
ImGui::TreePop();
}
if (ImGui::TreeNode("Background Color")){
ImGui::ColorEdit3("BC", &g_background_color[0]);
ImGui::TreePop();
}
if (ImGui::TreeNode("Style Editor"))
{
ImGui::ShowStyleEditor();
ImGui::TreePop();
}
if (ImGui::TreeNode("Logging"))
{
ImGui::LogButtons();
ImGui::TreePop();
}
}
ImGui::End();
g_show_transfer_function = ImGui::Begin("Transfer Function Window", &g_show_transfer_function_in_window, ImVec2(300, 500));
g_transfer_function_pos.x = ImGui::GetItemBoxMin().x;
g_transfer_function_pos.y = ImGui::GetItemBoxMin().y;
g_transfer_function_size.x = ImGui::GetItemBoxMax().x - ImGui::GetItemBoxMin().x;
g_transfer_function_size.y = ImGui::GetItemBoxMax().y - ImGui::GetItemBoxMin().y;
static unsigned byte_size = 255;
static ImVector<float> A; if (A.empty()){ A.resize(byte_size); }
if (g_redraw_tf){
g_redraw_tf = false;
image_data_type color_con = g_transfer_fun.get_RGBA_transfer_function_buffer();
for (unsigned i = 0; i != byte_size; ++i){
A[i] = color_con[i * 4 + 3];
}
}
ImGui::PlotLines("", &A.front(), (int)A.size(), (int)0, "", 0.0, 255.0, ImVec2(0, 70));
g_transfer_function_pos.x = ImGui::GetItemBoxMin().x;
g_transfer_function_pos.y = ImGui::GetIO().DisplaySize.y - ImGui::GetItemBoxMin().y - 70;
g_transfer_function_size.x = ImGui::GetItemBoxMax().x - ImGui::GetItemBoxMin().x;
g_transfer_function_size.y = ImGui::GetItemBoxMax().y - ImGui::GetItemBoxMin().y;
ImGui::SameLine(); ImGui::Text("Color:RGB Plot: Alpha");
static int data_value = 0;
ImGui::SliderInt("Data Value", &data_value, 0, 255);
static float col[4] = { 0.4f, 0.7f, 0.0f, 0.5f };
ImGui::ColorEdit4("color", col);
bool add_entry_to_tf = false;
add_entry_to_tf ^= ImGui::Button("Add entry"); ImGui::SameLine();
bool reset_tf = false;
reset_tf ^= ImGui::Button("Reset");
if (reset_tf){
g_transfer_fun.reset();
g_transfer_dirty = true;
g_redraw_tf = true;
}
if (add_entry_to_tf){
g_current_tf_data_value = data_value;
g_transfer_fun.add((unsigned)data_value, glm::vec4(col[0], col[1], col[2], col[3]));
g_transfer_dirty = true;
g_redraw_tf = true;
}
if (ImGui::CollapsingHeader("Manipulate Values")){
Transfer_function::container_type con = g_transfer_fun.get_piecewise_container();
bool delete_entry_from_tf = false;
static std::vector<int> g_c_data_value;
if (g_c_data_value.size() != con.size())
g_c_data_value.resize(con.size());
int i = 0;
for (Transfer_function::container_type::iterator c = con.begin(); c != con.end(); ++c)
{
int c_data_value = c->first;
glm::vec4 c_color_value = c->second;
g_c_data_value[i] = c_data_value;
std::stringstream ss;
if (c->first < 10)
ss << c->first << " ";
else if (c->first < 100)
ss << c->first << " ";
else
ss << c->first;
bool change_value = false;
change_value ^= ImGui::SliderInt(std::to_string(i).c_str(), &g_c_data_value[i], 0, 255); ImGui::SameLine();
if (change_value){
if (con.find(g_c_data_value[i]) == con.end()){
g_transfer_fun.remove(c_data_value);
g_transfer_fun.add((unsigned)g_c_data_value[i], c_color_value);
g_current_tf_data_value = g_c_data_value[i];
g_transfer_dirty = true;
g_redraw_tf = true;
}
}
//delete
bool delete_entry_from_tf = false;
delete_entry_from_tf ^= ImGui::Button(std::string("Delete: ").append(ss.str()).c_str());
if (delete_entry_from_tf){
g_current_tf_data_value = c_data_value;
g_transfer_fun.remove(g_current_tf_data_value);
g_transfer_dirty = true;
g_redraw_tf = true;
}
static float n_col[4] = { 0.4f, 0.7f, 0.0f, 0.5f };
memcpy(&n_col, &c_color_value, sizeof(float)* 4);
bool change_color = false;
change_color ^= ImGui::ColorEdit4(ss.str().c_str(), n_col);
if (change_color){
g_transfer_fun.add((unsigned)g_c_data_value[i], glm::vec4(n_col[0], n_col[1], n_col[2], n_col[3]));
g_current_tf_data_value = g_c_data_value[i];
g_transfer_dirty = true;
g_redraw_tf = true;
}
ImGui::Separator();
++i;
}
}
if (ImGui::CollapsingHeader("Transfer Function - Save/Load", 0, true, false))
{
ImGui::Text("Transferfunctions");
bool load_tf_1 = false;
bool load_tf_2 = false;
bool load_tf_3 = false;
bool load_tf_4 = false;
bool load_tf_5 = false;
bool load_tf_6 = false;
bool save_tf_1 = false;
bool save_tf_2 = false;
bool save_tf_3 = false;
bool save_tf_4 = false;
bool save_tf_5 = false;
bool save_tf_6 = false;
save_tf_1 ^= ImGui::Button("Save TF1"); ImGui::SameLine();
load_tf_1 ^= ImGui::Button("Load TF1");
save_tf_2 ^= ImGui::Button("Save TF2"); ImGui::SameLine();
load_tf_2 ^= ImGui::Button("Load TF2");
save_tf_3 ^= ImGui::Button("Save TF3"); ImGui::SameLine();
load_tf_3 ^= ImGui::Button("Load TF3");
save_tf_4 ^= ImGui::Button("Save TF4"); ImGui::SameLine();
load_tf_4 ^= ImGui::Button("Load TF4");
save_tf_5 ^= ImGui::Button("Save TF5"); ImGui::SameLine();
load_tf_5 ^= ImGui::Button("Load TF5");
save_tf_6 ^= ImGui::Button("Save TF6"); ImGui::SameLine();
load_tf_6 ^= ImGui::Button("Load TF6");
if (save_tf_1 || save_tf_2 || save_tf_3 || save_tf_4 || save_tf_5 || save_tf_6){
Transfer_function::container_type con = g_transfer_fun.get_piecewise_container();
std::vector<Transfer_function::element_type> save_vect;
for (Transfer_function::container_type::iterator c = con.begin(); c != con.end(); ++c)
{
save_vect.push_back(*c);
}
std::ofstream tf_file;
if (save_tf_1){ tf_file.open("TF1", std::ios::out | std::ofstream::binary); }
if (save_tf_2){ tf_file.open("TF2", std::ios::out | std::ofstream::binary); }
if (save_tf_3){ tf_file.open("TF3", std::ios::out | std::ofstream::binary); }
if (save_tf_4){ tf_file.open("TF4", std::ios::out | std::ofstream::binary); }
if (save_tf_5){ tf_file.open("TF5", std::ios::out | std::ofstream::binary); }
if (save_tf_6){ tf_file.open("TF6", std::ios::out | std::ofstream::binary); }
//std::copy(save_vect.begin(), save_vect.end(), std::ostreambuf_iterator<char>(tf_file));
tf_file.write((char*)&save_vect[0], sizeof(Transfer_function::element_type) * save_vect.size());
tf_file.close();
}
if (load_tf_1 || load_tf_2 || load_tf_3 || load_tf_4 || load_tf_5 || load_tf_6){
Transfer_function::container_type con = g_transfer_fun.get_piecewise_container();
std::vector<Transfer_function::element_type> load_vect;
std::ifstream tf_file;
if (load_tf_1){ tf_file.open("TF1", std::ios::in | std::ifstream::binary); }
if (load_tf_2){ tf_file.open("TF2", std::ios::in | std::ifstream::binary); }
if (load_tf_3){ tf_file.open("TF3", std::ios::in | std::ifstream::binary); }
if (load_tf_4){ tf_file.open("TF4", std::ios::in | std::ifstream::binary); }
if (load_tf_5){ tf_file.open("TF5", std::ios::in | std::ifstream::binary); }
if (load_tf_6){ tf_file.open("TF6", std::ios::in | std::ifstream::binary); }
if (tf_file.good()){
tf_file.seekg(0, tf_file.end);
size_t size = tf_file.tellg();
unsigned elements = (int)size / (unsigned)sizeof(Transfer_function::element_type);
tf_file.seekg(0);
load_vect.resize(elements);
tf_file.read((char*)&load_vect[0], size);
g_transfer_fun.reset();
g_transfer_dirty = true;
for (std::vector<Transfer_function::element_type>::iterator c = load_vect.begin(); c != load_vect.end(); ++c)
{
g_transfer_fun.add(c->first, c->second);
}
}
tf_file.close();
}
}
ImGui::End();
}
int main(int argc, char* argv[])
{
//g_win = Window(g_window_res);
//InitImGui();
ImGui_ImplGlfwGL3_Init(g_win.getGLFWwindow(), true);
// initialize the transfer function
// first clear possible old values
g_transfer_fun.reset();
// the add_stop method takes:
// - unsigned char or float - data value (0.0 .. 1.0) or (0..255)
// - vec4f - color and alpha value (0.0 .. 1.0) per channel
g_transfer_fun.add(0.0f, glm::vec4(0.0, 0.0, 0.0, 0.0));
g_transfer_fun.add(1.0f, glm::vec4(1.0, 1.0, 1.0, 1.0));
g_transfer_dirty = true;
///NOTHING TODO HERE-------------------------------------------------------------------------------
// init and upload volume texture
bool check = read_volume(g_file_string);
// init and upload transfer function texture
glActiveTexture(GL_TEXTURE1);
g_transfer_texture = createTexture2D(255u, 1u, (char*)&g_transfer_fun.get_RGBA_transfer_function_buffer()[0]);
// loading actual raytracing shader code (volume.vert, volume.frag)
// edit volume.frag to define the result of our volume raycaster
try {
g_volume_program = loadShaders(g_file_vertex_shader, g_file_fragment_shader,
g_task_chosen,
g_lighting_toggle,
g_shadow_toggle,
g_opacity_correction_toggle);
}
catch (std::logic_error& e) {
//std::cerr << e.what() << std::endl;
std::stringstream ss;
ss << e.what() << std::endl;
g_error_message = ss.str();
g_reload_shader_error = true;
}
// init object manipulator (turntable)
Manipulator manipulator;
// manage keys here
// add new input if neccessary (ie changing sampling distance, isovalues, ...)
while (!g_win.shouldClose()) {
float sampling_fact = g_sampling_distance_fact;
if (!first_frame > 0.0){
// exit window with escape
if (ImGui::IsKeyPressed(GLFW_KEY_ESCAPE)) {
g_win.stop();
}
if (ImGui::IsKeyPressed(GLFW_KEY_LEFT)) {
g_light_pos.x -= 0.5f;
}
if (ImGui::IsKeyPressed(GLFW_KEY_RIGHT)) {
g_light_pos.x += 0.5f;
}
if (ImGui::IsKeyPressed(GLFW_KEY_UP)) {
g_light_pos.z -= 0.5f;
}
if (ImGui::IsKeyPressed(GLFW_KEY_DOWN)) {
g_light_pos.z += 0.5f;
}
if (ImGui::IsKeyPressed(GLFW_KEY_MINUS)) {
g_iso_value -= 0.002f;
g_iso_value = std::max(g_iso_value, 0.0f);
}
if (ImGui::IsKeyPressed(GLFW_KEY_EQUAL) || ImGui::IsKeyPressed(GLFW_KEY_KP_ADD)) {
g_iso_value += 0.002f;
g_iso_value = std::min(g_iso_value, 1.0f);
}
if (ImGui::IsKeyPressed(GLFW_KEY_D)) {
g_sampling_distance -= 0.0001f;
g_sampling_distance = std::max(g_sampling_distance, 0.0001f);
}
if (ImGui::IsKeyPressed(GLFW_KEY_S)) {
g_sampling_distance += 0.0001f;
g_sampling_distance = std::min(g_sampling_distance, 0.2f);
}
if (ImGui::IsKeyPressed(GLFW_KEY_R)) {
g_reload_shader = true;
}
if (ImGui::IsMouseDown(GLFW_MOUSE_BUTTON_LEFT) || ImGui::IsMouseDown(GLFW_MOUSE_BUTTON_MIDDLE) || ImGui::IsMouseDown(GLFW_MOUSE_BUTTON_RIGHT)) {
sampling_fact = g_sampling_distance_fact_move;
}
}
// to add key inputs:
// check ImGui::IsKeyPressed(KEY_NAME)
// - KEY_NAME - key name (GLFW_KEY_A ... GLFW_KEY_Z)
//if (ImGui::IsKeyPressed(GLFW_KEY_X)){
//
// ... do something
//
//}
/// reload shader if key R ist pressed
if (g_reload_shader){
GLuint newProgram(0);
try {
//std::cout << "Reload shaders" << std::endl;
newProgram = loadShaders(g_file_vertex_shader, g_file_fragment_shader, g_task_chosen, g_lighting_toggle, g_shadow_toggle, g_opacity_correction_toggle);
g_error_message = "";
}
catch (std::logic_error& e) {
//std::cerr << e.what() << std::endl;
std::stringstream ss;
ss << e.what() << std::endl;
g_error_message = ss.str();
g_reload_shader_error = true;
newProgram = 0;
}
if (0 != newProgram) {
glDeleteProgram(g_volume_program);
g_volume_program = newProgram;
g_reload_shader_error = false;
}
else
{
g_reload_shader_error = true;
}
}
if (ImGui::IsKeyPressed(GLFW_KEY_R)) {
if (g_reload_shader_pressed != true) {
g_reload_shader = true;
g_reload_shader_pressed = true;
}
else{
g_reload_shader = false;
}
}
else {
g_reload_shader = false;
g_reload_shader_pressed = false;
}
if (g_transfer_dirty && !first_frame){
g_transfer_dirty = false;
static unsigned byte_size = 255;
image_data_type color_con = g_transfer_fun.get_RGBA_transfer_function_buffer();
glActiveTexture(GL_TEXTURE1);
updateTexture2D(g_transfer_texture, 255u, 1u, (char*)&g_transfer_fun.get_RGBA_transfer_function_buffer()[0]);
}
if (g_bilinear_interpolation){
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
else{
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
glm::ivec2 size = g_win.windowSize();
glViewport(0, 0, size.x, size.y);
glClearColor(g_background_color.x, g_background_color.y, g_background_color.z, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float fovy = 45.0f;
float aspect = (float)size.x / (float)size.y;
float zNear = 0.025f, zFar = 10.0f;
glm::mat4 projection = glm::perspective(fovy, aspect, zNear, zFar);
glm::vec3 translate_rot = g_max_volume_bounds * glm::vec3(-0.5f, -0.5f, -0.5f);
glm::vec3 translate_pos = g_max_volume_bounds * glm::vec3(+0.5f, -0.0f, -0.0f);
glm::vec3 eye = glm::vec3(0.0f, 0.0f, 1.5f);
glm::vec3 target = glm::vec3(0.0f);
glm::vec3 up(0.0f, 1.0f, 0.0f);
glm::mat4 view = glm::lookAt(eye, target, up);
glm::mat4 turntable_matrix = manipulator.matrix();
if (!g_over_gui){
turntable_matrix = manipulator.matrix(g_win);
}
glm::mat4 model_view = view
* glm::translate(translate_pos)
* turntable_matrix
// rotate head upright
* glm::rotate(0.5f*float(M_PI), glm::vec3(0.0f, 1.0f, 0.0f))
* glm::rotate(0.5f*float(M_PI), glm::vec3(1.0f, 0.0f, 0.0f))
* glm::translate(translate_rot)
;
glm::vec4 camera_translate = glm::column(glm::inverse(model_view), 3);
glm::vec3 camera_location = glm::vec3(camera_translate.x, camera_translate.y, camera_translate.z);
camera_location /= glm::vec3(camera_translate.w);
glm::vec4 light_location = glm::vec4(g_light_pos, 1.0f) * model_view;
glBindTexture(GL_TEXTURE_2D, g_transfer_texture);
glUseProgram(g_volume_program);
glUniform1i(glGetUniformLocation(g_volume_program, "volume_texture"), 0);
glUniform1i(glGetUniformLocation(g_volume_program, "transfer_texture"), 1);
glUniform3fv(glGetUniformLocation(g_volume_program, "camera_location"), 1,
glm::value_ptr(camera_location));
glUniform1f(glGetUniformLocation(g_volume_program, "sampling_distance"), g_sampling_distance * sampling_fact);
glUniform1f(glGetUniformLocation(g_volume_program, "sampling_distance_ref"), g_sampling_distance_fact_ref);
glUniform1f(glGetUniformLocation(g_volume_program, "iso_value"), g_iso_value);
glUniform3fv(glGetUniformLocation(g_volume_program, "max_bounds"), 1,
glm::value_ptr(g_max_volume_bounds));
glUniform3iv(glGetUniformLocation(g_volume_program, "volume_dimensions"), 1,
glm::value_ptr(g_vol_dimensions));
glUniform3fv(glGetUniformLocation(g_volume_program, "light_position"), 1,
glm::value_ptr(g_light_pos));
glUniform3fv(glGetUniformLocation(g_volume_program, "light_ambient_color"), 1,