R_LoadTexture functions take a palette pointer now
[xonotic/darkplaces.git] / r_shadow.c
1
2 #include "quakedef.h"
3 #include "r_shadow.h"
4
5 mempool_t *r_shadow_mempool;
6
7 int maxshadowelements;
8 int *shadowelements;
9 int maxtrianglefacinglight;
10 qbyte *trianglefacinglight;
11
12 rtexturepool_t *r_shadow_texturepool;
13 rtexture_t *r_shadow_normalsattenuationtexture;
14 rtexture_t *r_shadow_normalscubetexture;
15 rtexture_t *r_shadow_attenuation2dtexture;
16 rtexture_t *r_shadow_blankbumptexture;
17
18 cvar_t r_shadow1 = {0, "r_shadow1", "16"};
19 cvar_t r_shadow2 = {0, "r_shadow2", "2"};
20 cvar_t r_shadow3 = {0, "r_shadow3", "65536"};
21 cvar_t r_shadow4 = {0, "r_shadow4", "1"};
22 cvar_t r_shadow5 = {0, "r_shadow5", "0"};
23 cvar_t r_shadow6 = {0, "r_shadow6", "1"};
24 cvar_t r_light_realtime = {0, "r_light_realtime", "0"};
25 cvar_t r_light_quality = {0, "r_light_quality", "1"};
26 cvar_t r_light_gloss = {0, "r_light_gloss", "0"};
27 cvar_t r_light_debuglight = {0, "r_light_debuglight", "-1"};
28
29 void r_shadow_start(void)
30 {
31         // allocate vertex processing arrays
32         r_shadow_mempool = Mem_AllocPool("R_Shadow");
33         maxshadowelements = 0;
34         shadowelements = NULL;
35         maxtrianglefacinglight = 0;
36         trianglefacinglight = NULL;
37         r_shadow_normalsattenuationtexture = NULL;
38         r_shadow_normalscubetexture = NULL;
39         r_shadow_attenuation2dtexture = NULL;
40         r_shadow_blankbumptexture = NULL;
41         r_shadow_texturepool = NULL;
42 }
43
44 void r_shadow_shutdown(void)
45 {
46         r_shadow_normalsattenuationtexture = NULL;
47         r_shadow_normalscubetexture = NULL;
48         r_shadow_attenuation2dtexture = NULL;
49         r_shadow_blankbumptexture = NULL;
50         R_FreeTexturePool(&r_shadow_texturepool);
51         maxshadowelements = 0;
52         shadowelements = NULL;
53         maxtrianglefacinglight = 0;
54         trianglefacinglight = NULL;
55         Mem_FreePool(&r_shadow_mempool);
56 }
57
58 void r_shadow_newmap(void)
59 {
60 }
61
62 void R_Shadow_Init(void)
63 {
64         Cvar_RegisterVariable(&r_shadow1);
65         Cvar_RegisterVariable(&r_shadow2);
66         Cvar_RegisterVariable(&r_shadow3);
67         Cvar_RegisterVariable(&r_shadow4);
68         Cvar_RegisterVariable(&r_shadow5);
69         Cvar_RegisterVariable(&r_shadow6);
70         Cvar_RegisterVariable(&r_light_realtime);
71         Cvar_RegisterVariable(&r_light_quality);
72         Cvar_RegisterVariable(&r_light_gloss);
73         Cvar_RegisterVariable(&r_light_debuglight);
74         R_RegisterModule("R_Shadow", r_shadow_start, r_shadow_shutdown, r_shadow_newmap);
75 }
76
77 void R_Shadow_Volume(int numverts, int numtris, float *vertex, int *elements, int *neighbors, vec3_t relativelightorigin, float lightradius, float projectdistance, int visiblevolume)
78 {
79         int i, *e, *n, *out, tris;
80         float *v0, *v1, *v2, temp[3], f;
81         if (projectdistance < 0.1)
82         {
83                 Con_Printf("R_Shadow_Volume: projectdistance %f\n");
84                 return;
85         }
86 // terminology:
87 //
88 // frontface:
89 // a triangle facing the light source
90 //
91 // backface:
92 // a triangle not facing the light source
93 //
94 // shadow volume:
95 // an extrusion of the backfaces, beginning at the original geometry and
96 // ending further from the light source than the original geometry
97 // (presumably at least as far as the light's radius, if the light has a
98 // radius at all), capped at both front and back to avoid any problems
99 //
100 // description:
101 // draws the shadow volumes of the model.
102 // requirements:
103 // vertex loations must already be in vertex before use.
104 // vertex must have capacity for numverts * 2.
105
106         // make sure trianglefacinglight is big enough for this volume
107         if (maxtrianglefacinglight < numtris)
108         {
109                 maxtrianglefacinglight = numtris;
110                 if (trianglefacinglight)
111                         Mem_Free(trianglefacinglight);
112                 trianglefacinglight = Mem_Alloc(r_shadow_mempool, maxtrianglefacinglight);
113         }
114
115         // make sure shadowelements is big enough for this volume
116         if (maxshadowelements < numtris * 24)
117         {
118                 maxshadowelements = numtris * 24;
119                 if (shadowelements)
120                         Mem_Free(shadowelements);
121                 shadowelements = Mem_Alloc(r_shadow_mempool, maxshadowelements * sizeof(int));
122         }
123
124         // make projected vertices
125         // by clever use of elements we'll construct the whole shadow from
126         // the unprojected vertices and these projected vertices
127         for (i = 0, v0 = vertex, v1 = vertex + numverts * 4;i < numverts;i++, v0 += 4, v1 += 4)
128         {
129 #if 1
130                 v1[0] = v0[0] + 50.0f * (v0[0] - relativelightorigin[0]);
131                 v1[1] = v0[1] + 50.0f * (v0[1] - relativelightorigin[1]);
132                 v1[2] = v0[2] + 50.0f * (v0[2] - relativelightorigin[2]);
133 #elif 0
134                 VectorSubtract(v0, relativelightorigin, temp);
135                 f = lightradius / sqrt(DotProduct(temp,temp));
136                 if (f < 1)
137                         f = 1;
138                 VectorMA(relativelightorigin, f, temp, v1);
139 #else
140                 VectorSubtract(v0, relativelightorigin, temp);
141                 f = projectdistance / sqrt(DotProduct(temp,temp));
142                 VectorMA(v0, f, temp, v1);
143 #endif
144         }
145
146         // check which triangles are facing the light
147         for (i = 0, e = elements;i < numtris;i++, e += 3)
148         {
149                 // calculate triangle facing flag
150                 v0 = vertex + e[0] * 4;
151                 v1 = vertex + e[1] * 4;
152                 v2 = vertex + e[2] * 4;
153                 // we do not need to normalize the surface normal because both sides
154                 // of the comparison use it, therefore they are both multiplied the
155                 // same amount...  furthermore the subtract can be done on the
156                 // vectors, saving a little bit of math in the dotproducts
157 #if 0
158                 // fast version
159                 // subtracts v1 from v0 and v2, combined into a crossproduct,
160                 // combined with a dotproduct of the light location relative to the
161                 // first point of the triangle (any point works, since the triangle
162                 // is obviously flat), and finally a comparison to determine if the
163                 // light is infront of the triangle (the goal of this statement)
164                 trianglefacinglight[i] =
165                    (relativelightorigin[0] - v0[0]) * ((v0[1] - v1[1]) * (v2[2] - v1[2]) - (v0[2] - v1[2]) * (v2[1] - v1[1]))
166                  + (relativelightorigin[1] - v0[1]) * ((v0[2] - v1[2]) * (v2[0] - v1[0]) - (v0[0] - v1[0]) * (v2[2] - v1[2]))
167                  + (relativelightorigin[2] - v0[2]) * ((v0[0] - v1[0]) * (v2[1] - v1[1]) - (v0[1] - v1[1]) * (v2[0] - v1[0])) > 0;
168 #else
169                 // readable version
170                 {
171                 float dir0[3], dir1[3];
172
173                 // calculate two mostly perpendicular edge directions
174                 VectorSubtract(v0, v1, dir0);
175                 VectorSubtract(v2, v1, dir1);
176
177                 // we have two edge directions, we can calculate a third vector from
178                 // them, which is the direction of the surface normal (it's magnitude
179                 // is not 1 however)
180                 CrossProduct(dir0, dir1, temp);
181
182                 // this is entirely unnecessary, but kept for clarity
183                 //VectorNormalize(temp);
184
185                 // compare distance of light along normal, with distance of any point
186                 // of the triangle along the same normal (the triangle is planar,
187                 // I.E. flat, so all points give the same answer)
188                 // the normal is not normalized because it is used on both sides of
189                 // the comparison, so it's magnitude does not matter
190                 //trianglefacinglight[i] = DotProduct(relativelightorigin, temp) >= DotProduct(v0, temp);
191                 f = DotProduct(relativelightorigin, temp) - DotProduct(v0, temp);
192                 trianglefacinglight[i] = f > 0 && f < lightradius * sqrt(DotProduct(temp, temp));
193                 }
194 #endif
195         }
196
197         // output triangle elements
198         out = shadowelements;
199         tris = 0;
200
201         // check each backface for bordering frontfaces,
202         // and cast shadow polygons from those edges,
203         // also create front and back caps for shadow volume
204         for (i = 0, e = elements, n = neighbors;i < numtris;i++, e += 3, n += 3)
205         {
206                 if (!trianglefacinglight[i])
207                 {
208                         // triangle is backface and therefore casts shadow,
209                         // output front and back caps for shadow volume
210 #if 1
211                         // front cap (with flipped winding order)
212                         out[0] = e[0];
213                         out[1] = e[2];
214                         out[2] = e[1];
215                         // rear cap
216                         out[3] = e[0] + numverts;
217                         out[4] = e[1] + numverts;
218                         out[5] = e[2] + numverts;
219                         out += 6;
220                         tris += 2;
221 #else if 1
222                         // rear cap
223                         out[0] = e[0] + numverts;
224                         out[1] = e[1] + numverts;
225                         out[2] = e[2] + numverts;
226                         out += 3;
227                         tris += 1;
228 #endif
229                         // check the edges
230                         if (n[0] < 0 || trianglefacinglight[n[0]])
231                         {
232                                 out[0] = e[0];
233                                 out[1] = e[1];
234                                 out[2] = e[1] + numverts;
235                                 out[3] = e[0];
236                                 out[4] = e[1] + numverts;
237                                 out[5] = e[0] + numverts;
238                                 out += 6;
239                                 tris += 2;
240                         }
241                         if (n[1] < 0 || trianglefacinglight[n[1]])
242                         {
243                                 out[0] = e[1];
244                                 out[1] = e[2];
245                                 out[2] = e[2] + numverts;
246                                 out[3] = e[1];
247                                 out[4] = e[2] + numverts;
248                                 out[5] = e[1] + numverts;
249                                 out += 6;
250                                 tris += 2;
251                         }
252                         if (n[2] < 0 || trianglefacinglight[n[2]])
253                         {
254                                 out[0] = e[2];
255                                 out[1] = e[0];
256                                 out[2] = e[0] + numverts;
257                                 out[3] = e[2];
258                                 out[4] = e[0] + numverts;
259                                 out[5] = e[2] + numverts;
260                                 out += 6;
261                                 tris += 2;
262                         }
263                 }
264         }
265         R_Shadow_RenderVolume(numverts * 2, tris, shadowelements, visiblevolume);
266 }
267
268 void R_Shadow_RenderVolume(int numverts, int numtris, int *elements, int visiblevolume)
269 {
270         // draw the volume
271         if (visiblevolume)
272         {
273                 //qglDisable(GL_CULL_FACE);
274                 R_Mesh_Draw(numverts, numtris, elements);
275                 //qglEnable(GL_CULL_FACE);
276         }
277         else
278         {
279                 // increment stencil if backface is behind depthbuffer
280                 qglCullFace(GL_BACK); // quake is backwards, this culls front faces
281                 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
282                 R_Mesh_Draw(numverts, numtris, elements);
283                 // decrement stencil if frontface is behind depthbuffer
284                 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
285                 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
286                 R_Mesh_Draw(numverts, numtris, elements);
287         }
288 }
289
290 float r_shadow_atten1, r_shadow_atten2, r_shadow_atten5;
291 #define ATTEN3DSIZE 64
292 static void R_Shadow_Make3DTextures(void)
293 {
294         int x, y, z, d;
295         float v[3], intensity, ilen, length, bordercolor[4];
296         qbyte data[ATTEN3DSIZE][ATTEN3DSIZE][ATTEN3DSIZE][4];
297         if (r_light_quality.integer != 1 || !gl_texture3d)
298                 return;
299         for (z = 0;z < ATTEN3DSIZE;z++)
300         {
301                 for (y = 0;y < ATTEN3DSIZE;y++)
302                 {
303                         for (x = 0;x < ATTEN3DSIZE;x++)
304                         {
305                                 v[0] = (x + 0.5f) * (2.0f / (float) ATTEN3DSIZE) - 1.0f;
306                                 v[1] = (y + 0.5f) * (2.0f / (float) ATTEN3DSIZE) - 1.0f;
307                                 v[2] = (z + 0.5f) * (2.0f / (float) ATTEN3DSIZE) - 1.0f;
308                                 length = sqrt(DotProduct(v, v));
309                                 if (DotProduct(v, v) < 1)
310                                         intensity = (((r_shadow_atten1 / (length*length + r_shadow_atten5)) - (r_shadow_atten1 * r_shadow_atten2))) / 256.0f;
311                                 else
312                                         intensity = 0;
313                                 ilen = 127.0f * bound(0, intensity, 1) / length;
314                                 data[z][y][x][0] = 128.0f + ilen * v[0];
315                                 data[z][y][x][1] = 128.0f + ilen * v[1];
316                                 data[z][y][x][2] = 128.0f + ilen * v[2];
317                                 data[z][y][x][3] = 255;
318                         }
319                 }
320         }
321         r_shadow_normalsattenuationtexture = R_LoadTexture3D(r_shadow_texturepool, "normalsattenuation", ATTEN3DSIZE, ATTEN3DSIZE, ATTEN3DSIZE, &data[0][0][0][0], TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALWAYSPRECACHE, NULL);
322         bordercolor[0] = 0.5f;
323         bordercolor[1] = 0.5f;
324         bordercolor[2] = 0.5f;
325         bordercolor[3] = 1.0f;
326         qglTexParameterfv(GL_TEXTURE_3D, GL_TEXTURE_BORDER_COLOR, bordercolor);
327 }
328
329 static void R_Shadow_MakeTextures(void)
330 {
331         int x, y, z, d, side;
332         float v[3], s, t, intensity;
333         qbyte data[6][128][128][4];
334         R_FreeTexturePool(&r_shadow_texturepool);
335         r_shadow_texturepool = R_AllocTexturePool();
336         r_shadow_atten1 = r_shadow1.value;
337         r_shadow_atten2 = r_shadow2.value;
338         r_shadow_atten5 = r_shadow5.value;
339         for (y = 0;y < 128;y++)
340         {
341                 for (x = 0;x < 128;x++)
342                 {
343                         data[0][y][x][0] = 128;
344                         data[0][y][x][1] = 128;
345                         data[0][y][x][2] = 255;
346                         data[0][y][x][3] = 255;
347                 }
348         }
349         r_shadow_blankbumptexture = R_LoadTexture2D(r_shadow_texturepool, "blankbump", 128, 128, &data[0][0][0][0], TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
350         for (side = 0;side < 6;side++)
351         {
352                 for (y = 0;y < 128;y++)
353                 {
354                         for (x = 0;x < 128;x++)
355                         {
356                                 s = (x + 0.5f) * (2.0f / 128.0f) - 1.0f;
357                                 t = (y + 0.5f) * (2.0f / 128.0f) - 1.0f;
358                                 switch(side)
359                                 {
360                                 case 0:
361                                         v[0] = 1;
362                                         v[1] = -t;
363                                         v[2] = -s;
364                                         break;
365                                 case 1:
366                                         v[0] = -1;
367                                         v[1] = -t;
368                                         v[2] = s;
369                                         break;
370                                 case 2:
371                                         v[0] = s;
372                                         v[1] = 1;
373                                         v[2] = t;
374                                         break;
375                                 case 3:
376                                         v[0] = s;
377                                         v[1] = -1;
378                                         v[2] = -t;
379                                         break;
380                                 case 4:
381                                         v[0] = s;
382                                         v[1] = -t;
383                                         v[2] = 1;
384                                         break;
385                                 case 5:
386                                         v[0] = -s;
387                                         v[1] = -t;
388                                         v[2] = -1;
389                                         break;
390                                 }
391                                 intensity = 127.0f / sqrt(DotProduct(v, v));
392                                 data[side][y][x][0] = 128.0f + intensity * v[0];
393                                 data[side][y][x][1] = 128.0f + intensity * v[1];
394                                 data[side][y][x][2] = 128.0f + intensity * v[2];
395                                 data[side][y][x][3] = 255;
396                         }
397                 }
398         }
399         r_shadow_normalscubetexture = R_LoadTextureCubeMap(r_shadow_texturepool, "normalscube", 128, &data[0][0][0][0], TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP, NULL);
400         for (y = 0;y < 128;y++)
401         {
402                 for (x = 0;x < 128;x++)
403                 {
404                         v[0] = (x + 0.5f) * (2.0f / 128.0f) - 1.0f;
405                         v[1] = (y + 0.5f) * (2.0f / 128.0f) - 1.0f;
406                         v[2] = 0;
407                         if (DotProduct(v, v) < 1)
408                                 intensity = (((r_shadow_atten1 / (DotProduct(v, v)+r_shadow_atten5)) - (r_shadow_atten1 * r_shadow_atten2))) / 256.0f;
409                         else
410                                 intensity = 0;
411                         d = bound(0, intensity, 255) / sqrt(DotProduct(v, v));
412                         data[0][y][x][0] = d;
413                         data[0][y][x][1] = d;
414                         data[0][y][x][2] = d;
415                         data[0][y][x][3] = 255;
416                 }
417         }
418         r_shadow_attenuation2dtexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation2d", 128, 128, &data[0][0][0][0], TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP, NULL);
419         R_Shadow_Make3DTextures();
420 }
421
422 void R_Shadow_Stage_Begin(void)
423 {
424         rmeshstate_t m;
425
426         if (r_light_quality.integer == 1 && !gl_texture3d)
427         {
428                 Con_Printf("3D texture support not detected, falling back on slower 2D + 1D + normalization lighting\n");
429                 Cvar_SetValueQuick(&r_light_quality, 0);
430         }
431         //cl.worldmodel->numlights = min(cl.worldmodel->numlights, 1);
432         if (!r_shadow_attenuation2dtexture
433          || (r_light_quality.integer == 1 && !r_shadow_normalsattenuationtexture)
434          || r_shadow1.value != r_shadow_atten1
435          || r_shadow2.value != r_shadow_atten2
436          || r_shadow5.value != r_shadow_atten5)
437                 R_Shadow_MakeTextures();
438
439         memset(&m, 0, sizeof(m));
440         m.blendfunc1 = GL_ONE;
441         m.blendfunc2 = GL_ZERO;
442         R_Mesh_State(&m);
443         GL_Color(0, 0, 0, 1);
444 }
445
446 void R_Shadow_Stage_ShadowVolumes(void)
447 {
448         rmeshstate_t m;
449         memset(&m, 0, sizeof(m));
450         R_Mesh_TextureState(&m);
451         GL_Color(1, 1, 1, 1);
452         qglColorMask(0, 0, 0, 0);
453         qglDisable(GL_BLEND);
454         qglDepthMask(0);
455         qglDepthFunc(GL_LESS);
456         qglClearStencil(0);
457         qglClear(GL_STENCIL_BUFFER_BIT);
458         qglEnable(GL_STENCIL_TEST);
459         qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
460         qglStencilFunc(GL_ALWAYS, 0, 0xFF);
461 }
462
463 void R_Shadow_Stage_Light(void)
464 {
465         rmeshstate_t m;
466         memset(&m, 0, sizeof(m));
467         R_Mesh_TextureState(&m);
468         qglActiveTexture(GL_TEXTURE0_ARB);
469
470         qglEnable(GL_BLEND);
471         qglBlendFunc(GL_ONE, GL_ONE);
472         GL_Color(1, 1, 1, 1);
473         qglColorMask(1, 1, 1, 1);
474         qglDepthMask(0);
475         qglDepthFunc(GL_EQUAL);
476         qglEnable(GL_STENCIL_TEST);
477         qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
478         // only draw light where this geometry was already rendered AND the
479         // stencil is 0 (non-zero means shadow)
480         qglStencilFunc(GL_EQUAL, 0, 0xFF);
481 }
482
483 void R_Shadow_Stage_End(void)
484 {
485         rmeshstate_t m;
486         // attempt to restore state to what Mesh_State thinks it is
487         qglDisable(GL_BLEND);
488         qglBlendFunc(GL_ONE, GL_ZERO);
489         qglDepthMask(1);
490         // now restore the rest of the state to normal
491         GL_Color(1, 1, 1, 1);
492         qglColorMask(1, 1, 1, 1);
493         qglDepthFunc(GL_LEQUAL);
494         qglDisable(GL_STENCIL_TEST);
495         qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
496         qglStencilFunc(GL_ALWAYS, 0, 0xFF);
497 }
498
499 void R_Shadow_GenTexCoords_Attenuation2D(float *out, int numverts, const float *vertex, const float *svectors, const float *tvectors, const vec3_t relativelightorigin, float lightradius)
500 {
501         int i;
502         float lightvec[3], iradius;
503         iradius = 0.5f / lightradius;
504         for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, out += 4)
505         {
506                 VectorSubtract(vertex, relativelightorigin, lightvec);
507                 out[0] = 0.5f + DotProduct(svectors, lightvec) * iradius;
508                 out[1] = 0.5f + DotProduct(tvectors, lightvec) * iradius;
509         }
510 }
511
512 void R_Shadow_GenTexCoords_Attenuation1D(float *out, int numverts, const float *vertex, const float *normals, const vec3_t relativelightorigin, float lightradius)
513 {
514         int i;
515         float lightvec[3], iradius;
516         iradius = 0.5f / lightradius;
517         for (i = 0;i < numverts;i++, vertex += 4, normals += 4, out += 4)
518         {
519                 VectorSubtract(vertex, relativelightorigin, lightvec);
520                 out[0] = 0.5f + DotProduct(normals, lightvec) * iradius;
521                 out[1] = 0.5f;
522         }
523 }
524
525 void R_Shadow_GenTexCoords_Diffuse_Attenuation3D(float *out, int numverts, const float *vertex, const float *svectors, const float *tvectors, const float *normals, const vec3_t relativelightorigin, float lightradius)
526 {
527         int i;
528         float lightvec[3], iradius;
529         iradius = 0.5f / lightradius;
530         for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
531         {
532                 VectorSubtract(vertex, relativelightorigin, lightvec);
533                 if (r_shadow6.integer != 0)
534                 {
535                         VectorClear(lightvec);
536                         if (r_shadow6.integer > 0)
537                                 lightvec[(r_shadow6.integer - 1) % 3] = 64;
538                         else
539                                 lightvec[((-r_shadow6.integer) - 1) % 3] = -64;
540                 }
541                 if (r_shadow4.integer & 8)
542                         lightvec[0] = -lightvec[0];
543                 if (r_shadow4.integer & 16)
544                         lightvec[1] = -lightvec[1];
545                 if (r_shadow4.integer & 32)
546                         lightvec[2] = -lightvec[2];
547                 if (r_shadow4.integer & 1)
548                         out[0] = 0.5f - DotProduct(svectors, lightvec) * iradius;
549                 else
550                         out[0] = 0.5f + DotProduct(svectors, lightvec) * iradius;
551                 if (r_shadow4.integer & 2)
552                         out[1] = 0.5f - DotProduct(tvectors, lightvec) * iradius;
553                 else
554                         out[2] = 0.5f + DotProduct(tvectors, lightvec) * iradius;
555                 if (r_shadow4.integer & 4)
556                         out[2] = 0.5f - DotProduct(normals, lightvec) * iradius;
557                 else
558                         out[2] = 0.5f + DotProduct(normals, lightvec) * iradius;
559         }
560 }
561
562 void R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(float *out, int numverts, const float *vertex, const float *svectors, const float *tvectors, const float *normals, const vec3_t relativelightorigin, float lightradius)
563 {
564         int i;
565         float lightdir[3], iradius;
566         iradius = 0.5f / lightradius;
567         for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
568         {
569                 VectorSubtract(vertex, relativelightorigin, lightdir);
570                 // the cubemap normalizes this for us
571                 out[0] = DotProduct(svectors, lightdir);
572                 out[1] = DotProduct(tvectors, lightdir);
573                 out[2] = DotProduct(normals, lightdir);
574         }
575 }
576
577 void R_Shadow_GenTexCoords_Specular_Attenuation3D(float *out, int numverts, const float *vertex, const float *svectors, const float *tvectors, const float *normals, const vec3_t relativelightorigin, const vec3_t relativeeyeorigin, float lightradius)
578 {
579         int i;
580         float lightdir[3], eyedir[3], halfdir[3], lightdirlen, ilen, iradius;
581         iradius = 0.5f / lightradius;
582         for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
583         {
584                 VectorSubtract(vertex, relativelightorigin, lightdir);
585                 // this is used later to make the attenuation correct
586                 lightdirlen = sqrt(DotProduct(lightdir, lightdir)) * iradius;
587                 VectorNormalizeFast(lightdir);
588                 VectorSubtract(vertex, relativeeyeorigin, eyedir);
589                 VectorNormalizeFast(eyedir);
590                 VectorAdd(lightdir, eyedir, halfdir);
591                 VectorNormalizeFast(halfdir);
592                 out[0] = 0.5f + DotProduct(svectors, halfdir) * lightdirlen;
593                 out[1] = 0.5f + DotProduct(tvectors, halfdir) * lightdirlen;
594                 out[2] = 0.5f + DotProduct(normals, halfdir) * lightdirlen;
595         }
596 }
597
598 void R_Shadow_GenTexCoords_Specular_NormalCubeMap(float *out, int numverts, const float *vertex, const float *svectors, const float *tvectors, const float *normals, const vec3_t relativelightorigin, const vec3_t relativeeyeorigin, float lightradius)
599 {
600         int i;
601         float lightdir[3], eyedir[3], halfdir[3], lightdirlen, ilen, iradius;
602         iradius = 0.5f / lightradius;
603         for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
604         {
605                 VectorSubtract(vertex, relativelightorigin, lightdir);
606                 VectorNormalizeFast(lightdir);
607                 VectorSubtract(vertex, relativeeyeorigin, eyedir);
608                 VectorNormalizeFast(eyedir);
609                 VectorAdd(lightdir, eyedir, halfdir);
610                 // the cubemap normalizes this for us
611                 out[0] = DotProduct(svectors, halfdir);
612                 out[1] = DotProduct(tvectors, halfdir);
613                 out[2] = DotProduct(normals, halfdir);
614         }
615 }
616
617 void R_Shadow_GenTexCoords_LightCubeMap(float *out, int numverts, const float *vertex, const vec3_t relativelightorigin)
618 {
619         int i;
620         for (i = 0;i < numverts;i++, vertex += 4, out += 4)
621                 VectorSubtract(vertex, relativelightorigin, out);
622 }
623
624 void R_Shadow_RenderLighting(int numverts, int numtriangles, const int *elements, const float *svectors, const float *tvectors, const float *normals, const float *texcoords, const float *relativelightorigin, const float *relativeeyeorigin, float lightradius, const float *lightcolor, rtexture_t *basetexture, rtexture_t *glosstexture, rtexture_t *bumptexture, rtexture_t *lightcubemap)
625 {
626         float f;
627         rmeshstate_t m;
628         memset(&m, 0, sizeof(m));
629         if (!bumptexture)
630                 bumptexture = r_shadow_blankbumptexture;
631         f = 1.0f / r_shadow3.value;
632         if (r_light_quality.integer == 1)
633         {
634                 // 4 texture 3D path, two pass
635                 GL_Color(1,1,1,1);
636                 //lightcolor[0] * f, lightcolor[1] * f, lightcolor[2] * f, 1);
637                 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
638                 memcpy(varray_texcoord[2], texcoords, numverts * sizeof(float[4]));
639                 if (r_light_gloss.integer != 2)
640                 {
641                         m.tex[0] = R_GetTexture(bumptexture);
642                         m.tex3d[1] = R_GetTexture(r_shadow_normalsattenuationtexture);
643                         m.tex[2] = R_GetTexture(basetexture);
644                         m.texcubemap[3] = R_GetTexture(lightcubemap);
645                         m.texcombinergb[0] = GL_REPLACE;
646                         m.texcombinergb[1] = GL_DOT3_RGB_ARB;
647                         m.texcombinergb[2] = GL_MODULATE;
648                         m.texcombinergb[3] = GL_MODULATE;
649                         m.texrgbscale[2] = 2;
650                         R_Mesh_TextureState(&m);
651                         R_Shadow_GenTexCoords_Diffuse_Attenuation3D(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, lightradius);
652                         if (m.texcubemap[3])
653                                 R_Shadow_GenTexCoords_LightCubeMap(varray_texcoord[3], numverts, varray_vertex, relativelightorigin);
654                         R_Mesh_Draw(numverts, numtriangles, elements);
655                 }
656                 if (r_light_gloss.integer && glosstexture)
657                 {
658                         m.tex[0] = R_GetTexture(bumptexture);
659                         m.tex3d[1] = R_GetTexture(r_shadow_normalsattenuationtexture);
660                         m.tex[2] = R_GetTexture(glosstexture);
661                         m.texcubemap[3] = R_GetTexture(lightcubemap);
662                         m.texcombinergb[0] = GL_REPLACE;
663                         m.texcombinergb[1] = GL_DOT3_RGB_ARB;
664                         m.texcombinergb[2] = GL_MODULATE;
665                         m.texcombinergb[3] = GL_MODULATE;
666                         m.texrgbscale[2] = 2;
667                         R_Mesh_TextureState(&m);
668                         R_Shadow_GenTexCoords_Specular_Attenuation3D(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, relativeeyeorigin, lightradius);
669                         R_Mesh_Draw(numverts, numtriangles, elements);
670                 }
671         }
672         else
673         {
674                 //R_Mesh_TextureState(&m);
675         }
676 }
677