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