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 dlight_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_UpdateLights(void)
93 // 'm' is normal light, 'a' is no light, 'z' is double bright
94 i = (int)(cl.time * 10);
95 frac = (cl.time * 10) - i;
96 for (j = 0;j < MAX_LIGHTSTYLES;j++)
98 if (!cl_lightstyle || !cl_lightstyle[j].length)
100 d_lightstylevalue[j] = 256;
103 k = i % cl_lightstyle[j].length;
104 l = (i-1) % cl_lightstyle[j].length;
105 k = cl_lightstyle[j].map[k] - 'a';
106 l = cl_lightstyle[j].map[l] - 'a';
107 d_lightstylevalue[j] = ((k*frac)+(l*(1-frac)))*22;
113 if (!r_dynamic.integer || !cl_dlights)
116 // TODO: optimize to not scan whole cl_dlights array if possible
117 for (i = 0;i < MAX_DLIGHTS;i++)
119 if (cl_dlights[i].radius > 0)
121 R_RTLight_UpdateFromDLight(&cl_dlights[i].rtlight, &cl_dlights[i], false);
122 // FIXME: use pointer instead of copy
123 r_dlight[r_numdlights++] = cl_dlights[i];
124 c_dlights++; // count every dlight in use
129 void R_DrawCoronas(void)
132 float cscale, scale, viewdist, dist;
134 if (r_coronas.value < 0.01)
136 R_Mesh_Matrix(&r_identitymatrix);
137 viewdist = DotProduct(r_vieworigin, r_viewforward);
138 flag = r_rtworld ? LIGHTFLAG_REALTIMEMODE : LIGHTFLAG_NORMALMODE;
139 for (lnum = 0, light = r_shadow_worldlightchain;light;light = light->next, lnum++)
141 if ((light->flags & flag) && light->corona * r_coronas.value > 0 && (r_shadow_debuglight.integer < 0 || r_shadow_debuglight.integer == lnum) && (dist = (DotProduct(light->rtlight.shadoworigin, r_viewforward) - viewdist)) >= 24.0f && CL_TraceLine(light->rtlight.shadoworigin, r_vieworigin, NULL, NULL, true, NULL, SUPERCONTENTS_SOLID) == 1)
143 cscale = light->rtlight.corona * r_coronas.value * 0.25f;
144 scale = light->rtlight.radius * light->rtlight.coronasizescale;
145 R_DrawSprite(GL_ONE, GL_ONE, lightcorona, true, light->rtlight.shadoworigin, r_viewright, r_viewup, scale, -scale, -scale, scale, light->rtlight.color[0] * cscale, light->rtlight.color[1] * cscale, light->rtlight.color[2] * cscale, 1);
148 for (i = 0, light = r_dlight;i < r_numdlights;i++, light++)
150 if ((light->flags & flag) && light->corona * r_coronas.value > 0 && (dist = (DotProduct(light->origin, r_viewforward) - viewdist)) >= 24.0f && CL_TraceLine(light->origin, r_vieworigin, NULL, NULL, true, NULL, SUPERCONTENTS_SOLID) == 1)
152 cscale = light->corona * r_coronas.value * 0.25f;
153 scale = light->rtlight.radius * light->rtlight.coronasizescale;
154 if (gl_flashblend.integer)
159 R_DrawSprite(GL_ONE, GL_ONE, lightcorona, true, light->origin, r_viewright, r_viewup, scale, -scale, -scale, scale, light->color[0] * cscale, light->color[1] * cscale, light->color[2] * cscale, 1);
165 =============================================================================
169 =============================================================================
172 static int lightpvsbytes;
173 static qbyte lightpvs[(MAX_MAP_LEAFS+7)>>3];
180 static void R_RecursiveMarkLights(entity_render_t *ent, vec3_t lightorigin, dlight_t *light, int bit, int bitindex, mnode_t *node, qbyte *pvs, int pvsbits)
186 // for comparisons to minimum acceptable light
187 while(node->contents >= 0)
189 dist = PlaneDiff(lightorigin, node->plane);
190 if (dist > light->rtlight.lightmap_cullradius)
191 node = node->children[0];
194 if (dist >= -light->rtlight.lightmap_cullradius)
195 R_RecursiveMarkLights(ent, lightorigin, light, bit, bitindex, node->children[0], pvs, pvsbits);
196 node = node->children[1];
200 // check if leaf is visible according to pvs
201 leaf = (mleaf_t *)node;
202 i = leaf->clusterindex;
203 if (leaf->nummarksurfaces && (i >= pvsbits || CHECKPVSBIT(pvs, i)))
205 int *surfacepvsframes, d, impacts, impactt;
206 float sdist, maxdist, dist2, impact[3];
209 maxdist = light->rtlight.lightmap_cullradius2;
210 surfacepvsframes = ent->model->brushq1.surfacepvsframes;
211 for (i = 0;i < leaf->nummarksurfaces;i++)
213 if (surfacepvsframes[leaf->firstmarksurface[i]] != ent->model->brushq1.pvsframecount)
215 surf = ent->model->brushq1.surfaces + leaf->firstmarksurface[i];
216 dist = sdist = PlaneDiff(lightorigin, surf->plane);
217 if (surf->flags & SURF_PLANEBACK)
220 if (dist < -0.25f && !(surf->flags & SURF_LIGHTBOTHSIDES))
224 if (dist2 >= maxdist)
227 VectorCopy(lightorigin, impact);
228 if (surf->plane->type >= 3)
229 VectorMA(impact, -sdist, surf->plane->normal, impact);
231 impact[surf->plane->type] -= sdist;
233 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
235 d = bound(0, impacts, surf->extents[0] + 16) - impacts;
240 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
242 d = bound(0, impactt, surf->extents[1] + 16) - impactt;
247 if (surf->dlightframe != r_framecount) // not dynamic until now
249 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;
250 surf->dlightframe = r_framecount;
251 surf->cached_dlight = true;
253 surf->dlightbits[bitindex] |= bit;
258 void R_MarkLights(entity_render_t *ent)
260 int i, bit, bitindex;
263 if (!gl_flashblend.integer && r_dynamic.integer && ent->model && ent->model->brushq1.num_leafs)
265 for (i = 0, light = r_dlight;i < r_numdlights;i++, light++)
269 Matrix4x4_Transform(&ent->inversematrix, light->origin, lightorigin);
271 if (r_vismarklights.integer && ent->model->brush.FatPVS)
272 lightpvsbytes = ent->model->brush.FatPVS(ent->model, lightorigin, 0, lightpvs, sizeof(lightpvs));
273 R_RecursiveMarkLights(ent, lightorigin, light, bit, bitindex, ent->model->brushq1.nodes + ent->model->brushq1.hulls[0].firstclipnode, lightpvs, min(lightpvsbytes * 8, ent->model->brush.num_pvsclusters));
279 =============================================================================
283 =============================================================================
286 void R_CompleteLightPoint(vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal, const vec3_t p, int dynamic, const mleaf_t *leaf)
288 VectorClear(diffusecolor);
289 VectorClear(diffusenormal);
291 if (!r_fullbright.integer && r_refdef.worldmodel && r_refdef.worldmodel->brush.LightPoint)
293 ambientcolor[0] = ambientcolor[1] = ambientcolor[2] = r_ambient.value * (2.0f / 128.0f);
294 r_refdef.worldmodel->brush.LightPoint(r_refdef.worldmodel, p, ambientcolor, diffusecolor, diffusenormal);
297 VectorSet(ambientcolor, 1, 1, 1);
299 // FIXME: this .lights related stuff needs to be ported into the Mod_Q1BSP code
300 if (r_refdef.worldmodel->brushq1.numlights)
306 for (i = 0;i < r_refdef.worldmodel->brushq1.numlights;i++)
308 sl = r_refdef.worldmodel->brushq1.lights + i;
309 if (d_lightstylevalue[sl->style] > 0)
311 VectorSubtract (p, sl->origin, v);
312 f = ((1.0f / (DotProduct(v, v) * sl->falloff + sl->distbias)) - sl->subtract);
313 if (f > 0 && CL_TraceLine(p, sl->origin, NULL, NULL, false, NULL, SUPERCONTENTS_SOLID) == 1)
315 f *= d_lightstylevalue[sl->style] * (1.0f / 65536.0f);
316 VectorMA(ambientcolor, f, sl->light, ambientcolor);
327 // FIXME: this really should handle dlights as diffusecolor/diffusenormal somehow
328 for (i = 0;i < r_numdlights;i++)
330 light = r_dlight + i;
331 VectorSubtract(p, light->origin, v);
332 f = DotProduct(v, v);
333 if (f < light->rtlight.lightmap_cullradius2 && CL_TraceLine(p, light->origin, NULL, NULL, false, NULL, SUPERCONTENTS_SOLID) == 1)
335 f = (1.0f / (f + LIGHTOFFSET)) - light->rtlight.lightmap_subtract;
336 VectorMA(ambientcolor, f, light->rtlight.lightmap_light, ambientcolor);
347 // how much this light would contribute to ambient if replaced
352 // used for choosing only the brightest lights
357 static int nearlights;
358 static nearlight_t nearlight[MAX_DLIGHTS];
360 int R_LightModel(float *ambient4f, float *diffusecolor, float *diffusenormal, const entity_render_t *ent, float colorr, float colorg, float colorb, float colora, int worldcoords)
362 int i, j, maxnearlights;
363 float v[3], f, mscale, stylescale, intensity, ambientcolor[3], tempdiffusenormal[3];
369 maxnearlights = r_modellights.integer;
370 ambient4f[0] = ambient4f[1] = ambient4f[2] = r_ambient.value * (2.0f / 128.0f);
371 VectorClear(diffusecolor);
372 VectorClear(diffusenormal);
373 if (!(ent->flags & RENDER_LIGHT))
376 VectorSet(ambient4f, 1, 1, 1);
379 else if (r_lightmapintensity <= 0 && !(ent->flags & RENDER_TRANSPARENT))
383 if (r_refdef.worldmodel && r_refdef.worldmodel->brush.LightPoint)
385 r_refdef.worldmodel->brush.LightPoint(r_refdef.worldmodel, ent->origin, ambient4f, diffusecolor, tempdiffusenormal);
386 Matrix4x4_Transform3x3(&ent->inversematrix, tempdiffusenormal, diffusenormal);
387 VectorNormalize(diffusenormal);
390 VectorSet(ambient4f, 1, 1, 1);
393 // scale of the model's coordinate space, to alter light attenuation to match
394 // make the mscale squared so it can scale the squared distance results
395 mscale = ent->scale * ent->scale;
396 // FIXME: no support for .lights on non-Q1BSP?
398 for (i = 0;i < ent->numentlights;i++)
400 sl = r_refdef.worldmodel->brushq1.lights + ent->entlights[i];
401 stylescale = d_lightstylevalue[sl->style] * (1.0f / 65536.0f);
402 VectorSubtract (ent->origin, sl->origin, v);
403 f = ((1.0f / (DotProduct(v, v) * sl->falloff + sl->distbias)) - sl->subtract) * stylescale;
404 VectorScale(sl->light, f, ambientcolor);
405 intensity = DotProduct(ambientcolor, ambientcolor);
408 if (nearlights < maxnearlights)
412 for (j = 0;j < maxnearlights;j++)
414 if (nearlight[j].intensity < intensity)
416 if (nearlight[j].intensity > 0)
417 VectorAdd(ambient4f, nearlight[j].ambientlight, ambient4f);
422 if (j >= maxnearlights)
424 // this light is less significant than all others,
427 VectorAdd(ambient4f, ambientcolor, ambient4f);
432 nl->intensity = intensity;
433 // transform the light into the model's coordinate system
435 VectorCopy(sl->origin, nl->origin);
437 Matrix4x4_Transform(&ent->inversematrix, sl->origin, nl->origin);
438 // integrate mscale into falloff, for maximum speed
439 nl->falloff = sl->falloff * mscale;
440 VectorCopy(ambientcolor, nl->ambientlight);
441 nl->light[0] = sl->light[0] * stylescale * colorr * 4.0f;
442 nl->light[1] = sl->light[1] * stylescale * colorg * 4.0f;
443 nl->light[2] = sl->light[2] * stylescale * colorb * 4.0f;
444 nl->subtract = sl->subtract;
445 nl->offset = sl->distbias;
448 if (!r_rtdlight || (ent->flags & RENDER_TRANSPARENT))
450 // FIXME: this dlighting doesn't look like rtlights
451 for (i = 0;i < r_numdlights;i++)
453 light = r_dlight + i;
454 VectorCopy(light->origin, v);
455 if (v[0] < ent->mins[0]) v[0] = ent->mins[0];if (v[0] > ent->maxs[0]) v[0] = ent->maxs[0];
456 if (v[1] < ent->mins[1]) v[1] = ent->mins[1];if (v[1] > ent->maxs[1]) v[1] = ent->maxs[1];
457 if (v[2] < ent->mins[2]) v[2] = ent->mins[2];if (v[2] > ent->maxs[2]) v[2] = ent->maxs[2];
458 VectorSubtract (v, light->origin, v);
459 if (DotProduct(v, v) < light->rtlight.lightmap_cullradius2)
461 if (CL_TraceLine(ent->origin, light->origin, NULL, NULL, false, NULL, SUPERCONTENTS_SOLID) != 1)
463 VectorSubtract (ent->origin, light->origin, v);
464 f = ((1.0f / (DotProduct(v, v) + LIGHTOFFSET)) - light->rtlight.lightmap_subtract);
465 VectorScale(light->rtlight.lightmap_light, f, ambientcolor);
466 intensity = DotProduct(ambientcolor, ambientcolor);
469 if (nearlights < maxnearlights)
473 for (j = 0;j < maxnearlights;j++)
475 if (nearlight[j].intensity < intensity)
477 if (nearlight[j].intensity > 0)
478 VectorAdd(ambient4f, nearlight[j].ambientlight, ambient4f);
483 if (j >= maxnearlights)
485 // this light is less significant than all others,
488 VectorAdd(ambient4f, ambientcolor, ambient4f);
493 nl->intensity = intensity;
494 // transform the light into the model's coordinate system
496 VectorCopy(light->origin, nl->origin);
499 Matrix4x4_Transform(&ent->inversematrix, light->origin, nl->origin);
501 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"
502 , rd - r_dlight, ent->model->name
503 , light->origin[0], light->origin[1], light->origin[2]
504 , nl->origin[0], nl->origin[1], nl->origin[2]
505 , ent->inversematrix.m[0][0], ent->inversematrix.m[0][1], ent->inversematrix.m[0][2], ent->inversematrix.m[0][3]
506 , ent->inversematrix.m[1][0], ent->inversematrix.m[1][1], ent->inversematrix.m[1][2], ent->inversematrix.m[1][3]
507 , ent->inversematrix.m[2][0], ent->inversematrix.m[2][1], ent->inversematrix.m[2][2], ent->inversematrix.m[2][3]
508 , ent->inversematrix.m[3][0], ent->inversematrix.m[3][1], ent->inversematrix.m[3][2], ent->inversematrix.m[3][3]);
511 // integrate mscale into falloff, for maximum speed
512 nl->falloff = mscale;
513 VectorCopy(ambientcolor, nl->ambientlight);
514 nl->light[0] = light->rtlight.lightmap_light[0] * colorr * 4.0f;
515 nl->light[1] = light->rtlight.lightmap_light[1] * colorg * 4.0f;
516 nl->light[2] = light->rtlight.lightmap_light[2] * colorb * 4.0f;
517 nl->subtract = light->rtlight.lightmap_subtract;
518 nl->offset = LIGHTOFFSET;
523 ambient4f[0] *= colorr;
524 ambient4f[1] *= colorg;
525 ambient4f[2] *= colorb;
526 ambient4f[3] = colora;
527 diffusecolor[0] *= colorr;
528 diffusecolor[1] *= colorg;
529 diffusecolor[2] *= colorb;
530 return nearlights != 0 || DotProduct(diffusecolor, diffusecolor) > 0;
533 void R_LightModel_CalcVertexColors(const float *ambientcolor4f, const float *diffusecolor, const float *diffusenormal, int numverts, const float *vertex3f, const float *normal3f, float *color4f)
535 int i, j, usediffuse;
536 float color[4], v[3], dot, dist2, f, dnormal[3];
538 usediffuse = DotProduct(diffusecolor, diffusecolor) > 0;
539 // negate the diffuse normal to avoid the need to negate the
540 // dotproduct on each vertex
541 VectorNegate(diffusenormal, dnormal);
543 VectorNormalize(dnormal);
544 // directional shading code here
545 for (i = 0;i < numverts;i++, vertex3f += 3, normal3f += 3, color4f += 4)
547 VectorCopy4(ambientcolor4f, color);
549 // silly directional diffuse shading
552 dot = DotProduct(normal3f, dnormal);
554 VectorMA(color, dot, diffusecolor, color);
557 // pretty good lighting
558 for (j = 0, nl = &nearlight[0];j < nearlights;j++, nl++)
560 VectorSubtract(nl->origin, vertex3f, v);
561 // first eliminate negative lighting (back side)
562 dot = DotProduct(normal3f, v);
565 // we'll need this again later to normalize the dotproduct
566 dist2 = DotProduct(v,v);
567 // do the distance attenuation math
568 f = (1.0f / (dist2 * nl->falloff + nl->offset)) - nl->subtract;
571 // we must divide dot by sqrt(dist2) to compensate for
572 // the fact we did not normalize v before doing the
573 // dotproduct, the result is in the range 0 to 1 (we
574 // eliminated negative numbers already)
575 f *= dot / sqrt(dist2);
576 // blend in the lighting
577 VectorMA(color, f, nl->light, color);
581 VectorCopy4(color, color4f);
585 void R_UpdateEntLights(entity_render_t *ent)
590 if (r_lightmapintensity <= 0 && !(ent->flags & RENDER_TRANSPARENT))
592 VectorSubtract(ent->origin, ent->entlightsorigin, v);
593 if (ent->entlightsframe != (r_framecount - 1) || (realtime > ent->entlightstime && DotProduct(v,v) >= 1.0f))
595 ent->entlightstime = realtime + 0.1;
596 VectorCopy(ent->origin, ent->entlightsorigin);
597 ent->numentlights = 0;
598 if (r_refdef.worldmodel)
599 for (i = 0, sl = r_refdef.worldmodel->brushq1.lights;i < r_refdef.worldmodel->brushq1.numlights && ent->numentlights < MAXENTLIGHTS;i++, sl++)
600 if (CL_TraceLine(ent->origin, sl->origin, NULL, NULL, false, NULL, SUPERCONTENTS_SOLID) == 1)
601 ent->entlights[ent->numentlights++] = i;
603 ent->entlightsframe = r_framecount;