-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.cpp
More file actions
1314 lines (1207 loc) · 50 KB
/
Copy pathapp.cpp
File metadata and controls
1314 lines (1207 loc) · 50 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 "object.hpp"
#include "ui.hpp"
#include "shared.hpp"
#include "io.hpp"
#include "threads.hpp"
#include "timer.hpp"
#include "PAZ_Engine"
#include "PAZ_Math"
#include <limits>
#include <deque>
static constexpr double InteractRangeBehindSq = 4.;
static constexpr double InteractRangeInFrontSq = 9.;
static constexpr std::size_t MaxConsoleLines = 1000;
static constexpr float FontScale = 1.5f;
static constexpr int CharWidth = 5;
static constexpr double Timestep = 1./60.;
static constexpr int ShadowRes = 2*2*1024;
static constexpr float ZNear = 0.1;
static constexpr float ZFar = 1e3;
static paz::Framebuffer _geometryBuffer;
static paz::Framebuffer _shadowBuffer;
static paz::Framebuffer _oitAccumBuffer;
static paz::Framebuffer _oitDepthBuffer;
static paz::Framebuffer _renderBuffer;
static paz::Framebuffer _dofBuffer;
static paz::Framebuffer _postBuffer;
static paz::Framebuffer _lumBuffer;
static paz::RenderTarget _diffuseMap(paz::TextureFormat::RGBA16Float);
// ...
static paz::RenderTarget _emissMap(paz::TextureFormat::RGBA16Float);
static paz::RenderTarget _normalMap(paz::TextureFormat::RGBA16Float);
static paz::RenderTarget _depthMap(paz::TextureFormat::Depth32Float);
static paz::RenderTarget _hdrRender(paz::TextureFormat::RGBA16Float);
static paz::RenderTarget _dofRender(paz::TextureFormat::RGBA16Float);
static paz::RenderTarget _finalRender(paz::TextureFormat::RGBA16Float, paz::MinMagFilter::Linear, paz::MinMagFilter::Linear);
static paz::RenderTarget _finalLumMap(paz::TextureFormat::R16Float, paz::MinMagFilter::Linear, paz::MinMagFilter::Linear);
static paz::RenderTarget _oitAccumTex(paz::TextureFormat::RGBA16Float);
static paz::RenderTarget _oitRevealTex(paz::TextureFormat::RGBA8UNorm);
static paz::RenderTarget _shadowMap(paz::TextureFormat::Depth32Float, ShadowRes, ShadowRes);
static std::unordered_map<void*, paz::InstanceBuffer> _instances;
static paz::RenderPass _geometryPass;
static paz::RenderPass _shadowPass;
static paz::RenderPass _renderPass0;
static paz::RenderPass _renderPass1;
static paz::RenderPass _dofPass;
static paz::RenderPass _oitAccumPass;
static paz::RenderPass _oitCompositePass;
static paz::RenderPass _oitDepthPass;
static paz::RenderPass _postPass0;
static paz::RenderPass _lumPass;
static paz::RenderPass _fxaaPass;
static paz::RenderPass _postPass1;
static paz::RenderPass _consolePass;
static paz::RenderPass _textPass;
static paz::RenderPass _cursorPass;
static paz::VertexBuffer _quadVertices;
static paz::VertexBuffer _sphereVertices;
static paz::IndexBuffer _sphereIndices;
static paz::Texture _fontTex;
static paz::Texture _consoleFontTex;
static std::stringstream _msgStream;
static std::deque<std::string> _console;
static paz::ConsoleMode _consoleMode = paz::ConsoleMode::Disable;
static std::queue<std::pair<std::string, double>> _dialogs;
static double _dialogTime;
static bool _dialogSkipFrame;
static std::string _title;
static paz::Texture _defaultDiffTex;
static paz::Texture _cursor;
static paz::Texture _reticule;
static int _reticuleIdx;
static bool _reticuleHighlight;
static paz::ObjectPtr _cameraObject;
static paz::ObjectPtr _micObject;
static paz::ObjectPtr _soundSrc;
static bool _paused;
static double _accumTime;
static int _lookSensitivity = 5;
static double _gravAcc;
static paz::Threadpool _threads;
static bool _fxaaEnabled = true;
static float _dofMinDepth;
static float _dofMaxDepth;
static paz::Vec _sunDir = paz::Vec::Zero(4);
static std::array<float, 4> _sunIll;
double _logicTime = 0.;
static const std::vector<paz::Button> OptionsButtons =
{
{
[](){ return paz::Window::IsFullscreen() ? "Fullscreen: ON" :
"Fullscreen: OFF"; },
[](paz::Menu&)
{
if(paz::Window::IsFullscreen())
{
paz::Window::MakeWindowed();
paz::save_setting("fullscreen", "0");
}
else
{
paz::Window::MakeFullscreen();
paz::save_setting("fullscreen", "1");
}
}
},
{
[]()
{
if(paz::Window::HidpiSupported())
{
return paz::Window::HidpiEnabled() ? "HiDPI: ON" :
"HiDPI: OFF";
}
else
{
return "HiDPI: N/A";
}
},
[](paz::Menu&)
{
if(paz::Window::HidpiSupported())
{
if(paz::Window::HidpiEnabled())
{
paz::Window::DisableHidpi();
paz::save_setting("hidpi", "0");
}
else
{
paz::Window::EnableHidpi();
paz::save_setting("hidpi", "1");
}
}
},
[]()
{
return paz::Window::HidpiSupported();
}
},
{
[](){ return _fxaaEnabled ? "FXAA: ON" : "FXAA: OFF"; },
[](paz::Menu&)
{
_fxaaEnabled = !_fxaaEnabled;
paz::save_setting("fxaa", _fxaaEnabled ? "1" : "0");
}
},
{
[](){ return "Sensitivity: " + std::to_string(_lookSensitivity); },
[](paz::Menu&)
{
++_lookSensitivity;
if(_lookSensitivity > 10)
{
_lookSensitivity = 1;
}
paz::save_setting("sensitivity", std::to_string(_lookSensitivity));
}
},
{
[]()
{
if(paz::Window::SyncToggleSupported())
{
return paz::Window::SyncEnabled() ? "V-Sync: ON" :
"V-Sync: OFF";
}
else
{
return "V-Sync: N/A";
}
},
[](paz::Menu&)
{
if(paz::Window::SyncToggleSupported())
{
if(paz::Window::SyncEnabled())
{
paz::Window::DisableSync();
paz::save_setting("sync", "0");
}
else
{
paz::Window::EnableSync();
paz::save_setting("sync", "1");
}
}
},
[]()
{
return paz::Window::SyncToggleSupported();
}
},
{"Back", [](paz::Menu& m){ m.setState(0, 1); }}
};
static paz::Mat convert_mat(const std::array<float, 16>& m)
{
paz::Mat res(4, 4);
std::copy(m.begin(), m.end(), res.begin());
return res;
}
static std::array<float, 16> convert_mat(const paz::Mat& m)
{
if(m.rows() != 4 || m.cols() != 4)
{
throw std::runtime_error("Must be a 4x4 matrix.");
}
std::array<float, 16> res;
std::copy(m.begin(), m.end(), res.begin());
return res;
}
static std::array<float, 4> convert_vec(const paz::Vec& v)
{
if(v.size() != 4)
{
throw std::runtime_error("Must be a four-vector.");
}
std::array<float, 4> res;
std::copy(v.begin(), v.end(), res.begin());
return res;
}
static constexpr std::array<float, 8> QuadPos = {1, -1, 1, 1, -1, -1, -1, 1};
void paz::App::Init(const std::string& title)
{
if(load_setting("fullscreen") == "1")
{
Window::MakeFullscreen();
}
if(load_setting("hidpi") == "0")
{
Window::DisableHidpi();
}
if(load_setting("fxaa") == "0")
{
_fxaaEnabled = false;
}
try
{
_lookSensitivity = std::stoi(load_setting("sensitivity"));
}
catch(...){}
_lookSensitivity = clamp(_lookSensitivity, 1, 10);
Window::EnableDithering();
_title = title;
Window::SetTitle(_title);
_geometryBuffer.attach(_diffuseMap);
// ...
_geometryBuffer.attach(_emissMap);
_geometryBuffer.attach(_normalMap);
_geometryBuffer.attach(_depthMap);
_shadowBuffer.attach(_shadowMap);
_renderBuffer.attach(_hdrRender);
_dofBuffer.attach(_dofRender);
_oitAccumBuffer.attach(_oitAccumTex);
_oitAccumBuffer.attach(_oitRevealTex);
_oitAccumBuffer.attach(_depthMap);
_oitDepthBuffer.attach(_depthMap);
_postBuffer.attach(_finalRender);
_lumBuffer.attach(_finalLumMap);
const VertexFunction geometryVert(get_builtin("geometry.vert").str());
const VertexFunction shadowVert(get_builtin("shadow.vert").str());
const VertexFunction quadVert(get_builtin("quad.vert").str());
const VertexFunction sceneVert0(get_builtin("scene0.vert").str());
const VertexFunction sceneVert1(get_builtin("scene1.vert").str());
const VertexFunction textVert(get_builtin("text.vert").str());
const VertexFunction cursorVert(get_builtin("cursor.vert").str());
const VertexFunction oitVert(get_builtin("oit.vert").str());
const VertexFunction oitDepthVert(get_builtin("oitdepth.vert").str());
const FragmentFunction geometryFrag(get_builtin("geometry.frag").str());
const FragmentFunction shadowFrag(get_builtin("shadow.frag").str());
const FragmentFunction sceneFrag0(get_asset("scene0.frag").str());
const FragmentFunction sceneFrag1(get_asset("scene1.frag").str());
const FragmentFunction lumFrag(get_builtin("lum.frag").str());
const FragmentFunction fxaaFrag(get_builtin("fxaa.frag").str());
const FragmentFunction postFrag(get_builtin("post.frag").str());
const FragmentFunction consoleFrag(get_builtin("console.frag").str());
const FragmentFunction textFrag(get_builtin("text.frag").str());
const FragmentFunction cursorFrag(get_builtin("cursor.frag").str());
const FragmentFunction oitFrag(get_asset("oit.frag").str());
const FragmentFunction compositeFrag(get_builtin("composite.frag").str());
const FragmentFunction dofFrag(get_builtin("dof.frag").str());
const FragmentFunction emptyFrag(get_builtin("empty.frag").str());
_geometryPass = RenderPass(_geometryBuffer, geometryVert, geometryFrag);
_shadowPass = RenderPass(_shadowBuffer, shadowVert, shadowFrag);
_renderPass0 = RenderPass(_renderBuffer, sceneVert0, sceneFrag0);
_renderPass1 = RenderPass(_renderBuffer, sceneVert1, sceneFrag1, {BlendMode::
One_One});
_dofPass = RenderPass(_dofBuffer, quadVert, dofFrag);
_oitAccumPass = RenderPass(_oitAccumBuffer, oitVert, oitFrag, {BlendMode::
One_One, BlendMode::Zero_InvSrcAlpha});
_oitCompositePass = RenderPass(_renderBuffer, quadVert, compositeFrag,
{BlendMode::InvSrcAlpha_SrcAlpha});
_oitDepthPass = RenderPass(_oitDepthBuffer, oitDepthVert, emptyFrag);
_postPass0 = RenderPass(_postBuffer, quadVert, postFrag);
_lumPass = RenderPass(_lumBuffer, quadVert, lumFrag);
_fxaaPass = RenderPass(quadVert, fxaaFrag);
_postPass1 = RenderPass(quadVert, postFrag);
_consolePass = RenderPass(quadVert, consoleFrag, {BlendMode::
SrcAlpha_InvSrcAlpha});
_textPass = RenderPass(textVert, textFrag, {BlendMode::
SrcAlpha_InvSrcAlpha});
_cursorPass = RenderPass(cursorVert, cursorFrag, {BlendMode::
SrcAlpha_InvSrcAlpha});
_quadVertices.addAttribute(2, QuadPos);
{
std::vector<float> positions;
std::vector<float> uvs;
std::vector<float> normals;
std::vector<unsigned int> materials;
std::vector<std::string> materialNames;
std::vector<std::string> materialLibs;
std::vector<unsigned int> indices;
parse_model(get_builtin("icosphere3.pazmodel"), positions, uvs, normals,
materials, materialNames, materialLibs, indices);
_sphereVertices.addAttribute(4, positions);
_sphereIndices = IndexBuffer(indices);
}
_consoleFontTex = Texture(get_builtin_image("consolefont.pbm")); //TEMP - note that only red channel is used
try
{
_fontTex = Texture(get_asset_image("font.pbm")); //TEMP - note that only red channel is used
}
catch(...)
{
_fontTex = _consoleFontTex;
}
std::vector<unsigned char> temp(4*512*512);
for(int i = 0; i < 512; ++i)
{
for(int j = 0; j < 512; ++j)
{
if((i*8/512)%2 == (j*8/512)%2)
{
temp[4*(512*i + j) + 0] = 0;
temp[4*(512*i + j) + 1] = 255;
temp[4*(512*i + j) + 2] = 255;
}
else
{
temp[4*(512*i + j) + 0] = 255;
temp[4*(512*i + j) + 1] = 0;
temp[4*(512*i + j) + 2] = 0;
}
temp[4*(512*i + j) + 3] = 255;
}
}
_defaultDiffTex = Texture(TextureFormat::RGBA8UNorm_sRGB, 512, 512, temp.
data(), MinMagFilter::Linear, MinMagFilter::Linear, MipmapFilter::
Anisotropic, WrapMode::Repeat, WrapMode::Repeat);
_cursor = Texture(get_asset_image("cursor.pbm")); //TEMP - note that only red channel is used
_reticule = Texture(get_asset_image("reticule.pbm")); //TEMP - note that only red channel is used
}
void paz::App::Run(bool autostart)
{
Font menuFont(_fontTex, 2.*FontScale, CharWidth);
{
bool done = autostart;
Menu startMenu(menuFont, _title,
{
{
{"Start", [&](Menu&){ done = true; }},
{"Options", [](Menu& m){ m.setState(1, 0); }},
{"Quit", [](Menu&){ Window::Quit(); }}
},
OptionsButtons
}
);
while(!Window::Done() && !done)
{
Window::PollEvents();
if(startMenu.curPage() == 1 && (Window::KeyPressed(Key::Escape) ||
Window::MousePressed(MouseButton::Back) || Window::
GamepadPressed(GamepadButton::Start)))
{
startMenu.setState(0, 1);
}
startMenu.update();
_textPass.begin({LoadAction::Clear});
_textPass.read("font", startMenu.font().tex());
_textPass.uniform("u", 0.f);
_textPass.uniform("v", 0.f);
_textPass.uniform("width", Window::ViewportWidth());
_textPass.uniform("height", Window::ViewportHeight());
_textPass.uniform("charWidth", startMenu.font().charWidth());
_textPass.uniform("baseWidth", startMenu.font().tex().width());
_textPass.uniform("baseHeight", startMenu.font().tex().height());
_textPass.uniform("scale", startMenu.font().curScale());
_textPass.draw(PrimitiveType::TriangleStrip, _quadVertices, startMenu.
chars());
_textPass.end();
if(Window::MouseActive())
{
_cursorPass.begin({LoadAction::Load});
_cursorPass.uniform("x", static_cast<float>(Window::MousePos().
first)/(Window::Width() - 1));
_cursorPass.uniform("y", static_cast<float>(Window::MousePos().
second)/(Window::Height() - 1));
_cursorPass.uniform("idx", 0);
_cursorPass.uniform("aspect", static_cast<float>(_cursor.
height())/_cursor.width());
_cursorPass.uniform("h", static_cast<float>(startMenu.
curButtonEnabled()));
_cursorPass.uniform("scale", static_cast<int>(std::max(1.f, std::
round(20.f*Window::UiScale())))); //TEMP
_cursorPass.uniform("width", Window::ViewportWidth());
_cursorPass.uniform("height", Window::ViewportHeight());
_cursorPass.read("tex", _cursor);
_cursorPass.draw(PrimitiveType::TriangleStrip, _quadVertices);
_cursorPass.end();
}
Window::EndFrame();
}
if(Window::Done())
{
return;
}
}
Window::SetCursorMode(CursorMode::Disable);
Menu pauseMenu(menuFont, "Paused",
{
{
{"Resume", [&](Menu&)
{
_paused = false;
Window::SetCursorMode(CursorMode::Disable);
}
},
{"Options", [](Menu& m){ m.setState(1, 0); }},
{"Quit", [](Menu&){ Window::Quit(); }}
},
OptionsButtons
}
);
InputData input;
while(!Window::Done())
{
bool justPaused = false;
if(_paused)
{
_accumTime = 0.;
Window::PollEvents();
input.resetEvents();
}
else
{
_accumTime += Window::FrameTime();
Window::PollEvents();
if(Window::KeyPressed(Key::Escape) || Window::GamepadPressed(
GamepadButton::Start))
{
justPaused = true;
_paused = true;
pauseMenu.setState(0, 0);
}
input.copyEvents(0.2*_lookSensitivity);
while(_accumTime > 0.)
{
if(_consoleMode == ConsoleMode::LatestStep)
{
std::stringstream{}.swap(_msgStream);
}
Timer t;
do_physics(_gravAcc, ::Timestep);
do_collisions(_threads, ::Timestep);
const auto tempObjects = objects(); //TEMP - this prevents missed or multiple updates when `objects()` changes, but is not ideal
for(const auto& n : tempObjects)
{
if(objects().count(n.first))
{
reinterpret_cast<Object*>(n.first)->update(input);
}
}
_logicTime = t.get();
input.resetEvents();
_accumTime -= ::Timestep;
}
}
const double fac = 1. + _accumTime/::Timestep;
if(!_paused && _micObject && _soundSrc)
{
const Vec relPos{{mix(_soundSrc->xPrev() - _micObject->xPrev(),
_soundSrc->x() - _micObject->x(), fac), mix(_soundSrc->yPrev() -
_micObject->yPrev(), _soundSrc->y() - _micObject->y(), fac), mix(
_soundSrc->zPrev() - _micObject->zPrev(), _soundSrc->z() -
_micObject->z(), fac)}};
const Vec relVel{{_soundSrc->xVel() - _micObject->xVel(), _soundSrc->
yVel() - _micObject->yVel(), _soundSrc->zVel() - _micObject->
zVel()}};
const double dist = relPos.norm();
const Vec dir = relPos/dist;
const double wAttPrev = std::sqrt(1. - _micObject->xAttPrev()*
_micObject->xAttPrev() - _micObject->yAttPrev()*_micObject->
yAttPrev() - _micObject->zAttPrev()*_micObject->zAttPrev());
const double wAtt = std::sqrt(1. - _micObject->xAtt()*_micObject->
xAtt() - _micObject->yAtt()*_micObject->yAtt() - _micObject->
zAtt()*_micObject->zAtt());
const Vec micAtt = nlerp(Vec{{_micObject->xAttPrev(), _micObject->
yAttPrev(), _micObject->zAttPrev(), wAttPrev}}, Vec{{
_micObject->xAtt(), _micObject->yAtt(), _micObject->zAtt(),
wAtt}}, fac);
const Mat micRot = to_mat(micAtt);
const Vec micX = micRot.row(0).trans();
const Vec micY = micRot.row(1).trans();
const Vec lEar = std::sin(1.)*micX + std::cos(1.)*micY;
const Vec rEar = std::sin(1.)*micX - std::cos(1.)*micY;
const double vlos = dir.dot(relVel);
const double txPwr = 400.;
static constexpr double vs = 343.;
static constexpr double maxRxPwr = 0.3;
const double t0 = (vs + vlos)/(800.*Pi*dist); // assuming f = 200
const double rxPwr = std::min(maxRxPwr, txPwr*t0*t0);
double lPwr = rxPwr*(0.6 + 0.4*(dir.dot(lEar))); //TEMP
double rPwr = rxPwr*(0.6 + 0.4*(dir.dot(rEar))); //TEMP
lPwr /= SqrtTwo*(0.6 + 0.4*std::cos(0.5*Pi - 1.));
rPwr /= SqrtTwo*(0.6 + 0.4*std::cos(0.5*Pi - 1.));
const double lVol = std::sqrt(std::sqrt(std::max(0., lPwr)));
const double rVol = std::sqrt(std::sqrt(std::max(0., rPwr)));
const double lFreqScale = 1./(1. + vlos/vs);
const double rFreqScale = 1./(1. + vlos/vs);
AudioEngine::SetVolume(lVol, 0);
AudioEngine::SetVolume(rVol, 1);
AudioEngine::SetFreqScale(lFreqScale, 0);
AudioEngine::SetFreqScale(rFreqScale, 1);
}
else
{
AudioEngine::SetVolume(0.);
}
const double cameraWAttPrev = std::sqrt(1. - _cameraObject->xAttPrev()*
_cameraObject->xAttPrev() - _cameraObject->yAttPrev()*
_cameraObject->yAttPrev() - _cameraObject->zAttPrev()*
_cameraObject->zAttPrev());
const double cameraWAtt = std::sqrt(1. - _cameraObject->xAtt()*
_cameraObject->xAtt() - _cameraObject->yAtt()*_cameraObject->yAtt()
- _cameraObject->zAtt()*_cameraObject->zAtt());
const Vec cameraAtt = nlerp(Vec{{_cameraObject->xAttPrev(),
_cameraObject->yAttPrev(), _cameraObject->zAttPrev(),
cameraWAttPrev}}, Vec{{_cameraObject->xAtt(), _cameraObject->yAtt(),
_cameraObject->zAtt(), cameraWAtt}}, fac);
const Vec cameraPos{{mix(_cameraObject->xPrev(), _cameraObject->x(),
fac), mix(_cameraObject->yPrev(), _cameraObject->y(), fac), mix(
_cameraObject->zPrev(), _cameraObject->z(), fac)}};
const auto projection = perspective(1., Window::AspectRatio(), ZNear,
ZFar);
Mat view = Mat::Zero(4);
view(3, 3) = 1.;
view.setBlock(0, 0, 3, 3, Mat{{0., -1., 0.}, {0., 0., 1.}, {-1., 0.,
0.}}*to_mat(cameraAtt));
// Prepare for rendering.
std::unordered_map<void*, std::vector<const Object*>> objectsByModel;
std::vector<const Object*> invisibleObjects;
for(const auto& n : objects())
{
const Object* o = reinterpret_cast<const Object*>(n.first);
if(o->model()._i.empty() && o->model()._transp.empty())
{
invisibleObjects.push_back(o);
}
else
{
// Address held by `paz::Model::_t` identifies all copies of the
// same model.
objectsByModel[o->model()._t.get()].push_back(o);
}
}
for(const auto& n : objectsByModel)
{
if(!_instances.count(n.first) || _instances.at(n.first).size() != n.
second.size()) //TEMP - should only need to change _numInstances if larger, not reinit whole buffer
{
_instances[n.first] = InstanceBuffer(n.second.size());
_instances.at(n.first).addAttribute(4, DataType::Float);
_instances.at(n.first).addAttribute(2, DataType::Float);
}
}
std::vector<float> lightsData0;
std::vector<float> lightsData1;
for(const auto& n : objectsByModel)
{
std::array<std::vector<float>, 2> modelMatData;
modelMatData[0].resize(4*n.second.size());
modelMatData[1].resize(2*n.second.size());
for(std::size_t i = 0; i < n.second.size(); ++i)
{
const double wAttPrev = std::sqrt(1. - n.second[i]->xAttPrev()*
n.second[i]->xAttPrev() - n.second[i]->yAttPrev()*n.second[
i]->yAttPrev() - n.second[i]->zAttPrev()*n.second[i]->
zAttPrev());
const double wAtt = std::sqrt(1. - n.second[i]->xAtt()*n.second[
i]->xAtt() - n.second[i]->yAtt()*n.second[i]->yAtt() - n.
second[i]->zAtt()*n.second[i]->zAtt());
const Vec att = nlerp(Vec{{n.second[i]->xAttPrev(), n.second[
i]->yAttPrev(), n.second[i]->zAttPrev(), wAttPrev}}, Vec{{
n.second[i]->xAtt(), n.second[i]->yAtt(), n.second[i]->
zAtt(), wAtt}}, fac);
modelMatData[0][4*i + 0] = att(0);
modelMatData[0][4*i + 1] = att(1);
modelMatData[0][4*i + 2] = att(2);
modelMatData[0][4*i + 3] = mix(n.second[i]->xPrev(), n.second[
i]->x(), fac) - cameraPos(0);
modelMatData[1][2*i + 0] = mix(n.second[i]->yPrev(), n.second[
i]->y(), fac) - cameraPos(1);
modelMatData[1][2*i + 1] = mix(n.second[i]->zPrev(), n.second[
i]->z(), fac) - cameraPos(2);
if(!n.second[i]->lights().empty())
{
const Vec model0{{modelMatData[0][4*i + 0], modelMatData[0][4*i + 1], modelMatData[0][4*i + 2], modelMatData[0][4*i + 3]}};
const Vec model1{{modelMatData[1][2*i + 0], modelMatData[1][2*i + 1]}};
const Vec att{{model0(0), model0(1), model0(2), std::sqrt(1. - model0.head(3).normSq())}};
const double xx = att(0)*att(0);
const double yy = att(1)*att(1);
const double zz = att(2)*att(2);
const double xy = att(0)*att(1);
const double zw = att(2)*att(3);
const double xz = att(0)*att(2);
const double yw = att(1)*att(3);
const double yz = att(1)*att(2);
const double xw = att(0)*att(3);
const Mat mv = view*Mat{{1. - 2.*(yy + zz), 2.*(xy - zw), 2.*(xz + yw), model0(3)},
{2.*(xy + zw), 1. - 2.*(xx + zz), 2.*(yz - xw), model1(0)},
{2.*(xz - yw), 2.*(yz + xw), 1. - 2.*(xx + yy), model1(1)},
{0., 0., 0., 1.}};
for(const auto& m : n.second[i]->lights())
{
const Vec p = mv*Vec{{m[0], m[1], m[2], 1.}};
lightsData0.push_back(p(0));
lightsData0.push_back(p(1));
lightsData0.push_back(p(2));
lightsData0.push_back(m[3]);
lightsData1.push_back(m[4]);
lightsData1.push_back(m[5]);
lightsData1.push_back(m[6]);
lightsData1.push_back(0.f);
}
}
}
_instances.at(n.first).subAttribute(0, modelMatData[0]);
_instances.at(n.first).subAttribute(1, modelMatData[1]);
}
for(const auto& n : invisibleObjects)
{
if(!n->lights().empty())
{
const double wAttPrev = std::sqrt(1. - n->xAttPrev()*n->
xAttPrev() - n->yAttPrev()*n->yAttPrev() - n->zAttPrev()*n->
zAttPrev());
const double wAtt = std::sqrt(1. - n->xAtt()*n->xAtt() - n->
yAtt()*n->yAtt() - n->zAtt()*n->zAtt());
const Vec att = nlerp(Vec{{n->xAttPrev(), n->yAttPrev(), n->
zAttPrev(), wAttPrev}}, Vec{{n->xAtt(), n->yAtt(), n->
zAtt(), wAtt}}, fac);
const double xx = att(0)*att(0);
const double yy = att(1)*att(1);
const double zz = att(2)*att(2);
const double xy = att(0)*att(1);
const double zw = att(2)*att(3);
const double xz = att(0)*att(2);
const double yw = att(1)*att(3);
const double yz = att(1)*att(2);
const double xw = att(0)*att(3);
const Mat mv = view*Mat{{1. - 2.*(yy + zz), 2.*(xy - zw), 2.*(xz + yw), mix(n->xPrev(), n->x(), fac) - cameraPos(0)},
{2.*(xy + zw), 1. - 2.*(xx + zz), 2.*(yz - xw), mix(n->yPrev(), n->y(), fac) - cameraPos(1)},
{2.*(xz - yw), 2.*(yz + xw), 1. - 2.*(xx + yy), mix(n->zPrev(), n->z(), fac) - cameraPos(2)},
{0., 0., 0., 1.}};
for(const auto& m : n->lights())
{
const Vec p = mv*Vec{{m[0], m[1], m[2], 1.}};
lightsData0.push_back(p(0));
lightsData0.push_back(p(1));
lightsData0.push_back(p(2));
lightsData0.push_back(m[3]);
lightsData1.push_back(m[4]);
lightsData1.push_back(m[5]);
lightsData1.push_back(m[6]);
lightsData1.push_back(0.f);
}
}
}
InstanceBuffer lights; //TEMP - should just sub data if same number
lights.addAttribute(4, lightsData0);
lights.addAttribute(4, lightsData1);
// Get geometry map.
_geometryPass.begin(std::vector<LoadAction>(4, LoadAction::Clear),
LoadAction::Clear);
_geometryPass.cull(CullMode::Back);
_geometryPass.depth(DepthTestMode::Less);
_geometryPass.uniform("projection", projection);
_geometryPass.uniform("view", convert_mat(view));
for(const auto& n : objectsByModel)
{
if(n.second.back()->model()._i.empty())
{
continue;
}
if(n.second.back()->model()._diffTex.width())
{
_geometryPass.read("diffTex", n.second.back()->model().
_diffTex);
}
else
{
_geometryPass.read("diffTex", _defaultDiffTex);
}
std::array<float, 4> emiss;
std::copy(n.second.back()->model()._emiss.begin(), n.second.back()->
model()._emiss.end(), emiss.begin());
emiss[3] = 1.f;
_geometryPass.uniform("emiss", emiss);
_geometryPass.draw(PrimitiveType::Triangles, n.second.back()->
model()._v, _instances.at(n.first), n.second.back()->model().
_i);
}
_geometryPass.end();
static const auto lightProjection = paz::ortho(-100., 100., -100., 100., 0., ZFar); //TEMP
paz::Vec relLightPos(4);
const paz::Vec sunPos = 0.5*ZFar*_sunDir.head(3); //TEMP
relLightPos.setHead(3, sunPos - cameraPos);
relLightPos(3) = 1.;
paz::Mat lightView = paz::Mat::Zero(4);
{
const paz::Vec z = _sunDir.head(3).normalized();
paz::Vec y = paz::Vec{{0., 1., 0.}};
const paz::Vec x = y.cross(z).normalized();
y = z.cross(x).normalized();
lightView.setBlock(0, 0, 1, 3, x.trans());
lightView.setBlock(1, 0, 1, 3, y.trans());
lightView.setBlock(2, 0, 1, 3, z.trans());
}
lightView(3, 3) = 1.;
if((_sunDir(0) || _sunDir(1) || _sunDir(2)) && (_sunIll[0] || _sunIll[1]
|| _sunIll[2]))
{
_shadowPass.begin({}, paz::LoadAction::Clear);
_shadowPass.cull(paz::CullMode::Front);
_shadowPass.depth(paz::DepthTestMode::Less);
_shadowPass.uniform("projection", lightProjection);
_shadowPass.uniform("view", convert_mat(lightView));
_shadowPass.uniform("relLightPos", convert_vec(relLightPos)); //TEMP - should always subtract position on CPU side (`double`s)
for(const auto& n : objectsByModel)
{
if(n.second.back()->model()._i.empty())
{
continue;
}
_shadowPass.draw(PrimitiveType::Triangles, n.second.back()->
model()._v, _instances.at(n.first), n.second.back()->
model()._i);
}
_shadowPass.end();
}
// Render in HDR.
_renderPass0.begin({LoadAction::Clear});//, LoadAction::Load);
_renderPass0.read("diffuseMap", _diffuseMap);
// ...
_renderPass0.read("emissMap", _emissMap);
_renderPass0.read("normalMap", _normalMap);
_renderPass0.read("depthMap", _depthMap);
_renderPass0.uniform("invProjection", convert_mat(convert_mat(
projection).inv()));
_renderPass0.uniform("lightDir", convert_vec(view*_sunDir));
_renderPass0.uniform("ill", _sunIll);
_renderPass0.read("shadowMap", _shadowMap);
_renderPass0.uniform("invView", convert_mat(view.inv()));
_renderPass0.uniform("lightProjection", lightProjection);
_renderPass0.uniform("lightView", convert_mat(lightView));
_renderPass0.uniform("zNear", ZNear);
_renderPass0.uniform("zFar", ZFar);
_renderPass0.uniform("relLightPos", convert_vec(relLightPos));
_renderPass0.draw(PrimitiveType::TriangleStrip, _quadVertices);
_renderPass0.end();
if(!lights.empty())
{
_renderPass1.begin({LoadAction::Load});//, LoadAction::Load);
_renderPass1.cull(CullMode::Front);
// _renderPass1.depth(DepthTestMode::GreaterNoMask);
_renderPass1.read("diffuseMap", _diffuseMap);
// ...
_renderPass1.read("emissMap", _emissMap);
_renderPass1.read("normalMap", _normalMap);
_renderPass1.read("depthMap", _depthMap);
_renderPass1.uniform("projection", projection);
_renderPass1.uniform("invProjection", convert_mat(convert_mat(
projection).inv()));
_renderPass1.uniform("zNear", ZNear);
_renderPass1.uniform("zFar", ZFar);
_renderPass1.draw(PrimitiveType::Triangles, _sphereVertices, lights,
_sphereIndices);
_renderPass1.end();
}
// Render transparent objects.
bool oitEnabled = false;
for(const auto& n : objectsByModel)
{
if(!n.second.back()->model()._transp.empty())
{
oitEnabled = true;
break;
}
}
if(oitEnabled)
{
unsigned int numLights = lightsData0.size()/4;
if(numLights > 100)
{
throw std::runtime_error("Too many lights (" + std::to_string(numLights) + " > 100).");
}
_oitAccumPass.begin({LoadAction::Clear, LoadAction::Clear}, LoadAction::Load);
_oitAccumPass.depth(DepthTestMode::LessNoMask);
_oitAccumPass.uniform("numLights", numLights);
_oitAccumPass.uniform("light0", lightsData0);
_oitAccumPass.uniform("light1", lightsData1);
_oitAccumPass.uniform("projection", projection);
_oitAccumPass.uniform("invProjection", convert_mat(convert_mat(projection).inv())); //TEMP - precompute - used elsewhere
_oitAccumPass.uniform("sunDir", convert_vec(view*_sunDir)); //TEMP - precompute
_oitAccumPass.uniform("sunIll", _sunIll);
_oitAccumPass.uniform("view", convert_mat(view)); //TEMP - precompute
for(const auto& n : objectsByModel)
{
if(!n.second.back()->model()._transp.empty())
{
_oitAccumPass.draw(PrimitiveType::Triangles, n.second.back()->model()._transp, _instances.at(n.first));
}
}
_oitAccumPass.end();
_oitCompositePass.begin({LoadAction::Load});
_oitCompositePass.read("accumTex", _oitAccumTex);
_oitCompositePass.read("revealTex", _oitRevealTex);
_oitCompositePass.draw(PrimitiveType::TriangleStrip, _quadVertices);
_oitCompositePass.end();
}
const bool dofEnabled = _dofMinDepth < _dofMaxDepth;
if(dofEnabled)
{
if(oitEnabled) //TEMP - can we get nearest transparent depth while performing OIT ?
{
_oitDepthPass.begin();
_oitDepthPass.depth(DepthTestMode::Less);
_oitDepthPass.uniform("project", projection);
_oitDepthPass.uniform("view", convert_mat(view));
for(const auto& n : objectsByModel)
{
if(!n.second.back()->model()._transp.empty())
{
_oitDepthPass.draw(PrimitiveType::Triangles, n.second.back()->model()._transp, _instances.at(n.first));
}
}
_oitDepthPass.end();
}
_dofPass.begin();
_dofPass.read("hdrRender", _hdrRender);
_dofPass.read("depthMap", _depthMap);
_dofPass.uniform("minDepth", _dofMinDepth);
_dofPass.uniform("maxDepth", _dofMaxDepth);
_dofPass.uniform("aspectRatio", Window::AspectRatio());
_dofPass.draw(PrimitiveType::TriangleStrip, _quadVertices);
_dofPass.end();
}
if(_fxaaEnabled)
{
// Tonemap to linear LDR.
_postPass0.begin();
_postPass0.read("hdrRender", dofEnabled ? _dofRender : _hdrRender);
_postPass0.uniform("whitePoint", 1.f);
_postPass0.draw(PrimitiveType::TriangleStrip, _quadVertices);
_postPass0.end();
// Get luminance map.
_lumPass.begin();
_lumPass.read("img", _finalRender);
_lumPass.draw(PrimitiveType::TriangleStrip, _quadVertices);
_lumPass.end();
// Anti-alias.
_fxaaPass.begin();
_fxaaPass.read("img", _finalRender);
_fxaaPass.read("lum", _finalLumMap);
_fxaaPass.draw(PrimitiveType::TriangleStrip, _quadVertices);
_fxaaPass.end();
}
else
{
// Tonemap to linear LDR.
_postPass1.begin();
_postPass1.read("hdrRender", dofEnabled ? _dofRender : _hdrRender);
_postPass1.uniform("whitePoint", 1.f);
_postPass1.draw(PrimitiveType::TriangleStrip, _quadVertices);
_postPass1.end();
}
InstanceBuffer _dialogChars; //TEMP - needs wider scope because of MTLBuffer purge issue
if(!_dialogs.empty())
{
if(!_dialogSkipFrame)
{
const float scale = std::max(1.f, std::round(2.f*FontScale*
Window::UiScale()));
int maxCols = 0;
std::vector<float> highlightAttr;
std::vector<int> characterAttr;
std::vector<int> colAttr;
std::vector<int> rowAttr;
bool highlight = false;
int row = 0;
int col = 0;
for(auto n : _dialogs.front().first)
{
if(n == '`')
{
highlight = !highlight;
}
else if(n == ' ')
{
++col;
}
else if(n == '\t')
{
col = (col/4 + 1)*4;
}
else if(n >= '!' && n <= '~')
{
highlightAttr.push_back(highlight);
characterAttr.push_back(n - '!');
colAttr.push_back(col);
rowAttr.push_back(row);
++col;
}