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++)
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 d = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
158 if (dist2 >= maxdist)
163 d -= surf->extents[0] + 16;
167 if (dist2 >= maxdist)
172 d = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
177 if (dist2 >= maxdist)
182 d -= surf->extents[1] + 16;
186 if (dist2 >= maxdist)
191 if (surf->dlightframe != r_framecount) // not dynamic until now
193 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;
194 surf->dlightframe = r_framecount;
196 surf->dlightbits[bitindex] |= bit;
199 if (((surf->flags & SURF_PLANEBACK) == 0) == ((PlaneDist(lightorigin, surf->plane)) >= surf->plane->dist))
201 if (surf->dlightframe != r_framecount) // not dynamic until now
203 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;
204 surf->dlightframe = r_framecount;
206 surf->dlightbits[bitindex] |= bit;
211 if (node->children[0]->contents >= 0)
213 if (node->children[1]->contents >= 0)
215 R_OldMarkLights (lightorigin, light, bit, bitindex, node->children[0]);
216 node = node->children[1];
221 node = node->children[0];
225 else if (node->children[1]->contents >= 0)
227 node = node->children[1];
232 void R_NoVisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model)
234 R_OldMarkLights(lightorigin, light, bit, bitindex, model->nodes + model->hulls[0].firstclipnode);
237 void R_VisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model)
239 static int lightframe = 0;
240 mleaf_t *pvsleaf = Mod_PointInLeaf (lightorigin, model);
242 if (!r_dynamic.value)
245 if (!pvsleaf->compressed_vis)
246 { // no vis info, so make all visible
247 R_OldMarkLights(lightorigin, light, bit, bitindex, model->nodes + model->hulls[0].firstclipnode);
252 int i, k, m, c, leafnum;
253 msurface_t *surf, **mark;
255 byte *in = pvsleaf->compressed_vis;
256 int row = (model->numleafs+7)>>3;
257 float low[3], high[3], radius, dist, maxdist;
261 radius = light->radius * 3;
263 // clamp radius to avoid exceeding 32768 entry division table
267 low[0] = lightorigin[0] - radius;low[1] = lightorigin[1] - radius;low[2] = lightorigin[2] - radius;
268 high[0] = lightorigin[0] + radius;high[1] = lightorigin[1] + radius;high[2] = lightorigin[2] + radius;
270 // for comparisons to minimum acceptable light
271 maxdist = radius*radius;
279 for (i = 0;i < 8;i++)
283 leafnum = (k << 3)+i+1;
284 if (leafnum > model->numleafs)
286 leaf = &model->leafs[leafnum];
287 // if (leaf->visframe != r_framecount)
289 // if (leaf->contents == CONTENTS_SOLID)
291 // if out of the light radius, skip
292 if (leaf->mins[0] > high[0] || leaf->maxs[0] < low[0]
293 || leaf->mins[1] > high[1] || leaf->maxs[1] < low[1]
294 || leaf->mins[2] > high[2] || leaf->maxs[2] < low[2])
296 if (leaf->dlightframe != r_framecount) // not dynamic until now
298 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;
299 leaf->dlightframe = r_framecount;
301 leaf->dlightbits[bitindex] |= bit;
302 if ((m = leaf->nummarksurfaces))
304 mark = leaf->firstmarksurface;
308 // if not visible in current frame, or already marked because it was in another leaf we passed, skip
309 if (surf->lightframe == lightframe)
311 surf->lightframe = lightframe;
312 if (surf->visframe != r_framecount)
314 dist = PlaneDiff(lightorigin, surf->plane);
315 if (surf->flags & SURF_PLANEBACK)
317 // LordHavoc: make sure it is infront of the surface and not too far away
318 if (dist < radius && (dist > -0.25f || ((surf->flags & SURF_LIGHTBOTHSIDES) && dist > -radius)))
321 float dist2, impact[3];
325 impact[0] = light->origin[0] - surf->plane->normal[0] * dist;
326 impact[1] = light->origin[1] - surf->plane->normal[1] * dist;
327 impact[2] = light->origin[2] - surf->plane->normal[2] * dist;
329 d = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
334 if (dist2 >= maxdist)
339 d -= surf->extents[0] + 16;
343 if (dist2 >= maxdist)
348 d = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
353 if (dist2 >= maxdist)
358 d -= surf->extents[1] + 16;
362 if (dist2 >= maxdist)
367 if (surf->dlightframe != r_framecount) // not dynamic until now
369 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;
370 surf->dlightframe = r_framecount;
372 surf->dlightbits[bitindex] |= bit;
394 void R_PushDlights (void)
399 if (!r_dynamic.value)
404 for (i=0 ; i<MAX_DLIGHTS ; i++, l++)
408 // R_MarkLights (l->origin, l, 1<<(i&31), i >> 5, cl.worldmodel->nodes );
409 R_VisMarkLights (l->origin, l, 1<<(i&31), i >> 5, cl.worldmodel);
417 =============================================================================
421 =============================================================================
424 mplane_t *lightplane;
428 int RecursiveLightPoint (vec3_t color, mnode_t *node, vec3_t start, vec3_t end)
430 float front, back, frac;
434 if (node->contents < 0)
435 return false; // didn't hit anything
437 // calculate mid point
438 front = PlaneDiff (start, node->plane);
439 back = PlaneDiff (end, node->plane);
441 // LordHavoc: optimized recursion
442 if ((back < 0) == (front < 0))
443 // return RecursiveLightPoint (color, node->children[front < 0], start, end);
445 node = node->children[front < 0];
449 frac = front / (front-back);
450 mid[0] = start[0] + (end[0] - start[0])*frac;
451 mid[1] = start[1] + (end[1] - start[1])*frac;
452 mid[2] = start[2] + (end[2] - start[2])*frac;
454 // go down front side
455 if (RecursiveLightPoint (color, node->children[front < 0], start, mid))
456 return true; // hit something
461 // check for impact on this node
462 VectorCopy (mid, lightspot);
463 lightplane = node->plane;
465 surf = cl.worldmodel->surfaces + node->firstsurface;
466 for (i = 0;i < node->numsurfaces;i++, surf++)
468 if (surf->flags & SURF_DRAWTILED)
469 continue; // no lightmaps
471 ds = (int) ((float) DotProduct (mid, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3]);
472 dt = (int) ((float) DotProduct (mid, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3]);
474 if (ds < surf->texturemins[0] || dt < surf->texturemins[1])
477 ds -= surf->texturemins[0];
478 dt -= surf->texturemins[1];
480 if (ds > surf->extents[0] || dt > surf->extents[1])
486 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;
488 line3 = ((surf->extents[0]>>4)+1)*3;
490 lightmap = surf->samples + ((dt>>4) * ((surf->extents[0]>>4)+1) + (ds>>4))*3; // LordHavoc: *3 for color
492 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
494 scale = (float) d_lightstylevalue[surf->styles[maps]] * 1.0 / 256.0;
495 r00 += (float) lightmap[ 0] * scale;g00 += (float) lightmap[ 1] * scale;b00 += (float) lightmap[2] * scale;
496 r01 += (float) lightmap[ 3] * scale;g01 += (float) lightmap[ 4] * scale;b01 += (float) lightmap[5] * scale;
497 r10 += (float) lightmap[line3+0] * scale;g10 += (float) lightmap[line3+1] * scale;b10 += (float) lightmap[line3+2] * scale;
498 r11 += (float) lightmap[line3+3] * scale;g11 += (float) lightmap[line3+4] * scale;b11 += (float) lightmap[line3+5] * scale;
499 lightmap += ((surf->extents[0]>>4)+1) * ((surf->extents[1]>>4)+1)*3; // LordHavoc: *3 for colored lighting
502 color[0] += (float) ((int) ((((((((r11-r10) * dsfrac) >> 4) + r10)-((((r01-r00) * dsfrac) >> 4) + r00)) * dtfrac) >> 4) + ((((r01-r00) * dsfrac) >> 4) + r00)));
503 color[1] += (float) ((int) ((((((((g11-g10) * dsfrac) >> 4) + g10)-((((g01-g00) * dsfrac) >> 4) + g00)) * dtfrac) >> 4) + ((((g01-g00) * dsfrac) >> 4) + g00)));
504 color[2] += (float) ((int) ((((((((b11-b10) * dsfrac) >> 4) + b10)-((((b01-b00) * dsfrac) >> 4) + b00)) * dtfrac) >> 4) + ((((b01-b00) * dsfrac) >> 4) + b00)));
506 return true; // success
510 return RecursiveLightPoint (color, node->children[front >= 0], mid, end);
514 void R_LightPoint (vec3_t color, vec3_t p)
518 if (r_fullbright.value || !cl.worldmodel->lightdata)
520 color[0] = color[1] = color[2] = 255;
526 end[2] = p[2] - 2048;
528 color[0] = color[1] = color[2] = r_ambient.value * 2.0f;
529 RecursiveLightPoint (color, cl.worldmodel->nodes, p, end);
532 void SV_LightPoint (vec3_t color, vec3_t p)
536 if (!sv.worldmodel->lightdata)
538 color[0] = color[1] = color[2] = 255;
544 end[2] = p[2] - 2048;
546 color[0] = color[1] = color[2] = 0;
547 RecursiveLightPoint (color, sv.worldmodel->nodes, p, end);
551 int RecursiveLightPoint (vec3_t color, mnode_t *node, float x, float y, float startz, float endz)
553 int side, distz = endz - startz;
558 if (node->contents < 0)
559 return false; // didn't hit anything
561 switch (node->plane->type)
564 node = node->children[x < node->plane->dist];
567 node = node->children[y < node->plane->dist];
570 side = startz < node->plane->dist;
571 if ((endz < node->plane->dist) == side)
573 node = node->children[side];
576 // found an intersection
577 // mid = startz + (endz - startz) * (startz - node->plane->dist) / (startz - endz);
578 // mid = startz + distz * (startz - node->plane->dist) / (-distz);
579 // mid = startz + (-(startz - node->plane->dist));
580 // mid = startz - (startz - node->plane->dist);
581 // mid = startz + node->plane->dist - startz;
582 mid = node->plane->dist;
585 back = front = x * node->plane->normal[0] + y * node->plane->normal[1];
586 front += startz * node->plane->normal[2];
587 back += endz * node->plane->normal[2];
588 side = front < node->plane->dist;
589 if ((back < node->plane->dist) == side)
591 node = node->children[side];
594 // found an intersection
595 // mid = startz + (endz - startz) * ((front - node->plane->dist) / ((front - node->plane->dist) - (back - node->plane->dist)));
596 // mid = startz + (endz - startz) * ((front - node->plane->dist) / (front - back));
597 mid = startz + distz * (front - node->plane->dist) / (front - back);
601 // go down front side
602 if (node->children[side]->contents >= 0 && RecursiveLightPoint (color, node->children[side], x, y, startz, mid))
603 return true; // hit something
606 // check for impact on this node
607 if (node->numsurfaces)
614 lightplane = node->plane;
616 surf = cl.worldmodel->surfaces + node->firstsurface;
617 for (i = 0;i < node->numsurfaces;i++, surf++)
619 if (surf->flags & SURF_DRAWTILED)
620 continue; // no lightmaps
622 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]);
623 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]);
625 if (ds < surf->texturemins[0] || dt < surf->texturemins[1])
628 ds -= surf->texturemins[0];
629 dt -= surf->texturemins[1];
631 if (ds > surf->extents[0] || dt > surf->extents[1])
637 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;
638 line3 = ((surf->extents[0]>>4)+1)*3;
639 size3 = ((surf->extents[0]>>4)+1) * ((surf->extents[1]>>4)+1)*3; // LordHavoc: *3 for colored lighting
641 lightmap = surf->samples + ((dt>>4) * ((surf->extents[0]>>4)+1) + (ds>>4))*3; // LordHavoc: *3 for color
643 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
645 scale = d_lightstylevalue[surf->styles[maps]];
646 r00 += lightmap[ 0] * scale;g00 += lightmap[ 1] * scale;b00 += lightmap[ 2] * scale;
647 r01 += lightmap[ 3] * scale;g01 += lightmap[ 4] * scale;b01 += lightmap[ 5] * scale;
648 r10 += lightmap[line3+0] * scale;g10 += lightmap[line3+1] * scale;b10 += lightmap[line3+2] * scale;
649 r11 += lightmap[line3+3] * scale;g11 += lightmap[line3+4] * scale;b11 += lightmap[line3+5] * scale;
654 // LordHavoc: here's the readable version of the interpolation
655 // code, not quite as easy for the compiler to optimize...
657 // dsfrac is the X position in the lightmap pixel, * 16
658 // dtfrac is the Y position in the lightmap pixel, * 16
659 // r00 is top left corner, r01 is top right corner
660 // r10 is bottom left corner, r11 is bottom right corner
661 // g and b are the same layout.
662 // r0 and r1 are the top and bottom intermediate results
664 // first we interpolate the top two points, to get the top
666 r0 = (((r01-r00) * dsfrac) >> 4) + r00;
667 g0 = (((g01-g00) * dsfrac) >> 4) + g00;
668 b0 = (((b01-b00) * dsfrac) >> 4) + b00;
669 // then we interpolate the bottom two points, to get the
670 // bottom edge sample
671 r1 = (((r11-r10) * dsfrac) >> 4) + r10;
672 g1 = (((g11-g10) * dsfrac) >> 4) + g10;
673 b1 = (((b11-b10) * dsfrac) >> 4) + b10;
674 // then we interpolate the top and bottom samples to get the
675 // middle sample (the one which was requested)
676 r = (((r1-r0) * dtfrac) >> 4) + r0;
677 g = (((g1-g0) * dtfrac) >> 4) + g0;
678 b = (((b1-b0) * dtfrac) >> 4) + b0;
681 color[0] += (float) ((((((((r11-r10) * dsfrac) >> 4) + r10)-((((r01-r00) * dsfrac) >> 4) + r00)) * dtfrac) >> 4) + ((((r01-r00) * dsfrac) >> 4) + r00)) * (1.0f / 256.0f);
682 color[1] += (float) ((((((((g11-g10) * dsfrac) >> 4) + g10)-((((g01-g00) * dsfrac) >> 4) + g00)) * dtfrac) >> 4) + ((((g01-g00) * dsfrac) >> 4) + g00)) * (1.0f / 256.0f);
683 color[2] += (float) ((((((((b11-b10) * dsfrac) >> 4) + b10)-((((b01-b00) * dsfrac) >> 4) + b00)) * dtfrac) >> 4) + ((((b01-b00) * dsfrac) >> 4) + b00)) * (1.0f / 256.0f);
685 return true; // success
690 node = node->children[side ^ 1];
692 distz = endz - startz;
694 // return RecursiveLightPoint (color, node->children[side ^ 1], x, y, mid, endz);
698 void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits)
702 float brightness, r, f;
704 if (!r_dynamic.value || (!dlightbits[0] && !dlightbits[1] && !dlightbits[2] && !dlightbits[3] && !dlightbits[4] && !dlightbits[5] && !dlightbits[6] && !dlightbits[7]))
707 for (j = 0;j < (MAX_DLIGHTS >> 5);j++)
711 for (i=0 ; i<32 ; i++)
713 if (!((1 << i) & dlightbits[j]))
716 if (!cl_dlights[k].radius)
718 VectorSubtract (org, cl_dlights[k].origin, dist);
719 f = DotProduct(dist, dist) + LIGHTOFFSET;
720 r = cl_dlights[k].radius*cl_dlights[k].radius;
723 brightness = r * 128.0f / f;
724 color[0] += brightness * cl_dlights[k].color[0];
725 color[1] += brightness * cl_dlights[k].color[1];
726 color[2] += brightness * cl_dlights[k].color[2];
733 void R_CompleteLightPoint (vec3_t color, vec3_t p, int dynamic)
736 leaf = Mod_PointInLeaf(p, cl.worldmodel);
737 if (leaf->contents == CONTENTS_SOLID)
739 color[0] = color[1] = color[2] = 0;
743 if (r_fullbright.value || !cl.worldmodel->lightdata)
745 color[0] = color[1] = color[2] = 255;
749 color[0] = color[1] = color[2] = r_ambient.value * 2.0f;
750 RecursiveLightPoint (color, cl.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
753 R_DynamicLightPoint(color, p, leaf->dlightbits);
756 void R_ModelLightPoint (vec3_t color, vec3_t p, int *dlightbits)
759 leaf = Mod_PointInLeaf(p, cl.worldmodel);
760 if (leaf->contents == CONTENTS_SOLID)
762 color[0] = color[1] = color[2] = 0;
763 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
767 if (r_fullbright.value || !cl.worldmodel->lightdata)
769 color[0] = color[1] = color[2] = 255;
770 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
774 color[0] = color[1] = color[2] = r_ambient.value * 2.0f;
775 RecursiveLightPoint (color, cl.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
777 if (leaf->dlightframe == r_framecount)
779 dlightbits[0] = leaf->dlightbits[0];
780 dlightbits[1] = leaf->dlightbits[1];
781 dlightbits[2] = leaf->dlightbits[2];
782 dlightbits[3] = leaf->dlightbits[3];
783 dlightbits[4] = leaf->dlightbits[4];
784 dlightbits[5] = leaf->dlightbits[5];
785 dlightbits[6] = leaf->dlightbits[6];
786 dlightbits[7] = leaf->dlightbits[7];
789 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
792 /* // not currently used
793 void R_DynamicLightPointNoMask(vec3_t color, vec3_t org)
797 float brightness, r, f;
799 if (!r_dynamic.value)
802 for (i=0 ; i<MAX_DLIGHTS ; i++)
804 if (!cl_dlights[i].radius)
806 VectorSubtract (org, cl_dlights[i].origin, dist);
807 f = DotProduct(dist, dist) + LIGHTOFFSET;
808 r = cl_dlights[i].radius*cl_dlights[i].radius;
811 brightness = r * 256.0f / f;
812 if (cl_dlights[i].dark)
813 brightness = -brightness;
814 color[0] += brightness * cl_dlights[i].color[0];
815 color[1] += brightness * cl_dlights[i].color[1];
816 color[2] += brightness * cl_dlights[i].color[2];
822 void R_LightModel(entity_t *ent, int numverts, vec3_t center, vec3_t basecolor)
824 // LordHavoc: warning: reliance on int being 4 bytes here (of course the d_8to24table relies on that too...)
825 int i, j, nearlights = 0, color;
827 float t, t1, t2, t3, *avn;
833 } nearlight[MAX_DLIGHTS];
834 int modeldlightbits[8];
835 avc = aliasvertcolor;
837 a = (byte) bound((int) 0, (int) (modelalpha * 255.0f), (int) 255);
840 mod[0] = ent->render.colormod[0] * 0.5f;
841 mod[1] = ent->render.colormod[1] * 0.5f;
842 mod[2] = ent->render.colormod[2] * 0.5f;
846 mod[0] = ent->render.colormod[0];
847 mod[1] = ent->render.colormod[1];
848 mod[2] = ent->render.colormod[2];
850 if (ent->render.effects & EF_FULLBRIGHT)
852 ((byte *)&color)[0] = (byte) (255.0f * mod[0]);
853 ((byte *)&color)[1] = (byte) (255.0f * mod[1]);
854 ((byte *)&color)[2] = (byte) (255.0f * mod[2]);
855 ((byte *)&color)[3] = a;
856 for (i = 0;i < numverts;i++)
858 *((int *)avc) = color;
863 R_ModelLightPoint(basecolor, center, modeldlightbits);
865 basecolor[0] *= mod[0];
866 basecolor[1] *= mod[1];
867 basecolor[2] *= mod[2];
868 for (i = 0;i < MAX_DLIGHTS;i++)
870 if (!modeldlightbits[i >> 5])
875 if (!(modeldlightbits[i >> 5] & (1 << (i & 31))))
877 VectorSubtract (center, cl_dlights[i].origin, dist);
878 t2 = DotProduct(dist,dist) + LIGHTOFFSET;
879 t1 = cl_dlights[i].radius*cl_dlights[i].radius;
882 // transform the light into the model's coordinate system
883 if (gl_transform.value)
884 softwareuntransform(cl_dlights[i].origin, nearlight[nearlights].origin);
887 VectorCopy(cl_dlights[i].origin, nearlight[nearlights].origin);
889 nearlight[nearlights].color[0] = cl_dlights[i].color[0] * t1 * mod[0];
890 nearlight[nearlights].color[1] = cl_dlights[i].color[1] * t1 * mod[1];
891 nearlight[nearlights].color[2] = cl_dlights[i].color[2] * t1 * mod[2];
892 if (r_lightmodels.value && (ent == NULL || ent != cl_dlights[i].ent))
897 basecolor[0] += nearlight[nearlights].color[0] * t1;
898 basecolor[1] += nearlight[nearlights].color[1] * t1;
899 basecolor[2] += nearlight[nearlights].color[2] * t1;
903 t1 = bound(0, basecolor[0], 255);r = (byte) t1;
904 t1 = bound(0, basecolor[1], 255);g = (byte) t1;
905 t1 = bound(0, basecolor[2], 255);b = (byte) t1;
906 ((byte *)&color)[0] = r;
907 ((byte *)&color)[1] = g;
908 ((byte *)&color)[2] = b;
909 ((byte *)&color)[3] = a;
918 for (i = 0;i < numverts;i++)
920 VectorSubtract(nearlight[0].origin, av, v);
921 t = DotProduct(avn,v);
924 t /= (DotProduct(v,v) + LIGHTOFFSET);
925 temp = (int) ((float) (basecolor[0] + nearlight[0].color[0] * t));
926 avc[0] = bound(0, temp, 255);
927 temp = (int) ((float) (basecolor[1] + nearlight[0].color[1] * t));
928 avc[1] = bound(0, temp, 255);
929 temp = (int) ((float) (basecolor[2] + nearlight[0].color[2] * t));
930 avc[2] = bound(0, temp, 255);
934 *((int *)avc) = color;
942 for (i = 0;i < numverts;i++)
949 for (j = 0;j < nearlights;j++)
951 VectorSubtract(nearlight[j].origin, av, v);
952 t = DotProduct(avn,v);
955 t /= (DotProduct(v,v) + LIGHTOFFSET);
956 t1 += nearlight[j].color[0] * t;
957 t2 += nearlight[j].color[1] * t;
958 t3 += nearlight[j].color[2] * t;
966 avc[0] = bound(0, i1, 255);
968 avc[1] = bound(0, i2, 255);
970 avc[2] = bound(0, i3, 255);
973 else // dodge the costly float -> int conversions
974 *((int *)avc) = color;
983 for (i = 0;i < numverts;i++)
985 *((int *)avc) = color;