2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
24 cvar_t r_lightmodels = {0, "r_lightmodels", "1"};
26 void r_light_start(void)
30 void r_light_shutdown(void)
34 void r_light_newmap(void)
38 void R_Light_Init(void)
40 Cvar_RegisterVariable(&r_lightmodels);
41 R_RegisterModule("R_Light", r_light_start, r_light_shutdown, r_light_newmap);
49 void R_AnimateLight (void)
55 // 'm' is normal light, 'a' is no light, 'z' is double bright
56 i = (int)(cl.time*10);
57 for (j=0 ; j<MAX_LIGHTSTYLES ; j++)
59 if (!cl_lightstyle[j].length)
61 d_lightstylevalue[j] = 256;
64 k = i % cl_lightstyle[j].length;
65 k = cl_lightstyle[j].map[k] - 'a';
67 d_lightstylevalue[j] = k;
72 =============================================================================
76 =============================================================================
84 void R_OldMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, mnode_t *node)
94 // for comparisons to minimum acceptable light
95 maxdist = light->radius * light->radius;
97 // clamp radius to avoid exceeding 32768 entry division table
98 if (maxdist > 4194304)
102 if (node->contents < 0)
104 if (node->contents != CONTENTS_SOLID)
106 leaf = (mleaf_t *)node;
107 if (leaf->dlightframe != r_framecount) // not dynamic until now
109 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;
110 leaf->dlightframe = r_framecount;
112 leaf->dlightbits[bitindex] |= bit;
117 ndist = PlaneDiff(lightorigin, node->plane);
119 if (ndist > light->radius)
121 node = node->children[0];
124 if (ndist < -light->radius)
126 node = node->children[1];
131 surf = cl.worldmodel->surfaces + node->firstsurface;
132 for (i=0 ; i<node->numsurfaces ; i++, surf++)
134 int d, impacts, impactt;
135 float dist, dist2, impact[3];
136 if (surf->visframe != r_framecount)
139 if (surf->flags & SURF_PLANEBACK)
142 if (dist < -0.25f && !(surf->flags & SURF_LIGHTBOTHSIDES))
146 if (dist2 >= maxdist)
149 impact[0] = light->origin[0] - surf->plane->normal[0] * dist;
150 impact[1] = light->origin[1] - surf->plane->normal[1] * dist;
151 impact[2] = light->origin[2] - surf->plane->normal[2] * dist;
153 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
155 d = bound(0, impacts, surf->extents[0] + 16) - impacts;
160 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
162 d = bound(0, impactt, surf->extents[1] + 16) - impactt;
169 d = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
174 if (dist2 >= maxdist)
179 d -= surf->extents[0] + 16;
183 if (dist2 >= maxdist)
188 d = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
193 if (dist2 >= maxdist)
198 d -= surf->extents[1] + 16;
202 if (dist2 >= maxdist)
208 if (surf->dlightframe != r_framecount) // not dynamic until now
210 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;
211 surf->dlightframe = r_framecount;
213 surf->dlightbits[bitindex] |= bit;
216 if (((surf->flags & SURF_PLANEBACK) == 0) == ((PlaneDist(lightorigin, surf->plane)) >= surf->plane->dist))
218 if (surf->dlightframe != r_framecount) // not dynamic until now
220 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;
221 surf->dlightframe = r_framecount;
223 surf->dlightbits[bitindex] |= bit;
228 if (node->children[0]->contents >= 0)
230 if (node->children[1]->contents >= 0)
232 R_OldMarkLights (lightorigin, light, bit, bitindex, node->children[0]);
233 node = node->children[1];
238 node = node->children[0];
242 else if (node->children[1]->contents >= 0)
244 node = node->children[1];
249 void R_NoVisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model)
251 R_OldMarkLights(lightorigin, light, bit, bitindex, model->nodes + model->hulls[0].firstclipnode);
254 void R_VisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model)
256 static int lightframe = 0;
257 mleaf_t *pvsleaf = Mod_PointInLeaf (lightorigin, model);
259 if (!r_dynamic.value)
262 if (!pvsleaf->compressed_vis)
263 { // no vis info, so make all visible
264 R_OldMarkLights(lightorigin, light, bit, bitindex, model->nodes + model->hulls[0].firstclipnode);
269 int i, k, m, c, leafnum;
270 msurface_t *surf, **mark;
272 byte *in = pvsleaf->compressed_vis;
273 int row = (model->numleafs+7)>>3;
274 float low[3], high[3], radius, dist, maxdist;
278 radius = light->radius * 3;
280 // clamp radius to avoid exceeding 32768 entry division table
284 low[0] = lightorigin[0] - radius;low[1] = lightorigin[1] - radius;low[2] = lightorigin[2] - radius;
285 high[0] = lightorigin[0] + radius;high[1] = lightorigin[1] + radius;high[2] = lightorigin[2] + radius;
287 // for comparisons to minimum acceptable light
288 maxdist = radius*radius;
296 for (i = 0;i < 8;i++)
300 // warning to the clumsy: numleafs is one less than it should be, it only counts leafs with vis bits (skips leaf 0)
301 leafnum = (k << 3)+i+1;
302 if (leafnum > model->numleafs)
304 leaf = &model->leafs[leafnum];
305 // if (leaf->visframe != r_framecount)
307 // if (leaf->contents == CONTENTS_SOLID)
309 // if out of the light radius, skip
310 if (leaf->mins[0] > high[0] || leaf->maxs[0] < low[0]
311 || leaf->mins[1] > high[1] || leaf->maxs[1] < low[1]
312 || leaf->mins[2] > high[2] || leaf->maxs[2] < low[2])
314 if (leaf->dlightframe != r_framecount) // not dynamic until now
316 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;
317 leaf->dlightframe = r_framecount;
319 leaf->dlightbits[bitindex] |= bit;
320 if ((m = leaf->nummarksurfaces))
322 mark = leaf->firstmarksurface;
326 // if not visible in current frame, or already marked because it was in another leaf we passed, skip
327 if (surf->lightframe == lightframe)
329 surf->lightframe = lightframe;
330 if (surf->visframe != r_framecount)
332 dist = PlaneDiff(lightorigin, surf->plane);
333 if (surf->flags & SURF_PLANEBACK)
335 // LordHavoc: make sure it is infront of the surface and not too far away
336 if (dist < radius && (dist > -0.25f || ((surf->flags & SURF_LIGHTBOTHSIDES) && dist > -radius)))
338 int d, impacts, impactt;
339 float dist2, impact[3];
343 impact[0] = light->origin[0] - surf->plane->normal[0] * dist;
344 impact[1] = light->origin[1] - surf->plane->normal[1] * dist;
345 impact[2] = light->origin[2] - surf->plane->normal[2] * dist;
347 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
349 d = bound(0, impacts, surf->extents[0] + 16) - impacts;
354 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
356 d = bound(0, impactt, surf->extents[1] + 16) - impactt;
362 d = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
367 if (dist2 >= maxdist)
372 d -= surf->extents[0] + 16;
376 if (dist2 >= maxdist)
381 d = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
386 if (dist2 >= maxdist)
391 d -= surf->extents[1] + 16;
395 if (dist2 >= maxdist)
401 if (surf->dlightframe != r_framecount) // not dynamic until now
403 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;
404 surf->dlightframe = r_framecount;
406 surf->dlightbits[bitindex] |= bit;
428 void R_PushDlights (void)
433 if (!r_dynamic.value)
438 for (i=0 ; i<MAX_DLIGHTS ; i++, l++)
442 // R_MarkLights (l->origin, l, 1<<(i&31), i >> 5, cl.worldmodel->nodes );
443 R_VisMarkLights (l->origin, l, 1<<(i&31), i >> 5, cl.worldmodel);
451 =============================================================================
455 =============================================================================
458 mplane_t *lightplane;
462 int RecursiveLightPoint (vec3_t color, mnode_t *node, vec3_t start, vec3_t end)
464 float front, back, frac;
468 if (node->contents < 0)
469 return false; // didn't hit anything
471 // calculate mid point
472 front = PlaneDiff (start, node->plane);
473 back = PlaneDiff (end, node->plane);
475 // LordHavoc: optimized recursion
476 if ((back < 0) == (front < 0))
477 // return RecursiveLightPoint (color, node->children[front < 0], start, end);
479 node = node->children[front < 0];
483 frac = front / (front-back);
484 mid[0] = start[0] + (end[0] - start[0])*frac;
485 mid[1] = start[1] + (end[1] - start[1])*frac;
486 mid[2] = start[2] + (end[2] - start[2])*frac;
488 // go down front side
489 if (RecursiveLightPoint (color, node->children[front < 0], start, mid))
490 return true; // hit something
495 // check for impact on this node
496 VectorCopy (mid, lightspot);
497 lightplane = node->plane;
499 surf = cl.worldmodel->surfaces + node->firstsurface;
500 for (i = 0;i < node->numsurfaces;i++, surf++)
502 if (surf->flags & SURF_DRAWTILED)
503 continue; // no lightmaps
505 ds = (int) ((float) DotProduct (mid, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3]);
506 dt = (int) ((float) DotProduct (mid, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3]);
508 if (ds < surf->texturemins[0] || dt < surf->texturemins[1])
511 ds -= surf->texturemins[0];
512 dt -= surf->texturemins[1];
514 if (ds > surf->extents[0] || dt > surf->extents[1])
520 int maps, line3, dsfrac = ds & 15, dtfrac = dt & 15, r00 = 0, g00 = 0, b00 = 0, r01 = 0, g01 = 0, b01 = 0, r10 = 0, g10 = 0, b10 = 0, r11 = 0, g11 = 0, b11 = 0;
522 line3 = ((surf->extents[0]>>4)+1)*3;
524 lightmap = surf->samples + ((dt>>4) * ((surf->extents[0]>>4)+1) + (ds>>4))*3; // LordHavoc: *3 for color
526 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
528 scale = (float) d_lightstylevalue[surf->styles[maps]] * 1.0 / 256.0;
529 r00 += (float) lightmap[ 0] * scale;g00 += (float) lightmap[ 1] * scale;b00 += (float) lightmap[2] * scale;
530 r01 += (float) lightmap[ 3] * scale;g01 += (float) lightmap[ 4] * scale;b01 += (float) lightmap[5] * scale;
531 r10 += (float) lightmap[line3+0] * scale;g10 += (float) lightmap[line3+1] * scale;b10 += (float) lightmap[line3+2] * scale;
532 r11 += (float) lightmap[line3+3] * scale;g11 += (float) lightmap[line3+4] * scale;b11 += (float) lightmap[line3+5] * scale;
533 lightmap += ((surf->extents[0]>>4)+1) * ((surf->extents[1]>>4)+1)*3; // LordHavoc: *3 for colored lighting
536 color[0] += (float) ((int) ((((((((r11-r10) * dsfrac) >> 4) + r10)-((((r01-r00) * dsfrac) >> 4) + r00)) * dtfrac) >> 4) + ((((r01-r00) * dsfrac) >> 4) + r00)));
537 color[1] += (float) ((int) ((((((((g11-g10) * dsfrac) >> 4) + g10)-((((g01-g00) * dsfrac) >> 4) + g00)) * dtfrac) >> 4) + ((((g01-g00) * dsfrac) >> 4) + g00)));
538 color[2] += (float) ((int) ((((((((b11-b10) * dsfrac) >> 4) + b10)-((((b01-b00) * dsfrac) >> 4) + b00)) * dtfrac) >> 4) + ((((b01-b00) * dsfrac) >> 4) + b00)));
540 return true; // success
544 return RecursiveLightPoint (color, node->children[front >= 0], mid, end);
548 void R_LightPoint (vec3_t color, vec3_t p)
552 if (r_fullbright.value || !cl.worldmodel->lightdata)
554 color[0] = color[1] = color[2] = 255;
560 end[2] = p[2] - 2048;
562 color[0] = color[1] = color[2] = r_ambient.value * 2.0f;
563 RecursiveLightPoint (color, cl.worldmodel->nodes, p, end);
566 void SV_LightPoint (vec3_t color, vec3_t p)
570 if (!sv.worldmodel->lightdata)
572 color[0] = color[1] = color[2] = 255;
578 end[2] = p[2] - 2048;
580 color[0] = color[1] = color[2] = 0;
581 RecursiveLightPoint (color, sv.worldmodel->nodes, p, end);
585 int RecursiveLightPoint (vec3_t color, mnode_t *node, float x, float y, float startz, float endz)
587 int side, distz = endz - startz;
592 if (node->contents < 0)
593 return false; // didn't hit anything
595 switch (node->plane->type)
598 node = node->children[x < node->plane->dist];
601 node = node->children[y < node->plane->dist];
604 side = startz < node->plane->dist;
605 if ((endz < node->plane->dist) == side)
607 node = node->children[side];
610 // found an intersection
611 // mid = startz + (endz - startz) * (startz - node->plane->dist) / (startz - endz);
612 // mid = startz + distz * (startz - node->plane->dist) / (-distz);
613 // mid = startz + (-(startz - node->plane->dist));
614 // mid = startz - (startz - node->plane->dist);
615 // mid = startz + node->plane->dist - startz;
616 mid = node->plane->dist;
619 back = front = x * node->plane->normal[0] + y * node->plane->normal[1];
620 front += startz * node->plane->normal[2];
621 back += endz * node->plane->normal[2];
622 side = front < node->plane->dist;
623 if ((back < node->plane->dist) == side)
625 node = node->children[side];
628 // found an intersection
629 // mid = startz + (endz - startz) * ((front - node->plane->dist) / ((front - node->plane->dist) - (back - node->plane->dist)));
630 // mid = startz + (endz - startz) * ((front - node->plane->dist) / (front - back));
631 mid = startz + distz * (front - node->plane->dist) / (front - back);
635 // go down front side
636 if (node->children[side]->contents >= 0 && RecursiveLightPoint (color, node->children[side], x, y, startz, mid))
637 return true; // hit something
640 // check for impact on this node
641 if (node->numsurfaces)
648 lightplane = node->plane;
650 surf = cl.worldmodel->surfaces + node->firstsurface;
651 for (i = 0;i < node->numsurfaces;i++, surf++)
653 if (surf->flags & SURF_DRAWTILED)
654 continue; // no lightmaps
656 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]);
657 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]);
659 if (ds < surf->texturemins[0] || dt < surf->texturemins[1])
662 ds -= surf->texturemins[0];
663 dt -= surf->texturemins[1];
665 if (ds > surf->extents[0] || dt > surf->extents[1])
671 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;
672 line3 = ((surf->extents[0]>>4)+1)*3;
673 size3 = ((surf->extents[0]>>4)+1) * ((surf->extents[1]>>4)+1)*3; // LordHavoc: *3 for colored lighting
675 lightmap = surf->samples + ((dt>>4) * ((surf->extents[0]>>4)+1) + (ds>>4))*3; // LordHavoc: *3 for color
677 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
679 scale = d_lightstylevalue[surf->styles[maps]];
680 r00 += lightmap[ 0] * scale;g00 += lightmap[ 1] * scale;b00 += lightmap[ 2] * scale;
681 r01 += lightmap[ 3] * scale;g01 += lightmap[ 4] * scale;b01 += lightmap[ 5] * scale;
682 r10 += lightmap[line3+0] * scale;g10 += lightmap[line3+1] * scale;b10 += lightmap[line3+2] * scale;
683 r11 += lightmap[line3+3] * scale;g11 += lightmap[line3+4] * scale;b11 += lightmap[line3+5] * scale;
688 // LordHavoc: here's the readable version of the interpolation
689 // code, not quite as easy for the compiler to optimize...
691 // dsfrac is the X position in the lightmap pixel, * 16
692 // dtfrac is the Y position in the lightmap pixel, * 16
693 // r00 is top left corner, r01 is top right corner
694 // r10 is bottom left corner, r11 is bottom right corner
695 // g and b are the same layout.
696 // r0 and r1 are the top and bottom intermediate results
698 // first we interpolate the top two points, to get the top
700 r0 = (((r01-r00) * dsfrac) >> 4) + r00;
701 g0 = (((g01-g00) * dsfrac) >> 4) + g00;
702 b0 = (((b01-b00) * dsfrac) >> 4) + b00;
703 // then we interpolate the bottom two points, to get the
704 // bottom edge sample
705 r1 = (((r11-r10) * dsfrac) >> 4) + r10;
706 g1 = (((g11-g10) * dsfrac) >> 4) + g10;
707 b1 = (((b11-b10) * dsfrac) >> 4) + b10;
708 // then we interpolate the top and bottom samples to get the
709 // middle sample (the one which was requested)
710 r = (((r1-r0) * dtfrac) >> 4) + r0;
711 g = (((g1-g0) * dtfrac) >> 4) + g0;
712 b = (((b1-b0) * dtfrac) >> 4) + b0;
715 color[0] += (float) ((((((((r11-r10) * dsfrac) >> 4) + r10)-((((r01-r00) * dsfrac) >> 4) + r00)) * dtfrac) >> 4) + ((((r01-r00) * dsfrac) >> 4) + r00)) * (1.0f / 256.0f);
716 color[1] += (float) ((((((((g11-g10) * dsfrac) >> 4) + g10)-((((g01-g00) * dsfrac) >> 4) + g00)) * dtfrac) >> 4) + ((((g01-g00) * dsfrac) >> 4) + g00)) * (1.0f / 256.0f);
717 color[2] += (float) ((((((((b11-b10) * dsfrac) >> 4) + b10)-((((b01-b00) * dsfrac) >> 4) + b00)) * dtfrac) >> 4) + ((((b01-b00) * dsfrac) >> 4) + b00)) * (1.0f / 256.0f);
719 return true; // success
724 node = node->children[side ^ 1];
726 distz = endz - startz;
728 // return RecursiveLightPoint (color, node->children[side ^ 1], x, y, mid, endz);
732 void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits)
736 float brightness, r, f;
738 if (!r_dynamic.value || (!dlightbits[0] && !dlightbits[1] && !dlightbits[2] && !dlightbits[3] && !dlightbits[4] && !dlightbits[5] && !dlightbits[6] && !dlightbits[7]))
741 for (j = 0;j < (MAX_DLIGHTS >> 5);j++)
745 for (i=0 ; i<32 ; i++)
747 if (!((1 << i) & dlightbits[j]))
750 if (!cl_dlights[k].radius)
752 VectorSubtract (org, cl_dlights[k].origin, dist);
753 f = DotProduct(dist, dist) + LIGHTOFFSET;
754 r = cl_dlights[k].radius*cl_dlights[k].radius;
757 brightness = r * 128.0f / f;
758 color[0] += brightness * cl_dlights[k].color[0];
759 color[1] += brightness * cl_dlights[k].color[1];
760 color[2] += brightness * cl_dlights[k].color[2];
767 void R_CompleteLightPoint (vec3_t color, vec3_t p, int dynamic, mleaf_t *leaf)
770 leaf = Mod_PointInLeaf(p, cl.worldmodel);
772 if (leaf->contents == CONTENTS_SOLID)
774 color[0] = color[1] = color[2] = 0;
778 if (r_fullbright.value || !cl.worldmodel->lightdata)
780 color[0] = color[1] = color[2] = 255;
784 color[0] = color[1] = color[2] = r_ambient.value * 2.0f;
785 RecursiveLightPoint (color, cl.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
788 R_DynamicLightPoint(color, p, leaf->dlightbits);
791 void R_ModelLightPoint (vec3_t color, vec3_t p, int *dlightbits)
794 leaf = Mod_PointInLeaf(p, cl.worldmodel);
795 if (leaf->contents == CONTENTS_SOLID)
797 color[0] = color[1] = color[2] = 0;
798 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
802 if (r_fullbright.value || !cl.worldmodel->lightdata)
804 color[0] = color[1] = color[2] = 255;
805 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
809 color[0] = color[1] = color[2] = r_ambient.value * 2.0f;
810 RecursiveLightPoint (color, cl.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
812 if (leaf->dlightframe == r_framecount)
814 dlightbits[0] = leaf->dlightbits[0];
815 dlightbits[1] = leaf->dlightbits[1];
816 dlightbits[2] = leaf->dlightbits[2];
817 dlightbits[3] = leaf->dlightbits[3];
818 dlightbits[4] = leaf->dlightbits[4];
819 dlightbits[5] = leaf->dlightbits[5];
820 dlightbits[6] = leaf->dlightbits[6];
821 dlightbits[7] = leaf->dlightbits[7];
824 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
827 /* // not currently used
828 void R_DynamicLightPointNoMask(vec3_t color, vec3_t org)
832 float brightness, r, f;
834 if (!r_dynamic.value)
837 for (i=0 ; i<MAX_DLIGHTS ; i++)
839 if (!cl_dlights[i].radius)
841 VectorSubtract (org, cl_dlights[i].origin, dist);
842 f = DotProduct(dist, dist) + LIGHTOFFSET;
843 r = cl_dlights[i].radius*cl_dlights[i].radius;
846 brightness = r * 256.0f / f;
847 if (cl_dlights[i].dark)
848 brightness = -brightness;
849 color[0] += brightness * cl_dlights[i].color[0];
850 color[1] += brightness * cl_dlights[i].color[1];
851 color[2] += brightness * cl_dlights[i].color[2];
857 void R_LightModel(int numverts)
859 // LordHavoc: warning: reliance on int being 4 bytes here (of course the d_8to24table relies on that too...)
860 int i, j, nearlights = 0, color;
861 vec3_t dist, mod, basecolor, center;
862 float t, t1, t2, t3, *avn, number;
868 } nearlight[MAX_DLIGHTS];
869 int modeldlightbits[8];
870 avc = aliasvertcolor;
872 VectorCopy(currentrenderentity->origin, center);
873 a = (byte) bound((int) 0, (int) (currentrenderentity->alpha * 255.0f), (int) 255);
876 mod[0] = currentrenderentity->colormod[0] * 0.5f;
877 mod[1] = currentrenderentity->colormod[1] * 0.5f;
878 mod[2] = currentrenderentity->colormod[2] * 0.5f;
882 mod[0] = currentrenderentity->colormod[0];
883 mod[1] = currentrenderentity->colormod[1];
884 mod[2] = currentrenderentity->colormod[2];
886 if (currentrenderentity->effects & EF_FULLBRIGHT)
889 memset(avc, 0, 4 * numverts);
892 ((byte *)&color)[0] = (byte) (255.0f * mod[0]);
893 ((byte *)&color)[1] = (byte) (255.0f * mod[1]);
894 ((byte *)&color)[2] = (byte) (255.0f * mod[2]);
895 ((byte *)&color)[3] = a;
896 for (i = 0;i < numverts;i++)
898 *((int *)avc) = color;
904 R_ModelLightPoint(basecolor, center, modeldlightbits);
906 basecolor[0] *= mod[0];
907 basecolor[1] *= mod[1];
908 basecolor[2] *= mod[2];
909 for (i = 0;i < MAX_DLIGHTS;i++)
911 if (!modeldlightbits[i >> 5])
916 if (!(modeldlightbits[i >> 5] & (1 << (i & 31))))
918 VectorSubtract (center, cl_dlights[i].origin, dist);
919 t2 = DotProduct(dist,dist) + LIGHTOFFSET;
920 t1 = cl_dlights[i].radius*cl_dlights[i].radius;
923 if (TraceLine(center, cl_dlights[i].origin, NULL, NULL, 0))
925 // transform the light into the model's coordinate system
926 if (gl_transform.value)
927 softwareuntransform(cl_dlights[i].origin, nearlight[nearlights].origin);
929 VectorCopy(cl_dlights[i].origin, nearlight[nearlights].origin);
930 nearlight[nearlights].color[0] = cl_dlights[i].color[0] * t1 * mod[0];
931 nearlight[nearlights].color[1] = cl_dlights[i].color[1] * t1 * mod[1];
932 nearlight[nearlights].color[2] = cl_dlights[i].color[2] * t1 * mod[2];
933 if (r_lightmodels.value && currentrenderentity != cl_dlights[i].ent)
935 // boost color, to compensate for dark lighting calcs
936 VectorScale(nearlight[nearlights].color, cl_dlights[i].radius * 16.0f, nearlight[nearlights].color);
942 t1 = 1.0f / sqrt(t2);
945 *((long *)&t1) = 0x5f3759df - ((* (long *) &number) >> 1);
946 t1 = t1 * (1.5f - (number * 0.5f * t1 * t1));
949 basecolor[0] += nearlight[nearlights].color[0] * t1;
950 basecolor[1] += nearlight[nearlights].color[1] * t1;
951 basecolor[2] += nearlight[nearlights].color[2] * t1;
956 t1 = bound(0, basecolor[0], 255);r = (byte) t1;
957 t1 = bound(0, basecolor[1], 255);g = (byte) t1;
958 t1 = bound(0, basecolor[2], 255);b = (byte) t1;
959 ((byte *)&color)[0] = r;
960 ((byte *)&color)[1] = g;
961 ((byte *)&color)[2] = b;
962 ((byte *)&color)[3] = a;
969 for (i = 0;i < numverts;i++)
974 for (j = 0;j < nearlights;j++)
976 VectorSubtract(nearlight[j].origin, av, v);
977 t = DotProduct(avn,v);
981 t = 1.0f / sqrt(DotProduct(v,v) + 1.0f);
983 number = DotProduct(v, v) + LIGHTOFFSET;
984 *((long *)&t) = 0x5f3759df - ((* (long *) &number) >> 1);
985 t = t * (1.5f - (number * 0.5f * t * t));
988 t1 += nearlight[j].color[0] * t;
989 t2 += nearlight[j].color[1] * t;
990 t3 += nearlight[j].color[2] * t;
994 // FIXME: float to int conversions are very slow on x86 because of
995 // mode switchs (switch from nearest rounding to down, then back,
996 // each time), reimplement this part in assembly, SSE and 3DNow!
997 // versions recommended as well
1003 // later note: implemented bit hacking float to integer,
1004 // probably makes the issue irrelevant
1006 i1 = *((long *)&t1) & 0x007FFFFF;
1008 i2 = *((long *)&t2) & 0x007FFFFF;
1010 i3 = *((long *)&t3) & 0x007FFFFF;
1013 avc[0] = bound(0, i1, 255);
1014 avc[1] = bound(0, i2, 255);
1015 avc[2] = bound(0, i3, 255);
1024 for (i = 0;i < numverts;i++)
1026 *((int *)avc) = color;