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