/
rend_fakeradio.cpp
1079 lines (968 loc) · 42.9 KB
/
rend_fakeradio.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
/** @file rend_fakeradio.cpp Geometry generation for faked, radiosity lighting.
*
* @authors Copyright © 2004-2014 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2015 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include "render/rend_fakeradio.h"
#include <de/Vector>
#include <doomsday/console/var.h>
#include "clientapp.h"
#include "gl/gl_texmanager.h"
#include "MaterialAnimator"
#include "MaterialVariantSpec"
#include "world/map.h"
#include "ConvexSubspace"
#include "Line"
#include "Surface"
#include "client/clientsubsector.h"
#include "Face"
#include "HEdge"
#include "WallEdge"
#include "render/rend_main.h"
#include "render/r_main.h" // levelFullBright
#include "render/shadowedge.h"
#include "render/viewports.h" // R_FrameCount()
#include "render/store.h"
using namespace de;
using namespace world;
enum WallShadow
{
TopShadow,
BottomShadow,
LeftShadow,
RightShadow
};
static dfloat const MIN_OPEN = 0.1f;
static ddouble const MINDIFF = 8; ///< Min plane height difference (world units).
static ddouble const INDIFF = 8; ///< Max plane height for indifference offset.
static dfloat const MIN_SHADOW_DARKNESS = .0001f; ///< Minimum to qualify.
static ddouble const MIN_SHADOW_SIZE = 1; ///< In map units.
dint rendFakeRadio = true; ///< cvar
static dfloat fakeRadioDarkness = 1.2f; ///< cvar
byte devFakeRadioUpdate = true; ///< cvar
/**
* Returns the "shadow darkness" (factor) for the given @a ambientLight (level), derived
* from values in Config.
*
* @param ambientLight Ambient light level to process. It is assumed that adaptation has
* @em NOT yet been applied (it will be).
*/
static inline dfloat calcShadowDarkness(dfloat ambientLight)
{
ambientLight += Rend_LightAdaptationDelta(ambientLight);
return (0.6f - ambientLight * 0.4f) * 0.65f * ::fakeRadioDarkness;
}
/**
* Returns the "shadow size" in map units for the given @a ambientLight (level).
*
* @param ambientLight Ambient light level to process. It is assumed that adaptation has
* @em NOT yet been applied (it will be).
*/
static inline dfloat calcShadowSize(dfloat ambientLight)
{
return 2 * (8 + 16 - ambientLight * 16); /// @todo Make cvars out of constants.
}
/**
* Returns the "wall height" (i.e., distance in map units) of the wall described by
* @a leftEdge and @a rightEdge.
*
* @param leftEdge WallEdge describing the logical left-edge of the wall section.
* @param rightEdge WallEdge describing the logical right-edge of the wall section.
*
* @see wallWidth()
* @see wallDimensions()
*/
static inline ddouble wallHeight(WallEdge const &leftEdge, WallEdge const &rightEdge)
{
return rightEdge.top().origin().z - leftEdge.bottom().origin().z;
}
/**
* Returns the "wall width" (i.e., distance in map units) of the wall described by
* @a leftEdge and @a rightEdge.
*
* @param leftEdge WallEdge describing the logical left-edge of the wall section.
* @param rightEdge WallEdge describing the logical right-edge of the wall section.
*
* @see wallHeight()
* @see wallDimensions()
*/
static inline ddouble wallWidth(WallEdge const &leftEdge, WallEdge const &rightEdge)
{
return de::abs((rightEdge.origin() - leftEdge.origin()).length());
}
/**
* Returns the "wall dimensions" (i.e., distance in map units) of the wall described
* by @a leftEdge and @a rightEdge.
*
* @param leftEdge WallEdge describing the logical left-edge of the wall section.
* @param rightEdge WallEdge describing the logical right-edge of the wall section.
*
* @see wallWidth()
* @see wallHeight()
*/
#if 0
static Vector2d wallDimensions(WallEdge const &leftEdge, WallEdge const &rightEdge)
{
return Vector2d(wallWidth(leftEdge, rightEdge), wallHeight(leftEdge, rightEdge));
}
#endif
/**
* Returns the "wall offset" (i.e., distance in map units from the LineSide's vertex) of
* the wall described by @a leftEdge and @a rightEdge.
*
* @param leftEdge WallEdge describing the logical left-edge of the wall section.
* @param rightEdge WallEdge describing the logical right-edge of the wall section.
*/
static ddouble wallOffset(WallEdge const &leftEdge, WallEdge const &/*rightEdge*/)
{
return leftEdge.lineSideOffset();
}
/**
* Return the "wall side-openness" (factor) of specified side of the wall described by
* @a leftEdge and @a rightEdge.
*
* @param leftEdge WallEdge describing the logical left-edge of the wall section.
* @param rightEdge WallEdge describing the logical right-edge of the wall section.
* @param rightSide Use the right edge if @c true; otherwise the left edge.
*/
static dfloat wallSideOpenness(WallEdge const &leftEdge, WallEdge const &/*rightEdge*/, bool rightSide)
{
return leftEdge.lineSide().radioCornerSide(rightSide).corner;
}
/**
* Returns @c true if the wall described by @a leftEdge and @a rightEdge should receive
* the specified @a shadow.
*
* @param leftEdge WallEdge describing the logical left-edge of the wall section.
* @param rightEdge WallEdge describing the logical right-edge of the wall section.
* @param shadow WallShadow identifier.
* @param shadowSize Shadow size in map units.
*/
static bool wallReceivesShadow(WallEdge const &leftEdge, WallEdge const &rightEdge,
WallShadow shadow, dfloat shadowSize)
{
if(shadowSize <= 0) return false;
LineSide const &side = leftEdge.lineSide();
DENG2_ASSERT(side.leftHEdge());
auto const &subsec = side.leftHEdge()->face().mapElementAs<ConvexSubspace>().subsector().as<world::ClientSubsector>();
Plane const &visFloor = subsec.visFloor ();
Plane const &visCeiling = subsec.visCeiling();
switch(shadow)
{
case TopShadow:
return visCeiling.castsShadow()
&& rightEdge.top ().z() > visCeiling.heightSmoothed() - shadowSize
&& leftEdge .bottom().z() < visCeiling.heightSmoothed();
case BottomShadow:
return visFloor.castsShadow()
&& leftEdge .bottom().z() < visFloor .heightSmoothed() + shadowSize
&& rightEdge.top ().z() > visFloor .heightSmoothed();
case LeftShadow:
return (visFloor.castsShadow() || visCeiling.castsShadow())
&& wallSideOpenness(leftEdge, rightEdge, false/*left side*/) > 0
&& leftEdge.lineSideOffset() < shadowSize;
case RightShadow:
return (visFloor.castsShadow() || visCeiling.castsShadow())
&& wallSideOpenness(leftEdge, rightEdge, true/*right side*/) > 0
&& leftEdge.lineSideOffset() + wallWidth(leftEdge, rightEdge) > side.line().length() - shadowSize;
}
DENG2_ASSERT(!"Unknown WallShadow");
return false;
}
/**
* Determine the horizontal offset for a FakeRadio wall, shadow geometry.
*
* @param lineLength If negative; implies that the texture is flipped horizontally.
* @param segOffset Offset to the start of the segment.
*/
static inline dfloat calcTexCoordX(dfloat lineLength, dfloat segOffset)
{
return (lineLength > 0 ? segOffset : lineLength + segOffset);
}
/**
* Determine the vertical offset for a FakeRadio wall, shadow geometry.
*
* @param z Z height of the vertex.
* @param bottom Z height of the bottom of the wall section.
* @param top Z height of the top of the wall section.
* @param texHeight If negative; implies that the texture is flipped vertically.
*/
static inline dfloat calcTexCoordY(dfloat z, dfloat bottom, dfloat top, dfloat texHeight)
{
return (texHeight > 0 ? top - z : bottom - z);
}
struct ProjectedShadowData
{
lightingtexid_t texture;
Vector2f texOrigin;
Vector2f texDimensions;
Vector2f texCoords[4]; ///< { bl, tl, br, tr }
};
static void setTopShadowParams(WallEdge const &leftEdge, WallEdge const &rightEdge, ddouble shadowSize,
ProjectedShadowData &projected)
{
LineSide /*const*/ &side = leftEdge.lineSide();
DENG2_ASSERT(side.leftHEdge());
auto const &space = side.leftHEdge()->face().mapElementAs<ConvexSubspace>();
auto const &subsec = space.subsector().as<world::ClientSubsector>();
Plane const &visFloor = subsec.visFloor ();
Plane const &visCeiling = subsec.visCeiling();
de::zap(projected);
projected.texDimensions = Vector2f(0, shadowSize);
projected.texOrigin = Vector2f(0, calcTexCoordY(leftEdge.top().z(), subsec.visFloor().heightSmoothed()
, subsec.visCeiling().heightSmoothed(), shadowSize));
projected.texture = LST_RADIO_OO;
edgespan_t const &edgeSpan = side.radioEdgeSpan(true/*top*/);
// One or both neighbors without a back sector?
if(side.radioCornerSide(0/*left*/ ).corner == -1 ||
side.radioCornerSide(1/*right*/).corner == -1)
{
// At least one corner faces outwards.
projected.texture = LST_RADIO_OO;
projected.texDimensions.x = edgeSpan.length;
projected.texOrigin.x = calcTexCoordX(edgeSpan.length, edgeSpan.shift + wallOffset(leftEdge, rightEdge));
// Both corners face outwards?
if( (side.radioCornerSide(0/*left*/).corner == -1 && side.radioCornerSide(1/*right*/).corner == -1)
|| (side.radioCornerTop (0/*left*/).corner == -1 && side.radioCornerTop (1/*right*/).corner == -1))
{
projected.texture = LST_RADIO_OO;//CC;
}
// Right corner faces outwards?
else if(side.radioCornerSide(1/*right*/).corner == -1)
{
if(-side.radioCornerTop(0/*left*/).pOffset < 0 && side.radioCornerBottom(0/*left*/).pHeight < visCeiling.heightSmoothed())
{
projected.texture = LST_RADIO_OE;
// Must flip horizontally.
projected.texDimensions.x = -edgeSpan.length;
projected.texOrigin.x = calcTexCoordX(-edgeSpan.length, edgeSpan.shift + wallOffset(leftEdge, rightEdge));
}
}
else // Left corner faces outwards.
{
if(-side.radioCornerTop(1/*right*/).pOffset < 0 && side.radioCornerBottom(1/*right*/).pHeight < visCeiling.heightSmoothed())
{
projected.texture = LST_RADIO_OE;
}
}
}
else
{
// Corners WITH a neighbor back sector
projected.texDimensions.x = edgeSpan.length;
projected.texOrigin.x = calcTexCoordX(edgeSpan.length, edgeSpan.shift + wallOffset(leftEdge, rightEdge));
// Both corners face outwards?
if(side.radioCornerTop(0/*left*/).corner == -1 && side.radioCornerTop(1/*right*/).corner == -1)
{
projected.texture = LST_RADIO_OO;//CC;
}
// Right corner faces outwards?
else if(side.radioCornerTop(1/*right*/).corner == -1 && side.radioCornerTop(0/*left*/).corner > MIN_OPEN)
{
projected.texture = LST_RADIO_OO;
}
// Left corner faces outwards?
else if(side.radioCornerTop(0/*left*/).corner == -1 && side.radioCornerTop(1/*right*/).corner > MIN_OPEN)
{
projected.texture = LST_RADIO_OO;
}
// Both edges open?
else if(side.radioCornerTop(0/*left*/).corner <= MIN_OPEN && side.radioCornerTop(1/*right*/).corner <= MIN_OPEN)
{
projected.texture = LST_RADIO_OO;
if(side.radioCornerTop(0/*left*/).proximity && side.radioCornerTop(1/*right*/).proximity)
{
if(-side.radioCornerTop(0/*left*/).pOffset >= 0 && -side.radioCornerTop(1/*right*/).pOffset < 0)
{
projected.texture = LST_RADIO_CO;
// The shadow can't go over the higher edge.
if(shadowSize > -side.radioCornerTop(0/*left*/).pOffset)
{
if(-side.radioCornerTop(0/*left*/).pOffset < INDIFF)
{
projected.texture = LST_RADIO_OE;
}
else
{
projected.texDimensions.y = -side.radioCornerTop(0/*left*/).pOffset;
projected.texOrigin.y = calcTexCoordY(leftEdge.top().z(), visFloor.heightSmoothed(), visCeiling.heightSmoothed(), projected.texDimensions.y);
}
}
}
else if(-side.radioCornerTop(0/*left*/).pOffset < 0 && -side.radioCornerTop(1/*right*/).pOffset >= 0)
{
projected.texture = LST_RADIO_CO;
// Must flip horizontally.
projected.texDimensions.x = -edgeSpan.length;
projected.texOrigin.x = calcTexCoordX(-edgeSpan.length, edgeSpan.shift + wallOffset(leftEdge, rightEdge));
// The shadow can't go over the higher edge.
if(shadowSize > -side.radioCornerTop(1/*right*/).pOffset)
{
if(-side.radioCornerTop(1/*right*/).pOffset < INDIFF)
{
projected.texture = LST_RADIO_OE;
}
else
{
projected.texDimensions.y = -side.radioCornerTop(1/*right*/).pOffset;
projected.texOrigin.y = calcTexCoordY(leftEdge.top().z(), visFloor.heightSmoothed(), visCeiling.heightSmoothed(), projected.texDimensions.y);
}
}
}
}
else
{
if(-side.radioCornerTop(0/*left*/).pOffset < -MINDIFF)
{
projected.texture = LST_RADIO_OE;
// Must flip horizontally.
projected.texDimensions.x = -edgeSpan.length;
projected.texOrigin.x = calcTexCoordX(-edgeSpan.length, edgeSpan.shift + wallOffset(leftEdge, rightEdge));
}
else if(-side.radioCornerTop(1/*right*/).pOffset < -MINDIFF)
{
projected.texture = LST_RADIO_OE;
}
}
}
else if(side.radioCornerTop(0/*left*/).corner <= MIN_OPEN)
{
if(-side.radioCornerTop(0/*left*/).pOffset < 0)
projected.texture = LST_RADIO_CO;
else
projected.texture = LST_RADIO_OO;
// Must flip horizontally.
projected.texDimensions.x = -edgeSpan.length;
projected.texOrigin.x = calcTexCoordX(-edgeSpan.length, edgeSpan.shift + wallOffset(leftEdge, rightEdge));
}
else if(side.radioCornerTop(1/*right*/).corner <= MIN_OPEN)
{
if(-side.radioCornerTop(1/*right*/).pOffset < 0)
projected.texture = LST_RADIO_CO;
else
projected.texture = LST_RADIO_OO;
}
else // C/C ???
{
projected.texture = LST_RADIO_OO;
}
}
}
static void setBottomShadowParams(WallEdge const &leftEdge, WallEdge const &rightEdge, ddouble shadowSize,
ProjectedShadowData &projected)
{
LineSide /*const*/ &side = leftEdge.lineSide();
DENG2_ASSERT(side.leftHEdge());
auto const &subsec = side.leftHEdge()->face().mapElementAs<ConvexSubspace>().subsector().as<world::ClientSubsector>();
Plane const &visFloor = subsec.visFloor ();
Plane const &visCeiling = subsec.visCeiling();
de::zap(projected);
projected.texDimensions.y = -shadowSize;
projected.texOrigin.y = calcTexCoordY(leftEdge.top().z(), visFloor.heightSmoothed(), visCeiling.heightSmoothed(), -shadowSize);
projected.texture = LST_RADIO_OO;
edgespan_t const &edgeSpan = side.radioEdgeSpan(false/*bottom*/);
// Corners without a neighbor back sector?
if(side.radioCornerSide(0/*left*/).corner == -1 || side.radioCornerSide(1/*right*/).corner == -1)
{
// At least one corner faces outwards.
projected.texture = LST_RADIO_OO;
projected.texDimensions.x = edgeSpan.length;
projected.texOrigin.x = calcTexCoordX(edgeSpan.length, edgeSpan.shift + wallOffset(leftEdge, rightEdge));
// Both corners face outwards?
if( (side.radioCornerSide (0/*left*/).corner == -1 && side.radioCornerSide (1/*right*/).corner == -1)
|| (side.radioCornerBottom(0/*left*/).corner == -1 && side.radioCornerBottom(1/*right*/).corner == -1))
{
projected.texture = LST_RADIO_OO;//CC;
}
// Right corner faces outwards?
else if(side.radioCornerSide(1/*right*/).corner == -1)
{
if(side.radioCornerBottom(0/*left*/).pOffset < 0 && side.radioCornerTop(0/*left*/).pHeight > visFloor.heightSmoothed())
{
projected.texture = LST_RADIO_OE;
// Must flip horizontally.
projected.texDimensions.x = -edgeSpan.length;
projected.texOrigin.x = calcTexCoordX(-edgeSpan.length, edgeSpan.shift + wallOffset(leftEdge, rightEdge));
}
}
else // Left corner faces outwards.
{
if(side.radioCornerBottom(1/*right*/).pOffset < 0 && side.radioCornerTop(1/*right*/).pHeight > visFloor.heightSmoothed())
{
projected.texture = LST_RADIO_OE;
}
}
}
else // Corners WITH a neighbor back sector.
{
projected.texDimensions.x = edgeSpan.length;
projected.texOrigin.x = calcTexCoordX(edgeSpan.length, edgeSpan.shift + wallOffset(leftEdge, rightEdge));
// Both corners face outwards?
if(side.radioCornerBottom(0/*left*/).corner == -1 && side.radioCornerBottom(1/*right*/).corner == -1)
{
projected.texture = LST_RADIO_OO;//CC;
}
// Right corner faces outwards?
else if(side.radioCornerBottom(1/*right*/).corner == -1 && side.radioCornerBottom(0/*right*/).corner > MIN_OPEN)
{
projected.texture = LST_RADIO_OO;
}
// Left corner faces outwards?
else if(side.radioCornerBottom(0/*left*/).corner == -1 && side.radioCornerBottom(1/*right*/).corner > MIN_OPEN)
{
projected.texture = LST_RADIO_OO;
}
// Both edges open?
else if(side.radioCornerBottom(0/*left*/).corner <= MIN_OPEN && side.radioCornerBottom(1/*right*/).corner <= MIN_OPEN)
{
projected.texture = LST_RADIO_OO;
if(side.radioCornerBottom(0/*left*/).proximity && side.radioCornerBottom(1/*right*/).proximity)
{
if(side.radioCornerBottom(0/*left*/).pOffset >= 0 && side.radioCornerBottom(1/*right*/).pOffset < 0)
{
projected.texture = LST_RADIO_CO;
// The shadow can't go over the higher edge.
if(shadowSize > side.radioCornerBottom(0/*left*/).pOffset)
{
if(side.radioCornerBottom(0/*left*/).pOffset < INDIFF)
{
projected.texture = LST_RADIO_OE;
}
else
{
projected.texDimensions.y = -side.radioCornerBottom(0/*left*/).pOffset;
projected.texOrigin.y = calcTexCoordY(leftEdge.top().z(), visFloor.heightSmoothed(), visCeiling.heightSmoothed(), projected.texDimensions.y);
}
}
}
else if(side.radioCornerBottom(0/*left*/).pOffset < 0 && side.radioCornerBottom(1/*right*/).pOffset >= 0)
{
projected.texture = LST_RADIO_CO;
// Must flip horizontally.
projected.texDimensions.x = -edgeSpan.length;
projected.texOrigin.x = calcTexCoordX(-edgeSpan.length, edgeSpan.shift + wallOffset(leftEdge, rightEdge));
if(shadowSize > side.radioCornerBottom(1/*right*/).pOffset)
{
if(side.radioCornerBottom(1/*right*/).pOffset < INDIFF)
{
projected.texture = LST_RADIO_OE;
}
else
{
projected.texDimensions.y = -side.radioCornerBottom(1/*right*/).pOffset;
projected.texOrigin.y = calcTexCoordY(leftEdge.top().z(), visFloor.heightSmoothed(), visCeiling.heightSmoothed(), projected.texDimensions.y);
}
}
}
}
else
{
if(side.radioCornerBottom(0/*left*/).pOffset < -MINDIFF)
{
projected.texture = LST_RADIO_OE;
// Must flip horizontally.
projected.texDimensions.x = -edgeSpan.length;
projected.texOrigin.x = calcTexCoordX(-edgeSpan.length, edgeSpan.shift + wallOffset(leftEdge, rightEdge));
}
else if(side.radioCornerBottom(1/*right*/).pOffset < -MINDIFF)
{
projected.texture = LST_RADIO_OE;
}
}
}
// Right corner closed?
else if(side.radioCornerBottom(0/*left*/).corner <= MIN_OPEN)
{
if(side.radioCornerBottom(0/*left*/).pOffset < 0)
projected.texture = LST_RADIO_CO;
else
projected.texture = LST_RADIO_OO;
// Must flip horizontally.
projected.texDimensions.x = -edgeSpan.length;
projected.texOrigin.x = calcTexCoordX(-edgeSpan.length, edgeSpan.shift + wallOffset(leftEdge, rightEdge));
}
// Left Corner closed?
else if(side.radioCornerBottom(1/*right*/).corner <= MIN_OPEN)
{
if(side.radioCornerBottom(1/*right*/).pOffset < 0)
projected.texture = LST_RADIO_CO;
else
projected.texture = LST_RADIO_OO;
}
else // C/C ???
{
projected.texture = LST_RADIO_OO;
}
}
}
static void setSideShadowParams(WallEdge const &leftEdge, WallEdge const &rightEdge, bool rightSide,
ddouble shadowSize, ProjectedShadowData &projected)
{
LineSide /*const*/ &side = leftEdge.lineSide();
HEdge const *hedge = side.leftHEdge();
DENG2_ASSERT(hedge);
auto const &subsec = hedge->face().mapElementAs<ConvexSubspace>().subsector().as<world::ClientSubsector>();
Plane const &visFloor = subsec.visFloor ();
Plane const &visCeiling = subsec.visCeiling();
DENG2_ASSERT(visFloor.castsShadow() || visCeiling.castsShadow()); // sanity check.
de::zap(projected);
projected.texOrigin = Vector2f(0, leftEdge.bottom().z() - visFloor.heightSmoothed());
projected.texDimensions = Vector2f(0, visCeiling.heightSmoothed() - visFloor.heightSmoothed());
if(rightSide)
{
// Right shadow.
projected.texOrigin.x = -side.line().length() + wallOffset(leftEdge, rightEdge);
// Make sure the shadow isn't too big
if(shadowSize > side.line().length())
{
projected.texDimensions.x = -side.line().length();
if(side.radioCornerSide(0/*left*/).corner > MIN_OPEN)
projected.texDimensions.x /= 2;
}
else
{
projected.texDimensions.x = -shadowSize;
}
}
else
{
// Left shadow.
projected.texOrigin.x = wallOffset(leftEdge, rightEdge);
// Make sure the shadow isn't too big
if(shadowSize > side.line().length())
{
projected.texDimensions.x = side.line().length();
if(side.radioCornerSide(1/*right*/).corner > MIN_OPEN)
projected.texDimensions.x /= 2;
}
else
{
projected.texDimensions.x = shadowSize;
}
}
if(!hedge->twin().hasFace() || leftEdge.spec().section == LineSide::Middle)
{
if(!visFloor.castsShadow())
{
projected.texDimensions.y = -(visCeiling.heightSmoothed() - visFloor.heightSmoothed());
projected.texOrigin.y = calcTexCoordY(leftEdge.top().z(), visFloor.heightSmoothed(), visCeiling.heightSmoothed(), projected.texDimensions.y);
projected.texture = LST_RADIO_CO;
}
else if(!visCeiling.castsShadow())
{
projected.texture = LST_RADIO_CO;
}
else
{
projected.texture = LST_RADIO_CC;
}
}
else
{
auto const &bSpace = hedge->twin().face().mapElementAs<ConvexSubspace>();
if (bSpace.hasSubsector())
{
auto const &bSubsec = bSpace.subsector().as<world::ClientSubsector>();
ddouble const bFloor = bSubsec.visFloor ().heightSmoothed();
ddouble const bCeil = bSubsec.visCeiling().heightSmoothed();
if (bFloor > visFloor.heightSmoothed() && bCeil < visCeiling.heightSmoothed())
{
if (visFloor.castsShadow() && visCeiling.castsShadow())
{
projected.texture = LST_RADIO_CC;
}
else if (!visFloor.castsShadow())
{
projected.texOrigin.y = leftEdge.bottom().z() - visCeiling.heightSmoothed();
projected.texDimensions.y = -(visCeiling.heightSmoothed() - visFloor.heightSmoothed());
projected.texture = LST_RADIO_CO;
}
else
{
projected.texture = LST_RADIO_CO;
}
}
else if (bFloor > visFloor.heightSmoothed())
{
if (visFloor.castsShadow() && visCeiling.castsShadow())
{
projected.texture = LST_RADIO_CC;
}
else if (!visFloor.castsShadow())
{
projected.texOrigin.y = leftEdge.bottom().z() - visCeiling.heightSmoothed();
projected.texDimensions.y = -(visCeiling.heightSmoothed() - visFloor.heightSmoothed());
projected.texture = LST_RADIO_CO;
}
else
{
projected.texture = LST_RADIO_CO;
}
}
else if (bCeil < visCeiling.heightSmoothed())
{
if (visFloor.castsShadow() && visCeiling.castsShadow())
{
projected.texture = LST_RADIO_CC;
}
else if (!visFloor.castsShadow())
{
projected.texOrigin.y = leftEdge.bottom().z() - visCeiling.heightSmoothed();
projected.texDimensions.y = -(visCeiling.heightSmoothed() - visFloor.heightSmoothed());
projected.texture = LST_RADIO_CO;
}
else
{
projected.texture = LST_RADIO_CO;
}
}
}
}
}
static void quadTexCoords(Vector2f *tc, WallEdge const &leftEdge, WallEdge const &rightEdge,
Vector2f const &texOrigin, Vector2f const &texDimensions, bool horizontal)
{
DENG2_ASSERT(tc);
if(horizontal)
{
tc[0] = (texOrigin / texDimensions).yx();
tc[2] = tc[0] + Vector2f(0 , wallWidth(leftEdge, rightEdge)) / texDimensions.yx();
tc[3] = tc[0] + Vector2f(wallHeight(leftEdge, rightEdge), wallWidth(leftEdge, rightEdge)) / texDimensions.yx();
tc[1] = Vector2f(tc[3].x, tc[0].y);
}
else // Vertical.
{
tc[1] = texOrigin / texDimensions;
tc[0] = tc[1] + Vector2f(0 , wallHeight(leftEdge, rightEdge)) / texDimensions;
tc[2] = tc[1] + Vector2f(wallWidth(leftEdge, rightEdge), wallHeight(leftEdge, rightEdge)) / texDimensions;
tc[3] = Vector2f(tc[2].x, tc[1].y);
}
}
static bool projectWallShadow(WallEdge const &leftEdge, WallEdge const &rightEdge,
WallShadow shadow, ddouble shadowSize, ProjectedShadowData &projected)
{
if(!wallReceivesShadow(leftEdge, rightEdge, shadow, shadowSize))
return false;
switch(shadow)
{
case TopShadow:
setTopShadowParams(leftEdge, rightEdge, shadowSize, projected);
quadTexCoords(projected.texCoords, leftEdge, rightEdge,
projected.texOrigin, projected.texDimensions, false/*vertical*/);
return true;
case BottomShadow:
setBottomShadowParams(leftEdge, rightEdge, shadowSize, projected);
quadTexCoords(projected.texCoords, leftEdge, rightEdge,
projected.texOrigin, projected.texDimensions, false/*vertical*/);
return true;
case LeftShadow:
setSideShadowParams(leftEdge, rightEdge, false/*left side*/, shadowSize, projected);
quadTexCoords(projected.texCoords, leftEdge, rightEdge,
projected.texOrigin, projected.texDimensions, true/*horizontal*/);
return true;
case RightShadow:
setSideShadowParams(leftEdge, rightEdge, true/*right side*/, shadowSize, projected);
quadTexCoords(projected.texCoords, leftEdge, rightEdge,
projected.texOrigin, projected.texDimensions, true/*horizontal*/);
return true;
}
DENG2_ASSERT(!"Unknown WallShadow");
return false;
}
static void drawWallShadow(Vector3f const *posCoords, WallEdge const &leftEdge, WallEdge const &rightEdge,
dfloat shadowDark, ProjectedShadowData const &tp)
{
DENG2_ASSERT(posCoords);
// Uniform color - shadows are black.
Vector4ub const shadowColor(0, 0, 0, 255 * de::clamp(0.f, shadowDark, 1.0f));
DrawListSpec listSpec;
listSpec.group = ShadowGeom;
listSpec.texunits[TU_PRIMARY] = GLTextureUnit(GL_PrepareLSTexture(tp.texture), gl::ClampToEdge, gl::ClampToEdge);
DrawList &shadowList = ClientApp::renderSystem().drawLists().find(listSpec);
// Walls with edge divisions mean two trifans.
if(leftEdge.divisionCount() || rightEdge.divisionCount())
{
Store &buffer = ClientApp::renderSystem().buffer();
// Right fan.
{
duint const numVerts = 3 + rightEdge.divisionCount();
duint const base = buffer.allocateVertices(numVerts);
DrawList::Indices indices;
indices.resize(numVerts);
for(duint i = 0; i < numVerts; ++i)
{
indices[i] = base + i;
}
//
// Build geometry.
//
buffer.posCoords [indices[0 ]] = posCoords[0];
buffer.colorCoords [indices[0 ]] = shadowColor;
buffer.texCoords[0][indices[0 ]] = tp.texCoords[0];
buffer.posCoords [indices[1 ]] = posCoords[3];
buffer.colorCoords [indices[1 ]] = shadowColor;
buffer.texCoords[0][indices[1 ]] = tp.texCoords[3];
buffer.posCoords [indices[numVerts - 1]] = posCoords[2];
buffer.colorCoords [indices[numVerts - 1]] = shadowColor;
buffer.texCoords[0][indices[numVerts - 1]] = tp.texCoords[2];
for(dint i = 0; i < rightEdge.divisionCount(); ++i)
{
WorldEdge::Event const &icpt = rightEdge.at(rightEdge.lastDivision() - i);
buffer.posCoords [indices[2 + i]] = icpt.origin();
buffer.colorCoords [indices[2 + i]] = shadowColor;
buffer.texCoords[0][indices[2 + i]] = Vector2f(tp.texCoords[3].x, tp.texCoords[2].y + (tp.texCoords[3].y - tp.texCoords[2].y) * icpt.distance());
}
// Write the geometry?
if(::rendFakeRadio != 2)
{
shadowList.write(buffer, gl::TriangleFan, indices);
}
}
// Left fan.
{
duint const numVerts = 3 + leftEdge .divisionCount();
duint const base = buffer.allocateVertices(numVerts);
DrawList::Indices indices;
indices.resize(numVerts);
for(duint i = 0; i < numVerts; ++i)
{
indices[i] = base + i;
}
//
// Build geometry.
//
buffer.posCoords [indices[0 ]] = posCoords[3];
buffer.colorCoords [indices[0 ]] = shadowColor;
buffer.texCoords[0][indices[0 ]] = tp.texCoords[3];
buffer.posCoords [indices[1 ]] = posCoords[0];
buffer.colorCoords [indices[1 ]] = shadowColor;
buffer.texCoords[0][indices[1 ]] = tp.texCoords[0];
buffer.posCoords [indices[numVerts - 1]] = posCoords[1];
buffer.colorCoords [indices[numVerts - 1]] = shadowColor;
buffer.texCoords[0][indices[numVerts - 1]] = tp.texCoords[1];
for(dint i = 0; i < leftEdge.divisionCount(); ++i)
{
WorldEdge::Event const &icpt = leftEdge.at(leftEdge.firstDivision() + i);
buffer.posCoords [indices[2 + i]] = icpt.origin();
buffer.colorCoords [indices[2 + i]] = shadowColor;
buffer.texCoords[0][indices[2 + i]] = Vector2f(tp.texCoords[0].x, tp.texCoords[0].y + (tp.texCoords[1].y - tp.texCoords[0].y) * icpt.distance());
}
// Write the geometry?
if(::rendFakeRadio != 2)
{
shadowList.write(buffer, gl::TriangleFan, indices);
}
}
}
else
{
Store &buffer = ClientApp::renderSystem().buffer();
duint base = buffer.allocateVertices(4);
DrawList::Indices indices;
indices.resize(4);
for(duint i = 0; i < 4; ++i)
{
indices[i] = base + i;
}
//
// Build geometry.
//
for(duint i = 0; i < 4; ++i)
{
buffer.posCoords [indices[i]] = posCoords[i];
buffer.colorCoords [indices[i]] = shadowColor;
buffer.texCoords[0][indices[i]] = tp.texCoords[i];
}
// Write the geometry?
if(::rendFakeRadio != 2)
{
shadowList.write(buffer, gl::TriangleStrip, indices);
}
}
}
void Rend_DrawWallRadio(WallEdge const &leftEdge, WallEdge const &rightEdge, dfloat ambientLight)
{
// Disabled?
if(!::rendFakeRadio || ::levelFullBright || leftEdge.spec().flags.testFlag(WallSpec::NoFakeRadio))
return;
// Skip if the surface is not lit with ambient light.
dfloat const shadowDark = calcShadowDarkness(ambientLight);
if(shadowDark < MIN_SHADOW_DARKNESS)
return;
// Skip if the determined shadow size is too small.
dfloat const shadowSize = calcShadowSize(ambientLight);
if(shadowSize < MIN_SHADOW_SIZE)
return;
// Ensure we have up-to-date information for generating shadow geometry.
leftEdge.lineSide().updateRadioForFrame(R_FrameCount());
Vector3f const posCoords[] = {
leftEdge .bottom().origin(),
leftEdge .top ().origin(),
rightEdge.bottom().origin(),
rightEdge.top ().origin()
};
ProjectedShadowData projected;
if(projectWallShadow(leftEdge, rightEdge, TopShadow, shadowSize, projected))
{
drawWallShadow(posCoords, leftEdge, rightEdge, shadowDark,
projected);
}
if(projectWallShadow(leftEdge, rightEdge, BottomShadow, shadowSize, projected))
{
drawWallShadow(posCoords, leftEdge, rightEdge, shadowDark,
projected);
}
if(projectWallShadow(leftEdge, rightEdge, LeftShadow, shadowSize, projected))
{
drawWallShadow(posCoords, leftEdge, rightEdge,
shadowDark * de::cubed(wallSideOpenness(leftEdge, rightEdge, false/*left edge*/) * .8f),
projected);
}
if(projectWallShadow(leftEdge, rightEdge, RightShadow, shadowSize, projected))
{
drawWallShadow(posCoords, leftEdge, rightEdge,
shadowDark * de::cubed(wallSideOpenness(leftEdge, rightEdge, true/*right edge*/) * .8f),
projected);
}
}
/**
* Determines whether FakeRadio flat, shadow geometry should be drawn between the vertices of
* the given half-edges @a hEdges and prepares the ShadowEdges @a edges accordingly.
*
* @param edges ShadowEdge descriptors for both edges { left, right }.
* @param hEdges Half-edge accessors for both edges { left, right }.
* @param sectorPlaneIndex Logical index of the sector plane to consider a shadow for.
* @param shadowDark Shadow darkness factor.
*
* @return @c true if one or both edges are partially in shadow.
*/
static bool prepareFlatShadowEdges(ShadowEdge edges[2], HEdge const *hEdges[2], dint sectorPlaneIndex,
dfloat shadowDark)
{
DENG2_ASSERT(edges && hEdges && hEdges[0] && hEdges[1]);
// If the sector containing the shadowing line section is fully closed (i.e., volume is
// not positive) then skip shadow drawing entirely.
/// @todo Encapsulate this logic in ShadowEdge -ds
if(!hEdges[0]->hasFace() || !hEdges[0]->face().hasMapElement())
return false;
if(!hEdges[0]->face().mapElementAs<ConvexSubspace>().subsector().as<world::ClientSubsector>().hasWorldVolume())
return false;
for(dint i = 0; i < 2; ++i)
{
edges[i].init(*hEdges[i], i);
edges[i].prepare(sectorPlaneIndex);
}
return (edges[0].shadowStrength(shadowDark) >= .0001 && edges[1].shadowStrength(shadowDark) >= .0001);
}
static DrawList::Indices makeFlatShadowGeometry(Store &verts, gl::Primitive &primitive,
ShadowEdge const edges[2], dfloat shadowDark, bool haveFloor)
{
static duint const floorOrder[][4] = { { 0, 1, 2, 3 }, { 1, 2, 3, 0 } };
static duint const ceilOrder [][4] = { { 0, 3, 2, 1 }, { 1, 0, 3, 2 } };
static Vector4ub const white(255, 255, 255, 0);
static Vector4ub const black( 0, 0, 0, 0);
// What vertex winding order (0 = left, 1 = right)? (For best results, the cross edge
// should always be the shortest.)
duint const winding = (edges[1].length() > edges[0].length()? 1 : 0);
duint const *order = (haveFloor ? floorOrder[winding] : ceilOrder[winding]);
// Assign indices.
duint base = verts.allocateVertices(4);
DrawList::Indices indices;
indices.resize(4);
for(duint i = 0; i < 4; ++i)
{
indices[i] = base + i;
}
//
// Build the geometry.
//
primitive = gl::TriangleFan;
verts.posCoords[indices[order[0]]] = edges[0].outer();
verts.posCoords[indices[order[1]]] = edges[1].outer();
verts.posCoords[indices[order[2]]] = edges[1].inner();
verts.posCoords[indices[order[3]]] = edges[0].inner();
// Set uniform color.
Vector4ub const &uniformColor = (::renderWireframe? white : black); // White to assist visual debugging.
for(duint i = 0; i < 4; ++i)
{
verts.colorCoords[indices[i]] = uniformColor;
}
// Set outer edge opacity:
for(duint i = 0; i < 2; ++i)
{
verts.colorCoords[indices[order[i]]].w = dbyte( edges[i].shadowStrength(shadowDark) * 255 );
}