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