]> icculus.org git repositories - divverent/darkplaces.git/blob - r_light.c
9fd7478296e81ebde4c9f2101ca121af5d42b89f
[divverent/darkplaces.git] / r_light.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // r_light.c
21
22 #include "quakedef.h"
23 #include "cl_collision.h"
24 #include "r_shadow.h"
25
26 rdlight_t r_dlight[MAX_DLIGHTS];
27 int r_numdlights = 0;
28
29 cvar_t r_modellights = {CVAR_SAVE, "r_modellights", "4"};
30 cvar_t r_vismarklights = {0, "r_vismarklights", "1"};
31 cvar_t r_coronas = {CVAR_SAVE, "r_coronas", "1"};
32 cvar_t gl_flashblend = {CVAR_SAVE, "gl_flashblend", "1"};
33
34 static rtexture_t *lightcorona;
35 static rtexturepool_t *lighttexturepool;
36
37 void r_light_start(void)
38 {
39         float dx, dy;
40         int x, y, a;
41         qbyte pixels[32][32][4];
42         lighttexturepool = R_AllocTexturePool();
43         for (y = 0;y < 32;y++)
44         {
45                 dy = (y - 15.5f) * (1.0f / 16.0f);
46                 for (x = 0;x < 32;x++)
47                 {
48                         dx = (x - 15.5f) * (1.0f / 16.0f);
49                         a = ((1.0f / (dx * dx + dy * dy + 0.2f)) - (1.0f / (1.0f + 0.2))) * 32.0f / (1.0f / (1.0f + 0.2));
50                         a = bound(0, a, 255);
51                         pixels[y][x][0] = a;
52                         pixels[y][x][1] = a;
53                         pixels[y][x][2] = a;
54                         pixels[y][x][3] = 255;
55                 }
56         }
57         lightcorona = R_LoadTexture2D(lighttexturepool, "lightcorona", 32, 32, &pixels[0][0][0], TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
58 }
59
60 void r_light_shutdown(void)
61 {
62         lighttexturepool = NULL;
63         lightcorona = NULL;
64 }
65
66 void r_light_newmap(void)
67 {
68         int i;
69         for (i = 0;i < 256;i++)
70                 d_lightstylevalue[i] = 264;             // normal light value
71 }
72
73 void R_Light_Init(void)
74 {
75         Cvar_RegisterVariable(&r_modellights);
76         Cvar_RegisterVariable(&r_vismarklights);
77         Cvar_RegisterVariable(&r_coronas);
78         Cvar_RegisterVariable(&gl_flashblend);
79         R_RegisterModule("R_Light", r_light_start, r_light_shutdown, r_light_newmap);
80 }
81
82 /*
83 ==================
84 R_AnimateLight
85 ==================
86 */
87 void R_AnimateLight (void)
88 {
89         int i, j, k;
90
91 //
92 // light animations
93 // 'm' is normal light, 'a' is no light, 'z' is double bright
94         i = (int)(cl.time * 10);
95         for (j = 0;j < MAX_LIGHTSTYLES;j++)
96         {
97                 if (!cl_lightstyle || !cl_lightstyle[j].length)
98                 {
99                         d_lightstylevalue[j] = 256;
100                         continue;
101                 }
102                 k = i % cl_lightstyle[j].length;
103                 k = cl_lightstyle[j].map[k] - 'a';
104                 k = k*22;
105                 d_lightstylevalue[j] = k;
106         }
107 }
108
109
110 void R_BuildLightList(void)
111 {
112         int i;
113         dlight_t *cd;
114         rdlight_t *rd;
115
116         r_numdlights = 0;
117         c_dlights = 0;
118
119         if (!r_dynamic.integer || !cl_dlights)
120                 return;
121
122         for (i = 0;i < MAX_DLIGHTS;i++)
123         {
124                 cd = cl_dlights + i;
125                 if (cd->radius <= 0)
126                         continue;
127                 rd = &r_dlight[r_numdlights++];
128                 VectorCopy(cd->origin, rd->origin);
129                 VectorScale(cd->color, cd->radius * 64.0f, rd->light);
130                 rd->cullradius2 = DotProduct(rd->light, rd->light) * (0.25f / (64.0f * 64.0f)) + 4096.0f;
131                 // clamp radius to avoid overflowing division table in lightmap code
132                 if (rd->cullradius2 > (2048.0f * 2048.0f))
133                         rd->cullradius2 = (2048.0f * 2048.0f);
134                 rd->cullradius = sqrt(rd->cullradius2);
135                 rd->subtract = 1.0f / rd->cullradius2;
136                 rd->ent = cd->ent;
137                 c_dlights++; // count every dlight in use
138         }
139 }
140
141 void R_DrawCoronas(void)
142 {
143         int i;
144         float cscale, scale, viewdist, dist;
145         rdlight_t *rd;
146         if (!r_coronas.integer)
147                 return;
148         R_Mesh_Matrix(&r_identitymatrix);
149         viewdist = DotProduct(r_origin, vpn);
150         for (i = 0;i < r_numdlights;i++)
151         {
152                 rd = r_dlight + i;
153                 dist = (DotProduct(rd->origin, vpn) - viewdist);
154                 if (dist >= 24.0f && CL_TraceLine(rd->origin, r_origin, NULL, NULL, 0, true, NULL) == 1)
155                 {
156                         cscale = (1.0f / 131072.0f);
157                         scale = rd->cullradius * 0.25f;
158                         if (gl_flashblend.integer)
159                         {
160                                 cscale *= 4.0f;
161                                 scale *= 2.0f;
162                         }
163                         R_DrawSprite(GL_ONE, GL_ONE, lightcorona, true, rd->origin, vright, vup, scale, -scale, -scale, scale, rd->light[0] * cscale, rd->light[1] * cscale, rd->light[2] * cscale, 1);
164                 }
165         }
166 }
167
168 /*
169 =============================================================================
170
171 DYNAMIC LIGHTS
172
173 =============================================================================
174 */
175
176 static int lightpvsbytes;
177 static qbyte lightpvs[(MAX_MAP_LEAFS+7)>>3];
178
179 /*
180 =============
181 R_MarkLights
182 =============
183 */
184 static void R_RecursiveMarkLights(entity_render_t *ent, vec3_t lightorigin, rdlight_t *rd, int bit, int bitindex, mnode_t *node, qbyte *pvs, int pvsbits)
185 {
186         int i;
187         mleaf_t *leaf;
188         float dist;
189
190         // for comparisons to minimum acceptable light
191         while(node->contents >= 0)
192         {
193                 dist = PlaneDiff(lightorigin, node->plane);
194                 if (dist > rd->cullradius)
195                         node = node->children[0];
196                 else
197                 {
198                         if (dist >= -rd->cullradius)
199                                 R_RecursiveMarkLights(ent, lightorigin, rd, bit, bitindex, node->children[0], pvs, pvsbits);
200                         node = node->children[1];
201                 }
202         }
203
204         // check if leaf is visible according to pvs
205         leaf = (mleaf_t *)node;
206         i = (leaf - ent->model->brushq1.leafs) - 1;
207         if (leaf->nummarksurfaces && (i >= pvsbits || pvs[i >> 3] & (1 << (i & 7))))
208         {
209                 int *surfacepvsframes, d, impacts, impactt;
210                 float sdist, maxdist, dist2, impact[3];
211                 msurface_t *surf;
212                 // mark the polygons
213                 maxdist = rd->cullradius2;
214                 surfacepvsframes = ent->model->brushq1.surfacepvsframes;
215                 for (i = 0;i < leaf->nummarksurfaces;i++)
216                 {
217                         if (surfacepvsframes[leaf->firstmarksurface[i]] != ent->model->brushq1.pvsframecount)
218                                 continue;
219                         surf = ent->model->brushq1.surfaces + leaf->firstmarksurface[i];
220                         dist = sdist = PlaneDiff(lightorigin, surf->plane);
221                         if (surf->flags & SURF_PLANEBACK)
222                                 dist = -dist;
223
224                         if (dist < -0.25f && !(surf->flags & SURF_LIGHTBOTHSIDES))
225                                 continue;
226
227                         dist2 = dist * dist;
228                         if (dist2 >= maxdist)
229                                 continue;
230
231                         VectorCopy(lightorigin, impact);
232                         if (surf->plane->type >= 3)
233                                 VectorMA(impact, -sdist, surf->plane->normal, impact);
234                         else
235                                 impact[surf->plane->type] -= sdist;
236
237                         impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
238
239                         d = bound(0, impacts, surf->extents[0] + 16) - impacts;
240                         dist2 += d * d;
241                         if (dist2 > maxdist)
242                                 continue;
243
244                         impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
245
246                         d = bound(0, impactt, surf->extents[1] + 16) - impactt;
247                         dist2 += d * d;
248                         if (dist2 > maxdist)
249                                 continue;
250
251                         if (surf->dlightframe != r_framecount) // not dynamic until now
252                         {
253                                 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;
254                                 surf->dlightframe = r_framecount;
255                                 if (r_dlightmap.integer)
256                                         surf->cached_dlight = true;
257                         }
258                         surf->dlightbits[bitindex] |= bit;
259                 }
260         }
261 }
262
263 void R_MarkLights(entity_render_t *ent)
264 {
265         int i, bit, bitindex;
266         rdlight_t *rd;
267         vec3_t lightorigin;
268         if (!gl_flashblend.integer && r_dynamic.integer && ent->model)
269         {
270                 for (i = 0, rd = r_dlight;i < r_numdlights;i++, rd++)
271                 {
272                         bit = 1 << (i & 31);
273                         bitindex = i >> 5;
274                         Matrix4x4_Transform(&ent->inversematrix, rd->origin, lightorigin);
275                         lightpvsbytes = 0;
276                         if (r_vismarklights.integer && ent->model->brush.FatPVS)
277                                 lightpvsbytes = ent->model->brush.FatPVS(ent->model, lightorigin, 0, lightpvs, sizeof(lightpvs));
278                         R_RecursiveMarkLights(ent, lightorigin, rd, bit, bitindex, ent->model->brushq1.nodes + ent->model->brushq1.hulls[0].firstclipnode, lightpvs, min(lightpvsbytes * 8, ent->model->brushq1.num_leafs - 1));
279                 }
280         }
281 }
282
283 /*
284 =============================================================================
285
286 LIGHT SAMPLING
287
288 =============================================================================
289 */
290
291 void R_CompleteLightPoint(vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal, const vec3_t p, int dynamic, const mleaf_t *leaf)
292 {
293         VectorClear(diffusecolor);
294         VectorClear(diffusenormal);
295
296         if (!r_fullbright.integer && cl.worldmodel && cl.worldmodel->brush.LightPoint)
297         {
298                 ambientcolor[0] = ambientcolor[1] = ambientcolor[2] = r_ambient.value * (2.0f / 128.0f);
299                 cl.worldmodel->brush.LightPoint(cl.worldmodel, p, ambientcolor, diffusecolor, diffusenormal);
300         }
301         else
302                 VectorSet(ambientcolor, 1, 1, 1);
303
304         /*
305         if (leaf == NULL && cl.worldmodel != NULL)
306                 leaf = cl.worldmodel->brushq1.PointInLeaf(cl.worldmodel, p);
307         if (!leaf || leaf->contents == CONTENTS_SOLID || !cl.worldmodel->brushq1.lightdata)
308         {
309                 VectorSet(ambientcolor, 1, 1, 1);
310                 return;
311         }
312         */
313
314         // FIXME: this .lights related stuff needs to be ported into the Mod_Q1BSP code
315         if (cl.worldmodel->brushq1.numlights)
316         {
317                 int i;
318                 vec3_t v;
319                 float f;
320                 mlight_t *sl;
321                 for (i = 0;i < cl.worldmodel->brushq1.numlights;i++)
322                 {
323                         sl = cl.worldmodel->brushq1.lights + i;
324                         if (d_lightstylevalue[sl->style] > 0)
325                         {
326                                 VectorSubtract (p, sl->origin, v);
327                                 f = ((1.0f / (DotProduct(v, v) * sl->falloff + sl->distbias)) - sl->subtract);
328                                 if (f > 0 && CL_TraceLine(p, sl->origin, NULL, NULL, 0, false, NULL) == 1)
329                                 {
330                                         f *= d_lightstylevalue[sl->style] * (1.0f / 65536.0f);
331                                         VectorMA(ambientcolor, f, sl->light, ambientcolor);
332                                 }
333                         }
334                 }
335         }
336
337         if (dynamic)
338         {
339                 int i;
340                 float f, v[3];
341                 rdlight_t *rd;
342                 // FIXME: this really should handle dlights as diffusecolor/diffusenormal somehow
343                 for (i = 0;i < r_numdlights;i++)
344                 {
345                         rd = r_dlight + i;
346                         VectorSubtract(p, rd->origin, v);
347                         f = DotProduct(v, v);
348                         if (f < rd->cullradius2 && CL_TraceLine(p, rd->origin, NULL, NULL, 0, false, NULL) == 1)
349                         {
350                                 f = (1.0f / (f + LIGHTOFFSET)) - rd->subtract;
351                                 VectorMA(ambientcolor, f, rd->light, ambientcolor);
352                         }
353                 }
354         }
355 }
356
357 typedef struct
358 {
359         vec3_t origin;
360         //vec_t cullradius2;
361         vec3_t light;
362         // how much this light would contribute to ambient if replaced
363         vec3_t ambientlight;
364         vec_t subtract;
365         vec_t falloff;
366         vec_t offset;
367         // used for choosing only the brightest lights
368         vec_t intensity;
369 }
370 nearlight_t;
371
372 static int nearlights;
373 static nearlight_t nearlight[MAX_DLIGHTS];
374
375 int R_LightModel(float *ambient4f, float *diffusecolor, float *diffusenormal, const entity_render_t *ent, float colorr, float colorg, float colorb, float colora, int worldcoords)
376 {
377         int i, j, maxnearlights;
378         float v[3], f, mscale, stylescale, intensity, ambientcolor[3];
379         nearlight_t *nl;
380         mlight_t *sl;
381         rdlight_t *rd;
382
383         nearlights = 0;
384         maxnearlights = r_modellights.integer;
385         ambient4f[0] = ambient4f[1] = ambient4f[2] = r_ambient.value * (2.0f / 128.0f);
386         VectorClear(diffusecolor);
387         VectorClear(diffusenormal);
388         if (r_fullbright.integer || (ent->effects & EF_FULLBRIGHT))
389         {
390                 // highly rare
391                 VectorSet(ambient4f, 1, 1, 1);
392                 maxnearlights = 0;
393         }
394         else if (r_shadow_realtime_world.integer)
395                 maxnearlights = 0;
396         else
397         {
398                 if (cl.worldmodel && cl.worldmodel->brush.LightPoint)
399                         cl.worldmodel->brush.LightPoint(cl.worldmodel, ent->origin, ambient4f, diffusecolor, diffusenormal);
400                 else
401                         VectorSet(ambient4f, 1, 1, 1);
402         }
403
404         // scale of the model's coordinate space, to alter light attenuation to match
405         // make the mscale squared so it can scale the squared distance results
406         mscale = ent->scale * ent->scale;
407         // FIXME: no support for .lights on non-Q1BSP?
408         nl = &nearlight[0];
409         for (i = 0;i < ent->numentlights;i++)
410         {
411                 sl = cl.worldmodel->brushq1.lights + ent->entlights[i];
412                 stylescale = d_lightstylevalue[sl->style] * (1.0f / 65536.0f);
413                 VectorSubtract (ent->origin, sl->origin, v);
414                 f = ((1.0f / (DotProduct(v, v) * sl->falloff + sl->distbias)) - sl->subtract) * stylescale;
415                 VectorScale(sl->light, f, ambientcolor);
416                 intensity = DotProduct(ambientcolor, ambientcolor);
417                 if (f < 0)
418                         intensity *= -1.0f;
419                 if (nearlights < maxnearlights)
420                         j = nearlights++;
421                 else
422                 {
423                         for (j = 0;j < maxnearlights;j++)
424                         {
425                                 if (nearlight[j].intensity < intensity)
426                                 {
427                                         if (nearlight[j].intensity > 0)
428                                                 VectorAdd(ambient4f, nearlight[j].ambientlight, ambient4f);
429                                         break;
430                                 }
431                         }
432                 }
433                 if (j >= maxnearlights)
434                 {
435                         // this light is less significant than all others,
436                         // add it to ambient
437                         if (intensity > 0)
438                                 VectorAdd(ambient4f, ambientcolor, ambient4f);
439                 }
440                 else
441                 {
442                         nl = nearlight + j;
443                         nl->intensity = intensity;
444                         // transform the light into the model's coordinate system
445                         if (worldcoords)
446                                 VectorCopy(sl->origin, nl->origin);
447                         else
448                                 Matrix4x4_Transform(&ent->inversematrix, sl->origin, nl->origin);
449                         // integrate mscale into falloff, for maximum speed
450                         nl->falloff = sl->falloff * mscale;
451                         VectorCopy(ambientcolor, nl->ambientlight);
452                         nl->light[0] = sl->light[0] * stylescale * colorr * 4.0f;
453                         nl->light[1] = sl->light[1] * stylescale * colorg * 4.0f;
454                         nl->light[2] = sl->light[2] * stylescale * colorb * 4.0f;
455                         nl->subtract = sl->subtract;
456                         nl->offset = sl->distbias;
457                 }
458         }
459         if (!r_shadow_realtime_dlight.integer)
460         {
461                 for (i = 0;i < r_numdlights;i++)
462                 {
463                         rd = r_dlight + i;
464                         VectorCopy(rd->origin, v);
465                         if (v[0] < ent->mins[0]) v[0] = ent->mins[0];if (v[0] > ent->maxs[0]) v[0] = ent->maxs[0];
466                         if (v[1] < ent->mins[1]) v[1] = ent->mins[1];if (v[1] > ent->maxs[1]) v[1] = ent->maxs[1];
467                         if (v[2] < ent->mins[2]) v[2] = ent->mins[2];if (v[2] > ent->maxs[2]) v[2] = ent->maxs[2];
468                         VectorSubtract (v, rd->origin, v);
469                         if (DotProduct(v, v) < rd->cullradius2)
470                         {
471                                 if (CL_TraceLine(ent->origin, rd->origin, NULL, NULL, 0, false, NULL) != 1)
472                                         continue;
473                                 VectorSubtract (ent->origin, rd->origin, v);
474                                 f = ((1.0f / (DotProduct(v, v) + LIGHTOFFSET)) - rd->subtract);
475                                 VectorScale(rd->light, f, ambientcolor);
476                                 intensity = DotProduct(ambientcolor, ambientcolor);
477                                 if (f < 0)
478                                         intensity *= -1.0f;
479                                 if (nearlights < maxnearlights)
480                                         j = nearlights++;
481                                 else
482                                 {
483                                         for (j = 0;j < maxnearlights;j++)
484                                         {
485                                                 if (nearlight[j].intensity < intensity)
486                                                 {
487                                                         if (nearlight[j].intensity > 0)
488                                                                 VectorAdd(ambient4f, nearlight[j].ambientlight, ambient4f);
489                                                         break;
490                                                 }
491                                         }
492                                 }
493                                 if (j >= maxnearlights)
494                                 {
495                                         // this light is less significant than all others,
496                                         // add it to ambient
497                                         if (intensity > 0)
498                                                 VectorAdd(ambient4f, ambientcolor, ambient4f);
499                                 }
500                                 else
501                                 {
502                                         nl = nearlight + j;
503                                         nl->intensity = intensity;
504                                         // transform the light into the model's coordinate system
505                                         if (worldcoords)
506                                                 VectorCopy(rd->origin, nl->origin);
507                                         else
508                                         {
509                                                 Matrix4x4_Transform(&ent->inversematrix, rd->origin, nl->origin);
510                                                 /*
511                                                 Con_Printf("%i %s : %f %f %f : %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n"
512                                                 , rd - r_dlight, ent->model->name
513                                                 , rd->origin[0], rd->origin[1], rd->origin[2]
514                                                 , nl->origin[0], nl->origin[1], nl->origin[2]
515                                                 , ent->inversematrix.m[0][0], ent->inversematrix.m[0][1], ent->inversematrix.m[0][2], ent->inversematrix.m[0][3]
516                                                 , ent->inversematrix.m[1][0], ent->inversematrix.m[1][1], ent->inversematrix.m[1][2], ent->inversematrix.m[1][3]
517                                                 , ent->inversematrix.m[2][0], ent->inversematrix.m[2][1], ent->inversematrix.m[2][2], ent->inversematrix.m[2][3]
518                                                 , ent->inversematrix.m[3][0], ent->inversematrix.m[3][1], ent->inversematrix.m[3][2], ent->inversematrix.m[3][3]);
519                                                 */
520                                         }
521                                         // integrate mscale into falloff, for maximum speed
522                                         nl->falloff = mscale;
523                                         VectorCopy(ambientcolor, nl->ambientlight);
524                                         nl->light[0] = rd->light[0] * colorr * 4.0f;
525                                         nl->light[1] = rd->light[1] * colorg * 4.0f;
526                                         nl->light[2] = rd->light[2] * colorb * 4.0f;
527                                         nl->subtract = rd->subtract;
528                                         nl->offset = LIGHTOFFSET;
529                                 }
530                         }
531                 }
532         }
533         ambient4f[0] *= colorr;
534         ambient4f[1] *= colorg;
535         ambient4f[2] *= colorb;
536         ambient4f[3] = colora;
537         diffusecolor[0] *= colorr;
538         diffusecolor[1] *= colorg;
539         diffusecolor[2] *= colorb;
540         return nearlights != 0 || DotProduct(diffusecolor, diffusecolor) > 0;
541 }
542
543 void R_LightModel_CalcVertexColors(const float *ambientcolor4f, const float *diffusecolor, const float *diffusenormal, int numverts, const float *vertex3f, const float *normal3f, float *color4f)
544 {
545         int i, j, usediffuse;
546         float color[4], v[3], dot, dist2, f, dnormal[3];
547         nearlight_t *nl;
548         usediffuse = DotProduct(diffusecolor, diffusecolor) > 0;
549         VectorCopy(diffusenormal, dnormal);
550         if (usediffuse)
551                 VectorNormalize(dnormal);
552         // directional shading code here
553         for (i = 0;i < numverts;i++, vertex3f += 3, normal3f += 3, color4f += 4)
554         {
555                 VectorCopy4(ambientcolor4f, color);
556
557                 // silly directional diffuse shading
558                 if (usediffuse)
559                 {
560                         dot = DotProduct(normal3f, dnormal);
561                         if (dot > 0)
562                                 VectorMA(color, dot, diffusecolor, color);
563                 }
564
565                 // pretty good lighting
566                 for (j = 0, nl = &nearlight[0];j < nearlights;j++, nl++)
567                 {
568                         VectorSubtract(vertex3f, nl->origin, v);
569                         // first eliminate negative lighting (back side)
570                         dot = DotProduct(normal3f, v);
571                         if (dot > 0)
572                         {
573                                 // we'll need this again later to normalize the dotproduct
574                                 dist2 = DotProduct(v,v);
575                                 // do the distance attenuation math
576                                 f = (1.0f / (dist2 * nl->falloff + nl->offset)) - nl->subtract;
577                                 if (f > 0)
578                                 {
579                                         // we must divide dot by sqrt(dist2) to compensate for
580                                         // the fact we did not normalize v before doing the
581                                         // dotproduct, the result is in the range 0 to 1 (we
582                                         // eliminated negative numbers already)
583                                         f *= dot / sqrt(dist2);
584                                         // blend in the lighting
585                                         VectorMA(color, f, nl->light, color);
586                                 }
587                         }
588                 }
589                 VectorCopy4(color, color4f);
590         }
591 }
592
593 void R_UpdateEntLights(entity_render_t *ent)
594 {
595         int i;
596         const mlight_t *sl;
597         vec3_t v;
598         if (r_shadow_realtime_dlight.integer || gl_flashblend.integer)
599                 return;
600         VectorSubtract(ent->origin, ent->entlightsorigin, v);
601         if (ent->entlightsframe != (r_framecount - 1) || (realtime > ent->entlightstime && DotProduct(v,v) >= 1.0f))
602         {
603                 ent->entlightstime = realtime + 0.1;
604                 VectorCopy(ent->origin, ent->entlightsorigin);
605                 ent->numentlights = 0;
606                 if (cl.worldmodel)
607                         for (i = 0, sl = cl.worldmodel->brushq1.lights;i < cl.worldmodel->brushq1.numlights && ent->numentlights < MAXENTLIGHTS;i++, sl++)
608                                 if (CL_TraceLine(ent->origin, sl->origin, NULL, NULL, 0, false, NULL) == 1)
609                                         ent->entlights[ent->numentlights++] = i;
610         }
611         ent->entlightsframe = r_framecount;
612 }
613