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