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)
144 float cscale, scale, viewdist, dist;
146 if (!r_coronas.integer)
148 R_Mesh_Matrix(&r_identitymatrix);
149 viewdist = DotProduct(r_origin, vpn);
150 for (i = 0;i < r_numdlights;i++)
153 dist = (DotProduct(rd->origin, vpn) - viewdist);
154 if (dist >= 24.0f && CL_TraceLine(rd->origin, r_origin, NULL, NULL, 0, true, NULL) == 1)
156 cscale = (1.0f / 131072.0f);
157 scale = rd->cullradius * 0.25f;
158 if (gl_flashblend.integer)
163 R_DrawSprite(GL_ONE, GL_ONE, lightcorona, true, rd->origin, vright, vup, scale, -scale, -scale, scale, rd->light[0] * cscale, rd->light[1] * cscale, rd->light[2] * cscale, 1);
169 =============================================================================
173 =============================================================================
181 static void R_OldMarkLights (entity_render_t *ent, vec3_t lightorigin, rdlight_t *rd, int bit, int bitindex, mnode_t *node)
183 float ndist, maxdist;
185 int i, *surfacepvsframes;
186 int d, impacts, impactt;
187 float dist, dist2, impact[3];
189 if (!r_dynamic.integer)
192 // for comparisons to minimum acceptable light
193 maxdist = rd->cullradius2;
195 surfacepvsframes = ent->model->brushq1.surfacepvsframes;
197 if (node->contents < 0)
200 ndist = PlaneDiff(lightorigin, node->plane);
202 if (ndist > rd->cullradius)
204 node = node->children[0];
207 if (ndist < -rd->cullradius)
209 node = node->children[1];
214 surf = ent->model->brushq1.surfaces + node->firstsurface;
215 for (i = 0;i < node->numsurfaces;i++, surf++)
217 if (surfacepvsframes[surf->number] != ent->model->brushq1.pvsframecount)
220 if (surf->flags & SURF_PLANEBACK)
223 if (dist < -0.25f && !(surf->flags & SURF_LIGHTBOTHSIDES))
227 if (dist2 >= maxdist)
230 if (node->plane->type < 3)
232 VectorCopy(lightorigin, impact);
233 impact[node->plane->type] -= dist;
237 impact[0] = lightorigin[0] - surf->plane->normal[0] * dist;
238 impact[1] = lightorigin[1] - surf->plane->normal[1] * dist;
239 impact[2] = lightorigin[2] - surf->plane->normal[2] * dist;
242 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
244 d = bound(0, impacts, surf->extents[0] + 16) - impacts;
249 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
251 d = bound(0, impactt, surf->extents[1] + 16) - impactt;
256 if (surf->dlightframe != r_framecount) // not dynamic until now
258 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;
259 surf->dlightframe = r_framecount;
260 if (r_dlightmap.integer)
261 surf->cached_dlight = true;
263 surf->dlightbits[bitindex] |= bit;
266 if (node->children[0]->contents >= 0)
268 if (node->children[1]->contents >= 0)
270 R_OldMarkLights (ent, lightorigin, rd, bit, bitindex, node->children[0]);
271 node = node->children[1];
276 node = node->children[0];
280 else if (node->children[1]->contents >= 0)
282 node = node->children[1];
288 static void R_VisMarkLights (entity_render_t *ent, rdlight_t *rd, int bit, int bitindex)
290 static int lightframe = 0;
294 int i, k, m, c, leafnum, *surfacepvsframes, *mark;
299 float low[3], high[3], dist, maxdist;
301 if (!r_dynamic.integer || !ent->model)
304 Matrix4x4_Transform(&ent->inversematrix, rd->origin, lightorigin);
307 pvsleaf = model->brushq1.PointInLeaf(model, lightorigin);
311 in = pvsleaf->compressed_vis;
312 if (!r_vismarklights.integer || !in)
314 // told not to use pvs, or there's no pvs to use
315 R_OldMarkLights(ent, lightorigin, rd, bit, bitindex, model->brushq1.nodes + model->brushq1.hulls[0].firstclipnode);
321 low[0] = lightorigin[0] - rd->cullradius;low[1] = lightorigin[1] - rd->cullradius;low[2] = lightorigin[2] - rd->cullradius;
322 high[0] = lightorigin[0] + rd->cullradius;high[1] = lightorigin[1] + rd->cullradius;high[2] = lightorigin[2] + rd->cullradius;
324 // for comparisons to minimum acceptable light
325 maxdist = rd->cullradius2;
327 row = (model->brushq1.numleafs+7)>>3;
328 surfacepvsframes = model->brushq1.surfacepvsframes;
336 for (i = 0;i < 8;i++)
340 // warning to the clumsy: numleafs is one less than it should be, it only counts leafs with vis bits (skips leaf 0)
341 leafnum = (k << 3)+i+1;
342 if (leafnum > model->brushq1.numleafs)
344 leaf = &model->brushq1.leafs[leafnum];
345 if (leaf->mins[0] > high[0] || leaf->maxs[0] < low[0]
346 || leaf->mins[1] > high[1] || leaf->maxs[1] < low[1]
347 || leaf->mins[2] > high[2] || leaf->maxs[2] < low[2])
349 if ((m = leaf->nummarksurfaces))
351 mark = leaf->firstmarksurface;
354 surf = model->brushq1.surfaces + *mark++;
355 // if not visible in current frame, or already marked because it was in another leaf we passed, skip
356 if (surf->lightframe == lightframe)
358 surf->lightframe = lightframe;
359 if (surfacepvsframes[surf->number] != model->brushq1.pvsframecount)
361 dist = PlaneDiff(lightorigin, surf->plane);
362 if (surf->flags & SURF_PLANEBACK)
364 // LordHavoc: make sure it is infront of the surface and not too far away
365 if (dist < rd->cullradius && (dist > -0.25f || ((surf->flags & SURF_LIGHTBOTHSIDES) && dist > -rd->cullradius)))
368 int impacts, impactt;
369 float dist2, impact[3];
373 if (surf->plane->type < 3)
375 VectorCopy(lightorigin, impact);
376 impact[surf->plane->type] -= dist;
380 impact[0] = lightorigin[0] - surf->plane->normal[0] * dist;
381 impact[1] = lightorigin[1] - surf->plane->normal[1] * dist;
382 impact[2] = lightorigin[2] - surf->plane->normal[2] * dist;
385 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
386 d = bound(0, impacts, surf->extents[0] + 16) - impacts;
391 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
392 d = bound(0, impactt, surf->extents[1] + 16) - impactt;
397 if (surf->dlightframe != r_framecount) // not dynamic until now
399 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;
400 surf->dlightframe = r_framecount;
401 if (r_dlightmap.integer)
402 surf->cached_dlight = true;
404 surf->dlightbits[bitindex] |= bit;
419 void R_MarkLights(entity_render_t *ent)
422 if (!gl_flashblend.integer)
423 for (i = 0;i < r_numdlights;i++)
424 R_VisMarkLights (ent, r_dlight + i, 1 << (i & 31), i >> 5);
428 =============================================================================
432 =============================================================================
435 void R_CompleteLightPoint(vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal, const vec3_t p, int dynamic, const mleaf_t *leaf)
437 VectorClear(diffusecolor);
438 VectorClear(diffusenormal);
440 if (!r_fullbright.integer && cl.worldmodel && cl.worldmodel->brush.LightPoint)
442 ambientcolor[0] = ambientcolor[1] = ambientcolor[2] = r_ambient.value * (2.0f / 128.0f);
443 cl.worldmodel->brush.LightPoint(cl.worldmodel, p, ambientcolor, diffusecolor, diffusenormal);
446 VectorSet(ambientcolor, 1, 1, 1);
449 if (leaf == NULL && cl.worldmodel != NULL)
450 leaf = cl.worldmodel->brushq1.PointInLeaf(cl.worldmodel, p);
451 if (!leaf || leaf->contents == CONTENTS_SOLID || !cl.worldmodel->brushq1.lightdata)
453 VectorSet(ambientcolor, 1, 1, 1);
458 // FIXME: this .lights related stuff needs to be ported into the Mod_Q1BSP code
459 if (cl.worldmodel->brushq1.numlights)
465 for (i = 0;i < cl.worldmodel->brushq1.numlights;i++)
467 sl = cl.worldmodel->brushq1.lights + i;
468 if (d_lightstylevalue[sl->style] > 0)
470 VectorSubtract (p, sl->origin, v);
471 f = ((1.0f / (DotProduct(v, v) * sl->falloff + sl->distbias)) - sl->subtract);
472 if (f > 0 && CL_TraceLine(p, sl->origin, NULL, NULL, 0, false, NULL) == 1)
474 f *= d_lightstylevalue[sl->style] * (1.0f / 65536.0f);
475 VectorMA(ambientcolor, f, sl->light, ambientcolor);
486 // FIXME: this really should handle dlights as diffusecolor/diffusenormal somehow
487 for (i = 0;i < r_numdlights;i++)
490 VectorSubtract(p, rd->origin, v);
491 f = DotProduct(v, v);
492 if (f < rd->cullradius2 && CL_TraceLine(p, rd->origin, NULL, NULL, 0, false, NULL) == 1)
494 f = (1.0f / (f + LIGHTOFFSET)) - rd->subtract;
495 VectorMA(ambientcolor, f, rd->light, ambientcolor);
506 // how much this light would contribute to ambient if replaced
511 // used for choosing only the brightest lights
516 static int nearlights;
517 static nearlight_t nearlight[MAX_DLIGHTS];
519 int R_LightModel(float *ambient4f, float *diffusecolor, float *diffusenormal, const entity_render_t *ent, float colorr, float colorg, float colorb, float colora, int worldcoords)
521 int i, j, maxnearlights;
522 float v[3], f, mscale, stylescale, intensity, ambientcolor[3];
528 maxnearlights = r_modellights.integer;
529 ambient4f[0] = ambient4f[1] = ambient4f[2] = r_ambient.value * (2.0f / 128.0f);
530 VectorClear(diffusecolor);
531 VectorClear(diffusenormal);
532 if (r_fullbright.integer || (ent->effects & EF_FULLBRIGHT))
535 VectorSet(ambient4f, 1, 1, 1);
538 else if (r_shadow_realtime_world.integer)
542 if (cl.worldmodel && cl.worldmodel->brush.LightPoint)
543 cl.worldmodel->brush.LightPoint(cl.worldmodel, ent->origin, ambient4f, diffusecolor, diffusenormal);
545 VectorSet(ambient4f, 1, 1, 1);
548 // scale of the model's coordinate space, to alter light attenuation to match
549 // make the mscale squared so it can scale the squared distance results
550 mscale = ent->scale * ent->scale;
551 // FIXME: no support for .lights on non-Q1BSP?
553 for (i = 0;i < ent->numentlights;i++)
555 sl = cl.worldmodel->brushq1.lights + ent->entlights[i];
556 stylescale = d_lightstylevalue[sl->style] * (1.0f / 65536.0f);
557 VectorSubtract (ent->origin, sl->origin, v);
558 f = ((1.0f / (DotProduct(v, v) * sl->falloff + sl->distbias)) - sl->subtract) * stylescale;
559 VectorScale(sl->light, f, ambientcolor);
560 intensity = DotProduct(ambientcolor, ambientcolor);
563 if (nearlights < maxnearlights)
567 for (j = 0;j < maxnearlights;j++)
569 if (nearlight[j].intensity < intensity)
571 if (nearlight[j].intensity > 0)
572 VectorAdd(ambient4f, nearlight[j].ambientlight, ambient4f);
577 if (j >= maxnearlights)
579 // this light is less significant than all others,
582 VectorAdd(ambient4f, ambientcolor, ambient4f);
587 nl->intensity = intensity;
588 // transform the light into the model's coordinate system
590 VectorCopy(sl->origin, nl->origin);
592 Matrix4x4_Transform(&ent->inversematrix, sl->origin, nl->origin);
593 // integrate mscale into falloff, for maximum speed
594 nl->falloff = sl->falloff * mscale;
595 VectorCopy(ambientcolor, nl->ambientlight);
596 nl->light[0] = sl->light[0] * stylescale * colorr * 4.0f;
597 nl->light[1] = sl->light[1] * stylescale * colorg * 4.0f;
598 nl->light[2] = sl->light[2] * stylescale * colorb * 4.0f;
599 nl->subtract = sl->subtract;
600 nl->offset = sl->distbias;
603 if (!r_shadow_realtime_dlight.integer)
605 for (i = 0;i < r_numdlights;i++)
608 VectorCopy(rd->origin, v);
609 if (v[0] < ent->mins[0]) v[0] = ent->mins[0];if (v[0] > ent->maxs[0]) v[0] = ent->maxs[0];
610 if (v[1] < ent->mins[1]) v[1] = ent->mins[1];if (v[1] > ent->maxs[1]) v[1] = ent->maxs[1];
611 if (v[2] < ent->mins[2]) v[2] = ent->mins[2];if (v[2] > ent->maxs[2]) v[2] = ent->maxs[2];
612 VectorSubtract (v, rd->origin, v);
613 if (DotProduct(v, v) < rd->cullradius2)
615 if (CL_TraceLine(ent->origin, rd->origin, NULL, NULL, 0, false, NULL) != 1)
617 VectorSubtract (ent->origin, rd->origin, v);
618 f = ((1.0f / (DotProduct(v, v) + LIGHTOFFSET)) - rd->subtract);
619 VectorScale(rd->light, f, ambientcolor);
620 intensity = DotProduct(ambientcolor, ambientcolor);
623 if (nearlights < maxnearlights)
627 for (j = 0;j < maxnearlights;j++)
629 if (nearlight[j].intensity < intensity)
631 if (nearlight[j].intensity > 0)
632 VectorAdd(ambient4f, nearlight[j].ambientlight, ambient4f);
637 if (j >= maxnearlights)
639 // this light is less significant than all others,
642 VectorAdd(ambient4f, ambientcolor, ambient4f);
647 nl->intensity = intensity;
648 // transform the light into the model's coordinate system
650 VectorCopy(rd->origin, nl->origin);
653 Matrix4x4_Transform(&ent->inversematrix, rd->origin, nl->origin);
655 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"
656 , rd - r_dlight, ent->model->name
657 , rd->origin[0], rd->origin[1], rd->origin[2]
658 , nl->origin[0], nl->origin[1], nl->origin[2]
659 , ent->inversematrix.m[0][0], ent->inversematrix.m[0][1], ent->inversematrix.m[0][2], ent->inversematrix.m[0][3]
660 , ent->inversematrix.m[1][0], ent->inversematrix.m[1][1], ent->inversematrix.m[1][2], ent->inversematrix.m[1][3]
661 , ent->inversematrix.m[2][0], ent->inversematrix.m[2][1], ent->inversematrix.m[2][2], ent->inversematrix.m[2][3]
662 , ent->inversematrix.m[3][0], ent->inversematrix.m[3][1], ent->inversematrix.m[3][2], ent->inversematrix.m[3][3]);
665 // integrate mscale into falloff, for maximum speed
666 nl->falloff = mscale;
667 VectorCopy(ambientcolor, nl->ambientlight);
668 nl->light[0] = rd->light[0] * colorr * 4.0f;
669 nl->light[1] = rd->light[1] * colorg * 4.0f;
670 nl->light[2] = rd->light[2] * colorb * 4.0f;
671 nl->subtract = rd->subtract;
672 nl->offset = LIGHTOFFSET;
677 ambient4f[0] *= colorr;
678 ambient4f[1] *= colorg;
679 ambient4f[2] *= colorb;
680 ambient4f[3] = colora;
681 diffusecolor[0] *= colorr;
682 diffusecolor[1] *= colorg;
683 diffusecolor[2] *= colorb;
684 return nearlights != 0 || DotProduct(diffusecolor, diffusecolor) > 0;
687 void R_LightModel_CalcVertexColors(const float *ambientcolor4f, const float *diffusecolor, const float *diffusenormal, int numverts, const float *vertex3f, const float *normal3f, float *color4f)
689 int i, j, usediffuse;
690 float color[4], v[3], dot, dist2, f, dnormal[3];
692 usediffuse = DotProduct(diffusecolor, diffusecolor) > 0;
693 VectorCopy(diffusenormal, dnormal);
695 VectorNormalize(dnormal);
696 // directional shading code here
697 for (i = 0;i < numverts;i++, vertex3f += 3, normal3f += 3, color4f += 4)
699 VectorCopy4(ambientcolor4f, color);
701 // silly directional diffuse shading
704 dot = DotProduct(normal3f, dnormal);
706 VectorMA(color, dot, diffusecolor, color);
709 // pretty good lighting
710 for (j = 0, nl = &nearlight[0];j < nearlights;j++, nl++)
712 VectorSubtract(vertex3f, nl->origin, v);
713 // first eliminate negative lighting (back side)
714 dot = DotProduct(normal3f, v);
717 // we'll need this again later to normalize the dotproduct
718 dist2 = DotProduct(v,v);
719 // do the distance attenuation math
720 f = (1.0f / (dist2 * nl->falloff + nl->offset)) - nl->subtract;
723 // we must divide dot by sqrt(dist2) to compensate for
724 // the fact we did not normalize v before doing the
725 // dotproduct, the result is in the range 0 to 1 (we
726 // eliminated negative numbers already)
727 f *= dot / sqrt(dist2);
728 // blend in the lighting
729 VectorMA(color, f, nl->light, color);
733 VectorCopy4(color, color4f);
737 void R_UpdateEntLights(entity_render_t *ent)
742 if (r_shadow_realtime_dlight.integer || gl_flashblend.integer)
744 VectorSubtract(ent->origin, ent->entlightsorigin, v);
745 if (ent->entlightsframe != (r_framecount - 1) || (realtime > ent->entlightstime && DotProduct(v,v) >= 1.0f))
747 ent->entlightstime = realtime + 0.1;
748 VectorCopy(ent->origin, ent->entlightsorigin);
749 ent->numentlights = 0;
751 for (i = 0, sl = cl.worldmodel->brushq1.lights;i < cl.worldmodel->brushq1.numlights && ent->numentlights < MAXENTLIGHTS;i++, sl++)
752 if (CL_TraceLine(ent->origin, sl->origin, NULL, NULL, 0, false, NULL) == 1)
753 ent->entlights[ent->numentlights++] = i;
755 ent->entlightsframe = r_framecount;