]> icculus.org git repositories - divverent/darkplaces.git/blob - r_light.c
37a2a0fd1753f6ae5a0ae6ef46f375bc027c88b1
[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 extern cvar_t gl_transform;
27
28 void r_light_start()
29 {
30 }
31
32 void r_light_shutdown()
33 {
34 }
35
36 void r_light_newmap()
37 {
38 }
39
40 void R_Light_Init()
41 {
42         Cvar_RegisterVariable(&r_lightmodels);
43         R_RegisterModule("R_Light", r_light_start, r_light_shutdown, r_light_newmap);
44 }
45
46 int     r_dlightframecount;
47
48 /*
49 ==================
50 R_AnimateLight
51 ==================
52 */
53 void R_AnimateLight (void)
54 {
55         int                     i,j,k;
56         
57 //
58 // light animations
59 // 'm' is normal light, 'a' is no light, 'z' is double bright
60         i = (int)(cl.time*10);
61         for (j=0 ; j<MAX_LIGHTSTYLES ; j++)
62         {
63                 if (!cl_lightstyle[j].length)
64                 {
65                         d_lightstylevalue[j] = 256;
66                         continue;
67                 }
68                 k = i % cl_lightstyle[j].length;
69                 k = cl_lightstyle[j].map[k] - 'a';
70                 k = k*22;
71                 d_lightstylevalue[j] = k;
72         }       
73 }
74
75 /*
76 =============================================================================
77
78 DYNAMIC LIGHTS
79
80 =============================================================================
81 */
82
83 /*
84 =============
85 R_MarkLights
86 =============
87 */
88 void R_OldMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, mnode_t *node)
89 {
90         float           ndist, maxdist;
91         msurface_t      *surf;
92         int                     i;
93
94         if (!r_dynamic.value)
95                 return;
96
97         // for comparisons to minimum acceptable light
98         maxdist = light->radius * light->radius;
99
100         // clamp radius to avoid exceeding 32768 entry division table
101         if (maxdist > 4194304)
102                 maxdist = 4194304;
103
104 loc0:
105         if (node->contents < 0)
106                 return;
107
108         ndist = PlaneDiff(lightorigin, node->plane);
109         
110         if (ndist > light->radius)
111         {
112                 if (node->children[0]->contents >= 0) // LordHavoc: save some time by not pushing another stack frame
113                 {
114                         node = node->children[0];
115                         goto loc0;
116                 }
117                 return;
118         }
119         if (ndist < -light->radius)
120         {
121                 if (node->children[1]->contents >= 0) // LordHavoc: save some time by not pushing another stack frame
122                 {
123                         node = node->children[1];
124                         goto loc0;
125                 }
126                 return;
127         }
128
129         if (node->dlightframe != r_dlightframecount) // not dynamic until now
130         {
131                 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;
132                 node->dlightframe = r_dlightframecount;
133         }
134         node->dlightbits[bitindex] |= bit;
135
136 // mark the polygons
137         surf = cl.worldmodel->surfaces + node->firstsurface;
138         for (i=0 ; i<node->numsurfaces ; i++, surf++)
139         {
140                 int d;
141                 float dist, dist2, impact[3];
142                 dist = ndist;
143                 if (surf->flags & SURF_PLANEBACK)
144                         dist = -dist;
145
146                 if (dist < -0.25f && !(surf->flags & SURF_LIGHTBOTHSIDES))
147                         continue;
148
149                 dist2 = dist * dist;
150                 if (dist2 >= maxdist)
151                         continue;
152
153                 impact[0] = light->origin[0] - surf->plane->normal[0] * dist;
154                 impact[1] = light->origin[1] - surf->plane->normal[1] * dist;
155                 impact[2] = light->origin[2] - surf->plane->normal[2] * dist;
156
157                 d = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
158
159                 if (d < 0)
160                 {
161                         dist2 += d * d;
162                         if (dist2 >= maxdist)
163                                 continue;
164                 }
165                 else
166                 {
167                         d -= surf->extents[0] + 16;
168                         if (d > 0)
169                         {
170                                 dist2 += d * d;
171                                 if (dist2 >= maxdist)
172                                         continue;
173                         }
174                 }
175
176                 d = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
177
178                 if (d < 0)
179                 {
180                         dist2 += d * d;
181                         if (dist2 >= maxdist)
182                                 continue;
183                 }
184                 else
185                 {
186                         d -= surf->extents[1] + 16;
187                         if (d > 0)
188                         {
189                                 dist2 += d * d;
190                                 if (dist2 >= maxdist)
191                                         continue;
192                         }
193                 }
194
195                 if (surf->dlightframe != r_dlightframecount) // not dynamic until now
196                 {
197                         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;
198                         surf->dlightframe = r_dlightframecount;
199                 }
200                 surf->dlightbits[bitindex] |= bit;
201
202                 /*
203                 if (((surf->flags & SURF_PLANEBACK) == 0) == ((PlaneDist(lightorigin, surf->plane)) >= surf->plane->dist))
204                 {
205                         if (surf->dlightframe != r_dlightframecount) // not dynamic until now
206                         {
207                                 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;
208                                 surf->dlightframe = r_dlightframecount;
209                         }
210                         surf->dlightbits[bitindex] |= bit;
211                 }
212                 */
213         }
214
215         if (node->children[0]->contents >= 0)
216         {
217                 if (node->children[1]->contents >= 0)
218                 {
219                         R_OldMarkLights (lightorigin, light, bit, bitindex, node->children[0]);
220                         node = node->children[1];
221                         goto loc0;
222                 }
223                 else
224                 {
225                         node = node->children[0];
226                         goto loc0;
227                 }
228         }
229         else if (node->children[1]->contents >= 0)
230         {
231                 node = node->children[1];
232                 goto loc0;
233         }
234 }
235
236 void R_NoVisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model)
237 {
238         R_OldMarkLights(lightorigin, light, bit, bitindex, model->nodes + model->hulls[0].firstclipnode);
239 }
240
241 int lightframe = 0;
242 void R_VisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model)
243 {
244         mleaf_t *pvsleaf = Mod_PointInLeaf (lightorigin, model);
245
246         if (!r_dynamic.value)
247                 return;
248
249         if (!pvsleaf->compressed_vis)
250         {       // no vis info, so make all visible
251                 R_OldMarkLights(lightorigin, light, bit, bitindex, model->nodes + model->hulls[0].firstclipnode);
252                 return;
253         }
254         else
255         {
256                 int             i, k, l, m, c;
257                 msurface_t *surf, **mark;
258                 mleaf_t *leaf;
259                 byte    *in = pvsleaf->compressed_vis;
260                 int             row = (model->numleafs+7)>>3;
261                 float   low[3], high[3], radius, dist, maxdist;
262
263                 lightframe++;
264
265                 radius = light->radius * 2;
266
267                 // clamp radius to avoid exceeding 32768 entry division table
268                 if (radius > 2048)
269                         radius = 2048;
270
271                 low[0] = lightorigin[0] - radius;low[1] = lightorigin[1] - radius;low[2] = lightorigin[2] - radius;
272                 high[0] = lightorigin[0] + radius;high[1] = lightorigin[1] + radius;high[2] = lightorigin[2] + radius;
273
274                 // for comparisons to minimum acceptable light
275                 maxdist = radius*radius;
276
277                 k = 0;
278                 while (k < row)
279                 {
280                         c = *in++;
281                         if (c)
282                         {
283                                 l = model->numleafs - (k << 3);
284                                 if (l > 8)
285                                         l = 8;
286                                 for (i=0 ; i<l ; i++)
287                                 {
288                                         if (c & (1<<i))
289                                         {
290                                                 leaf = &model->leafs[(k << 3)+i+1];
291                                                 leaf->lightframe = lightframe;
292                                                 if (leaf->visframe != r_visframecount)
293                                                         continue;
294                                                 if (leaf->contents == CONTENTS_SOLID)
295                                                         continue;
296                                                 // if out of the light radius, skip
297                                                 if (leaf->minmaxs[0] > high[0] || leaf->minmaxs[3] < low[0]
298                                                  || leaf->minmaxs[1] > high[1] || leaf->minmaxs[4] < low[1]
299                                                  || leaf->minmaxs[2] > high[2] || leaf->minmaxs[5] < low[2])
300                                                         continue;
301                                                 if (leaf->dlightframe != r_dlightframecount) // not dynamic until now
302                                                 {
303                                                         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;
304                                                         leaf->dlightframe = r_dlightframecount;
305                                                 }
306                                                 leaf->dlightbits[bitindex] |= bit;
307                                                 if ((m = leaf->nummarksurfaces))
308                                                 {
309                                                         mark = leaf->firstmarksurface;
310                                                         do
311                                                         {
312                                                                 surf = *mark++;
313                                                                 if (surf->visframe != r_framecount || surf->lightframe == lightframe)
314                                                                         continue;
315                                                                 surf->lightframe = lightframe;
316                                                                 dist = PlaneDiff(lightorigin, surf->plane);
317                                                                 if (surf->flags & SURF_PLANEBACK)
318                                                                         dist = -dist;
319                                                                 // LordHavoc: make sure it is infront of the surface and not too far away
320                                                                 if ((dist >= -0.25f || (surf->flags & SURF_LIGHTBOTHSIDES)) && dist < radius)
321                                                                 {
322                                                                         int d;
323                                                                         float dist2, impact[3];
324
325                                                                         dist2 = dist * dist;
326
327                                                                         impact[0] = light->origin[0] - surf->plane->normal[0] * dist;
328                                                                         impact[1] = light->origin[1] - surf->plane->normal[1] * dist;
329                                                                         impact[2] = light->origin[2] - surf->plane->normal[2] * dist;
330
331                                                                         d = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
332
333                                                                         if (d < 0)
334                                                                         {
335                                                                                 dist2 += d * d;
336                                                                                 if (dist2 >= maxdist)
337                                                                                         continue;
338                                                                         }
339                                                                         else
340                                                                         {
341                                                                                 d -= surf->extents[0] + 16;
342                                                                                 if (d > 0)
343                                                                                 {
344                                                                                         dist2 += d * d;
345                                                                                         if (dist2 >= maxdist)
346                                                                                                 continue;
347                                                                                 }
348                                                                         }
349
350                                                                         d = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
351
352                                                                         if (d < 0)
353                                                                         {
354                                                                                 dist2 += d * d;
355                                                                                 if (dist2 >= maxdist)
356                                                                                         continue;
357                                                                         }
358                                                                         else
359                                                                         {
360                                                                                 d -= surf->extents[1] + 16;
361                                                                                 if (d > 0)
362                                                                                 {
363                                                                                         dist2 += d * d;
364                                                                                         if (dist2 >= maxdist)
365                                                                                                 continue;
366                                                                                 }
367                                                                         }
368
369                                                                         if (surf->dlightframe != r_dlightframecount) // not dynamic until now
370                                                                         {
371                                                                                 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;
372                                                                                 surf->dlightframe = r_dlightframecount;
373                                                                         }
374                                                                         surf->dlightbits[bitindex] |= bit;
375                                                                 }
376                                                         }
377                                                         while (--m);
378                                                 }
379                                         }
380                                 }
381                                 k++;
382                                 continue;
383                         }
384                 
385                         k += *in++;
386                 }
387         }
388 }
389
390
391 /*
392 =============
393 R_PushDlights
394 =============
395 */
396 void R_PushDlights (void)
397 {
398         int             i;
399         dlight_t        *l;
400
401         r_dlightframecount = r_framecount + 1;  // because the count hasn't advanced yet for this frame
402
403         if (!r_dynamic.value)
404                 return;
405
406         l = cl_dlights;
407
408         for (i=0 ; i<MAX_DLIGHTS ; i++, l++)
409         {
410                 if (!l->radius)
411                         continue;
412 //              R_MarkLights (l->origin, l, 1<<(i&31), i >> 5, cl.worldmodel->nodes );
413                 R_VisMarkLights (l->origin, l, 1<<(i&31), i >> 5, cl.worldmodel);
414         }
415 }
416
417
418 /*
419 =============================================================================
420
421 LIGHT SAMPLING
422
423 =============================================================================
424 */
425
426 mplane_t                *lightplane;
427 vec3_t                  lightspot;
428
429 extern cvar_t r_ambient;
430
431 /*
432 int RecursiveLightPoint (vec3_t color, mnode_t *node, vec3_t start, vec3_t end)
433 {
434         float           front, back, frac;
435         vec3_t          mid;
436
437 loc0:
438         if (node->contents < 0)
439                 return false;           // didn't hit anything
440         
441 // calculate mid point
442         front = PlaneDiff (start, node->plane);
443         back = PlaneDiff (end, node->plane);
444
445         // LordHavoc: optimized recursion
446         if ((back < 0) == (front < 0))
447 //              return RecursiveLightPoint (color, node->children[front < 0], start, end);
448         {
449                 node = node->children[front < 0];
450                 goto loc0;
451         }
452         
453         frac = front / (front-back);
454         mid[0] = start[0] + (end[0] - start[0])*frac;
455         mid[1] = start[1] + (end[1] - start[1])*frac;
456         mid[2] = start[2] + (end[2] - start[2])*frac;
457         
458 // go down front side
459         if (RecursiveLightPoint (color, node->children[front < 0], start, mid))
460                 return true;    // hit something
461         else
462         {
463                 int i, ds, dt;
464                 msurface_t *surf;
465         // check for impact on this node
466                 VectorCopy (mid, lightspot);
467                 lightplane = node->plane;
468
469                 surf = cl.worldmodel->surfaces + node->firstsurface;
470                 for (i = 0;i < node->numsurfaces;i++, surf++)
471                 {
472                         if (surf->flags & SURF_DRAWTILED)
473                                 continue;       // no lightmaps
474
475                         ds = (int) ((float) DotProduct (mid, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3]);
476                         dt = (int) ((float) DotProduct (mid, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3]);
477
478                         if (ds < surf->texturemins[0] || dt < surf->texturemins[1])
479                                 continue;
480                         
481                         ds -= surf->texturemins[0];
482                         dt -= surf->texturemins[1];
483                         
484                         if (ds > surf->extents[0] || dt > surf->extents[1])
485                                 continue;
486
487                         if (surf->samples)
488                         {
489                                 byte *lightmap;
490                                 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;
491                                 float scale;
492                                 line3 = ((surf->extents[0]>>4)+1)*3;
493
494                                 lightmap = surf->samples + ((dt>>4) * ((surf->extents[0]>>4)+1) + (ds>>4))*3; // LordHavoc: *3 for color
495
496                                 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
497                                 {
498                                         scale = (float) d_lightstylevalue[surf->styles[maps]] * 1.0 / 256.0;
499                                         r00 += (float) lightmap[      0] * scale;g00 += (float) lightmap[      1] * scale;b00 += (float) lightmap[2] * scale;
500                                         r01 += (float) lightmap[      3] * scale;g01 += (float) lightmap[      4] * scale;b01 += (float) lightmap[5] * scale;
501                                         r10 += (float) lightmap[line3+0] * scale;g10 += (float) lightmap[line3+1] * scale;b10 += (float) lightmap[line3+2] * scale;
502                                         r11 += (float) lightmap[line3+3] * scale;g11 += (float) lightmap[line3+4] * scale;b11 += (float) lightmap[line3+5] * scale;
503                                         lightmap += ((surf->extents[0]>>4)+1) * ((surf->extents[1]>>4)+1)*3; // LordHavoc: *3 for colored lighting
504                                 }
505
506                                 color[0] += (float) ((int) ((((((((r11-r10) * dsfrac) >> 4) + r10)-((((r01-r00) * dsfrac) >> 4) + r00)) * dtfrac) >> 4) + ((((r01-r00) * dsfrac) >> 4) + r00)));
507                                 color[1] += (float) ((int) ((((((((g11-g10) * dsfrac) >> 4) + g10)-((((g01-g00) * dsfrac) >> 4) + g00)) * dtfrac) >> 4) + ((((g01-g00) * dsfrac) >> 4) + g00)));
508                                 color[2] += (float) ((int) ((((((((b11-b10) * dsfrac) >> 4) + b10)-((((b01-b00) * dsfrac) >> 4) + b00)) * dtfrac) >> 4) + ((((b01-b00) * dsfrac) >> 4) + b00)));
509                         }
510                         return true; // success
511                 }
512
513         // go down back side
514                 return RecursiveLightPoint (color, node->children[front >= 0], mid, end);
515         }
516 }
517
518 void R_LightPoint (vec3_t color, vec3_t p)
519 {
520         vec3_t          end;
521         
522         if (r_fullbright.value || !cl.worldmodel->lightdata)
523         {
524                 color[0] = color[1] = color[2] = 255;
525                 return;
526         }
527
528         end[0] = p[0];
529         end[1] = p[1];
530         end[2] = p[2] - 2048;
531
532         color[0] = color[1] = color[2] = r_ambient.value * 2.0f;
533         RecursiveLightPoint (color, cl.worldmodel->nodes, p, end);
534 }
535
536 void SV_LightPoint (vec3_t color, vec3_t p)
537 {
538         vec3_t          end;
539         
540         if (!sv.worldmodel->lightdata)
541         {
542                 color[0] = color[1] = color[2] = 255;
543                 return;
544         }
545         
546         end[0] = p[0];
547         end[1] = p[1];
548         end[2] = p[2] - 2048;
549
550         color[0] = color[1] = color[2] = 0;
551         RecursiveLightPoint (color, sv.worldmodel->nodes, p, end);
552 }
553 */
554
555 int RecursiveLightPoint (vec3_t color, mnode_t *node, float x, float y, float startz, float endz)
556 {
557         int             side, distz = endz - startz;
558         float   front, back;
559         float   mid;
560
561 loc0:
562         if (node->contents < 0)
563                 return false;           // didn't hit anything
564
565         switch (node->plane->type)
566         {
567         case PLANE_X:
568                 node = node->children[x < node->plane->dist];
569                 goto loc0;
570         case PLANE_Y:
571                 node = node->children[y < node->plane->dist];
572                 goto loc0;
573         case PLANE_Z:
574                 side = startz < node->plane->dist;
575                 if ((endz < node->plane->dist) == side)
576                 {
577                         node = node->children[side];
578                         goto loc0;
579                 }
580                 // found an intersection
581 //              mid = startz + (endz - startz) * (startz - node->plane->dist) / (startz - endz);
582 //              mid = startz + distz * (startz - node->plane->dist) / (-distz);
583 //              mid = startz + (-(startz - node->plane->dist));
584 //              mid = startz - (startz - node->plane->dist);
585 //              mid = startz + node->plane->dist - startz;
586                 mid = node->plane->dist;
587                 break;
588         default:
589                 back = front = x * node->plane->normal[0] + y * node->plane->normal[1];
590                 front += startz * node->plane->normal[2];
591                 back += endz * node->plane->normal[2];
592                 side = front < node->plane->dist;
593                 if ((back < node->plane->dist) == side)
594                 {
595                         node = node->children[side];
596                         goto loc0;
597                 }
598                 // found an intersection
599 //              mid = startz + (endz - startz) * ((front - node->plane->dist) / ((front - node->plane->dist) - (back - node->plane->dist)));
600 //              mid = startz + (endz - startz) * ((front - node->plane->dist) / (front - back));
601                 mid = startz + distz * (front - node->plane->dist) / (front - back);
602                 break;
603         }
604         
605         // go down front side
606         if (node->children[side]->contents >= 0 && RecursiveLightPoint (color, node->children[side], x, y, startz, mid))
607                 return true;    // hit something
608         else
609         {
610                 // check for impact on this node
611                 if (node->numsurfaces)
612                 {
613                         int i, ds, dt;
614                         msurface_t *surf;
615                         lightspot[0] = x;
616                         lightspot[1] = y;
617                         lightspot[2] = mid;
618                         lightplane = node->plane;
619
620                         surf = cl.worldmodel->surfaces + node->firstsurface;
621                         for (i = 0;i < node->numsurfaces;i++, surf++)
622                         {
623                                 if (surf->flags & SURF_DRAWTILED)
624                                         continue;       // no lightmaps
625
626                                 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]);
627                                 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]);
628
629                                 if (ds < surf->texturemins[0] || dt < surf->texturemins[1])
630                                         continue;
631                                 
632                                 ds -= surf->texturemins[0];
633                                 dt -= surf->texturemins[1];
634                                 
635                                 if (ds > surf->extents[0] || dt > surf->extents[1])
636                                         continue;
637
638                                 if (surf->samples)
639                                 {
640                                         byte *lightmap;
641                                         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;
642                                         line3 = ((surf->extents[0]>>4)+1)*3;
643                                         size3 = ((surf->extents[0]>>4)+1) * ((surf->extents[1]>>4)+1)*3; // LordHavoc: *3 for colored lighting
644
645                                         lightmap = surf->samples + ((dt>>4) * ((surf->extents[0]>>4)+1) + (ds>>4))*3; // LordHavoc: *3 for color
646
647                                         for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
648                                         {
649                                                 scale = d_lightstylevalue[surf->styles[maps]];
650                                                 r00 += lightmap[      0] * scale;g00 += lightmap[      1] * scale;b00 += lightmap[      2] * scale;
651                                                 r01 += lightmap[      3] * scale;g01 += lightmap[      4] * scale;b01 += lightmap[      5] * scale;
652                                                 r10 += lightmap[line3+0] * scale;g10 += lightmap[line3+1] * scale;b10 += lightmap[line3+2] * scale;
653                                                 r11 += lightmap[line3+3] * scale;g11 += lightmap[line3+4] * scale;b11 += lightmap[line3+5] * scale;
654                                                 lightmap += size3;
655                                         }
656
657                                         color[0] += (float) ((((((((r11-r10) * dsfrac) >> 4) + r10)-((((r01-r00) * dsfrac) >> 4) + r00)) * dtfrac) >> 4) + ((((r01-r00) * dsfrac) >> 4) + r00)) * (1.0f / 256.0f);
658                                         color[1] += (float) ((((((((g11-g10) * dsfrac) >> 4) + g10)-((((g01-g00) * dsfrac) >> 4) + g00)) * dtfrac) >> 4) + ((((g01-g00) * dsfrac) >> 4) + g00)) * (1.0f / 256.0f);
659                                         color[2] += (float) ((((((((b11-b10) * dsfrac) >> 4) + b10)-((((b01-b00) * dsfrac) >> 4) + b00)) * dtfrac) >> 4) + ((((b01-b00) * dsfrac) >> 4) + b00)) * (1.0f / 256.0f);
660                                 }
661                                 return true; // success
662                         }
663                 }
664
665                 // go down back side
666                 node = node->children[side ^ 1];
667                 startz = mid;
668                 distz = endz - startz;
669                 goto loc0;
670 //              return RecursiveLightPoint (color, node->children[side ^ 1], x, y, mid, endz);
671         }
672 }
673
674 void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits)
675 {
676         int             i, j, k;
677         vec3_t  dist;
678         float   brightness, r, f;
679
680         if (!r_dynamic.value || (!dlightbits[0] && !dlightbits[1] && !dlightbits[2] && !dlightbits[3] && !dlightbits[4] && !dlightbits[5] && !dlightbits[6] && !dlightbits[7]))
681                 return;
682
683         for (j = 0;j < (MAX_DLIGHTS >> 5);j++)
684         {
685                 if (dlightbits[j])
686                 {
687                         for (i=0 ; i<32 ; i++)
688                         {
689                                 if (!((1 << i) & dlightbits[j]))
690                                         continue;
691                                 k = (j<<5)+i;
692                                 if (!cl_dlights[k].radius)
693                                         continue;
694                                 VectorSubtract (org, cl_dlights[k].origin, dist);
695                                 f = DotProduct(dist, dist) + LIGHTOFFSET;
696                                 r = cl_dlights[k].radius*cl_dlights[k].radius;
697                                 if (f < r)
698                                 {
699                                         brightness = r * 128.0f / f;
700                                         color[0] += brightness * cl_dlights[k].color[0];
701                                         color[1] += brightness * cl_dlights[k].color[1];
702                                         color[2] += brightness * cl_dlights[k].color[2];
703                                 }
704                         }
705                 }
706         }
707 }
708
709 void R_CompleteLightPoint (vec3_t color, vec3_t p, int dynamic)
710 {
711         mleaf_t *leaf;
712         leaf = Mod_PointInLeaf(p, cl.worldmodel);
713         if (leaf->contents == CONTENTS_SOLID)
714         {
715                 color[0] = color[1] = color[2] = 0;
716                 return;
717         }
718
719         if (r_fullbright.value || !cl.worldmodel->lightdata)
720         {
721                 color[0] = color[1] = color[2] = 255;
722                 return;
723         }
724         
725         color[0] = color[1] = color[2] = r_ambient.value * 2.0f;
726         RecursiveLightPoint (color, cl.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
727
728         if (dynamic)
729                 R_DynamicLightPoint(color, p, leaf->dlightbits);
730 }
731
732 void R_ModelLightPoint (vec3_t color, vec3_t p, int *dlightbits)
733 {
734         mleaf_t *leaf;
735         leaf = Mod_PointInLeaf(p, cl.worldmodel);
736         if (leaf->contents == CONTENTS_SOLID)
737         {
738                 color[0] = color[1] = color[2] = 0;
739                 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
740                 return;
741         }
742
743         if (r_fullbright.value || !cl.worldmodel->lightdata)
744         {
745                 color[0] = color[1] = color[2] = 255;
746                 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
747                 return;
748         }
749         
750         color[0] = color[1] = color[2] = r_ambient.value * 2.0f;
751         RecursiveLightPoint (color, cl.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
752
753         if (leaf->dlightframe == r_dlightframecount)
754         {
755                 dlightbits[0] = leaf->dlightbits[0];
756                 dlightbits[1] = leaf->dlightbits[1];
757                 dlightbits[2] = leaf->dlightbits[2];
758                 dlightbits[3] = leaf->dlightbits[3];
759                 dlightbits[4] = leaf->dlightbits[4];
760                 dlightbits[5] = leaf->dlightbits[5];
761                 dlightbits[6] = leaf->dlightbits[6];
762                 dlightbits[7] = leaf->dlightbits[7];
763         }
764         else
765                 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
766 }
767
768 /* // not currently used
769 void R_DynamicLightPointNoMask(vec3_t color, vec3_t org)
770 {
771         int             i;
772         vec3_t  dist;
773         float   brightness, r, f;
774
775         if (!r_dynamic.value)
776                 return;
777
778         for (i=0 ; i<MAX_DLIGHTS ; i++)
779         {
780                 if (!cl_dlights[i].radius)
781                         continue;
782                 VectorSubtract (org, cl_dlights[i].origin, dist);
783                 f = DotProduct(dist, dist) + LIGHTOFFSET;
784                 r = cl_dlights[i].radius*cl_dlights[i].radius;
785                 if (f < r)
786                 {
787                         brightness = r * 256.0f / f;
788                         if (cl_dlights[i].dark)
789                                 brightness = -brightness;
790                         color[0] += brightness * cl_dlights[i].color[0];
791                         color[1] += brightness * cl_dlights[i].color[1];
792                         color[2] += brightness * cl_dlights[i].color[2];
793                 }
794         }
795 }
796 */
797
798 extern float *aliasvert;
799 extern float *aliasvertnorm;
800 extern byte *aliasvertcolor;
801 extern float modelalpha;
802 void R_LightModel(entity_t *ent, int numverts, vec3_t center, vec3_t basecolor)
803 {
804         // LordHavoc: warning: reliance on int being 4 bytes here (of course the d_8to24table relies on that too...)
805         int i, j, nearlights = 0, color;
806         vec3_t dist, mod;
807         float t, t1, t2, t3, *avn;
808         byte r,g,b,a, *avc;
809         struct
810         {
811                 vec3_t color;
812                 vec3_t origin;
813         } nearlight[MAX_DLIGHTS];
814         int modeldlightbits[8];
815         avc = aliasvertcolor;
816         avn = aliasvertnorm;
817         a = (byte) bound((int) 0, (int) (modelalpha * 255.0f), (int) 255);
818         if (lighthalf)
819         {
820                 mod[0] = ent->colormod[0] * 0.5f;
821                 mod[1] = ent->colormod[1] * 0.5f;
822                 mod[2] = ent->colormod[2] * 0.5f;
823         }
824         else
825         {
826                 mod[0] = ent->colormod[0];
827                 mod[1] = ent->colormod[1];
828                 mod[2] = ent->colormod[2];
829         }
830         if (ent->effects & EF_FULLBRIGHT)
831         {
832                 ((byte *)&color)[0] = (byte) (255.0f * mod[0]);
833                 ((byte *)&color)[1] = (byte) (255.0f * mod[1]);
834                 ((byte *)&color)[2] = (byte) (255.0f * mod[2]);
835                 ((byte *)&color)[3] = a;
836                 for (i = 0;i < numverts;i++)
837                 {
838                         *((int *)avc) = color;
839                         avc += 4;
840                 }
841                 return;
842         }
843         R_ModelLightPoint(basecolor, center, modeldlightbits);
844
845         basecolor[0] *= mod[0];
846         basecolor[1] *= mod[1];
847         basecolor[2] *= mod[2];
848         for (i = 0;i < MAX_DLIGHTS;i++)
849         {
850                 if (!modeldlightbits[i >> 5])
851                 {
852                         i |= 31;
853                         continue;
854                 }
855                 if (!(modeldlightbits[i >> 5] & (1 << (i & 31))))
856                         continue;
857                 VectorSubtract (center, cl_dlights[i].origin, dist);
858                 t2 = DotProduct(dist,dist) + LIGHTOFFSET;
859                 t1 = cl_dlights[i].radius*cl_dlights[i].radius;
860                 if (t2 < t1)
861                 {
862                         // transform the light into the model's coordinate system
863                         if (gl_transform.value)
864                                 softwareuntransform(cl_dlights[i].origin, nearlight[nearlights].origin);
865                         else
866                         {
867                                 VectorCopy(cl_dlights[i].origin, nearlight[nearlights].origin);
868                         }
869                         nearlight[nearlights].color[0] = cl_dlights[i].color[0] * t1 * mod[0];
870                         nearlight[nearlights].color[1] = cl_dlights[i].color[1] * t1 * mod[1];
871                         nearlight[nearlights].color[2] = cl_dlights[i].color[2] * t1 * mod[2];
872                         if (r_lightmodels.value && (ent == NULL || ent != cl_dlights[i].ent))
873                                 nearlights++;
874                         else
875                         {
876                                 t1 = 1.0f / t2;
877                                 basecolor[0] += nearlight[nearlights].color[0] * t1;
878                                 basecolor[1] += nearlight[nearlights].color[1] * t1;
879                                 basecolor[2] += nearlight[nearlights].color[2] * t1;
880                         }
881                 }
882         }
883         t1 = bound(0, basecolor[0], 255);r = (byte) t1;
884         t1 = bound(0, basecolor[1], 255);g = (byte) t1;
885         t1 = bound(0, basecolor[2], 255);b = (byte) t1;
886         ((byte *)&color)[0] = r;
887         ((byte *)&color)[1] = g;
888         ((byte *)&color)[2] = b;
889         ((byte *)&color)[3] = a;
890         if (nearlights)
891         {
892                 int temp;
893                 vec3_t v;
894                 float *av;
895                 av = aliasvert;
896                 if (nearlights == 1)
897                 {
898                         for (i = 0;i < numverts;i++)
899                         {
900                                 VectorSubtract(nearlight[0].origin, av, v);
901                                 t = DotProduct(avn,v);
902                                 if (t > 0)
903                                 {
904                                         t /= (DotProduct(v,v) + LIGHTOFFSET);
905                                         temp = (int) ((float) (basecolor[0] + nearlight[0].color[0] * t));
906                                         avc[0] = bound(0, temp, 255);
907                                         temp = (int) ((float) (basecolor[1] + nearlight[0].color[1] * t));
908                                         avc[1] = bound(0, temp, 255);
909                                         temp = (int) ((float) (basecolor[2] + nearlight[0].color[2] * t));
910                                         avc[2] = bound(0, temp, 255);
911                                         avc[3] = a;
912                                 }
913                                 else
914                                         *((int *)avc) = color;
915                                 avc += 4;
916                                 av += 3;
917                                 avn += 3;
918                         }
919                 }
920                 else
921                 {
922                         for (i = 0;i < numverts;i++)
923                         {
924                                 int lit;
925                                 t1 = basecolor[0];
926                                 t2 = basecolor[1];
927                                 t3 = basecolor[2];
928                                 lit = false;
929                                 for (j = 0;j < nearlights;j++)
930                                 {
931                                         VectorSubtract(nearlight[j].origin, av, v);
932                                         t = DotProduct(avn,v);
933                                         if (t > 0)
934                                         {
935                                                 t /= (DotProduct(v,v) + LIGHTOFFSET);
936                                                 t1 += nearlight[j].color[0] * t;
937                                                 t2 += nearlight[j].color[1] * t;
938                                                 t3 += nearlight[j].color[2] * t;
939                                                 lit = true;
940                                         }
941                                 }
942                                 if (lit)
943                                 {
944                                         int i1, i2, i3;
945                                         i1 = (int) t1;
946                                         avc[0] = bound(0, i1, 255);
947                                         i2 = (int) t2;
948                                         avc[1] = bound(0, i2, 255);
949                                         i3 = (int) t3;
950                                         avc[2] = bound(0, i3, 255);
951                                         avc[3] = a;
952                                 }
953                                 else // dodge the costly float -> int conversions
954                                         *((int *)avc) = color;
955                                 avc += 4;
956                                 av += 3;
957                                 avn += 3;
958                         }
959                 }
960         }
961         else
962         {
963                 for (i = 0;i < numverts;i++)
964                 {
965                         *((int *)avc) = color;
966                         avc += 4;
967                 }
968         }
969 }