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_lightmodels = {CVAR_SAVE, "r_lightmodels", "1"};
29 cvar_t r_vismarklights = {0, "r_vismarklights", "1"};
31 static rtexture_t *lightcorona;
32 static rtexturepool_t *lighttexturepool;
34 void r_light_start(void)
38 qbyte pixels[32][32][4];
39 lighttexturepool = R_AllocTexturePool();
40 for (y = 0;y < 32;y++)
42 dy = (y - 15.5f) * (1.0f / 16.0f);
43 for (x = 0;x < 32;x++)
45 dx = (x - 15.5f) * (1.0f / 16.0f);
46 a = ((1.0f / (dx * dx + dy * dy + 0.2f)) - (1.0f / (1.0f + 0.2))) * 32.0f / (1.0f / (1.0f + 0.2));
48 pixels[y][x][0] = 255;
49 pixels[y][x][1] = 255;
50 pixels[y][x][2] = 255;
54 lightcorona = R_LoadTexture (lighttexturepool, "lightcorona", 32, 32, &pixels[0][0][0], TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_ALPHA);
57 void r_light_shutdown(void)
59 lighttexturepool = NULL;
63 void r_light_newmap(void)
67 void R_Light_Init(void)
69 Cvar_RegisterVariable(&r_lightmodels);
70 Cvar_RegisterVariable(&r_vismarklights);
71 R_RegisterModule("R_Light", r_light_start, r_light_shutdown, r_light_newmap);
79 void R_AnimateLight (void)
85 // 'm' is normal light, 'a' is no light, 'z' is double bright
86 i = (int)(cl.time * 10);
87 for (j = 0;j < MAX_LIGHTSTYLES;j++)
89 if (!cl_lightstyle[j].length)
91 d_lightstylevalue[j] = 256;
94 k = i % cl_lightstyle[j].length;
95 k = cl_lightstyle[j].map[k] - 'a';
97 d_lightstylevalue[j] = k;
102 void R_BuildLightList(void)
111 if (!r_dynamic.integer)
114 for (i = 0;i < MAX_DLIGHTS;i++)
119 rd = &r_dlight[r_numdlights++];
120 VectorCopy(cd->origin, rd->origin);
121 VectorScale(cd->color, cd->radius * 128.0f, rd->light);
122 rd->cullradius = (1.0f / 128.0f) * sqrt(DotProduct(rd->light, rd->light));
123 // clamp radius to avoid overflowing division table in lightmap code
124 if (rd->cullradius > 2048.0f)
125 rd->cullradius = 2048.0f;
126 rd->cullradius2 = rd->cullradius * rd->cullradius;
127 rd->lightsubtract = 1.0f / rd->cullradius2;
130 c_dlights++; // count every dlight in use
134 static int coronapolyindex[6] = {0, 1, 2, 0, 2, 3};
136 void R_DrawCoronas(void)
140 float tvxyz[4][4], tvst[4][2], scale, viewdist, diff[3], dist;
142 memset(&m, 0, sizeof(m));
143 m.transparent = false;
144 m.blendfunc1 = GL_SRC_ALPHA;
145 m.blendfunc2 = GL_ONE;
146 m.depthdisable = true; // magic
149 m.index = coronapolyindex;
150 m.vertex = &tvxyz[0][0];
151 m.vertexstep = sizeof(float[4]);
152 m.tex[0] = R_GetTexture(lightcorona);
153 m.texcoords[0] = &tvst[0][0];
154 m.texcoordstep[0] = sizeof(float[2]);
163 viewdist = DotProduct(r_origin, vpn);
164 for (i = 0;i < r_numdlights;i++)
167 dist = (DotProduct(rd->origin, vpn) - viewdist);
170 // trace to a point just barely closer to the eye
171 VectorSubtract(rd->origin, vpn, diff);
172 if (CL_TraceLine(r_origin, diff, NULL, NULL, 0, true) == 1)
174 scale = 1.0f / 131072.0f;
175 m.cr = rd->light[0] * scale;
176 m.cg = rd->light[1] * scale;
177 m.cb = rd->light[2] * scale;
181 VectorSubtract(rd->origin, r_origin, diff);
182 m.ca *= 1 - exp(fogdensity/DotProduct(diff,diff));
184 scale = rd->cullradius * 0.25f;
185 tvxyz[0][0] = rd->origin[0] - vright[0] * scale - vup[0] * scale;
186 tvxyz[0][1] = rd->origin[1] - vright[1] * scale - vup[1] * scale;
187 tvxyz[0][2] = rd->origin[2] - vright[2] * scale - vup[2] * scale;
188 tvxyz[1][0] = rd->origin[0] - vright[0] * scale + vup[0] * scale;
189 tvxyz[1][1] = rd->origin[1] - vright[1] * scale + vup[1] * scale;
190 tvxyz[1][2] = rd->origin[2] - vright[2] * scale + vup[2] * scale;
191 tvxyz[2][0] = rd->origin[0] + vright[0] * scale + vup[0] * scale;
192 tvxyz[2][1] = rd->origin[1] + vright[1] * scale + vup[1] * scale;
193 tvxyz[2][2] = rd->origin[2] + vright[2] * scale + vup[2] * scale;
194 tvxyz[3][0] = rd->origin[0] + vright[0] * scale - vup[0] * scale;
195 tvxyz[3][1] = rd->origin[1] + vright[1] * scale - vup[1] * scale;
196 tvxyz[3][2] = rd->origin[2] + vright[2] * scale - vup[2] * scale;
204 =============================================================================
208 =============================================================================
216 static void R_OldMarkLights (vec3_t lightorigin, rdlight_t *rd, int bit, int bitindex, mnode_t *node)
218 float ndist, maxdist;
223 if (!r_dynamic.integer)
226 // for comparisons to minimum acceptable light
227 maxdist = rd->cullradius2;
230 if (node->contents < 0)
232 if (node->contents != CONTENTS_SOLID)
234 leaf = (mleaf_t *)node;
235 if (leaf->dlightframe != r_framecount) // not dynamic until now
237 leaf->dlightbits[0] = leaf->dlightbits[1] = leaf->dlightbits[2] = leaf->dlightbits[3] = leaf->dlightbits[4] = leaf->dlightbits[5] = leaf->dlightbits[6] = leaf->dlightbits[7] = 0;
238 leaf->dlightframe = r_framecount;
240 leaf->dlightbits[bitindex] |= bit;
245 ndist = PlaneDiff(lightorigin, node->plane);
247 if (ndist > rd->cullradius)
249 node = node->children[0];
252 if (ndist < -rd->cullradius)
254 node = node->children[1];
259 surf = currentrenderentity->model->surfaces + node->firstsurface;
260 for (i=0 ; i<node->numsurfaces ; i++, surf++)
262 int d, impacts, impactt;
263 float dist, dist2, impact[3];
264 if (surf->visframe != r_framecount)
267 if (surf->flags & SURF_PLANEBACK)
270 if (dist < -0.25f && !(surf->flags & SURF_LIGHTBOTHSIDES))
274 if (dist2 >= maxdist)
277 if (node->plane->type < 3)
279 VectorCopy(rd->origin, impact);
280 impact[node->plane->type] -= dist;
284 impact[0] = rd->origin[0] - surf->plane->normal[0] * dist;
285 impact[1] = rd->origin[1] - surf->plane->normal[1] * dist;
286 impact[2] = rd->origin[2] - surf->plane->normal[2] * dist;
289 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
291 d = bound(0, impacts, surf->extents[0] + 16) - impacts;
296 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
298 d = bound(0, impactt, surf->extents[1] + 16) - impactt;
303 if (surf->dlightframe != r_framecount) // not dynamic until now
305 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;
306 surf->dlightframe = r_framecount;
308 surf->dlightbits[bitindex] |= bit;
311 if (node->children[0]->contents >= 0)
313 if (node->children[1]->contents >= 0)
315 R_OldMarkLights (lightorigin, rd, bit, bitindex, node->children[0]);
316 node = node->children[1];
321 node = node->children[0];
325 else if (node->children[1]->contents >= 0)
327 node = node->children[1];
333 static void R_VisMarkLights (rdlight_t *rd, int bit, int bitindex)
335 static int lightframe = 0;
339 int i, k, m, c, leafnum;
340 msurface_t *surf, **mark;
344 float low[3], high[3], dist, maxdist;
346 if (!r_dynamic.integer)
349 model = currentrenderentity->model;
350 softwareuntransform(rd->origin, lightorigin);
352 if (!r_vismarklights.integer)
354 R_OldMarkLights(lightorigin, rd, bit, bitindex, model->nodes + model->hulls[0].firstclipnode);
358 pvsleaf = Mod_PointInLeaf (lightorigin, model);
361 Con_Printf("R_VisMarkLights: NULL leaf??\n");
362 R_OldMarkLights(lightorigin, rd, bit, bitindex, model->nodes + model->hulls[0].firstclipnode);
366 in = pvsleaf->compressed_vis;
369 // no vis info, so make all visible
370 R_OldMarkLights(lightorigin, rd, bit, bitindex, model->nodes + model->hulls[0].firstclipnode);
376 low[0] = lightorigin[0] - rd->cullradius;low[1] = lightorigin[1] - rd->cullradius;low[2] = lightorigin[2] - rd->cullradius;
377 high[0] = lightorigin[0] + rd->cullradius;high[1] = lightorigin[1] + rd->cullradius;high[2] = lightorigin[2] + rd->cullradius;
379 // for comparisons to minimum acceptable light
380 maxdist = rd->cullradius2;
382 row = (model->numleafs+7)>>3;
390 for (i = 0;i < 8;i++)
394 // warning to the clumsy: numleafs is one less than it should be, it only counts leafs with vis bits (skips leaf 0)
395 leafnum = (k << 3)+i+1;
396 if (leafnum > model->numleafs)
398 leaf = &model->leafs[leafnum];
399 if (leaf->visframe != r_framecount
400 || leaf->contents == CONTENTS_SOLID
401 || leaf->mins[0] > high[0] || leaf->maxs[0] < low[0]
402 || leaf->mins[1] > high[1] || leaf->maxs[1] < low[1]
403 || leaf->mins[2] > high[2] || leaf->maxs[2] < low[2])
405 if (leaf->dlightframe != r_framecount)
407 // not dynamic until now
408 leaf->dlightbits[0] = leaf->dlightbits[1] = leaf->dlightbits[2] = leaf->dlightbits[3] = leaf->dlightbits[4] = leaf->dlightbits[5] = leaf->dlightbits[6] = leaf->dlightbits[7] = 0;
409 leaf->dlightframe = r_framecount;
411 leaf->dlightbits[bitindex] |= bit;
412 if ((m = leaf->nummarksurfaces))
414 mark = leaf->firstmarksurface;
418 // if not visible in current frame, or already marked because it was in another leaf we passed, skip
419 if (surf->lightframe == lightframe)
421 surf->lightframe = lightframe;
422 if (surf->visframe != r_framecount)
424 dist = PlaneDiff(lightorigin, surf->plane);
425 if (surf->flags & SURF_PLANEBACK)
427 // LordHavoc: make sure it is infront of the surface and not too far away
428 if (dist < rd->cullradius && (dist > -0.25f || ((surf->flags & SURF_LIGHTBOTHSIDES) && dist > -rd->cullradius)))
431 int impacts, impactt;
432 float dist2, impact[3];
436 if (surf->plane->type < 3)
438 VectorCopy(rd->origin, impact);
439 impact[surf->plane->type] -= dist;
443 impact[0] = rd->origin[0] - surf->plane->normal[0] * dist;
444 impact[1] = rd->origin[1] - surf->plane->normal[1] * dist;
445 impact[2] = rd->origin[2] - surf->plane->normal[2] * dist;
448 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
449 d = bound(0, impacts, surf->extents[0] + 16) - impacts;
454 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
455 d = bound(0, impactt, surf->extents[1] + 16) - impactt;
460 if (surf->dlightframe != r_framecount) // not dynamic until now
462 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;
463 surf->dlightframe = r_framecount;
465 surf->dlightbits[bitindex] |= bit;
480 void R_MarkLights(void)
483 for (i = 0;i < r_numdlights;i++)
484 R_VisMarkLights (r_dlight + i, 1 << (i & 31), i >> 5);
488 =============================================================================
492 =============================================================================
495 static int RecursiveLightPoint (vec3_t color, mnode_t *node, float x, float y, float startz, float endz)
497 int side, distz = endz - startz;
502 if (node->contents < 0)
503 return false; // didn't hit anything
505 switch (node->plane->type)
508 node = node->children[x < node->plane->dist];
511 node = node->children[y < node->plane->dist];
514 side = startz < node->plane->dist;
515 if ((endz < node->plane->dist) == side)
517 node = node->children[side];
520 // found an intersection
521 mid = node->plane->dist;
524 back = front = x * node->plane->normal[0] + y * node->plane->normal[1];
525 front += startz * node->plane->normal[2];
526 back += endz * node->plane->normal[2];
527 side = front < node->plane->dist;
528 if ((back < node->plane->dist) == side)
530 node = node->children[side];
533 // found an intersection
534 mid = startz + distz * (front - node->plane->dist) / (front - back);
538 // go down front side
539 if (node->children[side]->contents >= 0 && RecursiveLightPoint (color, node->children[side], x, y, startz, mid))
540 return true; // hit something
543 // check for impact on this node
544 if (node->numsurfaces)
549 surf = cl.worldmodel->surfaces + node->firstsurface;
550 for (i = 0;i < node->numsurfaces;i++, surf++)
552 if (!(surf->flags & SURF_LIGHTMAP))
553 continue; // no lightmaps
555 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]);
556 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]);
558 if (ds < surf->texturemins[0] || dt < surf->texturemins[1])
561 ds -= surf->texturemins[0];
562 dt -= surf->texturemins[1];
564 if (ds > surf->extents[0] || dt > surf->extents[1])
570 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;
571 line3 = ((surf->extents[0]>>4)+1)*3;
572 size3 = ((surf->extents[0]>>4)+1) * ((surf->extents[1]>>4)+1)*3; // LordHavoc: *3 for colored lighting
574 lightmap = surf->samples + ((dt>>4) * ((surf->extents[0]>>4)+1) + (ds>>4))*3; // LordHavoc: *3 for color
576 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
578 scale = d_lightstylevalue[surf->styles[maps]];
579 r00 += lightmap[ 0] * scale;g00 += lightmap[ 1] * scale;b00 += lightmap[ 2] * scale;
580 r01 += lightmap[ 3] * scale;g01 += lightmap[ 4] * scale;b01 += lightmap[ 5] * scale;
581 r10 += lightmap[line3+0] * scale;g10 += lightmap[line3+1] * scale;b10 += lightmap[line3+2] * scale;
582 r11 += lightmap[line3+3] * scale;g11 += lightmap[line3+4] * scale;b11 += lightmap[line3+5] * scale;
587 LordHavoc: here's the readable version of the interpolation
588 code, not quite as easy for the compiler to optimize...
590 dsfrac is the X position in the lightmap pixel, * 16
591 dtfrac is the Y position in the lightmap pixel, * 16
592 r00 is top left corner, r01 is top right corner
593 r10 is bottom left corner, r11 is bottom right corner
594 g and b are the same layout.
595 r0 and r1 are the top and bottom intermediate results
597 first we interpolate the top two points, to get the top
600 r0 = (((r01-r00) * dsfrac) >> 4) + r00;
601 g0 = (((g01-g00) * dsfrac) >> 4) + g00;
602 b0 = (((b01-b00) * dsfrac) >> 4) + b00;
604 then we interpolate the bottom two points, to get the
607 r1 = (((r11-r10) * dsfrac) >> 4) + r10;
608 g1 = (((g11-g10) * dsfrac) >> 4) + g10;
609 b1 = (((b11-b10) * dsfrac) >> 4) + b10;
611 then we interpolate the top and bottom samples to get the
612 middle sample (the one which was requested)
614 r = (((r1-r0) * dtfrac) >> 4) + r0;
615 g = (((g1-g0) * dtfrac) >> 4) + g0;
616 b = (((b1-b0) * dtfrac) >> 4) + b0;
619 color[0] += (float) ((((((((r11-r10) * dsfrac) >> 4) + r10)-((((r01-r00) * dsfrac) >> 4) + r00)) * dtfrac) >> 4) + ((((r01-r00) * dsfrac) >> 4) + r00)) * (1.0f / 32768.0f);
620 color[1] += (float) ((((((((g11-g10) * dsfrac) >> 4) + g10)-((((g01-g00) * dsfrac) >> 4) + g00)) * dtfrac) >> 4) + ((((g01-g00) * dsfrac) >> 4) + g00)) * (1.0f / 32768.0f);
621 color[2] += (float) ((((((((b11-b10) * dsfrac) >> 4) + b10)-((((b01-b00) * dsfrac) >> 4) + b00)) * dtfrac) >> 4) + ((((b01-b00) * dsfrac) >> 4) + b00)) * (1.0f / 32768.0f);
623 return true; // success
628 node = node->children[side ^ 1];
630 distz = endz - startz;
635 void R_CompleteLightPoint (vec3_t color, vec3_t p, int dynamic, mleaf_t *leaf)
643 leaf = Mod_PointInLeaf(p, cl.worldmodel);
645 if (leaf->contents == CONTENTS_SOLID)
647 color[0] = color[1] = color[2] = 0;
651 if (r_fullbright.integer || !cl.worldmodel->lightdata)
653 color[0] = color[1] = color[2] = 2;
657 color[0] = color[1] = color[2] = r_ambient.value * (2.0f / 128.0f);
658 if (cl.worldmodel->numlights)
660 for (i = 0;i < cl.worldmodel->numlights;i++)
662 sl = cl.worldmodel->lights + i;
663 if (d_lightstylevalue[sl->style] > 0)
665 VectorSubtract (p, sl->origin, dist);
666 f = DotProduct(dist, dist) + sl->distbias;
667 f = (1.0f / f) - sl->subtract;
670 if (CL_TraceLine(p, sl->origin, NULL, NULL, 0, false) == 1)
672 f *= d_lightstylevalue[sl->style] * (1.0f / 32768.0f);
673 VectorMA(color, f, sl->light, color);
681 RecursiveLightPoint (color, cl.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
683 if (dynamic && leaf->dlightframe == r_framecount)
685 dlightbits = leaf->dlightbits;
686 for (i = 0;i < r_numdlights;i++)
688 if (!(dlightbits[i >> 5] & (1 << (i & 31))))
691 VectorSubtract (p, rd->origin, dist);
692 f = DotProduct(dist, dist) + LIGHTOFFSET;
693 if (f < rd->cullradius2)
695 f = (1.0f / f) - rd->lightsubtract;
697 VectorMA(color, f, rd->light, color);
703 void R_ModelLightPoint (vec3_t color, vec3_t p, int *dlightbits)
706 leaf = Mod_PointInLeaf(p, cl.worldmodel);
707 if (leaf->contents == CONTENTS_SOLID)
709 color[0] = color[1] = color[2] = 0;
710 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
714 if (r_fullbright.integer || !cl.worldmodel->lightdata)
716 color[0] = color[1] = color[2] = 2;
717 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
721 color[0] = color[1] = color[2] = r_ambient.value * (2.0f / 128.0f);
722 if (!cl.worldmodel->numlights)
723 RecursiveLightPoint (color, cl.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
725 if (leaf->dlightframe == r_framecount)
727 dlightbits[0] = leaf->dlightbits[0];
728 dlightbits[1] = leaf->dlightbits[1];
729 dlightbits[2] = leaf->dlightbits[2];
730 dlightbits[3] = leaf->dlightbits[3];
731 dlightbits[4] = leaf->dlightbits[4];
732 dlightbits[5] = leaf->dlightbits[5];
733 dlightbits[6] = leaf->dlightbits[6];
734 dlightbits[7] = leaf->dlightbits[7];
737 dlightbits[0] = dlightbits[1] = dlightbits[2] = dlightbits[3] = dlightbits[4] = dlightbits[5] = dlightbits[6] = dlightbits[7] = 0;
740 void R_LightModel(int numverts, float colorr, float colorg, float colorb, int worldcoords)
742 int i, j, nearlights = 0;
743 float color[3], basecolor[3], v[3], t, *av, *avn, *avc, a, number, f, dist2, mscale;
753 nearlight[MAX_DLIGHTS], *nl;
754 int modeldlightbits[8];
756 a = currentrenderentity->alpha;
757 // scale of the model's coordinate space, to alter light attenuation to match
758 // make the mscale squared so it can scale the squared distance results
759 mscale = currentrenderentity->scale * currentrenderentity->scale;
760 if (r_fullbright.integer || r_ambient.value > 128.0f || currentrenderentity->effects & EF_FULLBRIGHT)
762 basecolor[0] = colorr * 2.0f;
763 basecolor[1] = colorg * 2.0f;
764 basecolor[2] = colorb * 2.0f;
768 if (r_lightmodels.integer)
770 R_ModelLightPoint(basecolor, currentrenderentity->origin, modeldlightbits);
773 VectorSubtract(currentrenderentity->origin, currentrenderentity->entlightsorigin, v);
774 if ((realtime > currentrenderentity->entlightstime && DotProduct(v,v) >= 1.0f) || currentrenderentity->numentlights >= MAXENTLIGHTS)
776 currentrenderentity->numentlights = 0;
777 currentrenderentity->entlightstime = realtime + 0.2;
778 VectorCopy(currentrenderentity->origin, currentrenderentity->entlightsorigin);
779 for (i = 0, sl = cl.worldmodel->lights;i < cl.worldmodel->numlights && nearlights < MAX_DLIGHTS;i++, sl++)
781 if (CL_TraceLine(currentrenderentity->origin, sl->origin, NULL, NULL, 0, false) == 1)
783 if (currentrenderentity->numentlights < MAXENTLIGHTS)
784 currentrenderentity->entlights[currentrenderentity->numentlights++] = i;
786 // integrate mscale into falloff, for maximum speed
787 nl->falloff = mscale * sl->falloff;
788 // transform the light into the model's coordinate system
790 VectorCopy(sl->origin, nl->origin);
792 softwareuntransform(sl->origin, nl->origin);
793 f = d_lightstylevalue[sl->style] * (1.0f / 65536.0f);
794 VectorScale(sl->light, f, nl->light);
795 //nl->cullradius2 = 99999999;
796 nl->lightsubtract = sl->subtract;
797 nl->offset = sl->distbias;
805 for (i = 0;i < currentrenderentity->numentlights && nearlights < MAX_DLIGHTS;i++)
807 sl = cl.worldmodel->lights + currentrenderentity->entlights[i];
809 // integrate mscale into falloff, for maximum speed
810 nl->falloff = mscale * sl->falloff;
811 // transform the light into the model's coordinate system
813 VectorCopy(sl->origin, nl->origin);
815 softwareuntransform(sl->origin, nl->origin);
816 f = d_lightstylevalue[sl->style] * (1.0f / 65536.0f);
817 VectorScale(sl->light, f, nl->light);
818 //nl->cullradius2 = 99999999;
819 nl->lightsubtract = sl->subtract;
820 nl->offset = sl->distbias;
825 for (i = 0;i < r_numdlights && nearlights < MAX_DLIGHTS;i++)
827 if (!(modeldlightbits[i >> 5] & (1 << (i & 31))))
829 if (currentrenderentity == r_dlight[i].ent)
831 f = (1.0f / LIGHTOFFSET) - nl->lightsubtract;
833 VectorMA(basecolor, f, r_dlight[i].light, basecolor);
837 // transform the light into the model's coordinate system
839 VectorCopy(r_dlight[i].origin, nl->origin);
841 softwareuntransform(r_dlight[i].origin, nl->origin);
842 // integrate mscale into falloff, for maximum speed
843 nl->falloff = mscale;
844 // scale the cullradius so culling by distance is done before mscale is applied
845 //nl->cullradius2 = r_dlight[i].cullradius2 * currentrenderentity->scale * currentrenderentity->scale;
846 nl->light[0] = r_dlight[i].light[0] * colorr;
847 nl->light[1] = r_dlight[i].light[1] * colorg;
848 nl->light[2] = r_dlight[i].light[2] * colorb;
849 nl->lightsubtract = r_dlight[i].lightsubtract;
850 nl->offset = LIGHTOFFSET;
857 R_CompleteLightPoint (basecolor, currentrenderentity->origin, true, NULL);
859 basecolor[0] *= colorr;
860 basecolor[1] *= colorg;
861 basecolor[2] *= colorb;
862 avc = aliasvertcolor;
867 for (i = 0;i < numverts;i++)
869 VectorCopy(basecolor, color);
870 for (j = 0, nl = &nearlight[0];j < nearlights;j++, nl++)
872 // distance attenuation
873 VectorSubtract(nl->origin, av, v);
874 dist2 = DotProduct(v,v);
875 f = (1.0f / (dist2 * nl->falloff + nl->offset)) - nl->lightsubtract;
878 // directional shading
880 t = 1.0f / sqrt(dist2);
882 number = DotProduct(v, v);
883 *((int *)&t) = 0x5f3759df - ((* (int *) &number) >> 1);
884 t = t * (1.5f - (number * 0.5f * t * t));
886 // DotProduct(avn,v) * t is dotproduct with a normalized v,
887 // the hardness variables are for backlighting/shinyness
888 f *= DotProduct(avn,v) * t * 0.5f + 0.5f;// * hardness + hardnessoffset;
890 VectorMA(color, f, nl->light, color);
894 VectorCopy(color, avc);
903 for (i = 0;i < numverts;i++)
905 VectorCopy(basecolor, avc);