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