muzzleflash fades out
[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 rdlight_t r_dlight[MAX_DLIGHTS];
25 int r_numdlights = 0;
26
27 cvar_t r_lightmodels = {CVAR_SAVE, "r_lightmodels", "1"};
28 cvar_t r_vismarklights = {0, "r_vismarklights", "1"};
29 //cvar_t r_lightmodelhardness = {CVAR_SAVE, "r_lightmodelhardness", "1"};
30
31 static rtexture_t *lightcorona;
32 static rtexturepool_t *lighttexturepool;
33
34 void r_light_start(void)
35 {
36         float dx, dy;
37         int x, y, a;
38         qbyte pixels[32][32][4];
39         lighttexturepool = R_AllocTexturePool();
40         for (y = 0;y < 32;y++)
41         {
42                 dy = (y - 15.5f) * (1.0f / 16.0f);
43                 for (x = 0;x < 32;x++)
44                 {
45                         dx = (x - 15.5f) * (1.0f / 16.0f);
46                         a = ((1.0f / (dx * dx + dy * dy + 0.2f)) - (1.0f / (1.0f + 0.2))) * 64.0f / (1.0f / (1.0f + 0.2));
47                         a = bound(0, a, 255);
48                         pixels[y][x][0] = 255;
49                         pixels[y][x][1] = 255;
50                         pixels[y][x][2] = 255;
51                         pixels[y][x][3] = a;
52                 }
53         }
54         lightcorona = R_LoadTexture (lighttexturepool, "lightcorona", 32, 32, &pixels[0][0][0], TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_ALPHA);
55 }
56
57 void r_light_shutdown(void)
58 {
59         lighttexturepool = NULL;
60         lightcorona = NULL;
61 }
62
63 void r_light_newmap(void)
64 {
65 }
66
67 void R_Light_Init(void)
68 {
69         Cvar_RegisterVariable(&r_lightmodels);
70         //Cvar_RegisterVariable(&r_lightmodelhardness);
71         Cvar_RegisterVariable(&r_vismarklights);
72         R_RegisterModule("R_Light", r_light_start, r_light_shutdown, r_light_newmap);
73 }
74
75 /*
76 ==================
77 R_AnimateLight
78 ==================
79 */
80 void R_AnimateLight (void)
81 {
82         int i, j, k;
83
84 //
85 // light animations
86 // 'm' is normal light, 'a' is no light, 'z' is double bright
87         i = (int)(cl.time * 10);
88         for (j = 0;j < MAX_LIGHTSTYLES;j++)
89         {
90                 if (!cl_lightstyle[j].length)
91                 {
92                         d_lightstylevalue[j] = 256;
93                         continue;
94                 }
95                 k = i % cl_lightstyle[j].length;
96                 k = cl_lightstyle[j].map[k] - 'a';
97                 k = k*22;
98                 d_lightstylevalue[j] = k;
99         }
100 }
101
102
103 void R_BuildLightList(void)
104 {
105         int                     i;
106         dlight_t        *cd;
107         rdlight_t       *rd;
108
109         r_numdlights = 0;
110         c_dlights = 0;
111
112         if (!r_dynamic.integer)
113                 return;
114
115         for (i = 0;i < MAX_DLIGHTS;i++)
116         {
117                 cd = cl_dlights + i;
118                 if (cd->radius <= 0)
119                         continue;
120                 rd = &r_dlight[r_numdlights++];
121                 VectorCopy(cd->origin, rd->origin);
122                 VectorScale(cd->color, cd->radius * 128.0f, rd->light);
123                 rd->cullradius = (1.0f / 128.0f) * sqrt(DotProduct(rd->light, rd->light));
124                 // clamp radius to avoid overflowing division table in lightmap code
125                 if (rd->cullradius > 2048.0f)
126                         rd->cullradius = 2048.0f;
127                 rd->cullradius2 = rd->cullradius * rd->cullradius;
128                 rd->lightsubtract = 1.0f / rd->cullradius2;
129                 rd->ent = cd->ent;
130                 r_numdlights++;
131                 c_dlights++; // count every dlight in use
132         }
133 }
134
135 static int coronapolyindex[6] = {0, 1, 2, 0, 2, 3};
136
137 void R_DrawCoronas(void)
138 {
139         int i;
140         rmeshinfo_t m;
141         float tvxyz[4][4], tvst[4][2], scale, viewdist, diff[3], dist;
142         rdlight_t *rd;
143         memset(&m, 0, sizeof(m));
144         m.transparent = false;
145         m.blendfunc1 = GL_SRC_ALPHA;
146         m.blendfunc2 = GL_ONE;
147         m.depthdisable = true; // magic
148         m.numtriangles = 2;
149         m.numverts = 4;
150         m.index = coronapolyindex;
151         m.vertex = &tvxyz[0][0];
152         m.vertexstep = sizeof(float[4]);
153         m.tex[0] = R_GetTexture(lightcorona);
154         m.texcoords[0] = &tvst[0][0];
155         m.texcoordstep[0] = sizeof(float[2]);
156         tvst[0][0] = 0;
157         tvst[0][1] = 0;
158         tvst[1][0] = 0;
159         tvst[1][1] = 1;
160         tvst[2][0] = 1;
161         tvst[2][1] = 1;
162         tvst[3][0] = 1;
163         tvst[3][1] = 0;
164         viewdist = DotProduct(r_origin, vpn);
165         for (i = 0;i < r_numdlights;i++)
166         {
167                 rd = r_dlight + i;
168                 dist = (DotProduct(rd->origin, vpn) - viewdist);
169                 if (dist >= 24.0f)
170                 {
171                         // trace to a point just barely closer to the eye
172                         VectorSubtract(rd->origin, vpn, diff);
173                         if (TraceLine(r_origin, diff, NULL, NULL, 0, true) == 1)
174                         {
175                                 scale = 1.0f / 262144.0f;
176                                 //scale = 64.0f / (DotProduct(diff,diff) + 1024.0f);
177                                 m.cr = rd->light[0] * scale;
178                                 m.cg = rd->light[1] * scale;
179                                 m.cb = rd->light[2] * scale;
180                                 m.ca = 1;
181                                 if (fogenabled)
182                                 {
183                                         VectorSubtract(rd->origin, r_origin, diff);
184                                         m.ca *= 1 - exp(fogdensity/DotProduct(diff,diff));
185                                 }
186                                 scale = rd->cullradius * 0.25f;
187                                 tvxyz[0][0] = rd->origin[0] - vright[0] * scale - vup[0] * scale;
188                                 tvxyz[0][1] = rd->origin[1] - vright[1] * scale - vup[1] * scale;
189                                 tvxyz[0][2] = rd->origin[2] - vright[2] * scale - vup[2] * scale;
190                                 tvxyz[1][0] = rd->origin[0] - vright[0] * scale + vup[0] * scale;
191                                 tvxyz[1][1] = rd->origin[1] - vright[1] * scale + vup[1] * scale;
192                                 tvxyz[1][2] = rd->origin[2] - vright[2] * scale + vup[2] * scale;
193                                 tvxyz[2][0] = rd->origin[0] + vright[0] * scale + vup[0] * scale;
194                                 tvxyz[2][1] = rd->origin[1] + vright[1] * scale + vup[1] * scale;
195                                 tvxyz[2][2] = rd->origin[2] + vright[2] * scale + vup[2] * scale;
196                                 tvxyz[3][0] = rd->origin[0] + vright[0] * scale - vup[0] * scale;
197                                 tvxyz[3][1] = rd->origin[1] + vright[1] * scale - vup[1] * scale;
198                                 tvxyz[3][2] = rd->origin[2] + vright[2] * scale - vup[2] * scale;
199                                 R_Mesh_Draw(&m);
200                         }
201                 }
202         }
203 }
204
205 /*
206 =============================================================================
207
208 DYNAMIC LIGHTS
209
210 =============================================================================
211 */
212
213 /*
214 =============
215 R_MarkLights
216 =============
217 */
218 static void R_OldMarkLights (vec3_t lightorigin, rdlight_t *rd, int bit, int bitindex, mnode_t *node)
219 {
220         float           ndist, maxdist;
221         msurface_t      *surf;
222         mleaf_t         *leaf;
223         int                     i;
224
225         if (!r_dynamic.integer)
226                 return;
227
228         // for comparisons to minimum acceptable light
229         maxdist = rd->cullradius2;
230
231 loc0:
232         if (node->contents < 0)
233         {
234                 if (node->contents != CONTENTS_SOLID)
235                 {
236                         leaf = (mleaf_t *)node;
237                         if (leaf->dlightframe != r_framecount) // not dynamic until now
238                         {
239                                 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;
240                                 leaf->dlightframe = r_framecount;
241                         }
242                         leaf->dlightbits[bitindex] |= bit;
243                 }
244                 return;
245         }
246
247         ndist = PlaneDiff(lightorigin, node->plane);
248
249         if (ndist > rd->cullradius)
250         {
251                 node = node->children[0];
252                 goto loc0;
253         }
254         if (ndist < -rd->cullradius)
255         {
256                 node = node->children[1];
257                 goto loc0;
258         }
259
260 // mark the polygons
261         surf = currentrenderentity->model->surfaces + node->firstsurface;
262         for (i=0 ; i<node->numsurfaces ; i++, surf++)
263         {
264                 int d, impacts, impactt;
265                 float dist, dist2, impact[3];
266                 if (surf->visframe != r_framecount)
267                         continue;
268                 dist = ndist;
269                 if (surf->flags & SURF_PLANEBACK)
270                         dist = -dist;
271
272                 if (dist < -0.25f && !(surf->flags & SURF_LIGHTBOTHSIDES))
273                         continue;
274
275                 dist2 = dist * dist;
276                 if (dist2 >= maxdist)
277                         continue;
278
279                 impact[0] = rd->origin[0] - surf->plane->normal[0] * dist;
280                 impact[1] = rd->origin[1] - surf->plane->normal[1] * dist;
281                 impact[2] = rd->origin[2] - surf->plane->normal[2] * dist;
282
283                 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
284
285                 d = bound(0, impacts, surf->extents[0] + 16) - impacts;
286                 dist2 += d * d;
287                 if (dist2 > maxdist)
288                         continue;
289
290                 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
291
292                 d = bound(0, impactt, surf->extents[1] + 16) - impactt;
293                 dist2 += d * d;
294                 if (dist2 > maxdist)
295                         continue;
296
297
298                 /*
299                 d = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
300
301                 if (d < 0)
302                 {
303                         dist2 += d * d;
304                         if (dist2 >= maxdist)
305                                 continue;
306                 }
307                 else
308                 {
309                         d -= surf->extents[0] + 16;
310                         if (d > 0)
311                         {
312                                 dist2 += d * d;
313                                 if (dist2 >= maxdist)
314                                         continue;
315                         }
316                 }
317
318                 d = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
319
320                 if (d < 0)
321                 {
322                         dist2 += d * d;
323                         if (dist2 >= maxdist)
324                                 continue;
325                 }
326                 else
327                 {
328                         d -= surf->extents[1] + 16;
329                         if (d > 0)
330                         {
331                                 dist2 += d * d;
332                                 if (dist2 >= maxdist)
333                                         continue;
334                         }
335                 }
336                 */
337
338                 if (surf->dlightframe != r_framecount) // not dynamic until now
339                 {
340                         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;
341                         surf->dlightframe = r_framecount;
342                 }
343                 surf->dlightbits[bitindex] |= bit;
344
345                 /*
346                 if (((surf->flags & SURF_PLANEBACK) == 0) == ((PlaneDist(lightorigin, surf->plane)) >= surf->plane->dist))
347                 {
348                         if (surf->dlightframe != r_framecount) // not dynamic until now
349                         {
350                                 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;
351                                 surf->dlightframe = r_framecount;
352                         }
353                         surf->dlightbits[bitindex] |= bit;
354                 }
355                 */
356         }
357
358         if (node->children[0]->contents >= 0)
359         {
360                 if (node->children[1]->contents >= 0)
361                 {
362                         R_OldMarkLights (lightorigin, rd, bit, bitindex, node->children[0]);
363                         node = node->children[1];
364                         goto loc0;
365                 }
366                 else
367                 {
368                         node = node->children[0];
369                         goto loc0;
370                 }
371         }
372         else if (node->children[1]->contents >= 0)
373         {
374                 node = node->children[1];
375                 goto loc0;
376         }
377 }
378
379 /*
380 static void R_NoVisMarkLights (rdlight_t *rd, int bit, int bitindex)
381 {
382         vec3_t lightorigin;
383         softwareuntransform(rd->origin, lightorigin);
384
385         R_OldMarkLights(lightorigin, rd, bit, bitindex, currentrenderentity->model->nodes + currentrenderentity->model->hulls[0].firstclipnode);
386 }
387 */
388
389 static void R_VisMarkLights (rdlight_t *rd, int bit, int bitindex)
390 {
391         static int lightframe = 0;
392         mleaf_t *pvsleaf;
393         vec3_t lightorigin;
394         model_t *model;
395         int             i, k, m, c, leafnum;
396         msurface_t *surf, **mark;
397         mleaf_t *leaf;
398         qbyte   *in;
399         int             row;
400         float   low[3], high[3], dist, maxdist;
401
402         if (!r_dynamic.integer)
403                 return;
404
405         model = currentrenderentity->model;
406         softwareuntransform(rd->origin, lightorigin);
407
408         if (!r_vismarklights.integer)
409         {
410                 R_OldMarkLights(lightorigin, rd, bit, bitindex, model->nodes + model->hulls[0].firstclipnode);
411                 return;
412         }
413
414         pvsleaf = Mod_PointInLeaf (lightorigin, model);
415         if (pvsleaf == NULL)
416         {
417                 Con_Printf("R_VisMarkLights: NULL leaf??\n");
418                 R_OldMarkLights(lightorigin, rd, bit, bitindex, model->nodes + model->hulls[0].firstclipnode);
419                 return;
420         }
421
422         in = pvsleaf->compressed_vis;
423         if (!in)
424         {
425                 // no vis info, so make all visible
426                 R_OldMarkLights(lightorigin, rd, bit, bitindex, model->nodes + model->hulls[0].firstclipnode);
427                 return;
428         }
429
430         lightframe++;
431
432         low[0] = lightorigin[0] - rd->cullradius;low[1] = lightorigin[1] - rd->cullradius;low[2] = lightorigin[2] - rd->cullradius;
433         high[0] = lightorigin[0] + rd->cullradius;high[1] = lightorigin[1] + rd->cullradius;high[2] = lightorigin[2] + rd->cullradius;
434
435         // for comparisons to minimum acceptable light
436         maxdist = rd->cullradius2;
437
438         row = (model->numleafs+7)>>3;
439
440         k = 0;
441         while (k < row)
442         {
443                 c = *in++;
444                 if (c)
445                 {
446                         for (i = 0;i < 8;i++)
447                         {
448                                 if (c & (1<<i))
449                                 {
450                                         // warning to the clumsy: numleafs is one less than it should be, it only counts leafs with vis bits (skips leaf 0)
451                                         leafnum = (k << 3)+i+1;
452                                         if (leafnum > model->numleafs)
453                                                 return;
454                                         leaf = &model->leafs[leafnum];
455                                         if (leaf->visframe != r_framecount
456                                          || leaf->contents == CONTENTS_SOLID
457                                          || leaf->mins[0] > high[0] || leaf->maxs[0] < low[0]
458                                          || leaf->mins[1] > high[1] || leaf->maxs[1] < low[1]
459                                          || leaf->mins[2] > high[2] || leaf->maxs[2] < low[2])
460                                                 continue;
461                                         if (leaf->dlightframe != r_framecount)
462                                         {
463                                                 // not dynamic until now
464                                                 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;
465                                                 leaf->dlightframe = r_framecount;
466                                         }
467                                         leaf->dlightbits[bitindex] |= bit;
468                                         if ((m = leaf->nummarksurfaces))
469                                         {
470                                                 mark = leaf->firstmarksurface;
471                                                 do
472                                                 {
473                                                         surf = *mark++;
474                                                         // if not visible in current frame, or already marked because it was in another leaf we passed, skip
475                                                         if (surf->lightframe == lightframe)
476                                                                 continue;
477                                                         surf->lightframe = lightframe;
478                                                         if (surf->visframe != r_framecount)
479                                                                 continue;
480                                                         dist = PlaneDiff(lightorigin, surf->plane);
481                                                         if (surf->flags & SURF_PLANEBACK)
482                                                                 dist = -dist;
483                                                         // LordHavoc: make sure it is infront of the surface and not too far away
484                                                         if (dist < rd->cullradius && (dist > -0.25f || ((surf->flags & SURF_LIGHTBOTHSIDES) && dist > -rd->cullradius)))
485                                                         {
486                                                                 int d;
487                                                                 int impacts, impactt;
488                                                                 float dist2, impact[3];
489
490                                                                 dist2 = dist * dist;
491
492                                                                 impact[0] = rd->origin[0] - surf->plane->normal[0] * dist;
493                                                                 impact[1] = rd->origin[1] - surf->plane->normal[1] * dist;
494                                                                 impact[2] = rd->origin[2] - surf->plane->normal[2] * dist;
495
496 #if 0
497                                                                 d = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
498                                                                 if (d < 0)
499                                                                 {
500                                                                         dist2 += d * d;
501                                                                         if (dist2 > maxdist)
502                                                                                 continue;
503                                                                 }
504                                                                 else
505                                                                 {
506                                                                         d -= surf->extents[0];
507                                                                         if (d < 0)
508                                                                         {
509                                                                                 dist2 += d * d;
510                                                                                 if (dist2 > maxdist)
511                                                                                         continue;
512                                                                         }
513                                                                 }
514
515                                                                 d = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
516                                                                 if (d < 0)
517                                                                 {
518                                                                         dist2 += d * d;
519                                                                         if (dist2 > maxdist)
520                                                                                 continue;
521                                                                 }
522                                                                 else
523                                                                 {
524                                                                         d -= surf->extents[1];
525                                                                         if (d < 0)
526                                                                         {
527                                                                                 dist2 += d * d;
528                                                                                 if (dist2 > maxdist)
529                                                                                         continue;
530                                                                         }
531                                                                 }
532
533 #else
534
535                                                                 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
536                                                                 d = bound(0, impacts, surf->extents[0] + 16) - impacts;
537                                                                 dist2 += d * d;
538                                                                 if (dist2 > maxdist)
539                                                                         continue;
540
541                                                                 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
542                                                                 d = bound(0, impactt, surf->extents[1] + 16) - impactt;
543                                                                 dist2 += d * d;
544                                                                 if (dist2 > maxdist)
545                                                                         continue;
546
547 #endif
548
549                                                                 if (surf->dlightframe != r_framecount) // not dynamic until now
550                                                                 {
551                                                                         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;
552                                                                         surf->dlightframe = r_framecount;
553                                                                 }
554                                                                 surf->dlightbits[bitindex] |= bit;
555                                                         }
556                                                 }
557                                                 while (--m);
558                                         }
559                                 }
560                         }
561                         k++;
562                         continue;
563                 }
564
565                 k += *in++;
566         }
567 }
568
569 void R_MarkLights(void)
570 {
571         int i;
572         for (i = 0;i < r_numdlights;i++)
573                 R_VisMarkLights (r_dlight + i, 1 << (i & 31), i >> 5);
574 }
575
576 /*
577 =============================================================================
578
579 LIGHT SAMPLING
580
581 =============================================================================
582 */
583
584 static int RecursiveLightPoint (vec3_t color, mnode_t *node, float x, float y, float startz, float endz)
585 {
586         int             side, distz = endz - startz;
587         float   front, back;
588         float   mid;
589
590 loc0:
591         if (node->contents < 0)
592                 return false;           // didn't hit anything
593
594         switch (node->plane->type)
595         {
596         case PLANE_X:
597                 node = node->children[x < node->plane->dist];
598                 goto loc0;
599         case PLANE_Y:
600                 node = node->children[y < node->plane->dist];
601                 goto loc0;
602         case PLANE_Z:
603                 side = startz < node->plane->dist;
604                 if ((endz < node->plane->dist) == side)
605                 {
606                         node = node->children[side];
607                         goto loc0;
608                 }
609                 // found an intersection
610 //              mid = startz + (endz - startz) * (startz - node->plane->dist) / (startz - endz);
611 //              mid = startz + distz * (startz - node->plane->dist) / (-distz);
612 //              mid = startz + (-(startz - node->plane->dist));
613 //              mid = startz - (startz - node->plane->dist);
614 //              mid = startz + node->plane->dist - startz;
615                 mid = node->plane->dist;
616                 break;
617         default:
618                 back = front = x * node->plane->normal[0] + y * node->plane->normal[1];
619                 front += startz * node->plane->normal[2];
620                 back += endz * node->plane->normal[2];
621                 side = front < node->plane->dist;
622                 if ((back < node->plane->dist) == side)
623                 {
624                         node = node->children[side];
625                         goto loc0;
626                 }
627                 // found an intersection
628 //              mid = startz + (endz - startz) * ((front - node->plane->dist) / ((front - node->plane->dist) - (back - node->plane->dist)));
629 //              mid = startz + (endz - startz) * ((front - node->plane->dist) / (front - back));
630                 mid = startz + distz * (front - node->plane->dist) / (front - back);
631                 break;
632         }
633
634         // go down front side
635         if (node->children[side]->contents >= 0 && RecursiveLightPoint (color, node->children[side], x, y, startz, mid))
636                 return true;    // hit something
637         else
638         {
639                 // check for impact on this node
640                 if (node->numsurfaces)
641                 {
642                         int i, ds, dt;
643                         msurface_t *surf;
644
645                         surf = cl.worldmodel->surfaces + node->firstsurface;
646                         for (i = 0;i < node->numsurfaces;i++, surf++)
647                         {
648                                 if (!(surf->flags & SURF_LIGHTMAP))
649                                         continue;       // no lightmaps
650
651                                 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]);
652                                 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]);
653
654                                 if (ds < surf->texturemins[0] || dt < surf->texturemins[1])
655                                         continue;
656
657                                 ds -= surf->texturemins[0];
658                                 dt -= surf->texturemins[1];
659
660                                 if (ds > surf->extents[0] || dt > surf->extents[1])
661                                         continue;
662
663                                 if (surf->samples)
664                                 {
665                                         qbyte *lightmap;
666                                         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;
667                                         line3 = ((surf->extents[0]>>4)+1)*3;
668                                         size3 = ((surf->extents[0]>>4)+1) * ((surf->extents[1]>>4)+1)*3; // LordHavoc: *3 for colored lighting
669
670                                         lightmap = surf->samples + ((dt>>4) * ((surf->extents[0]>>4)+1) + (ds>>4))*3; // LordHavoc: *3 for color
671
672                                         for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
673                                         {
674                                                 scale = d_lightstylevalue[surf->styles[maps]];
675                                                 r00 += lightmap[      0] * scale;g00 += lightmap[      1] * scale;b00 += lightmap[      2] * scale;
676                                                 r01 += lightmap[      3] * scale;g01 += lightmap[      4] * scale;b01 += lightmap[      5] * scale;
677                                                 r10 += lightmap[line3+0] * scale;g10 += lightmap[line3+1] * scale;b10 += lightmap[line3+2] * scale;
678                                                 r11 += lightmap[line3+3] * scale;g11 += lightmap[line3+4] * scale;b11 += lightmap[line3+5] * scale;
679                                                 lightmap += size3;
680                                         }
681
682                                         /*
683                                         // LordHavoc: here's the readable version of the interpolation
684                                         // code, not quite as easy for the compiler to optimize...
685
686                                         // dsfrac is the X position in the lightmap pixel, * 16
687                                         // dtfrac is the Y position in the lightmap pixel, * 16
688                                         // r00 is top left corner, r01 is top right corner
689                                         // r10 is bottom left corner, r11 is bottom right corner
690                                         // g and b are the same layout.
691                                         // r0 and r1 are the top and bottom intermediate results
692
693                                         // first we interpolate the top two points, to get the top
694                                         // edge sample
695                                         r0 = (((r01-r00) * dsfrac) >> 4) + r00;
696                                         g0 = (((g01-g00) * dsfrac) >> 4) + g00;
697                                         b0 = (((b01-b00) * dsfrac) >> 4) + b00;
698                                         // then we interpolate the bottom two points, to get the
699                                         // bottom edge sample
700                                         r1 = (((r11-r10) * dsfrac) >> 4) + r10;
701                                         g1 = (((g11-g10) * dsfrac) >> 4) + g10;
702                                         b1 = (((b11-b10) * dsfrac) >> 4) + b10;
703                                         // then we interpolate the top and bottom samples to get the
704                                         // middle sample (the one which was requested)
705                                         r = (((r1-r0) * dtfrac) >> 4) + r0;
706                                         g = (((g1-g0) * dtfrac) >> 4) + g0;
707                                         b = (((b1-b0) * dtfrac) >> 4) + b0;
708                                         */
709
710                                         color[0] += (float) ((((((((r11-r10) * dsfrac) >> 4) + r10)-((((r01-r00) * dsfrac) >> 4) + r00)) * dtfrac) >> 4) + ((((r01-r00) * dsfrac) >> 4) + r00)) * (1.0f / 32768.0f);
711                                         color[1] += (float) ((((((((g11-g10) * dsfrac) >> 4) + g10)-((((g01-g00) * dsfrac) >> 4) + g00)) * dtfrac) >> 4) + ((((g01-g00) * dsfrac) >> 4) + g00)) * (1.0f / 32768.0f);
712                                         color[2] += (float) ((((((((b11-b10) * dsfrac) >> 4) + b10)-((((b01-b00) * dsfrac) >> 4) + b00)) * dtfrac) >> 4) + ((((b01-b00) * dsfrac) >> 4) + b00)) * (1.0f / 32768.0f);
713                                 }
714                                 return true; // success
715                         }
716                 }
717
718                 // go down back side
719                 node = node->children[side ^ 1];
720                 startz = mid;
721                 distz = endz - startz;
722                 goto loc0;
723 //              return RecursiveLightPoint (color, node->children[side ^ 1], x, y, mid, endz);
724         }
725 }
726
727 void R_CompleteLightPoint (vec3_t color, vec3_t p, int dynamic, mleaf_t *leaf)
728 {
729         int      i, *dlightbits;
730         vec3_t dist;
731         float f;
732         rdlight_t *rd;
733         mlight_t *sl;
734         if (leaf == NULL)
735                 leaf = Mod_PointInLeaf(p, cl.worldmodel);
736
737         if (leaf->contents == CONTENTS_SOLID)
738         {
739                 color[0] = color[1] = color[2] = 0;
740                 return;
741         }
742
743         if (r_fullbright.integer || !cl.worldmodel->lightdata)
744         {
745                 color[0] = color[1] = color[2] = 2;
746                 return;
747         }
748
749         color[0] = color[1] = color[2] = r_ambient.value * (2.0f / 128.0f);
750         if (cl.worldmodel->numlights)
751         {
752                 for (i = 0;i < cl.worldmodel->numlights;i++)
753                 {
754                         sl = cl.worldmodel->lights + i;
755                         if (d_lightstylevalue[sl->style] > 0)
756                         {
757                                 VectorSubtract (p, sl->origin, dist);
758                                 f = DotProduct(dist, dist) + sl->distbias;
759                                 f = (1.0f / f) - sl->subtract;
760                                 if (f > 0)
761                                 {
762                                         if (TraceLine(p, sl->origin, NULL, NULL, 0, false) == 1)
763                                         {
764                                                 f *= d_lightstylevalue[sl->style] * (1.0f / 32768.0f);
765                                                 VectorMA(color, f, sl->light, color);
766                                         }
767                                 }
768                         }
769
770                 }
771         }
772         else
773                 RecursiveLightPoint (color, cl.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
774
775         if (dynamic && leaf->dlightframe == r_framecount)
776         {
777                 dlightbits = leaf->dlightbits;
778                 for (i = 0;i < r_numdlights;i++)
779                 {
780                         if (!(dlightbits[i >> 5] & (1 << (i & 31))))
781                                 continue;
782                         rd = r_dlight + i;
783                         VectorSubtract (p, rd->origin, dist);
784                         f = DotProduct(dist, dist) + LIGHTOFFSET;
785                         if (f < rd->cullradius2)
786                         {
787                                 f = (1.0f / f) - rd->lightsubtract;
788                                 if (f > 0)
789                                         VectorMA(color, f, rd->light, color);
790                         }
791                 }
792         }
793 }
794
795 void R_ModelLightPoint (vec3_t color, vec3_t p, int *dlightbits)
796 {
797         mleaf_t *leaf;
798         leaf = Mod_PointInLeaf(p, cl.worldmodel);
799         if (leaf->contents == CONTENTS_SOLID)
800         {
801                 color[0] = color[1] = color[2] = 0;
802                 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
803                 return;
804         }
805
806         if (r_fullbright.integer || !cl.worldmodel->lightdata)
807         {
808                 color[0] = color[1] = color[2] = 2;
809                 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
810                 return;
811         }
812
813         color[0] = color[1] = color[2] = r_ambient.value * (2.0f / 128.0f);
814         if (!cl.worldmodel->numlights)
815                 RecursiveLightPoint (color, cl.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
816
817         if (leaf->dlightframe == r_framecount)
818         {
819                 dlightbits[0] = leaf->dlightbits[0];
820                 dlightbits[1] = leaf->dlightbits[1];
821                 dlightbits[2] = leaf->dlightbits[2];
822                 dlightbits[3] = leaf->dlightbits[3];
823                 dlightbits[4] = leaf->dlightbits[4];
824                 dlightbits[5] = leaf->dlightbits[5];
825                 dlightbits[6] = leaf->dlightbits[6];
826                 dlightbits[7] = leaf->dlightbits[7];
827         }
828         else
829                 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
830 }
831
832 void R_LightModel(int numverts, float colorr, float colorg, float colorb, int worldcoords)
833 {
834         int i, j, nearlights = 0;
835         float color[3], basecolor[3], v[3], t, *av, *avn, *avc, a, number, f/*, hardness, hardnessoffset*/, dist2;
836         struct
837         {
838                 vec3_t origin;
839                 vec_t cullradius2;
840                 vec3_t light;
841                 vec_t lightsubtract;
842                 vec_t falloff;
843                 vec_t offset;
844         }
845         nearlight[MAX_DLIGHTS], *nl;
846         int modeldlightbits[8];
847         //staticlight_t *sl;
848         mlight_t *sl;
849         a = currentrenderentity->alpha;
850         if (currentrenderentity->effects & EF_FULLBRIGHT)
851         {
852                 basecolor[0] = colorr;
853                 basecolor[1] = colorg;
854                 basecolor[2] = colorb;
855         }
856         else
857         {
858                 if (r_lightmodels.integer)
859                 {
860                         R_ModelLightPoint(basecolor, currentrenderentity->origin, modeldlightbits);
861
862                         nl = &nearlight[0];
863                         /*
864                         // this code is unused for now
865                         for (i = 0, sl = staticlight;i < staticlights && nearlights < MAX_DLIGHTS;i++, sl++)
866                         {
867                                 if (TraceLine(currentrenderentity->origin, sl->origin, NULL, NULL, 0) == 1)
868                                 {
869                                         nl->fadetype = sl->fadetype;
870                                         nl->distancescale = sl->distancescale;
871                                         nl->radius = sl->radius;
872                                         // transform the light into the model's coordinate system
873                                         if (worldcoords)
874                                                 VectorCopy(sl->origin, nl->origin);
875                                         else
876                                                 softwareuntransform(sl->origin, nl->origin);
877                                         VectorCopy(sl->color, nl->light);
878                                         nl->cullradius2 = 99999999;
879                                         nl->lightsubtract = 0;
880                                         nl++;
881                                         nearlights++;
882                                 }
883                         }
884                         */
885                         // this code is unused for now
886                         for (i = 0, sl = cl.worldmodel->lights;i < cl.worldmodel->numlights && nearlights < MAX_DLIGHTS;i++, sl++)
887                         {
888                                 if (TraceLine(currentrenderentity->origin, sl->origin, NULL, NULL, 0, false) == 1)
889                                 {
890                                         nl->falloff = sl->falloff;
891                                         // transform the light into the model's coordinate system
892                                         if (worldcoords)
893                                                 VectorCopy(sl->origin, nl->origin);
894                                         else
895                                                 softwareuntransform(sl->origin, nl->origin);
896                                         f = d_lightstylevalue[sl->style] * (1.0f / 32768.0f);
897                                         VectorScale(sl->light, f, nl->light);
898                                         nl->cullradius2 = 99999999;
899                                         nl->lightsubtract = sl->subtract;
900                                         nl->offset = sl->distbias;
901                                         nl++;
902                                         nearlights++;
903                                 }
904                         }
905                         for (i = 0;i < r_numdlights && nearlights < MAX_DLIGHTS;i++)
906                         {
907                                 if (!(modeldlightbits[i >> 5] & (1 << (i & 31))))
908                                         continue;
909                                 if (currentrenderentity == r_dlight[i].ent)
910                                 {
911                                         f = (1.0f / LIGHTOFFSET) - nl->lightsubtract;
912                                         if (f > 0)
913                                                 VectorMA(basecolor, f, r_dlight[i].light, basecolor);
914                                 }
915                                 else
916                                 {
917                                         // convert 0-255 radius coloring to 0-1, while also amplifying the brightness by 16
918                                         //if (TraceLine(currentrenderentity->origin, r_dlight[i].origin, NULL, NULL, 0) == 1)
919                                         {
920                                                 // transform the light into the model's coordinate system
921                                                 if (worldcoords)
922                                                         VectorCopy(r_dlight[i].origin, nl->origin);
923                                                 else
924                                                         softwareuntransform(r_dlight[i].origin, nl->origin);
925                                                 nl->cullradius2 = r_dlight[i].cullradius2;
926                                                 nl->light[0] = r_dlight[i].light[0] * colorr;
927                                                 nl->light[1] = r_dlight[i].light[1] * colorg;
928                                                 nl->light[2] = r_dlight[i].light[2] * colorb;
929                                                 nl->lightsubtract = r_dlight[i].lightsubtract;
930                                                 nl->offset = LIGHTOFFSET;
931                                                 nl++;
932                                                 nearlights++;
933                                         }
934                                 }
935                         }
936                 }
937                 else
938                         R_CompleteLightPoint (basecolor, currentrenderentity->origin, true, NULL);
939         }
940         basecolor[0] *= colorr;
941         basecolor[1] *= colorg;
942         basecolor[2] *= colorb;
943         avc = aliasvertcolor;
944         if (nearlights)
945         {
946                 av = aliasvert;
947                 avn = aliasvertnorm;
948                 //hardness = r_lightmodelhardness.value;
949                 //hardnessoffset = (1.0f - hardness);
950                 for (i = 0;i < numverts;i++)
951                 {
952                         VectorCopy(basecolor, color);
953                         for (j = 0, nl = &nearlight[0];j < nearlights;j++, nl++)
954                         {
955                                 // distance attenuation
956                                 VectorSubtract(nl->origin, av, v);
957                                 dist2 = DotProduct(v,v);
958                                 if (dist2 < nl->cullradius2)
959                                 {
960                                         f = (1.0f / (dist2 + nl->offset)) - nl->lightsubtract;
961                                         if (f > 0)
962                                         {
963                                                 // directional shading
964                                                 #if SLOWMATH
965                                                 t = 1.0f / sqrt(dist2);
966                                                 #else
967                                                 number = DotProduct(v, v);
968                                                 *((int *)&t) = 0x5f3759df - ((* (int *) &number) >> 1);
969                                                 t = t * (1.5f - (number * 0.5f * t * t));
970                                                 #endif
971                                                 // DotProduct(avn,v) * t is dotproduct with a normalized v,
972                                                 // the hardness variables are for backlighting/shinyness
973                                                 f *= DotProduct(avn,v) * t;// * hardness + hardnessoffset;
974                                                 if (f > 0)
975                                                         VectorMA(color, f, nl->light, color);
976                                         }
977                                 }
978                         }
979
980                         VectorCopy(color, avc);
981                         avc[3] = a;
982                         avc += 4;
983                         av += 4;
984                         avn += 3;
985                 }
986         }
987         else
988         {
989                 for (i = 0;i < numverts;i++)
990                 {
991                         VectorCopy(basecolor, avc);
992                         avc[3] = a;
993                         avc += 4;
994                 }
995         }
996 }