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