clean up use of client structs in server
[xonotic/darkplaces.git] / r_light.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // r_light.c
21
22 #include "quakedef.h"
23
24 cvar_t r_lightmodels = {"r_lightmodels", "1"};
25 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: R_DynamicLightPoint - acumulates the dynamic lighting
682 void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits)
683 {
684         int             i, j, k;
685         vec3_t  dist;
686         float   brightness, r, f;
687
688         if (!r_dynamic.value || (!dlightbits[0] && !dlightbits[1] && !dlightbits[2] && !dlightbits[3] && !dlightbits[4] && !dlightbits[5] && !dlightbits[6] && !dlightbits[7]))
689                 return;
690
691         for (j = 0;j < (MAX_DLIGHTS >> 5);j++)
692         {
693                 if (dlightbits[j])
694                 {
695                         for (i=0 ; i<32 ; i++)
696                         {
697                                 if ((!((1 << (i&31)) & dlightbits[i>>5])) || cl_dlights[i].die < cl.time || !cl_dlights[i].radius)
698                                         continue;
699                                 k = (j<<5)+i;
700                                 VectorSubtract (org, cl_dlights[k].origin, dist);
701                                 f = DotProduct(dist, dist) + LIGHTOFFSET;
702                                 r = cl_dlights[k].radius*cl_dlights[k].radius*LIGHTSCALE;
703                                 if (f < r)
704                                 {
705                                         brightness = r * (256.0f / LIGHTSCALE2) / f;
706                                         color[0] += brightness * cl_dlights[k].color[0];
707                                         color[1] += brightness * cl_dlights[k].color[1];
708                                         color[2] += brightness * cl_dlights[k].color[2];
709                                 }
710                         }
711                 }
712         }
713 }
714
715 // same as above but no bitmask to check
716 void R_DynamicLightPointNoMask(vec3_t color, vec3_t org)
717 {
718         int             i;
719         vec3_t  dist;
720         float   brightness, r, f;
721
722         if (!r_dynamic.value)
723                 return;
724
725         for (i=0 ; i<MAX_DLIGHTS ; i++)
726         {
727                 if (cl_dlights[i].die < cl.time || !cl_dlights[i].radius)
728                         continue;
729                 VectorSubtract (org, cl_dlights[i].origin, dist);
730                 f = DotProduct(dist, dist) + LIGHTOFFSET;
731                 r = cl_dlights[i].radius*cl_dlights[i].radius*LIGHTSCALE;
732                 if (f < r)
733                 {
734                         brightness = r * (256.0f / LIGHTSCALE2) / f;
735                         if (cl_dlights[i].dark)
736                                 brightness = -brightness;
737                         color[0] += brightness * cl_dlights[i].color[0];
738                         color[1] += brightness * cl_dlights[i].color[1];
739                         color[2] += brightness * cl_dlights[i].color[2];
740                 }
741         }
742 }
743
744 void R_CompleteLightPoint (vec3_t color, vec3_t p)
745 {
746         R_LightPoint(color, p);
747         R_DynamicLightPointNoMask(color, p);
748 }
749
750 extern float *aliasvert;
751 extern float *modelaliasvert;
752 extern float *aliasvertnorm;
753 extern byte *aliasvertcolor;
754 extern float modelalpha;
755 extern int modeldlightbits[8];
756 void R_LightModel(int numverts, vec3_t center, vec3_t basecolor)
757 {
758         // LordHavoc: warning: reliance on int being 4 bytes here (of course the d_8to24table relies on that too...)
759         int i, j, nearlights = 0, color;
760         vec3_t dist, mod;
761         float t, t1, t2, t3, *avn;
762         byte r,g,b,a, *avc;
763         struct
764         {
765                 vec3_t color;
766                 vec3_t origin;
767         } nearlight[MAX_DLIGHTS];
768         avc = aliasvertcolor;
769         avn = aliasvertnorm;
770         a = (byte) bound((int) 0, (int) (modelalpha * 255.0f), (int) 255);
771         if (currententity->effects & EF_FULLBRIGHT)
772         {
773                 if (lighthalf)
774                 {
775                         ((byte *)&color)[0] = (byte) ((float) (128.0f * currententity->colormod[0]));
776                         ((byte *)&color)[1] = (byte) ((float) (128.0f * currententity->colormod[1]));
777                         ((byte *)&color)[2] = (byte) ((float) (128.0f * currententity->colormod[2]));
778                 }
779                 else
780                 {
781                         ((byte *)&color)[0] = (byte) ((float) (255.0f * currententity->colormod[0]));
782                         ((byte *)&color)[1] = (byte) ((float) (255.0f * currententity->colormod[1]));
783                         ((byte *)&color)[2] = (byte) ((float) (255.0f * currententity->colormod[2]));
784                 }
785                 ((byte *)&color)[3] = a;
786                 for (i = 0;i < numverts;i++)
787                 {
788                         *((int *)avc) = color;
789                         avc += 4;
790                 }
791                 return;
792         }
793         if (lighthalf)
794         {
795                 mod[0] = currententity->colormod[0] * 0.5f;
796                 mod[1] = currententity->colormod[1] * 0.5f;
797                 mod[2] = currententity->colormod[2] * 0.5f;
798         }
799         else
800         {
801                 mod[0] = currententity->colormod[0];
802                 mod[1] = currententity->colormod[1];
803                 mod[2] = currententity->colormod[2];
804         }
805         basecolor[0] *= mod[0];
806         basecolor[1] *= mod[1];
807         basecolor[2] *= mod[2];
808         if (!r_lightmodels.value)
809         {
810                 for (i = 0;i < MAX_DLIGHTS;i++)
811                 {
812                         if (!modeldlightbits[i >> 5])
813                         {
814                                 i |= 31;
815                                 continue;
816                         }
817                         if (!(modeldlightbits[i >> 5] & (1 << (i & 31))))
818                                 continue;
819                         VectorSubtract (center, cl_dlights[i].origin, dist);
820                         t1 = cl_dlights[i].radius*cl_dlights[i].radius*LIGHTSCALE;
821                         t2 = DotProduct(dist,dist) * (1.0f / LIGHTSCALE) + LIGHTOFFSET;
822                         if (t2 < t1)
823                         {
824                                 dist[0] = cl_dlights[i].color[0] * t1 * mod[0];
825                                 dist[1] = cl_dlights[i].color[1] * t1 * mod[1];
826                                 dist[2] = cl_dlights[i].color[2] * t1 * mod[2];
827                                 t1 = (224.0f / LIGHTSCALE / LIGHTSCALE) / t2;
828                                 basecolor[0] += dist[0] * t1;
829                                 basecolor[1] += dist[1] * t1;
830                                 basecolor[2] += dist[2] * t1;
831                         }
832                 }
833                 ((byte *)&color)[0] = bound(0, basecolor[0], 255);
834                 ((byte *)&color)[1] = bound(0, basecolor[1], 255);
835                 ((byte *)&color)[2] = bound(0, basecolor[2], 255);
836                 ((byte *)&color)[3] = a;
837                 for (i = 0;i < numverts;i++)
838                 {
839                         *((int *)avc) = color;
840                         avc += 4;
841                 }
842                 return;
843         }
844         else
845         {
846                 for (i = 0;i < MAX_DLIGHTS;i++)
847                 {
848                         if (!modeldlightbits[i >> 5])
849                         {
850                                 i |= 31;
851                                 continue;
852                         }
853                         if (!(modeldlightbits[i >> 5] & (1 << (i & 31))))
854                                 continue;
855                         VectorSubtract (center, cl_dlights[i].origin, dist);
856                         t1 = cl_dlights[i].radius*cl_dlights[i].radius*LIGHTSCALE;
857                         t2 = DotProduct(dist,dist) * (1.0f / LIGHTSCALE) + LIGHTOFFSET;
858                         if (t2 < t1)
859                         {
860                                 if (r_modelsdonttransformnormals.value)
861                                         softwareuntransform(cl_dlights[i].origin, nearlight[nearlights].origin);
862                                 else
863                                 {
864                                         VectorCopy(cl_dlights[i].origin, nearlight[nearlights].origin);
865                                 }
866                                 nearlight[nearlights].color[0] = cl_dlights[i].color[0] * t1 * mod[0];
867                                 nearlight[nearlights].color[1] = cl_dlights[i].color[1] * t1 * mod[1];
868                                 nearlight[nearlights].color[2] = cl_dlights[i].color[2] * t1 * mod[2];
869 //                              t1 = (128.0f / LIGHTSCALE2) / t2;
870 //                              basecolor[0] += nearlight[nearlights].color[0] * t1;
871 //                              basecolor[1] += nearlight[nearlights].color[1] * t1;
872 //                              basecolor[2] += nearlight[nearlights].color[2] * t1;
873                                 nearlights++;
874                         }
875                 }
876         }
877         t1 = bound(0, basecolor[0], 255);r = (byte) t1;
878         t1 = bound(0, basecolor[1], 255);g = (byte) t1;
879         t1 = bound(0, basecolor[2], 255);b = (byte) t1;
880         ((byte *)&color)[0] = r;
881         ((byte *)&color)[1] = g;
882         ((byte *)&color)[2] = b;
883         ((byte *)&color)[3] = a;
884         if (nearlights)
885         {
886                 int temp;
887                 vec3_t v;
888                 float *av;
889                 if (r_modelsdonttransformnormals.value)
890                         av = modelaliasvert;
891                 else
892                         av = aliasvert;
893                 if (nearlights == 1)
894                 {
895                         for (i = 0;i < numverts;i++)
896                         {
897                                 VectorSubtract(nearlight[0].origin, av, v);
898                                 t = DotProduct(avn,v);
899                                 if (t > 0)
900                                 {
901                                         t /= DotProduct(v,v);
902                                         temp = (int) ((float) (basecolor[0] + nearlight[0].color[0] * t));if (temp < 0) temp = 0;else if (temp > 255) temp = 255;avc[0] = temp;
903                                         temp = (int) ((float) (basecolor[1] + nearlight[0].color[1] * t));if (temp < 0) temp = 0;else if (temp > 255) temp = 255;avc[1] = temp;
904                                         temp = (int) ((float) (basecolor[2] + nearlight[0].color[2] * t));if (temp < 0) temp = 0;else if (temp > 255) temp = 255;avc[2] = temp;
905                                         avc[3] = a;
906                                 }
907                                 else
908                                         *((int *)avc) = color;
909                                 avc += 4;
910                                 av += 3;
911                                 avn += 3;
912                         }
913                 }
914                 else
915                 {
916                         int i1, i2, i3, k;
917                         for (i = 0;i < numverts;i++)
918                         {
919                                 t1 = basecolor[0];
920                                 t2 = basecolor[1];
921                                 t3 = basecolor[2];
922                                 k = false;
923                                 for (j = 0;j < nearlights;j++)
924                                 {
925                                         VectorSubtract(nearlight[j].origin, av, v);
926                                         t = DotProduct(avn,v);
927                                         if (t > 0)
928                                         {
929                                                 t /= DotProduct(v,v);
930                                                 t1 += nearlight[j].color[0] * t;
931                                                 t2 += nearlight[j].color[1] * t;
932                                                 t3 += nearlight[j].color[2] * t;
933                                                 k = true;
934                                         }
935                                 }
936                                 if (k)
937                                 {
938                                         i1 = t1;if (i1 < 0) i1 = 0;else if (i1 > 255) i1 = 255;avc[0] = i1;
939                                         i2 = t2;if (i2 < 0) i2 = 0;else if (i2 > 255) i2 = 255;avc[1] = i2;
940                                         i3 = t3;if (i3 < 0) i3 = 0;else if (i3 > 255) i3 = 255;avc[2] = i3;
941                                         avc[3] = a;
942                                 }
943                                 else // dodge the costly float -> int conversions
944                                         *((int *)avc) = color;
945                                 avc += 4;
946                                 av += 3;
947                                 avn += 3;
948                         }
949                 }
950         }
951         else
952         {
953                 for (i = 0;i < numverts;i++)
954                 {
955                         *((int *)avc) = color;
956                         avc += 4;
957                 }
958         }
959 }