]> icculus.org git repositories - divverent/darkplaces.git/blob - r_light.c
moved light matrix generation out of the render code and into the light creation...
[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_AnimateLight
85 ==================
86 */
87 void R_AnimateLight (void)
88 {
89         int i, j, k;
90
91 //
92 // light animations
93 // 'm' is normal light, 'a' is no light, 'z' is double bright
94         i = (int)(cl.time * 10);
95         for (j = 0;j < MAX_LIGHTSTYLES;j++)
96         {
97                 if (!cl_lightstyle || !cl_lightstyle[j].length)
98                 {
99                         d_lightstylevalue[j] = 256;
100                         continue;
101                 }
102                 k = i % cl_lightstyle[j].length;
103                 k = cl_lightstyle[j].map[k] - 'a';
104                 k = k*22;
105                 d_lightstylevalue[j] = k;
106         }
107 }
108
109
110 void R_BuildLightList(void)
111 {
112         int i;
113         dlight_t *cd;
114         rdlight_t *rd;
115
116         r_numdlights = 0;
117         c_dlights = 0;
118
119         if (!r_dynamic.integer || !cl_dlights)
120                 return;
121
122         for (i = 0;i < MAX_DLIGHTS;i++)
123         {
124                 cd = cl_dlights + i;
125                 if (cd->radius <= 0)
126                         continue;
127                 rd = &r_dlight[r_numdlights++];
128                 VectorCopy(cd->origin, rd->origin);
129                 VectorScale(cd->color, d_lightstylevalue[cd->style] * (1.0f / 256.0f), rd->color);
130                 rd->radius = bound(0, cd->radius, 2048.0f);
131                 VectorScale(rd->color, rd->radius * 64.0f, rd->light);
132 #if 0
133                 rd->cullradius2 = DotProduct(rd->light, rd->light) * (0.25f / (64.0f * 64.0f)) + 4096.0f;
134                 // clamp radius to avoid overflowing division table in lightmap code
135                 rd->cullradius2 = bound(0, rd->cullradius2, 2048.0f*2048.0f);
136                 rd->cullradius = sqrt(rd->cullradius2);
137 #else
138                 rd->cullradius = rd->radius;
139                 rd->cullradius2 = rd->cullradius * rd->cullradius;
140 #endif
141                 rd->subtract = 1.0f / rd->cullradius2;
142                 rd->ent = cd->ent;
143                 rd->cubemapnum = cd->cubemapnum;
144                 rd->shadow = cd->shadow;
145                 rd->corona = cd->corona;
146
147                 rd->matrix_lighttoworld = cd->matrix;
148                 Matrix4x4_ConcatScale(&rd->matrix_lighttoworld, rd->cullradius);
149                 Matrix4x4_Invert_Simple(&rd->matrix_worldtolight, &rd->matrix_lighttoworld);
150                 Matrix4x4_Concat(&rd->matrix_worldtoattenuationxyz, &matrix_attenuationxyz, &rd->matrix_worldtolight);
151                 Matrix4x4_Concat(&rd->matrix_worldtoattenuationz, &matrix_attenuationz, &rd->matrix_worldtolight);
152
153                 c_dlights++; // count every dlight in use
154         }
155 }
156
157 void R_DrawCoronas(void)
158 {
159         int i, lnum;
160         float cscale, scale, viewdist, dist;
161         rdlight_t *rd;
162         worldlight_t *wl;
163         if (!r_coronas.integer)
164                 return;
165         R_Mesh_Matrix(&r_identitymatrix);
166         viewdist = DotProduct(r_vieworigin, r_viewforward);
167         if (r_shadow_realtime_world.integer)
168         {
169                 for (lnum = 0, wl = r_shadow_worldlightchain;wl;wl = wl->next, lnum++)
170                 {
171                         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)
172                         {
173                                 cscale = wl->corona * r_coronas.value * 0.25f;
174                                 scale = wl->radius * 0.25f;
175                                 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);
176                         }
177                 }
178         }
179         for (i = 0;i < r_numdlights;i++)
180         {
181                 rd = r_dlight + i;
182                 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)
183                 {
184                         cscale = rd->corona * r_coronas.value * 0.25f;
185                         scale = rd->radius * 0.25f;
186                         if (gl_flashblend.integer)
187                         {
188                                 cscale *= 4.0f;
189                                 scale *= 2.0f;
190                         }
191                         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);
192                 }
193         }
194 }
195
196 /*
197 =============================================================================
198
199 DYNAMIC LIGHTS
200
201 =============================================================================
202 */
203
204 static int lightpvsbytes;
205 static qbyte lightpvs[(MAX_MAP_LEAFS+7)>>3];
206
207 /*
208 =============
209 R_MarkLights
210 =============
211 */
212 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)
213 {
214         int i;
215         mleaf_t *leaf;
216         float dist;
217
218         // for comparisons to minimum acceptable light
219         while(node->contents >= 0)
220         {
221                 dist = PlaneDiff(lightorigin, node->plane);
222                 if (dist > rd->cullradius)
223                         node = node->children[0];
224                 else
225                 {
226                         if (dist >= -rd->cullradius)
227                                 R_RecursiveMarkLights(ent, lightorigin, rd, bit, bitindex, node->children[0], pvs, pvsbits);
228                         node = node->children[1];
229                 }
230         }
231
232         // check if leaf is visible according to pvs
233         leaf = (mleaf_t *)node;
234         i = (leaf - ent->model->brushq1.leafs) - 1;
235         if (leaf->nummarksurfaces && (i >= pvsbits || pvs[i >> 3] & (1 << (i & 7))))
236         {
237                 int *surfacepvsframes, d, impacts, impactt;
238                 float sdist, maxdist, dist2, impact[3];
239                 msurface_t *surf;
240                 // mark the polygons
241                 maxdist = rd->cullradius2;
242                 surfacepvsframes = ent->model->brushq1.surfacepvsframes;
243                 for (i = 0;i < leaf->nummarksurfaces;i++)
244                 {
245                         if (surfacepvsframes[leaf->firstmarksurface[i]] != ent->model->brushq1.pvsframecount)
246                                 continue;
247                         surf = ent->model->brushq1.surfaces + leaf->firstmarksurface[i];
248                         dist = sdist = PlaneDiff(lightorigin, surf->plane);
249                         if (surf->flags & SURF_PLANEBACK)
250                                 dist = -dist;
251
252                         if (dist < -0.25f && !(surf->flags & SURF_LIGHTBOTHSIDES))
253                                 continue;
254
255                         dist2 = dist * dist;
256                         if (dist2 >= maxdist)
257                                 continue;
258
259                         VectorCopy(lightorigin, impact);
260                         if (surf->plane->type >= 3)
261                                 VectorMA(impact, -sdist, surf->plane->normal, impact);
262                         else
263                                 impact[surf->plane->type] -= sdist;
264
265                         impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
266
267                         d = bound(0, impacts, surf->extents[0] + 16) - impacts;
268                         dist2 += d * d;
269                         if (dist2 > maxdist)
270                                 continue;
271
272                         impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
273
274                         d = bound(0, impactt, surf->extents[1] + 16) - impactt;
275                         dist2 += d * d;
276                         if (dist2 > maxdist)
277                                 continue;
278
279                         if (surf->dlightframe != r_framecount) // not dynamic until now
280                         {
281                                 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;
282                                 surf->dlightframe = r_framecount;
283                                 surf->cached_dlight = true;
284                         }
285                         surf->dlightbits[bitindex] |= bit;
286                 }
287         }
288 }
289
290 void R_MarkLights(entity_render_t *ent)
291 {
292         int i, bit, bitindex;
293         rdlight_t *rd;
294         vec3_t lightorigin;
295         if (!gl_flashblend.integer && r_dynamic.integer && ent->model && ent->model->brushq1.numleafs)
296         {
297                 for (i = 0, rd = r_dlight;i < r_numdlights;i++, rd++)
298                 {
299                         bit = 1 << (i & 31);
300                         bitindex = i >> 5;
301                         Matrix4x4_Transform(&ent->inversematrix, rd->origin, lightorigin);
302                         lightpvsbytes = 0;
303                         if (r_vismarklights.integer && ent->model->brush.FatPVS)
304                                 lightpvsbytes = ent->model->brush.FatPVS(ent->model, lightorigin, 0, lightpvs, sizeof(lightpvs));
305                         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));
306                 }
307         }
308 }
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 && cl.worldmodel && cl.worldmodel->brush.LightPoint)
324         {
325                 ambientcolor[0] = ambientcolor[1] = ambientcolor[2] = r_ambient.value * (2.0f / 128.0f);
326                 cl.worldmodel->brush.LightPoint(cl.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 (cl.worldmodel->brushq1.numlights)
333         {
334                 int i;
335                 vec3_t v;
336                 float f;
337                 mlight_t *sl;
338                 for (i = 0;i < cl.worldmodel->brushq1.numlights;i++)
339                 {
340                         sl = cl.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                 rdlight_t *rd;
359                 // FIXME: this really should handle dlights as diffusecolor/diffusenormal somehow
360                 for (i = 0;i < r_numdlights;i++)
361                 {
362                         rd = r_dlight + i;
363                         VectorSubtract(p, rd->origin, v);
364                         f = DotProduct(v, v);
365                         if (f < rd->cullradius2 && CL_TraceLine(p, rd->origin, NULL, NULL, false, NULL, SUPERCONTENTS_SOLID) == 1)
366                         {
367                                 f = (1.0f / (f + LIGHTOFFSET)) - rd->subtract;
368                                 VectorMA(ambientcolor, f, rd->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         rdlight_t *rd;
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 (r_fullbright.integer || (ent->effects & EF_FULLBRIGHT))
406         {
407                 // highly rare
408                 VectorSet(ambient4f, 1, 1, 1);
409                 maxnearlights = 0;
410         }
411         else if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value <= 0)
412                 maxnearlights = 0;
413         else
414         {
415                 if (cl.worldmodel && cl.worldmodel->brush.LightPoint)
416                 {
417                         cl.worldmodel->brush.LightPoint(cl.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 = cl.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_shadow_realtime_dlight.integer)
481         {
482                 for (i = 0;i < r_numdlights;i++)
483                 {
484                         rd = r_dlight + i;
485                         VectorCopy(rd->origin, v);
486                         if (v[0] < ent->mins[0]) v[0] = ent->mins[0];if (v[0] > ent->maxs[0]) v[0] = ent->maxs[0];
487                         if (v[1] < ent->mins[1]) v[1] = ent->mins[1];if (v[1] > ent->maxs[1]) v[1] = ent->maxs[1];
488                         if (v[2] < ent->mins[2]) v[2] = ent->mins[2];if (v[2] > ent->maxs[2]) v[2] = ent->maxs[2];
489                         VectorSubtract (v, rd->origin, v);
490                         if (DotProduct(v, v) < rd->cullradius2)
491                         {
492                                 if (CL_TraceLine(ent->origin, rd->origin, NULL, NULL, false, NULL, SUPERCONTENTS_SOLID) != 1)
493                                         continue;
494                                 VectorSubtract (ent->origin, rd->origin, v);
495                                 f = ((1.0f / (DotProduct(v, v) + LIGHTOFFSET)) - rd->subtract);
496                                 VectorScale(rd->light, f, ambientcolor);
497                                 intensity = DotProduct(ambientcolor, ambientcolor);
498                                 if (f < 0)
499                                         intensity *= -1.0f;
500                                 if (nearlights < maxnearlights)
501                                         j = nearlights++;
502                                 else
503                                 {
504                                         for (j = 0;j < maxnearlights;j++)
505                                         {
506                                                 if (nearlight[j].intensity < intensity)
507                                                 {
508                                                         if (nearlight[j].intensity > 0)
509                                                                 VectorAdd(ambient4f, nearlight[j].ambientlight, ambient4f);
510                                                         break;
511                                                 }
512                                         }
513                                 }
514                                 if (j >= maxnearlights)
515                                 {
516                                         // this light is less significant than all others,
517                                         // add it to ambient
518                                         if (intensity > 0)
519                                                 VectorAdd(ambient4f, ambientcolor, ambient4f);
520                                 }
521                                 else
522                                 {
523                                         nl = nearlight + j;
524                                         nl->intensity = intensity;
525                                         // transform the light into the model's coordinate system
526                                         if (worldcoords)
527                                                 VectorCopy(rd->origin, nl->origin);
528                                         else
529                                         {
530                                                 Matrix4x4_Transform(&ent->inversematrix, rd->origin, nl->origin);
531                                                 /*
532                                                 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"
533                                                 , rd - r_dlight, ent->model->name
534                                                 , rd->origin[0], rd->origin[1], rd->origin[2]
535                                                 , nl->origin[0], nl->origin[1], nl->origin[2]
536                                                 , ent->inversematrix.m[0][0], ent->inversematrix.m[0][1], ent->inversematrix.m[0][2], ent->inversematrix.m[0][3]
537                                                 , ent->inversematrix.m[1][0], ent->inversematrix.m[1][1], ent->inversematrix.m[1][2], ent->inversematrix.m[1][3]
538                                                 , ent->inversematrix.m[2][0], ent->inversematrix.m[2][1], ent->inversematrix.m[2][2], ent->inversematrix.m[2][3]
539                                                 , ent->inversematrix.m[3][0], ent->inversematrix.m[3][1], ent->inversematrix.m[3][2], ent->inversematrix.m[3][3]);
540                                                 */
541                                         }
542                                         // integrate mscale into falloff, for maximum speed
543                                         nl->falloff = mscale;
544                                         VectorCopy(ambientcolor, nl->ambientlight);
545                                         nl->light[0] = rd->light[0] * colorr * 4.0f;
546                                         nl->light[1] = rd->light[1] * colorg * 4.0f;
547                                         nl->light[2] = rd->light[2] * colorb * 4.0f;
548                                         nl->subtract = rd->subtract;
549                                         nl->offset = LIGHTOFFSET;
550                                 }
551                         }
552                 }
553         }
554         ambient4f[0] *= colorr;
555         ambient4f[1] *= colorg;
556         ambient4f[2] *= colorb;
557         ambient4f[3] = colora;
558         diffusecolor[0] *= colorr;
559         diffusecolor[1] *= colorg;
560         diffusecolor[2] *= colorb;
561         return nearlights != 0 || DotProduct(diffusecolor, diffusecolor) > 0;
562 }
563
564 void R_LightModel_CalcVertexColors(const float *ambientcolor4f, const float *diffusecolor, const float *diffusenormal, int numverts, const float *vertex3f, const float *normal3f, float *color4f)
565 {
566         int i, j, usediffuse;
567         float color[4], v[3], dot, dist2, f, dnormal[3];
568         nearlight_t *nl;
569         usediffuse = DotProduct(diffusecolor, diffusecolor) > 0;
570         // negate the diffuse normal to avoid the need to negate the
571         // dotproduct on each vertex
572         VectorNegate(diffusenormal, dnormal);
573         if (usediffuse)
574                 VectorNormalize(dnormal);
575         // directional shading code here
576         for (i = 0;i < numverts;i++, vertex3f += 3, normal3f += 3, color4f += 4)
577         {
578                 VectorCopy4(ambientcolor4f, color);
579
580                 // silly directional diffuse shading
581                 if (usediffuse)
582                 {
583                         dot = DotProduct(normal3f, dnormal);
584                         if (dot > 0)
585                                 VectorMA(color, dot, diffusecolor, color);
586                 }
587
588                 // pretty good lighting
589                 for (j = 0, nl = &nearlight[0];j < nearlights;j++, nl++)
590                 {
591                         VectorSubtract(vertex3f, nl->origin, v);
592                         // first eliminate negative lighting (back side)
593                         dot = DotProduct(normal3f, v);
594                         if (dot > 0)
595                         {
596                                 // we'll need this again later to normalize the dotproduct
597                                 dist2 = DotProduct(v,v);
598                                 // do the distance attenuation math
599                                 f = (1.0f / (dist2 * nl->falloff + nl->offset)) - nl->subtract;
600                                 if (f > 0)
601                                 {
602                                         // we must divide dot by sqrt(dist2) to compensate for
603                                         // the fact we did not normalize v before doing the
604                                         // dotproduct, the result is in the range 0 to 1 (we
605                                         // eliminated negative numbers already)
606                                         f *= dot / sqrt(dist2);
607                                         // blend in the lighting
608                                         VectorMA(color, f, nl->light, color);
609                                 }
610                         }
611                 }
612                 VectorCopy4(color, color4f);
613         }
614 }
615
616 void R_UpdateEntLights(entity_render_t *ent)
617 {
618         int i;
619         const mlight_t *sl;
620         vec3_t v;
621         if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value <= 0)
622                 return;
623         VectorSubtract(ent->origin, ent->entlightsorigin, v);
624         if (ent->entlightsframe != (r_framecount - 1) || (realtime > ent->entlightstime && DotProduct(v,v) >= 1.0f))
625         {
626                 ent->entlightstime = realtime + 0.1;
627                 VectorCopy(ent->origin, ent->entlightsorigin);
628                 ent->numentlights = 0;
629                 if (cl.worldmodel)
630                         for (i = 0, sl = cl.worldmodel->brushq1.lights;i < cl.worldmodel->brushq1.numlights && ent->numentlights < MAXENTLIGHTS;i++, sl++)
631                                 if (CL_TraceLine(ent->origin, sl->origin, NULL, NULL, false, NULL, SUPERCONTENTS_SOLID) == 1)
632                                         ent->entlights[ent->numentlights++] = i;
633         }
634         ent->entlightsframe = r_framecount;
635 }
636