implemented deferred rendering (r_shadow_deferred cvar) to accelerate
[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 && 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)
2101                         {
2102                                 Con_Printf("R_Shadow_RenderMode_ShadowMap: glCheckFramebufferStatusEXT returned %i\n", status);
2103                                 Cvar_SetValueQuick(&r_shadow_shadowmapping, 0);
2104                         }
2105 #endif
2106                 }
2107                 CHECKGLERROR
2108                 if (r_shadow_shadowmap2dtexture) fbo = r_shadow_fbo2d;
2109                 r_shadow_shadowmap_texturescale[0] = 1.0f / R_TextureWidth(r_shadow_shadowmap2dtexture);
2110                 r_shadow_shadowmap_texturescale[1] = 1.0f / R_TextureHeight(r_shadow_shadowmap2dtexture);
2111                 r_shadow_rendermode = R_SHADOW_RENDERMODE_SHADOWMAP2D;
2112         }
2113         else if (r_shadow_shadowmode == R_SHADOW_SHADOWMODE_SHADOWMAPRECTANGLE)
2114         {
2115                 r_shadow_shadowmap_parameters[0] = 0.5f * (size - r_shadow_shadowmapborder);
2116                 r_shadow_shadowmap_parameters[1] = r_shadow_shadowmapvsdct ? 2.5f*size : size;
2117                 R_Viewport_InitRectSideView(&viewport, &rsurface.rtlight->matrix_lighttoworld, side, size, r_shadow_shadowmapborder, nearclip, farclip, NULL);
2118                 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAPRECTANGLE) goto init_done;
2119
2120                 // complex unrolled cube approach (more flexible)
2121                 if (r_shadow_shadowmapvsdct && !r_shadow_shadowmapvsdcttexture)
2122                         R_Shadow_MakeVSDCT();
2123                 if (!r_shadow_shadowmaprectangletexture)
2124                 {
2125 #if 1
2126                         r_shadow_shadowmaprectangletexture = R_LoadTextureShadowMapRectangle(r_shadow_texturepool, "shadowmap", maxsize*2, maxsize*3, r_shadow_shadowmapdepthbits, r_shadow_shadowmapsampler);
2127                         qglGenFramebuffersEXT(1, &r_shadow_fborectangle);CHECKGLERROR
2128                         qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r_shadow_fborectangle);CHECKGLERROR
2129                         qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_RECTANGLE_ARB, R_GetTexture(r_shadow_shadowmaprectangletexture), 0);CHECKGLERROR
2130                         // render depth into the fbo, do not render color at all
2131                         qglDrawBuffer(GL_NONE);CHECKGLERROR
2132                         qglReadBuffer(GL_NONE);CHECKGLERROR
2133                         status = qglCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);CHECKGLERROR
2134                         if (status != GL_FRAMEBUFFER_COMPLETE_EXT && r_shadow_shadowmapping.integer)
2135                         {
2136                                 Con_Printf("R_Shadow_RenderMode_ShadowMap: glCheckFramebufferStatusEXT returned %i\n", status);
2137                                 Cvar_SetValueQuick(&r_shadow_shadowmapping, 0);
2138                         }
2139 #endif
2140                 }
2141                 CHECKGLERROR
2142                 if(r_shadow_shadowmaprectangletexture) fbo = r_shadow_fborectangle;
2143                 r_shadow_shadowmap_texturescale[0] = 1.0f;
2144                 r_shadow_shadowmap_texturescale[1] = 1.0f;
2145                 r_shadow_rendermode = R_SHADOW_RENDERMODE_SHADOWMAPRECTANGLE;
2146         }
2147         else if (r_shadow_shadowmode == R_SHADOW_SHADOWMODE_SHADOWMAPCUBESIDE)
2148         {
2149                 r_shadow_shadowmap_parameters[0] = 1.0f;
2150                 r_shadow_shadowmap_parameters[1] = 1.0f;
2151                 R_Viewport_InitCubeSideView(&viewport, &rsurface.rtlight->matrix_lighttoworld, side, size, nearclip, farclip, NULL);
2152                 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAPCUBESIDE) goto init_done;
2153
2154                 // simple cube approach
2155                 if (!r_shadow_shadowmapcubetexture[r_shadow_shadowmaplod])
2156                 {
2157  #if 1
2158                         r_shadow_shadowmapcubetexture[r_shadow_shadowmaplod] = R_LoadTextureShadowMapCube(r_shadow_texturepool, "shadowmapcube", size, r_shadow_shadowmapdepthbits, r_shadow_shadowmapsampler);
2159                         qglGenFramebuffersEXT(1, &r_shadow_fbocubeside[r_shadow_shadowmaplod]);CHECKGLERROR
2160                         qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r_shadow_fbocubeside[r_shadow_shadowmaplod]);CHECKGLERROR
2161                         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
2162                         // render depth into the fbo, do not render color at all
2163                         qglDrawBuffer(GL_NONE);CHECKGLERROR
2164                         qglReadBuffer(GL_NONE);CHECKGLERROR
2165                         status = qglCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);CHECKGLERROR
2166                         if (status != GL_FRAMEBUFFER_COMPLETE_EXT && r_shadow_shadowmapping.integer)
2167                         {
2168                                 Con_Printf("R_Shadow_RenderMode_ShadowMap: glCheckFramebufferStatusEXT returned %i\n", status);
2169                                 Cvar_SetValueQuick(&r_shadow_shadowmapping, 0);
2170                         }
2171  #endif
2172                 }
2173                 CHECKGLERROR
2174                 if (r_shadow_shadowmapcubetexture[r_shadow_shadowmaplod]) fbo = r_shadow_fbocubeside[r_shadow_shadowmaplod];
2175                 r_shadow_shadowmap_texturescale[0] = 0.0f;
2176                 r_shadow_shadowmap_texturescale[1] = 0.0f;
2177                 r_shadow_rendermode = R_SHADOW_RENDERMODE_SHADOWMAPCUBESIDE;
2178         }
2179
2180         R_Shadow_RenderMode_Reset();
2181         if (fbo)
2182         {
2183                 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);CHECKGLERROR
2184                 R_SetupDepthOrShadowShader();
2185         }
2186         else
2187         {
2188                 R_SetupShowDepthShader();
2189                 qglClearColor(1,1,1,1);CHECKGLERROR
2190         }
2191         CHECKGLERROR
2192         GL_PolygonOffset(r_shadow_shadowmapping_polygonfactor.value, r_shadow_shadowmapping_polygonoffset.value);
2193         GL_DepthMask(true);
2194         GL_DepthTest(true);
2195         qglClearDepth(1);
2196         CHECKGLERROR
2197
2198 init_done:
2199         R_SetViewport(&viewport);
2200         GL_Scissor(viewport.x, viewport.y, viewport.width, viewport.height);
2201         if(r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAP2D || r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAPRECTANGLE)
2202         {
2203                 int flipped = (side&1)^(side>>2);
2204                 r_refdef.view.cullface_front = flipped ? r_shadow_cullface_back : r_shadow_cullface_front;
2205                 r_refdef.view.cullface_back = flipped ? r_shadow_cullface_front : r_shadow_cullface_back;
2206                 GL_CullFace(r_refdef.view.cullface_back);
2207         }
2208         else if(r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAPCUBESIDE)
2209         {
2210                 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
2211         }
2212         if (clear)
2213                 qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT |  GL_STENCIL_BUFFER_BIT);
2214         CHECKGLERROR
2215 }
2216
2217 void R_Shadow_RenderMode_SetShadowMapTexture(void)
2218 {
2219         if (r_shadow_shadowmode == R_SHADOW_SHADOWMODE_SHADOWMAP2D)
2220         {
2221                 r_shadow_usingshadowmap2d = true;
2222                 R_Mesh_TexBind(GL20TU_SHADOWMAP2D, R_GetTexture(r_shadow_shadowmap2dtexture));
2223                 CHECKGLERROR
2224         }
2225         else if (r_shadow_shadowmode == R_SHADOW_SHADOWMODE_SHADOWMAPRECTANGLE)
2226         {
2227                 r_shadow_usingshadowmaprect = true;
2228                 R_Mesh_TexBindAll(GL20TU_SHADOWMAPRECT, 0, 0, 0, R_GetTexture(r_shadow_shadowmaprectangletexture));
2229                 CHECKGLERROR
2230         }
2231         else if (r_shadow_shadowmode == R_SHADOW_SHADOWMODE_SHADOWMAPCUBESIDE)
2232         {
2233                 r_shadow_usingshadowmapcube = true;
2234                 R_Mesh_TexBindAll(GL20TU_SHADOWMAPCUBE, 0, 0, R_GetTexture(r_shadow_shadowmapcubetexture[r_shadow_shadowmaplod]), 0);
2235                 CHECKGLERROR
2236         }
2237
2238         if (r_shadow_shadowmapvsdct && (r_shadow_usingshadowmap2d || r_shadow_usingshadowmaprect))
2239         {
2240                 R_Mesh_TexBindAll(GL20TU_CUBEPROJECTION, 0, 0, R_GetTexture(r_shadow_shadowmapvsdcttexture), 0);
2241                 CHECKGLERROR
2242         }
2243 }
2244
2245 void R_Shadow_RenderMode_Lighting(qboolean stenciltest, qboolean transparent, qboolean shadowmapping)
2246 {
2247         if (transparent)
2248         {
2249                 r_shadow_lightscissor[0] = r_refdef.view.viewport.x;
2250                 r_shadow_lightscissor[1] = r_refdef.view.viewport.y;
2251                 r_shadow_lightscissor[2] = r_refdef.view.viewport.width;
2252                 r_shadow_lightscissor[3] = r_refdef.view.viewport.height;
2253         }
2254         CHECKGLERROR
2255         R_Shadow_RenderMode_Reset();
2256         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2257         if (!transparent)
2258         {
2259                 qglDepthFunc(GL_EQUAL);CHECKGLERROR
2260         }
2261         if (stenciltest)
2262         {
2263                 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
2264                 // only draw light where this geometry was already rendered AND the
2265                 // stencil is 128 (values other than this mean shadow)
2266                 qglStencilFunc(GL_EQUAL, 128, ~0);CHECKGLERROR
2267         }
2268         r_shadow_rendermode = r_shadow_lightingrendermode;
2269         // do global setup needed for the chosen lighting mode
2270         if (r_shadow_rendermode == R_SHADOW_RENDERMODE_LIGHT_GLSL)
2271         {
2272                 R_Mesh_TexBindAll(GL20TU_CUBE, 0, 0, R_GetTexture(rsurface.rtlight->currentcubemap), 0); // light filter
2273                 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 0);
2274                 CHECKGLERROR
2275                 if (shadowmapping)
2276                         R_Shadow_RenderMode_SetShadowMapTexture();
2277         }
2278         R_Mesh_ColorPointer(rsurface.array_color4f, 0, 0);
2279         CHECKGLERROR
2280 }
2281
2282 static const unsigned short bboxelements[36] =
2283 {
2284         5, 1, 3, 5, 3, 7,
2285         6, 2, 0, 6, 0, 4,
2286         7, 3, 2, 7, 2, 6,
2287         4, 0, 1, 4, 1, 5,
2288         4, 5, 7, 4, 7, 6,
2289         1, 0, 2, 1, 2, 3,
2290 };
2291
2292 static const float bboxpoints[8][3] =
2293 {
2294         {-1,-1,-1},
2295         { 1,-1,-1},
2296         {-1, 1,-1},
2297         { 1, 1,-1},
2298         {-1,-1, 1},
2299         { 1,-1, 1},
2300         {-1, 1, 1},
2301         { 1, 1, 1},
2302 };
2303
2304 void R_Shadow_RenderMode_DrawDeferredLight(qboolean stenciltest, qboolean shadowmapping)
2305 {
2306         int i;
2307         float vertex3f[8*3];
2308         const matrix4x4_t *matrix = &rsurface.rtlight->matrix_lighttoworld;
2309         CHECKGLERROR
2310         R_Shadow_RenderMode_Reset();
2311         r_shadow_rendermode = r_shadow_lightingrendermode;
2312         // do global setup needed for the chosen lighting mode
2313         if (r_shadow_rendermode == R_SHADOW_RENDERMODE_LIGHT_GLSL)
2314         {
2315                 R_Mesh_Matrix(&identitymatrix);
2316                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2317                 if (stenciltest)
2318                 {
2319                         qglEnable(GL_STENCIL_TEST);CHECKGLERROR
2320                         // only draw light where this geometry was already rendered AND the
2321                         // stencil is 128 (values other than this mean shadow)
2322                         qglStencilFunc(GL_EQUAL, 128, ~0);CHECKGLERROR
2323                 }
2324                 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r_shadow_prepasslightingfbo);CHECKGLERROR
2325                 R_Mesh_TexBindAll(GL20TU_SCREENDEPTH, 0, 0, 0, R_GetTexture(r_shadow_prepassgeometrydepthtexture));
2326                 R_Mesh_TexBindAll(GL20TU_SCREENNORMALMAP, 0, 0, 0, R_GetTexture(r_shadow_prepassgeometrynormalmaptexture));
2327                 R_Mesh_TexBindAll(GL20TU_CUBE, 0, 0, R_GetTexture(rsurface.rtlight->currentcubemap), 0); // light filter
2328                 if (shadowmapping)
2329                         R_Shadow_RenderMode_SetShadowMapTexture();
2330                 R_SetupSurfaceShader(rsurface.rtlight->currentcolor, false, rsurface.rtlight->ambientscale, rsurface.rtlight->diffusescale, rsurface.rtlight->specularscale, RSURFPASS_RTLIGHT);
2331                 //R_Mesh_TexBind(GL20TU_FOGMASK, R_GetTexture(r_texture_fogattenuation));
2332                 R_Mesh_TexBind(GL20TU_ATTENUATION, R_GetTexture(r_shadow_attenuationgradienttexture));
2333
2334                 for (i = 0;i < 8;i++)
2335                         Matrix4x4_Transform(matrix, bboxpoints[i], vertex3f + i*3);
2336                 CHECKGLERROR
2337                 //qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);CHECKGLERROR
2338                 R_Mesh_VertexPointer(vertex3f, 0, 0);
2339                 R_Mesh_ColorPointer(NULL, 0, 0);
2340                 GL_ColorMask(1,1,1,1);
2341                 //GL_Color(0.25f,0.05f,0.02f,1.0f);
2342                 //R_SetupGenericShader(false);
2343                 GL_DepthMask(false);
2344                 GL_DepthRange(0, 1);
2345                 GL_PolygonOffset(0, 0);
2346                 GL_DepthTest(true);
2347                 qglDepthFunc(GL_GREATER);CHECKGLERROR
2348                 GL_CullFace(r_refdef.view.cullface_back);
2349                 //GL_AlphaTest(false);
2350                 //qglDisable(GL_STENCIL_TEST);CHECKGLERROR
2351                 R_Mesh_Draw(0, 8, 0, 12, NULL, bboxelements, 0, 0);
2352         }
2353 }
2354
2355 void R_Shadow_RenderMode_VisibleShadowVolumes(void)
2356 {
2357         CHECKGLERROR
2358         R_Shadow_RenderMode_Reset();
2359         GL_BlendFunc(GL_ONE, GL_ONE);
2360         GL_DepthRange(0, 1);
2361         GL_DepthTest(r_showshadowvolumes.integer < 2);
2362         GL_Color(0.0, 0.0125 * r_refdef.view.colorscale, 0.1 * r_refdef.view.colorscale, 1);
2363         GL_PolygonOffset(r_refdef.shadowpolygonfactor, r_refdef.shadowpolygonoffset);CHECKGLERROR
2364         GL_CullFace(GL_NONE);
2365         r_shadow_rendermode = R_SHADOW_RENDERMODE_VISIBLEVOLUMES;
2366 }
2367
2368 void R_Shadow_RenderMode_VisibleLighting(qboolean stenciltest, qboolean transparent)
2369 {
2370         CHECKGLERROR
2371         R_Shadow_RenderMode_Reset();
2372         GL_BlendFunc(GL_ONE, GL_ONE);
2373         GL_DepthRange(0, 1);
2374         GL_DepthTest(r_showlighting.integer < 2);
2375         GL_Color(0.1 * r_refdef.view.colorscale, 0.0125 * r_refdef.view.colorscale, 0, 1);
2376         if (!transparent)
2377         {
2378                 qglDepthFunc(GL_EQUAL);CHECKGLERROR
2379         }
2380         if (stenciltest)
2381         {
2382                 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
2383                 qglStencilFunc(GL_EQUAL, 128, ~0);CHECKGLERROR
2384         }
2385         r_shadow_rendermode = R_SHADOW_RENDERMODE_VISIBLELIGHTING;
2386 }
2387
2388 void R_Shadow_RenderMode_End(void)
2389 {
2390         CHECKGLERROR
2391         R_Shadow_RenderMode_Reset();
2392         R_Shadow_RenderMode_ActiveLight(NULL);
2393         GL_DepthMask(true);
2394         GL_Scissor(r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
2395         r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
2396 }
2397
2398 int bboxedges[12][2] =
2399 {
2400         // top
2401         {0, 1}, // +X
2402         {0, 2}, // +Y
2403         {1, 3}, // Y, +X
2404         {2, 3}, // X, +Y
2405         // bottom
2406         {4, 5}, // +X
2407         {4, 6}, // +Y
2408         {5, 7}, // Y, +X
2409         {6, 7}, // X, +Y
2410         // verticals
2411         {0, 4}, // +Z
2412         {1, 5}, // X, +Z
2413         {2, 6}, // Y, +Z
2414         {3, 7}, // XY, +Z
2415 };
2416
2417 qboolean R_Shadow_ScissorForBBox(const float *mins, const float *maxs)
2418 {
2419         int i, ix1, iy1, ix2, iy2;
2420         float x1, y1, x2, y2;
2421         vec4_t v, v2;
2422         float vertex[20][3];
2423         int j, k;
2424         vec4_t plane4f;
2425         int numvertices;
2426         float corner[8][4];
2427         float dist[8];
2428         int sign[8];
2429         float f;
2430
2431         r_shadow_lightscissor[0] = r_refdef.view.viewport.x;
2432         r_shadow_lightscissor[1] = r_refdef.view.viewport.y;
2433         r_shadow_lightscissor[2] = r_refdef.view.viewport.width;
2434         r_shadow_lightscissor[3] = r_refdef.view.viewport.height;
2435
2436         if (!r_shadow_scissor.integer)
2437                 return false;
2438
2439         // if view is inside the light box, just say yes it's visible
2440         if (BoxesOverlap(r_refdef.view.origin, r_refdef.view.origin, mins, maxs))
2441                 return false;
2442
2443         x1 = y1 = x2 = y2 = 0;
2444
2445         // transform all corners that are infront of the nearclip plane
2446         VectorNegate(r_refdef.view.frustum[4].normal, plane4f);
2447         plane4f[3] = r_refdef.view.frustum[4].dist;
2448         numvertices = 0;
2449         for (i = 0;i < 8;i++)
2450         {
2451                 Vector4Set(corner[i], (i & 1) ? maxs[0] : mins[0], (i & 2) ? maxs[1] : mins[1], (i & 4) ? maxs[2] : mins[2], 1);
2452                 dist[i] = DotProduct4(corner[i], plane4f);
2453                 sign[i] = dist[i] > 0;
2454                 if (!sign[i])
2455                 {
2456                         VectorCopy(corner[i], vertex[numvertices]);
2457                         numvertices++;
2458                 }
2459         }
2460         // if some points are behind the nearclip, add clipped edge points to make
2461         // sure that the scissor boundary is complete
2462         if (numvertices > 0 && numvertices < 8)
2463         {
2464                 // add clipped edge points
2465                 for (i = 0;i < 12;i++)
2466                 {
2467                         j = bboxedges[i][0];
2468                         k = bboxedges[i][1];
2469                         if (sign[j] != sign[k])
2470                         {
2471                                 f = dist[j] / (dist[j] - dist[k]);
2472                                 VectorLerp(corner[j], f, corner[k], vertex[numvertices]);
2473                                 numvertices++;
2474                         }
2475                 }
2476         }
2477
2478         // if we have no points to check, the light is behind the view plane
2479         if (!numvertices)
2480                 return true;
2481
2482         // if we have some points to transform, check what screen area is covered
2483         x1 = y1 = x2 = y2 = 0;
2484         v[3] = 1.0f;
2485         //Con_Printf("%i vertices to transform...\n", numvertices);
2486         for (i = 0;i < numvertices;i++)
2487         {
2488                 VectorCopy(vertex[i], v);
2489                 R_Viewport_TransformToScreen(&r_refdef.view.viewport, v, v2);
2490                 //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]);
2491                 if (i)
2492                 {
2493                         if (x1 > v2[0]) x1 = v2[0];
2494                         if (x2 < v2[0]) x2 = v2[0];
2495                         if (y1 > v2[1]) y1 = v2[1];
2496                         if (y2 < v2[1]) y2 = v2[1];
2497                 }
2498                 else
2499                 {
2500                         x1 = x2 = v2[0];
2501                         y1 = y2 = v2[1];
2502                 }
2503         }
2504
2505         // now convert the scissor rectangle to integer screen coordinates
2506         ix1 = (int)(x1 - 1.0f);
2507         iy1 = vid.height - (int)(y2 - 1.0f);
2508         ix2 = (int)(x2 + 1.0f);
2509         iy2 = vid.height - (int)(y1 + 1.0f);
2510         //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
2511
2512         // clamp it to the screen
2513         if (ix1 < r_refdef.view.viewport.x) ix1 = r_refdef.view.viewport.x;
2514         if (iy1 < r_refdef.view.viewport.y) iy1 = r_refdef.view.viewport.y;
2515         if (ix2 > r_refdef.view.viewport.x + r_refdef.view.viewport.width) ix2 = r_refdef.view.viewport.x + r_refdef.view.viewport.width;
2516         if (iy2 > r_refdef.view.viewport.y + r_refdef.view.viewport.height) iy2 = r_refdef.view.viewport.y + r_refdef.view.viewport.height;
2517
2518         // if it is inside out, it's not visible
2519         if (ix2 <= ix1 || iy2 <= iy1)
2520                 return true;
2521
2522         // the light area is visible, set up the scissor rectangle
2523         r_shadow_lightscissor[0] = ix1;
2524         r_shadow_lightscissor[1] = iy1;
2525         r_shadow_lightscissor[2] = ix2 - ix1;
2526         r_shadow_lightscissor[3] = iy2 - iy1;
2527
2528         r_refdef.stats.lights_scissored++;
2529         return false;
2530 }
2531
2532 static void R_Shadow_RenderLighting_Light_Vertex_Shading(int firstvertex, int numverts, int numtriangles, const int *element3i, const float *diffusecolor, const float *ambientcolor)
2533 {
2534         const float *vertex3f = rsurface.vertex3f + 3 * firstvertex;
2535         const float *normal3f = rsurface.normal3f + 3 * firstvertex;
2536         float *color4f = rsurface.array_color4f + 4 * firstvertex;
2537         float dist, dot, distintensity, shadeintensity, v[3], n[3];
2538         switch (r_shadow_rendermode)
2539         {
2540         case R_SHADOW_RENDERMODE_LIGHT_VERTEX3DATTEN:
2541         case R_SHADOW_RENDERMODE_LIGHT_VERTEX2D1DATTEN:
2542                 if (VectorLength2(diffusecolor) > 0)
2543                 {
2544                         for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
2545                         {
2546                                 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
2547                                 Matrix4x4_Transform3x3(&rsurface.entitytolight, normal3f, n);
2548                                 if ((dot = DotProduct(n, v)) < 0)
2549                                 {
2550                                         shadeintensity = -dot / sqrt(VectorLength2(v) * VectorLength2(n));
2551                                         VectorMA(ambientcolor, shadeintensity, diffusecolor, color4f);
2552                                 }
2553                                 else
2554                                         VectorCopy(ambientcolor, color4f);
2555                                 if (r_refdef.fogenabled)
2556                                 {
2557                                         float f;
2558                                         f = RSurf_FogVertex(vertex3f);
2559                                         VectorScale(color4f, f, color4f);
2560                                 }
2561                                 color4f[3] = 1;
2562                         }
2563                 }
2564                 else
2565                 {
2566                         for (;numverts > 0;numverts--, vertex3f += 3, color4f += 4)
2567                         {
2568                                 VectorCopy(ambientcolor, color4f);
2569                                 if (r_refdef.fogenabled)
2570                                 {
2571                                         float f;
2572                                         Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
2573                                         f = RSurf_FogVertex(vertex3f);
2574                                         VectorScale(color4f, f, color4f);
2575                                 }
2576                                 color4f[3] = 1;
2577                         }
2578                 }
2579                 break;
2580         case R_SHADOW_RENDERMODE_LIGHT_VERTEX2DATTEN:
2581                 if (VectorLength2(diffusecolor) > 0)
2582                 {
2583                         for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
2584                         {
2585                                 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
2586                                 if ((dist = fabs(v[2])) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
2587                                 {
2588                                         Matrix4x4_Transform3x3(&rsurface.entitytolight, normal3f, n);
2589                                         if ((dot = DotProduct(n, v)) < 0)
2590                                         {
2591                                                 shadeintensity = -dot / sqrt(VectorLength2(v) * VectorLength2(n));
2592                                                 color4f[0] = (ambientcolor[0] + shadeintensity * diffusecolor[0]) * distintensity;
2593                                                 color4f[1] = (ambientcolor[1] + shadeintensity * diffusecolor[1]) * distintensity;
2594                                                 color4f[2] = (ambientcolor[2] + shadeintensity * diffusecolor[2]) * distintensity;
2595                                         }
2596                                         else
2597                                         {
2598                                                 color4f[0] = ambientcolor[0] * distintensity;
2599                                                 color4f[1] = ambientcolor[1] * distintensity;
2600                                                 color4f[2] = ambientcolor[2] * distintensity;
2601                                         }
2602                                         if (r_refdef.fogenabled)
2603                                         {
2604                                                 float f;
2605                                                 f = RSurf_FogVertex(vertex3f);
2606                                                 VectorScale(color4f, f, color4f);
2607                                         }
2608                                 }
2609                                 else
2610                                         VectorClear(color4f);
2611                                 color4f[3] = 1;
2612                         }
2613                 }
2614                 else
2615                 {
2616                         for (;numverts > 0;numverts--, vertex3f += 3, color4f += 4)
2617                         {
2618                                 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
2619                                 if ((dist = fabs(v[2])) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
2620                                 {
2621                                         color4f[0] = ambientcolor[0] * distintensity;
2622                                         color4f[1] = ambientcolor[1] * distintensity;
2623                                         color4f[2] = ambientcolor[2] * distintensity;
2624                                         if (r_refdef.fogenabled)
2625                                         {
2626                                                 float f;
2627                                                 f = RSurf_FogVertex(vertex3f);
2628                                                 VectorScale(color4f, f, color4f);
2629                                         }
2630                                 }
2631                                 else
2632                                         VectorClear(color4f);
2633                                 color4f[3] = 1;
2634                         }
2635                 }
2636                 break;
2637         case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
2638                 if (VectorLength2(diffusecolor) > 0)
2639                 {
2640                         for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
2641                         {
2642                                 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
2643                                 if ((dist = VectorLength(v)) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
2644                                 {
2645                                         distintensity = (1 - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist);
2646                                         Matrix4x4_Transform3x3(&rsurface.entitytolight, normal3f, n);
2647                                         if ((dot = DotProduct(n, v)) < 0)
2648                                         {
2649                                                 shadeintensity = -dot / sqrt(VectorLength2(v) * VectorLength2(n));
2650                                                 color4f[0] = (ambientcolor[0] + shadeintensity * diffusecolor[0]) * distintensity;
2651                                                 color4f[1] = (ambientcolor[1] + shadeintensity * diffusecolor[1]) * distintensity;
2652                                                 color4f[2] = (ambientcolor[2] + shadeintensity * diffusecolor[2]) * distintensity;
2653                                         }
2654                                         else
2655                                         {
2656                                                 color4f[0] = ambientcolor[0] * distintensity;
2657                                                 color4f[1] = ambientcolor[1] * distintensity;
2658                                                 color4f[2] = ambientcolor[2] * distintensity;
2659                                         }
2660                                         if (r_refdef.fogenabled)
2661                                         {
2662                                                 float f;
2663                                                 f = RSurf_FogVertex(vertex3f);
2664                                                 VectorScale(color4f, f, color4f);
2665                                         }
2666                                 }
2667                                 else
2668                                         VectorClear(color4f);
2669                                 color4f[3] = 1;
2670                         }
2671                 }
2672                 else
2673                 {
2674                         for (;numverts > 0;numverts--, vertex3f += 3, color4f += 4)
2675                         {
2676                                 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
2677                                 if ((dist = VectorLength(v)) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
2678                                 {
2679                                         distintensity = (1 - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist);
2680                                         color4f[0] = ambientcolor[0] * distintensity;
2681                                         color4f[1] = ambientcolor[1] * distintensity;
2682                                         color4f[2] = ambientcolor[2] * distintensity;
2683                                         if (r_refdef.fogenabled)
2684                                         {
2685                                                 float f;
2686                                                 f = RSurf_FogVertex(vertex3f);
2687                                                 VectorScale(color4f, f, color4f);
2688                                         }
2689                                 }
2690                                 else
2691                                         VectorClear(color4f);
2692                                 color4f[3] = 1;
2693                         }
2694                 }
2695                 break;
2696         default:
2697                 break;
2698         }
2699 }
2700
2701 static void R_Shadow_RenderLighting_VisibleLighting(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int element3i_bufferobject, int element3s_bufferobject, const vec3_t lightcolorbase, const vec3_t lightcolorpants, const vec3_t lightcolorshirt, rtexture_t *basetexture, rtexture_t *pantstexture, rtexture_t *shirttexture, rtexture_t *normalmaptexture, rtexture_t *glosstexture, float ambientscale, float diffusescale, float specularscale, qboolean dopants, qboolean doshirt)
2702 {
2703         // used to display how many times a surface is lit for level design purposes
2704         R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2705 }
2706
2707 static void R_Shadow_RenderLighting_Light_GLSL(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int element3i_bufferobject, int element3s_bufferobject, const vec3_t lightcolorbase, const vec3_t lightcolorpants, const vec3_t lightcolorshirt, rtexture_t *basetexture, rtexture_t *pantstexture, rtexture_t *shirttexture, rtexture_t *normalmaptexture, rtexture_t *glosstexture, float ambientscale, float diffusescale, float specularscale, qboolean dopants, qboolean doshirt)
2708 {
2709         // ARB2 GLSL shader path (GFFX5200, Radeon 9500)
2710         R_SetupSurfaceShader(lightcolorbase, false, ambientscale, diffusescale, specularscale, RSURFPASS_RTLIGHT);
2711         if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND))
2712                 R_Mesh_ColorPointer(rsurface.modellightmapcolor4f, rsurface.modellightmapcolor4f_bufferobject, rsurface.modellightmapcolor4f_bufferoffset);
2713         else
2714                 R_Mesh_ColorPointer(NULL, 0, 0);
2715         R_Mesh_TexMatrix(0, &rsurface.texture->currenttexmatrix);
2716         R_Mesh_TexMatrix(1, &rsurface.texture->currentbackgroundtexmatrix);
2717         R_Mesh_TexBind(GL20TU_NORMAL, R_GetTexture(rsurface.texture->currentskinframe->nmap));
2718         R_Mesh_TexBind(GL20TU_COLOR, R_GetTexture(rsurface.texture->basetexture));
2719         R_Mesh_TexBind(GL20TU_GLOSS, R_GetTexture(rsurface.texture->glosstexture));
2720         if (rsurface.texture->backgroundcurrentskinframe)
2721         {
2722                 R_Mesh_TexBind(GL20TU_SECONDARY_NORMAL, R_GetTexture(rsurface.texture->backgroundcurrentskinframe->nmap));
2723                 R_Mesh_TexBind(GL20TU_SECONDARY_COLOR, R_GetTexture(rsurface.texture->backgroundbasetexture));
2724                 R_Mesh_TexBind(GL20TU_SECONDARY_GLOSS, R_GetTexture(rsurface.texture->backgroundglosstexture));
2725                 R_Mesh_TexBind(GL20TU_SECONDARY_GLOW, R_GetTexture(rsurface.texture->backgroundcurrentskinframe->glow));
2726         }
2727         //R_Mesh_TexBindAll(GL20TU_CUBE, 0, 0, R_GetTexture(rsurface.rtlight->currentcubemap), 0);
2728         R_Mesh_TexBind(GL20TU_FOGMASK, R_GetTexture(r_texture_fogattenuation));
2729         if(rsurface.texture->colormapping)
2730         {
2731                 R_Mesh_TexBind(GL20TU_PANTS, R_GetTexture(rsurface.texture->currentskinframe->pants));
2732                 R_Mesh_TexBind(GL20TU_SHIRT, R_GetTexture(rsurface.texture->currentskinframe->shirt));
2733         }
2734         R_Mesh_TexBind(GL20TU_ATTENUATION, R_GetTexture(r_shadow_attenuationgradienttexture));
2735         R_Mesh_TexCoordPointer(0, 2, rsurface.texcoordtexture2f, rsurface.texcoordtexture2f_bufferobject, rsurface.texcoordtexture2f_bufferoffset);
2736         R_Mesh_TexCoordPointer(1, 3, rsurface.svector3f, rsurface.svector3f_bufferobject, rsurface.svector3f_bufferoffset);
2737         R_Mesh_TexCoordPointer(2, 3, rsurface.tvector3f, rsurface.tvector3f_bufferobject, rsurface.tvector3f_bufferoffset);
2738         R_Mesh_TexCoordPointer(3, 3, rsurface.normal3f, rsurface.normal3f_bufferobject, rsurface.normal3f_bufferoffset);
2739         if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
2740         {
2741                 qglDepthFunc(GL_EQUAL);CHECKGLERROR
2742         }
2743         R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2744         if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
2745         {
2746                 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
2747         }
2748 }
2749
2750 static void R_Shadow_RenderLighting_Light_Vertex_Pass(int firstvertex, int numvertices, int numtriangles, const int *element3i, vec3_t diffusecolor2, vec3_t ambientcolor2)
2751 {
2752         int renders;
2753         int i;
2754         int stop;
2755         int newfirstvertex;
2756         int newlastvertex;
2757         int newnumtriangles;
2758         int *newe;
2759         const int *e;
2760         float *c;
2761         int maxtriangles = 4096;
2762         int newelements[4096*3];
2763         R_Shadow_RenderLighting_Light_Vertex_Shading(firstvertex, numvertices, numtriangles, element3i, diffusecolor2, ambientcolor2);
2764         for (renders = 0;renders < 64;renders++)
2765         {
2766                 stop = true;
2767                 newfirstvertex = 0;
2768                 newlastvertex = 0;
2769                 newnumtriangles = 0;
2770                 newe = newelements;
2771                 // due to low fillrate on the cards this vertex lighting path is
2772                 // designed for, we manually cull all triangles that do not
2773                 // contain a lit vertex
2774                 // this builds batches of triangles from multiple surfaces and
2775                 // renders them at once
2776                 for (i = 0, e = element3i;i < numtriangles;i++, e += 3)
2777                 {
2778                         if (VectorLength2(rsurface.array_color4f + e[0] * 4) + VectorLength2(rsurface.array_color4f + e[1] * 4) + VectorLength2(rsurface.array_color4f + e[2] * 4) >= 0.01)
2779                         {
2780                                 if (newnumtriangles)
2781                                 {
2782                                         newfirstvertex = min(newfirstvertex, e[0]);
2783                                         newlastvertex  = max(newlastvertex, e[0]);
2784                                 }
2785                                 else
2786                                 {
2787                                         newfirstvertex = e[0];
2788                                         newlastvertex = e[0];
2789                                 }
2790                                 newfirstvertex = min(newfirstvertex, e[1]);
2791                                 newlastvertex  = max(newlastvertex, e[1]);
2792                                 newfirstvertex = min(newfirstvertex, e[2]);
2793                                 newlastvertex  = max(newlastvertex, e[2]);
2794                                 newe[0] = e[0];
2795                                 newe[1] = e[1];
2796                                 newe[2] = e[2];
2797                                 newnumtriangles++;
2798                                 newe += 3;
2799                                 if (newnumtriangles >= maxtriangles)
2800                                 {
2801                                         R_Mesh_Draw(newfirstvertex, newlastvertex - newfirstvertex + 1, 0, newnumtriangles, newelements, NULL, 0, 0);
2802                                         newnumtriangles = 0;
2803                                         newe = newelements;
2804                                         stop = false;
2805                                 }
2806                         }
2807                 }
2808                 if (newnumtriangles >= 1)
2809                 {
2810                         R_Mesh_Draw(newfirstvertex, newlastvertex - newfirstvertex + 1, 0, newnumtriangles, newelements, NULL, 0, 0);
2811                         stop = false;
2812                 }
2813                 // if we couldn't find any lit triangles, exit early
2814                 if (stop)
2815                         break;
2816                 // now reduce the intensity for the next overbright pass
2817                 // we have to clamp to 0 here incase the drivers have improper
2818                 // handling of negative colors
2819                 // (some old drivers even have improper handling of >1 color)
2820                 stop = true;
2821                 for (i = 0, c = rsurface.array_color4f + 4 * firstvertex;i < numvertices;i++, c += 4)
2822                 {
2823                         if (c[0] > 1 || c[1] > 1 || c[2] > 1)
2824                         {
2825                                 c[0] = max(0, c[0] - 1);
2826                                 c[1] = max(0, c[1] - 1);
2827                                 c[2] = max(0, c[2] - 1);
2828                                 stop = false;
2829                         }
2830                         else
2831                                 VectorClear(c);
2832                 }
2833                 // another check...
2834                 if (stop)
2835                         break;
2836         }
2837 }
2838
2839 static void R_Shadow_RenderLighting_Light_Vertex(int firstvertex, int numvertices, int numtriangles, const int *element3i, const vec3_t lightcolorbase, const vec3_t lightcolorpants, const vec3_t lightcolorshirt, rtexture_t *basetexture, rtexture_t *pantstexture, rtexture_t *shirttexture, rtexture_t *normalmaptexture, rtexture_t *glosstexture, float ambientscale, float diffusescale, float specularscale, qboolean dopants, qboolean doshirt)
2840 {
2841         // OpenGL 1.1 path (anything)
2842         float ambientcolorbase[3], diffusecolorbase[3];
2843         float ambientcolorpants[3], diffusecolorpants[3];
2844         float ambientcolorshirt[3], diffusecolorshirt[3];
2845         rmeshstate_t m;
2846         VectorScale(lightcolorbase, ambientscale * 2 * r_refdef.view.colorscale, ambientcolorbase);
2847         VectorScale(lightcolorbase, diffusescale * 2 * r_refdef.view.colorscale, diffusecolorbase);
2848         VectorScale(lightcolorpants, ambientscale * 2 * r_refdef.view.colorscale, ambientcolorpants);
2849         VectorScale(lightcolorpants, diffusescale * 2 * r_refdef.view.colorscale, diffusecolorpants);
2850         VectorScale(lightcolorshirt, ambientscale * 2 * r_refdef.view.colorscale, ambientcolorshirt);
2851         VectorScale(lightcolorshirt, diffusescale * 2 * r_refdef.view.colorscale, diffusecolorshirt);
2852         switch(r_shadow_rendermode)
2853         {
2854         case R_SHADOW_RENDERMODE_LIGHT_VERTEX3DATTEN:
2855                 memset(&m, 0, sizeof(m));
2856                 m.tex[0] = R_GetTexture(basetexture);
2857                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2858                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2859                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2860                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2861                 m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
2862                 m.texmatrix[1] = rsurface.entitytoattenuationxyz;
2863                 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2864                 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2865                 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2866                 R_Mesh_TextureState(&m);
2867                 break;
2868         case R_SHADOW_RENDERMODE_LIGHT_VERTEX2D1DATTEN:
2869                 memset(&m, 0, sizeof(m));
2870                 m.tex[0] = R_GetTexture(basetexture);
2871                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2872                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2873                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2874                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2875                 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
2876                 m.texmatrix[1] = rsurface.entitytoattenuationxyz;
2877                 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2878                 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2879                 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2880                 m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
2881                 m.texmatrix[2] = rsurface.entitytoattenuationz;
2882                 m.pointer_texcoord3f[2] = rsurface.vertex3f;
2883                 m.pointer_texcoord_bufferobject[2] = rsurface.vertex3f_bufferobject;
2884                 m.pointer_texcoord_bufferoffset[2] = rsurface.vertex3f_bufferoffset;
2885                 R_Mesh_TextureState(&m);
2886                 break;
2887         case R_SHADOW_RENDERMODE_LIGHT_VERTEX2DATTEN:
2888                 memset(&m, 0, sizeof(m));
2889                 m.tex[0] = R_GetTexture(basetexture);
2890                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2891                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2892                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2893                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2894                 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
2895                 m.texmatrix[1] = rsurface.entitytoattenuationxyz;
2896                 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2897                 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2898                 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2899                 R_Mesh_TextureState(&m);
2900                 break;
2901         case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
2902                 memset(&m, 0, sizeof(m));
2903                 m.tex[0] = R_GetTexture(basetexture);
2904                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2905                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2906                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2907                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2908                 R_Mesh_TextureState(&m);
2909                 break;
2910         default:
2911                 break;
2912         }
2913         //R_Mesh_TexBind(0, R_GetTexture(basetexture));
2914         R_Shadow_RenderLighting_Light_Vertex_Pass(firstvertex, numvertices, numtriangles, element3i, diffusecolorbase, ambientcolorbase);
2915         if (dopants)
2916         {
2917                 R_Mesh_TexBind(0, R_GetTexture(pantstexture));
2918                 R_Shadow_RenderLighting_Light_Vertex_Pass(firstvertex, numvertices, numtriangles, element3i, diffusecolorpants, ambientcolorpants);
2919         }
2920         if (doshirt)
2921         {
2922                 R_Mesh_TexBind(0, R_GetTexture(shirttexture));
2923                 R_Shadow_RenderLighting_Light_Vertex_Pass(firstvertex, numvertices, numtriangles, element3i, diffusecolorshirt, ambientcolorshirt);
2924         }
2925 }
2926
2927 extern cvar_t gl_lightmaps;
2928 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)
2929 {
2930         float ambientscale, diffusescale, specularscale;
2931         qboolean negated;
2932         vec3_t lightcolorbase, lightcolorpants, lightcolorshirt;
2933         rtexture_t *nmap;
2934         // calculate colors to render this texture with
2935         lightcolorbase[0] = rsurface.rtlight->currentcolor[0] * rsurface.texture->dlightcolor[0];
2936         lightcolorbase[1] = rsurface.rtlight->currentcolor[1] * rsurface.texture->dlightcolor[1];
2937         lightcolorbase[2] = rsurface.rtlight->currentcolor[2] * rsurface.texture->dlightcolor[2];
2938         ambientscale = rsurface.rtlight->ambientscale;
2939         diffusescale = rsurface.rtlight->diffusescale;
2940         specularscale = rsurface.rtlight->specularscale * rsurface.texture->specularscale;
2941         if (!r_shadow_usenormalmap.integer)
2942         {
2943                 ambientscale += 1.0f * diffusescale;
2944                 diffusescale = 0;
2945                 specularscale = 0;
2946         }
2947         if ((ambientscale + diffusescale) * VectorLength2(lightcolorbase) + specularscale * VectorLength2(lightcolorbase) < (1.0f / 1048576.0f))
2948                 return;
2949         negated = (lightcolorbase[0] + lightcolorbase[1] + lightcolorbase[2] < 0) && vid.support.ext_blend_subtract;
2950         if(negated)
2951         {
2952                 VectorNegate(lightcolorbase, lightcolorbase);
2953                 qglBlendEquationEXT(GL_FUNC_REVERSE_SUBTRACT_EXT);
2954         }
2955         RSurf_SetupDepthAndCulling();
2956         nmap = rsurface.texture->currentskinframe->nmap;
2957         if (gl_lightmaps.integer)
2958                 nmap = r_texture_blanknormalmap;
2959         if (rsurface.texture->colormapping && !gl_lightmaps.integer)
2960         {
2961                 qboolean dopants = rsurface.texture->currentskinframe->pants != NULL && VectorLength2(rsurface.colormap_pantscolor) >= (1.0f / 1048576.0f);
2962                 qboolean doshirt = rsurface.texture->currentskinframe->shirt != NULL && VectorLength2(rsurface.colormap_shirtcolor) >= (1.0f / 1048576.0f);
2963                 if (dopants)
2964                 {
2965                         lightcolorpants[0] = lightcolorbase[0] * rsurface.colormap_pantscolor[0];
2966                         lightcolorpants[1] = lightcolorbase[1] * rsurface.colormap_pantscolor[1];
2967                         lightcolorpants[2] = lightcolorbase[2] * rsurface.colormap_pantscolor[2];
2968                 }
2969                 else
2970                         VectorClear(lightcolorpants);
2971                 if (doshirt)
2972                 {
2973                         lightcolorshirt[0] = lightcolorbase[0] * rsurface.colormap_shirtcolor[0];
2974                         lightcolorshirt[1] = lightcolorbase[1] * rsurface.colormap_shirtcolor[1];
2975                         lightcolorshirt[2] = lightcolorbase[2] * rsurface.colormap_shirtcolor[2];
2976                 }
2977                 else
2978                         VectorClear(lightcolorshirt);
2979                 switch (r_shadow_rendermode)
2980                 {
2981                 case R_SHADOW_RENDERMODE_VISIBLELIGHTING:
2982                         GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST) && !r_showdisabledepthtest.integer);
2983                         R_Shadow_RenderLighting_VisibleLighting(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, lightcolorpants, lightcolorshirt, rsurface.texture->basetexture, rsurface.texture->currentskinframe->pants, rsurface.texture->currentskinframe->shirt, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, dopants, doshirt);
2984                         break;
2985                 case R_SHADOW_RENDERMODE_LIGHT_GLSL:
2986                         R_Shadow_RenderLighting_Light_GLSL(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, lightcolorpants, lightcolorshirt, rsurface.texture->basetexture, rsurface.texture->currentskinframe->pants, rsurface.texture->currentskinframe->shirt, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, dopants, doshirt);
2987                         break;
2988                 case R_SHADOW_RENDERMODE_LIGHT_VERTEX3DATTEN:
2989                 case R_SHADOW_RENDERMODE_LIGHT_VERTEX2D1DATTEN:
2990                 case R_SHADOW_RENDERMODE_LIGHT_VERTEX2DATTEN:
2991                 case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
2992                         R_Shadow_RenderLighting_Light_Vertex(firstvertex, numvertices, numtriangles, element3i + firsttriangle * 3, lightcolorbase, lightcolorpants, lightcolorshirt, rsurface.texture->basetexture, rsurface.texture->currentskinframe->pants, rsurface.texture->currentskinframe->shirt, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, dopants, doshirt);
2993                         break;
2994                 default:
2995                         Con_Printf("R_Shadow_RenderLighting: unknown r_shadow_rendermode %i\n", r_shadow_rendermode);
2996                         break;
2997                 }
2998         }
2999         else
3000         {
3001                 switch (r_shadow_rendermode)
3002                 {
3003                 case R_SHADOW_RENDERMODE_VISIBLELIGHTING:
3004                         GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST) && !r_showdisabledepthtest.integer);
3005                         R_Shadow_RenderLighting_VisibleLighting(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, vec3_origin, vec3_origin, rsurface.texture->basetexture, r_texture_black, r_texture_black, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, false, false);
3006                         break;
3007                 case R_SHADOW_RENDERMODE_LIGHT_GLSL:
3008                         R_Shadow_RenderLighting_Light_GLSL(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, vec3_origin, vec3_origin, rsurface.texture->basetexture, r_texture_black, r_texture_black, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, false, false);
3009                         break;
3010                 case R_SHADOW_RENDERMODE_LIGHT_VERTEX3DATTEN:
3011                 case R_SHADOW_RENDERMODE_LIGHT_VERTEX2D1DATTEN:
3012                 case R_SHADOW_RENDERMODE_LIGHT_VERTEX2DATTEN:
3013                 case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
3014                         R_Shadow_RenderLighting_Light_Vertex(firstvertex, numvertices, numtriangles, element3i + firsttriangle * 3, lightcolorbase, vec3_origin, vec3_origin, rsurface.texture->basetexture, r_texture_black, r_texture_black, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, false, false);
3015                         break;
3016                 default:
3017                         Con_Printf("R_Shadow_RenderLighting: unknown r_shadow_rendermode %i\n", r_shadow_rendermode);
3018                         break;
3019                 }
3020         }
3021         if(negated)
3022                 qglBlendEquationEXT(GL_FUNC_ADD_EXT);
3023 }
3024
3025 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)
3026 {
3027         matrix4x4_t tempmatrix = *matrix;
3028         Matrix4x4_Scale(&tempmatrix, r_shadow_lightradiusscale.value, 1);
3029
3030         // if this light has been compiled before, free the associated data
3031         R_RTLight_Uncompile(rtlight);
3032
3033         // clear it completely to avoid any lingering data
3034         memset(rtlight, 0, sizeof(*rtlight));
3035
3036         // copy the properties
3037         rtlight->matrix_lighttoworld = tempmatrix;
3038         Matrix4x4_Invert_Simple(&rtlight->matrix_worldtolight, &tempmatrix);
3039         Matrix4x4_OriginFromMatrix(&tempmatrix, rtlight->shadoworigin);
3040         rtlight->radius = Matrix4x4_ScaleFromMatrix(&tempmatrix);
3041         VectorCopy(color, rtlight->color);
3042         rtlight->cubemapname[0] = 0;
3043         if (cubemapname && cubemapname[0])
3044                 strlcpy(rtlight->cubemapname, cubemapname, sizeof(rtlight->cubemapname));
3045         rtlight->shadow = shadow;
3046         rtlight->corona = corona;
3047         rtlight->style = style;
3048         rtlight->isstatic = isstatic;
3049         rtlight->coronasizescale = coronasizescale;
3050         rtlight->ambientscale = ambientscale;
3051         rtlight->diffusescale = diffusescale;
3052         rtlight->specularscale = specularscale;
3053         rtlight->flags = flags;
3054
3055         // compute derived data
3056         //rtlight->cullradius = rtlight->radius;
3057         //rtlight->cullradius2 = rtlight->radius * rtlight->radius;
3058         rtlight->cullmins[0] = rtlight->shadoworigin[0] - rtlight->radius;
3059         rtlight->cullmins[1] = rtlight->shadoworigin[1] - rtlight->radius;
3060         rtlight->cullmins[2] = rtlight->shadoworigin[2] - rtlight->radius;
3061         rtlight->cullmaxs[0] = rtlight->shadoworigin[0] + rtlight->radius;
3062         rtlight->cullmaxs[1] = rtlight->shadoworigin[1] + rtlight->radius;
3063         rtlight->cullmaxs[2] = rtlight->shadoworigin[2] + rtlight->radius;
3064 }
3065
3066 // compiles rtlight geometry
3067 // (undone by R_FreeCompiledRTLight, which R_UpdateLight calls)
3068 void R_RTLight_Compile(rtlight_t *rtlight)
3069 {
3070         int i;
3071         int numsurfaces, numleafs, numleafpvsbytes, numshadowtrispvsbytes, numlighttrispvsbytes;
3072         int lighttris, shadowtris, shadowzpasstris, shadowzfailtris;
3073         entity_render_t *ent = r_refdef.scene.worldentity;
3074         dp_model_t *model = r_refdef.scene.worldmodel;
3075         unsigned char *data;
3076         shadowmesh_t *mesh;
3077
3078         // compile the light
3079         rtlight->compiled = true;
3080         rtlight->shadowmode = rtlight->shadow ? (int)r_shadow_shadowmode : -1;
3081         rtlight->static_numleafs = 0;
3082         rtlight->static_numleafpvsbytes = 0;
3083         rtlight->static_leaflist = NULL;
3084         rtlight->static_leafpvs = NULL;
3085         rtlight->static_numsurfaces = 0;
3086         rtlight->static_surfacelist = NULL;
3087         rtlight->static_shadowmap_receivers = 0x3F;
3088         rtlight->static_shadowmap_casters = 0x3F;
3089         rtlight->cullmins[0] = rtlight->shadoworigin[0] - rtlight->radius;
3090         rtlight->cullmins[1] = rtlight->shadoworigin[1] - rtlight->radius;
3091         rtlight->cullmins[2] = rtlight->shadoworigin[2] - rtlight->radius;
3092         rtlight->cullmaxs[0] = rtlight->shadoworigin[0] + rtlight->radius;
3093         rtlight->cullmaxs[1] = rtlight->shadoworigin[1] + rtlight->radius;
3094         rtlight->cullmaxs[2] = rtlight->shadoworigin[2] + rtlight->radius;
3095
3096         if (model && model->GetLightInfo)
3097         {
3098                 // this variable must be set for the CompileShadowVolume/CompileShadowMap code
3099                 r_shadow_compilingrtlight = rtlight;
3100                 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);
3101                 numleafpvsbytes = (model->brush.num_leafs + 7) >> 3;
3102                 numshadowtrispvsbytes = ((model->brush.shadowmesh ? model->brush.shadowmesh->numtriangles : model->surfmesh.num_triangles) + 7) >> 3;
3103                 numlighttrispvsbytes = (model->surfmesh.num_triangles + 7) >> 3;
3104                 data = (unsigned char *)Mem_Alloc(r_main_mempool, sizeof(int) * numsurfaces + sizeof(int) * numleafs + numleafpvsbytes + numshadowtrispvsbytes + numlighttrispvsbytes);
3105                 rtlight->static_numsurfaces = numsurfaces;
3106                 rtlight->static_surfacelist = (int *)data;data += sizeof(int) * numsurfaces;
3107                 rtlight->static_numleafs = numleafs;
3108                 rtlight->static_leaflist = (int *)data;data += sizeof(int) * numleafs;
3109                 rtlight->static_numleafpvsbytes = numleafpvsbytes;
3110                 rtlight->static_leafpvs = (unsigned char *)data;data += numleafpvsbytes;
3111                 rtlight->static_numshadowtrispvsbytes = numshadowtrispvsbytes;
3112                 rtlight->static_shadowtrispvs = (unsigned char *)data;data += numshadowtrispvsbytes;
3113                 rtlight->static_numlighttrispvsbytes = numlighttrispvsbytes;
3114                 rtlight->static_lighttrispvs = (unsigned char *)data;data += numlighttrispvsbytes;
3115                 if (rtlight->static_numsurfaces)
3116                         memcpy(rtlight->static_surfacelist, r_shadow_buffer_surfacelist, rtlight->static_numsurfaces * sizeof(*rtlight->static_surfacelist));
3117                 if (rtlight->static_numleafs)
3118                         memcpy(rtlight->static_leaflist, r_shadow_buffer_leaflist, rtlight->static_numleafs * sizeof(*rtlight->static_leaflist));
3119                 if (rtlight->static_numleafpvsbytes)
3120                         memcpy(rtlight->static_leafpvs, r_shadow_buffer_leafpvs, rtlight->static_numleafpvsbytes);
3121                 if (rtlight->static_numshadowtrispvsbytes)
3122                         memcpy(rtlight->static_shadowtrispvs, r_shadow_buffer_shadowtrispvs, rtlight->static_numshadowtrispvsbytes);
3123                 if (rtlight->static_numlighttrispvsbytes)
3124                         memcpy(rtlight->static_lighttrispvs, r_shadow_buffer_lighttrispvs, rtlight->static_numlighttrispvsbytes);
3125                 switch (rtlight->shadowmode)
3126                 {
3127                 case R_SHADOW_SHADOWMODE_SHADOWMAP2D:
3128                 case R_SHADOW_SHADOWMODE_SHADOWMAPRECTANGLE:
3129                 case R_SHADOW_SHADOWMODE_SHADOWMAPCUBESIDE:
3130                         if (model->CompileShadowMap && rtlight->shadow)
3131                                 model->CompileShadowMap(ent, rtlight->shadoworigin, NULL, rtlight->radius, numsurfaces, r_shadow_buffer_surfacelist);
3132                         break;
3133                 default:
3134                         if (model->CompileShadowVolume && rtlight->shadow)
3135                                 model->CompileShadowVolume(ent, rtlight->shadoworigin, NULL, rtlight->radius, numsurfaces, r_shadow_buffer_surfacelist);
3136                         break;
3137                 }
3138                 // now we're done compiling the rtlight
3139                 r_shadow_compilingrtlight = NULL;
3140         }
3141
3142
3143         // use smallest available cullradius - box radius or light radius
3144         //rtlight->cullradius = RadiusFromBoundsAndOrigin(rtlight->cullmins, rtlight->cullmaxs, rtlight->shadoworigin);
3145         //rtlight->cullradius = min(rtlight->cullradius, rtlight->radius);
3146
3147         shadowzpasstris = 0;
3148         if (rtlight->static_meshchain_shadow_zpass)
3149                 for (mesh = rtlight->static_meshchain_shadow_zpass;mesh;mesh = mesh->next)
3150                         shadowzpasstris += mesh->numtriangles;
3151
3152         shadowzfailtris = 0;
3153         if (rtlight->static_meshchain_shadow_zfail)
3154                 for (mesh = rtlight->static_meshchain_shadow_zfail;mesh;mesh = mesh->next)
3155                         shadowzfailtris += mesh->numtriangles;
3156
3157         lighttris = 0;
3158         if (rtlight->static_numlighttrispvsbytes)
3159                 for (i = 0;i < rtlight->static_numlighttrispvsbytes*8;i++)
3160                         if (CHECKPVSBIT(rtlight->static_lighttrispvs, i))
3161                                 lighttris++;
3162
3163         shadowtris = 0;
3164         if (rtlight->static_numlighttrispvsbytes)
3165                 for (i = 0;i < rtlight->static_numshadowtrispvsbytes*8;i++)
3166                         if (CHECKPVSBIT(rtlight->static_shadowtrispvs, i))
3167                                 shadowtris++;
3168
3169         if (developer.integer >= 10)
3170                 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);
3171 }
3172
3173 void R_RTLight_Uncompile(rtlight_t *rtlight)
3174 {
3175         if (rtlight->compiled)
3176         {
3177                 if (rtlight->static_meshchain_shadow_zpass)
3178                         Mod_ShadowMesh_Free(rtlight->static_meshchain_shadow_zpass);
3179                 rtlight->static_meshchain_shadow_zpass = NULL;
3180                 if (rtlight->static_meshchain_shadow_zfail)
3181                         Mod_ShadowMesh_Free(rtlight->static_meshchain_shadow_zfail);
3182                 rtlight->static_meshchain_shadow_zfail = NULL;
3183                 if (rtlight->static_meshchain_shadow_shadowmap)
3184                         Mod_ShadowMesh_Free(rtlight->static_meshchain_shadow_shadowmap);
3185                 rtlight->static_meshchain_shadow_shadowmap = NULL;
3186                 // these allocations are grouped
3187                 if (rtlight->static_surfacelist)
3188                         Mem_Free(rtlight->static_surfacelist);
3189                 rtlight->static_numleafs = 0;
3190                 rtlight->static_numleafpvsbytes = 0;
3191                 rtlight->static_leaflist = NULL;
3192                 rtlight->static_leafpvs = NULL;
3193                 rtlight->static_numsurfaces = 0;
3194                 rtlight->static_surfacelist = NULL;
3195                 rtlight->static_numshadowtrispvsbytes = 0;
3196                 rtlight->static_shadowtrispvs = NULL;
3197                 rtlight->static_numlighttrispvsbytes = 0;
3198                 rtlight->static_lighttrispvs = NULL;
3199                 rtlight->compiled = false;
3200         }
3201 }
3202
3203 void R_Shadow_UncompileWorldLights(void)
3204 {
3205         size_t lightindex;
3206         dlight_t *light;
3207         size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
3208         for (lightindex = 0;lightindex < range;lightindex++)
3209         {
3210                 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
3211                 if (!light)
3212                         continue;
3213                 R_RTLight_Uncompile(&light->rtlight);
3214         }
3215 }
3216
3217 void R_Shadow_ComputeShadowCasterCullingPlanes(rtlight_t *rtlight)
3218 {
3219         int i, j;
3220         mplane_t plane;
3221         // reset the count of frustum planes
3222         // see rtlight->cached_frustumplanes definition for how much this array
3223         // can hold
3224         rtlight->cached_numfrustumplanes = 0;
3225
3226         // haven't implemented a culling path for ortho rendering
3227         if (!r_refdef.view.useperspective)
3228         {
3229                 // check if the light is on screen and copy the 4 planes if it is
3230                 for (i = 0;i < 4;i++)
3231                         if (PlaneDiff(rtlight->shadoworigin, &r_refdef.view.frustum[i]) < -0.03125)
3232                                 break;
3233                 if (i == 4)
3234                         for (i = 0;i < 4;i++)
3235                                 rtlight->cached_frustumplanes[rtlight->cached_numfrustumplanes++] = r_refdef.view.frustum[i];
3236                 return;
3237         }
3238
3239 #if 1
3240         // generate a deformed frustum that includes the light origin, this is
3241         // used to cull shadow casting surfaces that can not possibly cast a
3242         // shadow onto the visible light-receiving surfaces, which can be a
3243         // performance gain
3244         //
3245         // if the light origin is onscreen the result will be 4 planes exactly
3246         // if the light origin is offscreen on only one axis the result will
3247         // be exactly 5 planes (split-side case)
3248         // if the light origin is offscreen on two axes the result will be
3249         // exactly 4 planes (stretched corner case)
3250         for (i = 0;i < 4;i++)
3251         {
3252                 // quickly reject standard frustum planes that put the light
3253                 // origin outside the frustum
3254                 if (PlaneDiff(rtlight->shadoworigin, &r_refdef.view.frustum[i]) < -0.03125)
3255                         continue;
3256                 // copy the plane
3257                 rtlight->cached_frustumplanes[rtlight->cached_numfrustumplanes++] = r_refdef.view.frustum[i];
3258         }
3259         // if all the standard frustum planes were accepted, the light is onscreen
3260         // otherwise we need to generate some more planes below...
3261         if (rtlight->cached_numfrustumplanes < 4)
3262         {
3263                 // at least one of the stock frustum planes failed, so we need to
3264                 // create one or two custom planes to enclose the light origin
3265                 for (i = 0;i < 4;i++)
3266                 {
3267                         // create a plane using the view origin and light origin, and a
3268                         // single point from the frustum corner set
3269                         TriangleNormal(r_refdef.view.origin, r_refdef.view.frustumcorner[i], rtlight->shadoworigin, plane.normal);
3270                         VectorNormalize(plane.normal);
3271                         plane.dist = DotProduct(r_refdef.view.origin, plane.normal);
3272                         // see if this plane is backwards and flip it if so
3273                         for (j = 0;j < 4;j++)
3274                                 if (j != i && DotProduct(r_refdef.view.frustumcorner[j], plane.normal) - plane.dist < -0.03125)
3275                                         break;
3276                         if (j < 4)
3277                         {
3278                                 VectorNegate(plane.normal, plane.normal);
3279                                 plane.dist *= -1;
3280                                 // flipped plane, test again to see if it is now valid
3281                                 for (j = 0;j < 4;j++)
3282                                         if (j != i && DotProduct(r_refdef.view.frustumcorner[j], plane.normal) - plane.dist < -0.03125)
3283                                                 break;
3284                                 // if the plane is still not valid, then it is dividing the
3285                                 // frustum and has to be rejected
3286                                 if (j < 4)
3287                                         continue;
3288                         }
3289                         // we have created a valid plane, compute extra info
3290                         PlaneClassify(&plane);
3291                         // copy the plane
3292                         rtlight->cached_frustumplanes[rtlight->cached_numfrustumplanes++] = plane;
3293 #if 1
3294                         // if we've found 5 frustum planes then we have constructed a
3295                         // proper split-side case and do not need to keep searching for
3296                         // planes to enclose the light origin
3297                         if (rtlight->cached_numfrustumplanes == 5)
3298                                 break;
3299 #endif
3300                 }
3301         }
3302 #endif
3303
3304 #if 0
3305         for (i = 0;i < rtlight->cached_numfrustumplanes;i++)
3306         {
3307                 plane = rtlight->cached_frustumplanes[i];
3308                 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));
3309         }
3310 #endif
3311
3312 #if 0
3313         // now add the light-space box planes if the light box is rotated, as any
3314         // caster outside the oriented light box is irrelevant (even if it passed
3315         // the worldspace light box, which is axial)
3316         if (rtlight->matrix_lighttoworld.m[0][0] != 1 || rtlight->matrix_lighttoworld.m[1][1] != 1 || rtlight->matrix_lighttoworld.m[2][2] != 1)
3317         {
3318                 for (i = 0;i < 6;i++)
3319                 {
3320                         vec3_t v;
3321                         VectorClear(v);
3322                         v[i >> 1] = (i & 1) ? -1 : 1;
3323                         Matrix4x4_Transform(&rtlight->matrix_lighttoworld, v, plane.normal);
3324                         VectorSubtract(plane.normal, rtlight->shadoworigin, plane.normal);
3325                         plane.dist = VectorNormalizeLength(plane.normal);
3326                         plane.dist += DotProduct(plane.normal, rtlight->shadoworigin);
3327                         rtlight->cached_frustumplanes[rtlight->cached_numfrustumplanes++] = plane;
3328                 }
3329         }
3330 #endif
3331
3332 #if 0
3333         // add the world-space reduced box planes
3334         for (i = 0;i < 6;i++)
3335         {
3336                 VectorClear(plane.normal);
3337                 plane.normal[i >> 1] = (i & 1) ? -1 : 1;
3338                 plane.dist = (i & 1) ? -rtlight->cached_cullmaxs[i >> 1] : rtlight->cached_cullmins[i >> 1];
3339                 rtlight->cached_frustumplanes[rtlight->cached_numfrustumplanes++] = plane;
3340         }
3341 #endif
3342
3343 #if 0
3344         {
3345         int j, oldnum;
3346         vec3_t points[8];
3347         vec_t bestdist;
3348         // reduce all plane distances to tightly fit the rtlight cull box, which
3349         // is in worldspace
3350         VectorSet(points[0], rtlight->cached_cullmins[0], rtlight->cached_cullmins[1], rtlight->cached_cullmins[2]);
3351         VectorSet(points[1], rtlight->cached_cullmaxs[0], rtlight->cached_cullmins[1], rtlight->cached_cullmins[2]);
3352         VectorSet(points[2], rtlight->cached_cullmins[0], rtlight->cached_cullmaxs[1], rtlight->cached_cullmins[2]);
3353         VectorSet(points[3], rtlight->cached_cullmaxs[0], rtlight->cached_cullmaxs[1], rtlight->cached_cullmins[2]);
3354         VectorSet(points[4], rtlight->cached_cullmins[0], rtlight->cached_cullmins[1], rtlight->cached_cullmaxs[2]);
3355         VectorSet(points[5], rtlight->cached_cullmaxs[0], rtlight->cached_cullmins[1], rtlight->cached_cullmaxs[2]);
3356         VectorSet(points[6], rtlight->cached_cullmins[0], rtlight->cached_cullmaxs[1], rtlight->cached_cullmaxs[2]);
3357         VectorSet(points[7], rtlight->cached_cullmaxs[0], rtlight->cached_cullmaxs[1], rtlight->cached_cullmaxs[2]);
3358         oldnum = rtlight->cached_numfrustumplanes;
3359         rtlight->cached_numfrustumplanes = 0;
3360         for (j = 0;j < oldnum;j++)
3361         {
3362                 // find the nearest point on the box to this plane
3363                 bestdist = DotProduct(rtlight->cached_frustumplanes[j].normal, points[0]);
3364                 for (i = 1;i < 8;i++)
3365                 {
3366                         dist = DotProduct(rtlight->cached_frustumplanes[j].normal, points[i]);
3367                         if (bestdist > dist)
3368                                 bestdist = dist;
3369                 }
3370                 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);
3371                 // if the nearest point is near or behind the plane, we want this
3372                 // plane, otherwise the plane is useless as it won't cull anything
3373                 if (rtlight->cached_frustumplanes[j].dist < bestdist + 0.03125)
3374                 {
3375                         PlaneClassify(&rtlight->cached_frustumplanes[j]);
3376                         rtlight->cached_frustumplanes[rtlight->cached_numfrustumplanes++] = rtlight->cached_frustumplanes[j];
3377                 }
3378         }
3379         }
3380 #endif
3381 }
3382
3383 void R_Shadow_DrawWorldShadow_ShadowMap(int numsurfaces, int *surfacelist, const unsigned char *trispvs, const unsigned char *surfacesides)
3384 {
3385         shadowmesh_t *mesh;
3386
3387         RSurf_ActiveWorldEntity();
3388
3389         if (rsurface.rtlight->compiled && r_shadow_realtime_world_compile.integer && r_shadow_realtime_world_compileshadow.integer)
3390         {
3391                 CHECKGLERROR
3392                 GL_CullFace(GL_NONE);
3393         mesh = rsurface.rtlight->static_meshchain_shadow_shadowmap;
3394         for (;mesh;mesh = mesh->next)
3395         {
3396                         if (!mesh->sidetotals[r_shadow_shadowmapside])
3397                                 continue;
3398             r_refdef.stats.lights_shadowtriangles += mesh->sidetotals[r_shadow_shadowmapside];
3399             R_Mesh_VertexPointer(mesh->vertex3f, mesh->vbo, mesh->vbooffset_vertex3f);
3400             R_Mesh_Draw(0, mesh->numverts, mesh->sideoffsets[r_shadow_shadowmapside], mesh->sidetotals[r_shadow_shadowmapside], mesh->element3i, mesh->element3s, mesh->ebo3i, mesh->ebo3s);
3401         }
3402         CHECKGLERROR
3403     }
3404         else if (r_refdef.scene.worldentity->model)
3405                 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);
3406
3407         rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
3408 }
3409
3410 void R_Shadow_DrawWorldShadow_ShadowVolume(int numsurfaces, int *surfacelist, const unsigned char *trispvs)
3411 {
3412         qboolean zpass = false;
3413         shadowmesh_t *mesh;
3414         int t, tend;
3415         int surfacelistindex;
3416         msurface_t *surface;
3417
3418         RSurf_ActiveWorldEntity();
3419
3420         if (rsurface.rtlight->compiled && r_shadow_realtime_world_compile.integer && r_shadow_realtime_world_compileshadow.integer)
3421         {
3422                 CHECKGLERROR
3423                 if (r_shadow_rendermode != R_SHADOW_RENDERMODE_VISIBLEVOLUMES)
3424                 {
3425                         zpass = R_Shadow_UseZPass(r_refdef.scene.worldmodel->normalmins, r_refdef.scene.worldmodel->normalmaxs);
3426                         R_Shadow_RenderMode_StencilShadowVolumes(zpass);
3427                 }
3428                 mesh = zpass ? rsurface.rtlight->static_meshchain_shadow_zpass : rsurface.rtlight->static_meshchain_shadow_zfail;
3429                 for (;mesh;mesh = mesh->next)
3430                 {
3431                         r_refdef.stats.lights_shadowtriangles += mesh->numtriangles;
3432                         R_Mesh_VertexPointer(mesh->vertex3f, mesh->vbo, mesh->vbooffset_vertex3f);
3433                         GL_LockArrays(0, mesh->numverts);
3434                         if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZPASS_STENCIL)
3435                         {
3436                                 // increment stencil if frontface is infront of depthbuffer
3437                                 GL_CullFace(r_refdef.view.cullface_back);
3438                                 qglStencilOp(GL_KEEP, GL_KEEP, GL_INCR);CHECKGLERROR
3439                                 R_Mesh_Draw(0, mesh->numverts, 0, mesh->numtriangles, mesh->element3i, mesh->element3s, mesh->ebo3i, mesh->ebo3s);
3440                                 // decrement stencil if backface is infront of depthbuffer
3441                                 GL_CullFace(r_refdef.view.cullface_front);
3442                                 qglStencilOp(GL_KEEP, GL_KEEP, GL_DECR);CHECKGLERROR
3443                         }
3444                         else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZFAIL_STENCIL)
3445                         {
3446                                 // decrement stencil if backface is behind depthbuffer
3447                                 GL_CullFace(r_refdef.view.cullface_front);
3448                                 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
3449                                 R_Mesh_Draw(0, mesh->numverts, 0, mesh->numtriangles, mesh->element3i, mesh->element3s, mesh->ebo3i, mesh->ebo3s);
3450                                 // increment stencil if frontface is behind depthbuffer
3451                                 GL_CullFace(r_refdef.view.cullface_back);
3452                                 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR
3453                         }
3454                         R_Mesh_Draw(0, mesh->numverts, 0, mesh->numtriangles, mesh->element3i, mesh->element3s, mesh->ebo3i, mesh->ebo3s);
3455                         GL_LockArrays(0, 0);
3456                 }
3457                 CHECKGLERROR
3458         }
3459         else if (numsurfaces && r_refdef.scene.worldmodel->brush.shadowmesh && r_shadow_culltriangles.integer)
3460         {
3461                 R_Shadow_PrepareShadowMark(r_refdef.scene.worldmodel->brush.shadowmesh->numtriangles);
3462                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
3463                 {
3464                         surface = r_refdef.scene.worldmodel->data_surfaces + surfacelist[surfacelistindex];
3465                        &