-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathltface.c
711 lines (564 loc) · 14.1 KB
/
ltface.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
#include "light.h"
byte *AllocLitEntry (int offset);
float PointDist (vec3_t p1, vec3_t p2)
{
int i;
vec_t t;
t = 0;
for (i = 0; i < 3; i++)
t += (p2[i] - p1[i]) * (p2[i] - p1[i]);
// don't blow up...
if (t == 0) t = 1;
return sqrt(t);
}
/*
============
CastRay
Returns the distance between the points, or -1 if blocked
=============
*/
vec_t CastRay (vec3_t p1, vec3_t p2)
{
qboolean trace;
trace = TestLine (p1, p2);
if (!trace)
return -1; // ray was blocked
return PointDist (p1, p2);
}
/*
===============================================================================
SAMPLE POINT DETERMINATION
void SetupBlock (dface_t *f) Returns with surfpt[] set
This is a little tricky because the lightmap covers more area than the face.
If done in the straightforward fashion, some of the
sample points will be inside walls or on the other side of walls, causing
false shadows and light bleeds.
To solve this, I only consider a sample point valid if a line can be drawn
between it and the exact midpoint of the face. If invalid, it is adjusted
towards the center until it is valid.
(this doesn't completely work)
===============================================================================
*/
#define SINGLEMAP (18 * 18 * 4 * 4)
typedef struct lightinfo_s
{
vec3_t lightmaps[MAXLIGHTMAPS][SINGLEMAP];
int numlightstyles;
vec_t *light;
vec_t facedist;
vec3_t facenormal;
int numsurfpt;
vec3_t surfpt[SINGLEMAP];
vec3_t texorg;
vec3_t worldtotex[2]; // s = (world - texorg) . worldtotex[0]
vec3_t textoworld[2]; // world = texorg + s * textoworld[0]
vec_t exactmins[2], exactmaxs[2];
int texmins[2], texsize[2];
int lightstyles[256];
int surfnum;
dface_t *face;
} lightinfo_t;
/*
================
CalcFaceVectors
Fills in texorg, worldtotex. and textoworld
================
*/
void CalcFaceVectors (lightinfo_t *l)
{
texinfo_t *tex;
int i, j;
vec3_t texnormal;
float distscale;
vec_t dist, len;
tex = &texinfo[l->face->texinfo];
// convert from float to vec_t
for (i=0 ; i<2 ; i++)
for (j=0 ; j<3 ; j++)
l->worldtotex[i][j] = tex->vecs[i][j];
// calculate a normal to the texture axis. points can be moved along this
// without changing their S/T
texnormal[0] = tex->vecs[1][1]*tex->vecs[0][2]
- tex->vecs[1][2]*tex->vecs[0][1];
texnormal[1] = tex->vecs[1][2]*tex->vecs[0][0]
- tex->vecs[1][0]*tex->vecs[0][2];
texnormal[2] = tex->vecs[1][0]*tex->vecs[0][1]
- tex->vecs[1][1]*tex->vecs[0][0];
VectorNormalize (texnormal);
// flip it towards plane normal
distscale = DotProduct (texnormal, l->facenormal);
if (!distscale)
Error ("Texture axis perpendicular to face");
if (distscale < 0)
{
distscale = -distscale;
VectorSubtract (vec3_origin, texnormal, texnormal);
}
// distscale is the ratio of the distance along the texture normal to
// the distance along the plane normal
distscale = 1/distscale;
for (i=0 ; i<2 ; i++)
{
len = VectorLength (l->worldtotex[i]);
dist = DotProduct (l->worldtotex[i], l->facenormal);
dist *= distscale;
VectorMA (l->worldtotex[i], -dist, texnormal, l->textoworld[i]);
VectorScale (l->textoworld[i], (1/len)*(1/len), l->textoworld[i]);
}
// calculate texorg on the texture plane
for (i=0 ; i<3 ; i++)
l->texorg[i] = -tex->vecs[0][3]* l->textoworld[0][i] - tex->vecs[1][3] * l->textoworld[1][i];
// project back to the face plane
dist = DotProduct (l->texorg, l->facenormal) - l->facedist - 1;
dist *= distscale;
VectorMA (l->texorg, -dist, texnormal, l->texorg);
}
/*
================
CalcFaceExtents
Fills in s->texmins[] and s->texsize[]
also sets exactmins[] and exactmaxs[]
================
*/
void CalcFaceExtents (lightinfo_t *l)
{
dface_t *s;
vec_t mins[2], maxs[2], val;
int i,j, e;
dvertex_t *v;
texinfo_t *tex;
s = l->face;
mins[0] = mins[1] = 999999;
maxs[0] = maxs[1] = -99999;
tex = &texinfo[s->texinfo];
for (i=0 ; i<s->numedges ; i++)
{
e = dsurfedges[s->firstedge+i];
if (e >= 0)
v = dvertexes + dedges[e].v[0];
else
v = dvertexes + dedges[-e].v[1];
for (j=0 ; j<2 ; j++)
{
val = v->point[0] * tex->vecs[j][0] +
v->point[1] * tex->vecs[j][1] +
v->point[2] * tex->vecs[j][2] +
tex->vecs[j][3];
if (val < mins[j])
mins[j] = val;
if (val > maxs[j])
maxs[j] = val;
}
}
for (i=0 ; i<2 ; i++)
{
l->exactmins[i] = mins[i];
l->exactmaxs[i] = maxs[i];
mins[i] = floor(mins[i]/16);
maxs[i] = ceil(maxs[i]/16);
l->texmins[i] = mins[i];
l->texsize[i] = maxs[i] - mins[i];
if (l->texsize[i] > 17)
Error ("Bad surface extents");
}
}
/*
=================
CalcPoints
For each texture aligned grid point, back project onto the plane
to get the world xyz value of the sample point
=================
*/
int c_bad;
void CalcPoints (lightinfo_t *l)
{
int i;
int s, t, j;
int w, h, step;
vec_t starts, startt, us, ut;
vec_t *surf;
vec_t mids, midt;
vec3_t facemid, move;
//
// fill in surforg
// the points are biased towards the center of the surface
// to help avoid edge cases just inside walls
//
surf = l->surfpt[0];
mids = (l->exactmaxs[0] + l->exactmins[0])/2;
midt = (l->exactmaxs[1] + l->exactmins[1])/2;
for (j=0 ; j<3 ; j++)
facemid[j] = l->texorg[j] + l->textoworld[0][j]*mids + l->textoworld[1][j]*midt;
h = (l->texsize[1] + 1) * extrasamples;
w = (l->texsize[0] + 1) * extrasamples;
starts = l->texmins[0] * 16;
startt = l->texmins[1] * 16;
if (extrasamples > 1)
{
starts -= 16 / extrasamples;
startt -= 16 / extrasamples;
}
step = 16 / extrasamples;
l->numsurfpt = w * h;
for (t=0 ; t<h ; t++)
{
for (s=0 ; s<w ; s++, surf+=3)
{
us = starts + s*step;
ut = startt + t*step;
// if a line can be traced from surf to facemid, the point is good
for (i=0 ; i<6 ; i++)
{
// calculate texture point
for (j=0 ; j<3 ; j++)
surf[j] = l->texorg[j] + l->textoworld[0][j]*us
+ l->textoworld[1][j]*ut;
if (CastRay (facemid, surf) != -1)
break; // got it
if (i & 1)
{
if (us > mids)
{
us -= 8;
if (us < mids)
us = mids;
}
else
{
us += 8;
if (us > mids)
us = mids;
}
}
else
{
if (ut > midt)
{
ut -= 8;
if (ut < midt)
ut = midt;
}
else
{
ut += 8;
if (ut > midt)
ut = midt;
}
}
// move surf 8 pixels towards the center
VectorSubtract (facemid, surf, move);
VectorNormalize (move);
VectorMA (surf, 8, move, surf);
}
if (i == 2)
c_bad++;
}
}
}
/*
===============================================================================
FACE LIGHTING
===============================================================================
*/
int c_culldistplane, c_proper;
/*
================
SingleLightFace
================
*/
void ColourLight (entity_t *light, dface_t *face, float add);
void SingleLightFace (entity_t *light, lightinfo_t *l, qboolean test)
{
vec_t dist;
vec3_t incoming;
vec_t angle;
vec_t add;
vec_t *surf;
qboolean hit;
int mapnum;
int size;
int c, i;
vec3_t rel;
vec3_t spotvec;
vec_t falloff;
vec3_t Dummy[SINGLEMAP];
vec3_t *lightsamp;
VectorSubtract (light->origin, bsp_origin, rel);
dist = scaledist * (DotProduct (rel, l->facenormal) - l->facedist);
// don't bother with lights behind the surface
if (dist <= 0) return;
// don't bother with light too far away
if (dist > light->light)
{
c_culldistplane++;
return;
}
if (light->targetent)
{
VectorSubtract (light->targetent->origin, light->origin, spotvec);
VectorNormalize (spotvec);
if (!light->angle)
falloff = -cos (20 * Q_PI / 180);
else falloff = -cos (light->angle / 2 * Q_PI / 180);
}
else
{
// shut up compiler warnings
falloff = 0;
}
mapnum = 0;
for (mapnum = 0; mapnum < l->numlightstyles; mapnum++)
if (l->lightstyles[mapnum] == light->style)
break;
lightsamp = l->lightmaps[mapnum];
if (mapnum == l->numlightstyles)
{
// init a new light map
if (mapnum == MAXLIGHTMAPS)
{
// We might be exceeding the limit but hold off warning
// until we see that the light actually hits this face
lightsamp = Dummy; // Make sure we don't trash l->lightmaps array
}
size = (l->texsize[1] + 1) * (l->texsize[0] + 1);
for (i = 0; i < size; i++)
{
lightsamp[i][0] = 0;
lightsamp[i][1] = 0;
lightsamp[i][2] = 0;
}
}
// check it for real
hit = false;
c_proper++;
surf = l->surfpt[0];
for (c = 0; c < l->numsurfpt; c++, surf += 3)
{
dist = CastRay (light->origin, surf) * scaledist;
// light doesn't reach
if (dist < 0) continue;
VectorSubtract (light->origin, surf, incoming);
VectorNormalize (incoming);
angle = DotProduct (incoming, l->facenormal);
if (light->targetent)
{
// spotlight cutoff
if (DotProduct (spotvec, incoming) > falloff)
continue;
}
angle = (1.0 - scalecos) + scalecos * angle;
add = light->light - dist;
add *= angle;
if (add < 0) continue;
if (test)
{
// process the colour
ColourLight (light, l->face, add);
// one hit is enough here
return;
}
lightsamp[c][0] += add * light->colour[0] / 255;
lightsamp[c][1] += add * light->colour[1] / 255;
lightsamp[c][2] += add * light->colour[2] / 255;
// ignore real tiny lights
if (lightsamp[c][0] > 1) hit = true;
if (lightsamp[c][1] > 1) hit = true;
if (lightsamp[c][2] > 1) hit = true;
}
if (mapnum == l->numlightstyles && hit)
{
if (mapnum == MAXLIGHTMAPS)
{
if (light->style == 0)
{
--mapnum;
// replace the last style with this one
memcpy (l->lightmaps[mapnum], lightsamp, SINGLEMAP * sizeof(vec3_t));
l->lightstyles[mapnum] = 0;
}
return;
}
// the style has some real data now
l->lightstyles[mapnum] = light->style;
l->numlightstyles++;
}
}
/*
============
FixMinlight
============
*/
void FixMinlight (lightinfo_t *l)
{
int i, j;
float minlight;
minlight = minlights[l->surfnum];
// if minlight is set, there must be a style 0 light map
if (!minlight) return;
for (i = 0; i < l->numlightstyles; i++)
{
if (l->lightstyles[i] == 0)
break;
}
if (i == l->numlightstyles)
{
// oh well..
if (l->numlightstyles == MAXLIGHTMAPS) return;
for (j = 0; j < l->numsurfpt; j++)
{
l->lightmaps[i][j][0] = minlight;
l->lightmaps[i][j][1] = minlight;
l->lightmaps[i][j][2] = minlight;
}
l->lightstyles[i] = 0;
l->numlightstyles++;
}
else
{
for (j = 0; j < l->numsurfpt; j++)
{
if (l->lightmaps[i][j][0] < minlight) l->lightmaps[i][j][0] = minlight;
if (l->lightmaps[i][j][1] < minlight) l->lightmaps[i][j][1] = minlight;
if (l->lightmaps[i][j][2] < minlight) l->lightmaps[i][j][2] = minlight;
}
}
}
/*
============
LightFace
============
*/
void LightFace (int surfnum, qboolean test)
{
dface_t *f;
lightinfo_t l;
int s, t;
int i,j,k,c;
vec3_t total;
int size;
int lightmapwidth, lightmapsize;
byte *out;
byte *out3;
vec3_t *light;
int w, h;
f = dfaces + surfnum;
// some surfaces don't need lightmaps
f->lightofs = -1;
for (j = 0; j < MAXLIGHTMAPS; j++)
f->styles[j] = 255;
if (texinfo[f->texinfo].flags & TEX_SPECIAL)
{
// non-lit texture
return;
}
memset (&l, 0, sizeof (l));
l.surfnum = surfnum;
l.face = f;
// rotate plane
VectorCopy (dplanes[f->planenum].normal, l.facenormal);
l.facedist = dplanes[f->planenum].dist;
if (f->side)
{
VectorSubtract (vec3_origin, l.facenormal, l.facenormal);
l.facedist = -l.facedist;
}
CalcFaceVectors (&l);
CalcFaceExtents (&l);
CalcPoints (&l);
lightmapwidth = l.texsize[0] + 1;
size = lightmapwidth * (l.texsize[1] + 1);
if (size > SINGLEMAP) Error ("Bad lightmap size");
for (i = 0; i < MAXLIGHTMAPS; i++)
l.lightstyles[i] = 255;
// cast all lights
l.numlightstyles = 0;
for (i = 0; i < num_entities; i++)
{
// not a light!!!
if (!entities[i].islight) continue;
if (entities[i].light > 0)
{
int oldlight;
if (test)
{
// don't check if it's already coloured
if (entities[i].precolour) continue;
// halve the light range when testing
oldlight = entities[i].light;
entities[i].light /= 2;
}
SingleLightFace (&entities[i], &l, test);
// restore the light range now
if (test) entities[i].light = oldlight;
}
}
if (test)
{
// no need to do any more
return;
}
FixMinlight (&l);
if (!l.numlightstyles)
{
// no light hitting it
return;
}
// save out the values
for (i = 0; i < MAXLIGHTMAPS; i++)
f->styles[i] = l.lightstyles[i];
lightmapsize = size * l.numlightstyles;
out = GetFileSpace (lightmapsize);
f->lightofs = out - filebase;
// ensure that the 3 component lighting is at the correct offset
out3 = AllocLitEntry (f->lightofs * 3);
// extra filtering
h = (l.texsize[1] + 1) * extrasamples;
w = (l.texsize[0] + 1) * extrasamples;
for (i = 0; i < l.numlightstyles; i++)
{
if (l.lightstyles[i] == 0xff)
Error ("Wrote empty lightmap");
light = l.lightmaps[i];
c = 0;
for (t = 0; t <= l.texsize[1]; t++)
{
for (s = 0; s <= l.texsize[0]; s++, c++)
{
if (extrasamples > 1)
{
// filtered sample
total[0] = total[1] = total[2] = 0;
for (j = 0; j < extrasamples; j++)
{
for (k = 0; k < extrasamples; k++)
{
total[0] += light[(t * extrasamples + j) * w + s * extrasamples + k][0];
total[1] += light[(t * extrasamples + j) * w + s * extrasamples + k][1];
total[2] += light[(t * extrasamples + j) * w + s * extrasamples + k][2];
}
}
total[0] /= (extrasamples * extrasamples);
total[1] /= (extrasamples * extrasamples);
total[2] /= (extrasamples * extrasamples);
}
else
{
total[0] = light[c][0];
total[1] = light[c][1];
total[2] = light[c][2];
}
// scale before clamping
total[0] *= rangescale;
total[1] *= rangescale;
total[2] *= rangescale;
if (total[0] > 255) total[0] = 255; else if (total[0] < 0) total[0] = 0;
if (total[1] > 255) total[1] = 255; else if (total[1] < 0) total[1] = 0;
if (total[2] > 255) total[2] = 255; else if (total[2] < 0) total[2] = 0;
*out++ = (byte) ((total[0] * 30 + total[1] * 59 + total[2] * 11) / 100);
*out3++ = (byte) total[0];
*out3++ = (byte) total[1];
*out3++ = (byte) total[2];
}
}
}
}