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 cvar_t r_modellights = {CVAR_SAVE, "r_modellights", "4"};
27 cvar_t r_vismarklights = {0, "r_vismarklights", "1"};
28 cvar_t r_coronas = {CVAR_SAVE, "r_coronas", "1"};
29 cvar_t gl_flashblend = {CVAR_SAVE, "gl_flashblend", "0"};
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));
51 pixels[y][x][3] = 255;
54 lightcorona = R_LoadTexture2D(lighttexturepool, "lightcorona", 32, 32, &pixels[0][0][0], TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
57 void r_light_shutdown(void)
59 lighttexturepool = NULL;
63 void r_light_newmap(void)
66 for (i = 0;i < 256;i++)
67 r_refdef.lightstylevalue[i] = 264; // normal light value
70 void R_Light_Init(void)
72 Cvar_RegisterVariable(&r_modellights);
73 Cvar_RegisterVariable(&r_vismarklights);
74 Cvar_RegisterVariable(&r_coronas);
75 Cvar_RegisterVariable(&gl_flashblend);
76 R_RegisterModule("R_Light", r_light_start, r_light_shutdown, r_light_newmap);
79 void R_DrawCoronas(void)
82 float cscale, scale, viewdist, dist;
84 if (r_coronas.value < 0.01)
86 R_Mesh_Matrix(&r_identitymatrix);
87 viewdist = DotProduct(r_vieworigin, r_viewforward);
88 flag = r_rtworld ? LIGHTFLAG_REALTIMEMODE : LIGHTFLAG_NORMALMODE;
89 for (lnum = 0, light = r_shadow_worldlightchain;light;light = light->next, lnum++)
91 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_TraceBox(light->rtlight.shadoworigin, vec3_origin, vec3_origin, r_vieworigin, true, NULL, SUPERCONTENTS_SOLID, false).fraction == 1)
93 cscale = light->rtlight.corona * r_coronas.value * 0.25f;
94 scale = light->rtlight.radius * light->rtlight.coronasizescale;
95 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);
98 for (i = 0;i < r_refdef.numlights;i++)
100 light = r_refdef.lights[i];
101 if ((light->flags & flag) && light->corona * r_coronas.value > 0 && (dist = (DotProduct(light->origin, r_viewforward) - viewdist)) >= 24.0f && CL_TraceBox(light->origin, vec3_origin, vec3_origin, r_vieworigin, true, NULL, SUPERCONTENTS_SOLID, false).fraction == 1)
103 cscale = light->corona * r_coronas.value * 0.25f;
104 scale = light->rtlight.radius * light->rtlight.coronasizescale;
105 if (gl_flashblend.integer)
110 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);
116 =============================================================================
120 =============================================================================
123 void R_CompleteLightPoint(vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal, const vec3_t p, int dynamic)
125 VectorClear(diffusecolor);
126 VectorClear(diffusenormal);
128 if (!r_fullbright.integer && r_refdef.worldmodel && r_refdef.worldmodel->brush.LightPoint)
130 ambientcolor[0] = ambientcolor[1] = ambientcolor[2] = r_ambient.value * (2.0f / 128.0f);
131 r_refdef.worldmodel->brush.LightPoint(r_refdef.worldmodel, p, ambientcolor, diffusecolor, diffusenormal);
134 VectorSet(ambientcolor, 1, 1, 1);
136 // FIXME: this .lights related stuff needs to be ported into the Mod_Q1BSP code
137 if (r_refdef.worldmodel->brushq1.numlights)
143 for (i = 0;i < r_refdef.worldmodel->brushq1.numlights;i++)
145 sl = r_refdef.worldmodel->brushq1.lights + i;
146 if (r_refdef.lightstylevalue[sl->style] > 0)
148 VectorSubtract (p, sl->origin, v);
149 f = ((1.0f / (DotProduct(v, v) * sl->falloff + sl->distbias)) - sl->subtract);
150 if (f > 0 && CL_TraceBox(p, vec3_origin, vec3_origin, sl->origin, false, NULL, SUPERCONTENTS_SOLID, false).fraction == 1)
152 f *= r_refdef.lightstylevalue[sl->style] * (1.0f / 65536.0f);
154 VectorMA(ambientcolor, f, sl->light, ambientcolor);
165 // FIXME: this really should handle dlights as diffusecolor/diffusenormal somehow
166 // FIXME: this should be updated to match rtlight falloff!
167 for (i = 0;i < r_refdef.numlights;i++)
169 light = r_refdef.lights[i];
170 VectorSubtract(p, light->origin, v);
171 f = DotProduct(v, v);
172 if (f < light->rtlight.lightmap_cullradius2 && CL_TraceBox(p, vec3_origin, vec3_origin, light->origin, false, NULL, SUPERCONTENTS_SOLID, false).fraction == 1)
174 f = (1.0f / (f + LIGHTOFFSET)) - light->rtlight.lightmap_subtract;
176 VectorMA(ambientcolor, f, light->rtlight.lightmap_light, ambientcolor);
182 typedef struct nearlight_s
187 // how much this light would contribute to ambient if replaced
192 // used for choosing only the brightest lights
197 static int nearlights;
198 static nearlight_t nearlight[MAX_DLIGHTS];
200 int R_LightModel(float *ambient4f, float *diffusecolor, float *diffusenormal, const entity_render_t *ent, float colorr, float colorg, float colorb, float colora, int worldcoords)
202 int i, j, maxnearlights;
203 float v[3], f, mscale, stylescale, intensity, ambientcolor[3], tempdiffusenormal[3];
209 maxnearlights = r_modellights.integer;
210 ambient4f[0] = ambient4f[1] = ambient4f[2] = r_ambient.value * (2.0f / 128.0f);
211 VectorClear(diffusecolor);
212 VectorClear(diffusenormal);
213 if (!(ent->flags & RENDER_LIGHT))
216 VectorSet(ambient4f, 1, 1, 1);
219 else if (r_lightmapintensity <= 0 && !(ent->flags & RENDER_TRANSPARENT))
223 if (r_refdef.worldmodel && r_refdef.worldmodel->brush.LightPoint)
225 r_refdef.worldmodel->brush.LightPoint(r_refdef.worldmodel, ent->origin, ambient4f, diffusecolor, tempdiffusenormal);
226 Matrix4x4_Transform3x3(&ent->inversematrix, tempdiffusenormal, diffusenormal);
227 VectorNormalize(diffusenormal);
230 VectorSet(ambient4f, 1, 1, 1);
233 // scale of the model's coordinate space, to alter light attenuation to match
234 // make the mscale squared so it can scale the squared distance results
235 mscale = ent->scale * ent->scale;
236 // FIXME: no support for .lights on non-Q1BSP?
238 for (i = 0;i < ent->numentlights;i++)
240 sl = r_refdef.worldmodel->brushq1.lights + ent->entlights[i];
241 stylescale = r_refdef.lightstylevalue[sl->style] * (1.0f / 65536.0f);
242 VectorSubtract (ent->origin, sl->origin, v);
243 f = ((1.0f / (DotProduct(v, v) * sl->falloff + sl->distbias)) - sl->subtract) * stylescale;
244 VectorScale(sl->light, f, ambientcolor);
245 intensity = DotProduct(ambientcolor, ambientcolor);
248 if (nearlights < maxnearlights)
252 for (j = 0;j < maxnearlights;j++)
254 if (nearlight[j].intensity < intensity)
256 if (nearlight[j].intensity > 0)
257 VectorAdd(ambient4f, nearlight[j].ambientlight, ambient4f);
262 if (j >= maxnearlights)
264 // this light is less significant than all others,
267 VectorAdd(ambient4f, ambientcolor, ambient4f);
272 nl->intensity = intensity;
273 // transform the light into the model's coordinate system
275 VectorCopy(sl->origin, nl->origin);
277 Matrix4x4_Transform(&ent->inversematrix, sl->origin, nl->origin);
278 // integrate mscale into falloff, for maximum speed
279 nl->falloff = sl->falloff * mscale;
280 VectorCopy(ambientcolor, nl->ambientlight);
281 nl->light[0] = sl->light[0] * stylescale * colorr * 4.0f;
282 nl->light[1] = sl->light[1] * stylescale * colorg * 4.0f;
283 nl->light[2] = sl->light[2] * stylescale * colorb * 4.0f;
284 nl->subtract = sl->subtract;
285 nl->offset = sl->distbias;
288 if (ent->flags & RENDER_TRANSPARENT)
290 // FIXME: this dlighting doesn't look like rtlights
291 for (i = 0;i < r_refdef.numlights;i++)
293 light = r_refdef.lights[i];
294 VectorCopy(light->origin, v);
295 if (v[0] < ent->mins[0]) v[0] = ent->mins[0];if (v[0] > ent->maxs[0]) v[0] = ent->maxs[0];
296 if (v[1] < ent->mins[1]) v[1] = ent->mins[1];if (v[1] > ent->maxs[1]) v[1] = ent->maxs[1];
297 if (v[2] < ent->mins[2]) v[2] = ent->mins[2];if (v[2] > ent->maxs[2]) v[2] = ent->maxs[2];
298 VectorSubtract (v, light->origin, v);
299 if (DotProduct(v, v) < light->rtlight.lightmap_cullradius2)
301 if (CL_TraceBox(ent->origin, vec3_origin, vec3_origin, light->origin, false, NULL, SUPERCONTENTS_SOLID, false).fraction != 1)
303 VectorSubtract (ent->origin, light->origin, v);
304 f = ((1.0f / (DotProduct(v, v) + LIGHTOFFSET)) - light->rtlight.lightmap_subtract);
305 VectorScale(light->rtlight.lightmap_light, f, ambientcolor);
306 intensity = DotProduct(ambientcolor, ambientcolor);
309 if (nearlights < maxnearlights)
313 for (j = 0;j < maxnearlights;j++)
315 if (nearlight[j].intensity < intensity)
317 if (nearlight[j].intensity > 0)
318 VectorAdd(ambient4f, nearlight[j].ambientlight, ambient4f);
323 if (j >= maxnearlights)
325 // this light is less significant than all others,
328 VectorAdd(ambient4f, ambientcolor, ambient4f);
333 nl->intensity = intensity;
334 // transform the light into the model's coordinate system
336 VectorCopy(light->origin, nl->origin);
339 Matrix4x4_Transform(&ent->inversematrix, light->origin, nl->origin);
341 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"
342 , rd - cl_dlights, ent->model->name
343 , light->origin[0], light->origin[1], light->origin[2]
344 , nl->origin[0], nl->origin[1], nl->origin[2]
345 , ent->inversematrix.m[0][0], ent->inversematrix.m[0][1], ent->inversematrix.m[0][2], ent->inversematrix.m[0][3]
346 , ent->inversematrix.m[1][0], ent->inversematrix.m[1][1], ent->inversematrix.m[1][2], ent->inversematrix.m[1][3]
347 , ent->inversematrix.m[2][0], ent->inversematrix.m[2][1], ent->inversematrix.m[2][2], ent->inversematrix.m[2][3]
348 , ent->inversematrix.m[3][0], ent->inversematrix.m[3][1], ent->inversematrix.m[3][2], ent->inversematrix.m[3][3]);
351 // integrate mscale into falloff, for maximum speed
352 nl->falloff = mscale;
353 VectorCopy(ambientcolor, nl->ambientlight);
354 nl->light[0] = light->rtlight.lightmap_light[0] * colorr * 4.0f;
355 nl->light[1] = light->rtlight.lightmap_light[1] * colorg * 4.0f;
356 nl->light[2] = light->rtlight.lightmap_light[2] * colorb * 4.0f;
357 nl->subtract = light->rtlight.lightmap_subtract;
358 nl->offset = LIGHTOFFSET;
363 ambient4f[0] *= colorr;
364 ambient4f[1] *= colorg;
365 ambient4f[2] *= colorb;
366 ambient4f[3] = colora;
367 diffusecolor[0] *= colorr;
368 diffusecolor[1] *= colorg;
369 diffusecolor[2] *= colorb;
370 return nearlights != 0 || DotProduct(diffusecolor, diffusecolor) > 0;
373 void R_LightModel_CalcVertexColors(const float *ambientcolor4f, const float *diffusecolor, const float *diffusenormal, int numverts, const float *vertex3f, const float *normal3f, float *color4f)
375 int i, j, usediffuse;
376 float color[4], v[3], dot, dist2, f, dnormal[3];
378 usediffuse = DotProduct(diffusecolor, diffusecolor) > 0;
379 // negate the diffuse normal to avoid the need to negate the
380 // dotproduct on each vertex
381 VectorNegate(diffusenormal, dnormal);
383 VectorNormalize(dnormal);
384 // directional shading code here
385 for (i = 0;i < numverts;i++, vertex3f += 3, normal3f += 3, color4f += 4)
387 VectorCopy4(ambientcolor4f, color);
389 // silly directional diffuse shading
392 dot = DotProduct(normal3f, dnormal);
394 VectorMA(color, dot, diffusecolor, color);
397 // pretty good lighting
398 for (j = 0, nl = &nearlight[0];j < nearlights;j++, nl++)
400 VectorSubtract(nl->origin, vertex3f, v);
401 // first eliminate negative lighting (back side)
402 dot = DotProduct(normal3f, v);
405 // we'll need this again later to normalize the dotproduct
406 dist2 = DotProduct(v,v);
407 // do the distance attenuation math
408 f = (1.0f / (dist2 * nl->falloff + nl->offset)) - nl->subtract;
411 // we must divide dot by sqrt(dist2) to compensate for
412 // the fact we did not normalize v before doing the
413 // dotproduct, the result is in the range 0 to 1 (we
414 // eliminated negative numbers already)
415 f *= dot / sqrt(dist2);
416 // blend in the lighting
417 VectorMA(color, f, nl->light, color);
421 VectorCopy4(color, color4f);
425 void R_UpdateEntLights(entity_render_t *ent)
430 if (r_lightmapintensity <= 0 && !(ent->flags & RENDER_TRANSPARENT))
432 VectorSubtract(ent->origin, ent->entlightsorigin, v);
433 if (ent->entlightsframe != (r_framecount - 1) || (realtime > ent->entlightstime && DotProduct(v,v) >= 1.0f))
435 ent->entlightstime = realtime + 0.1;
436 VectorCopy(ent->origin, ent->entlightsorigin);
437 ent->numentlights = 0;
438 if (r_refdef.worldmodel)
439 for (i = 0, sl = r_refdef.worldmodel->brushq1.lights;i < r_refdef.worldmodel->brushq1.numlights && ent->numentlights < MAXENTLIGHTS;i++, sl++)
440 if (CL_TraceBox(ent->origin, vec3_origin, vec3_origin, sl->origin, false, NULL, SUPERCONTENTS_SOLID, false).fraction == 1)
441 ent->entlights[ent->numentlights++] = i;
443 ent->entlightsframe = r_framecount;