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.
23 #include "cl_collision.h"
25 rdlight_t r_dlight[MAX_DLIGHTS];
28 cvar_t r_modellights = {CVAR_SAVE, "r_modellights", "4"};
29 cvar_t r_vismarklights = {0, "r_vismarklights", "1"};
31 static rtexture_t *lightcorona;
32 static rtexturepool_t *lighttexturepool;
34 void r_light_start(void)
38 qbyte pixels[32][32][4];
39 lighttexturepool = R_AllocTexturePool();
40 for (y = 0;y < 32;y++)
42 dy = (y - 15.5f) * (1.0f / 16.0f);
43 for (x = 0;x < 32;x++)
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))) * 32.0f / (1.0f / (1.0f + 0.2));
51 pixels[y][x][3] = 255;
54 lightcorona = R_LoadTexture (lighttexturepool, "lightcorona", 32, 32, &pixels[0][0][0], TEXTYPE_RGBA, TEXF_PRECACHE);
57 void r_light_shutdown(void)
59 lighttexturepool = NULL;
63 void r_light_newmap(void)
67 void R_Light_Init(void)
69 Cvar_RegisterVariable(&r_modellights);
70 Cvar_RegisterVariable(&r_vismarklights);
71 R_RegisterModule("R_Light", r_light_start, r_light_shutdown, r_light_newmap);
79 void R_AnimateLight (void)
85 // 'm' is normal light, 'a' is no light, 'z' is double bright
86 i = (int)(cl.time * 10);
87 for (j = 0;j < MAX_LIGHTSTYLES;j++)
89 if (!cl_lightstyle[j].length)
91 d_lightstylevalue[j] = 256;
94 k = i % cl_lightstyle[j].length;
95 k = cl_lightstyle[j].map[k] - 'a';
97 d_lightstylevalue[j] = k;
102 void R_BuildLightList(void)
111 if (!r_dynamic.integer)
114 for (i = 0;i < MAX_DLIGHTS;i++)
119 rd = &r_dlight[r_numdlights++];
120 VectorCopy(cd->origin, rd->origin);
121 VectorScale(cd->color, cd->radius * 64.0f, rd->light);
122 rd->cullradius2 = DotProduct(rd->light, rd->light) * (0.25f / (64.0f * 64.0f)) + 4096.0f;
123 // clamp radius to avoid overflowing division table in lightmap code
124 if (rd->cullradius2 > (2048.0f * 2048.0f))
125 rd->cullradius2 = (2048.0f * 2048.0f);
126 rd->cullradius = sqrt(rd->cullradius2);
127 rd->subtract = 1.0f / rd->cullradius2;
130 c_dlights++; // count every dlight in use
134 void R_DrawCoronas(void)
138 float scale, viewdist, diff[3], dist;
140 memset(&m, 0, sizeof(m));
141 m.transparent = false;
142 m.blendfunc1 = GL_ONE;
143 m.blendfunc2 = GL_ONE;
144 m.depthdisable = true; // magic
147 m.tex[0] = R_GetTexture(lightcorona);
148 viewdist = DotProduct(r_origin, vpn);
149 for (i = 0;i < r_numdlights;i++)
152 dist = (DotProduct(rd->origin, vpn) - viewdist);
155 // trace to a point just barely closer to the eye
156 VectorSubtract(rd->origin, vpn, diff);
157 if (CL_TraceLine(r_origin, diff, NULL, NULL, 0, true) == 1 && R_Mesh_Draw_GetBuffer(&m, false))
159 scale = m.colorscale * (1.0f / 131072.0f);
162 VectorSubtract(rd->origin, r_origin, diff);
163 scale *= 1 - exp(fogdensity/DotProduct(diff,diff));
171 m.color[0] = m.color[4] = m.color[8] = m.color[12] = rd->light[0] * scale;
172 m.color[1] = m.color[5] = m.color[9] = m.color[13] = rd->light[1] * scale;
173 m.color[2] = m.color[6] = m.color[10] = m.color[14] = rd->light[2] * scale;
174 m.color[3] = m.color[7] = m.color[11] = m.color[15] = 1;
175 m.texcoords[0][0] = 0;
176 m.texcoords[0][1] = 0;
177 m.texcoords[0][2] = 0;
178 m.texcoords[0][3] = 1;
179 m.texcoords[0][4] = 1;
180 m.texcoords[0][5] = 1;
181 m.texcoords[0][6] = 1;
182 m.texcoords[0][7] = 0;
183 scale = rd->cullradius * 0.25f;
184 m.vertex[0] = rd->origin[0] - vright[0] * scale - vup[0] * scale;
185 m.vertex[1] = rd->origin[1] - vright[1] * scale - vup[1] * scale;
186 m.vertex[2] = rd->origin[2] - vright[2] * scale - vup[2] * scale;
187 m.vertex[4] = rd->origin[0] - vright[0] * scale + vup[0] * scale;
188 m.vertex[5] = rd->origin[1] - vright[1] * scale + vup[1] * scale;
189 m.vertex[6] = rd->origin[2] - vright[2] * scale + vup[2] * scale;
190 m.vertex[8] = rd->origin[0] + vright[0] * scale + vup[0] * scale;
191 m.vertex[9] = rd->origin[1] + vright[1] * scale + vup[1] * scale;
192 m.vertex[10] = rd->origin[2] + vright[2] * scale + vup[2] * scale;
193 m.vertex[12] = rd->origin[0] + vright[0] * scale - vup[0] * scale;
194 m.vertex[13] = rd->origin[1] + vright[1] * scale - vup[1] * scale;
195 m.vertex[14] = rd->origin[2] + vright[2] * scale - vup[2] * scale;
203 =============================================================================
207 =============================================================================
215 static void R_OldMarkLights (vec3_t lightorigin, rdlight_t *rd, int bit, int bitindex, mnode_t *node)
217 float ndist, maxdist;
222 if (!r_dynamic.integer)
225 // for comparisons to minimum acceptable light
226 maxdist = rd->cullradius2;
229 if (node->contents < 0)
231 if (node->contents != CONTENTS_SOLID)
233 leaf = (mleaf_t *)node;
234 if (leaf->dlightframe != r_framecount) // not dynamic until now
236 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;
237 leaf->dlightframe = r_framecount;
239 leaf->dlightbits[bitindex] |= bit;
244 ndist = PlaneDiff(lightorigin, node->plane);
246 if (ndist > rd->cullradius)
248 node = node->children[0];
251 if (ndist < -rd->cullradius)
253 node = node->children[1];
258 surf = currentrenderentity->model->surfaces + node->firstsurface;
259 for (i=0 ; i<node->numsurfaces ; i++, surf++)
261 int d, impacts, impactt;
262 float dist, dist2, impact[3];
263 if (surf->visframe != r_framecount)
266 if (surf->flags & SURF_PLANEBACK)
269 if (dist < -0.25f && !(surf->flags & SURF_LIGHTBOTHSIDES))
273 if (dist2 >= maxdist)
276 if (node->plane->type < 3)
278 VectorCopy(rd->origin, impact);
279 impact[node->plane->type] -= dist;
283 impact[0] = rd->origin[0] - surf->plane->normal[0] * dist;
284 impact[1] = rd->origin[1] - surf->plane->normal[1] * dist;
285 impact[2] = rd->origin[2] - surf->plane->normal[2] * dist;
288 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
290 d = bound(0, impacts, surf->extents[0] + 16) - impacts;
295 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
297 d = bound(0, impactt, surf->extents[1] + 16) - impactt;
302 if (surf->dlightframe != r_framecount) // not dynamic until now
304 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;
305 surf->dlightframe = r_framecount;
307 surf->dlightbits[bitindex] |= bit;
310 if (node->children[0]->contents >= 0)
312 if (node->children[1]->contents >= 0)
314 R_OldMarkLights (lightorigin, rd, bit, bitindex, node->children[0]);
315 node = node->children[1];
320 node = node->children[0];
324 else if (node->children[1]->contents >= 0)
326 node = node->children[1];
332 static void R_VisMarkLights (rdlight_t *rd, int bit, int bitindex)
334 static int lightframe = 0;
338 int i, k, m, c, leafnum;
339 msurface_t *surf, **mark;
343 float low[3], high[3], dist, maxdist;
345 if (!r_dynamic.integer)
348 model = currentrenderentity->model;
349 softwareuntransform(rd->origin, lightorigin);
351 if (!r_vismarklights.integer)
353 R_OldMarkLights(lightorigin, rd, bit, bitindex, model->nodes + model->hulls[0].firstclipnode);
357 pvsleaf = Mod_PointInLeaf (lightorigin, model);
360 Con_Printf("R_VisMarkLights: NULL leaf??\n");
361 R_OldMarkLights(lightorigin, rd, bit, bitindex, model->nodes + model->hulls[0].firstclipnode);
365 in = pvsleaf->compressed_vis;
368 // no vis info, so make all visible
369 R_OldMarkLights(lightorigin, rd, bit, bitindex, model->nodes + model->hulls[0].firstclipnode);
375 low[0] = lightorigin[0] - rd->cullradius;low[1] = lightorigin[1] - rd->cullradius;low[2] = lightorigin[2] - rd->cullradius;
376 high[0] = lightorigin[0] + rd->cullradius;high[1] = lightorigin[1] + rd->cullradius;high[2] = lightorigin[2] + rd->cullradius;
378 // for comparisons to minimum acceptable light
379 maxdist = rd->cullradius2;
381 row = (model->numleafs+7)>>3;
389 for (i = 0;i < 8;i++)
393 // warning to the clumsy: numleafs is one less than it should be, it only counts leafs with vis bits (skips leaf 0)
394 leafnum = (k << 3)+i+1;
395 if (leafnum > model->numleafs)
397 leaf = &model->leafs[leafnum];
398 if (leaf->visframe != r_framecount
399 || leaf->contents == CONTENTS_SOLID
400 || leaf->mins[0] > high[0] || leaf->maxs[0] < low[0]
401 || leaf->mins[1] > high[1] || leaf->maxs[1] < low[1]
402 || leaf->mins[2] > high[2] || leaf->maxs[2] < low[2])
404 if (leaf->dlightframe != r_framecount)
406 // not dynamic until now
407 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;
408 leaf->dlightframe = r_framecount;
410 leaf->dlightbits[bitindex] |= bit;
411 if ((m = leaf->nummarksurfaces))
413 mark = leaf->firstmarksurface;
417 // if not visible in current frame, or already marked because it was in another leaf we passed, skip
418 if (surf->lightframe == lightframe)
420 surf->lightframe = lightframe;
421 if (surf->visframe != r_framecount)
423 dist = PlaneDiff(lightorigin, surf->plane);
424 if (surf->flags & SURF_PLANEBACK)
426 // LordHavoc: make sure it is infront of the surface and not too far away
427 if (dist < rd->cullradius && (dist > -0.25f || ((surf->flags & SURF_LIGHTBOTHSIDES) && dist > -rd->cullradius)))
430 int impacts, impactt;
431 float dist2, impact[3];
435 if (surf->plane->type < 3)
437 VectorCopy(rd->origin, impact);
438 impact[surf->plane->type] -= dist;
442 impact[0] = rd->origin[0] - surf->plane->normal[0] * dist;
443 impact[1] = rd->origin[1] - surf->plane->normal[1] * dist;
444 impact[2] = rd->origin[2] - surf->plane->normal[2] * dist;
447 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
448 d = bound(0, impacts, surf->extents[0] + 16) - impacts;
453 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
454 d = bound(0, impactt, surf->extents[1] + 16) - impactt;
459 if (surf->dlightframe != r_framecount) // not dynamic until now
461 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;
462 surf->dlightframe = r_framecount;
464 surf->dlightbits[bitindex] |= bit;
479 void R_MarkLights(void)
482 for (i = 0;i < r_numdlights;i++)
483 R_VisMarkLights (r_dlight + i, 1 << (i & 31), i >> 5);
487 =============================================================================
491 =============================================================================
494 static int RecursiveLightPoint (vec3_t color, mnode_t *node, float x, float y, float startz, float endz)
496 int side, distz = endz - startz;
501 if (node->contents < 0)
502 return false; // didn't hit anything
504 switch (node->plane->type)
507 node = node->children[x < node->plane->dist];
510 node = node->children[y < node->plane->dist];
513 side = startz < node->plane->dist;
514 if ((endz < node->plane->dist) == side)
516 node = node->children[side];
519 // found an intersection
520 mid = node->plane->dist;
523 back = front = x * node->plane->normal[0] + y * node->plane->normal[1];
524 front += startz * node->plane->normal[2];
525 back += endz * node->plane->normal[2];
526 side = front < node->plane->dist;
527 if ((back < node->plane->dist) == side)
529 node = node->children[side];
532 // found an intersection
533 mid = startz + distz * (front - node->plane->dist) / (front - back);
537 // go down front side
538 if (node->children[side]->contents >= 0 && RecursiveLightPoint (color, node->children[side], x, y, startz, mid))
539 return true; // hit something
542 // check for impact on this node
543 if (node->numsurfaces)
548 surf = cl.worldmodel->surfaces + node->firstsurface;
549 for (i = 0;i < node->numsurfaces;i++, surf++)
551 if (!(surf->flags & SURF_LIGHTMAP))
552 continue; // no lightmaps
554 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]);
555 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]);
557 if (ds < surf->texturemins[0] || dt < surf->texturemins[1])
560 ds -= surf->texturemins[0];
561 dt -= surf->texturemins[1];
563 if (ds > surf->extents[0] || dt > surf->extents[1])
569 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;
570 line3 = ((surf->extents[0]>>4)+1)*3;
571 size3 = ((surf->extents[0]>>4)+1) * ((surf->extents[1]>>4)+1)*3; // LordHavoc: *3 for colored lighting
573 lightmap = surf->samples + ((dt>>4) * ((surf->extents[0]>>4)+1) + (ds>>4))*3; // LordHavoc: *3 for color
575 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
577 scale = d_lightstylevalue[surf->styles[maps]];
578 r00 += lightmap[ 0] * scale;g00 += lightmap[ 1] * scale;b00 += lightmap[ 2] * scale;
579 r01 += lightmap[ 3] * scale;g01 += lightmap[ 4] * scale;b01 += lightmap[ 5] * scale;
580 r10 += lightmap[line3+0] * scale;g10 += lightmap[line3+1] * scale;b10 += lightmap[line3+2] * scale;
581 r11 += lightmap[line3+3] * scale;g11 += lightmap[line3+4] * scale;b11 += lightmap[line3+5] * scale;
586 LordHavoc: here's the readable version of the interpolation
587 code, not quite as easy for the compiler to optimize...
589 dsfrac is the X position in the lightmap pixel, * 16
590 dtfrac is the Y position in the lightmap pixel, * 16
591 r00 is top left corner, r01 is top right corner
592 r10 is bottom left corner, r11 is bottom right corner
593 g and b are the same layout.
594 r0 and r1 are the top and bottom intermediate results
596 first we interpolate the top two points, to get the top
599 r0 = (((r01-r00) * dsfrac) >> 4) + r00;
600 g0 = (((g01-g00) * dsfrac) >> 4) + g00;
601 b0 = (((b01-b00) * dsfrac) >> 4) + b00;
603 then we interpolate the bottom two points, to get the
606 r1 = (((r11-r10) * dsfrac) >> 4) + r10;
607 g1 = (((g11-g10) * dsfrac) >> 4) + g10;
608 b1 = (((b11-b10) * dsfrac) >> 4) + b10;
610 then we interpolate the top and bottom samples to get the
611 middle sample (the one which was requested)
613 r = (((r1-r0) * dtfrac) >> 4) + r0;
614 g = (((g1-g0) * dtfrac) >> 4) + g0;
615 b = (((b1-b0) * dtfrac) >> 4) + b0;
618 color[0] += (float) ((((((((r11-r10) * dsfrac) >> 4) + r10)-((((r01-r00) * dsfrac) >> 4) + r00)) * dtfrac) >> 4) + ((((r01-r00) * dsfrac) >> 4) + r00)) * (1.0f / 32768.0f);
619 color[1] += (float) ((((((((g11-g10) * dsfrac) >> 4) + g10)-((((g01-g00) * dsfrac) >> 4) + g00)) * dtfrac) >> 4) + ((((g01-g00) * dsfrac) >> 4) + g00)) * (1.0f / 32768.0f);
620 color[2] += (float) ((((((((b11-b10) * dsfrac) >> 4) + b10)-((((b01-b00) * dsfrac) >> 4) + b00)) * dtfrac) >> 4) + ((((b01-b00) * dsfrac) >> 4) + b00)) * (1.0f / 32768.0f);
622 return true; // success
627 node = node->children[side ^ 1];
629 distz = endz - startz;
634 void R_CompleteLightPoint (vec3_t color, vec3_t p, int dynamic, mleaf_t *leaf)
642 leaf = Mod_PointInLeaf(p, cl.worldmodel);
644 if (leaf->contents == CONTENTS_SOLID)
646 color[0] = color[1] = color[2] = 0;
650 if (r_fullbright.integer || !cl.worldmodel->lightdata)
652 color[0] = color[1] = color[2] = 2;
656 color[0] = color[1] = color[2] = r_ambient.value * (2.0f / 128.0f);
657 if (cl.worldmodel->numlights)
659 for (i = 0;i < cl.worldmodel->numlights;i++)
661 sl = cl.worldmodel->lights + i;
662 if (d_lightstylevalue[sl->style] > 0)
664 VectorSubtract (p, sl->origin, v);
665 f = ((1.0f / (DotProduct(v, v) * sl->falloff + sl->distbias)) - sl->subtract);
666 if (f > 0 && CL_TraceLine(p, sl->origin, NULL, NULL, 0, false) == 1)
668 f *= d_lightstylevalue[sl->style] * (1.0f / 65536.0f);
669 VectorMA(color, f, sl->light, color);
675 RecursiveLightPoint (color, cl.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
677 if (dynamic && leaf->dlightframe == r_framecount)
679 dlightbits = leaf->dlightbits;
680 for (i = 0;i < r_numdlights;i++)
682 if (!(dlightbits[i >> 5] & (1 << (i & 31))))
685 VectorSubtract (p, rd->origin, v);
686 f = DotProduct(v, v);
687 if (f < rd->cullradius2)
689 f = (1.0f / (f + LIGHTOFFSET)) - rd->subtract;
690 VectorMA(color, f, rd->light, color);
696 void R_ModelLightPoint (vec3_t color, vec3_t p, int *dlightbits)
699 leaf = Mod_PointInLeaf(p, cl.worldmodel);
700 if (leaf->contents == CONTENTS_SOLID)
702 color[0] = color[1] = color[2] = 0;
703 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
707 if (r_fullbright.integer || !cl.worldmodel->lightdata || currentrenderentity->effects & EF_FULLBRIGHT)
709 color[0] = color[1] = color[2] = 2;
710 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
714 color[0] = color[1] = color[2] = r_ambient.value * (2.0f / 128.0f);
715 if (!cl.worldmodel->numlights)
716 RecursiveLightPoint (color, cl.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
718 if (leaf->dlightframe == r_framecount)
720 dlightbits[0] = leaf->dlightbits[0];
721 dlightbits[1] = leaf->dlightbits[1];
722 dlightbits[2] = leaf->dlightbits[2];
723 dlightbits[3] = leaf->dlightbits[3];
724 dlightbits[4] = leaf->dlightbits[4];
725 dlightbits[5] = leaf->dlightbits[5];
726 dlightbits[6] = leaf->dlightbits[6];
727 dlightbits[7] = leaf->dlightbits[7];
730 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
733 void R_LightModel(int numverts, float colorr, float colorg, float colorb, int worldcoords)
735 int i, j, nearlights = 0, maxnearlights = r_modellights.integer;
736 float color[3], basecolor[3], v[3], t, *av, *avn, *avc, a, f, dist2, mscale, dot, stylescale, intensity, ambientcolor[3];
742 // how much this light would contribute to ambient if replaced
747 // used for choosing only the brightest lights
750 nearlight[MAX_DLIGHTS], *nl;
751 int modeldlightbits[8];
754 a = currentrenderentity->alpha;
755 // scale of the model's coordinate space, to alter light attenuation to match
756 // make the mscale squared so it can scale the squared distance results
757 mscale = currentrenderentity->scale * currentrenderentity->scale;
758 if ((maxnearlights != 0) && !r_fullbright.integer && !(currentrenderentity->effects & EF_FULLBRIGHT))
760 R_ModelLightPoint(basecolor, currentrenderentity->origin, modeldlightbits);
763 VectorSubtract(currentrenderentity->origin, currentrenderentity->entlightsorigin, v);
764 if ((realtime > currentrenderentity->entlightstime && DotProduct(v,v) >= 1.0f))
766 currentrenderentity->numentlights = 0;
767 currentrenderentity->entlightstime = realtime + 0.2;
768 VectorCopy(currentrenderentity->origin, currentrenderentity->entlightsorigin);
769 for (i = 0, sl = cl.worldmodel->lights;i < cl.worldmodel->numlights && currentrenderentity->numentlights < MAXENTLIGHTS;i++, sl++)
770 if (CL_TraceLine(currentrenderentity->origin, sl->origin, NULL, NULL, 0, false) == 1)
771 currentrenderentity->entlights[currentrenderentity->numentlights++] = i;
773 for (i = 0;i < currentrenderentity->numentlights;i++)
775 sl = cl.worldmodel->lights + currentrenderentity->entlights[i];
776 stylescale = d_lightstylevalue[sl->style] * (1.0f / 65536.0f);
777 VectorSubtract (currentrenderentity->origin, sl->origin, v);
778 f = ((1.0f / (DotProduct(v, v) * sl->falloff + sl->distbias)) - sl->subtract) * stylescale;
779 VectorScale(sl->light, f, ambientcolor);
780 intensity = DotProduct(ambientcolor, ambientcolor);
783 if (nearlights < maxnearlights)
787 for (j = 0;j < maxnearlights;j++)
789 if (nearlight[j].intensity < intensity)
791 if (nearlight[j].intensity > 0)
792 VectorAdd(basecolor, nearlight[j].ambientlight, basecolor);
797 if (j >= maxnearlights)
799 // this light is less significant than all others,
802 VectorAdd(basecolor, ambientcolor, basecolor);
807 nl->intensity = intensity;
808 // transform the light into the model's coordinate system
810 VectorCopy(sl->origin, nl->origin);
812 softwareuntransform(sl->origin, nl->origin);
813 // integrate mscale into falloff, for maximum speed
814 nl->falloff = sl->falloff * mscale;
815 VectorCopy(ambientcolor, nl->ambientlight);
816 nl->light[0] = sl->light[0] * stylescale * colorr * 4.0f;
817 nl->light[1] = sl->light[1] * stylescale * colorg * 4.0f;
818 nl->light[2] = sl->light[2] * stylescale * colorb * 4.0f;
819 nl->subtract = sl->subtract;
820 nl->offset = sl->distbias;
823 for (i = 0;i < r_numdlights;i++)
825 if (!(modeldlightbits[i >> 5] & (1 << (i & 31))))
828 VectorSubtract (currentrenderentity->origin, rd->origin, v);
829 f = ((1.0f / (DotProduct(v, v) + LIGHTOFFSET)) - rd->subtract);
830 VectorScale(rd->light, f, ambientcolor);
831 intensity = DotProduct(ambientcolor, ambientcolor);
834 if (nearlights < maxnearlights)
838 for (j = 0;j < maxnearlights;j++)
840 if (nearlight[j].intensity < intensity)
842 if (nearlight[j].intensity > 0)
843 VectorAdd(basecolor, nearlight[j].ambientlight, basecolor);
848 if (j >= maxnearlights)
850 // this light is less significant than all others,
853 VectorAdd(basecolor, ambientcolor, basecolor);
858 nl->intensity = intensity;
859 // transform the light into the model's coordinate system
861 VectorCopy(rd->origin, nl->origin);
863 softwareuntransform(rd->origin, nl->origin);
864 // integrate mscale into falloff, for maximum speed
865 nl->falloff = mscale;
866 VectorCopy(ambientcolor, nl->ambientlight);
867 nl->light[0] = rd->light[0] * colorr * 4.0f;
868 nl->light[1] = rd->light[1] * colorg * 4.0f;
869 nl->light[2] = rd->light[2] * colorb * 4.0f;
870 nl->subtract = rd->subtract;
871 nl->offset = LIGHTOFFSET;
877 R_CompleteLightPoint (basecolor, currentrenderentity->origin, true, NULL);
879 basecolor[0] *= colorr;
880 basecolor[1] *= colorg;
881 basecolor[2] *= colorb;
882 avc = aliasvertcolor;
887 for (i = 0;i < numverts;i++)
889 VectorCopy(basecolor, color);
890 for (j = 0, nl = &nearlight[0];j < nearlights;j++, nl++)
892 VectorSubtract(nl->origin, av, v);
893 // directional shading
894 dot = DotProduct(avn,v);
897 // the vertex normal faces the light
899 // do the distance attenuation
900 dist2 = DotProduct(v,v);
901 f = (1.0f / (dist2 * nl->falloff + nl->offset)) - nl->subtract;
905 t = 1.0f / sqrt(dist2);
907 *((int *)&t) = 0x5f3759df - ((* (int *) &dist2) >> 1);
908 t = t * (1.5f - (dist2 * 0.5f * t * t));
911 // dot * t is dotproduct with a normalized v.
912 // (the result would be -1 to +1, but we already
913 // eliminated the <= 0 case, so it is 0 to 1)
915 // the hardness variables are for backlighting/shinyness
916 // these have been hardwired at * 0.5 + 0.5 to match
917 // the quake map lighting utility's equations
918 f *= dot * t;// * 0.5f + 0.5f;// * hardness + hardnessoffset;
919 VectorMA(color, f, nl->light, color);
924 VectorCopy(color, avc);
933 for (i = 0;i < numverts;i++)
935 VectorCopy(basecolor, avc);