/
hw_sprites.cpp
1301 lines (1138 loc) · 38.3 KB
/
hw_sprites.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
//
//---------------------------------------------------------------------------
//
// Copyright(C) 2002-2016 Christoph Oelckers
// All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see http://www.gnu.org/licenses/
//
//--------------------------------------------------------------------------
//
/*
** gl_sprite.cpp
** Sprite/Particle rendering
**
*/
#include "p_local.h"
#include "p_effect.h"
#include "g_level.h"
#include "doomstat.h"
#include "r_defs.h"
#include "r_sky.h"
#include "r_utility.h"
#include "a_pickups.h"
#include "d_player.h"
#include "g_levellocals.h"
#include "events.h"
#include "actorinlines.h"
#include "r_data/r_vanillatrans.h"
#include "r_data/matrix.h"
#include "r_data/models/models.h"
#include "vectors.h"
#include "hwrenderer/models/hw_models.h"
#include "hwrenderer/scene/hw_drawstructs.h"
#include "hwrenderer/scene/hw_drawinfo.h"
#include "hwrenderer/scene/hw_fakeflat.h"
#include "hwrenderer/scene/hw_portal.h"
#include "hwrenderer/data/flatvertices.h"
#include "hwrenderer/utility/hw_cvars.h"
#include "hwrenderer/utility/hw_clock.h"
#include "hwrenderer/utility/hw_lighting.h"
#include "hwrenderer/textures/hw_material.h"
#include "hwrenderer/dynlights/hw_dynlightdata.h"
#include "hwrenderer/dynlights/hw_lightbuffer.h"
#include "hw_renderstate.h"
extern TArray<spritedef_t> sprites;
extern TArray<spriteframe_t> SpriteFrames;
extern uint32_t r_renderercaps;
const float LARGE_VALUE = 1e19f;
EXTERN_CVAR(Bool, r_debug_disable_vis_filter)
EXTERN_CVAR(Float, transsouls)
//==========================================================================
//
//
//
//==========================================================================
void GLSprite::DrawSprite(HWDrawInfo *di, FRenderState &state, bool translucent)
{
bool additivefog = false;
bool foglayer = false;
int rel = fullbright ? 0 : getExtraLight();
auto &vp = di->Viewpoint;
if (translucent)
{
// The translucent pass requires special setup for the various modes.
// for special render styles brightmaps would not look good - especially for subtractive.
if (RenderStyle.BlendOp != STYLEOP_Add)
{
state.EnableBrightmap(false);
}
// Optionally use STYLE_ColorBlend in place of STYLE_Add for fullbright items.
if (RenderStyle == LegacyRenderStyles[STYLE_Add] && trans > 1.f - FLT_EPSILON &&
gl_usecolorblending && !di->isFullbrightScene() && actor &&
fullbright && gltexture && !gltexture->tex->GetTranslucency())
{
RenderStyle = LegacyRenderStyles[STYLE_ColorAdd];
}
state.SetRenderStyle(RenderStyle);
state.SetTextureMode(RenderStyle);
if (hw_styleflags == STYLEHW_NoAlphaTest)
{
state.AlphaFunc(Alpha_GEqual, 0.f);
}
else if (!gltexture || !gltexture->tex->GetTranslucency()) state.AlphaFunc(Alpha_GEqual, gl_mask_sprite_threshold);
else state.AlphaFunc(Alpha_Greater, 0.f);
if (RenderStyle.BlendOp == STYLEOP_Shadow)
{
float fuzzalpha = 0.44f;
float minalpha = 0.1f;
// fog + fuzz don't work well without some fiddling with the alpha value!
if (!Colormap.FadeColor.isBlack())
{
float dist = Dist2(vp.Pos.X, vp.Pos.Y, x, y);
int fogd = hw_GetFogDensity(lightlevel, Colormap.FadeColor, Colormap.FogDensity, Colormap.BlendFactor);
// this value was determined by trial and error and is scale dependent!
float factor = 0.05f + exp(-fogd * dist / 62500.f);
fuzzalpha *= factor;
minalpha *= factor;
}
state.AlphaFunc(Alpha_GEqual, gl_mask_sprite_threshold);
state.SetColor(0.2f, 0.2f, 0.2f, fuzzalpha, Colormap.Desaturation);
additivefog = true;
lightlist = nullptr; // the fuzz effect does not use the sector's light level so splitting is not needed.
}
else if (RenderStyle.BlendOp == STYLEOP_Add && RenderStyle.DestAlpha == STYLEALPHA_One)
{
additivefog = true;
}
}
else if (modelframe == nullptr)
{
// This still needs to set the texture mode. As blend mode it will always use GL_ONE/GL_ZERO
state.SetTextureMode(RenderStyle);
state.SetDepthBias(-1, -128);
}
if (RenderStyle.BlendOp != STYLEOP_Shadow)
{
if (level.HasDynamicLights && !di->isFullbrightScene() && !fullbright)
{
if (dynlightindex == -1) // only set if we got no light buffer index. This covers all cases where sprite lighting is used.
{
float out[3];
di->GetDynSpriteLight(gl_light_sprites ? actor : nullptr, gl_light_particles ? particle : nullptr, out);
state.SetDynLight(out[0], out[1], out[2]);
}
}
sector_t *cursec = actor ? actor->Sector : particle ? particle->subsector->sector : nullptr;
if (cursec != nullptr)
{
const PalEntry finalcol = fullbright
? ThingColor
: ThingColor.Modulate(cursec->SpecialColors[sector_t::sprites]);
state.SetObjectColor(finalcol);
}
state.SetColor(lightlevel, rel, di->isFullbrightScene(), Colormap, trans);
}
if (Colormap.FadeColor.isBlack()) foglevel = lightlevel;
if (RenderStyle.Flags & STYLEF_FadeToBlack)
{
Colormap.FadeColor = 0;
additivefog = true;
}
if (RenderStyle.BlendOp == STYLEOP_RevSub || RenderStyle.BlendOp == STYLEOP_Sub)
{
if (!modelframe)
{
// non-black fog with subtractive style needs special treatment
if (!Colormap.FadeColor.isBlack())
{
foglayer = true;
// Due to the two-layer approach we need to force an alpha test that lets everything pass
state.AlphaFunc(Alpha_Greater, 0);
}
}
else RenderStyle.BlendOp = STYLEOP_Fuzz; // subtractive with models is not going to work.
}
if (!foglayer) state.SetFog(foglevel, rel, di->isFullbrightScene(), &Colormap, additivefog);
else
{
state.EnableFog(false);
state.SetFog(0, 0);
}
if (gltexture) state.SetMaterial(gltexture, CLAMP_XY, translation, OverrideShader);
else if (!modelframe) state.EnableTexture(false);
//SetColor(lightlevel, rel, Colormap, trans);
unsigned int iter = lightlist ? lightlist->Size() : 1;
bool clipping = false;
if (lightlist || topclip != LARGE_VALUE || bottomclip != -LARGE_VALUE)
{
clipping = true;
state.EnableSplit(true);
}
secplane_t bottomp = { { 0, 0, -1. }, bottomclip, 1. };
secplane_t topp = { { 0, 0, -1. }, topclip, 1. };
for (unsigned i = 0; i < iter; i++)
{
if (lightlist)
{
// set up the light slice
secplane_t *topplane = i == 0 ? &topp : &(*lightlist)[i].plane;
secplane_t *lowplane = i == (*lightlist).Size() - 1 ? &bottomp : &(*lightlist)[i + 1].plane;
int thislight = (*lightlist)[i].caster != nullptr ? hw_ClampLight(*(*lightlist)[i].p_lightlevel) : lightlevel;
int thisll = actor == nullptr ? thislight : (uint8_t)actor->Sector->CheckSpriteGlow(thislight, actor->InterpolatedPosition(vp.TicFrac));
FColormap thiscm;
thiscm.CopyFog(Colormap);
thiscm.CopyFrom3DLight(&(*lightlist)[i]);
if (level.flags3 & LEVEL3_NOCOLOREDSPRITELIGHTING)
{
thiscm.Decolorize();
}
state.SetColor(thisll, rel, di->isFullbrightScene(), thiscm, trans);
if (!foglayer)
{
state.SetFog(thislight, rel, di->isFullbrightScene(), &thiscm, additivefog);
}
state.SetSplitPlanes(*topplane, *lowplane);
}
else if (clipping)
{
state.SetSplitPlanes(topp, bottomp);
}
if (!modelframe)
{
state.SetNormal(0, 0, 0);
if (screen->BuffersArePersistent())
{
CreateVertices(di);
}
if (polyoffset)
{
state.SetDepthBias(-1, -128);
}
state.SetLightIndex(-1);
state.Draw(DT_TriangleStrip, vertexindex, 4);
if (foglayer)
{
// If we get here we know that we have colored fog and no fixed colormap.
state.SetFog(foglevel, rel, false, &Colormap, additivefog);
state.SetTextureMode(TM_FOGLAYER);
state.SetRenderStyle(STYLE_Translucent);
state.Draw(DT_TriangleStrip, vertexindex, 4);
state.SetTextureMode(TM_NORMAL);
}
}
else
{
FGLModelRenderer renderer(di, state, dynlightindex);
renderer.RenderModel(x, y, z, modelframe, actor, di->Viewpoint.TicFrac);
state.SetVertexBuffer(screen->mVertexData);
}
}
if (clipping)
{
state.EnableSplit(false);
}
if (translucent)
{
state.EnableBrightmap(true);
state.SetRenderStyle(STYLE_Translucent);
state.SetTextureMode(TM_NORMAL);
if (actor != nullptr && (actor->renderflags & RF_SPRITETYPEMASK) == RF_FLATSPRITE)
{
state.ClearDepthBias();
}
}
else if (modelframe == nullptr)
{
state.ClearDepthBias();
}
state.SetObjectColor(0xffffffff);
state.EnableTexture(true);
state.SetDynLight(0, 0, 0);
}
//==========================================================================
//
//
//
//==========================================================================
bool GLSprite::CalculateVertices(HWDrawInfo *di, FVector3 *v, DVector3 *vp)
{
const auto &HWAngles = di->Viewpoint.HWAngles;
if (actor != nullptr && (actor->renderflags & RF_SPRITETYPEMASK) == RF_FLATSPRITE)
{
Matrix3x4 mat;
mat.MakeIdentity();
// [MC] Rotate around the center or offsets given to the sprites.
// Counteract any existing rotations, then rotate the angle.
// Tilt the actor up or down based on pitch (increase 'somersaults' forward).
// Then counteract the roll and DO A BARREL ROLL.
FAngle pitch = (float)-Angles.Pitch.Degrees;
pitch.Normalized180();
mat.Translate(x, z, y);
mat.Rotate(0, 1, 0, 270. - Angles.Yaw.Degrees);
mat.Rotate(1, 0, 0, pitch.Degrees);
if (actor->renderflags & RF_ROLLCENTER)
{
float cx = (x1 + x2) * 0.5;
float cy = (y1 + y2) * 0.5;
mat.Translate(cx - x, 0, cy - y);
mat.Rotate(0, 1, 0, - Angles.Roll.Degrees);
mat.Translate(-cx, -z, -cy);
}
else
{
mat.Rotate(0, 1, 0, - Angles.Roll.Degrees);
mat.Translate(-x, -z, -y);
}
v[0] = mat * FVector3(x2, z, y2);
v[1] = mat * FVector3(x1, z, y2);
v[2] = mat * FVector3(x2, z, y1);
v[3] = mat * FVector3(x1, z, y1);
return true;
}
// [BB] Billboard stuff
const bool drawWithXYBillboard = ((particle && gl_billboard_particles) || (!(actor && actor->renderflags & RF_FORCEYBILLBOARD)
//&& di->mViewActor != nullptr
&& (gl_billboard_mode == 1 || (actor && actor->renderflags & RF_FORCEXYBILLBOARD))));
const bool drawBillboardFacingCamera = gl_billboard_faces_camera;
// [Nash] has +ROLLSPRITE
const bool drawRollSpriteActor = (actor != nullptr && actor->renderflags & RF_ROLLSPRITE);
// [fgsfds] check sprite type mask
uint32_t spritetype = (uint32_t)-1;
if (actor != nullptr) spritetype = actor->renderflags & RF_SPRITETYPEMASK;
// [Nash] is a flat sprite
const bool isFlatSprite = (actor != nullptr) && (spritetype == RF_WALLSPRITE || spritetype == RF_FLATSPRITE);
const bool useOffsets = (actor != nullptr) && !(actor->renderflags & RF_ROLLCENTER);
// [Nash] check for special sprite drawing modes
if (drawWithXYBillboard || drawBillboardFacingCamera || drawRollSpriteActor || isFlatSprite)
{
// Compute center of sprite
float xcenter = (x1 + x2)*0.5;
float ycenter = (y1 + y2)*0.5;
float zcenter = (z1 + z2)*0.5;
float xx = -xcenter + x;
float zz = -zcenter + z;
float yy = -ycenter + y;
Matrix3x4 mat;
mat.MakeIdentity();
mat.Translate(xcenter, zcenter, ycenter); // move to sprite center
// Order of rotations matters. Perform yaw rotation (Y, face camera) before pitch (X, tilt up/down).
if (drawBillboardFacingCamera && !isFlatSprite)
{
// [CMB] Rotate relative to camera XY position, not just camera direction,
// which is nicer in VR
float xrel = xcenter - vp->X;
float yrel = ycenter - vp->Y;
float absAngleDeg = RAD2DEG(atan2(-yrel, xrel));
float counterRotationDeg = 270. - HWAngles.Yaw.Degrees; // counteracts existing sprite rotation
float relAngleDeg = counterRotationDeg + absAngleDeg;
mat.Rotate(0, 1, 0, relAngleDeg);
}
// [fgsfds] calculate yaw vectors
float yawvecX = 0, yawvecY = 0, rollDegrees = 0;
float angleRad = (270. - HWAngles.Yaw).Radians();
if (actor) rollDegrees = Angles.Roll.Degrees;
if (isFlatSprite)
{
yawvecX = Angles.Yaw.Cos();
yawvecY = Angles.Yaw.Sin();
}
// [fgsfds] Rotate the sprite about the sight vector (roll)
if (spritetype == RF_WALLSPRITE)
{
mat.Rotate(0, 1, 0, 0);
if (drawRollSpriteActor)
{
if (useOffsets) mat.Translate(xx, zz, yy);
mat.Rotate(yawvecX, 0, yawvecY, rollDegrees);
if (useOffsets) mat.Translate(-xx, -zz, -yy);
}
}
else if (drawRollSpriteActor)
{
if (useOffsets) mat.Translate(xx, zz, yy);
if (drawWithXYBillboard)
{
mat.Rotate(-sin(angleRad), 0, cos(angleRad), -HWAngles.Pitch.Degrees);
}
mat.Rotate(cos(angleRad), 0, sin(angleRad), rollDegrees);
if (useOffsets) mat.Translate(-xx, -zz, -yy);
}
else if (drawWithXYBillboard)
{
// Rotate the sprite about the vector starting at the center of the sprite
// triangle strip and with direction orthogonal to where the player is looking
// in the x/y plane.
mat.Rotate(-sin(angleRad), 0, cos(angleRad), -HWAngles.Pitch.Degrees);
}
mat.Translate(-xcenter, -zcenter, -ycenter); // retreat from sprite center
v[0] = mat * FVector3(x1, z1, y1);
v[1] = mat * FVector3(x2, z1, y2);
v[2] = mat * FVector3(x1, z2, y1);
v[3] = mat * FVector3(x2, z2, y2);
}
else // traditional "Y" billboard mode
{
v[0] = FVector3(x1, z1, y1);
v[1] = FVector3(x2, z1, y2);
v[2] = FVector3(x1, z2, y1);
v[3] = FVector3(x2, z2, y2);
}
return false;
}
//==========================================================================
//
//
//
//==========================================================================
inline void GLSprite::PutSprite(HWDrawInfo *di, bool translucent)
{
// That's a lot of checks...
if (modelframe && !modelframe->isVoxel && RenderStyle.BlendOp != STYLEOP_Shadow && gl_light_sprites && level.HasDynamicLights && !di->isFullbrightScene() && !fullbright)
{
hw_GetDynModelLight(actor, lightdata);
dynlightindex = screen->mLights->UploadLights(lightdata);
}
else
dynlightindex = -1;
vertexindex = -1;
if (!screen->BuffersArePersistent())
{
CreateVertices(di);
}
di->AddSprite(this, translucent);
}
//==========================================================================
//
//
//
//==========================================================================
void GLSprite::CreateVertices(HWDrawInfo *di)
{
if (modelframe == nullptr)
{
FVector3 v[4];
polyoffset = CalculateVertices(di, v, &di->Viewpoint.Pos);
auto vert = screen->mVertexData->AllocVertices(4);
auto vp = vert.first;
vertexindex = vert.second;
vp[0].Set(v[0][0], v[0][1], v[0][2], ul, vt);
vp[1].Set(v[1][0], v[1][1], v[1][2], ur, vt);
vp[2].Set(v[2][0], v[2][1], v[2][2], ul, vb);
vp[3].Set(v[3][0], v[3][1], v[3][2], ur, vb);
}
}
//==========================================================================
//
//
//
//==========================================================================
void GLSprite::SplitSprite(HWDrawInfo *di, sector_t * frontsector, bool translucent)
{
GLSprite copySprite;
double lightbottom;
unsigned int i;
bool put=false;
TArray<lightlist_t> & lightlist=frontsector->e->XFloor.lightlist;
for(i=0;i<lightlist.Size();i++)
{
// Particles don't go through here so we can safely assume that actor is not nullptr
if (i<lightlist.Size()-1) lightbottom=lightlist[i+1].plane.ZatPoint(actor);
else lightbottom=frontsector->floorplane.ZatPoint(actor);
if (lightbottom<z2) lightbottom=z2;
if (lightbottom<z1)
{
copySprite=*this;
copySprite.lightlevel = hw_ClampLight(*lightlist[i].p_lightlevel);
copySprite.Colormap.CopyLight(lightlist[i].extra_colormap);
if (level.flags3 & LEVEL3_NOCOLOREDSPRITELIGHTING)
{
copySprite.Colormap.Decolorize();
}
if (!ThingColor.isWhite())
{
copySprite.Colormap.LightColor.r = (copySprite.Colormap.LightColor.r*ThingColor.r) >> 8;
copySprite.Colormap.LightColor.g = (copySprite.Colormap.LightColor.g*ThingColor.g) >> 8;
copySprite.Colormap.LightColor.b = (copySprite.Colormap.LightColor.b*ThingColor.b) >> 8;
}
z1=copySprite.z2=lightbottom;
vt=copySprite.vb=copySprite.vt+
(lightbottom-copySprite.z1)*(copySprite.vb-copySprite.vt)/(z2-copySprite.z1);
copySprite.PutSprite(di, translucent);
put=true;
}
}
}
//==========================================================================
//
//
//
//==========================================================================
void GLSprite::PerformSpriteClipAdjustment(AActor *thing, const DVector2 &thingpos, float spriteheight)
{
const float NO_VAL = 100000000.0f;
bool clipthing = (thing->player || thing->flags3&MF3_ISMONSTER || thing->IsKindOf(NAME_Inventory)) && (thing->flags&MF_ICECORPSE || !(thing->flags&MF_CORPSE));
bool smarterclip = !clipthing && gl_spriteclip == 3;
if (clipthing || gl_spriteclip > 1)
{
float btm = NO_VAL;
float top = -NO_VAL;
extsector_t::xfloor &x = thing->Sector->e->XFloor;
if (x.ffloors.Size())
{
for (unsigned int i = 0; i < x.ffloors.Size(); i++)
{
F3DFloor * ff = x.ffloors[i];
if (ff->flags & FF_THISINSIDE) continue; // only relevant for software rendering.
float floorh = ff->top.plane->ZatPoint(thingpos);
float ceilingh = ff->bottom.plane->ZatPoint(thingpos);
if (floorh == thing->floorz)
{
btm = floorh;
}
if (ceilingh == thing->ceilingz)
{
top = ceilingh;
}
if (btm != NO_VAL && top != -NO_VAL)
{
break;
}
}
}
else if (thing->Sector->GetHeightSec())
{
if (thing->flags2&MF2_ONMOBJ && thing->floorz ==
thing->Sector->heightsec->floorplane.ZatPoint(thingpos))
{
btm = thing->floorz;
top = thing->ceilingz;
}
}
if (btm == NO_VAL)
btm = thing->Sector->floorplane.ZatPoint(thing) - thing->Floorclip;
if (top == NO_VAL)
top = thing->Sector->ceilingplane.ZatPoint(thingpos);
// +/-1 to account for the one pixel empty frame around the sprite.
float diffb = (z2+1) - btm;
float difft = (z1-1) - top;
if (diffb >= 0 /*|| !gl_sprite_clip_to_floor*/) diffb = 0;
// Adjust sprites clipping into ceiling and adjust clipping adjustment for tall graphics
if (smarterclip)
{
// Reduce slightly clipping adjustment of corpses
if (thing->flags & MF_CORPSE || spriteheight > fabs(diffb))
{
float ratio = clamp<float>((fabs(diffb) * (float)gl_sclipfactor / (spriteheight + 1)), 0.5, 1.0);
diffb *= ratio;
}
if (!diffb)
{
if (difft <= 0) difft = 0;
if (difft >= (float)gl_sclipthreshold)
{
// dumb copy of the above.
if (!(thing->flags3&MF3_ISMONSTER) || (thing->flags&MF_NOGRAVITY) || (thing->flags&MF_CORPSE) || difft > (float)gl_sclipthreshold)
{
difft = 0;
}
}
if (spriteheight > fabs(difft))
{
float ratio = clamp<float>((fabs(difft) * (float)gl_sclipfactor / (spriteheight + 1)), 0.5, 1.0);
difft *= ratio;
}
z2 -= difft;
z1 -= difft;
}
}
if (diffb <= (0 - (float)gl_sclipthreshold)) // such a large displacement can't be correct!
{
// for living monsters standing on the floor allow a little more.
if (!(thing->flags3&MF3_ISMONSTER) || (thing->flags&MF_NOGRAVITY) || (thing->flags&MF_CORPSE) || diffb < (-1.8*(float)gl_sclipthreshold))
{
diffb = 0;
}
}
z2 -= diffb;
z1 -= diffb;
}
}
//==========================================================================
//
//
//
//==========================================================================
void GLSprite::Process(HWDrawInfo *di, AActor* thing, sector_t * sector, area_t in_area, int thruportal)
{
sector_t rs;
sector_t * rendersector;
if (thing == nullptr)
return;
// [ZZ] allow CustomSprite-style direct picnum specification
bool isPicnumOverride = thing->picnum.isValid();
// Don't waste time projecting sprites that are definitely not visible.
if ((thing->sprite == 0 && !isPicnumOverride) || !thing->IsVisibleToPlayer() || ((thing->renderflags & RF_MASKROTATION) && !thing->IsInsideVisibleAngles()))
{
return;
}
const auto &vp = di->Viewpoint;
AActor *camera = vp.camera;
if (thing->renderflags & RF_INVISIBLE || !thing->RenderStyle.IsVisible(thing->Alpha))
{
if (!(thing->flags & MF_STEALTH) || !di->isStealthVision() || thing == camera)
return;
}
// check renderrequired vs ~r_rendercaps, if anything matches we don't support that feature,
// check renderhidden vs r_rendercaps, if anything matches we do support that feature and should hide it.
if ((!r_debug_disable_vis_filter && !!(thing->RenderRequired & ~r_renderercaps)) ||
(!!(thing->RenderHidden & r_renderercaps)))
return;
int spritenum = thing->sprite;
DVector2 sprscale = thing->Scale;
if (thing->player != nullptr)
{
P_CheckPlayerSprite(thing, spritenum, sprscale);
}
// [RH] Interpolate the sprite's position to make it look smooth
DVector3 thingpos = thing->InterpolatedPosition(vp.TicFrac);
if (thruportal == 1) thingpos += level.Displacements.getOffset(thing->Sector->PortalGroup, sector->PortalGroup);
// Some added checks if the camera actor is not supposed to be seen. It can happen that some portal setup has this actor in view in which case it may not be skipped here
if (thing == camera && !vp.showviewer)
{
DVector3 thingorigin = thing->Pos();
if (thruportal == 1) thingorigin += level.Displacements.getOffset(thing->Sector->PortalGroup, sector->PortalGroup);
if (fabs(thingorigin.X - vp.ActorPos.X) < 2 && fabs(thingorigin.Y - vp.ActorPos.Y) < 2) return;
}
// Thing is invisible if close to the camera.
if (thing->renderflags & RF_MAYBEINVISIBLE)
{
if (fabs(thingpos.X - vp.Pos.X) < 32 && fabs(thingpos.Y - vp.Pos.Y) < 32) return;
}
// Too close to the camera. This doesn't look good if it is a sprite.
if (fabs(thingpos.X - vp.Pos.X) < 2 && fabs(thingpos.Y - vp.Pos.Y) < 2)
{
if (vp.Pos.Z >= thingpos.Z - 2 && vp.Pos.Z <= thingpos.Z + thing->Height + 2)
{
// exclude vertically moving objects from this check.
if (!thing->Vel.isZero())
{
if (!FindModelFrame(thing->GetClass(), spritenum, thing->frame, false))
{
return;
}
}
}
}
// don't draw first frame of a player missile
if (thing->flags&MF_MISSILE)
{
if (!(thing->flags7 & MF7_FLYCHEAT) && thing->target == vp.ViewActor && vp.ViewActor != nullptr)
{
double speed = thing->Vel.Length();
if (speed >= thing->target->radius / 2)
{
double clipdist = clamp(thing->Speed, thing->target->radius, thing->target->radius * 2);
if ((thingpos - vp.Pos).LengthSquared() < clipdist * clipdist) return;
}
}
thing->flags7 |= MF7_FLYCHEAT; // do this only once for the very first frame, but not if it gets into range again.
}
if (thruportal != 2 && di->mClipPortal != nullptr)
{
int clipres = di->mClipPortal->ClipPoint(thingpos);
if (clipres == PClip_InFront) return;
}
// disabled because almost none of the actual game code is even remotely prepared for this. If desired, use the INTERPOLATE flag.
if (thing->renderflags & RF_INTERPOLATEANGLES)
Angles = thing->InterpolatedAngles(vp.TicFrac);
else
Angles = thing->Angles;
player_t *player = &players[consoleplayer];
FloatRect r;
if (sector->sectornum != thing->Sector->sectornum && !thruportal)
{
// This cannot create a copy in the fake sector cache because it'd interfere with the main thread, so provide a local buffer for the copy.
// Adding synchronization for this one case would cost more than it might save if the result here could be cached.
rendersector = hw_FakeFlat(thing->Sector, in_area, false, &rs);
}
else
{
rendersector = sector;
}
topclip = rendersector->PortalBlocksMovement(sector_t::ceiling) ? LARGE_VALUE : rendersector->GetPortalPlaneZ(sector_t::ceiling);
bottomclip = rendersector->PortalBlocksMovement(sector_t::floor) ? -LARGE_VALUE : rendersector->GetPortalPlaneZ(sector_t::floor);
uint32_t spritetype = (thing->renderflags & RF_SPRITETYPEMASK);
x = thingpos.X;
z = thingpos.Z;
y = thingpos.Y;
if (spritetype == RF_FACESPRITE) z -= thing->Floorclip; // wall and flat sprites are to be considered level geometry so this may not apply.
// [RH] Make floatbobbing a renderer-only effect.
if (thing->flags2 & MF2_FLOATBOB)
{
float fz = thing->GetBobOffset(vp.TicFrac);
z += fz;
}
modelframe = isPicnumOverride ? nullptr : FindModelFrame(thing->GetClass(), spritenum, thing->frame, !!(thing->flags & MF_DROPPED));
if (!modelframe)
{
bool mirror;
DAngle ang = (thingpos - vp.Pos).Angle();
FTextureID patch;
// [ZZ] add direct picnum override
if (isPicnumOverride)
{
// Animate picnum overrides.
auto tex = TexMan.GetTexture(thing->picnum, true);
if (tex == nullptr) return;
patch = tex->GetID();
mirror = false;
}
else
{
DAngle sprangle;
int rot;
if (!(thing->renderflags & RF_FLATSPRITE) || thing->flags7 & MF7_SPRITEANGLE)
{
sprangle = thing->GetSpriteAngle(ang, vp.TicFrac);
rot = -1;
}
else
{
// Flat sprites cannot rotate in a predictable manner.
sprangle = 0.;
rot = 0;
}
patch = sprites[spritenum].GetSpriteFrame(thing->frame, rot, sprangle, &mirror, !!(thing->renderflags & RF_SPRITEFLIP));
}
if (!patch.isValid()) return;
int type = thing->renderflags & RF_SPRITETYPEMASK;
gltexture = FMaterial::ValidateTexture(patch, (type == RF_FACESPRITE), false);
if (!gltexture)
return;
vt = gltexture->GetSpriteVT();
vb = gltexture->GetSpriteVB();
if (thing->renderflags & RF_YFLIP) std::swap(vt, vb);
gltexture->GetSpriteRect(&r);
// [SP] SpriteFlip
if (thing->renderflags & RF_SPRITEFLIP)
thing->renderflags ^= RF_XFLIP;
if (mirror ^ !!(thing->renderflags & RF_XFLIP))
{
r.left = -r.width - r.left; // mirror the sprite's x-offset
ul = gltexture->GetSpriteUL();
ur = gltexture->GetSpriteUR();
}
else
{
ul = gltexture->GetSpriteUR();
ur = gltexture->GetSpriteUL();
}
if (thing->renderflags & RF_SPRITEFLIP) // [SP] Flip back
thing->renderflags ^= RF_XFLIP;
r.Scale(sprscale.X, sprscale.Y);
float rightfac = -r.left;
float leftfac = rightfac - r.width;
float bottomfac = -r.top;
float topfac = bottomfac - r.height;
z1 = z - r.top;
z2 = z1 - r.height;
float spriteheight = sprscale.Y * r.height;
// Tests show that this doesn't look good for many decorations and corpses
if (spriteheight > 0 && gl_spriteclip > 0 && (thing->renderflags & RF_SPRITETYPEMASK) == RF_FACESPRITE)
{
PerformSpriteClipAdjustment(thing, thingpos, spriteheight);
}
float viewvecX;
float viewvecY;
switch (spritetype)
{
case RF_FACESPRITE:
viewvecX = vp.ViewVector.X;
viewvecY = vp.ViewVector.Y;
x1 = x - viewvecY*leftfac;
x2 = x - viewvecY*rightfac;
y1 = y + viewvecX*leftfac;
y2 = y + viewvecX*rightfac;
break;
case RF_FLATSPRITE:
{
x1 = x + leftfac;
x2 = x + rightfac;
y1 = y - topfac;
y2 = y - bottomfac;
}
break;
case RF_WALLSPRITE:
viewvecX = Angles.Yaw.Cos();
viewvecY = Angles.Yaw.Sin();
x1 = x + viewvecY*leftfac;
x2 = x + viewvecY*rightfac;
y1 = y - viewvecX*leftfac;
y2 = y - viewvecX*rightfac;
break;
}
}
else
{
x1 = x2 = x;
y1 = y2 = y;
z1 = z2 = z;
gltexture = nullptr;
}
depth = FloatToFixed((x - vp.Pos.X) * vp.TanCos + (y - vp.Pos.Y) * vp.TanSin);
// light calculation
bool enhancedvision = false;
// allow disabling of the fullbright flag by a brightmap definition
// (e.g. to do the gun flashes of Doom's zombies correctly.
fullbright = (thing->flags5 & MF5_BRIGHT) ||
((thing->renderflags & RF_FULLBRIGHT) && (!gltexture || !gltexture->tex->isFullbrightDisabled()));
lightlevel = fullbright ? 255 :
hw_ClampLight(rendersector->GetTexture(sector_t::ceiling) == skyflatnum ?
rendersector->GetCeilingLight() : rendersector->GetFloorLight());
foglevel = (uint8_t)clamp<short>(rendersector->lightlevel, 0, 255);
lightlevel = rendersector->CheckSpriteGlow(lightlevel, thingpos);
ThingColor = (thing->RenderStyle.Flags & STYLEF_ColorIsFixed) ? thing->fillcolor : 0xffffff;
ThingColor.a = 255;
RenderStyle = thing->RenderStyle;
// colormap stuff is a little more complicated here...
if (di->isFullbrightScene())
{
enhancedvision = di->isStealthVision();
Colormap.Clear();
if (di->isNightvision())
{
if ((thing->IsKindOf(NAME_Inventory) || thing->flags3&MF3_ISMONSTER || thing->flags&MF_MISSILE || thing->flags&MF_CORPSE))
{
RenderStyle.Flags |= STYLEF_InvertSource;
}
}
}
else
{
Colormap = rendersector->Colormap;
if (fullbright)
{
if (rendersector == &level.sectors[rendersector->sectornum] || in_area != area_below)
// under water areas keep their color for fullbright objects
{
// Only make the light white but keep everything else (fog, desaturation and Boom colormap.)
Colormap.MakeWhite();
}
else
{
// Keep the color, but brighten things a bit so that a difference can be seen.
Colormap.LightColor.r = (3 * Colormap.LightColor.r + 0xff) / 4;
Colormap.LightColor.g = (3 * Colormap.LightColor.g + 0xff) / 4;
Colormap.LightColor.b = (3 * Colormap.LightColor.b + 0xff) / 4;
}
}
else if (level.flags3 & LEVEL3_NOCOLOREDSPRITELIGHTING)
{
Colormap.Decolorize();
}
}
translation = thing->Translation;
OverrideShader = -1;
trans = thing->Alpha;
hw_styleflags = STYLEHW_Normal;
if (RenderStyle.BlendOp >= STYLEOP_Fuzz && RenderStyle.BlendOp <= STYLEOP_FuzzOrRevSub)
{
RenderStyle.CheckFuzz();
if (RenderStyle.BlendOp == STYLEOP_Fuzz)
{
if (gl_fuzztype != 0 && !(RenderStyle.Flags & STYLEF_InvertSource))
{
RenderStyle = LegacyRenderStyles[STYLE_Translucent];
OverrideShader = SHADER_NoTexture + gl_fuzztype;
trans = 0.99f; // trans may not be 1 here
hw_styleflags = STYLEHW_NoAlphaTest;
}
else
{
// Without shaders only the standard effect is available.
RenderStyle.BlendOp = STYLEOP_Shadow;
}
}
}
if (RenderStyle.Flags & STYLEF_TransSoulsAlpha)
{
trans = transsouls;
}
else if (RenderStyle.Flags & STYLEF_Alpha1)
{
trans = 1.f;
}
if (r_UseVanillaTransparency)