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