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