-
Notifications
You must be signed in to change notification settings - Fork 14
/
game.cpp
4930 lines (4266 loc) · 179 KB
/
game.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
#include "game.h"
#ifdef WIN32
QEvent::Type Game::keypress = QEvent::KeyPress;
#else
QEvent::Type Game::keypress = (QEvent::Type)6;
#endif
Game::Game() :
do_exit(false),
fadestate(FADE_NONE),
gamepad_button_press(false),
draw_cursor(false),
cursor_active(-1),
rmb_held(false),
delta_time(0.0f)
{
deltat_time.start();
player = new Player();
for (int i=0; i<2; ++i) {
spatial_active[i] = false;
undo_object[i] = QPointer <RoomObject> (new RoomObject());
controller_x[i] = 0.0f;
controller_y[i] = 0.0f;
last_controller_x[i] = 0.0f;
last_controller_y[i] = 0.0f;
}
bookmarks = new BookmarkManager();
bookmarks->LoadBookmarks();
bookmarks->LoadWorkspaces();
controller_manager = new ControllerManager();
env = new Environment();
multi_players = new MultiPlayerManager();
virtualmenu = new VirtualMenu();
virtualmenu->SetBookmarkManager(bookmarks);
virtualmenu->SetMultiPlayerManager(multi_players);
global_uuid = 0;
state = JVR_STATE_DEFAULT;
show_position_mode = false;
teleport_hold_time_required = 125;
unit_scale = 1;
unit_translate_amount = QVector<float>({1.0f, 0.1f, 0.01f, 0.001f}); //std::initializer_lists
unit_rotate_amount = QVector<float>({90.0f, 15.0f, 5.0f, 1.0f});
unit_scale_amount = QVector<float>({1.0f, 0.1f, 0.01f, 0.001f});
WebAsset::LoadAuthData();
Initialize();
}
Game::~Game()
{
multi_players->SetEnabled(false);
// Analytics::PostEvent("session", "end", NULL, "end");
SoundManager::StopAll();
delete env;
//access filtered cubemap thing
FilteredCubemapManager::GetSingleton()->SetShutdown(true);
MathUtil::FlushErrorLog();
// This needs cleaned up while Renderer is still valid
AssetImage::null_cubemap_tex_handle = nullptr;
AssetImage::null_image_tex_handle = nullptr;
GeomIOSystem::SetShuttingDown(true);
}
void Game::Initialize()
{
env->Reset();
multi_players->Initialize();
multi_players->SetEnabled(SettingsManager::GetMultiplayerEnabled());
const float s = 0.05f;
info_text_geom.SetFixedSize(true, s);
info2_text_geom.SetFixedSize(true, s);
WebAsset::SetUseCache(SettingsManager::GetCacheEnabled());
bool cache_setting = WebAsset::GetUseCache();
WebAsset::SetUseCache(cache_setting);
FilteredCubemapManager* cubemap_manager = FilteredCubemapManager::GetSingleton();
cubemap_manager->Initialize();
}
void Game::AddPrivateWebsurface()
{
const unsigned int index = private_websurfaces.size();
PrivateWebsurface p;
const QString u = SettingsManager::GetWebsurfaceURL();
p.asset = new AssetWebSurface();
p.asset->GetProperties()->SetID("__web_id");
p.asset->GetProperties()->SetWidth(1366);
p.asset->GetProperties()->SetHeight(768);
p.asset->GetProperties()->SetSaveToMarkup(false);
p.asset->SetSrc(u, u);
p.plane_obj = new AssetObject();
p.plane_obj->SetSrc(MathUtil::GetApplicationURL(), "assets/primitives/plane.obj");
p.plane_obj->Load();
p.obj = new RoomObject();
p.obj->SetType(TYPE_OBJECT);
p.obj->SetInterfaceObject(true);
p.obj->GetProperties()->SetID("plane");
p.obj->GetProperties()->SetJSID("__plane" + QString::number(index));
p.obj->GetProperties()->SetLighting(false);
p.obj->GetProperties()->SetWebsurfaceID("__web_id" + QString::number(index));
p.obj->GetProperties()->SetCullFace("none");
p.obj->GetProperties()->SetVisible("false");
p.obj->SetAssetObject(p.plane_obj);
p.obj->SetAssetWebSurface(p.asset);
private_websurfaces.push_back(p);
}
void Game::RemovePrivateWebsurface()
{
if (!private_websurfaces.isEmpty()) {
int ind = private_websurfaces.size()-1;
for (int i=0; i<private_websurfaces.size(); ++i) {
if (websurface_selected[0] == private_websurfaces[i].asset) {
ind = i;
break;
}
}
PrivateWebsurface & p = private_websurfaces[ind];
if (p.asset) {
delete p.asset;
}
if (p.plane_obj) {
delete p.plane_obj;
}
if (p.obj) {
delete p.obj;
}
private_websurfaces.removeAt(ind);
}
}
void Game::SetPrivateWebsurfacesVisible(const bool b)
{
for (int i=0; i<private_websurfaces.size(); ++i) {
QPointer <RoomObject> o = private_websurfaces[i].obj;
if (o) {
if (b) {
QMatrix4x4 xform = player->GetTransform();
const float angle = -(float(i%5) - float(qMin(private_websurfaces.size()-1, 4))/2.0f) * 45.0f;
xform.rotate(angle, 0, 1, 0);
xform.translate(0,player->GetProperties()->GetEyePoint().y()
- player->GetProperties()->GetPos()->toQVector3D().y() + (i/5) * 0.5f - 0.2f,-1.0f);
o->GetProperties()->SetPos(xform.map(QVector3D(0,0,0)));
o->GetProperties()->SetXDir(xform.mapVector(QVector3D(1,0,0)));
o->GetProperties()->SetYDir(xform.mapVector(QVector3D(0,1,0)));
o->GetProperties()->SetZDir(xform.mapVector(QVector3D(0,0,1)));
o->GetProperties()->SetScale(QVector3D(0.8f, 0.45f, 1.0f));
}
o->GetProperties()->SetVisible(b);
}
}
}
bool Game::GetPrivateWebsurfacesVisible() const
{
for (int i=0; i<private_websurfaces.size(); ++i) {
if (private_websurfaces[i].obj && private_websurfaces[i].obj->GetProperties()->GetVisible()) {
return true;
}
}
return false;
}
void Game::UpdatePrivateWebsurfaces()
{
for (int i=0; i<private_websurfaces.size(); ++i) {
QPointer <AssetWebSurface> a = private_websurfaces[i].asset;
if (a) {
if (!a->GetStarted()) {
a->SetStarted(true);
a->Load();
}
if (a->GetWebView()) {
a->GetWebView()->update();
}
a->UpdateGL();
}
}
}
void Game::DrawPrivateWebsurfacesGL(QPointer <AssetShader> shader)
{
for (int i=0; i<private_websurfaces.size(); ++i) {
if (private_websurfaces[i].asset) {
private_websurfaces[i].asset->UpdateGL();
}
if (private_websurfaces[i].plane_obj) {
private_websurfaces[i].plane_obj->Update();
private_websurfaces[i].plane_obj->UpdateGL();
}
if (private_websurfaces[i].obj) {
//qDebug() << "draw" << i;
private_websurfaces[i].obj->Update(0.0f);
private_websurfaces[i].obj->DrawGL(shader, true, QVector3D(0,0,0));
}
}
}
void Game::Update()
{
if (env.isNull() || env->GetCurRoom().isNull()) {
return;
}
//delta_t processing
delta_time = 0.0;
if (deltat_time.elapsed() > 0) {
delta_time = double(deltat_time.restart()) / 1000.0;
}
player->SetDeltaTime(delta_time);
//Update imported files
UpdateImportList();
//Controllers (gamepad, Touch, Vive)
UpdateControllers();
//Update private websurfaces
UpdatePrivateWebsurfaces();
//update virtual menu
UpdateVirtualMenu();
//Update AssetRecordings
UpdateAssetRecordings();
//Update "player"
RoomObject::SetDrawAssetObjectTeleport(state == JVR_STATE_INTERACT_TELEPORT);
QPointer <Room> r = env->GetCurRoom();
if (r) {
const QPair <QVector3D, QVector3D> reset_volume = r->GetResetVolume();
const QVector3D p = player->GetProperties()->GetPos()->toQVector3D();
if (fadestate == FADE_NONE &&
p.x() > qMin(reset_volume.first.x(), reset_volume.second.x()) &&
p.y() > qMin(reset_volume.first.y(), reset_volume.second.y()) &&
p.z() > qMin(reset_volume.first.z(), reset_volume.second.z()) &&
p.x() < qMax(reset_volume.first.x(), reset_volume.second.x()) &&
p.y() < qMax(reset_volume.first.y(), reset_volume.second.y()) &&
p.z() < qMax(reset_volume.first.z(), reset_volume.second.z())) {
StartResetPlayer();
}
//update player's movement in the current room
const float walk_speed = r->GetProperties()->GetWalkSpeed();
const float run_speed = r->GetProperties()->GetRunSpeed();
player->Update((player->GetFlying() ||
player->GetRunning()) ? run_speed : walk_speed);
//auto-load any portals?
QHash <QString, QPointer <RoomObject> > & envobjects = r->GetRoomObjects();
for (auto & p : envobjects) {
if (p && p->GetType() == TYPE_LINK) {
if (!p->GetProperties()->GetOpen() && p->GetProperties()->GetAutoLoad() && !p->GetProperties()->GetAutoLoadTriggered()) {
p->GetProperties()->SetOpen(true);
p->GetProperties()->SetAutoLoadTriggered(true);
env->AddRoom(p);
}
}
}
}
//update cursors
UpdateCursorAndTeleportTransforms();
//update overlays
UpdateOverlays();
//update follow mode
//UpdateFollowMode();
WebAsset::SetUseCache(SettingsManager::GetCacheEnabled());
//update VOIP
UpdateAudio();
//update assets
UpdateAssets();
//update multiplayers
UpdateMultiplayer();
//do environment::update1 and process portal crossings
QPointer <Room> r0 = env->GetCurRoom();
env->Update1(player, multi_players);
QPointer <Room> r1 = env->GetCurRoom();
//clear selection if player moved and hide menu
if (r0 != r1) {
ClearSelection(0);
ClearSelection(1);
SetPrivateWebsurfacesVisible(false);
}
if (r1) {
player->GetProperties()->SetURL(r1->GetProperties()->GetURL());
}
//63.1 - log errors as they happen
MathUtil::FlushErrorLog();
}
float Game::UpdateCursorRaycast(const QMatrix4x4 transform, const int cursor_index)
{
//qDebug() << "Game::UpdateCursorRaycast" << transform;
//Ray emit position is defined by column index 3 (position)
//Ray emit direction is defined by negation of column index 2 (z basis vector)
const QVector3D ray_p = transform.column(3).toVector3D();
const QVector3D ray_d = transform.column(2).toVector3D();
struct IntersectionElement {
QPointer <RoomObject> envobject;
QPointer <RoomObject> webobject;
QPointer <RoomObject> menuobject;
QVector3D v;
QVector3D n;
QVector2D uv;
};
QPointer <Room> r = env->GetCurRoom();
QList <IntersectionElement> intersection_list;
QHash <QString, QPointer <RoomObject> > & envobjects = r->GetRoomObjects();
const bool room_overrides_teleport = r->GetProperties()->GetTeleportOverride();
//intersect with room template
QPointer <RoomTemplate> room_temp = r->GetRoomTemplate();
if (!room_overrides_teleport && room_temp && room_temp->GetEnvObject()) {
QList <QVector3D> vs;
QList <QVector3D> ns;
QList <QVector2D> uvs;
if (room_temp->GetEnvObject()->GetRaycastIntersection(transform, vs, ns, uvs)) {
for (int i=0; i<vs.size(); ++i) {
IntersectionElement intersection_element;
intersection_element.envobject = room_temp->GetEnvObject();
intersection_element.v = vs[i];
intersection_element.n = ns[i];
intersection_element.uv = uvs[i];
intersection_list.push_back(intersection_element);
}
}
}
//intersect with room objects (includes room portals)
QPointer <RoomObject> select = r->GetRoomObject(selected[cursor_index]);
for (QPointer <RoomObject> & o : envobjects) {
//null or already selected
if (o.isNull() || o == select) {
continue;
}
//56.0 - even invisible walls should keep player in
if (o->GetInterfaceObject() && !o->GetProperties()->GetVisible()) {
continue;
}
//Room teleport overrides
if (room_overrides_teleport && o->GetTeleportAssetObject() == 0 && o->GetType() == TYPE_OBJECT) {
continue;
}
QList <QVector3D> vs;
QList <QVector3D> ns;
QList <QVector2D> uvs;
o->GetRaycastIntersection(transform, vs, ns, uvs);
for (int i=0; i<vs.size(); ++i) {
IntersectionElement intersection_element;
intersection_element.envobject = o;
intersection_element.v = vs[i];
intersection_element.n = ns[i];
intersection_element.uv = uvs[i];
intersection_list.push_back(intersection_element);
}
}
//private websurfaces
if (GetPrivateWebsurfacesVisible()) {
for (int j=0; j<private_websurfaces.size(); ++j) {
QList <QVector3D> vs;
QList <QVector3D> ns;
QList <QVector2D> uvs;
if (private_websurfaces[j].obj->GetRaycastIntersection(transform, vs, ns, uvs)) {
for (int i=0; i<vs.size(); ++i) {
IntersectionElement intersection_element;
intersection_element.webobject = private_websurfaces[j].obj;
intersection_element.v = vs[i];
intersection_element.n = ns[i];
intersection_element.uv = uvs[i];
intersection_list.push_back(intersection_element);
}
}
}
}
//virtual menu
if (virtualmenu->GetVisible()) {
QHash <QString, QPointer <RoomObject> > & os = virtualmenu->GetEnvObjects();
for (QPointer <RoomObject> & o : os) {
if (o) {
QList <QVector3D> vs;
QList <QVector3D> ns;
QList <QVector2D> uvs;
if (o->GetRaycastIntersection(transform, vs, ns, uvs)) {
for (int i=0; i<vs.size(); ++i) {
IntersectionElement intersection_element;
intersection_element.menuobject = o;
intersection_element.v = vs[i];
intersection_element.n = ns[i];
intersection_element.uv = uvs[i];
intersection_list.push_back(intersection_element);
//qDebug() << "menu hit" << virtualmenu->GetVisible() << o->GetProperties()->GetJSID();
}
}
}
}
}
//find closest intersection point as the object for interaction
float min_dist = FLT_MAX;
int min_index = -1;
for (int i=0; i<intersection_list.size(); ++i) {
const float each_dist = (ray_p - intersection_list[i].v).length();
if (each_dist < min_dist - 0.001f) { //56.0 - subtract some min distance from minimum, prevents "selection aliasing"
min_index = i;
min_dist = each_dist;
/*if (intersection_list[i].envobject) {
qDebug() << "new min" << min_dist << intersection_list[i].envobject->GetJSID() << intersection_list[i].envobject->GetTeleportID();
}*/
}
}
//set user's cursor based on nearest interaction point
bool clear_websurface = true;
bool clear_video = true;
if (min_index >= 0) {
player->SetCursorPos(intersection_list[min_index].v, cursor_index);
player->SetCursorScale(player->ComputeCursorScale(cursor_index), cursor_index);
QVector3D x(1,0,0);
QVector3D y(0,1,0);
QVector3D z = intersection_list[min_index].n;
if (QVector3D::dotProduct(ray_d, z) > 0.0f) {
z = -z;
}
if (fabsf(QVector3D::dotProduct(y, z)) > 0.9f) {
x = player->GetRightDir();
y = QVector3D::crossProduct(z, x).normalized();
x = QVector3D::crossProduct(y, z).normalized();
}
else {
x = QVector3D::crossProduct(y, z).normalized();
y = QVector3D::crossProduct(z, x).normalized();
}
player->SetCursorXDir(x, cursor_index);
player->SetCursorYDir(y, cursor_index);
player->SetCursorZDir(z, cursor_index);
if (intersection_list[min_index].envobject) {
player->SetCursorObject(intersection_list[min_index].envobject->GetProperties()->GetJSID(), cursor_index);
const QPointer <AssetWebSurface> web = intersection_list[min_index].envobject->GetAssetWebSurface();
const QPointer <AssetVideo> vid = intersection_list[min_index].envobject->GetAssetVideo();
if (web) {
//57.1 - select websurfaces only on hover
if (controller_manager->GetUsingSpatiallyTrackedControllers() && !controller_manager->GetStates()[cursor_index].GetClick().hover) {
}
else {
//websurface_selected[cursor_index] = web;
}
clear_websurface = false;
}
if (vid) {
video_selected[cursor_index] = vid;
clear_video = false;
}
}
else if (intersection_list[min_index].menuobject) {
player->SetCursorObject(intersection_list[min_index].menuobject->GetProperties()->GetJSID(), cursor_index);
}
else if (intersection_list[min_index].webobject) {
player->SetCursorObject(intersection_list[min_index].webobject->GetProperties()->GetJSID(), cursor_index);
//websurface_selected[cursor_index] = intersection_list[min_index].webobject->GetAssetWebSurface();
clear_websurface = false;
}
player->SetCursorActive(true, cursor_index);
cursor_uv[cursor_index] = QPointF(intersection_list[min_index].uv.x(), intersection_list[min_index].uv.y());
}
else {
player->SetCursorObject("", cursor_index);
player->SetCursorPos(player->GetProperties()->GetEyePoint() + player->GetProperties()->GetViewDir()->toQVector3D(), cursor_index);
player->SetCursorXDir(QVector3D::crossProduct(player->GetProperties()->GetUpDir()->toQVector3D(),
-player->GetProperties()->GetViewDir()->toQVector3D()).normalized(), cursor_index);
player->SetCursorYDir(player->GetProperties()->GetUpDir()->toQVector3D(), cursor_index);
player->SetCursorZDir(-player->GetProperties()->GetViewDir()->toQVector3D(), cursor_index);
player->SetCursorScale(0.0f, cursor_index);
player->SetCursorActive(false, cursor_index);
cursor_uv[cursor_index] = QPointF(0,0);
}
if (clear_websurface) {
websurface_selected[cursor_index].clear();
}
if (clear_video) {
video_selected[cursor_index].clear();
}
//qDebug() << "Game::UpdateCursorRaycast" << ray_p << ray_d << intersection_list.size() << player->GetCursorPos(0);
return min_dist;
}
void Game::DrawFadingGL()
{
QPointer <AssetShader> shader = Room::GetTransparencyShader();
if (shader == NULL || !shader->GetCompiled()) {
return;
}
if (fadestate == FADE_NONE) {
return;
}
Renderer * renderer = Renderer::m_pimpl;
renderer->SetDefaultFaceCullMode(FaceCullMode::DISABLED);
renderer->SetStencilOp(StencilOp(StencilOpAction::KEEP, StencilOpAction::KEEP, StencilOpAction::KEEP));
renderer->SetStencilFunc(StencilFunc(StencilTestFuncion::ALWAYS, StencilReferenceValue(0), StencilMask(0xffffffff)));
renderer->SetDepthFunc(DepthFunc::ALWAYS);
shader->SetUseTextureAll(false);
shader->SetUseClipPlane(false);
shader->SetFogEnabled(false);
const QVector3D v = player->GetProperties()->GetEyePoint();
const float s = (GetCurrentNearDist()+GetCurrentFarDist())*0.5f; //60.0 - cube needs to fit within clip planes
MathUtil::PushModelMatrix();
MathUtil::ModelMatrix().translate(v);
MathUtil::ModelMatrix().scale(s);
switch (fadestate) {
case FADE_NONE:
break;
case FADE_RELOAD1:
case FADE_RELOAD2:
case FADE_RESETPLAYER1:
case FADE_RESETPLAYER2:
case FADE_FORWARD_PLAYER1:
case FADE_FORWARD_PLAYER2:
case FADE_TELEPORT1:
case FADE_TELEPORT2:
{
const float duration = (fadestate == FADE_TELEPORT1 || fadestate == FADE_TELEPORT2) ? 250.0f : 1000.0f;
const float alpha = (fadestate == FADE_RELOAD1 ||
fadestate == FADE_RESETPLAYER1 ||
fadestate == FADE_FORWARD_PLAYER1 ||
fadestate == FADE_TELEPORT1) ? (float(fade_time.elapsed()) / duration) : 1.0f - (float(fade_time.elapsed()) / duration);
shader->SetAmbient(QVector3D(1,1,1));
shader->SetDiffuse(QVector3D(1,1,1));
shader->SetEmission(QVector3D(0,0,0));
shader->SetConstColour(QVector4D(0,0,0,alpha));
shader->SetUseTextureAll(false);
shader->SetUseLighting(false);
shader->UpdateObjectUniforms();
Renderer * renderer = Renderer::m_pimpl;
AbstractRenderCommand a(PrimitiveType::TRIANGLES,
renderer->GetTexturedCube2PrimCount(),
0,
0,
0,
renderer->GetTexturedCube2VAO(),
shader->GetProgramHandle(),
shader->GetFrameUniforms(),
shader->GetRoomUniforms(),
shader->GetObjectUniforms(),
shader->GetMaterialUniforms(),
renderer->GetCurrentlyBoundTextures(),
renderer->GetDefaultFaceCullMode(),
renderer->GetDepthFunc(),
renderer->GetDepthMask(),
renderer->GetStencilFunc(),
renderer->GetStencilOp(),
renderer->GetColorMask());
renderer->PushAbstractRenderCommand(a);
if (fade_time.elapsed() >= duration) {
switch (fadestate) {
case FADE_RELOAD1:
fade_time.start();
fadestate = FADE_RELOAD2;
env->ReloadRoom();
break;
case FADE_RELOAD2:
fadestate = FADE_NONE;
break;
case FADE_RESETPLAYER1:
{
fade_time.start();
fadestate = FADE_RESETPLAYER2;
if (env->GetCurRoom() && env->GetCurRoom()->GetParent()) {
env->NavigateToRoom(player, env->GetCurRoom()->GetParent());
}
else {
ResetPlayer();
}
}
break;
case FADE_RESETPLAYER2:
fadestate = FADE_NONE;
break;
case FADE_FORWARD_PLAYER1:
{
fade_time.start();
fadestate = FADE_FORWARD_PLAYER2;
bool moved_forward = false;
if (env->GetCurRoom()) {
//navigate "forward" to last of current room's child rooms
QList <QPointer <Room> > & children = env->GetCurRoom()->GetChildren();
if (!children.isEmpty() && children.last()) {
env->NavigateToRoom(player, children.last());
moved_forward = true;
}
}
if (!moved_forward) {
ResetPlayer();
}
}
break;
case FADE_FORWARD_PLAYER2:
fadestate = FADE_NONE;
break;
case FADE_TELEPORT1:
fade_time.start();
fadestate = FADE_TELEPORT2;
TeleportPlayer();
break;
case FADE_TELEPORT2:
fadestate = FADE_NONE;
break;
default:
break;
}
}
}
break;
}
shader->SetConstColour(QVector4D(1,1,1,1));
renderer->SetDefaultFaceCullMode(FaceCullMode::BACK);
renderer->SetDepthFunc(DepthFunc::LEQUAL);
MathUtil::PopModelMatrix();
}
QMatrix4x4 Game::GetMouseCursorTransform() const
{
return mouse_cursor_transform;
}
void Game::ComputeMouseCursorTransform(const QSize p_windowSize, const QPointF p_mousePos)
{
/*
qDebug() << "Game::ComputeMouseCursorTransform" << p_mousePos << p_windowSize;
qDebug() << "model" << MathUtil::ModelMatrix();
qDebug() << "view" << MathUtil::ViewMatrix();
qDebug() << "Game::ComputeMouseCursorTransform" << (MathUtil::ModelMatrix() * MathUtil::ViewMatrix()).column(2) << (MathUtil::ModelMatrix() * MathUtil::ViewMatrix()).column(3);
*/
GLdouble modelMatrix[16];
GLdouble projMatrix[16];
GLint viewport[4];
QMatrix4x4 persp;
QPointer <Room> r = env->GetCurRoom();
const float near_dist = r->GetProperties()->GetNearDist();
const float far_dist = r->GetProperties()->GetFarDist();
if (player->GetHMDEnabled()) { //54.10 hack: forces cursor to remain at centre (look to click)
persp.perspective(0.01f, float(p_windowSize.width()) / float(p_windowSize.height()), near_dist, far_dist);
}
else {
persp.perspective(SettingsManager::GetFOV(), float(p_windowSize.width()) / float(p_windowSize.height()), near_dist, far_dist);
}
for (uint32_t i = 0; i < 16; ++i)
{
modelMatrix[i] = (MathUtil::ModelMatrix() * MathUtil::ViewMatrix()).constData()[i];
projMatrix[i] = persp.constData()[i]; //52.11 - bugfix for skewed projection matrices, make ray come out from centre
}
viewport[0] = 0;
viewport[1] = 0;
viewport[2] = p_windowSize.width();
viewport[3] = p_windowSize.height();
GLdouble new_x, new_y, new_z;
MathUtil::UnProject(p_mousePos.x(), p_mousePos.y(), 0.9999f, modelMatrix, projMatrix, viewport, &new_x, &new_y, &new_z);
const QVector3D z = (QVector3D(new_x, new_y, new_z) - player->GetProperties()->GetEyePoint()).normalized();
const QVector3D x = QVector3D::crossProduct(QVector3D(0,1,0), z).normalized();
const QVector3D y = QVector3D::crossProduct(x, z).normalized();
QMatrix4x4 m;
m.setColumn(0, x);
m.setColumn(1, y);
m.setColumn(2, z);
m.setColumn(3, player->GetProperties()->GetEyePoint());
m.setRow(3, QVector4D(0,0,0,1));
//qDebug() << "Game::ComputeMouseCursorTransform()" << cursor_win << new_x << new_y << new_z << m;
mouse_cursor_transform = m;
}
void Game::DrawGL(const float ipd, const QMatrix4x4 head_xform, const bool set_modelmatrix, const QSize p_windowSize, const QPointF p_mousePos)
{
QPointer <AssetShader> trans_shader = Room::GetTransparencyShader();
if (trans_shader == NULL || !trans_shader->GetCompiled()) {
return;
}
//setup camera
if (!set_modelmatrix) {
MathUtil::PushModelMatrix();
player->SetViewGL(true, ipd, head_xform);
MathUtil::PopModelMatrix();
}
else {
player->SetViewGL(true, ipd, head_xform);
}
//54.10 - move update call here, because some player position stuff has not updated, and this causes JS set attribs to jump around
env->Update2(player, multi_players);
if (!controller_manager->GetUsingSpatiallyTrackedControllers()) {
ComputeMouseCursorTransform(p_windowSize, p_mousePos);
UpdateCursorRaycast(GetMouseCursorTransform(), 0);
}
// Draw current room
env->draw_current_room(multi_players, player, true);
// TODO: This is temporaily disabled while I implement the new system using Bullet Physics
/*
if (show_position_mode && show_collision_volumes)
{
trans_shader->BindShaderGL();
env->GetPlayerRoom()->DrawCollisionModelGL(trans_shader);
trans_shader->UnbindShaderGL();
}*/
// Update the cursor (Object ID, normal, world-space location)
QPointer <Room> r = env->GetCurRoom();
Renderer::m_pimpl->BeginScope(RENDERER::RENDER_SCOPE::VIRTUAL_MENU);
r->BindShader(Room::GetTransparencyShader());
DrawVirtualMenu();
r->UnbindShader(Room::GetTransparencyShader());
Renderer::m_pimpl->EndCurrentScope();
// Draw child rooms
env->draw_child_rooms(multi_players, player, true);
// Always update player avatar's head transform
QPointer <RoomObject> player_avatar = multi_players->GetPlayer();
if (player_avatar) {
player_avatar->GetProperties()->SetViewDir(player->GetProperties()->GetViewDir());
player_avatar->GetProperties()->SetUpDir(player->GetProperties()->GetUpDir());
}
//draw player avatar (if enabled)
Renderer::m_pimpl->BeginScope(RENDERER::RENDER_SCOPE::AVATARS);
if (SettingsManager::GetSelfAvatar()) {
QPointer <AssetShader> shader = Room::GetTransparencyShader();
if (r->GetAssetShader()) {
shader = r->GetAssetShader();
}
r->BindShader(shader);
QPointer <RoomObject> player_avatar = multi_players->GetPlayer();
GhostFrame frame0;
GhostFrame frame1;
frame1.time_sec = 1.0f;
frame1.pos = player->GetProperties()->GetPos()->toQVector3D();
frame1.dir = player->GetProperties()->GetDir()->toQVector3D();
frame1.dir.setY(0.0f);
frame1.dir.normalize();
frame1.SetHeadXForm(player->GetProperties()->GetUpDir()->toQVector3D(),
player->GetProperties()->GetViewDir()->toQVector3D());
frame1.hands.first = player->GetHand(0);
frame1.hands.second = player->GetHand(1);
player_avatar->SetHMDType(player->GetHMDType());
player_avatar->GetProperties()->SetPos(player->GetProperties()->GetPos()->toQVector3D());
// set first and last frames to be the same (current packet)
frame0 = frame1;
frame0.time_sec = 0.0f;
QVector <GhostFrame> frames;
frames.push_back(frame0);
frames.push_back(frame1);
player_avatar->GetAssetGhost()->SetFromFrames(frames, 1000);
// ghost needs to be processed
player_avatar->Update(player->GetDeltaTime());
player_avatar->DrawGL(shader, true, player->GetProperties()->GetPos()->toQVector3D());
r->UnbindShader(shader);
}
Renderer::m_pimpl->EndCurrentScope();
//draw controllers (vive, oculus touch, Leap Motion, etc.)
Renderer::m_pimpl->BeginScope(RENDERER::RENDER_SCOPE::CONTROLLERS);
if (controller_manager) {
QPointer <AssetShader> shader = Room::GetTransparencyShader();
if (r->GetAssetShader()) {
shader = r->GetAssetShader();
}
r->BindShader(shader);
controller_manager->DrawGL(shader, player->GetTransform());
r->UnbindShader(shader);
}
Renderer::m_pimpl->EndCurrentScope();
// Draw menu before cursor update if it is selected
Renderer::m_pimpl->BeginScope(RENDERER::RENDER_SCOPE::CURSOR);
DrawCursorGL();
Renderer::m_pimpl->EndCurrentScope();
Renderer::m_pimpl->BeginScope(RENDERER::RENDER_SCOPE::OVERLAYS);
DrawOverlaysGL();
DrawFadingGL();
Renderer::m_pimpl->EndCurrentScope();
// Recomputes the AABB's for all RoomObjects and adds them to a fresh instance of
// the rooms physics world then draws it
/*if (show_collision_volumes)
{
env->GetPlayerRoom()->ResetPhysicsWorld();
}*/
}
void Game::initializeGL()
{
//cursor stuff
state = JVR_STATE_DEFAULT;
AssetImage::initializeGL();
TextGeom::initializeGL();
RoomObject::initializeGL();
SpinAnimation::initializeGL();
Room::initializeGL();
fade_time.start();
}
void Game::mouseMoveEvent(QMouseEvent * e, const float x, const float y, const int cursor_index, const QSize , const QPointF )
{
cursor_win += QPointF(x, y);
mouse_move_accum += QPointF(x, y) * 0.05f;
const bool left_btn = ((e->buttons() & Qt::LeftButton) > 0);
QPointer <Room> r = env->GetCurRoom();
QPointer <RoomObject> selected_obj = r->GetRoomObject(selected[cursor_index]);
QPointer <RoomObject> cursor_obj = r->GetRoomObject(player->GetCursorObject(cursor_index));
switch (state) {
case JVR_STATE_DEFAULT:
case JVR_STATE_INTERACT_TELEPORT:
case JVR_STATE_DRAGDROP:
//release 60.0 - spin/tilt view BEFORE passing mouse move event to websurface
player->SpinView(x * 0.1f, true);
if (GetMouseDoPitch()) {
const float tilt_amount = y * 0.1f * (SettingsManager::GetInvertYEnabled() ? 1.0f : -1.0f);
player->TiltView(tilt_amount);
}
r->CallJSFunction("room.onMouseMove", player, multi_players);
if (left_btn) {
r->CallJSFunction("room.onMouseDrag", player, multi_players);
}
//qDebug() << "Game::mouseMoveEvent" << selected[cursor_index] << envobjects.contains(selected[cursor_index]);
if (state == JVR_STATE_DRAGDROP) {
UpdateDragAndDropPosition(selected_obj, cursor_index);
}
if (websurface_selected[cursor_index]){
const QPoint cursor_pos(float(websurface_selected[cursor_index]->GetProperties()->GetWidth())*cursor_uv[cursor_index].x(),
float(websurface_selected[cursor_index]->GetProperties()->GetHeight())*cursor_uv[cursor_index].y());
//Release 60.0 - note: player spin/tilt has to happen before this, as this method does not seem to contnue after this call
QMouseEvent e2(QEvent::MouseMove, cursor_pos, e->button(), e->buttons(), e->modifiers());
websurface_selected[cursor_index]->mouseMoveEvent(&e2, cursor_index);
QString url_str = websurface_selected[cursor_index]->GetLinkClicked(cursor_index).toString().trimmed();
if (state != JVR_STATE_DRAGDROP && !(url_str == websurface_selected[cursor_index]->GetURL() || url_str == "") && ((e->buttons() & Qt::RightButton) > 0) && rmb_held_time.elapsed() > 100) {
DragAndDropFromWebsurface(keys[Qt::Key_Control]?"Drag+Drop":"Drag+Pin", cursor_index);
//websurface_selected[cursor_index].clear();
}
}
else if (cursor_obj) {
//qDebug() << player->GetCursorObject(cursor_index) << o->GetJSID() << o->GetOriginalURL() << o->GetWebSurfaceID() << o->GetUUID();
if (websurface_selected[cursor_index] && cursor_obj->GetType() == TYPE_OBJECT && cursor_obj->GetAssetWebSurface()) {
/*
if websurface is still on about:blank, load it
if (websurface_selected[cursor_index]->GetURL() == "about:blank") {
websurface_selected[cursor_index]->SetURL(websurface_selected[cursor_index]->GetS("src"));
}
*/
//52.8 - disable mousemove on websurfaces to prevent native drag and drop operation
QPoint cursor_pos(float(websurface_selected[cursor_index]->GetProperties()->GetWidth())*cursor_uv[cursor_index].x(),
float(websurface_selected[cursor_index]->GetProperties()->GetHeight())*cursor_uv[cursor_index].y());
QMouseEvent e2(QEvent::MouseMove, cursor_pos, e->button(), e->buttons(), Qt::NoModifier);
websurface_selected[cursor_index]->mouseMoveEvent(&e2, cursor_index);
}
else if (video_selected[cursor_index] && (cursor_obj->GetType() == TYPE_VIDEO || cursor_obj->GetType() == TYPE_OBJECT) && cursor_obj->GetAssetVideo()) {
if (left_btn) {
QPoint cursor_pos(float(video_selected[cursor_index]->GetWidth(cursor_obj->GetMediaContext()))*cursor_uv[cursor_index].x(),
float(video_selected[cursor_index]->GetHeight(cursor_obj->GetMediaContext()))*cursor_uv[cursor_index].y());
QMouseEvent e2(QEvent::MouseMove, cursor_pos, e->button(), e->buttons(), Qt::NoModifier);
video_selected[cursor_index]->mouseMoveEvent(cursor_obj->GetMediaContext(), &e2);
}
}
}
break;
case JVR_STATE_UNIT_TRANSLATE:
if (selected_obj) {
if (fabsf(mouse_move_accum.x()) > 1.0f) {
EditModeTranslate(selected_obj,mouse_move_accum.x(),0,0);
mouse_move_accum.setX(0.0f);
}
if (fabsf(mouse_move_accum.y()) > 1.0f) {
EditModeTranslate(selected_obj,0, mouse_move_accum.y(),0);
mouse_move_accum.setY(0.0f);
}
}
break;
case JVR_STATE_UNIT_ROTATE:
if (selected_obj) {
if (fabsf(mouse_move_accum.x()) > 1.0f) {
EditModeRotate(selected_obj,mouse_move_accum.x(),0,0);
mouse_move_accum.setX(0.0f);
}
if (fabsf(mouse_move_accum.y()) > 1.0f) {
EditModeRotate(selected_obj,0, mouse_move_accum.y(),0);
mouse_move_accum.setY(0.0f);
}
}