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))
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;
202 =============================================================================
206 =============================================================================
214 static void R_OldMarkLights (vec3_t lightorigin, rdlight_t *rd, int bit, int bitindex, mnode_t *node)
216 float ndist, maxdist;
221 if (!r_dynamic.integer)
224 // for comparisons to minimum acceptable light
225 maxdist = rd->cullradius2;
228 if (node->contents < 0)
230 if (node->contents != CONTENTS_SOLID)
232 leaf = (mleaf_t *)node;
233 if (leaf->dlightframe != r_framecount) // not dynamic until now
235 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;
236 leaf->dlightframe = r_framecount;
238 leaf->dlightbits[bitindex] |= bit;
243 ndist = PlaneDiff(lightorigin, node->plane);
245 if (ndist > rd->cullradius)
247 node = node->children[0];
250 if (ndist < -rd->cullradius)
252 node = node->children[1];
257 surf = currentrenderentity->model->surfaces + node->firstsurface;
258 for (i=0 ; i<node->numsurfaces ; i++, surf++)
260 int d, impacts, impactt;
261 float dist, dist2, impact[3];
262 if (surf->visframe != r_framecount)
265 if (surf->flags & SURF_PLANEBACK)
268 if (dist < -0.25f && !(surf->flags & SURF_LIGHTBOTHSIDES))
272 if (dist2 >= maxdist)
275 if (node->plane->type < 3)
277 VectorCopy(rd->origin, impact);
278 impact[node->plane->type] -= dist;
282 impact[0] = rd->origin[0] - surf->plane->normal[0] * dist;
283 impact[1] = rd->origin[1] - surf->plane->normal[1] * dist;
284 impact[2] = rd->origin[2] - surf->plane->normal[2] * dist;
287 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
289 d = bound(0, impacts, surf->extents[0] + 16) - impacts;
294 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
296 d = bound(0, impactt, surf->extents[1] + 16) - impactt;
301 if (surf->dlightframe != r_framecount) // not dynamic until now
303 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;
304 surf->dlightframe = r_framecount;
306 surf->dlightbits[bitindex] |= bit;
309 if (node->children[0]->contents >= 0)
311 if (node->children[1]->contents >= 0)
313 R_OldMarkLights (lightorigin, rd, bit, bitindex, node->children[0]);
314 node = node->children[1];
319 node = node->children[0];
323 else if (node->children[1]->contents >= 0)
325 node = node->children[1];
331 static void R_VisMarkLights (rdlight_t *rd, int bit, int bitindex)
333 static int lightframe = 0;
337 int i, k, m, c, leafnum;
338 msurface_t *surf, **mark;
342 float low[3], high[3], dist, maxdist;
344 if (!r_dynamic.integer)
347 model = currentrenderentity->model;
348 softwareuntransform(rd->origin, lightorigin);
350 if (!r_vismarklights.integer)
352 R_OldMarkLights(lightorigin, rd, bit, bitindex, model->nodes + model->hulls[0].firstclipnode);
356 pvsleaf = Mod_PointInLeaf (lightorigin, model);
359 Con_Printf("R_VisMarkLights: NULL leaf??\n");
360 R_OldMarkLights(lightorigin, rd, bit, bitindex, model->nodes + model->hulls[0].firstclipnode);
364 in = pvsleaf->compressed_vis;
367 // no vis info, so make all visible
368 R_OldMarkLights(lightorigin, rd, bit, bitindex, model->nodes + model->hulls[0].firstclipnode);
374 low[0] = lightorigin[0] - rd->cullradius;low[1] = lightorigin[1] - rd->cullradius;low[2] = lightorigin[2] - rd->cullradius;
375 high[0] = lightorigin[0] + rd->cullradius;high[1] = lightorigin[1] + rd->cullradius;high[2] = lightorigin[2] + rd->cullradius;
377 // for comparisons to minimum acceptable light
378 maxdist = rd->cullradius2;
380 row = (model->numleafs+7)>>3;
388 for (i = 0;i < 8;i++)
392 // warning to the clumsy: numleafs is one less than it should be, it only counts leafs with vis bits (skips leaf 0)
393 leafnum = (k << 3)+i+1;
394 if (leafnum > model->numleafs)
396 leaf = &model->leafs[leafnum];
397 if (leaf->visframe != r_framecount
398 || leaf->contents == CONTENTS_SOLID
399 || leaf->mins[0] > high[0] || leaf->maxs[0] < low[0]
400 || leaf->mins[1] > high[1] || leaf->maxs[1] < low[1]
401 || leaf->mins[2] > high[2] || leaf->maxs[2] < low[2])
403 if (leaf->dlightframe != r_framecount)
405 // not dynamic until now
406 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;
407 leaf->dlightframe = r_framecount;
409 leaf->dlightbits[bitindex] |= bit;
410 if ((m = leaf->nummarksurfaces))
412 mark = leaf->firstmarksurface;
416 // if not visible in current frame, or already marked because it was in another leaf we passed, skip
417 if (surf->lightframe == lightframe)
419 surf->lightframe = lightframe;
420 if (surf->visframe != r_framecount)
422 dist = PlaneDiff(lightorigin, surf->plane);
423 if (surf->flags & SURF_PLANEBACK)
425 // LordHavoc: make sure it is infront of the surface and not too far away
426 if (dist < rd->cullradius && (dist > -0.25f || ((surf->flags & SURF_LIGHTBOTHSIDES) && dist > -rd->cullradius)))
429 int impacts, impactt;
430 float dist2, impact[3];
434 if (surf->plane->type < 3)
436 VectorCopy(rd->origin, impact);
437 impact[surf->plane->type] -= dist;
441 impact[0] = rd->origin[0] - surf->plane->normal[0] * dist;
442 impact[1] = rd->origin[1] - surf->plane->normal[1] * dist;
443 impact[2] = rd->origin[2] - surf->plane->normal[2] * dist;
446 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
447 d = bound(0, impacts, surf->extents[0] + 16) - impacts;
452 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
453 d = bound(0, impactt, surf->extents[1] + 16) - impactt;
458 if (surf->dlightframe != r_framecount) // not dynamic until now
460 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;
461 surf->dlightframe = r_framecount;
463 surf->dlightbits[bitindex] |= bit;
478 void R_MarkLights(void)
481 for (i = 0;i < r_numdlights;i++)
482 R_VisMarkLights (r_dlight + i, 1 << (i & 31), i >> 5);
486 =============================================================================
490 =============================================================================
493 static int RecursiveLightPoint (vec3_t color, mnode_t *node, float x, float y, float startz, float endz)
495 int side, distz = endz - startz;
500 if (node->contents < 0)
501 return false; // didn't hit anything
503 switch (node->plane->type)
506 node = node->children[x < node->plane->dist];
509 node = node->children[y < node->plane->dist];
512 side = startz < node->plane->dist;
513 if ((endz < node->plane->dist) == side)
515 node = node->children[side];
518 // found an intersection
519 mid = node->plane->dist;
522 back = front = x * node->plane->normal[0] + y * node->plane->normal[1];
523 front += startz * node->plane->normal[2];
524 back += endz * node->plane->normal[2];
525 side = front < node->plane->dist;
526 if ((back < node->plane->dist) == side)
528 node = node->children[side];
531 // found an intersection
532 mid = startz + distz * (front - node->plane->dist) / (front - back);
536 // go down front side
537 if (node->children[side]->contents >= 0 && RecursiveLightPoint (color, node->children[side], x, y, startz, mid))
538 return true; // hit something
541 // check for impact on this node
542 if (node->numsurfaces)
547 surf = cl.worldmodel->surfaces + node->firstsurface;
548 for (i = 0;i < node->numsurfaces;i++, surf++)
550 if (!(surf->flags & SURF_LIGHTMAP))
551 continue; // no lightmaps
553 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]);
554 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]);
556 if (ds < surf->texturemins[0] || dt < surf->texturemins[1])
559 ds -= surf->texturemins[0];
560 dt -= surf->texturemins[1];
562 if (ds > surf->extents[0] || dt > surf->extents[1])
568 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;
569 line3 = ((surf->extents[0]>>4)+1)*3;
570 size3 = ((surf->extents[0]>>4)+1) * ((surf->extents[1]>>4)+1)*3; // LordHavoc: *3 for colored lighting
572 lightmap = surf->samples + ((dt>>4) * ((surf->extents[0]>>4)+1) + (ds>>4))*3; // LordHavoc: *3 for color
574 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
576 scale = d_lightstylevalue[surf->styles[maps]];
577 r00 += lightmap[ 0] * scale;g00 += lightmap[ 1] * scale;b00 += lightmap[ 2] * scale;
578 r01 += lightmap[ 3] * scale;g01 += lightmap[ 4] * scale;b01 += lightmap[ 5] * scale;
579 r10 += lightmap[line3+0] * scale;g10 += lightmap[line3+1] * scale;b10 += lightmap[line3+2] * scale;
580 r11 += lightmap[line3+3] * scale;g11 += lightmap[line3+4] * scale;b11 += lightmap[line3+5] * scale;
585 LordHavoc: here's the readable version of the interpolation
586 code, not quite as easy for the compiler to optimize...
588 dsfrac is the X position in the lightmap pixel, * 16
589 dtfrac is the Y position in the lightmap pixel, * 16
590 r00 is top left corner, r01 is top right corner
591 r10 is bottom left corner, r11 is bottom right corner
592 g and b are the same layout.
593 r0 and r1 are the top and bottom intermediate results
595 first we interpolate the top two points, to get the top
598 r0 = (((r01-r00) * dsfrac) >> 4) + r00;
599 g0 = (((g01-g00) * dsfrac) >> 4) + g00;
600 b0 = (((b01-b00) * dsfrac) >> 4) + b00;
602 then we interpolate the bottom two points, to get the
605 r1 = (((r11-r10) * dsfrac) >> 4) + r10;
606 g1 = (((g11-g10) * dsfrac) >> 4) + g10;
607 b1 = (((b11-b10) * dsfrac) >> 4) + b10;
609 then we interpolate the top and bottom samples to get the
610 middle sample (the one which was requested)
612 r = (((r1-r0) * dtfrac) >> 4) + r0;
613 g = (((g1-g0) * dtfrac) >> 4) + g0;
614 b = (((b1-b0) * dtfrac) >> 4) + b0;
617 color[0] += (float) ((((((((r11-r10) * dsfrac) >> 4) + r10)-((((r01-r00) * dsfrac) >> 4) + r00)) * dtfrac) >> 4) + ((((r01-r00) * dsfrac) >> 4) + r00)) * (1.0f / 32768.0f);
618 color[1] += (float) ((((((((g11-g10) * dsfrac) >> 4) + g10)-((((g01-g00) * dsfrac) >> 4) + g00)) * dtfrac) >> 4) + ((((g01-g00) * dsfrac) >> 4) + g00)) * (1.0f / 32768.0f);
619 color[2] += (float) ((((((((b11-b10) * dsfrac) >> 4) + b10)-((((b01-b00) * dsfrac) >> 4) + b00)) * dtfrac) >> 4) + ((((b01-b00) * dsfrac) >> 4) + b00)) * (1.0f / 32768.0f);
621 return true; // success
626 node = node->children[side ^ 1];
628 distz = endz - startz;
633 void R_CompleteLightPoint (vec3_t color, vec3_t p, int dynamic, mleaf_t *leaf)
641 leaf = Mod_PointInLeaf(p, cl.worldmodel);
643 if (leaf->contents == CONTENTS_SOLID)
645 color[0] = color[1] = color[2] = 0;
649 if (r_fullbright.integer || !cl.worldmodel->lightdata)
651 color[0] = color[1] = color[2] = 2;
655 color[0] = color[1] = color[2] = r_ambient.value * (2.0f / 128.0f);
656 if (cl.worldmodel->numlights)
658 for (i = 0;i < cl.worldmodel->numlights;i++)
660 sl = cl.worldmodel->lights + i;
661 if (d_lightstylevalue[sl->style] > 0)
663 VectorSubtract (p, sl->origin, v);
664 f = ((1.0f / (DotProduct(v, v) * sl->falloff + sl->distbias)) - sl->subtract);
665 if (f > 0 && CL_TraceLine(p, sl->origin, NULL, NULL, 0, false) == 1)
667 f *= d_lightstylevalue[sl->style] * (1.0f / 65536.0f);
668 VectorMA(color, f, sl->light, color);
674 RecursiveLightPoint (color, cl.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
676 if (dynamic && leaf->dlightframe == r_framecount)
678 dlightbits = leaf->dlightbits;
679 for (i = 0;i < r_numdlights;i++)
681 if (!(dlightbits[i >> 5] & (1 << (i & 31))))
684 VectorSubtract (p, rd->origin, v);
685 f = DotProduct(v, v);
686 if (f < rd->cullradius2)
688 f = (1.0f / (f + LIGHTOFFSET)) - rd->subtract;
689 VectorMA(color, f, rd->light, color);
695 void R_ModelLightPoint (vec3_t color, vec3_t p, int *dlightbits)
698 leaf = Mod_PointInLeaf(p, cl.worldmodel);
699 if (leaf->contents == CONTENTS_SOLID)
701 color[0] = color[1] = color[2] = 0;
702 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
706 if (r_fullbright.integer || !cl.worldmodel->lightdata || currentrenderentity->effects & EF_FULLBRIGHT)
708 color[0] = color[1] = color[2] = 2;
709 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
713 color[0] = color[1] = color[2] = r_ambient.value * (2.0f / 128.0f);
714 if (!cl.worldmodel->numlights)
715 RecursiveLightPoint (color, cl.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
717 if (leaf->dlightframe == r_framecount)
719 dlightbits[0] = leaf->dlightbits[0];
720 dlightbits[1] = leaf->dlightbits[1];
721 dlightbits[2] = leaf->dlightbits[2];
722 dlightbits[3] = leaf->dlightbits[3];
723 dlightbits[4] = leaf->dlightbits[4];
724 dlightbits[5] = leaf->dlightbits[5];
725 dlightbits[6] = leaf->dlightbits[6];
726 dlightbits[7] = leaf->dlightbits[7];
729 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
732 void R_LightModel(int numverts, float colorr, float colorg, float colorb, int worldcoords)
734 int i, j, nearlights = 0, maxnearlights = r_modellights.integer;
735 float color[3], basecolor[3], v[3], t, *av, *avn, *avc, a, f, dist2, mscale, dot, stylescale, intensity, ambientcolor[3];
741 // how much this light would contribute to ambient if replaced
746 // used for choosing only the brightest lights
749 nearlight[MAX_DLIGHTS], *nl;
750 int modeldlightbits[8];
753 a = currentrenderentity->alpha;
754 // scale of the model's coordinate space, to alter light attenuation to match
755 // make the mscale squared so it can scale the squared distance results
756 mscale = currentrenderentity->scale * currentrenderentity->scale;
757 if ((maxnearlights != 0) && !r_fullbright.integer && !(currentrenderentity->effects & EF_FULLBRIGHT))
759 R_ModelLightPoint(basecolor, currentrenderentity->origin, modeldlightbits);
762 VectorSubtract(currentrenderentity->origin, currentrenderentity->entlightsorigin, v);
763 if ((realtime > currentrenderentity->entlightstime && DotProduct(v,v) >= 1.0f))
765 currentrenderentity->numentlights = 0;
766 currentrenderentity->entlightstime = realtime + 0.2;
767 VectorCopy(currentrenderentity->origin, currentrenderentity->entlightsorigin);
768 for (i = 0, sl = cl.worldmodel->lights;i < cl.worldmodel->numlights && currentrenderentity->numentlights < MAXENTLIGHTS;i++, sl++)
769 if (CL_TraceLine(currentrenderentity->origin, sl->origin, NULL, NULL, 0, false) == 1)
770 currentrenderentity->entlights[currentrenderentity->numentlights++] = i;
772 for (i = 0;i < currentrenderentity->numentlights;i++)
774 sl = cl.worldmodel->lights + currentrenderentity->entlights[i];
775 stylescale = d_lightstylevalue[sl->style] * (1.0f / 65536.0f);
776 VectorSubtract (currentrenderentity->origin, sl->origin, v);
777 f = ((1.0f / (DotProduct(v, v) * sl->falloff + sl->distbias)) - sl->subtract) * stylescale;
778 VectorScale(sl->light, f, ambientcolor);
779 intensity = DotProduct(ambientcolor, ambientcolor);
782 if (nearlights < maxnearlights)
786 for (j = 0;j < maxnearlights;j++)
788 if (nearlight[j].intensity < intensity)
790 if (nearlight[j].intensity > 0)
791 VectorAdd(basecolor, nearlight[j].ambientlight, basecolor);
796 if (j >= maxnearlights)
798 // this light is less significant than all others,
801 VectorAdd(basecolor, ambientcolor, basecolor);
806 nl->intensity = intensity;
807 // transform the light into the model's coordinate system
809 VectorCopy(sl->origin, nl->origin);
811 softwareuntransform(sl->origin, nl->origin);
812 // integrate mscale into falloff, for maximum speed
813 nl->falloff = sl->falloff * mscale;
814 VectorCopy(ambientcolor, nl->ambientlight);
815 nl->light[0] = sl->light[0] * stylescale * colorr * 4.0f;
816 nl->light[1] = sl->light[1] * stylescale * colorg * 4.0f;
817 nl->light[2] = sl->light[2] * stylescale * colorb * 4.0f;
818 nl->subtract = sl->subtract;
819 nl->offset = sl->distbias;
822 for (i = 0;i < r_numdlights;i++)
824 if (!(modeldlightbits[i >> 5] & (1 << (i & 31))))
827 VectorSubtract (currentrenderentity->origin, rd->origin, v);
828 f = ((1.0f / (DotProduct(v, v) + LIGHTOFFSET)) - rd->subtract);
829 VectorScale(rd->light, f, ambientcolor);
830 intensity = DotProduct(ambientcolor, ambientcolor);
833 if (nearlights < maxnearlights)
837 for (j = 0;j < maxnearlights;j++)
839 if (nearlight[j].intensity < intensity)
841 if (nearlight[j].intensity > 0)
842 VectorAdd(basecolor, nearlight[j].ambientlight, basecolor);
847 if (j >= maxnearlights)
849 // this light is less significant than all others,
852 VectorAdd(basecolor, ambientcolor, basecolor);
857 nl->intensity = intensity;
858 // transform the light into the model's coordinate system
860 VectorCopy(rd->origin, nl->origin);
862 softwareuntransform(rd->origin, nl->origin);
863 // integrate mscale into falloff, for maximum speed
864 nl->falloff = mscale;
865 VectorCopy(ambientcolor, nl->ambientlight);
866 nl->light[0] = rd->light[0] * colorr * 4.0f;
867 nl->light[1] = rd->light[1] * colorg * 4.0f;
868 nl->light[2] = rd->light[2] * colorb * 4.0f;
869 nl->subtract = rd->subtract;
870 nl->offset = LIGHTOFFSET;
876 R_CompleteLightPoint (basecolor, currentrenderentity->origin, true, NULL);
878 basecolor[0] *= colorr;
879 basecolor[1] *= colorg;
880 basecolor[2] *= colorb;
881 avc = aliasvertcolor;
886 for (i = 0;i < numverts;i++)
888 VectorCopy(basecolor, color);
889 for (j = 0, nl = &nearlight[0];j < nearlights;j++, nl++)
891 VectorSubtract(nl->origin, av, v);
892 // directional shading
893 dot = DotProduct(avn,v);
896 // the vertex normal faces the light
898 // do the distance attenuation
899 dist2 = DotProduct(v,v);
900 f = (1.0f / (dist2 * nl->falloff + nl->offset)) - nl->subtract;
904 t = 1.0f / sqrt(dist2);
906 *((int *)&t) = 0x5f3759df - ((* (int *) &dist2) >> 1);
907 t = t * (1.5f - (dist2 * 0.5f * t * t));
910 // dot * t is dotproduct with a normalized v.
911 // (the result would be -1 to +1, but we already
912 // eliminated the <= 0 case, so it is 0 to 1)
914 // the hardness variables are for backlighting/shinyness
915 // these have been hardwired at * 0.5 + 0.5 to match
916 // the quake map lighting utility's equations
917 f *= dot * t;// * 0.5f + 0.5f;// * hardness + hardnessoffset;
918 VectorMA(color, f, nl->light, color);
923 VectorCopy(color, avc);
932 for (i = 0;i < numverts;i++)
934 VectorCopy(basecolor, avc);