added explicit casts for agl functions when calling GL_GetProcAddress
[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 dlight_t r_dlight[MAX_DLIGHTS];
27 int r_numdlights = 0;
28
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", "0"};
33
34 static rtexture_t *lightcorona;
35 static rtexturepool_t *lighttexturepool;
36
37 void r_light_start(void)
38 {
39         float dx, dy;
40         int x, y, a;
41         qbyte pixels[32][32][4];
42         lighttexturepool = R_AllocTexturePool();
43         for (y = 0;y < 32;y++)
44         {
45                 dy = (y - 15.5f) * (1.0f / 16.0f);
46                 for (x = 0;x < 32;x++)
47                 {
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));
50                         a = bound(0, a, 255);
51                         pixels[y][x][0] = a;
52                         pixels[y][x][1] = a;
53                         pixels[y][x][2] = a;
54                         pixels[y][x][3] = 255;
55                 }
56         }
57         lightcorona = R_LoadTexture2D(lighttexturepool, "lightcorona", 32, 32, &pixels[0][0][0], TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
58 }
59
60 void r_light_shutdown(void)
61 {
62         lighttexturepool = NULL;
63         lightcorona = NULL;
64 }
65
66 void r_light_newmap(void)
67 {
68         int i;
69         for (i = 0;i < 256;i++)
70                 d_lightstylevalue[i] = 264;             // normal light value
71 }
72
73 void R_Light_Init(void)
74 {
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);
80 }
81
82 /*
83 ==================
84 R_UpdateLights
85 ==================
86 */
87 void R_UpdateLights(void)
88 {
89         float frac;
90         int i, j, k, l;
91
92 // light animations
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++)
97         {
98                 if (!cl_lightstyle || !cl_lightstyle[j].length)
99                 {
100                         d_lightstylevalue[j] = 256;
101                         continue;
102                 }
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;
108         }
109
110         r_numdlights = 0;
111         c_dlights = 0;
112
113         if (!r_dynamic.integer || !cl_dlights)
114                 return;
115
116         // TODO: optimize to not scan whole cl_dlights array if possible
117         for (i = 0;i < MAX_DLIGHTS;i++)
118         {
119                 if (cl_dlights[i].radius > 0)
120                 {
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
125                 }
126         }
127 }
128
129 void R_DrawCoronas(void)
130 {
131         int i, lnum, flag;
132         float cscale, scale, viewdist, dist;
133         dlight_t *light;
134         if (r_coronas.value < 0.01)
135                 return;
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++)
140         {
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_TraceBox(light->rtlight.shadoworigin, vec3_origin, vec3_origin, r_vieworigin, true, NULL, SUPERCONTENTS_SOLID, false).fraction == 1)
142                 {
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);
146                 }
147         }
148         for (i = 0, light = r_dlight;i < r_numdlights;i++, light++)
149         {
150                 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)
151                 {
152                         cscale = light->corona * r_coronas.value * 0.25f;
153                         scale = light->rtlight.radius * light->rtlight.coronasizescale;
154                         if (gl_flashblend.integer)
155                         {
156                                 cscale *= 4.0f;
157                                 scale *= 2.0f;
158                         }
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);
160                 }
161         }
162 }
163
164 /*
165 =============================================================================
166
167 LIGHT SAMPLING
168
169 =============================================================================
170 */
171
172 void R_CompleteLightPoint(vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal, const vec3_t p, int dynamic)
173 {
174         VectorClear(diffusecolor);
175         VectorClear(diffusenormal);
176
177         if (!r_fullbright.integer && r_refdef.worldmodel && r_refdef.worldmodel->brush.LightPoint)
178         {
179                 ambientcolor[0] = ambientcolor[1] = ambientcolor[2] = r_ambient.value * (2.0f / 128.0f);
180                 r_refdef.worldmodel->brush.LightPoint(r_refdef.worldmodel, p, ambientcolor, diffusecolor, diffusenormal);
181         }
182         else
183                 VectorSet(ambientcolor, 1, 1, 1);
184
185         // FIXME: this .lights related stuff needs to be ported into the Mod_Q1BSP code
186         if (r_refdef.worldmodel->brushq1.numlights)
187         {
188                 int i;
189                 vec3_t v;
190                 float f;
191                 mlight_t *sl;
192                 for (i = 0;i < r_refdef.worldmodel->brushq1.numlights;i++)
193                 {
194                         sl = r_refdef.worldmodel->brushq1.lights + i;
195                         if (d_lightstylevalue[sl->style] > 0)
196                         {
197                                 VectorSubtract (p, sl->origin, v);
198                                 f = ((1.0f / (DotProduct(v, v) * sl->falloff + sl->distbias)) - sl->subtract);
199                                 if (f > 0 && CL_TraceBox(p, vec3_origin, vec3_origin, sl->origin, false, NULL, SUPERCONTENTS_SOLID, false).fraction == 1)
200                                 {
201                                         f *= d_lightstylevalue[sl->style] * (1.0f / 65536.0f);
202                                         if (f > 0)
203                                                 VectorMA(ambientcolor, f, sl->light, ambientcolor);
204                                 }
205                         }
206                 }
207         }
208
209         if (dynamic)
210         {
211                 int i;
212                 float f, v[3];
213                 dlight_t *light;
214                 // FIXME: this really should handle dlights as diffusecolor/diffusenormal somehow
215                 for (i = 0;i < r_numdlights;i++)
216                 {
217                         light = r_dlight + i;
218                         VectorSubtract(p, light->origin, v);
219                         f = DotProduct(v, v);
220                         if (f < light->rtlight.lightmap_cullradius2 && CL_TraceBox(p, vec3_origin, vec3_origin, light->origin, false, NULL, SUPERCONTENTS_SOLID, false).fraction == 1)
221                         {
222                                 f = (1.0f / (f + LIGHTOFFSET)) - light->rtlight.lightmap_subtract;
223                                 if (f > 0)
224                                         VectorMA(ambientcolor, f, light->rtlight.lightmap_light, ambientcolor);
225                         }
226                 }
227         }
228 }
229
230 typedef struct nearlight_s
231 {
232         vec3_t origin;
233         //vec_t cullradius2;
234         vec3_t light;
235         // how much this light would contribute to ambient if replaced
236         vec3_t ambientlight;
237         vec_t subtract;
238         vec_t falloff;
239         vec_t offset;
240         // used for choosing only the brightest lights
241         vec_t intensity;
242 }
243 nearlight_t;
244
245 static int nearlights;
246 static nearlight_t nearlight[MAX_DLIGHTS];
247
248 int R_LightModel(float *ambient4f, float *diffusecolor, float *diffusenormal, const entity_render_t *ent, float colorr, float colorg, float colorb, float colora, int worldcoords)
249 {
250         int i, j, maxnearlights;
251         float v[3], f, mscale, stylescale, intensity, ambientcolor[3], tempdiffusenormal[3];
252         nearlight_t *nl;
253         mlight_t *sl;
254         dlight_t *light;
255
256         nearlights = 0;
257         maxnearlights = r_modellights.integer;
258         ambient4f[0] = ambient4f[1] = ambient4f[2] = r_ambient.value * (2.0f / 128.0f);
259         VectorClear(diffusecolor);
260         VectorClear(diffusenormal);
261         if (!(ent->flags & RENDER_LIGHT))
262         {
263                 // highly rare
264                 VectorSet(ambient4f, 1, 1, 1);
265                 maxnearlights = 0;
266         }
267         else if (r_lightmapintensity <= 0 && !(ent->flags & RENDER_TRANSPARENT))
268                 maxnearlights = 0;
269         else
270         {
271                 if (r_refdef.worldmodel && r_refdef.worldmodel->brush.LightPoint)
272                 {
273                         r_refdef.worldmodel->brush.LightPoint(r_refdef.worldmodel, ent->origin, ambient4f, diffusecolor, tempdiffusenormal);
274                         Matrix4x4_Transform3x3(&ent->inversematrix, tempdiffusenormal, diffusenormal);
275                         VectorNormalize(diffusenormal);
276                 }
277                 else
278                         VectorSet(ambient4f, 1, 1, 1);
279         }
280
281         // scale of the model's coordinate space, to alter light attenuation to match
282         // make the mscale squared so it can scale the squared distance results
283         mscale = ent->scale * ent->scale;
284         // FIXME: no support for .lights on non-Q1BSP?
285         nl = &nearlight[0];
286         for (i = 0;i < ent->numentlights;i++)
287         {
288                 sl = r_refdef.worldmodel->brushq1.lights + ent->entlights[i];
289                 stylescale = d_lightstylevalue[sl->style] * (1.0f / 65536.0f);
290                 VectorSubtract (ent->origin, sl->origin, v);
291                 f = ((1.0f / (DotProduct(v, v) * sl->falloff + sl->distbias)) - sl->subtract) * stylescale;
292                 VectorScale(sl->light, f, ambientcolor);
293                 intensity = DotProduct(ambientcolor, ambientcolor);
294                 if (f < 0)
295                         intensity *= -1.0f;
296                 if (nearlights < maxnearlights)
297                         j = nearlights++;
298                 else
299                 {
300                         for (j = 0;j < maxnearlights;j++)
301                         {
302                                 if (nearlight[j].intensity < intensity)
303                                 {
304                                         if (nearlight[j].intensity > 0)
305                                                 VectorAdd(ambient4f, nearlight[j].ambientlight, ambient4f);
306                                         break;
307                                 }
308                         }
309                 }
310                 if (j >= maxnearlights)
311                 {
312                         // this light is less significant than all others,
313                         // add it to ambient
314                         if (intensity > 0)
315                                 VectorAdd(ambient4f, ambientcolor, ambient4f);
316                 }
317                 else
318                 {
319                         nl = nearlight + j;
320                         nl->intensity = intensity;
321                         // transform the light into the model's coordinate system
322                         if (worldcoords)
323                                 VectorCopy(sl->origin, nl->origin);
324                         else
325                                 Matrix4x4_Transform(&ent->inversematrix, sl->origin, nl->origin);
326                         // integrate mscale into falloff, for maximum speed
327                         nl->falloff = sl->falloff * mscale;
328                         VectorCopy(ambientcolor, nl->ambientlight);
329                         nl->light[0] = sl->light[0] * stylescale * colorr * 4.0f;
330                         nl->light[1] = sl->light[1] * stylescale * colorg * 4.0f;
331                         nl->light[2] = sl->light[2] * stylescale * colorb * 4.0f;
332                         nl->subtract = sl->subtract;
333                         nl->offset = sl->distbias;
334                 }
335         }
336         if (ent->flags & RENDER_TRANSPARENT)
337         {
338                 // FIXME: this dlighting doesn't look like rtlights
339                 for (i = 0;i < r_numdlights;i++)
340                 {
341                         light = r_dlight + i;
342                         VectorCopy(light->origin, v);
343                         if (v[0] < ent->mins[0]) v[0] = ent->mins[0];if (v[0] > ent->maxs[0]) v[0] = ent->maxs[0];
344                         if (v[1] < ent->mins[1]) v[1] = ent->mins[1];if (v[1] > ent->maxs[1]) v[1] = ent->maxs[1];
345                         if (v[2] < ent->mins[2]) v[2] = ent->mins[2];if (v[2] > ent->maxs[2]) v[2] = ent->maxs[2];
346                         VectorSubtract (v, light->origin, v);
347                         if (DotProduct(v, v) < light->rtlight.lightmap_cullradius2)
348                         {
349                                 if (CL_TraceBox(ent->origin, vec3_origin, vec3_origin, light->origin, false, NULL, SUPERCONTENTS_SOLID, false).fraction != 1)
350                                         continue;
351                                 VectorSubtract (ent->origin, light->origin, v);
352                                 f = ((1.0f / (DotProduct(v, v) + LIGHTOFFSET)) - light->rtlight.lightmap_subtract);
353                                 VectorScale(light->rtlight.lightmap_light, f, ambientcolor);
354                                 intensity = DotProduct(ambientcolor, ambientcolor);
355                                 if (f < 0)
356                                         intensity *= -1.0f;
357                                 if (nearlights < maxnearlights)
358                                         j = nearlights++;
359                                 else
360                                 {
361                                         for (j = 0;j < maxnearlights;j++)
362                                         {
363                                                 if (nearlight[j].intensity < intensity)
364                                                 {
365                                                         if (nearlight[j].intensity > 0)
366                                                                 VectorAdd(ambient4f, nearlight[j].ambientlight, ambient4f);
367                                                         break;
368                                                 }
369                                         }
370                                 }
371                                 if (j >= maxnearlights)
372                                 {
373                                         // this light is less significant than all others,
374                                         // add it to ambient
375                                         if (intensity > 0)
376                                                 VectorAdd(ambient4f, ambientcolor, ambient4f);
377                                 }
378                                 else
379                                 {
380                                         nl = nearlight + j;
381                                         nl->intensity = intensity;
382                                         // transform the light into the model's coordinate system
383                                         if (worldcoords)
384                                                 VectorCopy(light->origin, nl->origin);
385                                         else
386                                         {
387                                                 Matrix4x4_Transform(&ent->inversematrix, light->origin, nl->origin);
388                                                 /*
389                                                 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"
390                                                 , rd - r_dlight, ent->model->name
391                                                 , light->origin[0], light->origin[1], light->origin[2]
392                                                 , nl->origin[0], nl->origin[1], nl->origin[2]
393                                                 , ent->inversematrix.m[0][0], ent->inversematrix.m[0][1], ent->inversematrix.m[0][2], ent->inversematrix.m[0][3]
394                                                 , ent->inversematrix.m[1][0], ent->inversematrix.m[1][1], ent->inversematrix.m[1][2], ent->inversematrix.m[1][3]
395                                                 , ent->inversematrix.m[2][0], ent->inversematrix.m[2][1], ent->inversematrix.m[2][2], ent->inversematrix.m[2][3]
396                                                 , ent->inversematrix.m[3][0], ent->inversematrix.m[3][1], ent->inversematrix.m[3][2], ent->inversematrix.m[3][3]);
397                                                 */
398                                         }
399                                         // integrate mscale into falloff, for maximum speed
400                                         nl->falloff = mscale;
401                                         VectorCopy(ambientcolor, nl->ambientlight);
402                                         nl->light[0] = light->rtlight.lightmap_light[0] * colorr * 4.0f;
403                                         nl->light[1] = light->rtlight.lightmap_light[1] * colorg * 4.0f;
404                                         nl->light[2] = light->rtlight.lightmap_light[2] * colorb * 4.0f;
405                                         nl->subtract = light->rtlight.lightmap_subtract;
406                                         nl->offset = LIGHTOFFSET;
407                                 }
408                         }
409                 }
410         }
411         ambient4f[0] *= colorr;
412         ambient4f[1] *= colorg;
413         ambient4f[2] *= colorb;
414         ambient4f[3] = colora;
415         diffusecolor[0] *= colorr;
416         diffusecolor[1] *= colorg;
417         diffusecolor[2] *= colorb;
418         return nearlights != 0 || DotProduct(diffusecolor, diffusecolor) > 0;
419 }
420
421 void R_LightModel_CalcVertexColors(const float *ambientcolor4f, const float *diffusecolor, const float *diffusenormal, int numverts, const float *vertex3f, const float *normal3f, float *color4f)
422 {
423         int i, j, usediffuse;
424         float color[4], v[3], dot, dist2, f, dnormal[3];
425         nearlight_t *nl;
426         usediffuse = DotProduct(diffusecolor, diffusecolor) > 0;
427         // negate the diffuse normal to avoid the need to negate the
428         // dotproduct on each vertex
429         VectorNegate(diffusenormal, dnormal);
430         if (usediffuse)
431                 VectorNormalize(dnormal);
432         // directional shading code here
433         for (i = 0;i < numverts;i++, vertex3f += 3, normal3f += 3, color4f += 4)
434         {
435                 VectorCopy4(ambientcolor4f, color);
436
437                 // silly directional diffuse shading
438                 if (usediffuse)
439                 {
440                         dot = DotProduct(normal3f, dnormal);
441                         if (dot > 0)
442                                 VectorMA(color, dot, diffusecolor, color);
443                 }
444
445                 // pretty good lighting
446                 for (j = 0, nl = &nearlight[0];j < nearlights;j++, nl++)
447                 {
448                         VectorSubtract(nl->origin, vertex3f, v);
449                         // first eliminate negative lighting (back side)
450                         dot = DotProduct(normal3f, v);
451                         if (dot > 0)
452                         {
453                                 // we'll need this again later to normalize the dotproduct
454                                 dist2 = DotProduct(v,v);
455                                 // do the distance attenuation math
456                                 f = (1.0f / (dist2 * nl->falloff + nl->offset)) - nl->subtract;
457                                 if (f > 0)
458                                 {
459                                         // we must divide dot by sqrt(dist2) to compensate for
460                                         // the fact we did not normalize v before doing the
461                                         // dotproduct, the result is in the range 0 to 1 (we
462                                         // eliminated negative numbers already)
463                                         f *= dot / sqrt(dist2);
464                                         // blend in the lighting
465                                         VectorMA(color, f, nl->light, color);
466                                 }
467                         }
468                 }
469                 VectorCopy4(color, color4f);
470         }
471 }
472
473 void R_UpdateEntLights(entity_render_t *ent)
474 {
475         int i;
476         const mlight_t *sl;
477         vec3_t v;
478         if (r_lightmapintensity <= 0 && !(ent->flags & RENDER_TRANSPARENT))
479                 return;
480         VectorSubtract(ent->origin, ent->entlightsorigin, v);
481         if (ent->entlightsframe != (r_framecount - 1) || (realtime > ent->entlightstime && DotProduct(v,v) >= 1.0f))
482         {
483                 ent->entlightstime = realtime + 0.1;
484                 VectorCopy(ent->origin, ent->entlightsorigin);
485                 ent->numentlights = 0;
486                 if (r_refdef.worldmodel)
487                         for (i = 0, sl = r_refdef.worldmodel->brushq1.lights;i < r_refdef.worldmodel->brushq1.numlights && ent->numentlights < MAXENTLIGHTS;i++, sl++)
488                                 if (CL_TraceBox(ent->origin, vec3_origin, vec3_origin, sl->origin, false, NULL, SUPERCONTENTS_SOLID, false).fraction == 1)
489                                         ent->entlights[ent->numentlights++] = i;
490         }
491         ent->entlightsframe = r_framecount;
492 }
493