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