/
r_main.c
1334 lines (1094 loc) · 37.1 KB
/
r_main.c
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 r_main.c
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2006-2012 Daniel Swanson <danij@dengine.net>
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/**
* Refresh Subsystem.
*
* The refresh daemon has the highest-level rendering code.
* The view window is handled by refresh. The more specialized
* rendering code in rend_*.c does things inside the view window.
*/
// HEADER FILES ------------------------------------------------------------
#include <math.h>
#include <assert.h>
#include "de_base.h"
#include "de_console.h"
#include "de_system.h"
#include "de_filesys.h"
#include "de_network.h"
#include "de_render.h"
#include "de_refresh.h"
#include "de_graphics.h"
#include "de_audio.h"
#include "de_misc.h"
#include "de_ui.h"
#include "font.h"
// MACROS ------------------------------------------------------------------
#ifdef LIBDENG_CAMERA_MOVEMENT_ANALYSIS
float devCameraMovementStartTime = 0; // sysTime
float devCameraMovementStartTimeRealSecs = 0;
#endif
BEGIN_PROF_TIMERS()
PROF_MOBJ_INIT_ADD
END_PROF_TIMERS()
// TYPES -------------------------------------------------------------------
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
D_CMD(ViewGrid);
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
extern byte freezeRLs;
extern boolean firstFrameAfterLoad;
// PUBLIC DATA DEFINITIONS -------------------------------------------------
int validCount = 1; // Increment every time a check is made.
int frameCount; // Just for profiling purposes.
int rendInfoTris = 0;
int useVSync = 0;
// Precalculated math tables.
fixed_t* fineCosine = &finesine[FINEANGLES / 4];
int extraLight; // Bumped light from gun blasts.
float extraLightDelta;
float frameTimePos; // 0...1: fractional part for sharp game tics.
int loadInStartupMode = false;
fontid_t fontFixed, fontVariable[FONTSTYLE_COUNT];
// PRIVATE DATA DEFINITIONS ------------------------------------------------
static int rendCameraSmooth = true; // Smoothed by default.
static boolean resetNextViewer = true;
static viewdata_t viewDataOfConsole[DDMAXPLAYERS]; // Indexed by console number.
static byte showFrameTimePos = false;
static byte showViewAngleDeltas = false;
static byte showViewPosDeltas = false;
static int gridCols, gridRows;
static viewport_t viewportOfLocalPlayer[DDMAXPLAYERS], *currentViewport;
// CODE --------------------------------------------------------------------
/**
* Register console variables.
*/
void R_Register(void)
{
C_VAR_INT("con-show-during-setup", &loadInStartupMode, 0, 0, 1);
C_VAR_INT("rend-camera-smooth", &rendCameraSmooth, CVF_HIDE, 0, 1);
C_VAR_BYTE("rend-info-deltas-angles", &showViewAngleDeltas, 0, 0, 1);
C_VAR_BYTE("rend-info-deltas-pos", &showViewPosDeltas, 0, 0, 1);
C_VAR_BYTE("rend-info-frametime", &showFrameTimePos, 0, 0, 1);
C_VAR_BYTE("rend-info-rendpolys", &rendInfoRPolys, CVF_NO_ARCHIVE, 0, 1);
C_VAR_INT("rend-info-tris", &rendInfoTris, 0, 0, 1);
// C_VAR_INT("rend-vsync", &useVSync, 0, 0, 1);
C_CMD("viewgrid", "ii", ViewGrid);
Materials_Register();
}
const char* R_ChooseFixedFont(void)
{
if(theWindow->geometry.size.width < 300)
return "console11";
if(theWindow->geometry.size.width > 768)
return "console18";
return "console14";
}
const char* R_ChooseVariableFont(fontstyle_t style, int resX, int resY)
{
const int SMALL_LIMIT = 500;
const int MED_LIMIT = 800;
switch(style)
{
default:
return (resY < SMALL_LIMIT ? "normal12" :
resY < MED_LIMIT ? "normal18" :
"normal24");
case FS_LIGHT:
return (resY < SMALL_LIMIT ? "normallight12" :
resY < MED_LIMIT ? "normallight18" :
"normallight24");
case FS_BOLD:
return (resY < SMALL_LIMIT ? "normalbold12" :
resY < MED_LIMIT ? "normalbold18" :
"normalbold24");
}
}
static fontid_t loadSystemFont(const char* name)
{
ddstring_t resourcePath;
font_t* font;
Uri* uri;
assert(name && name[0]);
// Compose the resource name.
uri = Uri_NewWithPath2(FN_SYSTEM_NAME":", RC_NULL);
Uri_SetPath(uri, name);
// Compose the resource data path.
// \todo This is currently rather awkward due
Str_Init(&resourcePath);
Str_Appendf(&resourcePath, "}data/"FONTS_RESOURCE_NAMESPACE_NAME"/%s.dfn", name);
#if defined(UNIX) && !defined(MACOSX)
// Case-sensitive file system.
/// @todo Unkludge this: handle in a more generic manner.
strlwr(resourcePath.str);
#endif
F_ExpandBasePath(&resourcePath, &resourcePath);
font = R_CreateFontFromFile(uri, Str_Text(&resourcePath));
Str_Free(&resourcePath);
Uri_Delete(uri);
if(!font)
{
Con_Error("loadSystemFont: Failed loading font \"%s\".", name);
exit(1); // Unreachable.
}
return Fonts_Id(font);
}
void R_LoadSystemFonts(void)
{
if(isDedicated) return;
fontFixed = loadSystemFont(R_ChooseFixedFont());
fontVariable[FS_NORMAL] = loadSystemFont(R_ChooseVariableFont(FS_NORMAL, theWindow->geometry.size.width, theWindow->geometry.size.height));
fontVariable[FS_BOLD] = loadSystemFont(R_ChooseVariableFont(FS_BOLD, theWindow->geometry.size.width, theWindow->geometry.size.height));
fontVariable[FS_LIGHT] = loadSystemFont(R_ChooseVariableFont(FS_LIGHT, theWindow->geometry.size.width, theWindow->geometry.size.height));
Con_SetFont(fontFixed);
}
boolean R_IsSkySurface(const surface_t* suf)
{
return (suf && suf->material && Material_IsSkyMasked(suf->material));
}
/**
* Update the view origin position for player @a consoleNum.
*
* @param consoleNum Console number.
*
* \note Part of the Doomsday public API.
*/
void R_SetViewOrigin(int consoleNum, float const origin[3])
{
if(consoleNum < 0 || consoleNum >= DDMAXPLAYERS) return;
V3_Copy(viewDataOfConsole[consoleNum].latest.pos, origin);
}
/**
* Update the view yaw angle for player @a consoleNum.
*
* @param consoleNum Console number.
*
* \note Part of the Doomsday public API.
*/
void R_SetViewAngle(int consoleNum, angle_t angle)
{
if(consoleNum < 0 || consoleNum >= DDMAXPLAYERS) return;
viewDataOfConsole[consoleNum].latest.angle = angle;
}
/**
* Update the view pitch angle for player @a consoleNum.
*
* @param consoleNum Console number.
*
* \note Part of the Doomsday public API.
*/
void R_SetViewPitch(int consoleNum, float pitch)
{
if(consoleNum < 0 || consoleNum >= DDMAXPLAYERS) return;
viewDataOfConsole[consoleNum].latest.pitch = pitch;
}
void R_SetupDefaultViewWindow(int consoleNum)
{
viewdata_t* vd = &viewDataOfConsole[consoleNum];
if(consoleNum < 0 || consoleNum >= DDMAXPLAYERS) return;
vd->window.origin.x = vd->windowOld.origin.x = vd->windowTarget.origin.x = 0;
vd->window.origin.y = vd->windowOld.origin.y = vd->windowTarget.origin.y = 0;
vd->window.size.width = vd->windowOld.size.width = vd->windowTarget.size.width = theWindow->geometry.size.width;
vd->window.size.height = vd->windowOld.size.height = vd->windowTarget.size.height = theWindow->geometry.size.height;
vd->windowInter = 1;
}
void R_ViewWindowTicker(int consoleNum, timespan_t ticLength)
{
#define LERP(start, end, pos) (end * pos + start * (1 - pos))
viewdata_t* vd = &viewDataOfConsole[consoleNum];
if(consoleNum < 0 || consoleNum >= DDMAXPLAYERS) return;
vd->windowInter += (float)(.4 * ticLength * TICRATE);
if(vd->windowInter >= 1)
{
memcpy(&vd->window, &vd->windowTarget, sizeof(vd->window));
}
else
{
const float x = LERP(vd->windowOld.origin.x, vd->windowTarget.origin.x, vd->windowInter);
const float y = LERP(vd->windowOld.origin.y, vd->windowTarget.origin.y, vd->windowInter);
const float w = LERP(vd->windowOld.size.width, vd->windowTarget.size.width, vd->windowInter);
const float h = LERP(vd->windowOld.size.height, vd->windowTarget.size.height, vd->windowInter);
vd->window.origin.x = ROUND(x);
vd->window.origin.y = ROUND(y);
vd->window.size.width = ROUND(w);
vd->window.size.height = ROUND(h);
}
#undef LERP
}
/// \note Part of the Doomsday public API.
int R_ViewWindowGeometry(int player, RectRaw* geometry)
{
const viewdata_t* vd;
if(!geometry) return false;
if(player < 0 || player >= DDMAXPLAYERS) return false;
vd = &viewDataOfConsole[player];
memcpy(geometry, &vd->window, sizeof *geometry);
return true;
}
/// \note Part of the Doomsday public API.
int R_ViewWindowOrigin(int player, Point2Raw* origin)
{
const viewdata_t* vd;
if(!origin) return false;
if(player < 0 || player >= DDMAXPLAYERS) return false;
vd = &viewDataOfConsole[player];
memcpy(origin, &vd->window.origin, sizeof *origin);
return true;
}
/// \note Part of the Doomsday public API.
int R_ViewWindowSize(int player, Size2Raw* size)
{
const viewdata_t* vd;
if(!size) return false;
if(player < 0 || player >= DDMAXPLAYERS) return false;
vd = &viewDataOfConsole[player];
memcpy(size, &vd->window.size, sizeof *size);
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.
*
* \note Part of the Doomsday public API.
*/
void R_SetViewWindowGeometry(int player, const RectRaw* geometry, boolean interpolate)
{
int p = P_ConsoleToLocal(player);
if(p < 0) return;
{
const viewport_t* vp = &viewportOfLocalPlayer[p];
viewdata_t* vd = &viewDataOfConsole[player];
RectRaw newGeom;
// Clamp to valid range.
newGeom.origin.x = MINMAX_OF(0, geometry->origin.x, vp->geometry.size.width);
newGeom.origin.y = MINMAX_OF(0, geometry->origin.y, vp->geometry.size.height);
newGeom.size.width = abs(geometry->size.width);
newGeom.size.height = abs(geometry->size.height);
if(newGeom.origin.x + newGeom.size.width > vp->geometry.size.width)
newGeom.size.width = vp->geometry.size.width - newGeom.origin.x;
if(newGeom.origin.y + newGeom.size.height > vp->geometry.size.height)
newGeom.size.height = vp->geometry.size.height - newGeom.origin.y;
// Already at this target?
if(vd->window.origin.x == newGeom.origin.x &&
vd->window.origin.y == newGeom.origin.y &&
vd->window.size.width == newGeom.size.width &&
vd->window.size.height == newGeom.size.height)
return;
// Record the new target.
memcpy(&vd->windowTarget, &newGeom, sizeof vd->windowTarget);
// Restart or advance the interpolation timer?
// If dimensions have not yet been set - do not interpolate.
if(interpolate && !(vd->window.size.width == 0 && vd->window.size.height == 0))
{
vd->windowInter = 0;
memcpy(&vd->windowOld, &vd->window, sizeof(vd->windowOld));
}
else
{
vd->windowInter = 1; // Update on next frame.
memcpy(&vd->windowOld, &vd->windowTarget, sizeof(vd->windowOld));
}
}
}
/// \note Part of the Doomsday public API.
int R_ViewPortGeometry(int player, RectRaw* geometry)
{
viewport_t* vp;
int p;
if(!geometry) return false;
p = P_ConsoleToLocal(player);
if(p == -1) return false;
vp = &viewportOfLocalPlayer[p];
memcpy(geometry, &vp->geometry, sizeof *geometry);
return true;
}
/// \note Part of the Doomsday public API.
int R_ViewPortOrigin(int player, Point2Raw* origin)
{
viewport_t* vp;
int p;
if(!origin) return false;
p = P_ConsoleToLocal(player);
if(p == -1) return false;
vp = &viewportOfLocalPlayer[p];
memcpy(origin, &vp->geometry.origin, sizeof *origin);
return true;
}
/// \note Part of the Doomsday public API.
int R_ViewPortSize(int player, Size2Raw* size)
{
viewport_t* vp;
int p;
if(!size) return false;
p = P_ConsoleToLocal(player);
if(p == -1) return false;
vp = &viewportOfLocalPlayer[p];
memcpy(size, &vp->geometry.size, sizeof *size);
return true;
}
/// \note Part of the Doomsday public API.
void R_SetViewPortPlayer(int consoleNum, int viewPlayer)
{
int 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, int col, int row)
{
assert(port);
{
RectRaw* rect = &port->geometry;
const int x = col * theWindow->geometry.size.width / gridCols;
const int y = row * theWindow->geometry.size.height / gridRows;
const int width = (col+1) * theWindow->geometry.size.width / gridCols - x;
const int height = (row+1) * theWindow->geometry.size.height / gridRows - y;
ddhook_viewport_reshape_t p;
boolean doReshape = false;
if(rect->origin.x == x && rect->origin.y == y && rect->size.width == width && rect->size.height == height)
return;
if(port->console != -1 && Plug_CheckForHook(HOOK_VIEWPORT_RESHAPE))
{
memcpy(&p.oldGeometry, rect, sizeof(p.oldGeometry));
doReshape = true;
}
rect->origin.x = x;
rect->origin.y = y;
rect->size.width = width;
rect->size.height = height;
if(doReshape)
{
memcpy(&p.geometry, rect, sizeof(p.geometry));
DD_CallHooks(HOOK_VIEWPORT_RESHAPE, port->console, (void*)&p);
}
}
}
/**
* Attempt to set up a view grid and calculate the viewports. Set 'numCols' and
* 'numRows' to zero to just update the viewport coordinates.
*/
boolean R_SetViewGrid(int numCols, int numRows)
{
int x, y, p, console;
if(numCols > 0 && numRows > 0)
{
if(numCols * numRows > DDMAXPLAYERS)
return false;
if(numCols > DDMAXPLAYERS)
numCols = DDMAXPLAYERS;
if(numRows > DDMAXPLAYERS)
numRows = DDMAXPLAYERS;
gridCols = numCols;
gridRows = numRows;
}
p = 0;
for(y = 0; y < gridRows; ++y)
{
for(x = 0; x < gridCols; ++x)
{
// The console number is -1 if the viewport belongs to no one.
viewport_t* vp = viewportOfLocalPlayer + p;
console = P_LocalToConsole(p);
if(console != -1)
{
vp->console = clients[console].viewConsole;
}
else
{
vp->console = -1;
}
R_UpdateViewPortGeometry(vp, x, y);
++p;
}
}
return true;
}
/**
* One-time initialization of the refresh daemon. Called by DD_Main.
*/
void R_Init(void)
{
R_LoadSystemFonts();
R_InitColorPalettes();
R_InitTranslationTables();
R_InitRawTexs();
R_InitSvgs();
R_InitViewWindow();
R_SkyInit();
Rend_Init();
frameCount = 0;
}
/**
* Re-initialize almost everything.
*/
void R_Update(void)
{
// Reset file IDs so previously seen files can be processed again.
F_ResetFileIds();
// Re-read definitions.
Def_Read();
R_UpdateData();
R_InitSprites(); // Fully reinitialize sprites.
R_InitModels(); // Defs might've changed.
R_UpdateTranslationTables();
Def_PostInit();
P_UpdateParticleGens(); // Defs might've changed.
// Reset the archived map cache (the available maps may have changed).
DAM_Init();
{ uint i;
for(i = 0; i < DDMAXPLAYERS; ++i)
{
player_t* plr = &ddPlayers[i];
ddplayer_t* ddpl = &plr->shared;
// States have changed, the states are unknown.
ddpl->pSprites[0].statePtr = ddpl->pSprites[1].statePtr = NULL;
}}
// Update all world surfaces.
{ uint i;
for(i = 0; i < NUM_SECTORS; ++i)
{
sector_t* sec = §ors[i];
uint j;
for(j = 0; j < sec->planeCount; ++j)
Surface_Update(&sec->SP_planesurface(j));
}}
{ uint i;
for(i = 0; i < NUM_SIDEDEFS; ++i)
{
sidedef_t* side = &sideDefs[i];
Surface_Update(&side->SW_topsurface);
Surface_Update(&side->SW_middlesurface);
Surface_Update(&side->SW_bottomsurface);
}}
{ uint i;
for(i = 0; i < NUM_POLYOBJS; ++i)
{
polyobj_t* po = polyObjs[i];
linedef_t** lineIter;
for(lineIter = po->lines; *lineIter; lineIter++)
{
linedef_t* line = *lineIter;
sidedef_t* side = line->L_frontside;
Surface_Update(&side->SW_middlesurface);
}
}}
R_MapInitSurfaceLists();
// The rendering lists have persistent data that has changed during
// the re-initialization.
RL_DeleteLists();
// Update the secondary title and the game status.
Rend_ConsoleUpdateTitle();
#if _DEBUG
Z_CheckHeap();
#endif
}
/**
* Shutdown the refresh daemon.
*/
void R_Shutdown(void)
{
R_ClearAnimGroups();
R_ShutdownSprites();
R_ShutdownModels();
R_ShutdownSvgs();
R_ShutdownViewWindow();
Fonts_Shutdown();
Rend_Shutdown();
}
void R_Ticker(timespan_t time)
{
int i;
for(i = 0; i < DDMAXPLAYERS; ++i)
{
R_ViewWindowTicker(i, time);
}
}
void R_ResetViewer(void)
{
resetNextViewer = 1;
}
void R_InterpolateViewer(viewer_t* start, viewer_t* end, float pos, viewer_t* out)
{
float inv = 1 - pos;
int delta;
out->pos[VX] = inv * start->pos[VX] + pos * end->pos[VX];
out->pos[VY] = inv * start->pos[VY] + pos * end->pos[VY];
out->pos[VZ] = inv * start->pos[VZ] + pos * end->pos[VZ];
delta = (int)end->angle - (int)start->angle;
out->angle = start->angle + (int)(pos * delta);
out->pitch = inv * start->pitch + pos * end->pitch;
}
void R_CopyViewer(viewer_t* dst, const viewer_t* src)
{
V3_Copy(dst->pos, src->pos);
dst->angle = src->angle;
dst->pitch = src->pitch;
}
const viewdata_t* R_ViewData(int consoleNum)
{
assert(consoleNum >= 0 && consoleNum < DDMAXPLAYERS);
return &viewDataOfConsole[consoleNum];
}
/**
* 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)
{
#define MAXMOVE 32
if(fabs(dst->pos[VX] - src->pos[VX]) > MAXMOVE ||
fabs(dst->pos[VY] - src->pos[VY]) > MAXMOVE)
{
V3_Copy(src->pos, dst->pos);
}
if(abs((int) dst->angle - (int) src->angle) >= ANGLE_45)
{
#ifdef _DEBUG
Con_Message("R_CheckViewerLimits: Snap camera angle to %08x.\n", dst->angle);
#endif
src->angle = dst->angle;
}
#undef MAXMOVE
}
/**
* Retrieve the current sharp camera position.
*/
void R_GetSharpView(viewer_t* view, player_t* player)
{
viewdata_t* vd = &viewDataOfConsole[player - ddPlayers];
ddplayer_t* ddpl;
if(!player || !player->shared.mo) return;
ddpl = &player->shared;
R_CopyViewer(view, &vd->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.
*/
angle_t pitch = LOOKDIR2DEG(view->pitch) / 360 * ANGLE_MAX;
angle_t angle = view->angle;
float distance = 90;
angle = view->angle >> ANGLETOFINESHIFT;
pitch >>= ANGLETOFINESHIFT;
view->pos[VX] -= distance * FIX2FLT(fineCosine[angle]);
view->pos[VY] -= distance * FIX2FLT(finesine[angle]);
view->pos[VZ] -= distance * FIX2FLT(finesine[pitch]);
}
// Check that the viewZ doesn't go too high or low.
// Cameras are not restricted.
if(!(ddpl->flags & DDPF_CAMERA))
{
if(view->pos[VZ] > ddpl->mo->ceilingZ - 4)
{
view->pos[VZ] = ddpl->mo->ceilingZ - 4;
}
if(view->pos[VZ] < ddpl->mo->floorZ + 4)
{
view->pos[VZ] = ddpl->mo->floorZ + 4;
}
}
}
/**
* Update the sharp world data by rotating the stored values of plane
* heights and sharp camera positions.
*/
void R_NewSharpWorld(void)
{
int i;
if(resetNextViewer)
resetNextViewer = 2;
for(i = 0; i < DDMAXPLAYERS; ++i)
{
viewer_t sharpView;
viewdata_t* vd = &viewDataOfConsole[i];
player_t* plr = &ddPlayers[i];
if(/*(plr->shared.flags & DDPF_LOCAL) &&*/
(!plr->shared.inGame || !plr->shared.mo))
{
continue;
}
R_GetSharpView(&sharpView, 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.
memcpy(&vd->lastSharp[0], &vd->lastSharp[1], sizeof(viewer_t));
memcpy(&vd->lastSharp[1], &sharpView, sizeof(sharpView));
R_CheckViewerLimits(vd->lastSharp, &sharpView);
}
R_UpdateTrackedPlanes();
R_UpdateSurfaceScroll();
}
void R_CreateMobjLinks(void)
{
uint i;
sector_t* seciter;
#ifdef DD_PROFILE
static int p;
if(++p > 40)
{
p = 0;
PRINT_PROF( PROF_MOBJ_INIT_ADD );
}
#endif
BEGIN_PROF( PROF_MOBJ_INIT_ADD );
for(i = 0, seciter = sectors; i < NUM_SECTORS; seciter++, ++i)
{
mobj_t* iter;
for(iter = seciter->mobjList; iter; iter = iter->sNext)
{
R_ObjlinkCreate(iter, OT_MOBJ); // For spreading purposes.
}
}
END_PROF( PROF_MOBJ_INIT_ADD );
}
/**
* Prepare for rendering view(s) of the world.
*/
void R_BeginWorldFrame(void)
{
R_ClearSectorFlags();
R_InterpolateTrackedPlanes(resetNextViewer);
R_InterpolateSurfaceScroll(resetNextViewer);
if(!freezeRLs)
{
LG_Update();
SB_BeginFrame();
LO_BeginWorldFrame();
R_ClearObjlinksForFrame(); // Zeroes the links.
// Clear the objlinks.
R_InitForNewFrame();
// Generate surface decorations for the frame.
Rend_InitDecorationsForFrame();
// Spawn omnilights for decorations.
Rend_AddLuminousDecorations();
// Spawn omnilights for mobjs.
LO_AddLuminousMobjs();
// Create objlinks for mobjs.
R_CreateMobjLinks();
// Link all active particle generators into the world.
P_CreatePtcGenLinks();
// Link objs to all contacted surfaces.
R_LinkObjs();
}
}
/**
* Wrap up after drawing view(s) of the world.
*/
void R_EndWorldFrame(void)
{
if(!freezeRLs)
{
// Wrap up with Source, Bias lights.
SB_EndFrame();
}
}
void R_UpdateViewer(int consoleNum)
{
#define VIEWPOS_MAX_SMOOTHDISTANCE 172
viewdata_t* vd = viewDataOfConsole + consoleNum;
player_t* player = ddPlayers + consoleNum;
viewer_t sharpView, smoothView;
float yawRad, pitchRad;
uint an;
assert(consoleNum >= 0 && consoleNum < DDMAXPLAYERS);
if(!player->shared.inGame || !player->shared.mo) return;
R_GetSharpView(&sharpView, player);
if(resetNextViewer ||
V3_Distance(vd->current.pos, sharpView.pos) > VIEWPOS_MAX_SMOOTHDISTANCE)
{
// Keep reseting until a new sharp world has arrived.
if(resetNextViewer > 1)
resetNextViewer = 0;
// Just view from the sharp position.
R_CopyViewer(&vd->current, &sharpView);
memcpy(&vd->lastSharp[0], &sharpView, sizeof(sharpView));
memcpy(&vd->lastSharp[1], &sharpView, sizeof(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.
R_InterpolateViewer(vd->lastSharp, vd->lastSharp + 1, frameTimePos, &smoothView);
// 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;
R_CopyViewer(&vd->current, &smoothView);
// Monitor smoothness of yaw/pitch changes.
if(showViewAngleDeltas)
{
typedef struct oldangle_s {
double time;
float yaw, pitch;
} oldangle_t;
static oldangle_t oldangle[DDMAXPLAYERS];
oldangle_t* old = &oldangle[viewPlayer - ddPlayers];
float yaw = (double)smoothView.angle / ANGLE_MAX * 360;
Con_Message("(%i) F=%.3f dt=%-10.3f dx=%-10.3f dy=%-10.3f "
"Rdx=%-10.3f Rdy=%-10.3f\n",
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)
{
typedef struct oldpos_s {
double time;
float x, y, z;
} oldpos_t;
static oldpos_t oldpos[DDMAXPLAYERS];
oldpos_t* old = &oldpos[viewPlayer - ddPlayers];
Con_Message("(%i) F=%.3f dt=%-10.3f dx=%-10.3f dy=%-10.3f dz=%-10.3f dx/dt=%-10.3f dy/dt=%-10.3f\n",
//"Rdx=%-10.3f Rdy=%-10.3f\n",
SECONDS_TO_TICKS(gameTime),
frameTimePos,
sysTime - old->time,
smoothView.pos[0] - old->x,
smoothView.pos[1] - old->y,
smoothView.pos[2] - old->z,
(smoothView.pos[0] - old->x) / (sysTime - old->time),
(smoothView.pos[1] - old->y) / (sysTime - old->time));
old->x = smoothView.pos[VX];
old->y = smoothView.pos[VY];
old->z = smoothView.pos[VZ];
old->time = sysTime;
}
}
// Update viewer.
an = vd->current.angle >> ANGLETOFINESHIFT;
vd->viewSin = FIX2FLT(finesine[an]);
vd->viewCos = FIX2FLT(fineCosine[an]);
// Calculate the front, up and side unit vectors.
yawRad = ((vd->current.angle / (float) ANGLE_MAX) *2) * PI;
pitchRad = vd->current.pitch * 85 / 110.f / 180 * PI;
// The front vector.
vd->frontVec[VX] = cos(yawRad) * cos(pitchRad);
vd->frontVec[VZ] = sin(yawRad) * cos(pitchRad);
vd->frontVec[VY] = sin(pitchRad);
// The up vector.
vd->upVec[VX] = -cos(yawRad) * sin(pitchRad);
vd->upVec[VZ] = -sin(yawRad) * sin(pitchRad);
vd->upVec[VY] = cos(pitchRad);
// The side vector is the cross product of the front and up vectors.
M_CrossProduct(vd->frontVec, vd->upVec, vd->sideVec);
#undef VIEWPOS_MAX_SMOOTHDISTANCE
}
/**
* Prepare rendering the view of the given player.
*/