added indirection cube map for shadowmapping and revised filtering modes
[xonotic/darkplaces.git] / r_shadow.c
1
2 /*
3 Terminology: Stencil Shadow Volume (sometimes called Stencil Shadows)
4 An extrusion of the lit faces, beginning at the original geometry and ending
5 further from the light source than the original geometry (presumably at least
6 as far as the light's radius, if the light has a radius at all), capped at
7 both front and back to avoid any problems (extrusion from dark faces also
8 works but has a different set of problems)
9
10 This is normally rendered using Carmack's Reverse technique, in which
11 backfaces behind zbuffer (zfail) increment the stencil, and frontfaces behind
12 zbuffer (zfail) decrement the stencil, the result is a stencil value of zero
13 where shadows did not intersect the visible geometry, suitable as a stencil
14 mask for rendering lighting everywhere but shadow.
15
16 In our case to hopefully avoid the Creative Labs patent, we draw the backfaces
17 as decrement and the frontfaces as increment, and we redefine the DepthFunc to
18 GL_LESS (the patent uses GL_GEQUAL) which causes zfail when behind surfaces
19 and zpass when infront (the patent draws where zpass with a GL_GEQUAL test),
20 additionally we clear stencil to 128 to avoid the need for the unclamped
21 incr/decr extension (not related to patent).
22
23 Patent warning:
24 This algorithm may be covered by Creative's patent (US Patent #6384822),
25 however that patent is quite specific about increment on backfaces and
26 decrement on frontfaces where zpass with GL_GEQUAL depth test, which is
27 opposite this implementation and partially opposite Carmack's Reverse paper
28 (which uses GL_LESS, but increments on backfaces and decrements on frontfaces).
29
30
31
32 Terminology: Stencil Light Volume (sometimes called Light Volumes)
33 Similar to a Stencil Shadow Volume, but inverted; rather than containing the
34 areas in shadow it contains the areas in light, this can only be built
35 quickly for certain limited cases (such as portal visibility from a point),
36 but is quite useful for some effects (sunlight coming from sky polygons is
37 one possible example, translucent occluders is another example).
38
39
40
41 Terminology: Optimized Stencil Shadow Volume
42 A Stencil Shadow Volume that has been processed sufficiently to ensure it has
43 no duplicate coverage of areas (no need to shadow an area twice), often this
44 greatly improves performance but is an operation too costly to use on moving
45 lights (however completely optimal Stencil Light Volumes can be constructed
46 in some ideal cases).
47
48
49
50 Terminology: Per Pixel Lighting (sometimes abbreviated PPL)
51 Per pixel evaluation of lighting equations, at a bare minimum this involves
52 DOT3 shading of diffuse lighting (per pixel dotproduct of negated incidence
53 vector and surface normal, using a texture of the surface bumps, called a
54 NormalMap) if supported by hardware; in our case there is support for cards
55 which are incapable of DOT3, the quality is quite poor however.  Additionally
56 it is desirable to have specular evaluation per pixel, per vertex
57 normalization of specular halfangle vectors causes noticable distortion but
58 is unavoidable on hardware without GL_ARB_fragment_program or
59 GL_ARB_fragment_shader.
60
61
62
63 Terminology: Normalization CubeMap
64 A cubemap containing normalized dot3-encoded (vectors of length 1 or less
65 encoded as RGB colors) for any possible direction, this technique allows per
66 pixel calculation of incidence vector for per pixel lighting purposes, which
67 would not otherwise be possible per pixel without GL_ARB_fragment_program or
68 GL_ARB_fragment_shader.
69
70
71
72 Terminology: 2D+1D Attenuation Texturing
73 A very crude approximation of light attenuation with distance which results
74 in cylindrical light shapes which fade vertically as a streak (some games
75 such as Doom3 allow this to be rotated to be less noticable in specific
76 cases), the technique is simply modulating lighting by two 2D textures (which
77 can be the same) on different axes of projection (XY and Z, typically), this
78 is the second best technique available without 3D Attenuation Texturing,
79 GL_ARB_fragment_program or GL_ARB_fragment_shader technology.
80
81
82
83 Terminology: 2D+1D Inverse Attenuation Texturing
84 A clever method described in papers on the Abducted engine, this has a squared
85 distance texture (bright on the outside, black in the middle), which is used
86 twice using GL_ADD blending, the result of this is used in an inverse modulate
87 (GL_ONE_MINUS_DST_ALPHA, GL_ZERO) to implement the equation
88 lighting*=(1-((X*X+Y*Y)+(Z*Z))) which is spherical (unlike 2D+1D attenuation
89 texturing).
90
91
92
93 Terminology: 3D Attenuation Texturing
94 A slightly crude approximation of light attenuation with distance, its flaws
95 are limited radius and resolution (performance tradeoffs).
96
97
98
99 Terminology: 3D Attenuation-Normalization Texturing
100 A 3D Attenuation Texture merged with a Normalization CubeMap, by making the
101 vectors shorter the lighting becomes darker, a very effective optimization of
102 diffuse lighting if 3D Attenuation Textures are already used.
103
104
105
106 Terminology: Light Cubemap Filtering
107 A technique for modeling non-uniform light distribution according to
108 direction, for example a lantern may use a cubemap to describe the light
109 emission pattern of the cage around the lantern (as well as soot buildup
110 discoloring the light in certain areas), often also used for softened grate
111 shadows and light shining through a stained glass window (done crudely by
112 texturing the lighting with a cubemap), another good example would be a disco
113 light.  This technique is used heavily in many games (Doom3 does not support
114 this however).
115
116
117
118 Terminology: Light Projection Filtering
119 A technique for modeling shadowing of light passing through translucent
120 surfaces, allowing stained glass windows and other effects to be done more
121 elegantly than possible with Light Cubemap Filtering by applying an occluder
122 texture to the lighting combined with a stencil light volume to limit the lit
123 area, this technique is used by Doom3 for spotlights and flashlights, among
124 other things, this can also be used more generally to render light passing
125 through multiple translucent occluders in a scene (using a light volume to
126 describe the area beyond the occluder, and thus mask off rendering of all
127 other areas).
128
129
130
131 Terminology: Doom3 Lighting
132 A combination of Stencil Shadow Volume, Per Pixel Lighting, Normalization
133 CubeMap, 2D+1D Attenuation Texturing, and Light Projection Filtering, as
134 demonstrated by the game Doom3.
135 */
136
137 #include "quakedef.h"
138 #include "r_shadow.h"
139 #include "cl_collision.h"
140 #include "portals.h"
141 #include "image.h"
142
143 #define R_SHADOW_SHADOWMAP_NUMCUBEMAPS 8
144
145 extern void R_Shadow_EditLights_Init(void);
146
147 typedef enum r_shadow_rendermode_e
148 {
149         R_SHADOW_RENDERMODE_NONE,
150         R_SHADOW_RENDERMODE_ZPASS_STENCIL,
151         R_SHADOW_RENDERMODE_ZPASS_SEPARATESTENCIL,
152         R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE,
153         R_SHADOW_RENDERMODE_ZFAIL_STENCIL,
154         R_SHADOW_RENDERMODE_ZFAIL_SEPARATESTENCIL,
155         R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE,
156         R_SHADOW_RENDERMODE_LIGHT_VERTEX,
157         R_SHADOW_RENDERMODE_LIGHT_DOT3,
158         R_SHADOW_RENDERMODE_LIGHT_GLSL,
159         R_SHADOW_RENDERMODE_VISIBLEVOLUMES,
160         R_SHADOW_RENDERMODE_VISIBLELIGHTING,
161         R_SHADOW_RENDERMODE_SHADOWMAPRECTANGLE,
162         R_SHADOW_RENDERMODE_SHADOWMAP2D,
163         R_SHADOW_RENDERMODE_SHADOWMAPCUBESIDE,
164 }
165 r_shadow_rendermode_t;
166
167 r_shadow_rendermode_t r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
168 r_shadow_rendermode_t r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_NONE;
169 r_shadow_rendermode_t r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_NONE;
170 r_shadow_rendermode_t r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_NONE;
171 float r_shadow_shadowmap_texturescale[4];
172 float r_shadow_shadowmap_parameters[4];
173 int r_shadow_drawbuffer;
174 int r_shadow_readbuffer;
175 GLuint r_shadow_fborectangle;
176 int r_shadow_shadowmode;
177 int r_shadow_shadowmapmaxsize;
178 int r_shadow_shadowmapfilter;
179 int r_shadow_shadowmapborder;
180 int r_shadow_lightscissor[4];
181
182 int maxshadowtriangles;
183 int *shadowelements;
184
185 int maxshadowvertices;
186 float *shadowvertex3f;
187
188 int maxshadowmark;
189 int numshadowmark;
190 int *shadowmark;
191 int *shadowmarklist;
192 int shadowmarkcount;
193
194 int maxvertexupdate;
195 int *vertexupdate;
196 int *vertexremap;
197 int vertexupdatenum;
198
199 int r_shadow_buffer_numleafpvsbytes;
200 unsigned char *r_shadow_buffer_visitingleafpvs;
201 unsigned char *r_shadow_buffer_leafpvs;
202 int *r_shadow_buffer_leaflist;
203
204 int r_shadow_buffer_numsurfacepvsbytes;
205 unsigned char *r_shadow_buffer_surfacepvs;
206 int *r_shadow_buffer_surfacelist;
207
208 int r_shadow_buffer_numshadowtrispvsbytes;
209 unsigned char *r_shadow_buffer_shadowtrispvs;
210 int r_shadow_buffer_numlighttrispvsbytes;
211 unsigned char *r_shadow_buffer_lighttrispvs;
212
213 rtexturepool_t *r_shadow_texturepool;
214 rtexture_t *r_shadow_attenuationgradienttexture;
215 rtexture_t *r_shadow_attenuation2dtexture;
216 rtexture_t *r_shadow_attenuation3dtexture;
217 rtexture_t *r_shadow_lightcorona;
218 rtexture_t *r_shadow_shadowmaprectangletexture;
219 rtexture_t *r_shadow_shadowmapcubeprojectiontexture[R_SHADOW_SHADOWMAP_NUMCUBEMAPS];
220 int r_shadow_shadowmapsize; // changes for each light based on distance
221 int r_shadow_shadowmaplod; // changes for each light based on distance
222
223 // lights are reloaded when this changes
224 char r_shadow_mapname[MAX_QPATH];
225
226 // used only for light filters (cubemaps)
227 rtexturepool_t *r_shadow_filters_texturepool;
228
229 cvar_t r_shadow_bumpscale_basetexture = {0, "r_shadow_bumpscale_basetexture", "0", "generate fake bumpmaps from diffuse textures at this bumpyness, try 4 to match tenebrae, higher values increase depth, requires r_restart to take effect"};
230 cvar_t r_shadow_bumpscale_bumpmap = {0, "r_shadow_bumpscale_bumpmap", "4", "what magnitude to interpret _bump.tga textures as, higher values increase depth, requires r_restart to take effect"};
231 cvar_t r_shadow_debuglight = {0, "r_shadow_debuglight", "-1", "renders only one light, for level design purposes or debugging"};
232 cvar_t r_shadow_usenormalmap = {CVAR_SAVE, "r_shadow_usenormalmap", "1", "enables use of directional shading on lights"};
233 cvar_t r_shadow_gloss = {CVAR_SAVE, "r_shadow_gloss", "1", "0 disables gloss (specularity) rendering, 1 uses gloss if textures are found, 2 forces a flat metallic specular effect on everything without textures (similar to tenebrae)"};
234 cvar_t r_shadow_gloss2intensity = {0, "r_shadow_gloss2intensity", "0.125", "how bright the forced flat gloss should look if r_shadow_gloss is 2"};
235 cvar_t r_shadow_glossintensity = {0, "r_shadow_glossintensity", "1", "how bright textured glossmaps should look if r_shadow_gloss is 1 or 2"};
236 cvar_t r_shadow_glossexponent = {0, "r_shadow_glossexponent", "32", "how 'sharp' the gloss should appear (specular power)"};
237 cvar_t r_shadow_glossexact = {0, "r_shadow_glossexact", "0", "use exact reflection math for gloss (slightly slower, but should look a tad better)"};
238 cvar_t r_shadow_lightattenuationdividebias = {0, "r_shadow_lightattenuationdividebias", "1", "changes attenuation texture generation"};
239 cvar_t r_shadow_lightattenuationlinearscale = {0, "r_shadow_lightattenuationlinearscale", "2", "changes attenuation texture generation"};
240 cvar_t r_shadow_lightintensityscale = {0, "r_shadow_lightintensityscale", "1", "renders all world lights brighter or darker"};
241 cvar_t r_shadow_lightradiusscale = {0, "r_shadow_lightradiusscale", "1", "renders all world lights larger or smaller"};
242 cvar_t r_shadow_portallight = {0, "r_shadow_portallight", "1", "use portal culling to exactly determine lit triangles when compiling world lights"};
243 cvar_t r_shadow_projectdistance = {0, "r_shadow_projectdistance", "1000000", "how far to cast shadows"};
244 cvar_t r_shadow_frontsidecasting = {0, "r_shadow_frontsidecasting", "1", "whether to cast shadows from illuminated triangles (front side of model) or unlit triangles (back side of model)"};
245 cvar_t r_shadow_realtime_dlight = {CVAR_SAVE, "r_shadow_realtime_dlight", "1", "enables rendering of dynamic lights such as explosions and rocket light"};
246 cvar_t r_shadow_realtime_dlight_shadows = {CVAR_SAVE, "r_shadow_realtime_dlight_shadows", "1", "enables rendering of shadows from dynamic lights"};
247 cvar_t r_shadow_realtime_dlight_svbspculling = {0, "r_shadow_realtime_dlight_svbspculling", "0", "enables svbsp optimization on dynamic lights (very slow!)"};
248 cvar_t r_shadow_realtime_dlight_portalculling = {0, "r_shadow_realtime_dlight_portalculling", "0", "enables portal optimization on dynamic lights (slow!)"};
249 cvar_t r_shadow_realtime_world = {CVAR_SAVE, "r_shadow_realtime_world", "0", "enables rendering of full world lighting (whether loaded from the map, or a .rtlights file, or a .ent file, or a .lights file produced by hlight)"};
250 cvar_t r_shadow_realtime_world_lightmaps = {CVAR_SAVE, "r_shadow_realtime_world_lightmaps", "0", "brightness to render lightmaps when using full world lighting, try 0.5 for a tenebrae-like appearance"};
251 cvar_t r_shadow_realtime_world_shadows = {CVAR_SAVE, "r_shadow_realtime_world_shadows", "1", "enables rendering of shadows from world lights"};
252 cvar_t r_shadow_realtime_world_compile = {0, "r_shadow_realtime_world_compile", "1", "enables compilation of world lights for higher performance rendering"};
253 cvar_t r_shadow_realtime_world_compileshadow = {0, "r_shadow_realtime_world_compileshadow", "1", "enables compilation of shadows from world lights for higher performance rendering"};
254 cvar_t r_shadow_realtime_world_compilesvbsp = {0, "r_shadow_realtime_world_compilesvbsp", "1", "enables svbsp optimization during compilation"};
255 cvar_t r_shadow_realtime_world_compileportalculling = {0, "r_shadow_realtime_world_compileportalculling", "1", "enables portal-based culling optimization during compilation"};
256 cvar_t r_shadow_scissor = {0, "r_shadow_scissor", "1", "use scissor optimization of light rendering (restricts rendering to the portion of the screen affected by the light)"};
257 cvar_t r_shadow_shadowmapping = {CVAR_SAVE, "r_shadow_shadowmapping", "0", "enables use of shadowmapping (depth texture sampling) instead of stencil shadow volumes, requires gl_fbo 1"};
258 cvar_t r_shadow_shadowmapping_filterquality = {CVAR_SAVE, "r_shadow_shadowmapping_filterquality", "0", "shadowmap filter modes: 0 = no filtering, 1 = bilinear, 2 = bilinear small blur (fast), 3 = bilinear large blur (slow)"};
259 cvar_t r_shadow_shadowmapping_minsize = {CVAR_SAVE, "r_shadow_shadowmapping_minsize", "32", "shadowmap size limit"};
260 cvar_t r_shadow_shadowmapping_maxsize = {CVAR_SAVE, "r_shadow_shadowmapping_maxsize", "512", "shadowmap size limit"};
261 cvar_t r_shadow_shadowmapping_lod_bias = {CVAR_SAVE, "r_shadow_shadowmapping_lod_bias", "16", "shadowmap size bias"};
262 cvar_t r_shadow_shadowmapping_lod_scale = {CVAR_SAVE, "r_shadow_shadowmapping_lod_scale", "128", "shadowmap size scaling parameter"};
263 cvar_t r_shadow_shadowmapping_bordersize = {CVAR_SAVE, "r_shadow_shadowmapping_bordersize", "4", "shadowmap size bias for filtering"};
264 cvar_t r_shadow_shadowmapping_nearclip = {CVAR_SAVE, "r_shadow_shadowmapping_nearclip", "1", "shadowmap nearclip in world units"};
265 cvar_t r_shadow_shadowmapping_bias = {CVAR_SAVE, "r_shadow_shadowmapping_bias", "0.03", "shadowmap bias parameter (this is multiplied by nearclip * 1024 / lodsize)"};
266 cvar_t r_shadow_culltriangles = {0, "r_shadow_culltriangles", "1", "performs more expensive tests to remove unnecessary triangles of lit surfaces"};
267 cvar_t r_shadow_polygonfactor = {0, "r_shadow_polygonfactor", "0", "how much to enlarge shadow volume polygons when rendering (should be 0!)"};
268 cvar_t r_shadow_polygonoffset = {0, "r_shadow_polygonoffset", "1", "how much to push shadow volumes into the distance when rendering, to reduce chances of zfighting artifacts (should not be less than 0)"};
269 cvar_t r_shadow_texture3d = {0, "r_shadow_texture3d", "1", "use 3D voxel textures for spherical attenuation rather than cylindrical (does not affect r_glsl lighting)"};
270 cvar_t r_coronas = {CVAR_SAVE, "r_coronas", "1", "brightness of corona flare effects around certain lights, 0 disables corona effects"};
271 cvar_t r_coronas_occlusionsizescale = {CVAR_SAVE, "r_coronas_occlusionsizescale", "0.1", "size of light source for corona occlusion checksm the proportion of hidden pixels controls corona intensity"};
272 cvar_t r_coronas_occlusionquery = {CVAR_SAVE, "r_coronas_occlusionquery", "1", "use GL_ARB_occlusion_query extension if supported (fades coronas according to visibility)"};
273 cvar_t gl_flashblend = {CVAR_SAVE, "gl_flashblend", "0", "render bright coronas for dynamic lights instead of actual lighting, fast but ugly"};
274 cvar_t gl_ext_separatestencil = {0, "gl_ext_separatestencil", "1", "make use of OpenGL 2.0 glStencilOpSeparate or GL_ATI_separate_stencil extension"};
275 cvar_t gl_ext_stenciltwoside = {0, "gl_ext_stenciltwoside", "1", "make use of GL_EXT_stenciltwoside extension (NVIDIA only)"};
276 cvar_t r_editlights = {0, "r_editlights", "0", "enables .rtlights file editing mode"};
277 cvar_t r_editlights_cursordistance = {0, "r_editlights_cursordistance", "1024", "maximum distance of cursor from eye"};
278 cvar_t r_editlights_cursorpushback = {0, "r_editlights_cursorpushback", "0", "how far to pull the cursor back toward the eye"};
279 cvar_t r_editlights_cursorpushoff = {0, "r_editlights_cursorpushoff", "4", "how far to push the cursor off the impacted surface"};
280 cvar_t r_editlights_cursorgrid = {0, "r_editlights_cursorgrid", "4", "snaps cursor to this grid size"};
281 cvar_t r_editlights_quakelightsizescale = {CVAR_SAVE, "r_editlights_quakelightsizescale", "1", "changes size of light entities loaded from a map"};
282
283 // note the table actually includes one more value, just to avoid the need to clamp the distance index due to minor math error
284 #define ATTENTABLESIZE 256
285 // 1D gradient, 2D circle and 3D sphere attenuation textures
286 #define ATTEN1DSIZE 32
287 #define ATTEN2DSIZE 64
288 #define ATTEN3DSIZE 32
289
290 static float r_shadow_attendividebias; // r_shadow_lightattenuationdividebias
291 static float r_shadow_attenlinearscale; // r_shadow_lightattenuationlinearscale
292 static float r_shadow_attentable[ATTENTABLESIZE+1];
293
294 rtlight_t *r_shadow_compilingrtlight;
295 static memexpandablearray_t r_shadow_worldlightsarray;
296 dlight_t *r_shadow_selectedlight;
297 dlight_t r_shadow_bufferlight;
298 vec3_t r_editlights_cursorlocation;
299
300 extern int con_vislines;
301
302 typedef struct cubemapinfo_s
303 {
304         char basename[64];
305         rtexture_t *texture;
306 }
307 cubemapinfo_t;
308
309 #define MAX_CUBEMAPS 256
310 static int numcubemaps;
311 static cubemapinfo_t cubemaps[MAX_CUBEMAPS];
312
313 void R_Shadow_UncompileWorldLights(void);
314 void R_Shadow_ClearWorldLights(void);
315 void R_Shadow_SaveWorldLights(void);
316 void R_Shadow_LoadWorldLights(void);
317 void R_Shadow_LoadLightsFile(void);
318 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void);
319 void R_Shadow_EditLights_Reload_f(void);
320 void R_Shadow_ValidateCvars(void);
321 static void R_Shadow_MakeTextures(void);
322
323 // VorteX: custom editor light sprites
324 #define EDLIGHTSPRSIZE                  8
325 cachepic_t *r_editlights_sprcursor;
326 cachepic_t *r_editlights_sprlight;
327 cachepic_t *r_editlights_sprnoshadowlight;
328 cachepic_t *r_editlights_sprcubemaplight;
329 cachepic_t *r_editlights_sprcubemapnoshadowlight;
330 cachepic_t *r_editlights_sprselection;
331
332 void R_Shadow_FreeShadowMaps(void)
333 {
334         int i;
335
336         r_shadow_shadowmapmaxsize = bound(1, r_shadow_shadowmapping_maxsize.integer, 2048);
337         r_shadow_shadowmode = r_shadow_shadowmapping.integer;
338     r_shadow_shadowmapfilter = r_shadow_shadowmapping_filterquality.integer;
339     r_shadow_shadowmapborder = bound(0, r_shadow_shadowmapping_bordersize.integer, 16);
340         r_shadow_shadowmaplod = -1;
341
342         CHECKGLERROR
343         if (r_shadow_fborectangle)
344                 qglDeleteFramebuffersEXT(1, &r_shadow_fborectangle);
345         r_shadow_fborectangle = 0;
346         CHECKGLERROR
347
348         if (r_shadow_shadowmaprectangletexture)
349                 R_FreeTexture(r_shadow_shadowmaprectangletexture);
350         r_shadow_shadowmaprectangletexture = NULL;
351
352     for (i = 0;i < R_SHADOW_SHADOWMAP_NUMCUBEMAPS;i++)
353         if (r_shadow_shadowmapcubeprojectiontexture[i])
354             R_FreeTexture(r_shadow_shadowmapcubeprojectiontexture[i]);
355     memset(r_shadow_shadowmapcubeprojectiontexture, 0, sizeof(r_shadow_shadowmapcubeprojectiontexture));
356
357         CHECKGLERROR
358 }
359
360 void r_shadow_start(void)
361 {
362         // allocate vertex processing arrays
363         numcubemaps = 0;
364         r_shadow_attenuationgradienttexture = NULL;
365         r_shadow_attenuation2dtexture = NULL;
366         r_shadow_attenuation3dtexture = NULL;
367         r_shadow_shadowmode = 0;
368         r_shadow_shadowmaprectangletexture = NULL;
369     memset(r_shadow_shadowmapcubeprojectiontexture, 0, sizeof(r_shadow_shadowmapcubeprojectiontexture));
370         r_shadow_shadowmapmaxsize = 0;
371         r_shadow_shadowmapsize = 0;
372         r_shadow_shadowmaplod = 0;
373     r_shadow_shadowmapfilter = 0;
374         r_shadow_fborectangle = 0;
375
376         R_Shadow_FreeShadowMaps();
377
378         r_shadow_texturepool = NULL;
379         r_shadow_filters_texturepool = NULL;
380         R_Shadow_ValidateCvars();
381         R_Shadow_MakeTextures();
382         maxshadowtriangles = 0;
383         shadowelements = NULL;
384         maxshadowvertices = 0;
385         shadowvertex3f = NULL;
386         maxvertexupdate = 0;
387         vertexupdate = NULL;
388         vertexremap = NULL;
389         vertexupdatenum = 0;
390         maxshadowmark = 0;
391         numshadowmark = 0;
392         shadowmark = NULL;
393         shadowmarklist = NULL;
394         shadowmarkcount = 0;
395         r_shadow_buffer_numleafpvsbytes = 0;
396         r_shadow_buffer_visitingleafpvs = NULL;
397         r_shadow_buffer_leafpvs = NULL;
398         r_shadow_buffer_leaflist = NULL;
399         r_shadow_buffer_numsurfacepvsbytes = 0;
400         r_shadow_buffer_surfacepvs = NULL;
401         r_shadow_buffer_surfacelist = NULL;
402         r_shadow_buffer_numshadowtrispvsbytes = 0;
403         r_shadow_buffer_shadowtrispvs = NULL;
404         r_shadow_buffer_numlighttrispvsbytes = 0;
405         r_shadow_buffer_lighttrispvs = NULL;
406 }
407
408 void r_shadow_shutdown(void)
409 {
410         CHECKGLERROR
411         R_Shadow_UncompileWorldLights();
412
413         R_Shadow_FreeShadowMaps();
414
415         CHECKGLERROR
416         numcubemaps = 0;
417         r_shadow_attenuationgradienttexture = NULL;
418         r_shadow_attenuation2dtexture = NULL;
419         r_shadow_attenuation3dtexture = NULL;
420         R_FreeTexturePool(&r_shadow_texturepool);
421         R_FreeTexturePool(&r_shadow_filters_texturepool);
422         maxshadowtriangles = 0;
423         if (shadowelements)
424                 Mem_Free(shadowelements);
425         shadowelements = NULL;
426         if (shadowvertex3f)
427                 Mem_Free(shadowvertex3f);
428         shadowvertex3f = NULL;
429         maxvertexupdate = 0;
430         if (vertexupdate)
431                 Mem_Free(vertexupdate);
432         vertexupdate = NULL;
433         if (vertexremap)
434                 Mem_Free(vertexremap);
435         vertexremap = NULL;
436         vertexupdatenum = 0;
437         maxshadowmark = 0;
438         numshadowmark = 0;
439         if (shadowmark)
440                 Mem_Free(shadowmark);
441         shadowmark = NULL;
442         if (shadowmarklist)
443                 Mem_Free(shadowmarklist);
444         shadowmarklist = NULL;
445         shadowmarkcount = 0;
446         r_shadow_buffer_numleafpvsbytes = 0;
447         if (r_shadow_buffer_visitingleafpvs)
448                 Mem_Free(r_shadow_buffer_visitingleafpvs);
449         r_shadow_buffer_visitingleafpvs = NULL;
450         if (r_shadow_buffer_leafpvs)
451                 Mem_Free(r_shadow_buffer_leafpvs);
452         r_shadow_buffer_leafpvs = NULL;
453         if (r_shadow_buffer_leaflist)
454                 Mem_Free(r_shadow_buffer_leaflist);
455         r_shadow_buffer_leaflist = NULL;
456         r_shadow_buffer_numsurfacepvsbytes = 0;
457         if (r_shadow_buffer_surfacepvs)
458                 Mem_Free(r_shadow_buffer_surfacepvs);
459         r_shadow_buffer_surfacepvs = NULL;
460         if (r_shadow_buffer_surfacelist)
461                 Mem_Free(r_shadow_buffer_surfacelist);
462         r_shadow_buffer_surfacelist = NULL;
463         r_shadow_buffer_numshadowtrispvsbytes = 0;
464         if (r_shadow_buffer_shadowtrispvs)
465                 Mem_Free(r_shadow_buffer_shadowtrispvs);
466         r_shadow_buffer_numlighttrispvsbytes = 0;
467         if (r_shadow_buffer_lighttrispvs)
468                 Mem_Free(r_shadow_buffer_lighttrispvs);
469 }
470
471 void r_shadow_newmap(void)
472 {
473         if (cl.worldmodel && strncmp(cl.worldmodel->name, r_shadow_mapname, sizeof(r_shadow_mapname)))
474                 R_Shadow_EditLights_Reload_f();
475 }
476
477 void R_Shadow_Help_f(void)
478 {
479         Con_Printf(
480 "Documentation on r_shadow system:\n"
481 "Settings:\n"
482 "r_shadow_bumpscale_basetexture : base texture as bumpmap with this scale\n"
483 "r_shadow_bumpscale_bumpmap : depth scale for bumpmap conversion\n"
484 "r_shadow_debuglight : render only this light number (-1 = all)\n"
485 "r_shadow_gloss 0/1/2 : no gloss, gloss textures only, force gloss\n"
486 "r_shadow_gloss2intensity : brightness of forced gloss\n"
487 "r_shadow_glossintensity : brightness of textured gloss\n"
488 "r_shadow_lightattenuationlinearscale : used to generate attenuation texture\n"
489 "r_shadow_lightattenuationdividebias : used to generate attenuation texture\n"
490 "r_shadow_lightintensityscale : scale rendering brightness of all lights\n"
491 "r_shadow_lightradiusscale : scale rendering radius of all lights\n"
492 "r_shadow_portallight : use portal visibility for static light precomputation\n"
493 "r_shadow_projectdistance : shadow volume projection distance\n"
494 "r_shadow_realtime_dlight : use high quality dynamic lights in normal mode\n"
495 "r_shadow_realtime_dlight_shadows : cast shadows from dlights\n"
496 "r_shadow_realtime_world : use high quality world lighting mode\n"
497 "r_shadow_realtime_world_lightmaps : use lightmaps in addition to lights\n"
498 "r_shadow_realtime_world_shadows : cast shadows from world lights\n"
499 "r_shadow_realtime_world_compile : compile surface/visibility information\n"
500 "r_shadow_realtime_world_compileshadow : compile shadow geometry\n"
501 "r_shadow_scissor : use scissor optimization\n"
502 "r_shadow_polygonfactor : nudge shadow volumes closer/further\n"
503 "r_shadow_polygonoffset : nudge shadow volumes closer/further\n"
504 "r_shadow_texture3d : use 3d attenuation texture (if hardware supports)\n"
505 "r_showlighting : useful for performance testing; bright = slow!\n"
506 "r_showshadowvolumes : useful for performance testing; bright = slow!\n"
507 "Commands:\n"
508 "r_shadow_help : this help\n"
509         );
510 }
511
512 void R_Shadow_Init(void)
513 {
514         Cvar_RegisterVariable(&r_shadow_bumpscale_basetexture);
515         Cvar_RegisterVariable(&r_shadow_bumpscale_bumpmap);
516         Cvar_RegisterVariable(&r_shadow_usenormalmap);
517         Cvar_RegisterVariable(&r_shadow_debuglight);
518         Cvar_RegisterVariable(&r_shadow_gloss);
519         Cvar_RegisterVariable(&r_shadow_gloss2intensity);
520         Cvar_RegisterVariable(&r_shadow_glossintensity);
521         Cvar_RegisterVariable(&r_shadow_glossexponent);
522         Cvar_RegisterVariable(&r_shadow_glossexact);
523         Cvar_RegisterVariable(&r_shadow_lightattenuationdividebias);
524         Cvar_RegisterVariable(&r_shadow_lightattenuationlinearscale);
525         Cvar_RegisterVariable(&r_shadow_lightintensityscale);
526         Cvar_RegisterVariable(&r_shadow_lightradiusscale);
527         Cvar_RegisterVariable(&r_shadow_portallight);
528         Cvar_RegisterVariable(&r_shadow_projectdistance);
529         Cvar_RegisterVariable(&r_shadow_frontsidecasting);
530         Cvar_RegisterVariable(&r_shadow_realtime_dlight);
531         Cvar_RegisterVariable(&r_shadow_realtime_dlight_shadows);
532         Cvar_RegisterVariable(&r_shadow_realtime_dlight_svbspculling);
533         Cvar_RegisterVariable(&r_shadow_realtime_dlight_portalculling);
534         Cvar_RegisterVariable(&r_shadow_realtime_world);
535         Cvar_RegisterVariable(&r_shadow_realtime_world_lightmaps);
536         Cvar_RegisterVariable(&r_shadow_realtime_world_shadows);
537         Cvar_RegisterVariable(&r_shadow_realtime_world_compile);
538         Cvar_RegisterVariable(&r_shadow_realtime_world_compileshadow);
539         Cvar_RegisterVariable(&r_shadow_realtime_world_compilesvbsp);
540         Cvar_RegisterVariable(&r_shadow_realtime_world_compileportalculling);
541         Cvar_RegisterVariable(&r_shadow_scissor);
542         Cvar_RegisterVariable(&r_shadow_shadowmapping);
543         Cvar_RegisterVariable(&r_shadow_shadowmapping_filterquality);
544         Cvar_RegisterVariable(&r_shadow_shadowmapping_maxsize);
545         Cvar_RegisterVariable(&r_shadow_shadowmapping_minsize);
546         Cvar_RegisterVariable(&r_shadow_shadowmapping_lod_bias);
547         Cvar_RegisterVariable(&r_shadow_shadowmapping_lod_scale);
548         Cvar_RegisterVariable(&r_shadow_shadowmapping_bordersize);
549         Cvar_RegisterVariable(&r_shadow_shadowmapping_nearclip);
550         Cvar_RegisterVariable(&r_shadow_shadowmapping_bias);
551         Cvar_RegisterVariable(&r_shadow_culltriangles);
552         Cvar_RegisterVariable(&r_shadow_polygonfactor);
553         Cvar_RegisterVariable(&r_shadow_polygonoffset);
554         Cvar_RegisterVariable(&r_shadow_texture3d);
555         Cvar_RegisterVariable(&r_coronas);
556         Cvar_RegisterVariable(&r_coronas_occlusionsizescale);
557         Cvar_RegisterVariable(&r_coronas_occlusionquery);
558         Cvar_RegisterVariable(&gl_flashblend);
559         Cvar_RegisterVariable(&gl_ext_separatestencil);
560         Cvar_RegisterVariable(&gl_ext_stenciltwoside);
561         if (gamemode == GAME_TENEBRAE)
562         {
563                 Cvar_SetValue("r_shadow_gloss", 2);
564                 Cvar_SetValue("r_shadow_bumpscale_basetexture", 4);
565         }
566         Cmd_AddCommand("r_shadow_help", R_Shadow_Help_f, "prints documentation on console commands and variables used by realtime lighting and shadowing system");
567         R_Shadow_EditLights_Init();
568         Mem_ExpandableArray_NewArray(&r_shadow_worldlightsarray, r_main_mempool, sizeof(dlight_t), 128);
569         maxshadowtriangles = 0;
570         shadowelements = NULL;
571         maxshadowvertices = 0;
572         shadowvertex3f = NULL;
573         maxvertexupdate = 0;
574         vertexupdate = NULL;
575         vertexremap = NULL;
576         vertexupdatenum = 0;
577         maxshadowmark = 0;
578         numshadowmark = 0;
579         shadowmark = NULL;
580         shadowmarklist = NULL;
581         shadowmarkcount = 0;
582         r_shadow_buffer_numleafpvsbytes = 0;
583         r_shadow_buffer_visitingleafpvs = NULL;
584         r_shadow_buffer_leafpvs = NULL;
585         r_shadow_buffer_leaflist = NULL;
586         r_shadow_buffer_numsurfacepvsbytes = 0;
587         r_shadow_buffer_surfacepvs = NULL;
588         r_shadow_buffer_surfacelist = NULL;
589         r_shadow_buffer_shadowtrispvs = NULL;
590         r_shadow_buffer_lighttrispvs = NULL;
591         R_RegisterModule("R_Shadow", r_shadow_start, r_shadow_shutdown, r_shadow_newmap);
592 }
593
594 matrix4x4_t matrix_attenuationxyz =
595 {
596         {
597                 {0.5, 0.0, 0.0, 0.5},
598                 {0.0, 0.5, 0.0, 0.5},
599                 {0.0, 0.0, 0.5, 0.5},
600                 {0.0, 0.0, 0.0, 1.0}
601         }
602 };
603
604 matrix4x4_t matrix_attenuationz =
605 {
606         {
607                 {0.0, 0.0, 0.5, 0.5},
608                 {0.0, 0.0, 0.0, 0.5},
609                 {0.0, 0.0, 0.0, 0.5},
610                 {0.0, 0.0, 0.0, 1.0}
611         }
612 };
613
614 void R_Shadow_ResizeShadowArrays(int numvertices, int numtriangles)
615 {
616         // make sure shadowelements is big enough for this volume
617         if (maxshadowtriangles < numtriangles)
618         {
619                 maxshadowtriangles = numtriangles;
620                 if (shadowelements)
621                         Mem_Free(shadowelements);
622                 shadowelements = (int *)Mem_Alloc(r_main_mempool, maxshadowtriangles * sizeof(int[24]));
623         }
624         // make sure shadowvertex3f is big enough for this volume
625         if (maxshadowvertices < numvertices)
626         {
627                 maxshadowvertices = numvertices;
628                 if (shadowvertex3f)
629                         Mem_Free(shadowvertex3f);
630                 shadowvertex3f = (float *)Mem_Alloc(r_main_mempool, maxshadowvertices * sizeof(float[6]));
631         }
632 }
633
634 static void R_Shadow_EnlargeLeafSurfaceTrisBuffer(int numleafs, int numsurfaces, int numshadowtriangles, int numlighttriangles)
635 {
636         int numleafpvsbytes = (((numleafs + 7) >> 3) + 255) & ~255;
637         int numsurfacepvsbytes = (((numsurfaces + 7) >> 3) + 255) & ~255;
638         int numshadowtrispvsbytes = (((numshadowtriangles + 7) >> 3) + 255) & ~255;
639         int numlighttrispvsbytes = (((numlighttriangles + 7) >> 3) + 255) & ~255;
640         if (r_shadow_buffer_numleafpvsbytes < numleafpvsbytes)
641         {
642                 if (r_shadow_buffer_visitingleafpvs)
643                         Mem_Free(r_shadow_buffer_visitingleafpvs);
644                 if (r_shadow_buffer_leafpvs)
645                         Mem_Free(r_shadow_buffer_leafpvs);
646                 if (r_shadow_buffer_leaflist)
647                         Mem_Free(r_shadow_buffer_leaflist);
648                 r_shadow_buffer_numleafpvsbytes = numleafpvsbytes;
649                 r_shadow_buffer_visitingleafpvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numleafpvsbytes);
650                 r_shadow_buffer_leafpvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numleafpvsbytes);
651                 r_shadow_buffer_leaflist = (int *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numleafpvsbytes * 8 * sizeof(*r_shadow_buffer_leaflist));
652         }
653         if (r_shadow_buffer_numsurfacepvsbytes < numsurfacepvsbytes)
654         {
655                 if (r_shadow_buffer_surfacepvs)
656                         Mem_Free(r_shadow_buffer_surfacepvs);
657                 if (r_shadow_buffer_surfacelist)
658                         Mem_Free(r_shadow_buffer_surfacelist);
659                 r_shadow_buffer_numsurfacepvsbytes = numsurfacepvsbytes;
660                 r_shadow_buffer_surfacepvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numsurfacepvsbytes);
661                 r_shadow_buffer_surfacelist = (int *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numsurfacepvsbytes * 8 * sizeof(*r_shadow_buffer_surfacelist));
662         }
663         if (r_shadow_buffer_numshadowtrispvsbytes < numshadowtrispvsbytes)
664         {
665                 if (r_shadow_buffer_shadowtrispvs)
666                         Mem_Free(r_shadow_buffer_shadowtrispvs);
667                 r_shadow_buffer_numshadowtrispvsbytes = numshadowtrispvsbytes;
668                 r_shadow_buffer_shadowtrispvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numshadowtrispvsbytes);
669         }
670         if (r_shadow_buffer_numlighttrispvsbytes < numlighttrispvsbytes)
671         {
672                 if (r_shadow_buffer_lighttrispvs)
673                         Mem_Free(r_shadow_buffer_lighttrispvs);
674                 r_shadow_buffer_numlighttrispvsbytes = numlighttrispvsbytes;
675                 r_shadow_buffer_lighttrispvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numlighttrispvsbytes);
676         }
677 }
678
679 void R_Shadow_PrepareShadowMark(int numtris)
680 {
681         // make sure shadowmark is big enough for this volume
682         if (maxshadowmark < numtris)
683         {
684                 maxshadowmark = numtris;
685                 if (shadowmark)
686                         Mem_Free(shadowmark);
687                 if (shadowmarklist)
688                         Mem_Free(shadowmarklist);
689                 shadowmark = (int *)Mem_Alloc(r_main_mempool, maxshadowmark * sizeof(*shadowmark));
690                 shadowmarklist = (int *)Mem_Alloc(r_main_mempool, maxshadowmark * sizeof(*shadowmarklist));
691                 shadowmarkcount = 0;
692         }
693         shadowmarkcount++;
694         // if shadowmarkcount wrapped we clear the array and adjust accordingly
695         if (shadowmarkcount == 0)
696         {
697                 shadowmarkcount = 1;
698                 memset(shadowmark, 0, maxshadowmark * sizeof(*shadowmark));
699         }
700         numshadowmark = 0;
701 }
702
703 static int R_Shadow_ConstructShadowVolume_ZFail(int innumvertices, int innumtris, const int *inelement3i, const int *inneighbor3i, const float *invertex3f, int *outnumvertices, int *outelement3i, float *outvertex3f, const float *projectorigin, const float *projectdirection, float projectdistance, int numshadowmarktris, const int *shadowmarktris)
704 {
705         int i, j;
706         int outtriangles = 0, outvertices = 0;
707         const int *element;
708         const float *vertex;
709         float ratio, direction[3], projectvector[3];
710
711         if (projectdirection)
712                 VectorScale(projectdirection, projectdistance, projectvector);
713         else
714                 VectorClear(projectvector);
715
716         // create the vertices
717         if (projectdirection)
718         {
719                 for (i = 0;i < numshadowmarktris;i++)
720                 {
721                         element = inelement3i + shadowmarktris[i] * 3;
722                         for (j = 0;j < 3;j++)
723                         {
724                                 if (vertexupdate[element[j]] != vertexupdatenum)
725                                 {
726                                         vertexupdate[element[j]] = vertexupdatenum;
727                                         vertexremap[element[j]] = outvertices;
728                                         vertex = invertex3f + element[j] * 3;
729                                         // project one copy of the vertex according to projectvector
730                                         VectorCopy(vertex, outvertex3f);
731                                         VectorAdd(vertex, projectvector, (outvertex3f + 3));
732                                         outvertex3f += 6;
733                                         outvertices += 2;
734                                 }
735                         }
736                 }
737         }
738         else
739         {
740                 for (i = 0;i < numshadowmarktris;i++)
741                 {
742                         element = inelement3i + shadowmarktris[i] * 3;
743                         for (j = 0;j < 3;j++)
744                         {
745                                 if (vertexupdate[element[j]] != vertexupdatenum)
746                                 {
747                                         vertexupdate[element[j]] = vertexupdatenum;
748                                         vertexremap[element[j]] = outvertices;
749                                         vertex = invertex3f + element[j] * 3;
750                                         // project one copy of the vertex to the sphere radius of the light
751                                         // (FIXME: would projecting it to the light box be better?)
752                                         VectorSubtract(vertex, projectorigin, direction);
753                                         ratio = projectdistance / VectorLength(direction);
754                                         VectorCopy(vertex, outvertex3f);
755                                         VectorMA(projectorigin, ratio, direction, (outvertex3f + 3));
756                                         outvertex3f += 6;
757                                         outvertices += 2;
758                                 }
759                         }
760                 }
761         }
762
763         if (r_shadow_frontsidecasting.integer)
764         {
765                 for (i = 0;i < numshadowmarktris;i++)
766                 {
767                         int remappedelement[3];
768                         int markindex;
769                         const int *neighbortriangle;
770
771                         markindex = shadowmarktris[i] * 3;
772                         element = inelement3i + markindex;
773                         neighbortriangle = inneighbor3i + markindex;
774                         // output the front and back triangles
775                         outelement3i[0] = vertexremap[element[0]];
776                         outelement3i[1] = vertexremap[element[1]];
777                         outelement3i[2] = vertexremap[element[2]];
778                         outelement3i[3] = vertexremap[element[2]] + 1;
779                         outelement3i[4] = vertexremap[element[1]] + 1;
780                         outelement3i[5] = vertexremap[element[0]] + 1;
781
782                         outelement3i += 6;
783                         outtriangles += 2;
784                         // output the sides (facing outward from this triangle)
785                         if (shadowmark[neighbortriangle[0]] != shadowmarkcount)
786                         {
787                                 remappedelement[0] = vertexremap[element[0]];
788                                 remappedelement[1] = vertexremap[element[1]];
789                                 outelement3i[0] = remappedelement[1];
790                                 outelement3i[1] = remappedelement[0];
791                                 outelement3i[2] = remappedelement[0] + 1;
792                                 outelement3i[3] = remappedelement[1];
793                                 outelement3i[4] = remappedelement[0] + 1;
794                                 outelement3i[5] = remappedelement[1] + 1;
795
796                                 outelement3i += 6;
797                                 outtriangles += 2;
798                         }
799                         if (shadowmark[neighbortriangle[1]] != shadowmarkcount)
800                         {
801                                 remappedelement[1] = vertexremap[element[1]];
802                                 remappedelement[2] = vertexremap[element[2]];
803                                 outelement3i[0] = remappedelement[2];
804                                 outelement3i[1] = remappedelement[1];
805                                 outelement3i[2] = remappedelement[1] + 1;
806                                 outelement3i[3] = remappedelement[2];
807                                 outelement3i[4] = remappedelement[1] + 1;
808                                 outelement3i[5] = remappedelement[2] + 1;
809
810                                 outelement3i += 6;
811                                 outtriangles += 2;
812                         }
813                         if (shadowmark[neighbortriangle[2]] != shadowmarkcount)
814                         {
815                                 remappedelement[0] = vertexremap[element[0]];
816                                 remappedelement[2] = vertexremap[element[2]];
817                                 outelement3i[0] = remappedelement[0];
818                                 outelement3i[1] = remappedelement[2];
819                                 outelement3i[2] = remappedelement[2] + 1;
820                                 outelement3i[3] = remappedelement[0];
821                                 outelement3i[4] = remappedelement[2] + 1;
822                                 outelement3i[5] = remappedelement[0] + 1;
823
824                                 outelement3i += 6;
825                                 outtriangles += 2;
826                         }
827                 }
828         }
829         else
830         {
831                 for (i = 0;i < numshadowmarktris;i++)
832                 {
833                         int remappedelement[3];
834                         int markindex;
835                         const int *neighbortriangle;
836
837                         markindex = shadowmarktris[i] * 3;
838                         element = inelement3i + markindex;
839                         neighbortriangle = inneighbor3i + markindex;
840                         // output the front and back triangles
841                         outelement3i[0] = vertexremap[element[2]];
842                         outelement3i[1] = vertexremap[element[1]];
843                         outelement3i[2] = vertexremap[element[0]];
844                         outelement3i[3] = vertexremap[element[0]] + 1;
845                         outelement3i[4] = vertexremap[element[1]] + 1;
846                         outelement3i[5] = vertexremap[element[2]] + 1;
847
848                         outelement3i += 6;
849                         outtriangles += 2;
850                         // output the sides (facing outward from this triangle)
851                         if (shadowmark[neighbortriangle[0]] != shadowmarkcount)
852                         {
853                                 remappedelement[0] = vertexremap[element[0]];
854                                 remappedelement[1] = vertexremap[element[1]];
855                                 outelement3i[0] = remappedelement[0];
856                                 outelement3i[1] = remappedelement[1];
857                                 outelement3i[2] = remappedelement[1] + 1;
858                                 outelement3i[3] = remappedelement[0];
859                                 outelement3i[4] = remappedelement[1] + 1;
860                                 outelement3i[5] = remappedelement[0] + 1;
861
862                                 outelement3i += 6;
863                                 outtriangles += 2;
864                         }
865                         if (shadowmark[neighbortriangle[1]] != shadowmarkcount)
866                         {
867                                 remappedelement[1] = vertexremap[element[1]];
868                                 remappedelement[2] = vertexremap[element[2]];
869                                 outelement3i[0] = remappedelement[1];
870                                 outelement3i[1] = remappedelement[2];
871                                 outelement3i[2] = remappedelement[2] + 1;
872                                 outelement3i[3] = remappedelement[1];
873                                 outelement3i[4] = remappedelement[2] + 1;
874                                 outelement3i[5] = remappedelement[1] + 1;
875
876                                 outelement3i += 6;
877                                 outtriangles += 2;
878                         }
879                         if (shadowmark[neighbortriangle[2]] != shadowmarkcount)
880                         {
881                                 remappedelement[0] = vertexremap[element[0]];
882                                 remappedelement[2] = vertexremap[element[2]];
883                                 outelement3i[0] = remappedelement[2];
884                                 outelement3i[1] = remappedelement[0];
885                                 outelement3i[2] = remappedelement[0] + 1;
886                                 outelement3i[3] = remappedelement[2];
887                                 outelement3i[4] = remappedelement[0] + 1;
888                                 outelement3i[5] = remappedelement[2] + 1;
889
890                                 outelement3i += 6;
891                                 outtriangles += 2;
892                         }
893                 }
894         }
895         if (outnumvertices)
896                 *outnumvertices = outvertices;
897         return outtriangles;
898 }
899
900 static int R_Shadow_ConstructShadowVolume_ZPass(int innumvertices, int innumtris, const int *inelement3i, const int *inneighbor3i, const float *invertex3f, int *outnumvertices, int *outelement3i, float *outvertex3f, const float *projectorigin, const float *projectdirection, float projectdistance, int numshadowmarktris, const int *shadowmarktris)
901 {
902         int i, j, k;
903         int outtriangles = 0, outvertices = 0;
904         const int *element;
905         const float *vertex;
906         float ratio, direction[3], projectvector[3];
907         qboolean side[4];
908
909         if (projectdirection)
910                 VectorScale(projectdirection, projectdistance, projectvector);
911         else
912                 VectorClear(projectvector);
913
914         for (i = 0;i < numshadowmarktris;i++)
915         {
916                 int remappedelement[3];
917                 int markindex;
918                 const int *neighbortriangle;
919
920                 markindex = shadowmarktris[i] * 3;
921                 neighbortriangle = inneighbor3i + markindex;
922                 side[0] = shadowmark[neighbortriangle[0]] == shadowmarkcount;
923                 side[1] = shadowmark[neighbortriangle[1]] == shadowmarkcount;
924                 side[2] = shadowmark[neighbortriangle[2]] == shadowmarkcount;
925                 if (side[0] + side[1] + side[2] == 0)
926                         continue;
927
928                 side[3] = side[0];
929                 element = inelement3i + markindex;
930
931                 // create the vertices
932                 for (j = 0;j < 3;j++)
933                 {
934                         if (side[j] + side[j+1] == 0)
935                                 continue;
936                         k = element[j];
937                         if (vertexupdate[k] != vertexupdatenum)
938                         {
939                                 vertexupdate[k] = vertexupdatenum;
940                                 vertexremap[k] = outvertices;
941                                 vertex = invertex3f + k * 3;
942                                 VectorCopy(vertex, outvertex3f);
943                                 if (projectdirection)
944                                 {
945                                         // project one copy of the vertex according to projectvector
946                                         VectorAdd(vertex, projectvector, (outvertex3f + 3));
947                                 }
948                                 else
949                                 {
950                                         // project one copy of the vertex to the sphere radius of the light
951                                         // (FIXME: would projecting it to the light box be better?)
952                                         VectorSubtract(vertex, projectorigin, direction);
953                                         ratio = projectdistance / VectorLength(direction);
954                                         VectorMA(projectorigin, ratio, direction, (outvertex3f + 3));
955                                 }
956                                 outvertex3f += 6;
957                                 outvertices += 2;
958                         }
959                 }
960
961                 // output the sides (facing outward from this triangle)
962                 if (!side[0])
963                 {
964                         remappedelement[0] = vertexremap[element[0]];
965                         remappedelement[1] = vertexremap[element[1]];
966                         outelement3i[0] = remappedelement[1];
967                         outelement3i[1] = remappedelement[0];
968                         outelement3i[2] = remappedelement[0] + 1;
969                         outelement3i[3] = remappedelement[1];
970                         outelement3i[4] = remappedelement[0] + 1;
971                         outelement3i[5] = remappedelement[1] + 1;
972
973                         outelement3i += 6;
974                         outtriangles += 2;
975                 }
976                 if (!side[1])
977                 {
978                         remappedelement[1] = vertexremap[element[1]];
979                         remappedelement[2] = vertexremap[element[2]];
980                         outelement3i[0] = remappedelement[2];
981                         outelement3i[1] = remappedelement[1];
982                         outelement3i[2] = remappedelement[1] + 1;
983                         outelement3i[3] = remappedelement[2];
984                         outelement3i[4] = remappedelement[1] + 1;
985                         outelement3i[5] = remappedelement[2] + 1;
986
987                         outelement3i += 6;
988                         outtriangles += 2;
989                 }
990                 if (!side[2])
991                 {
992                         remappedelement[0] = vertexremap[element[0]];
993                         remappedelement[2] = vertexremap[element[2]];
994                         outelement3i[0] = remappedelement[0];
995                         outelement3i[1] = remappedelement[2];
996                         outelement3i[2] = remappedelement[2] + 1;
997                         outelement3i[3] = remappedelement[0];
998                         outelement3i[4] = remappedelement[2] + 1;
999                         outelement3i[5] = remappedelement[0] + 1;
1000
1001                         outelement3i += 6;
1002                         outtriangles += 2;
1003                 }
1004         }
1005         if (outnumvertices)
1006                 *outnumvertices = outvertices;
1007         return outtriangles;
1008 }
1009
1010 void R_Shadow_MarkVolumeFromBox(int firsttriangle, int numtris, const float *invertex3f, const int *elements, const vec3_t projectorigin, const vec3_t projectdirection, const vec3_t lightmins, const vec3_t lightmaxs, const vec3_t surfacemins, const vec3_t surfacemaxs)
1011 {
1012         int t, tend;
1013         const int *e;
1014         const float *v[3];
1015         float normal[3];
1016         if (!BoxesOverlap(lightmins, lightmaxs, surfacemins, surfacemaxs))
1017                 return;
1018         tend = firsttriangle + numtris;
1019         if (BoxInsideBox(surfacemins, surfacemaxs, lightmins, lightmaxs))
1020         {
1021                 // surface box entirely inside light box, no box cull
1022                 if (projectdirection)
1023                 {
1024                         for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
1025                         {
1026                                 TriangleNormal(invertex3f + e[0] * 3, invertex3f + e[1] * 3, invertex3f + e[2] * 3, normal);
1027                                 if (r_shadow_frontsidecasting.integer == (DotProduct(normal, projectdirection) < 0))
1028                                         shadowmarklist[numshadowmark++] = t;
1029                         }
1030                 }
1031                 else
1032                 {
1033                         for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
1034                                 if (r_shadow_frontsidecasting.integer == PointInfrontOfTriangle(projectorigin, invertex3f + e[0] * 3, invertex3f + e[1] * 3, invertex3f + e[2] * 3))
1035                                         shadowmarklist[numshadowmark++] = t;
1036                 }
1037         }
1038         else
1039         {
1040                 // surface box not entirely inside light box, cull each triangle
1041                 if (projectdirection)
1042                 {
1043                         for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
1044                         {
1045                                 v[0] = invertex3f + e[0] * 3;
1046                                 v[1] = invertex3f + e[1] * 3;
1047                                 v[2] = invertex3f + e[2] * 3;
1048                                 TriangleNormal(v[0], v[1], v[2], normal);
1049                                 if (r_shadow_frontsidecasting.integer == (DotProduct(normal, projectdirection) < 0)
1050                                  && TriangleOverlapsBox(v[0], v[1], v[2], lightmins, lightmaxs))
1051                                         shadowmarklist[numshadowmark++] = t;
1052                         }
1053                 }
1054                 else
1055                 {
1056                         for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
1057                         {
1058                                 v[0] = invertex3f + e[0] * 3;
1059                                 v[1] = invertex3f + e[1] * 3;
1060                                 v[2] = invertex3f + e[2] * 3;
1061                                 if (r_shadow_frontsidecasting.integer == PointInfrontOfTriangle(projectorigin, v[0], v[1], v[2])
1062                                  && TriangleOverlapsBox(v[0], v[1], v[2], lightmins, lightmaxs))
1063                                         shadowmarklist[numshadowmark++] = t;
1064                         }
1065                 }
1066         }
1067 }
1068
1069 qboolean R_Shadow_UseZPass(vec3_t mins, vec3_t maxs)
1070 {
1071 #if 1
1072         return false;
1073 #else
1074         if (r_shadow_compilingrtlight || !r_shadow_frontsidecasting.integer || !r_shadow_usezpassifpossible.integer)
1075                 return false;
1076         // check if the shadow volume intersects the near plane
1077         //
1078         // a ray between the eye and light origin may intersect the caster,
1079         // indicating that the shadow may touch the eye location, however we must
1080         // test the near plane (a polygon), not merely the eye location, so it is
1081         // easiest to enlarge the caster bounding shape slightly for this.
1082         // TODO
1083         return true;
1084 #endif
1085 }
1086
1087 void R_Shadow_VolumeFromList(int numverts, int numtris, const float *invertex3f, const int *elements, const int *neighbors, const vec3_t projectorigin, const vec3_t projectdirection, float projectdistance, int nummarktris, const int *marktris, vec3_t trismins, vec3_t trismaxs)
1088 {
1089         int i, tris, outverts;
1090         if (projectdistance < 0.1)
1091         {
1092                 Con_Printf("R_Shadow_Volume: projectdistance %f\n", projectdistance);
1093                 return;
1094         }
1095         if (!numverts || !nummarktris)
1096                 return;
1097         // make sure shadowelements is big enough for this volume
1098         if (maxshadowtriangles < nummarktris || maxshadowvertices < numverts)
1099                 R_Shadow_ResizeShadowArrays((numverts + 255) & ~255, (nummarktris + 255) & ~255);
1100
1101         if (maxvertexupdate < numverts)
1102         {
1103                 maxvertexupdate = numverts;
1104                 if (vertexupdate)
1105                         Mem_Free(vertexupdate);
1106                 if (vertexremap)
1107                         Mem_Free(vertexremap);
1108                 vertexupdate = (int *)Mem_Alloc(r_main_mempool, maxvertexupdate * sizeof(int));
1109                 vertexremap = (int *)Mem_Alloc(r_main_mempool, maxvertexupdate * sizeof(int));
1110                 vertexupdatenum = 0;
1111         }
1112         vertexupdatenum++;
1113         if (vertexupdatenum == 0)
1114         {
1115                 vertexupdatenum = 1;
1116                 memset(vertexupdate, 0, maxvertexupdate * sizeof(int));
1117                 memset(vertexremap, 0, maxvertexupdate * sizeof(int));
1118         }
1119
1120         for (i = 0;i < nummarktris;i++)
1121                 shadowmark[marktris[i]] = shadowmarkcount;
1122
1123         if (r_shadow_compilingrtlight)
1124         {
1125                 // if we're compiling an rtlight, capture the mesh
1126                 //tris = R_Shadow_ConstructShadowVolume_ZPass(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
1127                 //Mod_ShadowMesh_AddMesh(r_main_mempool, r_shadow_compilingrtlight->static_meshchain_shadow_zpass, NULL, NULL, NULL, shadowvertex3f, NULL, NULL, NULL, NULL, tris, shadowelements);
1128                 tris = R_Shadow_ConstructShadowVolume_ZFail(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
1129                 Mod_ShadowMesh_AddMesh(r_main_mempool, r_shadow_compilingrtlight->static_meshchain_shadow_zfail, NULL, NULL, NULL, shadowvertex3f, NULL, NULL, NULL, NULL, tris, shadowelements);
1130         }
1131         else
1132         {
1133                 // decide which type of shadow to generate and set stencil mode
1134                 R_Shadow_RenderMode_StencilShadowVolumes(R_Shadow_UseZPass(trismins, trismaxs));
1135                 // generate the sides or a solid volume, depending on type
1136                 if (r_shadow_rendermode >= R_SHADOW_RENDERMODE_ZPASS_STENCIL && r_shadow_rendermode <= R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE)
1137                         tris = R_Shadow_ConstructShadowVolume_ZPass(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
1138                 else
1139                         tris = R_Shadow_ConstructShadowVolume_ZFail(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
1140                 r_refdef.stats.lights_dynamicshadowtriangles += tris;
1141                 r_refdef.stats.lights_shadowtriangles += tris;
1142                 CHECKGLERROR
1143                 R_Mesh_VertexPointer(shadowvertex3f, 0, 0);
1144                 GL_LockArrays(0, outverts);
1145                 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZPASS_STENCIL)
1146                 {
1147                         // increment stencil if frontface is infront of depthbuffer
1148                         GL_CullFace(r_refdef.view.cullface_front);
1149                         qglStencilOp(GL_KEEP, GL_KEEP, GL_DECR);CHECKGLERROR
1150                         R_Mesh_Draw(0, outverts, 0, tris, shadowelements, NULL, 0, 0);
1151                         // decrement stencil if backface is infront of depthbuffer
1152                         GL_CullFace(r_refdef.view.cullface_back);
1153                         qglStencilOp(GL_KEEP, GL_KEEP, GL_INCR);CHECKGLERROR
1154                 }
1155                 else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZFAIL_STENCIL)
1156                 {
1157                         // decrement stencil if backface is behind depthbuffer
1158                         GL_CullFace(r_refdef.view.cullface_front);
1159                         qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
1160                         R_Mesh_Draw(0, outverts, 0, tris, shadowelements, NULL, 0, 0);
1161                         // increment stencil if frontface is behind depthbuffer
1162                         GL_CullFace(r_refdef.view.cullface_back);
1163                         qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR
1164                 }
1165                 R_Mesh_Draw(0, outverts, 0, tris, shadowelements, NULL, 0, 0);
1166                 GL_LockArrays(0, 0);
1167                 CHECKGLERROR
1168         }
1169 }
1170
1171 void R_Shadow_ShadowMapFromList(int numverts, int numtris, const float *vertex3f, int vertex3f_bufferobject, int vertex3f_bufferoffset, const int *elements, int nummarktris, const int *marktris)
1172 {
1173         int i, tris = nummarktris;
1174         int *outelement3i;
1175         const int *element;
1176         if (!numverts || !nummarktris)
1177                 return;
1178         // make sure shadowelements is big enough for this mesh
1179         if (maxshadowtriangles < nummarktris || maxshadowvertices < numverts)
1180                 R_Shadow_ResizeShadowArrays((numverts + 255) & ~255, (nummarktris + 255) & ~255);
1181
1182         // gather up the (sparse) triangles into one array
1183         outelement3i = shadowelements;
1184         for (i = 0;i < nummarktris;i++)
1185         {
1186                 element = elements + marktris[i] * 3;
1187                 outelement3i[0] = element[0];
1188                 outelement3i[1] = element[1];
1189                 outelement3i[2] = element[2];
1190                 outelement3i += 3;
1191         }
1192
1193         r_refdef.stats.lights_dynamicshadowtriangles += tris;
1194         r_refdef.stats.lights_shadowtriangles += tris;
1195         R_Mesh_VertexPointer(vertex3f, vertex3f_bufferobject, vertex3f_bufferoffset);
1196         R_Mesh_Draw(0, numverts, 0, tris, shadowelements, NULL, 0, 0);
1197 }
1198
1199 static void R_Shadow_MakeTextures_MakeCorona(void)
1200 {
1201         float dx, dy;
1202         int x, y, a;
1203         unsigned char pixels[32][32][4];
1204         for (y = 0;y < 32;y++)
1205         {
1206                 dy = (y - 15.5f) * (1.0f / 16.0f);
1207                 for (x = 0;x < 32;x++)
1208                 {
1209                         dx = (x - 15.5f) * (1.0f / 16.0f);
1210                         a = (int)(((1.0f / (dx * dx + dy * dy + 0.2f)) - (1.0f / (1.0f + 0.2))) * 32.0f / (1.0f / (1.0f + 0.2)));
1211                         a = bound(0, a, 255);
1212                         pixels[y][x][0] = a;
1213                         pixels[y][x][1] = a;
1214                         pixels[y][x][2] = a;
1215                         pixels[y][x][3] = 255;
1216                 }
1217         }
1218         r_shadow_lightcorona = R_LoadTexture2D(r_shadow_texturepool, "lightcorona", 32, 32, &pixels[0][0][0], TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_FORCELINEAR, NULL);
1219 }
1220
1221 static unsigned int R_Shadow_MakeTextures_SamplePoint(float x, float y, float z)
1222 {
1223         float dist = sqrt(x*x+y*y+z*z);
1224         float intensity = dist < 1 ? ((1.0f - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist)) : 0;
1225         // note this code could suffer byte order issues except that it is multiplying by an integer that reads the same both ways
1226         return (unsigned char)bound(0, intensity * 256.0f, 255) * 0x01010101;
1227 }
1228
1229 static void R_Shadow_MakeTextures(void)
1230 {
1231         int x, y, z;
1232         float intensity, dist;
1233         unsigned int *data;
1234         R_FreeTexturePool(&r_shadow_texturepool);
1235         r_shadow_texturepool = R_AllocTexturePool();
1236         r_shadow_attenlinearscale = r_shadow_lightattenuationlinearscale.value;
1237         r_shadow_attendividebias = r_shadow_lightattenuationdividebias.value;
1238         data = (unsigned int *)Mem_Alloc(tempmempool, max(max(ATTEN3DSIZE*ATTEN3DSIZE*ATTEN3DSIZE, ATTEN2DSIZE*ATTEN2DSIZE), ATTEN1DSIZE) * 4);
1239         // the table includes one additional value to avoid the need to clamp indexing due to minor math errors
1240         for (x = 0;x <= ATTENTABLESIZE;x++)
1241         {
1242                 dist = (x + 0.5f) * (1.0f / ATTENTABLESIZE) * (1.0f / 0.9375);
1243                 intensity = dist < 1 ? ((1.0f - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist)) : 0;
1244                 r_shadow_attentable[x] = bound(0, intensity, 1);
1245         }
1246         // 1D gradient texture
1247         for (x = 0;x < ATTEN1DSIZE;x++)
1248                 data[x] = R_Shadow_MakeTextures_SamplePoint((x + 0.5f) * (1.0f / ATTEN1DSIZE) * (1.0f / 0.9375), 0, 0);
1249         r_shadow_attenuationgradienttexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation1d", ATTEN1DSIZE, 1, (unsigned char *)data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCELINEAR, NULL);
1250         // 2D circle texture
1251         for (y = 0;y < ATTEN2DSIZE;y++)
1252                 for (x = 0;x < ATTEN2DSIZE;x++)
1253                         data[y*ATTEN2DSIZE+x] = R_Shadow_MakeTextures_SamplePoint(((x + 0.5f) * (2.0f / ATTEN2DSIZE) - 1.0f) * (1.0f / 0.9375), ((y + 0.5f) * (2.0f / ATTEN2DSIZE) - 1.0f) * (1.0f / 0.9375), 0);
1254         r_shadow_attenuation2dtexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation2d", ATTEN2DSIZE, ATTEN2DSIZE, (unsigned char *)data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCELINEAR, NULL);
1255         // 3D sphere texture
1256         if (r_shadow_texture3d.integer && gl_texture3d)
1257         {
1258                 for (z = 0;z < ATTEN3DSIZE;z++)
1259                         for (y = 0;y < ATTEN3DSIZE;y++)
1260                                 for (x = 0;x < ATTEN3DSIZE;x++)
1261                                         data[(z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x] = R_Shadow_MakeTextures_SamplePoint(((x + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375), ((y + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375), ((z + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375));
1262                 r_shadow_attenuation3dtexture = R_LoadTexture3D(r_shadow_texturepool, "attenuation3d", ATTEN3DSIZE, ATTEN3DSIZE, ATTEN3DSIZE, (unsigned char *)data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCELINEAR, NULL);
1263         }
1264         else
1265                 r_shadow_attenuation3dtexture = NULL;
1266         Mem_Free(data);
1267
1268         R_Shadow_MakeTextures_MakeCorona();
1269
1270         // Editor light sprites
1271         r_editlights_sprcursor = Draw_CachePic ("gfx/editlights/cursor");
1272         r_editlights_sprlight = Draw_CachePic ("gfx/editlights/light");
1273         r_editlights_sprnoshadowlight = Draw_CachePic ("gfx/editlights/noshadow");
1274         r_editlights_sprcubemaplight = Draw_CachePic ("gfx/editlights/cubemaplight");
1275         r_editlights_sprcubemapnoshadowlight = Draw_CachePic ("gfx/editlights/cubemapnoshadowlight");
1276         r_editlights_sprselection = Draw_CachePic ("gfx/editlights/selection");
1277 }
1278
1279 void R_Shadow_ValidateCvars(void)
1280 {
1281         if (r_shadow_texture3d.integer && !gl_texture3d)
1282                 Cvar_SetValueQuick(&r_shadow_texture3d, 0);
1283         if (gl_ext_separatestencil.integer && !gl_support_separatestencil)
1284                 Cvar_SetValueQuick(&gl_ext_separatestencil, 0);
1285         if (gl_ext_stenciltwoside.integer && !gl_support_stenciltwoside)
1286                 Cvar_SetValueQuick(&gl_ext_stenciltwoside, 0);
1287 }
1288
1289 void R_Shadow_RenderMode_Begin(void)
1290 {
1291         GLint drawbuffer;
1292         GLint readbuffer;
1293         R_Shadow_ValidateCvars();
1294
1295         if (!r_shadow_attenuation2dtexture
1296          || (!r_shadow_attenuation3dtexture && r_shadow_texture3d.integer)
1297          || r_shadow_lightattenuationdividebias.value != r_shadow_attendividebias
1298          || r_shadow_lightattenuationlinearscale.value != r_shadow_attenlinearscale)
1299                 R_Shadow_MakeTextures();
1300
1301         CHECKGLERROR
1302         R_Mesh_ColorPointer(NULL, 0, 0);
1303         R_Mesh_ResetTextureState();
1304         GL_BlendFunc(GL_ONE, GL_ZERO);
1305         GL_DepthRange(0, 1);
1306         GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
1307         GL_DepthTest(true);
1308         GL_DepthMask(false);
1309         GL_Color(0, 0, 0, 1);
1310         GL_Scissor(r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
1311
1312         r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
1313
1314         if (gl_ext_separatestencil.integer)
1315         {
1316                 r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_SEPARATESTENCIL;
1317                 r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_SEPARATESTENCIL;
1318         }
1319         else if (gl_ext_stenciltwoside.integer)
1320         {
1321                 r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE;
1322                 r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE;
1323         }
1324         else
1325         {
1326                 r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_STENCIL;
1327                 r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_STENCIL;
1328         }
1329
1330         if (r_glsl.integer && gl_support_fragment_shader)
1331                 r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_GLSL;
1332         else if (gl_dot3arb && gl_texturecubemap && r_textureunits.integer >= 2 && gl_combine.integer && gl_stencil)
1333                 r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_DOT3;
1334         else
1335                 r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_VERTEX;
1336
1337         CHECKGLERROR
1338         qglGetIntegerv(GL_DRAW_BUFFER, &drawbuffer);CHECKGLERROR
1339         qglGetIntegerv(GL_READ_BUFFER, &readbuffer);CHECKGLERROR
1340         r_shadow_drawbuffer = drawbuffer;
1341         r_shadow_readbuffer = readbuffer;
1342 }
1343
1344 void R_Shadow_RenderMode_ActiveLight(const rtlight_t *rtlight)
1345 {
1346         rsurface.rtlight = rtlight;
1347 }
1348
1349 void R_Shadow_RenderMode_Reset(void)
1350 {
1351         CHECKGLERROR
1352         if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE || r_shadow_rendermode == R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE)
1353         {
1354                 qglDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
1355         }
1356         if (gl_support_ext_framebuffer_object)
1357         {
1358                 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);CHECKGLERROR
1359         }
1360         qglDrawBuffer(r_shadow_drawbuffer);CHECKGLERROR
1361         qglReadBuffer(r_shadow_readbuffer);CHECKGLERROR
1362         R_SetViewport(&r_refdef.view.viewport);
1363         GL_Scissor(r_shadow_lightscissor[0], r_shadow_lightscissor[1], r_shadow_lightscissor[2], r_shadow_lightscissor[3]);
1364         R_Mesh_ColorPointer(NULL, 0, 0);
1365         R_Mesh_ResetTextureState();
1366         GL_DepthRange(0, 1);
1367         GL_DepthTest(true);
1368         GL_DepthMask(false);
1369         qglDepthFunc(GL_LEQUAL);CHECKGLERROR
1370         GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);CHECKGLERROR
1371         qglDisable(GL_STENCIL_TEST);CHECKGLERROR
1372         qglStencilMask(~0);CHECKGLERROR
1373         qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);CHECKGLERROR
1374         qglStencilFunc(GL_ALWAYS, 128, ~0);CHECKGLERROR
1375         GL_CullFace(r_refdef.view.cullface_back);
1376         GL_Color(1, 1, 1, 1);
1377         GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
1378         GL_BlendFunc(GL_ONE, GL_ZERO);
1379         R_SetupGenericShader(false);
1380         CHECKGLERROR
1381 }
1382
1383 void R_Shadow_ClearStencil(void)
1384 {
1385         CHECKGLERROR
1386         GL_Clear(GL_STENCIL_BUFFER_BIT);
1387         r_refdef.stats.lights_clears++;
1388 }
1389
1390 void R_Shadow_RenderMode_StencilShadowVolumes(qboolean zpass)
1391 {
1392         r_shadow_rendermode_t mode = zpass ? r_shadow_shadowingrendermode_zpass : r_shadow_shadowingrendermode_zfail;
1393         if (r_shadow_rendermode == mode)
1394                 return;
1395         CHECKGLERROR
1396         R_Shadow_RenderMode_Reset();
1397         GL_ColorMask(0, 0, 0, 0);
1398         GL_PolygonOffset(r_refdef.shadowpolygonfactor, r_refdef.shadowpolygonoffset);CHECKGLERROR
1399         R_SetupDepthOrShadowShader();
1400         qglDepthFunc(GL_LESS);CHECKGLERROR
1401         qglEnable(GL_STENCIL_TEST);CHECKGLERROR
1402         r_shadow_rendermode = mode;
1403         switch(mode)
1404         {
1405         default:
1406                 break;
1407         case R_SHADOW_RENDERMODE_ZPASS_SEPARATESTENCIL:
1408                 GL_CullFace(GL_NONE);
1409                 qglStencilOpSeparate(r_refdef.view.cullface_front, GL_KEEP, GL_KEEP, GL_INCR);CHECKGLERROR
1410                 qglStencilOpSeparate(r_refdef.view.cullface_back, GL_KEEP, GL_KEEP, GL_DECR);CHECKGLERROR
1411                 break;
1412         case R_SHADOW_RENDERMODE_ZFAIL_SEPARATESTENCIL:
1413                 GL_CullFace(GL_NONE);
1414                 qglStencilOpSeparate(r_refdef.view.cullface_front, GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR
1415                 qglStencilOpSeparate(r_refdef.view.cullface_back, GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
1416                 break;
1417         case R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE:
1418                 GL_CullFace(GL_NONE);
1419                 qglEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
1420                 qglActiveStencilFaceEXT(r_refdef.view.cullface_front);CHECKGLERROR
1421                 qglStencilMask(~0);CHECKGLERROR
1422                 qglStencilOp(GL_KEEP, GL_KEEP, GL_INCR);CHECKGLERROR
1423                 qglActiveStencilFaceEXT(r_refdef.view.cullface_back);CHECKGLERROR
1424                 qglStencilMask(~0);CHECKGLERROR
1425                 qglStencilOp(GL_KEEP, GL_KEEP, GL_DECR);CHECKGLERROR
1426                 break;
1427         case R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE:
1428                 GL_CullFace(GL_NONE);
1429                 qglEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
1430                 qglActiveStencilFaceEXT(r_refdef.view.cullface_front);CHECKGLERROR
1431                 qglStencilMask(~0);CHECKGLERROR
1432                 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR
1433                 qglActiveStencilFaceEXT(r_refdef.view.cullface_back);CHECKGLERROR
1434                 qglStencilMask(~0);CHECKGLERROR
1435                 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
1436                 break;
1437         }
1438 }
1439
1440 void R_Shadow_RenderMode_ShadowMap(int side, qboolean clear, int size)
1441 {
1442         int status;
1443         int maxsize;
1444         float nearclip, farclip, bias;
1445         r_viewport_t viewport;
1446         CHECKGLERROR
1447         maxsize = r_shadow_shadowmapmaxsize;
1448         nearclip = r_shadow_shadowmapping_nearclip.value / rsurface.rtlight->radius;
1449         farclip = 1.0f;
1450         bias = r_shadow_shadowmapping_bias.value * nearclip * (1024.0f / size);// * rsurface.rtlight->radius;
1451         // complex unrolled cube approach (more flexible)
1452         if (!r_shadow_shadowmapcubeprojectiontexture[r_shadow_shadowmaplod])
1453                 r_shadow_shadowmapcubeprojectiontexture[r_shadow_shadowmaplod] = R_LoadTextureCubeProjection(r_shadow_texturepool, "shadowmapcubeprojection", size, r_shadow_shadowmapborder);
1454         if (!r_shadow_shadowmaprectangletexture)
1455         {
1456 #if 1
1457                 r_shadow_shadowmaprectangletexture = R_LoadTextureShadowMapRectangle(r_shadow_texturepool, "shadowmap", maxsize*2, maxsize*3, r_shadow_shadowmapfilter == 1 || r_shadow_shadowmapfilter == 2);
1458                 qglGenFramebuffersEXT(1, &r_shadow_fborectangle);CHECKGLERROR
1459                 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r_shadow_fborectangle);CHECKGLERROR
1460                 qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_RECTANGLE_ARB, R_GetTexture(r_shadow_shadowmaprectangletexture), 0);CHECKGLERROR
1461 #endif
1462         }
1463         CHECKGLERROR
1464         R_Shadow_RenderMode_Reset();
1465         if (r_shadow_shadowmaprectangletexture)
1466         {
1467                 // render depth into the fbo, do not render color at all
1468                 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r_shadow_fborectangle);CHECKGLERROR
1469                 qglDrawBuffer(GL_NONE);CHECKGLERROR
1470                 qglReadBuffer(GL_NONE);CHECKGLERROR
1471                 status = qglCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);CHECKGLERROR
1472                 if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
1473                 {
1474                         Con_Printf("R_Shadow_RenderMode_ShadowMap: glCheckFramebufferStatusEXT returned %i\n", status);
1475                         Cvar_SetValueQuick(&r_shadow_shadowmapping, 0);
1476                 }
1477                 R_SetupDepthOrShadowShader();
1478         }
1479         else
1480         {
1481                 R_SetupShowDepthShader();
1482                 qglClearColor(1,1,1,1);CHECKGLERROR
1483         }
1484         R_Viewport_InitRectSideView(&viewport, &rsurface.rtlight->matrix_lighttoworld, side, size, r_shadow_shadowmapborder, nearclip, farclip, NULL);
1485         r_shadow_shadowmap_texturescale[0] = 2*size;
1486         r_shadow_shadowmap_texturescale[1] = 3*size;
1487         r_shadow_shadowmap_texturescale[2] = 0.5f + 0.5f * (farclip + nearclip) / (farclip - nearclip);
1488         r_shadow_shadowmap_texturescale[3] = -nearclip * farclip / (farclip - nearclip) - 0.5f * bias;
1489         // compat for ALU cubemap calcs
1490     r_shadow_shadowmap_parameters[0] = 0.5f * (size - r_shadow_shadowmapborder);
1491         r_shadow_shadowmap_parameters[1] = size;
1492         r_shadow_shadowmap_parameters[2] = r_shadow_shadowmap_texturescale[2];
1493         r_shadow_shadowmap_parameters[3] = r_shadow_shadowmap_texturescale[3];
1494         r_shadow_rendermode = R_SHADOW_RENDERMODE_SHADOWMAPRECTANGLE;
1495         CHECKGLERROR
1496         R_SetViewport(&viewport);
1497         GL_PolygonOffset(0, 0);
1498         GL_CullFace(GL_NONE); // quake is backwards
1499         GL_Scissor(viewport.x, viewport.y, viewport.width, viewport.height);
1500         GL_DepthMask(true);
1501         GL_DepthTest(true);
1502         qglClearDepth(1);CHECKGLERROR
1503         CHECKGLERROR
1504         if (clear)
1505                 qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT |  GL_STENCIL_BUFFER_BIT);
1506         CHECKGLERROR
1507 }
1508
1509 void R_Shadow_RenderMode_Lighting(qboolean stenciltest, qboolean transparent, qboolean shadowmapping)
1510 {
1511         CHECKGLERROR
1512         R_Shadow_RenderMode_Reset();
1513         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1514         if (!transparent)
1515         {
1516                 qglDepthFunc(GL_EQUAL);CHECKGLERROR
1517         }
1518         if (stenciltest)
1519         {
1520                 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
1521                 // only draw light where this geometry was already rendered AND the
1522                 // stencil is 128 (values other than this mean shadow)
1523                 qglStencilFunc(GL_EQUAL, 128, ~0);CHECKGLERROR
1524         }
1525         r_shadow_rendermode = r_shadow_lightingrendermode;
1526         // do global setup needed for the chosen lighting mode
1527         if (r_shadow_rendermode == R_SHADOW_RENDERMODE_LIGHT_GLSL)
1528         {
1529                 R_Mesh_TexBindCubeMap(GL20TU_CUBE, R_GetTexture(rsurface.rtlight->currentcubemap)); // light filter
1530                 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 0);
1531                 CHECKGLERROR
1532                 if (shadowmapping)
1533                 {
1534                         R_Mesh_TexBindRectangle(GL20TU_SHADOWMAPRECT, R_GetTexture(r_shadow_shadowmaprectangletexture));
1535                         CHECKGLERROR
1536                         R_Mesh_TexBindCubeMap(GL20TU_CUBEPROJECTION, R_GetTexture(r_shadow_shadowmapcubeprojectiontexture[r_shadow_shadowmaplod]));
1537                         CHECKGLERROR
1538                 }
1539         }
1540         else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_LIGHT_VERTEX)
1541                 R_Mesh_ColorPointer(rsurface.array_color4f, 0, 0);
1542         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1543         CHECKGLERROR
1544 }
1545
1546 void R_Shadow_RenderMode_VisibleShadowVolumes(void)
1547 {
1548         CHECKGLERROR
1549         R_Shadow_RenderMode_Reset();
1550         GL_BlendFunc(GL_ONE, GL_ONE);
1551         GL_DepthRange(0, 1);
1552         GL_DepthTest(r_showshadowvolumes.integer < 2);
1553         GL_Color(0.0, 0.0125 * r_refdef.view.colorscale, 0.1 * r_refdef.view.colorscale, 1);
1554         GL_PolygonOffset(r_refdef.shadowpolygonfactor, r_refdef.shadowpolygonoffset);CHECKGLERROR
1555         GL_CullFace(GL_NONE);
1556         r_shadow_rendermode = R_SHADOW_RENDERMODE_VISIBLEVOLUMES;
1557 }
1558
1559 void R_Shadow_RenderMode_VisibleLighting(qboolean stenciltest, qboolean transparent)
1560 {
1561         CHECKGLERROR
1562         R_Shadow_RenderMode_Reset();
1563         GL_BlendFunc(GL_ONE, GL_ONE);
1564         GL_DepthRange(0, 1);
1565         GL_DepthTest(r_showlighting.integer < 2);
1566         GL_Color(0.1 * r_refdef.view.colorscale, 0.0125 * r_refdef.view.colorscale, 0, 1);
1567         if (!transparent)
1568         {
1569                 qglDepthFunc(GL_EQUAL);CHECKGLERROR
1570         }
1571         if (stenciltest)
1572         {
1573                 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
1574                 qglStencilFunc(GL_EQUAL, 128, ~0);CHECKGLERROR
1575         }
1576         r_shadow_rendermode = R_SHADOW_RENDERMODE_VISIBLELIGHTING;
1577 }
1578
1579 void R_Shadow_RenderMode_End(void)
1580 {
1581         CHECKGLERROR
1582         R_Shadow_RenderMode_Reset();
1583         R_Shadow_RenderMode_ActiveLight(NULL);
1584         GL_DepthMask(true);
1585         GL_Scissor(r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
1586         r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
1587 }
1588
1589 int bboxedges[12][2] =
1590 {
1591         // top
1592         {0, 1}, // +X
1593         {0, 2}, // +Y
1594         {1, 3}, // Y, +X
1595         {2, 3}, // X, +Y
1596         // bottom
1597         {4, 5}, // +X
1598         {4, 6}, // +Y
1599         {5, 7}, // Y, +X
1600         {6, 7}, // X, +Y
1601         // verticals
1602         {0, 4}, // +Z
1603         {1, 5}, // X, +Z
1604         {2, 6}, // Y, +Z
1605         {3, 7}, // XY, +Z
1606 };
1607
1608 qboolean R_Shadow_ScissorForBBox(const float *mins, const float *maxs)
1609 {
1610         int i, ix1, iy1, ix2, iy2;
1611         float x1, y1, x2, y2;
1612         vec4_t v, v2;
1613         float vertex[20][3];
1614         int j, k;
1615         vec4_t plane4f;
1616         int numvertices;
1617         float corner[8][4];
1618         float dist[8];
1619         int sign[8];
1620         float f;
1621
1622         r_shadow_lightscissor[0] = r_refdef.view.viewport.x;
1623         r_shadow_lightscissor[1] = r_refdef.view.viewport.y;
1624         r_shadow_lightscissor[2] = r_refdef.view.viewport.width;
1625         r_shadow_lightscissor[3] = r_refdef.view.viewport.height;
1626
1627         if (!r_shadow_scissor.integer)
1628                 return false;
1629
1630         // if view is inside the light box, just say yes it's visible
1631         if (BoxesOverlap(r_refdef.view.origin, r_refdef.view.origin, mins, maxs))
1632                 return false;
1633
1634         x1 = y1 = x2 = y2 = 0;
1635
1636         // transform all corners that are infront of the nearclip plane
1637         VectorNegate(r_refdef.view.frustum[4].normal, plane4f);
1638         plane4f[3] = r_refdef.view.frustum[4].dist;
1639         numvertices = 0;
1640         for (i = 0;i < 8;i++)
1641         {
1642                 Vector4Set(corner[i], (i & 1) ? maxs[0] : mins[0], (i & 2) ? maxs[1] : mins[1], (i & 4) ? maxs[2] : mins[2], 1);
1643                 dist[i] = DotProduct4(corner[i], plane4f);
1644                 sign[i] = dist[i] > 0;
1645                 if (!sign[i])
1646                 {
1647                         VectorCopy(corner[i], vertex[numvertices]);
1648                         numvertices++;
1649                 }
1650         }
1651         // if some points are behind the nearclip, add clipped edge points to make
1652         // sure that the scissor boundary is complete
1653         if (numvertices > 0 && numvertices < 8)
1654         {
1655                 // add clipped edge points
1656                 for (i = 0;i < 12;i++)
1657                 {
1658                         j = bboxedges[i][0];
1659                         k = bboxedges[i][1];
1660                         if (sign[j] != sign[k])
1661                         {
1662                                 f = dist[j] / (dist[j] - dist[k]);
1663                                 VectorLerp(corner[j], f, corner[k], vertex[numvertices]);
1664                                 numvertices++;
1665                         }
1666                 }
1667         }
1668
1669         // if we have no points to check, the light is behind the view plane
1670         if (!numvertices)
1671                 return true;
1672
1673         // if we have some points to transform, check what screen area is covered
1674         x1 = y1 = x2 = y2 = 0;
1675         v[3] = 1.0f;
1676         //Con_Printf("%i vertices to transform...\n", numvertices);
1677         for (i = 0;i < numvertices;i++)
1678         {
1679                 VectorCopy(vertex[i], v);
1680                 R_Viewport_TransformToScreen(&r_refdef.view.viewport, v, v2);
1681                 //Con_Printf("%.3f %.3f %.3f %.3f transformed to %.3f %.3f %.3f %.3f\n", v[0], v[1], v[2], v[3], v2[0], v2[1], v2[2], v2[3]);
1682                 if (i)
1683                 {
1684                         if (x1 > v2[0]) x1 = v2[0];
1685                         if (x2 < v2[0]) x2 = v2[0];
1686                         if (y1 > v2[1]) y1 = v2[1];
1687                         if (y2 < v2[1]) y2 = v2[1];
1688                 }
1689                 else
1690                 {
1691                         x1 = x2 = v2[0];
1692                         y1 = y2 = v2[1];
1693                 }
1694         }
1695
1696         // now convert the scissor rectangle to integer screen coordinates
1697         ix1 = (int)(x1 - 1.0f);
1698         iy1 = vid.height - (int)(y2 - 1.0f);
1699         ix2 = (int)(x2 + 1.0f);
1700         iy2 = vid.height - (int)(y1 + 1.0f);
1701         //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
1702
1703         // clamp it to the screen
1704         if (ix1 < r_refdef.view.viewport.x) ix1 = r_refdef.view.viewport.x;
1705         if (iy1 < r_refdef.view.viewport.y) iy1 = r_refdef.view.viewport.y;
1706         if (ix2 > r_refdef.view.viewport.x + r_refdef.view.viewport.width) ix2 = r_refdef.view.viewport.x + r_refdef.view.viewport.width;
1707         if (iy2 > r_refdef.view.viewport.y + r_refdef.view.viewport.height) iy2 = r_refdef.view.viewport.y + r_refdef.view.viewport.height;
1708
1709         // if it is inside out, it's not visible
1710         if (ix2 <= ix1 || iy2 <= iy1)
1711                 return true;
1712
1713         // the light area is visible, set up the scissor rectangle
1714         r_shadow_lightscissor[0] = ix1;
1715         r_shadow_lightscissor[1] = iy1;
1716         r_shadow_lightscissor[2] = ix2 - ix1;
1717         r_shadow_lightscissor[3] = iy2 - iy1;
1718
1719         r_refdef.stats.lights_scissored++;
1720         return false;
1721 }
1722
1723 static void R_Shadow_RenderLighting_Light_Vertex_Shading(int firstvertex, int numverts, int numtriangles, const int *element3i, const float *diffusecolor, const float *ambientcolor)
1724 {
1725         float *vertex3f = rsurface.vertex3f + 3 * firstvertex;
1726         float *normal3f = rsurface.normal3f + 3 * firstvertex;
1727         float *color4f = rsurface.array_color4f + 4 * firstvertex;
1728         float dist, dot, distintensity, shadeintensity, v[3], n[3];
1729         if (r_textureunits.integer >= 3)
1730         {
1731                 if (VectorLength2(diffusecolor) > 0)
1732                 {
1733                         for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
1734                         {
1735                                 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
1736                                 Matrix4x4_Transform3x3(&rsurface.entitytolight, normal3f, n);
1737                                 if ((dot = DotProduct(n, v)) < 0)
1738                                 {
1739                                         shadeintensity = -dot / sqrt(VectorLength2(v) * VectorLength2(n));
1740                                         VectorMA(ambientcolor, shadeintensity, diffusecolor, color4f);
1741                                 }
1742                                 else
1743                                         VectorCopy(ambientcolor, color4f);
1744                                 if (r_refdef.fogenabled)
1745                                 {
1746                                         float f;
1747                                         f = FogPoint_Model(vertex3f);
1748                                         VectorScale(color4f, f, color4f);
1749                                 }
1750                                 color4f[3] = 1;
1751                         }
1752                 }
1753                 else
1754                 {
1755                         for (;numverts > 0;numverts--, vertex3f += 3, color4f += 4)
1756                         {
1757                                 VectorCopy(ambientcolor, color4f);
1758                                 if (r_refdef.fogenabled)
1759                                 {
1760                                         float f;
1761                                         Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
1762                                         f = FogPoint_Model(vertex3f);
1763                                         VectorScale(color4f, f, color4f);
1764                                 }
1765                                 color4f[3] = 1;
1766                         }
1767                 }
1768         }
1769         else if (r_textureunits.integer >= 2)
1770         {
1771                 if (VectorLength2(diffusecolor) > 0)
1772                 {
1773                         for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
1774                         {
1775                                 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
1776                                 if ((dist = fabs(v[2])) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
1777                                 {
1778                                         Matrix4x4_Transform3x3(&rsurface.entitytolight, normal3f, n);
1779                                         if ((dot = DotProduct(n, v)) < 0)
1780                                         {
1781                                                 shadeintensity = -dot / sqrt(VectorLength2(v) * VectorLength2(n));
1782                                                 color4f[0] = (ambientcolor[0] + shadeintensity * diffusecolor[0]) * distintensity;
1783                                                 color4f[1] = (ambientcolor[1] + shadeintensity * diffusecolor[1]) * distintensity;
1784                                                 color4f[2] = (ambientcolor[2] + shadeintensity * diffusecolor[2]) * distintensity;
1785                                         }
1786                                         else
1787                                         {
1788                                                 color4f[0] = ambientcolor[0] * distintensity;
1789                                                 color4f[1] = ambientcolor[1] * distintensity;
1790                                                 color4f[2] = ambientcolor[2] * distintensity;
1791                                         }
1792                                         if (r_refdef.fogenabled)
1793                                         {
1794                                                 float f;
1795                                                 f = FogPoint_Model(vertex3f);
1796                                                 VectorScale(color4f, f, color4f);
1797                                         }
1798                                 }
1799                                 else
1800                                         VectorClear(color4f);
1801                                 color4f[3] = 1;
1802                         }
1803                 }
1804                 else
1805                 {
1806                         for (;numverts > 0;numverts--, vertex3f += 3, color4f += 4)
1807                         {
1808                                 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
1809                                 if ((dist = fabs(v[2])) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
1810                                 {
1811                                         color4f[0] = ambientcolor[0] * distintensity;
1812                                         color4f[1] = ambientcolor[1] * distintensity;
1813                                         color4f[2] = ambientcolor[2] * distintensity;
1814                                         if (r_refdef.fogenabled)
1815                                         {
1816                                                 float f;
1817                                                 f = FogPoint_Model(vertex3f);
1818                                                 VectorScale(color4f, f, color4f);
1819                                         }
1820                                 }
1821                                 else
1822                                         VectorClear(color4f);
1823                                 color4f[3] = 1;
1824                         }
1825                 }
1826         }
1827         else
1828         {
1829                 if (VectorLength2(diffusecolor) > 0)
1830                 {
1831                         for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
1832                         {
1833                                 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
1834                                 if ((dist = VectorLength(v)) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
1835                                 {
1836                                         distintensity = (1 - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist);
1837                                         Matrix4x4_Transform3x3(&rsurface.entitytolight, normal3f, n);
1838                                         if ((dot = DotProduct(n, v)) < 0)
1839                                         {
1840                                                 shadeintensity = -dot / sqrt(VectorLength2(v) * VectorLength2(n));
1841                                                 color4f[0] = (ambientcolor[0] + shadeintensity * diffusecolor[0]) * distintensity;
1842                                                 color4f[1] = (ambientcolor[1] + shadeintensity * diffusecolor[1]) * distintensity;
1843                                                 color4f[2] = (ambientcolor[2] + shadeintensity * diffusecolor[2]) * distintensity;
1844                                         }
1845                                         else
1846                                         {
1847                                                 color4f[0] = ambientcolor[0] * distintensity;
1848                                                 color4f[1] = ambientcolor[1] * distintensity;
1849                                                 color4f[2] = ambientcolor[2] * distintensity;
1850                                         }
1851                                         if (r_refdef.fogenabled)
1852                                         {
1853                                                 float f;
1854                                                 f = FogPoint_Model(vertex3f);
1855                                                 VectorScale(color4f, f, color4f);
1856                                         }
1857                                 }
1858                                 else
1859                                         VectorClear(color4f);
1860                                 color4f[3] = 1;
1861                         }
1862                 }
1863                 else
1864                 {
1865                         for (;numverts > 0;numverts--, vertex3f += 3, color4f += 4)
1866                         {
1867                                 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
1868                                 if ((dist = VectorLength(v)) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
1869                                 {
1870                                         distintensity = (1 - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist);
1871                                         color4f[0] = ambientcolor[0] * distintensity;
1872                                         color4f[1] = ambientcolor[1] * distintensity;
1873                                         color4f[2] = ambientcolor[2] * distintensity;
1874                                         if (r_refdef.fogenabled)
1875                                         {
1876                                                 float f;
1877                                                 f = FogPoint_Model(vertex3f);
1878                                                 VectorScale(color4f, f, color4f);
1879                                         }
1880                                 }
1881                                 else
1882                                         VectorClear(color4f);
1883                                 color4f[3] = 1;
1884                         }
1885                 }
1886         }
1887 }
1888
1889 // TODO: use glTexGen instead of feeding vertices to texcoordpointer?
1890
1891 static void R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(int firstvertex, int numvertices, int numtriangles, const int *element3i)
1892 {
1893         int i;
1894         float       *out3f     = rsurface.array_texcoord3f + 3 * firstvertex;
1895         const float *vertex3f  = rsurface.vertex3f         + 3 * firstvertex;
1896         const float *svector3f = rsurface.svector3f        + 3 * firstvertex;
1897         const float *tvector3f = rsurface.tvector3f        + 3 * firstvertex;
1898         const float *normal3f  = rsurface.normal3f         + 3 * firstvertex;
1899         float lightdir[3];
1900         for (i = 0;i < numvertices;i++, vertex3f += 3, svector3f += 3, tvector3f += 3, normal3f += 3, out3f += 3)
1901         {
1902                 VectorSubtract(rsurface.entitylightorigin, vertex3f, lightdir);
1903                 // the cubemap normalizes this for us
1904                 out3f[0] = DotProduct(svector3f, lightdir);
1905                 out3f[1] = DotProduct(tvector3f, lightdir);
1906                 out3f[2] = DotProduct(normal3f, lightdir);
1907         }
1908 }
1909
1910 static void R_Shadow_GenTexCoords_Specular_NormalCubeMap(int firstvertex, int numvertices, int numtriangles, const int *element3i)
1911 {
1912         int i;
1913         float       *out3f     = rsurface.array_texcoord3f + 3 * firstvertex;
1914         const float *vertex3f  = rsurface.vertex3f         + 3 * firstvertex;
1915         const float *svector3f = rsurface.svector3f        + 3 * firstvertex;
1916         const float *tvector3f = rsurface.tvector3f        + 3 * firstvertex;
1917         const float *normal3f  = rsurface.normal3f         + 3 * firstvertex;
1918         float lightdir[3], eyedir[3], halfdir[3];
1919         for (i = 0;i < numvertices;i++, vertex3f += 3, svector3f += 3, tvector3f += 3, normal3f += 3, out3f += 3)
1920         {
1921                 VectorSubtract(rsurface.entitylightorigin, vertex3f, lightdir);
1922                 VectorNormalize(lightdir);
1923                 VectorSubtract(rsurface.modelorg, vertex3f, eyedir);
1924                 VectorNormalize(eyedir);
1925                 VectorAdd(lightdir, eyedir, halfdir);
1926                 // the cubemap normalizes this for us
1927                 out3f[0] = DotProduct(svector3f, halfdir);
1928                 out3f[1] = DotProduct(tvector3f, halfdir);
1929                 out3f[2] = DotProduct(normal3f, halfdir);
1930         }
1931 }
1932
1933 static void R_Shadow_RenderLighting_VisibleLighting(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int element3i_bufferobject, int element3s_bufferobject, const vec3_t lightcolorbase, const vec3_t lightcolorpants, const vec3_t lightcolorshirt, rtexture_t *basetexture, rtexture_t *pantstexture, rtexture_t *shirttexture, rtexture_t *normalmaptexture, rtexture_t *glosstexture, float ambientscale, float diffusescale, float specularscale, qboolean dopants, qboolean doshirt)
1934 {
1935         // used to display how many times a surface is lit for level design purposes
1936         R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
1937 }
1938
1939 static void R_Shadow_RenderLighting_Light_GLSL(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int element3i_bufferobject, int element3s_bufferobject, const vec3_t lightcolorbase, const vec3_t lightcolorpants, const vec3_t lightcolorshirt, rtexture_t *basetexture, rtexture_t *pantstexture, rtexture_t *shirttexture, rtexture_t *normalmaptexture, rtexture_t *glosstexture, float ambientscale, float diffusescale, float specularscale, qboolean dopants, qboolean doshirt)
1940 {
1941         // ARB2 GLSL shader path (GFFX5200, Radeon 9500)
1942         R_SetupSurfaceShader(lightcolorbase, false, ambientscale, diffusescale, specularscale, RSURFPASS_RTLIGHT);
1943         if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND))
1944                 R_Mesh_ColorPointer(rsurface.modellightmapcolor4f, rsurface.modellightmapcolor4f_bufferobject, rsurface.modellightmapcolor4f_bufferoffset);
1945         else
1946                 R_Mesh_ColorPointer(NULL, 0, 0);
1947         R_Mesh_TexMatrix(0, &rsurface.texture->currenttexmatrix);
1948         R_Mesh_TexMatrix(1, &rsurface.texture->currentbackgroundtexmatrix);
1949         R_Mesh_TexBind(GL20TU_NORMAL, R_GetTexture(rsurface.texture->currentskinframe->nmap));
1950         R_Mesh_TexBind(GL20TU_COLOR, R_GetTexture(rsurface.texture->basetexture));
1951         R_Mesh_TexBind(GL20TU_GLOSS, R_GetTexture(rsurface.texture->glosstexture));
1952         if (rsurface.texture->backgroundcurrentskinframe)
1953         {
1954                 R_Mesh_TexBind(GL20TU_SECONDARY_NORMAL, R_GetTexture(rsurface.texture->backgroundcurrentskinframe->nmap));
1955                 R_Mesh_TexBind(GL20TU_SECONDARY_COLOR, R_GetTexture(rsurface.texture->backgroundbasetexture));
1956                 R_Mesh_TexBind(GL20TU_SECONDARY_GLOSS, R_GetTexture(rsurface.texture->backgroundglosstexture));
1957                 R_Mesh_TexBind(GL20TU_SECONDARY_GLOW, R_GetTexture(rsurface.texture->backgroundcurrentskinframe->glow));
1958         }
1959         //R_Mesh_TexBindCubeMap(GL20TU_CUBE, R_GetTexture(rsurface.rtlight->currentcubemap));
1960         R_Mesh_TexBind(GL20TU_FOGMASK, R_GetTexture(r_texture_fogattenuation));
1961         if(rsurface.texture->colormapping)
1962         {
1963                 R_Mesh_TexBind(GL20TU_PANTS, R_GetTexture(rsurface.texture->currentskinframe->pants));
1964                 R_Mesh_TexBind(GL20TU_SHIRT, R_GetTexture(rsurface.texture->currentskinframe->shirt));
1965         }
1966         R_Mesh_TexBind(GL20TU_ATTENUATION, R_GetTexture(r_shadow_attenuationgradienttexture));
1967         R_Mesh_TexCoordPointer(0, 2, rsurface.texcoordtexture2f, rsurface.texcoordtexture2f_bufferobject, rsurface.texcoordtexture2f_bufferoffset);
1968         R_Mesh_TexCoordPointer(1, 3, rsurface.svector3f, rsurface.svector3f_bufferobject, rsurface.svector3f_bufferoffset);
1969         R_Mesh_TexCoordPointer(2, 3, rsurface.tvector3f, rsurface.tvector3f_bufferobject, rsurface.tvector3f_bufferoffset);
1970         R_Mesh_TexCoordPointer(3, 3, rsurface.normal3f, rsurface.normal3f_bufferobject, rsurface.normal3f_bufferoffset);
1971         if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
1972         {
1973                 qglDepthFunc(GL_EQUAL);CHECKGLERROR
1974         }
1975         R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
1976         if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
1977         {
1978                 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
1979         }
1980 }
1981
1982 static void R_Shadow_RenderLighting_Light_Dot3_Finalize(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int element3i_bufferobject, int element3s_bufferobject, float r, float g, float b)
1983 {
1984         // shared final code for all the dot3 layers
1985         int renders;
1986         GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 0);
1987         for (renders = 0;renders < 64 && (r > 0 || g > 0 || b > 0);renders++, r--, g--, b--)
1988         {
1989                 GL_Color(bound(0, r, 1), bound(0, g, 1), bound(0, b, 1), 1);
1990                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
1991         }
1992 }
1993
1994 static void R_Shadow_RenderLighting_Light_Dot3_AmbientPass(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int element3i_bufferobject, int element3s_bufferobject, const vec3_t lightcolorbase, rtexture_t *basetexture, float colorscale)
1995 {
1996         rmeshstate_t m;
1997         // colorscale accounts for how much we multiply the brightness
1998         // during combine.
1999         //
2000         // mult is how many times the final pass of the lighting will be
2001         // performed to get more brightness than otherwise possible.
2002         //
2003         // Limit mult to 64 for sanity sake.
2004         GL_Color(1,1,1,1);
2005         if (r_shadow_texture3d.integer && rsurface.rtlight->currentcubemap != r_texture_whitecube && r_textureunits.integer >= 4)
2006         {
2007                 // 3 3D combine path (Geforce3, Radeon 8500)
2008                 memset(&m, 0, sizeof(m));
2009                 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
2010                 m.pointer_texcoord3f[0] = rsurface.vertex3f;
2011                 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
2012                 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
2013                 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
2014                 m.tex[1] = R_GetTexture(basetexture);
2015                 m.pointer_texcoord[1] = rsurface.texcoordtexture2f;
2016                 m.pointer_texcoord_bufferobject[1] = rsurface.texcoordtexture2f_bufferobject;
2017                 m.pointer_texcoord_bufferoffset[1] = rsurface.texcoordtexture2f_bufferoffset;
2018                 m.texmatrix[1] = rsurface.texture->currenttexmatrix;
2019                 m.texcubemap[2] = R_GetTexture(rsurface.rtlight->currentcubemap);
2020                 m.pointer_texcoord3f[2] = rsurface.vertex3f;
2021                 m.pointer_texcoord_bufferobject[2] = rsurface.vertex3f_bufferobject;
2022                 m.pointer_texcoord_bufferoffset[2] = rsurface.vertex3f_bufferoffset;
2023                 m.texmatrix[2] = rsurface.entitytolight;
2024                 GL_BlendFunc(GL_ONE, GL_ONE);
2025         }
2026         else if (r_shadow_texture3d.integer && rsurface.rtlight->currentcubemap == r_texture_whitecube && r_textureunits.integer >= 2)
2027         {
2028                 // 2 3D combine path (Geforce3, original Radeon)
2029                 memset(&m, 0, sizeof(m));
2030                 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
2031                 m.pointer_texcoord3f[0] = rsurface.vertex3f;
2032                 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
2033                 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
2034                 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
2035                 m.tex[1] = R_GetTexture(basetexture);
2036                 m.pointer_texcoord[1] = rsurface.texcoordtexture2f;
2037                 m.pointer_texcoord_bufferobject[1] = rsurface.texcoordtexture2f_bufferobject;
2038                 m.pointer_texcoord_bufferoffset[1] = rsurface.texcoordtexture2f_bufferoffset;
2039                 m.texmatrix[1] = rsurface.texture->currenttexmatrix;
2040                 GL_BlendFunc(GL_ONE, GL_ONE);
2041         }
2042         else if (r_textureunits.integer >= 4 && rsurface.rtlight->currentcubemap != r_texture_whitecube)
2043         {
2044                 // 4 2D combine path (Geforce3, Radeon 8500)
2045                 memset(&m, 0, sizeof(m));
2046                 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
2047                 m.pointer_texcoord3f[0] = rsurface.vertex3f;
2048                 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
2049                 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
2050                 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
2051                 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
2052                 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2053                 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2054                 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2055                 m.texmatrix[1] = rsurface.entitytoattenuationz;
2056                 m.tex[2] = R_GetTexture(basetexture);
2057                 m.pointer_texcoord[2] = rsurface.texcoordtexture2f;
2058                 m.pointer_texcoord_bufferobject[2] = rsurface.texcoordtexture2f_bufferobject;
2059                 m.pointer_texcoord_bufferoffset[2] = rsurface.texcoordtexture2f_bufferoffset;
2060                 m.texmatrix[2] = rsurface.texture->currenttexmatrix;
2061                 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2062                 {
2063                         m.texcubemap[3] = R_GetTexture(rsurface.rtlight->currentcubemap);
2064                         m.pointer_texcoord3f[3] = rsurface.vertex3f;
2065                         m.pointer_texcoord_bufferobject[3] = rsurface.vertex3f_bufferobject;
2066                         m.pointer_texcoord_bufferoffset[3] = rsurface.vertex3f_bufferoffset;
2067                         m.texmatrix[3] = rsurface.entitytolight;
2068                 }
2069                 GL_BlendFunc(GL_ONE, GL_ONE);
2070         }
2071         else if (r_textureunits.integer >= 3 && rsurface.rtlight->currentcubemap == r_texture_whitecube)
2072         {
2073                 // 3 2D combine path (Geforce3, original Radeon)
2074                 memset(&m, 0, sizeof(m));
2075                 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
2076                 m.pointer_texcoord3f[0] = rsurface.vertex3f;
2077                 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
2078                 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
2079                 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
2080                 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
2081                 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2082                 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2083                 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2084                 m.texmatrix[1] = rsurface.entitytoattenuationz;
2085                 m.tex[2] = R_GetTexture(basetexture);
2086                 m.pointer_texcoord[2] = rsurface.texcoordtexture2f;
2087                 m.pointer_texcoord_bufferobject[2] = rsurface.texcoordtexture2f_bufferobject;
2088                 m.pointer_texcoord_bufferoffset[2] = rsurface.texcoordtexture2f_bufferoffset;
2089                 m.texmatrix[2] = rsurface.texture->currenttexmatrix;
2090                 GL_BlendFunc(GL_ONE, GL_ONE);
2091         }
2092         else
2093         {
2094                 // 2/2/2 2D combine path (any dot3 card)
2095                 memset(&m, 0, sizeof(m));
2096                 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
2097                 m.pointer_texcoord3f[0] = rsurface.vertex3f;
2098                 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
2099                 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
2100                 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
2101                 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
2102                 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2103                 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2104                 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2105                 m.texmatrix[1] = rsurface.entitytoattenuationz;
2106                 R_Mesh_TextureState(&m);
2107                 GL_ColorMask(0,0,0,1);
2108                 GL_BlendFunc(GL_ONE, GL_ZERO);
2109                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2110
2111                 // second pass
2112                 memset(&m, 0, sizeof(m));
2113                 m.tex[0] = R_GetTexture(basetexture);
2114                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2115                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2116                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2117                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2118                 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2119                 {
2120                         m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
2121                         m.pointer_texcoord3f[1] = rsurface.vertex3f;
2122                         m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2123                         m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2124                         m.texmatrix[1] = rsurface.entitytolight;
2125                 }
2126                 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2127         }
2128         // this final code is shared
2129         R_Mesh_TextureState(&m);
2130         R_Shadow_RenderLighting_Light_Dot3_Finalize(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase[0] * colorscale, lightcolorbase[1] * colorscale, lightcolorbase[2] * colorscale);
2131 }
2132
2133 static void R_Shadow_RenderLighting_Light_Dot3_DiffusePass(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int element3i_bufferobject, int element3s_bufferobject, const vec3_t lightcolorbase, rtexture_t *basetexture, rtexture_t *normalmaptexture, float colorscale)
2134 {
2135         rmeshstate_t m;
2136         // colorscale accounts for how much we multiply the brightness
2137         // during combine.
2138         //
2139         // mult is how many times the final pass of the lighting will be
2140         // performed to get more brightness than otherwise possible.
2141         //
2142         // Limit mult to 64 for sanity sake.
2143         GL_Color(1,1,1,1);
2144         // generate normalization cubemap texcoords
2145         R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(firstvertex, numvertices, numtriangles, element3i);
2146         if (r_shadow_texture3d.integer && r_textureunits.integer >= 4)
2147         {
2148                 // 3/2 3D combine path (Geforce3, Radeon 8500)
2149                 memset(&m, 0, sizeof(m));
2150                 m.tex[0] = R_GetTexture(normalmaptexture);
2151                 m.texcombinergb[0] = GL_REPLACE;
2152                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2153                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2154                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2155                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2156                 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
2157                 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2158                 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
2159                 m.pointer_texcoord_bufferobject[1] = 0;
2160                 m.pointer_texcoord_bufferoffset[1] = 0;
2161                 m.tex3d[2] = R_GetTexture(r_shadow_attenuation3dtexture);
2162                 m.pointer_texcoord3f[2] = rsurface.vertex3f;
2163                 m.pointer_texcoord_bufferobject[2] = rsurface.vertex3f_bufferobject;
2164                 m.pointer_texcoord_bufferoffset[2] = rsurface.vertex3f_bufferoffset;
2165                 m.texmatrix[2] = rsurface.entitytoattenuationxyz;
2166                 R_Mesh_TextureState(&m);
2167                 GL_ColorMask(0,0,0,1);
2168                 GL_BlendFunc(GL_ONE, GL_ZERO);
2169                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2170
2171                 // second pass
2172                 memset(&m, 0, sizeof(m));
2173                 m.tex[0] = R_GetTexture(basetexture);
2174                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2175                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2176                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2177                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2178                 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2179                 {
2180                         m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
2181                         m.pointer_texcoord3f[1] = rsurface.vertex3f;
2182                         m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2183                         m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2184                         m.texmatrix[1] = rsurface.entitytolight;
2185                 }
2186                 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2187         }
2188         else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && rsurface.rtlight->currentcubemap != r_texture_whitecube)
2189         {
2190                 // 1/2/2 3D combine path (original Radeon)
2191                 memset(&m, 0, sizeof(m));
2192                 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
2193                 m.pointer_texcoord3f[0] = rsurface.vertex3f;
2194                 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
2195                 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
2196                 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
2197                 R_Mesh_TextureState(&m);
2198                 GL_ColorMask(0,0,0,1);
2199                 GL_BlendFunc(GL_ONE, GL_ZERO);
2200                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2201
2202                 // second pass
2203                 memset(&m, 0, sizeof(m));
2204                 m.tex[0] = R_GetTexture(normalmaptexture);
2205                 m.texcombinergb[0] = GL_REPLACE;
2206                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2207                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2208                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2209                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2210                 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
2211                 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2212                 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
2213                 m.pointer_texcoord_bufferobject[1] = 0;
2214                 m.pointer_texcoord_bufferoffset[1] = 0;
2215                 R_Mesh_TextureState(&m);
2216                 GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
2217                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2218
2219                 // second pass
2220                 memset(&m, 0, sizeof(m));
2221                 m.tex[0] = R_GetTexture(basetexture);
2222                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2223                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2224                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2225                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2226                 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2227                 {
2228                         m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
2229                         m.pointer_texcoord3f[1] = rsurface.vertex3f;
2230                         m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2231                         m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2232                         m.texmatrix[1] = rsurface.entitytolight;
2233                 }
2234                 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2235         }
2236         else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && rsurface.rtlight->currentcubemap == r_texture_whitecube)
2237         {
2238                 // 2/2 3D combine path (original Radeon)
2239                 memset(&m, 0, sizeof(m));
2240                 m.tex[0] = R_GetTexture(normalmaptexture);
2241                 m.texcombinergb[0] = GL_REPLACE;
2242                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2243                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2244                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2245                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2246                 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
2247                 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2248                 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
2249                 m.pointer_texcoord_bufferobject[1] = 0;
2250                 m.pointer_texcoord_bufferoffset[1] = 0;
2251                 R_Mesh_TextureState(&m);
2252                 GL_ColorMask(0,0,0,1);
2253                 GL_BlendFunc(GL_ONE, GL_ZERO);
2254                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2255
2256                 // second pass
2257                 memset(&m, 0, sizeof(m));
2258                 m.tex[0] = R_GetTexture(basetexture);
2259                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2260                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2261                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2262                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2263                 m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
2264                 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2265                 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2266                 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2267                 m.texmatrix[1] = rsurface.entitytoattenuationxyz;
2268                 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2269         }
2270         else if (r_textureunits.integer >= 4)
2271         {
2272                 // 4/2 2D combine path (Geforce3, Radeon 8500)
2273                 memset(&m, 0, sizeof(m));
2274                 m.tex[0] = R_GetTexture(normalmaptexture);
2275                 m.texcombinergb[0] = GL_REPLACE;
2276                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2277                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2278                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2279                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2280                 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
2281                 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2282                 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
2283                 m.pointer_texcoord_bufferobject[1] = 0;
2284                 m.pointer_texcoord_bufferoffset[1] = 0;
2285                 m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
2286                 m.pointer_texcoord3f[2] = rsurface.vertex3f;
2287                 m.pointer_texcoord_bufferobject[2] = rsurface.vertex3f_bufferobject;
2288                 m.pointer_texcoord_bufferoffset[2] = rsurface.vertex3f_bufferoffset;
2289                 m.texmatrix[2] = rsurface.entitytoattenuationxyz;
2290                 m.tex[3] = R_GetTexture(r_shadow_attenuation2dtexture);
2291                 m.pointer_texcoord3f[3] = rsurface.vertex3f;
2292                 m.pointer_texcoord_bufferobject[3] = rsurface.vertex3f_bufferobject;
2293                 m.pointer_texcoord_bufferoffset[3] = rsurface.vertex3f_bufferoffset;
2294                 m.texmatrix[3] = rsurface.entitytoattenuationz;
2295                 R_Mesh_TextureState(&m);
2296                 GL_ColorMask(0,0,0,1);
2297                 GL_BlendFunc(GL_ONE, GL_ZERO);
2298                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2299
2300                 // second pass
2301                 memset(&m, 0, sizeof(m));
2302                 m.tex[0] = R_GetTexture(basetexture);
2303                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2304                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2305                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2306                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2307                 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2308                 {
2309                         m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
2310                         m.pointer_texcoord3f[1] = rsurface.vertex3f;
2311                         m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2312                         m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2313                         m.texmatrix[1] = rsurface.entitytolight;
2314                 }
2315                 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2316         }
2317         else
2318         {
2319                 // 2/2/2 2D combine path (any dot3 card)
2320                 memset(&m, 0, sizeof(m));
2321                 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
2322                 m.pointer_texcoord3f[0] = rsurface.vertex3f;
2323                 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
2324                 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
2325                 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
2326                 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
2327                 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2328                 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
2329                 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
2330                 m.texmatrix[1] = rsurface.entitytoattenuationz;
2331                 R_Mesh_TextureState(&m);
2332                 GL_ColorMask(0,0,0,1);
2333                 GL_BlendFunc(GL_ONE, GL_ZERO);
2334                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2335
2336                 // second pass
2337                 memset(&m, 0, sizeof(m));
2338                 m.tex[0] = R_GetTexture(normalmaptexture);
2339                 m.texcombinergb[0] = GL_REPLACE;
2340                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2341                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2342                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2343                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2344                 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
2345                 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2346                 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
2347                 m.pointer_texcoord_bufferobject[1] = 0;
2348                 m.pointer_texcoord_bufferoffset[1] = 0;
2349                 R_Mesh_TextureState(&m);
2350                 GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
2351                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2352
2353                 // second pass
2354                 memset(&m, 0, sizeof(m));
2355                 m.tex[0] = R_GetTexture(basetexture);
2356                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2357                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2358                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2359                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2360                 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2361                 {
2362                         m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
2363                         m.pointer_texcoord3f[1] = rsurface.vertex3f;
2364                         m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2365                         m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2366                         m.texmatrix[1] = rsurface.entitytolight;
2367                 }
2368                 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2369         }
2370         // this final code is shared
2371         R_Mesh_TextureState(&m);
2372         R_Shadow_RenderLighting_Light_Dot3_Finalize(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase[0] * colorscale, lightcolorbase[1] * colorscale, lightcolorbase[2] * colorscale);
2373 }
2374
2375 static void R_Shadow_RenderLighting_Light_Dot3_SpecularPass(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int element3i_bufferobject, int element3s_bufferobject, const vec3_t lightcolorbase, rtexture_t *glosstexture, rtexture_t *normalmaptexture, float colorscale)
2376 {
2377         float glossexponent;
2378         rmeshstate_t m;
2379         // FIXME: detect blendsquare!
2380         //if (!gl_support_blendsquare)
2381         //      return;
2382         GL_Color(1,1,1,1);
2383         // generate normalization cubemap texcoords
2384         R_Shadow_GenTexCoords_Specular_NormalCubeMap(firstvertex, numvertices, numtriangles, element3i);
2385         if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && rsurface.rtlight->currentcubemap != r_texture_whitecube)
2386         {
2387                 // 2/0/0/1/2 3D combine blendsquare path
2388                 memset(&m, 0, sizeof(m));
2389                 m.tex[0] = R_GetTexture(normalmaptexture);
2390                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2391                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2392                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2393                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2394                 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
2395                 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2396                 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
2397                 m.pointer_texcoord_bufferobject[1] = 0;
2398                 m.pointer_texcoord_bufferoffset[1] = 0;
2399                 R_Mesh_TextureState(&m);
2400                 GL_ColorMask(0,0,0,1);
2401                 // this squares the result
2402                 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
2403                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2404
2405                 // second and third pass
2406                 R_Mesh_ResetTextureState();
2407                 // square alpha in framebuffer a few times to make it shiny
2408                 GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
2409                 for (glossexponent = 2;glossexponent * 2 <= r_shadow_glossexponent.value;glossexponent *= 2)
2410                         R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2411
2412                 // fourth pass
2413                 memset(&m, 0, sizeof(m));
2414                 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
2415                 m.pointer_texcoord3f[0] = rsurface.vertex3f;
2416                 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
2417                 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
2418                 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
2419                 R_Mesh_TextureState(&m);
2420                 GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
2421                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2422
2423                 // fifth pass
2424                 memset(&m, 0, sizeof(m));
2425                 m.tex[0] = R_GetTexture(glosstexture);
2426                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2427                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2428                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2429                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2430                 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2431                 {
2432                         m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
2433                         m.pointer_texcoord3f[1] = rsurface.vertex3f;
2434                         m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2435                         m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2436                         m.texmatrix[1] = rsurface.entitytolight;
2437                 }
2438                 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2439         }
2440         else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && rsurface.rtlight->currentcubemap == r_texture_whitecube /* && gl_support_blendsquare*/) // FIXME: detect blendsquare!
2441         {
2442                 // 2/0/0/2 3D combine blendsquare path
2443                 memset(&m, 0, sizeof(m));
2444                 m.tex[0] = R_GetTexture(normalmaptexture);
2445                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2446                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2447                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2448                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2449                 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
2450                 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2451                 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
2452                 m.pointer_texcoord_bufferobject[1] = 0;
2453                 m.pointer_texcoord_bufferoffset[1] = 0;
2454                 R_Mesh_TextureState(&m);
2455                 GL_ColorMask(0,0,0,1);
2456                 // this squares the result
2457                 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
2458                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2459
2460                 // second and third pass
2461                 R_Mesh_ResetTextureState();
2462                 // square alpha in framebuffer a few times to make it shiny
2463                 GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
2464                 for (glossexponent = 2;glossexponent * 2 <= r_shadow_glossexponent.value;glossexponent *= 2)
2465                         R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2466
2467                 // fourth pass
2468                 memset(&m, 0, sizeof(m));
2469                 m.tex[0] = R_GetTexture(glosstexture);
2470                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2471                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2472                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2473                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2474                 m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
2475                 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2476                 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2477                 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2478                 m.texmatrix[1] = rsurface.entitytoattenuationxyz;
2479                 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2480         }
2481         else
2482         {
2483                 // 2/0/0/2/2 2D combine blendsquare path
2484                 memset(&m, 0, sizeof(m));
2485                 m.tex[0] = R_GetTexture(normalmaptexture);
2486                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2487                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2488                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2489                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2490                 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
2491                 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2492                 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
2493                 m.pointer_texcoord_bufferobject[1] = 0;
2494                 m.pointer_texcoord_bufferoffset[1] = 0;
2495                 R_Mesh_TextureState(&m);
2496                 GL_ColorMask(0,0,0,1);
2497                 // this squares the result
2498                 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
2499                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2500
2501                 // second and third pass
2502                 R_Mesh_ResetTextureState();
2503                 // square alpha in framebuffer a few times to make it shiny
2504                 GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
2505                 for (glossexponent = 2;glossexponent * 2 <= r_shadow_glossexponent.value;glossexponent *= 2)
2506                         R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2507
2508                 // fourth pass
2509                 memset(&m, 0, sizeof(m));
2510                 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
2511                 m.pointer_texcoord3f[0] = rsurface.vertex3f;
2512                 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
2513                 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
2514                 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
2515                 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
2516                 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2517                 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2518                 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2519                 m.texmatrix[1] = rsurface.entitytoattenuationz;
2520                 R_Mesh_TextureState(&m);
2521                 GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
2522                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2523
2524                 // fifth pass
2525                 memset(&m, 0, sizeof(m));
2526                 m.tex[0] = R_GetTexture(glosstexture);
2527                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2528                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2529                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2530                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2531                 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2532                 {
2533                         m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
2534                         m.pointer_texcoord3f[1] = rsurface.vertex3f;
2535                         m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2536                         m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2537                         m.texmatrix[1] = rsurface.entitytolight;
2538                 }
2539                 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2540         }
2541         // this final code is shared
2542         R_Mesh_TextureState(&m);
2543         R_Shadow_RenderLighting_Light_Dot3_Finalize(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase[0] * colorscale, lightcolorbase[1] * colorscale, lightcolorbase[2] * colorscale);
2544 }
2545
2546 static void R_Shadow_RenderLighting_Light_Dot3(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int element3i_bufferobject, int element3s_bufferobject, const vec3_t lightcolorbase, const vec3_t lightcolorpants, const vec3_t lightcolorshirt, rtexture_t *basetexture, rtexture_t *pantstexture, rtexture_t *shirttexture, rtexture_t *normalmaptexture, rtexture_t *glosstexture, float ambientscale, float diffusescale, float specularscale, qboolean dopants, qboolean doshirt)
2547 {
2548         // ARB path (any Geforce, any Radeon)
2549         qboolean doambient = ambientscale > 0;
2550         qboolean dodiffuse = diffusescale > 0;
2551         qboolean dospecular = specularscale > 0;
2552         if (!doambient && !dodiffuse && !dospecular)
2553                 return;
2554         R_Mesh_ColorPointer(NULL, 0, 0);
2555         if (doambient)
2556                 R_Shadow_RenderLighting_Light_Dot3_AmbientPass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, basetexture, ambientscale * r_refdef.view.colorscale);
2557         if (dodiffuse)
2558                 R_Shadow_RenderLighting_Light_Dot3_DiffusePass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, basetexture, normalmaptexture, diffusescale * r_refdef.view.colorscale);
2559         if (dopants)
2560         {
2561                 if (doambient)
2562                         R_Shadow_RenderLighting_Light_Dot3_AmbientPass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorpants, pantstexture, ambientscale * r_refdef.view.colorscale);
2563                 if (dodiffuse)
2564                         R_Shadow_RenderLighting_Light_Dot3_DiffusePass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorpants, pantstexture, normalmaptexture, diffusescale * r_refdef.view.colorscale);
2565         }
2566         if (doshirt)
2567         {
2568                 if (doambient)
2569                         R_Shadow_RenderLighting_Light_Dot3_AmbientPass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorshirt, shirttexture, ambientscale * r_refdef.view.colorscale);
2570                 if (dodiffuse)
2571                         R_Shadow_RenderLighting_Light_Dot3_DiffusePass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorshirt, shirttexture, normalmaptexture, diffusescale * r_refdef.view.colorscale);
2572         }
2573         if (dospecular)
2574                 R_Shadow_RenderLighting_Light_Dot3_SpecularPass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, glosstexture, normalmaptexture, specularscale * r_refdef.view.colorscale);
2575 }
2576
2577 static void R_Shadow_RenderLighting_Light_Vertex_Pass(int firstvertex, int numvertices, int numtriangles, const int *element3i, vec3_t diffusecolor2, vec3_t ambientcolor2)
2578 {
2579         int renders;
2580         int i;
2581         int stop;
2582         int newfirstvertex;
2583         int newlastvertex;
2584         int newnumtriangles;
2585         int *newe;
2586         const int *e;
2587         float *c;
2588         int maxtriangles = 4096;
2589         int newelements[4096*3];
2590         R_Shadow_RenderLighting_Light_Vertex_Shading(firstvertex, numvertices, numtriangles, element3i, diffusecolor2, ambientcolor2);
2591         for (renders = 0;renders < 64;renders++)
2592         {
2593                 stop = true;
2594                 newfirstvertex = 0;
2595                 newlastvertex = 0;
2596                 newnumtriangles = 0;
2597                 newe = newelements;
2598                 // due to low fillrate on the cards this vertex lighting path is
2599                 // designed for, we manually cull all triangles that do not
2600                 // contain a lit vertex
2601                 // this builds batches of triangles from multiple surfaces and
2602                 // renders them at once
2603                 for (i = 0, e = element3i;i < numtriangles;i++, e += 3)
2604                 {
2605                         if (VectorLength2(rsurface.array_color4f + e[0] * 4) + VectorLength2(rsurface.array_color4f + e[1] * 4) + VectorLength2(rsurface.array_color4f + e[2] * 4) >= 0.01)
2606                         {
2607                                 if (newnumtriangles)
2608                                 {
2609                                         newfirstvertex = min(newfirstvertex, e[0]);
2610                                         newlastvertex  = max(newlastvertex, e[0]);
2611                                 }
2612                                 else
2613                                 {
2614                                         newfirstvertex = e[0];
2615                                         newlastvertex = e[0];
2616                                 }
2617                                 newfirstvertex = min(newfirstvertex, e[1]);
2618                                 newlastvertex  = max(newlastvertex, e[1]);
2619                                 newfirstvertex = min(newfirstvertex, e[2]);
2620                                 newlastvertex  = max(newlastvertex, e[2]);
2621                                 newe[0] = e[0];
2622                                 newe[1] = e[1];
2623                                 newe[2] = e[2];
2624                                 newnumtriangles++;
2625                                 newe += 3;
2626                                 if (newnumtriangles >= maxtriangles)
2627                                 {
2628                                         R_Mesh_Draw(newfirstvertex, newlastvertex - newfirstvertex + 1, 0, newnumtriangles, newelements, NULL, 0, 0);
2629                                         newnumtriangles = 0;
2630                                         newe = newelements;
2631                                         stop = false;
2632                                 }
2633                         }
2634                 }
2635                 if (newnumtriangles >= 1)
2636                 {
2637                         R_Mesh_Draw(newfirstvertex, newlastvertex - newfirstvertex + 1, 0, newnumtriangles, newelements, NULL, 0, 0);
2638                         stop = false;
2639                 }
2640                 // if we couldn't find any lit triangles, exit early
2641                 if (stop)
2642                         break;
2643                 // now reduce the intensity for the next overbright pass
2644                 // we have to clamp to 0 here incase the drivers have improper
2645                 // handling of negative colors
2646                 // (some old drivers even have improper handling of >1 color)
2647                 stop = true;
2648                 for (i = 0, c = rsurface.array_color4f + 4 * firstvertex;i < numvertices;i++, c += 4)
2649                 {
2650                         if (c[0] > 1 || c[1] > 1 || c[2] > 1)
2651                         {
2652                                 c[0] = max(0, c[0] - 1);
2653                                 c[1] = max(0, c[1] - 1);
2654                                 c[2] = max(0, c[2] - 1);
2655                                 stop = false;
2656                         }
2657                         else
2658                                 VectorClear(c);
2659                 }
2660                 // another check...
2661                 if (stop)
2662                         break;
2663         }
2664 }
2665
2666 static void R_Shadow_RenderLighting_Light_Vertex(int firstvertex, int numvertices, int numtriangles, const int *element3i, const vec3_t lightcolorbase, const vec3_t lightcolorpants, const vec3_t lightcolorshirt, rtexture_t *basetexture, rtexture_t *pantstexture, rtexture_t *shirttexture, rtexture_t *normalmaptexture, rtexture_t *glosstexture, float ambientscale, float diffusescale, float specularscale, qboolean dopants, qboolean doshirt)
2667 {
2668         // OpenGL 1.1 path (anything)
2669         float ambientcolorbase[3], diffusecolorbase[3];
2670         float ambientcolorpants[3], diffusecolorpants[3];
2671         float ambientcolorshirt[3], diffusecolorshirt[3];
2672         rmeshstate_t m;
2673         VectorScale(lightcolorbase, ambientscale * 2 * r_refdef.view.colorscale, ambientcolorbase);
2674         VectorScale(lightcolorbase, diffusescale * 2 * r_refdef.view.colorscale, diffusecolorbase);
2675         VectorScale(lightcolorpants, ambientscale * 2 * r_refdef.view.colorscale, ambientcolorpants);
2676         VectorScale(lightcolorpants, diffusescale * 2 * r_refdef.view.colorscale, diffusecolorpants);
2677         VectorScale(lightcolorshirt, ambientscale * 2 * r_refdef.view.colorscale, ambientcolorshirt);
2678         VectorScale(lightcolorshirt, diffusescale * 2 * r_refdef.view.colorscale, diffusecolorshirt);
2679         memset(&m, 0, sizeof(m));
2680         m.tex[0] = R_GetTexture(basetexture);
2681         m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2682         m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2683         m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2684         m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2685         if (r_textureunits.integer >= 2)
2686         {
2687                 // voodoo2 or TNT
2688                 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
2689                 m.texmatrix[1] = rsurface.entitytoattenuationxyz;
2690                 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2691                 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2692                 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2693                 if (r_textureunits.integer >= 3)
2694                 {
2695                         // Voodoo4 or Kyro (or Geforce3/Radeon with gl_combine off)
2696                         m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
2697                         m.texmatrix[2] = rsurface.entitytoattenuationz;
2698                         m.pointer_texcoord3f[2] = rsurface.vertex3f;
2699                         m.pointer_texcoord_bufferobject[2] = rsurface.vertex3f_bufferobject;
2700                         m.pointer_texcoord_bufferoffset[2] = rsurface.vertex3f_bufferoffset;
2701                 }
2702         }
2703         R_Mesh_TextureState(&m);
2704         //R_Mesh_TexBind(0, R_GetTexture(basetexture));
2705         R_Shadow_RenderLighting_Light_Vertex_Pass(firstvertex, numvertices, numtriangles, element3i, diffusecolorbase, ambientcolorbase);
2706         if (dopants)
2707         {
2708                 R_Mesh_TexBind(0, R_GetTexture(pantstexture));
2709                 R_Shadow_RenderLighting_Light_Vertex_Pass(firstvertex, numvertices, numtriangles, element3i, diffusecolorpants, ambientcolorpants);
2710         }
2711         if (doshirt)
2712         {
2713                 R_Mesh_TexBind(0, R_GetTexture(shirttexture));
2714                 R_Shadow_RenderLighting_Light_Vertex_Pass(firstvertex, numvertices, numtriangles, element3i, diffusecolorshirt, ambientcolorshirt);
2715         }
2716 }
2717
2718 extern cvar_t gl_lightmaps;
2719 void R_Shadow_RenderLighting(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int element3i_bufferobject, int element3s_bufferobject)
2720 {
2721         float ambientscale, diffusescale, specularscale;
2722         vec3_t lightcolorbase, lightcolorpants, lightcolorshirt;
2723         rtexture_t *nmap;
2724         // calculate colors to render this texture with
2725         lightcolorbase[0] = rsurface.rtlight->currentcolor[0] * rsurface.texture->dlightcolor[0];
2726         lightcolorbase[1] = rsurface.rtlight->currentcolor[1] * rsurface.texture->dlightcolor[1];
2727         lightcolorbase[2] = rsurface.rtlight->currentcolor[2] * rsurface.texture->dlightcolor[2];
2728         ambientscale = rsurface.rtlight->ambientscale;
2729         diffusescale = rsurface.rtlight->diffusescale;
2730         specularscale = rsurface.rtlight->specularscale * rsurface.texture->specularscale;
2731         if (!r_shadow_usenormalmap.integer)
2732         {
2733                 ambientscale += 1.0f * diffusescale;
2734                 diffusescale = 0;
2735                 specularscale = 0;
2736         }
2737         if ((ambientscale + diffusescale) * VectorLength2(lightcolorbase) + specularscale * VectorLength2(lightcolorbase) < (1.0f / 1048576.0f))
2738                 return;
2739         RSurf_SetupDepthAndCulling();
2740         nmap = rsurface.texture->currentskinframe->nmap;
2741         if (gl_lightmaps.integer)
2742                 nmap = r_texture_blanknormalmap;
2743         if (rsurface.texture->colormapping && !gl_lightmaps.integer)
2744         {
2745                 qboolean dopants = rsurface.texture->currentskinframe->pants != NULL && VectorLength2(rsurface.colormap_pantscolor) >= (1.0f / 1048576.0f);
2746                 qboolean doshirt = rsurface.texture->currentskinframe->shirt != NULL && VectorLength2(rsurface.colormap_shirtcolor) >= (1.0f / 1048576.0f);
2747                 if (dopants)
2748                 {
2749                         lightcolorpants[0] = lightcolorbase[0] * rsurface.colormap_pantscolor[0];
2750                         lightcolorpants[1] = lightcolorbase[1] * rsurface.colormap_pantscolor[1];
2751                         lightcolorpants[2] = lightcolorbase[2] * rsurface.colormap_pantscolor[2];
2752                 }
2753                 else
2754                         VectorClear(lightcolorpants);
2755                 if (doshirt)
2756                 {
2757                         lightcolorshirt[0] = lightcolorbase[0] * rsurface.colormap_shirtcolor[0];
2758                         lightcolorshirt[1] = lightcolorbase[1] * rsurface.colormap_shirtcolor[1];
2759                         lightcolorshirt[2] = lightcolorbase[2] * rsurface.colormap_shirtcolor[2];
2760                 }
2761                 else
2762                         VectorClear(lightcolorshirt);
2763                 switch (r_shadow_rendermode)
2764                 {
2765                 case R_SHADOW_RENDERMODE_VISIBLELIGHTING:
2766                         GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST) && !r_showdisabledepthtest.integer);
2767                         R_Shadow_RenderLighting_VisibleLighting(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, lightcolorpants, lightcolorshirt, rsurface.texture->basetexture, rsurface.texture->currentskinframe->pants, rsurface.texture->currentskinframe->shirt, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, dopants, doshirt);
2768                         break;
2769                 case R_SHADOW_RENDERMODE_LIGHT_GLSL:
2770                         R_Shadow_RenderLighting_Light_GLSL(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, lightcolorpants, lightcolorshirt, rsurface.texture->basetexture, rsurface.texture->currentskinframe->pants, rsurface.texture->currentskinframe->shirt, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, dopants, doshirt);
2771                         break;
2772                 case R_SHADOW_RENDERMODE_LIGHT_DOT3:
2773                         R_Shadow_RenderLighting_Light_Dot3(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, lightcolorpants, lightcolorshirt, rsurface.texture->basetexture, rsurface.texture->currentskinframe->pants, rsurface.texture->currentskinframe->shirt, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, dopants, doshirt);
2774                         break;
2775                 case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
2776                         R_Shadow_RenderLighting_Light_Vertex(firstvertex, numvertices, numtriangles, element3i + firsttriangle * 3, lightcolorbase, lightcolorpants, lightcolorshirt, rsurface.texture->basetexture, rsurface.texture->currentskinframe->pants, rsurface.texture->currentskinframe->shirt, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, dopants, doshirt);
2777                         break;
2778                 default:
2779                         Con_Printf("R_Shadow_RenderLighting: unknown r_shadow_rendermode %i\n", r_shadow_rendermode);
2780                         break;
2781                 }
2782         }
2783         else
2784         {
2785                 switch (r_shadow_rendermode)
2786                 {
2787                 case R_SHADOW_RENDERMODE_VISIBLELIGHTING:
2788                         GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST) && !r_showdisabledepthtest.integer);
2789                         R_Shadow_RenderLighting_VisibleLighting(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, vec3_origin, vec3_origin, rsurface.texture->basetexture, r_texture_black, r_texture_black, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, false, false);
2790                         break;
2791                 case R_SHADOW_RENDERMODE_LIGHT_GLSL:
2792                         R_Shadow_RenderLighting_Light_GLSL(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, vec3_origin, vec3_origin, rsurface.texture->basetexture, r_texture_black, r_texture_black, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, false, false);
2793                         break;
2794                 case R_SHADOW_RENDERMODE_LIGHT_DOT3:
2795                         R_Shadow_RenderLighting_Light_Dot3(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, vec3_origin, vec3_origin, rsurface.texture->basetexture, r_texture_black, r_texture_black, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, false, false);
2796                         break;
2797                 case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
2798                         R_Shadow_RenderLighting_Light_Vertex(firstvertex, numvertices, numtriangles, element3i + firsttriangle * 3, lightcolorbase, vec3_origin, vec3_origin, rsurface.texture->basetexture, r_texture_black, r_texture_black, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, false, false);
2799                         break;
2800                 default:
2801                         Con_Printf("R_Shadow_RenderLighting: unknown r_shadow_rendermode %i\n", r_shadow_rendermode);
2802                         break;
2803                 }
2804         }
2805 }
2806
2807 void R_RTLight_Update(rtlight_t *rtlight, int isstatic, matrix4x4_t *matrix, vec3_t color, int style, const char *cubemapname, int shadow, vec_t corona, vec_t coronasizescale, vec_t ambientscale, vec_t diffusescale, vec_t specularscale, int flags)
2808 {
2809         matrix4x4_t tempmatrix = *matrix;
2810         Matrix4x4_Scale(&tempmatrix, r_shadow_lightradiusscale.value, 1);
2811
2812         // if this light has been compiled before, free the associated data
2813         R_RTLight_Uncompile(rtlight);
2814
2815         // clear it completely to avoid any lingering data
2816         memset(rtlight, 0, sizeof(*rtlight));
2817
2818         // copy the properties
2819         rtlight->matrix_lighttoworld = tempmatrix;
2820         Matrix4x4_Invert_Simple(&rtlight->matrix_worldtolight, &tempmatrix);
2821         Matrix4x4_OriginFromMatrix(&tempmatrix, rtlight->shadoworigin);
2822         rtlight->radius = Matrix4x4_ScaleFromMatrix(&tempmatrix);
2823         VectorCopy(color, rtlight->color);
2824         rtlight->cubemapname[0] = 0;
2825         if (cubemapname && cubemapname[0])
2826                 strlcpy(rtlight->cubemapname, cubemapname, sizeof(rtlight->cubemapname));
2827         rtlight->shadow = shadow;
2828         rtlight->corona = corona;
2829         rtlight->style = style;
2830         rtlight->isstatic = isstatic;
2831         rtlight->coronasizescale = coronasizescale;
2832         rtlight->ambientscale = ambientscale;
2833         rtlight->diffusescale = diffusescale;
2834         rtlight->specularscale = specularscale;
2835         rtlight->flags = flags;
2836
2837         // compute derived data
2838         //rtlight->cullradius = rtlight->radius;
2839         //rtlight->cullradius2 = rtlight->radius * rtlight->radius;
2840         rtlight->cullmins[0] = rtlight->shadoworigin[0] - rtlight->radius;
2841         rtlight->cullmins[1] = rtlight->shadoworigin[1] - rtlight->radius;
2842         rtlight->cullmins[2] = rtlight->shadoworigin[2] - rtlight->radius;
2843         rtlight->cullmaxs[0] = rtlight->shadoworigin[0] + rtlight->radius;
2844         rtlight->cullmaxs[1] = rtlight->shadoworigin[1] + rtlight->radius;
2845         rtlight->cullmaxs[2] = rtlight->shadoworigin[2] + rtlight->radius;
2846 }
2847
2848 // compiles rtlight geometry
2849 // (undone by R_FreeCompiledRTLight, which R_UpdateLight calls)
2850 void R_RTLight_Compile(rtlight_t *rtlight)
2851 {
2852         int i;
2853         int numsurfaces, numleafs, numleafpvsbytes, numshadowtrispvsbytes, numlighttrispvsbytes;
2854         int lighttris, shadowtris, shadowzpasstris, shadowzfailtris;
2855         entity_render_t *ent = r_refdef.scene.worldentity;
2856         dp_model_t *model = r_refdef.scene.worldmodel;
2857         unsigned char *data;
2858         shadowmesh_t *mesh;
2859
2860         // compile the light
2861         rtlight->compiled = true;
2862         rtlight->static_numleafs = 0;
2863         rtlight->static_numleafpvsbytes = 0;
2864         rtlight->static_leaflist = NULL;
2865         rtlight->static_leafpvs = NULL;
2866         rtlight->static_numsurfaces = 0;
2867         rtlight->static_surfacelist = NULL;
2868         rtlight->cullmins[0] = rtlight->shadoworigin[0] - rtlight->radius;
2869         rtlight->cullmins[1] = rtlight->shadoworigin[1] - rtlight->radius;
2870         rtlight->cullmins[2] = rtlight->shadoworigin[2] - rtlight->radius;
2871         rtlight->cullmaxs[0] = rtlight->shadoworigin[0] + rtlight->radius;
2872         rtlight->cullmaxs[1] = rtlight->shadoworigin[1] + rtlight->radius;
2873         rtlight->cullmaxs[2] = rtlight->shadoworigin[2] + rtlight->radius;
2874
2875         if (model && model->GetLightInfo)
2876         {
2877                 // this variable must be set for the CompileShadowVolume code
2878                 r_shadow_compilingrtlight = rtlight;
2879                 R_Shadow_EnlargeLeafSurfaceTrisBuffer(model->brush.num_leafs, model->num_surfaces, model->brush.shadowmesh ? model->brush.shadowmesh->numtriangles : model->surfmesh.num_triangles, model->surfmesh.num_triangles);
2880                 model->GetLightInfo(ent, rtlight->shadoworigin, rtlight->radius, rtlight->cullmins, rtlight->cullmaxs, r_shadow_buffer_leaflist, r_shadow_buffer_leafpvs, &numleafs, r_shadow_buffer_surfacelist, r_shadow_buffer_surfacepvs, &numsurfaces, r_shadow_buffer_shadowtrispvs, r_shadow_buffer_lighttrispvs, r_shadow_buffer_visitingleafpvs);
2881                 numleafpvsbytes = (model->brush.num_leafs + 7) >> 3;
2882                 numshadowtrispvsbytes = ((model->brush.shadowmesh ? model->brush.shadowmesh->numtriangles : model->surfmesh.num_triangles) + 7) >> 3;
2883                 numlighttrispvsbytes = (model->surfmesh.num_triangles + 7) >> 3;
2884                 data = (unsigned char *)Mem_Alloc(r_main_mempool, sizeof(int) * numsurfaces + sizeof(int) * numleafs + numleafpvsbytes + numshadowtrispvsbytes + numlighttrispvsbytes);
2885                 rtlight->static_numsurfaces = numsurfaces;
2886                 rtlight->static_surfacelist = (int *)data;data += sizeof(int) * numsurfaces;
2887                 rtlight->static_numleafs = numleafs;
2888                 rtlight->static_leaflist = (int *)data;data += sizeof(int) * numleafs;
2889                 rtlight->static_numleafpvsbytes = numleafpvsbytes;
2890                 rtlight->static_leafpvs = (unsigned char *)data;data += numleafpvsbytes;
2891                 rtlight->static_numshadowtrispvsbytes = numshadowtrispvsbytes;
2892                 rtlight->static_shadowtrispvs = (unsigned char *)data;data += numshadowtrispvsbytes;
2893                 rtlight->static_numlighttrispvsbytes = numlighttrispvsbytes;
2894                 rtlight->static_lighttrispvs = (unsigned char *)data;data += numlighttrispvsbytes;
2895                 if (rtlight->static_numsurfaces)
2896                         memcpy(rtlight->static_surfacelist, r_shadow_buffer_surfacelist, rtlight->static_numsurfaces * sizeof(*rtlight->static_surfacelist));
2897                 if (rtlight->static_numleafs)
2898                         memcpy(rtlight->static_leaflist, r_shadow_buffer_leaflist, rtlight->static_numleafs * sizeof(*rtlight->static_leaflist));
2899                 if (rtlight->static_numleafpvsbytes)
2900                         memcpy(rtlight->static_leafpvs, r_shadow_buffer_leafpvs, rtlight->static_numleafpvsbytes);
2901                 if (rtlight->static_numshadowtrispvsbytes)
2902                         memcpy(rtlight->static_shadowtrispvs, r_shadow_buffer_shadowtrispvs, rtlight->static_numshadowtrispvsbytes);
2903                 if (rtlight->static_numlighttrispvsbytes)
2904                         memcpy(rtlight->static_lighttrispvs, r_shadow_buffer_lighttrispvs, rtlight->static_numlighttrispvsbytes);
2905                 if (model->CompileShadowVolume && rtlight->shadow)
2906                         model->CompileShadowVolume(ent, rtlight->shadoworigin, NULL, rtlight->radius, numsurfaces, r_shadow_buffer_surfacelist);
2907                 // now we're done compiling the rtlight
2908                 r_shadow_compilingrtlight = NULL;
2909         }
2910
2911
2912         // use smallest available cullradius - box radius or light radius
2913         //rtlight->cullradius = RadiusFromBoundsAndOrigin(rtlight->cullmins, rtlight->cullmaxs, rtlight->shadoworigin);
2914         //rtlight->cullradius = min(rtlight->cullradius, rtlight->radius);
2915
2916         shadowzpasstris = 0;
2917         if (rtlight->static_meshchain_shadow_zpass)
2918                 for (mesh = rtlight->static_meshchain_shadow_zpass;mesh;mesh = mesh->next)
2919                         shadowzpasstris += mesh->numtriangles;
2920
2921         shadowzfailtris = 0;
2922         if (rtlight->static_meshchain_shadow_zfail)
2923                 for (mesh = rtlight->static_meshchain_shadow_zfail;mesh;mesh = mesh->next)
2924                         shadowzfailtris += mesh->numtriangles;
2925
2926         lighttris = 0;
2927         if (rtlight->static_numlighttrispvsbytes)
2928                 for (i = 0;i < rtlight->static_numlighttrispvsbytes*8;i++)
2929                         if (CHECKPVSBIT(rtlight->static_lighttrispvs, i))
2930                                 lighttris++;
2931
2932         shadowtris = 0;
2933         if (rtlight->static_numlighttrispvsbytes)
2934                 for (i = 0;i < rtlight->static_numshadowtrispvsbytes*8;i++)
2935                         if (CHECKPVSBIT(rtlight->static_shadowtrispvs, i))
2936                                 shadowtris++;
2937
2938         if (developer.integer >= 10)
2939                 Con_Printf("static light built: %f %f %f : %f %f %f box, %i light triangles, %i shadow triangles, %i zpass/%i zfail compiled shadow volume triangles\n", rtlight->cullmins[0], rtlight->cullmins[1], rtlight->cullmins[2], rtlight->cullmaxs[0], rtlight->cullmaxs[1], rtlight->cullmaxs[2], lighttris, shadowtris, shadowzpasstris, shadowzfailtris);
2940 }
2941
2942 void R_RTLight_Uncompile(rtlight_t *rtlight)
2943 {
2944         if (rtlight->compiled)
2945         {
2946                 if (rtlight->static_meshchain_shadow_zpass)
2947                         Mod_ShadowMesh_Free(rtlight->static_meshchain_shadow_zpass);
2948                 rtlight->static_meshchain_shadow_zpass = NULL;
2949                 if (rtlight->static_meshchain_shadow_zfail)
2950                         Mod_ShadowMesh_Free(rtlight->static_meshchain_shadow_zfail);
2951                 rtlight->static_meshchain_shadow_zfail = NULL;
2952                 // these allocations are grouped
2953                 if (rtlight->static_surfacelist)
2954                         Mem_Free(rtlight->static_surfacelist);
2955                 rtlight->static_numleafs = 0;
2956                 rtlight->static_numleafpvsbytes = 0;
2957                 rtlight->static_leaflist = NULL;
2958                 rtlight->static_leafpvs = NULL;
2959                 rtlight->static_numsurfaces = 0;
2960                 rtlight->static_surfacelist = NULL;
2961                 rtlight->static_numshadowtrispvsbytes = 0;
2962                 rtlight->static_shadowtrispvs = NULL;
2963                 rtlight->static_numlighttrispvsbytes = 0;
2964                 rtlight->static_lighttrispvs = NULL;
2965                 rtlight->compiled = false;
2966         }
2967 }
2968
2969 void R_Shadow_UncompileWorldLights(void)
2970 {
2971         size_t lightindex;
2972         dlight_t *light;
2973         size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
2974         for (lightindex = 0;lightindex < range;lightindex++)
2975         {
2976                 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
2977                 if (!light)
2978                         continue;
2979                 R_RTLight_Uncompile(&light->rtlight);
2980         }
2981 }
2982
2983 void R_Shadow_ComputeShadowCasterCullingPlanes(rtlight_t *rtlight)
2984 {
2985         int i, j;
2986         mplane_t plane;
2987         // reset the count of frustum planes
2988         // see rsurface.rtlight_frustumplanes definition for how much this array
2989         // can hold
2990         rsurface.rtlight_numfrustumplanes = 0;
2991
2992         // haven't implemented a culling path for ortho rendering
2993         if (!r_refdef.view.useperspective)
2994         {
2995                 // check if the light is on screen and copy the 4 planes if it is
2996                 for (i = 0;i < 4;i++)
2997                         if (PlaneDiff(rtlight->shadoworigin, &r_refdef.view.frustum[i]) < -0.03125)
2998                                 break;
2999                 if (i == 4)
3000                         for (i = 0;i < 4;i++)
3001                                 rsurface.rtlight_frustumplanes[rsurface.rtlight_numfrustumplanes++] = r_refdef.view.frustum[i];
3002                 return;
3003         }
3004
3005 #if 1
3006         // generate a deformed frustum that includes the light origin, this is
3007         // used to cull shadow casting surfaces that can not possibly cast a
3008         // shadow onto the visible light-receiving surfaces, which can be a
3009         // performance gain
3010         //
3011         // if the light origin is onscreen the result will be 4 planes exactly
3012         // if the light origin is offscreen on only one axis the result will
3013         // be exactly 5 planes (split-side case)
3014         // if the light origin is offscreen on two axes the result will be
3015         // exactly 4 planes (stretched corner case)
3016         for (i = 0;i < 4;i++)
3017         {
3018                 // quickly reject standard frustum planes that put the light
3019                 // origin outside the frustum
3020                 if (PlaneDiff(rtlight->shadoworigin, &r_refdef.view.frustum[i]) < -0.03125)
3021                         continue;
3022                 // copy the plane
3023                 rsurface.rtlight_frustumplanes[rsurface.rtlight_numfrustumplanes++] = r_refdef.view.frustum[i];
3024         }
3025         // if all the standard frustum planes were accepted, the light is onscreen
3026         // otherwise we need to generate some more planes below...
3027         if (rsurface.rtlight_numfrustumplanes < 4)
3028         {
3029                 // at least one of the stock frustum planes failed, so we need to
3030                 // create one or two custom planes to enclose the light origin
3031                 for (i = 0;i < 4;i++)
3032                 {
3033                         // create a plane using the view origin and light origin, and a
3034                         // single point from the frustum corner set
3035                         TriangleNormal(r_refdef.view.origin, r_refdef.view.frustumcorner[i], rtlight->shadoworigin, plane.normal);
3036                         VectorNormalize(plane.normal);
3037                         plane.dist = DotProduct(r_refdef.view.origin, plane.normal);
3038                         // see if this plane is backwards and flip it if so
3039                         for (j = 0;j < 4;j++)
3040                                 if (j != i && DotProduct(r_refdef.view.frustumcorner[j], plane.normal) - plane.dist < -0.03125)
3041                                         break;
3042                         if (j < 4)
3043                         {
3044                                 VectorNegate(plane.normal, plane.normal);
3045                                 plane.dist *= -1;
3046                                 // flipped plane, test again to see if