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