2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
23 #include "cl_collision.h"
26 rdlight_t r_dlight[MAX_DLIGHTS];
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"};
34 static rtexture_t *lightcorona;
35 static rtexturepool_t *lighttexturepool;
37 void r_light_start(void)
41 qbyte pixels[32][32][4];
42 lighttexturepool = R_AllocTexturePool();
43 for (y = 0;y < 32;y++)
45 dy = (y - 15.5f) * (1.0f / 16.0f);
46 for (x = 0;x < 32;x++)
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));
54 pixels[y][x][3] = 255;
57 lightcorona = R_LoadTexture2D(lighttexturepool, "lightcorona", 32, 32, &pixels[0][0][0], TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
60 void r_light_shutdown(void)
62 lighttexturepool = NULL;
66 void r_light_newmap(void)
69 for (i = 0;i < 256;i++)
70 d_lightstylevalue[i] = 264; // normal light value
73 void R_Light_Init(void)
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);
87 void R_AnimateLight (void)
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++)
97 if (!cl_lightstyle || !cl_lightstyle[j].length)
99 d_lightstylevalue[j] = 256;
102 k = i % cl_lightstyle[j].length;
103 k = cl_lightstyle[j].map[k] - 'a';
105 d_lightstylevalue[j] = k;
110 void R_BuildLightList(void)
119 if (!r_dynamic.integer || !cl_dlights)
122 for (i = 0;i < MAX_DLIGHTS;i++)
127 rd = &r_dlight[r_numdlights++];
128 VectorCopy(cd->origin, rd->origin);
129 VectorScale(cd->color, cd->radius * 64.0f, rd->light);
130 rd->cullradius2 = DotProduct(rd->light, rd->light) * (0.25f / (64.0f * 64.0f)) + 4096.0f;
131 // clamp radius to avoid overflowing division table in lightmap code
132 if (rd->cullradius2 > (2048.0f * 2048.0f))
133 rd->cullradius2 = (2048.0f * 2048.0f);
134 rd->cullradius = sqrt(rd->cullradius2);
135 rd->subtract = 1.0f / rd->cullradius2;
137 c_dlights++; // count every dlight in use
141 void R_DrawCoronas(void)
145 float scale, viewdist, diff[3], dist;
147 if (!r_coronas.integer)
149 memset(&m, 0, sizeof(m));
150 m.blendfunc1 = GL_ONE;
151 m.blendfunc2 = GL_ONE;
152 m.depthdisable = true; // magic
153 m.tex[0] = R_GetTexture(lightcorona);
154 R_Mesh_Matrix(&r_identitymatrix);
156 viewdist = DotProduct(r_origin, vpn);
157 varray_texcoord[0][ 0] = 0;varray_texcoord[0][ 1] = 0;
158 varray_texcoord[0][ 4] = 0;varray_texcoord[0][ 5] = 1;
159 varray_texcoord[0][ 8] = 1;varray_texcoord[0][ 9] = 1;
160 varray_texcoord[0][12] = 1;varray_texcoord[0][13] = 0;
161 for (i = 0;i < r_numdlights;i++)
164 dist = (DotProduct(rd->origin, vpn) - viewdist);
165 if (dist >= 24.0f && CL_TraceLine(rd->origin, r_origin, NULL, NULL, 0, true, NULL) == 1)
167 scale = r_colorscale * (1.0f / 131072.0f);
168 if (gl_flashblend.integer)
172 VectorSubtract(rd->origin, r_origin, diff);
173 scale *= 1 - exp(fogdensity/DotProduct(diff,diff));
175 GL_Color(rd->light[0] * scale, rd->light[1] * scale, rd->light[2] * scale, 1);
176 scale = rd->cullradius * 0.25f;
177 if (gl_flashblend.integer)
179 varray_vertex[0] = rd->origin[0] - vright[0] * scale - vup[0] * scale;
180 varray_vertex[1] = rd->origin[1] - vright[1] * scale - vup[1] * scale;
181 varray_vertex[2] = rd->origin[2] - vright[2] * scale - vup[2] * scale;
182 varray_vertex[4] = rd->origin[0] - vright[0] * scale + vup[0] * scale;
183 varray_vertex[5] = rd->origin[1] - vright[1] * scale + vup[1] * scale;
184 varray_vertex[6] = rd->origin[2] - vright[2] * scale + vup[2] * scale;
185 varray_vertex[8] = rd->origin[0] + vright[0] * scale + vup[0] * scale;
186 varray_vertex[9] = rd->origin[1] + vright[1] * scale + vup[1] * scale;
187 varray_vertex[10] = rd->origin[2] + vright[2] * scale + vup[2] * scale;
188 varray_vertex[12] = rd->origin[0] + vright[0] * scale - vup[0] * scale;
189 varray_vertex[13] = rd->origin[1] + vright[1] * scale - vup[1] * scale;
190 varray_vertex[14] = rd->origin[2] + vright[2] * scale - vup[2] * scale;
191 R_Mesh_Draw(4, 2, polygonelements);
197 =============================================================================
201 =============================================================================
209 static void R_OldMarkLights (entity_render_t *ent, vec3_t lightorigin, rdlight_t *rd, int bit, int bitindex, mnode_t *node)
211 float ndist, maxdist;
213 int i, *surfacepvsframes;
214 int d, impacts, impactt;
215 float dist, dist2, impact[3];
217 if (!r_dynamic.integer)
220 // for comparisons to minimum acceptable light
221 maxdist = rd->cullradius2;
223 surfacepvsframes = ent->model->surfacepvsframes;
225 if (node->contents < 0)
228 ndist = PlaneDiff(lightorigin, node->plane);
230 if (ndist > rd->cullradius)
232 node = node->children[0];
235 if (ndist < -rd->cullradius)
237 node = node->children[1];
242 surf = ent->model->surfaces + node->firstsurface;
243 for (i = 0;i < node->numsurfaces;i++, surf++)
245 if (surfacepvsframes[surf->number] != ent->model->pvsframecount)
248 if (surf->flags & SURF_PLANEBACK)
251 if (dist < -0.25f && !(surf->flags & SURF_LIGHTBOTHSIDES))
255 if (dist2 >= maxdist)
258 if (node->plane->type < 3)
260 VectorCopy(lightorigin, impact);
261 impact[node->plane->type] -= dist;
265 impact[0] = lightorigin[0] - surf->plane->normal[0] * dist;
266 impact[1] = lightorigin[1] - surf->plane->normal[1] * dist;
267 impact[2] = lightorigin[2] - surf->plane->normal[2] * dist;
270 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
272 d = bound(0, impacts, surf->extents[0] + 16) - impacts;
277 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
279 d = bound(0, impactt, surf->extents[1] + 16) - impactt;
284 if (surf->dlightframe != r_framecount) // not dynamic until now
286 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;
287 surf->dlightframe = r_framecount;
288 if (r_dlightmap.integer)
289 surf->cached_dlight = true;
291 surf->dlightbits[bitindex] |= bit;
294 if (node->children[0]->contents >= 0)
296 if (node->children[1]->contents >= 0)
298 R_OldMarkLights (ent, lightorigin, rd, bit, bitindex, node->children[0]);
299 node = node->children[1];
304 node = node->children[0];
308 else if (node->children[1]->contents >= 0)
310 node = node->children[1];
316 static void R_VisMarkLights (entity_render_t *ent, rdlight_t *rd, int bit, int bitindex)
318 static int lightframe = 0;
322 int i, k, m, c, leafnum, *surfacepvsframes, *mark;
327 float low[3], high[3], dist, maxdist;
329 if (!r_dynamic.integer)
332 Matrix4x4_Transform(&ent->inversematrix, rd->origin, lightorigin);
335 pvsleaf = Mod_PointInLeaf (lightorigin, model);
339 in = pvsleaf->compressed_vis;
340 if (!r_vismarklights.integer || !in)
342 // told not to use pvs, or there's no pvs to use
343 R_OldMarkLights(ent, lightorigin, rd, bit, bitindex, model->nodes + model->hulls[0].firstclipnode);
349 low[0] = lightorigin[0] - rd->cullradius;low[1] = lightorigin[1] - rd->cullradius;low[2] = lightorigin[2] - rd->cullradius;
350 high[0] = lightorigin[0] + rd->cullradius;high[1] = lightorigin[1] + rd->cullradius;high[2] = lightorigin[2] + rd->cullradius;
352 // for comparisons to minimum acceptable light
353 maxdist = rd->cullradius2;
355 row = (model->numleafs+7)>>3;
356 surfacepvsframes = model->surfacepvsframes;
364 for (i = 0;i < 8;i++)
368 // warning to the clumsy: numleafs is one less than it should be, it only counts leafs with vis bits (skips leaf 0)
369 leafnum = (k << 3)+i+1;
370 if (leafnum > model->numleafs)
372 leaf = &model->leafs[leafnum];
373 if (leaf->mins[0] > high[0] || leaf->maxs[0] < low[0]
374 || leaf->mins[1] > high[1] || leaf->maxs[1] < low[1]
375 || leaf->mins[2] > high[2] || leaf->maxs[2] < low[2])
377 if ((m = leaf->nummarksurfaces))
379 mark = leaf->firstmarksurface;
382 surf = model->surfaces + *mark++;
383 // if not visible in current frame, or already marked because it was in another leaf we passed, skip
384 if (surf->lightframe == lightframe)
386 surf->lightframe = lightframe;
387 if (surfacepvsframes[surf->number] != model->pvsframecount)
389 dist = PlaneDiff(lightorigin, surf->plane);
390 if (surf->flags & SURF_PLANEBACK)
392 // LordHavoc: make sure it is infront of the surface and not too far away
393 if (dist < rd->cullradius && (dist > -0.25f || ((surf->flags & SURF_LIGHTBOTHSIDES) && dist > -rd->cullradius)))
396 int impacts, impactt;
397 float dist2, impact[3];
401 if (surf->plane->type < 3)
403 VectorCopy(lightorigin, impact);
404 impact[surf->plane->type] -= dist;
408 impact[0] = lightorigin[0] - surf->plane->normal[0] * dist;
409 impact[1] = lightorigin[1] - surf->plane->normal[1] * dist;
410 impact[2] = lightorigin[2] - surf->plane->normal[2] * dist;
413 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
414 d = bound(0, impacts, surf->extents[0] + 16) - impacts;
419 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
420 d = bound(0, impactt, surf->extents[1] + 16) - impactt;
425 if (surf->dlightframe != r_framecount) // not dynamic until now
427 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;
428 surf->dlightframe = r_framecount;
429 if (r_dlightmap.integer)
430 surf->cached_dlight = true;
432 surf->dlightbits[bitindex] |= bit;
447 void R_MarkLights(entity_render_t *ent)
450 if (!gl_flashblend.integer)
451 for (i = 0;i < r_numdlights;i++)
452 R_VisMarkLights (ent, r_dlight + i, 1 << (i & 31), i >> 5);
456 =============================================================================
460 =============================================================================
463 static int RecursiveLightPoint (vec3_t color, const mnode_t *node, float x, float y, float startz, float endz)
465 int side, distz = endz - startz;
470 if (node->contents < 0)
471 return false; // didn't hit anything
473 switch (node->plane->type)
476 node = node->children[x < node->plane->dist];
479 node = node->children[y < node->plane->dist];
482 side = startz < node->plane->dist;
483 if ((endz < node->plane->dist) == side)
485 node = node->children[side];
488 // found an intersection
489 mid = node->plane->dist;
492 back = front = x * node->plane->normal[0] + y * node->plane->normal[1];
493 front += startz * node->plane->normal[2];
494 back += endz * node->plane->normal[2];
495 side = front < node->plane->dist;
496 if ((back < node->plane->dist) == side)
498 node = node->children[side];
501 // found an intersection
502 mid = startz + distz * (front - node->plane->dist) / (front - back);
506 // go down front side
507 if (node->children[side]->contents >= 0 && RecursiveLightPoint (color, node->children[side], x, y, startz, mid))
508 return true; // hit something
511 // check for impact on this node
512 if (node->numsurfaces)
517 surf = cl.worldmodel->surfaces + node->firstsurface;
518 for (i = 0;i < node->numsurfaces;i++, surf++)
520 if (!(surf->flags & SURF_LIGHTMAP))
521 continue; // no lightmaps
523 ds = (int) (x * surf->texinfo->vecs[0][0] + y * surf->texinfo->vecs[0][1] + mid * surf->texinfo->vecs[0][2] + surf->texinfo->vecs[0][3]);
524 dt = (int) (x * surf->texinfo->vecs[1][0] + y * surf->texinfo->vecs[1][1] + mid * surf->texinfo->vecs[1][2] + surf->texinfo->vecs[1][3]);
526 if (ds < surf->texturemins[0] || dt < surf->texturemins[1])
529 ds -= surf->texturemins[0];
530 dt -= surf->texturemins[1];
532 if (ds > surf->extents[0] || dt > surf->extents[1])
538 int maps, line3, size3, dsfrac = ds & 15, dtfrac = dt & 15, scale = 0, r00 = 0, g00 = 0, b00 = 0, r01 = 0, g01 = 0, b01 = 0, r10 = 0, g10 = 0, b10 = 0, r11 = 0, g11 = 0, b11 = 0;
539 line3 = ((surf->extents[0]>>4)+1)*3;
540 size3 = ((surf->extents[0]>>4)+1) * ((surf->extents[1]>>4)+1)*3; // LordHavoc: *3 for colored lighting
542 lightmap = surf->samples + ((dt>>4) * ((surf->extents[0]>>4)+1) + (ds>>4))*3; // LordHavoc: *3 for color
544 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
546 scale = d_lightstylevalue[surf->styles[maps]];
547 r00 += lightmap[ 0] * scale;g00 += lightmap[ 1] * scale;b00 += lightmap[ 2] * scale;
548 r01 += lightmap[ 3] * scale;g01 += lightmap[ 4] * scale;b01 += lightmap[ 5] * scale;
549 r10 += lightmap[line3+0] * scale;g10 += lightmap[line3+1] * scale;b10 += lightmap[line3+2] * scale;
550 r11 += lightmap[line3+3] * scale;g11 += lightmap[line3+4] * scale;b11 += lightmap[line3+5] * scale;
555 LordHavoc: here's the readable version of the interpolation
556 code, not quite as easy for the compiler to optimize...
558 dsfrac is the X position in the lightmap pixel, * 16
559 dtfrac is the Y position in the lightmap pixel, * 16
560 r00 is top left corner, r01 is top right corner
561 r10 is bottom left corner, r11 is bottom right corner
562 g and b are the same layout.
563 r0 and r1 are the top and bottom intermediate results
565 first we interpolate the top two points, to get the top
568 r0 = (((r01-r00) * dsfrac) >> 4) + r00;
569 g0 = (((g01-g00) * dsfrac) >> 4) + g00;
570 b0 = (((b01-b00) * dsfrac) >> 4) + b00;
572 then we interpolate the bottom two points, to get the
575 r1 = (((r11-r10) * dsfrac) >> 4) + r10;
576 g1 = (((g11-g10) * dsfrac) >> 4) + g10;
577 b1 = (((b11-b10) * dsfrac) >> 4) + b10;
579 then we interpolate the top and bottom samples to get the
580 middle sample (the one which was requested)
582 r = (((r1-r0) * dtfrac) >> 4) + r0;
583 g = (((g1-g0) * dtfrac) >> 4) + g0;
584 b = (((b1-b0) * dtfrac) >> 4) + b0;
587 color[0] += (float) ((((((((r11-r10) * dsfrac) >> 4) + r10)-((((r01-r00) * dsfrac) >> 4) + r00)) * dtfrac) >> 4) + ((((r01-r00) * dsfrac) >> 4) + r00)) * (1.0f / 32768.0f);
588 color[1] += (float) ((((((((g11-g10) * dsfrac) >> 4) + g10)-((((g01-g00) * dsfrac) >> 4) + g00)) * dtfrac) >> 4) + ((((g01-g00) * dsfrac) >> 4) + g00)) * (1.0f / 32768.0f);
589 color[2] += (float) ((((((((b11-b10) * dsfrac) >> 4) + b10)-((((b01-b00) * dsfrac) >> 4) + b00)) * dtfrac) >> 4) + ((((b01-b00) * dsfrac) >> 4) + b00)) * (1.0f / 32768.0f);
591 return true; // success
596 node = node->children[side ^ 1];
598 distz = endz - startz;
603 void R_CompleteLightPoint (vec3_t color, const vec3_t p, int dynamic, const mleaf_t *leaf)
611 leaf = Mod_PointInLeaf(p, cl.worldmodel);
612 if (!leaf || leaf->contents == CONTENTS_SOLID || r_fullbright.integer || !cl.worldmodel->lightdata)
614 color[0] = color[1] = color[2] = 1;
618 color[0] = color[1] = color[2] = r_ambient.value * (2.0f / 128.0f);
619 if (cl.worldmodel->numlights)
621 for (i = 0;i < cl.worldmodel->numlights;i++)
623 sl = cl.worldmodel->lights + i;
624 if (d_lightstylevalue[sl->style] > 0)
626 VectorSubtract (p, sl->origin, v);
627 f = ((1.0f / (DotProduct(v, v) * sl->falloff + sl->distbias)) - sl->subtract);
628 if (f > 0 && CL_TraceLine(p, sl->origin, NULL, NULL, 0, false, NULL) == 1)
630 f *= d_lightstylevalue[sl->style] * (1.0f / 65536.0f);
631 VectorMA(color, f, sl->light, color);
637 RecursiveLightPoint (color, cl.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
641 for (i = 0;i < r_numdlights;i++)
644 VectorSubtract (p, rd->origin, v);
645 f = DotProduct(v, v);
646 if (f < rd->cullradius2 && CL_TraceLine(p, rd->origin, NULL, NULL, 0, false, NULL) == 1)
648 f = (1.0f / (f + LIGHTOFFSET)) - rd->subtract;
649 VectorMA(color, f, rd->light, color);
655 void R_ModelLightPoint (const entity_render_t *ent, vec3_t color, const vec3_t p)
658 leaf = Mod_PointInLeaf(p, cl.worldmodel);
659 if (!leaf || leaf->contents == CONTENTS_SOLID || r_fullbright.integer || !cl.worldmodel->lightdata || ent->effects & EF_FULLBRIGHT)
661 color[0] = color[1] = color[2] = 1;
665 color[0] = color[1] = color[2] = r_ambient.value * (2.0f / 128.0f);
666 if (!cl.worldmodel->numlights && r_shadow_lightingmode < 2)
667 RecursiveLightPoint (color, cl.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
670 void R_LightModel(const entity_render_t *ent, int numverts, float *vertices, float *normals, float *colors, float colorr, float colorg, float colorb, int worldcoords)
672 int i, j, nearlights = 0, maxnearlights = r_modellights.integer;
673 float color[3], basecolor[3], v[3], t, *av, *avn, *avc, a, f, dist2, mscale, dot, stylescale, intensity, ambientcolor[3];
679 // how much this light would contribute to ambient if replaced
684 // used for choosing only the brightest lights
687 nearlight[MAX_DLIGHTS], *nl;
691 // scale of the model's coordinate space, to alter light attenuation to match
692 // make the mscale squared so it can scale the squared distance results
693 mscale = ent->scale * ent->scale;
694 if ((maxnearlights != 0) && !r_fullbright.integer && !(ent->effects & EF_FULLBRIGHT))
696 R_ModelLightPoint(ent, basecolor, ent->origin);
698 if (r_shadow_lightingmode < 2)
701 for (i = 0;i < ent->numentlights;i++)
703 sl = cl.worldmodel->lights + ent->entlights[i];
704 stylescale = d_lightstylevalue[sl->style] * (1.0f / 65536.0f);
705 VectorSubtract (ent->origin, sl->origin, v);
706 f = ((1.0f / (DotProduct(v, v) * sl->falloff + sl->distbias)) - sl->subtract) * stylescale;
707 VectorScale(sl->light, f, ambientcolor);
708 intensity = DotProduct(ambientcolor, ambientcolor);
711 if (nearlights < maxnearlights)
715 for (j = 0;j < maxnearlights;j++)
717 if (nearlight[j].intensity < intensity)
719 if (nearlight[j].intensity > 0)
720 VectorAdd(basecolor, nearlight[j].ambientlight, basecolor);
725 if (j >= maxnearlights)
727 // this light is less significant than all others,
730 VectorAdd(basecolor, ambientcolor, basecolor);
735 nl->intensity = intensity;
736 // transform the light into the model's coordinate system
738 VectorCopy(sl->origin, nl->origin);
740 Matrix4x4_Transform(&ent->inversematrix, sl->origin, nl->origin);
741 // integrate mscale into falloff, for maximum speed
742 nl->falloff = sl->falloff * mscale;
743 VectorCopy(ambientcolor, nl->ambientlight);
744 nl->light[0] = sl->light[0] * stylescale * colorr * 4.0f;
745 nl->light[1] = sl->light[1] * stylescale * colorg * 4.0f;
746 nl->light[2] = sl->light[2] * stylescale * colorb * 4.0f;
747 nl->subtract = sl->subtract;
748 nl->offset = sl->distbias;
751 if (r_shadow_lightingmode < 1)
753 for (i = 0;i < r_numdlights;i++)
756 VectorCopy(rd->origin, v);
757 if (v[0] < ent->mins[0]) v[0] = ent->mins[0];if (v[0] > ent->maxs[0]) v[0] = ent->maxs[0];
758 if (v[1] < ent->mins[1]) v[1] = ent->mins[1];if (v[1] > ent->maxs[1]) v[1] = ent->maxs[1];
759 if (v[2] < ent->mins[2]) v[2] = ent->mins[2];if (v[2] > ent->maxs[2]) v[2] = ent->maxs[2];
760 VectorSubtract (v, rd->origin, v);
761 if (DotProduct(v, v) < rd->cullradius2)
763 if (CL_TraceLine(ent->origin, rd->origin, NULL, NULL, 0, false, NULL) != 1)
765 VectorSubtract (ent->origin, rd->origin, v);
766 f = ((1.0f / (DotProduct(v, v) + LIGHTOFFSET)) - rd->subtract);
767 VectorScale(rd->light, f, ambientcolor);
768 intensity = DotProduct(ambientcolor, ambientcolor);
771 if (nearlights < maxnearlights)
775 for (j = 0;j < maxnearlights;j++)
777 if (nearlight[j].intensity < intensity)
779 if (nearlight[j].intensity > 0)
780 VectorAdd(basecolor, nearlight[j].ambientlight, basecolor);
785 if (j >= maxnearlights)
787 // this light is less significant than all others,
790 VectorAdd(basecolor, ambientcolor, basecolor);
795 nl->intensity = intensity;
796 // transform the light into the model's coordinate system
798 VectorCopy(rd->origin, nl->origin);
801 Matrix4x4_Transform(&ent->inversematrix, rd->origin, nl->origin);
803 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"
804 , rd - r_dlight, ent->model->name
805 , rd->origin[0], rd->origin[1], rd->origin[2]
806 , nl->origin[0], nl->origin[1], nl->origin[2]
807 , ent->inversematrix.m[0][0], ent->inversematrix.m[0][1], ent->inversematrix.m[0][2], ent->inversematrix.m[0][3]
808 , ent->inversematrix.m[1][0], ent->inversematrix.m[1][1], ent->inversematrix.m[1][2], ent->inversematrix.m[1][3]
809 , ent->inversematrix.m[2][0], ent->inversematrix.m[2][1], ent->inversematrix.m[2][2], ent->inversematrix.m[2][3]
810 , ent->inversematrix.m[3][0], ent->inversematrix.m[3][1], ent->inversematrix.m[3][2], ent->inversematrix.m[3][3]);
813 // integrate mscale into falloff, for maximum speed
814 nl->falloff = mscale;
815 VectorCopy(ambientcolor, nl->ambientlight);
816 nl->light[0] = rd->light[0] * colorr * 4.0f;
817 nl->light[1] = rd->light[1] * colorg * 4.0f;
818 nl->light[2] = rd->light[2] * colorb * 4.0f;
819 nl->subtract = rd->subtract;
820 nl->offset = LIGHTOFFSET;
828 R_CompleteLightPoint (basecolor, ent->origin, true, NULL);
829 basecolor[0] *= colorr;
830 basecolor[1] *= colorg;
831 basecolor[2] *= colorb;
838 for (i = 0;i < numverts;i++)
840 VectorCopy(basecolor, color);
841 for (j = 0, nl = &nearlight[0];j < nearlights;j++, nl++)
843 VectorSubtract(nl->origin, av, v);
844 // directional shading
845 dot = DotProduct(avn,v);
848 // the vertex normal faces the light
850 // do the distance attenuation
851 dist2 = DotProduct(v,v);
852 f = (1.0f / (dist2 * nl->falloff + nl->offset)) - nl->subtract;
856 t = 1.0f / sqrt(dist2);
858 //*((int *)&t) = 0x5f3759df - ((* (int *) &dist2) >> 1);
859 //t = t * (1.5f - (dist2 * 0.5f * t * t));
862 // dot * t is dotproduct with a normalized v.
863 // (the result would be -1 to +1, but we already
864 // eliminated the <= 0 case, so it is 0 to 1)
866 // the hardness variables are for backlighting/shinyness
867 // these have been hardwired at * 0.5 + 0.5 to match
868 // the quake map lighting utility's equations
869 f *= dot * t;// * 0.5f + 0.5f;// * hardness + hardnessoffset;
870 VectorMA(color, f, nl->light, color);
875 VectorCopy(color, avc);
883 GL_Color(basecolor[0], basecolor[1], basecolor[2], a);
886 void R_UpdateEntLights(entity_render_t *ent)
891 if (r_shadow_lightingmode >= 2)
893 VectorSubtract(ent->origin, ent->entlightsorigin, v);
894 if (ent->entlightsframe != (r_framecount - 1) || (realtime > ent->entlightstime && DotProduct(v,v) >= 1.0f))
896 ent->entlightstime = realtime + 0.1;
897 VectorCopy(ent->origin, ent->entlightsorigin);
898 ent->numentlights = 0;
900 for (i = 0, sl = cl.worldmodel->lights;i < cl.worldmodel->numlights && ent->numentlights < MAXENTLIGHTS;i++, sl++)
901 if (CL_TraceLine(ent->origin, sl->origin, NULL, NULL, 0, false, NULL) == 1)
902 ent->entlights[ent->numentlights++] = i;
904 ent->entlightsframe = r_framecount;