/
polymost.cpp
3946 lines (3134 loc) · 128 KB
/
polymost.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
/**************************************************************************************************
"POLYMOST" code originally written by Ken Silverman
Ken Silverman's official web site: http://www.advsys.net/ken
**************************************************************************************************/
#include "build.h"
#include "automap.h"
#include "engine_priv.h"
#include "mdsprite.h"
#include "polymost.h"
#include "files.h"
#include "buildtiles.h"
#include "bitmap.h"
#include "../../glbackend/glbackend.h"
#include "c_cvars.h"
#include "gamecvars.h"
#include "v_video.h"
#include "flatvertices.h"
#include "palettecontainer.h"
#include "texturemanager.h"
#include "hw_renderstate.h"
#include "printf.h"
#include "gamefuncs.h"
#include "hw_drawinfo.h"
#include "gamestruct.h"
#include "gamestruct.h"
#include "hw_voxels.h"
#ifdef _MSC_VER
// just make it shut up. Most of this file will go down the drain anyway soon.
#pragma warning(disable:4244)
#endif
typedef struct {
union { double x; double d; };
union { double y; double u; };
union { double z; double v; };
} vec3d_t;
static_assert(sizeof(vec3d_t) == sizeof(double) * 3);
int32_t xyaspect = -1;
int skiptile = -1;
FGameTexture* GetSkyTexture(int basetile, int lognumtiles, const int16_t* tilemap, int remap = 0);
int32_t polymost_voxdraw(voxmodel_t* m, tspriteptr_t const tspr, bool rotate);
int checkTranslucentReplacement(FTextureID picnum, int pal);
CVARD(Bool, hw_animsmoothing, true, CVAR_ARCHIVE | CVAR_GLOBALCONFIG, "enable/disable model animation smoothing")
CVARD(Bool, hw_models, false, CVAR_ARCHIVE | CVAR_GLOBALCONFIG, "enable/disable model rendering")
CVARD(Bool, hw_parallaxskypanning, true, CVAR_ARCHIVE | CVAR_GLOBALCONFIG, "enable/disable parallaxed floor/ceiling panning when drawing a parallaxing sky")
CVARD(Float, hw_shadescale, 1.0f, CVAR_ARCHIVE | CVAR_GLOBALCONFIG, "multiplier for shading")
int pm_smoothratio;
//{ "r_yshearing", "enable/disable y-shearing", (void*)&r_yshearing, CVAR_BOOL, 0, 1 }, disabled because not fully functional
// For testing - will be removed later.
CVAR(Int, skytile, 0, 0)
CVAR(Bool, testnewrenderer, false, CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
extern fixed_t global100horiz; // (-100..300)-scale horiz (the one passed to drawrooms)
static vec3_t spritesxyz[MAXSPRITESONSCREEN + 1];
static int16_t thewall[MAXWALLSB];
static int16_t bunchp2[MAXWALLSB], thesector[MAXWALLSB];
static int16_t bunchfirst[MAXWALLSB], bunchlast[MAXWALLSB];
static int16_t numscans, numbunches;
static int16_t maskwall[MAXWALLSB], maskwallcnt;
static int16_t sectorborder[256];
static tspriteptr_t tspriteptr[MAXSPRITESONSCREEN + 1];
tspritetype pm_tsprite[MAXSPRITESONSCREEN];
int pm_spritesortcnt;
namespace Polymost
{
typedef struct { float x, cy[2], fy[2]; int32_t tag; int16_t n, p, ctag, ftag; } vsptyp;
#define VSPMAX 2048 //<- careful!
static vsptyp vsp[VSPMAX];
static int32_t gtag, viewportNodeCount;
static float xbl, xbr, xbt, xbb;
static int32_t domost_rejectcount;
static float dxb1[MAXWALLSB], dxb2[MAXWALLSB];
//POGOTODO: the SCISDIST could be set to 0 now to allow close objects to render properly,
// but there's a nasty rendering bug that needs to be dug into when setting SCISDIST lower than 1
#define SCISDIST 1.f //close plane clipping distance
#define SOFTROTMAT 0
static int32_t r_pogoDebug = 0;
static float gviewxrange;
static float ghoriz, ghoriz2;
static float ghorizcorrect;
double gxyaspect;
float gyxscale, ghalfx, grhalfxdown10, grhalfxdown10x, ghalfy;
float gcosang, gsinang, gcosang2, gsinang2;
float gtang = 0.f;
static float gchang = 0, gshang = 0, gctang = 0, gstang = 0;
static float gvrcorrection = 1.f;
static vec3d_t xtex, ytex, otex, xtex2, ytex2, otex2;
static float fsearchx, fsearchy, fsearchz;
static int psectnum, pwallnum, pbottomwall, pisbottomwall, psearchstat;
static int32_t drawpoly_srepeat = 0, drawpoly_trepeat = 0;
#define MAX_DRAWPOLY_VERTS 8
static int32_t lastglpolygonmode = 0; //FUK
static int32_t r_yshearing = 0;
// used for fogcalc
static float fogresult, fogresult2;
static char ptempbuf[MAXWALLSB<<1];
// polymost ART sky control
static int32_t r_parallaxskyclamping = 1;
#define MIN_CACHETIME_PRINT 10
#define Bfabsf fabsf
static int32_t drawingskybox = 0;
static hitdata_t polymost_hitdata;
FGameTexture* globalskytex = nullptr;
void polymost_outputGLDebugMessage(uint8_t severity, const char* format, ...)
{
}
static void set_globalang(fixed_t const ang)
{
globalang = FixedToInt(ang) & 2047;
qglobalang = ang & 0x7FFFFFF;
float const f_ang = FixedToFloat(ang);
float const fcosang = bcosf(f_ang);
float const fsinang = bsinf(f_ang);
#ifdef USE_OPENGL
fcosglobalang = fcosang;
fsinglobalang = fsinang;
#endif
cosglobalang = (int)fcosang;
singlobalang = (int)fsinang;
cosviewingrangeglobalang = MulScale(cosglobalang, viewingrange, 16);
sinviewingrangeglobalang = MulScale(singlobalang, viewingrange, 16);
}
static inline float polymost_invsqrt_approximation(float x)
{
// this is the comment
return 1.f / sqrtf(x);
}
static float sectorVisibility(int sectnum)
{
// Beware of wraparound madness...
int v = sector[sectnum].visibility;
return v? ((uint8_t)(v + 16)) / 16.f : 1.f;
}
static void tileUpdatePicnum(short* const tileptr, int const obj)
{
auto& tile = *tileptr;
if (picanm[tile].sf & PICANM_ANIMTYPE_MASK)
tile += animateoffs(tile, obj);
if (((obj & 16384) == 16384) && (globalorientation & CSTAT_WALL_ROTATE_90) && RotTile(tile).newtile != -1)
tile = RotTile(tile).newtile;
}
//--------------------------------------------------------------------------------------------------
//Use this for both initialization and uninitialization of OpenGL.
//in-place multiply m0=m0*m1
static float* multiplyMatrix4f(float m0[4*4], const float m1[4*4])
{
float mR[4*4];
#define multMatrix4RowCol(r, c) mR[r*4+c] = m0[r*4]*m1[c] + m0[r*4+1]*m1[c+4] + m0[r*4+2]*m1[c+8] + m0[r*4+3]*m1[c+12]
multMatrix4RowCol(0, 0);
multMatrix4RowCol(0, 1);
multMatrix4RowCol(0, 2);
multMatrix4RowCol(0, 3);
multMatrix4RowCol(1, 0);
multMatrix4RowCol(1, 1);
multMatrix4RowCol(1, 2);
multMatrix4RowCol(1, 3);
multMatrix4RowCol(2, 0);
multMatrix4RowCol(2, 1);
multMatrix4RowCol(2, 2);
multMatrix4RowCol(2, 3);
multMatrix4RowCol(3, 0);
multMatrix4RowCol(3, 1);
multMatrix4RowCol(3, 2);
multMatrix4RowCol(3, 3);
memcpy(m0, mR, sizeof(float)*4*4);
return m0;
#undef multMatrix4RowCol
}
void polymost_glreset()
{
//Reset if this is -1 (meaning 1st texture call ever), or > 0 (textures in memory)
gcosang = gcosang2 = 16384.f/262144.f;
gsinang = gsinang2 = 0.f;
}
FileReader GetBaseResource(const char* fn);
static void resizeglcheck(void)
{
const int32_t ourxdimen = (windowxy2.x-windowxy1.x+1);
float ratio = 1;
const int32_t fovcorrect = (int32_t)(ourxdimen*ratio - ourxdimen);
ratio = 1.f/ratio;
GLInterface.SetViewport(windowxy1.x-(fovcorrect/2), ydim-(windowxy2.y+1),
ourxdimen+fovcorrect, windowxy2.y-windowxy1.y+1);
float m[4][4]{};
float const nearclip = 4.0f / (gxyaspect * gyxscale);
float const farclip = 65536.f;
m[0][0] = 1.f;
m[1][1] = fxdimen / (fydimen * ratio);
m[2][0] = 2.f * ghoriz2 * gstang / fxdimen;
m[2][1] = 2.f * (ghoriz2 * gctang + ghorizcorrect) / fydimen;
m[2][2] = (farclip + nearclip) / (farclip - nearclip);
m[2][3] = 1.f;
m[3][2] = -(2.f * farclip * nearclip) / (farclip - nearclip);
renderSetProjectionMatrix(&m[0][0]);
}
//(dpx,dpy) specifies an n-sided polygon. The polygon must be a convex clockwise loop.
// n must be <= 8 (assume clipping can double number of vertices)
//method: 0:solid, 1:masked(255 is transparent), 2:transluscent #1, 3:transluscent #2
// +4 means it's a sprite, so wraparound isn't needed
// drawpoly's hack globals
static int32_t pow2xsplit = 0, skyzbufferhack = 0, flatskyrender = 0;
static float drawpoly_alpha = 0.f;
static uint8_t drawpoly_blend = 0;
int32_t polymost_maskWallHasTranslucency(walltype const * const wall)
{
if (wall->cstat & CSTAT_WALL_TRANSLUCENT)
return true;
return checkTranslucentReplacement(tileGetTexture(wall->picnum)->GetID(), wall->pal);
}
int32_t polymost_spriteHasTranslucency(spritetype const * const tspr)
{
if ((tspr->cstat & CSTAT_SPRITE_TRANSLUCENT) || (tspr->clipdist & TSPR_FLAGS_DRAW_LAST) ||
((unsigned)tspr->owner < MAXSPRITES && spriteext[tspr->owner].alpha))
return true;
return checkTranslucentReplacement(tileGetTexture(tspr->picnum)->GetID(), tspr->pal);
}
static void polymost_updaterotmat(void)
{
//Up/down rotation
float matrix[16] = {
1.f, 0.f, 0.f, 0.f,
0.f, gchang, -gshang*gvrcorrection, 0.f,
0.f, gshang/gvrcorrection, gchang, 0.f,
0.f, 0.f, 0.f, 1.f,
};
// Tilt rotation
float tiltmatrix[16] = {
gctang, -gstang, 0.f, 0.f,
gstang, gctang, 0.f, 0.f,
0.f, 0.f, 1.f, 0.f,
0.f, 0.f, 0.f, 1.f,
};
multiplyMatrix4f(matrix, tiltmatrix);
renderSetViewMatrix(matrix);
float fxdimen = FixedToFloat(xdimenscale);
renderSetVisibility(g_visibility * fxdimen * (1.f / (65536.f)) / r_ambientlight);
}
const vec2_16_t tileSize(int index)
{
vec2_16_t v = { (int16_t)tileWidth(index), (int16_t)tileHeight(index) };
return v;
}
static void polymost_flatskyrender(vec2f_t const* const dpxy, int32_t const n, int32_t method, const vec2_16_t& tilesize);
// Hack for Duke's camera until I can find out why this behaves erratically.
static void polymost_drawpoly(vec2f_t const * const dpxy, int32_t const n, int32_t method, const vec2_16_t &tilesize)
{
if (method == DAMETH_BACKFACECULL || globalpicnum == skiptile ||
(uint32_t)globalpicnum >= MAXTILES)
return;
const int32_t method_ = method;
if (n == 3)
{
if ((dpxy[0].x-dpxy[1].x) * (dpxy[2].y-dpxy[1].y) >=
(dpxy[2].x-dpxy[1].x) * (dpxy[0].y-dpxy[1].y)) return; //for triangle
}
else if (n > 3)
{
float f = 0; //f is area of polygon / 2
for (bssize_t i=n-2, j=n-1,k=0; k<n; i=j,j=k,k++)
f += (dpxy[i].x-dpxy[k].x)*dpxy[j].y;
if (f <= 0) return;
}
static int32_t skyzbufferhack_pass = 0;
if (flatskyrender && skyzbufferhack_pass == 0)
{
polymost_flatskyrender(dpxy, n, method|DAMETH_SKY, tilesize);
return;
}
if (!lookups.checkTable(globalpal))
globalfloorpal = globalpal = 0;
//Load texture (globalpicnum)
setgotpic(globalpicnum);
vec2_t tsiz = { tilesize.x, tilesize.y };
assert(n <= MAX_DRAWPOLY_VERTS);
int j = 0;
float px[8], py[8], dd[8], uu[8], vv[8];
for (bssize_t i=0; i<n; ++i)
{
px[j] = dpxy[i].x;
py[j] = dpxy[i].y;
dd[j] = (dpxy[i].x * xtex.d + dpxy[i].y * ytex.d + otex.d);
if (dd[j] <= 0.f) // invalid polygon
return;
uu[j] = (dpxy[i].x * xtex.u + dpxy[i].y * ytex.u + otex.u);
vv[j] = (dpxy[i].x * xtex.v + dpxy[i].y * ytex.v + otex.v);
j++;
}
while ((j >= 3) && (px[j-1] == px[0]) && (py[j-1] == py[0])) j--;
if (j < 3)
return;
int const npoints = j;
float usub = 0;
float vsub = 0;
// This only takes effect for textures with their default set to SamplerClampXY.
int sampleroverride = CLAMP_NONE;
if (method & DAMETH_CLAMPED)
{
if (!drawpoly_srepeat) sampleroverride |= CLAMP_Y;
if (!drawpoly_trepeat) sampleroverride |= CLAMP_X;
}
int palid = TRANSLATION(Translation_Remap + curbasepal, globalpal);
GLInterface.SetFade(globalfloorpal);
bool success = GLInterface.SetTexture(globalskytex? globalskytex : tileGetTexture(globalpicnum), globalskytex? 0 : palid, sampleroverride);
if (!success)
{
tsiz.x = tsiz.y = 1;
GLInterface.SetColorMask(false); //Hack to update Z-buffer for invalid mirror textures
}
GLInterface.SetShade(globalshade, numshades);
if ((method & DAMETH_WALL) != 0)
{
int32_t size = tilesize.y;
int32_t size2;
for (size2 = 1; size2 < size; size2 += size2) {}
if (size == size2)
GLInterface.SetNpotEmulation(0.f, 0.f);
else
{
float xOffset = 1.f / tilesize.x;
GLInterface.SetNpotEmulation((1.f*size2) / size, xOffset);
}
}
else
{
GLInterface.SetNpotEmulation(0.f, 0.f);
}
vec2_t tsiz2 = tsiz;
if (method & DAMETH_MASKPROPS)
{
SetRenderStyleFromBlend((method & DAMETH_MASKPROPS) > DAMETH_MASK, drawpoly_blend, (method & DAMETH_MASKPROPS) == DAMETH_TRANS2);
}
if (!(method & (DAMETH_CLAMPED | DAMETH_MASKPROPS)))
GLInterface.SetTextureMode(TM_OPAQUE);
float pc[4];
// The shade rgb from the tint is ignored here.
pc[0] = (float)globalr * (1.f / 255.f);
pc[1] = (float)globalg * (1.f / 255.f);
pc[2] = (float)globalb * (1.f / 255.f);
pc[3] = GetAlphaFromBlend(method & DAMETH_MASKPROPS, drawpoly_blend) * (1.f - drawpoly_alpha);
if (skyzbufferhack_pass)
pc[3] = 0.01f;
GLInterface.SetColor(pc[0], pc[1], pc[2], pc[3]);
vec2f_t const scale = { 1.f / tsiz2.x, 1.f / tsiz2.y };
auto data = screen->mVertexData->AllocVertices(npoints);
auto vt = data.first;
for (bssize_t i = 0; i < npoints; ++i, vt++)
{
float const r = 1.f / dd[i];
if (tileGetTexture(globalpicnum)->GetTexture()->isHardwareCanvas())
{
//update texcoords, canvas textures are upside down!
vt->SetTexCoord(
uu[i] * r * scale.x - usub,
1.f - (vv[i] * r * scale.y - vsub));
}
else
{
//update texcoords
vt->SetTexCoord(
uu[i] * r * scale.x - usub,
vv[i] * r * scale.y - vsub);
}
//update verts
vt->SetVertex(
(px[i] - ghalfx) * r * grhalfxdown10x * 1024.f,
(ghalfy - py[i]) * r * grhalfxdown10 * 1024.f,
r);
}
GLInterface.Draw(DT_TriangleFan, data.second, npoints);
GLInterface.SetNpotEmulation(0.f, 0.f);
GLInterface.SetTextureMode(TM_NORMAL);
if (skyzbufferhack && skyzbufferhack_pass == 0)
{
vec3d_t const bxtex = xtex, bytex = ytex, botex = otex;
xtex = xtex2, ytex = ytex2, otex = otex2;
GLInterface.SetColorMask(false);
GLInterface.Draw(DT_TriangleFan, data.second, npoints);
GLInterface.SetColorMask(true);
xtex = bxtex, ytex = bytex, otex = botex;
}
if (!success)
GLInterface.SetColorMask(true);
}
static inline void vsp_finalize_init(int32_t const vcnt)
{
for (bssize_t i=0; i<vcnt; ++i)
{
vsp[i].cy[1] = vsp[i+1].cy[0]; vsp[i].ctag = i;
vsp[i].fy[1] = vsp[i+1].fy[0]; vsp[i].ftag = i;
vsp[i].n = i+1; vsp[i].p = i-1;
// vsp[i].tag = -1;
}
vsp[vcnt-1].n = 0; vsp[0].p = vcnt-1;
//VSPMAX-1 is dummy empty node
for (bssize_t i=vcnt; i<VSPMAX; i++) { vsp[i].n = i+1; vsp[i].p = i-1; }
vsp[VSPMAX-1].n = vcnt; vsp[vcnt].p = VSPMAX-1;
}
#define COMBINE_STRIPS
#ifdef COMBINE_STRIPS
static inline void vsdel(int const i)
{
//Delete i
int const pi = vsp[i].p;
int const ni = vsp[i].n;
vsp[ni].p = pi;
vsp[pi].n = ni;
//Add i to empty list
vsp[i].n = vsp[VSPMAX-1].n;
vsp[i].p = VSPMAX-1;
vsp[vsp[VSPMAX-1].n].p = i;
vsp[VSPMAX-1].n = i;
}
static inline void vsmerge(int const i, int const ni)
{
vsp[i].cy[1] = vsp[ni].cy[1];
vsp[i].fy[1] = vsp[ni].fy[1];
vsdel(ni);
}
#endif
static inline int32_t vsinsaft(int const i)
{
//i = next element from empty list
int32_t const r = vsp[VSPMAX-1].n;
vsp[vsp[r].n].p = VSPMAX-1;
vsp[VSPMAX-1].n = vsp[r].n;
vsp[r] = vsp[i]; //copy i to r
//insert r after i
vsp[r].p = i; vsp[r].n = vsp[i].n;
vsp[vsp[i].n].p = r; vsp[i].n = r;
return r;
}
static int32_t domostpolymethod = DAMETH_NOMASK;
#define DOMOST_OFFSET .01f
static void polymost_clipmost(vec2f_t *dpxy, int &n, float x0, float x1, float y0top, float y0bot, float y1top, float y1bot)
{
if (y0bot < y0top || y1bot < y1top)
return;
//Clip to (x0,y0top)-(x1,y1top)
vec2f_t dp2[8];
float t0, t1;
int n2 = 0;
t1 = -((dpxy[0].x - x0) * (y1top - y0top) - (dpxy[0].y - y0top) * (x1 - x0));
for (bssize_t i=0; i<n; i++)
{
int j = i + 1;
if (j >= n)
j = 0;
t0 = t1;
t1 = -((dpxy[j].x - x0) * (y1top - y0top) - (dpxy[j].y - y0top) * (x1 - x0));
if (t0 >= 0)
dp2[n2++] = dpxy[i];
if ((t0 >= 0) != (t1 >= 0) && (t0 <= 0) != (t1 <= 0))
{
float const r = t0 / (t0 - t1);
dp2[n2] = { (dpxy[j].x - dpxy[i].x) * r + dpxy[i].x,
(dpxy[j].y - dpxy[i].y) * r + dpxy[i].y };
n2++;
}
}
if (n2 < 3)
{
n = 0;
return;
}
//Clip to (x1,y1bot)-(x0,y0bot)
t1 = -((dp2[0].x - x1) * (y0bot - y1bot) - (dp2[0].y - y1bot) * (x0 - x1));
n = 0;
for (bssize_t i = 0, j = 1; i < n2; j = ++i + 1)
{
if (j >= n2)
j = 0;
t0 = t1;
t1 = -((dp2[j].x - x1) * (y0bot - y1bot) - (dp2[j].y - y1bot) * (x0 - x1));
if (t0 >= 0)
dpxy[n++] = dp2[i];
if ((t0 >= 0) != (t1 >= 0) && (t0 <= 0) != (t1 <= 0))
{
float const r = t0 / (t0 - t1);
dpxy[n] = { (dp2[j].x - dp2[i].x) * r + dp2[i].x,
(dp2[j].y - dp2[i].y) * r + dp2[i].y };
n++;
}
}
if (n < 3)
{
n = 0;
return;
}
}
static void polymost_domost(float x0, float y0, float x1, float y1, float y0top = 0.f, float y0bot = -1.f, float y1top = 0.f, float y1bot = -1.f)
{
int const dir = (x0 < x1);
polymost_outputGLDebugMessage(3, "polymost_domost(x0:%f, y0:%f, x1:%f, y1:%f, y0top:%f, y0bot:%f, y1top:%f, y1bot:%f)",
x0, y0, x1, y1, y0top, y0bot, y1top, y1bot);
y0top -= DOMOST_OFFSET;
y1top -= DOMOST_OFFSET;
y0bot += DOMOST_OFFSET;
y1bot += DOMOST_OFFSET;
if (dir) //clip dmost (floor)
{
y0 -= DOMOST_OFFSET;
y1 -= DOMOST_OFFSET;
}
else //clip umost (ceiling)
{
if (x0 == x1) return;
std::swap(x0, x1);
std::swap(y0, y1);
std::swap(y0top, y1top);
std::swap(y0bot, y1bot);
y0 += DOMOST_OFFSET;
y1 += DOMOST_OFFSET; //necessary?
}
// Test if span is outside screen bounds
if (x1 < xbl || x0 > xbr)
{
domost_rejectcount++;
return;
}
vec2f_t dm0 = { x0 - DOMOST_OFFSET, y0 };
vec2f_t dm1 = { x1 + DOMOST_OFFSET, y1 };
float const slop = (dm1.y - dm0.y) / (dm1.x - dm0.x);
if (dm0.x < xbl)
{
dm0.y += slop*(xbl-dm0.x);
dm0.x = xbl;
}
if (dm1.x > xbr)
{
dm1.y += slop*(xbr-dm1.x);
dm1.x = xbr;
}
drawpoly_alpha = 0.f;
drawpoly_blend = 0;
vec2f_t n0, n1;
float spx[4];
int32_t spt[4];
int firstnode = vsp[0].n;
for (bssize_t newi, i=vsp[0].n; i; i=newi)
{
newi = vsp[i].n; n0.x = vsp[i].x; n1.x = vsp[newi].x;
if (dm0.x >= n1.x)
{
firstnode = i;
continue;
}
if (n0.x >= dm1.x)
break;
if (vsp[i].ctag <= 0) continue;
float const dx = n1.x-n0.x;
float const cy[2] = { vsp[i].cy[0], vsp[i].fy[0] },
cv[2] = { vsp[i].cy[1]-cy[0], vsp[i].fy[1]-cy[1] };
int scnt = 0;
//Test if left edge requires split (dm0.x,dm0.y) (nx0,cy(0)),<dx,cv(0)>
if ((dm0.x > n0.x) && (dm0.x < n1.x))
{
float const t = (dm0.x-n0.x)*cv[dir] - (dm0.y-cy[dir])*dx;
if (((!dir) && (t < 0.f)) || ((dir) && (t > 0.f)))
{ spx[scnt] = dm0.x; spt[scnt] = -1; scnt++; }
}
//Test for intersection on umost (0) and dmost (1)
float const d[2] = { ((dm0.y - dm1.y) * dx) - ((dm0.x - dm1.x) * cv[0]),
((dm0.y - dm1.y) * dx) - ((dm0.x - dm1.x) * cv[1]) };
float const n[2] = { ((dm0.y - cy[0]) * dx) - ((dm0.x - n0.x) * cv[0]),
((dm0.y - cy[1]) * dx) - ((dm0.x - n0.x) * cv[1]) };
float const fnx[2] = { dm0.x + ((n[0] / d[0]) * (dm1.x - dm0.x)),
dm0.x + ((n[1] / d[1]) * (dm1.x - dm0.x)) };
if ((fabsf(d[0]) > fabsf(n[0])) && (d[0] * n[0] >= 0.f) && (fnx[0] > n0.x) && (fnx[0] < n1.x))
spx[scnt] = fnx[0], spt[scnt++] = 0;
if ((fabsf(d[1]) > fabsf(n[1])) && (d[1] * n[1] >= 0.f) && (fnx[1] > n0.x) && (fnx[1] < n1.x))
spx[scnt] = fnx[1], spt[scnt++] = 1;
//Nice hack to avoid full sort later :)
if ((scnt >= 2) && (spx[scnt-1] < spx[scnt-2]))
{
std::swap(spx[scnt-1], spx[scnt-2]);
std::swap(spt[scnt-1], spt[scnt-2]);
}
//Test if right edge requires split
if ((dm1.x > n0.x) && (dm1.x < n1.x))
{
float const t = (dm1.x-n0.x)*cv[dir] - (dm1.y-cy[dir])*dx;
if (((!dir) && (t < 0.f)) || ((dir) && (t > 0.f)))
{ spx[scnt] = dm1.x; spt[scnt] = -1; scnt++; }
}
vsp[i].tag = vsp[newi].tag = -1;
float const rdx = 1.f/dx;
for (bssize_t z=0, vcnt=0; z<=scnt; z++,i=vcnt)
{
float t;
if (z == scnt)
goto skip;
t = (spx[z]-n0.x)*rdx;
vcnt = vsinsaft(i);
vsp[i].cy[1] = t*cv[0] + cy[0];
vsp[i].fy[1] = t*cv[1] + cy[1];
vsp[vcnt].x = spx[z];
vsp[vcnt].cy[0] = vsp[i].cy[1];
vsp[vcnt].fy[0] = vsp[i].fy[1];
vsp[vcnt].tag = spt[z];
skip: ;
int32_t const ni = vsp[i].n; if (!ni) continue; //this 'if' fixes many bugs!
float const dx0 = vsp[i].x; if (dm0.x > dx0) continue;
float const dx1 = vsp[ni].x; if (dm1.x < dx1) continue;
n0.y = (dx0-dm0.x)*slop + dm0.y;
n1.y = (dx1-dm0.x)*slop + dm0.y;
// dx0 dx1
// ~ ~
//----------------------------
// t0+=0 t1+=0
// vsp[i].cy[0] vsp[i].cy[1]
//============================
// t0+=1 t1+=3
//============================
// vsp[i].fy[0] vsp[i].fy[1]
// t0+=2 t1+=6
//
// ny0 ? ny1 ?
int k = 4;
if ((vsp[i].tag == 0) || (n0.y <= vsp[i].cy[0]+DOMOST_OFFSET)) k--;
if ((vsp[i].tag == 1) || (n0.y >= vsp[i].fy[0]-DOMOST_OFFSET)) k++;
if ((vsp[ni].tag == 0) || (n1.y <= vsp[i].cy[1]+DOMOST_OFFSET)) k -= 3;
if ((vsp[ni].tag == 1) || (n1.y >= vsp[i].fy[1]-DOMOST_OFFSET)) k += 3;
if (!dir)
{
switch (k)
{
case 4:
case 5:
case 7:
{
vec2f_t dpxy[8] = {
{ dx0, vsp[i].cy[0] }, { dx1, vsp[i].cy[1] }, { dx1, n1.y }, { dx0, n0.y }
};
int n = 4;
polymost_clipmost(dpxy, n, x0, x1, y0top, y0bot, y1top, y1bot);
polymost_drawpoly(dpxy, n, domostpolymethod, tileSize(globalpicnum));
vsp[i].cy[0] = n0.y;
vsp[i].cy[1] = n1.y;
vsp[i].ctag = gtag;
}
break;
case 1:
case 2:
{
vec2f_t dpxy[8] = { { dx0, vsp[i].cy[0] }, { dx1, vsp[i].cy[1] }, { dx0, n0.y } };
int n = 3;
polymost_clipmost(dpxy, n, x0, x1, y0top, y0bot, y1top, y1bot);
polymost_drawpoly(dpxy, n, domostpolymethod, tileSize(globalpicnum));
vsp[i].cy[0] = n0.y;
vsp[i].ctag = gtag;
}
break;
case 3:
case 6:
{
vec2f_t dpxy[8] = { { dx0, vsp[i].cy[0] }, { dx1, vsp[i].cy[1] }, { dx1, n1.y } };
int n = 3;
polymost_clipmost(dpxy, n, x0, x1, y0top, y0bot, y1top, y1bot);
polymost_drawpoly(dpxy, n, domostpolymethod, tileSize(globalpicnum));
vsp[i].cy[1] = n1.y;
vsp[i].ctag = gtag;
}
break;
case 8:
{
vec2f_t dpxy[8] = {
{ dx0, vsp[i].cy[0] }, { dx1, vsp[i].cy[1] }, { dx1, vsp[i].fy[1] }, { dx0, vsp[i].fy[0] }
};
int n = 4;
polymost_clipmost(dpxy, n, x0, x1, y0top, y0bot, y1top, y1bot);
polymost_drawpoly(dpxy, n, domostpolymethod, tileSize(globalpicnum));
vsp[i].ctag = vsp[i].ftag = -1;
}
default: break;
}
}
else
{
switch (k)
{
case 4:
case 3:
case 1:
{
vec2f_t dpxy[8] = {
{ dx0, n0.y }, { dx1, n1.y }, { dx1, vsp[i].fy[1] }, { dx0, vsp[i].fy[0] }
};
int n = 4;
polymost_clipmost(dpxy, n, x0, x1, y0top, y0bot, y1top, y1bot);
polymost_drawpoly(dpxy, n, domostpolymethod, tileSize(globalpicnum));
vsp[i].fy[0] = n0.y;
vsp[i].fy[1] = n1.y;
vsp[i].ftag = gtag;
}
break;
case 7:
case 6:
{
vec2f_t dpxy[8] = { { dx0, n0.y }, { dx1, vsp[i].fy[1] }, { dx0, vsp[i].fy[0] } };
int n = 3;
polymost_clipmost(dpxy, n, x0, x1, y0top, y0bot, y1top, y1bot);
polymost_drawpoly(dpxy, n, domostpolymethod, tileSize(globalpicnum));
vsp[i].fy[0] = n0.y;
vsp[i].ftag = gtag;
}
break;
case 5:
case 2:
{
vec2f_t dpxy[8] = { { dx0, vsp[i].fy[0] }, { dx1, n1.y }, { dx1, vsp[i].fy[1] } };
int n = 3;
polymost_clipmost(dpxy, n, x0, x1, y0top, y0bot, y1top, y1bot);
polymost_drawpoly(dpxy, n, domostpolymethod, tileSize(globalpicnum));
vsp[i].fy[1] = n1.y;
vsp[i].ftag = gtag;
}
break;
case 0:
{
vec2f_t dpxy[8] = { { dx0, vsp[i].cy[0] }, { dx1, vsp[i].cy[1] }, { dx1, vsp[i].fy[1] }, { dx0, vsp[i].fy[0] } };
int n = 4;
polymost_clipmost(dpxy, n, x0, x1, y0top, y0bot, y1top, y1bot);
polymost_drawpoly(dpxy, n, domostpolymethod, tileSize(globalpicnum));
vsp[i].ctag = vsp[i].ftag = -1;
}
default:
break;
}
}
}
}
gtag++;
//Combine neighboring vertical strips with matching collinear top&bottom edges
//This prevents x-splits from propagating through the entire scan
#ifdef COMBINE_STRIPS
int i = firstnode;
do
{
if (vsp[i].x >= dm1.x)
break;
if ((vsp[i].cy[0]+DOMOST_OFFSET*2 >= vsp[i].fy[0]) && (vsp[i].cy[1]+DOMOST_OFFSET*2 >= vsp[i].fy[1]))
vsp[i].ctag = vsp[i].ftag = -1;
int const ni = vsp[i].n;
//POGO: specially treat the viewport nodes so that we will never end up in a situation where we accidentally access the sentinel node
if (ni >= viewportNodeCount)
{
if ((vsp[i].ctag == vsp[ni].ctag) && (vsp[i].ftag == vsp[ni].ftag))
{
vsmerge(i, ni);
continue;
}
if (vsp[ni].x - vsp[i].x < DOMOST_OFFSET)
{
vsp[i].x = vsp[ni].x;
vsp[i].cy[0] = vsp[ni].cy[0];
vsp[i].fy[0] = vsp[ni].fy[0];
vsp[i].ctag = vsp[ni].ctag;
vsp[i].ftag = vsp[ni].ftag;
vsmerge(i, ni);
continue;
}
}
i = ni;
}
while (i);
#endif
}
// variables that are set to ceiling- or floor-members, depending
// on which one is processed right now
static int32_t global_cf_z;
static float global_cf_xpanning, global_cf_ypanning, global_cf_heinum;
static int32_t global_cf_shade, global_cf_pal, global_cf_fogpal;
static float (*global_getzofslope_func)(usectorptr_t, float, float);
static void polymost_internal_nonparallaxed(vec2f_t n0, vec2f_t n1, float ryp0, float ryp1, float x0, float x1,
float y0, float y1, int32_t sectnum)
{
int const have_floor = sectnum & MAXSECTORS;
sectnum &= ~MAXSECTORS;
auto const sec = (usectorptr_t)§or[sectnum];
// comments from floor code:
//(singlobalang/-16384*(sx-ghalfx) + 0*(sy-ghoriz) + (cosviewingrangeglobalang/16384)*ghalfx)*d + globalposx = u*16
//(cosglobalang/ 16384*(sx-ghalfx) + 0*(sy-ghoriz) + (sinviewingrangeglobalang/16384)*ghalfx)*d + globalposy = v*16
//( 0*(sx-ghalfx) + 1*(sy-ghoriz) + ( 0)*ghalfx)*d + globalposz/16 = (sec->floorz/16)
float ft[4] = { fglobalposx, fglobalposy, fcosglobalang, fsinglobalang };
polymost_outputGLDebugMessage(3, "polymost_internal_nonparallaxed(n0:{x:%f, y:%f}, n1:{x:%f, y:%f}, ryp0:%f, ryp1:%f, x0:%f, x1:%f, y0:%f, y1:%f, sectnum:%d)",
n0.x, n0.y, n1.x, n1.y, ryp0, ryp1, x0, x1, y0, y1, sectnum);
if (globalorientation & 64)