]> icculus.org git repositories - divverent/darkplaces.git/blob - r_light.c
speedups to R_WorldNode, and some shrinkage on the surface struct
[divverent/darkplaces.git] / r_light.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // r_light.c
21
22 #include "quakedef.h"
23
24 cvar_t r_lightmodels = {"r_lightmodels", "1"};
25
26 void r_light_start()
27 {
28 }
29
30 void r_light_shutdown()
31 {
32 }
33
34 void R_Light_Init()
35 {
36         Cvar_RegisterVariable(&r_lightmodels);
37         R_RegisterModule("R_Light", r_light_start, r_light_shutdown);
38 }
39
40 int     r_dlightframecount;
41
42 /*
43 ==================
44 R_AnimateLight
45 ==================
46 */
47 void R_AnimateLight (void)
48 {
49         int                     i,j,k;
50         
51 //
52 // light animations
53 // 'm' is normal light, 'a' is no light, 'z' is double bright
54         i = (int)(cl.time*10);
55         for (j=0 ; j<MAX_LIGHTSTYLES ; j++)
56         {
57                 if (!cl_lightstyle[j].length)
58                 {
59                         d_lightstylevalue[j] = 256;
60                         continue;
61                 }
62                 k = i % cl_lightstyle[j].length;
63                 k = cl_lightstyle[j].map[k] - 'a';
64                 k = k*22;
65                 d_lightstylevalue[j] = k;
66         }       
67 }
68
69 /*
70 =============================================================================
71
72 DYNAMIC LIGHTS
73
74 =============================================================================
75 */
76
77 /*
78 =============
79 R_MarkLights
80 =============
81 */
82 void R_OldMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, mnode_t *node)
83 {
84         float           dist;
85         msurface_t      *surf;
86         int                     i;
87
88 loc0:
89         if (node->contents < 0)
90                 return;
91
92         dist = PlaneDiff(lightorigin, node->plane);
93         
94         if (dist > light->radius)
95         {
96                 if (node->children[0]->contents >= 0) // LordHavoc: save some time by not pushing another stack frame
97                 {
98                         node = node->children[0];
99                         goto loc0;
100                 }
101                 return;
102         }
103         if (dist < -light->radius)
104         {
105                 if (node->children[1]->contents >= 0) // LordHavoc: save some time by not pushing another stack frame
106                 {
107                         node = node->children[1];
108                         goto loc0;
109                 }
110                 return;
111         }
112
113         if (node->dlightframe != r_dlightframecount) // not dynamic until now
114         {
115                 node->dlightbits[0] = node->dlightbits[1] = node->dlightbits[2] = node->dlightbits[3] = node->dlightbits[4] = node->dlightbits[5] = node->dlightbits[6] = node->dlightbits[7] = 0;
116                 node->dlightframe = r_dlightframecount;
117         }
118         node->dlightbits[bitindex] |= bit;
119
120 // mark the polygons
121         surf = cl.worldmodel->surfaces + node->firstsurface;
122         for (i=0 ; i<node->numsurfaces ; i++, surf++)
123         {
124                 if (surf->dlightframe != r_dlightframecount) // not dynamic until now
125                 {
126                         surf->dlightbits[0] = surf->dlightbits[1] = surf->dlightbits[2] = surf->dlightbits[3] = surf->dlightbits[4] = surf->dlightbits[5] = surf->dlightbits[6] = surf->dlightbits[7] = 0;
127                         surf->dlightframe = r_dlightframecount;
128                 }
129                 surf->dlightbits[bitindex] |= bit;
130         }
131
132         if (node->children[0]->contents >= 0)
133         {
134                 if (node->children[1]->contents >= 0)
135                 {
136                         R_OldMarkLights (lightorigin, light, bit, bitindex, node->children[0]);
137                         node = node->children[1];
138                         goto loc0;
139                 }
140                 else
141                 {
142                         node = node->children[0];
143                         goto loc0;
144                 }
145         }
146         else if (node->children[1]->contents >= 0)
147         {
148                 node = node->children[1];
149                 goto loc0;
150         }
151 }
152
153 void R_NoVisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model)
154 {
155         R_OldMarkLights(lightorigin, light, bit, bitindex, model->nodes + model->hulls[0].firstclipnode);
156 }
157
158 int lightframe = 0;
159 void R_VisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model)
160 {
161         mleaf_t *pvsleaf = Mod_PointInLeaf (lightorigin, model);
162
163         if (!pvsleaf->compressed_vis)
164         {       // no vis info, so make all visible
165                 R_OldMarkLights(lightorigin, light, bit, bitindex, model->nodes + model->hulls[0].firstclipnode);
166                 return;
167         }
168         else
169         {
170                 int             i, k, l, m, c;
171                 msurface_t *surf, **mark;
172                 mleaf_t *leaf;
173                 byte    *in = pvsleaf->compressed_vis;
174                 int             row = (model->numleafs+7)>>3;
175                 float   low[3], high[3], radius;
176
177                 radius = light->radius * 4.0f;
178                 low[0] = lightorigin[0] - radius;low[1] = lightorigin[1] - radius;low[2] = lightorigin[2] - radius;
179                 high[0] = lightorigin[0] + radius;high[1] = lightorigin[1] + radius;high[2] = lightorigin[2] + radius;
180
181                 lightframe++;
182                 k = 0;
183                 while (k < row)
184                 {
185                         c = *in++;
186                         if (c)
187                         {
188                                 l = model->numleafs - (k << 3);
189                                 if (l > 8)
190                                         l = 8;
191                                 for (i=0 ; i<l ; i++)
192                                 {
193                                         if (c & (1<<i))
194                                         {
195                                                 leaf = &model->leafs[(k << 3)+i+1];
196                                                 leaf->lightframe = lightframe;
197                                                 if (leaf->visframe != r_visframecount)
198                                                         continue;
199                                                 if (leaf->contents == CONTENTS_SOLID)
200                                                         continue;
201                                                 // if out of the light radius, skip
202                                                 /*
203                                                 if (leaf->minmaxs[0] > high[0] || leaf->minmaxs[3] < low[0]
204                                                  || leaf->minmaxs[1] > high[1] || leaf->minmaxs[4] < low[1]
205                                                  || leaf->minmaxs[2] > high[2] || leaf->minmaxs[5] < low[2])
206                                                         continue;
207                                                 */
208                                                 if (leaf->dlightframe != r_dlightframecount) // not dynamic until now
209                                                 {
210                                                         leaf->dlightbits[0] = leaf->dlightbits[1] = leaf->dlightbits[2] = leaf->dlightbits[3] = leaf->dlightbits[4] = leaf->dlightbits[5] = leaf->dlightbits[6] = leaf->dlightbits[7] = 0;
211                                                         leaf->dlightframe = r_dlightframecount;
212                                                 }
213                                                 leaf->dlightbits[bitindex] |= bit;
214                                                 if ((m = leaf->nummarksurfaces))
215                                                 {
216                                                         mark = leaf->firstmarksurface;
217                                                         do
218                                                         {
219                                                                 surf = *mark++;
220                                                                 if (surf->visframe != r_framecount || surf->lightframe == lightframe)
221                                                                         continue;
222                                                                 surf->lightframe = lightframe;
223                                                                 //if (((surf->flags & SURF_PLANEBACK) == 0) == ((PlaneDiff(lightorigin, surf->plane)) >= 0))
224                                                                 {
225                                                                         if (surf->dlightframe != r_dlightframecount) // not dynamic until now
226                                                                         {
227                                                                                 surf->dlightbits[0] = surf->dlightbits[1] = surf->dlightbits[2] = surf->dlightbits[3] = surf->dlightbits[4] = surf->dlightbits[5] = surf->dlightbits[6] = surf->dlightbits[7] = 0;
228                                                                                 surf->dlightframe = r_dlightframecount;
229                                                                         }
230                                                                         surf->dlightbits[bitindex] |= bit;
231                                                                 }
232                                                         }
233                                                         while (--m);
234                                                 }
235                                         }
236                                 }
237                                 k++;
238                                 continue;
239                         }
240                 
241                         k += *in++;
242                 }
243         }
244 }
245
246
247 /*
248 =============
249 R_PushDlights
250 =============
251 */
252 void R_PushDlights (void)
253 {
254         int             i;
255         dlight_t        *l;
256
257         r_dlightframecount = r_framecount + 1;  // because the count hasn't advanced yet for this frame
258
259         if (!r_dynamic.value)
260                 return;
261
262         l = cl_dlights;
263
264         for (i=0 ; i<MAX_DLIGHTS ; i++, l++)
265         {
266                 if (l->die < cl.time || !l->radius)
267                         continue;
268 //              R_MarkLights (l->origin, l, 1<<(i&31), i >> 5, cl.worldmodel->nodes );
269                 R_VisMarkLights (l->origin, l, 1<<(i&31), i >> 5, cl.worldmodel);
270         }
271 }
272
273
274 /*
275 =============================================================================
276
277 LIGHT SAMPLING
278
279 =============================================================================
280 */
281
282 mplane_t                *lightplane;
283 vec3_t                  lightspot;
284
285 extern cvar_t r_ambient;
286
287 /*
288 int RecursiveLightPoint (vec3_t color, mnode_t *node, vec3_t start, vec3_t end)
289 {
290         float           front, back, frac;
291         vec3_t          mid;
292
293 loc0:
294         if (node->contents < 0)
295                 return false;           // didn't hit anything
296         
297 // calculate mid point
298         front = PlaneDiff (start, node->plane);
299         back = PlaneDiff (end, node->plane);
300
301         // LordHavoc: optimized recursion
302         if ((back < 0) == (front < 0))
303 //              return RecursiveLightPoint (color, node->children[front < 0], start, end);
304         {
305                 node = node->children[front < 0];
306                 goto loc0;
307         }
308         
309         frac = front / (front-back);
310         mid[0] = start[0] + (end[0] - start[0])*frac;
311         mid[1] = start[1] + (end[1] - start[1])*frac;
312         mid[2] = start[2] + (end[2] - start[2])*frac;
313         
314 // go down front side
315         if (RecursiveLightPoint (color, node->children[front < 0], start, mid))
316                 return true;    // hit something
317         else
318         {
319                 int i, ds, dt;
320                 msurface_t *surf;
321         // check for impact on this node
322                 VectorCopy (mid, lightspot);
323                 lightplane = node->plane;
324
325                 surf = cl.worldmodel->surfaces + node->firstsurface;
326                 for (i = 0;i < node->numsurfaces;i++, surf++)
327                 {
328                         if (surf->flags & SURF_DRAWTILED)
329                                 continue;       // no lightmaps
330
331                         ds = (int) ((float) DotProduct (mid, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3]);
332                         dt = (int) ((float) DotProduct (mid, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3]);
333
334                         if (ds < surf->texturemins[0] || dt < surf->texturemins[1])
335                                 continue;
336                         
337                         ds -= surf->texturemins[0];
338                         dt -= surf->texturemins[1];
339                         
340                         if (ds > surf->extents[0] || dt > surf->extents[1])
341                                 continue;
342
343                         if (surf->samples)
344                         {
345                                 byte *lightmap;
346                                 int maps, line3, dsfrac = ds & 15, dtfrac = dt & 15, r00 = 0, g00 = 0, b00 = 0, r01 = 0, g01 = 0, b01 = 0, r10 = 0, g10 = 0, b10 = 0, r11 = 0, g11 = 0, b11 = 0;
347                                 float scale;
348                                 line3 = ((surf->extents[0]>>4)+1)*3;
349
350                                 lightmap = surf->samples + ((dt>>4) * ((surf->extents[0]>>4)+1) + (ds>>4))*3; // LordHavoc: *3 for color
351
352                                 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
353                                 {
354                                         scale = (float) d_lightstylevalue[surf->styles[maps]] * 1.0 / 256.0;
355                                         r00 += (float) lightmap[      0] * scale;g00 += (float) lightmap[      1] * scale;b00 += (float) lightmap[2] * scale;
356                                         r01 += (float) lightmap[      3] * scale;g01 += (float) lightmap[      4] * scale;b01 += (float) lightmap[5] * scale;
357                                         r10 += (float) lightmap[line3+0] * scale;g10 += (float) lightmap[line3+1] * scale;b10 += (float) lightmap[line3+2] * scale;
358                                         r11 += (float) lightmap[line3+3] * scale;g11 += (float) lightmap[line3+4] * scale;b11 += (float) lightmap[line3+5] * scale;
359                                         lightmap += ((surf->extents[0]>>4)+1) * ((surf->extents[1]>>4)+1)*3; // LordHavoc: *3 for colored lighting
360                                 }
361
362                                 color[0] += (float) ((int) ((((((((r11-r10) * dsfrac) >> 4) + r10)-((((r01-r00) * dsfrac) >> 4) + r00)) * dtfrac) >> 4) + ((((r01-r00) * dsfrac) >> 4) + r00)));
363                                 color[1] += (float) ((int) ((((((((g11-g10) * dsfrac) >> 4) + g10)-((((g01-g00) * dsfrac) >> 4) + g00)) * dtfrac) >> 4) + ((((g01-g00) * dsfrac) >> 4) + g00)));
364                                 color[2] += (float) ((int) ((((((((b11-b10) * dsfrac) >> 4) + b10)-((((b01-b00) * dsfrac) >> 4) + b00)) * dtfrac) >> 4) + ((((b01-b00) * dsfrac) >> 4) + b00)));
365                         }
366                         return true; // success
367                 }
368
369         // go down back side
370                 return RecursiveLightPoint (color, node->children[front >= 0], mid, end);
371         }
372 }
373
374 void R_LightPoint (vec3_t color, vec3_t p)
375 {
376         vec3_t          end;
377         
378         if (r_fullbright.value || !cl.worldmodel->lightdata)
379         {
380                 color[0] = color[1] = color[2] = 255;
381                 return;
382         }
383
384         end[0] = p[0];
385         end[1] = p[1];
386         end[2] = p[2] - 2048;
387
388         color[0] = color[1] = color[2] = r_ambient.value * 2.0f;
389         RecursiveLightPoint (color, cl.worldmodel->nodes, p, end);
390 }
391
392 void SV_LightPoint (vec3_t color, vec3_t p)
393 {
394         vec3_t          end;
395         
396         if (!sv.worldmodel->lightdata)
397         {
398                 color[0] = color[1] = color[2] = 255;
399                 return;
400         }
401         
402         end[0] = p[0];
403         end[1] = p[1];
404         end[2] = p[2] - 2048;
405
406         color[0] = color[1] = color[2] = 0;
407         RecursiveLightPoint (color, sv.worldmodel->nodes, p, end);
408 }
409 */
410
411 int RecursiveLightPoint (vec3_t color, mnode_t *node, float x, float y, float startz, float endz)
412 {
413         int             side, distz = endz - startz;
414         float   front, back;
415         float   mid;
416
417 loc0:
418         if (node->contents < 0)
419                 return false;           // didn't hit anything
420
421         switch (node->plane->type)
422         {
423         case PLANE_X:
424                 node = node->children[x < node->plane->dist];
425                 goto loc0;
426         case PLANE_Y:
427                 node = node->children[y < node->plane->dist];
428                 goto loc0;
429         case PLANE_Z:
430                 side = startz < node->plane->dist;
431                 if ((endz < node->plane->dist) == side)
432                 {
433                         node = node->children[side];
434                         goto loc0;
435                 }
436                 // found an intersection
437 //              mid = startz + (endz - startz) * (startz - node->plane->dist) / (startz - endz);
438 //              mid = startz + distz * (startz - node->plane->dist) / (-distz);
439 //              mid = startz + (-(startz - node->plane->dist));
440 //              mid = startz - (startz - node->plane->dist);
441 //              mid = startz + node->plane->dist - startz;
442                 mid = node->plane->dist;
443                 break;
444         default:
445                 back = front = x * node->plane->normal[0] + y * node->plane->normal[1];
446                 front += startz * node->plane->normal[2];
447                 back += endz * node->plane->normal[2];
448                 side = front < node->plane->dist;
449                 if ((back < node->plane->dist) == side)
450                 {
451                         node = node->children[side];
452                         goto loc0;
453                 }
454                 // found an intersection
455 //              mid = startz + (endz - startz) * ((front - node->plane->dist) / ((front - node->plane->dist) - (back - node->plane->dist)));
456 //              mid = startz + (endz - startz) * ((front - node->plane->dist) / (front - back));
457                 mid = startz + distz * (front - node->plane->dist) / (front - back);
458                 break;
459         }
460         
461         // go down front side
462         if (node->children[side]->contents >= 0 && RecursiveLightPoint (color, node->children[side], x, y, startz, mid))
463                 return true;    // hit something
464         else
465         {
466                 // check for impact on this node
467                 if (node->numsurfaces)
468                 {
469                         int i, ds, dt;
470                         msurface_t *surf;
471                         lightspot[0] = x;
472                         lightspot[1] = y;
473                         lightspot[2] = mid;
474                         lightplane = node->plane;
475
476                         surf = cl.worldmodel->surfaces + node->firstsurface;
477                         for (i = 0;i < node->numsurfaces;i++, surf++)
478                         {
479                                 if (surf->flags & SURF_DRAWTILED)
480                                         continue;       // no lightmaps
481
482                                 ds = (int) (x * surf->texinfo->vecs[0][0] + y * surf->texinfo->vecs[0][1] + mid * surf->texinfo->vecs[0][2] + surf->texinfo->vecs[0][3]);
483                                 dt = (int) (x * surf->texinfo->vecs[1][0] + y * surf->texinfo->vecs[1][1] + mid * surf->texinfo->vecs[1][2] + surf->texinfo->vecs[1][3]);
484
485                                 if (ds < surf->texturemins[0] || dt < surf->texturemins[1])
486                                         continue;
487                                 
488                                 ds -= surf->texturemins[0];
489                                 dt -= surf->texturemins[1];
490                                 
491                                 if (ds > surf->extents[0] || dt > surf->extents[1])
492                                         continue;
493
494                                 if (surf->samples)
495                                 {
496                                         byte *lightmap;
497                                         int maps, line3, size3, dsfrac = ds & 15, dtfrac = dt & 15, scale = 0, r00 = 0, g00 = 0, b00 = 0, r01 = 0, g01 = 0, b01 = 0, r10 = 0, g10 = 0, b10 = 0, r11 = 0, g11 = 0, b11 = 0;
498                                         line3 = ((surf->extents[0]>>4)+1)*3;
499                                         size3 = ((surf->extents[0]>>4)+1) * ((surf->extents[1]>>4)+1)*3; // LordHavoc: *3 for colored lighting
500
501                                         lightmap = surf->samples + ((dt>>4) * ((surf->extents[0]>>4)+1) + (ds>>4))*3; // LordHavoc: *3 for color
502
503                                         for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
504                                         {
505                                                 scale = d_lightstylevalue[surf->styles[maps]];
506                                                 r00 += lightmap[      0] * scale;g00 += lightmap[      1] * scale;b00 += lightmap[      2] * scale;
507                                                 r01 += lightmap[      3] * scale;g01 += lightmap[      4] * scale;b01 += lightmap[      5] * scale;
508                                                 r10 += lightmap[line3+0] * scale;g10 += lightmap[line3+1] * scale;b10 += lightmap[line3+2] * scale;
509                                                 r11 += lightmap[line3+3] * scale;g11 += lightmap[line3+4] * scale;b11 += lightmap[line3+5] * scale;
510                                                 lightmap += size3;
511                                         }
512
513                                         color[0] += (float) ((((((((r11-r10) * dsfrac) >> 4) + r10)-((((r01-r00) * dsfrac) >> 4) + r00)) * dtfrac) >> 4) + ((((r01-r00) * dsfrac) >> 4) + r00)) * (1.0f / 256.0f);
514                                         color[1] += (float) ((((((((g11-g10) * dsfrac) >> 4) + g10)-((((g01-g00) * dsfrac) >> 4) + g00)) * dtfrac) >> 4) + ((((g01-g00) * dsfrac) >> 4) + g00)) * (1.0f / 256.0f);
515                                         color[2] += (float) ((((((((b11-b10) * dsfrac) >> 4) + b10)-((((b01-b00) * dsfrac) >> 4) + b00)) * dtfrac) >> 4) + ((((b01-b00) * dsfrac) >> 4) + b00)) * (1.0f / 256.0f);
516                                 }
517                                 return true; // success
518                         }
519                 }
520
521                 // go down back side
522                 node = node->children[side ^ 1];
523                 startz = mid;
524                 distz = endz - startz;
525                 goto loc0;
526 //              return RecursiveLightPoint (color, node->children[side ^ 1], x, y, mid, endz);
527         }
528 }
529
530 void R_LightPoint (vec3_t color, vec3_t p)
531 {
532         if (r_fullbright.value || !cl.worldmodel->lightdata)
533         {
534                 color[0] = color[1] = color[2] = 255;
535                 return;
536         }
537         
538         color[0] = color[1] = color[2] = r_ambient.value * 2.0f;
539         RecursiveLightPoint (color, cl.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
540 }
541
542 // LordHavoc: added light checking to the server
543 void SV_LightPoint (vec3_t color, vec3_t p)
544 {
545         if (!sv.worldmodel->lightdata)
546         {
547                 color[0] = color[1] = color[2] = 255;
548                 return;
549         }
550         
551         color[0] = color[1] = color[2] = 0;
552         RecursiveLightPoint (color, sv.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
553 }
554
555 // LordHavoc: R_DynamicLightPoint - acumulates the dynamic lighting
556 void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits)
557 {
558         int             i, j, k;
559         vec3_t  dist;
560         float   brightness, r, f;
561
562         if (!r_dynamic.value || (!dlightbits[0] && !dlightbits[1] && !dlightbits[2] && !dlightbits[3] && !dlightbits[4] && !dlightbits[5] && !dlightbits[6] && !dlightbits[7]))
563                 return;
564
565         for (j = 0;j < (MAX_DLIGHTS >> 5);j++)
566         {
567                 if (dlightbits[j])
568                 {
569                         for (i=0 ; i<32 ; i++)
570                         {
571                                 if ((!((1 << (i&31)) & dlightbits[i>>5])) || cl_dlights[i].die < cl.time || !cl_dlights[i].radius)
572                                         continue;
573                                 k = (j<<5)+i;
574                                 VectorSubtract (org, cl_dlights[k].origin, dist);
575                                 f = DotProduct(dist, dist) + LIGHTOFFSET;
576                                 r = cl_dlights[k].radius*cl_dlights[k].radius*LIGHTSCALE;
577                                 if (f < r)
578                                 {
579                                         brightness = r * 16.0f / f;
580                                         color[0] += brightness * cl_dlights[k].color[0];
581                                         color[1] += brightness * cl_dlights[k].color[1];
582                                         color[2] += brightness * cl_dlights[k].color[2];
583                                 }
584                         }
585                 }
586         }
587 }
588
589 // same as above but no bitmask to check
590 void R_DynamicLightPointNoMask(vec3_t color, vec3_t org)
591 {
592         int             i;
593         vec3_t  dist;
594         float   brightness, r, f;
595
596         if (!r_dynamic.value)
597                 return;
598
599         for (i=0 ; i<MAX_DLIGHTS ; i++)
600         {
601                 if (cl_dlights[i].die < cl.time || !cl_dlights[i].radius)
602                         continue;
603                 VectorSubtract (org, cl_dlights[i].origin, dist);
604                 f = DotProduct(dist, dist) + LIGHTOFFSET;
605                 r = cl_dlights[i].radius*cl_dlights[i].radius*LIGHTSCALE;
606                 if (f < r)
607                 {
608                         brightness = r * 16.0f / f;
609                         if (cl_dlights[i].dark)
610                                 brightness = -brightness;
611                         color[0] += brightness * cl_dlights[i].color[0];
612                         color[1] += brightness * cl_dlights[i].color[1];
613                         color[2] += brightness * cl_dlights[i].color[2];
614                 }
615         }
616 }
617
618 void R_CompleteLightPoint (vec3_t color, vec3_t p)
619 {
620         R_LightPoint(color, p);
621         R_DynamicLightPointNoMask(color, p);
622 }
623
624 extern float *aliasvert;
625 extern float *aliasvertnorm;
626 extern byte *aliasvertcolor;
627 extern float modelalpha;
628 extern qboolean lighthalf;
629 extern int modeldlightbits[8];
630 void R_LightModel(int numverts, vec3_t center, vec3_t basecolor)
631 {
632         // LordHavoc: warning: reliance on int being 4 bytes here (of course the d_8to24table relies on that too...)
633         int i, j, nearlights = 0, color;
634         vec3_t dist, mod;
635         float t, t1, t2, t3, *avn;
636         byte r,g,b,a, *avc;
637         struct
638         {
639                 vec3_t color;
640                 vec3_t origin;
641         } nearlight[MAX_DLIGHTS];
642         avc = aliasvertcolor;
643         avn = aliasvertnorm;
644         a = (byte) bound((int) 0, (int) (modelalpha * 255.0f), (int) 255);
645         if (currententity->effects & EF_FULLBRIGHT)
646         {
647                 if (lighthalf)
648                 {
649                         ((byte *)&color)[0] = (byte) ((float) (128.0f * currententity->colormod[0]));
650                         ((byte *)&color)[1] = (byte) ((float) (128.0f * currententity->colormod[1]));
651                         ((byte *)&color)[2] = (byte) ((float) (128.0f * currententity->colormod[2]));
652                 }
653                 else
654                 {
655                         ((byte *)&color)[0] = (byte) ((float) (255.0f * currententity->colormod[0]));
656                         ((byte *)&color)[1] = (byte) ((float) (255.0f * currententity->colormod[1]));
657                         ((byte *)&color)[2] = (byte) ((float) (255.0f * currententity->colormod[2]));
658                 }
659                 ((byte *)&color)[3] = a;
660                 for (i = 0;i < numverts;i++)
661                 {
662                         *((int *)avc) = color;
663                         avc += 4;
664                 }
665                 return;
666         }
667         if (lighthalf)
668         {
669                 mod[0] = currententity->colormod[0] * 0.5f;
670                 mod[1] = currententity->colormod[1] * 0.5f;
671                 mod[2] = currententity->colormod[2] * 0.5f;
672         }
673         else
674         {
675                 mod[0] = currententity->colormod[0];
676                 mod[1] = currententity->colormod[1];
677                 mod[2] = currententity->colormod[2];
678         }
679         basecolor[0] *= mod[0];
680         basecolor[1] *= mod[1];
681         basecolor[2] *= mod[2];
682         if (r_lightmodels.value)
683         {
684                 for (i = 0;i < MAX_DLIGHTS;i++)
685                 {
686                         if (!modeldlightbits[i >> 5])
687                         {
688                                 i |= 31;
689                                 continue;
690                         }
691                         if (!(modeldlightbits[i >> 5] & (1 << (i & 31))))
692                                 continue;
693                         VectorSubtract (center, cl_dlights[i].origin, dist);
694                         t1 = cl_dlights[i].radius*cl_dlights[i].radius*LIGHTSCALE;
695                         t2 = DotProduct(dist,dist) + LIGHTOFFSET;
696                         if (t2 < t1)
697                         {
698                                 VectorCopy(cl_dlights[i].origin, nearlight[nearlights].origin);
699                                 nearlight[nearlights].color[0] = cl_dlights[i].color[0] * cl_dlights[i].radius * cl_dlights[i].radius * mod[0];
700                                 nearlight[nearlights].color[1] = cl_dlights[i].color[1] * cl_dlights[i].radius * cl_dlights[i].radius * mod[1];
701                                 nearlight[nearlights].color[2] = cl_dlights[i].color[2] * cl_dlights[i].radius * cl_dlights[i].radius * mod[2];
702                                 t1 = (128.0f / LIGHTSCALE) / t2;
703                                 basecolor[0] += nearlight[nearlights].color[0] * t1;
704                                 basecolor[1] += nearlight[nearlights].color[1] * t1;
705                                 basecolor[2] += nearlight[nearlights].color[2] * t1;
706                                 nearlights++;
707                         }
708                 }
709         }
710         else
711         {
712                 for (i = 0;i < MAX_DLIGHTS;i++)
713                 {
714                         if (!modeldlightbits[i >> 5])
715                         {
716                                 i |= 31;
717                                 continue;
718                         }
719                         if (!(modeldlightbits[i >> 5] & (1 << (i & 31))))
720                                 continue;
721                         VectorSubtract (center, cl_dlights[i].origin, dist);
722                         t1 = cl_dlights[i].radius*cl_dlights[i].radius*LIGHTSCALE;
723                         t2 = DotProduct(dist,dist) + LIGHTOFFSET;
724                         if (t2 < t1)
725                         {
726                                 dist[0] = cl_dlights[i].color[0] * cl_dlights[i].radius * cl_dlights[i].radius * mod[0];
727                                 dist[1] = cl_dlights[i].color[1] * cl_dlights[i].radius * cl_dlights[i].radius * mod[1];
728                                 dist[2] = cl_dlights[i].color[2] * cl_dlights[i].radius * cl_dlights[i].radius * mod[2];
729                                 t1 = (192.0f / LIGHTSCALE) / t2;
730                                 basecolor[0] += dist[0] * t1;
731                                 basecolor[1] += dist[1] * t1;
732                                 basecolor[2] += dist[2] * t1;
733                         }
734                 }
735         }
736         t1 = bound(0, basecolor[0], 255);r = (byte) t1;
737         t1 = bound(0, basecolor[1], 255);g = (byte) t1;
738         t1 = bound(0, basecolor[2], 255);b = (byte) t1;
739         ((byte *)&color)[0] = r;
740         ((byte *)&color)[1] = g;
741         ((byte *)&color)[2] = b;
742         ((byte *)&color)[3] = a;
743         if (nearlights)
744         {
745                 int temp;
746                 vec3_t v;
747                 float *av;
748                 av = aliasvert;
749                 if (nearlights == 1)
750                 {
751                         for (i = 0;i < numverts;i++)
752                         {
753                                 VectorSubtract(nearlight[0].origin, av, v);
754                                 t = DotProduct(avn,v);
755                                 if (t > 0)
756                                 {
757                                         t /= DotProduct(v,v);
758                                         temp = (int) ((float) (basecolor[0] + nearlight[0].color[0] * t));if (temp < 0) temp = 0;else if (temp > 255) temp = 255;avc[0] = temp;
759                                         temp = (int) ((float) (basecolor[1] + nearlight[0].color[1] * t));if (temp < 0) temp = 0;else if (temp > 255) temp = 255;avc[1] = temp;
760                                         temp = (int) ((float) (basecolor[2] + nearlight[0].color[2] * t));if (temp < 0) temp = 0;else if (temp > 255) temp = 255;avc[2] = temp;
761                                         avc[3] = a;
762                                 }
763                                 else
764                                         *((int *)avc) = color;
765                                 avc += 4;
766                                 av+=3;
767                                 avn+=3;
768                         }
769                 }
770                 else
771                 {
772                         int i1, i2, i3, k;
773                         for (i = 0;i < numverts;i++)
774                         {
775                                 t1 = basecolor[0];
776                                 t2 = basecolor[1];
777                                 t3 = basecolor[2];
778                                 k = false;
779                                 for (j = 0;j < nearlights;j++)
780                                 {
781                                         VectorSubtract(nearlight[j].origin, av, v);
782                                         t = DotProduct(avn,v);
783                                         if (t > 0)
784                                         {
785                                                 t /= DotProduct(v,v);
786                                                 t1 += nearlight[j].color[0] * t;
787                                                 t2 += nearlight[j].color[1] * t;
788                                                 t3 += nearlight[j].color[2] * t;
789                                                 k = true;
790                                         }
791                                 }
792                                 if (k) // dodge the costly float -> int conversions
793                                 {
794                                         i1 = t1;if (i1 < 0) i1 = 0;else if (i1 > 255) i1 = 255;avc[0] = i1;
795                                         i2 = t2;if (i2 < 0) i2 = 0;else if (i2 > 255) i2 = 255;avc[1] = i2;
796                                         i3 = t3;if (i3 < 0) i3 = 0;else if (i3 > 255) i3 = 255;avc[2] = i3;
797                                         avc[3] = a;
798                                 }
799                                 else
800                                         *((int *)avc) = color;
801                                 avc += 4;
802                         }
803                 }
804         }
805         else
806         {
807                 for (i = 0;i < numverts;i++)
808                 {
809                         *((int *)avc) = color;
810                         avc += 4;
811                 }
812         }
813 }