]> icculus.org git repositories - divverent/darkplaces.git/blob - r_light.c
changed SV_Move calls to use MOVE_NORMAL or MOVE_NOMONSTERS instead of false/true...
[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 = {0, "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 void R_VisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model)
238 {
239         static int lightframe = 0;
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, m, c, leafnum;
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 * 3;
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                                 for (i = 0;i < 8;i++)
280                                 {
281                                         if (c & (1<<i))
282                                         {
283                                                 leafnum = (k << 3)+i+1;
284                                                 if (leafnum > model->numleafs)
285                                                         return;
286                                                 leaf = &model->leafs[leafnum];
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 not visible in current frame, or already marked because it was in another leaf we passed, skip
309                                                                 if (surf->lightframe == lightframe)
310                                                                         continue;
311                                                                 surf->lightframe = lightframe;
312                                                                 if (surf->visframe != r_framecount)
313                                                                         continue;
314                                                                 dist = PlaneDiff(lightorigin, surf->plane);
315                                                                 if (surf->flags & SURF_PLANEBACK)
316                                                                         dist = -dist;
317                                                                 // LordHavoc: make sure it is infront of the surface and not too far away
318                                                                 if (dist < radius && (dist > -0.25f || ((surf->flags & SURF_LIGHTBOTHSIDES) && dist > -radius)))
319                                                                 {
320                                                                         int d;
321                                                                         float dist2, impact[3];
322
323                                                                         dist2 = dist * dist;
324
325                                                                         impact[0] = light->origin[0] - surf->plane->normal[0] * dist;
326                                                                         impact[1] = light->origin[1] - surf->plane->normal[1] * dist;
327                                                                         impact[2] = light->origin[2] - surf->plane->normal[2] * dist;
328
329                                                                         d = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
330
331                                                                         if (d < 0)
332                                                                         {
333                                                                                 dist2 += d * d;
334                                                                                 if (dist2 >= maxdist)
335                                                                                         continue;
336                                                                         }
337                                                                         else
338                                                                         {
339                                                                                 d -= surf->extents[0] + 16;
340                                                                                 if (d > 0)
341                                                                                 {
342                                                                                         dist2 += d * d;
343                                                                                         if (dist2 >= maxdist)
344                                                                                                 continue;
345                                                                                 }
346                                                                         }
347
348                                                                         d = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
349
350                                                                         if (d < 0)
351                                                                         {
352                                                                                 dist2 += d * d;
353                                                                                 if (dist2 >= maxdist)
354                                                                                         continue;
355                                                                         }
356                                                                         else
357                                                                         {
358                                                                                 d -= surf->extents[1] + 16;
359                                                                                 if (d > 0)
360                                                                                 {
361                                                                                         dist2 += d * d;
362                                                                                         if (dist2 >= maxdist)
363                                                                                                 continue;
364                                                                                 }
365                                                                         }
366
367                                                                         if (surf->dlightframe != r_framecount) // not dynamic until now
368                                                                         {
369                                                                                 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;
370                                                                                 surf->dlightframe = r_framecount;
371                                                                         }
372                                                                         surf->dlightbits[bitindex] |= bit;
373                                                                 }
374                                                         }
375                                                         while (--m);
376                                                 }
377                                         }
378                                 }
379                                 k++;
380                                 continue;
381                         }
382                 
383                         k += *in++;
384                 }
385         }
386 }
387
388
389 /*
390 =============
391 R_PushDlights
392 =============
393 */
394 void R_PushDlights (void)
395 {
396         int             i;
397         dlight_t        *l;
398
399         if (!r_dynamic.value)
400                 return;
401
402         l = cl_dlights;
403
404         for (i=0 ; i<MAX_DLIGHTS ; i++, l++)
405         {
406                 if (!l->radius)
407                         continue;
408 //              R_MarkLights (l->origin, l, 1<<(i&31), i >> 5, cl.worldmodel->nodes );
409                 R_VisMarkLights (l->origin, l, 1<<(i&31), i >> 5, cl.worldmodel);
410         }
411
412
413 }
414
415
416 /*
417 =============================================================================
418
419 LIGHT SAMPLING
420
421 =============================================================================
422 */
423
424 mplane_t                *lightplane;
425 vec3_t                  lightspot;
426
427 /*
428 int RecursiveLightPoint (vec3_t color, mnode_t *node, vec3_t start, vec3_t end)
429 {
430         float           front, back, frac;
431         vec3_t          mid;
432
433 loc0:
434         if (node->contents < 0)
435                 return false;           // didn't hit anything
436         
437 // calculate mid point
438         front = PlaneDiff (start, node->plane);
439         back = PlaneDiff (end, node->plane);
440
441         // LordHavoc: optimized recursion
442         if ((back < 0) == (front < 0))
443 //              return RecursiveLightPoint (color, node->children[front < 0], start, end);
444         {
445                 node = node->children[front < 0];
446                 goto loc0;
447         }
448         
449         frac = front / (front-back);
450         mid[0] = start[0] + (end[0] - start[0])*frac;
451         mid[1] = start[1] + (end[1] - start[1])*frac;
452         mid[2] = start[2] + (end[2] - start[2])*frac;
453         
454 // go down front side
455         if (RecursiveLightPoint (color, node->children[front < 0], start, mid))
456                 return true;    // hit something
457         else
458         {
459                 int i, ds, dt;
460                 msurface_t *surf;
461         // check for impact on this node
462                 VectorCopy (mid, lightspot);
463                 lightplane = node->plane;
464
465                 surf = cl.worldmodel->surfaces + node->firstsurface;
466                 for (i = 0;i < node->numsurfaces;i++, surf++)
467                 {
468                         if (surf->flags & SURF_DRAWTILED)
469                                 continue;       // no lightmaps
470
471                         ds = (int) ((float) DotProduct (mid, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3]);
472                         dt = (int) ((float) DotProduct (mid, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3]);
473
474                         if (ds < surf->texturemins[0] || dt < surf->texturemins[1])
475                                 continue;
476                         
477                         ds -= surf->texturemins[0];
478                         dt -= surf->texturemins[1];
479                         
480                         if (ds > surf->extents[0] || dt > surf->extents[1])
481                                 continue;
482
483                         if (surf->samples)
484                         {
485                                 byte *lightmap;
486                                 int maps, line3, dsfrac = ds & 15, dtfrac = dt & 15, r00 = 0, g00 = 0, b00 = 0, r01 = 0, g01 = 0, b01 = 0, r10 = 0, g10 = 0, b10 = 0, r11 = 0, g11 = 0, b11 = 0;
487                                 float scale;
488                                 line3 = ((surf->extents[0]>>4)+1)*3;
489
490                                 lightmap = surf->samples + ((dt>>4) * ((surf->extents[0]>>4)+1) + (ds>>4))*3; // LordHavoc: *3 for color
491
492                                 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
493                                 {
494                                         scale = (float) d_lightstylevalue[surf->styles[maps]] * 1.0 / 256.0;
495                                         r00 += (float) lightmap[      0] * scale;g00 += (float) lightmap[      1] * scale;b00 += (float) lightmap[2] * scale;
496                                         r01 += (float) lightmap[      3] * scale;g01 += (float) lightmap[      4] * scale;b01 += (float) lightmap[5] * scale;
497                                         r10 += (float) lightmap[line3+0] * scale;g10 += (float) lightmap[line3+1] * scale;b10 += (float) lightmap[line3+2] * scale;
498                                         r11 += (float) lightmap[line3+3] * scale;g11 += (float) lightmap[line3+4] * scale;b11 += (float) lightmap[line3+5] * scale;
499                                         lightmap += ((surf->extents[0]>>4)+1) * ((surf->extents[1]>>4)+1)*3; // LordHavoc: *3 for colored lighting
500                                 }
501
502                                 color[0] += (float) ((int) ((((((((r11-r10) * dsfrac) >> 4) + r10)-((((r01-r00) * dsfrac) >> 4) + r00)) * dtfrac) >> 4) + ((((r01-r00) * dsfrac) >> 4) + r00)));
503                                 color[1] += (float) ((int) ((((((((g11-g10) * dsfrac) >> 4) + g10)-((((g01-g00) * dsfrac) >> 4) + g00)) * dtfrac) >> 4) + ((((g01-g00) * dsfrac) >> 4) + g00)));
504                                 color[2] += (float) ((int) ((((((((b11-b10) * dsfrac) >> 4) + b10)-((((b01-b00) * dsfrac) >> 4) + b00)) * dtfrac) >> 4) + ((((b01-b00) * dsfrac) >> 4) + b00)));
505                         }
506                         return true; // success
507                 }
508
509         // go down back side
510                 return RecursiveLightPoint (color, node->children[front >= 0], mid, end);
511         }
512 }
513
514 void R_LightPoint (vec3_t color, vec3_t p)
515 {
516         vec3_t          end;
517         
518         if (r_fullbright.value || !cl.worldmodel->lightdata)
519         {
520                 color[0] = color[1] = color[2] = 255;
521                 return;
522         }
523
524         end[0] = p[0];
525         end[1] = p[1];
526         end[2] = p[2] - 2048;
527
528         color[0] = color[1] = color[2] = r_ambient.value * 2.0f;
529         RecursiveLightPoint (color, cl.worldmodel->nodes, p, end);
530 }
531
532 void SV_LightPoint (vec3_t color, vec3_t p)
533 {
534         vec3_t          end;
535         
536         if (!sv.worldmodel->lightdata)
537         {
538                 color[0] = color[1] = color[2] = 255;
539                 return;
540         }
541         
542         end[0] = p[0];
543         end[1] = p[1];
544         end[2] = p[2] - 2048;
545
546         color[0] = color[1] = color[2] = 0;
547         RecursiveLightPoint (color, sv.worldmodel->nodes, p, end);
548 }
549 */
550
551 int RecursiveLightPoint (vec3_t color, mnode_t *node, float x, float y, float startz, float endz)
552 {
553         int             side, distz = endz - startz;
554         float   front, back;
555         float   mid;
556
557 loc0:
558         if (node->contents < 0)
559                 return false;           // didn't hit anything
560
561         switch (node->plane->type)
562         {
563         case PLANE_X:
564                 node = node->children[x < node->plane->dist];
565                 goto loc0;
566         case PLANE_Y:
567                 node = node->children[y < node->plane->dist];
568                 goto loc0;
569         case PLANE_Z:
570                 side = startz < node->plane->dist;
571                 if ((endz < node->plane->dist) == side)
572                 {
573                         node = node->children[side];
574                         goto loc0;
575                 }
576                 // found an intersection
577 //              mid = startz + (endz - startz) * (startz - node->plane->dist) / (startz - endz);
578 //              mid = startz + distz * (startz - node->plane->dist) / (-distz);
579 //              mid = startz + (-(startz - node->plane->dist));
580 //              mid = startz - (startz - node->plane->dist);
581 //              mid = startz + node->plane->dist - startz;
582                 mid = node->plane->dist;
583                 break;
584         default:
585                 back = front = x * node->plane->normal[0] + y * node->plane->normal[1];
586                 front += startz * node->plane->normal[2];
587                 back += endz * node->plane->normal[2];
588                 side = front < node->plane->dist;
589                 if ((back < node->plane->dist) == side)
590                 {
591                         node = node->children[side];
592                         goto loc0;
593                 }
594                 // found an intersection
595 //              mid = startz + (endz - startz) * ((front - node->plane->dist) / ((front - node->plane->dist) - (back - node->plane->dist)));
596 //              mid = startz + (endz - startz) * ((front - node->plane->dist) / (front - back));
597                 mid = startz + distz * (front - node->plane->dist) / (front - back);
598                 break;
599         }
600         
601         // go down front side
602         if (node->children[side]->contents >= 0 && RecursiveLightPoint (color, node->children[side], x, y, startz, mid))
603                 return true;    // hit something
604         else
605         {
606                 // check for impact on this node
607                 if (node->numsurfaces)
608                 {
609                         int i, ds, dt;
610                         msurface_t *surf;
611                         lightspot[0] = x;
612                         lightspot[1] = y;
613                         lightspot[2] = mid;
614                         lightplane = node->plane;
615
616                         surf = cl.worldmodel->surfaces + node->firstsurface;
617                         for (i = 0;i < node->numsurfaces;i++, surf++)
618                         {
619                                 if (surf->flags & SURF_DRAWTILED)
620                                         continue;       // no lightmaps
621
622                                 ds = (int) (x * surf->texinfo->vecs[0][0] + y * surf->texinfo->vecs[0][1] + mid * surf->texinfo->vecs[0][2] + surf->texinfo->vecs[0][3]);
623                                 dt = (int) (x * surf->texinfo->vecs[1][0] + y * surf->texinfo->vecs[1][1] + mid * surf->texinfo->vecs[1][2] + surf->texinfo->vecs[1][3]);
624
625                                 if (ds < surf->texturemins[0] || dt < surf->texturemins[1])
626                                         continue;
627                                 
628                                 ds -= surf->texturemins[0];
629                                 dt -= surf->texturemins[1];
630                                 
631                                 if (ds > surf->extents[0] || dt > surf->extents[1])
632                                         continue;
633
634                                 if (surf->samples)
635                                 {
636                                         byte *lightmap;
637                                         int maps, line3, size3, dsfrac = ds & 15, dtfrac = dt & 15, scale = 0, r00 = 0, g00 = 0, b00 = 0, r01 = 0, g01 = 0, b01 = 0, r10 = 0, g10 = 0, b10 = 0, r11 = 0, g11 = 0, b11 = 0;
638                                         line3 = ((surf->extents[0]>>4)+1)*3;
639                                         size3 = ((surf->extents[0]>>4)+1) * ((surf->extents[1]>>4)+1)*3; // LordHavoc: *3 for colored lighting
640
641                                         lightmap = surf->samples + ((dt>>4) * ((surf->extents[0]>>4)+1) + (ds>>4))*3; // LordHavoc: *3 for color
642
643                                         for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
644                                         {
645                                                 scale = d_lightstylevalue[surf->styles[maps]];
646                                                 r00 += lightmap[      0] * scale;g00 += lightmap[      1] * scale;b00 += lightmap[      2] * scale;
647                                                 r01 += lightmap[      3] * scale;g01 += lightmap[      4] * scale;b01 += lightmap[      5] * scale;
648                                                 r10 += lightmap[line3+0] * scale;g10 += lightmap[line3+1] * scale;b10 += lightmap[line3+2] * scale;
649                                                 r11 += lightmap[line3+3] * scale;g11 += lightmap[line3+4] * scale;b11 += lightmap[line3+5] * scale;
650                                                 lightmap += size3;
651                                         }
652
653                                         /*
654                                         // LordHavoc: here's the readable version of the interpolation
655                                         // code, not quite as easy for the compiler to optimize...
656
657                                         // dsfrac is the X position in the lightmap pixel, * 16
658                                         // dtfrac is the Y position in the lightmap pixel, * 16
659                                         // r00 is top left corner, r01 is top right corner
660                                         // r10 is bottom left corner, r11 is bottom right corner
661                                         // g and b are the same layout.
662                                         // r0 and r1 are the top and bottom intermediate results
663
664                                         // first we interpolate the top two points, to get the top
665                                         // edge sample
666                                         r0 = (((r01-r00) * dsfrac) >> 4) + r00;
667                                         g0 = (((g01-g00) * dsfrac) >> 4) + g00;
668                                         b0 = (((b01-b00) * dsfrac) >> 4) + b00;
669                                         // then we interpolate the bottom two points, to get the
670                                         // bottom edge sample
671                                         r1 = (((r11-r10) * dsfrac) >> 4) + r10;
672                                         g1 = (((g11-g10) * dsfrac) >> 4) + g10;
673                                         b1 = (((b11-b10) * dsfrac) >> 4) + b10;
674                                         // then we interpolate the top and bottom samples to get the
675                                         // middle sample (the one which was requested)
676                                         r = (((r1-r0) * dtfrac) >> 4) + r0;
677                                         g = (((g1-g0) * dtfrac) >> 4) + g0;
678                                         b = (((b1-b0) * dtfrac) >> 4) + b0;
679                                         */
680
681                                         color[0] += (float) ((((((((r11-r10) * dsfrac) >> 4) + r10)-((((r01-r00) * dsfrac) >> 4) + r00)) * dtfrac) >> 4) + ((((r01-r00) * dsfrac) >> 4) + r00)) * (1.0f / 256.0f);
682                                         color[1] += (float) ((((((((g11-g10) * dsfrac) >> 4) + g10)-((((g01-g00) * dsfrac) >> 4) + g00)) * dtfrac) >> 4) + ((((g01-g00) * dsfrac) >> 4) + g00)) * (1.0f / 256.0f);
683                                         color[2] += (float) ((((((((b11-b10) * dsfrac) >> 4) + b10)-((((b01-b00) * dsfrac) >> 4) + b00)) * dtfrac) >> 4) + ((((b01-b00) * dsfrac) >> 4) + b00)) * (1.0f / 256.0f);
684                                 }
685                                 return true; // success
686                         }
687                 }
688
689                 // go down back side
690                 node = node->children[side ^ 1];
691                 startz = mid;
692                 distz = endz - startz;
693                 goto loc0;
694 //              return RecursiveLightPoint (color, node->children[side ^ 1], x, y, mid, endz);
695         }
696 }
697
698 void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits)
699 {
700         int             i, j, k;
701         vec3_t  dist;
702         float   brightness, r, f;
703
704         if (!r_dynamic.value || (!dlightbits[0] && !dlightbits[1] && !dlightbits[2] && !dlightbits[3] && !dlightbits[4] && !dlightbits[5] && !dlightbits[6] && !dlightbits[7]))
705                 return;
706
707         for (j = 0;j < (MAX_DLIGHTS >> 5);j++)
708         {
709                 if (dlightbits[j])
710                 {
711                         for (i=0 ; i<32 ; i++)
712                         {
713                                 if (!((1 << i) & dlightbits[j]))
714                                         continue;
715                                 k = (j<<5)+i;
716                                 if (!cl_dlights[k].radius)
717                                         continue;
718                                 VectorSubtract (org, cl_dlights[k].origin, dist);
719                                 f = DotProduct(dist, dist) + LIGHTOFFSET;
720                                 r = cl_dlights[k].radius*cl_dlights[k].radius;
721                                 if (f < r)
722                                 {
723                                         brightness = r * 128.0f / f;
724                                         color[0] += brightness * cl_dlights[k].color[0];
725                                         color[1] += brightness * cl_dlights[k].color[1];
726                                         color[2] += brightness * cl_dlights[k].color[2];
727                                 }
728                         }
729                 }
730         }
731 }
732
733 void R_CompleteLightPoint (vec3_t color, vec3_t p, int dynamic)
734 {
735         mleaf_t *leaf;
736         leaf = Mod_PointInLeaf(p, cl.worldmodel);
737         if (leaf->contents == CONTENTS_SOLID)
738         {
739                 color[0] = color[1] = color[2] = 0;
740                 return;
741         }
742
743         if (r_fullbright.value || !cl.worldmodel->lightdata)
744         {
745                 color[0] = color[1] = color[2] = 255;
746                 return;
747         }
748         
749         color[0] = color[1] = color[2] = r_ambient.value * 2.0f;
750         RecursiveLightPoint (color, cl.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
751
752         if (dynamic)
753                 R_DynamicLightPoint(color, p, leaf->dlightbits);
754 }
755
756 void R_ModelLightPoint (vec3_t color, vec3_t p, int *dlightbits)
757 {
758         mleaf_t *leaf;
759         leaf = Mod_PointInLeaf(p, cl.worldmodel);
760         if (leaf->contents == CONTENTS_SOLID)
761         {
762                 color[0] = color[1] = color[2] = 0;
763                 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
764                 return;
765         }
766
767         if (r_fullbright.value || !cl.worldmodel->lightdata)
768         {
769                 color[0] = color[1] = color[2] = 255;
770                 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
771                 return;
772         }
773         
774         color[0] = color[1] = color[2] = r_ambient.value * 2.0f;
775         RecursiveLightPoint (color, cl.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
776
777         if (leaf->dlightframe == r_framecount)
778         {
779                 dlightbits[0] = leaf->dlightbits[0];
780                 dlightbits[1] = leaf->dlightbits[1];
781                 dlightbits[2] = leaf->dlightbits[2];
782                 dlightbits[3] = leaf->dlightbits[3];
783                 dlightbits[4] = leaf->dlightbits[4];
784                 dlightbits[5] = leaf->dlightbits[5];
785                 dlightbits[6] = leaf->dlightbits[6];
786                 dlightbits[7] = leaf->dlightbits[7];
787         }
788         else
789                 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
790 }
791
792 /* // not currently used
793 void R_DynamicLightPointNoMask(vec3_t color, vec3_t org)
794 {
795         int             i;
796         vec3_t  dist;
797         float   brightness, r, f;
798
799         if (!r_dynamic.value)
800                 return;
801
802         for (i=0 ; i<MAX_DLIGHTS ; i++)
803         {
804                 if (!cl_dlights[i].radius)
805                         continue;
806                 VectorSubtract (org, cl_dlights[i].origin, dist);
807                 f = DotProduct(dist, dist) + LIGHTOFFSET;
808                 r = cl_dlights[i].radius*cl_dlights[i].radius;
809                 if (f < r)
810                 {
811                         brightness = r * 256.0f / f;
812                         if (cl_dlights[i].dark)
813                                 brightness = -brightness;
814                         color[0] += brightness * cl_dlights[i].color[0];
815                         color[1] += brightness * cl_dlights[i].color[1];
816                         color[2] += brightness * cl_dlights[i].color[2];
817                 }
818         }
819 }
820 */
821
822 void R_LightModel(entity_t *ent, int numverts, vec3_t center, vec3_t basecolor)
823 {
824         // LordHavoc: warning: reliance on int being 4 bytes here (of course the d_8to24table relies on that too...)
825         int i, j, nearlights = 0, color;
826         vec3_t dist, mod;
827         float t, t1, t2, t3, *avn;
828         byte r,g,b,a, *avc;
829         struct
830         {
831                 vec3_t color;
832                 vec3_t origin;
833         } nearlight[MAX_DLIGHTS];
834         int modeldlightbits[8];
835         avc = aliasvertcolor;
836         avn = aliasvertnorm;
837         a = (byte) bound((int) 0, (int) (modelalpha * 255.0f), (int) 255);
838         if (lighthalf)
839         {
840                 mod[0] = ent->render.colormod[0] * 0.5f;
841                 mod[1] = ent->render.colormod[1] * 0.5f;
842                 mod[2] = ent->render.colormod[2] * 0.5f;
843         }
844         else
845         {
846                 mod[0] = ent->render.colormod[0];
847                 mod[1] = ent->render.colormod[1];
848                 mod[2] = ent->render.colormod[2];
849         }
850         if (ent->render.effects & EF_FULLBRIGHT)
851         {
852                 ((byte *)&color)[0] = (byte) (255.0f * mod[0]);
853                 ((byte *)&color)[1] = (byte) (255.0f * mod[1]);
854                 ((byte *)&color)[2] = (byte) (255.0f * mod[2]);
855                 ((byte *)&color)[3] = a;
856                 for (i = 0;i < numverts;i++)
857                 {
858                         *((int *)avc) = color;
859                         avc += 4;
860                 }
861                 return;
862         }
863         R_ModelLightPoint(basecolor, center, modeldlightbits);
864
865         basecolor[0] *= mod[0];
866         basecolor[1] *= mod[1];
867         basecolor[2] *= mod[2];
868         for (i = 0;i < MAX_DLIGHTS;i++)
869         {
870                 if (!modeldlightbits[i >> 5])
871                 {
872                         i |= 31;
873                         continue;
874                 }
875                 if (!(modeldlightbits[i >> 5] & (1 << (i & 31))))
876                         continue;
877                 VectorSubtract (center, cl_dlights[i].origin, dist);
878                 t2 = DotProduct(dist,dist) + LIGHTOFFSET;
879                 t1 = cl_dlights[i].radius*cl_dlights[i].radius;
880                 if (t2 < t1)
881                 {
882                         // transform the light into the model's coordinate system
883                         if (gl_transform.value)
884                                 softwareuntransform(cl_dlights[i].origin, nearlight[nearlights].origin);
885                         else
886                         {
887                                 VectorCopy(cl_dlights[i].origin, nearlight[nearlights].origin);
888                         }
889                         nearlight[nearlights].color[0] = cl_dlights[i].color[0] * t1 * mod[0];
890                         nearlight[nearlights].color[1] = cl_dlights[i].color[1] * t1 * mod[1];
891                         nearlight[nearlights].color[2] = cl_dlights[i].color[2] * t1 * mod[2];
892                         if (r_lightmodels.value && (ent == NULL || ent != cl_dlights[i].ent))
893                                 nearlights++;
894                         else
895                         {
896                                 t1 = 1.0f / t2;
897                                 basecolor[0] += nearlight[nearlights].color[0] * t1;
898                                 basecolor[1] += nearlight[nearlights].color[1] * t1;
899                                 basecolor[2] += nearlight[nearlights].color[2] * t1;
900                         }
901                 }
902         }
903         t1 = bound(0, basecolor[0], 255);r = (byte) t1;
904         t1 = bound(0, basecolor[1], 255);g = (byte) t1;
905         t1 = bound(0, basecolor[2], 255);b = (byte) t1;
906         ((byte *)&color)[0] = r;
907         ((byte *)&color)[1] = g;
908         ((byte *)&color)[2] = b;
909         ((byte *)&color)[3] = a;
910         if (nearlights)
911         {
912                 int temp;
913                 vec3_t v;
914                 float *av;
915                 av = aliasvert;
916                 if (nearlights == 1)
917                 {
918                         for (i = 0;i < numverts;i++)
919                         {
920                                 VectorSubtract(nearlight[0].origin, av, v);
921                                 t = DotProduct(avn,v);
922                                 if (t > 0)
923                                 {
924                                         t /= (DotProduct(v,v) + LIGHTOFFSET);
925                                         temp = (int) ((float) (basecolor[0] + nearlight[0].color[0] * t));
926                                         avc[0] = bound(0, temp, 255);
927                                         temp = (int) ((float) (basecolor[1] + nearlight[0].color[1] * t));
928                                         avc[1] = bound(0, temp, 255);
929                                         temp = (int) ((float) (basecolor[2] + nearlight[0].color[2] * t));
930                                         avc[2] = bound(0, temp, 255);
931                                         avc[3] = a;
932                                 }
933                                 else
934                                         *((int *)avc) = color;
935                                 avc += 4;
936                                 av += 3;
937                                 avn += 3;
938                         }
939                 }
940                 else
941                 {
942                         for (i = 0;i < numverts;i++)
943                         {
944                                 int lit;
945                                 t1 = basecolor[0];
946                                 t2 = basecolor[1];
947                                 t3 = basecolor[2];
948                                 lit = false;
949                                 for (j = 0;j < nearlights;j++)
950                                 {
951                                         VectorSubtract(nearlight[j].origin, av, v);
952                                         t = DotProduct(avn,v);
953                                         if (t > 0)
954                                         {
955                                                 t /= (DotProduct(v,v) + LIGHTOFFSET);
956                                                 t1 += nearlight[j].color[0] * t;
957                                                 t2 += nearlight[j].color[1] * t;
958                                                 t3 += nearlight[j].color[2] * t;
959                                                 lit = true;
960                                         }
961                                 }
962                                 if (lit)
963                                 {
964                                         int i1, i2, i3;
965                                         i1 = (int) t1;
966                                         avc[0] = bound(0, i1, 255);
967                                         i2 = (int) t2;
968                                         avc[1] = bound(0, i2, 255);
969                                         i3 = (int) t3;
970                                         avc[2] = bound(0, i3, 255);
971                                         avc[3] = a;
972                                 }
973                                 else // dodge the costly float -> int conversions
974                                         *((int *)avc) = color;
975                                 avc += 4;
976                                 av += 3;
977                                 avn += 3;
978                         }
979                 }
980         }
981         else
982         {
983                 for (i = 0;i < numverts;i++)
984                 {
985                         *((int *)avc) = color;
986                         avc += 4;
987                 }
988         }
989 }