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_coronas = {CVAR_SAVE, "r_coronas", "1"};
28 cvar_t gl_flashblend = {CVAR_SAVE, "gl_flashblend", "0"};
30 static rtexture_t *lightcorona;
31 static rtexturepool_t *lighttexturepool;
33 void r_light_start(void)
37 unsigned char pixels[32][32][4];
38 lighttexturepool = R_AllocTexturePool();
39 for (y = 0;y < 32;y++)
41 dy = (y - 15.5f) * (1.0f / 16.0f);
42 for (x = 0;x < 32;x++)
44 dx = (x - 15.5f) * (1.0f / 16.0f);
45 a = ((1.0f / (dx * dx + dy * dy + 0.2f)) - (1.0f / (1.0f + 0.2))) * 32.0f / (1.0f / (1.0f + 0.2));
50 pixels[y][x][3] = 255;
53 lightcorona = R_LoadTexture2D(lighttexturepool, "lightcorona", 32, 32, &pixels[0][0][0], TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
56 void r_light_shutdown(void)
58 lighttexturepool = NULL;
62 void r_light_newmap(void)
65 for (i = 0;i < 256;i++)
66 r_refdef.lightstylevalue[i] = 264; // normal light value
69 void R_Light_Init(void)
71 Cvar_RegisterVariable(&r_modellights);
72 Cvar_RegisterVariable(&r_coronas);
73 Cvar_RegisterVariable(&gl_flashblend);
74 R_RegisterModule("R_Light", r_light_start, r_light_shutdown, r_light_newmap);
77 void R_DrawCoronas(void)
80 float cscale, scale, viewdist, dist;
82 if (r_coronas.value < 0.01)
84 R_Mesh_Matrix(&r_identitymatrix);
85 viewdist = DotProduct(r_vieworigin, r_viewforward);
86 flag = r_rtworld ? LIGHTFLAG_REALTIMEMODE : LIGHTFLAG_NORMALMODE;
87 for (lnum = 0, light = r_shadow_worldlightchain;light;light = light->next, lnum++)
89 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)
91 cscale = light->rtlight.corona * r_coronas.value * 0.25f;
92 scale = light->rtlight.radius * light->rtlight.coronasizescale;
93 R_DrawSprite(GL_ONE, GL_ONE, lightcorona, NULL, 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);
96 for (i = 0;i < r_refdef.numlights;i++)
98 light = r_refdef.lights[i];
99 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)
101 cscale = light->corona * r_coronas.value * 0.25f;
102 scale = light->rtlight.radius * light->rtlight.coronasizescale;
103 if (gl_flashblend.integer)
108 R_DrawSprite(GL_ONE, GL_ONE, lightcorona, NULL, true, light->origin, r_viewright, r_viewup, scale, -scale, -scale, scale, light->color[0] * cscale, light->color[1] * cscale, light->color[2] * cscale, 1);
114 =============================================================================
118 =============================================================================
121 void R_CompleteLightPoint(vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal, const vec3_t p, int dynamic)
123 VectorClear(diffusecolor);
124 VectorClear(diffusenormal);
126 if (!r_fullbright.integer && r_refdef.worldmodel && r_refdef.worldmodel->brush.LightPoint)
128 ambientcolor[0] = ambientcolor[1] = ambientcolor[2] = r_ambient.value * (2.0f / 128.0f);
129 r_refdef.worldmodel->brush.LightPoint(r_refdef.worldmodel, p, ambientcolor, diffusecolor, diffusenormal);
132 VectorSet(ambientcolor, 1, 1, 1);
134 // FIXME: this .lights related stuff needs to be ported into the Mod_Q1BSP code
135 if (r_refdef.worldmodel->brushq1.numlights)
141 for (i = 0;i < r_refdef.worldmodel->brushq1.numlights;i++)
143 sl = r_refdef.worldmodel->brushq1.lights + i;
144 if (r_refdef.lightstylevalue[sl->style] > 0)
146 VectorSubtract (p, sl->origin, v);
147 f = ((1.0f / (DotProduct(v, v) * sl->falloff + sl->distbias)) - sl->subtract);
148 if (f > 0 && CL_TraceBox(p, vec3_origin, vec3_origin, sl->origin, false, NULL, SUPERCONTENTS_SOLID, false).fraction == 1)
150 f *= r_refdef.lightstylevalue[sl->style] * (1.0f / 65536.0f);
152 VectorMA(ambientcolor, f, sl->light, ambientcolor);
163 // FIXME: this really should handle dlights as diffusecolor/diffusenormal somehow
164 // FIXME: this should be updated to match rtlight falloff!
165 for (i = 0;i < r_refdef.numlights;i++)
167 light = r_refdef.lights[i];
168 VectorSubtract(p, light->origin, v);
169 f = DotProduct(v, v);
170 if (f < light->rtlight.lightmap_cullradius2 && CL_TraceBox(p, vec3_origin, vec3_origin, light->origin, false, NULL, SUPERCONTENTS_SOLID, false).fraction == 1)
172 f = (1.0f / (f + LIGHTOFFSET)) - light->rtlight.lightmap_subtract;
174 VectorMA(ambientcolor, f, light->rtlight.lightmap_light, ambientcolor);
180 typedef struct nearlight_s
185 // how much this light would contribute to ambient if replaced
190 // used for choosing only the brightest lights
195 static int nearlights;
196 static nearlight_t nearlight[MAX_DLIGHTS];
198 int R_LightModel(float *ambient4f, float *diffusecolor, float *diffusenormal, const entity_render_t *ent, float colorr, float colorg, float colorb, float colora, int worldcoords)
200 int i, j, maxnearlights;
201 float v[3], f, mscale, stylescale, intensity, ambientcolor[3], tempdiffusenormal[3];
207 maxnearlights = r_modellights.integer;
208 ambient4f[0] = ambient4f[1] = ambient4f[2] = r_ambient.value * (2.0f / 128.0f);
209 VectorClear(diffusecolor);
210 VectorClear(diffusenormal);
211 if (!(ent->flags & RENDER_LIGHT))
214 VectorSet(ambient4f, 1, 1, 1);
217 else if (r_lightmapintensity <= 0 && !(ent->flags & RENDER_TRANSPARENT))
221 if (r_refdef.worldmodel && r_refdef.worldmodel->brush.LightPoint)
223 r_refdef.worldmodel->brush.LightPoint(r_refdef.worldmodel, ent->origin, ambient4f, diffusecolor, tempdiffusenormal);
224 Matrix4x4_Transform3x3(&ent->inversematrix, tempdiffusenormal, diffusenormal);
225 VectorNormalize(diffusenormal);
228 VectorSet(ambient4f, 1, 1, 1);
231 // scale of the model's coordinate space, to alter light attenuation to match
232 // make the mscale squared so it can scale the squared distance results
233 mscale = ent->scale * ent->scale;
234 // FIXME: no support for .lights on non-Q1BSP?
236 for (i = 0;i < ent->numentlights;i++)
238 sl = r_refdef.worldmodel->brushq1.lights + ent->entlights[i];
239 stylescale = r_refdef.lightstylevalue[sl->style] * (1.0f / 65536.0f);
240 VectorSubtract (ent->origin, sl->origin, v);
241 f = ((1.0f / (DotProduct(v, v) * sl->falloff + sl->distbias)) - sl->subtract) * stylescale;
242 VectorScale(sl->light, f, ambientcolor);
243 intensity = DotProduct(ambientcolor, ambientcolor);
246 if (nearlights < maxnearlights)
250 for (j = 0;j < maxnearlights;j++)
252 if (nearlight[j].intensity < intensity)
254 if (nearlight[j].intensity > 0)
255 VectorAdd(ambient4f, nearlight[j].ambientlight, ambient4f);
260 if (j >= maxnearlights)
262 // this light is less significant than all others,
265 VectorAdd(ambient4f, ambientcolor, ambient4f);
270 nl->intensity = intensity;
271 // transform the light into the model's coordinate system
273 VectorCopy(sl->origin, nl->origin);
275 Matrix4x4_Transform(&ent->inversematrix, sl->origin, nl->origin);
276 // integrate mscale into falloff, for maximum speed
277 nl->falloff = sl->falloff * mscale;
278 VectorCopy(ambientcolor, nl->ambientlight);
279 nl->light[0] = sl->light[0] * stylescale * colorr * 4.0f;
280 nl->light[1] = sl->light[1] * stylescale * colorg * 4.0f;
281 nl->light[2] = sl->light[2] * stylescale * colorb * 4.0f;
282 nl->subtract = sl->subtract;
283 nl->offset = sl->distbias;
286 if (ent->flags & RENDER_TRANSPARENT)
288 // FIXME: this dlighting doesn't look like rtlights
289 for (i = 0;i < r_refdef.numlights;i++)
291 light = r_refdef.lights[i];
292 VectorCopy(light->origin, v);
293 if (v[0] < ent->mins[0]) v[0] = ent->mins[0];if (v[0] > ent->maxs[0]) v[0] = ent->maxs[0];
294 if (v[1] < ent->mins[1]) v[1] = ent->mins[1];if (v[1] > ent->maxs[1]) v[1] = ent->maxs[1];
295 if (v[2] < ent->mins[2]) v[2] = ent->mins[2];if (v[2] > ent->maxs[2]) v[2] = ent->maxs[2];
296 VectorSubtract (v, light->origin, v);
297 if (DotProduct(v, v) < light->rtlight.lightmap_cullradius2)
299 if (CL_TraceBox(ent->origin, vec3_origin, vec3_origin, light->origin, false, NULL, SUPERCONTENTS_SOLID, false).fraction != 1)
301 VectorSubtract (ent->origin, light->origin, v);
302 f = ((1.0f / (DotProduct(v, v) + LIGHTOFFSET)) - light->rtlight.lightmap_subtract);
303 VectorScale(light->rtlight.lightmap_light, f, ambientcolor);
304 intensity = DotProduct(ambientcolor, ambientcolor);
307 if (nearlights < maxnearlights)
311 for (j = 0;j < maxnearlights;j++)
313 if (nearlight[j].intensity < intensity)
315 if (nearlight[j].intensity > 0)
316 VectorAdd(ambient4f, nearlight[j].ambientlight, ambient4f);
321 if (j >= maxnearlights)
323 // this light is less significant than all others,
326 VectorAdd(ambient4f, ambientcolor, ambient4f);
331 nl->intensity = intensity;
332 // transform the light into the model's coordinate system
334 VectorCopy(light->origin, nl->origin);
337 Matrix4x4_Transform(&ent->inversematrix, light->origin, nl->origin);
339 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"
340 , rd - cl_dlights, ent->model->name
341 , light->origin[0], light->origin[1], light->origin[2]
342 , nl->origin[0], nl->origin[1], nl->origin[2]
343 , ent->inversematrix.m[0][0], ent->inversematrix.m[0][1], ent->inversematrix.m[0][2], ent->inversematrix.m[0][3]
344 , ent->inversematrix.m[1][0], ent->inversematrix.m[1][1], ent->inversematrix.m[1][2], ent->inversematrix.m[1][3]
345 , ent->inversematrix.m[2][0], ent->inversematrix.m[2][1], ent->inversematrix.m[2][2], ent->inversematrix.m[2][3]
346 , ent->inversematrix.m[3][0], ent->inversematrix.m[3][1], ent->inversematrix.m[3][2], ent->inversematrix.m[3][3]);
349 // integrate mscale into falloff, for maximum speed
350 nl->falloff = mscale;
351 VectorCopy(ambientcolor, nl->ambientlight);
352 nl->light[0] = light->rtlight.lightmap_light[0] * colorr * 4.0f;
353 nl->light[1] = light->rtlight.lightmap_light[1] * colorg * 4.0f;
354 nl->light[2] = light->rtlight.lightmap_light[2] * colorb * 4.0f;
355 nl->subtract = light->rtlight.lightmap_subtract;
356 nl->offset = LIGHTOFFSET;
361 ambient4f[0] *= colorr;
362 ambient4f[1] *= colorg;
363 ambient4f[2] *= colorb;
364 ambient4f[3] = colora;
365 diffusecolor[0] *= colorr;
366 diffusecolor[1] *= colorg;
367 diffusecolor[2] *= colorb;
368 return nearlights != 0 || DotProduct(diffusecolor, diffusecolor) > 0;
371 void R_LightModel_CalcVertexColors(const float *ambientcolor4f, const float *diffusecolor, const float *diffusenormal, int numverts, const float *vertex3f, const float *normal3f, float *color4f)
373 int i, j, usediffuse;
374 float color[4], v[3], dot, dist2, f, dnormal[3];
376 usediffuse = DotProduct(diffusecolor, diffusecolor) > 0;
377 // negate the diffuse normal to avoid the need to negate the
378 // dotproduct on each vertex
379 VectorNegate(diffusenormal, dnormal);
381 VectorNormalize(dnormal);
382 // directional shading code here
383 for (i = 0;i < numverts;i++, vertex3f += 3, normal3f += 3, color4f += 4)
385 VectorCopy4(ambientcolor4f, color);
387 // silly directional diffuse shading
390 dot = DotProduct(normal3f, dnormal);
392 VectorMA(color, dot, diffusecolor, color);
395 // pretty good lighting
396 for (j = 0, nl = &nearlight[0];j < nearlights;j++, nl++)
398 VectorSubtract(nl->origin, vertex3f, v);
399 // first eliminate negative lighting (back side)
400 dot = DotProduct(normal3f, v);
403 // we'll need this again later to normalize the dotproduct
404 dist2 = DotProduct(v,v);
405 // do the distance attenuation math
406 f = (1.0f / (dist2 * nl->falloff + nl->offset)) - nl->subtract;
409 // we must divide dot by sqrt(dist2) to compensate for
410 // the fact we did not normalize v before doing the
411 // dotproduct, the result is in the range 0 to 1 (we
412 // eliminated negative numbers already)
413 f *= dot / sqrt(dist2);
414 // blend in the lighting
415 VectorMA(color, f, nl->light, color);
419 VectorCopy4(color, color4f);
423 void R_UpdateEntLights(entity_render_t *ent)
428 if (r_lightmapintensity <= 0 && !(ent->flags & RENDER_TRANSPARENT))
430 VectorSubtract(ent->origin, ent->entlightsorigin, v);
431 if (ent->entlightsframe != (r_framecount - 1) || (realtime > ent->entlightstime && DotProduct(v,v) >= 1.0f))
433 ent->entlightstime = realtime + 0.1;
434 VectorCopy(ent->origin, ent->entlightsorigin);
435 ent->numentlights = 0;
436 if (r_refdef.worldmodel)
437 for (i = 0, sl = r_refdef.worldmodel->brushq1.lights;i < r_refdef.worldmodel->brushq1.numlights && ent->numentlights < MAXENTLIGHTS;i++, sl++)
438 if (CL_TraceBox(ent->origin, vec3_origin, vec3_origin, sl->origin, false, NULL, SUPERCONTENTS_SOLID, false).fraction == 1)
439 ent->entlights[ent->numentlights++] = i;
441 ent->entlightsframe = r_framecount;