/
r_things.c
1650 lines (1394 loc) · 49.7 KB
/
r_things.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
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2008 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2006-2008 Daniel Swanson <danij@dengine.net>
*\author Copyright © 2006 Jamie Jones <jamie_jones_au@yahoo.com.au>
*\author Copyright © 1993-1996 by id Software, Inc.
*
* 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
*/
/**
* r_things.c: Object Management and Refresh
*/
/**
* Sprite rotation 0 is facing the viewer, rotation 1 is one angle
* turn CLOCKWISE around the axis. This is not the same as the angle,
* which increases counter clockwise (protractor).
*/
// HEADER FILES ------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include "de_base.h"
#include "de_console.h"
#include "de_network.h"
#include "de_refresh.h"
#include "de_play.h"
#include "de_render.h"
#include "de_graphics.h"
#include "de_misc.h"
#include "def_main.h"
// MACROS ------------------------------------------------------------------
#define MAX_FRAMES (128)
#define MAX_OBJECT_RADIUS (128)
// TYPES -------------------------------------------------------------------
typedef struct vlightnode_s {
struct vlightnode_s* next, *nextUsed;
vlight_t vlight;
} vlightnode_t;
typedef struct vlightlist_s {
vlightnode_t* head;
boolean sortByDist;
} vlightlist_t;
typedef struct {
vec3_t pos;
boolean haveList;
uint listIdx;
} vlightiterparams_t;
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
float weaponOffsetScale = 0.3183f; // 1/Pi
int weaponOffsetScaleY = 1000;
float weaponFOVShift = 45;
float modelSpinSpeed = 1;
int alwaysAlign = 0;
int noSpriteZWrite = false;
float pspOffset[2] = {0, 0};
// useSRVO: 1 = models only, 2 = sprites + models
int useSRVO = 2, useSRVOAngle = true;
int psp3d;
// Variables used to look up and range check sprites patches.
spritedef_t* sprites = 0;
int numSprites;
vissprite_t visSprites[MAXVISSPRITES], *visSpriteP;
vispsprite_t visPSprites[DDMAXPSPRITES];
int maxModelDistance = 1500;
int levelFullBright = false;
vissprite_t visSprSortedHead;
// PRIVATE DATA DEFINITIONS ------------------------------------------------
// vlight nodes.
static vlightnode_t* vLightFirst, *vLightCursor;
// vlight link lists.
static uint numVLightLinkLists = 0, vLightLinkListCursor = 0;
static vlightlist_t* vLightLinkLists;
static spriteframe_t sprTemp[MAX_FRAMES];
static int maxFrame;
static char* spriteName;
static vissprite_t overflowVisSprite;
static const float worldLight[3] = {-.400891f, -.200445f, .601336f};
// CODE --------------------------------------------------------------------
/**
* Initialize the vlight system in preparation for rendering view(s) of the
* game world. Called by R_InitLevel().
*/
void VL_InitForMap(void)
{
vLightLinkLists = NULL;
numVLightLinkLists = 0, vLightLinkListCursor = 0;
}
/**
* Moves all used vlight nodes to the list of unused nodes, so they can be
* reused.
*/
void VL_InitForNewFrame(void)
{
// Start reusing nodes from the first one in the list.
vLightCursor = vLightFirst;
// Clear the mobj vlight link lists.
vLightLinkListCursor = 0;
if(numVLightLinkLists)
memset(vLightLinkLists, 0, numVLightLinkLists * sizeof(vlightlist_t));
}
/**
* Create a new vlight list.
*
* @param sortByDist Lights in this list will be automatically sorted by
* their approximate distance from the point being lit.
* @return Identifier for the new list.
*/
static uint newVLightList(boolean sortByDist)
{
vlightlist_t* list;
// Ran out of vlight link lists?
if(++vLightLinkListCursor >= numVLightLinkLists)
{
uint newNum = numVLightLinkLists * 2;
if(!newNum)
newNum = 2;
vLightLinkLists =
Z_Realloc(vLightLinkLists, newNum * sizeof(vlightlist_t),
PU_MAP);
numVLightLinkLists = newNum;
}
list = &vLightLinkLists[vLightLinkListCursor-1];
list->head = NULL;
list->sortByDist = sortByDist;
return vLightLinkListCursor - 1;
}
static vlightnode_t* newVLightNode(void)
{
vlightnode_t* node;
// Have we run out of nodes?
if(vLightCursor == NULL)
{
node = Z_Malloc(sizeof(vlightnode_t), PU_STATIC, NULL);
// Link the new node to the list.
node->nextUsed = vLightFirst;
vLightFirst = node;
}
else
{
node = vLightCursor;
vLightCursor = vLightCursor->nextUsed;
}
node->next = NULL;
return node;
}
/**
* @return Ptr to a new vlight node. If the list of unused
* nodes is empty, a new node is created.
*/
static vlightnode_t* newVLight(void)
{
vlightnode_t* node = newVLightNode();
//vlight_t* vlight = &node->vlight;
//// \todo move vlight setup here.
return node;
}
static void linkVLightNodeToList(vlightnode_t* node, uint listIndex)
{
vlightlist_t* list = &vLightLinkLists[listIndex];
if(list->sortByDist && list->head)
{
vlightnode_t* last, *iter;
vlight_t* vlight;
last = iter = list->head;
do
{
vlight = &node->vlight;
// Is this closer than the one being added?
if(node->vlight.approxDist > vlight->approxDist)
{
last = iter;
iter = iter->next;
}
else
{ // Need to insert it here.
node->next = last->next;
last->next = node;
return;
}
} while(iter);
}
node->next = list->head;
list->head = node;
}
/**
* Local function for R_InitSprites.
*/
static void installSpriteLump(lumpnum_t lump, uint frame, uint rotation,
boolean flipped)
{
int r;
material_t* mat;
R_NewSpriteTexture(lump, &mat);
if(frame >= 30 || rotation > 8)
{
/*Con_Error("installSpriteLump: Bad frame characters in lump %i",
(int) lump);*/
return;
}
if((int) frame > maxFrame)
maxFrame = frame;
if(rotation == 0)
{
// The lump should be used for all rotations.
sprTemp[frame].rotate = false;
for(r = 0; r < 8; ++r)
{
sprTemp[frame].mats[r] = mat;
sprTemp[frame].flip[r] = (byte) flipped;
}
return;
}
sprTemp[frame].rotate = true;
rotation--; // Make 0 based.
sprTemp[frame].mats[rotation] = mat;
sprTemp[frame].flip[rotation] = (byte) flipped;
}
static void initSpriteDefs(void)
{
int i, l, intname, frame, rotation;
boolean inSpriteBlock;
float starttime = Sys_GetSeconds();
sprites = Z_Malloc(numSprites * sizeof(*sprites), PU_SPRITE, NULL);
/**
* Scan all the lump names for each of the names, noting the highest
* frame letter. Just compare 4 characters as ints.
*/
for(i = 0; i < numSprites; ++i)
{
spriteName = sprNames[i].name;
memset(sprTemp, -1, sizeof(sprTemp));
maxFrame = -1;
intname = *(int *) spriteName;
// Scan the lumps, filling in the frames for whatever is found.
inSpriteBlock = false;
for(l = 0; l < numLumps; ++l)
{
char* name = lumpInfo[l].name;
if(!strnicmp(name, "S_START", 7))
{
// We've arrived at *a* sprite block.
inSpriteBlock = true;
continue;
}
else if(!strnicmp(name, "S_END", 5))
{
// The sprite block ends.
inSpriteBlock = false;
continue;
}
if(!inSpriteBlock)
continue;
// Check that the first four letters match the sprite name.
if(*(int *) name == intname)
{
// Check that the name is valid.
if(!name[4] || !name[5] || (name[6] && !name[7]))
continue; // This is not a sprite frame.
// Indices 5 and 7 must be numbers (0-8).
if(name[5] < '0' || name[5] > '8')
continue;
if(name[7] && (name[7] < '0' || name[7] > '8'))
continue;
frame = name[4] - 'A';
rotation = name[5] - '0';
installSpriteLump(l, frame, rotation, false);
if(name[6])
{
frame = name[6] - 'A';
rotation = name[7] - '0';
installSpriteLump(l, frame, rotation, true);
}
}
}
/**
* Check the frames that were found for completeness.
*/
if(maxFrame == -1)
{
sprites[i].numFrames = 0;
//Con_Error ("R_InitSprites: No lumps found for sprite %s", namelist[i]);
}
maxFrame++;
for(frame = 0; frame < maxFrame; frame++)
{
switch((int) sprTemp[frame].rotate)
{
case -1: // No rotations were found for that frame at all.
Con_Error("R_InitSprites: No patches found for %s frame %c.",
spriteName, frame + 'A');
break;
case 0: // Only the first rotation is needed.
break;
case 1: // Must have all 8 frames.
for(rotation = 0; rotation < 8; ++rotation)
{
if(!sprTemp[frame].mats[rotation])
Con_Error("R_InitSprites: Sprite %s frame %c is "
"missing rotations.", spriteName,
frame + 'A');
}
break;
default:
Con_Error("R_InitSpriteDefs: Invalid value, "
"sprTemp[frame].rotate = %i.",
(int) sprTemp[frame].rotate);
break;
}
}
// Allocate space for the frames present and copy sprTemp to it.
sprites[i].numFrames = maxFrame;
sprites[i].spriteFrames =
Z_Malloc(maxFrame * sizeof(spriteframe_t), PU_SPRITE, NULL);
memcpy(sprites[i].spriteFrames, sprTemp,
maxFrame * sizeof(spriteframe_t));
}
VERBOSE(Con_Message("R_InitSpriteDefs: Done in %.2f seconds.\n",
Sys_GetSeconds() - starttime));
}
/**
* Builds the sprite rotation matrixes to account for horizontally flipped
* sprites. Will report an error if the lumps are inconsistant.
*
* Sprite lump names are 4 characters for the actor, a letter for the frame,
* and a number for the rotation, A sprite that is flippable will have an
* additional letter/number appended. The rotation character can be 0 to
* signify no rotations.
*/
void R_InitSpriteDefs(void)
{
numSpriteTextures = 0;
numSprites = countSprNames.num;
// Check that some sprites are defined.
if(!numSprites)
return;
initSpriteDefs();
}
material_t* R_GetMaterialForSprite(int sprite, int frame)
{
spritedef_t* sprDef;
if((unsigned) sprite >= (unsigned) numSprites)
return NULL;
sprDef = &sprites[sprite];
if(frame >= sprDef->numFrames)
return NULL;
return sprDef->spriteFrames[frame].mats[0];
}
void R_GetSpriteInfo(int sprite, int frame, spriteinfo_t* info)
{
spritedef_t* sprDef;
spriteframe_t* sprFrame;
spritetex_t* sprTex;
material_t* mat;
#ifdef RANGECHECK
if((unsigned) sprite >= (unsigned) numSprites)
Con_Error("R_GetSpriteInfo: invalid sprite number %i.\n", sprite);
#endif
sprDef = &sprites[sprite];
if(frame >= sprDef->numFrames)
{
// We have no information to return.
memset(info, 0, sizeof(*info));
return;
}
sprFrame = &sprDef->spriteFrames[frame];
mat = sprFrame->mats[0];
sprTex = spriteTextures[mat->tex->ofTypeID];
info->numFrames = sprDef->numFrames;
info->materialNum = R_GetMaterialNum(mat);
info->realLump = sprTex->lump;
info->flip = sprFrame->flip[0];
info->offset = sprTex->offX;
info->topOffset = sprTex->offY;
info->width = mat->width;
info->height = mat->height;
}
void R_GetPatchInfo(lumpnum_t lump, patchinfo_t* info)
{
lumppatch_t* patch =
(lumppatch_t*) W_CacheLumpNum(lump, PU_CACHE);
memset(info, 0, sizeof(*info));
info->lump = info->realLump = lump;
info->width = SHORT(patch->width);
info->height = SHORT(patch->height);
info->topOffset = SHORT(patch->topOffset);
info->offset = SHORT(patch->leftOffset);
}
/**
* @return Radius of the mobj as it would visually appear to be.
*/
float R_VisualRadius(mobj_t* mo)
{
modeldef_t* mf, *nextmf;
material_t* mat;
// If models are being used, use the model's radius.
if(useModels)
{
R_CheckModelFor(mo, &mf, &nextmf);
if(mf)
{
// Returns the model's radius!
return mf->visualRadius;
}
}
// Use the sprite frame's width.
if((mat = R_GetMaterialForSprite(mo->sprite, mo->frame)))
return mat->width / 2;
return mo->radius; // Fallback.
}
void R_InitSprites(void)
{
// Free all previous sprite memory.
Z_FreeTags(PU_SPRITE, PU_SPRITE);
R_InitSpriteDefs();
}
/**
* Called at frame start.
*/
void R_ClearSprites(void)
{
visSpriteP = visSprites;
}
vissprite_t* R_NewVisSprite(void)
{
vissprite_t* spr;
if(visSpriteP == &visSprites[MAXVISSPRITES])
{
spr = &overflowVisSprite;
}
else
{
visSpriteP++;
spr = visSpriteP - 1;
}
memset(spr, 0, sizeof(*spr));
return spr;
}
/**
* If 3D models are found for psprites, here we will create vissprites for
* them.
*/
void R_ProjectPlayerSprites(void)
{
int i;
float inter;
modeldef_t* mf, *nextmf;
ddpsprite_t* psp;
boolean isFullBright = (levelFullBright != 0);
boolean isModel;
ddplayer_t* ddpl = &viewPlayer->shared;
psp3d = false;
// Cameramen have no psprites.
if((ddpl->flags & DDPF_CAMERA) || (ddpl->flags & DDPF_CHASECAM))
return;
// Determine if we should be drawing all the psprites full bright?
if(!isFullBright)
{
for(i = 0, psp = ddpl->pSprites; i < DDMAXPSPRITES; ++i, psp++)
{
if(!psp->statePtr)
continue;
// If one of the psprites is fullbright, both are.
if(psp->statePtr->flags & STF_FULLBRIGHT)
isFullBright = true;
}
}
for(i = 0, psp = ddpl->pSprites; i < DDMAXPSPRITES; ++i, psp++)
{
vispsprite_t* spr = &visPSprites[i];
spr->type = VPSPR_SPRITE;
spr->psp = psp;
if(!psp->statePtr)
continue;
// First, determine whether this is a model or a sprite.
isModel = false;
if(useModels)
{ // Is there a model for this frame?
mobj_t dummy;
// Setup a dummy for the call to R_CheckModelFor.
dummy.state = psp->statePtr;
dummy.tics = psp->tics;
inter = R_CheckModelFor(&dummy, &mf, &nextmf);
if(mf)
isModel = true;
}
if(isModel)
{ // Yes, draw a 3D model (in Rend_Draw3DPlayerSprites).
// There are 3D psprites.
psp3d = true;
spr->type = VPSPR_MODEL;
spr->data.model.subsector = ddpl->mo->subsector;
spr->data.model.flags = 0;
// 32 is the raised weapon height.
spr->data.model.gzt = viewZ;
spr->data.model.secFloor = ddpl->mo->subsector->sector->SP_floorvisheight;
spr->data.model.secCeil = ddpl->mo->subsector->sector->SP_ceilvisheight;
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;
spr->center[VX] = viewX;
spr->center[VY] = viewY;
spr->center[VZ] = viewZ;
// Offsets to rotation angles.
spr->data.model.yawAngleOffset = psp->pos[VX] * weaponOffsetScale - 90;
spr->data.model.pitchAngleOffset =
(32 - psp->pos[VY]) * weaponOffsetScale * weaponOffsetScaleY / 1000.0f;
// Is the FOV shift in effect?
if(weaponFOVShift > 0 && fieldOfView > 90)
spr->data.model.yawAngleOffset -= weaponFOVShift * (fieldOfView - 90) / 90;
// Real rotation angles.
spr->data.model.yaw =
viewAngle / (float) ANGLE_MAX *-360 + spr->data.model.yawAngleOffset + 90;
spr->data.model.pitch = viewPitch * 85 / 110 + spr->data.model.yawAngleOffset;
memset(spr->data.model.visOff, 0, sizeof(spr->data.model.visOff));
spr->data.model.alpha = psp->alpha;
spr->data.model.stateFullBright = false;
}
else
{ // No, draw a 2D sprite (in Rend_DrawPlayerSprites).
spr->type = VPSPR_SPRITE;
// Adjust the center slightly so an angle can be calculated.
spr->center[VX] = viewX;
spr->center[VY] = viewY;
spr->center[VZ] = viewZ;
spr->data.sprite.subsector = ddpl->mo->subsector;
spr->data.sprite.alpha = psp->alpha;
spr->data.sprite.isFullBright = isFullBright;
}
}
}
float R_MovementYaw(float momx, float momy)
{
// Multiply by 100 to get some artificial accuracy in bamsAtan2.
return BANG2DEG(bamsAtan2(-100 * momy, 100 * momx));
}
float R_MovementPitch(float momx, float momy, float momz)
{
return
BANG2DEG(bamsAtan2
(100 * momz, 100 * P_AccurateDistance(momx, momy)));
}
typedef struct {
vissprite_t* vis;
const mobj_t* mo;
boolean floorAdjust;
} vismobjzparams_t;
/**
* Determine the correct Z coordinate for the mobj. The visible Z coordinate
* may be slightly different than the actual Z coordinate due to smoothed
* plane movement.
*/
boolean RIT_VisMobjZ(sector_t* sector, void* data)
{
vismobjzparams_t* params;
assert(sector != NULL);
assert(data != NULL);
params = (vismobjzparams_t*) data;
if(params->floorAdjust && params->mo->pos[VZ] == sector->SP_floorheight)
{
params->vis->center[VZ] = sector->SP_floorvisheight;
}
if(params->mo->pos[VZ] + params->mo->height == sector->SP_ceilheight)
{
params->vis->center[VZ] = sector->SP_ceilvisheight - params->mo->height;
}
return true;
}
static void setupSpriteParamsForVisSprite(rendspriteparams_t *params,
float x, float y, float z, float distance, float visOffX, float visOffY, float visOffZ,
float secFloor, float secCeil,
float floorClip, float top,
material_t* mat, boolean matFlipS, boolean matFlipT, blendmode_t blendMode,
float ambientColorR, float ambientColorG, float ambientColorB, float alpha,
uint vLightListIdx,
int transMap, int transClass, subsector_t* ssec,
boolean floorAdjust, boolean fitTop, boolean fitBottom,
boolean viewAligned,
boolean brightShadow, boolean shadow, boolean altShadow,
boolean fullBright)
{
spritetex_t* sprTex = spriteTextures[mat->tex->ofTypeID];
if(!params)
return; // Wha?
params->width = mat->width;
params->height = mat->height;
params->center[VX] = x;
params->center[VY] = y;
params->center[VZ] = z;
params->srvo[VX] = visOffX;
params->srvo[VY] = visOffY;
params->srvo[VZ] = visOffZ;
params->distance = distance;
params->viewOffX = (float) sprTex->offX - mat->width / 2.0f;
params->subsector = ssec;
params->viewAligned = viewAligned;
params->noZWrite = noSpriteZWrite;
params->mat = mat;
params->tMap = transMap;
params->tClass = transClass;
params->matOffset[0] = sprTex->texCoord[0];
params->matOffset[1] = sprTex->texCoord[1];
params->matFlip[0] = matFlipS;
params->matFlip[1] = matFlipT;
params->blendMode = blendMode;
params->ambientColor[CR] = ambientColorR;
params->ambientColor[CG] = ambientColorG;
params->ambientColor[CB] = ambientColorB;
params->ambientColor[CA] = alpha;
params->vLightListIdx = vLightListIdx;
}
void setupModelParamsForVisSprite(rendmodelparams_t *params,
float x, float y, float z, float distance,
float visOffX, float visOffY, float visOffZ, float gzt, float yaw, float yawAngleOffset, float pitch, float pitchAngleOffset,
struct modeldef_s* mf, struct modeldef_s* nextMF, float inter,
float ambientColorR, float ambientColorG, float ambientColorB, float alpha,
uint vLightListIdx,
int id, int selector, subsector_t* ssec, int mobjDDFlags,
boolean viewAlign, boolean fullBright,
boolean alwaysInterpolate)
{
if(!params)
return; // Hmm...
params->mf = mf;
params->nextMF = nextMF;
params->inter = inter;
params->alwaysInterpolate = alwaysInterpolate;
params->id = id;
params->selector = selector;
params->flags = mobjDDFlags;
params->center[VX] = x;
params->center[VY] = y;
params->center[VZ] = z;
params->srvo[VX] = visOffX;
params->srvo[VY] = visOffY;
params->srvo[VZ] = visOffZ;
params->gzt = gzt;
params->distance = distance;
params->yaw = yaw;
params->extraYawAngle = 0;
params->yawAngleOffset = yawAngleOffset;
params->pitch = pitch;
params->extraPitchAngle = 0;
params->pitchAngleOffset = pitchAngleOffset;
params->extraScale = 0;
params->viewAlign = viewAlign;
params->mirror = 0;
params->shineYawOffset = 0;
params->shinePitchOffset = 0;
params->shineTranslateWithViewerPos = false;
params->shinepspriteCoordSpace = false;
params->ambientColor[CR] = ambientColorR;
params->ambientColor[CG] = ambientColorG;
params->ambientColor[CB] = ambientColorB;
params->ambientColor[CA] = alpha;
params->vLightListIdx = vLightListIdx;
}
void getLightingParams(float x, float y, float z, subsector_t* ssec,
float distance, boolean fullBright,
float ambientColor[3], uint* vLightListIdx)
{
if(fullBright)
{
ambientColor[CR] = ambientColor[CG] = ambientColor[CB] = 1;
*vLightListIdx = 0;
}
else
{
collectaffectinglights_params_t lparams;
if(useBias)
{
vec3_t point;
// Evaluate the position in the light grid.
V3_Set(point, x, y, z);
LG_Evaluate(point, ambientColor);
}
else
{
float lightLevel = ssec->sector->lightLevel;
const float* secColor =
R_GetSectorLightColor(ssec->sector);
/* if(spr->type == VSPR_DECORATION)
{
// Wall decorations receive an additional light delta.
lightLevel += R_WallAngleLightLevelDelta(linedef, side);
} */
// Apply distance attenuation.
lightLevel = R_DistAttenuateLightLevel(distance, lightLevel);
// Add extra light.
lightLevel += R_ExtraLightDelta();
Rend_ApplyLightAdaptation(&lightLevel);
// Determine the final ambientColor in affect.
ambientColor[CR] = lightLevel * secColor[CR];
ambientColor[CG] = lightLevel * secColor[CG];
ambientColor[CB] = lightLevel * secColor[CB];
}
Rend_ApplyTorchLight(ambientColor, distance);
lparams.starkLight = false;
lparams.center[VX] = x;
lparams.center[VY] = y;
lparams.center[VZ] = z;
lparams.subsector = ssec;
lparams.ambientColor = ambientColor;
*vLightListIdx = R_CollectAffectingLights(&lparams);
}
}
/**
* Generates a vissprite for a mobj if it might be visible.
*/
void R_ProjectSprite(mobj_t* mo)
{
sector_t* sect = mo->subsector->sector;
float thangle = 0, alpha, floorClip, secFloor, secCeil;
float pos[2], yaw, pitch;
vec3_t visOff;
spritedef_t* sprDef;
spriteframe_t* sprFrame = NULL;
int i, transClass;
unsigned rot;
boolean matFlipS, matFlipT;
vissprite_t* vis;
angle_t ang;
boolean align, fullBright, viewAlign, floorAdjust;
modeldef_t* mf = NULL, *nextmf = NULL;
float interp = 0, distance, gzt;
material_t* mat;
spritetex_t* sprTex;
vismobjzparams_t params;
visspritetype_t visType = VSPR_SPRITE;
float ambientColor[3];
uint vLightListIdx = 0;
if(mo->ddFlags & DDMF_DONTDRAW || mo->translucency == 0xff ||
mo->state == NULL || mo->state == states)
{
// Never make a vissprite when DDMF_DONTDRAW is set or when
// the mo is fully transparent, or when the mo hasn't got
// a valid state.
return;
}
// Transform the origin point.
pos[VX] = mo->pos[VX] - viewX;
pos[VY] = mo->pos[VY] - viewY;
// Decide which patch to use for sprite relative to player.
#ifdef RANGECHECK
if((unsigned) mo->sprite >= (unsigned) numSprites)
{
Con_Error("R_ProjectSprite: invalid sprite number %i\n",
mo->sprite);
}
#endif
sprDef = &sprites[mo->sprite];
if(mo->frame >= sprDef->numFrames)
{
// The frame is not defined, we can't display this object.
return;
}
sprFrame = &sprDef->spriteFrames[mo->frame];
// Determine distance to object.
distance = Rend_PointDist2D(mo->pos);
// Check for a 3D model.
if(useModels)
{
interp = R_CheckModelFor(mo, &mf, &nextmf);
if(mf && !(mf->flags & MFF_NO_DISTANCE_CHECK) && maxModelDistance &&
distance > maxModelDistance)
{
// Don't use a 3D model.
mf = nextmf = NULL;
interp = -1;
}
}
if(sprFrame->rotate && !mf)
{ // Choose a different rotation based on player view.
ang = R_PointToAngle(mo->pos[VX], mo->pos[VY]);
rot = (ang - mo->angle + (unsigned) (ANG45 / 2) * 9) >> 29;
mat = sprFrame->mats[rot];
matFlipS = (boolean) sprFrame->flip[rot];
}
else
{ // Use single rotation for all views.
mat = sprFrame->mats[0];
matFlipS = (boolean) sprFrame->flip[0];
}
matFlipT = false;
sprTex = spriteTextures[mat->tex->ofTypeID];
// Align to the view plane?
if(mo->ddFlags & DDMF_VIEWALIGN)
align = true;
else
align = false;
if(alwaysAlign == 1)
align = true;
// Perform visibility checking.
if(!mf)
{ // Its a sprite.
if(!align && alwaysAlign != 2 && alwaysAlign != 3)
{
float center[2], v1[2], v2[2];
float width, offset;
visType = VSPR_SPRITE;
width = (float) mat->width;
offset = (float) sprTex->offX - (width / 2);
// Project a line segment relative to the view in 2D, then check
// if not entirely clipped away in the 360 degree angle clipper.
center[VX] = mo->pos[VX];
center[VY] = mo->pos[VY];
M_ProjectViewRelativeLine2D(center, (align || alwaysAlign == 3),
width, offset, v1, v2);
// Check for visibility.
if(!C_CheckViewRelSeg(v1[VX], v1[VY], v2[VX], v2[VY]))
return; // Isn't visible.