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"
25 rdlight_t r_dlight[MAX_DLIGHTS];
28 cvar_t r_modellights = {CVAR_SAVE, "r_modellights", "4"};
29 cvar_t r_vismarklights = {0, "r_vismarklights", "1"};
30 cvar_t r_coronas = {CVAR_SAVE, "r_coronas", "1"};
31 cvar_t gl_flashblend = {CVAR_SAVE, "gl_flashblend", "1"};
33 static rtexture_t *lightcorona;
34 static rtexturepool_t *lighttexturepool;
36 void r_light_start(void)
40 qbyte pixels[32][32][4];
41 lighttexturepool = R_AllocTexturePool();
42 for (y = 0;y < 32;y++)
44 dy = (y - 15.5f) * (1.0f / 16.0f);
45 for (x = 0;x < 32;x++)
47 dx = (x - 15.5f) * (1.0f / 16.0f);
48 a = ((1.0f / (dx * dx + dy * dy + 0.2f)) - (1.0f / (1.0f + 0.2))) * 32.0f / (1.0f / (1.0f + 0.2));
53 pixels[y][x][3] = 255;
56 lightcorona = R_LoadTexture2D(lighttexturepool, "lightcorona", 32, 32, &pixels[0][0][0], TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
59 void r_light_shutdown(void)
61 lighttexturepool = NULL;
65 void r_light_newmap(void)
68 for (i = 0;i < 256;i++)
69 d_lightstylevalue[i] = 264; // normal light value
72 void R_Light_Init(void)
74 Cvar_RegisterVariable(&r_modellights);
75 Cvar_RegisterVariable(&r_vismarklights);
76 Cvar_RegisterVariable(&r_coronas);
77 Cvar_RegisterVariable(&gl_flashblend);
78 R_RegisterModule("R_Light", r_light_start, r_light_shutdown, r_light_newmap);
86 void R_AnimateLight (void)
92 // 'm' is normal light, 'a' is no light, 'z' is double bright
93 i = (int)(cl.time * 10);
94 for (j = 0;j < MAX_LIGHTSTYLES;j++)
96 if (!cl_lightstyle || !cl_lightstyle[j].length)
98 d_lightstylevalue[j] = 256;
101 k = i % cl_lightstyle[j].length;
102 k = cl_lightstyle[j].map[k] - 'a';
104 d_lightstylevalue[j] = k;
109 void R_BuildLightList(void)
118 if (!r_dynamic.integer || !cl_dlights)
121 for (i = 0;i < MAX_DLIGHTS;i++)
126 rd = &r_dlight[r_numdlights++];
127 VectorCopy(cd->origin, rd->origin);
128 VectorScale(cd->color, cd->radius * 64.0f, rd->light);
129 rd->cullradius2 = DotProduct(rd->light, rd->light) * (0.25f / (64.0f * 64.0f)) + 4096.0f;
130 // clamp radius to avoid overflowing division table in lightmap code
131 if (rd->cullradius2 > (2048.0f * 2048.0f))
132 rd->cullradius2 = (2048.0f * 2048.0f);
133 rd->cullradius = sqrt(rd->cullradius2);
134 rd->subtract = 1.0f / rd->cullradius2;
136 c_dlights++; // count every dlight in use
140 void R_DrawCoronas(void)
144 float scale, viewdist, diff[3], dist;
146 if (!r_coronas.integer)
148 memset(&m, 0, sizeof(m));
149 m.blendfunc1 = GL_ONE;
150 m.blendfunc2 = GL_ONE;
151 m.depthdisable = true; // magic
152 m.tex[0] = R_GetTexture(lightcorona);
153 R_Mesh_Matrix(&r_identitymatrix);
155 viewdist = DotProduct(r_origin, vpn);
156 varray_texcoord[0][ 0] = 0;varray_texcoord[0][ 1] = 0;
157 varray_texcoord[0][ 4] = 0;varray_texcoord[0][ 5] = 1;
158 varray_texcoord[0][ 8] = 1;varray_texcoord[0][ 9] = 1;
159 varray_texcoord[0][12] = 1;varray_texcoord[0][13] = 0;
160 for (i = 0;i < r_numdlights;i++)
163 dist = (DotProduct(rd->origin, vpn) - viewdist);
164 if (dist >= 24.0f && CL_TraceLine(rd->origin, r_origin, NULL, NULL, 0, true, NULL) == 1)
166 scale = r_colorscale * (1.0f / 131072.0f);
167 if (gl_flashblend.integer)
171 VectorSubtract(rd->origin, r_origin, diff);
172 scale *= 1 - exp(fogdensity/DotProduct(diff,diff));
174 GL_Color(rd->light[0] * scale, rd->light[1] * scale, rd->light[2] * scale, 1);
175 scale = rd->cullradius * 0.25f;
176 if (gl_flashblend.integer)
178 varray_vertex[0] = rd->origin[0] - vright[0] * scale - vup[0] * scale;
179 varray_vertex[1] = rd->origin[1] - vright[1] * scale - vup[1] * scale;
180 varray_vertex[2] = rd->origin[2] - vright[2] * scale - vup[2] * scale;
181 varray_vertex[4] = rd->origin[0] - vright[0] * scale + vup[0] * scale;
182 varray_vertex[5] = rd->origin[1] - vright[1] * scale + vup[1] * scale;
183 varray_vertex[6] = rd->origin[2] - vright[2] * scale + vup[2] * scale;
184 varray_vertex[8] = rd->origin[0] + vright[0] * scale + vup[0] * scale;
185 varray_vertex[9] = rd->origin[1] + vright[1] * scale + vup[1] * scale;
186 varray_vertex[10] = rd->origin[2] + vright[2] * scale + vup[2] * scale;
187 varray_vertex[12] = rd->origin[0] + vright[0] * scale - vup[0] * scale;
188 varray_vertex[13] = rd->origin[1] + vright[1] * scale - vup[1] * scale;
189 varray_vertex[14] = rd->origin[2] + vright[2] * scale - vup[2] * scale;
190 R_Mesh_Draw(4, 2, polygonelements);
196 =============================================================================
200 =============================================================================
208 static void R_OldMarkLights (entity_render_t *ent, vec3_t lightorigin, rdlight_t *rd, int bit, int bitindex, mnode_t *node)
210 float ndist, maxdist;
212 int i, *surfacepvsframes;
213 int d, impacts, impactt;
214 float dist, dist2, impact[3];
216 if (!r_dynamic.integer)
219 // for comparisons to minimum acceptable light
220 maxdist = rd->cullradius2;
222 surfacepvsframes = ent->model->surfacepvsframes;
224 if (node->contents < 0)
227 ndist = PlaneDiff(lightorigin, node->plane);
229 if (ndist > rd->cullradius)
231 node = node->children[0];
234 if (ndist < -rd->cullradius)
236 node = node->children[1];
241 surf = ent->model->surfaces + node->firstsurface;
242 for (i = 0;i < node->numsurfaces;i++, surf++)
244 if (surfacepvsframes[surf->number] != ent->model->pvsframecount)
247 if (surf->flags & SURF_PLANEBACK)
250 if (dist < -0.25f && !(surf->flags & SURF_LIGHTBOTHSIDES))
254 if (dist2 >= maxdist)
257 if (node->plane->type < 3)
259 VectorCopy(lightorigin, impact);
260 impact[node->plane->type] -= dist;
264 impact[0] = lightorigin[0] - surf->plane->normal[0] * dist;
265 impact[1] = lightorigin[1] - surf->plane->normal[1] * dist;
266 impact[2] = lightorigin[2] - surf->plane->normal[2] * dist;
269 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
271 d = bound(0, impacts, surf->extents[0] + 16) - impacts;
276 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
278 d = bound(0, impactt, surf->extents[1] + 16) - impactt;
283 if (surf->dlightframe != r_framecount) // not dynamic until now
285 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;
286 surf->dlightframe = r_framecount;
288 surf->dlightbits[bitindex] |= bit;
291 if (node->children[0]->contents >= 0)
293 if (node->children[1]->contents >= 0)
295 R_OldMarkLights (ent, lightorigin, rd, bit, bitindex, node->children[0]);
296 node = node->children[1];
301 node = node->children[0];
305 else if (node->children[1]->contents >= 0)
307 node = node->children[1];
313 static void R_VisMarkLights (entity_render_t *ent, rdlight_t *rd, int bit, int bitindex)
315 static int lightframe = 0;
319 int i, k, m, c, leafnum, *surfacepvsframes, *mark;
324 float low[3], high[3], dist, maxdist;
326 if (!r_dynamic.integer)
329 Matrix4x4_Transform(&ent->inversematrix, rd->origin, lightorigin);
332 pvsleaf = Mod_PointInLeaf (lightorigin, model);
336 in = pvsleaf->compressed_vis;
337 if (!r_vismarklights.integer || !in)
339 // told not to use pvs, or there's no pvs to use
340 R_OldMarkLights(ent, lightorigin, rd, bit, bitindex, model->nodes + model->hulls[0].firstclipnode);
346 low[0] = lightorigin[0] - rd->cullradius;low[1] = lightorigin[1] - rd->cullradius;low[2] = lightorigin[2] - rd->cullradius;
347 high[0] = lightorigin[0] + rd->cullradius;high[1] = lightorigin[1] + rd->cullradius;high[2] = lightorigin[2] + rd->cullradius;
349 // for comparisons to minimum acceptable light
350 maxdist = rd->cullradius2;
352 row = (model->numleafs+7)>>3;
353 surfacepvsframes = model->surfacepvsframes;
361 for (i = 0;i < 8;i++)
365 // warning to the clumsy: numleafs is one less than it should be, it only counts leafs with vis bits (skips leaf 0)
366 leafnum = (k << 3)+i+1;
367 if (leafnum > model->numleafs)
369 leaf = &model->leafs[leafnum];
370 if (leaf->mins[0] > high[0] || leaf->maxs[0] < low[0]
371 || leaf->mins[1] > high[1] || leaf->maxs[1] < low[1]
372 || leaf->mins[2] > high[2] || leaf->maxs[2] < low[2])
374 if ((m = leaf->nummarksurfaces))
376 mark = leaf->firstmarksurface;
379 surf = model->surfaces + *mark++;
380 // if not visible in current frame, or already marked because it was in another leaf we passed, skip
381 if (surf->lightframe == lightframe)
383 surf->lightframe = lightframe;
384 if (surfacepvsframes[surf->number] != model->pvsframecount)
386 dist = PlaneDiff(lightorigin, surf->plane);
387 if (surf->flags & SURF_PLANEBACK)
389 // LordHavoc: make sure it is infront of the surface and not too far away
390 if (dist < rd->cullradius && (dist > -0.25f || ((surf->flags & SURF_LIGHTBOTHSIDES) && dist > -rd->cullradius)))
393 int impacts, impactt;
394 float dist2, impact[3];
398 if (surf->plane->type < 3)
400 VectorCopy(lightorigin, impact);
401 impact[surf->plane->type] -= dist;
405 impact[0] = lightorigin[0] - surf->plane->normal[0] * dist;
406 impact[1] = lightorigin[1] - surf->plane->normal[1] * dist;
407 impact[2] = lightorigin[2] - surf->plane->normal[2] * dist;
410 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
411 d = bound(0, impacts, surf->extents[0] + 16) - impacts;
416 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
417 d = bound(0, impactt, surf->extents[1] + 16) - impactt;
422 if (surf->dlightframe != r_framecount) // not dynamic until now
424 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;
425 surf->dlightframe = r_framecount;
427 surf->dlightbits[bitindex] |= bit;
442 void R_MarkLights(entity_render_t *ent)
445 if (!gl_flashblend.integer)
446 for (i = 0;i < r_numdlights;i++)
447 R_VisMarkLights (ent, r_dlight + i, 1 << (i & 31), i >> 5);
451 =============================================================================
455 =============================================================================
458 static int RecursiveLightPoint (vec3_t color, const mnode_t *node, float x, float y, float startz, float endz)
460 int side, distz = endz - startz;
465 if (node->contents < 0)
466 return false; // didn't hit anything
468 switch (node->plane->type)
471 node = node->children[x < node->plane->dist];
474 node = node->children[y < node->plane->dist];
477 side = startz < node->plane->dist;
478 if ((endz < node->plane->dist) == side)
480 node = node->children[side];
483 // found an intersection
484 mid = node->plane->dist;
487 back = front = x * node->plane->normal[0] + y * node->plane->normal[1];
488 front += startz * node->plane->normal[2];
489 back += endz * node->plane->normal[2];
490 side = front < node->plane->dist;
491 if ((back < node->plane->dist) == side)
493 node = node->children[side];
496 // found an intersection
497 mid = startz + distz * (front - node->plane->dist) / (front - back);
501 // go down front side
502 if (node->children[side]->contents >= 0 && RecursiveLightPoint (color, node->children[side], x, y, startz, mid))
503 return true; // hit something
506 // check for impact on this node
507 if (node->numsurfaces)
512 surf = cl.worldmodel->surfaces + node->firstsurface;
513 for (i = 0;i < node->numsurfaces;i++, surf++)
515 if (!(surf->flags & SURF_LIGHTMAP))
516 continue; // no lightmaps
518 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]);
519 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]);
521 if (ds < surf->texturemins[0] || dt < surf->texturemins[1])
524 ds -= surf->texturemins[0];
525 dt -= surf->texturemins[1];
527 if (ds > surf->extents[0] || dt > surf->extents[1])
533 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;
534 line3 = ((surf->extents[0]>>4)+1)*3;
535 size3 = ((surf->extents[0]>>4)+1) * ((surf->extents[1]>>4)+1)*3; // LordHavoc: *3 for colored lighting
537 lightmap = surf->samples + ((dt>>4) * ((surf->extents[0]>>4)+1) + (ds>>4))*3; // LordHavoc: *3 for color
539 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
541 scale = d_lightstylevalue[surf->styles[maps]];
542 r00 += lightmap[ 0] * scale;g00 += lightmap[ 1] * scale;b00 += lightmap[ 2] * scale;
543 r01 += lightmap[ 3] * scale;g01 += lightmap[ 4] * scale;b01 += lightmap[ 5] * scale;
544 r10 += lightmap[line3+0] * scale;g10 += lightmap[line3+1] * scale;b10 += lightmap[line3+2] * scale;
545 r11 += lightmap[line3+3] * scale;g11 += lightmap[line3+4] * scale;b11 += lightmap[line3+5] * scale;
550 LordHavoc: here's the readable version of the interpolation
551 code, not quite as easy for the compiler to optimize...
553 dsfrac is the X position in the lightmap pixel, * 16
554 dtfrac is the Y position in the lightmap pixel, * 16
555 r00 is top left corner, r01 is top right corner
556 r10 is bottom left corner, r11 is bottom right corner
557 g and b are the same layout.
558 r0 and r1 are the top and bottom intermediate results
560 first we interpolate the top two points, to get the top
563 r0 = (((r01-r00) * dsfrac) >> 4) + r00;
564 g0 = (((g01-g00) * dsfrac) >> 4) + g00;
565 b0 = (((b01-b00) * dsfrac) >> 4) + b00;
567 then we interpolate the bottom two points, to get the
570 r1 = (((r11-r10) * dsfrac) >> 4) + r10;
571 g1 = (((g11-g10) * dsfrac) >> 4) + g10;
572 b1 = (((b11-b10) * dsfrac) >> 4) + b10;
574 then we interpolate the top and bottom samples to get the
575 middle sample (the one which was requested)
577 r = (((r1-r0) * dtfrac) >> 4) + r0;
578 g = (((g1-g0) * dtfrac) >> 4) + g0;
579 b = (((b1-b0) * dtfrac) >> 4) + b0;
582 color[0] += (float) ((((((((r11-r10) * dsfrac) >> 4) + r10)-((((r01-r00) * dsfrac) >> 4) + r00)) * dtfrac) >> 4) + ((((r01-r00) * dsfrac) >> 4) + r00)) * (1.0f / 32768.0f);
583 color[1] += (float) ((((((((g11-g10) * dsfrac) >> 4) + g10)-((((g01-g00) * dsfrac) >> 4) + g00)) * dtfrac) >> 4) + ((((g01-g00) * dsfrac) >> 4) + g00)) * (1.0f / 32768.0f);
584 color[2] += (float) ((((((((b11-b10) * dsfrac) >> 4) + b10)-((((b01-b00) * dsfrac) >> 4) + b00)) * dtfrac) >> 4) + ((((b01-b00) * dsfrac) >> 4) + b00)) * (1.0f / 32768.0f);
586 return true; // success
591 node = node->children[side ^ 1];
593 distz = endz - startz;
598 void R_CompleteLightPoint (vec3_t color, const vec3_t p, int dynamic, const mleaf_t *leaf)
606 leaf = Mod_PointInLeaf(p, cl.worldmodel);
607 if (!leaf || leaf->contents == CONTENTS_SOLID || r_fullbright.integer || !cl.worldmodel->lightdata)
609 color[0] = color[1] = color[2] = 1;
613 color[0] = color[1] = color[2] = r_ambient.value * (2.0f / 128.0f);
614 if (cl.worldmodel->numlights)
616 for (i = 0;i < cl.worldmodel->numlights;i++)
618 sl = cl.worldmodel->lights + i;
619 if (d_lightstylevalue[sl->style] > 0)
621 VectorSubtract (p, sl->origin, v);
622 f = ((1.0f / (DotProduct(v, v) * sl->falloff + sl->distbias)) - sl->subtract);
623 if (f > 0 && CL_TraceLine(p, sl->origin, NULL, NULL, 0, false, NULL) == 1)
625 f *= d_lightstylevalue[sl->style] * (1.0f / 65536.0f);
626 VectorMA(color, f, sl->light, color);
632 RecursiveLightPoint (color, cl.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
636 for (i = 0;i < r_numdlights;i++)
639 VectorSubtract (p, rd->origin, v);
640 f = DotProduct(v, v);
641 if (f < rd->cullradius2 && CL_TraceLine(p, rd->origin, NULL, NULL, 0, false, NULL) == 1)
643 f = (1.0f / (f + LIGHTOFFSET)) - rd->subtract;
644 VectorMA(color, f, rd->light, color);
650 void R_ModelLightPoint (const entity_render_t *ent, vec3_t color, const vec3_t p)
653 leaf = Mod_PointInLeaf(p, cl.worldmodel);
654 if (!leaf || leaf->contents == CONTENTS_SOLID || r_fullbright.integer || !cl.worldmodel->lightdata || ent->effects & EF_FULLBRIGHT)
656 color[0] = color[1] = color[2] = 1;
660 color[0] = color[1] = color[2] = r_ambient.value * (2.0f / 128.0f);
661 if (!cl.worldmodel->numlights)
662 RecursiveLightPoint (color, cl.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
665 void R_LightModel(const entity_render_t *ent, int numverts, float *vertices, float *normals, float *colors, float colorr, float colorg, float colorb, int worldcoords)
667 int i, j, nearlights = 0, maxnearlights = r_modellights.integer;
668 float color[3], basecolor[3], v[3], t, *av, *avn, *avc, a, f, dist2, mscale, dot, stylescale, intensity, ambientcolor[3];
674 // how much this light would contribute to ambient if replaced
679 // used for choosing only the brightest lights
682 nearlight[MAX_DLIGHTS], *nl;
686 // scale of the model's coordinate space, to alter light attenuation to match
687 // make the mscale squared so it can scale the squared distance results
688 mscale = ent->scale * ent->scale;
689 if ((maxnearlights != 0) && !r_fullbright.integer && !(ent->effects & EF_FULLBRIGHT))
691 R_ModelLightPoint(ent, basecolor, ent->origin);
694 for (i = 0;i < ent->numentlights;i++)
696 sl = cl.worldmodel->lights + ent->entlights[i];
697 stylescale = d_lightstylevalue[sl->style] * (1.0f / 65536.0f);
698 VectorSubtract (ent->origin, sl->origin, v);
699 f = ((1.0f / (DotProduct(v, v) * sl->falloff + sl->distbias)) - sl->subtract) * stylescale;
700 VectorScale(sl->light, f, ambientcolor);
701 intensity = DotProduct(ambientcolor, ambientcolor);
704 if (nearlights < maxnearlights)
708 for (j = 0;j < maxnearlights;j++)
710 if (nearlight[j].intensity < intensity)
712 if (nearlight[j].intensity > 0)
713 VectorAdd(basecolor, nearlight[j].ambientlight, basecolor);
718 if (j >= maxnearlights)
720 // this light is less significant than all others,
723 VectorAdd(basecolor, ambientcolor, basecolor);
728 nl->intensity = intensity;
729 // transform the light into the model's coordinate system
731 VectorCopy(sl->origin, nl->origin);
733 Matrix4x4_Transform(&ent->inversematrix, sl->origin, nl->origin);
734 // integrate mscale into falloff, for maximum speed
735 nl->falloff = sl->falloff * mscale;
736 VectorCopy(ambientcolor, nl->ambientlight);
737 nl->light[0] = sl->light[0] * stylescale * colorr * 4.0f;
738 nl->light[1] = sl->light[1] * stylescale * colorg * 4.0f;
739 nl->light[2] = sl->light[2] * stylescale * colorb * 4.0f;
740 nl->subtract = sl->subtract;
741 nl->offset = sl->distbias;
744 for (i = 0;i < r_numdlights;i++)
747 VectorCopy(rd->origin, v);
748 if (v[0] < ent->mins[0]) v[0] = ent->mins[0];if (v[0] > ent->maxs[0]) v[0] = ent->maxs[0];
749 if (v[1] < ent->mins[1]) v[1] = ent->mins[1];if (v[1] > ent->maxs[1]) v[1] = ent->maxs[1];
750 if (v[2] < ent->mins[2]) v[2] = ent->mins[2];if (v[2] > ent->maxs[2]) v[2] = ent->maxs[2];
751 VectorSubtract (v, rd->origin, v);
752 if (DotProduct(v, v) < rd->cullradius2)
754 if (CL_TraceLine(ent->origin, rd->origin, NULL, NULL, 0, false, NULL) != 1)
756 VectorSubtract (ent->origin, rd->origin, v);
757 f = ((1.0f / (DotProduct(v, v) + LIGHTOFFSET)) - rd->subtract);
758 VectorScale(rd->light, f, ambientcolor);
759 intensity = DotProduct(ambientcolor, ambientcolor);
762 if (nearlights < maxnearlights)
766 for (j = 0;j < maxnearlights;j++)
768 if (nearlight[j].intensity < intensity)
770 if (nearlight[j].intensity > 0)
771 VectorAdd(basecolor, nearlight[j].ambientlight, basecolor);
776 if (j >= maxnearlights)
778 // this light is less significant than all others,
781 VectorAdd(basecolor, ambientcolor, basecolor);
786 nl->intensity = intensity;
787 // transform the light into the model's coordinate system
789 VectorCopy(rd->origin, nl->origin);
792 Matrix4x4_Transform(&ent->inversematrix, rd->origin, nl->origin);
794 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"
795 , rd - r_dlight, ent->model->name
796 , rd->origin[0], rd->origin[1], rd->origin[2]
797 , nl->origin[0], nl->origin[1], nl->origin[2]
798 , ent->inversematrix.m[0][0], ent->inversematrix.m[0][1], ent->inversematrix.m[0][2], ent->inversematrix.m[0][3]
799 , ent->inversematrix.m[1][0], ent->inversematrix.m[1][1], ent->inversematrix.m[1][2], ent->inversematrix.m[1][3]
800 , ent->inversematrix.m[2][0], ent->inversematrix.m[2][1], ent->inversematrix.m[2][2], ent->inversematrix.m[2][3]
801 , ent->inversematrix.m[3][0], ent->inversematrix.m[3][1], ent->inversematrix.m[3][2], ent->inversematrix.m[3][3]);
804 // integrate mscale into falloff, for maximum speed
805 nl->falloff = mscale;
806 VectorCopy(ambientcolor, nl->ambientlight);
807 nl->light[0] = rd->light[0] * colorr * 4.0f;
808 nl->light[1] = rd->light[1] * colorg * 4.0f;
809 nl->light[2] = rd->light[2] * colorb * 4.0f;
810 nl->subtract = rd->subtract;
811 nl->offset = LIGHTOFFSET;
818 R_CompleteLightPoint (basecolor, ent->origin, true, NULL);
820 basecolor[0] *= colorr;
821 basecolor[1] *= colorg;
822 basecolor[2] *= colorb;
828 for (i = 0;i < numverts;i++)
830 VectorCopy(basecolor, color);
831 for (j = 0, nl = &nearlight[0];j < nearlights;j++, nl++)
833 VectorSubtract(nl->origin, av, v);
834 // directional shading
835 dot = DotProduct(avn,v);
838 // the vertex normal faces the light
840 // do the distance attenuation
841 dist2 = DotProduct(v,v);
842 f = (1.0f / (dist2 * nl->falloff + nl->offset)) - nl->subtract;
846 t = 1.0f / sqrt(dist2);
848 //*((int *)&t) = 0x5f3759df - ((* (int *) &dist2) >> 1);
849 //t = t * (1.5f - (dist2 * 0.5f * t * t));
852 // dot * t is dotproduct with a normalized v.
853 // (the result would be -1 to +1, but we already
854 // eliminated the <= 0 case, so it is 0 to 1)
856 // the hardness variables are for backlighting/shinyness
857 // these have been hardwired at * 0.5 + 0.5 to match
858 // the quake map lighting utility's equations
859 f *= dot * t;// * 0.5f + 0.5f;// * hardness + hardnessoffset;
860 VectorMA(color, f, nl->light, color);
865 VectorCopy(color, avc);
874 for (i = 0;i < numverts;i++)
876 VectorCopy(basecolor, avc);
883 void R_UpdateEntLights(entity_render_t *ent)
888 VectorSubtract(ent->origin, ent->entlightsorigin, v);
889 if (ent->entlightsframe != (r_framecount - 1) || (realtime > ent->entlightstime && DotProduct(v,v) >= 1.0f))
891 ent->entlightstime = realtime + 0.1;
892 VectorCopy(ent->origin, ent->entlightsorigin);
893 ent->numentlights = 0;
895 for (i = 0, sl = cl.worldmodel->lights;i < cl.worldmodel->numlights && ent->numentlights < MAXENTLIGHTS;i++, sl++)
896 if (CL_TraceLine(ent->origin, sl->origin, NULL, NULL, 0, false, NULL) == 1)
897 ent->entlights[ent->numentlights++] = i;
899 ent->entlightsframe = r_framecount;