]> icculus.org git repositories - divverent/darkplaces.git/blob - r_light.c
improved TraceLine in chase.c to be more generally useful (should move it to another...
[divverent/darkplaces.git] / r_light.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // r_light.c
21
22 #include "quakedef.h"
23
24 cvar_t r_lightmodels = {"r_lightmodels", "1"};
25 cvar_t r_modelsdonttransformnormals = {"r_modelsdonttransformnormals", "0"};
26
27 void r_light_start()
28 {
29 }
30
31 void r_light_shutdown()
32 {
33 }
34
35 void R_Light_Init()
36 {
37         Cvar_RegisterVariable(&r_lightmodels);
38         Cvar_RegisterVariable(&r_modelsdonttransformnormals);
39         R_RegisterModule("R_Light", r_light_start, r_light_shutdown);
40 }
41
42 int     r_dlightframecount;
43
44 /*
45 ==================
46 R_AnimateLight
47 ==================
48 */
49 void R_AnimateLight (void)
50 {
51         int                     i,j,k;
52         
53 //
54 // light animations
55 // 'm' is normal light, 'a' is no light, 'z' is double bright
56         i = (int)(cl.time*10);
57         for (j=0 ; j<MAX_LIGHTSTYLES ; j++)
58         {
59                 if (!cl_lightstyle[j].length)
60                 {
61                         d_lightstylevalue[j] = 256;
62                         continue;
63                 }
64                 k = i % cl_lightstyle[j].length;
65                 k = cl_lightstyle[j].map[k] - 'a';
66                 k = k*22;
67                 d_lightstylevalue[j] = k;
68         }       
69 }
70
71 /*
72 =============================================================================
73
74 DYNAMIC LIGHTS
75
76 =============================================================================
77 */
78
79 /*
80 =============
81 R_MarkLights
82 =============
83 */
84 void R_OldMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, mnode_t *node)
85 {
86         float           ndist, maxdist;
87         msurface_t      *surf;
88         int                     i;
89
90         if (!r_dynamic.value)
91                 return;
92
93         // for comparisons to minimum acceptable light
94         maxdist = light->radius * light->radius * LIGHTSCALE;
95
96         // clamp radius to avoid exceeding 32768 entry division table
97         if (maxdist > 4194304)
98                 maxdist = 4194304;
99
100 loc0:
101         if (node->contents < 0)
102                 return;
103
104         ndist = PlaneDiff(lightorigin, node->plane);
105         
106         if (ndist > light->radius)
107         {
108                 if (node->children[0]->contents >= 0) // LordHavoc: save some time by not pushing another stack frame
109                 {
110                         node = node->children[0];
111                         goto loc0;
112                 }
113                 return;
114         }
115         if (ndist < -light->radius)
116         {
117                 if (node->children[1]->contents >= 0) // LordHavoc: save some time by not pushing another stack frame
118                 {
119                         node = node->children[1];
120                         goto loc0;
121                 }
122                 return;
123         }
124
125         if (node->dlightframe != r_dlightframecount) // not dynamic until now
126         {
127                 node->dlightbits[0] = node->dlightbits[1] = node->dlightbits[2] = node->dlightbits[3] = node->dlightbits[4] = node->dlightbits[5] = node->dlightbits[6] = node->dlightbits[7] = 0;
128                 node->dlightframe = r_dlightframecount;
129         }
130         node->dlightbits[bitindex] |= bit;
131
132 // mark the polygons
133         surf = cl.worldmodel->surfaces + node->firstsurface;
134         for (i=0 ; i<node->numsurfaces ; i++, surf++)
135         {
136                 int d;
137                 float dist, dist2, impact[3];
138                 dist = ndist;
139                 if (surf->flags & SURF_PLANEBACK)
140                         dist = -dist;
141
142                 if (dist < -0.25f)
143                         continue;
144
145                 dist2 = dist * dist;
146                 if (dist2 >= maxdist)
147                         continue;
148
149                 impact[0] = light->origin[0] - surf->plane->normal[0] * dist;
150                 impact[1] = light->origin[1] - surf->plane->normal[1] * dist;
151                 impact[2] = light->origin[2] - surf->plane->normal[2] * dist;
152
153                 d = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
154
155                 if (d < 0)
156                 {
157                         dist2 += d * d;
158                         if (dist2 >= maxdist)
159                                 continue;
160                 }
161                 else
162                 {
163                         d -= surf->extents[0] + 16;
164                         if (d > 0)
165                         {
166                                 dist2 += d * d;
167                                 if (dist2 >= maxdist)
168                                         continue;
169                         }
170                 }
171
172                 d = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
173
174                 if (d < 0)
175                 {
176                         dist2 += d * d;
177                         if (dist2 >= maxdist)
178                                 continue;
179                 }
180                 else
181                 {
182                         d -= surf->extents[1] + 16;
183                         if (d > 0)
184                         {
185                                 dist2 += d * d;
186                                 if (dist2 >= maxdist)
187                                         continue;
188                         }
189                 }
190
191                 if (surf->dlightframe != r_dlightframecount) // not dynamic until now
192                 {
193                         surf->dlightbits[0] = surf->dlightbits[1] = surf->dlightbits[2] = surf->dlightbits[3] = surf->dlightbits[4] = surf->dlightbits[5] = surf->dlightbits[6] = surf->dlightbits[7] = 0;
194                         surf->dlightframe = r_dlightframecount;
195                 }
196                 surf->dlightbits[bitindex] |= bit;
197
198                 /*
199                 if (((surf->flags & SURF_PLANEBACK) == 0) == ((PlaneDist(lightorigin, surf->plane)) >= surf->plane->dist))
200                 {
201                         if (surf->dlightframe != r_dlightframecount) // not dynamic until now
202                         {
203                                 surf->dlightbits[0] = surf->dlightbits[1] = surf->dlightbits[2] = surf->dlightbits[3] = surf->dlightbits[4] = surf->dlightbits[5] = surf->dlightbits[6] = surf->dlightbits[7] = 0;
204                                 surf->dlightframe = r_dlightframecount;
205                         }
206                         surf->dlightbits[bitindex] |= bit;
207                 }
208                 */
209         }
210
211         if (node->children[0]->contents >= 0)
212         {
213                 if (node->children[1]->contents >= 0)
214                 {
215                         R_OldMarkLights (lightorigin, light, bit, bitindex, node->children[0]);
216                         node = node->children[1];
217                         goto loc0;
218                 }
219                 else
220                 {
221                         node = node->children[0];
222                         goto loc0;
223                 }
224         }
225         else if (node->children[1]->contents >= 0)
226         {
227                 node = node->children[1];
228                 goto loc0;
229         }
230 }
231
232 void R_NoVisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model)
233 {
234         R_OldMarkLights(lightorigin, light, bit, bitindex, model->nodes + model->hulls[0].firstclipnode);
235 }
236
237 int lightframe = 0;
238 void R_VisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model)
239 {
240         mleaf_t *pvsleaf = Mod_PointInLeaf (lightorigin, model);
241
242         if (!r_dynamic.value)
243                 return;
244
245         if (!pvsleaf->compressed_vis)
246         {       // no vis info, so make all visible
247                 R_OldMarkLights(lightorigin, light, bit, bitindex, model->nodes + model->hulls[0].firstclipnode);
248                 return;
249         }
250         else
251         {
252                 int             i, k, l, m, c;
253                 msurface_t *surf, **mark;
254                 mleaf_t *leaf;
255                 byte    *in = pvsleaf->compressed_vis;
256                 int             row = (model->numleafs+7)>>3;
257                 float   low[3], high[3], radius, dist, maxdist;
258
259                 radius = light->radius * LIGHTSCALE1;
260
261                 // clamp radius to avoid exceeding 32768 entry division table
262                 if (radius > 2048)
263                         radius = 2048;
264
265                 low[0] = lightorigin[0] - radius;low[1] = lightorigin[1] - radius;low[2] = lightorigin[2] - radius;
266                 high[0] = lightorigin[0] + radius;high[1] = lightorigin[1] + radius;high[2] = lightorigin[2] + radius;
267
268                 // for comparisons to minimum acceptable light
269                 maxdist = radius*radius;
270
271                 lightframe++;
272                 k = 0;
273                 while (k < row)
274                 {
275                         c = *in++;
276                         if (c)
277                         {
278                                 l = model->numleafs - (k << 3);
279                                 if (l > 8)
280                                         l = 8;
281                                 for (i=0 ; i<l ; i++)
282                                 {
283                                         if (c & (1<<i))
284                                         {
285                                                 leaf = &model->leafs[(k << 3)+i+1];
286                                                 leaf->lightframe = lightframe;
287                                                 if (leaf->visframe != r_visframecount)
288                                                         continue;
289                                                 if (leaf->contents == CONTENTS_SOLID)
290                                                         continue;
291                                                 // if out of the light radius, skip
292                                                 if (leaf->minmaxs[0] > high[0] || leaf->minmaxs[3] < low[0]
293                                                  || leaf->minmaxs[1] > high[1] || leaf->minmaxs[4] < low[1]
294                                                  || leaf->minmaxs[2] > high[2] || leaf->minmaxs[5] < low[2])
295                                                         continue;
296                                                 if (leaf->dlightframe != r_dlightframecount) // 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_dlightframecount;
300                                                 }
301                                                 leaf->dlightbits[bitindex] |= bit;
302                                                 if ((m = leaf->nummarksurfaces))
303                                                 {
304                                                         mark = leaf->firstmarksurface;
305                                                         do
306                                                         {
307                                                                 surf = *mark++;
308                                                                 if (surf->visframe != r_framecount || surf->lightframe == lightframe)
309                                                                         continue;
310                                                                 surf->lightframe = lightframe;
311                                                                 dist = PlaneDiff(lightorigin, surf->plane);
312                                                                 if (surf->flags & SURF_PLANEBACK)
313                                                                         dist = -dist;
314                                                                 // LordHavoc: make sure it is infront of the surface and not too far away
315                                                                 if (dist >= -0.25f && dist < radius)
316                                                                 {
317                                                                         int d;
318                                                                         float dist2, impact[3];
319
320                                                                         dist2 = dist * dist;
321
322                                                                         impact[0] = light->origin[0] - surf->plane->normal[0] * dist;
323                                                                         impact[1] = light->origin[1] - surf->plane->normal[1] * dist;
324                                                                         impact[2] = light->origin[2] - surf->plane->normal[2] * dist;
325
326                                                                         d = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
327
328                                                                         if (d < 0)
329                                                                         {
330                                                                                 dist2 += d * d;
331                                                                                 if (dist2 >= maxdist)
332                                                                                         continue;
333                                                                         }
334                                                                         else
335                                                                         {
336                                                                                 d -= surf->extents[0] + 16;
337                                                                                 if (d > 0)
338                                                                                 {
339                                                                                         dist2 += d * d;
340                                                                                         if (dist2 >= maxdist)
341                                                                                                 continue;
342                                                                                 }
343                                                                         }
344
345                                                                         d = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
346
347                                                                         if (d < 0)
348                                                                         {
349                                                                                 dist2 += d * d;
350                                                                                 if (dist2 >= maxdist)
351                                                                                         continue;
352                                                                         }
353                                                                         else
354                                                                         {
355                                                                                 d -= surf->extents[1] + 16;
356                                                                                 if (d > 0)
357                                                                                 {
358                                                                                         dist2 += d * d;
359                                                                                         if (dist2 >= maxdist)
360                                                                                                 continue;
361                                                                                 }
362                                                                         }
363
364                                                                         if (surf->dlightframe != r_dlightframecount) // not dynamic until now
365                                                                         {
366                                                                                 surf->dlightbits[0] = surf->dlightbits[1] = surf->dlightbits[2] = surf->dlightbits[3] = surf->dlightbits[4] = surf->dlightbits[5] = surf->dlightbits[6] = surf->dlightbits[7] = 0;
367                                                                                 surf->dlightframe = r_dlightframecount;
368                                                                         }
369                                                                         surf->dlightbits[bitindex] |= bit;
370                                                                 }
371                                                         }
372                                                         while (--m);
373                                                 }
374                                         }
375                                 }
376                                 k++;
377                                 continue;
378                         }
379                 
380                         k += *in++;
381                 }
382         }
383 }
384
385
386 /*
387 =============
388 R_PushDlights
389 =============
390 */
391 void R_PushDlights (void)
392 {
393         int             i;
394         dlight_t        *l;
395
396         r_dlightframecount = r_framecount + 1;  // because the count hasn't advanced yet for this frame
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->die < cl.time || !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_LightPoint (vec3_t color, vec3_t p)
670 {
671         if (r_fullbright.value || !cl.worldmodel->lightdata)
672         {
673                 color[0] = color[1] = color[2] = 255;
674                 return;
675         }
676         
677         color[0] = color[1] = color[2] = r_ambient.value * 2.0f;
678         RecursiveLightPoint (color, cl.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
679 }
680
681 // LordHavoc: added light checking to the server
682 void SV_LightPoint (vec3_t color, vec3_t p)
683 {
684         if (!sv.worldmodel->lightdata)
685         {
686                 color[0] = color[1] = color[2] = 255;
687                 return;
688         }
689         
690         color[0] = color[1] = color[2] = 0;
691         RecursiveLightPoint (color, sv.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
692 }
693
694 // LordHavoc: R_DynamicLightPoint - acumulates the dynamic lighting
695 void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits)
696 {
697         int             i, j, k;
698         vec3_t  dist;
699         float   brightness, r, f;
700
701         if (!r_dynamic.value || (!dlightbits[0] && !dlightbits[1] && !dlightbits[2] && !dlightbits[3] && !dlightbits[4] && !dlightbits[5] && !dlightbits[6] && !dlightbits[7]))
702                 return;
703
704         for (j = 0;j < (MAX_DLIGHTS >> 5);j++)
705         {
706                 if (dlightbits[j])
707                 {
708                         for (i=0 ; i<32 ; i++)
709                         {
710                                 if ((!((1 << (i&31)) & dlightbits[i>>5])) || cl_dlights[i].die < cl.time || !cl_dlights[i].radius)
711                                         continue;
712                                 k = (j<<5)+i;
713                                 VectorSubtract (org, cl_dlights[k].origin, dist);
714                                 f = DotProduct(dist, dist) + LIGHTOFFSET;
715                                 r = cl_dlights[k].radius*cl_dlights[k].radius*LIGHTSCALE;
716                                 if (f < r)
717                                 {
718                                         brightness = r * (256.0f / LIGHTSCALE2) / f;
719                                         color[0] += brightness * cl_dlights[k].color[0];
720                                         color[1] += brightness * cl_dlights[k].color[1];
721                                         color[2] += brightness * cl_dlights[k].color[2];
722                                 }
723                         }
724                 }
725         }
726 }
727
728 // same as above but no bitmask to check
729 void R_DynamicLightPointNoMask(vec3_t color, vec3_t org)
730 {
731         int             i;
732         vec3_t  dist;
733         float   brightness, r, f;
734
735         if (!r_dynamic.value)
736                 return;
737
738         for (i=0 ; i<MAX_DLIGHTS ; i++)
739         {
740                 if (cl_dlights[i].die < cl.time || !cl_dlights[i].radius)
741                         continue;
742                 VectorSubtract (org, cl_dlights[i].origin, dist);
743                 f = DotProduct(dist, dist) + LIGHTOFFSET;
744                 r = cl_dlights[i].radius*cl_dlights[i].radius*LIGHTSCALE;
745                 if (f < r)
746                 {
747                         brightness = r * (256.0f / LIGHTSCALE2) / f;
748                         if (cl_dlights[i].dark)
749                                 brightness = -brightness;
750                         color[0] += brightness * cl_dlights[i].color[0];
751                         color[1] += brightness * cl_dlights[i].color[1];
752                         color[2] += brightness * cl_dlights[i].color[2];
753                 }
754         }
755 }
756
757 void R_CompleteLightPoint (vec3_t color, vec3_t p)
758 {
759         R_LightPoint(color, p);
760         R_DynamicLightPointNoMask(color, p);
761 }
762
763 extern float *aliasvert;
764 extern float *modelaliasvert;
765 extern float *aliasvertnorm;
766 extern byte *aliasvertcolor;
767 extern float modelalpha;
768 extern int modeldlightbits[8];
769 void R_LightModel(int numverts, vec3_t center, vec3_t basecolor)
770 {
771         // LordHavoc: warning: reliance on int being 4 bytes here (of course the d_8to24table relies on that too...)
772         int i, j, nearlights = 0, color;
773         vec3_t dist, mod;
774         float t, t1, t2, t3, *avn;
775         byte r,g,b,a, *avc;
776         struct
777         {
778                 vec3_t color;
779                 vec3_t origin;
780         } nearlight[MAX_DLIGHTS];
781         avc = aliasvertcolor;
782         avn = aliasvertnorm;
783         a = (byte) bound((int) 0, (int) (modelalpha * 255.0f), (int) 255);
784         if (currententity->effects & EF_FULLBRIGHT)
785         {
786                 if (lighthalf)
787                 {
788                         ((byte *)&color)[0] = (byte) ((float) (128.0f * currententity->colormod[0]));
789                         ((byte *)&color)[1] = (byte) ((float) (128.0f * currententity->colormod[1]));
790                         ((byte *)&color)[2] = (byte) ((float) (128.0f * currententity->colormod[2]));
791                 }
792                 else
793                 {
794                         ((byte *)&color)[0] = (byte) ((float) (255.0f * currententity->colormod[0]));
795                         ((byte *)&color)[1] = (byte) ((float) (255.0f * currententity->colormod[1]));
796                         ((byte *)&color)[2] = (byte) ((float) (255.0f * currententity->colormod[2]));
797                 }
798                 ((byte *)&color)[3] = a;
799                 for (i = 0;i < numverts;i++)
800                 {
801                         *((int *)avc) = color;
802                         avc += 4;
803                 }
804                 return;
805         }
806         if (lighthalf)
807         {
808                 mod[0] = currententity->colormod[0] * 0.5f;
809                 mod[1] = currententity->colormod[1] * 0.5f;
810                 mod[2] = currententity->colormod[2] * 0.5f;
811         }
812         else
813         {
814                 mod[0] = currententity->colormod[0];
815                 mod[1] = currententity->colormod[1];
816                 mod[2] = currententity->colormod[2];
817         }
818         basecolor[0] *= mod[0];
819         basecolor[1] *= mod[1];
820         basecolor[2] *= mod[2];
821         if (!r_lightmodels.value)
822         {
823                 for (i = 0;i < MAX_DLIGHTS;i++)
824                 {
825                         if (!modeldlightbits[i >> 5])
826                         {
827                                 i |= 31;
828                                 continue;
829                         }
830                         if (!(modeldlightbits[i >> 5] & (1 << (i & 31))))
831                                 continue;
832                         VectorSubtract (center, cl_dlights[i].origin, dist);
833                         t1 = cl_dlights[i].radius*cl_dlights[i].radius*LIGHTSCALE;
834                         t2 = DotProduct(dist,dist) * (1.0f / LIGHTSCALE) + LIGHTOFFSET;
835                         if (t2 < t1)
836                         {
837                                 dist[0] = cl_dlights[i].color[0] * t1 * mod[0];
838                                 dist[1] = cl_dlights[i].color[1] * t1 * mod[1];
839                                 dist[2] = cl_dlights[i].color[2] * t1 * mod[2];
840                                 t1 = (224.0f / LIGHTSCALE / LIGHTSCALE) / t2;
841                                 basecolor[0] += dist[0] * t1;
842                                 basecolor[1] += dist[1] * t1;
843                                 basecolor[2] += dist[2] * t1;
844                         }
845                 }
846                 ((byte *)&color)[0] = bound(0, basecolor[0], 255);
847                 ((byte *)&color)[1] = bound(0, basecolor[1], 255);
848                 ((byte *)&color)[2] = bound(0, basecolor[2], 255);
849                 ((byte *)&color)[3] = a;
850                 for (i = 0;i < numverts;i++)
851                 {
852                         *((int *)avc) = color;
853                         avc += 4;
854                 }
855                 return;
856         }
857         else
858         {
859                 for (i = 0;i < MAX_DLIGHTS;i++)
860                 {
861                         if (!modeldlightbits[i >> 5])
862                         {
863                                 i |= 31;
864                                 continue;
865                         }
866                         if (!(modeldlightbits[i >> 5] & (1 << (i & 31))))
867                                 continue;
868                         VectorSubtract (center, cl_dlights[i].origin, dist);
869                         t1 = cl_dlights[i].radius*cl_dlights[i].radius*LIGHTSCALE;
870                         t2 = DotProduct(dist,dist) * (1.0f / LIGHTSCALE) + LIGHTOFFSET;
871                         if (t2 < t1)
872                         {
873                                 if (r_modelsdonttransformnormals.value)
874                                         softwareuntransform(cl_dlights[i].origin, nearlight[nearlights].origin);
875                                 else
876                                 {
877                                         VectorCopy(cl_dlights[i].origin, nearlight[nearlights].origin);
878                                 }
879                                 nearlight[nearlights].color[0] = cl_dlights[i].color[0] * t1 * mod[0];
880                                 nearlight[nearlights].color[1] = cl_dlights[i].color[1] * t1 * mod[1];
881                                 nearlight[nearlights].color[2] = cl_dlights[i].color[2] * t1 * mod[2];
882 //                              t1 = (128.0f / LIGHTSCALE2) / t2;
883 //                              basecolor[0] += nearlight[nearlights].color[0] * t1;
884 //                              basecolor[1] += nearlight[nearlights].color[1] * t1;
885 //                              basecolor[2] += nearlight[nearlights].color[2] * t1;
886                                 nearlights++;
887                         }
888                 }
889         }
890         t1 = bound(0, basecolor[0], 255);r = (byte) t1;
891         t1 = bound(0, basecolor[1], 255);g = (byte) t1;
892         t1 = bound(0, basecolor[2], 255);b = (byte) t1;
893         ((byte *)&color)[0] = r;
894         ((byte *)&color)[1] = g;
895         ((byte *)&color)[2] = b;
896         ((byte *)&color)[3] = a;
897         if (nearlights)
898         {
899                 int temp;
900                 vec3_t v;
901                 float *av;
902                 if (r_modelsdonttransformnormals.value)
903                         av = modelaliasvert;
904                 else
905                         av = aliasvert;
906                 if (nearlights == 1)
907                 {
908                         for (i = 0;i < numverts;i++)
909                         {
910                                 VectorSubtract(nearlight[0].origin, av, v);
911                                 t = DotProduct(avn,v);
912                                 if (t > 0)
913                                 {
914                                         t /= DotProduct(v,v);
915                                         temp = (int) ((float) (basecolor[0] + nearlight[0].color[0] * t));if (temp < 0) temp = 0;else if (temp > 255) temp = 255;avc[0] = temp;
916                                         temp = (int) ((float) (basecolor[1] + nearlight[0].color[1] * t));if (temp < 0) temp = 0;else if (temp > 255) temp = 255;avc[1] = temp;
917                                         temp = (int) ((float) (basecolor[2] + nearlight[0].color[2] * t));if (temp < 0) temp = 0;else if (temp > 255) temp = 255;avc[2] = temp;
918                                         avc[3] = a;
919                                 }
920                                 else
921                                         *((int *)avc) = color;
922                                 avc += 4;
923                                 av += 3;
924                                 avn += 3;
925                         }
926                 }
927                 else
928                 {
929                         int i1, i2, i3, k;
930                         for (i = 0;i < numverts;i++)
931                         {
932                                 t1 = basecolor[0];
933                                 t2 = basecolor[1];
934                                 t3 = basecolor[2];
935                                 k = false;
936                                 for (j = 0;j < nearlights;j++)
937                                 {
938                                         VectorSubtract(nearlight[j].origin, av, v);
939                                         t = DotProduct(avn,v);
940                                         if (t > 0)
941                                         {
942                                                 t /= DotProduct(v,v);
943                                                 t1 += nearlight[j].color[0] * t;
944                                                 t2 += nearlight[j].color[1] * t;
945                                                 t3 += nearlight[j].color[2] * t;
946                                                 k = true;
947                                         }
948                                 }
949                                 if (k)
950                                 {
951                                         i1 = t1;if (i1 < 0) i1 = 0;else if (i1 > 255) i1 = 255;avc[0] = i1;
952                                         i2 = t2;if (i2 < 0) i2 = 0;else if (i2 > 255) i2 = 255;avc[1] = i2;
953                                         i3 = t3;if (i3 < 0) i3 = 0;else if (i3 > 255) i3 = 255;avc[2] = i3;
954                                         avc[3] = a;
955                                 }
956                                 else // dodge the costly float -> int conversions
957                                         *((int *)avc) = color;
958                                 avc += 4;
959                                 av += 3;
960                                 avn += 3;
961                         }
962                 }
963         }
964         else
965         {
966                 for (i = 0;i < numverts;i++)
967                 {
968                         *((int *)avc) = color;
969                         avc += 4;
970                 }
971         }
972 }