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