eliminated R_Mesh_TextureState function and rmeshstate_t struct because
[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 #define R_SHADOW_SHADOWMAP_NUMCUBEMAPS 8
144
145 extern void R_Shadow_EditLights_Init(void);
146
147 typedef enum r_shadow_rendermode_e
148 {
149         R_SHADOW_RENDERMODE_NONE,
150         R_SHADOW_RENDERMODE_ZPASS_STENCIL,
151         R_SHADOW_RENDERMODE_ZPASS_SEPARATESTENCIL,
152         R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE,
153         R_SHADOW_RENDERMODE_ZFAIL_STENCIL,
154         R_SHADOW_RENDERMODE_ZFAIL_SEPARATESTENCIL,
155         R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE,
156         R_SHADOW_RENDERMODE_LIGHT_VERTEX,
157         R_SHADOW_RENDERMODE_LIGHT_VERTEX2DATTEN,
158         R_SHADOW_RENDERMODE_LIGHT_VERTEX2D1DATTEN,
159         R_SHADOW_RENDERMODE_LIGHT_VERTEX3DATTEN,
160         R_SHADOW_RENDERMODE_LIGHT_GLSL,
161         R_SHADOW_RENDERMODE_VISIBLEVOLUMES,
162         R_SHADOW_RENDERMODE_VISIBLELIGHTING,
163         R_SHADOW_RENDERMODE_SHADOWMAP2D,
164         R_SHADOW_RENDERMODE_SHADOWMAPRECTANGLE,
165         R_SHADOW_RENDERMODE_SHADOWMAPCUBESIDE,
166 }
167 r_shadow_rendermode_t;
168
169 typedef enum r_shadow_shadowmode_e
170 {
171     R_SHADOW_SHADOWMODE_STENCIL,
172     R_SHADOW_SHADOWMODE_SHADOWMAP2D,
173     R_SHADOW_SHADOWMODE_SHADOWMAPRECTANGLE,
174     R_SHADOW_SHADOWMODE_SHADOWMAPCUBESIDE
175 }
176 r_shadow_shadowmode_t;
177
178 r_shadow_rendermode_t r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
179 r_shadow_rendermode_t r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_NONE;
180 r_shadow_rendermode_t r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_NONE;
181 r_shadow_rendermode_t r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_NONE;
182 qboolean r_shadow_usingshadowmaprect;
183 qboolean r_shadow_usingshadowmap2d;
184 qboolean r_shadow_usingshadowmapcube;
185 int r_shadow_shadowmapside;
186 float r_shadow_shadowmap_texturescale[2];
187 float r_shadow_shadowmap_parameters[4];
188 #if 0
189 int r_shadow_drawbuffer;
190 int r_shadow_readbuffer;
191 #endif
192 int r_shadow_cullface_front, r_shadow_cullface_back;
193 GLuint r_shadow_fborectangle;
194 GLuint r_shadow_fbocubeside[R_SHADOW_SHADOWMAP_NUMCUBEMAPS];
195 GLuint r_shadow_fbo2d;
196 r_shadow_shadowmode_t r_shadow_shadowmode;
197 int r_shadow_shadowmapfilterquality;
198 int r_shadow_shadowmaptexturetype;
199 int r_shadow_shadowmapdepthbits;
200 int r_shadow_shadowmapmaxsize;
201 qboolean r_shadow_shadowmapvsdct;
202 qboolean r_shadow_shadowmapsampler;
203 int r_shadow_shadowmappcf;
204 int r_shadow_shadowmapborder;
205 int r_shadow_lightscissor[4];
206 qboolean r_shadow_usingdeferredprepass;
207
208 int maxshadowtriangles;
209 int *shadowelements;
210
211 int maxshadowvertices;
212 float *shadowvertex3f;
213
214 int maxshadowmark;
215 int numshadowmark;
216 int *shadowmark;
217 int *shadowmarklist;
218 int shadowmarkcount;
219
220 int maxshadowsides;
221 int numshadowsides;
222 unsigned char *shadowsides;
223 int *shadowsideslist;
224
225 int maxvertexupdate;
226 int *vertexupdate;
227 int *vertexremap;
228 int vertexupdatenum;
229
230 int r_shadow_buffer_numleafpvsbytes;
231 unsigned char *r_shadow_buffer_visitingleafpvs;
232 unsigned char *r_shadow_buffer_leafpvs;
233 int *r_shadow_buffer_leaflist;
234
235 int r_shadow_buffer_numsurfacepvsbytes;
236 unsigned char *r_shadow_buffer_surfacepvs;
237 int *r_shadow_buffer_surfacelist;
238 unsigned char *r_shadow_buffer_surfacesides;
239
240 int r_shadow_buffer_numshadowtrispvsbytes;
241 unsigned char *r_shadow_buffer_shadowtrispvs;
242 int r_shadow_buffer_numlighttrispvsbytes;
243 unsigned char *r_shadow_buffer_lighttrispvs;
244
245 rtexturepool_t *r_shadow_texturepool;
246 rtexture_t *r_shadow_attenuationgradienttexture;
247 rtexture_t *r_shadow_attenuation2dtexture;
248 rtexture_t *r_shadow_attenuation3dtexture;
249 skinframe_t *r_shadow_lightcorona;
250 rtexture_t *r_shadow_shadowmaprectangletexture;
251 rtexture_t *r_shadow_shadowmap2dtexture;
252 rtexture_t *r_shadow_shadowmapcubetexture[R_SHADOW_SHADOWMAP_NUMCUBEMAPS];
253 rtexture_t *r_shadow_shadowmapvsdcttexture;
254 int r_shadow_shadowmapsize; // changes for each light based on distance
255 int r_shadow_shadowmaplod; // changes for each light based on distance
256
257 GLuint r_shadow_prepassgeometryfbo;
258 GLuint r_shadow_prepasslightingfbo;
259 int r_shadow_prepass_width;
260 int r_shadow_prepass_height;
261 rtexture_t *r_shadow_prepassgeometrydepthtexture;
262 rtexture_t *r_shadow_prepassgeometrynormalmaptexture;
263 rtexture_t *r_shadow_prepasslightingdiffusetexture;
264 rtexture_t *r_shadow_prepasslightingspeculartexture;
265
266 // lights are reloaded when this changes
267 char r_shadow_mapname[MAX_QPATH];
268
269 // used only for light filters (cubemaps)
270 rtexturepool_t *r_shadow_filters_texturepool;
271
272 static const GLenum r_shadow_prepasslightingdrawbuffers[2] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT};
273
274 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"};
275 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"};
276 cvar_t r_shadow_debuglight = {0, "r_shadow_debuglight", "-1", "renders only one light, for level design purposes or debugging"};
277 cvar_t r_shadow_deferred = {CVAR_SAVE, "r_shadow_deferred", "0", "uses image-based lighting instead of geometry-based lighting, the method used renders a depth image and a normalmap image, renders lights into separate diffuse and specular images, and then combines this into the normal rendering, requires r_shadow_shadowmapping"};
278 cvar_t r_shadow_deferred_8bitrange = {CVAR_SAVE, "r_shadow_deferred_8bitrange", "2", "dynamic range of image-based lighting when using 32bit color (does not apply to fp)"};
279 //cvar_t r_shadow_deferred_fp = {CVAR_SAVE, "r_shadow_deferred_fp", "0", "use 16bit (1) or 32bit (2) floating point for accumulation of image-based lighting"};
280 cvar_t r_shadow_usenormalmap = {CVAR_SAVE, "r_shadow_usenormalmap", "1", "enables use of directional shading on lights"};
281 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)"};
282 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"};
283 cvar_t r_shadow_glossintensity = {0, "r_shadow_glossintensity", "1", "how bright textured glossmaps should look if r_shadow_gloss is 1 or 2"};
284 cvar_t r_shadow_glossexponent = {0, "r_shadow_glossexponent", "32", "how 'sharp' the gloss should appear (specular power)"};
285 cvar_t r_shadow_gloss2exponent = {0, "r_shadow_gloss2exponent", "32", "same as r_shadow_glossexponent but for forced gloss (gloss 2) surfaces"};
286 cvar_t r_shadow_glossexact = {0, "r_shadow_glossexact", "0", "use exact reflection math for gloss (slightly slower, but should look a tad better)"};
287 cvar_t r_shadow_lightattenuationdividebias = {0, "r_shadow_lightattenuationdividebias", "1", "changes attenuation texture generation"};
288 cvar_t r_shadow_lightattenuationlinearscale = {0, "r_shadow_lightattenuationlinearscale", "2", "changes attenuation texture generation"};
289 cvar_t r_shadow_lightintensityscale = {0, "r_shadow_lightintensityscale", "1", "renders all world lights brighter or darker"};
290 cvar_t r_shadow_lightradiusscale = {0, "r_shadow_lightradiusscale", "1", "renders all world lights larger or smaller"};
291 cvar_t r_shadow_portallight = {0, "r_shadow_portallight", "1", "use portal culling to exactly determine lit triangles when compiling world lights"};
292 cvar_t r_shadow_projectdistance = {0, "r_shadow_projectdistance", "1000000", "how far to cast shadows"};
293 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)"};
294 cvar_t r_shadow_realtime_dlight = {CVAR_SAVE, "r_shadow_realtime_dlight", "1", "enables rendering of dynamic lights such as explosions and rocket light"};
295 cvar_t r_shadow_realtime_dlight_shadows = {CVAR_SAVE, "r_shadow_realtime_dlight_shadows", "1", "enables rendering of shadows from dynamic lights"};
296 cvar_t r_shadow_realtime_dlight_svbspculling = {0, "r_shadow_realtime_dlight_svbspculling", "0", "enables svbsp optimization on dynamic lights (very slow!)"};
297 cvar_t r_shadow_realtime_dlight_portalculling = {0, "r_shadow_realtime_dlight_portalculling", "0", "enables portal optimization on dynamic lights (slow!)"};
298 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)"};
299 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"};
300 cvar_t r_shadow_realtime_world_shadows = {CVAR_SAVE, "r_shadow_realtime_world_shadows", "1", "enables rendering of shadows from world lights"};
301 cvar_t r_shadow_realtime_world_compile = {0, "r_shadow_realtime_world_compile", "1", "enables compilation of world lights for higher performance rendering"};
302 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"};
303 cvar_t r_shadow_realtime_world_compilesvbsp = {0, "r_shadow_realtime_world_compilesvbsp", "1", "enables svbsp optimization during compilation"};
304 cvar_t r_shadow_realtime_world_compileportalculling = {0, "r_shadow_realtime_world_compileportalculling", "1", "enables portal-based culling optimization during compilation"};
305 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)"};
306 cvar_t r_shadow_shadowmapping = {CVAR_SAVE, "r_shadow_shadowmapping", "0", "enables use of shadowmapping (depth texture sampling) instead of stencil shadow volumes, requires gl_fbo 1"};
307 cvar_t r_shadow_shadowmapping_texturetype = {CVAR_SAVE, "r_shadow_shadowmapping_texturetype", "-1", "shadowmap texture types: -1 = auto-select, 0 = 2D, 1 = rectangle, 2 = cubemap"};
308 cvar_t r_shadow_shadowmapping_filterquality = {CVAR_SAVE, "r_shadow_shadowmapping_filterquality", "-1", "shadowmap filter modes: -1 = auto-select, 0 = no filtering, 1 = bilinear, 2 = bilinear 2x2 blur (fast), 3 = 3x3 blur (moderate), 4 = 4x4 blur (slow)"};
309 cvar_t r_shadow_shadowmapping_depthbits = {CVAR_SAVE, "r_shadow_shadowmapping_depthbits", "24", "requested minimum shadowmap texture depth bits"};
310 cvar_t r_shadow_shadowmapping_vsdct = {CVAR_SAVE, "r_shadow_shadowmapping_vsdct", "1", "enables use of virtual shadow depth cube texture"};
311 cvar_t r_shadow_shadowmapping_minsize = {CVAR_SAVE, "r_shadow_shadowmapping_minsize", "32", "shadowmap size limit"};
312 cvar_t r_shadow_shadowmapping_maxsize = {CVAR_SAVE, "r_shadow_shadowmapping_maxsize", "512", "shadowmap size limit"};
313 cvar_t r_shadow_shadowmapping_precision = {CVAR_SAVE, "r_shadow_shadowmapping_precision", "1", "makes shadowmaps have a maximum resolution of this number of pixels per light source radius unit such that, for example, at precision 0.5 a light with radius 200 will have a maximum resolution of 100 pixels"};
314 //cvar_t r_shadow_shadowmapping_lod_bias = {CVAR_SAVE, "r_shadow_shadowmapping_lod_bias", "16", "shadowmap size bias"};
315 //cvar_t r_shadow_shadowmapping_lod_scale = {CVAR_SAVE, "r_shadow_shadowmapping_lod_scale", "128", "shadowmap size scaling parameter"};
316 cvar_t r_shadow_shadowmapping_bordersize = {CVAR_SAVE, "r_shadow_shadowmapping_bordersize", "4", "shadowmap size bias for filtering"};
317 cvar_t r_shadow_shadowmapping_nearclip = {CVAR_SAVE, "r_shadow_shadowmapping_nearclip", "1", "shadowmap nearclip in world units"};
318 cvar_t r_shadow_shadowmapping_bias = {CVAR_SAVE, "r_shadow_shadowmapping_bias", "0.03", "shadowmap bias parameter (this is multiplied by nearclip * 1024 / lodsize)"};
319 cvar_t r_shadow_shadowmapping_polygonfactor = {CVAR_SAVE, "r_shadow_shadowmapping_polygonfactor", "2", "slope-dependent shadowmapping bias"};
320 cvar_t r_shadow_shadowmapping_polygonoffset = {CVAR_SAVE, "r_shadow_shadowmapping_polygonoffset", "0", "constant shadowmapping bias"};
321 cvar_t r_shadow_culltriangles = {0, "r_shadow_culltriangles", "1", "performs more expensive tests to remove unnecessary triangles of lit surfaces"};
322 cvar_t r_shadow_polygonfactor = {0, "r_shadow_polygonfactor", "0", "how much to enlarge shadow volume polygons when rendering (should be 0!)"};
323 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)"};
324 cvar_t r_shadow_texture3d = {0, "r_shadow_texture3d", "1", "use 3D voxel textures for spherical attenuation rather than cylindrical (does not affect OpenGL 2.0 render path)"};
325 cvar_t r_coronas = {CVAR_SAVE, "r_coronas", "1", "brightness of corona flare effects around certain lights, 0 disables corona effects"};
326 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"};
327 cvar_t r_coronas_occlusionquery = {CVAR_SAVE, "r_coronas_occlusionquery", "1", "use GL_ARB_occlusion_query extension if supported (fades coronas according to visibility)"};
328 cvar_t gl_flashblend = {CVAR_SAVE, "gl_flashblend", "0", "render bright coronas for dynamic lights instead of actual lighting, fast but ugly"};
329 cvar_t gl_ext_separatestencil = {0, "gl_ext_separatestencil", "1", "make use of OpenGL 2.0 glStencilOpSeparate or GL_ATI_separate_stencil extension"};
330 cvar_t gl_ext_stenciltwoside = {0, "gl_ext_stenciltwoside", "1", "make use of GL_EXT_stenciltwoside extension (NVIDIA only)"};
331 cvar_t r_editlights = {0, "r_editlights", "0", "enables .rtlights file editing mode"};
332 cvar_t r_editlights_cursordistance = {0, "r_editlights_cursordistance", "1024", "maximum distance of cursor from eye"};
333 cvar_t r_editlights_cursorpushback = {0, "r_editlights_cursorpushback", "0", "how far to pull the cursor back toward the eye"};
334 cvar_t r_editlights_cursorpushoff = {0, "r_editlights_cursorpushoff", "4", "how far to push the cursor off the impacted surface"};
335 cvar_t r_editlights_cursorgrid = {0, "r_editlights_cursorgrid", "4", "snaps cursor to this grid size"};
336 cvar_t r_editlights_quakelightsizescale = {CVAR_SAVE, "r_editlights_quakelightsizescale", "1", "changes size of light entities loaded from a map"};
337
338 // note the table actually includes one more value, just to avoid the need to clamp the distance index due to minor math error
339 #define ATTENTABLESIZE 256
340 // 1D gradient, 2D circle and 3D sphere attenuation textures
341 #define ATTEN1DSIZE 32
342 #define ATTEN2DSIZE 64
343 #define ATTEN3DSIZE 32
344
345 static float r_shadow_attendividebias; // r_shadow_lightattenuationdividebias
346 static float r_shadow_attenlinearscale; // r_shadow_lightattenuationlinearscale
347 static float r_shadow_attentable[ATTENTABLESIZE+1];
348
349 rtlight_t *r_shadow_compilingrtlight;
350 static memexpandablearray_t r_shadow_worldlightsarray;
351 dlight_t *r_shadow_selectedlight;
352 dlight_t r_shadow_bufferlight;
353 vec3_t r_editlights_cursorlocation;
354
355 extern int con_vislines;
356
357 typedef struct cubemapinfo_s
358 {
359         char basename[64];
360         rtexture_t *texture;
361 }
362 cubemapinfo_t;
363
364 static int numcubemaps;
365 static cubemapinfo_t cubemaps[MAX_CUBEMAPS];
366
367 void R_Shadow_UncompileWorldLights(void);
368 void R_Shadow_ClearWorldLights(void);
369 void R_Shadow_SaveWorldLights(void);
370 void R_Shadow_LoadWorldLights(void);
371 void R_Shadow_LoadLightsFile(void);
372 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void);
373 void R_Shadow_EditLights_Reload_f(void);
374 void R_Shadow_ValidateCvars(void);
375 static void R_Shadow_MakeTextures(void);
376
377 #define EDLIGHTSPRSIZE                  8
378 skinframe_t *r_editlights_sprcursor;
379 skinframe_t *r_editlights_sprlight;
380 skinframe_t *r_editlights_sprnoshadowlight;
381 skinframe_t *r_editlights_sprcubemaplight;
382 skinframe_t *r_editlights_sprcubemapnoshadowlight;
383 skinframe_t *r_editlights_sprselection;
384
385 void R_Shadow_SetShadowMode(void)
386 {
387         r_shadow_shadowmapmaxsize = bound(1, r_shadow_shadowmapping_maxsize.integer, (int)vid.maxtexturesize_2d / 4);
388         r_shadow_shadowmapvsdct = r_shadow_shadowmapping_vsdct.integer != 0;
389         r_shadow_shadowmapfilterquality = r_shadow_shadowmapping_filterquality.integer;
390         r_shadow_shadowmaptexturetype = r_shadow_shadowmapping_texturetype.integer;
391         r_shadow_shadowmapdepthbits = r_shadow_shadowmapping_depthbits.integer;
392         r_shadow_shadowmapborder = bound(0, r_shadow_shadowmapping_bordersize.integer, 16);
393         r_shadow_shadowmaplod = -1;
394         r_shadow_shadowmapsize = 0;
395         r_shadow_shadowmapsampler = false;
396         r_shadow_shadowmappcf = 0;
397         r_shadow_shadowmode = R_SHADOW_SHADOWMODE_STENCIL;
398         switch(vid.renderpath)
399         {
400         case RENDERPATH_GL20:
401                 if ((r_shadow_shadowmapping.integer || r_shadow_deferred.integer) && vid.support.ext_framebuffer_object)
402                 {
403                         if(r_shadow_shadowmapfilterquality < 0)
404                         {
405                                 if(strstr(gl_vendor, "NVIDIA")) 
406                                 {
407                                         r_shadow_shadowmapsampler = vid.support.arb_shadow;
408                                         r_shadow_shadowmappcf = 1;
409                                 }
410                                 else if(vid.support.amd_texture_texture4 || vid.support.arb_texture_gather) 
411                                         r_shadow_shadowmappcf = 1;
412                                 else if(strstr(gl_vendor, "ATI")) 
413                                         r_shadow_shadowmappcf = 1;
414                                 else 
415                                         r_shadow_shadowmapsampler = vid.support.arb_shadow;
416                         }
417                         else 
418                         {
419                                 switch (r_shadow_shadowmapfilterquality)
420                                 {
421                                 case 1:
422                                         r_shadow_shadowmapsampler = vid.support.arb_shadow;
423                                         break;
424                                 case 2:
425                                         r_shadow_shadowmapsampler = vid.support.arb_shadow;
426                                         r_shadow_shadowmappcf = 1;
427                                         break;
428                                 case 3:
429                                         r_shadow_shadowmappcf = 1;
430                                         break;
431                                 case 4:
432                                         r_shadow_shadowmappcf = 2;
433                                         break;
434                                 }
435                         }
436                         switch (r_shadow_shadowmaptexturetype)
437                         {
438                         case 0:
439                                 r_shadow_shadowmode = R_SHADOW_SHADOWMODE_SHADOWMAP2D;
440                                 break;
441                         case 1:
442                                 r_shadow_shadowmode = R_SHADOW_SHADOWMODE_SHADOWMAPRECTANGLE;
443                                 break;
444                         case 2:
445                                 r_shadow_shadowmode = R_SHADOW_SHADOWMODE_SHADOWMAPCUBESIDE;
446                                 break;
447                         default:
448                                 if((vid.support.amd_texture_texture4 || vid.support.arb_texture_gather) && r_shadow_shadowmappcf && !r_shadow_shadowmapsampler)
449                                         r_shadow_shadowmode = R_SHADOW_SHADOWMODE_SHADOWMAP2D;
450                                 else if(vid.support.arb_texture_rectangle) 
451                                         r_shadow_shadowmode = R_SHADOW_SHADOWMODE_SHADOWMAPRECTANGLE;
452                                 else
453                                         r_shadow_shadowmode = R_SHADOW_SHADOWMODE_SHADOWMAP2D;
454                                 break;
455                         }
456                 }
457                 break;
458         case RENDERPATH_GL13:
459                 break;
460         case RENDERPATH_GL11:
461                 break;
462         }
463 }
464
465 void R_Shadow_FreeShadowMaps(void)
466 {
467         int i;
468
469         R_Shadow_SetShadowMode();
470
471         if (!vid.support.ext_framebuffer_object || !vid.support.arb_fragment_shader)
472                 return;
473
474         CHECKGLERROR
475
476         if (r_shadow_fborectangle)
477                 qglDeleteFramebuffersEXT(1, &r_shadow_fborectangle);CHECKGLERROR
478         r_shadow_fborectangle = 0;
479
480         if (r_shadow_fbo2d)
481                 qglDeleteFramebuffersEXT(1, &r_shadow_fbo2d);CHECKGLERROR
482         r_shadow_fbo2d = 0;
483         for (i = 0;i < R_SHADOW_SHADOWMAP_NUMCUBEMAPS;i++)
484                 if (r_shadow_fbocubeside[i])
485                         qglDeleteFramebuffersEXT(1, &r_shadow_fbocubeside[i]);CHECKGLERROR
486         memset(r_shadow_fbocubeside, 0, sizeof(r_shadow_fbocubeside));
487
488         if (r_shadow_shadowmaprectangletexture)
489                 R_FreeTexture(r_shadow_shadowmaprectangletexture);
490         r_shadow_shadowmaprectangletexture = NULL;
491
492         if (r_shadow_shadowmap2dtexture)
493                 R_FreeTexture(r_shadow_shadowmap2dtexture);
494         r_shadow_shadowmap2dtexture = NULL;
495
496         for (i = 0;i < R_SHADOW_SHADOWMAP_NUMCUBEMAPS;i++)
497                 if (r_shadow_shadowmapcubetexture[i])
498                         R_FreeTexture(r_shadow_shadowmapcubetexture[i]);
499         memset(r_shadow_shadowmapcubetexture, 0, sizeof(r_shadow_shadowmapcubetexture));
500
501         if (r_shadow_shadowmapvsdcttexture)
502                 R_FreeTexture(r_shadow_shadowmapvsdcttexture);
503         r_shadow_shadowmapvsdcttexture = NULL;
504
505         CHECKGLERROR
506 }
507
508 void r_shadow_start(void)
509 {
510         // allocate vertex processing arrays
511         numcubemaps = 0;
512         r_shadow_attenuationgradienttexture = NULL;
513         r_shadow_attenuation2dtexture = NULL;
514         r_shadow_attenuation3dtexture = NULL;
515         r_shadow_shadowmode = R_SHADOW_SHADOWMODE_STENCIL;
516         r_shadow_shadowmaprectangletexture = NULL;
517         r_shadow_shadowmap2dtexture = NULL;
518         memset(r_shadow_shadowmapcubetexture, 0, sizeof(r_shadow_shadowmapcubetexture));
519         r_shadow_shadowmapvsdcttexture = NULL;
520         r_shadow_shadowmapmaxsize = 0;
521         r_shadow_shadowmapsize = 0;
522         r_shadow_shadowmaplod = 0;
523         r_shadow_shadowmapfilterquality = -1;
524         r_shadow_shadowmaptexturetype = -1;
525         r_shadow_shadowmapdepthbits = 0;
526         r_shadow_shadowmapvsdct = false;
527         r_shadow_shadowmapsampler = false;
528         r_shadow_shadowmappcf = 0;
529         r_shadow_fborectangle = 0;
530         r_shadow_fbo2d = 0;
531         memset(r_shadow_fbocubeside, 0, sizeof(r_shadow_fbocubeside));
532
533         R_Shadow_FreeShadowMaps();
534
535         r_shadow_texturepool = NULL;
536         r_shadow_filters_texturepool = NULL;
537         R_Shadow_ValidateCvars();
538         R_Shadow_MakeTextures();
539         maxshadowtriangles = 0;
540         shadowelements = NULL;
541         maxshadowvertices = 0;
542         shadowvertex3f = NULL;
543         maxvertexupdate = 0;
544         vertexupdate = NULL;
545         vertexremap = NULL;
546         vertexupdatenum = 0;
547         maxshadowmark = 0;
548         numshadowmark = 0;
549         shadowmark = NULL;
550         shadowmarklist = NULL;
551         shadowmarkcount = 0;
552         maxshadowsides = 0;
553         numshadowsides = 0;
554         shadowsides = NULL;
555         shadowsideslist = NULL;
556         r_shadow_buffer_numleafpvsbytes = 0;
557         r_shadow_buffer_visitingleafpvs = NULL;
558         r_shadow_buffer_leafpvs = NULL;
559         r_shadow_buffer_leaflist = NULL;
560         r_shadow_buffer_numsurfacepvsbytes = 0;
561         r_shadow_buffer_surfacepvs = NULL;
562         r_shadow_buffer_surfacelist = NULL;
563         r_shadow_buffer_surfacesides = NULL;
564         r_shadow_buffer_numshadowtrispvsbytes = 0;
565         r_shadow_buffer_shadowtrispvs = NULL;
566         r_shadow_buffer_numlighttrispvsbytes = 0;
567         r_shadow_buffer_lighttrispvs = NULL;
568
569         r_shadow_usingdeferredprepass = false;
570         r_shadow_prepass_width = r_shadow_prepass_height = 0;
571 }
572
573 static void R_Shadow_FreeDeferred(void);
574 void r_shadow_shutdown(void)
575 {
576         CHECKGLERROR
577         R_Shadow_UncompileWorldLights();
578
579         R_Shadow_FreeShadowMaps();
580
581         r_shadow_usingdeferredprepass = false;
582         if (r_shadow_prepass_width)
583                 R_Shadow_FreeDeferred();
584         r_shadow_prepass_width = r_shadow_prepass_height = 0;
585
586         CHECKGLERROR
587         numcubemaps = 0;
588         r_shadow_attenuationgradienttexture = NULL;
589         r_shadow_attenuation2dtexture = NULL;
590         r_shadow_attenuation3dtexture = NULL;
591         R_FreeTexturePool(&r_shadow_texturepool);
592         R_FreeTexturePool(&r_shadow_filters_texturepool);
593         maxshadowtriangles = 0;
594         if (shadowelements)
595                 Mem_Free(shadowelements);
596         shadowelements = NULL;
597         if (shadowvertex3f)
598                 Mem_Free(shadowvertex3f);
599         shadowvertex3f = NULL;
600         maxvertexupdate = 0;
601         if (vertexupdate)
602                 Mem_Free(vertexupdate);
603         vertexupdate = NULL;
604         if (vertexremap)
605                 Mem_Free(vertexremap);
606         vertexremap = NULL;
607         vertexupdatenum = 0;
608         maxshadowmark = 0;
609         numshadowmark = 0;
610         if (shadowmark)
611                 Mem_Free(shadowmark);
612         shadowmark = NULL;
613         if (shadowmarklist)
614                 Mem_Free(shadowmarklist);
615         shadowmarklist = NULL;
616         shadowmarkcount = 0;
617         maxshadowsides = 0;
618         numshadowsides = 0;
619         if (shadowsides)
620                 Mem_Free(shadowsides);
621         shadowsides = NULL;
622         if (shadowsideslist)
623                 Mem_Free(shadowsideslist);
624         shadowsideslist = NULL;
625         r_shadow_buffer_numleafpvsbytes = 0;
626         if (r_shadow_buffer_visitingleafpvs)
627                 Mem_Free(r_shadow_buffer_visitingleafpvs);
628         r_shadow_buffer_visitingleafpvs = NULL;
629         if (r_shadow_buffer_leafpvs)
630                 Mem_Free(r_shadow_buffer_leafpvs);
631         r_shadow_buffer_leafpvs = NULL;
632         if (r_shadow_buffer_leaflist)
633                 Mem_Free(r_shadow_buffer_leaflist);
634         r_shadow_buffer_leaflist = NULL;
635         r_shadow_buffer_numsurfacepvsbytes = 0;
636         if (r_shadow_buffer_surfacepvs)
637                 Mem_Free(r_shadow_buffer_surfacepvs);
638         r_shadow_buffer_surfacepvs = NULL;
639         if (r_shadow_buffer_surfacelist)
640                 Mem_Free(r_shadow_buffer_surfacelist);
641         r_shadow_buffer_surfacelist = NULL;
642         if (r_shadow_buffer_surfacesides)
643                 Mem_Free(r_shadow_buffer_surfacesides);
644         r_shadow_buffer_surfacesides = NULL;
645         r_shadow_buffer_numshadowtrispvsbytes = 0;
646         if (r_shadow_buffer_shadowtrispvs)
647                 Mem_Free(r_shadow_buffer_shadowtrispvs);
648         r_shadow_buffer_numlighttrispvsbytes = 0;
649         if (r_shadow_buffer_lighttrispvs)
650                 Mem_Free(r_shadow_buffer_lighttrispvs);
651 }
652
653 void r_shadow_newmap(void)
654 {
655         if (r_shadow_lightcorona)                 R_SkinFrame_MarkUsed(r_shadow_lightcorona);
656         if (r_editlights_sprcursor)               R_SkinFrame_MarkUsed(r_editlights_sprcursor);
657         if (r_editlights_sprlight)                R_SkinFrame_MarkUsed(r_editlights_sprlight);
658         if (r_editlights_sprnoshadowlight)        R_SkinFrame_MarkUsed(r_editlights_sprnoshadowlight);
659         if (r_editlights_sprcubemaplight)         R_SkinFrame_MarkUsed(r_editlights_sprcubemaplight);
660         if (r_editlights_sprcubemapnoshadowlight) R_SkinFrame_MarkUsed(r_editlights_sprcubemapnoshadowlight);
661         if (r_editlights_sprselection)            R_SkinFrame_MarkUsed(r_editlights_sprselection);
662         if (cl.worldmodel && strncmp(cl.worldmodel->name, r_shadow_mapname, sizeof(r_shadow_mapname)))
663                 R_Shadow_EditLights_Reload_f();
664 }
665
666 void R_Shadow_Init(void)
667 {
668         Cvar_RegisterVariable(&r_shadow_bumpscale_basetexture);
669         Cvar_RegisterVariable(&r_shadow_bumpscale_bumpmap);
670         Cvar_RegisterVariable(&r_shadow_usenormalmap);
671         Cvar_RegisterVariable(&r_shadow_debuglight);
672         Cvar_RegisterVariable(&r_shadow_deferred);
673         Cvar_RegisterVariable(&r_shadow_deferred_8bitrange);
674 //      Cvar_RegisterVariable(&r_shadow_deferred_fp);
675         Cvar_RegisterVariable(&r_shadow_gloss);
676         Cvar_RegisterVariable(&r_shadow_gloss2intensity);
677         Cvar_RegisterVariable(&r_shadow_glossintensity);
678         Cvar_RegisterVariable(&r_shadow_glossexponent);
679         Cvar_RegisterVariable(&r_shadow_gloss2exponent);
680         Cvar_RegisterVariable(&r_shadow_glossexact);
681         Cvar_RegisterVariable(&r_shadow_lightattenuationdividebias);
682         Cvar_RegisterVariable(&r_shadow_lightattenuationlinearscale);
683         Cvar_RegisterVariable(&r_shadow_lightintensityscale);
684         Cvar_RegisterVariable(&r_shadow_lightradiusscale);
685         Cvar_RegisterVariable(&r_shadow_portallight);
686         Cvar_RegisterVariable(&r_shadow_projectdistance);
687         Cvar_RegisterVariable(&r_shadow_frontsidecasting);
688         Cvar_RegisterVariable(&r_shadow_realtime_dlight);
689         Cvar_RegisterVariable(&r_shadow_realtime_dlight_shadows);
690         Cvar_RegisterVariable(&r_shadow_realtime_dlight_svbspculling);
691         Cvar_RegisterVariable(&r_shadow_realtime_dlight_portalculling);
692         Cvar_RegisterVariable(&r_shadow_realtime_world);
693         Cvar_RegisterVariable(&r_shadow_realtime_world_lightmaps);
694         Cvar_RegisterVariable(&r_shadow_realtime_world_shadows);
695         Cvar_RegisterVariable(&r_shadow_realtime_world_compile);
696         Cvar_RegisterVariable(&r_shadow_realtime_world_compileshadow);
697         Cvar_RegisterVariable(&r_shadow_realtime_world_compilesvbsp);
698         Cvar_RegisterVariable(&r_shadow_realtime_world_compileportalculling);
699         Cvar_RegisterVariable(&r_shadow_scissor);
700         Cvar_RegisterVariable(&r_shadow_shadowmapping);
701         Cvar_RegisterVariable(&r_shadow_shadowmapping_vsdct);
702         Cvar_RegisterVariable(&r_shadow_shadowmapping_texturetype);
703         Cvar_RegisterVariable(&r_shadow_shadowmapping_filterquality);
704         Cvar_RegisterVariable(&r_shadow_shadowmapping_depthbits);
705         Cvar_RegisterVariable(&r_shadow_shadowmapping_precision);
706         Cvar_RegisterVariable(&r_shadow_shadowmapping_maxsize);
707         Cvar_RegisterVariable(&r_shadow_shadowmapping_minsize);
708 //      Cvar_RegisterVariable(&r_shadow_shadowmapping_lod_bias);
709 //      Cvar_RegisterVariable(&r_shadow_shadowmapping_lod_scale);
710         Cvar_RegisterVariable(&r_shadow_shadowmapping_bordersize);
711         Cvar_RegisterVariable(&r_shadow_shadowmapping_nearclip);
712         Cvar_RegisterVariable(&r_shadow_shadowmapping_bias);
713         Cvar_RegisterVariable(&r_shadow_shadowmapping_polygonfactor);
714         Cvar_RegisterVariable(&r_shadow_shadowmapping_polygonoffset);
715         Cvar_RegisterVariable(&r_shadow_culltriangles);
716         Cvar_RegisterVariable(&r_shadow_polygonfactor);
717         Cvar_RegisterVariable(&r_shadow_polygonoffset);
718         Cvar_RegisterVariable(&r_shadow_texture3d);
719         Cvar_RegisterVariable(&r_coronas);
720         Cvar_RegisterVariable(&r_coronas_occlusionsizescale);
721         Cvar_RegisterVariable(&r_coronas_occlusionquery);
722         Cvar_RegisterVariable(&gl_flashblend);
723         Cvar_RegisterVariable(&gl_ext_separatestencil);
724         Cvar_RegisterVariable(&gl_ext_stenciltwoside);
725         if (gamemode == GAME_TENEBRAE)
726         {
727                 Cvar_SetValue("r_shadow_gloss", 2);
728                 Cvar_SetValue("r_shadow_bumpscale_basetexture", 4);
729         }
730         R_Shadow_EditLights_Init();
731         Mem_ExpandableArray_NewArray(&r_shadow_worldlightsarray, r_main_mempool, sizeof(dlight_t), 128);
732         maxshadowtriangles = 0;
733         shadowelements = NULL;
734         maxshadowvertices = 0;
735         shadowvertex3f = NULL;
736         maxvertexupdate = 0;
737         vertexupdate = NULL;
738         vertexremap = NULL;
739         vertexupdatenum = 0;
740         maxshadowmark = 0;
741         numshadowmark = 0;
742         shadowmark = NULL;
743         shadowmarklist = NULL;
744         shadowmarkcount = 0;
745         maxshadowsides = 0;
746         numshadowsides = 0;
747         shadowsides = NULL;
748         shadowsideslist = NULL;
749         r_shadow_buffer_numleafpvsbytes = 0;
750         r_shadow_buffer_visitingleafpvs = NULL;
751         r_shadow_buffer_leafpvs = NULL;
752         r_shadow_buffer_leaflist = NULL;
753         r_shadow_buffer_numsurfacepvsbytes = 0;
754         r_shadow_buffer_surfacepvs = NULL;
755         r_shadow_buffer_surfacelist = NULL;
756         r_shadow_buffer_surfacesides = NULL;
757         r_shadow_buffer_shadowtrispvs = NULL;
758         r_shadow_buffer_lighttrispvs = NULL;
759         R_RegisterModule("R_Shadow", r_shadow_start, r_shadow_shutdown, r_shadow_newmap);
760 }
761
762 matrix4x4_t matrix_attenuationxyz =
763 {
764         {
765                 {0.5, 0.0, 0.0, 0.5},
766                 {0.0, 0.5, 0.0, 0.5},
767                 {0.0, 0.0, 0.5, 0.5},
768                 {0.0, 0.0, 0.0, 1.0}
769         }
770 };
771
772 matrix4x4_t matrix_attenuationz =
773 {
774         {
775                 {0.0, 0.0, 0.5, 0.5},
776                 {0.0, 0.0, 0.0, 0.5},
777                 {0.0, 0.0, 0.0, 0.5},
778                 {0.0, 0.0, 0.0, 1.0}
779         }
780 };
781
782 void R_Shadow_ResizeShadowArrays(int numvertices, int numtriangles, int vertscale, int triscale)
783 {
784         numvertices = ((numvertices + 255) & ~255) * vertscale;
785         numtriangles = ((numtriangles + 255) & ~255) * triscale;
786         // make sure shadowelements is big enough for this volume
787         if (maxshadowtriangles < numtriangles)
788         {
789                 maxshadowtriangles = numtriangles;
790                 if (shadowelements)
791                         Mem_Free(shadowelements);
792                 shadowelements = (int *)Mem_Alloc(r_main_mempool, maxshadowtriangles * sizeof(int[3]));
793         }
794         // make sure shadowvertex3f is big enough for this volume
795         if (maxshadowvertices < numvertices)
796         {
797                 maxshadowvertices = numvertices;
798                 if (shadowvertex3f)
799                         Mem_Free(shadowvertex3f);
800                 shadowvertex3f = (float *)Mem_Alloc(r_main_mempool, maxshadowvertices * sizeof(float[3]));
801         }
802 }
803
804 static void R_Shadow_EnlargeLeafSurfaceTrisBuffer(int numleafs, int numsurfaces, int numshadowtriangles, int numlighttriangles)
805 {
806         int numleafpvsbytes = (((numleafs + 7) >> 3) + 255) & ~255;
807         int numsurfacepvsbytes = (((numsurfaces + 7) >> 3) + 255) & ~255;
808         int numshadowtrispvsbytes = (((numshadowtriangles + 7) >> 3) + 255) & ~255;
809         int numlighttrispvsbytes = (((numlighttriangles + 7) >> 3) + 255) & ~255;
810         if (r_shadow_buffer_numleafpvsbytes < numleafpvsbytes)
811         {
812                 if (r_shadow_buffer_visitingleafpvs)
813                         Mem_Free(r_shadow_buffer_visitingleafpvs);
814                 if (r_shadow_buffer_leafpvs)
815                         Mem_Free(r_shadow_buffer_leafpvs);
816                 if (r_shadow_buffer_leaflist)
817                         Mem_Free(r_shadow_buffer_leaflist);
818                 r_shadow_buffer_numleafpvsbytes = numleafpvsbytes;
819                 r_shadow_buffer_visitingleafpvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numleafpvsbytes);
820                 r_shadow_buffer_leafpvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numleafpvsbytes);
821                 r_shadow_buffer_leaflist = (int *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numleafpvsbytes * 8 * sizeof(*r_shadow_buffer_leaflist));
822         }
823         if (r_shadow_buffer_numsurfacepvsbytes < numsurfacepvsbytes)
824         {
825                 if (r_shadow_buffer_surfacepvs)
826                         Mem_Free(r_shadow_buffer_surfacepvs);
827                 if (r_shadow_buffer_surfacelist)
828                         Mem_Free(r_shadow_buffer_surfacelist);
829                 if (r_shadow_buffer_surfacesides)
830                         Mem_Free(r_shadow_buffer_surfacesides);
831                 r_shadow_buffer_numsurfacepvsbytes = numsurfacepvsbytes;
832                 r_shadow_buffer_surfacepvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numsurfacepvsbytes);
833                 r_shadow_buffer_surfacelist = (int *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numsurfacepvsbytes * 8 * sizeof(*r_shadow_buffer_surfacelist));
834                 r_shadow_buffer_surfacesides = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numsurfacepvsbytes * 8 * sizeof(*r_shadow_buffer_surfacelist));
835         }
836         if (r_shadow_buffer_numshadowtrispvsbytes < numshadowtrispvsbytes)
837         {
838                 if (r_shadow_buffer_shadowtrispvs)
839                         Mem_Free(r_shadow_buffer_shadowtrispvs);
840                 r_shadow_buffer_numshadowtrispvsbytes = numshadowtrispvsbytes;
841                 r_shadow_buffer_shadowtrispvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numshadowtrispvsbytes);
842         }
843         if (r_shadow_buffer_numlighttrispvsbytes < numlighttrispvsbytes)
844         {
845                 if (r_shadow_buffer_lighttrispvs)
846                         Mem_Free(r_shadow_buffer_lighttrispvs);
847                 r_shadow_buffer_numlighttrispvsbytes = numlighttrispvsbytes;
848                 r_shadow_buffer_lighttrispvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numlighttrispvsbytes);
849         }
850 }
851
852 void R_Shadow_PrepareShadowMark(int numtris)
853 {
854         // make sure shadowmark is big enough for this volume
855         if (maxshadowmark < numtris)
856         {
857                 maxshadowmark = numtris;
858                 if (shadowmark)
859                         Mem_Free(shadowmark);
860                 if (shadowmarklist)
861                         Mem_Free(shadowmarklist);
862                 shadowmark = (int *)Mem_Alloc(r_main_mempool, maxshadowmark * sizeof(*shadowmark));
863                 shadowmarklist = (int *)Mem_Alloc(r_main_mempool, maxshadowmark * sizeof(*shadowmarklist));
864                 shadowmarkcount = 0;
865         }
866         shadowmarkcount++;
867         // if shadowmarkcount wrapped we clear the array and adjust accordingly
868         if (shadowmarkcount == 0)
869         {
870                 shadowmarkcount = 1;
871                 memset(shadowmark, 0, maxshadowmark * sizeof(*shadowmark));
872         }
873         numshadowmark = 0;
874 }
875
876 void R_Shadow_PrepareShadowSides(int numtris)
877 {
878     if (maxshadowsides < numtris)
879     {
880         maxshadowsides = numtris;
881         if (shadowsides)
882                         Mem_Free(shadowsides);
883                 if (shadowsideslist)
884                         Mem_Free(shadowsideslist);
885                 shadowsides = (unsigned char *)Mem_Alloc(r_main_mempool, maxshadowsides * sizeof(*shadowsides));
886                 shadowsideslist = (int *)Mem_Alloc(r_main_mempool, maxshadowsides * sizeof(*shadowsideslist));
887         }
888         numshadowsides = 0;
889 }
890
891 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)
892 {
893         int i, j;
894         int outtriangles = 0, outvertices = 0;
895         const int *element;
896         const float *vertex;
897         float ratio, direction[3], projectvector[3];
898
899         if (projectdirection)
900                 VectorScale(projectdirection, projectdistance, projectvector);
901         else
902                 VectorClear(projectvector);
903
904         // create the vertices
905         if (projectdirection)
906         {
907                 for (i = 0;i < numshadowmarktris;i++)
908                 {
909                         element = inelement3i + shadowmarktris[i] * 3;
910                         for (j = 0;j < 3;j++)
911                         {
912                                 if (vertexupdate[element[j]] != vertexupdatenum)
913                                 {
914                                         vertexupdate[element[j]] = vertexupdatenum;
915                                         vertexremap[element[j]] = outvertices;
916                                         vertex = invertex3f + element[j] * 3;
917                                         // project one copy of the vertex according to projectvector
918                                         VectorCopy(vertex, outvertex3f);
919                                         VectorAdd(vertex, projectvector, (outvertex3f + 3));
920                                         outvertex3f += 6;
921                                         outvertices += 2;
922                                 }
923                         }
924                 }
925         }
926         else
927         {
928                 for (i = 0;i < numshadowmarktris;i++)
929                 {
930                         element = inelement3i + shadowmarktris[i] * 3;
931                         for (j = 0;j < 3;j++)
932                         {
933                                 if (vertexupdate[element[j]] != vertexupdatenum)
934                                 {
935                                         vertexupdate[element[j]] = vertexupdatenum;
936                                         vertexremap[element[j]] = outvertices;
937                                         vertex = invertex3f + element[j] * 3;
938                                         // project one copy of the vertex to the sphere radius of the light
939                                         // (FIXME: would projecting it to the light box be better?)
940                                         VectorSubtract(vertex, projectorigin, direction);
941                                         ratio = projectdistance / VectorLength(direction);
942                                         VectorCopy(vertex, outvertex3f);
943                                         VectorMA(projectorigin, ratio, direction, (outvertex3f + 3));
944                                         outvertex3f += 6;
945                                         outvertices += 2;
946                                 }
947                         }
948                 }
949         }
950
951         if (r_shadow_frontsidecasting.integer)
952         {
953                 for (i = 0;i < numshadowmarktris;i++)
954                 {
955                         int remappedelement[3];
956                         int markindex;
957                         const int *neighbortriangle;
958
959                         markindex = shadowmarktris[i] * 3;
960                         element = inelement3i + markindex;
961                         neighbortriangle = inneighbor3i + markindex;
962                         // output the front and back triangles
963                         outelement3i[0] = vertexremap[element[0]];
964                         outelement3i[1] = vertexremap[element[1]];
965                         outelement3i[2] = vertexremap[element[2]];
966                         outelement3i[3] = vertexremap[element[2]] + 1;
967                         outelement3i[4] = vertexremap[element[1]] + 1;
968                         outelement3i[5] = vertexremap[element[0]] + 1;
969
970                         outelement3i += 6;
971                         outtriangles += 2;
972                         // output the sides (facing outward from this triangle)
973                         if (shadowmark[neighbortriangle[0]] != shadowmarkcount)
974                         {
975                                 remappedelement[0] = vertexremap[element[0]];
976                                 remappedelement[1] = vertexremap[element[1]];
977                                 outelement3i[0] = remappedelement[1];
978                                 outelement3i[1] = remappedelement[0];
979                                 outelement3i[2] = remappedelement[0] + 1;
980                                 outelement3i[3] = remappedelement[1];
981                                 outelement3i[4] = remappedelement[0] + 1;
982                                 outelement3i[5] = remappedelement[1] + 1;
983
984                                 outelement3i += 6;
985                                 outtriangles += 2;
986                         }
987                         if (shadowmark[neighbortriangle[1]] != shadowmarkcount)
988                         {
989                                 remappedelement[1] = vertexremap[element[1]];
990                                 remappedelement[2] = vertexremap[element[2]];
991                                 outelement3i[0] = remappedelement[2];
992                                 outelement3i[1] = remappedelement[1];
993                                 outelement3i[2] = remappedelement[1] + 1;
994                                 outelement3i[3] = remappedelement[2];
995                                 outelement3i[4] = remappedelement[1] + 1;
996                                 outelement3i[5] = remappedelement[2] + 1;
997
998                                 outelement3i += 6;
999                                 outtriangles += 2;
1000                         }
1001                         if (shadowmark[neighbortriangle[2]] != shadowmarkcount)
1002                         {
1003                                 remappedelement[0] = vertexremap[element[0]];
1004                                 remappedelement[2] = vertexremap[element[2]];
1005                                 outelement3i[0] = remappedelement[0];
1006                                 outelement3i[1] = remappedelement[2];
1007                                 outelement3i[2] = remappedelement[2] + 1;
1008                                 outelement3i[3] = remappedelement[0];
1009                                 outelement3i[4] = remappedelement[2] + 1;
1010                                 outelement3i[5] = remappedelement[0] + 1;
1011
1012                                 outelement3i += 6;
1013                                 outtriangles += 2;
1014                         }
1015                 }
1016         }
1017         else
1018         {
1019                 for (i = 0;i < numshadowmarktris;i++)
1020                 {
1021                         int remappedelement[3];
1022                         int markindex;
1023                         const int *neighbortriangle;
1024
1025                         markindex = shadowmarktris[i] * 3;
1026                         element = inelement3i + markindex;
1027                         neighbortriangle = inneighbor3i + markindex;
1028                         // output the front and back triangles
1029                         outelement3i[0] = vertexremap[element[2]];
1030                         outelement3i[1] = vertexremap[element[1]];
1031                         outelement3i[2] = vertexremap[element[0]];
1032                         outelement3i[3] = vertexremap[element[0]] + 1;
1033                         outelement3i[4] = vertexremap[element[1]] + 1;
1034                         outelement3i[5] = vertexremap[element[2]] + 1;
1035
1036                         outelement3i += 6;
1037                         outtriangles += 2;
1038                         // output the sides (facing outward from this triangle)
1039                         if (shadowmark[neighbortriangle[0]] != shadowmarkcount)
1040                         {
1041                                 remappedelement[0] = vertexremap[element[0]];
1042                                 remappedelement[1] = vertexremap[element[1]];
1043                                 outelement3i[0] = remappedelement[0];
1044                                 outelement3i[1] = remappedelement[1];
1045                                 outelement3i[2] = remappedelement[1] + 1;
1046                                 outelement3i[3] = remappedelement[0];
1047                                 outelement3i[4] = remappedelement[1] + 1;
1048                                 outelement3i[5] = remappedelement[0] + 1;
1049
1050                                 outelement3i += 6;
1051                                 outtriangles += 2;
1052                         }
1053                         if (shadowmark[neighbortriangle[1]] != shadowmarkcount)
1054                         {
1055                                 remappedelement[1] = vertexremap[element[1]];
1056                                 remappedelement[2] = vertexremap[element[2]];
1057                                 outelement3i[0] = remappedelement[1];
1058                                 outelement3i[1] = remappedelement[2];
1059                                 outelement3i[2] = remappedelement[2] + 1;
1060                                 outelement3i[3] = remappedelement[1];
1061                                 outelement3i[4] = remappedelement[2] + 1;
1062                                 outelement3i[5] = remappedelement[1] + 1;
1063
1064                                 outelement3i += 6;
1065                                 outtriangles += 2;
1066                         }
1067                         if (shadowmark[neighbortriangle[2]] != shadowmarkcount)
1068                         {
1069                                 remappedelement[0] = vertexremap[element[0]];
1070                                 remappedelement[2] = vertexremap[element[2]];
1071                                 outelement3i[0] = remappedelement[2];
1072                                 outelement3i[1] = remappedelement[0];
1073                                 outelement3i[2] = remappedelement[0] + 1;
1074                                 outelement3i[3] = remappedelement[2];
1075                                 outelement3i[4] = remappedelement[0] + 1;
1076                                 outelement3i[5] = remappedelement[2] + 1;
1077
1078                                 outelement3i += 6;
1079                                 outtriangles += 2;
1080                         }
1081                 }
1082         }
1083         if (outnumvertices)
1084                 *outnumvertices = outvertices;
1085         return outtriangles;
1086 }
1087
1088 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)
1089 {
1090         int i, j, k;
1091         int outtriangles = 0, outvertices = 0;
1092         const int *element;
1093         const float *vertex;
1094         float ratio, direction[3], projectvector[3];
1095         qboolean side[4];
1096
1097         if (projectdirection)
1098                 VectorScale(projectdirection, projectdistance, projectvector);
1099         else
1100                 VectorClear(projectvector);
1101
1102         for (i = 0;i < numshadowmarktris;i++)
1103         {
1104                 int remappedelement[3];
1105                 int markindex;
1106                 const int *neighbortriangle;
1107
1108                 markindex = shadowmarktris[i] * 3;
1109                 neighbortriangle = inneighbor3i + markindex;
1110                 side[0] = shadowmark[neighbortriangle[0]] == shadowmarkcount;
1111                 side[1] = shadowmark[neighbortriangle[1]] == shadowmarkcount;
1112                 side[2] = shadowmark[neighbortriangle[2]] == shadowmarkcount;
1113                 if (side[0] + side[1] + side[2] == 0)
1114                         continue;
1115
1116                 side[3] = side[0];
1117                 element = inelement3i + markindex;
1118
1119                 // create the vertices
1120                 for (j = 0;j < 3;j++)
1121                 {
1122                         if (side[j] + side[j+1] == 0)
1123                                 continue;
1124                         k = element[j];
1125                         if (vertexupdate[k] != vertexupdatenum)
1126                         {
1127                                 vertexupdate[k] = vertexupdatenum;
1128                                 vertexremap[k] = outvertices;
1129                                 vertex = invertex3f + k * 3;
1130                                 VectorCopy(vertex, outvertex3f);
1131                                 if (projectdirection)
1132                                 {
1133                                         // project one copy of the vertex according to projectvector
1134                                         VectorAdd(vertex, projectvector, (outvertex3f + 3));
1135                                 }
1136                                 else
1137                                 {
1138                                         // project one copy of the vertex to the sphere radius of the light
1139                                         // (FIXME: would projecting it to the light box be better?)
1140                                         VectorSubtract(vertex, projectorigin, direction);
1141                                         ratio = projectdistance / VectorLength(direction);
1142                                         VectorMA(projectorigin, ratio, direction, (outvertex3f + 3));
1143                                 }
1144                                 outvertex3f += 6;
1145                                 outvertices += 2;
1146                         }
1147                 }
1148
1149                 // output the sides (facing outward from this triangle)
1150                 if (!side[0])
1151                 {
1152                         remappedelement[0] = vertexremap[element[0]];
1153                         remappedelement[1] = vertexremap[element[1]];
1154                         outelement3i[0] = remappedelement[1];
1155                         outelement3i[1] = remappedelement[0];
1156                         outelement3i[2] = remappedelement[0] + 1;
1157                         outelement3i[3] = remappedelement[1];
1158                         outelement3i[4] = remappedelement[0] + 1;
1159                         outelement3i[5] = remappedelement[1] + 1;
1160
1161                         outelement3i += 6;
1162                         outtriangles += 2;
1163                 }
1164                 if (!side[1])
1165                 {
1166                         remappedelement[1] = vertexremap[element[1]];
1167                         remappedelement[2] = vertexremap[element[2]];
1168                         outelement3i[0] = remappedelement[2];
1169                         outelement3i[1] = remappedelement[1];
1170                         outelement3i[2] = remappedelement[1] + 1;
1171                         outelement3i[3] = remappedelement[2];
1172                         outelement3i[4] = remappedelement[1] + 1;
1173                         outelement3i[5] = remappedelement[2] + 1;
1174
1175                         outelement3i += 6;
1176                         outtriangles += 2;
1177                 }
1178                 if (!side[2])
1179                 {
1180                         remappedelement[0] = vertexremap[element[0]];
1181                         remappedelement[2] = vertexremap[element[2]];
1182                         outelement3i[0] = remappedelement[0];
1183                         outelement3i[1] = remappedelement[2];
1184                         outelement3i[2] = remappedelement[2] + 1;
1185                         outelement3i[3] = remappedelement[0];
1186                         outelement3i[4] = remappedelement[2] + 1;
1187                         outelement3i[5] = remappedelement[0] + 1;
1188
1189                         outelement3i += 6;
1190                         outtriangles += 2;
1191                 }
1192         }
1193         if (outnumvertices)
1194                 *outnumvertices = outvertices;
1195         return outtriangles;
1196 }
1197
1198 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)
1199 {
1200         int t, tend;
1201         const int *e;
1202         const float *v[3];
1203         float normal[3];
1204         if (!BoxesOverlap(lightmins, lightmaxs, surfacemins, surfacemaxs))
1205                 return;
1206         tend = firsttriangle + numtris;
1207         if (BoxInsideBox(surfacemins, surfacemaxs, lightmins, lightmaxs))
1208         {
1209                 // surface box entirely inside light box, no box cull
1210                 if (projectdirection)
1211                 {
1212                         for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
1213                         {
1214                                 TriangleNormal(invertex3f + e[0] * 3, invertex3f + e[1] * 3, invertex3f + e[2] * 3, normal);
1215                                 if (r_shadow_frontsidecasting.integer == (DotProduct(normal, projectdirection) < 0))
1216                                         shadowmarklist[numshadowmark++] = t;
1217                         }
1218                 }
1219                 else
1220                 {
1221                         for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
1222                                 if (r_shadow_frontsidecasting.integer == PointInfrontOfTriangle(projectorigin, invertex3f + e[0] * 3, invertex3f + e[1] * 3, invertex3f + e[2] * 3))
1223                                         shadowmarklist[numshadowmark++] = t;
1224                 }
1225         }
1226         else
1227         {
1228                 // surface box not entirely inside light box, cull each triangle
1229                 if (projectdirection)
1230                 {
1231                         for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
1232                         {
1233                                 v[0] = invertex3f + e[0] * 3;
1234                                 v[1] = invertex3f + e[1] * 3;
1235                                 v[2] = invertex3f + e[2] * 3;
1236                                 TriangleNormal(v[0], v[1], v[2], normal);
1237                                 if (r_shadow_frontsidecasting.integer == (DotProduct(normal, projectdirection) < 0)
1238                                  && TriangleOverlapsBox(v[0], v[1], v[2], lightmins, lightmaxs))
1239                                         shadowmarklist[numshadowmark++] = t;
1240                         }
1241                 }
1242                 else
1243                 {
1244                         for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
1245                         {
1246                                 v[0] = invertex3f + e[0] * 3;
1247                                 v[1] = invertex3f + e[1] * 3;
1248                                 v[2] = invertex3f + e[2] * 3;
1249                                 if (r_shadow_frontsidecasting.integer == PointInfrontOfTriangle(projectorigin, v[0], v[1], v[2])
1250                                  && TriangleOverlapsBox(v[0], v[1], v[2], lightmins, lightmaxs))
1251                                         shadowmarklist[numshadowmark++] = t;
1252                         }
1253                 }
1254         }
1255 }
1256
1257 qboolean R_Shadow_UseZPass(vec3_t mins, vec3_t maxs)
1258 {
1259 #if 1
1260         return false;
1261 #else
1262         if (r_shadow_compilingrtlight || !r_shadow_frontsidecasting.integer || !r_shadow_usezpassifpossible.integer)
1263                 return false;
1264         // check if the shadow volume intersects the near plane
1265         //
1266         // a ray between the eye and light origin may intersect the caster,
1267         // indicating that the shadow may touch the eye location, however we must
1268         // test the near plane (a polygon), not merely the eye location, so it is
1269         // easiest to enlarge the caster bounding shape slightly for this.
1270         // TODO
1271         return true;
1272 #endif
1273 }
1274
1275 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)
1276 {
1277         int i, tris, outverts;
1278         if (projectdistance < 0.1)
1279         {
1280                 Con_Printf("R_Shadow_Volume: projectdistance %f\n", projectdistance);
1281                 return;
1282         }
1283         if (!numverts || !nummarktris)
1284                 return;
1285         // make sure shadowelements is big enough for this volume
1286         if (maxshadowtriangles < nummarktris*8 || maxshadowvertices < numverts*2)
1287                 R_Shadow_ResizeShadowArrays(numverts, nummarktris, 2, 8);
1288
1289         if (maxvertexupdate < numverts)
1290         {
1291                 maxvertexupdate = numverts;
1292                 if (vertexupdate)
1293                         Mem_Free(vertexupdate);
1294                 if (vertexremap)
1295                         Mem_Free(vertexremap);
1296                 vertexupdate = (int *)Mem_Alloc(r_main_mempool, maxvertexupdate * sizeof(int));
1297                 vertexremap = (int *)Mem_Alloc(r_main_mempool, maxvertexupdate * sizeof(int));
1298                 vertexupdatenum = 0;
1299         }
1300         vertexupdatenum++;
1301         if (vertexupdatenum == 0)
1302         {
1303                 vertexupdatenum = 1;
1304                 memset(vertexupdate, 0, maxvertexupdate * sizeof(int));
1305                 memset(vertexremap, 0, maxvertexupdate * sizeof(int));
1306         }
1307
1308         for (i = 0;i < nummarktris;i++)
1309                 shadowmark[marktris[i]] = shadowmarkcount;
1310
1311         if (r_shadow_compilingrtlight)
1312         {
1313                 // if we're compiling an rtlight, capture the mesh
1314                 //tris = R_Shadow_ConstructShadowVolume_ZPass(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
1315                 //Mod_ShadowMesh_AddMesh(r_main_mempool, r_shadow_compilingrtlight->static_meshchain_shadow_zpass, NULL, NULL, NULL, shadowvertex3f, NULL, NULL, NULL, NULL, tris, shadowelements);
1316                 tris = R_Shadow_ConstructShadowVolume_ZFail(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
1317                 Mod_ShadowMesh_AddMesh(r_main_mempool, r_shadow_compilingrtlight->static_meshchain_shadow_zfail, NULL, NULL, NULL, shadowvertex3f, NULL, NULL, NULL, NULL, tris, shadowelements);
1318         }
1319         else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_VISIBLEVOLUMES)
1320         {
1321                 tris = R_Shadow_ConstructShadowVolume_ZFail(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
1322                 R_Mesh_VertexPointer(shadowvertex3f, 0, 0);
1323                 R_Mesh_Draw(0, outverts, 0, tris, shadowelements, NULL, 0, 0);
1324         }
1325         else
1326         {
1327                 // decide which type of shadow to generate and set stencil mode
1328                 R_Shadow_RenderMode_StencilShadowVolumes(R_Shadow_UseZPass(trismins, trismaxs));
1329                 // generate the sides or a solid volume, depending on type
1330                 if (r_shadow_rendermode >= R_SHADOW_RENDERMODE_ZPASS_STENCIL && r_shadow_rendermode <= R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE)
1331                         tris = R_Shadow_ConstructShadowVolume_ZPass(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
1332                 else
1333                         tris = R_Shadow_ConstructShadowVolume_ZFail(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
1334                 r_refdef.stats.lights_dynamicshadowtriangles += tris;
1335                 r_refdef.stats.lights_shadowtriangles += tris;
1336                 CHECKGLERROR
1337                 R_Mesh_VertexPointer(shadowvertex3f, 0, 0);
1338                 GL_LockArrays(0, outverts);
1339                 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZPASS_STENCIL)
1340                 {
1341                         // increment stencil if frontface is infront of depthbuffer
1342                         GL_CullFace(r_refdef.view.cullface_front);
1343                         qglStencilOp(GL_KEEP, GL_KEEP, GL_DECR);CHECKGLERROR
1344                         R_Mesh_Draw(0, outverts, 0, tris, shadowelements, NULL, 0, 0);
1345                         // decrement stencil if backface is infront of depthbuffer
1346                         GL_CullFace(r_refdef.view.cullface_back);
1347                         qglStencilOp(GL_KEEP, GL_KEEP, GL_INCR);CHECKGLERROR
1348                 }
1349                 else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZFAIL_STENCIL)
1350                 {
1351                         // decrement stencil if backface is behind depthbuffer
1352                         GL_CullFace(r_refdef.view.cullface_front);
1353                         qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
1354                         R_Mesh_Draw(0, outverts, 0, tris, shadowelements, NULL, 0, 0);
1355                         // increment stencil if frontface is behind depthbuffer
1356                         GL_CullFace(r_refdef.view.cullface_back);
1357                         qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR
1358                 }
1359                 R_Mesh_Draw(0, outverts, 0, tris, shadowelements, NULL, 0, 0);
1360                 GL_LockArrays(0, 0);
1361                 CHECKGLERROR
1362         }
1363 }
1364
1365 int R_Shadow_CalcTriangleSideMask(const vec3_t p1, const vec3_t p2, const vec3_t p3, float bias)
1366 {
1367     // p1, p2, p3 are in the cubemap's local coordinate system
1368     // bias = border/(size - border)
1369         int mask = 0x3F;
1370
1371     float dp1 = p1[0] + p1[1], dn1 = p1[0] - p1[1], ap1 = fabs(dp1), an1 = fabs(dn1),
1372           dp2 = p2[0] + p2[1], dn2 = p2[0] - p2[1], ap2 = fabs(dp2), an2 = fabs(dn2),
1373           dp3 = p3[0] + p3[1], dn3 = p3[0] - p3[1], ap3 = fabs(dp3), an3 = fabs(dn3);
1374         if(ap1 > bias*an1 && ap2 > bias*an2 && ap3 > bias*an3)
1375         mask &= (3<<4)
1376                         | (dp1 >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2))
1377                         | (dp2 >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2))
1378                         | (dp3 >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2));
1379     if(an1 > bias*ap1 && an2 > bias*ap2 && an3 > bias*ap3)
1380         mask &= (3<<4)
1381             | (dn1 >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2))
1382             | (dn2 >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2))            
1383             | (dn3 >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2));
1384
1385     dp1 = p1[1] + p1[2], dn1 = p1[1] - p1[2], ap1 = fabs(dp1), an1 = fabs(dn1),
1386     dp2 = p2[1] + p2[2], dn2 = p2[1] - p2[2], ap2 = fabs(dp2), an2 = fabs(dn2),
1387     dp3 = p3[1] + p3[2], dn3 = p3[1] - p3[2], ap3 = fabs(dp3), an3 = fabs(dn3);
1388     if(ap1 > bias*an1 && ap2 > bias*an2 && ap3 > bias*an3)
1389         mask &= (3<<0)
1390             | (dp1 >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4))
1391             | (dp2 >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4))            
1392             | (dp3 >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4));
1393     if(an1 > bias*ap1 && an2 > bias*ap2 && an3 > bias*ap3)
1394         mask &= (3<<0)
1395             | (dn1 >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4))
1396             | (dn2 >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4))
1397             | (dn3 >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4));
1398
1399     dp1 = p1[2] + p1[0], dn1 = p1[2] - p1[0], ap1 = fabs(dp1), an1 = fabs(dn1),
1400     dp2 = p2[2] + p2[0], dn2 = p2[2] - p2[0], ap2 = fabs(dp2), an2 = fabs(dn2),
1401     dp3 = p3[2] + p3[0], dn3 = p3[2] - p3[0], ap3 = fabs(dp3), an3 = fabs(dn3);
1402     if(ap1 > bias*an1 && ap2 > bias*an2 && ap3 > bias*an3)
1403         mask &= (3<<2)
1404             | (dp1 >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0))
1405             | (dp2 >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0))
1406             | (dp3 >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0));
1407     if(an1 > bias*ap1 && an2 > bias*ap2 && an3 > bias*ap3)
1408         mask &= (3<<2)
1409             | (dn1 >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0))
1410             | (dn2 >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0))
1411             | (dn3 >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0));
1412
1413         return mask;
1414 }
1415
1416 int R_Shadow_CalcBBoxSideMask(const vec3_t mins, const vec3_t maxs, const matrix4x4_t *worldtolight, const matrix4x4_t *radiustolight, float bias)
1417 {
1418         vec3_t center, radius, lightcenter, lightradius, pmin, pmax;
1419         float dp1, dn1, ap1, an1, dp2, dn2, ap2, an2;
1420         int mask = 0x3F;
1421
1422         VectorSubtract(maxs, mins, radius);
1423     VectorScale(radius, 0.5f, radius);
1424     VectorAdd(mins, radius, center);
1425     Matrix4x4_Transform(worldtolight, center, lightcenter);
1426         Matrix4x4_Transform3x3(radiustolight, radius, lightradius);
1427         VectorSubtract(lightcenter, lightradius, pmin);
1428         VectorAdd(lightcenter, lightradius, pmax);
1429
1430     dp1 = pmax[0] + pmax[1], dn1 = pmax[0] - pmin[1], ap1 = fabs(dp1), an1 = fabs(dn1),
1431     dp2 = pmin[0] + pmin[1], dn2 = pmin[0] - pmax[1], ap2 = fabs(dp2), an2 = fabs(dn2);
1432     if(ap1 > bias*an1 && ap2 > bias*an2)
1433         mask &= (3<<4)
1434             | (dp1 >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2))
1435             | (dp2 >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2));
1436     if(an1 > bias*ap1 && an2 > bias*ap2)
1437         mask &= (3<<4)
1438             | (dn1 >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2))
1439             | (dn2 >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2));
1440
1441     dp1 = pmax[1] + pmax[2], dn1 = pmax[1] - pmin[2], ap1 = fabs(dp1), an1 = fabs(dn1),
1442     dp2 = pmin[1] + pmin[2], dn2 = pmin[1] - pmax[2], ap2 = fabs(dp2), an2 = fabs(dn2);
1443     if(ap1 > bias*an1 && ap2 > bias*an2)
1444         mask &= (3<<0)
1445             | (dp1 >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4))
1446             | (dp2 >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4));
1447     if(an1 > bias*ap1 && an2 > bias*ap2)
1448         mask &= (3<<0)
1449             | (dn1 >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4))
1450             | (dn2 >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4));
1451
1452     dp1 = pmax[2] + pmax[0], dn1 = pmax[2] - pmin[0], ap1 = fabs(dp1), an1 = fabs(dn1),
1453     dp2 = pmin[2] + pmin[0], dn2 = pmin[2] - pmax[0], ap2 = fabs(dp2), an2 = fabs(dn2);
1454     if(ap1 > bias*an1 && ap2 > bias*an2)
1455         mask &= (3<<2)
1456             | (dp1 >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0))
1457             | (dp2 >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0));
1458     if(an1 > bias*ap1 && an2 > bias*ap2)
1459         mask &= (3<<2)
1460             | (dn1 >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0))
1461             | (dn2 >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0));
1462
1463     return mask;
1464 }
1465
1466 #define R_Shadow_CalcEntitySideMask(ent, worldtolight, radiustolight, bias) R_Shadow_CalcBBoxSideMask((ent)->mins, (ent)->maxs, worldtolight, radiustolight, bias)
1467
1468 int R_Shadow_CalcSphereSideMask(const vec3_t p, float radius, float bias)
1469 {
1470     // p is in the cubemap's local coordinate system
1471     // bias = border/(size - border)
1472     float dxyp = p[0] + p[1], dxyn = p[0] - p[1], axyp = fabs(dxyp), axyn = fabs(dxyn);
1473     float dyzp = p[1] + p[2], dyzn = p[1] - p[2], ayzp = fabs(dyzp), ayzn = fabs(dyzn);
1474     float dzxp = p[2] + p[0], dzxn = p[2] - p[0], azxp = fabs(dzxp), azxn = fabs(dzxn);
1475     int mask = 0x3F;
1476     if(axyp > bias*axyn + radius) mask &= dxyp < 0 ? ~((1<<0)|(1<<2)) : ~((2<<0)|(2<<2));
1477     if(axyn > bias*axyp + radius) mask &= dxyn < 0 ? ~((1<<0)|(2<<2)) : ~((2<<0)|(1<<2));
1478     if(ayzp > bias*ayzn + radius) mask &= dyzp < 0 ? ~((1<<2)|(1<<4)) : ~((2<<2)|(2<<4));
1479     if(ayzn > bias*ayzp + radius) mask &= dyzn < 0 ? ~((1<<2)|(2<<4)) : ~((2<<2)|(1<<4));
1480     if(azxp > bias*azxn + radius) mask &= dzxp < 0 ? ~((1<<4)|(1<<0)) : ~((2<<4)|(2<<0));
1481     if(azxn > bias*azxp + radius) mask &= dzxn < 0 ? ~((1<<4)|(2<<0)) : ~((2<<4)|(1<<0));
1482     return mask;
1483 }
1484
1485 int R_Shadow_CullFrustumSides(rtlight_t *rtlight, float size, float border)
1486 {
1487         int i;
1488         vec3_t p, n;
1489         int sides = 0x3F, masks[6] = { 3<<4, 3<<4, 3<<0, 3<<0, 3<<2, 3<<2 };
1490         float scale = (size - 2*border)/size, len;
1491         float bias = border / (float)(size - border), dp, dn, ap, an;
1492         // check if cone enclosing side would cross frustum plane 
1493         scale = 2 / (scale*scale + 2);
1494         for (i = 0;i < 5;i++)
1495         {
1496                 if (PlaneDiff(rtlight->shadoworigin, &r_refdef.view.frustum[i]) > -0.03125)
1497                         continue;
1498                 Matrix4x4_Transform3x3(&rtlight->matrix_worldtolight, r_refdef.view.frustum[i].normal, n);
1499                 len = scale*VectorLength2(n);
1500                 if(n[0]*n[0] > len) sides &= n[0] < 0 ? ~(1<<0) : ~(2 << 0);
1501                 if(n[1]*n[1] > len) sides &= n[1] < 0 ? ~(1<<2) : ~(2 << 2);
1502                 if(n[2]*n[2] > len) sides &= n[2] < 0 ? ~(1<<4) : ~(2 << 4);
1503         }
1504         if (PlaneDiff(rtlight->shadoworigin, &r_refdef.view.frustum[4]) >= r_refdef.farclip - r_refdef.nearclip + 0.03125)
1505         {
1506         Matrix4x4_Transform3x3(&rtlight->matrix_worldtolight, r_refdef.view.frustum[4].normal, n);
1507         len = scale*VectorLength(n);
1508                 if(n[0]*n[0] > len) sides &= n[0] >= 0 ? ~(1<<0) : ~(2 << 0);
1509                 if(n[1]*n[1] > len) sides &= n[1] >= 0 ? ~(1<<2) : ~(2 << 2);
1510                 if(n[2]*n[2] > len) sides &= n[2] >= 0 ? ~(1<<4) : ~(2 << 4);
1511         }
1512         // this next test usually clips off more sides than the former, but occasionally clips fewer/different ones, so do both and combine results
1513         // check if frustum corners/origin cross plane sides
1514         for (i = 0;i < 5;i++)
1515         {
1516                 Matrix4x4_Transform(&rtlight->matrix_worldtolight, !i ? r_refdef.view.origin : r_refdef.view.frustumcorner[i-1], p);
1517                 dp = p[0] + p[1], dn = p[0] - p[1], ap = fabs(dp), an = fabs(dn),
1518                 masks[0] |= ap <= bias*an ? 0x3F : (dp >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2));
1519                 masks[1] |= an <= bias*ap ? 0x3F : (dn >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2));
1520                 dp = p[1] + p[2], dn = p[1] - p[2], ap = fabs(dp), an = fabs(dn),
1521                 masks[2] |= ap <= bias*an ? 0x3F : (dp >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4));
1522                 masks[3] |= an <= bias*ap ? 0x3F : (dn >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4));
1523                 dp = p[2] + p[0], dn = p[2] - p[0], ap = fabs(dp), an = fabs(dn),
1524                 masks[4] |= ap <= bias*an ? 0x3F : (dp >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0));
1525                 masks[5] |= an <= bias*ap ? 0x3F : (dn >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0));
1526         }
1527         return sides & masks[0] & masks[1] & masks[2] & masks[3] & masks[4] & masks[5];
1528 }
1529
1530 int R_Shadow_ChooseSidesFromBox(int firsttriangle, int numtris, const float *invertex3f, const int *elements, const matrix4x4_t *worldtolight, const vec3_t projectorigin, const vec3_t projectdirection, const vec3_t lightmins, const vec3_t lightmaxs, const vec3_t surfacemins, const vec3_t surfacemaxs, int *totals)
1531 {
1532         int t, tend;
1533         const int *e;
1534         const float *v[3];
1535         float normal[3];
1536         vec3_t p[3];
1537         float bias;
1538         int mask, surfacemask = 0;
1539         if (!BoxesOverlap(lightmins, lightmaxs, surfacemins, surfacemaxs))
1540                 return 0;
1541         bias = r_shadow_shadowmapborder / (float)(r_shadow_shadowmapmaxsize - r_shadow_shadowmapborder);
1542         tend = firsttriangle + numtris;
1543         if (BoxInsideBox(surfacemins, surfacemaxs, lightmins, lightmaxs))
1544         {
1545                 // surface box entirely inside light box, no box cull
1546                 if (projectdirection)
1547                 {
1548                         for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
1549                         {
1550                                 v[0] = invertex3f + e[0] * 3, v[1] = invertex3f + e[1] * 3, v[2] = invertex3f + e[2] * 3;
1551                                 TriangleNormal(v[0], v[1], v[2], normal);
1552                                 if (r_shadow_frontsidecasting.integer == (DotProduct(normal, projectdirection) < 0))
1553                                 {
1554                                         Matrix4x4_Transform(worldtolight, v[0], p[0]), Matrix4x4_Transform(worldtolight, v[1], p[1]), Matrix4x4_Transform(worldtolight, v[2], p[2]);
1555                                         mask = R_Shadow_CalcTriangleSideMask(p[0], p[1], p[2], bias);
1556                                         surfacemask |= mask;
1557                                         if(totals)
1558                                         {
1559                                                 totals[0] += mask&1, totals[1] += (mask>>1)&1, totals[2] += (mask>>2)&1, totals[3] += (mask>>3)&1, totals[4] += (mask>>4)&1, totals[5] += mask>>5;
1560                                                 shadowsides[numshadowsides] = mask;
1561                                                 shadowsideslist[numshadowsides++] = t;
1562                                         }
1563                                 }
1564                         }
1565                 }
1566                 else
1567                 {
1568                         for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
1569                         {
1570                                 v[0] = invertex3f + e[0] * 3, v[1] = invertex3f + e[1] * 3,     v[2] = invertex3f + e[2] * 3;
1571                                 if (r_shadow_frontsidecasting.integer == PointInfrontOfTriangle(projectorigin, v[0], v[1], v[2]))
1572                                 {
1573                                         Matrix4x4_Transform(worldtolight, v[0], p[0]), Matrix4x4_Transform(worldtolight, v[1], p[1]), Matrix4x4_Transform(worldtolight, v[2], p[2]);
1574                                         mask = R_Shadow_CalcTriangleSideMask(p[0], p[1], p[2], bias);
1575                                         surfacemask |= mask;
1576                                         if(totals)
1577                                         {
1578                                                 totals[0] += mask&1, totals[1] += (mask>>1)&1, totals[2] += (mask>>2)&1, totals[3] += (mask>>3)&1, totals[4] += (mask>>4)&1, totals[5] += mask>>5;
1579                                                 shadowsides[numshadowsides] = mask;
1580                                                 shadowsideslist[numshadowsides++] = t;
1581                                         }
1582                                 }
1583                         }
1584                 }
1585         }
1586         else
1587         {
1588                 // surface box not entirely inside light box, cull each triangle
1589                 if (projectdirection)
1590                 {
1591                         for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
1592                         {
1593                                 v[0] = invertex3f + e[0] * 3, v[1] = invertex3f + e[1] * 3,     v[2] = invertex3f + e[2] * 3;
1594                                 TriangleNormal(v[0], v[1], v[2], normal);
1595                                 if (r_shadow_frontsidecasting.integer == (DotProduct(normal, projectdirection) < 0)
1596                                  && TriangleOverlapsBox(v[0], v[1], v[2], lightmins, lightmaxs))
1597                                 {
1598                                         Matrix4x4_Transform(worldtolight, v[0], p[0]), Matrix4x4_Transform(worldtolight, v[1], p[1]), Matrix4x4_Transform(worldtolight, v[2], p[2]);
1599                                         mask = R_Shadow_CalcTriangleSideMask(p[0], p[1], p[2], bias);
1600                                         surfacemask |= mask;
1601                                         if(totals)
1602                                         {
1603                                                 totals[0] += mask&1, totals[1] += (mask>>1)&1, totals[2] += (mask>>2)&1, totals[3] += (mask>>3)&1, totals[4] += (mask>>4)&1, totals[5] += mask>>5;
1604                                                 shadowsides[numshadowsides] = mask;
1605                                                 shadowsideslist[numshadowsides++] = t;
1606                                         }
1607                                 }
1608                         }
1609                 }
1610                 else
1611                 {
1612                         for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
1613                         {
1614                                 v[0] = invertex3f + e[0] * 3, v[1] = invertex3f + e[1] * 3, v[2] = invertex3f + e[2] * 3;
1615                                 if (r_shadow_frontsidecasting.integer == PointInfrontOfTriangle(projectorigin, v[0], v[1], v[2])
1616                                  && TriangleOverlapsBox(v[0], v[1], v[2], lightmins, lightmaxs))
1617                                 {
1618                                         Matrix4x4_Transform(worldtolight, v[0], p[0]), Matrix4x4_Transform(worldtolight, v[1], p[1]), Matrix4x4_Transform(worldtolight, v[2], p[2]);
1619                                         mask = R_Shadow_CalcTriangleSideMask(p[0], p[1], p[2], bias);
1620                                         surfacemask |= mask;
1621                                         if(totals)
1622                                         {
1623                                                 totals[0] += mask&1, totals[1] += (mask>>1)&1, totals[2] += (mask>>2)&1, totals[3] += (mask>>3)&1, totals[4] += (mask>>4)&1, totals[5] += mask>>5;
1624                                                 shadowsides[numshadowsides] = mask;
1625                                                 shadowsideslist[numshadowsides++] = t;
1626                                         }
1627                                 }
1628                         }
1629                 }
1630         }
1631         return surfacemask;
1632 }
1633
1634 void R_Shadow_ShadowMapFromList(int numverts, int numtris, const float *vertex3f, const int *elements, int numsidetris, const int *sidetotals, const unsigned char *sides, const int *sidetris)
1635 {
1636         int i, j, outtriangles = 0;
1637         int *outelement3i[6];
1638         if (!numverts || !numsidetris || !r_shadow_compilingrtlight)
1639                 return;
1640         outtriangles = sidetotals[0] + sidetotals[1] + sidetotals[2] + sidetotals[3] + sidetotals[4] + sidetotals[5];
1641         // make sure shadowelements is big enough for this mesh
1642         if (maxshadowtriangles < outtriangles)
1643                 R_Shadow_ResizeShadowArrays(0, outtriangles, 0, 1);
1644
1645         // compute the offset and size of the separate index lists for each cubemap side
1646         outtriangles = 0;
1647         for (i = 0;i < 6;i++)
1648         {
1649                 outelement3i[i] = shadowelements + outtriangles * 3;
1650                 r_shadow_compilingrtlight->static_meshchain_shadow_shadowmap->sideoffsets[i] = outtriangles;
1651                 r_shadow_compilingrtlight->static_meshchain_shadow_shadowmap->sidetotals[i] = sidetotals[i];
1652                 outtriangles += sidetotals[i];
1653         }
1654
1655         // gather up the (sparse) triangles into separate index lists for each cubemap side
1656         for (i = 0;i < numsidetris;i++)
1657         {
1658                 const int *element = elements + sidetris[i] * 3;
1659                 for (j = 0;j < 6;j++)
1660                 {
1661                         if (sides[i] & (1 << j))
1662                         {
1663                                 outelement3i[j][0] = element[0];
1664                                 outelement3i[j][1] = element[1];
1665                                 outelement3i[j][2] = element[2];
1666                                 outelement3i[j] += 3;
1667                         }
1668                 }
1669         }
1670                         
1671         Mod_ShadowMesh_AddMesh(r_main_mempool, r_shadow_compilingrtlight->static_meshchain_shadow_shadowmap, NULL, NULL, NULL, vertex3f, NULL, NULL, NULL, NULL, outtriangles, shadowelements);
1672 }
1673
1674 static void R_Shadow_MakeTextures_MakeCorona(void)
1675 {
1676         float dx, dy;
1677         int x, y, a;
1678         unsigned char pixels[32][32][4];
1679         for (y = 0;y < 32;y++)
1680         {
1681                 dy = (y - 15.5f) * (1.0f / 16.0f);
1682                 for (x = 0;x < 32;x++)
1683                 {
1684                         dx = (x - 15.5f) * (1.0f / 16.0f);
1685                         a = (int)(((1.0f / (dx * dx + dy * dy + 0.2f)) - (1.0f / (1.0f + 0.2))) * 32.0f / (1.0f / (1.0f + 0.2)));
1686                         a = bound(0, a, 255);
1687                         pixels[y][x][0] = a;
1688                         pixels[y][x][1] = a;
1689                         pixels[y][x][2] = a;
1690                         pixels[y][x][3] = 255;
1691                 }
1692         }
1693         r_shadow_lightcorona = R_SkinFrame_LoadInternalBGRA("lightcorona", TEXF_PRECACHE | TEXF_FORCELINEAR, &pixels[0][0][0], 32, 32);
1694 }
1695
1696 static unsigned int R_Shadow_MakeTextures_SamplePoint(float x, float y, float z)
1697 {
1698         float dist = sqrt(x*x+y*y+z*z);
1699         float intensity = dist < 1 ? ((1.0f - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist)) : 0;
1700         // note this code could suffer byte order issues except that it is multiplying by an integer that reads the same both ways
1701         return (unsigned char)bound(0, intensity * 256.0f, 255) * 0x01010101;
1702 }
1703
1704 static void R_Shadow_MakeTextures(void)
1705 {
1706         int x, y, z;
1707         float intensity, dist;
1708         unsigned int *data;
1709         R_Shadow_FreeShadowMaps();
1710         R_FreeTexturePool(&r_shadow_texturepool);
1711         r_shadow_texturepool = R_AllocTexturePool();
1712         r_shadow_attenlinearscale = r_shadow_lightattenuationlinearscale.value;
1713         r_shadow_attendividebias = r_shadow_lightattenuationdividebias.value;
1714         data = (unsigned int *)Mem_Alloc(tempmempool, max(max(ATTEN3DSIZE*ATTEN3DSIZE*ATTEN3DSIZE, ATTEN2DSIZE*ATTEN2DSIZE), ATTEN1DSIZE) * 4);
1715         // the table includes one additional value to avoid the need to clamp indexing due to minor math errors
1716         for (x = 0;x <= ATTENTABLESIZE;x++)
1717         {
1718                 dist = (x + 0.5f) * (1.0f / ATTENTABLESIZE) * (1.0f / 0.9375);
1719                 intensity = dist < 1 ? ((1.0f - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist)) : 0;
1720                 r_shadow_attentable[x] = bound(0, intensity, 1);
1721         }
1722         // 1D gradient texture
1723         for (x = 0;x < ATTEN1DSIZE;x++)
1724                 data[x] = R_Shadow_MakeTextures_SamplePoint((x + 0.5f) * (1.0f / ATTEN1DSIZE) * (1.0f / 0.9375), 0, 0);
1725         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);
1726         // 2D circle texture
1727         for (y = 0;y < ATTEN2DSIZE;y++)
1728                 for (x = 0;x < ATTEN2DSIZE;x++)
1729                         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);
1730         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);
1731         // 3D sphere texture
1732         if (r_shadow_texture3d.integer && vid.support.ext_texture_3d)
1733         {
1734                 for (z = 0;z < ATTEN3DSIZE;z++)
1735                         for (y = 0;y < ATTEN3DSIZE;y++)
1736                                 for (x = 0;x < ATTEN3DSIZE;x++)
1737                                         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));
1738                 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);
1739         }
1740         else
1741                 r_shadow_attenuation3dtexture = NULL;
1742         Mem_Free(data);
1743
1744         R_Shadow_MakeTextures_MakeCorona();
1745
1746         // Editor light sprites
1747         r_editlights_sprcursor = R_SkinFrame_LoadInternal8bit("gfx/editlights/cursor", TEXF_PRECACHE | TEXF_ALPHA | TEXF_CLAMP, (const unsigned char *)
1748         "................"
1749         ".3............3."
1750         "..5...2332...5.."
1751         "...7.3....3.7..."
1752         "....7......7...."
1753         "...3.7....7.3..."
1754         "..2...7..7...2.."
1755         "..3..........3.."
1756         "..3..........3.."
1757         "..2...7..7...2.."
1758         "...3.7....7.3..."
1759         "....7......7...."
1760         "...7.3....3.7..."
1761         "..5...2332...5.."
1762         ".3............3."
1763         "................"
1764         , 16, 16, palette_bgra_embeddedpic, palette_bgra_embeddedpic);
1765         r_editlights_sprlight = R_SkinFrame_LoadInternal8bit("gfx/editlights/light", TEXF_PRECACHE | TEXF_ALPHA | TEXF_CLAMP, (const unsigned char *)
1766         "................"
1767         "................"
1768         "......1111......"
1769         "....11233211...."
1770         "...1234554321..."
1771         "...1356776531..."
1772         "..124677776421.."
1773         "..135777777531.."
1774         "..135777777531.."
1775         "..124677776421.."
1776         "...1356776531..."
1777         "...1234554321..."
1778         "....11233211...."
1779         "......1111......"
1780         "................"
1781         "................"
1782         , 16, 16, palette_bgra_embeddedpic, palette_bgra_embeddedpic);
1783         r_editlights_sprnoshadowlight = R_SkinFrame_LoadInternal8bit("gfx/editlights/noshadow", TEXF_PRECACHE | TEXF_ALPHA | TEXF_CLAMP, (const unsigned char *)
1784         "................"
1785         "................"
1786         "......1111......"
1787         "....11233211...."
1788         "...1234554321..."
1789         "...1356226531..."
1790         "..12462..26421.."
1791         "..1352....2531.."
1792         "..1352....2531.."
1793         "..12462..26421.."
1794         "...1356226531..."
1795         "...1234554321..."
1796         "....11233211...."
1797         "......1111......"
1798         "................"
1799         "................"
1800         , 16, 16, palette_bgra_embeddedpic, palette_bgra_embeddedpic);
1801         r_editlights_sprcubemaplight = R_SkinFrame_LoadInternal8bit("gfx/editlights/cubemaplight", TEXF_PRECACHE | TEXF_ALPHA | TEXF_CLAMP, (const unsigned char *)
1802         "................"
1803         "................"
1804         "......2772......"
1805         "....27755772...."
1806         "..277533335772.."
1807         "..753333333357.."
1808         "..777533335777.."
1809         "..735775577537.."
1810         "..733357753337.."
1811         "..733337733337.."
1812         "..753337733357.."
1813         "..277537735772.."
1814         "....27777772...."
1815         "......2772......"
1816         "................"
1817         "................"
1818         , 16, 16, palette_bgra_embeddedpic, palette_bgra_embeddedpic);
1819         r_editlights_sprcubemapnoshadowlight = R_SkinFrame_LoadInternal8bit("gfx/editlights/cubemapnoshadowlight", TEXF_PRECACHE | TEXF_ALPHA | TEXF_CLAMP, (const unsigned char *)
1820         "................"
1821         "................"
1822         "......2772......"
1823         "....27722772...."
1824         "..2772....2772.."
1825         "..72........27.."
1826         "..7772....2777.."
1827         "..7.27722772.7.."
1828         "..7...2772...7.."
1829         "..7....77....7.."
1830         "..72...77...27.."
1831         "..2772.77.2772.."
1832         "....27777772...."
1833         "......2772......"
1834         "................"
1835         "................"
1836         , 16, 16, palette_bgra_embeddedpic, palette_bgra_embeddedpic);
1837         r_editlights_sprselection = R_SkinFrame_LoadInternal8bit("gfx/editlights/selection", TEXF_PRECACHE | TEXF_ALPHA | TEXF_CLAMP, (unsigned char *)
1838         "................"
1839         ".777752..257777."
1840         ".742........247."
1841         ".72..........27."
1842         ".7............7."
1843         ".5............5."
1844         ".2............2."
1845         "................"
1846         "................"
1847         ".2............2."
1848         ".5............5."
1849         ".7............7."
1850         ".72..........27."
1851         ".742........247."
1852         ".777752..257777."
1853         "................"
1854         , 16, 16, palette_bgra_embeddedpic, palette_bgra_embeddedpic);
1855 }
1856
1857 void R_Shadow_ValidateCvars(void)
1858 {
1859         if (r_shadow_texture3d.integer && !vid.support.ext_texture_3d)
1860                 Cvar_SetValueQuick(&r_shadow_texture3d, 0);
1861         if (gl_ext_separatestencil.integer && !vid.support.ati_separate_stencil)
1862                 Cvar_SetValueQuick(&gl_ext_separatestencil, 0);
1863         if (gl_ext_stenciltwoside.integer && !vid.support.ext_stencil_two_side)
1864                 Cvar_SetValueQuick(&gl_ext_stenciltwoside, 0);
1865 }
1866
1867 void R_Shadow_RenderMode_Begin(void)
1868 {
1869 #if 0
1870         GLint drawbuffer;
1871         GLint readbuffer;
1872 #endif
1873         R_Shadow_ValidateCvars();
1874
1875         if (!r_shadow_attenuation2dtexture
1876          || (!r_shadow_attenuation3dtexture && r_shadow_texture3d.integer)
1877          || r_shadow_lightattenuationdividebias.value != r_shadow_attendividebias
1878          || r_shadow_lightattenuationlinearscale.value != r_shadow_attenlinearscale)
1879                 R_Shadow_MakeTextures();
1880
1881         CHECKGLERROR
1882         R_Mesh_ColorPointer(NULL, 0, 0);
1883         R_Mesh_ResetTextureState();
1884         GL_BlendFunc(GL_ONE, GL_ZERO);
1885         GL_DepthRange(0, 1);
1886         GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
1887         GL_DepthTest(true);
1888         GL_DepthMask(false);
1889         GL_Color(0, 0, 0, 1);
1890         GL_Scissor(r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
1891
1892         r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
1893
1894         if (gl_ext_separatestencil.integer && vid.support.ati_separate_stencil)
1895         {
1896                 r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_SEPARATESTENCIL;
1897                 r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_SEPARATESTENCIL;
1898         }
1899         else if (gl_ext_stenciltwoside.integer && vid.support.ext_stencil_two_side)
1900         {
1901                 r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE;
1902                 r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE;
1903         }
1904         else
1905         {
1906                 r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_STENCIL;
1907                 r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_STENCIL;
1908         }
1909
1910         switch(vid.renderpath)
1911         {
1912         case RENDERPATH_GL20:
1913                 r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_GLSL;
1914                 break;
1915         case RENDERPATH_GL13:
1916         case RENDERPATH_GL11:
1917                 if (r_textureunits.integer >= 2 && vid.texunits >= 2 && r_shadow_texture3d.integer && r_shadow_attenuation3dtexture)
1918                         r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_VERTEX3DATTEN;
1919                 else if (r_textureunits.integer >= 3 && vid.texunits >= 3)
1920                         r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_VERTEX2D1DATTEN;
1921                 else if (r_textureunits.integer >= 2 && vid.texunits >= 2)
1922                         r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_VERTEX2DATTEN;
1923                 else
1924                         r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_VERTEX;
1925                 break;
1926         }
1927
1928         CHECKGLERROR
1929 #if 0
1930         qglGetIntegerv(GL_DRAW_BUFFER, &drawbuffer);CHECKGLERROR
1931         qglGetIntegerv(GL_READ_BUFFER, &readbuffer);CHECKGLERROR
1932         r_shadow_drawbuffer = drawbuffer;
1933         r_shadow_readbuffer = readbuffer;
1934 #endif
1935         r_shadow_cullface_front = r_refdef.view.cullface_front;
1936         r_shadow_cullface_back = r_refdef.view.cullface_back;
1937 }
1938
1939 void R_Shadow_RenderMode_ActiveLight(const rtlight_t *rtlight)
1940 {
1941         rsurface.rtlight = rtlight;
1942 }
1943
1944 void R_Shadow_RenderMode_Reset(void)
1945 {
1946         CHECKGLERROR
1947         if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE || r_shadow_rendermode == R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE)
1948         {
1949                 qglDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
1950         }
1951         if (vid.support.ext_framebuffer_object)
1952         {
1953                 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);CHECKGLERROR
1954         }
1955 #if 0
1956         qglDrawBuffer(r_shadow_drawbuffer);CHECKGLERROR
1957         qglReadBuffer(r_shadow_readbuffer);CHECKGLERROR
1958 #endif
1959         R_SetViewport(&r_refdef.view.viewport);
1960         GL_Scissor(r_shadow_lightscissor[0], r_shadow_lightscissor[1], r_shadow_lightscissor[2], r_shadow_lightscissor[3]);
1961         R_Mesh_ColorPointer(NULL, 0, 0);
1962         R_Mesh_ResetTextureState();
1963         GL_DepthRange(0, 1);
1964         GL_DepthTest(true);
1965         GL_DepthMask(false);
1966         qglDepthFunc(GL_LEQUAL);CHECKGLERROR
1967         GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);CHECKGLERROR
1968         qglDisable(GL_STENCIL_TEST);CHECKGLERROR
1969         qglStencilMask(~0);CHECKGLERROR
1970         qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);CHECKGLERROR
1971         qglStencilFunc(GL_ALWAYS, 128, ~0);CHECKGLERROR
1972         r_refdef.view.cullface_front = r_shadow_cullface_front;
1973         r_refdef.view.cullface_back = r_shadow_cullface_back;
1974         GL_CullFace(r_refdef.view.cullface_back);
1975         GL_Color(1, 1, 1, 1);
1976         GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
1977         GL_BlendFunc(GL_ONE, GL_ZERO);
1978         R_SetupGenericShader(false);
1979         r_shadow_usingshadowmaprect = false;
1980         r_shadow_usingshadowmapcube = false;
1981         r_shadow_usingshadowmap2d = false;
1982         CHECKGLERROR
1983 }
1984
1985 void R_Shadow_ClearStencil(void)
1986 {
1987         CHECKGLERROR
1988         GL_Clear(GL_STENCIL_BUFFER_BIT);
1989         r_refdef.stats.lights_clears++;
1990 }
1991
1992 void R_Shadow_RenderMode_StencilShadowVolumes(qboolean zpass)
1993 {
1994         r_shadow_rendermode_t mode = zpass ? r_shadow_shadowingrendermode_zpass : r_shadow_shadowingrendermode_zfail;
1995         if (r_shadow_rendermode == mode)
1996                 return;
1997         CHECKGLERROR
1998         R_Shadow_RenderMode_Reset();
1999         GL_ColorMask(0, 0, 0, 0);
2000         GL_PolygonOffset(r_refdef.shadowpolygonfactor, r_refdef.shadowpolygonoffset);CHECKGLERROR
2001         R_SetupDepthOrShadowShader();
2002         qglDepthFunc(GL_LESS);CHECKGLERROR
2003         qglEnable(GL_STENCIL_TEST);CHECKGLERROR
2004         r_shadow_rendermode = mode;
2005         switch(mode)
2006         {
2007         default:
2008                 break;
2009         case R_SHADOW_RENDERMODE_ZPASS_SEPARATESTENCIL:
2010                 GL_CullFace(GL_NONE);
2011                 qglStencilOpSeparate(r_refdef.view.cullface_front, GL_KEEP, GL_KEEP, GL_INCR);CHECKGLERROR
2012                 qglStencilOpSeparate(r_refdef.view.cullface_back, GL_KEEP, GL_KEEP, GL_DECR);CHECKGLERROR
2013                 break;
2014         case R_SHADOW_RENDERMODE_ZFAIL_SEPARATESTENCIL:
2015                 GL_CullFace(GL_NONE);
2016                 qglStencilOpSeparate(r_refdef.view.cullface_front, GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR
2017                 qglStencilOpSeparate(r_refdef.view.cullface_back, GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
2018                 break;
2019         case R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE:
2020                 GL_CullFace(GL_NONE);
2021                 qglEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
2022                 qglActiveStencilFaceEXT(r_refdef.view.cullface_front);CHECKGLERROR
2023                 qglStencilMask(~0);CHECKGLERROR
2024                 qglStencilOp(GL_KEEP, GL_KEEP, GL_INCR);CHECKGLERROR
2025                 qglActiveStencilFaceEXT(r_refdef.view.cullface_back);CHECKGLERROR
2026                 qglStencilMask(~0);CHECKGLERROR
2027                 qglStencilOp(GL_KEEP, GL_KEEP, GL_DECR);CHECKGLERROR
2028                 break;
2029         case R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE:
2030                 GL_CullFace(GL_NONE);
2031                 qglEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
2032                 qglActiveStencilFaceEXT(r_refdef.view.cullface_front);CHECKGLERROR
2033                 qglStencilMask(~0);CHECKGLERROR
2034                 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR
2035                 qglActiveStencilFaceEXT(r_refdef.view.cullface_back);CHECKGLERROR
2036                 qglStencilMask(~0);CHECKGLERROR
2037                 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
2038                 break;
2039         }
2040 }
2041
2042 static void R_Shadow_MakeVSDCT(void)
2043 {
2044         // maps to a 2x3 texture rectangle with normalized coordinates
2045         // +-
2046         // XX
2047         // YY
2048         // ZZ
2049         // stores abs(dir.xy), offset.xy/2.5
2050         unsigned char data[4*6] =
2051         {
2052                 255, 0, 0x33, 0x33, // +X: <1, 0>, <0.5, 0.5>
2053                 255, 0, 0x99, 0x33, // -X: <1, 0>, <1.5, 0.5>
2054                 0, 255, 0x33, 0x99, // +Y: <0, 1>, <0.5, 1.5>
2055                 0, 255, 0x99, 0x99, // -Y: <0, 1>, <1.5, 1.5>
2056                 0,   0, 0x33, 0xFF, // +Z: <0, 0>, <0.5, 2.5>
2057                 0,   0, 0x99, 0xFF, // -Z: <0, 0>, <1.5, 2.5>
2058         };
2059         r_shadow_shadowmapvsdcttexture = R_LoadTextureCubeMap(r_shadow_texturepool, "shadowmapvsdct", 1, data, TEXTYPE_RGBA, TEXF_ALWAYSPRECACHE | TEXF_FORCENEAREST | TEXF_CLAMP | TEXF_ALPHA, NULL); 
2060 }
2061
2062 void R_Shadow_RenderMode_ShadowMap(int side, qboolean clear, int size)
2063 {
2064         int status;
2065         int maxsize;
2066         float nearclip, farclip, bias;
2067         r_viewport_t viewport;
2068         GLuint fbo = 0;
2069         CHECKGLERROR
2070         maxsize = r_shadow_shadowmapmaxsize;
2071         nearclip = r_shadow_shadowmapping_nearclip.value / rsurface.rtlight->radius;
2072         farclip = 1.0f;
2073         bias = r_shadow_shadowmapping_bias.value * nearclip * (1024.0f / size);// * rsurface.rtlight->radius;
2074         r_shadow_shadowmap_parameters[2] = 0.5f + 0.5f * (farclip + nearclip) / (farclip - nearclip);
2075         r_shadow_shadowmap_parameters[3] = -nearclip * farclip / (farclip - nearclip) - 0.5f * bias;
2076         r_shadow_shadowmapside = side;
2077         r_shadow_shadowmapsize = size;
2078         if (r_shadow_shadowmode == R_SHADOW_SHADOWMODE_SHADOWMAP2D)
2079         {
2080                 r_shadow_shadowmap_parameters[0] = 0.5f * (size - r_shadow_shadowmapborder);
2081                 r_shadow_shadowmap_parameters[1] = r_shadow_shadowmapvsdct ? 2.5f*size : size;
2082                 R_Viewport_InitRectSideView(&viewport, &rsurface.rtlight->matrix_lighttoworld, side, size, r_shadow_shadowmapborder, nearclip, farclip, NULL);
2083                 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAP2D) goto init_done;
2084
2085                 // complex unrolled cube approach (more flexible)
2086                 if (r_shadow_shadowmapvsdct && !r_shadow_shadowmapvsdcttexture)
2087                         R_Shadow_MakeVSDCT();
2088                 if (!r_shadow_shadowmap2dtexture)
2089                 {
2090 #if 1
2091                         int w = maxsize*2, h = vid.support.arb_texture_non_power_of_two ? maxsize*3 : maxsize*4;
2092                         r_shadow_shadowmap2dtexture = R_LoadTextureShadowMap2D(r_shadow_texturepool, "shadowmap", w, h, r_shadow_shadowmapdepthbits, r_shadow_shadowmapsampler);
2093                         qglGenFramebuffersEXT(1, &r_shadow_fbo2d);CHECKGLERROR
2094                         qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r_shadow_fbo2d);CHECKGLERROR
2095                         qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, R_GetTexture(r_shadow_shadowmap2dtexture), 0);CHECKGLERROR
2096             // render depth into the fbo, do not render color at all
2097                         qglDrawBuffer(GL_NONE);CHECKGLERROR
2098                         qglReadBuffer(GL_NONE);CHECKGLERROR
2099                         status = qglCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);CHECKGLERROR
2100                         if (status != GL_FRAMEBUFFER_COMPLETE_EXT && (r_shadow_shadowmapping.integer || r_shadow_deferred.integer))
2101                         {
2102                                 Con_Printf("R_Shadow_RenderMode_ShadowMap: glCheckFramebufferStatusEXT returned %i\n", status);
2103                                 Cvar_SetValueQuick(&r_shadow_shadowmapping, 0);
2104                                 Cvar_SetValueQuick(&r_shadow_deferred, 0);
2105                         }
2106 #endif
2107                 }
2108                 CHECKGLERROR
2109                 if (r_shadow_shadowmap2dtexture) fbo = r_shadow_fbo2d;
2110                 r_shadow_shadowmap_texturescale[0] = 1.0f / R_TextureWidth(r_shadow_shadowmap2dtexture);
2111                 r_shadow_shadowmap_texturescale[1] = 1.0f / R_TextureHeight(r_shadow_shadowmap2dtexture);
2112                 r_shadow_rendermode = R_SHADOW_RENDERMODE_SHADOWMAP2D;
2113         }
2114         else if (r_shadow_shadowmode == R_SHADOW_SHADOWMODE_SHADOWMAPRECTANGLE)
2115         {
2116                 r_shadow_shadowmap_parameters[0] = 0.5f * (size - r_shadow_shadowmapborder);
2117                 r_shadow_shadowmap_parameters[1] = r_shadow_shadowmapvsdct ? 2.5f*size : size;
2118                 R_Viewport_InitRectSideView(&viewport, &rsurface.rtlight->matrix_lighttoworld, side, size, r_shadow_shadowmapborder, nearclip, farclip, NULL);
2119                 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAPRECTANGLE) goto init_done;
2120
2121                 // complex unrolled cube approach (more flexible)
2122                 if (r_shadow_shadowmapvsdct && !r_shadow_shadowmapvsdcttexture)
2123                         R_Shadow_MakeVSDCT();
2124                 if (!r_shadow_shadowmaprectangletexture)
2125                 {
2126 #if 1
2127                         r_shadow_shadowmaprectangletexture = R_LoadTextureShadowMapRectangle(r_shadow_texturepool, "shadowmap", maxsize*2, maxsize*3, r_shadow_shadowmapdepthbits, r_shadow_shadowmapsampler);
2128                         qglGenFramebuffersEXT(1, &r_shadow_fborectangle);CHECKGLERROR
2129                         qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r_shadow_fborectangle);CHECKGLERROR
2130                         qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_RECTANGLE_ARB, R_GetTexture(r_shadow_shadowmaprectangletexture), 0);CHECKGLERROR
2131                         // render depth into the fbo, do not render color at all
2132                         qglDrawBuffer(GL_NONE);CHECKGLERROR
2133                         qglReadBuffer(GL_NONE);CHECKGLERROR
2134                         status = qglCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);CHECKGLERROR
2135                         if (status != GL_FRAMEBUFFER_COMPLETE_EXT && (r_shadow_shadowmapping.integer || r_shadow_deferred.integer))
2136                         {
2137                                 Con_Printf("R_Shadow_RenderMode_ShadowMap: glCheckFramebufferStatusEXT returned %i\n", status);
2138                                 Cvar_SetValueQuick(&r_shadow_shadowmapping, 0);
2139                                 Cvar_SetValueQuick(&r_shadow_deferred, 0);
2140                         }
2141 #endif
2142                 }
2143                 CHECKGLERROR
2144                 if(r_shadow_shadowmaprectangletexture) fbo = r_shadow_fborectangle;
2145                 r_shadow_shadowmap_texturescale[0] = 1.0f;
2146                 r_shadow_shadowmap_texturescale[1] = 1.0f;
2147                 r_shadow_rendermode = R_SHADOW_RENDERMODE_SHADOWMAPRECTANGLE;
2148         }
2149         else if (r_shadow_shadowmode == R_SHADOW_SHADOWMODE_SHADOWMAPCUBESIDE)
2150         {
2151                 r_shadow_shadowmap_parameters[0] = 1.0f;
2152                 r_shadow_shadowmap_parameters[1] = 1.0f;
2153                 R_Viewport_InitCubeSideView(&viewport, &rsurface.rtlight->matrix_lighttoworld, side, size, nearclip, farclip, NULL);
2154                 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAPCUBESIDE) goto init_done;
2155
2156                 // simple cube approach
2157                 if (!r_shadow_shadowmapcubetexture[r_shadow_shadowmaplod])
2158                 {
2159  #if 1
2160                         r_shadow_shadowmapcubetexture[r_shadow_shadowmaplod] = R_LoadTextureShadowMapCube(r_shadow_texturepool, "shadowmapcube", size, r_shadow_shadowmapdepthbits, r_shadow_shadowmapsampler);
2161                         qglGenFramebuffersEXT(1, &r_shadow_fbocubeside[r_shadow_shadowmaplod]);CHECKGLERROR
2162                         qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r_shadow_fbocubeside[r_shadow_shadowmaplod]);CHECKGLERROR
2163                         qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + side, R_GetTexture(r_shadow_shadowmapcubetexture[r_shadow_shadowmaplod]), 0);CHECKGLERROR
2164                         // render depth into the fbo, do not render color at all
2165                         qglDrawBuffer(GL_NONE);CHECKGLERROR
2166                         qglReadBuffer(GL_NONE);CHECKGLERROR
2167                         status = qglCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);CHECKGLERROR
2168                         if (status != GL_FRAMEBUFFER_COMPLETE_EXT && (r_shadow_shadowmapping.integer || r_shadow_deferred.integer))
2169                         {
2170                                 Con_Printf("R_Shadow_RenderMode_ShadowMap: glCheckFramebufferStatusEXT returned %i\n", status);
2171                                 Cvar_SetValueQuick(&r_shadow_shadowmapping, 0);
2172                                 Cvar_SetValueQuick(&r_shadow_deferred, 0);
2173                         }
2174  #endif
2175                 }
2176                 CHECKGLERROR
2177                 if (r_shadow_shadowmapcubetexture[r_shadow_shadowmaplod]) fbo = r_shadow_fbocubeside[r_shadow_shadowmaplod];
2178                 r_shadow_shadowmap_texturescale[0] = 0.0f;
2179                 r_shadow_shadowmap_texturescale[1] = 0.0f;
2180                 r_shadow_rendermode = R_SHADOW_RENDERMODE_SHADOWMAPCUBESIDE;
2181         }
2182
2183         R_Shadow_RenderMode_Reset();
2184         if (fbo)
2185         {
2186                 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);CHECKGLERROR
2187                 R_SetupDepthOrShadowShader();
2188         }
2189         else
2190         {
2191                 R_SetupShowDepthShader();
2192                 qglClearColor(1,1,1,1);CHECKGLERROR
2193         }
2194         CHECKGLERROR
2195         GL_PolygonOffset(r_shadow_shadowmapping_polygonfactor.value, r_shadow_shadowmapping_polygonoffset.value);
2196         GL_DepthMask(true);
2197         GL_DepthTest(true);
2198         qglClearDepth(1);
2199         CHECKGLERROR
2200
2201 init_done:
2202         R_SetViewport(&viewport);
2203         GL_Scissor(viewport.x, viewport.y, viewport.width, viewport.height);
2204         if(r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAP2D || r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAPRECTANGLE)
2205         {
2206                 int flipped = (side&1)^(side>>2);
2207                 r_refdef.view.cullface_front = flipped ? r_shadow_cullface_back : r_shadow_cullface_front;
2208                 r_refdef.view.cullface_back = flipped ? r_shadow_cullface_front : r_shadow_cullface_back;
2209                 GL_CullFace(r_refdef.view.cullface_back);
2210         }
2211         else if(r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAPCUBESIDE)
2212         {
2213                 qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + side, R_GetTexture(r_shadow_shadowmapcubetexture[r_shadow_shadowmaplod]), 0);CHECKGLERROR
2214         }
2215         if (clear)
2216                 qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT |  GL_STENCIL_BUFFER_BIT);
2217         CHECKGLERROR
2218 }
2219
2220 void R_Shadow_RenderMode_SetShadowMapTexture(void)
2221 {
2222         if (r_shadow_shadowmode == R_SHADOW_SHADOWMODE_SHADOWMAP2D)
2223         {
2224                 r_shadow_usingshadowmap2d = true;
2225                 R_Mesh_TexBind(GL20TU_SHADOWMAP2D, R_GetTexture(r_shadow_shadowmap2dtexture));
2226                 CHECKGLERROR
2227         }
2228         else if (r_shadow_shadowmode == R_SHADOW_SHADOWMODE_SHADOWMAPRECTANGLE)
2229         {
2230                 r_shadow_usingshadowmaprect = true;
2231                 R_Mesh_TexBindAll(GL20TU_SHADOWMAPRECT, 0, 0, 0, R_GetTexture(r_shadow_shadowmaprectangletexture));
2232                 CHECKGLERROR
2233         }
2234         else if (r_shadow_shadowmode == R_SHADOW_SHADOWMODE_SHADOWMAPCUBESIDE)
2235         {
2236                 r_shadow_usingshadowmapcube = true;
2237                 R_Mesh_TexBindAll(GL20TU_SHADOWMAPCUBE, 0, 0, R_GetTexture(r_shadow_shadowmapcubetexture[r_shadow_shadowmaplod]), 0);
2238                 CHECKGLERROR
2239         }
2240
2241         if (r_shadow_shadowmapvsdct && (r_shadow_usingshadowmap2d || r_shadow_usingshadowmaprect))
2242         {
2243                 R_Mesh_TexBindAll(GL20TU_CUBEPROJECTION, 0, 0, R_GetTexture(r_shadow_shadowmapvsdcttexture), 0);
2244                 CHECKGLERROR
2245         }
2246 }
2247
2248 void R_Shadow_RenderMode_Lighting(qboolean stenciltest, qboolean transparent, qboolean shadowmapping)
2249 {
2250         if (transparent)
2251         {
2252                 r_shadow_lightscissor[0] = r_refdef.view.viewport.x;
2253                 r_shadow_lightscissor[1] = r_refdef.view.viewport.y;
2254                 r_shadow_lightscissor[2] = r_refdef.view.viewport.width;
2255                 r_shadow_lightscissor[3] = r_refdef.view.viewport.height;
2256         }
2257         CHECKGLERROR
2258         R_Shadow_RenderMode_Reset();
2259         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2260         if (!transparent)
2261         {
2262                 qglDepthFunc(GL_EQUAL);CHECKGLERROR
2263         }
2264         if (stenciltest)
2265         {
2266                 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
2267                 // only draw light where this geometry was already rendered AND the
2268                 // stencil is 128 (values other than this mean shadow)
2269                 qglStencilFunc(GL_EQUAL, 128, ~0);CHECKGLERROR
2270         }
2271         r_shadow_rendermode = r_shadow_lightingrendermode;
2272         // do global setup needed for the chosen lighting mode
2273         if (r_shadow_rendermode == R_SHADOW_RENDERMODE_LIGHT_GLSL)
2274         {
2275                 R_Mesh_TexBindAll(GL20TU_CUBE, 0, 0, R_GetTexture(rsurface.rtlight->currentcubemap), 0); // light filter
2276                 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 0);
2277                 CHECKGLERROR
2278                 if (shadowmapping)
2279                         R_Shadow_RenderMode_SetShadowMapTexture();
2280         }
2281         R_Mesh_ColorPointer(rsurface.array_color4f, 0, 0);
2282         CHECKGLERROR
2283 }
2284
2285 static const unsigned short bboxelements[36] =
2286 {
2287         5, 1, 3, 5, 3, 7,
2288         6, 2, 0, 6, 0, 4,
2289         7, 3, 2, 7, 2, 6,
2290         4, 0, 1, 4, 1, 5,
2291         4, 5, 7, 4, 7, 6,
2292         1, 0, 2, 1, 2, 3,
2293 };
2294
2295 static const float bboxpoints[8][3] =
2296 {
2297         {-1,-1,-1},
2298         { 1,-1,-1},
2299         {-1, 1,-1},
2300         { 1, 1,-1},
2301         {-1,-1, 1},
2302         { 1,-1, 1},
2303         {-1, 1, 1},
2304         { 1, 1, 1},
2305 };
2306
2307 void R_Shadow_RenderMode_DrawDeferredLight(qboolean stenciltest, qboolean shadowmapping)
2308 {
2309         int i;
2310         float vertex3f[8*3];
2311         const matrix4x4_t *matrix = &rsurface.rtlight->matrix_lighttoworld;
2312         CHECKGLERROR
2313         R_Shadow_RenderMode_Reset();
2314         r_shadow_rendermode = r_shadow_lightingrendermode;
2315         // do global setup needed for the chosen lighting mode
2316         if (r_shadow_rendermode == R_SHADOW_RENDERMODE_LIGHT_GLSL)
2317         {
2318                 R_Mesh_Matrix(&identitymatrix);
2319                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2320                 if (stenciltest)
2321                 {
2322                         qglEnable(GL_STENCIL_TEST);CHECKGLERROR
2323                         // only draw light where this geometry was already rendered AND the
2324                         // stencil is 128 (values other than this mean shadow)
2325                         qglStencilFunc(GL_EQUAL, 128, ~0);CHECKGLERROR
2326                 }
2327                 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r_shadow_prepasslightingfbo);CHECKGLERROR
2328                 R_Mesh_TexBindAll(GL20TU_SCREENDEPTH, 0, 0, 0, R_GetTexture(r_shadow_prepassgeometrydepthtexture));
2329                 R_Mesh_TexBindAll(GL20TU_SCREENNORMALMAP, 0, 0, 0, R_GetTexture(r_shadow_prepassgeometrynormalmaptexture));
2330                 R_Mesh_TexBindAll(GL20TU_CUBE, 0, 0, R_GetTexture(rsurface.rtlight->currentcubemap), 0); // light filter
2331                 if (shadowmapping)
2332                         R_Shadow_RenderMode_SetShadowMapTexture();
2333                 R_SetupDeferredLightShader(rsurface.rtlight);
2334                 //R_Mesh_TexBind(GL20TU_FOGMASK, R_GetTexture(r_texture_fogattenuation));
2335                 R_Mesh_TexBind(GL20TU_ATTENUATION, R_GetTexture(r_shadow_attenuationgradienttexture));
2336
2337                 for (i = 0;i < 8;i++)
2338                         Matrix4x4_Transform(matrix, bboxpoints[i], vertex3f + i*3);
2339                 CHECKGLERROR
2340                 //qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);CHECKGLERROR
2341                 R_Mesh_VertexPointer(vertex3f, 0, 0);
2342                 R_Mesh_ColorPointer(NULL, 0, 0);
2343                 GL_ColorMask(1,1,1,1);
2344                 //GL_Color(0.25f,0.05f,0.02f,1.0f);
2345                 //R_SetupGenericShader(false);
2346                 GL_DepthMask(false);
2347                 GL_DepthRange(0, 1);
2348                 GL_PolygonOffset(0, 0);
2349                 GL_DepthTest(true);
2350                 qglDepthFunc(GL_GREATER);CHECKGLERROR
2351                 GL_CullFace(r_refdef.view.cullface_back);
2352                 //GL_AlphaTest(false);
2353                 //qglDisable(GL_STENCIL_TEST);CHECKGLERROR
2354                 R_Mesh_Draw(0, 8, 0, 12, NULL, bboxelements, 0, 0);
2355         }
2356 }
2357
2358 void R_Shadow_RenderMode_VisibleShadowVolumes(void)
2359 {
2360         CHECKGLERROR
2361         R_Shadow_RenderMode_Reset();
2362         GL_BlendFunc(GL_ONE, GL_ONE);
2363         GL_DepthRange(0, 1);
2364         GL_DepthTest(r_showshadowvolumes.integer < 2);
2365         GL_Color(0.0, 0.0125 * r_refdef.view.colorscale, 0.1 * r_refdef.view.colorscale, 1);
2366         GL_PolygonOffset(r_refdef.shadowpolygonfactor, r_refdef.shadowpolygonoffset);CHECKGLERROR
2367         GL_CullFace(GL_NONE);
2368         r_shadow_rendermode = R_SHADOW_RENDERMODE_VISIBLEVOLUMES;
2369 }
2370
2371 void R_Shadow_RenderMode_VisibleLighting(qboolean stenciltest, qboolean transparent)
2372 {
2373         CHECKGLERROR
2374         R_Shadow_RenderMode_Reset();
2375         GL_BlendFunc(GL_ONE, GL_ONE);
2376         GL_DepthRange(0, 1);
2377         GL_DepthTest(r_showlighting.integer < 2);
2378         GL_Color(0.1 * r_refdef.view.colorscale, 0.0125 * r_refdef.view.colorscale, 0, 1);
2379         if (!transparent)
2380         {
2381                 qglDepthFunc(GL_EQUAL);CHECKGLERROR
2382         }
2383         if (stenciltest)
2384         {
2385                 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
2386                 qglStencilFunc(GL_EQUAL, 128, ~0);CHECKGLERROR
2387         }
2388         r_shadow_rendermode = R_SHADOW_RENDERMODE_VISIBLELIGHTING;
2389 }
2390
2391 void R_Shadow_RenderMode_End(void)
2392 {
2393         CHECKGLERROR
2394         R_Shadow_RenderMode_Reset();
2395         R_Shadow_RenderMode_ActiveLight(NULL);
2396         GL_DepthMask(true);
2397         GL_Scissor(r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
2398         r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
2399 }
2400
2401 int bboxedges[12][2] =
2402 {
2403         // top
2404         {0, 1}, // +X
2405         {0, 2}, // +Y
2406         {1, 3}, // Y, +X
2407         {2, 3}, // X, +Y
2408         // bottom
2409         {4, 5}, // +X
2410         {4, 6}, // +Y
2411         {5, 7}, // Y, +X
2412         {6, 7}, // X, +Y
2413         // verticals
2414         {0, 4}, // +Z
2415         {1, 5}, // X, +Z
2416         {2, 6}, // Y, +Z
2417         {3, 7}, // XY, +Z
2418 };
2419
2420 qboolean R_Shadow_ScissorForBBox(const float *mins, const float *maxs)
2421 {
2422         int i, ix1, iy1, ix2, iy2;
2423         float x1, y1, x2, y2;
2424         vec4_t v, v2;
2425         float vertex[20][3];
2426         int j, k;
2427         vec4_t plane4f;
2428         int numvertices;
2429         float corner[8][4];
2430         float dist[8];
2431         int sign[8];
2432         float f;
2433
2434         r_shadow_lightscissor[0] = r_refdef.view.viewport.x;
2435         r_shadow_lightscissor[1] = r_refdef.view.viewport.y;
2436         r_shadow_lightscissor[2] = r_refdef.view.viewport.width;
2437         r_shadow_lightscissor[3] = r_refdef.view.viewport.height;
2438
2439         if (!r_shadow_scissor.integer)
2440                 return false;
2441
2442         // if view is inside the light box, just say yes it's visible
2443         if (BoxesOverlap(r_refdef.view.origin, r_refdef.view.origin, mins, maxs))
2444                 return false;
2445
2446         x1 = y1 = x2 = y2 = 0;
2447
2448         // transform all corners that are infront of the nearclip plane
2449         VectorNegate(r_refdef.view.frustum[4].normal, plane4f);
2450         plane4f[3] = r_refdef.view.frustum[4].dist;
2451         numvertices = 0;
2452         for (i = 0;i < 8;i++)
2453         {
2454                 Vector4Set(corner[i], (i & 1) ? maxs[0] : mins[0], (i & 2) ? maxs[1] : mins[1], (i & 4) ? maxs[2] : mins[2], 1);
2455                 dist[i] = DotProduct4(corner[i], plane4f);
2456                 sign[i] = dist[i] > 0;
2457                 if (!sign[i])
2458                 {
2459                         VectorCopy(corner[i], vertex[numvertices]);
2460                         numvertices++;
2461                 }
2462         }
2463         // if some points are behind the nearclip, add clipped edge points to make
2464         // sure that the scissor boundary is complete
2465         if (numvertices > 0 && numvertices < 8)
2466         {
2467                 // add clipped edge points
2468                 for (i = 0;i < 12;i++)
2469                 {
2470                         j = bboxedges[i][0];
2471                         k = bboxedges[i][1];
2472                         if (sign[j] != sign[k])
2473                         {
2474                                 f = dist[j] / (dist[j] - dist[k]);
2475                                 VectorLerp(corner[j], f, corner[k], vertex[numvertices]);
2476                                 numvertices++;
2477                         }
2478                 }
2479         }
2480
2481         // if we have no points to check, the light is behind the view plane
2482         if (!numvertices)
2483                 return true;
2484
2485         // if we have some points to transform, check what screen area is covered
2486         x1 = y1 = x2 = y2 = 0;
2487         v[3] = 1.0f;
2488         //Con_Printf("%i vertices to transform...\n", numvertices);
2489         for (i = 0;i < numvertices;i++)
2490         {
2491                 VectorCopy(vertex[i], v);
2492                 R_Viewport_TransformToScreen(&r_refdef.view.viewport, v, v2);
2493                 //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]);
2494                 if (i)
2495                 {
2496                         if (x1 > v2[0]) x1 = v2[0];
2497                         if (x2 < v2[0]) x2 = v2[0];
2498                         if (y1 > v2[1]) y1 = v2[1];
2499                         if (y2 < v2[1]) y2 = v2[1];
2500                 }
2501                 else
2502                 {
2503                         x1 = x2 = v2[0];
2504                         y1 = y2 = v2[1];
2505                 }
2506         }
2507
2508         // now convert the scissor rectangle to integer screen coordinates
2509         ix1 = (int)(x1 - 1.0f);
2510         iy1 = vid.height - (int)(y2 - 1.0f);
2511         ix2 = (int)(x2 + 1.0f);
2512         iy2 = vid.height - (int)(y1 + 1.0f);
2513         //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
2514
2515         // clamp it to the screen
2516         if (ix1 < r_refdef.view.viewport.x) ix1 = r_refdef.view.viewport.x;
2517         if (iy1 < r_refdef.view.viewport.y) iy1 = r_refdef.view.viewport.y;
2518         if (ix2 > r_refdef.view.viewport.x + r_refdef.view.viewport.width) ix2 = r_refdef.view.viewport.x + r_refdef.view.viewport.width;
2519         if (iy2 > r_refdef.view.viewport.y + r_refdef.view.viewport.height) iy2 = r_refdef.view.viewport.y + r_refdef.view.viewport.height;
2520
2521         // if it is inside out, it's not visible
2522         if (ix2 <= ix1 || iy2 <= iy1)
2523                 return true;
2524
2525         // the light area is visible, set up the scissor rectangle
2526         r_shadow_lightscissor[0] = ix1;
2527         r_shadow_lightscissor[1] = iy1;
2528         r_shadow_lightscissor[2] = ix2 - ix1;
2529         r_shadow_lightscissor[3] = iy2 - iy1;
2530
2531         r_refdef.stats.lights_scissored++;
2532         return false;
2533 }
2534
2535 static void R_Shadow_RenderLighting_Light_Vertex_Shading(int firstvertex, int numverts, int numtriangles, const int *element3i, const float *diffusecolor, const float *ambientcolor)
2536 {
2537         const float *vertex3f = rsurface.vertex3f + 3 * firstvertex;
2538         const float *normal3f = rsurface.normal3f + 3 * firstvertex;
2539         float *color4f = rsurface.array_color4f + 4 * firstvertex;
2540         float dist, dot, distintensity, shadeintensity, v[3], n[3];
2541         switch (r_shadow_rendermode)
2542         {
2543         case R_SHADOW_RENDERMODE_LIGHT_VERTEX3DATTEN:
2544         case R_SHADOW_RENDERMODE_LIGHT_VERTEX2D1DATTEN:
2545                 if (VectorLength2(diffusecolor) > 0)
2546                 {
2547                         for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
2548                         {
2549                                 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
2550                                 Matrix4x4_Transform3x3(&rsurface.entitytolight, normal3f, n);
2551                                 if ((dot = DotProduct(n, v)) < 0)
2552                                 {
2553                                         shadeintensity = -dot / sqrt(VectorLength2(v) * VectorLength2(n));
2554                                         VectorMA(ambientcolor, shadeintensity, diffusecolor, color4f);
2555                                 }
2556                                 else
2557                                         VectorCopy(ambientcolor, color4f);
2558                                 if (r_refdef.fogenabled)
2559                                 {
2560                                         float f;
2561                                         f = RSurf_FogVertex(vertex3f);
2562                                         VectorScale(color4f, f, color4f);
2563                                 }
2564                                 color4f[3] = 1;
2565                         }
2566                 }
2567                 else
2568                 {
2569                         for (;numverts > 0;numverts--, vertex3f += 3, color4f += 4)
2570                         {
2571                                 VectorCopy(ambientcolor, color4f);
2572                                 if (r_refdef.fogenabled)
2573                                 {
2574                                         float f;
2575                                         Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
2576                                         f = RSurf_FogVertex(vertex3f);
2577                                         VectorScale(color4f, f, color4f);
2578                                 }
2579                                 color4f[3] = 1;
2580                         }
2581                 }
2582                 break;
2583         case R_SHADOW_RENDERMODE_LIGHT_VERTEX2DATTEN:
2584                 if (VectorLength2(diffusecolor) > 0)
2585                 {
2586                         for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
2587                         {
2588                                 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
2589                                 if ((dist = fabs(v[2])) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
2590                                 {
2591                                         Matrix4x4_Transform3x3(&rsurface.entitytolight, normal3f, n);
2592                                         if ((dot = DotProduct(n, v)) < 0)
2593                                         {
2594                                                 shadeintensity = -dot / sqrt(VectorLength2(v) * VectorLength2(n));
2595                                                 color4f[0] = (ambientcolor[0] + shadeintensity * diffusecolor[0]) * distintensity;
2596                                                 color4f[1] = (ambientcolor[1] + shadeintensity * diffusecolor[1]) * distintensity;
2597                                                 color4f[2] = (ambientcolor[2] + shadeintensity * diffusecolor[2]) * distintensity;
2598                                         }
2599                                         else
2600                                         {
2601                                                 color4f[0] = ambientcolor[0] * distintensity;
2602                                                 color4f[1] = ambientcolor[1] * distintensity;
2603                                                 color4f[2] = ambientcolor[2] * distintensity;
2604                                         }
2605                                         if (r_refdef.fogenabled)
2606                                         {
2607                                                 float f;
2608                                                 f = RSurf_FogVertex(vertex3f);
2609                                                 VectorScale(color4f, f, color4f);
2610                                         }
2611                                 }
2612                                 else
2613                                         VectorClear(color4f);
2614                                 color4f[3] = 1;
2615                         }
2616                 }
2617                 else
2618                 {
2619                         for (;numverts > 0;numverts--, vertex3f += 3, color4f += 4)
2620                         {
2621                                 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
2622                                 if ((dist = fabs(v[2])) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
2623                                 {
2624                                         color4f[0] = ambientcolor[0] * distintensity;
2625                                         color4f[1] = ambientcolor[1] * distintensity;
2626                                         color4f[2] = ambientcolor[2] * distintensity;
2627                                         if (r_refdef.fogenabled)
2628                                         {
2629                                                 float f;
2630                                                 f = RSurf_FogVertex(vertex3f);
2631                                                 VectorScale(color4f, f, color4f);
2632                                         }
2633                                 }
2634                                 else
2635                                         VectorClear(color4f);
2636                                 color4f[3] = 1;
2637                         }
2638                 }
2639                 break;
2640         case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
2641                 if (VectorLength2(diffusecolor) > 0)
2642                 {
2643                         for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
2644                         {
2645                                 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
2646                                 if ((dist = VectorLength(v)) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
2647                                 {
2648                                         distintensity = (1 - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist);
2649                                         Matrix4x4_Transform3x3(&rsurface.entitytolight, normal3f, n);
2650                                         if ((dot = DotProduct(n, v)) < 0)
2651                                         {
2652                                                 shadeintensity = -dot / sqrt(VectorLength2(v) * VectorLength2(n));
2653                                                 color4f[0] = (ambientcolor[0] + shadeintensity * diffusecolor[0]) * distintensity;
2654                                                 color4f[1] = (ambientcolor[1] + shadeintensity * diffusecolor[1]) * distintensity;
2655                                                 color4f[2] = (ambientcolor[2] + shadeintensity * diffusecolor[2]) * distintensity;
2656                                         }
2657                                         else
2658                                         {
2659                                                 color4f[0] = ambientcolor[0] * distintensity;
2660                                                 color4f[1] = ambientcolor[1] * distintensity;
2661                                                 color4f[2] = ambientcolor[2] * distintensity;
2662                                         }
2663                                         if (r_refdef.fogenabled)
2664                                         {
2665                                                 float f;
2666                                                 f = RSurf_FogVertex(vertex3f);
2667                                                 VectorScale(color4f, f, color4f);
2668                                         }
2669                                 }
2670                                 else
2671                                         VectorClear(color4f);
2672                                 color4f[3] = 1;
2673                         }
2674                 }
2675                 else
2676                 {
2677                         for (;numverts > 0;numverts--, vertex3f += 3, color4f += 4)
2678                         {
2679                                 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
2680                                 if ((dist = VectorLength(v)) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
2681                                 {
2682                                         distintensity = (1 - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist);
2683                                         color4f[0] = ambientcolor[0] * distintensity;
2684                                         color4f[1] = ambientcolor[1] * distintensity;
2685                                         color4f[2] = ambientcolor[2] * distintensity;
2686                                         if (r_refdef.fogenabled)
2687                                         {
2688                                                 float f;
2689                                                 f = RSurf_FogVertex(vertex3f);
2690                                                 VectorScale(color4f, f, color4f);
2691                                         }
2692                                 }
2693                                 else
2694                                         VectorClear(color4f);
2695                                 color4f[3] = 1;
2696                         }
2697                 }
2698                 break;
2699         default:
2700                 break;
2701         }
2702 }
2703
2704 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)
2705 {
2706         // used to display how many times a surface is lit for level design purposes
2707         R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2708 }
2709
2710 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 lightcolor, float ambientscale, float diffusescale, float specularscale)
2711 {
2712         // ARB2 GLSL shader path (GFFX5200, Radeon 9500)
2713         R_SetupSurfaceShader(lightcolor, false, ambientscale, diffusescale, specularscale, RSURFPASS_RTLIGHT);
2714         if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND))
2715                 R_Mesh_ColorPointer(rsurface.modellightmapcolor4f, rsurface.modellightmapcolor4f_bufferobject, rsurface.modellightmapcolor4f_bufferoffset);
2716         else
2717                 R_Mesh_ColorPointer(NULL, 0, 0);
2718         R_Mesh_TexMatrix(0, &rsurface.texture->currenttexmatrix);
2719         R_Mesh_TexMatrix(1, &rsurface.texture->currentbackgroundtexmatrix);
2720         R_Mesh_TexBind(GL20TU_NORMAL, R_GetTexture(rsurface.texture->currentskinframe->nmap));
2721         R_Mesh_TexBind(GL20TU_COLOR, R_GetTexture(rsurface.texture->basetexture));
2722         R_Mesh_TexBind(GL20TU_GLOSS, R_GetTexture(rsurface.texture->glosstexture));
2723         if (rsurface.texture->backgroundcurrentskinframe)
2724         {
2725                 R_Mesh_TexBind(GL20TU_SECONDARY_NORMAL, R_GetTexture(rsurface.texture->backgroundcurrentskinframe->nmap));
2726                 R_Mesh_TexBind(GL20TU_SECONDARY_COLOR, R_GetTexture(rsurface.texture->backgroundbasetexture));
2727                 R_Mesh_TexBind(GL20TU_SECONDARY_GLOSS, R_GetTexture(rsurface.texture->backgroundglosstexture));
2728                 R_Mesh_TexBind(GL20TU_SECONDARY_GLOW, R_GetTexture(rsurface.texture->backgroundcurrentskinframe->glow));
2729         }
2730         //R_Mesh_TexBindAll(GL20TU_CUBE, 0, 0, R_GetTexture(rsurface.rtlight->currentcubemap), 0);
2731         R_Mesh_TexBind(GL20TU_FOGMASK, R_GetTexture(r_texture_fogattenuation));
2732         if(rsurface.texture->colormapping)
2733         {
2734                 R_Mesh_TexBind(GL20TU_PANTS, R_GetTexture(rsurface.texture->currentskinframe->pants));
2735                 R_Mesh_TexBind(GL20TU_SHIRT, R_GetTexture(rsurface.texture->currentskinframe->shirt));
2736         }
2737         R_Mesh_TexBind(GL20TU_ATTENUATION, R_GetTexture(r_shadow_attenuationgradienttexture));
2738         R_Mesh_TexCoordPointer(0, 2, rsurface.texcoordtexture2f, rsurface.texcoordtexture2f_bufferobject, rsurface.texcoordtexture2f_bufferoffset);
2739         R_Mesh_TexCoordPointer(1, 3, rsurface.svector3f, rsurface.svector3f_bufferobject, rsurface.svector3f_bufferoffset);
2740         R_Mesh_TexCoordPointer(2, 3, rsurface.tvector3f, rsurface.tvector3f_bufferobject, rsurface.tvector3f_bufferoffset);
2741         R_Mesh_TexCoordPointer(3, 3, rsurface.normal3f, rsurface.normal3f_bufferobject, rsurface.normal3f_bufferoffset);
2742         if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
2743         {
2744                 qglDepthFunc(GL_EQUAL);CHECKGLERROR
2745         }
2746         R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2747         if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
2748         {
2749                 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
2750         }
2751 }
2752
2753 static void R_Shadow_RenderLighting_Light_Vertex_Pass(int firstvertex, int numvertices, int numtriangles, const int *element3i, vec3_t diffusecolor2, vec3_t ambientcolor2)
2754 {
2755         int renders;
2756         int i;
2757         int stop;
2758         int newfirstvertex;
2759         int newlastvertex;
2760         int newnumtriangles;
2761         int *newe;
2762         const int *e;
2763         float *c;
2764         int maxtriangles = 4096;
2765         static int newelements[4096*3];
2766         R_Shadow_RenderLighting_Light_Vertex_Shading(firstvertex, numvertices, numtriangles, element3i, diffusecolor2, ambientcolor2);
2767         for (renders = 0;renders < 64;renders++)
2768         {
2769                 stop = true;
2770                 newfirstvertex = 0;
2771                 newlastvertex = 0;
2772                 newnumtriangles = 0;
2773                 newe = newelements;
2774                 // due to low fillrate on the cards this vertex lighting path is
2775                 // designed for, we manually cull all triangles that do not
2776                 // contain a lit vertex
2777                 // this builds batches of triangles from multiple surfaces and
2778                 // renders them at once
2779                 for (i = 0, e = element3i;i < numtriangles;i++, e += 3)
2780                 {
2781                         if (VectorLength2(rsurface.array_color4f + e[0] * 4) + VectorLength2(rsurface.array_color4f + e[1] * 4) + VectorLength2(rsurface.array_color4f + e[2] * 4) >= 0.01)
2782                         {
2783                                 if (newnumtriangles)
2784                                 {
2785                                         newfirstvertex = min(newfirstvertex, e[0]);
2786                                         newlastvertex  = max(newlastvertex, e[0]);
2787                                 }
2788                                 else
2789                                 {
2790                                         newfirstvertex = e[0];
2791                                         newlastvertex = e[0];
2792                                 }
2793                                 newfirstvertex = min(newfirstvertex, e[1]);
2794                                 newlastvertex  = max(newlastvertex, e[1]);
2795                                 newfirstvertex = min(newfirstvertex, e[2]);
2796                                 newlastvertex  = max(newlastvertex, e[2]);
2797                                 newe[0] = e[0];
2798                                 newe[1] = e[1];
2799                                 newe[2] = e[2];
2800                                 newnumtriangles++;
2801                                 newe += 3;
2802                                 if (newnumtriangles >= maxtriangles)
2803                                 {
2804                                         R_Mesh_Draw(newfirstvertex, newlastvertex - newfirstvertex + 1, 0, newnumtriangles, newelements, NULL, 0, 0);
2805                                         newnumtriangles = 0;
2806                                         newe = newelements;
2807                                         stop = false;
2808                                 }
2809                         }
2810                 }
2811                 if (newnumtriangles >= 1)
2812                 {
2813                         R_Mesh_Draw(newfirstvertex, newlastvertex - newfirstvertex + 1, 0, newnumtriangles, newelements, NULL, 0, 0);
2814                         stop = false;
2815                 }
2816                 // if we couldn't find any lit triangles, exit early
2817                 if (stop)
2818                         break;
2819                 // now reduce the intensity for the next overbright pass
2820                 // we have to clamp to 0 here incase the drivers have improper
2821                 // handling of negative colors
2822                 // (some old drivers even have improper handling of >1 color)
2823                 stop = true;
2824                 for (i = 0, c = rsurface.array_color4f + 4 * firstvertex;i < numvertices;i++, c += 4)
2825                 {
2826                         if (c[0] > 1 || c[1] > 1 || c[2] > 1)
2827                         {
2828                                 c[0] = max(0, c[0] - 1);
2829                                 c[1] = max(0, c[1] - 1);
2830                                 c[2] = max(0, c[2] - 1);
2831                                 stop = false;
2832                         }
2833                         else
2834                                 VectorClear(c);
2835                 }
2836                 // another check...
2837                 if (stop)
2838                         break;
2839         }
2840 }
2841
2842 static void R_Shadow_RenderLighting_Light_Vertex(int firstvertex, int numvertices, int numtriangles, const int *element3i, const vec3_t lightcolor, float ambientscale, float diffusescale)
2843 {
2844         // OpenGL 1.1 path (anything)
2845         float ambientcolorbase[3], diffusecolorbase[3];
2846         float ambientcolorpants[3], diffusecolorpants[3];
2847         float ambientcolorshirt[3], diffusecolorshirt[3];
2848         const float *surfacecolor = rsurface.texture->dlightcolor;
2849         const float *surfacepants = rsurface.colormap_pantscolor;
2850         const float *surfaceshirt = rsurface.colormap_shirtcolor;
2851         rtexture_t *basetexture = rsurface.texture->basetexture;
2852         rtexture_t *pantstexture = rsurface.texture->currentskinframe->pants;
2853         rtexture_t *shirttexture = rsurface.texture->currentskinframe->shirt;
2854         qboolean dopants = pantstexture && VectorLength2(surfacepants) >= (1.0f / 1048576.0f);
2855         qboolean doshirt = shirttexture && VectorLength2(surfaceshirt) >= (1.0f / 1048576.0f);
2856         ambientscale *= 2 * r_refdef.view.colorscale;
2857         diffusescale *= 2 * r_refdef.view.colorscale;
2858         ambientcolorbase[0] = lightcolor[0] * ambientscale * surfacecolor[0];ambientcolorbase[1] = lightcolor[1] * ambientscale * surfacecolor[1];ambientcolorbase[2] = lightcolor[2] * ambientscale * surfacecolor[2];
2859         diffusecolorbase[0] = lightcolor[0] * diffusescale * surfacecolor[0];diffusecolorbase[1] = lightcolor[1] * diffusescale * surfacecolor[1];diffusecolorbase[2] = lightcolor[2] * diffusescale * surfacecolor[2];
2860         ambientcolorpants[0] = ambientcolorbase[0] * surfacepants[0];ambientcolorpants[1] = ambientcolorbase[1] * surfacepants[1];ambientcolorpants[2] = ambientcolorbase[2] * surfacepants[2];
2861         diffusecolorpants[0] = diffusecolorbase[0] * surfacepants[0];diffusecolorpants[1] = diffusecolorbase[1] * surfacepants[1];diffusecolorpants[2] = diffusecolorbase[2] * surfacepants[2];
2862         ambientcolorshirt[0] = ambientcolorbase[0] * surfaceshirt[0];ambientcolorshirt[1] = ambientcolorbase[1] * surfaceshirt[1];ambientcolorshirt[2] = ambientcolorbase[2] * surfaceshirt[2];
2863         diffusecolorshirt[0] = diffusecolorbase[0] * surfaceshirt[0];diffusecolorshirt[1] = diffusecolorbase[1] * surfaceshirt[1];diffusecolorshirt[2] = diffusecolorbase[2] * surfaceshirt[2];
2864         R_Mesh_TexBind(0, R_GetTexture(basetexture));
2865         R_Mesh_TexMatrix(0, &rsurface.texture->currenttexmatrix);
2866         R_Mesh_TexCombine(0, GL_MODULATE, GL_MODULATE, 1, 1);
2867         R_Mesh_TexCoordPointer(0, 2, rsurface.texcoordtexture2f, rsurface.texcoordtexture2f_bufferobject, rsurface.texcoordtexture2f_bufferoffset);
2868         switch(r_shadow_rendermode)
2869         {
2870         case R_SHADOW_RENDERMODE_LIGHT_VERTEX3DATTEN:
2871                 R_Mesh_TexBindAll(1, 0, R_GetTexture(r_shadow_attenuation3dtexture), 0, 0);
2872                 R_Mesh_TexMatrix(1, &rsurface.entitytoattenuationxyz);
2873                 R_Mesh_TexCombine(1, GL_MODULATE, GL_MODULATE, 1, 1);
2874                 R_Mesh_TexCoordPointer(1, 3, rsurface.vertex3f, rsurface.vertex3f_bufferobject, rsurface.vertex3f_bufferoffset);
2875                 break;
2876         case R_SHADOW_RENDERMODE_LIGHT_VERTEX2D1DATTEN:
2877                 R_Mesh_TexBind(2, R_GetTexture(r_shadow_attenuation2dtexture));
2878                 R_Mesh_TexMatrix(2, &rsurface.entitytoattenuationz);
2879                 R_Mesh_TexCombine(2, GL_MODULATE, GL_MODULATE, 1, 1);
2880                 R_Mesh_TexCoordPointer(2, 3, rsurface.vertex3f, rsurface.vertex3f_bufferobject, rsurface.vertex3f_bufferoffset);
2881                 // fall through
2882         case R_SHADOW_RENDERMODE_LIGHT_VERTEX2DATTEN:
2883                 R_Mesh_TexBind(1, R_GetTexture(r_shadow_attenuation2dtexture));
2884                 R_Mesh_TexMatrix(1, &rsurface.entitytoattenuationxyz);
2885                 R_Mesh_TexCombine(1, GL_MODULATE, GL_MODULATE, 1, 1);
2886                 R_Mesh_TexCoordPointer(1, 3, rsurface.vertex3f, rsurface.vertex3f_bufferobject, rsurface.vertex3f_bufferoffset);
2887                 break;
2888         case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
2889                 break;
2890         default:
2891                 break;
2892         }
2893         //R_Mesh_TexBind(0, R_GetTexture(basetexture));
2894         R_Shadow_RenderLighting_Light_Vertex_Pass(firstvertex, numvertices, numtriangles, element3i, diffusecolorbase, ambientcolorbase);
2895         if (dopants)
2896         {
2897                 R_Mesh_TexBind(0, R_GetTexture(pantstexture));
2898                 R_Shadow_RenderLighting_Light_Vertex_Pass(firstvertex, numvertices, numtriangles, element3i, diffusecolorpants, ambientcolorpants);
2899         }
2900         if (doshirt)
2901         {
2902                 R_Mesh_TexBind(0, R_GetTexture(shirttexture));
2903                 R_Shadow_RenderLighting_Light_Vertex_Pass(firstvertex, numvertices, numtriangles, element3i, diffusecolorshirt, ambientcolorshirt);
2904         }
2905 }
2906
2907 extern cvar_t gl_lightmaps;
2908 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)
2909 {
2910         float ambientscale, diffusescale, specularscale;
2911         qboolean negated;
2912         float lightcolor[3];
2913         VectorCopy(rsurface.rtlight->currentcolor, lightcolor);
2914         ambientscale = rsurface.rtlight->ambientscale;
2915         diffusescale = rsurface.rtlight->diffusescale;
2916         specularscale = rsurface.rtlight->specularscale * rsurface.texture->specularscale;
2917         if (!r_shadow_usenormalmap.integer)
2918         {
2919                 ambientscale += 1.0f * diffusescale;
2920                 diffusescale = 0;
2921                 specularscale = 0;
2922         }
2923         if ((ambientscale + diffusescale) * VectorLength2(lightcolor) + specularscale * VectorLength2(lightcolor) < (1.0f / 1048576.0f))
2924                 return;
2925         negated = (lightcolor[0] + lightcolor[1] + lightcolor[2] < 0) && vid.support.ext_blend_subtract;
2926         if(negated)
2927         {
2928                 VectorNegate(lightcolor, lightcolor);
2929                 qglBlendEquationEXT(GL_FUNC_REVERSE_SUBTRACT_EXT);
2930         }
2931         RSurf_SetupDepthAndCulling();
2932         switch (r_shadow_rendermode)
2933         {
2934         case R_SHADOW_RENDERMODE_VISIBLELIGHTING:
2935                 GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST) && !r_showdisabledepthtest.integer);
2936                 R_Shadow_RenderLighting_VisibleLighting(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2937                 break;
2938         case R_SHADOW_RENDERMODE_LIGHT_GLSL:
2939                 R_Shadow_RenderLighting_Light_GLSL(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolor, ambientscale, diffusescale, specularscale);
2940                 break;
2941         case R_SHADOW_RENDERMODE_LIGHT_VERTEX3DATTEN:
2942         case R_SHADOW_RENDERMODE_LIGHT_VERTEX2D1DATTEN:
2943         case R_SHADOW_RENDERMODE_LIGHT_VERTEX2DATTEN:
2944         case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
2945                 R_Shadow_RenderLighting_Light_Vertex(firstvertex, numvertices, numtriangles, element3i + firsttriangle * 3, lightcolor, ambientscale, diffusescale);
2946                 break;
2947         default:
2948                 Con_Printf("R_Shadow_RenderLighting: unknown r_shadow_rendermode %i\n", r_shadow_rendermode);
2949                 break;
2950         }
2951         if(negated)
2952                 qglBlendEquationEXT(GL_FUNC_ADD_EXT);
2953 }
2954
2955 void R_RTLight_Update(rtlight_t *rtlight, int isstatic, matrix4x4_t *matrix, vec3_t color, int style, const char *cubemapname, int shadow, vec_t corona, vec_t coronasizescale, vec_t ambientscale, vec_t diffusescale, vec_t specularscale, int flags)
2956 {
2957         matrix4x4_t tempmatrix = *matrix;
2958         Matrix4x4_Scale(&tempmatrix, r_shadow_lightradiusscale.value, 1);
2959
2960         // if this light has been compiled before, free the associated data
2961         R_RTLight_Uncompile(rtlight);
2962
2963         // clear it completely to avoid any lingering data
2964         memset(rtlight, 0, sizeof(*rtlight));
2965
2966         // copy the properties
2967         rtlight->matrix_lighttoworld = tempmatrix;
2968         Matrix4x4_Invert_Simple(&rtlight->matrix_worldtolight, &tempmatrix);
2969         Matrix4x4_OriginFromMatrix(&tempmatrix, rtlight->shadoworigin);
2970         rtlight->radius = Matrix4x4_ScaleFromMatrix(&tempmatrix);
2971         VectorCopy(color, rtlight->color);
2972         rtlight->cubemapname[0] = 0;
2973         if (cubemapname && cubemapname[0])
2974                 strlcpy(rtlight->cubemapname, cubemapname, sizeof(rtlight->cubemapname));
2975         rtlight->shadow = shadow;
2976         rtlight->corona = corona;
2977         rtlight->style = style;
2978         rtlight->isstatic = isstatic;
2979         rtlight->coronasizescale = coronasizescale;
2980         rtlight->ambientscale = ambientscale;
2981         rtlight->diffusescale = diffusescale;
2982         rtlight->specularscale = specularscale;
2983         rtlight->flags = flags;
2984
2985         // compute derived data
2986         //rtlight->cullradius = rtlight->radius;
2987         //rtlight->cullradius2 = rtlight->radius * rtlight->radius;
2988         rtlight->cullmins[0] = rtlight->shadoworigin[0] - rtlight->radius;
2989         rtlight->cullmins[1] = rtlight->shadoworigin[1] - rtlight->radius;
2990         rtlight->cullmins[2] = rtlight->shadoworigin[2] - rtlight->radius;
2991         rtlight->cullmaxs[0] = rtlight->shadoworigin[0] + rtlight->radius;
2992         rtlight->cullmaxs[1] = rtlight->shadoworigin[1] + rtlight->radius;
2993         rtlight->cullmaxs[2] = rtlight->shadoworigin[2] + rtlight->radius;
2994 }
2995
2996 // compiles rtlight geometry
2997 // (undone by R_FreeCompiledRTLight, which R_UpdateLight calls)
2998 void R_RTLight_Compile(rtlight_t *rtlight)
2999 {
3000         int i;
3001         int numsurfaces, numleafs, numleafpvsbytes, numshadowtrispvsbytes, numlighttrispvsbytes;
3002         int lighttris, shadowtris, shadowzpasstris, shadowzfailtris;
3003         entity_render_t *ent = r_refdef.scene.worldentity;
3004         dp_model_t *model = r_refdef.scene.worldmodel;
3005         unsigned char *data;
3006         shadowmesh_t *mesh;
3007
3008         // compile the light
3009         rtlight->compiled = true;
3010         rtlight->shadowmode = rtlight->shadow ? (int)r_shadow_shadowmode : -1;
3011         rtlight->static_numleafs = 0;
3012         rtlight->static_numleafpvsbytes = 0;
3013         rtlight->static_leaflist = NULL;
3014         rtlight->static_leafpvs = NULL;
3015         rtlight->static_numsurfaces = 0;
3016         rtlight->static_surfacelist = NULL;
3017         rtlight->static_shadowmap_receivers = 0x3F;
3018         rtlight->static_shadowmap_casters = 0x3F;
3019         rtlight->cullmins[0] = rtlight->shadoworigin[0] - rtlight->radius;
3020         rtlight->cullmins[1] = rtlight->shadoworigin[1] - rtlight->radius;
3021         rtlight->cullmins[2] = rtlight->shadoworigin[2] - rtlight->radius;
3022         rtlight->cullmaxs[0] = rtlight->shadoworigin[0] + rtlight->radius;
3023         rtlight->cullmaxs[1] = rtlight->shadoworigin[1] + rtlight->radius;
3024         rtlight->cullmaxs[2] = rtlight->shadoworigin[2] + rtlight->radius;
3025
3026         if (model && model->GetLightInfo)
3027         {
3028                 // this variable must be set for the CompileShadowVolume/CompileShadowMap code
3029                 r_shadow_compilingrtlight = rtlight;
3030                 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, 0, NULL);
3031                 numleafpvsbytes = (model->brush.num_leafs + 7) >> 3;
3032                 numshadowtrispvsbytes = ((model->brush.shadowmesh ? model->brush.shadowmesh->numtriangles : model->surfmesh.num_triangles) + 7) >> 3;
3033                 numlighttrispvsbytes = (model->surfmesh.num_triangles + 7) >> 3;
3034                 data = (unsigned char *)Mem_Alloc(r_main_mempool, sizeof(int) * numsurfaces + sizeof(int) * numleafs + numleafpvsbytes + numshadowtrispvsbytes + numlighttrispvsbytes);
3035                 rtlight->static_numsurfaces = numsurfaces;
3036                 rtlight->static_surfacelist = (int *)data;data += sizeof(int) * numsurfaces;
3037                 rtlight->static_numleafs = numleafs;
3038                 rtlight->static_leaflist = (int *)data;data += sizeof(int) * numleafs;
3039                 rtlight->static_numleafpvsbytes = numleafpvsbytes;
3040                 rtlight->static_leafpvs = (unsigned char *)data;data += numleafpvsbytes;
3041                 rtlight->static_numshadowtrispvsbytes = numshadowtrispvsbytes;
3042                 rtlight->static_shadowtrispvs = (unsigned char *)data;data += numshadowtrispvsbytes;
3043                 rtlight->static_numlighttrispvsbytes = numlighttrispvsbytes;
3044                 rtlight->static_lighttrispvs = (unsigned char *)data;data += numlighttrispvsbytes;
3045                 if (rtlight->static_numsurfaces)
3046                         memcpy(rtlight->static_surfacelist, r_shadow_buffer_surfacelist, rtlight->static_numsurfaces * sizeof(*rtlight->static_surfacelist));
3047                 if (rtlight->static_numleafs)
3048                         memcpy(rtlight->static_leaflist, r_shadow_buffer_leaflist, rtlight->static_numleafs * sizeof(*rtlight->static_leaflist));
3049                 if (rtlight->static_numleafpvsbytes)
3050                         memcpy(rtlight->static_leafpvs, r_shadow_buffer_leafpvs, rtlight->static_numleafpvsbytes);
3051                 if (rtlight->static_numshadowtrispvsbytes)
3052                         memcpy(rtlight->static_shadowtrispvs, r_shadow_buffer_shadowtrispvs, rtlight->static_numshadowtrispvsbytes);
3053                 if (rtlight->static_numlighttrispvsbytes)
3054                         memcpy(rtlight->static_lighttrispvs, r_shadow_buffer_lighttrispvs, rtlight->static_numlighttrispvsbytes);
3055                 switch (rtlight->shadowmode)
3056                 {
3057                 case R_SHADOW_SHADOWMODE_SHADOWMAP2D:
3058                 case R_SHADOW_SHADOWMODE_SHADOWMAPRECTANGLE:
3059                 case R_SHADOW_SHADOWMODE_SHADOWMAPCUBESIDE:
3060                         if (model->CompileShadowMap && rtlight->shadow)
3061                                 model->CompileShadowMap(ent, rtlight->shadoworigin, NULL, rtlight->radius, numsurfaces, r_shadow_buffer_surfacelist);
3062                         break;
3063                 default:
3064                         if (model->CompileShadowVolume && rtlight->shadow)
3065                                 model->CompileShadowVolume(ent, rtlight->shadoworigin, NULL, rtlight->radius, numsurfaces, r_shadow_buffer_surfacelist);
3066                         break;
3067                 }
3068                 // now we're done compiling the rtlight
3069                 r_shadow_compilingrtlight = NULL;
3070         }
3071
3072
3073         // use smallest available cullradius - box radius or light radius
3074         //rtlight->cullradius = RadiusFromBoundsAndOrigin(rtlight->cullmins, rtlight->cullmaxs, rtlight->shadoworigin);
3075         //rtlight->cullradius = min(rtlight->cullradius, rtlight->radius);
3076
3077         shadowzpasstris = 0;
3078         if (rtlight->static_meshchain_shadow_zpass)
3079                 for (mesh = rtlight->static_meshchain_shadow_zpass;mesh;mesh = mesh->next)
3080                         shadowzpasstris += mesh->numtriangles;
3081
3082         shadowzfailtris = 0;
3083         if (rtlight->static_meshchain_shadow_zfail)
3084                 for (mesh = rtlight->static_meshchain_shadow_zfail;mesh;mesh = mesh->next)
3085                         shadowzfailtris += mesh->numtriangles;
3086
3087         lighttris = 0;
3088         if (rtlight->static_numlighttrispvsbytes)
3089                 for (i = 0;i < rtlight->static_numlighttrispvsbytes*8;i++)
3090                         if (CHECKPVSBIT(rtlight->static_lighttrispvs, i))
3091                                 lighttris++;
3092
3093         shadowtris = 0;
3094         if (rtlight->static_numlighttrispvsbytes)
3095                 for (i = 0;i < rtlight->static_numshadowtrispvsbytes*8;i++)
3096                         if (CHECKPVSBIT(rtlight->static_shadowtrispvs, i))
3097                                 shadowtris++;
3098
3099         if (developer.integer >= 10)
3100                 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);
3101 }
3102
3103 void R_RTLight_Uncompile(rtlight_t *rtlight)
3104 {
3105         if (rtlight->compiled)
3106         {
3107                 if (rtlight->static_meshchain_shadow_zpass)
3108                         Mod_ShadowMesh_Free(rtlight->static_meshchain_shadow_zpass);
3109                 rtlight->static_meshchain_shadow_zpass = NULL;
3110                 if (rtlight->static_meshchain_shadow_zfail)
3111                         Mod_ShadowMesh_Free(rtlight->static_meshchain_shadow_zfail);
3112                 rtlight->static_meshchain_shadow_zfail = NULL;
3113                 if (rtlight->static_meshchain_shadow_shadowmap)
3114                         Mod_ShadowMesh_Free(rtlight->static_meshchain_shadow_shadowmap);
3115                 rtlight->static_meshchain_shadow_shadowmap = NULL;
3116                 // these allocations are grouped
3117                 if (rtlight->static_surfacelist)
3118                         Mem_Free(rtlight->static_surfacelist);
3119                 rtlight->static_numleafs = 0;
3120                 rtlight->static_numleafpvsbytes = 0;
3121                 rtlight->static_leaflist = NULL;
3122                 rtlight->static_leafpvs = NULL;
3123                 rtlight->static_numsurfaces = 0;
3124                 rtlight->static_surfacelist = NULL;
3125                 rtlight->static_numshadowtrispvsbytes = 0;
3126                 rtlight->static_shadowtrispvs = NULL;
3127                 rtlight->static_numlighttrispvsbytes = 0;
3128                 rtlight->static_lighttrispvs = NULL;
3129                 rtlight->compiled = false;
3130         }
3131 }
3132
3133 void R_Shadow_UncompileWorldLights(void)
3134 {
3135         size_t lightindex;
3136         dlight_t *light;
3137         size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
3138         for (lightindex = 0;lightindex < range;lightindex++)
3139         {
3140                 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
3141                 if (!light)
3142                         continue;
3143                 R_RTLight_Uncompile(&light->rtlight);
3144         }
3145 }
3146
3147 void R_Shadow_ComputeShadowCasterCullingPlanes(rtlight_t *rtlight)
3148 {
3149         int i, j;
3150         mplane_t plane;
3151         // reset the count of frustum planes
3152         // see rtlight->cached_frustumplanes definition for how much this array
3153         // can hold
3154         rtlight->cached_numfrustumplanes = 0;
3155
3156         // haven't implemented a culling path for ortho rendering
3157         if (!r_refdef.view.useperspective)
3158         {
3159                 // check if the light is on screen and copy the 4 planes if it is
3160                 for (i = 0;i < 4;i++)
3161                         if (PlaneDiff(rtlight->shadoworigin, &r_refdef.view.frustum[i]) < -0.03125)
3162                                 break;
3163                 if (i == 4)
3164                         for (i = 0;i < 4;i++)
3165                                 rtlight->cached_frustumplanes[rtlight->cached_numfrustumplanes++] = r_refdef.view.frustum[i];
3166                 return;
3167         }
3168
3169 #if 1
3170         // generate a deformed frustum that includes the light origin, this is
3171         // used to cull shadow casting surfaces that can not possibly cast a
3172         // shadow onto the visible light-receiving surfaces, which can be a
3173         // performance gain
3174         //
3175         // if the light origin is onscreen the result will be 4 planes exactly
3176         // if the light origin is offscreen on only one axis the result will
3177         // be exactly 5 planes (split-side case)
3178         // if the light origin is offscreen on two axes the result will be
3179         // exactly 4 planes (stretched corner case)
3180         for (i = 0;i < 4;i++)
3181         {
3182                 // quickly reject standard frustum planes that put the light
3183                 // origin outside the frustum
3184                 if (PlaneDiff(rtlight->shadoworigin, &r_refdef.view.frustum[i]) < -0.03125)
3185                         continue;
3186                 // copy the plane
3187                 rtlight->cached_frustumplanes[rtlight->cached_numfrustumplanes++] = r_refdef.view.frustum[i];
3188         }
3189         // if all the standard frustum planes were accepted, the light is onscreen
3190         // otherwise we need to generate some more planes below...
3191         if (rtlight->cached_numfrustumplanes < 4)
3192         {
3193                 // at least one of the stock frustum planes failed, so we need to
3194                 // create one or two custom planes to enclose the light origin
3195                 for (i = 0;i < 4;i++)
3196                 {
3197                         // create a plane using the view origin and light origin, and a
3198                         // single point from the frustum corner set
3199                         TriangleNormal(r_refdef.view.origin, r_refdef.view.frustumcorner[i], rtlight->shadoworigin, plane.normal);
3200                         VectorNormalize(plane.normal);
3201                         plane.dist = DotProduct(r_refdef.view.origin, plane.normal);
3202                         // see if this plane is backwards and flip it if so
3203                         for (j = 0;j < 4;j++)
3204                                 if (j != i && DotProduct(r_refdef.view.frustumcorner[j], plane.normal) - plane.dist < -0.03125)
3205                                         break;
3206                         if (j < 4)
3207                         {
3208                                 VectorNegate(plane.normal, plane.normal);
3209                                 plane.dist *= -1;
3210                                 // flipped plane, test again to see if it is now valid
3211                                 for (j = 0;j < 4;j++)
3212                                         if (j != i && DotProduct(r_refdef.view.frustumcorner[j], plane.normal) - plane.dist < -0.03125)
3213                                                 break;
3214                                 // if the plane is still not valid, then it is dividing the
3215                                 // frustum and has to be rejected
3216                                 if (j < 4)
3217                                         continue;
3218                         }
3219                         // we have created a valid plane, compute extra info
3220                         PlaneClassify(&plane);
3221                         // copy the plane
3222                         rtlight->cached_frustumplanes[rtlight->cached_numfrustumplanes++] = plane;
3223 #if 1
3224                         // if we've found 5 frustum planes then we have constructed a
3225                         // proper split-side case and do not need to keep searching for
3226                         // planes to enclose the light origin
3227                         if (rtlight->cached_numfrustumplanes == 5)
3228                                 break;
3229 #endif
3230                 }
3231         }
3232 #endif
3233
3234 #if 0
3235         for (i = 0;i < rtlight->cached_numfrustumplanes;i++)
3236         {
3237                 plane = rtlight->cached_frustumplanes[i];
3238                 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));
3239         }
3240 #endif
3241
3242 #if 0
3243         // now add the light-space box planes if the light box is rotated, as any
3244         // caster outside the oriented light box is irrelevant (even if it passed
3245         // the worldspace light box, which is axial)
3246         if (rtlight->matrix_lighttoworld.m[0][0] != 1 || rtlight->matrix_lighttoworld.m[1][1] != 1 || rtlight->matrix_lighttoworld.m[2][2] != 1)
3247         {
3248                 for (i = 0;i < 6;i++)
3249                 {
3250                         vec3_t v;
3251                         VectorClear(v);
3252                         v[i >> 1] = (i & 1) ? -1 : 1;
3253                         Matrix4x4_Transform(&rtlight->matrix_lighttoworld, v, plane.normal);
3254                         VectorSubtract(plane.normal, rtlight->shadoworigin, plane.normal);
3255                         plane.dist = VectorNormalizeLength(plane.normal);
3256                         plane.dist += DotProduct(plane.normal, rtlight->shadoworigin);
3257                         rtlight->cached_frustumplanes[rtlight->cached_numfrustumplanes++] = plane;
3258                 }
3259         }
3260 #endif
3261
3262 #if 0
3263         // add the world-space reduced box planes
3264         for (i = 0;i < 6;i++)
3265         {
3266                 VectorClear(plane.normal);
3267                 plane.normal[i >> 1] = (i & 1) ? -1 : 1;
3268                 plane.dist = (i & 1) ? -rtlight->cached_cullmaxs[i >> 1] : rtlight->cached_cullmins[i >> 1];
3269                 rtlight->cached_frustumplanes[rtlight->cached_numfrustumplanes++] = plane;
3270         }
3271 #endif
3272
3273 #if 0
3274         {
3275         int j, oldnum;
3276         vec3_t points[8];
3277         vec_t bestdist;
3278         // reduce all plane distances to tightly fit the rtlight cull box, which
3279         // is in worldspace
3280         VectorSet(points[0], rtlight->cached_cullmins[0], rtlight->cached_cullmins[1], rtlight->cached_cullmins[2]);
3281         VectorSet(points[1], rtlight->cached_cullmaxs[0], rtlight->cached_cullmins[1], rtlight->cached_cullmins[2]);
3282         VectorSet(points[2], rtlight->cached_cullmins[0], rtlight->cached_cullmaxs[1], rtlight->cached_cullmins[2]);
3283         VectorSet(points[3], rtlight->cached_cullmaxs[0], rtlight->cached_cullmaxs[1], rtlight->cached_cullmins[2]);
3284         VectorSet(points[4], rtlight->cached_cullmins[0], rtlight->cached_cullmins[1], rtlight->cached_cullmaxs[2]);
3285         VectorSet(points[5], rtlight->cached_cullmaxs[0], rtlight->cached_cullmins[1], rtlight->cached_cullmaxs[2]);
3286         VectorSet(points[6], rtlight->cached_cullmins[0], rtlight->cached_cullmaxs[1], rtlight->cached_cullmaxs[2]);
3287         VectorSet(points[7], rtlight->cached_cullmaxs[0], rtlight->cached_cullmaxs[1], rtlight->cached_cullmaxs[2]);
3288         oldnum = rtlight->cached_numfrustumplanes;
3289         rtlight->cached_numfrustumplanes = 0;
3290         for (j = 0;j < oldnum;j++)
3291         {
3292                 // find the nearest point on the box to this plane
3293                 bestdist = DotProduct(rtlight->cached_frustumplanes[j].normal, points[0]);
3294                 for (i = 1;i < 8;i++)
3295                 {
3296                         dist = DotProduct(rtlight->cached_frustumplanes[j].normal, points[i]);
3297                         if (bestdist > dist)
3298                                 bestdist = dist;
3299                 }
3300                 Con_Printf("light %p %splane #%i %f %f %f : %f < %f\n", rtlight, rtlight->cached_frustumplanes[j].dist < bestdist + 0.03125 ? "^2" : "^1", j, rtlight->cached_frustumplanes[j].normal[0], rtlight->cached_frustumplanes[j].normal[1], rtlight->cached_frustumplanes[j].normal[2], rtlight->cached_frustumplanes[j].dist, bestdist);
3301                 // if the nearest point is near or behind the plane, we want this
3302                 // plane, otherwise the plane is useless as it won't cull anything
3303                 if (rtlight->cached_frustumplanes[j].dist < bestdist + 0.03125)
3304                 {
3305                         PlaneClassify(&rtlight->cached_frustumplanes[j]);
3306                         rtlight->cached_frustumplanes[rtlight->cached_numfrustumplanes++] = rtlight->cached_frustumplanes[j];
3307                 }
3308         }
3309         }
3310 #endif
3311 }
3312
3313 void R_Shadow_DrawWorldShadow_ShadowMap(int numsurfaces, int *surfacelist, const unsigned char *trispvs, const unsigned char *surfacesides)
3314 {
3315         shadowmesh_t *mesh;
3316
3317         RSurf_ActiveWorldEntity();
3318
3319         if (rsurface.rtlight->compiled && r_shadow_realtime_world_compile.integer && r_shadow_realtime_world_compileshadow.integer)
3320         {
3321                 CHECKGLERROR
3322                 GL_CullFace(GL_NONE);
3323         mesh = rsurface.rtlight->static_meshchain_shadow_shadowmap;
3324         for (;mesh;mesh = mesh->next)
3325         {
3326                         if (!mesh->sidetotals[r_shadow_shadowmapside])
3327                                 continue;
3328             r_refdef.stats.lights_shadowtriangles += mesh->sidetotals[r_shadow_shadowmapside];
3329             R_Mesh_VertexPointer(mesh->vertex3f, mesh->vbo, mesh->vbooffset_vertex3f);
3330             R_Mesh_Draw(0, mesh->numverts, mesh->sideoffsets[r_shadow_shadowmapside], mesh->sidetotals[r_shadow_shadowmapside], mesh->element3i, mesh->element3s, mesh->ebo3i, mesh->ebo3s);
3331         }
3332         CHECKGLERROR
3333     }
3334         else if (r_refdef.scene.worldentity->model)
3335                 r_refdef.scene.worldmodel->DrawShadowMap(r_shadow_shadowmapside, r_refdef.scene.worldentity, rsurface.rtlight->shadoworigin, NULL, rsurface.rtlight->radius, numsurfaces, surfacelist, surfacesides, rsurface.rtlight->cached_cullmins, rsurface.rtlight->cached_cullmaxs);
3336
3337         rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
3338 }
3339
3340 void R_Shadow_DrawWorldShadow_ShadowVolume(int numsurfaces, int *surfacelist, const unsigned char *trispvs)
3341 {
3342         qboolean zpass = false;
3343         shadowmesh_t *mesh;
3344         int t, tend;
3345         int surfacelistindex;
3346         msurface_t *surface;
3347
3348         RSurf_ActiveWorldEntity();
3349
3350         if (rsurface.rtlight->compiled && r_shadow_realtime_world_compile.integer && r_shadow_realtime_world_compileshadow.integer)
3351         {
3352                 CHECKGLERROR
3353                 if (r_shadow_rendermode != R_SHADOW_RENDERMODE_VISIBLEVOLUMES)
3354                 {
3355                         zpass = R_Shadow_UseZPass(r_refdef.scene.worldmodel->normalmins, r_refdef.scene.worldmodel->normalmaxs);
3356                         R_Shadow_RenderMode_StencilShadowVolumes(zpass);
3357                 }
3358                 mesh = zpass ? rsurface.rtlight->static_meshchain_shadow_zpass : rsurface.rtlight->static_meshchain_shadow_zfail;
3359                 for (;mesh;mesh = mesh->next)
3360                 {
3361                         r_refdef.stats.lights_shadowtriangles += mesh->numtriangles;
3362                         R_Mesh_VertexPointer(mesh->vertex3f, mesh->vbo, mesh->vbooffset_vertex3f);
3363                         GL_LockArrays(0, mesh->numverts);
3364                         if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZPASS_STENCIL)
3365                         {
3366                                 // increment stencil if frontface is infront of depthbuffer
3367                                 GL_CullFace(r_refdef.view.cullface_back);
3368                                 qglStencilOp(GL_KEEP, GL_KEEP, GL_INCR);CHECKGLERROR
3369                                 R_Mesh_Draw(0, mesh->numverts, 0, mesh->numtriangles, mesh->element3i, mesh->element3s, mesh->ebo3i, mesh->ebo3s);
3370                                 // decrement stencil if backface is infront of depthbuffer
3371                                 GL_CullFace(r_refdef.view.cullface_front);
3372                                 qglStencilOp(GL_KEEP, GL_KEEP, GL_DECR);CHECKGLERROR
3373                         }
3374                         else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZFAIL_STENCIL)
3375                         {
3376                                 // decrement stencil if backface is behind depthbuffer
3377                                 GL_CullFace(r_refdef.view.cullface_front);
3378                                 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
3379                                 R_Mesh_Draw(0, mesh->numverts, 0, mesh->numtriangles, mesh->element3i, mesh->element3s, mesh->ebo3i, mesh->ebo3s);
3380                                 // increment stencil if frontface is behind depthbuffer
3381                                 GL_CullFace(r_refdef.view.cullface_back);
3382                                 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR
3383                         }
3384                         R_Mesh_Draw(0, mesh->numverts, 0, mesh->numtriangles, mesh->element3i, mesh->element3s, mesh->ebo3i, mesh->ebo3s);
3385                         GL_LockArrays(0, 0);
3386                 }
3387                 CHECKGLERROR
3388         }
3389         else if (numsurfaces && r_refdef.scene.worldmodel->brush.shadowmesh && r_shadow_culltriangles.integer)
3390         {
3391                 R_Shadow_PrepareShadowMark(r_refdef.scene.worldmodel->brush.shadowmesh->numtriangles);
3392                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
3393                 {
3394                         surface = r_refdef.scene.worldmodel->data_surfaces + surfacelist[surfacelistindex];
3395                         for (t = surface->num_firstshadowmeshtriangle, tend = t + surface->num_triangles;t < tend;t++)
3396                                 if (CHECKPVSBIT(trispvs, t))
3397                                         shadowmarklist[numshadowmark++] = t;
3398                 }
3399                 R_Shadow_VolumeFromList(r_refdef.scene.worldmodel->brush.shadowmesh->numverts, r_refdef.scene.worldmodel->brush.shadowmesh->numtriangles, r_refdef.scene.worldmodel->brush.shadowmesh->vertex3f, r_refdef.scene.worldmodel->brush.shadowmesh->element3i, r_refdef.scene.worldmodel->brush.shadowmesh->neighbor3i, rsurface.rtlight->shadoworigin, NULL, rsurface.rtlight->radius + r_refdef.scene.worldmodel->radius*2 + r_shadow_projectdistance.value, numshadowmark, shadowmarklist, r_refdef.scene.worldmodel->normalmins, r_refdef.scene.worldmodel->normalmaxs);
3400         }
3401         else if (numsurfaces)
3402                 r_refdef.scene.worldmodel->DrawShadowVolume(r_refdef.scene.worldentity, rsurface.rtlight->shadoworigin, NULL, rsurface.rtlight->radius, numsurfaces, surfacelist, rsurface.rtlight->cached_cullmins, rsurface.rtlight->cached_cullmaxs);
3403
3404         rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
3405 }
3406
3407 void R_Shadow_DrawEntityShadow(entity_render_t *ent)
3408 {
3409         vec3_t relativeshadoworigin, relativeshadowmins, relativeshadowmaxs;
3410         vec_t relativeshadowradius;
3411         RSurf_ActiveModelEntity(ent, false, false, false);
3412         Matrix4x4_Transform(&ent->inversematrix, rsurface.rtlight->shadoworigin, relativeshadoworigin);
3413         relativeshadowradius = rsurface.rtlight->radius / ent->scale;
3414         relativeshadowmins[0] = relativeshadoworigin[0] - relativeshadowradius;
3415         relativeshadowmins[1] = relativeshadoworigin[1] - relativeshadowradius;
3416         relativeshadowmins[2] = relativeshadoworigin[2] - relativeshadowradius;
3417         relativeshadowmaxs[0] = relativeshadoworigin[0] + relativeshadowradius;
3418         relativeshadowmaxs[1] = relativeshadoworigin[1] + relativeshadowradius;
3419         relativeshadowmaxs[2] = relativeshadoworigin[2] + relativeshadowradius;
3420         if (r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAPRECTANGLE || r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAPCUBESIDE || r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAP2D)
3421         {
3422                 ent->model->DrawShadowMap(r_shadow_shadowmapside, ent, relativeshadoworigin, NULL, relativeshadowradius, ent->model->nummodelsurfaces, ent->model->sortedmodelsurfaces, NULL, relativeshadowmins, relativeshadowmaxs);
3423         }
3424         else
3425                 ent->model->DrawShadowVolume(ent, relativeshadoworigin, NULL, relativeshadowradius, ent->model->nummodelsurfaces, ent->model->sortedmodelsurfaces, relativeshadowmins, relativeshadowmaxs);
3426         rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
3427 }
3428
3429 void R_Shadow_SetupEntityLight(const entity_render_t *ent)
3430 {
3431         // set up properties for rendering light onto this entity
3432         RSurf_ActiveModelEntity(ent, true, true, false);
3433         GL_AlphaTest(false);
3434         Matrix4x4_Concat(&rsurface.entitytolight, &rsurface.rtlight->matrix_worldtolight, &ent->matrix);
3435         Matrix4x4_Concat(&rsurface.entitytoattenuationxyz, &matrix_attenuationxyz, &rsurface.entitytolight);
3436         Matrix4x4_Concat(&rsurface.entitytoattenuationz, &matrix_attenuationz, &rsurface.entitytolight);
3437         Matrix4x4_Transform(&ent->inversematrix, rsurface.rtlight->shadoworigin, rsurface.entitylightorigin);
3438         switch(r_shadow_lightingrendermode)
3439         {
3440         case R_SHADOW_RENDERMODE_LIGHT_GLSL:
3441                 R_Mesh_TexMatrix(3, &rsurface.entitytolight);
3442                 break;
3443         default:
3444                 break;
3445         }
3446 }
3447
3448 void R_Shadow_DrawWorldLight(int numsurfaces, int *surfacelist, const unsigned char *trispvs)
3449 {
3450         if (!r_refdef.scene.worldmodel->DrawLight)
3451                 return;
3452
3453         // set up properties for rendering light onto this entity
3454         RSurf_ActiveWorldEntity();
3455         GL_AlphaTest(false);
3456         rsurface.entitytolight = rsurface.rtlight->matrix_worldtolight;
3457         Matrix4x4_Concat(&rsurface.entitytoattenuationxyz, &matrix_attenuationxyz, &rsurface.entitytolight);
3458         Matrix4x4_Concat(&rsurface.entitytoattenuationz, &matrix_attenuationz, &rsurface.entitytolight);
3459         VectorCopy(rsurface.rtlight->shadoworigin, rsurface.entitylightorigin);
3460         switch(r_shadow_lightingrendermode)
3461         {
3462         case R_SHADOW_RENDERMODE_LIGHT_GLSL:
3463                 R_Mesh_TexMatrix(3, &rsurface.entitytolight);
3464                 break;
3465         default:
3466                 break;
3467         }
3468
3469         r_refdef.scene.worldmodel->DrawLight(r_refdef.scene.worldentity, numsurfaces, surfacelist, trispvs);
3470
3471         rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
3472 }
3473
3474 void R_Shadow_DrawEntityLight(entity_render_t *ent)
3475 {
3476         dp_model_t *model = ent->model;
3477         if (!model->DrawLight)
3478                 return;
3479
3480         R_Shadow_SetupEntityLight(ent);
3481
3482         model->DrawLight(ent, model->nummodelsurfaces, model->sortedmodelsurfaces, NULL);
3483
3484         rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
3485 }
3486
3487 void R_Shadow_PrepareLight(rtlight_t *rtlight)