added TEXF_CLAMP flag for textures
[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;
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);
322 }
323
324 static void R_Shadow_MakeTextures(void)
325 {
326         int x, y, z, d, side;
327         float v[3], s, t, intensity;
328         qbyte data[6][128][128][4];
329         R_FreeTexturePool(&r_shadow_texturepool);
330         r_shadow_texturepool = R_AllocTexturePool();
331         r_shadow_atten1 = r_shadow1.value;
332         r_shadow_atten2 = r_shadow2.value;
333         r_shadow_atten5 = r_shadow5.value;
334         for (y = 0;y < 128;y++)
335         {
336                 for (x = 0;x < 128;x++)
337                 {
338                         data[0][y][x][0] = 128;
339                         data[0][y][x][1] = 128;
340                         data[0][y][x][2] = 255;
341                         data[0][y][x][3] = 255;
342                 }
343         }
344         r_shadow_blankbumptexture = R_LoadTexture(r_shadow_texturepool, "blankbump", 128, 128, &data[0][0][0][0], TEXTYPE_RGBA, TEXF_PRECACHE);
345         for (side = 0;side < 6;side++)
346         {
347                 for (y = 0;y < 128;y++)
348                 {
349                         for (x = 0;x < 128;x++)
350                         {
351                                 s = (x + 0.5f) * (2.0f / 128.0f) - 1.0f;
352                                 t = (y + 0.5f) * (2.0f / 128.0f) - 1.0f;
353                                 switch(side)
354                                 {
355                                 case 0:
356                                         v[0] = 1;
357                                         v[1] = -t;
358                                         v[2] = -s;
359                                         break;
360                                 case 1:
361                                         v[0] = -1;
362                                         v[1] = -t;
363                                         v[2] = s;
364                                         break;
365                                 case 2:
366                                         v[0] = s;
367                                         v[1] = 1;
368                                         v[2] = t;
369                                         break;
370                                 case 3:
371                                         v[0] = s;
372                                         v[1] = -1;
373                                         v[2] = -t;
374                                         break;
375                                 case 4:
376                                         v[0] = s;
377                                         v[1] = -t;
378                                         v[2] = 1;
379                                         break;
380                                 case 5:
381                                         v[0] = -s;
382                                         v[1] = -t;
383                                         v[2] = -1;
384                                         break;
385                                 }
386                                 intensity = 127.0f / sqrt(DotProduct(v, v));
387                                 data[side][y][x][0] = 128.0f + intensity * v[0];
388                                 data[side][y][x][1] = 128.0f + intensity * v[1];
389                                 data[side][y][x][2] = 128.0f + intensity * v[2];
390                                 data[side][y][x][3] = 255;
391                         }
392                 }
393         }
394         r_shadow_normalscubetexture = R_LoadTextureCubeMap(r_shadow_texturepool, "normalscube", 128, &data[0][0][0][0], TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP);
395         for (y = 0;y < 128;y++)
396         {
397                 for (x = 0;x < 128;x++)
398                 {
399                         v[0] = (x + 0.5f) * (2.0f / 128.0f) - 1.0f;
400                         v[1] = (y + 0.5f) * (2.0f / 128.0f) - 1.0f;
401                         v[2] = 0;
402                         if (DotProduct(v, v) < 1)
403                                 intensity = (((r_shadow_atten1 / (DotProduct(v, v)+r_shadow_atten5)) - (r_shadow_atten1 * r_shadow_atten2))) / 256.0f;
404                         else
405                                 intensity = 0;
406                         d = bound(0, intensity, 255) / sqrt(DotProduct(v, v));
407                         data[0][y][x][0] = d;
408                         data[0][y][x][1] = d;
409                         data[0][y][x][2] = d;
410                         data[0][y][x][3] = 255;
411                 }
412         }
413         r_shadow_attenuation2dtexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation2d", 128, 128, &data[0][0][0][0], TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP);
414         R_Shadow_Make3DTextures();
415 }
416
417 void R_Shadow_Stage_Begin(void)
418 {
419         rmeshstate_t m;
420
421         if (r_light_quality.integer == 1 && !gl_texture3d)
422         {
423                 Con_Printf("3D texture support not detected, falling back on slower 2D + 1D + normalization lighting\n");
424                 Cvar_SetValueQuick(&r_light_quality, 0);
425         }
426         //cl.worldmodel->numlights = min(cl.worldmodel->numlights, 1);
427         if (!r_shadow_attenuation2dtexture
428          || (r_light_quality.integer == 1 && !r_shadow_normalsattenuationtexture)
429          || r_shadow1.value != r_shadow_atten1
430          || r_shadow2.value != r_shadow_atten2
431          || r_shadow5.value != r_shadow_atten5)
432                 R_Shadow_MakeTextures();
433
434         memset(&m, 0, sizeof(m));
435         m.blendfunc1 = GL_ONE;
436         m.blendfunc2 = GL_ZERO;
437         R_Mesh_State(&m);
438         GL_Color(0, 0, 0, 1);
439 }
440
441 void R_Shadow_Stage_ShadowVolumes(void)
442 {
443         rmeshstate_t m;
444         memset(&m, 0, sizeof(m));
445         R_Mesh_TextureState(&m);
446         GL_Color(1, 1, 1, 1);
447         qglColorMask(0, 0, 0, 0);
448         qglDisable(GL_BLEND);
449         qglDepthMask(0);
450         qglDepthFunc(GL_LESS);
451         qglClearStencil(0);
452         qglClear(GL_STENCIL_BUFFER_BIT);
453         qglEnable(GL_STENCIL_TEST);
454         qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
455         qglStencilFunc(GL_ALWAYS, 0, 0xFF);
456 }
457
458 void R_Shadow_Stage_Light(void)
459 {
460         rmeshstate_t m;
461         memset(&m, 0, sizeof(m));
462         R_Mesh_TextureState(&m);
463         qglActiveTexture(GL_TEXTURE0_ARB);
464
465         qglEnable(GL_BLEND);
466         qglBlendFunc(GL_ONE, GL_ONE);
467         GL_Color(1, 1, 1, 1);
468         qglColorMask(1, 1, 1, 1);
469         qglDepthMask(0);
470         qglDepthFunc(GL_EQUAL);
471         qglEnable(GL_STENCIL_TEST);
472         qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
473         // only draw light where this geometry was already rendered AND the
474         // stencil is 0 (non-zero means shadow)
475         qglStencilFunc(GL_EQUAL, 0, 0xFF);
476 }
477
478 void R_Shadow_Stage_End(void)
479 {
480         rmeshstate_t m;
481         // attempt to restore state to what Mesh_State thinks it is
482         qglDisable(GL_BLEND);
483         qglBlendFunc(GL_ONE, GL_ZERO);
484         qglDepthMask(1);
485         // now restore the rest of the state to normal
486         GL_Color(1, 1, 1, 1);
487         qglColorMask(1, 1, 1, 1);
488         qglDepthFunc(GL_LEQUAL);
489         qglDisable(GL_STENCIL_TEST);
490         qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
491         qglStencilFunc(GL_ALWAYS, 0, 0xFF);
492 }
493
494 void R_Shadow_GenTexCoords_Attenuation2D(float *out, int numverts, const float *vertex, const float *svectors, const float *tvectors, const vec3_t relativelightorigin, float lightradius)
495 {
496         int i;
497         float lightvec[3], iradius;
498         iradius = 0.5f / lightradius;
499         for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, out += 4)
500         {
501                 VectorSubtract(vertex, relativelightorigin, lightvec);
502                 out[0] = 0.5f + DotProduct(svectors, lightvec) * iradius;
503                 out[1] = 0.5f + DotProduct(tvectors, lightvec) * iradius;
504         }
505 }
506
507 void R_Shadow_GenTexCoords_Attenuation1D(float *out, int numverts, const float *vertex, const float *normals, const vec3_t relativelightorigin, float lightradius)
508 {
509         int i;
510         float lightvec[3], iradius;
511         iradius = 0.5f / lightradius;
512         for (i = 0;i < numverts;i++, vertex += 4, normals += 4, out += 4)
513         {
514                 VectorSubtract(vertex, relativelightorigin, lightvec);
515                 out[0] = 0.5f + DotProduct(normals, lightvec) * iradius;
516                 out[1] = 0.5f;
517         }
518 }
519
520 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)
521 {
522         int i;
523         float lightvec[3], iradius;
524         iradius = 0.5f / lightradius;
525         for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
526         {
527                 VectorSubtract(vertex, relativelightorigin, lightvec);
528                 out[0] = 0.5f + DotProduct(svectors, lightvec) * iradius;
529                 out[1] = 0.5f + DotProduct(tvectors, lightvec) * iradius;
530                 out[2] = 0.5f + DotProduct(normals, lightvec) * iradius;
531         }
532 }
533
534 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)
535 {
536         int i;
537         float lightdir[3], iradius;
538         iradius = 0.5f / lightradius;
539         for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
540         {
541                 VectorSubtract(vertex, relativelightorigin, lightdir);
542                 // the cubemap normalizes this for us
543                 out[0] = DotProduct(svectors, lightdir);
544                 out[1] = DotProduct(tvectors, lightdir);
545                 out[2] = DotProduct(normals, lightdir);
546         }
547 }
548
549 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)
550 {
551         int i;
552         float lightdir[3], eyedir[3], halfdir[3], lightdirlen, ilen, iradius;
553         iradius = 0.5f / lightradius;
554         for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
555         {
556                 VectorSubtract(vertex, relativelightorigin, lightdir);
557                 // this is used later to make the attenuation correct
558                 lightdirlen = sqrt(DotProduct(lightdir, lightdir)) * iradius;
559                 VectorNormalizeFast(lightdir);
560                 VectorSubtract(vertex, relativeeyeorigin, eyedir);
561                 VectorNormalizeFast(eyedir);
562                 VectorAdd(lightdir, eyedir, halfdir);
563                 VectorNormalizeFast(halfdir);
564                 out[0] = 0.5f + DotProduct(svectors, halfdir) * lightdirlen;
565                 out[1] = 0.5f + DotProduct(tvectors, halfdir) * lightdirlen;
566                 out[2] = 0.5f + DotProduct(normals, halfdir) * lightdirlen;
567         }
568 }
569
570 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)
571 {
572         int i;
573         float lightdir[3], eyedir[3], halfdir[3], lightdirlen, ilen, iradius;
574         iradius = 0.5f / lightradius;
575         for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
576         {
577                 VectorSubtract(vertex, relativelightorigin, lightdir);
578                 VectorNormalizeFast(lightdir);
579                 VectorSubtract(vertex, relativeeyeorigin, eyedir);
580                 VectorNormalizeFast(eyedir);
581                 VectorAdd(lightdir, eyedir, halfdir);
582                 // the cubemap normalizes this for us
583                 out[0] = DotProduct(svectors, halfdir);
584                 out[1] = DotProduct(tvectors, halfdir);
585                 out[2] = DotProduct(normals, halfdir);
586         }
587 }
588
589 void R_Shadow_GenTexCoords_LightCubeMap(float *out, int numverts, const float *vertex, const vec3_t relativelightorigin)
590 {
591         int i;
592         for (i = 0;i < numverts;i++, vertex += 4, out += 4)
593                 VectorSubtract(vertex, relativelightorigin, out);
594 }
595
596 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)
597 {
598         float f;
599         rmeshstate_t m;
600         memset(&m, 0, sizeof(m));
601         if (!bumptexture)
602                 bumptexture = r_shadow_blankbumptexture;
603         f = 1.0f / r_shadow3.value;
604         if (r_light_quality.integer == 1)
605         {
606                 // 4 texture 3D path, two pass
607                 GL_Color(1,1,1,1);
608                 //lightcolor[0] * f, lightcolor[1] * f, lightcolor[2] * f, 1);
609                 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
610                 memcpy(varray_texcoord[2], texcoords, numverts * sizeof(float[4]));
611                 if (r_light_gloss.integer != 2)
612                 {
613                         m.tex[0] = R_GetTexture(bumptexture);
614                         m.tex3d[1] = R_GetTexture(r_shadow_normalsattenuationtexture);
615                         m.tex[2] = R_GetTexture(basetexture);
616                         m.texcubemap[3] = R_GetTexture(lightcubemap);
617                         m.texcombinergb[0] = GL_REPLACE;
618                         m.texcombinergb[1] = GL_DOT3_RGB_ARB;
619                         m.texcombinergb[2] = GL_MODULATE;
620                         m.texcombinergb[3] = GL_MODULATE;
621                         R_Mesh_TextureState(&m);
622                         R_Shadow_GenTexCoords_Diffuse_Attenuation3D(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, lightradius);
623                         if (m.texcubemap[3])
624                                 R_Shadow_GenTexCoords_LightCubeMap(varray_texcoord[3], numverts, varray_vertex, relativelightorigin);
625                         R_Mesh_Draw(numverts, numtriangles, elements);
626                 }
627                 if (r_light_gloss.integer && glosstexture)
628                 {
629                         m.tex[0] = R_GetTexture(bumptexture);
630                         m.tex3d[1] = R_GetTexture(r_shadow_normalsattenuationtexture);
631                         m.tex[2] = R_GetTexture(glosstexture);
632                         m.texcubemap[3] = R_GetTexture(lightcubemap);
633                         m.texcombinergb[0] = GL_REPLACE;
634                         m.texcombinergb[1] = GL_DOT3_RGB_ARB;
635                         m.texcombinergb[2] = GL_MODULATE;
636                         m.texcombinergb[3] = GL_MODULATE;
637                         R_Mesh_TextureState(&m);
638                         R_Shadow_GenTexCoords_Specular_Attenuation3D(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, relativeeyeorigin, lightradius);
639                         R_Mesh_Draw(numverts, numtriangles, elements);
640                 }
641         }
642         else
643         {
644                 //R_Mesh_TextureState(&m);
645         }
646 }
647