-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1683 lines (1437 loc) · 56.7 KB
/
Copy pathmain.cpp
File metadata and controls
1683 lines (1437 loc) · 56.7 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 "glad/glad.h"
#include "SDL3/SDL.h"
#include "imgui.h"
#include "imgui_impl_sdl3.h"
#include "imgui_impl_opengl3.h"
#include "common.hpp"
//engine system
#include "camera.hpp"
#include "bvh.hpp"
#include "environment.hpp"
#include "scene_management.hpp"
//stb libraries (to save)
#include "stb_image.h"
#include "stb_image_write.h"
//multithreading
#include <omp.h>
#include <iostream>
#include <thread>
#include <atomic> //for safe communication between threads
#include <chrono>
#include <cstdarg>
//function to create an OpenGL texture from a color buffer (used for displaying the rendered image in ImGui)
GLuint create_texture_from_buffer(const std::vector<color>& buffer, int width, int height) {
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
//convert the color buffer(double/float) to float format for OpenGL
std::vector<float> float_data;
float_data.reserve(buffer.size() * 3);
for (const auto& c : buffer) {
float_data.push_back(static_cast<float>(c.x()));
float_data.push_back(static_cast<float>(c.y()));
float_data.push_back(static_cast<float>(c.z()));
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, width, height, 0, GL_RGB, GL_FLOAT, float_data.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
return textureID;
}
//simple logging system for the engine with timestamp and severity levels, also stores recent frame times for performance graphing
struct AppLog {
std::vector<std::string> items;
float frame_times[90] = { 0 }; //circular buffer for frame times (last 90 frames ~ 3 seconds at 30fps)
int offset = 0;
int last_lines_count = 0;
//function to add a log entry with timestamp and formatting support (like printf)
void add_log(const char* fmt, ...) {
char buf[256];
va_list args;
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
time_t now = time(0);
tm* ltm = localtime(&now);
char time_buf[16];
strftime(time_buf, sizeof(time_buf), "[%H:%M:%S] ", ltm);
items.push_back(std::string(time_buf) + buf);
if (items.size() > 500) {
items.erase(items.begin()); //memory limits
}
}
//function to draw the performance metrics and logs in the ImGui window
void draw_tab_content(const camera& cam, bool is_rendering) {
//performance section tab
ImGui::TextColored(ImVec4(0.0f, 1.0f, 1.0f, 1.0f), "Performance Metrics");
float fps = ImGui::GetIO().Framerate;
float frame_time = 1000.0f / fps;
//update circular buffer
frame_times[offset] = frame_time;
offset = (offset + 1) % 90;
char overlay[64];
sprintf(overlay, "Avg: %.2f ms", frame_time);
//frame time graph
ImGui::PlotLines("##frames", frame_times, 90, offset, overlay, 0.0f, 50.0f, ImVec2(-1, 80));
ImGui::Text("FPS: %.1f", fps);
//calculate how many new lines have been rendered since the last GUI update
int lines_this_frame = cam.lines_rendered - last_lines_count;
if (lines_this_frame < 0) {
lines_this_frame = 0; // reset przy nowym renderze
}
double rays_this_moment = static_cast<double>(cam.image_width) * lines_this_frame * cam.samples_per_pixel * cam.max_depth;
float frame_time_sec = 1.0f / ImGui::GetIO().Framerate;
float mrays_per_sec = 0.0f;
if (frame_time_sec > 0 && is_rendering) {
mrays_per_sec = static_cast<float>((rays_this_moment / frame_time_sec) / 1000000.0);
}
last_lines_count = cam.lines_rendered;
ImGui::SameLine(ImGui::GetWindowWidth() * 0.5f);
ImGui::Text("Throughput: %.2f Mrays/s", mrays_per_sec);
ImGui::Spacing();
ImGui::Separator();
ImGui::Spacing();
//logs scetion tab
ImGui::TextColored(ImVec4(1.0f, 0.8f, 0.0f, 1.0f), "Engine Logs");
if (ImGui::Button("Clear Log")) {
items.clear();
}
ImGui::BeginChild("LogScroll", ImVec2(0, 0), true, ImGuiWindowFlags_HorizontalScrollbar);
for (const auto& item : items) {
const char* msg = item.c_str();
if (strstr(msg, "[Error]")) {
//red for errors
ImGui::TextColored(ImVec4(1.0f, 0.2f, 0.2f, 1.0f), "%s", msg);
} else if (strstr(msg, "[Config]")) {
//blue for settings changes
ImGui::TextColored(ImVec4(0.3f, 0.7f, 1.0f, 1.0f), "%s", msg);
} else if (strstr(msg, "[Render]") || strstr(msg, "[System]")) {
//green for render status and engine start
ImGui::TextColored(ImVec4(0.4f, 1.0f, 0.4f, 1.0f), "%s", msg);
} else if (strstr(msg, "[Debug]")) {
//yellow for debug modes
ImGui::TextColored(ImVec4(1.0f, 0.8f, 0.0f, 1.0f), "%s", msg);
} else {
//white for everything else
ImGui::TextUnformatted(msg);
}
}
if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) {
ImGui::SetScrollHereY(1.0f);
}
ImGui::EndChild();
ImGui::Spacing();
ImGui::SeparatorText("System & Scene Info");
#ifdef _OPENMP
ImGui::BulletText("Hardware Threads: %d", omp_get_max_threads());
#else
ImGui::BulletText("Hardware Threads: 1 (OpenMP disabled)");
#endif
ImGui::BulletText("Render Resolution: %d x %d", cam.image_width, cam.image_height);
//VRAM/RAM for frame buffer
float mem = (cam.image_width * cam.image_height * 4.0f) / 1048576.0f;
ImGui::BulletText("Frame Buffer Memory: %.2f MB", mem);
if (is_rendering) {
float progress = (float)cam.lines_rendered / (float)cam.image_height;
ImGui::ProgressBar(progress, ImVec2(-1, 0), "Rendering...");
}
}
};
//global instance of the log system
static AppLog engine_info;
int main(int argc, char* argv[]) {
engine_info.add_log("[Render] -Zenith Engine Started");
#ifdef _OPEMMP
engine_info.add_log("[Render] OpenMP initialized with %d threads.", omp_get_max_threads());
#endif //_OPEMMP
// - 1. LOADING MATERIALS FROM THE LIBRARY -
MaterialLibrary mat_lib;
load_materials(mat_lib); //from scene_management.hpp
// - 2. OBJECT LOADER (.obj) -
sceneAssetsLoader assets;
// - 3. CREATE CAMERA -
camera cam;
cam.refresh_hdr_list(); //scan folder assets/hdr_maps/
// - 4. LOADING THE GEOMETRY -
hittable_list world = build_geometry(mat_lib,
assets,
cam.use_fog,
static_cast<double>(cam.fog_density),
color(cam.fog_color[0], cam.fog_color[1], cam.fog_color[2])); //from scene_management.hpp
// - 5. BVH ACCELERATION STRUCTURE -
shared_ptr<hittable> bvh_world = make_shared<bvh_node>(world);
// - 6. CREATE ENVIRONMENT -
EnvironmentSettings env;
env.load_hdr(cam.get_default_hdr_path());
env.intensity = 1.0;
// - 7. POST-PROCESSING -
post_processor my_post;
// - WINDOW AND OPENGL INITIALIZATION FOR IMGUI DISPLAY OF THE RENDERED IMAGE -
SDL_Init(SDL_INIT_VIDEO);
#ifdef __APPLE__
// MacOS
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); // Wymagane na macOS
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
#else
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
#endif
SDL_Window* window = SDL_CreateWindow("-Zenith Frame_buffer", 1500, 844,
SDL_WINDOW_OPENGL | SDL_WINDOW_HIGH_PIXEL_DENSITY);
SDL_GLContext gl_context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, gl_context); //make sure context is active
SDL_GL_SetSwapInterval(1); //V-Sync ON
gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress);
// - INITIALIZE IMGUI -
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui_ImplSDL3_InitForOpenGL(window, gl_context);
ImGui_ImplOpenGL3_Init("#version 330");
bool running = true;
std::atomic<bool> is_rendering{ false }; //flag to indicate if rendering is in progress
std::chrono::steady_clock::time_point render_start_time; //to measure render duration
bool is_active_session = false;
float last_render_duration = 0.0f;
bool render_was_cancelled = false;
float last_progress_percent = 0.0f;
GLuint rendered_texture = 0; //global texture ID for rendered image
std::thread render_thread; //thread declaration
// - MAIN LOOP -
while (running) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
ImGui_ImplSDL3_ProcessEvent(&event);
if (event.type == SDL_EVENT_QUIT) {
running = false;
}
}
//start the ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL3_NewFrame();
//for MacOS with retina
glViewport(0, 0, 1500, 844);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); //dark turquosie instead of black
glClear(GL_COLOR_BUFFER_BIT);
ImGui::NewFrame();
bool needs_update = false; //flag to indicate if any setting changed
bool should_restart = false; //flag to indicate if render should be restarted
// - WINDOW 1: CONTROL PANEL -
ImGui::Begin("Engine Control Panel");
if (ImGui::BeginTabBar("Tabs")) {
//camera settings tab
if (ImGui::BeginTabItem("Camera & Quality")) {
//buffers definition
static double lookfrom_buf[3] = {
cam.lookfrom.x(),
cam.lookfrom.y(),
cam.lookfrom.z()
};
static double lookat_buf[3] = {
cam.lookat.x(),
cam.lookat.y(),
cam.lookat.z()
};
static double vup_buf[3] = {
cam.vup.x(),
cam.vup.y(),
cam.vup.z()
};
//refresh the buffers
if (!ImGui::IsAnyItemActive()) {
lookfrom_buf[0] = cam.lookfrom.x();
lookfrom_buf[1] = cam.lookfrom.y();
lookfrom_buf[2] = cam.lookfrom.z();
lookat_buf[0] = cam.lookat.x();
lookat_buf[1] = cam.lookat.y();
lookat_buf[2] = cam.lookat.z();
vup_buf[0] = cam.vup.x();
vup_buf[1] = cam.vup.y();
vup_buf[2] = cam.vup.z();
}
//image resolution and aspect ratio
ImGui::SeparatorText("Resolution & Aspect");
bool rendering = is_rendering.load();
//blockage for aspect ratio while rendering
if (rendering) {
ImGui::BeginDisabled();
}
if (ImGui::InputInt("Image Width", &cam.image_width)) {
if (cam.image_width < 100) {
cam.image_width = 100;
}
}
//flag for customized image ratio
static bool custom_ratio = false;
if (ImGui::Checkbox("Custom Resolution (Unlock Height)", &custom_ratio)) {
cam.image_height = static_cast<int>(cam.image_width / cam.aspect_ratio);
}
if (!custom_ratio) {
//slider for aspect ratio
static float aspect = 1.777f; // 16/9
if (ImGui::SliderFloat("Aspect Ratio", &aspect, 0.5f, 2.5f, "%.3f")) {
cam.aspect_ratio = (double)aspect;
}
//quick buttons for some standard aspect ratios
if (ImGui::Button("16:9")) {
aspect = 1.777f;
cam.aspect_ratio = 16.0 / 9.0;
}
ImGui::SameLine();
if (ImGui::Button("4:3")) {
aspect = 1.333f;
cam.aspect_ratio = 4.0 / 3.0;
}
ImGui::SameLine();
if (ImGui::Button("1:1")) {
aspect = 1.000f;
cam.aspect_ratio = 1.0;
}
//calculate image height automatically
cam.image_height = static_cast<int>(cam.image_width / cam.aspect_ratio);
ImGui::Text("Locked Height: %d", cam.image_height);
} else {
//custom mode: height is unlocked
if (ImGui::InputInt("Image Height", &cam.image_height)) {
if (cam.image_height < 100) {
cam.image_height = 100;
}
//update aspect ratio
cam.aspect_ratio = (double)cam.image_width / cam.image_height;
}
ImGui::TextDisabled("Current Ratio: %.3f", cam.aspect_ratio);
}
//unlock blockage for aspect ratio section
if (rendering) {
ImGui::EndDisabled();
}
ImGui::SeparatorText("Position & Orientation");
//look from
if (ImGui::InputScalarN("Look From", ImGuiDataType_Double, lookfrom_buf, 3)) {
cam.lookfrom = point3(lookfrom_buf[0], lookfrom_buf[1], lookfrom_buf[2]);
engine_info.add_log("[Config] Camera position changed to (%.2f, %.2f, %.2f)",
cam.lookfrom.x(),
cam.lookfrom.y(),
cam.lookfrom.z()
);
should_restart = true;
}
//look at
if (ImGui::InputScalarN("Look At", ImGuiDataType_Double, lookat_buf, 3)) {
cam.lookat = point3(lookat_buf[0], lookat_buf[1], lookat_buf[2]);
engine_info.add_log("[Config] Camera look-at point changed to (%.2f, %.2f, %.2f)",
cam.lookat.x(),
cam.lookat.y(),
cam.lookat.z()
);
should_restart = true;
}
//up vector
ImGui::Text("Up Vector");
//create a temporary buffer for InputScalarN to handle the data
vup_buf[0] = cam.vup.x();
vup_buf[1] = cam.vup.y();
vup_buf[2] = cam.vup.z();
ImGui::PushID("UpVectorFields");
if (ImGui::InputScalarN("##vup_input", ImGuiDataType_Double, vup_buf, 3)) {
cam.vup = vec3(vup_buf[0], vup_buf[1], vup_buf[2]);
engine_info.add_log("[Config] Camera up vector changed to (%.2f, %.2f, %.2f)",
cam.vup.x(),
cam.vup.y(),
cam.vup.z()
);
should_restart = true;
}
ImGui::PopID();
//reset and normalize
if (ImGui::Button("Reset Up (0,1,0)")) {
cam.vup = vec3(0, 1, 0);
engine_info.add_log("[Config] Camera up vector reset to (0, 1, 0)");
should_restart = true;
}
ImGui::SameLine();
if (ImGui::Button("Normalize Up")) {
cam.vup = unit_vector(cam.vup);
engine_info.add_log("[Config] Camera up vector normalized to (%.2f, %.2f, %.2f)",
cam.vup.x(),
cam.vup.y(),
cam.vup.z()
);
should_restart = true;
}
ImGui::SeparatorText("Optics");
//fov
float vfov_f = static_cast<float>(cam.vfov);
if (ImGui::SliderFloat("FOV", &vfov_f, 1.0f, 120.0f)) {
cam.vfov = static_cast<double>(vfov_f);
should_restart = true;
}
if (ImGui::IsItemDeactivatedAfterEdit()) {
engine_info.add_log("[Config] Camera FOV finalized at %.1f degrees", cam.vfov);
}
//aperture (defocus_angle)
float aperture_f = static_cast<float>(cam.defocus_angle);
if (ImGui::SliderFloat("Aperture", &aperture_f, 0.0f, 5.0f)) {
cam.defocus_angle = static_cast<double>(aperture_f);
should_restart = true;
}
if (ImGui::IsItemDeactivatedAfterEdit()) {
engine_info.add_log("[Config] Camera aperture finalized at %.2f", cam.defocus_angle);
}
//focus distance
float focus_f = static_cast<float>(cam.focus_dist);
if (ImGui::SliderFloat("Focus Dist", &focus_f, 0.1f, 50.0f)) {
cam.focus_dist = static_cast<double>(focus_f);
should_restart = true;
}
if (ImGui::IsItemDeactivatedAfterEdit()) {
engine_info.add_log("[Config] Camera focus distance finalized at %.2f", cam.focus_dist);
}
//samples per pixel
ImGui::SeparatorText("Quality");
if (ImGui::InputInt("Samples per Pixel", &cam.samples_per_pixel)) {
if (cam.samples_per_pixel < 1) {
cam.samples_per_pixel = 1;
}
engine_info.add_log("[Config] Samples per Pixel set to %d", cam.samples_per_pixel);
should_restart = true;
}
//max depth
if (ImGui::SliderInt("Max Depth", &cam.max_depth, 1, 100)) {
should_restart = true;
}
if (ImGui::IsItemDeactivatedAfterEdit()) {
engine_info.add_log("[Config] Max Depth finalized at %d", cam.max_depth);
}
ImGui::SeparatorText("Render Passes");
//dropdown passes
ImGui::Text("Active View:");
//reference to static array of pass names in camera class
if (ImGui::BeginCombo("##SelectPass", camera::pass_names[static_cast<int>(cam.current_display_pass)])) {
for (int n = 0; n < 7; n++) {
render_pass p = static_cast<render_pass>(n);
//display only if checkbox for the pass is active (except RGB always active)
bool is_enabled = true;
if (p == render_pass::DENOISE) {
is_enabled = cam.use_denoiser;
} else if (p == render_pass::ALBEDO) {
is_enabled = cam.use_albedo_buffer;
} else if (p == render_pass::NORMALS) {
is_enabled = cam.use_normal_buffer;
} else if (p == render_pass::REFLECTIONS) {
is_enabled = cam.use_reflection;
} else if (p == render_pass::REFRACTIONS) {
is_enabled = cam.use_refraction;
} else if (p == render_pass::Z_DEPTH) {
is_enabled = cam.use_z_depth_buffer;
}
if (!is_enabled && p != render_pass::RGB) {
continue;
}
const bool is_selected = (cam.current_display_pass == p);
if (ImGui::Selectable(camera::pass_names[n], is_selected)) {
cam.current_display_pass = p;
my_post.needs_update = true;
engine_info.add_log("[Config] Active view set to %s", camera::pass_names[n]);
}
}
ImGui::EndCombo();
}
ImGui::Spacing();
ImGui::Separator();
ImGui::Spacing();
//passes checkboxes for activation (except RGB always active)
ImGui::Text("Compute Passes:");
//block checkboxes during rendering
bool is_rendering_now = is_rendering.load();
ImGui::BeginDisabled(is_rendering_now);
auto check_pass_safety = [&](bool active, render_pass p) {
if (!active && cam.current_display_pass == p) {
cam.current_display_pass = render_pass::RGB;
my_post.needs_update = true;
engine_info.add_log("[Config] Active view reset to RGB because the selected pass was deactivated");
}
};
if (ImGui::Checkbox("Denoise", &cam.use_denoiser)) {
check_pass_safety(cam.use_denoiser, render_pass::DENOISE);
}
if (ImGui::Checkbox("Albedo", &cam.use_albedo_buffer)) {
check_pass_safety(cam.use_albedo_buffer, render_pass::ALBEDO);
}
if (ImGui::Checkbox("Normals", &cam.use_normal_buffer)) {
check_pass_safety(cam.use_normal_buffer, render_pass::NORMALS);
}
if (ImGui::Checkbox("Reflections(Mirrors)", &cam.use_reflection)) {
check_pass_safety(cam.use_reflection, render_pass::REFLECTIONS);
}
if (ImGui::Checkbox("Refractions(Glass)", &cam.use_refraction)) {
check_pass_safety(cam.use_refraction, render_pass::REFRACTIONS);
}
if (ImGui::Checkbox("Z-Depth", &cam.use_z_depth_buffer)) {
check_pass_safety(cam.use_z_depth_buffer, render_pass::Z_DEPTH);
}
if (cam.use_z_depth_buffer) {
ImGui::Indent();
if (ImGui::SliderFloat("Max Distance", &my_post.z_depth_max_dist, 0.1f, 50.0f)) {
my_post.needs_update = true;
}
if (ImGui::IsItemDeactivatedAfterEdit()) {
engine_info.add_log("[Config] Z-Depth max distance finalized at %.2f", my_post.z_depth_max_dist);
}
ImGui::Unindent();
}
ImGui::EndDisabled();
ImGui::EndTabItem();
}
//environment settings tab
if (ImGui::BeginTabItem("Environment")) {
//buffer definition
static double sun_dir_buf[3] = {
env.sun_direction.x(),
env.sun_direction.y(),
env.sun_direction.z()
};
static float rot_deg = 0.0f;
static float tilt_deg = 0.0f;
static float roll_deg = 0.0f;
static float sun_col[3] = {
(float)env.sun_color.x(),
(float)env.sun_color.y(),
(float)env.sun_color.z()
};
static float bg_col[3] = {
(float)env.background_color.x(),
(float)env.background_color.y(),
(float)env.background_color.z()
};
//desychronization and crash prevention
if (!ImGui::IsAnyItemActive() && env.needs_ui_sync) {
engine_info.add_log("[Debug] Synchronizing environment buffers with current settings");
sun_dir_buf[0] = env.sun_direction.x();
sun_dir_buf[1] = env.sun_direction.y();
sun_dir_buf[2] = env.sun_direction.z();
sun_col[0] = (float)env.sun_color.x();
sun_col[1] = (float)env.sun_color.y();
sun_col[2] = (float)env.sun_color.z();
bg_col[0] = (float)env.background_color.x();
bg_col[1] = (float)env.background_color.y();
bg_col[2] = (float)env.background_color.z();
engine_info.add_log("[System] UI Buffers synchronized with Engine State.");
env.needs_ui_sync = false;
}
ImGui::SeparatorText("Sky Mode Selection");
//switching buttons for environment mode (use enum from EnvironmentSetting)
int current_mode = (int)env.mode();
if (ImGui::RadioButton("HDR Map", current_mode == EnvironmentSettings::HDR_MAP)) {
env.set_mode(EnvironmentSettings::HDR_MAP);
engine_info.add_log("[Config] Environment mode set to HDR Map");
should_restart = true;
}
ImGui::SameLine();
if (ImGui::RadioButton("Physical Sun", current_mode == EnvironmentSettings::PHYSICAL_SUN)) {
env.set_mode(EnvironmentSettings::PHYSICAL_SUN);
engine_info.add_log("[Config] Environment mode set to Physical Sun & Sky");
should_restart = true;
}
ImGui::SameLine();
if (ImGui::RadioButton("Solid Color", current_mode == EnvironmentSettings::SOLID_COLOR)) {
env.set_mode(EnvironmentSettings::SOLID_COLOR);
engine_info.add_log("[Config] Environment mode set to Solid Color");
should_restart = true;
}
ImGui::Separator();
//global intensity
float intensity_f = static_cast<float>(env.intensity);
if (ImGui::SliderFloat("Global Intensity", &intensity_f, 0.0f, 5.0f)) {
env.intensity = static_cast<double>(intensity_f);
should_restart = true;
}
if (ImGui::IsItemDeactivatedAfterEdit()) {
engine_info.add_log("[Config] Environment global intensity finalized at %.2f", env.intensity);
}
//environmental fog
if (ImGui::CollapsingHeader("Environmental Fog")) {
if (ImGui::Checkbox("Enable Fog", &cam.use_fog)) {
engine_info.add_log("[Config] Environmental fog %s", cam.use_fog ? "enabled" : "disabled");
should_restart = true;
}
if (cam.use_fog) {
if (ImGui::SliderFloat("Density", &cam.fog_density, 0.0001f, 0.05f, "%.4f", ImGuiSliderFlags_Logarithmic)) {
should_restart = true;
}
if (ImGui::IsItemDeactivatedAfterEdit()) {
engine_info.add_log("[Config] Fog density finalized at %.4f", cam.fog_density);
}
if (ImGui::ColorEdit3("Fog Color", cam.fog_color)) {
engine_info.add_log("[Config] Fog color set to (%.2f, %.2f, %.2f)",
cam.fog_color[0],
cam.fog_color[1],
cam.fog_color[2]
);
should_restart = true;
}
}
}
//HDRI mode settings
if (env.mode() == EnvironmentSettings::HDR_MAP) {
if (!ImGui::IsAnyItemActive()) {
rot_deg = static_cast<float>(radians_to_degrees(env.hdri_rotation));
tilt_deg = static_cast<float>(radians_to_degrees(env.hdri_tilt));
roll_deg = static_cast<float>(radians_to_degrees(env.hdri_roll));
}
ImGui::TextColored(ImVec4(0.4f, 0.8f, 1.0f, 1.0f), "HDR Map Settings");
//dynamic hdr loading
ImGui::Text("Active HDR map: %s", env.current_hdr_name.c_str());
if (ImGui::BeginCombo("Select HDR", env.current_hdr_name.c_str())) {
for (int n = 0; n < cam.hdr_files.size(); n++) {
const bool is_selected = (env.current_hdr_name == cam.hdr_files[n]);
if (ImGui::Selectable(cam.hdr_files[n].c_str(), is_selected)) {
//loading
env.load_hdr(HDR_DIR + cam.hdr_files[n]);
engine_info.add_log("[Config] Loaded HDR map: %s", cam.hdr_files[n].c_str());
//render reset
cam.reset_accumulator();
should_restart = true;
//rotation parameters reset for a new hdr map
rot_deg = 0.0f;
tilt_deg = 0.0f;
roll_deg = 0.0f;
env.hdri_rotation = 0.0;
env.hdri_tilt = 0.0;
env.hdri_roll = 0.0;
}
if (is_selected) {
ImGui::SetItemDefaultFocus();
}
}
ImGui::EndCombo();
}
if (ImGui::Button("Refresh Folder")) {
engine_info.add_log("[Debug] Refreshing HDR map list from folder");
cam.refresh_hdr_list();
}
ImGui::Separator();
if (ImGui::Button("Reset Orientation")) {
engine_info.add_log("[Config] HDRI orientation reset to default");
//reset parameters to default
rot_deg = 0.0f;
tilt_deg = 0.0f;
roll_deg = 0.0f;
env.hdri_rotation = 0.0;
env.hdri_tilt = 0.0;
env.hdri_roll = 0.0;
should_restart = true;
}
//Y-axis rotation
if (ImGui::SliderFloat("Rotation (Y-axis)", &rot_deg, -180.0f, 180.0f)) {
env.hdri_rotation = degrees_to_radians(rot_deg);
should_restart = true;
}
if (ImGui::IsItemDeactivatedAfterEdit()) {
engine_info.add_log("[Config] HDRI rotation finalized at %.1f degrees", rot_deg);
}
//X-axis tilt
if (ImGui::SliderFloat("Tilt (X-axis)", &tilt_deg, -90.0f, 90.0f)) {
env.hdri_tilt = degrees_to_radians(tilt_deg);
should_restart = true;
}
if (ImGui::IsItemDeactivatedAfterEdit()) {
engine_info.add_log("[Config] HDRI rotation finalized at %.1f degrees", tilt_deg);
}
//Z-axis roll
if (ImGui::SliderFloat("Roll (Z-axis)", &roll_deg, -180.0f, 180.0f)) {
env.hdri_roll = degrees_to_radians(roll_deg);
should_restart = true;
}
if (ImGui::IsItemDeactivatedAfterEdit()) {
engine_info.add_log("[Config] HDRI rotation finalized at %.1f degrees", roll_deg);
}
}
//physical Sun mode settings
if (env.mode() == EnvironmentSettings::PHYSICAL_SUN) {
ImGui::TextColored(ImVec4(1.0f, 0.9f, 0.4f, 1.0f), "Physical Sun & Sky");
//sun color
//checkbox for sun color blockage
if (ImGui::Checkbox("Auto Sun Color (Atmospheric)", &env.auto_sun_color)) {
engine_info.add_log("[Config] Automatic sun color based on elevation %s", env.auto_sun_color ? "enabled" : "disabled");
should_restart = true;
}
//visual blockage for ColorEdit
if (env.auto_sun_color) {
ImGui::BeginDisabled();
}
if (ImGui::ColorEdit3("Sun Color", sun_col)) {
env.sun_color = color(sun_col[0], sun_col[1], sun_col[2]);
engine_info.add_log("[Config] Sun color set to (%.2f, %.2f, %.2f)",
env.sun_color.x(),
env.sun_color.y(),
env.sun_color.z()
);
should_restart = true;
}
//end sun color blockage
if (env.auto_sun_color) {
ImGui::EndDisabled();
}
//sun intensity
float sun_int_f = static_cast<float>(env.sun_intensity);
if (ImGui::SliderFloat("Sun Intensity", &sun_int_f, 0.0f, 20.0f)) {
env.sun_intensity = static_cast<double>(sun_int_f);
should_restart = true;
}
if (ImGui::IsItemDeactivatedAfterEdit()) {
engine_info.add_log("[Config] Sun intensity finalized at %.2f", env.sun_intensity);
}
//sun size
float sun_size_f = static_cast<float>(env.sun_size);
if (ImGui::SliderFloat("Sun Size", &sun_size_f, 0.0f, 10.0f)) {
env.sun_size = static_cast<double>(sun_size_f);
should_restart = true;
}
if (ImGui::IsItemDeactivatedAfterEdit()) {
engine_info.add_log("[Config] Sun size finalized at %.2f", env.sun_size);
}
ImGui::Separator();
//mode switch
static bool use_astro = false;
static bool last_use_astro = false;
if (ImGui::Checkbox("Use daylight System (time/data)", &use_astro)) {
if (use_astro != last_use_astro) {
engine_info.add_log("[System] Daylight system %s", use_astro ? "enabled" : "disabled");
last_use_astro = use_astro;
}
}
if (use_astro) {
//static variable for converter
static float sun_time = 12.0f; //0-24 hours
static int sun_day = 172; //day of the year (1-365, 172 is the summer solstice)
static float latitude = 52.0f; //latitude (set as default Poland 52)
bool astro_changed = false;
astro_changed |= ImGui::SliderFloat("Hour", &sun_time, 0.0f, 24.0f, "%.2f h");
astro_changed |= ImGui::SliderInt("Day", &sun_day, 1, 365);
astro_changed |= ImGui::SliderFloat("Latitude", &latitude, -90.0f, 90.0f, "%.1f deg");
if (astro_changed) {
//astronomical maths
double declination = 23.45 * std::sin(degrees_to_radians(360.0 / 365.0 * (sun_day - 81)));
double hour_angle = (sun_time - 12.0) * 15.0;
double lat_rad = degrees_to_radians(latitude);
double dec_rad = degrees_to_radians(declination);
double ha_rad = degrees_to_radians(hour_angle);
double sin_el = std::sin(lat_rad) * std::sin(dec_rad) +
std::cos(lat_rad) * std::cos(dec_rad) * std::cos(ha_rad);
double elevation = radians_to_degrees(std::asin(std::clamp(sin_el, -1.0, 1.0)));
double cos_az = (std::sin(dec_rad) - std::sin(lat_rad) * sin_el) /
(std::cos(lat_rad) * std::cos(std::asin(sin_el)));
double azimuth = radians_to_degrees(std::acos(std::clamp(cos_az, -1.0, 1.0)));
if (hour_angle > 0) {
azimuth = 360.0 - azimuth;
engine_info.add_log("[Debug] Sun is in the afternoon, adjusting azimuth to %.1f degrees", azimuth);
}
//update directional vector
env.sun_direction = direction_from_spherical(elevation, azimuth);
//automatic sun color
if (env.auto_sun_color) {
double s_height = env.sun_direction.y();
//higher the sun more red less blue
double warm_factor = std::clamp(1.0 - s_height * 2.0, 0.0, 1.0);
env.sun_color = color(1.0, 0.9 - warm_factor * 0.6, 0.8 - warm_factor * 0.8);
//update color buffer
sun_col[0] = (float)env.sun_color.x();
sun_col[1] = (float)env.sun_color.y();
sun_col[2] = (float)env.sun_color.z();
engine_info.add_log("[Config] Sun color automatically updated to (%.2f, %.2f, %.2f) based on elevation",
env.sun_color.x(),
env.sun_color.y(),
env.sun_color.z()
);
}
//synchronize buffer
sun_dir_buf[0] = env.sun_direction.x();
sun_dir_buf[1] = env.sun_direction.y();
sun_dir_buf[2] = env.sun_direction.z();
engine_info.add_log("[Debug] Synchronizing sun direction buffer to (%.2f, %.2f, %.2f)",
sun_dir_buf[0],
sun_dir_buf[1],
sun_dir_buf[2]
);
should_restart = true;
}
if (ImGui::IsItemDeactivatedAfterEdit()) {
engine_info.add_log("[Config] Daylight system parameters finalized: Hour=%.2f, Day=%d, Latitude=%.1f",
sun_time,
sun_day,
latitude
);
}
}
//sun direction
if (use_astro) {
ImGui::BeginDisabled();
}
ImGui::Text("Sun Direction (Manual)");
if (ImGui::InputScalarN("##sun_dir_input", ImGuiDataType_Double, sun_dir_buf, 3)) {
env.sun_direction = vec3(sun_dir_buf[0], sun_dir_buf[1], sun_dir_buf[2]);
engine_info.add_log("[Config] Sun direction manually set to (%.2f, %.2f, %.2f)",
env.sun_direction.x(),
env.sun_direction.y(),
env.sun_direction.z()
);
should_restart = true;
}
//button to normalize the sun direction vector
ImGui::SameLine();
if (ImGui::Button("Normalize")) {
env.sun_direction = unit_vector(env.sun_direction);
//update buffer to normalize values
sun_dir_buf[0] = env.sun_direction.x();
sun_dir_buf[1] = env.sun_direction.y();
sun_dir_buf[2] = env.sun_direction.z();
engine_info.add_log("[Config] Sun direction vector normalized to (%.2f, %.2f, %.2f)",
env.sun_direction.x(),
env.sun_direction.y(),
env.sun_direction.z()
);
should_restart = true;
}
if (use_astro) {
ImGui::EndDisabled();
ImGui::TextDisabled("(Controlled by Daylight System)");
}
}
//solid background color
if (env._mode == EnvironmentSettings::SOLID_COLOR) {
ImGui::TextColored(ImVec4(0.7f, 0.7f, 0.7f, 1.0f), "Solid Color Mode");
if (ImGui::ColorEdit3("Background Color", bg_col)) {
env.background_color = color(bg_col[0], bg_col[1], bg_col[2]);
should_restart = true;
}
if (ImGui::IsItemDeactivatedAfterEdit()) {
engine_info.add_log("[Config] Background color finalized at (%.2f, %.2f, %.2f)",
env.background_color.x(),
env.background_color.y(),
env.background_color.z()
);
}
}
ImGui::EndTabItem();
}
//post-processing tab
if (ImGui::BeginTabItem("Post-Process")) {
//buffers definition
static float col_bal[3] = {
(float)my_post.color_balance.x(),
(float)my_post.color_balance.y(),
(float)my_post.color_balance.z()
};
//synchronization
if (!ImGui::IsItemActive()) {
col_bal[0] = (float)my_post.color_balance.x();
col_bal[1] = (float)my_post.color_balance.y();
col_bal[2] = (float)my_post.color_balance.z();
}
ImGui::SeparatorText("Debug Views");
//auxiliary function
auto DebugToggle = [&](const char* id, bool& flag, ImVec4 col) {
int pushed_colors = 0;
int pushed_vars = 0;
if (flag) {
//highligt active debug mode with brighter color and border
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(col.x * 1.2f, col.y * 1.2f, col.z * 1.2f, 1.0f));
pushed_colors++;
ImGui::PushStyleColor(ImGuiCol_Border, ImVec4(1, 1, 1, 1));
pushed_colors++;
ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1.0f);
pushed_vars++;
} else {
ImGui::PushStyleColor(ImGuiCol_Button, col);
pushed_colors++;
}
if (ImGui::Button(id, ImVec2(30, 30))) {
flag = !flag; //toggle the debug flag
engine_info.add_log("[Debug] Channel %s toggled: %s", id, flag ? "ON" : "OFF");
//if color debug mode is activated, deactivate luminance and vice versa
if (&flag == &my_post.debug.luminance) {
if (my_post.debug.luminance) {
//if L on = R, G, B off
my_post.debug.red = my_post.debug.green = my_post.debug.blue = false;
} else {
//if L off = R, G, B on
my_post.debug.red = my_post.debug.green = my_post.debug.blue = true;
}
} else {
//if any of the R,G,B on == L off