Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
2883 lines (2173 loc) · 93.9 KB
/
Copy pathmain.cpp
File metadata and controls
2883 lines (2173 loc) · 93.9 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 <iostream>
#include <toolkit/render_window.hpp>
#include <toolkit/texture.hpp>
#include <vec/vec.hpp>
#include <GLFW/glfw3.h>
#include <SFML/Graphics.hpp>
#include <CL/cl_ext.h>
#include "metric.hpp"
#include "chromaticity.hpp"
#include <imgui/misc/freetype/imgui_freetype.h>
#include <imgui/imgui_internal.h>
#include "js_interop.hpp"
#include <toolkit/fs_helpers.hpp>
#include <filesystem>
#include "workshop/steam_ugc_manager.hpp"
#include "content_manager.hpp"
#include "equation_context.hpp"
#include "fullscreen_window_manager.hpp"
#include "raw_input.hpp"
#include "metric_manager.hpp"
#include "graphics_settings.hpp"
#include <networking/serialisable.hpp>
#include "input_manager.hpp"
#include "print.hpp"
#include "triangle.hpp"
#include "triangle_manager.hpp"
#include "physics.hpp"
#include "dynamic_feature_config.hpp"
#include "render_state.hpp"
#include <thread>
#include "common.cl"
//#include "dual_complex.hpp"
/**
Big list of general relativity references so i can shut some browser tabs
https://arxiv.org/pdf/1601.02063.pdf - GPU renderer
https://arxiv.org/pdf/1511.06025.pdf - CPU renderer - primary source from which this raytracer is derived
https://en.wikipedia.org/wiki/Frame_fields_in_general_relativity#Example:_Static_observers_in_Schwarzschild_vacuum - frame fields
https://www.spacetimetravel.org/wurmlochflug/wurmlochflug.html - renderings of wormholes
https://www.damtp.cam.ac.uk/user/hsr1000/lecturenotes_2012.pdf - lecture notes for relativity, misc everything
https://arxiv.org/pdf/0904.4184.pdf - spacetime catalogue, contains a bunch of metrics and tetrads
https://arxiv.org/pdf/1104.4829.pdf - gram schmidt orthonormalisation in a relativistic context
https://arxiv.org/pdf/1702.05802.pdf - double kerr (massless strut)
https://arxiv.org/ftp/arxiv/papers/1008/1008.3244.pdf - double kerr (massles strut)
https://arxiv.org/pdf/1702.02209.pdf - rotating double kerr with a massless strut
https://arxiv.org/pdf/1905.05273.pdf - janis-newman-winicour rendering + accretion disk
https://arxiv.org/pdf/1408.6041.pdf - alternative formulation of janis-newman-winicour line element that's less singularity inducing
http://www.roma1.infn.it/teongrav/VALERIA/TEACHING/ONDE_GRAV_STELLE_BUCHINERI/AA2012_13/Kerr.pdf - kerr info
http://cloud.yukterez.net/relativistic.raytracer/kerr.90.1720.png - kerr reference picture
https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods - runge kutta
https://physics.stackexchange.com/questions/409106/finding-the-metric-tensor-from-a-line-element - line element / metric tensor
https://arxiv.org/pdf/0706.0622.pdf - kerr spacetime coordinate systems
http://www.roma1.infn.it/teongrav/leonardo/bh/bhcap3.pdf - kerr, again
https://www.wolframalpha.com/input/?i=%28x%5E2+%2B+y%5E2%29+%2F+%28r%5E2+%2B+a%5E2%29+%2B+z%5E2%2Fr%5E2+%3D+1%2C+solve+for+r - solving for r in kerr-schild
https://arxiv.org/pdf/0807.0734.pdf - symplectic integrators
https://iopscience.iop.org/article/10.1088/0067-0049/184/2/387/pdf - radiative transport
https://javierrubioblog.files.wordpress.com/2015/12/chapter4.pdf - coordinate transforms
https://arxiv.org/pdf/1308.2298.pdf - double balanced kerr
https://www.researchgate.net/figure/Shadows-of-the-double-Schwarzschild-BH-solution-with-equal-masses-0-and-different-BH_fig1_323026854 - binary schwarzs
https://www2.mpia-hd.mpg.de/homes/tmueller/pdfs/catalogue_2014-05-21.pdf - loads of good information about everything, metric catalogue
https://www.sciencedirect.com/science/article/pii/S0370269319304563 - binary kerr
https://www.sciencedirect.com/science/article/pii/S0370269320307206 - binary kerr newman, equal
https://arxiv.org/abs/1502.03809 - the interstellar paper
https://arxiv.org/pdf/2110.00679.pdf - unequal binary kerr newman
https://arxiv.org/pdf/2110.04879.pdf - binary kerr newman, unequal, extremal
https://www.reed.edu/physics/courses/Physics411/html/page2/page2.html - some useful info
https://www.uio.no/studier/emner/matnat/astro/nedlagte-emner/AST1100/h11/undervisningsmateriale/lecture15.pdf - useful basic info
https://theconfused.me/blog/numerical-integration-of-light-paths-in-a-schwarzschild-metric/ - simple schwarzschild raytracer
https://github.com/stranger80/GraviRayTraceSharp/blob/master/GraviRayTraceSharp/ - raytracer with runge kutta integrator
https://en.wikipedia.org/wiki/Interior_Schwarzschild_metric - metric for the inside of a body
https://en.wikipedia.org/wiki/Vaidya_metric - radiating metric
https://en.wikipedia.org/wiki/Category:Exact_solutions_in_general_relativity - more exact solutions
https://en.wikipedia.org/wiki/Tetrad_formalism - tetrads/coordinate basis
https://arxiv.org/abs/gr-qc/0507014v1 - numerical relativity
https://arxiv.org/pdf/gr-qc/0104063.pdf - numerical relativity
https://en.wikipedia.org/wiki/Two-body_problem_in_general_relativity#Schwarzschild_solution - useful references on numerical relativity
https://en.wikipedia.org/wiki/Kerr%E2%80%93Newman_metric - kerr-newman (charged + rotating)
https://en.wikipedia.org/wiki/Cash%E2%80%93Karp_method - runge-kutta with adaptive error
https://drum.lib.umd.edu/bitstream/handle/1903/2202/2004-berry-healy-jas.pdf;jsessionid=B20F478B9DB479C86B9DD179A24331F3?sequence=7 - integration
https://www.pp.rhul.ac.uk/~cowan/ph2150/kepler_xy.pdf - good explanation of integration
https://core.ac.uk/download/pdf/1321518.pdf - numerical relativity phd
https://arxiv.org/pdf/gr-qc/9509020.pdf - numerical relativity
https://www.cec.uchile.cl/cinetica/pcordero/MC_libros/NumericalRecipesinC.pdf - 710
https://arxiv.org/pdf/0712.4333.pdf - coordinate system choices for schwarzschild and kerr (hyperboloidal)
https://iopscience.iop.org/article/10.1088/1361-6382/ab6e3e/pdf - another
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.1002.1336&rep=rep1&type=pdf
http://yukterez.net/ - loads of good stuff
https://scholarworks.rit.edu/cgi/viewcontent.cgi?article=11286&context=theses - lots more good stuff, numerical relativity
https://physics.stackexchange.com/questions/51915/can-one-raise-indices-on-covariant-derivative-and-products-thereof - transforming covariant derivative indices
http://ccom.ucsd.edu/~lindblom/Talks/Milwaukee_14October2011.pdf - simple introduction to numerical relativity
http://ccom.ucsd.edu/~lindblom/Talks/NRBeijing1.pdf - seems to be more up to date
https://www.aanda.org/articles/aa/pdf/2012/09/aa19599-12.pdf - radiative transfer
https://arxiv.org/pdf/0704.0986.pdf - tetrad info
https://www.researchgate.net/figure/View-of-a-static-observer-located-at-x-0-y-4-in-the-positive-y-direction-for-t_fig2_225428633 - alcubierre. Successfully managed to replicate this https://imgur.com/a/48SONjV. This paper is an absolute goldmine of useful information
https://arxiv.org/pdf/astro-ph/9707230.pdf - neutron star numerical relativity
https://www.aanda.org/articles/aa/full_html/2012/07/aa19209-12/aa19209-12.html - a* with a thin disk
https://gyoto.obspm.fr/GyotoManual.pdf - gyoto, general relativity tracer
https://core.ac.uk/download/pdf/25279526.pdf - binary black hole approximation?
https://hal.archives-ouvertes.fr/hal-01862911/document - natario warp drive
https://arxiv.org/pdf/1908.10757.pdf - good tetrad reference
https://academic.oup.com/ptep/article/2015/4/043E02/1524372 - interesting kerr coordinate system
"how do i convert rgb to wavelengths"
https://github.com/colour-science/smits1999
https://github.com/appleseedhq/appleseed/blob/54ce23fc940087180511cb5659d8a7aac33712fb/src/appleseed/foundation/image/colorspace.h#L956
https://github.com/wip-/RgbToSpectrum/blob/master/Spectra/SimpleSpectrum.cs
https://en.wikipedia.org/wiki/Dominant_wavelength
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.40.9608&rep=rep1&type=pdf
https://www.fourmilab.ch/documents/specrend/specrend.c
https://www.researchgate.net/publication/308305862_Relationship_between_peak_wavelength_and_dominant_wavelength_of_light_sources_based_on_vector-based_dominant_wavelength_calculation_method
https://www.semrock.com/how-to-calculate-luminosity-dominant-wavelength-and-excitation-purity.aspx
*/
///perfectly fine
vec4f cartesian_to_schwarz(vec4f position)
{
vec3f polar = cartesian_to_polar((vec3f){position.y(), position.z(), position.w()});
return (vec4f){position.x(), polar.x(), polar.y(), polar.z()};
}
#define GENERIC_METRIC
void execute_kernel(graphics_settings& sett,
cl::command_queue& cqueue, cl::buffer& rays_in,
cl::buffer& count_in,
cl::buffer& ray_time_min, cl::buffer& ray_time_max,
//cl::buffer& visual_path, cl::buffer& visual_ray_counts,
triangle_rendering::manager& manage,
physics& phys,
int num_rays,
bool use_device_side_enqueue,
dynamic_feature_config& dfg,
cl::buffer& dynamic_config,
cl::buffer& dynamic_feature_config,
int width, int height,
render_state& st,
single_render_state& single_state)
{
if(use_device_side_enqueue)
{
int fallback = 0;
cl::args run_args;
run_args.push_back(rays_in);
run_args.push_back(count_in);
run_args.push_back(fallback);
run_args.push_back(dynamic_config);
cqueue.exec("relauncher_generic", run_args, {1}, {1});
}
else
{
st.stored_ray_counts.set_to_zero(cqueue);
if(dfg.get_feature<bool>("use_triangle_rendering"))
{
cl_int my_min = INT_MAX;
cl_int my_max = INT_MIN;
ray_time_min.fill(cqueue, my_min);
ray_time_max.fill(cqueue, my_max);
}
int mouse_x = ImGui::GetMousePos().x;
int mouse_y = ImGui::GetMousePos().y;
cl::args run_args;
run_args.push_back(rays_in);
run_args.push_back(count_in);
run_args.push_back(ray_time_min);
run_args.push_back(ray_time_max);
run_args.push_back(dynamic_config);
run_args.push_back(dynamic_feature_config);
run_args.push_back(width);
run_args.push_back(height);
run_args.push_back(mouse_x);
run_args.push_back(mouse_y);
if(dfg.get_feature<bool>("use_triangle_rendering"))
run_args.push_back(single_state.stored_rays);
else
run_args.push_back(nullptr);
run_args.push_back(st.stored_ray_counts);
run_args.push_back(single_state.last_max_stored_rays);
cqueue.exec("do_generic_rays", run_args, {width*height}, {sett.workgroup_size[0]*sett.workgroup_size[1]});
}
}
int calculate_ray_count(int width, int height)
{
return height * width;
}
struct main_menu
{
enum windows
{
MAIN,
SETTINGS,
QUIT
};
graphics_settings sett;
background_settings background_sett;
int state = MAIN;
bool should_open = false;
bool is_open = true;
bool dirty_settings = false;
bool should_quit = false;
bool already_started = false;
bool load()
{
background_sett.path1 = "./backgrounds/nasa.png";
background_sett.path2 = "./backgrounds/nasa.png";
background_sett.load();
bool loaded_settings = false;
if(file::exists("settings.json"))
{
try
{
nlohmann::json js = nlohmann::json::parse(file::read("settings.json", file::mode::BINARY));
deserialise<graphics_settings>(js, sett, serialise_mode::DISK);
loaded_settings = true;
}
catch(std::exception& ex)
{
std::cout << "Failed to load settings.json " << ex.what() << std::endl;
}
}
return loaded_settings;
}
bool is_first_time_main_menu_open()
{
return is_open && !already_started;
}
void display_main_menu()
{
std::string start_string = already_started ? "Continue" : "Start";
if(ImGui::Button(start_string.c_str()))
{
already_started = true;
close();
}
if(ImGui::Button("Settings"))
{
state = SETTINGS;
}
if(ImGui::Button("Quit"))
{
state = QUIT;
}
}
void display_settings_menu(render_window& win, input_manager& input, background_images& bi)
{
if(ImGui::BeginTabBar("Tab Bar"))
{
if(ImGui::BeginTabItem("Video"))
{
dirty_settings |= sett.display_video_settings();
ImGui::EndTabItem();
}
if(ImGui::BeginTabItem("Controls"))
{
dirty_settings |= sett.display_control_settings();
ImGui::EndTabItem();
}
if(ImGui::BeginTabItem("Keybinds"))
{
input.display_key_rebindings(win);
ImGui::EndTabItem();
}
if(ImGui::BeginTabItem("Background"))
{
background_sett.display(bi);
ImGui::EndTabItem();
}
ImGui::EndTabBar();
}
if(ImGui::Button("Back"))
{
state = MAIN;
}
}
void display_quit_menu()
{
ImGui::Text("Are you sure?");
ImGui::SameLine();
if(ImGui::Button("Yes"))
{
should_quit = true;
}
ImGui::SameLine();
if(ImGui::Button("No"))
{
state = MAIN;
}
}
void close()
{
is_open = false;
}
void open()
{
should_open = true;
}
void poll_open()
{
if(should_open)
{
//ImGui::OpenPopup("Main Menu");
is_open = true;
}
should_open = false;
}
bool in_settings()
{
return state == SETTINGS && is_open;
}
void display(render_window& win, input_manager& input, background_images& bi)
{
if(state == SETTINGS)
{
vec2i dim = win.get_window_size();
vec2i menu_size = dim/2;
vec2i menu_tl = (dim / 2) - menu_size/2;
ImGui::SetNextWindowSize(ImVec2(dim.x()/2, dim.y()/2), ImGuiCond_Always);
ImVec2 tl = ImGui::GetMainViewport()->Pos;
ImGui::SetNextWindowPos(ImVec2(tl.x + menu_tl.x(), tl.y + menu_tl.y()), ImGuiCond_Always);
}
ImGui::Begin("Main Menu", nullptr, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse);
//if(ImGui::BeginPopupModal("Main Menu", nullptr, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse))
{
int current_state = state;
if(current_state == MAIN)
{
display_main_menu();
}
if(current_state == SETTINGS)
{
display_settings_menu(win, input, bi);
}
if(current_state == QUIT)
{
display_quit_menu();
}
if(ImGui::IsKeyPressed(GLFW_KEY_ESCAPE))
{
if(current_state == MAIN)
{
close();
}
state = MAIN;
}
ImVec2 viewport_size = ImGui::GetMainViewport()->Size;
ImVec2 viewport_pos = ImGui::GetMainViewport()->Pos;
ImVec2 window_size = ImGui::GetWindowSize();
ImGui::SetWindowPos({viewport_pos.x + viewport_size.x/2 - window_size.x/2, viewport_pos.y + viewport_size.y/2 - window_size.y/2});
ImGui::End();
//ImGui::EndPopup();
}
}
};
template<typename T>
struct read_queue
{
struct element
{
T data = T{};
cl::event evt;
cl::buffer gpu_buffer;
element(cl::context& ctx) : gpu_buffer(ctx){}
};
std::vector<element*> q;
void start_read(cl::context& ctx, cl::command_queue& async_q, cl::buffer&& buf, std::vector<cl::event> wait_on)
{
element* e = new element(ctx);
e->gpu_buffer = std::move(buf);
e->evt = e->gpu_buffer.read_async(async_q, (char*)&e->data, sizeof(T), wait_on);
q.push_back(e);
}
std::vector<std::pair<T, cl::buffer>> fetch()
{
std::vector<std::pair<T, cl::buffer>> ret;
for(int i=0; i < (int)q.size(); i++)
{
element* e = q[i];
if(e->evt.is_finished())
{
ret.push_back({e->data, std::move(e->gpu_buffer)});
q.erase(q.begin() + i);
i--;
delete e;
continue;
}
}
return ret;
}
};
template<typename T>
struct read_queue_pool
{
read_queue<T> q;
std::vector<cl::buffer> pool;
cl::buffer get_buffer(cl::context& ctx)
{
if(pool.size() > 4)
{
cl::buffer next = pool.front();
pool.erase(pool.begin());
return next;
}
cl::buffer buf(ctx);
buf.alloc(sizeof(T));
return buf;
}
void start_read(cl::context& ctx, cl::command_queue& async_q, cl::buffer&& buf, std::vector<cl::event> wait_on)
{
q.start_read(ctx, async_q, std::move(buf), wait_on);
}
std::vector<T> fetch()
{
std::vector<T> ret;
std::vector<std::pair<T, cl::buffer>> impl_fetch = q.fetch();
for(auto& [d, buf] : impl_fetch)
{
ret.push_back(std::move(d));
pool.push_back(buf);
}
return ret;
}
};
std::vector<triangle> make_cube(vec3f pos)
{
std::vector<triangle> tris;
float d = 0.5f;
{
triangle& t = tris.emplace_back();
t.set_vert(0, {0, 0, 0});
t.set_vert(1, {0+d, 0, 0});
t.set_vert(2, {0, 0, d});
}
{
triangle& t = tris.emplace_back();
t.set_vert(0, {0, 0, d});
t.set_vert(1, {0+d, 0, 0});
t.set_vert(2, {0+d, 0, d});
}
{
triangle& t = tris.emplace_back();
t.set_vert(0, {0, d, 0});
t.set_vert(1, {0+d, d, 0});
t.set_vert(2, {0, d, d});
}
{
triangle& t = tris.emplace_back();
t.set_vert(0, {0, d, d});
t.set_vert(1, {0+d, d, 0});
t.set_vert(2, {0+d, d, d});
}
{
triangle& t = tris.emplace_back();
t.set_vert(0, {0, 0, 0});
t.set_vert(1, {0, 0, d});
t.set_vert(2, {0, d, 0});
}
{
triangle& t = tris.emplace_back();
t.set_vert(0, {0, d, d});
t.set_vert(1, {0, 0, d});
t.set_vert(2, {0, d, 0});
}
{
triangle& t = tris.emplace_back();
t.set_vert(0, {0+d, d, d});
t.set_vert(1, {0+d, 0, d});
t.set_vert(2, {0+d, d, 0});
}
{
triangle& t = tris.emplace_back();
t.set_vert(0, {0+d, 0, 0});
t.set_vert(1, {0+d, 0, d});
t.set_vert(2, {0+d, d, 0});
}
{
triangle& t = tris.emplace_back();
t.set_vert(0, {0, 0, 0});
t.set_vert(1, {0+d, 0, 0});
t.set_vert(2, {0, d, 0});
}
{
triangle& t = tris.emplace_back();
t.set_vert(0, {0+d, d, 0});
t.set_vert(1, {0+d, 0, 0});
t.set_vert(2, {0, d, 0});
}
{
triangle& t = tris.emplace_back();
t.set_vert(0, {0, 0, d});
t.set_vert(1, {0+d, 0, d});
t.set_vert(2, {0, d, d});
}
{
triangle& t = tris.emplace_back();
t.set_vert(0, {0+d, d, d});
t.set_vert(1, {0+d, 0, d});
t.set_vert(2, {0, d, d});
}
for(triangle& t : tris)
{
t.v0x += pos.x();
t.v1x += pos.x();
t.v2x += pos.x();
t.v0y += pos.y();
t.v1y += pos.y();
t.v2y += pos.y();
t.v0z += pos.z();
t.v1z += pos.z();
t.v2z += pos.z();
}
return tris;
}
void DragFloatCol(const std::string& name, cl_float4& val, int highlight)
{
ImGui::BeginGroup();
ImGui::PushMultiItemsWidths(4, ImGui::CalcItemWidth());
for(int i=0; i < 4; i++)
{
if(highlight == i)
ImGui::PushStyleColor(ImGuiCol_FrameBg, ImVec4(0.8, 0.1, 0.1, 1));
ImGui::DragFloat(("###_" + std::to_string(i) + "_" + name).c_str(), &val.s[i]);
if(highlight == i && ImGui::IsItemHovered())
{
ImGui::SetTooltip("Timelike");
}
if(highlight == i)
ImGui::PopStyleColor(1);
ImGui::PopItemWidth();
ImGui::SameLine(0, ImGui::GetStyle().ItemInnerSpacing.x);
}
ImGui::Text("%s", name.c_str());
ImGui::EndGroup();
}
struct camera
{
vec4f pos;
quat rot;
camera()
{
pos = {0, 0, -4, 0};
rot.load_from_axis_angle({1, 0, 0, -M_PI/2});
}
void handle_input(vec2f mouse_delta, vec4f keyboard_input, float universe_size)
{
///translation is: .x is forward - back, .y = right - left, .z = down - up
///totally arbitrary
quat local_camera_quat = rot;
if(mouse_delta.x() != 0)
{
quat q;
q.load_from_axis_angle((vec4f){0, 0, -1, mouse_delta.x()});
local_camera_quat = q * local_camera_quat;
}
{
vec3f right = rot_quat((vec3f){1, 0, 0}, local_camera_quat);
if(mouse_delta.y() != 0)
{
quat q;
q.load_from_axis_angle({right.x(), right.y(), right.z(), mouse_delta.y()});
local_camera_quat = q * local_camera_quat;
}
}
vec4f local_camera_pos_cart = pos;
vec3f up = {0, 0, -1};
vec3f right = rot_quat((vec3f){1, 0, 0}, local_camera_quat);
vec3f forw = rot_quat((vec3f){0, 0, 1}, local_camera_quat);
vec3f offset = {0,0,0};
offset += forw * keyboard_input.x();
offset += right * keyboard_input.y();
offset += up * keyboard_input.z();
local_camera_pos_cart.x() += keyboard_input.w();
local_camera_pos_cart.y() += offset.x();
local_camera_pos_cart.z() += offset.y();
local_camera_pos_cart.w() += offset.z();
{
float rad = local_camera_pos_cart.yzw().length();
if(rad > universe_size * 0.99f)
{
vec3f next = local_camera_pos_cart.yzw().norm() * universe_size * 0.99f;
local_camera_pos_cart.y() = next.x();
local_camera_pos_cart.z() = next.y();
local_camera_pos_cart.w() = next.z();
}
}
pos = local_camera_pos_cart;
rot = local_camera_quat;
}
};
template<typename T>
struct async_executor
{
std::mutex mut;
std::vector<T> yields;
int peek()
{
std::lock_guard guard(mut);
return yields.size();
}
void add(T&& in)
{
std::lock_guard guard(mut);
yields.push_back(std::move(in));
}
std::optional<T> produce(bool at_least_one = false)
{
if(!at_least_one)
{
std::lock_guard guard(mut);
if(yields.size() == 0)
return std::nullopt;
auto val = std::move(yields.front());
yields.erase(yields.begin());
return val;
}
else
{
while(1)
{
std::lock_guard guard(mut);
if(yields.size() == 0)
continue;
auto val = std::move(yields.front());
yields.erase(yields.begin());
return val;
}
return std::nullopt;
}
}
};
struct gl_image_shared
{
texture tex;
cl::gl_rendertexture rtex;
gl_image_shared(cl::context& ctx) : rtex(ctx){}
void make(int width, int height)
{
texture_settings new_sett;
new_sett.width = width;
new_sett.height = height;
new_sett.is_srgb = false;
new_sett.generate_mipmaps = false;
tex.load_from_memory(new_sett, nullptr);
rtex.create_from_texture(tex.handle);
}
};
template<typename T>
struct spsc
{
std::mutex mut;
std::vector<T> dat;
void add(T&& in)
{
std::lock_guard guard(mut);
dat.push_back(std::move(in));
}
std::optional<T> produce()
{
std::lock_guard guard(mut);
if(dat.size() == 0)
return std::nullopt;
if(dat.size() > 0)
{
auto val = std::move(dat[0]);
dat.erase(dat.begin());
return std::move(val);
}
return std::nullopt;
}
};
struct gl_image_shared_queue
{
std::vector<gl_image_shared> free_images;
std::mutex mut;
void push_free(gl_image_shared&& gl)
{
std::scoped_lock guard(mut);
free_images.push_back(std::move(gl));
}
gl_image_shared pop_free_or_make_new(cl::context& ctx, int width, int height)
{
std::scoped_lock guard(mut);
if(free_images.size() > 4)
{
auto size = free_images.front().rtex.size<2>();
if(size.x() == width && size.y() == height)
{
auto next = std::move(free_images.front());
free_images.erase(free_images.begin());
return next;
}
else
{
free_images.clear();
}
}
gl_image_shared shared(ctx);
shared.make(width, height);
return shared;
}
};
void test_overlap()
{
static_assert(periodic_range_overlaps(0, 2, 0, 1, 2));
static_assert(periodic_range_overlaps(0, 1, 0, 8, 2));
static_assert(periodic_range_overlaps(0, 1, 2, 3, 2));
static_assert(!periodic_range_overlaps(0, 1, 3.1, 3.9, 2));
static_assert(periodic_range_overlaps(0.1, 0.3, 3.1, 4.2, 2));
static_assert(periodic_range_overlaps(3.1, 4.2, 0.1, 0.3, 2));
}
struct object_content
{
std::vector<std::filesystem::path> entries;
int selected = -1;
bool use_cube = true;
void load()
{
entries.clear();
std::string folder = "./models";
file::mkdir(folder);
try
{
for(const auto& entry : std::filesystem::directory_iterator{folder})
{
if(entry.path().string().ends_with(".obj"))
{
entries.push_back(std::filesystem::absolute(entry));
}
}
}
catch(...)
{
std::cout << "Invalid object content " << folder << std::endl;
}
}
void display()
{
ImGui::Checkbox("Use Cube", &use_cube);
if(ImGui::BeginListBox("Objects"))
{
for(int i=0; i < (int)entries.size(); i++)
{
std::string item_name = entries[i].filename().string();
if(ImGui::Selectable(item_name.c_str(), selected == i))
{
use_cube = false;
selected = i;
}
}
ImGui::EndListBox();
}
}
std::string get()
{
selected = clamp(selected, 0, (int)entries.size() - 1);
if(entries.size() == 0)
return "";
return entries.at(selected).string();
}
};
///i need the ability to have dynamic parameters
int main(int argc, char* argv[])
{
printf("Started\n");
test_overlap();
#ifdef REDIRECT_STDOUT
printf("Redirecting output\n");
*stdout = *fopen("debug.txt","w");
#endif // REDIRECT_STDOUT
std::optional<std::string> start_metric;
bool should_print_frametime = false;
for(int i=1; i < argc - 1; i++)
{
std::string current = argv[i];
std::string next = argv[i + 1];
if(current == "-bench")
{
start_metric = next;
should_debug = false;
should_print_frametime = true;
}
if(current == "-start")
{
start_metric = next;
}
}
printf("Parsed args\n");
bool has_new_content = false;
steam_info steam;
printf("Set up steam context\n");
ugc_view workshop;
workshop.only_get_subscribed();
printf("Workshop started\n");