/
hw_portal.cpp
977 lines (816 loc) · 28.2 KB
/
hw_portal.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
//
//---------------------------------------------------------------------------
//
// Copyright(C) 2004-2018 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 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 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/
//
//--------------------------------------------------------------------------
//
/*
** hw_portal.cpp
** portal maintenance classes for skyboxes, horizons etc. (API independent parts)
**
*/
#include "c_dispatch.h"
#include "hw_portal.h"
#include "hw_clipper.h"
#include "hw_renderstate.h"
#include "flatvertices.h"
#include "hw_clock.h"
#include "texturemanager.h"
#include "gamestruct.h"
EXTERN_CVAR(Int, r_mirror_recursions)
EXTERN_CVAR(Bool, gl_portals)
//-----------------------------------------------------------------------------
//
// StartFrame
//
//-----------------------------------------------------------------------------
void FPortalSceneState::StartFrame()
{
if (renderdepth == 0)
{
inskybox = false;
screen->instack[plane_floor] = screen->instack[plane_ceiling] = 0;
}
renderdepth++;
}
//-----------------------------------------------------------------------------
//
// printing portal info
//
//-----------------------------------------------------------------------------
static bool gl_portalinfo;
CCMD(gl_portalinfo)
{
gl_portalinfo = true;
}
static FString indent;
FPortalSceneState portalState;
//-----------------------------------------------------------------------------
//
// EndFrame
//
//-----------------------------------------------------------------------------
void FPortalSceneState::EndFrame(HWDrawInfo *di, FRenderState &state)
{
HWPortal * p;
if (gl_portalinfo)
{
Printf("%s%d portals, depth = %d\n%s{\n", indent.GetChars(), di->Portals.Size(), renderdepth, indent.GetChars());
indent += " ";
}
while (di->Portals.Pop(p) && p)
{
if (gl_portalinfo)
{
Printf("%sProcessing %s, depth = %d\n", indent.GetChars(), p->GetName(), renderdepth);
}
if (p->lines.Size() > 0)
{
RenderPortal(p, state, true, di);
}
delete p;
}
renderdepth--;
if (gl_portalinfo)
{
indent.Truncate(long(indent.Len()-2));
Printf("%s}\n", indent.GetChars());
if (indent.Len() == 0) gl_portalinfo = false;
}
}
//-----------------------------------------------------------------------------
//
// Renders one sky portal without a stencil. Only useful if this is the only portal in view.
//
//-----------------------------------------------------------------------------
bool FPortalSceneState::RenderFirstSkyPortal(int recursion, HWDrawInfo *outer_di, FRenderState &state)
{
HWPortal* best = nullptr;
unsigned bestindex = 0;
if (recursion > 0 || outer_di->Portals.Size() != 1 || !outer_di->Portals[0]->IsSky()) return false;
best = outer_di->Portals[0];
outer_di->Portals.Clear();
RenderPortal(best, state, false, outer_di);
delete best;
return true;
}
void FPortalSceneState::RenderPortal(HWPortal *p, FRenderState &state, bool usestencil, HWDrawInfo *outer_di)
{
if (gl_portals) outer_di->RenderPortal(p, state, usestencil);
}
//-----------------------------------------------------------------------------
//
// DrawPortalStencil
//
//-----------------------------------------------------------------------------
void HWPortal::DrawPortalStencil(FRenderState &state, int pass)
{
if (mPrimIndices.Size() == 0)
{
mPrimIndices.Resize(2 * lines.Size());
for (unsigned int i = 0; i < lines.Size(); i++)
{
mPrimIndices[i * 2] = lines[i].vertindex;
mPrimIndices[i * 2 + 1] = lines[i].vertcount;
}
if (NeedCap() && lines.Size() > 1 && planesused != 0)
{
screen->mVertexData->Map();
if (planesused & (1 << plane_floor))
{
auto verts = screen->mVertexData->AllocVertices(4);
auto ptr = verts.first;
ptr[0].Set((float)boundingBox.left, -32767.f, (float)boundingBox.top, 0, 0);
ptr[1].Set((float)boundingBox.right, -32767.f, (float)boundingBox.top, 0, 0);
ptr[2].Set((float)boundingBox.left, -32767.f, (float)boundingBox.bottom, 0, 0);
ptr[3].Set((float)boundingBox.right, -32767.f, (float)boundingBox.bottom, 0, 0);
mBottomCap = verts.second;
}
if (planesused & (1 << plane_ceiling))
{
auto verts = screen->mVertexData->AllocVertices(4);
auto ptr = verts.first;
ptr[0].Set((float)boundingBox.left, 32767.f, (float)boundingBox.top, 0, 0);
ptr[1].Set((float)boundingBox.right, 32767.f, (float)boundingBox.top, 0, 0);
ptr[2].Set((float)boundingBox.left, 32767.f, (float)boundingBox.bottom, 0, 0);
ptr[3].Set((float)boundingBox.right, 32767.f, (float)boundingBox.bottom, 0, 0);
mTopCap = verts.second;
}
screen->mVertexData->Unmap();
}
}
for (unsigned int i = 0; i < mPrimIndices.Size(); i += 2)
{
state.Draw(DT_TriangleFan, mPrimIndices[i], mPrimIndices[i + 1], i == 0);
}
if (NeedCap() && lines.Size() > 1)
{
// The cap's depth handling needs special treatment so that it won't block further portal caps.
if (pass == STP_DepthRestore) state.SetDepthRange(1, 1);
if (mBottomCap != ~0u)
{
state.Draw(DT_TriangleStrip, mBottomCap, 4, false);
}
if (mTopCap != ~0u)
{
state.Draw(DT_TriangleStrip, mTopCap, 4, false);
}
if (pass == STP_DepthRestore) state.SetDepthRange(0, 1);
}
}
//-----------------------------------------------------------------------------
//
// Start
//
//-----------------------------------------------------------------------------
void HWPortal::SetupStencil(HWDrawInfo *di, FRenderState &state, bool usestencil)
{
Clocker c(PortalAll);
rendered_portals++;
if (usestencil)
{
// Create stencil
state.SetStencil(0, SOP_Increment); // create stencil, increment stencil of valid pixels
state.SetColorMask(false);
state.SetEffect(EFF_STENCIL);
state.EnableTexture(false);
state.ResetColor();
state.SetDepthFunc(DF_Less);
if (NeedDepthBuffer())
{
state.SetDepthMask(false); // don't write to Z-buffer!
DrawPortalStencil(state, STP_Stencil);
// Clear Z-buffer
state.SetStencil(1, SOP_Keep); // draw sky into stencil. This stage doesn't modify the stencil.
state.SetDepthMask(true); // enable z-buffer again
state.SetDepthRange(1, 1);
state.SetDepthFunc(DF_Always);
DrawPortalStencil(state, STP_DepthClear);
// set normal drawing mode
state.EnableTexture(true);
state.SetDepthRange(0, 1);
state.SetDepthFunc(DF_Less);
state.SetColorMask(true);
state.SetEffect(EFF_NONE);
}
else
{
// No z-buffer is needed therefore we can skip all the complicated stuff that is involved
// Note: We must draw the stencil with z-write enabled here because there is no second pass!
state.SetDepthMask(true);
DrawPortalStencil(state, STP_AllInOne);
state.SetStencil(1, SOP_Keep); // draw sky into stencil. This stage doesn't modify the stencil.
state.EnableTexture(true);
state.SetColorMask(true);
state.SetEffect(EFF_NONE);
state.EnableDepthTest(false);
state.SetDepthMask(false); // don't write to Z-buffer!
}
screen->stencilValue++;
}
else
{
if (!NeedDepthBuffer())
{
state.SetDepthMask(false);
state.EnableDepthTest(false);
}
}
// save viewpoint
//savedvisibility = di->Viewpoint.
}
//-----------------------------------------------------------------------------
//
// End
//
//-----------------------------------------------------------------------------
void HWPortal::RemoveStencil(HWDrawInfo *di, FRenderState &state, bool usestencil)
{
Clocker c(PortalAll);
bool needdepth = NeedDepthBuffer();
// Restore the old view
auto &vp = di->Viewpoint;
//if (vp.camera != nullptr) vp.camera->renderflags = (vp.camera->renderflags & ~RF_MAYBEINVISIBLE) | savedvisibility;
if (usestencil)
{
state.SetColorMask(false); // no graphics
state.SetEffect(EFF_NONE);
state.ResetColor();
state.EnableTexture(false);
if (needdepth)
{
// first step: reset the depth buffer to max. depth
state.SetDepthRange(1, 1); // always
state.SetDepthFunc(DF_Always); // write the farthest depth value
DrawPortalStencil(state, STP_DepthClear);
}
else
{
state.EnableDepthTest(true);
}
// second step: restore the depth buffer to the previous values and reset the stencil
state.SetDepthFunc(DF_LEqual);
state.SetDepthRange(0, 1);
state.SetStencil(0, SOP_Decrement);
DrawPortalStencil(state, STP_DepthRestore);
state.SetDepthFunc(DF_Less);
state.EnableTexture(true);
state.SetEffect(EFF_NONE);
state.SetColorMask(true);
screen->stencilValue--;
// restore old stencil op.
state.SetStencil(0, SOP_Keep);
}
else
{
if (needdepth)
{
state.Clear(CT_Depth);
}
else
{
state.EnableDepthTest(true);
state.SetDepthMask(true);
}
// This draws a valid z-buffer into the stencil's contents to ensure it
// doesn't get overwritten by the level's geometry.
state.ResetColor();
state.SetDepthFunc(DF_LEqual);
state.SetDepthRange(0, 1);
state.SetColorMask(0, 0, 0, 1); // mark portal in alpha channel but don't touch color
state.SetEffect(EFF_STENCIL);
state.EnableTexture(false);
state.SetRenderStyle(STYLE_Source);
DrawPortalStencil(state, STP_DepthRestore);
state.SetEffect(EFF_NONE);
state.EnableTexture(true);
state.SetColorMask(true);
state.SetDepthFunc(DF_Less);
}
}
//-----------------------------------------------------------------------------
//
//
//
//-----------------------------------------------------------------------------
void HWScenePortalBase::DrawContents(HWDrawInfo* di, FRenderState& state)
{
if (Setup(di, state, di->mClipper))
{
auto type = GetType();
gi->EnterPortal(di->Viewpoint.CameraSprite, type);
di->DrawScene(DM_PORTAL, type == PORTAL_SECTOR_CEILING);
gi->LeavePortal(di->Viewpoint.CameraSprite, type);
Shutdown(di, state);
}
else state.ClearScreen();
}
//-----------------------------------------------------------------------------
//
//
//
//-----------------------------------------------------------------------------
void HWScenePortalBase::ClearClipper(HWDrawInfo *di, Clipper *clipper)
{
clipper->SetVisibleRange(di->Viewpoint.RotAngle, di->FrustumAngle());
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//
//
// Common code for line to line and mirror portals
//
//
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
inline int P_GetLineSide(const DVector2& pos, const walltype* line)
{
auto delta = WallDelta(line);
double v = (pos.Y - WallStartY(line) * delta.X + WallStartX(line) - pos.X) * delta.Y;
return v < -1. / 65536. ? -1 : v > 1. / 65536 ? 1 : 0;
}
bool P_ClipLineToPortal(walltype* line, walltype* portal, DVector2 view)
{
int behind1 = P_GetLineSide(WallStart(line), portal);
int behind2 = P_GetLineSide(WallEnd(line), portal);
if (behind1 == 0 && behind2 == 0)
{
// The line is parallel to the portal and cannot possibly be visible.
return true;
}
// If one point lies on the same straight line than the portal, the other vertex will determine sidedness alone.
else if (behind2 == 0) behind2 = behind1;
else if (behind1 == 0) behind1 = behind2;
if (behind1 > 0 && behind2 > 0)
{
// The line is behind the portal, i.e. between viewer and portal line, and must be rejected
return true;
}
else if (behind1 < 0 && behind2 < 0)
{
// The line is in front of the portal, i.e. the portal is between viewer and line. This line must not be rejected
return false;
}
else
{
// The line intersects with the portal straight, so we need to do another check to see how both ends of the portal lie in relation to the viewer.
int viewside = P_GetLineSide(view, line);
int p1side = P_GetLineSide(WallStart(portal), line);
int p2side = P_GetLineSide(WallEnd(portal), line);
// Do the same handling of points on the portal straight as above.
if (p1side == 0) p1side = p2side;
else if (p2side == 0) p2side = p1side;
// If the portal is on the other side of the line than the viewpoint, there is no possibility to see this line inside the portal.
return (p1side == p2side && viewside != p1side);
}
}
int HWLinePortal::ClipSeg(walltype *seg, const DVector3 &viewpos)
{
return P_ClipLineToPortal(seg, line, viewpos) ? PClip_InFront : PClip_Inside;
}
int HWLinePortal::ClipSector(sectortype *sub)
{
// this seg is completely behind the mirror
for (unsigned int i = 0; i<sub->wallnum; i++)
{
if (PointOnLineSide(WallStart(&wall[sub->wallptr]), line) == 0) return PClip_Inside;
}
return PClip_InFront;
}
int HWLinePortal::ClipPoint(const DVector2 &pos)
{
if (PointOnLineSide(pos, line))
{
return PClip_InFront;
}
return PClip_Inside;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//
//
// Mirror Portal
//
//
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//
//
//
//-----------------------------------------------------------------------------
bool HWMirrorPortal::Setup(HWDrawInfo *di, FRenderState &rstate, Clipper *clipper)
{
auto state = mState;
if (state->renderdepth > r_mirror_recursions)
{
return false;
}
auto &vp = di->Viewpoint;
di->mClipPortal = this;
int x = line->x;
int y = line->y;
int dx = wall[line->point2].x - x;
int dy = wall[line->point2].y - y;
// this can overflow so use 64 bit math.
const int64_t j = int64_t(dx) * dx + int64_t(dy) * dy;
if (j == 0)
return false;
vec2_t view = { int(vp.Pos.X * 16), int(vp.Pos.Y * -16) };
int64_t i = ((int64_t(view.x) - x) * dx + (int64_t(view.y) - y) * dy) << 1;
int newx = int((x << 1) + Scale(dx, i, j) - view.x);
int newy = int((y << 1) + Scale(dy, i, j) - view.y);
auto myan = bvectangbam(dx, dy);
auto newan = myan + myan - bamang(vp.RotAngle);
vp.RotAngle = newan.asbam();
vp.SectNums = nullptr;
vp.SectCount = line->sector;
vp.Pos.X = newx / 16.f;
vp.Pos.Y = newy / -16.f;
vp.HWAngles.Yaw = -90.f + newan.asdeg();
double FocalTangent = tan(vp.FieldOfView.Radians() / 2);
DAngle an = 270. - vp.HWAngles.Yaw.Degrees;
vp.TanSin = FocalTangent * an.Sin();
vp.TanCos = FocalTangent * an.Cos();
vp.ViewVector = an.ToVector();
state->MirrorFlag++;
di->SetClipLine(line);
di->SetupView(rstate, vp.Pos.X, vp.Pos.Y, vp.Pos.Z, !!(state->MirrorFlag & 1), !!(state->PlaneMirrorFlag & 1));
ClearClipper(di, clipper);
auto startan = bvectangbam(line->x - newx, line->y - newy);
auto endan = bvectangbam(wall[line->point2].x - newx, wall[line->point2].y - newy);
clipper->RestrictVisibleRange(endan, startan); // we check the line from the backside so angles are reversed.
return true;
}
void HWMirrorPortal::Shutdown(HWDrawInfo *di, FRenderState &rstate)
{
mState->MirrorFlag--;
}
const char *HWMirrorPortal::GetName() { return "Mirror"; }
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//
//
// Line to line Portal
//
//
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//
// only used by Blood - they can not change the angle.
//
//-----------------------------------------------------------------------------
bool HWLineToLinePortal::Setup(HWDrawInfo *di, FRenderState &rstate, Clipper *clipper)
{
// TODO: Handle recursion more intelligently
auto &state = mState;
if (state->renderdepth>r_mirror_recursions)
{
return false;
}
auto &vp = di->Viewpoint;
di->mClipPortal = this;
auto srccenter = (WallStart(origin) + WallEnd(origin)) / 2;
auto destcenter = (WallStart(line) + WallEnd(line)) / 2;
DVector2 npos = vp.Pos - srccenter + destcenter;
#if 0 // Blood does not rotate these. Needs map checking to make sure it can be added.
int dx = wall[origin->point2].x - origin->x;
int dy = wall[origin->point2].y - origin->y;
int dx2 = wall[line->point2].x - line->x;
int dy2 = wall[line->point2].y - line->y;
auto srcang = bvectangbam(dx, dy);
auto destang = bvectangbam(-dx, -dy);
vp.RotAngle += (destang - srcang).asbam();
#endif
// Nothing in the entire setup mandates that both lines have the same length.
// So we need to calculate the clip range from the origin line, not the destination, because that is what determines the visible part of the scene.
int origx = vp.Pos.X * 16;
int origy = vp.Pos.Y * -16;
vp.SectNums = nullptr;
vp.SectCount = line->sector;
vp.Pos.X = npos.X;
vp.Pos.Y = npos.Y;
di->SetClipLine(line);
di->SetupView(rstate, vp.Pos.X, vp.Pos.Y, vp.Pos.Z, !!(state->MirrorFlag & 1), !!(state->PlaneMirrorFlag & 1));
ClearClipper(di, clipper);
auto startan = bvectangbam(origin->x - origx, origin->y - origy);
auto endan = bvectangbam(wall[origin->point2].x - origx, wall[origin->point2].y - origy);
clipper->RestrictVisibleRange(startan, endan);
return true;
}
const char *HWLineToLinePortal::GetName() { return "LineToLine"; }
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//
//
// Line to sprite Portal
//
//
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//
// only used by SW.
//
//-----------------------------------------------------------------------------
bool HWLineToSpritePortal::Setup(HWDrawInfo* di, FRenderState& rstate, Clipper* clipper)
{
// TODO: Handle recursion more intelligently
auto& state = mState;
if (state->renderdepth > r_mirror_recursions)
{
return false;
}
auto& vp = di->Viewpoint;
di->mClipPortal = this;
auto srccenter = (WallStart(origin) + WallEnd(origin)) / 2;
DVector2 destcenter ={ camera->x / 16.f, camera->y / -16.f };
DVector2 npos = vp.Pos - srccenter + destcenter;
int origx = vp.Pos.X * 16;
int origy = vp.Pos.Y * -16;
vp.SectNums = nullptr;
vp.SectCount = camera->sectnum;
vp.Pos.X = npos.X;
vp.Pos.Y = npos.Y;
di->SetClipLine(line);
di->SetupView(rstate, vp.Pos.X, vp.Pos.Y, vp.Pos.Z, !!(state->MirrorFlag & 1), !!(state->PlaneMirrorFlag & 1));
ClearClipper(di, clipper);
auto startan = bvectangbam(origin->x - origx, origin->y - origy);
auto endan = bvectangbam(wall[origin->point2].x - origx, wall[origin->point2].y - origy);
clipper->RestrictVisibleRange(startan, endan);
return true;
}
const char* HWLineToSpritePortal::GetName() { return "LineToSprite"; }
#if 0 // currently none of the games has any support for this. Maybe later.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//
//
// Skybox Portal
//
//
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//
// GLSkyboxPortal::DrawContents
//
//-----------------------------------------------------------------------------
bool HWSkyboxPortal::Setup(HWDrawInfo *di, FRenderState &rstate, Clipper *clipper)
{
auto state = mState;
old_pm = state->PlaneMirrorMode;
if (mState->skyboxrecursion >= 3)
{
return false;
}
auto &vp = di->Viewpoint;
state->skyboxrecursion++;
state->PlaneMirrorMode = 0;
state->inskybox = true;
AActor *origin = portal->mSkybox;
portal->mFlags |= PORTSF_INSKYBOX;
vp.extralight = 0;
oldclamp = rstate.SetDepthClamp(false);
vp.Pos = origin->InterpolatedPosition(vp.TicFrac);
vp.ActorPos = origin->Pos();
vp.Angles.Yaw += (origin->PrevAngles.Yaw + deltaangle(origin->PrevAngles.Yaw, origin->Angles.Yaw) * vp.TicFrac);
// Don't let the viewpoint be too close to a floor or ceiling
double floorh = origin->Sector->floorplane.ZatPoint(origin->Pos());
double ceilh = origin->Sector->ceilingplane.ZatPoint(origin->Pos());
if (vp.Pos.Z < floorh + 4) vp.Pos.Z = floorh + 4;
if (vp.Pos.Z > ceilh - 4) vp.Pos.Z = ceilh - 4;
vp.ViewActor = origin;
di->SetupView(rstate, vp.Pos.X, vp.Pos.Y, vp.Pos.Z, !!(state->MirrorFlag & 1), !!(state->PlaneMirrorFlag & 1));
di->SetViewArea();
ClearClipper(di, clipper);
di->UpdateCurrentMapSection();
return true;
}
void HWSkyboxPortal::Shutdown(HWDrawInfo *di, FRenderState &rstate)
{
rstate.SetDepthClamp(oldclamp);
auto state = mState;
portal->mFlags &= ~PORTSF_INSKYBOX;
state->inskybox = false;
state->skyboxrecursion--;
state->PlaneMirrorMode = old_pm;
}
const char *HWSkyboxPortal::GetName() { return "Skybox"; }
bool HWSkyboxPortal::AllowSSAO() { return false; } // [MK] sector skyboxes don't allow SSAO by default
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//
//
// Sector stack Portal
//
//
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//
// GLSectorStackPortal::DrawContents
//
//-----------------------------------------------------------------------------
bool HWSectorStackPortal::Setup(HWDrawInfo *di, FRenderState &rstate, Clipper *clipper)
{
// TODO: Handle recursion more intelligently
auto& state = mState;
if (state->renderdepth > r_mirror_recursions)
{
return false;
}
auto portal = origin;
auto &vp = di->Viewpoint;
vp.Pos += DVector3(portal->dx / 16., portal->dy / -16., portal->dz / -256.);
vp.SectNums = portal->targets.Data();
vp.SectCount = portal->targets.Size();
type = origin->type;
state->insectorportal = true;
// avoid recursions!
screen->instack[type == PORTAL_SECTOR_CEILING ? 1 : 0]++;
di->SetupView(rstate, vp.Pos.X, vp.Pos.Y, vp.Pos.Z, !!(state->MirrorFlag & 1), !!(state->PlaneMirrorFlag & 1));
//SetupCoverage(di);
ClearClipper(di, clipper);
return true;
}
void HWSectorStackPortal::Shutdown(HWDrawInfo *di, FRenderState &rstate)
{
screen->instack[type == PORTAL_SECTOR_CEILING ? 1 : 0]--;
mState->insectorportal = false;
}
const char *HWSectorStackPortal::GetName() { return "Sectorstack"; }
#if 0
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//
//
// Plane Mirror Portal (currently not needed, Witchaven 2 is the only Build game using such a feature)
//
//
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//
// GLPlaneMirrorPortal::DrawContents
//
//-----------------------------------------------------------------------------
bool HWPlaneMirrorPortal::Setup(HWDrawInfo *di, FRenderState &rstate, Clipper *clipper)
{
auto state = mState;
if (state->renderdepth > r_mirror_recursions)
{
return false;
}
// A plane mirror needs to flip the portal exclusion logic because inside the mirror, up is down and down is up.
std::swap(screen->instack[plane_floor], screen->instack[plane_ceiling]);
auto &vp = di->Viewpoint;
old_pm = state->PlaneMirrorMode;
// the player is always visible in a mirror.
vp.showviewer = true;
double planez = origin->ZatPoint(vp.Pos);
vp.Pos.Z = 2 * planez - vp.Pos.Z;
vp.ViewActor = nullptr;
state->PlaneMirrorMode = origin->fC() < 0 ? -1 : 1;
state->PlaneMirrorFlag++;
di->SetClipHeight(planez, state->PlaneMirrorMode < 0 ? -1.f : 1.f);
di->SetupView(rstate, vp.Pos.X, vp.Pos.Y, vp.Pos.Z, !!(state->MirrorFlag & 1), !!(state->PlaneMirrorFlag & 1));
ClearClipper(di, clipper);
di->UpdateCurrentMapSection();
return true;
}
void HWPlaneMirrorPortal::Shutdown(HWDrawInfo *di, FRenderState &rstate)
{
auto state = mState;
state->PlaneMirrorFlag--;
state->PlaneMirrorMode = old_pm;
std::swap(screen->instack[plane_floor], screen->instack[plane_ceiling]);
}
const char *HWPlaneMirrorPortal::GetName() { return origin->fC() < 0? "Planemirror ceiling" : "Planemirror floor"; }
#endif
#if 0
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//
//
// Horizon Portal
//
//
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
HWHorizonPortal::HWHorizonPortal(FPortalSceneState *s, HWHorizonInfo * pt, FRenderViewpoint &vp, bool local)
: HWPortal(s, local)
{
origin = pt;
// create the vertex data for this horizon portal.
HWSectorPlane * sp = &origin->plane;
const float vx = vp.Pos.X;
const float vy = vp.Pos.Y;
const float vz = vp.Pos.Z;
const float z = sp->Texheight;
const float tz = (z - vz);
// Draw to some far away boundary
// This is not drawn as larger strips because it causes visual glitches.
auto verts = screen->mVertexData->AllocVertices(1024 + 10);
auto ptr = verts.first;
for (int xx = -32768; xx < 32768; xx += 4096)
{
float x = xx + vx;
for (int yy = -32768; yy < 32768; yy += 4096)
{
float y = yy + vy;
ptr->Set(x, z, y, x / 64, -y / 64);
ptr++;
ptr->Set(x + 4096, z, y, x / 64 + 64, -y / 64);
ptr++;
ptr->Set(x, z, y + 4096, x / 64, -y / 64 - 64);
ptr++;
ptr->Set(x + 4096, z, y + 4096, x / 64 + 64, -y / 64 - 64);
ptr++;
}
}
// fill the gap between the polygon and the true horizon
// Since I can't draw into infinity there can always be a
// small gap
ptr->Set(-32768 + vx, z, -32768 + vy, 512.f, 0);
ptr++;
ptr->Set(-32768 + vx, vz, -32768 + vy, 512.f, tz);
ptr++;
ptr->Set(-32768 + vx, z, 32768 + vy, -512.f, 0);
ptr++;
ptr->Set(-32768 + vx, vz, 32768 + vy, -512.f, tz);
ptr++;
ptr->Set(32768 + vx, z, 32768 + vy, 512.f, 0);
ptr++;
ptr->Set(32768 + vx, vz, 32768 + vy, 512.f, tz);
ptr++;
ptr->Set(32768 + vx, z, -32768 + vy, -512.f, 0);
ptr++;
ptr->Set(32768 + vx, vz, -32768 + vy, -512.f, tz);
ptr++;
ptr->Set(-32768 + vx, z, -32768 + vy, 512.f, 0);
ptr++;
ptr->Set(-32768 + vx, vz, -32768 + vy, 512.f, tz);
ptr++;
voffset = verts.second;
vcount = 1024;
}
//-----------------------------------------------------------------------------
//
// HWHorizonPortal::DrawContents
//
//-----------------------------------------------------------------------------
void HWHorizonPortal::DrawContents(HWDrawInfo *di, FRenderState &state)
{
Clocker c(PortalAll);
HWSectorPlane * sp = &origin->plane;
auto &vp = di->Viewpoint;
auto texture = TexMan.GetGameTexture(sp->texture, true);
if (!texture || !texture->isValid())
{
state.ClearScreen();
return;
}
di->SetCameraPos(vp.Pos);
if (texture->isFullbright())
{
// glowing textures are always drawn full bright without color
di->SetColor(state, 255, 0, false, origin->colormap, 1.f);
di->SetFog(state, 255, 0, false, &origin->colormap, false);
}
else
{
int rel = getExtraLight();
di->SetColor(state, origin->lightlevel, rel, di->isFullbrightScene(), origin->colormap, 1.0f);
di->SetFog(state, origin->lightlevel, rel, di->isFullbrightScene(), &origin->colormap, false);
}
state.SetMaterial(texture, UF_Texture, 0, CLAMP_NONE, 0, -1);
state.SetObjectColor(origin->specialcolor);
SetPlaneTextureRotation(state, sp, texture);
state.AlphaFunc(Alpha_GEqual, 0.f);
state.SetRenderStyle(STYLE_Source);
for (unsigned i = 0; i < vcount; i += 4)
{
state.Draw(DT_TriangleStrip, voffset + i, 4, true);// i == 0);
}
state.Draw(DT_TriangleStrip, voffset + vcount, 10, false);
state.EnableTextureMatrix(false);
}
#endif