]> icculus.org git repositories - divverent/darkplaces.git/blob - r_light.c
no view tilt when dead in GAME_FNIGGIUM
[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 dlight_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_UpdateLights
85 ==================
86 */
87 void R_UpdateLights(void)
88 {
89         int i, j, k;
90
91 // light animations
92 // 'm' is normal light, 'a' is no light, 'z' is double bright
93         i = (int)(cl.time * 10);
94         for (j = 0;j < MAX_LIGHTSTYLES;j++)
95         {
96                 if (!cl_lightstyle || !cl_lightstyle[j].length)
97                 {
98                         d_lightstylevalue[j] = 256;
99                         continue;
100                 }
101                 k = i % cl_lightstyle[j].length;
102                 k = cl_lightstyle[j].map[k] - 'a';
103                 k = k*22;
104                 d_lightstylevalue[j] = k;
105         }
106
107         r_numdlights = 0;
108         c_dlights = 0;
109
110         if (!r_dynamic.integer || !cl_dlights)
111                 return;
112
113         for (i = 0;i < MAX_DLIGHTS;i++)
114         {
115                 if (cl_dlights[i].radius > 0)
116                 {
117                         R_RTLight_UpdateFromDLight(&cl_dlights[i].rtlight, &cl_dlights[i], false);
118                         // FIXME: use pointer instead of copy
119                         r_dlight[r_numdlights++] = cl_dlights[i];
120                         c_dlights++; // count every dlight in use
121                 }
122         }
123 }
124
125 void R_DrawCoronas(void)
126 {
127         int i, lnum;
128         float cscale, scale, viewdist, dist;
129         dlight_t *light;
130         if (!r_coronas.integer)
131                 return;
132         R_Mesh_Matrix(&r_identitymatrix);
133         viewdist = DotProduct(r_vieworigin, r_viewforward);
134         if (r_shadow_realtime_world.integer)
135         {
136                 for (lnum = 0, light = r_shadow_worldlightchain;light;light = light->next, lnum++)
137                 {
138                         if (light->rtlight.corona * r_coronas.value > 0 && (r_shadow_debuglight.integer < 0 || r_shadow_debuglight.integer == lnum) && (dist = (DotProduct(light->rtlight.shadoworigin, r_viewforward) - viewdist)) >= 24.0f && CL_TraceLine(light->rtlight.shadoworigin, r_vieworigin, NULL, NULL, true, NULL, SUPERCONTENTS_SOLID) == 1)
139                         {
140                                 cscale = light->rtlight.corona * r_coronas.value * 0.25f;
141                                 scale = light->rtlight.radius * 0.25f;
142                                 R_DrawSprite(GL_ONE, GL_ONE, lightcorona, true, light->rtlight.shadoworigin, r_viewright, r_viewup, scale, -scale, -scale, scale, light->rtlight.color[0] * cscale, light->rtlight.color[1] * cscale, light->rtlight.color[2] * cscale, 1);
143                         }
144                 }
145         }
146         for (i = 0, light = r_dlight;i < r_numdlights;i++, light++)
147         {
148                 if (light->corona * r_coronas.value > 0 && (dist = (DotProduct(light->origin, r_viewforward) - viewdist)) >= 24.0f && CL_TraceLine(light->origin, r_vieworigin, NULL, NULL, true, NULL, SUPERCONTENTS_SOLID) == 1)
149                 {
150                         cscale = light->corona * r_coronas.value * 0.25f;
151                         scale = light->radius * 0.25f;
152                         if (gl_flashblend.integer)
153                         {
154                                 cscale *= 4.0f;
155                                 scale *= 2.0f;
156                         }
157                         R_DrawSprite(GL_ONE, GL_ONE, lightcorona, true, light->origin, r_viewright, r_viewup, scale, -scale, -scale, scale, light->color[0] * cscale, light->color[1] * cscale, light->color[2] * cscale, 1);
158                 }
159         }
160 }
161
162 /*
163 =============================================================================
164
165 DYNAMIC LIGHTS
166
167 =============================================================================
168 */
169
170 static int lightpvsbytes;
171 static qbyte lightpvs[(MAX_MAP_LEAFS+7)>>3];
172
173 /*
174 =============
175 R_MarkLights
176 =============
177 */
178 static void R_RecursiveMarkLights(entity_render_t *ent, vec3_t lightorigin, dlight_t *light, int bit, int bitindex, mnode_t *node, qbyte *pvs, int pvsbits)
179 {
180         int i;
181         mleaf_t *leaf;
182         float dist;
183
184         // for comparisons to minimum acceptable light
185         while(node->contents >= 0)
186         {
187                 dist = PlaneDiff(lightorigin, node->plane);
188                 if (dist > light->rtlight.lightmap_cullradius)
189                         node = node->children[0];
190                 else
191                 {
192                         if (dist >= -light->rtlight.lightmap_cullradius)
193                                 R_RecursiveMarkLights(ent, lightorigin, light, bit, bitindex, node->children[0], pvs, pvsbits);
194                         node = node->children[1];
195                 }
196         }
197
198         // check if leaf is visible according to pvs
199         leaf = (mleaf_t *)node;
200         i = leaf->clusterindex;
201         if (leaf->nummarksurfaces && (i >= pvsbits || CHECKPVSBIT(pvs, i)))
202         {
203                 int *surfacepvsframes, d, impacts, impactt;
204                 float sdist, maxdist, dist2, impact[3];
205                 msurface_t *surf;
206                 // mark the polygons
207                 maxdist = light->rtlight.lightmap_cullradius2;
208                 surfacepvsframes = ent->model->brushq1.surfacepvsframes;
209                 for (i = 0;i < leaf->nummarksurfaces;i++)
210                 {
211                         if (surfacepvsframes[leaf->firstmarksurface[i]] != ent->model->brushq1.pvsframecount)
212                                 continue;
213                         surf = ent->model->brushq1.surfaces + leaf->firstmarksurface[i];
214                         dist = sdist = PlaneDiff(lightorigin, surf->plane);
215                         if (surf->flags & SURF_PLANEBACK)
216                                 dist = -dist;
217
218                         if (dist < -0.25f && !(surf->flags & SURF_LIGHTBOTHSIDES))
219                                 continue;
220
221                         dist2 = dist * dist;
222                         if (dist2 >= maxdist)
223                                 continue;
224
225                         VectorCopy(lightorigin, impact);
226                         if (surf->plane->type >= 3)
227                                 VectorMA(impact, -sdist, surf->plane->normal, impact);
228                         else
229                                 impact[surf->plane->type] -= sdist;
230
231                         impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
232
233                         d = bound(0, impacts, surf->extents[0] + 16) - impacts;
234                         dist2 += d * d;
235                         if (dist2 > maxdist)
236                                 continue;
237
238                         impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
239
240                         d = bound(0, impactt, surf->extents[1] + 16) - impactt;
241                         dist2 += d * d;
242                         if (dist2 > maxdist)
243                                 continue;
244
245                         if (surf->dlightframe != r_framecount) // not dynamic until now
246                         {
247                                 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;
248                                 surf->dlightframe = r_framecount;
249                                 surf->cached_dlight = true;
250                         }
251                         surf->dlightbits[bitindex] |= bit;
252                 }
253         }
254 }
255
256 void R_MarkLights(entity_render_t *ent)
257 {
258         int i, bit, bitindex;
259         dlight_t *light;
260         vec3_t lightorigin;
261         if (!gl_flashblend.integer && r_dynamic.integer && ent->model && ent->model->brushq1.num_leafs)
262         {
263                 for (i = 0, light = r_dlight;i < r_numdlights;i++, light++)
264                 {
265                         bit = 1 << (i & 31);
266                         bitindex = i >> 5;
267                         Matrix4x4_Transform(&ent->inversematrix, light->origin, lightorigin);
268                         lightpvsbytes = 0;
269                         if (r_vismarklights.integer && ent->model->brush.FatPVS)
270                                 lightpvsbytes = ent->model->brush.FatPVS(ent->model, lightorigin, 0, lightpvs, sizeof(lightpvs));
271                         R_RecursiveMarkLights(ent, lightorigin, light, bit, bitindex, ent->model->brushq1.nodes + ent->model->brushq1.hulls[0].firstclipnode, lightpvs, min(lightpvsbytes * 8, ent->model->brush.num_pvsclusters));
272                 }
273         }
274 }
275
276 /*
277 =============================================================================
278
279 LIGHT SAMPLING
280
281 =============================================================================
282 */
283
284 void R_CompleteLightPoint(vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal, const vec3_t p, int dynamic, const mleaf_t *leaf)
285 {
286         VectorClear(diffusecolor);
287         VectorClear(diffusenormal);
288
289         if (!r_fullbright.integer && cl.worldmodel && cl.worldmodel->brush.LightPoint)
290         {
291                 ambientcolor[0] = ambientcolor[1] = ambientcolor[2] = r_ambient.value * (2.0f / 128.0f);
292                 cl.worldmodel->brush.LightPoint(cl.worldmodel, p, ambientcolor, diffusecolor, diffusenormal);
293         }
294         else
295                 VectorSet(ambientcolor, 1, 1, 1);
296
297         // FIXME: this .lights related stuff needs to be ported into the Mod_Q1BSP code
298         if (cl.worldmodel->brushq1.numlights)
299         {
300                 int i;
301                 vec3_t v;
302                 float f;
303                 mlight_t *sl;
304                 for (i = 0;i < cl.worldmodel->brushq1.numlights;i++)
305                 {
306                         sl = cl.worldmodel->brushq1.lights + i;
307                         if (d_lightstylevalue[sl->style] > 0)
308                         {
309                                 VectorSubtract (p, sl->origin, v);
310                                 f = ((1.0f / (DotProduct(v, v) * sl->falloff + sl->distbias)) - sl->subtract);
311                                 if (f > 0 && CL_TraceLine(p, sl->origin, NULL, NULL, false, NULL, SUPERCONTENTS_SOLID) == 1)
312                                 {
313                                         f *= d_lightstylevalue[sl->style] * (1.0f / 65536.0f);
314                                         VectorMA(ambientcolor, f, sl->light, ambientcolor);
315                                 }
316                         }
317                 }
318         }
319
320         if (dynamic)
321         {
322                 int i;
323                 float f, v[3];
324                 dlight_t *light;
325                 // FIXME: this really should handle dlights as diffusecolor/diffusenormal somehow
326                 for (i = 0;i < r_numdlights;i++)
327                 {
328                         light = r_dlight + i;
329                         VectorSubtract(p, light->origin, v);
330                         f = DotProduct(v, v);
331                         if (f < light->rtlight.lightmap_cullradius2 && CL_TraceLine(p, light->origin, NULL, NULL, false, NULL, SUPERCONTENTS_SOLID) == 1)
332                         {
333                                 f = (1.0f / (f + LIGHTOFFSET)) - light->rtlight.lightmap_subtract;
334                                 VectorMA(ambientcolor, f, light->rtlight.lightmap_light, ambientcolor);
335                         }
336                 }
337         }
338 }
339
340 typedef struct
341 {
342         vec3_t origin;
343         //vec_t cullradius2;
344         vec3_t light;
345         // how much this light would contribute to ambient if replaced
346         vec3_t ambientlight;
347         vec_t subtract;
348         vec_t falloff;
349         vec_t offset;
350         // used for choosing only the brightest lights
351         vec_t intensity;
352 }
353 nearlight_t;
354
355 static int nearlights;
356 static nearlight_t nearlight[MAX_DLIGHTS];
357
358 int R_LightModel(float *ambient4f, float *diffusecolor, float *diffusenormal, const entity_render_t *ent, float colorr, float colorg, float colorb, float colora, int worldcoords)
359 {
360         int i, j, maxnearlights;
361         float v[3], f, mscale, stylescale, intensity, ambientcolor[3], tempdiffusenormal[3];
362         nearlight_t *nl;
363         mlight_t *sl;
364         dlight_t *light;
365
366         nearlights = 0;
367         maxnearlights = r_modellights.integer;
368         ambient4f[0] = ambient4f[1] = ambient4f[2] = r_ambient.value * (2.0f / 128.0f);
369         VectorClear(diffusecolor);
370         VectorClear(diffusenormal);
371         if (r_fullbright.integer || (ent->effects & EF_FULLBRIGHT))
372         {
373                 // highly rare
374                 VectorSet(ambient4f, 1, 1, 1);
375                 maxnearlights = 0;
376         }
377         else if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value <= 0)
378                 maxnearlights = 0;
379         else
380         {
381                 if (cl.worldmodel && cl.worldmodel->brush.LightPoint)
382                 {
383                         cl.worldmodel->brush.LightPoint(cl.worldmodel, ent->origin, ambient4f, diffusecolor, tempdiffusenormal);
384                         Matrix4x4_Transform3x3(&ent->inversematrix, tempdiffusenormal, diffusenormal);
385                         VectorNormalize(diffusenormal);
386                 }
387                 else
388                         VectorSet(ambient4f, 1, 1, 1);
389         }
390
391         // scale of the model's coordinate space, to alter light attenuation to match
392         // make the mscale squared so it can scale the squared distance results
393         mscale = ent->scale * ent->scale;
394         // FIXME: no support for .lights on non-Q1BSP?
395         nl = &nearlight[0];
396         for (i = 0;i < ent->numentlights;i++)
397         {
398                 sl = cl.worldmodel->brushq1.lights + ent->entlights[i];
399                 stylescale = d_lightstylevalue[sl->style] * (1.0f / 65536.0f);
400                 VectorSubtract (ent->origin, sl->origin, v);
401                 f = ((1.0f / (DotProduct(v, v) * sl->falloff + sl->distbias)) - sl->subtract) * stylescale;
402                 VectorScale(sl->light, f, ambientcolor);
403                 intensity = DotProduct(ambientcolor, ambientcolor);
404                 if (f < 0)
405                         intensity *= -1.0f;
406                 if (nearlights < maxnearlights)
407                         j = nearlights++;
408                 else
409                 {
410                         for (j = 0;j < maxnearlights;j++)
411                         {
412                                 if (nearlight[j].intensity < intensity)
413                                 {
414                                         if (nearlight[j].intensity > 0)
415                                                 VectorAdd(ambient4f, nearlight[j].ambientlight, ambient4f);
416                                         break;
417                                 }
418                         }
419                 }
420                 if (j >= maxnearlights)
421                 {
422                         // this light is less significant than all others,
423                         // add it to ambient
424                         if (intensity > 0)
425                                 VectorAdd(ambient4f, ambientcolor, ambient4f);
426                 }
427                 else
428                 {
429                         nl = nearlight + j;
430                         nl->intensity = intensity;
431                         // transform the light into the model's coordinate system
432                         if (worldcoords)
433                                 VectorCopy(sl->origin, nl->origin);
434                         else
435                                 Matrix4x4_Transform(&ent->inversematrix, sl->origin, nl->origin);
436                         // integrate mscale into falloff, for maximum speed
437                         nl->falloff = sl->falloff * mscale;
438                         VectorCopy(ambientcolor, nl->ambientlight);
439                         nl->light[0] = sl->light[0] * stylescale * colorr * 4.0f;
440                         nl->light[1] = sl->light[1] * stylescale * colorg * 4.0f;
441                         nl->light[2] = sl->light[2] * stylescale * colorb * 4.0f;
442                         nl->subtract = sl->subtract;
443                         nl->offset = sl->distbias;
444                 }
445         }
446         if (!r_shadow_realtime_dlight.integer)
447         {
448                 for (i = 0;i < r_numdlights;i++)
449                 {
450                         light = r_dlight + i;
451                         VectorCopy(light->origin, v);
452                         if (v[0] < ent->mins[0]) v[0] = ent->mins[0];if (v[0] > ent->maxs[0]) v[0] = ent->maxs[0];
453                         if (v[1] < ent->mins[1]) v[1] = ent->mins[1];if (v[1] > ent->maxs[1]) v[1] = ent->maxs[1];
454                         if (v[2] < ent->mins[2]) v[2] = ent->mins[2];if (v[2] > ent->maxs[2]) v[2] = ent->maxs[2];
455                         VectorSubtract (v, light->origin, v);
456                         if (DotProduct(v, v) < light->rtlight.lightmap_cullradius2)
457                         {
458                                 if (CL_TraceLine(ent->origin, light->origin, NULL, NULL, false, NULL, SUPERCONTENTS_SOLID) != 1)
459                                         continue;
460                                 VectorSubtract (ent->origin, light->origin, v);
461                                 f = ((1.0f / (DotProduct(v, v) + LIGHTOFFSET)) - light->rtlight.lightmap_subtract);
462                                 VectorScale(light->rtlight.lightmap_light, f, ambientcolor);
463                                 intensity = DotProduct(ambientcolor, ambientcolor);
464                                 if (f < 0)
465                                         intensity *= -1.0f;
466                                 if (nearlights < maxnearlights)
467                                         j = nearlights++;
468                                 else
469                                 {
470                                         for (j = 0;j < maxnearlights;j++)
471                                         {
472                                                 if (nearlight[j].intensity < intensity)
473                                                 {
474                                                         if (nearlight[j].intensity > 0)
475                                                                 VectorAdd(ambient4f, nearlight[j].ambientlight, ambient4f);
476                                                         break;
477                                                 }
478                                         }
479                                 }
480                                 if (j >= maxnearlights)
481                                 {
482                                         // this light is less significant than all others,
483                                         // add it to ambient
484                                         if (intensity > 0)
485                                                 VectorAdd(ambient4f, ambientcolor, ambient4f);
486                                 }
487                                 else
488                                 {
489                                         nl = nearlight + j;
490                                         nl->intensity = intensity;
491                                         // transform the light into the model's coordinate system
492                                         if (worldcoords)
493                                                 VectorCopy(light->origin, nl->origin);
494                                         else
495                                         {
496                                                 Matrix4x4_Transform(&ent->inversematrix, light->origin, nl->origin);
497                                                 /*
498                                                 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"
499                                                 , rd - r_dlight, ent->model->name
500                                                 , light->origin[0], light->origin[1], light->origin[2]
501                                                 , nl->origin[0], nl->origin[1], nl->origin[2]
502                                                 , ent->inversematrix.m[0][0], ent->inversematrix.m[0][1], ent->inversematrix.m[0][2], ent->inversematrix.m[0][3]
503                                                 , ent->inversematrix.m[1][0], ent->inversematrix.m[1][1], ent->inversematrix.m[1][2], ent->inversematrix.m[1][3]
504                                                 , ent->inversematrix.m[2][0], ent->inversematrix.m[2][1], ent->inversematrix.m[2][2], ent->inversematrix.m[2][3]
505                                                 , ent->inversematrix.m[3][0], ent->inversematrix.m[3][1], ent->inversematrix.m[3][2], ent->inversematrix.m[3][3]);
506                                                 */
507                                         }
508                                         // integrate mscale into falloff, for maximum speed
509                                         nl->falloff = mscale;
510                                         VectorCopy(ambientcolor, nl->ambientlight);
511                                         nl->light[0] = light->rtlight.lightmap_light[0] * colorr * 4.0f;
512                                         nl->light[1] = light->rtlight.lightmap_light[1] * colorg * 4.0f;
513                                         nl->light[2] = light->rtlight.lightmap_light[2] * colorb * 4.0f;
514                                         nl->subtract = light->rtlight.lightmap_subtract;
515                                         nl->offset = LIGHTOFFSET;
516                                 }
517                         }
518                 }
519         }
520         ambient4f[0] *= colorr;
521         ambient4f[1] *= colorg;
522         ambient4f[2] *= colorb;
523         ambient4f[3] = colora;
524         diffusecolor[0] *= colorr;
525         diffusecolor[1] *= colorg;
526         diffusecolor[2] *= colorb;
527         return nearlights != 0 || DotProduct(diffusecolor, diffusecolor) > 0;
528 }
529
530 void R_LightModel_CalcVertexColors(const float *ambientcolor4f, const float *diffusecolor, const float *diffusenormal, int numverts, const float *vertex3f, const float *normal3f, float *color4f)
531 {
532         int i, j, usediffuse;
533         float color[4], v[3], dot, dist2, f, dnormal[3];
534         nearlight_t *nl;
535         usediffuse = DotProduct(diffusecolor, diffusecolor) > 0;
536         // negate the diffuse normal to avoid the need to negate the
537         // dotproduct on each vertex
538         VectorNegate(diffusenormal, dnormal);
539         if (usediffuse)
540                 VectorNormalize(dnormal);
541         // directional shading code here
542         for (i = 0;i < numverts;i++, vertex3f += 3, normal3f += 3, color4f += 4)
543         {
544                 VectorCopy4(ambientcolor4f, color);
545
546                 // silly directional diffuse shading
547                 if (usediffuse)
548                 {
549                         dot = DotProduct(normal3f, dnormal);
550                         if (dot > 0)
551                                 VectorMA(color, dot, diffusecolor, color);
552                 }
553
554                 // pretty good lighting
555                 for (j = 0, nl = &nearlight[0];j < nearlights;j++, nl++)
556                 {
557                         VectorSubtract(vertex3f, nl->origin, v);
558                         // first eliminate negative lighting (back side)
559                         dot = DotProduct(normal3f, v);
560                         if (dot > 0)
561                         {
562                                 // we'll need this again later to normalize the dotproduct
563                                 dist2 = DotProduct(v,v);
564                                 // do the distance attenuation math
565                                 f = (1.0f / (dist2 * nl->falloff + nl->offset)) - nl->subtract;
566                                 if (f > 0)
567                                 {
568                                         // we must divide dot by sqrt(dist2) to compensate for
569                                         // the fact we did not normalize v before doing the
570                                         // dotproduct, the result is in the range 0 to 1 (we
571                                         // eliminated negative numbers already)
572                                         f *= dot / sqrt(dist2);
573                                         // blend in the lighting
574                                         VectorMA(color, f, nl->light, color);
575                                 }
576                         }
577                 }
578                 VectorCopy4(color, color4f);
579         }
580 }
581
582 void R_UpdateEntLights(entity_render_t *ent)
583 {
584         int i;
585         const mlight_t *sl;
586         vec3_t v;
587         if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value <= 0)
588                 return;
589         VectorSubtract(ent->origin, ent->entlightsorigin, v);
590         if (ent->entlightsframe != (r_framecount - 1) || (realtime > ent->entlightstime && DotProduct(v,v) >= 1.0f))
591         {
592                 ent->entlightstime = realtime + 0.1;
593                 VectorCopy(ent->origin, ent->entlightsorigin);
594                 ent->numentlights = 0;
595                 if (cl.worldmodel)
596                         for (i = 0, sl = cl.worldmodel->brushq1.lights;i < cl.worldmodel->brushq1.numlights && ent->numentlights < MAXENTLIGHTS;i++, sl++)
597                                 if (CL_TraceLine(ent->origin, sl->origin, NULL, NULL, false, NULL, SUPERCONTENTS_SOLID) == 1)
598                                         ent->entlights[ent->numentlights++] = i;
599         }
600         ent->entlightsframe = r_framecount;
601 }
602