-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlight.c
2770 lines (2347 loc) · 73 KB
/
light.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -------------------------------------------------------------------------------
Copyright (C) 1999-2006 Id Software, Inc. and contributors.
For a list of contributors, see the accompanying CONTRIBUTORS file.
This file is part of GtkRadiant.
GtkRadiant 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.
GtkRadiant 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 GtkRadiant; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
----------------------------------------------------------------------------------
This code has been altered significantly from its original form, to support
several games based on the Quake III Arena engine, in the form of "Q3Map2."
------------------------------------------------------------------------------- */
/* marker */
#define LIGHT_C
/* dependencies */
#include "kmap2.h"
/*
CreateSunLight() - ydnar
this creates a sun light
*/
static void CreateSunLight(sun_t * sun)
{
int i;
float photons, d, angle, elevation, da, de;
vec3_t direction;
light_t *light;
/* dummy check */
if(sun == NULL)
return;
/* fixup */
if(sun->numSamples < 1)
sun->numSamples = 1;
/* set photons */
photons = sun->photons / sun->numSamples;
/* create the right number of suns */
for(i = 0; i < sun->numSamples; i++)
{
/* calculate sun direction */
if(i == 0)
VectorCopy(sun->direction, direction);
else
{
/*
sun->direction[ 0 ] = cos( angle ) * cos( elevation );
sun->direction[ 1 ] = sin( angle ) * cos( elevation );
sun->direction[ 2 ] = sin( elevation );
xz_dist = sqrt( x*x + z*z )
latitude = atan2( xz_dist, y ) * RADIANS
longitude = atan2( x, z ) * RADIANS
*/
d = sqrt(sun->direction[0] * sun->direction[0] + sun->direction[1] * sun->direction[1]);
angle = atan2(sun->direction[1], sun->direction[0]);
elevation = atan2(sun->direction[2], d);
/* jitter the angles (loop to keep random sample within sun->deviance steridians) */
do
{
da = (Random() * 2.0f - 1.0f) * sun->deviance;
de = (Random() * 2.0f - 1.0f) * sun->deviance;
} while((da * da + de * de) > (sun->deviance * sun->deviance));
angle += da;
elevation += de;
/* debug code */
//% Sys_Printf( "%d: Angle: %3.4f Elevation: %3.3f\n", sun->numSamples, (angle / Q_PI * 180.0f), (elevation / Q_PI * 180.0f) );
/* create new vector */
direction[0] = cos(angle) * cos(elevation);
direction[1] = sin(angle) * cos(elevation);
direction[2] = sin(elevation);
}
/* create a light */
numSunLights++;
light = safe_malloc(sizeof(*light));
memset(light, 0, sizeof(*light));
light->next = lights;
lights = light;
/* initialize the light */
light->flags = LIGHT_SUN_DEFAULT;
light->type = EMIT_SUN;
light->fade = 1.0f;
light->falloffTolerance = falloffTolerance;
light->filterRadius = sun->filterRadius / sun->numSamples;
light->style = noStyles ? LS_NORMAL : sun->style;
/* set the light's position out to infinity */
VectorMA(vec3_origin, (MAX_WORLD_COORD * 8.0f), direction, light->origin); /* MAX_WORLD_COORD * 2.0f */
/* set the facing to be the inverse of the sun direction */
VectorScale(direction, -1.0, light->normal);
light->dist = DotProduct(light->origin, light->normal);
/* set color and photons */
VectorCopy(sun->color, light->color);
light->photons = photons * skyScale;
}
/* another sun? */
if(sun->next != NULL)
CreateSunLight(sun->next);
}
/*
CreateSkyLights() - ydnar
simulates sky light with multiple suns
*/
static void CreateSkyLights(vec3_t color, float value, int iterations, float filterRadius, int style)
{
int i, j, numSuns;
int angleSteps, elevationSteps;
float angle, elevation;
float angleStep, elevationStep;
float step, start;
sun_t sun;
/* dummy check */
if(value <= 0.0f || iterations < 2)
return;
/* calculate some stuff */
step = 2.0f / (iterations - 1);
start = -1.0f;
/* basic sun setup */
VectorCopy(color, sun.color);
sun.deviance = 0.0f;
sun.filterRadius = filterRadius;
sun.numSamples = 1;
sun.style = noStyles ? LS_NORMAL : style;
sun.next = NULL;
/* setup */
elevationSteps = iterations - 1;
angleSteps = elevationSteps * 4;
angle = 0.0f;
elevationStep = DEG2RAD(90.0f / iterations); /* skip elevation 0 */
angleStep = DEG2RAD(360.0f / angleSteps);
/* calc individual sun brightness */
numSuns = angleSteps * elevationSteps + 1;
sun.photons = value / numSuns;
/* iterate elevation */
elevation = elevationStep * 0.5f;
angle = 0.0f;
for(i = 0, elevation = elevationStep * 0.5f; i < elevationSteps; i++)
{
/* iterate angle */
for(j = 0; j < angleSteps; j++)
{
/* create sun */
sun.direction[0] = cos(angle) * cos(elevation);
sun.direction[1] = sin(angle) * cos(elevation);
sun.direction[2] = sin(elevation);
CreateSunLight(&sun);
/* move */
angle += angleStep;
}
/* move */
elevation += elevationStep;
angle += angleStep / elevationSteps;
}
/* create vertical sun */
VectorSet(sun.direction, 0.0f, 0.0f, 1.0f);
CreateSunLight(&sun);
/* short circuit */
return;
}
/*
CreateEntityLights()
creates lights from light entities
*/
void CreateEntityLights(void)
{
int i, j;
light_t *light, *light2;
entity_t *e, *e2;
const char *name;
const char *target, *keepLights;
vec3_t dest;
const char *_color;
float intensity, scale, deviance, filterRadius;
int spawnflags, flags, numSamples;
qboolean junior;
/* hypov8 add: stop duplicate lights */
keepLights = ValueForKey(&entities[0], "_keepLights");
/* go throught entity list and find lights */
for(i = 0; i < numEntities; i++)
{
/* get entity */
e = &entities[i];
name = ValueForKey(e, "classname");
/* ydnar: check for lightJunior */
if(Q_strncasecmp(name, "lightJunior", 11) == 0)
junior = qtrue;
else if(Q_strncasecmp(name, "light", 5) == 0)
junior = qfalse;
else
continue;
/* allow realtime only lights to be skipped */
if(IntForKey(e, "noradiosity") || IntForKey(e, "realtime")) //new name
continue;
/* remove lights from bsp if .map was loaded */
if (keepLights[0] != '1' && i < numBSPEntities)
{
/* remove without a target set */
target = ValueForKey(e, "targetname");
if (target[0] == '\0')
continue;
}
/* lights with target names (and therefore styles) are only parsed from BSP */
target = ValueForKey(e, "targetname"); //no longer used
if(target[0] != '\0' && i >= numBSPEntities)
continue;
/* create a light */
numPointLights++;
light = safe_malloc(sizeof(*light));
memset(light, 0, sizeof(*light));
light->next = lights;
lights = light;
/* handle spawnflags */
spawnflags = IntForKey(e, "spawnflags");
/* ydnar: quake 3+ light behavior */
if(wolfLight == qfalse)
{
/* set default flags */
flags = LIGHT_Q3A_DEFAULT;
/* linear attenuation? */
if(spawnflags & 1
|| IntForKey(e, "_linear")) /* hypov8 add: darkradiant limitation */
{
flags |= LIGHT_ATTEN_LINEAR;
flags &= ~LIGHT_ATTEN_ANGLE;
}
/* no angle attenuate? */
if (spawnflags & 2
||IntForKey(e, "_attangle")) /* hypov8 add: darkradiant limitation */
{
flags &= ~LIGHT_ATTEN_ANGLE;
}
}
/* ydnar: wolf light behavior */
else
{
/* set default flags */
flags = LIGHT_WOLF_DEFAULT;
/* inverse distance squared attenuation? */
if(spawnflags & 1)
{
flags &= ~LIGHT_ATTEN_LINEAR;
flags |= LIGHT_ATTEN_ANGLE;
}
/* angle attenuate? */
if(spawnflags & 2)
flags |= LIGHT_ATTEN_ANGLE;
}
/* other flags (borrowed from wolf) */
/* wolf dark light? */
if((spawnflags & 4) || (spawnflags & 8))
flags |= LIGHT_DARK;
/* nogrid? */
if(spawnflags & 16
|| IntForKey(e, "_nogridlight")) /* hypov8 add: darkradiant limitation */
{
flags &= ~LIGHT_GRID;
}
/* junior? */
if(junior)
{
flags |= LIGHT_GRID;
flags &= ~LIGHT_SURFACES;
}
/* vortex: unnormalized? */
if(spawnflags & 32)
flags |= LIGHT_UNNORMALIZED;
/* vortex: distance atten? */
if(spawnflags & 64)
flags |= LIGHT_ATTEN_DISTANCE;
/* store the flags */
light->flags = flags;
/* ydnar: set fade key (from wolf) */
light->fade = 1.0f;
if(light->flags & LIGHT_ATTEN_LINEAR)
{
light->fade = FloatForKey(e, "fade");
if(light->fade == 0.0f)
light->fade = 1.0f;
}
/* ydnar: set angle scaling (from vlight) */
light->angleScale = FloatForKey(e, "_anglescale");
if(light->angleScale != 0.0f)
light->flags |= LIGHT_ATTEN_ANGLE;
/* set origin */
GetVectorForKey(e, "origin", light->origin);
light->style = IntForKey(e, "_style");
if(light->style == LS_NORMAL)
light->style = IntForKey(e, "style");
if(light->style < LS_NORMAL || light->style >= LS_NONE)
Error("Invalid lightstyle (%d) on entity %d", light->style, i);
if(light->style != LS_NORMAL)
{
Sys_FPrintf(SYS_WRN, "WARNING: Styled light found targeting %s\n **", target);
}
/* set light intensity */
GetVectorForKey(e, "light_radius", light->radius);
intensity = VectorLength(light->radius);
if(!intensity)
intensity = FloatForKey(e, "_light");
if(intensity == 0.0f)
intensity = FloatForKey(e, "light");
if(intensity == 0.0f)
intensity = 300.0f;
/* ydnar: set light scale (sof2) */
scale = FloatForKey(e, "scale");
if(scale == 0.0f)
scale = 1.0f;
intensity *= scale;
/* ydnar: get deviance and samples */
deviance = FloatForKey(e, "_deviance");
if(deviance == 0.0f)
deviance = FloatForKey(e, "_deviation");
if(deviance == 0.0f)
deviance = FloatForKey(e, "_jitter");
numSamples = IntForKey(e, "_samples");
if(deviance < 0.0f || numSamples < 1)
{
deviance = 0.0f;
numSamples = 1;
}
intensity /= numSamples;
/* ydnar: get filter radius */
filterRadius = FloatForKey(e, "_filterradius");
if(filterRadius == 0.0f)
filterRadius = FloatForKey(e, "_filteradius");
if(filterRadius == 0.0f)
filterRadius = FloatForKey(e, "_filter");
if(filterRadius < 0.0f)
filterRadius = 0.0f;
light->filterRadius = filterRadius;
/* set light color */
_color = ValueForKey(e, "_color");
if(_color && _color[0])
{
sscanf(_color, "%f %f %f", &light->color[0], &light->color[1], &light->color[2]);
if(!(light->flags & LIGHT_UNNORMALIZED))
{
ColorNormalize(light->color, light->color);
}
}
else
light->color[0] = light->color[1] = light->color[2] = 1.0f;
light->extraDist = FloatForKey(e, "_extradist");
if(light->extraDist == 0.0f)
light->extraDist = extraDist;
intensity = intensity * pointScale;
light->photons = intensity;
light->type = EMIT_POINT;
/* set falloff threshold */
light->falloffTolerance = falloffTolerance / numSamples;
/* lights with a target will be spotlights */
target = ValueForKey(e, "target");
if(target[0])
{
float radius;
float dist;
sun_t sun;
const char *_sun;
/* get target */
e2 = FindTargetEntity(target);
if(e2 == NULL)
{
Sys_Printf("WARNING: light at (%i %i %i) has missing target\n",
(int)light->origin[0], (int)light->origin[1], (int)light->origin[2]);
}
else
{
/* not a point light */
numPointLights--;
numSpotLights++;
/* make a spotlight */
GetVectorForKey(e2, "origin", dest);
VectorSubtract(dest, light->origin, light->normal);
dist = VectorNormalize2(light->normal, light->normal);
radius = FloatForKey(e, "radius");
if(!radius)
radius = 64;
if(!dist)
dist = 64;
light->radiusByDist = (radius + 16) / dist;
light->type = EMIT_SPOT;
/* ydnar: wolf mods: spotlights always use nonlinear + angle attenuation */
light->flags &= ~LIGHT_ATTEN_LINEAR;
light->flags |= LIGHT_ATTEN_ANGLE;
light->fade = 1.0f;
/* ydnar: is this a sun? */
_sun = ValueForKey(e, "_sun");
if(_sun[0] == '1')
{
/* not a spot light */
numSpotLights--;
/* unlink this light */
lights = light->next;
/* make a sun */
VectorScale(light->normal, -1.0f, sun.direction);
VectorCopy(light->color, sun.color);
sun.photons = (intensity / pointScale);
sun.deviance = deviance / 180.0f * Q_PI;
sun.numSamples = numSamples;
sun.style = noStyles ? LS_NORMAL : light->style;
sun.next = NULL;
/* make a sun light */
CreateSunLight(&sun);
/* free original light */
free(light);
light = NULL;
/* skip the rest of this love story */
continue;
}
}
}
/* jitter the light */
for(j = 1; j < numSamples; j++)
{
/* create a light */
light2 = safe_malloc(sizeof(*light));
memcpy(light2, light, sizeof(*light));
light2->next = lights;
lights = light2;
/* add to counts */
if(light->type == EMIT_SPOT)
numSpotLights++;
else
numPointLights++;
/* jitter it */
light2->origin[0] = light->origin[0] + (Random() * 2.0f - 1.0f) * deviance;
light2->origin[1] = light->origin[1] + (Random() * 2.0f - 1.0f) * deviance;
light2->origin[2] = light->origin[2] + (Random() * 2.0f - 1.0f) * deviance;
}
}
}
/*
CreateSurfaceLights() - ydnar
this hijacks the radiosity code to generate surface lights for first pass
*/
#define APPROX_BOUNCE 1.0f
void CreateSurfaceLights(void)
{
int i;
bspDrawSurface_t *ds;
surfaceInfo_t *info;
shaderInfo_t *si;
light_t *light;
float subdivide;
vec3_t origin;
clipWork_t cw;
const char *nss;
/* get sun shader supressor */
nss = ValueForKey(&entities[0], "_noshadersun");
/* walk the list of surfaces */
for(i = 0; i < numBSPDrawSurfaces; i++)
{
/* get surface and other bits */
ds = &bspDrawSurfaces[i];
info = &surfaceInfos[i];
si = info->si;
/* sunlight? */
if(si->sun != NULL && nss[0] != '1')
{
Sys_FPrintf(SYS_VRB, "Sun: %s\n", si->shader);
CreateSunLight(si->sun);
si->sun = NULL; /* FIXME: leak! */
}
/* sky light? */
if(si->skyLightValue > 0.0f)
{
Sys_FPrintf(SYS_VRB, "Sky: %s\n", si->shader);
CreateSkyLights(si->color, si->skyLightValue, si->skyLightIterations, si->lightFilterRadius, si->lightStyle);
si->skyLightValue = 0.0f; /* FIXME: hack! */
}
/* try to early out */
if(si->value <= 0)
continue;
/* autosprite shaders become point lights */
if(si->autosprite)
{
/* create an average xyz */
VectorAdd(info->mins, info->maxs, origin);
VectorScale(origin, 0.5f, origin);
/* create a light */
light = safe_malloc(sizeof(*light));
memset(light, 0, sizeof(*light));
light->next = lights;
lights = light;
/* set it up */
light->flags = LIGHT_Q3A_DEFAULT;
light->type = EMIT_POINT;
light->photons = si->value * pointScale;
light->fade = 1.0f;
light->si = si;
VectorCopy(origin, light->origin);
VectorCopy(si->color, light->color);
light->falloffTolerance = falloffTolerance;
light->style = si->lightStyle;
/* add to point light count and continue */
numPointLights++;
continue;
}
/* get subdivision amount */
if(si->lightSubdivide > 0)
subdivide = si->lightSubdivide;
else
subdivide = defaultLightSubdivide;
/* switch on type */
switch (ds->surfaceType)
{
case MST_PLANAR:
case MST_TRIANGLE_SOUP:
RadLightForTriangles(i, 0, info->lm, si, APPROX_BOUNCE, subdivide, &cw);
break;
case MST_PATCH:
RadLightForPatch(i, 0, info->lm, si, APPROX_BOUNCE, subdivide, &cw);
break;
default:
break;
}
}
}
/*
SetEntityOrigins()
find the offset values for inline models
*/
void SetEntityOrigins(void)
{
int i, j, k, f;
entity_t *e;
vec3_t origin;
const char *key;
int modelnum;
bspModel_t *dm;
bspDrawSurface_t *ds;
/* ydnar: copy drawverts into private storage for nefarious purposes */
yDrawVerts = safe_malloc(numBSPDrawVerts * sizeof(bspDrawVert_t));
memcpy(yDrawVerts, bspDrawVerts, numBSPDrawVerts * sizeof(bspDrawVert_t));
/* set the entity origins */
for(i = 0; i < numEntities; i++)
{
/* get entity and model */
e = &entities[i];
key = ValueForKey(e, "model");
if(key[0] != '*')
continue;
modelnum = atoi(key + 1);
dm = &bspModels[modelnum];
/* get entity origin */
key = ValueForKey(e, "origin");
if(key[0] == '\0')
continue;
GetVectorForKey(e, "origin", origin);
/* set origin for all surfaces for this model */
for(j = 0; j < dm->numBSPSurfaces; j++)
{
/* get drawsurf */
ds = &bspDrawSurfaces[dm->firstBSPSurface + j];
/* set its verts */
for(k = 0; k < ds->numVerts; k++)
{
f = ds->firstVert + k;
VectorAdd(origin, bspDrawVerts[f].xyz, yDrawVerts[f].xyz);
}
}
}
}
/*
PointToPolygonFormFactor()
calculates the area over a point/normal hemisphere a winding covers
ydnar: fixme: there has to be a faster way to calculate this
without the expensive per-vert sqrts and transcendental functions
ydnar 2002-09-30: added -faster switch because only 19% deviance > 10%
between this and the approximation
*/
#define ONE_OVER_2PI 0.159154942f //% (1.0f / (2.0f * 3.141592657f))
float PointToPolygonFormFactor(const vec3_t point, const vec3_t normal, const winding_t * w)
{
vec3_t triVector, triNormal;
int i, j;
vec3_t dirs[MAX_POINTS_ON_WINDING];
float total;
float dot, angle, facing;
/* this is expensive */
for(i = 0; i < w->numpoints; i++)
{
VectorSubtract(w->p[i], point, dirs[i]);
VectorNormalize2(dirs[i], dirs[i]);
}
/* duplicate first vertex to avoid mod operation */
VectorCopy(dirs[0], dirs[i]);
/* calculcate relative area */
total = 0.0f;
for(i = 0; i < w->numpoints; i++)
{
/* get a triangle */
j = i + 1;
dot = DotProduct(dirs[i], dirs[j]);
/* roundoff can cause slight creep, which gives an IND from acos */
if(dot > 1.0f)
dot = 1.0f;
else if(dot < -1.0f)
dot = -1.0f;
/* get the angle */
angle = acos(dot);
CrossProduct(dirs[i], dirs[j], triVector);
if(VectorNormalize2(triVector, triNormal) < 0.0001f)
continue;
facing = DotProduct(normal, triNormal);
total += facing * angle;
/* ydnar: this was throwing too many errors with radiosity + crappy maps. ignoring it. */
if(total > 6.3f || total < -6.3f)
return 0.0f;
}
/* now in the range of 0 to 1 over the entire incoming hemisphere */
//% total /= (2.0f * 3.141592657f);
total *= ONE_OVER_2PI;
return total;
}
/*
LightContributionTosample()
determines the amount of light reaching a sample (luxel or vertex) from a given light
*/
int LightContributionToSample(trace_t * trace)
{
light_t *light;
float angle;
float add;
float dist;
float addDeluxe = 0.0f, addDeluxeBounceScale = 0.25f;
qboolean angledDeluxe = qfalse;
float colorBrightness;
/* get light */
light = trace->light;
/* clear color */
VectorClear(trace->color);
VectorClear(trace->colorNoShadow);
VectorClear(trace->directionContribution);
colorBrightness = RGBTOGRAY(light->color) * (1.0f / 255.0f);
/* ydnar: early out */
if(!(light->flags & LIGHT_SURFACES) || light->envelope <= 0.0f)
return 0;
/* do some culling checks */
if(light->type != EMIT_SUN)
{
/* MrE: if the light is behind the surface */
if(trace->twoSided == qfalse)
if(DotProduct(light->origin, trace->normal) - DotProduct(trace->origin, trace->normal) < 0.0f)
return 0;
/* ydnar: test pvs */
if(!ClusterVisible(trace->cluster, light->cluster))
return 0;
}
/* exact point to polygon form factor */
if(light->type == EMIT_AREA)
{
float factor;
float d;
vec3_t pushedOrigin;
/* project sample point into light plane */
d = DotProduct(trace->origin, light->normal) - light->dist;
if(d < 3.0f)
{
/* sample point behind plane? */
if(!(light->flags & LIGHT_TWOSIDED) && d < -1.0f)
return 0;
/* sample plane coincident? */
if(d > -3.0f && DotProduct(trace->normal, light->normal) > 0.9f)
return 0;
}
/* nudge the point so that it is clearly forward of the light */
/* so that surfaces meeting a light emitter don't get black edges */
if(d > -8.0f && d < 8.0f)
VectorMA(trace->origin, (8.0f - d), light->normal, pushedOrigin);
else
VectorCopy(trace->origin, pushedOrigin);
/* get direction and distance */
VectorCopy(light->origin, trace->end);
dist = SetupTrace(trace);
if(dist >= light->envelope)
return 0;
/* ptpff approximation */
if(faster)
{
/* angle attenuation */
angle = DotProduct(trace->normal, trace->direction);
/* twosided lighting */
if(trace->twoSided)
angle = fabs(angle);
/* attenuate */
angle *= -DotProduct(light->normal, trace->direction);
if(angle == 0.0f)
return 0;
else if(angle < 0.0f && (trace->twoSided || (light->flags & LIGHT_TWOSIDED)))
angle = -angle;
/* clamp the distance to prevent super hot spots */
dist = sqrt(dist * dist + light->extraDist * light->extraDist);
if(dist < 16.0f)
dist = 16.0f;
add = light->photons / (dist * dist) * angle;
if(deluxemap)
{
if(angledDeluxe)
addDeluxe = light->photons / (dist * dist) * angle;
else
addDeluxe = light->photons / (dist * dist);
}
}
else
{
/* calculate the contribution */
factor = PointToPolygonFormFactor(pushedOrigin, trace->normal, light->w);
if(factor == 0.0f)
return 0;
else if(factor < 0.0f)
{
/* twosided lighting */
if(trace->twoSided || (light->flags & LIGHT_TWOSIDED))
{
factor = -factor;
/* push light origin to other side of the plane */
VectorMA(light->origin, -2.0f, light->normal, trace->end);
dist = SetupTrace(trace);
if(dist >= light->envelope)
return 0;
}
else
return 0;
}
/* ydnar: moved to here */
add = factor * light->add;
if(deluxemap)
addDeluxe = add;
}
}
/* point/spot lights */
else if(light->type == EMIT_POINT || light->type == EMIT_SPOT)
{
/* get direction and distance */
VectorCopy(light->origin, trace->end);
dist = SetupTrace(trace);
if(dist >= light->envelope) //hypov8 todo: check distances
return 0;
/* clamp the distance to prevent super hot spots */
dist = sqrt(dist * dist + light->extraDist * light->extraDist);
if(dist < 16.0f)
dist = 16.0f;
/* angle attenuation */
if(light->flags & LIGHT_ATTEN_ANGLE)
{
/* standard Lambert attenuation */
float dot = DotProduct(trace->normal, trace->direction);
/* twosided lighting */
if(trace->twoSided)
dot = fabs(dot);
/* jal: optional half Lambert attenuation (http://developer.valvesoftware.com/wiki/Half_Lambert) */
if(lightAngleHL)
{
if(dot > 0.001f) // skip coplanar
{
if(dot > 1.0f)
dot = 1.0f;
dot = (dot * 0.5f) + 0.5f;
dot *= dot;
}
else
dot = 0;
}
angle = dot;
}
else
angle = 1.0f;
if(light->angleScale != 0.0f)
{
angle /= light->angleScale;
if(angle > 1.0f)
angle = 1.0f;
}
/* attenuate */
if(light->flags & LIGHT_ATTEN_LINEAR)
{
add = angle * light->photons * linearScale - (dist * light->fade);
if(add < 0.0f)
add = 0.0f;
if(deluxemap)
{
if(angledDeluxe)
addDeluxe = angle * light->photons * linearScale - (dist * light->fade);
else
addDeluxe = light->photons * linearScale - (dist * light->fade);
if(addDeluxe < 0.0f)
addDeluxe = 0.0f;
}
}
else
{
add = (light->photons / (dist * dist)) * angle;
if(add < 0.0f)
add = 0.0f;
if(deluxemap)
{
if(angledDeluxe)
addDeluxe = (light->photons / (dist * dist)) * angle;
else
addDeluxe = (light->photons / (dist * dist));
}
if(addDeluxe < 0.0f)
addDeluxe = 0.0f;
}
/* handle spotlights */
if(light->type == EMIT_SPOT)
{
float distByNormal, radiusAtDist, sampleRadius;
vec3_t pointAtDist, distToSample;
/* do cone calculation */
distByNormal = -DotProduct(trace->displacement, light->normal);
if(distByNormal < 0.0f)
return 0;
VectorMA(light->origin, distByNormal, light->normal, pointAtDist);
radiusAtDist = light->radiusByDist * distByNormal;
VectorSubtract(trace->origin, pointAtDist, distToSample);
sampleRadius = VectorLength(distToSample);
/* outside the cone */
if(sampleRadius >= radiusAtDist)
return 0;
/* attenuate */
if(sampleRadius > (radiusAtDist - 32.0f))
{