]> icculus.org git repositories - divverent/darkplaces.git/blob - r_light.c
got rid of unused r_vismarklights cvar
[divverent/darkplaces.git] / r_light.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
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.
8
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.
12
13 See the GNU General Public License for more details.
14
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.
18
19 */
20 // r_light.c
21
22 #include "quakedef.h"
23 #include "cl_collision.h"
24 #include "r_shadow.h"
25
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"};
29
30 static rtexture_t *lightcorona;
31 static rtexturepool_t *lighttexturepool;
32
33 void r_light_start(void)
34 {
35         float dx, dy;
36         int x, y, a;
37         unsigned char pixels[32][32][4];
38         lighttexturepool = R_AllocTexturePool();
39         for (y = 0;y < 32;y++)
40         {
41                 dy = (y - 15.5f) * (1.0f / 16.0f);
42                 for (x = 0;x < 32;x++)
43                 {
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));
46                         a = bound(0, a, 255);
47                         pixels[y][x][0] = a;
48                         pixels[y][x][1] = a;
49                         pixels[y][x][2] = a;
50                         pixels[y][x][3] = 255;
51                 }
52         }
53         lightcorona = R_LoadTexture2D(lighttexturepool, "lightcorona", 32, 32, &pixels[0][0][0], TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
54 }
55
56 void r_light_shutdown(void)
57 {
58         lighttexturepool = NULL;
59         lightcorona = NULL;
60 }
61
62 void r_light_newmap(void)
63 {
64         int i;
65         for (i = 0;i < 256;i++)
66                 r_refdef.lightstylevalue[i] = 264;              // normal light value
67 }
68
69 void R_Light_Init(void)
70 {
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);
75 }
76
77 void R_DrawCoronas(void)
78 {
79         int i, lnum, flag;
80         float cscale, scale, viewdist, dist;
81         dlight_t *light;
82         if (r_coronas.value < 0.01)
83                 return;
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++)
88         {
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)
90                 {
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);
94                 }
95         }
96         for (i = 0;i < r_refdef.numlights;i++)
97         {
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)
100                 {
101                         cscale = light->corona * r_coronas.value * 0.25f;
102                         scale = light->rtlight.radius * light->rtlight.coronasizescale;
103                         if (gl_flashblend.integer)
104                         {
105                                 cscale *= 4.0f;
106                                 scale *= 2.0f;
107                         }
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);
109                 }
110         }
111 }
112
113 /*
114 =============================================================================
115
116 LIGHT SAMPLING
117
118 =============================================================================
119 */
120
121 void R_CompleteLightPoint(vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal, const vec3_t p, int dynamic)
122 {
123         VectorClear(diffusecolor);
124         VectorClear(diffusenormal);
125
126         if (!r_fullbright.integer && r_refdef.worldmodel && r_refdef.worldmodel->brush.LightPoint)
127         {
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);
130         }
131         else
132                 VectorSet(ambientcolor, 1, 1, 1);
133
134         // FIXME: this .lights related stuff needs to be ported into the Mod_Q1BSP code
135         if (r_refdef.worldmodel->brushq1.numlights)
136         {
137                 int i;
138                 vec3_t v;
139                 float f;
140                 mlight_t *sl;
141                 for (i = 0;i < r_refdef.worldmodel->brushq1.numlights;i++)
142                 {
143                         sl = r_refdef.worldmodel->brushq1.lights + i;
144                         if (r_refdef.lightstylevalue[sl->style] > 0)
145                         {
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)
149                                 {
150                                         f *= r_refdef.lightstylevalue[sl->style] * (1.0f / 65536.0f);
151                                         if (f > 0)
152                                                 VectorMA(ambientcolor, f, sl->light, ambientcolor);
153                                 }
154                         }
155                 }
156         }
157
158         if (dynamic)
159         {
160                 int i;
161                 float f, v[3];
162                 dlight_t *light;
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++)
166                 {
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)
171                         {
172                                 f = (1.0f / (f + LIGHTOFFSET)) - light->rtlight.lightmap_subtract;
173                                 if (f > 0)
174                                         VectorMA(ambientcolor, f, light->rtlight.lightmap_light, ambientcolor);
175                         }
176                 }
177         }
178 }
179
180 typedef struct nearlight_s
181 {
182         vec3_t origin;
183         //vec_t cullradius2;
184         vec3_t light;
185         // how much this light would contribute to ambient if replaced
186         vec3_t ambientlight;
187         vec_t subtract;
188         vec_t falloff;
189         vec_t offset;
190         // used for choosing only the brightest lights
191         vec_t intensity;
192 }
193 nearlight_t;
194
195 static int nearlights;
196 static nearlight_t nearlight[MAX_DLIGHTS];
197
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)
199 {
200         int i, j, maxnearlights;
201         float v[3], f, mscale, stylescale, intensity, ambientcolor[3], tempdiffusenormal[3];
202         nearlight_t *nl;
203         mlight_t *sl;
204         dlight_t *light;
205
206         nearlights = 0;
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))
212         {
213                 // highly rare
214                 VectorSet(ambient4f, 1, 1, 1);
215                 maxnearlights = 0;
216         }
217         else if (r_lightmapintensity <= 0 && !(ent->flags & RENDER_TRANSPARENT))
218                 maxnearlights = 0;
219         else
220         {
221                 if (r_refdef.worldmodel && r_refdef.worldmodel->brush.LightPoint)
222                 {
223                         r_refdef.worldmodel->brush.LightPoint(r_refdef.worldmodel, ent->origin, ambient4f, diffusecolor, tempdiffusenormal);
224                         Matrix4x4_Transform3x3(&ent->inversematrix, tempdiffusenormal, diffusenormal);
225                         VectorNormalize(diffusenormal);
226                 }
227                 else
228                         VectorSet(ambient4f, 1, 1, 1);
229         }
230
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?
235         nl = &nearlight[0];
236         for (i = 0;i < ent->numentlights;i++)
237         {
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);
244                 if (f < 0)
245                         intensity *= -1.0f;
246                 if (nearlights < maxnearlights)
247                         j = nearlights++;
248                 else
249                 {
250                         for (j = 0;j < maxnearlights;j++)
251                         {
252                                 if (nearlight[j].intensity < intensity)
253                                 {
254                                         if (nearlight[j].intensity > 0)
255                                                 VectorAdd(ambient4f, nearlight[j].ambientlight, ambient4f);
256                                         break;
257                                 }
258                         }
259                 }
260                 if (j >= maxnearlights)
261                 {
262                         // this light is less significant than all others,
263                         // add it to ambient
264                         if (intensity > 0)
265                                 VectorAdd(ambient4f, ambientcolor, ambient4f);
266                 }
267                 else
268                 {
269                         nl = nearlight + j;
270                         nl->intensity = intensity;
271                         // transform the light into the model's coordinate system
272                         if (worldcoords)
273                                 VectorCopy(sl->origin, nl->origin);
274                         else
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;
284                 }
285         }
286         if (ent->flags & RENDER_TRANSPARENT)
287         {
288                 // FIXME: this dlighting doesn't look like rtlights
289                 for (i = 0;i < r_refdef.numlights;i++)
290                 {
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)
298                         {
299                                 if (CL_TraceBox(ent->origin, vec3_origin, vec3_origin, light->origin, false, NULL, SUPERCONTENTS_SOLID, false).fraction != 1)
300                                         continue;
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);
305                                 if (f < 0)
306                                         intensity *= -1.0f;
307                                 if (nearlights < maxnearlights)
308                                         j = nearlights++;
309                                 else
310                                 {
311                                         for (j = 0;j < maxnearlights;j++)
312                                         {
313                                                 if (nearlight[j].intensity < intensity)
314                                                 {
315                                                         if (nearlight[j].intensity > 0)
316                                                                 VectorAdd(ambient4f, nearlight[j].ambientlight, ambient4f);
317                                                         break;
318                                                 }
319                                         }
320                                 }
321                                 if (j >= maxnearlights)
322                                 {
323                                         // this light is less significant than all others,
324                                         // add it to ambient
325                                         if (intensity > 0)
326                                                 VectorAdd(ambient4f, ambientcolor, ambient4f);
327                                 }
328                                 else
329                                 {
330                                         nl = nearlight + j;
331                                         nl->intensity = intensity;
332                                         // transform the light into the model's coordinate system
333                                         if (worldcoords)
334                                                 VectorCopy(light->origin, nl->origin);
335                                         else
336                                         {
337                                                 Matrix4x4_Transform(&ent->inversematrix, light->origin, nl->origin);
338                                                 /*
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]);
347                                                 */
348                                         }
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;
357                                 }
358                         }
359                 }
360         }
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;
369 }
370
371 void R_LightModel_CalcVertexColors(const float *ambientcolor4f, const float *diffusecolor, const float *diffusenormal, int numverts, const float *vertex3f, const float *normal3f, float *color4f)
372 {
373         int i, j, usediffuse;
374         float color[4], v[3], dot, dist2, f, dnormal[3];
375         nearlight_t *nl;
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);
380         if (usediffuse)
381                 VectorNormalize(dnormal);
382         // directional shading code here
383         for (i = 0;i < numverts;i++, vertex3f += 3, normal3f += 3, color4f += 4)
384         {
385                 VectorCopy4(ambientcolor4f, color);
386
387                 // silly directional diffuse shading
388                 if (usediffuse)
389                 {
390                         dot = DotProduct(normal3f, dnormal);
391                         if (dot > 0)
392                                 VectorMA(color, dot, diffusecolor, color);
393                 }
394
395                 // pretty good lighting
396                 for (j = 0, nl = &nearlight[0];j < nearlights;j++, nl++)
397                 {
398                         VectorSubtract(nl->origin, vertex3f, v);
399                         // first eliminate negative lighting (back side)
400                         dot = DotProduct(normal3f, v);
401                         if (dot > 0)
402                         {
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;
407                                 if (f > 0)
408                                 {
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);
416                                 }
417                         }
418                 }
419                 VectorCopy4(color, color4f);
420         }
421 }
422
423 void R_UpdateEntLights(entity_render_t *ent)
424 {
425         int i;
426         const mlight_t *sl;
427         vec3_t v;
428         if (r_lightmapintensity <= 0 && !(ent->flags & RENDER_TRANSPARENT))
429                 return;
430         VectorSubtract(ent->origin, ent->entlightsorigin, v);
431         if (ent->entlightsframe != (r_framecount - 1) || (realtime > ent->entlightstime && DotProduct(v,v) >= 1.0f))
432         {
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;
440         }
441         ent->entlightsframe = r_framecount;
442 }
443