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