cleaned up nearly all of the externs in .c files (moved to appropriate .h files)
[divverent/darkplaces.git] / r_light.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // r_light.c
21
22 #include "quakedef.h"
23
24 cvar_t r_lightmodels = {"r_lightmodels", "1"};
25
26 void r_light_start(void)
27 {
28 }
29
30 void r_light_shutdown(void)
31 {
32 }
33
34 void r_light_newmap(void)
35 {
36 }
37
38 void R_Light_Init(void)
39 {
40         Cvar_RegisterVariable(&r_lightmodels);
41         R_RegisterModule("R_Light", r_light_start, r_light_shutdown, r_light_newmap);
42 }
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         mleaf_t         *leaf;
89         int                     i;
90
91         if (!r_dynamic.value)
92                 return;
93
94         // for comparisons to minimum acceptable light
95         maxdist = light->radius * light->radius;
96
97         // clamp radius to avoid exceeding 32768 entry division table
98         if (maxdist > 4194304)
99                 maxdist = 4194304;
100
101 loc0:
102         if (node->contents < 0)
103         {
104                 if (node->contents != CONTENTS_SOLID)
105                 {
106                         leaf = (mleaf_t *)node;
107                         if (leaf->dlightframe != r_framecount) // not dynamic until now
108                         {
109                                 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;
110                                 leaf->dlightframe = r_framecount;
111                         }
112                         leaf->dlightbits[bitindex] |= bit;
113                 }
114                 return;
115         }
116
117         ndist = PlaneDiff(lightorigin, node->plane);
118         
119         if (ndist > light->radius)
120         {
121                 node = node->children[0];
122                 goto loc0;
123         }
124         if (ndist < -light->radius)
125         {
126                 node = node->children[1];
127                 goto loc0;
128         }
129
130 // mark the polygons
131         surf = cl.worldmodel->surfaces + node->firstsurface;
132         for (i=0 ; i<node->numsurfaces ; i++, surf++)
133         {
134                 int d;
135                 float dist, dist2, impact[3];
136                 if (surf->visframe != r_framecount)
137                         continue;
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_framecount) // 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_framecount;
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_framecount) // 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_framecount;
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                                                 if (leaf->visframe != r_framecount)
288                                                         continue;
289                                                 if (leaf->contents == CONTENTS_SOLID)
290                                                         continue;
291                                                 // if out of the light radius, skip
292                                                 if (leaf->mins[0] > high[0] || leaf->maxs[0] < low[0]
293                                                  || leaf->mins[1] > high[1] || leaf->maxs[1] < low[1]
294                                                  || leaf->mins[2] > high[2] || leaf->maxs[2] < low[2])
295                                                         continue;
296                                                 if (leaf->dlightframe != r_framecount) // not dynamic until now
297                                                 {
298                                                         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;
299                                                         leaf->dlightframe = r_framecount;
300                                                 }
301                                                 leaf->dlightbits[bitindex] |= bit;
302                                                 if ((m = leaf->nummarksurfaces))
303                                                 {
304                                                         mark = leaf->firstmarksurface;
305                                                         do
306                                                         {
307                                                                 surf = *mark++;
308                                                                 if (surf->visframe != r_framecount || surf->lightframe == lightframe)
309                                                                         continue;
310                                                                 surf->lightframe = lightframe;
311                                                                 dist = PlaneDiff(lightorigin, surf->plane);
312                                                                 if (surf->flags & SURF_PLANEBACK)
313                                                                         dist = -dist;
314                                                                 // LordHavoc: make sure it is infront of the surface and not too far away
315                                                                 if ((dist >= -0.25f || (surf->flags & SURF_LIGHTBOTHSIDES)) && dist < radius)
316                                                                 {
317                                                                         int d;
318                                                                         float dist2, impact[3];
319
320                                                                         dist2 = dist * dist;
321
322                                                                         impact[0] = light->origin[0] - surf->plane->normal[0] * dist;
323                                                                         impact[1] = light->origin[1] - surf->plane->normal[1] * dist;
324                                                                         impact[2] = light->origin[2] - surf->plane->normal[2] * dist;
325
326                                                                         d = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
327
328                                                                         if (d < 0)
329                                                                         {
330                                                                                 dist2 += d * d;
331                                                                                 if (dist2 >= maxdist)
332                                                                                         continue;
333                                                                         }
334                                                                         else
335                                                                         {
336                                                                                 d -= surf->extents[0] + 16;
337                                                                                 if (d > 0)
338                                                                                 {
339                                                                                         dist2 += d * d;
340                                                                                         if (dist2 >= maxdist)
341                                                                                                 continue;
342                                                                                 }
343                                                                         }
344
345                                                                         d = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
346
347                                                                         if (d < 0)
348                                                                         {
349                                                                                 dist2 += d * d;
350                                                                                 if (dist2 >= maxdist)
351                                                                                         continue;
352                                                                         }
353                                                                         else
354                                                                         {
355                                                                                 d -= surf->extents[1] + 16;
356                                                                                 if (d > 0)
357                                                                                 {
358                                                                                         dist2 += d * d;
359                                                                                         if (dist2 >= maxdist)
360                                                                                                 continue;
361                                                                                 }
362                                                                         }
363
364                                                                         if (surf->dlightframe != r_framecount) // not dynamic until now
365                                                                         {
366                                                                                 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;
367                                                                                 surf->dlightframe = r_framecount;
368                                                                         }
369                                                                         surf->dlightbits[bitindex] |= bit;
370                                                                 }
371                                                         }
372                                                         while (--m);
373                                                 }
374                                         }
375                                 }
376                                 k++;
377                                 continue;
378                         }
379                 
380                         k += *in++;
381                 }
382         }
383 }
384
385
386 /*
387 =============
388 R_PushDlights
389 =============
390 */
391 void R_PushDlights (void)
392 {
393         int             i;
394         dlight_t        *l;
395
396         if (!r_dynamic.value)
397                 return;
398
399         l = cl_dlights;
400
401         for (i=0 ; i<MAX_DLIGHTS ; i++, l++)
402         {
403                 if (!l->radius)
404                         continue;
405 //              R_MarkLights (l->origin, l, 1<<(i&31), i >> 5, cl.worldmodel->nodes );
406                 R_VisMarkLights (l->origin, l, 1<<(i&31), i >> 5, cl.worldmodel);
407         }
408 }
409
410
411 /*
412 =============================================================================
413
414 LIGHT SAMPLING
415
416 =============================================================================
417 */
418
419 mplane_t                *lightplane;
420 vec3_t                  lightspot;
421
422 /*
423 int RecursiveLightPoint (vec3_t color, mnode_t *node, vec3_t start, vec3_t end)
424 {
425         float           front, back, frac;
426         vec3_t          mid;
427
428 loc0:
429         if (node->contents < 0)
430                 return false;           // didn't hit anything
431         
432 // calculate mid point
433         front = PlaneDiff (start, node->plane);
434         back = PlaneDiff (end, node->plane);
435
436         // LordHavoc: optimized recursion
437         if ((back < 0) == (front < 0))
438 //              return RecursiveLightPoint (color, node->children[front < 0], start, end);
439         {
440                 node = node->children[front < 0];
441                 goto loc0;
442         }
443         
444         frac = front / (front-back);
445         mid[0] = start[0] + (end[0] - start[0])*frac;
446         mid[1] = start[1] + (end[1] - start[1])*frac;
447         mid[2] = start[2] + (end[2] - start[2])*frac;
448         
449 // go down front side
450         if (RecursiveLightPoint (color, node->children[front < 0], start, mid))
451                 return true;    // hit something
452         else
453         {
454                 int i, ds, dt;
455                 msurface_t *surf;
456         // check for impact on this node
457                 VectorCopy (mid, lightspot);
458                 lightplane = node->plane;
459
460                 surf = cl.worldmodel->surfaces + node->firstsurface;
461                 for (i = 0;i < node->numsurfaces;i++, surf++)
462                 {
463                         if (surf->flags & SURF_DRAWTILED)
464                                 continue;       // no lightmaps
465
466                         ds = (int) ((float) DotProduct (mid, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3]);
467                         dt = (int) ((float) DotProduct (mid, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3]);
468
469                         if (ds < surf->texturemins[0] || dt < surf->texturemins[1])
470                                 continue;
471                         
472                         ds -= surf->texturemins[0];
473                         dt -= surf->texturemins[1];
474                         
475                         if (ds > surf->extents[0] || dt > surf->extents[1])
476                                 continue;
477
478                         if (surf->samples)
479                         {
480                                 byte *lightmap;
481                                 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;
482                                 float scale;
483                                 line3 = ((surf->extents[0]>>4)+1)*3;
484
485                                 lightmap = surf->samples + ((dt>>4) * ((surf->extents[0]>>4)+1) + (ds>>4))*3; // LordHavoc: *3 for color
486
487                                 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
488                                 {
489                                         scale = (float) d_lightstylevalue[surf->styles[maps]] * 1.0 / 256.0;
490                                         r00 += (float) lightmap[      0] * scale;g00 += (float) lightmap[      1] * scale;b00 += (float) lightmap[2] * scale;
491                                         r01 += (float) lightmap[      3] * scale;g01 += (float) lightmap[      4] * scale;b01 += (float) lightmap[5] * scale;
492                                         r10 += (float) lightmap[line3+0] * scale;g10 += (float) lightmap[line3+1] * scale;b10 += (float) lightmap[line3+2] * scale;
493                                         r11 += (float) lightmap[line3+3] * scale;g11 += (float) lightmap[line3+4] * scale;b11 += (float) lightmap[line3+5] * scale;
494                                         lightmap += ((surf->extents[0]>>4)+1) * ((surf->extents[1]>>4)+1)*3; // LordHavoc: *3 for colored lighting
495                                 }
496
497                                 color[0] += (float) ((int) ((((((((r11-r10) * dsfrac) >> 4) + r10)-((((r01-r00) * dsfrac) >> 4) + r00)) * dtfrac) >> 4) + ((((r01-r00) * dsfrac) >> 4) + r00)));
498                                 color[1] += (float) ((int) ((((((((g11-g10) * dsfrac) >> 4) + g10)-((((g01-g00) * dsfrac) >> 4) + g00)) * dtfrac) >> 4) + ((((g01-g00) * dsfrac) >> 4) + g00)));
499                                 color[2] += (float) ((int) ((((((((b11-b10) * dsfrac) >> 4) + b10)-((((b01-b00) * dsfrac) >> 4) + b00)) * dtfrac) >> 4) + ((((b01-b00) * dsfrac) >> 4) + b00)));
500                         }
501                         return true; // success
502                 }
503
504         // go down back side
505                 return RecursiveLightPoint (color, node->children[front >= 0], mid, end);
506         }
507 }
508
509 void R_LightPoint (vec3_t color, vec3_t p)
510 {
511         vec3_t          end;
512         
513         if (r_fullbright.value || !cl.worldmodel->lightdata)
514         {
515                 color[0] = color[1] = color[2] = 255;
516                 return;
517         }
518
519         end[0] = p[0];
520         end[1] = p[1];
521         end[2] = p[2] - 2048;
522
523         color[0] = color[1] = color[2] = r_ambient.value * 2.0f;
524         RecursiveLightPoint (color, cl.worldmodel->nodes, p, end);
525 }
526
527 void SV_LightPoint (vec3_t color, vec3_t p)
528 {
529         vec3_t          end;
530         
531         if (!sv.worldmodel->lightdata)
532         {
533                 color[0] = color[1] = color[2] = 255;
534                 return;
535         }
536         
537         end[0] = p[0];
538         end[1] = p[1];
539         end[2] = p[2] - 2048;
540
541         color[0] = color[1] = color[2] = 0;
542         RecursiveLightPoint (color, sv.worldmodel->nodes, p, end);
543 }
544 */
545
546 int RecursiveLightPoint (vec3_t color, mnode_t *node, float x, float y, float startz, float endz)
547 {
548         int             side, distz = endz - startz;
549         float   front, back;
550         float   mid;
551
552 loc0:
553         if (node->contents < 0)
554                 return false;           // didn't hit anything
555
556         switch (node->plane->type)
557         {
558         case PLANE_X:
559                 node = node->children[x < node->plane->dist];
560                 goto loc0;
561         case PLANE_Y:
562                 node = node->children[y < node->plane->dist];
563                 goto loc0;
564         case PLANE_Z:
565                 side = startz < node->plane->dist;
566                 if ((endz < node->plane->dist) == side)
567                 {
568                         node = node->children[side];
569                         goto loc0;
570                 }
571                 // found an intersection
572 //              mid = startz + (endz - startz) * (startz - node->plane->dist) / (startz - endz);
573 //              mid = startz + distz * (startz - node->plane->dist) / (-distz);
574 //              mid = startz + (-(startz - node->plane->dist));
575 //              mid = startz - (startz - node->plane->dist);
576 //              mid = startz + node->plane->dist - startz;
577                 mid = node->plane->dist;
578                 break;
579         default:
580                 back = front = x * node->plane->normal[0] + y * node->plane->normal[1];
581                 front += startz * node->plane->normal[2];
582                 back += endz * node->plane->normal[2];
583                 side = front < node->plane->dist;
584                 if ((back < node->plane->dist) == side)
585                 {
586                         node = node->children[side];
587                         goto loc0;
588                 }
589                 // found an intersection
590 //              mid = startz + (endz - startz) * ((front - node->plane->dist) / ((front - node->plane->dist) - (back - node->plane->dist)));
591 //              mid = startz + (endz - startz) * ((front - node->plane->dist) / (front - back));
592                 mid = startz + distz * (front - node->plane->dist) / (front - back);
593                 break;
594         }
595         
596         // go down front side
597         if (node->children[side]->contents >= 0 && RecursiveLightPoint (color, node->children[side], x, y, startz, mid))
598                 return true;    // hit something
599         else
600         {
601                 // check for impact on this node
602                 if (node->numsurfaces)
603                 {
604                         int i, ds, dt;
605                         msurface_t *surf;
606                         lightspot[0] = x;
607                         lightspot[1] = y;
608                         lightspot[2] = mid;
609                         lightplane = node->plane;
610
611                         surf = cl.worldmodel->surfaces + node->firstsurface;
612                         for (i = 0;i < node->numsurfaces;i++, surf++)
613                         {
614                                 if (surf->flags & SURF_DRAWTILED)
615                                         continue;       // no lightmaps
616
617                                 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]);
618                                 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]);
619
620                                 if (ds < surf->texturemins[0] || dt < surf->texturemins[1])
621                                         continue;
622                                 
623                                 ds -= surf->texturemins[0];
624                                 dt -= surf->texturemins[1];
625                                 
626                                 if (ds > surf->extents[0] || dt > surf->extents[1])
627                                         continue;
628
629                                 if (surf->samples)
630                                 {
631                                         byte *lightmap;
632                                         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;
633                                         line3 = ((surf->extents[0]>>4)+1)*3;
634                                         size3 = ((surf->extents[0]>>4)+1) * ((surf->extents[1]>>4)+1)*3; // LordHavoc: *3 for colored lighting
635
636                                         lightmap = surf->samples + ((dt>>4) * ((surf->extents[0]>>4)+1) + (ds>>4))*3; // LordHavoc: *3 for color
637
638                                         for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
639                                         {
640                                                 scale = d_lightstylevalue[surf->styles[maps]];
641                                                 r00 += lightmap[      0] * scale;g00 += lightmap[      1] * scale;b00 += lightmap[      2] * scale;
642                                                 r01 += lightmap[      3] * scale;g01 += lightmap[      4] * scale;b01 += lightmap[      5] * scale;
643                                                 r10 += lightmap[line3+0] * scale;g10 += lightmap[line3+1] * scale;b10 += lightmap[line3+2] * scale;
644                                                 r11 += lightmap[line3+3] * scale;g11 += lightmap[line3+4] * scale;b11 += lightmap[line3+5] * scale;
645                                                 lightmap += size3;
646                                         }
647
648                                         color[0] += (float) ((((((((r11-r10) * dsfrac) >> 4) + r10)-((((r01-r00) * dsfrac) >> 4) + r00)) * dtfrac) >> 4) + ((((r01-r00) * dsfrac) >> 4) + r00)) * (1.0f / 256.0f);
649                                         color[1] += (float) ((((((((g11-g10) * dsfrac) >> 4) + g10)-((((g01-g00) * dsfrac) >> 4) + g00)) * dtfrac) >> 4) + ((((g01-g00) * dsfrac) >> 4) + g00)) * (1.0f / 256.0f);
650                                         color[2] += (float) ((((((((b11-b10) * dsfrac) >> 4) + b10)-((((b01-b00) * dsfrac) >> 4) + b00)) * dtfrac) >> 4) + ((((b01-b00) * dsfrac) >> 4) + b00)) * (1.0f / 256.0f);
651                                 }
652                                 return true; // success
653                         }
654                 }
655
656                 // go down back side
657                 node = node->children[side ^ 1];
658                 startz = mid;
659                 distz = endz - startz;
660                 goto loc0;
661 //              return RecursiveLightPoint (color, node->children[side ^ 1], x, y, mid, endz);
662         }
663 }
664
665 void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits)
666 {
667         int             i, j, k;
668         vec3_t  dist;
669         float   brightness, r, f;
670
671         if (!r_dynamic.value || (!dlightbits[0] && !dlightbits[1] && !dlightbits[2] && !dlightbits[3] && !dlightbits[4] && !dlightbits[5] && !dlightbits[6] && !dlightbits[7]))
672                 return;
673
674         for (j = 0;j < (MAX_DLIGHTS >> 5);j++)
675         {
676                 if (dlightbits[j])
677                 {
678                         for (i=0 ; i<32 ; i++)
679                         {
680                                 if (!((1 << i) & dlightbits[j]))
681                                         continue;
682                                 k = (j<<5)+i;
683                                 if (!cl_dlights[k].radius)
684                                         continue;
685                                 VectorSubtract (org, cl_dlights[k].origin, dist);
686                                 f = DotProduct(dist, dist) + LIGHTOFFSET;
687                                 r = cl_dlights[k].radius*cl_dlights[k].radius;
688                                 if (f < r)
689                                 {
690                                         brightness = r * 128.0f / f;
691                                         color[0] += brightness * cl_dlights[k].color[0];
692                                         color[1] += brightness * cl_dlights[k].color[1];
693                                         color[2] += brightness * cl_dlights[k].color[2];
694                                 }
695                         }
696                 }
697         }
698 }
699
700 void R_CompleteLightPoint (vec3_t color, vec3_t p, int dynamic)
701 {
702         mleaf_t *leaf;
703         leaf = Mod_PointInLeaf(p, cl.worldmodel);
704         if (leaf->contents == CONTENTS_SOLID)
705         {
706                 color[0] = color[1] = color[2] = 0;
707                 return;
708         }
709
710         if (r_fullbright.value || !cl.worldmodel->lightdata)
711         {
712                 color[0] = color[1] = color[2] = 255;
713                 return;
714         }
715         
716         color[0] = color[1] = color[2] = r_ambient.value * 2.0f;
717         RecursiveLightPoint (color, cl.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
718
719         if (dynamic)
720                 R_DynamicLightPoint(color, p, leaf->dlightbits);
721 }
722
723 void R_ModelLightPoint (vec3_t color, vec3_t p, int *dlightbits)
724 {
725         mleaf_t *leaf;
726         leaf = Mod_PointInLeaf(p, cl.worldmodel);
727         if (leaf->contents == CONTENTS_SOLID)
728         {
729                 color[0] = color[1] = color[2] = 0;
730                 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
731                 return;
732         }
733
734         if (r_fullbright.value || !cl.worldmodel->lightdata)
735         {
736                 color[0] = color[1] = color[2] = 255;
737                 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
738                 return;
739         }
740         
741         color[0] = color[1] = color[2] = r_ambient.value * 2.0f;
742         RecursiveLightPoint (color, cl.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
743
744         if (leaf->dlightframe == r_framecount)
745         {
746                 dlightbits[0] = leaf->dlightbits[0];
747                 dlightbits[1] = leaf->dlightbits[1];
748                 dlightbits[2] = leaf->dlightbits[2];
749                 dlightbits[3] = leaf->dlightbits[3];
750                 dlightbits[4] = leaf->dlightbits[4];
751                 dlightbits[5] = leaf->dlightbits[5];
752                 dlightbits[6] = leaf->dlightbits[6];
753                 dlightbits[7] = leaf->dlightbits[7];
754         }
755         else
756                 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
757 }
758
759 /* // not currently used
760 void R_DynamicLightPointNoMask(vec3_t color, vec3_t org)
761 {
762         int             i;
763         vec3_t  dist;
764         float   brightness, r, f;
765
766         if (!r_dynamic.value)
767                 return;
768
769         for (i=0 ; i<MAX_DLIGHTS ; i++)
770         {
771                 if (!cl_dlights[i].radius)
772                         continue;
773                 VectorSubtract (org, cl_dlights[i].origin, dist);
774                 f = DotProduct(dist, dist) + LIGHTOFFSET;
775                 r = cl_dlights[i].radius*cl_dlights[i].radius;
776                 if (f < r)
777                 {
778                         brightness = r * 256.0f / f;
779                         if (cl_dlights[i].dark)
780                                 brightness = -brightness;
781                         color[0] += brightness * cl_dlights[i].color[0];
782                         color[1] += brightness * cl_dlights[i].color[1];
783                         color[2] += brightness * cl_dlights[i].color[2];
784                 }
785         }
786 }
787 */
788
789 void R_LightModel(entity_t *ent, int numverts, vec3_t center, vec3_t basecolor)
790 {
791         // LordHavoc: warning: reliance on int being 4 bytes here (of course the d_8to24table relies on that too...)
792         int i, j, nearlights = 0, color;
793         vec3_t dist, mod;
794         float t, t1, t2, t3, *avn;
795         byte r,g,b,a, *avc;
796         struct
797         {
798                 vec3_t color;
799                 vec3_t origin;
800         } nearlight[MAX_DLIGHTS];
801         int modeldlightbits[8];
802         avc = aliasvertcolor;
803         avn = aliasvertnorm;
804         a = (byte) bound((int) 0, (int) (modelalpha * 255.0f), (int) 255);
805         if (lighthalf)
806         {
807                 mod[0] = ent->render.colormod[0] * 0.5f;
808                 mod[1] = ent->render.colormod[1] * 0.5f;
809                 mod[2] = ent->render.colormod[2] * 0.5f;
810         }
811         else
812         {
813                 mod[0] = ent->render.colormod[0];
814                 mod[1] = ent->render.colormod[1];
815                 mod[2] = ent->render.colormod[2];
816         }
817         if (ent->render.effects & EF_FULLBRIGHT)
818         {
819                 ((byte *)&color)[0] = (byte) (255.0f * mod[0]);
820                 ((byte *)&color)[1] = (byte) (255.0f * mod[1]);
821                 ((byte *)&color)[2] = (byte) (255.0f * mod[2]);
822                 ((byte *)&color)[3] = a;
823                 for (i = 0;i < numverts;i++)
824                 {
825                         *((int *)avc) = color;
826                         avc += 4;
827                 }
828                 return;
829         }
830         R_ModelLightPoint(basecolor, center, modeldlightbits);
831
832         basecolor[0] *= mod[0];
833         basecolor[1] *= mod[1];
834         basecolor[2] *= mod[2];
835         for (i = 0;i < MAX_DLIGHTS;i++)
836         {
837                 if (!modeldlightbits[i >> 5])
838                 {
839                         i |= 31;
840                         continue;
841                 }
842                 if (!(modeldlightbits[i >> 5] & (1 << (i & 31))))
843                         continue;
844                 VectorSubtract (center, cl_dlights[i].origin, dist);
845                 t2 = DotProduct(dist,dist) + LIGHTOFFSET;
846                 t1 = cl_dlights[i].radius*cl_dlights[i].radius;
847                 if (t2 < t1)
848                 {
849                         // transform the light into the model's coordinate system
850                         if (gl_transform.value)
851                                 softwareuntransform(cl_dlights[i].origin, nearlight[nearlights].origin);
852                         else
853                         {
854                                 VectorCopy(cl_dlights[i].origin, nearlight[nearlights].origin);
855                         }
856                         nearlight[nearlights].color[0] = cl_dlights[i].color[0] * t1 * mod[0];
857                         nearlight[nearlights].color[1] = cl_dlights[i].color[1] * t1 * mod[1];
858                         nearlight[nearlights].color[2] = cl_dlights[i].color[2] * t1 * mod[2];
859                         if (r_lightmodels.value && (ent == NULL || ent != cl_dlights[i].ent))
860                                 nearlights++;
861                         else
862                         {
863                                 t1 = 1.0f / t2;
864                                 basecolor[0] += nearlight[nearlights].color[0] * t1;
865                                 basecolor[1] += nearlight[nearlights].color[1] * t1;
866                                 basecolor[2] += nearlight[nearlights].color[2] * t1;
867                         }
868                 }
869         }
870         t1 = bound(0, basecolor[0], 255);r = (byte) t1;
871         t1 = bound(0, basecolor[1], 255);g = (byte) t1;
872         t1 = bound(0, basecolor[2], 255);b = (byte) t1;
873         ((byte *)&color)[0] = r;
874         ((byte *)&color)[1] = g;
875         ((byte *)&color)[2] = b;
876         ((byte *)&color)[3] = a;
877         if (nearlights)
878         {
879                 int temp;
880                 vec3_t v;
881                 float *av;
882                 av = aliasvert;
883                 if (nearlights == 1)
884                 {
885                         for (i = 0;i < numverts;i++)
886                         {
887                                 VectorSubtract(nearlight[0].origin, av, v);
888                                 t = DotProduct(avn,v);
889                                 if (t > 0)
890                                 {
891                                         t /= (DotProduct(v,v) + LIGHTOFFSET);
892                                         temp = (int) ((float) (basecolor[0] + nearlight[0].color[0] * t));
893                                         avc[0] = bound(0, temp, 255);
894                                         temp = (int) ((float) (basecolor[1] + nearlight[0].color[1] * t));
895                                         avc[1] = bound(0, temp, 255);
896                                         temp = (int) ((float) (basecolor[2] + nearlight[0].color[2] * t));
897                                         avc[2] = bound(0, temp, 255);
898                                         avc[3] = a;
899                                 }
900                                 else
901                                         *((int *)avc) = color;
902                                 avc += 4;
903                                 av += 3;
904                                 avn += 3;
905                         }
906                 }
907                 else
908                 {
909                         for (i = 0;i < numverts;i++)
910                         {
911                                 int lit;
912                                 t1 = basecolor[0];
913                                 t2 = basecolor[1];
914                                 t3 = basecolor[2];
915                                 lit = false;
916                                 for (j = 0;j < nearlights;j++)
917                                 {
918                                         VectorSubtract(nearlight[j].origin, av, v);
919                                         t = DotProduct(avn,v);
920                                         if (t > 0)
921                                         {
922                                                 t /= (DotProduct(v,v) + LIGHTOFFSET);
923                                                 t1 += nearlight[j].color[0] * t;
924                                                 t2 += nearlight[j].color[1] * t;
925                                                 t3 += nearlight[j].color[2] * t;
926                                                 lit = true;
927                                         }
928                                 }
929                                 if (lit)
930                                 {
931                                         int i1, i2, i3;
932                                         i1 = (int) t1;
933                                         avc[0] = bound(0, i1, 255);
934                                         i2 = (int) t2;
935                                         avc[1] = bound(0, i2, 255);
936                                         i3 = (int) t3;
937                                         avc[2] = bound(0, i3, 255);
938                                         avc[3] = a;
939                                 }
940                                 else // dodge the costly float -> int conversions
941                                         *((int *)avc) = color;
942                                 avc += 4;
943                                 av += 3;
944                                 avn += 3;
945                         }
946                 }
947         }
948         else
949         {
950                 for (i = 0;i < numverts;i++)
951                 {
952                         *((int *)avc) = color;
953                         avc += 4;
954                 }
955         }
956 }