/
viewports.cpp
1481 lines (1215 loc) · 43.8 KB
/
viewports.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
/** @file viewports.cpp Player viewports and related low-level rendering.
*
* @authors Copyright © 2003-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2015 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de_platform.h"
#include "render/viewports.h"
#include <QBitArray>
#include <de/concurrency.h>
#include <de/timer.h>
#include <de/vector1.h>
#include <de/GLInfo>
#include <de/GLState>
#include <doomsday/filesys/fs_util.h>
#include "clientapp.h"
#include "api_console.h"
#include "dd_main.h"
#include "dd_loop.h"
//#include "ui/editors/edit_bias.h"
#include "gl/gl_main.h"
#include "api_render.h"
#include "render/angleclipper.h"
#include "render/cameralensfx.h"
#include "render/fx/bloom.h"
#include "render/playerweaponanimator.h"
#include "render/r_draw.h"
#include "render/r_main.h"
#include "render/rendersystem.h"
#include "render/rendpoly.h"
#include "render/skydrawable.h"
#include "render/vissprite.h"
#include "render/vr.h"
#include "network/net_demo.h"
#include "world/linesighttest.h"
#include "world/thinkers.h"
#include "world/p_object.h"
#include "world/p_players.h"
#include "world/sky.h"
#include "BspLeaf"
#include "ConvexSubspace"
#include "Surface"
#include "Contact"
#include "client/clientsubsector.h"
#include "ui/ui_main.h"
#include "ui/clientwindow.h"
//#include "ui/widgets/gameuiwidget.h"
using namespace de;
using namespace world;
dd_bool firstFrameAfterLoad;
static dint loadInStartupMode;
static dint rendCameraSmooth = true; ///< Smoothed by default.
static dbyte showFrameTimePos;
static dbyte showViewAngleDeltas;
static dbyte showViewPosDeltas;
dint rendInfoTris;
static viewport_t *currentViewport;
struct FrameLuminous
{
coord_t distance;
duint isClipped;
};
static QVector<FrameLuminous> frameLuminous;
static QBitArray subspacesVisible;
static QBitArray generatorsVisible(Map::MAX_GENERATORS);
static dint frameCount;
static dint gridCols, gridRows;
static viewport_t viewportOfLocalPlayer[DDMAXPLAYERS];
static dint resetNextViewer = true;
dint R_FrameCount()
{
return frameCount;
}
void R_ResetFrameCount()
{
frameCount = 0;
}
#undef R_SetViewOrigin
DENG_EXTERN_C void R_SetViewOrigin(dint consoleNum, coord_t const origin[3])
{
if(consoleNum < 0 || consoleNum >= DDMAXPLAYERS) return;
DD_Player(consoleNum)->viewport().latest.origin = Vector3d(origin);
}
#undef R_SetViewAngle
DENG_EXTERN_C void R_SetViewAngle(dint consoleNum, angle_t angle)
{
if(consoleNum < 0 || consoleNum >= DDMAXPLAYERS) return;
DD_Player(consoleNum)->viewport().latest.setAngle(angle);
}
#undef R_SetViewPitch
DENG_EXTERN_C void R_SetViewPitch(dint consoleNum, dfloat pitch)
{
if(consoleNum < 0 || consoleNum >= DDMAXPLAYERS) return;
DD_Player(consoleNum)->viewport().latest.pitch = pitch;
}
void R_SetupDefaultViewWindow(dint consoleNum)
{
viewdata_t *vd = &DD_Player(consoleNum)->viewport();
if(consoleNum < 0 || consoleNum >= DDMAXPLAYERS) return;
vd->window =
vd->windowOld =
vd->windowTarget = Rectanglei::fromSize(Vector2i(0, 0), Vector2ui(DENG_GAMEVIEW_WIDTH, DENG_GAMEVIEW_HEIGHT));
vd->windowInter = 1;
}
void R_ViewWindowTicker(dint consoleNum, timespan_t ticLength)
{
viewdata_t *vd = &DD_Player(consoleNum)->viewport();
if(consoleNum < 0 || consoleNum >= DDMAXPLAYERS)
{
return;
}
vd->windowInter += dfloat(.4 * ticLength * TICRATE);
if(vd->windowInter >= 1)
{
vd->window = vd->windowTarget;
}
else
{
vd->window.moveTopLeft(Vector2i(de::roundf(de::lerp<dfloat>(vd->windowOld.topLeft.x, vd->windowTarget.topLeft.x, vd->windowInter)),
de::roundf(de::lerp<dfloat>(vd->windowOld.topLeft.y, vd->windowTarget.topLeft.y, vd->windowInter))));
vd->window.setSize(Vector2ui(de::roundf(de::lerp<dfloat>(vd->windowOld.width(), vd->windowTarget.width(), vd->windowInter)),
de::roundf(de::lerp<dfloat>(vd->windowOld.height(), vd->windowTarget.height(), vd->windowInter))));
}
}
#undef R_ViewWindowGeometry
DENG_EXTERN_C dint R_ViewWindowGeometry(dint player, RectRaw *geometry)
{
if(!geometry) return false;
if(player < 0 || player >= DDMAXPLAYERS) return false;
viewdata_t const &vd = DD_Player(player)->viewport();
geometry->origin.x = vd.window.topLeft.x;
geometry->origin.y = vd.window.topLeft.y;
geometry->size.width = vd.window.width();
geometry->size.height = vd.window.height();
return true;
}
#undef R_ViewWindowOrigin
DENG_EXTERN_C dint R_ViewWindowOrigin(dint player, Point2Raw *origin)
{
if(!origin) return false;
if(player < 0 || player >= DDMAXPLAYERS) return false;
viewdata_t const &vd = DD_Player(player)->viewport();
origin->x = vd.window.topLeft.x;
origin->y = vd.window.topLeft.y;
return true;
}
#undef R_ViewWindowSize
DENG_EXTERN_C dint R_ViewWindowSize(dint player, Size2Raw *size)
{
if(!size) return false;
if(player < 0 || player >= DDMAXPLAYERS) return false;
viewdata_t const &vd = DD_Player(player)->viewport();
size->width = vd.window.width();
size->height = vd.window.height();
return true;
}
/**
* @note Do not change values used during refresh here because we might be
* partway through rendering a frame. Changes should take effect on next
* refresh only.
*/
#undef R_SetViewWindowGeometry
DENG_EXTERN_C void R_SetViewWindowGeometry(dint player, RectRaw const *geometry, dd_bool interpolate)
{
dint p = P_ConsoleToLocal(player);
if(p < 0) return;
viewport_t const *vp = &viewportOfLocalPlayer[p];
viewdata_t *vd = &DD_Player(player)->viewport();
Rectanglei newGeom = Rectanglei::fromSize(Vector2i(de::clamp<dint>(0, geometry->origin.x, vp->geometry.width()),
de::clamp<dint>(0, geometry->origin.y, vp->geometry.height())),
Vector2ui(de::abs(geometry->size.width),
de::abs(geometry->size.height)));
if((unsigned) newGeom.bottomRight.x > vp->geometry.width())
{
newGeom.setWidth(vp->geometry.width() - newGeom.topLeft.x);
}
if((unsigned) newGeom.bottomRight.y > vp->geometry.height())
{
newGeom.setHeight(vp->geometry.height() - newGeom.topLeft.y);
}
// Already at this target?
if(vd->window == newGeom)
{
return;
}
// Record the new target.
vd->windowTarget = newGeom;
// Restart or advance the interpolation timer?
// If dimensions have not yet been set - do not interpolate.
if(interpolate && vd->window.size() != Vector2ui(0, 0))
{
vd->windowOld = vd->window;
vd->windowInter = 0;
}
else
{
vd->windowOld = vd->windowTarget;
vd->windowInter = 1; // Update on next frame.
}
}
#undef R_ViewPortGeometry
DENG_EXTERN_C dint R_ViewPortGeometry(dint player, RectRaw *geometry)
{
if(!geometry) return false;
dint p = P_ConsoleToLocal(player);
if(p == -1) return false;
viewport_t const &vp = viewportOfLocalPlayer[p];
geometry->origin.x = vp.geometry.topLeft.x;
geometry->origin.y = vp.geometry.topLeft.y;
geometry->size.width = vp.geometry.width();
geometry->size.height = vp.geometry.height();
return true;
}
#undef R_ViewPortOrigin
DENG_EXTERN_C dint R_ViewPortOrigin(dint player, Point2Raw *origin)
{
if(!origin) return false;
dint p = P_ConsoleToLocal(player);
if(p == -1) return false;
viewport_t const &vp = viewportOfLocalPlayer[p];
origin->x = vp.geometry.topLeft.x;
origin->y = vp.geometry.topLeft.y;
return true;
}
#undef R_ViewPortSize
DENG_EXTERN_C dint R_ViewPortSize(dint player, Size2Raw *size)
{
if(!size) return false;
dint p = P_ConsoleToLocal(player);
if(p == -1) return false;
viewport_t const &vp = viewportOfLocalPlayer[p];
size->width = vp.geometry.width();
size->height = vp.geometry.height();
return true;
}
#undef R_SetViewPortPlayer
DENG_EXTERN_C void R_SetViewPortPlayer(dint consoleNum, dint viewPlayer)
{
dint p = P_ConsoleToLocal(consoleNum);
if(p != -1)
{
viewportOfLocalPlayer[p].console = viewPlayer;
}
}
/**
* Calculate the placement and dimensions of a specific viewport.
* Assumes that the grid has already been configured.
*/
void R_UpdateViewPortGeometry(viewport_t *port, dint col, dint row)
{
DENG2_ASSERT(port);
Rectanglei newGeom = Rectanglei(Vector2i(DENG_GAMEVIEW_X + col * DENG_GAMEVIEW_WIDTH / gridCols,
DENG_GAMEVIEW_Y + row * DENG_GAMEVIEW_HEIGHT / gridRows),
Vector2i(DENG_GAMEVIEW_X + (col+1) * DENG_GAMEVIEW_WIDTH / gridCols,
DENG_GAMEVIEW_Y + (row+1) * DENG_GAMEVIEW_HEIGHT / gridRows));
ddhook_viewport_reshape_t p;
if(port->geometry == newGeom) return;
bool doReshape = false;
if(port->console != -1 && Plug_CheckForHook(HOOK_VIEWPORT_RESHAPE))
{
p.oldGeometry.origin.x = port->geometry.topLeft.x;
p.oldGeometry.origin.y = port->geometry.topLeft.y;
p.oldGeometry.size.width = port->geometry.width();
p.oldGeometry.size.height = port->geometry.height();
doReshape = true;
}
port->geometry = newGeom;
if(doReshape)
{
p.geometry.origin.x = port->geometry.topLeft.x;
p.geometry.origin.y = port->geometry.topLeft.y;
p.geometry.size.width = port->geometry.width();
p.geometry.size.height = port->geometry.height();
DoomsdayApp::plugins().callAllHooks(HOOK_VIEWPORT_RESHAPE, port->console, (void *)&p);
}
}
bool R_SetViewGrid(dint numCols, dint numRows)
{
if(numCols > 0 && numRows > 0)
{
if(numCols * numRows > DDMAXPLAYERS)
{
return false;
}
if(numCols != gridCols || numRows != gridRows)
{
// The number of consoles has changes; LensFx needs to reallocate resources
// only for the consoles in use.
/// @todo This could be done smarter, only for the affected viewports. -jk
LensFx_GLRelease();
}
if(numCols > DDMAXPLAYERS)
numCols = DDMAXPLAYERS;
if(numRows > DDMAXPLAYERS)
numRows = DDMAXPLAYERS;
gridCols = numCols;
gridRows = numRows;
}
dint p = 0;
for(dint y = 0; y < gridRows; ++y)
for(dint x = 0; x < gridCols; ++x)
{
// The console number is -1 if the viewport belongs to no one.
viewport_t *vp = &viewportOfLocalPlayer[p];
dint const console = P_LocalToConsole(p);
if(console != -1)
{
vp->console = DD_Player(console)->viewConsole;
}
else
{
vp->console = -1;
}
R_UpdateViewPortGeometry(vp, x, y);
++p;
}
return true;
}
void R_ResetViewer()
{
resetNextViewer = 1;
}
dint R_NextViewer()
{
return resetNextViewer;
}
/**
* The components whose difference is too large for interpolation will be
* snapped to the sharp values.
*/
void R_CheckViewerLimits(viewer_t *src, viewer_t *dst)
{
dint const MAXMOVE = 32;
/// @todo Remove this snapping. The game should determine this and disable the
/// the interpolation as required.
if(fabs(dst->origin.x - src->origin.x) > MAXMOVE ||
fabs(dst->origin.y - src->origin.y) > MAXMOVE)
{
src->origin = dst->origin;
}
/*
if(abs(dint(dst->angle) - dint(src->angle)) >= ANGLE_45)
{
LOG_DEBUG("R_CheckViewerLimits: Snap camera angle to %08x.") << dst->angle;
src->angle = dst->angle;
}
*/
}
/**
* Retrieve the current sharp camera position.
*/
viewer_t R_SharpViewer(ClientPlayer &player)
{
DENG2_ASSERT(player.publicData().mo);
ddplayer_t const &ddpl = player.publicData();
viewer_t view(player.viewport().latest);
if((ddpl.flags & DDPF_CHASECAM) && !(ddpl.flags & DDPF_CAMERA))
{
// STUB
// This needs to be fleshed out with a proper third person
// camera control setup. Currently we simply project the viewer's
// position a set distance behind the ddpl.
dfloat const distance = 90;
duint angle = view.angle() >> ANGLETOFINESHIFT;
duint pitch = angle_t(LOOKDIR2DEG(view.pitch) / 360 * ANGLE_MAX) >> ANGLETOFINESHIFT;
view.origin -= Vector3d(FIX2FLT(fineCosine[angle]),
FIX2FLT(finesine[angle]),
FIX2FLT(finesine[pitch])) * distance;
}
// Check that the viewZ doesn't go too high or low.
// Cameras are not restricted.
if(!(ddpl.flags & DDPF_CAMERA))
{
if(view.origin.z > ddpl.mo->ceilingZ - 4)
{
view.origin.z = ddpl.mo->ceilingZ - 4;
}
if(view.origin.z < ddpl.mo->floorZ + 4)
{
view.origin.z = ddpl.mo->floorZ + 4;
}
}
return view;
}
void R_NewSharpWorld()
{
if(resetNextViewer)
{
resetNextViewer = 2;
}
for(dint i = 0; i < DDMAXPLAYERS; ++i)
{
player_t *plr = DD_Player(i);
viewdata_t *vd = &plr->viewport();
if(!plr->isInGame())
{
continue;
}
viewer_t sharpView = R_SharpViewer(*plr);
// The game tic has changed, which means we have an updated sharp
// camera position. However, the position is at the beginning of
// the tic and we are most likely not at a sharp tic boundary, in
// time. We will move the viewer positions one step back in the
// buffer. The effect of this is that [0] is the previous sharp
// position and [1] is the current one.
vd->lastSharp[0] = vd->lastSharp[1];
vd->lastSharp[1] = sharpView;
R_CheckViewerLimits(vd->lastSharp, &sharpView);
}
if(ClientApp::world().hasMap())
{
Map &map = ClientApp::world().map();
map.updateTrackedPlanes();
map.updateScrollingSurfaces();
}
}
void R_UpdateViewer(dint consoleNum)
{
DENG2_ASSERT(consoleNum >= 0 && consoleNum < DDMAXPLAYERS);
dint const VIEWPOS_MAX_SMOOTHDISTANCE = 172;
player_t *player = DD_Player(consoleNum);
viewdata_t *vd = &player->viewport();
if(!player->isInGame()) return;
viewer_t sharpView = R_SharpViewer(*player);
if(resetNextViewer ||
(sharpView.origin - vd->current.origin).length() > VIEWPOS_MAX_SMOOTHDISTANCE)
{
// Keep reseting until a new sharp world has arrived.
if(resetNextViewer > 1)
{
resetNextViewer = 0;
}
// Just view from the sharp position.
vd->current = sharpView;
vd->lastSharp[0] = vd->lastSharp[1] = sharpView;
}
// While the game is paused there is no need to calculate any
// time offsets or interpolated camera positions.
else //if(!clientPaused)
{
// Calculate the smoothed camera position, which is somewhere between
// the previous and current sharp positions. This introduces a slight
// delay (max. 1/35 sec) to the movement of the smoothed camera.
viewer_t smoothView = vd->lastSharp[0].lerp(vd->lastSharp[1], frameTimePos);
// Use the latest view angles known to us if the interpolation flags
// are not set. The interpolation flags are used when the view angles
// are updated during the sharp tics and need to be smoothed out here.
// For example, view locking (dead or camera setlock).
/*if(!(player->shared.flags & DDPF_INTERYAW))
smoothView.angle = sharpView.angle;*/
/*if(!(player->shared.flags & DDPF_INTERPITCH))
smoothView.pitch = sharpView.pitch;*/
vd->current = smoothView;
// Monitor smoothness of yaw/pitch changes.
if(showViewAngleDeltas)
{
struct OldAngle {
ddouble time;
dfloat yaw;
dfloat pitch;
};
static OldAngle oldAngle[DDMAXPLAYERS];
OldAngle *old = &oldAngle[DoomsdayApp::players().indexOf(viewPlayer)];
dfloat yaw = (ddouble)smoothView.angle() / ANGLE_MAX * 360;
LOGDEV_MSG("(%i) F=%.3f dt=%-10.3f dx=%-10.3f dy=%-10.3f "
"Rdx=%-10.3f Rdy=%-10.3f")
<< SECONDS_TO_TICKS(gameTime)
<< frameTimePos
<< sysTime - old->time
<< yaw - old->yaw
<< smoothView.pitch - old->pitch
<< (yaw - old->yaw) / (sysTime - old->time)
<< (smoothView.pitch - old->pitch) / (sysTime - old->time);
old->yaw = yaw;
old->pitch = smoothView.pitch;
old->time = sysTime;
}
// The Rdx and Rdy should stay constant when moving.
if(showViewPosDeltas)
{
struct OldPos {
ddouble time;
Vector3f pos;
};
static OldPos oldPos[DDMAXPLAYERS];
OldPos *old = &oldPos[DoomsdayApp::players().indexOf(viewPlayer)];
LOGDEV_MSG("(%i) F=%.3f dt=%-10.3f dx=%-10.3f dy=%-10.3f dz=%-10.3f dx/dt=%-10.3f dy/dt=%-10.3f")
<< SECONDS_TO_TICKS(gameTime)
<< frameTimePos
<< sysTime - old->time
<< smoothView.origin.x - old->pos.x
<< smoothView.origin.y - old->pos.y
<< smoothView.origin.z - old->pos.z
<< (smoothView.origin.x - old->pos.x) / (sysTime - old->time)
<< (smoothView.origin.y - old->pos.y) / (sysTime - old->time);
old->pos = smoothView.origin;
old->time = sysTime;
}
}
// Update viewer.
angle_t const viewYaw = vd->current.angle();
duint const an = viewYaw >> ANGLETOFINESHIFT;
vd->viewSin = FIX2FLT(finesine[an]);
vd->viewCos = FIX2FLT(fineCosine[an]);
// Calculate the front, up and side unit vectors.
dfloat const yawRad = ((viewYaw / (dfloat) ANGLE_MAX) *2) * PI;
dfloat const pitchRad = vd->current.pitch * 85 / 110.f / 180 * PI;
// The front vector.
vd->frontVec.x = cos(yawRad) * cos(pitchRad);
vd->frontVec.z = sin(yawRad) * cos(pitchRad);
vd->frontVec.y = sin(pitchRad);
// The up vector.
vd->upVec.x = -cos(yawRad) * sin(pitchRad);
vd->upVec.z = -sin(yawRad) * sin(pitchRad);
vd->upVec.y = cos(pitchRad);
// The side vector is the cross product of the front and up vectors.
vd->sideVec = vd->frontVec.cross(vd->upVec);
}
/**
* Prepare rendering the view of the given player.
*/
void R_SetupFrame(player_t *player)
{
#define MINEXTRALIGHTFRAMES 2
// This is now the current view player.
viewPlayer = player;
// Reset the GL triangle counter.
//polyCounter = 0;
if(showFrameTimePos)
{
LOGDEV_VERBOSE("frametime = %f") << frameTimePos;
}
// Handle extralight (used to light up the world momentarily (used for
// e.g. gun flashes). We want to avoid flickering, so when ever it is
// enabled; make it last for a few frames.
if(player->targetExtraLight != player->publicData().extraLight)
{
player->targetExtraLight = player->publicData().extraLight;
player->extraLightCounter = MINEXTRALIGHTFRAMES;
}
if(player->extraLightCounter > 0)
{
player->extraLightCounter--;
if(player->extraLightCounter == 0)
player->extraLight = player->targetExtraLight;
}
validCount++;
extraLight = player->extraLight;
extraLightDelta = extraLight / 16.0f;
if(!freezeRLs)
{
R_ClearVisSprites();
}
#undef MINEXTRALIGHTFRAMES
}
void R_RenderPlayerViewBorder()
{
R_DrawViewBorder();
}
void R_UseViewPort(viewport_t const *vp)
{
DENG2_ASSERT_IN_RENDER_THREAD();
DENG_ASSERT_GL_CONTEXT_ACTIVE();
if (!vp)
{
currentViewport = nullptr;
/*ClientWindow::main().game().glApplyViewport(
Rectanglei::fromSize(Vector2i(DENG_GAMEVIEW_X, DENG_GAMEVIEW_Y),
Vector2ui(DENG_GAMEVIEW_WIDTH, DENG_GAMEVIEW_HEIGHT)));*/
}
else
{
currentViewport = const_cast<viewport_t *>(vp);
//ClientWindow::main().game().glApplyViewport(vp->geometry);
}
}
void R_UseViewPort(int consoleNum)
{
int local = P_ConsoleToLocal(consoleNum);
if (local >= 0)
{
R_UseViewPort(&viewportOfLocalPlayer[local]);
}
else
{
R_UseViewPort(nullptr);
}
}
Rectanglei R_ConsoleRect(int console)
{
int local = P_ConsoleToLocal(console);
if (local < 0) return Rectanglei();
auto const &port = viewportOfLocalPlayer[local];
return Rectanglei(port.geometry.topLeft.x,
port.geometry.topLeft.y,
port.geometry.width(),
port.geometry.height());
}
Rectanglei R_Console3DViewRect(int console)
{
Rectanglei rect = R_ConsoleRect(console);
auto const &pv = DD_Player(console)->viewport();
return Rectanglei(rect.left() + pv.window.topLeft.x,
rect.top() + pv.window.topLeft.y,
de::min(rect.width(), pv.window.width()),
de::min(rect.height(), pv.window.height()));
}
viewport_t const *R_CurrentViewPort()
{
return currentViewport;
}
void R_RenderBlankView()
{
UI_DrawDDBackground(Point2Raw{0, 0}, Size2Raw{{{320, 200}}}, 1);
}
static void setupPlayerSprites()
{
DENG2_ASSERT(viewPlayer);
// There are no 3D psprites.
::psp3d = false;
ddplayer_t *ddpl = &viewPlayer->publicData();
// Cameramen have no psprites.
if((ddpl->flags & DDPF_CAMERA) || (ddpl->flags & DDPF_CHASECAM))
return;
if(!ddpl->mo) return;
mobj_t *mob = ddpl->mo;
if(!Mobj_HasSubsector(*mob)) return;
auto &subsec = Mobj_Subsector(*mob).as<world::ClientSubsector>();
// Determine if we should be drawing all the psprites full bright?
bool fullBright = CPP_BOOL(::levelFullBright);
if(!fullBright)
{
for(ddpsprite_t const &psp : ddpl->pSprites)
{
if(!psp.statePtr) continue;
// If one of the psprites is fullbright, both are.
if(psp.statePtr->flags & STF_FULLBRIGHT)
{
fullBright = true;
}
}
}
viewdata_t const *viewData = &viewPlayer->viewport();
for(dint i = 0; i < DDMAXPSPRITES; ++i)
{
vispsprite_t *spr = &visPSprites[i];
spr->type = VPSPR_SPRITE;
spr->psp = &ddpl->pSprites[i];
spr->origin = viewData->current.origin;
spr->bspLeaf = &Mobj_BspLeafAtOrigin(*mob);
if(!spr->psp->statePtr) continue;
// First, determine whether this is a model or a sprite.
FrameModelDef *mf = nullptr, *nextmf = nullptr;
dfloat inter = 0;
if(useModels)
{
if(viewPlayer->playerWeaponAnimator().hasModel())
{
viewPlayer->playerWeaponAnimator().setupVisPSprite(*spr);
// There are 3D psprites.
::psp3d = true;
continue;
}
else
{
// Is there a model for this frame?
MobjThinker dummy;
// Setup a dummy for the call to R_CheckModelFor.
dummy->state = spr->psp->statePtr;
dummy->tics = spr->psp->tics;
mf = Mobj_ModelDef(dummy, &nextmf, &inter);
}
}
// Use a 3D model?
if(mf)
{
// There are 3D psprites.
::psp3d = true;
spr->type = VPSPR_MODEL;
spr->data.model.flags = 0;
// 32 is the raised weapon height.
spr->data.model.topZ = viewData->current.origin.z;
spr->data.model.secFloor = subsec.visFloor().heightSmoothed();
spr->data.model.secCeil = subsec.visCeiling().heightSmoothed();
spr->data.model.pClass = 0;
spr->data.model.floorClip = 0;
spr->data.model.mf = mf;
spr->data.model.nextMF = nextmf;
spr->data.model.inter = inter;
spr->data.model.viewAligned = true;
// Offsets to rotation angles.
spr->data.model.yawAngleOffset = spr->psp->pos[0] * weaponOffsetScale - 90;
spr->data.model.pitchAngleOffset =
(32 - spr->psp->pos[1]) * weaponOffsetScale * weaponOffsetScaleY / 1000.0f;
// Is the FOV shift in effect?
if (weaponFOVShift > 0 && weaponFixedFOV > 90)
{
spr->data.model.pitchAngleOffset -= weaponFOVShift * (weaponFixedFOV - 90) / 90;
}
// Real rotation angles.
spr->data.model.yaw =
viewData->current.angle() / (dfloat) ANGLE_MAX *-360 + spr->data.model.yawAngleOffset + 90;
spr->data.model.pitch = viewData->current.pitch * 85 / 110 + spr->data.model.yawAngleOffset;
std::memset(spr->data.model.visOff, 0, sizeof(spr->data.model.visOff));
spr->data.model.alpha = spr->psp->alpha;
spr->data.model.stateFullBright = (spr->psp->flags & DDPSPF_FULLBRIGHT) != 0;
}
else
{
// No, draw a 2D sprite (in Rend_DrawPlayerSprites).
spr->type = VPSPR_SPRITE;
spr->data.sprite.alpha = spr->psp->alpha;
spr->data.sprite.isFullBright = (spr->psp->flags & DDPSPF_FULLBRIGHT) != 0;
}
}
}
static Matrix4f frameViewerMatrix;
static void setupViewMatrix()
{
auto &rend = ClientApp::renderSystem();
// These will be the matrices for the current frame.
rend.uProjectionMatrix() = Rend_GetProjectionMatrix();
rend.uViewMatrix() = Rend_GetModelViewMatrix(DoomsdayApp::players().indexOf(viewPlayer));
frameViewerMatrix = rend.uProjectionMatrix().toMatrix4f() * rend.uViewMatrix().toMatrix4f();
}
Matrix4f const &Viewer_Matrix()
{
return frameViewerMatrix;
}
enum ViewState { Default2D, PlayerView3D, PlayerSprite2D };
static void changeViewState(ViewState viewState) //, viewport_t const *port, viewdata_t const *viewData)
{
//DENG2_ASSERT(port && viewData);
DENG2_ASSERT_IN_RENDER_THREAD();
DENG_ASSERT_GL_CONTEXT_ACTIVE();
switch (viewState)
{
case PlayerView3D:
DGL_CullFace(DGL_BACK);
DGL_Enable(DGL_DEPTH_TEST);
GL_ProjectionMatrix(); // The 3D projection matrix.
break;
case PlayerSprite2D:
{
auto const conRect = R_ConsoleRect(displayPlayer);
auto const viewRect = R_Console3DViewRect(displayPlayer);
dint const height = dint(SCREENHEIGHT
* ( float(conRect.width()) * float(viewRect.height())
/ float(viewRect.width()) )
/ float(conRect.height()));
scalemode_t sm = R_ChooseScaleMode(SCREENWIDTH, SCREENHEIGHT,
conRect.width(), conRect.height(),
scalemode_t(weaponScaleMode));
DGL_MatrixMode(DGL_PROJECTION);
DGL_LoadIdentity();
if(sm == SCALEMODE_STRETCH)
{
DGL_Ortho(0, 0, SCREENWIDTH, height, -1, 1);
}
else
{
// Use an orthographic projection in native screenspace. Then
// translate and scale the projection to produce an aspect
// corrected coordinate space at 4:3, aligned vertically to
// the bottom and centered horizontally in the window.
DGL_Ortho(0, 0, conRect.width(), conRect.height(), -1, 1);
DGL_Translatef(conRect.width()/2, conRect.height(), 0);
if(conRect.width() >= conRect.height())
{
DGL_Scalef(dfloat( conRect.height() ) / SCREENHEIGHT,
dfloat( conRect.height() ) / SCREENHEIGHT, 1);
}
else
{
DGL_Scalef(dfloat( conRect.width() ) / SCREENWIDTH,
dfloat( conRect.width() ) / SCREENWIDTH, 1);
}
// Special case: viewport height is greater than width.
// Apply an additional scaling factor to prevent player sprites
// looking too small.
if(conRect.height() > conRect.width())
{
dfloat extraScale = (dfloat(conRect.height() * 2) / conRect.width()) / 2;
DGL_Scalef(extraScale, extraScale, 1);
}
DGL_Translatef(-(SCREENWIDTH / 2), -SCREENHEIGHT, 0);
DGL_Scalef(1, dfloat( SCREENHEIGHT ) / height, 1);
}
DGL_MatrixMode(DGL_MODELVIEW);
DGL_LoadIdentity();
// Depth testing must be disabled so that psprite 1 will be drawn
// on top of psprite 0 (Doom plasma rifle fire).
DGL_Disable(DGL_DEPTH_TEST);
break;
}
case Default2D:
DGL_CullFace(DGL_NONE);
DGL_Disable(DGL_DEPTH_TEST);
break;
}
//std::memcpy(¤tView, port, sizeof(currentView));
//viewpx = port->geometry.topLeft.x + viewData->window.topLeft.x;
//viewpy = port->geometry.topLeft.y + viewData->window.topLeft.y;
/*auto const viewRect = R_Console3DViewRect(displayPlayer);
viewpx = 0;
viewpy = 0;
viewpw = int(viewRect.width());