-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
1887 lines (1652 loc) · 81.6 KB
/
main.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
#define VERSION "0.3"
#ifndef _DEBUG
#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
#endif
#pragma warning(disable:26495)
#define IMGUI_DEFINE_MATH_OPERATORS
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#define _USE_MATH_DEFINES
#define NOMINMAX
#include <Windows.h>
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include <imgui_internal.h>
#include <implot.h>
#include <stb/stb_image.h>
#include <stb/stb_image_resize2.h>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/rotate_vector.hpp>
#include <glm/gtx/vector_angle.hpp>
#include <glm/gtx/string_cast.hpp>
#include <filesystem>
#include <exception>
#include <string>
#include <vector>
#include <random>
#include <iostream>
#include <math.h>
#include <bitset>
#include <limits>
#include <functional>
#include <locale>
#include <codecvt>
void GLAPIENTRY glMessageCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam) {
if (type != GL_DEBUG_TYPE_ERROR) return;
fprintf(stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n", "** GL ERROR **", type, severity, message);
}
namespace ImGui {
ImFont* font;
static bool ToggleButton(const char* name, bool* v) {
ImVec2 p = ImGui::GetCursorScreenPos();
ImDrawList* draw_list = ImGui::GetWindowDrawList();
float height = ImGui::GetFrameHeight();
float width = height * 1.8f;
float radius = height * 0.50f;
float rounding = 0.4f;
ImGui::InvisibleButton(name, ImVec2(width, height));
if (ImGui::IsItemClicked()) {
*v ^= 1;
return true;
}
ImGuiContext& gg = *GImGui;
float ANIM_SPEED = 0.055f;
if (gg.LastActiveId == gg.CurrentWindow->GetID(name))
float t_anim = ImSaturate(gg.LastActiveIdTimer / ANIM_SPEED);
if (ImGui::IsItemHovered())
draw_list->AddRectFilled(p, ImVec2(p.x + width, p.y + height), ImGui::GetColorU32(ImVec4(0.2196f, 0.2196f, 0.2196f, 1.0f)), height * rounding);
else
draw_list->AddRectFilled(p, ImVec2(p.x + width, p.y + height), ImGui::GetColorU32(*v ? ImVec4(0.2196f, 0.2196f, 0.2196f, 1.0f) : ImVec4(0.08f, 0.08f, 0.08f, 1.0f)), height * rounding);
ImVec2 center = ImVec2(radius + (*v ? 1 : 0) * (width - radius * 2.0f), radius);
draw_list->AddRectFilled(ImVec2((p.x + center.x) - 9.0f, p.y + 1.5f),
ImVec2((p.x + (width / 2) + center.x) - 9.0f, p.y + height - 1.5f), IM_COL32(255, 255, 255, 255), height * rounding);
ImGui::SameLine(p.x + 22.f);
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + 3.f);
ImGui::Text(name);
return false;
}
static void HelpMarker(const char* desc) {
ImGui::TextDisabled("(?)");
if (ImGui::BeginItemTooltip()) {
ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
ImGui::TextUnformatted(desc);
ImGui::PopTextWrapPos();
ImGui::EndTooltip();
}
}
inline static void load_theme() {
ImGui::StyleColorsDark();
ImVec4* colors = ImGui::GetStyle().Colors;
colors[ImGuiCol_Text] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
colors[ImGuiCol_TextDisabled] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f);
colors[ImGuiCol_WindowBg] = ImVec4(0.10f, 0.10f, 0.10f, 1.00f);
colors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
colors[ImGuiCol_PopupBg] = ImVec4(0.19f, 0.19f, 0.19f, 0.92f);
colors[ImGuiCol_Border] = ImVec4(0.19f, 0.19f, 0.19f, 0.29f);
colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.24f);
colors[ImGuiCol_FrameBg] = ImVec4(0.05f, 0.05f, 0.05f, 0.54f);
colors[ImGuiCol_FrameBgHovered] = ImVec4(0.19f, 0.19f, 0.19f, 0.54f);
colors[ImGuiCol_FrameBgActive] = ImVec4(0.20f, 0.22f, 0.23f, 1.00f);
colors[ImGuiCol_TitleBg] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
colors[ImGuiCol_TitleBgActive] = ImVec4(0.06f, 0.06f, 0.06f, 1.00f);
colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
colors[ImGuiCol_MenuBarBg] = ImVec4(0.14f, 0.14f, 0.14f, 1.00f);
colors[ImGuiCol_ScrollbarBg] = ImVec4(0.05f, 0.05f, 0.05f, 0.54f);
colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.34f, 0.34f, 0.34f, 0.54f);
colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.40f, 0.40f, 0.40f, 0.54f);
colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.56f, 0.56f, 0.56f, 0.54f);
colors[ImGuiCol_CheckMark] = ImVec4(0.33f, 0.67f, 0.86f, 1.00f);
colors[ImGuiCol_SliderGrab] = ImVec4(0.34f, 0.34f, 0.34f, 0.54f);
colors[ImGuiCol_SliderGrabActive] = ImVec4(0.56f, 0.56f, 0.56f, 0.54f);
colors[ImGuiCol_Button] = ImVec4(0.25f, 0.25f, 0.25f, 0.54f);
colors[ImGuiCol_ButtonHovered] = ImVec4(0.39f, 0.39f, 0.39f, 0.54f);
colors[ImGuiCol_ButtonActive] = ImVec4(0.30f, 0.32f, 0.33f, 1.00f);
colors[ImGuiCol_Header] = ImVec4(0.00f, 0.00f, 0.00f, 0.52f);
colors[ImGuiCol_HeaderHovered] = ImVec4(0.00f, 0.00f, 0.00f, 0.36f);
colors[ImGuiCol_HeaderActive] = ImVec4(0.20f, 0.22f, 0.23f, 0.33f);
colors[ImGuiCol_Separator] = ImVec4(0.28f, 0.28f, 0.28f, 0.29f);
colors[ImGuiCol_SeparatorHovered] = ImVec4(0.44f, 0.44f, 0.44f, 0.29f);
colors[ImGuiCol_SeparatorActive] = ImVec4(0.40f, 0.44f, 0.47f, 1.00f);
colors[ImGuiCol_ResizeGrip] = ImVec4(0.28f, 0.28f, 0.28f, 0.29f);
colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.44f, 0.44f, 0.44f, 0.29f);
colors[ImGuiCol_ResizeGripActive] = ImVec4(0.40f, 0.44f, 0.47f, 1.00f);
colors[ImGuiCol_Tab] = ImVec4(0.00f, 0.00f, 0.00f, 0.52f);
colors[ImGuiCol_TabHovered] = ImVec4(0.14f, 0.14f, 0.14f, 1.00f);
colors[ImGuiCol_TabActive] = ImVec4(0.20f, 0.20f, 0.20f, 0.36f);
colors[ImGuiCol_TabUnfocused] = ImVec4(0.00f, 0.00f, 0.00f, 0.52f);
colors[ImGuiCol_TabUnfocusedActive] = ImVec4(0.14f, 0.14f, 0.14f, 1.00f);
colors[ImGuiCol_DockingPreview] = ImVec4(0.33f, 0.67f, 0.86f, 1.00f);
colors[ImGuiCol_DockingEmptyBg] = ImVec4(1.00f, 0.00f, 0.00f, 1.00f);
colors[ImGuiCol_PlotLines] = ImVec4(1.00f, 0.00f, 0.00f, 1.00f);
colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.00f, 0.00f, 1.00f);
colors[ImGuiCol_PlotHistogram] = ImVec4(1.00f, 0.00f, 0.00f, 1.00f);
colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.00f, 0.00f, 1.00f);
colors[ImGuiCol_TableHeaderBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.52f);
colors[ImGuiCol_TableBorderStrong] = ImVec4(0.00f, 0.00f, 0.00f, 0.52f);
colors[ImGuiCol_TableBorderLight] = ImVec4(0.28f, 0.28f, 0.28f, 0.29f);
colors[ImGuiCol_TableRowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
colors[ImGuiCol_TableRowBgAlt] = ImVec4(1.00f, 1.00f, 1.00f, 0.06f);
colors[ImGuiCol_TextSelectedBg] = ImVec4(0.20f, 0.22f, 0.23f, 1.00f);
colors[ImGuiCol_DragDropTarget] = ImVec4(0.33f, 0.67f, 0.86f, 1.00f);
colors[ImGuiCol_NavHighlight] = ImVec4(1.00f, 0.00f, 0.00f, 1.00f);
colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 0.00f, 0.00f, 0.70f);
colors[ImGuiCol_NavWindowingDimBg] = ImVec4(1.00f, 0.00f, 0.00f, 0.20f);
colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.70f);
ImGuiStyle& style = ImGui::GetStyle();
style.WindowPadding = ImVec2(8.00f, 8.00f);
style.FramePadding = ImVec2(5.00f, 2.00f);
style.CellPadding = ImVec2(6.00f, 6.00f);
style.ItemSpacing = ImVec2(6.00f, 6.00f);
style.ItemInnerSpacing = ImVec2(6.00f, 6.00f);
style.TouchExtraPadding = ImVec2(0.00f, 0.00f);
style.IndentSpacing = 25;
style.ScrollbarSize = 15;
style.GrabMinSize = 10;
style.WindowBorderSize = 1;
style.ChildBorderSize = 1;
style.PopupBorderSize = 1;
style.FrameBorderSize = 1;
style.TabBorderSize = 1;
style.WindowRounding = 7;
style.ChildRounding = 4;
style.FrameRounding = 3;
style.PopupRounding = 4;
style.ScrollbarRounding = 9;
style.GrabRounding = 3;
style.LogSliderDeadzone = 4;
style.TabRounding = 4;
}
}
class Error : public std::exception {
wchar_t msg[1024];
public:
Error(const wchar_t* message) {
wcsncpy_s(msg, message, 18);
this->display();
}
Error(const char* message) {
mbstowcs_s(nullptr, msg, message, 1024);
this->display();
}
void display() {
MessageBoxW(NULL, msg, L"Error", MB_OK | MB_ICONERROR);
}
};
#include "resource.h"
class Shader {
GLuint vertexShader;
GLuint fragmentShader;
GLuint vao, vbo;
GLuint textureID;
float vertices[12] = {
-1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f,
-1.0f, -1.0f,
1.0f, 1.0f,
1.0f, -1.0f
};
protected:
static inline char* read_resource(int name, DWORD* size = nullptr) {
HMODULE handle = GetModuleHandleW(NULL);
HRSRC rc = FindResourceW(handle, MAKEINTRESOURCE(name), MAKEINTRESOURCE(TEXTFILE));
if (rc == NULL) return nullptr;
HGLOBAL rcData = LoadResource(handle, rc);
if (rcData == NULL) return nullptr;
DWORD s = SizeofResource(handle, rc);
char* res = new char[s + 1];
memcpy(res, static_cast<const char*>(LockResource(rcData)), s);
res[s] = '\0';
if (size) *size = s;
return res;
}
static inline void check_for_errors(GLuint shader) {
int success;
char infoLog[1024];
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
throw Error(infoLog);
}
}
public:
GLuint id = 0;
inline glm::vec3 load_textures(int tex) {
char buffer[MAX_PATH] = { 0 };
GetModuleFileNameA(NULL, buffer, MAX_PATH);
std::string::size_type pos = std::string(buffer).find_last_of("\\/");
int w, h, nrChannels;
unsigned char* data;
auto blankdata = new unsigned char[16 * 16 * 3] {};
glm::vec3 avgColor = glm::vec3(0.f);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_CUBE_MAP, this->textureID);
std::string root = std::string(buffer).substr(0, pos);
for (int i = 0; i < 6; i++) {
std::string path = std::format("{}\\assets\\{}{}.jpg", root, tex, i);
data = stbi_load(path.c_str(), &w, &h, &nrChannels, 0);
if (!data && std::filesystem::exists(std::format("{}\\assets", root))) {
throw Error("Skybox not available");
}
else if (!data) {
for (i = 0; i < 6; i++) glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, 16, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, blankdata);
return glm::vec3(0.f);
}
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
for (int i = 0; i < w * nrChannels; i += nrChannels) {
for (int j = 0; j < h; j++) {
int base = w * j + i;
avgColor += glm::vec3(data[base] / 255.f, data[base + 1] / 255.f, data[base + 2] / 255.f);
}
}
stbi_image_free(data);
}
return avgColor /= static_cast<float>(w * h * 6);
}
inline virtual void create() {
if (id) free();
char* vertexSource = read_resource(IDR_VRTX);
char* fragmentSource = read_resource(IDR_FRAG);
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexSource, NULL);
glCompileShader(vertexShader);
check_for_errors(vertexShader);
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
glCompileShader(fragmentShader);
check_for_errors(fragmentShader);
delete[] vertexSource;
delete[] fragmentSource;
id = glCreateProgram();
glAttachShader(id, vertexShader);
glAttachShader(id, fragmentShader);
glLinkProgram(id);
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), nullptr);
glActiveTexture(GL_TEXTURE1);
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
glm::vec3 ambientLight = load_textures(0);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glUniform3f(glGetUniformLocation(id, "ambientLight"), ambientLight.r, ambientLight.g, ambientLight.b);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
glUniform1i(glGetUniformLocation(id, "skybox"), 0);
}
inline virtual void use() const {
glUseProgram(id);
glBindVertexArray(vao);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
}
inline void free() { glDeleteProgram(id); }
};
class PostProcessing : public Shader {
public:
GLuint fbo;
GLuint vertexShader;
GLuint upsampleShader;
GLuint dwsampleShader;
GLuint displaytShader;
GLuint upsampleProgramID;
GLuint dwsampleProgramID;
GLuint displaytProgramID;
GLuint vao, vbo;
float vertices[24] = {
-1.0f, -1.0f, 0.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f, 0.0f
};
struct bloomMip {
glm::vec2 size;
glm::ivec2 intSize;
unsigned int texture;
};
glm::ivec2 screenSize;
int mipChainLength;
std::vector<bloomMip> mMipChain;
inline PostProcessing(int w, int h, int length) : mipChainLength(length) {
screenSize = { w, h };
}
inline virtual void create() override {
char* fullquadSource = read_resource(IDR_FULLQUAD);
char* upsampleSource = read_resource(IDR_UPSAMPLE);
char* dwsampleSource = read_resource(IDR_DWSAMPLE);
char* displaytSource = read_resource(IDR_DISPLAYT);
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &fullquadSource, NULL);
glCompileShader(vertexShader);
check_for_errors(vertexShader);
upsampleShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(upsampleShader, 1, &upsampleSource, NULL);
glCompileShader(upsampleShader);
check_for_errors(upsampleShader);
dwsampleShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(dwsampleShader, 1, &dwsampleSource, NULL);
glCompileShader(dwsampleShader);
check_for_errors(dwsampleShader);
displaytShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(displaytShader, 1, &displaytSource, NULL);
glCompileShader(displaytShader);
check_for_errors(displaytShader);
upsampleProgramID = glCreateProgram();
glAttachShader(upsampleProgramID, vertexShader);
glAttachShader(upsampleProgramID, upsampleShader);
glLinkProgram(upsampleProgramID);
dwsampleProgramID = glCreateProgram();
glAttachShader(dwsampleProgramID, vertexShader);
glAttachShader(dwsampleProgramID, dwsampleShader);
glLinkProgram(dwsampleProgramID);
displaytProgramID = glCreateProgram();
glAttachShader(displaytProgramID, vertexShader);
glAttachShader(displaytProgramID, displaytShader);
glLinkProgram(displaytProgramID);
glDeleteShader(vertexShader);
glDeleteShader(upsampleShader);
glDeleteShader(dwsampleShader);
glUseProgram(upsampleProgramID);
glUniform1i(glGetUniformLocation(upsampleProgramID, "srcTexture"), 0);
glUseProgram(dwsampleProgramID);
glUniform1i(glGetUniformLocation(dwsampleProgramID, "srcTexture"), 0);
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), nullptr);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
glEnableVertexAttribArray(1);
glBindVertexArray(0);
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glm::vec2 mipSize = screenSize;
glm::ivec2 mipIntSize = static_cast<glm::ivec2>(screenSize);
for (int i = 0; i < mipChainLength; i++) {
bloomMip mip;
mip.size = mipSize;
mip.intSize = mipIntSize;
glGenTextures(1, &mip.texture);
glBindTexture(GL_TEXTURE_2D, mip.texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R11F_G11F_B10F, (int)mipSize.x, (int)mipSize.y, 0, GL_RGBA, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
mipSize *= 0.5f;
mipIntSize /= 2;
mMipChain.emplace_back(mip);
}
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mMipChain[0].texture, 0);
unsigned int attachments[1] = { GL_COLOR_ATTACHMENT0 };
glDrawBuffers(1, attachments);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
throw Error("Error generating framebuffer for bloom shader");
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
inline void render(bool bloom, GLuint srcTexture, float threshold, float exposure, int accumulationFrameIndex) {
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
this->renderDownsamples(srcTexture, threshold, accumulationFrameIndex);
this->renderUpsamples(exposure, accumulationFrameIndex);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, screenSize.x, screenSize.y);
glUseProgram(displaytProgramID);
glUniform1i(glGetUniformLocation(displaytProgramID, "screenTexture"), 0);
glUniform2i(glGetUniformLocation(displaytProgramID, "screenSize"), screenSize.x, screenSize.y);
glBindVertexArray(vao);
glActiveTexture(GL_TEXTURE0);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_SRC_ALPHA);
glUniform1i(glGetUniformLocation(displaytProgramID, "accumulationFrameIndex"), accumulationFrameIndex);
glUniform1f(glGetUniformLocation(displaytProgramID, "transparency"), 0.0);
glBindTexture(GL_TEXTURE_2D, srcTexture);
glDrawArrays(GL_TRIANGLES, 0, 6);
if (bloom) {
glUniform1i(glGetUniformLocation(displaytProgramID, "accumulationFrameIndex"), -1);
glUniform1f(glGetUniformLocation(displaytProgramID, "transparency"), 1.0);
glBindTexture(GL_TEXTURE_2D, mMipChain[0].texture);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
glDisable(GL_BLEND);
glBindVertexArray(0);
}
inline void renderDownsamples(GLuint srcTexture, float threshold, int accumulationFrameIndex) {
glUseProgram(dwsampleProgramID);
glUniform2f(glGetUniformLocation(dwsampleProgramID, "srcResolution"), static_cast<float>(screenSize.x), static_cast<float>(screenSize.y));
glUniform1f(glGetUniformLocation(dwsampleProgramID, "threshold"), threshold);
glUniform1i(glGetUniformLocation(dwsampleProgramID, "accumulationFrameIndex"), accumulationFrameIndex);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, srcTexture);
glBindVertexArray(vao);
for (int i = 0; i < mMipChain.size(); i++) {
const bloomMip& mip = mMipChain[i];
glViewport(0, 0, static_cast<GLsizei>(mip.size.x), static_cast<GLsizei>(mip.size.y));
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mip.texture, 0);
glUniform1i(glGetUniformLocation(dwsampleProgramID, "depth"), i);
glDrawArrays(GL_TRIANGLES, 0, 6);
glUniform2f(glGetUniformLocation(dwsampleProgramID, "srcResolution"), mip.size.x, mip.size.y);
glBindTexture(GL_TEXTURE_2D, mip.texture);
}
glBindVertexArray(0);
glUseProgram(0);
}
inline void renderUpsamples(float exposure, int accumulationFrameIndex) {
glUseProgram(upsampleProgramID);
glUniform1f(glGetUniformLocation(upsampleProgramID, "exposure"), exposure);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
glBindVertexArray(vao);
for (size_t i = mMipChain.size() - 1; i > 0; i--) {
const bloomMip& mip = mMipChain[i];
const bloomMip& nextMip = mMipChain[i - 1];
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mip.texture);
glViewport(0, 0, static_cast<GLsizei>(nextMip.size.x), static_cast<GLsizei>(nextMip.size.y));
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, nextMip.texture, 0);
glUniform1i(glGetUniformLocation(upsampleProgramID, "depth"), static_cast<GLint>(i));
glDrawArrays(GL_TRIANGLES, 0, 6);
}
glBindVertexArray(0);
glDisable(GL_BLEND);
glUseProgram(0);
}
inline void free() {
for (int i = 0; i < mMipChain.size(); i++) {
glDeleteTextures(1, &mMipChain[i].texture);
mMipChain[i].texture = 0;
}
glDeleteFramebuffers(1, &fbo);
glDeleteProgram(upsampleProgramID);
glDeleteProgram(dwsampleProgramID);
}
};
class ComputeShader : public Shader {
GLuint computeShader;
public:
inline void create(bool insert_collisionCode) {
if (id) free();
int success;
char infoLog[1024];
DWORD cSize;
char* cs = read_resource(IDR_CMPT, &cSize);
char* collisionCode = read_resource(IDR_COLL);
char* computeSource = new char[cSize + 4096];
sprintf_s(computeSource, cSize + 4096, cs, insert_collisionCode ? collisionCode : "", insert_collisionCode ? collisionCode : "");
delete[] cs;
computeShader = glCreateShader(GL_COMPUTE_SHADER);
glShaderSource(computeShader, 1, &computeSource, NULL);
glCompileShader(computeShader);
glGetShaderiv(computeShader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(computeShader, 1024, NULL, infoLog);
throw Error(infoLog);
}
delete[] computeSource;
id = glCreateProgram();
glAttachShader(id, computeShader);
glLinkProgram(id);
glDeleteShader(computeShader);
}
inline virtual void use() const override {
glUseProgram(id);
}
};
struct Particle {
glm::vec3 pos;
glm::vec3 vel;
glm::vec3 acc;
float mass;
float temp;
float radius;
float axial_tilt;
float rotational_period;
float yaw;
glm::vec3 albedo;
glm::vec3 emissionColor;
glm::vec3 absorptionColor;
float luminosity;
float specularity;
float metallicity;
float translucency;
float refractive_index;
float blurriness;
int texture;
int normmap;
int specmap;
};
class Camera {
GLuint shader = NULL;
public:
glm::vec3 position = { 0.f, 0.f, 10.f };
glm::vec3 direction = { 0.f, 0.f, -1.f };
glm::vec3 upvector = { 0.f, 1.f, 0.f };
glm::vec3 localCoords;
glm::vec3 localUp;
float proximity = 5;
Particle* following;
float yaw = -90.0f;
float pitch = 0.0f;
float fov = 60.f;
float sensitivity = 0.1f;
float speed = 10.f;
glm::vec3 velocity = { 0.f, 0.f, 0.f };
float farPlane = 1e+6f;
float nearPlane = 0.0001f;
bool mouseLocked = false;
void projMat(float w, float h, const GLuint shaderID) {
shader = shaderID;
glm::mat4 view;
if (following)
view = glm::lookAt(position, position - localCoords, { 0.f, 1.f, 0.f });
else
view = glm::lookAt(position, position + direction, { 0.f, 1.f, 0.f });
auto proj = glm::perspective(glm::radians(fov), w / h, nearPlane, farPlane);
glUniformMatrix4fv(glGetUniformLocation(shader, "viewMatrix"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(shader, "projMatrix"), 1, GL_FALSE, glm::value_ptr(proj));
glUniformMatrix4fv(glGetUniformLocation(shader, "invViewMatrix"), 1, GL_FALSE, glm::value_ptr(glm::inverse(view)));
glUniformMatrix4fv(glGetUniformLocation(shader, "invProjMatrix"), 1, GL_FALSE, glm::value_ptr(glm::inverse(proj)));
}
void processInput(std::bitset<6> keys, float dt, Particle* p = nullptr) {
following = p;
if (!p) {
glm::vec3 upvec = direction;
upvec.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch + 90));
upvec.y = sin(glm::radians(pitch + 90));
upvec.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch + 90));
float ds = pow(0.0001f, dt);
velocity = glm::vec3(
(keys[0] && !keys[2] ? speed : (keys[2] && !keys[0] ? -speed : velocity.x * ds)),
(keys[4] && !keys[5] ? speed : (keys[5] && !keys[4] ? -speed : velocity.y * ds)),
(keys[1] && !keys[3] ? speed : (keys[3] && !keys[1] ? -speed : velocity.z * ds))
);
position += glm::vec3(
velocity.x * dt * direction +
velocity.y * dt * upvector +
velocity.z * dt * glm::cross(upvec, direction)
);
}
else {
proximity += speed * dt * (keys[0] && !keys[2] ? -1.f : (keys[2] && !keys[0] ? 1.f : 0.f));
if (proximity < 1.2f) proximity = 1.2f;
rotate(0, 0);
position = following->pos + localCoords;
}
glUniform3fv(glGetUniformLocation(shader, "cameraPos"), 1, glm::value_ptr(position));
}
void rotate(float xoffset, float yoffset) {
yaw += xoffset * sensitivity * (following ? -1.f : 1.f);
pitch += yoffset * sensitivity;
if (pitch > 89.0f) pitch = 89.0f;
if (pitch < -89.0f) pitch = -89.0f;
direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
direction.y = sin(glm::radians(pitch));
direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
direction = glm::normalize(direction);
if (following) {
localCoords.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
localCoords.y = sin(glm::radians(pitch));
localCoords.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
localCoords = glm::normalize(localCoords) * (following->radius * proximity);
}
}
};
struct Scene {
int num_particles = 100;
float min_distance = 2.5f;
float max_distance = 5.f;
float min_mass = 1e+6f;
float max_mass = 1e+7f;
float min_density = 10e+8f;
float max_density = 10e+9f;
float min_temperature = 0.f;
float max_temperature = 0.f;
float min_velocity = 0.5f;
float max_velocity = 1.0f;
bool orbital_velocity = true;
float planar_deviation = 45.f;
bool central_body = true;
float central_mass = 1e+10f;
float central_density = 2e+10f;
float central_temperature = 3e+3;
float central_luminosity = 15.f;
bool prevent_overlap = false;
};
struct Controls {
GLenum forward = GLFW_KEY_W;
GLenum backward = GLFW_KEY_S;
GLenum leftward = GLFW_KEY_A;
GLenum rightward = GLFW_KEY_D;
GLenum upward = GLFW_KEY_SPACE;
GLenum downward = GLFW_KEY_LEFT_SHIFT;
GLenum del = GLFW_KEY_DELETE;
GLenum camctrl = GLFW_KEY_LEFT_CONTROL;
GLenum pause = GLFW_KEY_Q;
GLenum reverse = GLFW_KEY_E;
GLenum speedup = GLFW_KEY_R;
GLenum speeddown = GLFW_KEY_F;
GLenum follow = GLFW_KEY_X;
};
class NBodiment {
public:
Shader* shader;
ComputeShader* cmptshader;
PostProcessing* pprocshader;
Camera camera;
Scene scene;
Controls controls;
GLuint ssbo;
GLuint fbo;
GLuint screenTexture;
GLFWwindow* window;
GLFWmonitor* monitor;
GLFWcursor* arrow;
GLFWcursor* hand;
std::vector<Particle> original;
std::vector<Particle> pBuffer;
float time;
std::vector<float> timeAxis;
std::vector<float> kinetic_energy;
float min_ke = FLT_MAX, max_ke = FLT_MIN;
glm::ivec2 res;
glm::ivec2 pos = { 60, 60 };
std::bitset<6> keys{ 0x0 };
glm::dvec2 prevMousePos{ 0.f, 0.f };
double lastSpeedChange = -5.0;
float timeStep = 1.0f;
bool paused = false;
bool was_paused = false;
bool reverse = false;
int collisionType = 0;
bool mutual_attraction = true;
int strongestAttractor = 0;
std::vector<std::string> textures{ "None", "Add new texture..." };
GLuint textureArray;
std::vector<std::string> specmaps{ "None", "Add new specular map..." };
GLuint specmapArray;
std::vector<std::string> normmaps{ "None", "Add new normal map... " };
GLuint normmapArray;
int hovering;
int selected = -1;
int following = -1;
bool hoveringParticle = false;
bool selectedParticle = false;
bool lockedToParticle = false;
glm::dvec2 mousePos;
int numRaysPerPixel = 10;
int renderer = 1;
bool shadows = true;
bool bloom = true;
float bloomThreshold = 1.f;
float exposure = 0.05f;
int accumulationFrameIndex = 0;
bool skybox = false;
int skyboxImage = 0;
Particle satellite;
float orbital_radius;
glm::vec3 orbital_velocity;
float orbital_period;
NBodiment() {
std::setlocale(LC_CTYPE, ".UTF8");
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
res = { mode->width, mode->height };
glfwWindowHint(GLFW_RED_BITS, mode->redBits);
glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
#ifdef _DEBUG
res = { 1280, 910 };
window = glfwCreateWindow(res.x, res.y, "NBodiment", NULL, NULL);
#else
window = glfwCreateWindow(res.x, res.y, "NBodiment", monitor, NULL);
#endif
if (window == nullptr)
throw Error("Failed to create OpenGL context");
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwSetWindowUserPointer(window, this);
if (glfwRawMouseMotionSupported()) glfwSetInputMode(window, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE);
glfwGetCursorPos(window, &prevMousePos.x, &prevMousePos.y);
if (!gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress)))
throw Error("Failed to load GLAD");
glEnable(GL_DEBUG_OUTPUT);
glGenBuffers(1, &ssbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, ssbo);
cmptshader = new ComputeShader();
cmptshader->create(collisionType != 2);
cmptshader->use();
glShaderStorageBlockBinding(cmptshader->id, glGetProgramResourceIndex(cmptshader->id, GL_SHADER_STORAGE_BLOCK, "vBuffer"), 0);
glUniform1i(glGetUniformLocation(cmptshader->id, "collisionType"), collisionType);
glUniform1i(glGetUniformLocation(cmptshader->id, "mutualAttraction"), mutual_attraction);
pprocshader = new PostProcessing(res.x, res.y, 8);
pprocshader->create();
shader = new Shader();
shader->create();
generate_scene();
shader->use();
glShaderStorageBlockBinding(shader->id, glGetProgramResourceIndex(shader->id, GL_SHADER_STORAGE_BLOCK, "vBuffer"), 0);
glUniform3f(glGetUniformLocation(shader->id, "cameraPos"), camera.position.x, camera.position.y, camera.position.z);
glUniform1i(glGetUniformLocation(shader->id, "numParticles"), static_cast<GLint>(pBuffer.size()));
glUniform1i(glGetUniformLocation(shader->id, "spp"), numRaysPerPixel);
glUniform1i(glGetUniformLocation(shader->id, "renderer"), renderer);
glUniform1i(glGetUniformLocation(shader->id, "shadows"), shadows);
glUniform2f(glGetUniformLocation(shader->id, "screenSize"), res.x, res.y);
glActiveTexture(GL_TEXTURE3);
glGenTextures(1, &textureArray);
glBindTexture(GL_TEXTURE_2D_ARRAY, textureArray);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_RGB8, 4096, 2048, 16);
glActiveTexture(GL_TEXTURE4);
glGenTextures(1, &normmapArray);
glBindTexture(GL_TEXTURE_2D_ARRAY, normmapArray);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_RGB8, 4096, 2048, 16);
glActiveTexture(GL_TEXTURE5);
glGenTextures(1, &specmapArray);
glBindTexture(GL_TEXTURE_2D_ARRAY, specmapArray);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_RGB8, 4096, 2048, 16);
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &screenTexture);
glBindTexture(GL_TEXTURE_2D, screenTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, res.x, res.y, 0, GL_RGBA, GL_FLOAT, NULL);
glBindTexture(GL_TEXTURE_2D, NULL);
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, screenTexture, 0);
glfwSetFramebufferSizeCallback(window, on_windowResize);
glfwSetMouseButtonCallback(window, on_mousePress);
glfwSetKeyCallback(window, on_keyPress);
glfwSetScrollCallback(window, on_mouseScroll);
glfwSetCursorPosCallback(window, on_mouseMove);
arrow = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
hand = glfwCreateStandardCursor(GLFW_HAND_CURSOR);
on_windowResize(window, res.x, res.y);
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.Fonts->AddFontDefault();
ImGui::font = io.Fonts->AddFontFromFileTTF("C:\\Windows\\Fonts\\consola.ttf", 11.f);
IM_ASSERT(ImGui::font != NULL);
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
io.IniFilename = NULL;
io.LogFilename = NULL;
ImGui::load_theme();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 460");
char buffer[MAX_PATH] = { 0 };
GetModuleFileNameA(NULL, buffer, MAX_PATH);
std::string::size_type pos = std::string(buffer).find_last_of("\\/");
std::string root = std::string(buffer).substr(0, pos);
skybox = std::filesystem::exists(std::format("{}\\assets", root));
camera = Camera();
}
static inline void on_windowResize(GLFWwindow* window, int w, int h) {
if (w == 0 || h == 0) return;
NBodiment* app = static_cast<NBodiment*>(glfwGetWindowUserPointer(window));
app->res = { w, h };
glViewport(0, 0, app->res.x, app->res.y);
glUniform2f(glGetUniformLocation(app->shader->id, "screenSize"), static_cast<float>(w), static_cast<float>(h));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, app->screenTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, app->res.x, app->res.y, 0, GL_RGBA, GL_FLOAT, NULL);
app->pprocshader->screenSize = app->res;
glm::vec2 mipSize = app->res;
glm::ivec2 mipIntSize = static_cast<glm::ivec2>(app->res);
for (PostProcessing::bloomMip& mip : app->pprocshader->mMipChain) {
mip.size = mipSize;
mip.intSize = mipIntSize;
glBindTexture(GL_TEXTURE_2D, mip.texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R11F_G11F_B10F, (int)mipSize.x, (int)mipSize.y, 0, GL_RGBA, GL_FLOAT, nullptr);
mipSize *= 0.5f;
mipIntSize /= 2;
}
app->accumulationFrameIndex = 0;
}
static inline void on_keyPress(GLFWwindow* window, int key, int scancode, int action, int mods) {
NBodiment* app = static_cast<NBodiment*>(glfwGetWindowUserPointer(window));
switch (action) {
case GLFW_PRESS: