4b25a3ba6b3034eecdb2f85568005998d32d7630
[xonotic/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_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"};
30
31 static rtexture_t *lightcorona;
32 static rtexturepool_t *lighttexturepool;
33
34 void r_light_start(void)
35 {
36         float dx, dy;
37         int x, y, a;
38         qbyte pixels[32][32][4];
39         lighttexturepool = R_AllocTexturePool();
40         for (y = 0;y < 32;y++)
41         {
42                 dy = (y - 15.5f) * (1.0f / 16.0f);
43                 for (x = 0;x < 32;x++)
44                 {
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));
47                         a = bound(0, a, 255);
48                         pixels[y][x][0] = a;
49                         pixels[y][x][1] = a;
50                         pixels[y][x][2] = a;
51                         pixels[y][x][3] = 255;
52                 }
53         }
54         lightcorona = R_LoadTexture2D(lighttexturepool, "lightcorona", 32, 32, &pixels[0][0][0], TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
55 }
56
57 void r_light_shutdown(void)
58 {
59         lighttexturepool = NULL;
60         lightcorona = NULL;
61 }
62
63 void r_light_newmap(void)
64 {
65         int i;
66         for (i = 0;i < 256;i++)
67                 r_refdef.lightstylevalue[i] = 264;              // normal light value
68 }
69
70 void R_Light_Init(void)
71 {
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);
77 }
78
79 void R_DrawCoronas(void)
80 {
81         int i, lnum, flag;
82         float cscale, scale, viewdist, dist;
83         dlight_t *light;
84         if (r_coronas.value < 0.01)
85                 return;
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++)
90         {
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)
92                 {
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, 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                 }
97         }
98         for (i = 0;i < r_refdef.numlights;i++)
99         {
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)
102                 {
103                         cscale = light->corona * r_coronas.value * 0.25f;
104                         scale = light->rtlight.radius * light->rtlight.coronasizescale;
105                         if (gl_flashblend.integer)
106                         {
107                                 cscale *= 4.0f;
108                                 scale *= 2.0f;
109                         }
110                         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);
111                 }
112         }
113 }
114
115 /*
116 =============================================================================
117
118 LIGHT SAMPLING
119
120 =============================================================================
121 */
122
123 void R_CompleteLightPoint(vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal, const vec3_t p, int dynamic)
124 {
125         VectorClear(diffusecolor);
126         VectorClear(diffusenormal);
127
128         if (!r_fullbright.integer && r_refdef.worldmodel && r_refdef.worldmodel->brush.LightPoint)
129         {
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);
132         }
133         else
134                 VectorSet(ambientcolor, 1, 1, 1);
135
136         // FIXME: this .lights related stuff needs to be ported into the Mod_Q1BSP code
137         if (r_refdef.worldmodel->brushq1.numlights)
138         {
139                 int i;
140                 vec3_t v;
141                 float f;
142                 mlight_t *sl;
143                 for (i = 0;i < r_refdef.worldmodel->brushq1.numlights;i++)
144                 {
145                         sl = r_refdef.worldmodel->brushq1.lights + i;
146                         if (r_refdef.lightstylevalue[sl->style] > 0)
147                         {
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)
151                                 {
152                                         f *= r_refdef.lightstylevalue[sl->style] * (1.0f / 65536.0f);
153                                         if (f > 0)
154                                                 VectorMA(ambientcolor, f, sl->light, ambientcolor);
155                                 }
156                         }
157                 }
158         }
159
160         if (dynamic)
161         {
162                 int i;
163                 float f, v[3];
164                 dlight_t *light;
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++)
168                 {
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)
173                         {
174                                 f = (1.0f / (f + LIGHTOFFSET)) - light->rtlight.lightmap_subtract;
175                                 if (f > 0)
176                                         VectorMA(ambientcolor, f, light->rtlight.lightmap_light, ambientcolor);
177                         }
178                 }
179         }
180 }
181
182 typedef struct nearlight_s
183 {
184         vec3_t origin;
185         //vec_t cullradius2;
186         vec3_t light;
187         // how much this light would contribute to ambient if replaced
188         vec3_t ambientlight;
189         vec_t subtract;
190         vec_t falloff;
191         vec_t offset;
192         // used for choosing only the brightest lights
193         vec_t intensity;
194 }
195 nearlight_t;
196
197 static int nearlights;
198 static nearlight_t nearlight[MAX_DLIGHTS];
199
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)
201 {
202         int i, j, maxnearlights;
203         float v[3], f, mscale, stylescale, intensity, ambientcolor[3], tempdiffusenormal[3];
204         nearlight_t *nl;
205         mlight_t *sl;
206         dlight_t *light;
207
208         nearlights = 0;
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))
214         {
215                 // highly rare
216                 VectorSet(ambient4f, 1, 1, 1);
217                 maxnearlights = 0;
218         }
219         else if (r_lightmapintensity <= 0 && !(ent->flags & RENDER_TRANSPARENT))
220                 maxnearlights = 0;
221         else
222         {
223                 if (r_refdef.worldmodel && r_refdef.worldmodel->brush.LightPoint)
224                 {
225                         r_refdef.worldmodel->brush.LightPoint(r_refdef.worldmodel, ent->origin, ambient4f, diffusecolor, tempdiffusenormal);
226                         Matrix4x4_Transform3x3(&ent->inversematrix, tempdiffusenormal, diffusenormal);
227                         VectorNormalize(diffusenormal);
228                 }
229                 else
230                         VectorSet(ambient4f, 1, 1, 1);
231         }
232
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?
237         nl = &nearlight[0];
238         for (i = 0;i < ent->numentlights;i++)
239         {
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);
246                 if (f < 0)
247                         intensity *= -1.0f;
248                 if (nearlights < maxnearlights)
249                         j = nearlights++;
250                 else
251                 {
252                         for (j = 0;j < maxnearlights;j++)
253                         {
254                                 if (nearlight[j].intensity < intensity)
255                                 {
256                                         if (nearlight[j].intensity > 0)
257                                                 VectorAdd(ambient4f, nearlight[j].ambientlight, ambient4f);
258                                         break;
259                                 }
260                         }
261                 }
262                 if (j >= maxnearlights)
263                 {
264                         // this light is less significant than all others,
265                         // add it to ambient
266                         if (intensity > 0)
267                                 VectorAdd(ambient4f, ambientcolor, ambient4f);
268                 }
269                 else
270                 {
271                         nl = nearlight + j;
272                         nl->intensity = intensity;
273                         // transform the light into the model's coordinate system
274                         if (worldcoords)
275                                 VectorCopy(sl->origin, nl->origin);
276                         else
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;
286                 }
287         }
288         if (ent->flags & RENDER_TRANSPARENT)
289         {
290                 // FIXME: this dlighting doesn't look like rtlights
291                 for (i = 0;i < r_refdef.numlights;i++)
292                 {
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)
300                         {
301                                 if (CL_TraceBox(ent->origin, vec3_origin, vec3_origin, light->origin, false, NULL, SUPERCONTENTS_SOLID, false).fraction != 1)
302                                         continue;
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);
307                                 if (f < 0)
308                                         intensity *= -1.0f;
309                                 if (nearlights < maxnearlights)
310                                         j = nearlights++;
311                                 else
312                                 {
313                                         for (j = 0;j < maxnearlights;j++)
314                                         {
315                                                 if (nearlight[j].intensity < intensity)
316                                                 {
317                                                         if (nearlight[j].intensity > 0)
318                                                                 VectorAdd(ambient4f, nearlight[j].ambientlight, ambient4f);
319                                                         break;
320                                                 }
321                                         }
322                                 }
323                                 if (j >= maxnearlights)
324                                 {
325                                         // this light is less significant than all others,
326                                         // add it to ambient
327                                         if (intensity > 0)
328                                                 VectorAdd(ambient4f, ambientcolor, ambient4f);
329                                 }
330                                 else
331                                 {
332                                         nl = nearlight + j;
333                                         nl->intensity = intensity;
334                                         // transform the light into the model's coordinate system
335                                         if (worldcoords)
336                                                 VectorCopy(light->origin, nl->origin);
337                                         else
338                                         {
339                                                 Matrix4x4_Transform(&ent->inversematrix, light->origin, nl->origin);
340                                                 /*
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]);
349                                                 */
350                                         }
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;
359                                 }
360                         }
361                 }
362         }
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;
371 }
372
373 void R_LightModel_CalcVertexColors(const float *ambientcolor4f, const float *diffusecolor, const float *diffusenormal, int numverts, const float *vertex3f, const float *normal3f, float *color4f)
374 {
375         int i, j, usediffuse;
376         float color[4], v[3], dot, dist2, f, dnormal[3];
377         nearlight_t *nl;
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);
382         if (usediffuse)
383                 VectorNormalize(dnormal);
384         // directional shading code here
385         for (i = 0;i < numverts;i++, vertex3f += 3, normal3f += 3, color4f += 4)
386         {
387                 VectorCopy4(ambientcolor4f, color);
388
389                 // silly directional diffuse shading
390                 if (usediffuse)
391                 {
392                         dot = DotProduct(normal3f, dnormal);
393                         if (dot > 0)
394                                 VectorMA(color, dot, diffusecolor, color);
395                 }
396
397                 // pretty good lighting
398                 for (j = 0, nl = &nearlight[0];j < nearlights;j++, nl++)
399                 {
400                         VectorSubtract(nl->origin, vertex3f, v);
401                         // first eliminate negative lighting (back side)
402                         dot = DotProduct(normal3f, v);
403                         if (dot > 0)
404                         {
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;
409                                 if (f > 0)
410                                 {
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);
418                                 }
419                         }
420                 }
421                 VectorCopy4(color, color4f);
422         }
423 }
424
425 void R_UpdateEntLights(entity_render_t *ent)
426 {
427         int i;
428         const mlight_t *sl;
429         vec3_t v;
430         if (r_lightmapintensity <= 0 && !(ent->flags & RENDER_TRANSPARENT))
431                 return;
432         VectorSubtract(ent->origin, ent->entlightsorigin, v);
433         if (ent->entlightsframe != (r_framecount - 1) || (realtime > ent->entlightstime && DotProduct(v,v) >= 1.0f))
434         {
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;
442         }
443         ent->entlightsframe = r_framecount;
444 }
445