forked from hitmen047/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvraddetailprops.cpp
1037 lines (858 loc) · 30.6 KB
/
vraddetailprops.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $Revision: $
// $NoKeywords: $
//
// This file contains code to allow us to associate client data with bsp leaves.
//
//=============================================================================//
#include "vrad.h"
#include "Bsplib.h"
#include "GameBSPFile.h"
#include "UtlBuffer.h"
#include "utlvector.h"
#include "CModel.h"
#include "studio.h"
#include "pacifier.h"
#include "vraddetailprops.h"
#include "mathlib/halton.h"
#include "messbuf.h"
#include "byteswap.h"
bool LoadStudioModel( char const* pModelName, CUtlBuffer& buf );
//-----------------------------------------------------------------------------
// Purpose: Writes a glview text file containing the collision surface in question
// Input : *pCollide -
// *pFilename -
//-----------------------------------------------------------------------------
void DumpRayToGlView( Ray_t const& ray, float dist, Vector* pColor, const char *pFilename )
{
Vector dir = ray.m_Delta;
float len = VectorNormalize(dir);
if (len < 1e-3)
return;
Vector up( 0, 0, 1 );
Vector crossDir;
if (fabs(DotProduct(up, dir)) - 1.0f < -1e-3 )
{
CrossProduct( dir, up, crossDir );
VectorNormalize(crossDir);
}
else
{
up.Init( 0, 1, 0 );
CrossProduct( dir, up, crossDir );
VectorNormalize(crossDir);
}
Vector end;
Vector start1, start2;
VectorMA( ray.m_Start, dist, ray.m_Delta, end );
VectorMA( ray.m_Start, -2, crossDir, start1 );
VectorMA( ray.m_Start, 2, crossDir, start2 );
FileHandle_t fp = g_pFileSystem->Open( pFilename, "a" );
int vert = 0;
CmdLib_FPrintf( fp, "3\n" );
CmdLib_FPrintf( fp, "%6.3f %6.3f %6.3f %.2f %.2f %.2f\n", start1.x, start1.y, start1.z,
pColor->x, pColor->y, pColor->z );
vert++;
CmdLib_FPrintf( fp, "%6.3f %6.3f %6.3f %.2f %.2f %.2f\n", start2.x, start2.y, start2.z,
pColor->x, pColor->y, pColor->z );
vert++;
CmdLib_FPrintf( fp, "%6.3f %6.3f %6.3f %.2f %.2f %.2f\n", end.x, end.y, end.z,
pColor->x, pColor->y, pColor->z );
vert++;
g_pFileSystem->Close( fp );
}
//-----------------------------------------------------------------------------
// This puppy is used to construct the game lumps
//-----------------------------------------------------------------------------
static CUtlVector<DetailPropLightstylesLump_t> s_DetailPropLightStyleLumpLDR;
static CUtlVector<DetailPropLightstylesLump_t> s_DetailPropLightStyleLumpHDR;
static CUtlVector<DetailPropLightstylesLump_t> *s_pDetailPropLightStyleLump = &s_DetailPropLightStyleLumpLDR;
//-----------------------------------------------------------------------------
// An amount to add to each model to get to the model center
//-----------------------------------------------------------------------------
CUtlVector<Vector> g_ModelCenterOffset;
CUtlVector<Vector> g_SpriteCenterOffset;
void VRadDetailProps_SetHDRMode( bool bHDR )
{
if( bHDR )
{
s_pDetailPropLightStyleLump = &s_DetailPropLightStyleLumpHDR;
}
else
{
s_pDetailPropLightStyleLump = &s_DetailPropLightStyleLumpLDR;
}
}
//-----------------------------------------------------------------------------
// Finds ambient sky lights
//-----------------------------------------------------------------------------
static directlight_t* FindAmbientSkyLight()
{
static directlight_t *s_pCachedSkylight = NULL;
// Don't keep searching for the same light.
if ( !s_pCachedSkylight )
{
// find any ambient lights
directlight_t* dl;
for (dl = activelights; dl != 0; dl = dl->next)
{
if (dl->light.type == emit_skyambient)
{
s_pCachedSkylight = dl;
break;
}
}
}
return s_pCachedSkylight;
}
//-----------------------------------------------------------------------------
// Compute world center of a prop
//-----------------------------------------------------------------------------
static void ComputeWorldCenter( DetailObjectLump_t& prop, Vector& center, Vector& normal )
{
// Transform the offset into world space
Vector forward, right;
AngleVectors( prop.m_Angles, &forward, &right, &normal );
VectorCopy( prop.m_Origin, center );
// FIXME: Take orientation into account?
switch (prop.m_Type )
{
case DETAIL_PROP_TYPE_MODEL:
VectorMA( center, g_ModelCenterOffset[prop.m_DetailModel].x, forward, center );
VectorMA( center, -g_ModelCenterOffset[prop.m_DetailModel].y, right, center );
VectorMA( center, g_ModelCenterOffset[prop.m_DetailModel].z, normal, center );
break;
case DETAIL_PROP_TYPE_SPRITE:
Vector vecOffset;
VectorMultiply( g_SpriteCenterOffset[prop.m_DetailModel], prop.m_flScale, vecOffset );
VectorMA( center, vecOffset.x, forward, center );
VectorMA( center, -vecOffset.y, right, center );
VectorMA( center, vecOffset.z, normal, center );
break;
}
}
//-----------------------------------------------------------------------------
// Computes max direct lighting for a single detal prop
//-----------------------------------------------------------------------------
static void ComputeMaxDirectLighting( DetailObjectLump_t& prop, Vector* maxcolor, int iThread )
{
// The max direct lighting must be along the direction to one
// of the static lights....
Vector origin, normal;
ComputeWorldCenter( prop, origin, normal );
if ( !origin.IsValid() || !normal.IsValid() )
{
static bool s_Warned = false;
if ( !s_Warned )
{
Warning("WARNING: Bogus detail props encountered!\n" );
s_Warned = true;
}
// fill with debug color
for ( int i = 0; i < MAX_LIGHTSTYLES; ++i)
{
maxcolor[i].Init(1,0,0);
}
return;
}
int cluster = ClusterFromPoint(origin);
Vector delta;
CUtlVector< directlight_t* > lights;
CUtlVector< Vector > directions;
directlight_t* dl;
for (dl = activelights; dl != 0; dl = dl->next)
{
// skyambient doesn't affect dlights..
if (dl->light.type == emit_skyambient)
continue;
// is this lights cluster visible?
if ( PVSCheck( dl->pvs, cluster ) )
{
lights.AddToTail(dl);
VectorSubtract( dl->light.origin, origin, delta );
VectorNormalize( delta );
directions.AddToTail( delta );
}
}
// Find the max illumination
int i;
for ( i = 0; i < MAX_LIGHTSTYLES; ++i)
{
maxcolor[i].Init(0,0,0);
}
// NOTE: See version 10 for a method where we choose a normal based on whichever
// one produces the maximum possible illumination. This appeared to work better on
// e3_town, so I'm trying it now; hopefully it'll be good for all cases.
int j;
for ( j = 0; j < lights.Count(); ++j)
{
dl = lights[j];
SSE_sampleLightOutput_t out;
FourVectors origin4;
FourVectors normal4;
origin4.DuplicateVector( origin );
normal4.DuplicateVector( normal );
GatherSampleLightSSE ( out, dl, -1, origin4, &normal4, 1, iThread );
VectorMA( maxcolor[dl->light.style], out.m_flFalloff.m128_f32[0] * out.m_flDot[0].m128_f32[0], dl->light.intensity, maxcolor[dl->light.style] );
}
}
//-----------------------------------------------------------------------------
// Computes the ambient term from a particular surface
//-----------------------------------------------------------------------------
static void ComputeAmbientFromSurface( dface_t* pFace, directlight_t* pSkylight,
Vector& radcolor )
{
texinfo_t* pTex = &texinfo[pFace->texinfo];
if (pTex)
{
// If we hit the sky, use the sky ambient
if (pTex->flags & SURF_SKY)
{
if (pSkylight)
{
// add in sky ambient
VectorDivide( pSkylight->light.intensity, 255.0f, radcolor );
}
}
else
{
VectorMultiply( radcolor, dtexdata[pTex->texdata].reflectivity, radcolor );
}
}
}
//-----------------------------------------------------------------------------
// Computes the lightmap color at a particular point
//-----------------------------------------------------------------------------
static void ComputeLightmapColorFromAverage( dface_t* pFace, directlight_t* pSkylight, float scale, Vector pColor[MAX_LIGHTSTYLES] )
{
texinfo_t* pTex = &texinfo[pFace->texinfo];
if (pTex->flags & SURF_SKY)
{
if (pSkylight)
{
// add in sky ambient
Vector amb = pSkylight->light.intensity / 255.0f;
pColor[0] += amb * scale;
}
return;
}
for (int maps = 0 ; maps < MAXLIGHTMAPS && pFace->styles[maps] != 255 ; ++maps)
{
ColorRGBExp32* pAvgColor = dface_AvgLightColor( pFace, maps );
// this code expects values from [0..1] not [0..255]
Vector color;
color[0] = TexLightToLinear( pAvgColor->r, pAvgColor->exponent );
color[1] = TexLightToLinear( pAvgColor->g, pAvgColor->exponent );
color[2] = TexLightToLinear( pAvgColor->b, pAvgColor->exponent );
ComputeAmbientFromSurface( pFace, pSkylight, color );
int style = pFace->styles[maps];
pColor[style] += color * scale;
}
}
//-----------------------------------------------------------------------------
// Returns true if the surface has bumped lightmaps
//-----------------------------------------------------------------------------
static bool SurfHasBumpedLightmaps( dface_t *pSurf )
{
bool hasBumpmap = false;
if( ( texinfo[pSurf->texinfo].flags & SURF_BUMPLIGHT ) &&
( !( texinfo[pSurf->texinfo].flags & SURF_NOLIGHT ) ) )
{
hasBumpmap = true;
}
return hasBumpmap;
}
//-----------------------------------------------------------------------------
// Computes the lightmap color at a particular point
//-----------------------------------------------------------------------------
static void ComputeLightmapColorPointSample( dface_t* pFace, directlight_t* pSkylight, Vector2D const& luv, float scale, Vector pColor[MAX_LIGHTSTYLES] )
{
// face unaffected by light
if (pFace->lightofs == -1 )
return;
int smax = ( pFace->m_LightmapTextureSizeInLuxels[0] ) + 1;
int tmax = ( pFace->m_LightmapTextureSizeInLuxels[1] ) + 1;
// luv is in the space of the accumulated lightmap page; we need to convert
// it to be in the space of the surface
int ds = clamp( (int)luv.x, 0, smax-1 );
int dt = clamp( (int)luv.y, 0, tmax-1 );
int offset = smax * tmax;
if ( SurfHasBumpedLightmaps( pFace ) )
offset *= ( NUM_BUMP_VECTS + 1 );
ColorRGBExp32* pLightmap = (ColorRGBExp32*)&pdlightdata->Base()[pFace->lightofs];
pLightmap += dt * smax + ds;
for (int maps = 0 ; maps < MAXLIGHTMAPS && pFace->styles[maps] != 255 ; ++maps)
{
int style = pFace->styles[maps];
Vector color;
color[0] = TexLightToLinear( pLightmap->r, pLightmap->exponent );
color[1] = TexLightToLinear( pLightmap->g, pLightmap->exponent );
color[2] = TexLightToLinear( pLightmap->b, pLightmap->exponent );
ComputeAmbientFromSurface( pFace, pSkylight, color );
pColor[style] += color * scale;
pLightmap += offset;
}
}
//-----------------------------------------------------------------------------
// Tests a particular node
//-----------------------------------------------------------------------------
class CLightSurface : public IBSPNodeEnumerator
{
public:
CLightSurface(int iThread) : m_pSurface(0), m_HitFrac(1.0f), m_bHasLuxel(false), m_iThread(iThread) {}
// call back with a node and a context
bool EnumerateNode( int node, Ray_t const& ray, float f, int context )
{
dface_t* pSkySurface = 0;
// Compute the actual point
Vector pt;
VectorMA( ray.m_Start, f, ray.m_Delta, pt );
dnode_t* pNode = &dnodes[node];
dface_t* pFace = &g_pFaces[pNode->firstface];
for (int i=0 ; i < pNode->numfaces ; ++i, ++pFace)
{
// Don't take into account faces that are int a leaf
if ( !pFace->onNode )
continue;
// Don't test displacement faces
if ( pFace->dispinfo != -1 )
continue;
texinfo_t* pTex = &texinfo[pFace->texinfo];
// Don't immediately return when we hit sky;
// we may actually hit another surface
if (pTex->flags & SURF_SKY)
{
if (TestPointAgainstSkySurface( pt, pFace ))
{
pSkySurface = pFace;
}
continue;
}
if (TestPointAgainstSurface( pt, pFace, pTex ))
{
m_HitFrac = f;
m_pSurface = pFace;
m_bHasLuxel = true;
return false;
}
}
// if we hit a sky surface, return it
m_pSurface = pSkySurface;
return (m_pSurface == 0);
}
// call back with a leaf and a context
virtual bool EnumerateLeaf( int leaf, Ray_t const& ray, float start, float end, int context )
{
bool hit = false;
dleaf_t* pLeaf = &dleafs[leaf];
for (int i=0 ; i < pLeaf->numleaffaces ; ++i)
{
Assert( pLeaf->firstleafface + i < numleaffaces );
Assert( dleaffaces[pLeaf->firstleafface + i] < numfaces );
dface_t* pFace = &g_pFaces[dleaffaces[pLeaf->firstleafface + i]];
// Don't test displacement faces; we need to check another list
if ( pFace->dispinfo != -1 )
continue;
// Don't take into account faces that are on a node
if ( pFace->onNode )
continue;
// Find intersection point against detail brushes
texinfo_t* pTex = &texinfo[pFace->texinfo];
dplane_t* pPlane = &dplanes[pFace->planenum];
// Backface cull...
if (DotProduct( pPlane->normal, ray.m_Delta ) > 0)
continue;
float startDotN = DotProduct( ray.m_Start, pPlane->normal );
float deltaDotN = DotProduct( ray.m_Delta, pPlane->normal );
float front = startDotN + start * deltaDotN - pPlane->dist;
float back = startDotN + end * deltaDotN - pPlane->dist;
int side = front < 0;
// Blow it off if it doesn't split the plane...
if ( (back < 0) == side )
continue;
// Don't test a surface that is farther away from the closest found intersection
float f = front / (front-back);
float mid = start * (1.0f - f) + end * f;
if (mid >= m_HitFrac)
continue;
Vector pt;
VectorMA( ray.m_Start, mid, ray.m_Delta, pt );
if (TestPointAgainstSurface( pt, pFace, pTex ))
{
m_HitFrac = mid;
m_pSurface = pFace;
hit = true;
m_bHasLuxel = true;
}
}
// Now try to clip against all displacements in the leaf
float dist;
Vector2D luxelCoord;
dface_t *pDispFace;
StaticDispMgr()->ClipRayToDispInLeaf( s_DispTested[m_iThread], ray, leaf, dist, pDispFace, luxelCoord );
if (dist < m_HitFrac)
{
m_HitFrac = dist;
m_pSurface = pDispFace;
Vector2DCopy( luxelCoord, m_LuxelCoord );
hit = true;
m_bHasLuxel = true;
}
return !hit;
}
bool FindIntersection( Ray_t const& ray )
{
StaticDispMgr()->StartRayTest( s_DispTested[m_iThread] );
return !EnumerateNodesAlongRay( ray, this, 0 );
}
private:
bool TestPointAgainstSurface( Vector const& pt, dface_t* pFace, texinfo_t* pTex )
{
// no lightmaps on this surface? punt...
// FIXME: should be water surface?
if (pTex->flags & SURF_NOLIGHT)
return false;
// See where in lightmap space our intersection point is
float s, t;
s = DotProduct (pt.Base(), pTex->lightmapVecsLuxelsPerWorldUnits[0]) +
pTex->lightmapVecsLuxelsPerWorldUnits[0][3];
t = DotProduct (pt.Base(), pTex->lightmapVecsLuxelsPerWorldUnits[1]) +
pTex->lightmapVecsLuxelsPerWorldUnits[1][3];
// Not in the bounds of our lightmap? punt...
if( s < pFace->m_LightmapTextureMinsInLuxels[0] || t < pFace->m_LightmapTextureMinsInLuxels[1] )
return false;
// assuming a square lightmap (FIXME: which ain't always the case),
// lets see if it lies in that rectangle. If not, punt...
float ds = s - pFace->m_LightmapTextureMinsInLuxels[0];
float dt = t - pFace->m_LightmapTextureMinsInLuxels[1];
if( ds > pFace->m_LightmapTextureSizeInLuxels[0] || dt > pFace->m_LightmapTextureSizeInLuxels[1] )
return false;
m_LuxelCoord.x = ds;
m_LuxelCoord.y = dt;
return true;
}
bool TestPointAgainstSkySurface( Vector const &pt, dface_t *pFace )
{
// Create sky face winding.
Vector v( 0.0f, 0.0f, 0.0f );
winding_t *pWinding = WindingFromFace( pFace, v );
// Test point in winding. (Since it is at the node, it is in the plane.)
bool bRet = PointInWinding( pt, pWinding );
FreeWinding( pWinding );
return bRet;
}
public:
int m_iThread;
dface_t* m_pSurface;
float m_HitFrac;
Vector2D m_LuxelCoord;
bool m_bHasLuxel;
};
bool CastRayInLeaf( int iThread, const Vector &start, const Vector &end, int leafIndex, float *pFraction, Vector *pNormal )
{
pFraction[0] = 1.0f;
Ray_t ray;
ray.Init( start, end, vec3_origin, vec3_origin );
CBaseTrace trace;
if ( TraceLeafBrushes( leafIndex, start, end, trace ) != 1.0f )
{
pFraction[0] = trace.fraction;
*pNormal = trace.plane.normal;
}
else
{
Assert(!trace.startsolid && !trace.allsolid);
}
StaticDispMgr()->StartRayTest( s_DispTested[iThread] );
// Now try to clip against all displacements in the leaf
float dist;
Vector normal;
StaticDispMgr()->ClipRayToDispInLeaf( s_DispTested[iThread], ray, leafIndex, dist, &normal );
if ( dist < pFraction[0] )
{
pFraction[0] = dist;
*pNormal = normal;
}
return pFraction[0] != 1.0f ? true : false;
}
//-----------------------------------------------------------------------------
// Computes ambient lighting along a specified ray.
// Ray represents a cone, tanTheta is the tan of the inner cone angle
//-----------------------------------------------------------------------------
void CalcRayAmbientLighting( int iThread, const Vector &vStart, const Vector &vEnd, float tanTheta, Vector color[MAX_LIGHTSTYLES] )
{
Ray_t ray;
ray.Init( vStart, vEnd, vec3_origin, vec3_origin );
directlight_t *pSkyLight = FindAmbientSkyLight();
CLightSurface surfEnum(iThread);
if (!surfEnum.FindIntersection( ray ))
return;
// compute the approximate radius of a circle centered around the intersection point
float dist = ray.m_Delta.Length() * tanTheta * surfEnum.m_HitFrac;
// until 20" we use the point sample, then blend in the average until we're covering 40"
// This is attempting to model the ray as a cone - in the ideal case we'd simply sample all
// luxels in the intersection of the cone with the surface. Since we don't have surface
// neighbor information computed we'll just approximate that sampling with a blend between
// a point sample and the face average.
// This yields results that are similar in that aliasing is reduced at distance while
// point samples provide accuracy for intersections with near geometry
float scaleAvg = RemapValClamped( dist, 20, 40, 0.0f, 1.0f );
if ( !surfEnum.m_bHasLuxel )
{
// don't have luxel UV, so just use average sample
scaleAvg = 1.0;
}
float scaleSample = 1.0f - scaleAvg;
if (scaleAvg != 0)
{
ComputeLightmapColorFromAverage( surfEnum.m_pSurface, pSkyLight, scaleAvg, color );
}
if (scaleSample != 0)
{
ComputeLightmapColorPointSample( surfEnum.m_pSurface, pSkyLight, surfEnum.m_LuxelCoord, scaleSample, color );
}
}
//-----------------------------------------------------------------------------
// Compute ambient lighting component at specified position.
//-----------------------------------------------------------------------------
static void ComputeAmbientLightingAtPoint( int iThread, const Vector &origin, Vector radcolor[NUMVERTEXNORMALS], Vector color[MAX_LIGHTSTYLES] )
{
// NOTE: I'm not dealing with shadow-casting static props here
// This is for speed, although we can add it if it turns out to
// be important
// sample world by casting N rays distributed across a sphere
Vector upend;
int j;
for ( j = 0; j < MAX_LIGHTSTYLES; ++j)
{
color[j].Init( 0,0,0 );
}
float tanTheta = tan(VERTEXNORMAL_CONE_INNER_ANGLE);
for (int i = 0; i < NUMVERTEXNORMALS; i++)
{
VectorMA( origin, COORD_EXTENT * 1.74, g_anorms[i], upend );
// Now that we've got a ray, see what surface we've hit
CalcRayAmbientLighting( iThread, origin, upend, tanTheta, color );
// DumpRayToGlView( ray, surfEnum.m_HitFrac, &color[0], "test.out" );
}
for ( j = 0; j < MAX_LIGHTSTYLES; ++j)
{
VectorMultiply( color[j], 255.0f / (float)NUMVERTEXNORMALS, color[j] );
}
}
//-----------------------------------------------------------------------------
// Trace hemispherical rays from a vertex, accumulating indirect
// sources at each ray termination.
//-----------------------------------------------------------------------------
void ComputeIndirectLightingAtPoint( Vector &position, Vector &normal, Vector &outColor,
int iThread, bool force_fast, bool bIgnoreNormals )
{
Ray_t ray;
CLightSurface surfEnum(iThread);
outColor.Init();
int nSamples = NUMVERTEXNORMALS;
if ( do_fast || force_fast )
nSamples /= 4;
else
nSamples *= g_flSkySampleScale;
float totalDot = 0;
DirectionalSampler_t sampler;
for (int j = 0; j < nSamples; j++)
{
Vector samplingNormal = sampler.NextValue();
float dot;
if ( bIgnoreNormals )
dot = (0.7071/2);
else
dot = DotProduct( normal, samplingNormal );
if ( dot <= EQUAL_EPSILON )
{
// reject angles behind our plane
continue;
}
totalDot += dot;
// trace to determine surface
Vector vEnd;
VectorScale( samplingNormal, MAX_TRACE_LENGTH, vEnd );
VectorAdd( position, vEnd, vEnd );
ray.Init( position, vEnd, vec3_origin, vec3_origin );
if ( !surfEnum.FindIntersection( ray ) )
continue;
// get color from surface lightmap
texinfo_t* pTex = &texinfo[surfEnum.m_pSurface->texinfo];
if ( !pTex || pTex->flags & SURF_SKY )
{
// ignore contribution from sky
// sky ambient already accounted for during direct pass
continue;
}
if ( surfEnum.m_pSurface->styles[0] == 255 || surfEnum.m_pSurface->lightofs < 0 )
{
// no light affects this face
continue;
}
Vector lightmapColor;
if ( !surfEnum.m_bHasLuxel )
{
ColorRGBExp32* pAvgLightmapColor = dface_AvgLightColor( surfEnum.m_pSurface, 0 );
ColorRGBExp32ToVector( *pAvgLightmapColor, lightmapColor );
}
else
{
// get color from displacement
int smax = ( surfEnum.m_pSurface->m_LightmapTextureSizeInLuxels[0] ) + 1;
int tmax = ( surfEnum.m_pSurface->m_LightmapTextureSizeInLuxels[1] ) + 1;
// luxelcoord is in the space of the accumulated lightmap page; we need to convert
// it to be in the space of the surface
int ds = clamp( (int)surfEnum.m_LuxelCoord.x, 0, smax-1 );
int dt = clamp( (int)surfEnum.m_LuxelCoord.y, 0, tmax-1 );
ColorRGBExp32* pLightmap = (ColorRGBExp32*)&(*pdlightdata)[surfEnum.m_pSurface->lightofs];
pLightmap += dt * smax + ds;
ColorRGBExp32ToVector( *pLightmap, lightmapColor );
}
float invLengthSqr = 1.0f / (1.0f + ((vEnd - position) * surfEnum.m_HitFrac / 128.0).LengthSqr());
// Include falloff using invsqrlaw.
VectorMultiply( lightmapColor, invLengthSqr * dtexdata[pTex->texdata].reflectivity, lightmapColor );
VectorAdd( outColor, lightmapColor, outColor );
}
if ( totalDot )
{
VectorScale( outColor, 1.0f/totalDot, outColor );
}
}
static void ComputeAmbientLighting( int iThread, DetailObjectLump_t& prop, Vector color[MAX_LIGHTSTYLES] )
{
Vector origin, normal;
ComputeWorldCenter( prop, origin, normal );
if ( !origin.IsValid() || !normal.IsValid() )
{
static bool s_Warned = false;
if ( !s_Warned )
{
Warning("WARNING: Bogus detail props encountered!\n" );
s_Warned = true;
}
// fill with debug color
for ( int i = 0; i < MAX_LIGHTSTYLES; ++i)
{
color[i].Init(1,0,0);
}
return;
}
Vector radcolor[NUMVERTEXNORMALS];
ComputeAmbientLightingAtPoint( iThread, origin, radcolor, color );
}
//-----------------------------------------------------------------------------
// Computes lighting for a single detal prop
//-----------------------------------------------------------------------------
static void ComputeLighting( DetailObjectLump_t& prop, int iThread )
{
// We're going to take the maximum of the ambient lighting and
// the strongest directional light. This works because we're assuming
// the props will have built-in faked lighting.
Vector directColor[MAX_LIGHTSTYLES];
Vector ambColor[MAX_LIGHTSTYLES];
// Get the max influence of all direct lights
ComputeMaxDirectLighting( prop, directColor, iThread );
// Get the ambient lighting + lightstyles
ComputeAmbientLighting( iThread, prop, ambColor );
// Base lighting
Vector totalColor;
VectorAdd( directColor[0], ambColor[0], totalColor );
VectorToColorRGBExp32( totalColor, prop.m_Lighting );
bool hasLightstyles = false;
prop.m_LightStyleCount = 0;
// lightstyles
for (int i = 1; i < MAX_LIGHTSTYLES; ++i )
{
VectorAdd( directColor[i], ambColor[i], totalColor );
totalColor *= 0.5f;
if ((totalColor[0] != 0.0f) || (totalColor[1] != 0.0f) ||
(totalColor[2] != 0.0f) )
{
if (!hasLightstyles)
{
prop.m_LightStyles = s_pDetailPropLightStyleLump->Size();
hasLightstyles = true;
}
int j = s_pDetailPropLightStyleLump->AddToTail();
VectorToColorRGBExp32( totalColor, (*s_pDetailPropLightStyleLump)[j].m_Lighting );
(*s_pDetailPropLightStyleLump)[j].m_Style = i;
++prop.m_LightStyleCount;
}
}
}
//-----------------------------------------------------------------------------
// Unserialization
//-----------------------------------------------------------------------------
static void UnserializeModelDict( CUtlBuffer& buf )
{
// Get origin offset for each model...
int count = buf.GetInt();
while ( --count >= 0 )
{
DetailObjectDictLump_t lump;
buf.Get( &lump, sizeof(DetailObjectDictLump_t) );
int i = g_ModelCenterOffset.AddToTail();
CUtlBuffer mdlbuf;
if (LoadStudioModel( lump.m_Name, mdlbuf ))
{
studiohdr_t* pHdr = (studiohdr_t*)mdlbuf.Base();
VectorAdd( pHdr->hull_min, pHdr->hull_max, g_ModelCenterOffset[i] );
g_ModelCenterOffset[i] *= 0.5f;
}
else
{
g_ModelCenterOffset[i].Init(0,0,0);
}
}
}
static void UnserializeSpriteDict( CUtlBuffer& buf )
{
// Get origin offset for each model...
int count = buf.GetInt();
while ( --count >= 0 )
{
DetailSpriteDictLump_t lump;
buf.Get( &lump, sizeof(DetailSpriteDictLump_t) );
// For these sprites, x goes out the front, y right, z up
int i = g_SpriteCenterOffset.AddToTail();
g_SpriteCenterOffset[i].x = 0.0f;
g_SpriteCenterOffset[i].y = lump.m_LR.x + lump.m_UL.x;
g_SpriteCenterOffset[i].z = lump.m_LR.y + lump.m_UL.y;
g_SpriteCenterOffset[i] *= 0.5f;
}
}
//-----------------------------------------------------------------------------
// Unserializes the detail props
//-----------------------------------------------------------------------------
static int UnserializeDetailProps( DetailObjectLump_t*& pProps )
{
GameLumpHandle_t handle = g_GameLumps.GetGameLumpHandle( GAMELUMP_DETAIL_PROPS );
if (g_GameLumps.GetGameLumpVersion(handle) != GAMELUMP_DETAIL_PROPS_VERSION)
return 0;
// Unserialize
CUtlBuffer buf( g_GameLumps.GetGameLump(handle), g_GameLumps.GameLumpSize( handle ), CUtlBuffer::READ_ONLY );
UnserializeModelDict( buf );
UnserializeSpriteDict( buf );
// Now we're pointing to the detail prop data
// This actually works because the scope of the game lump data
// is global and the buf was just pointing to it.
int count = buf.GetInt();
if (count)
{
pProps = (DetailObjectLump_t*)buf.PeekGet();
}
else
{
pProps = 0;
}
return count;
}
//-----------------------------------------------------------------------------
// Writes the detail lighting lump
//-----------------------------------------------------------------------------
static void WriteDetailLightingLump( int lumpID, int lumpVersion, CUtlVector<DetailPropLightstylesLump_t> &lumpData )
{
GameLumpHandle_t handle = g_GameLumps.GetGameLumpHandle(lumpID);
if (handle != g_GameLumps.InvalidGameLump())
g_GameLumps.DestroyGameLump(handle);
int lightsize = lumpData.Size() * sizeof(DetailPropLightstylesLump_t);
int lumpsize = lightsize + sizeof(int);
handle = g_GameLumps.CreateGameLump( lumpID, lumpsize, 0, lumpVersion );
// Serialize the data
CUtlBuffer buf( g_GameLumps.GetGameLump(handle), lumpsize );
buf.PutInt( lumpData.Size() );
if (lightsize)
buf.Put( lumpData.Base(), lightsize );
}
static void WriteDetailLightingLumps( void )
{
WriteDetailLightingLump( GAMELUMP_DETAIL_PROP_LIGHTING, GAMELUMP_DETAIL_PROP_LIGHTING_VERSION, s_DetailPropLightStyleLumpLDR );
WriteDetailLightingLump( GAMELUMP_DETAIL_PROP_LIGHTING_HDR, GAMELUMP_DETAIL_PROP_LIGHTING_HDR_VERSION, s_DetailPropLightStyleLumpHDR );
}
// need to do this so that if we are building HDR data, the LDR data is intact, and vice versa.s
void UnserializeDetailPropLighting( int lumpID, int lumpVersion, CUtlVector<DetailPropLightstylesLump_t> &lumpData )
{
GameLumpHandle_t handle = g_GameLumps.GetGameLumpHandle( lumpID );
if( handle == g_GameLumps.InvalidGameLump() )
{
return;
}
if (g_GameLumps.GetGameLumpVersion(handle) != lumpVersion)
return;
// Unserialize
CUtlBuffer buf( g_GameLumps.GetGameLump(handle), g_GameLumps.GameLumpSize( handle ), CUtlBuffer::READ_ONLY );
int count = buf.GetInt();
if( !count )
{
return;
}
lumpData.SetCount( count );
int lightsize = lumpData.Size() * sizeof(DetailPropLightstylesLump_t);
buf.Get( lumpData.Base(), lightsize );
}
DetailObjectLump_t *g_pMPIDetailProps = NULL;
void VMPI_ProcessDetailPropWU( int iThread, int iWorkUnit, MessageBuffer *pBuf )
{
CUtlVector<DetailPropLightstylesLump_t> *pDetailPropLump = s_pDetailPropLightStyleLump;
DetailObjectLump_t& prop = g_pMPIDetailProps[iWorkUnit];
ComputeLighting( prop, iThread );
// Send the results back...
pBuf->write( &prop.m_Lighting, sizeof( prop.m_Lighting ) );
pBuf->write( &prop.m_LightStyleCount, sizeof( prop.m_LightStyleCount ) );
pBuf->write( &prop.m_LightStyles, sizeof( prop.m_LightStyles ) );
for ( int i=0; i < prop.m_LightStyleCount; i++ )
{
DetailPropLightstylesLump_t *l = &pDetailPropLump->Element( i + prop.m_LightStyles );
pBuf->write( &l->m_Lighting, sizeof( l->m_Lighting ) );
pBuf->write( &l->m_Style, sizeof( l->m_Style ) );
}
}
void VMPI_ReceiveDetailPropWU( int iWorkUnit, MessageBuffer *pBuf, int iWorker )
{
CUtlVector<DetailPropLightstylesLump_t> *pDetailPropLump = s_pDetailPropLightStyleLump;
DetailObjectLump_t& prop = g_pMPIDetailProps[iWorkUnit];
pBuf->read( &prop.m_Lighting, sizeof( prop.m_Lighting ) );
pBuf->read( &prop.m_LightStyleCount, sizeof( prop.m_LightStyleCount ) );
pBuf->read( &prop.m_LightStyles, sizeof( prop.m_LightStyles ) );
pDetailPropLump->EnsureCount( prop.m_LightStyles + prop.m_LightStyleCount );
for ( int i=0; i < prop.m_LightStyleCount; i++ )
{
DetailPropLightstylesLump_t *l = &pDetailPropLump->Element( i + prop.m_LightStyles );
pBuf->read( &l->m_Lighting, sizeof( l->m_Lighting ) );