]> icculus.org git repositories - divverent/darkplaces.git/blob - r_shadow.c
updated project files to include cap_avi.c and cap_ogg.c
[divverent/darkplaces.git] / r_shadow.c
1
2 /*
3 Terminology: Stencil Shadow Volume (sometimes called Stencil Shadows)
4 An extrusion of the lit faces, beginning at the original geometry and ending
5 further from the light source than the original geometry (presumably at least
6 as far as the light's radius, if the light has a radius at all), capped at
7 both front and back to avoid any problems (extrusion from dark faces also
8 works but has a different set of problems)
9
10 This is normally rendered using Carmack's Reverse technique, in which
11 backfaces behind zbuffer (zfail) increment the stencil, and frontfaces behind
12 zbuffer (zfail) decrement the stencil, the result is a stencil value of zero
13 where shadows did not intersect the visible geometry, suitable as a stencil
14 mask for rendering lighting everywhere but shadow.
15
16 In our case to hopefully avoid the Creative Labs patent, we draw the backfaces
17 as decrement and the frontfaces as increment, and we redefine the DepthFunc to
18 GL_LESS (the patent uses GL_GEQUAL) which causes zfail when behind surfaces
19 and zpass when infront (the patent draws where zpass with a GL_GEQUAL test),
20 additionally we clear stencil to 128 to avoid the need for the unclamped
21 incr/decr extension (not related to patent).
22
23 Patent warning:
24 This algorithm may be covered by Creative's patent (US Patent #6384822),
25 however that patent is quite specific about increment on backfaces and
26 decrement on frontfaces where zpass with GL_GEQUAL depth test, which is
27 opposite this implementation and partially opposite Carmack's Reverse paper
28 (which uses GL_LESS, but increments on backfaces and decrements on frontfaces).
29
30
31
32 Terminology: Stencil Light Volume (sometimes called Light Volumes)
33 Similar to a Stencil Shadow Volume, but inverted; rather than containing the
34 areas in shadow it contains the areas in light, this can only be built
35 quickly for certain limited cases (such as portal visibility from a point),
36 but is quite useful for some effects (sunlight coming from sky polygons is
37 one possible example, translucent occluders is another example).
38
39
40
41 Terminology: Optimized Stencil Shadow Volume
42 A Stencil Shadow Volume that has been processed sufficiently to ensure it has
43 no duplicate coverage of areas (no need to shadow an area twice), often this
44 greatly improves performance but is an operation too costly to use on moving
45 lights (however completely optimal Stencil Light Volumes can be constructed
46 in some ideal cases).
47
48
49
50 Terminology: Per Pixel Lighting (sometimes abbreviated PPL)
51 Per pixel evaluation of lighting equations, at a bare minimum this involves
52 DOT3 shading of diffuse lighting (per pixel dotproduct of negated incidence
53 vector and surface normal, using a texture of the surface bumps, called a
54 NormalMap) if supported by hardware; in our case there is support for cards
55 which are incapable of DOT3, the quality is quite poor however.  Additionally
56 it is desirable to have specular evaluation per pixel, per vertex
57 normalization of specular halfangle vectors causes noticable distortion but
58 is unavoidable on hardware without GL_ARB_fragment_program or
59 GL_ARB_fragment_shader.
60
61
62
63 Terminology: Normalization CubeMap
64 A cubemap containing normalized dot3-encoded (vectors of length 1 or less
65 encoded as RGB colors) for any possible direction, this technique allows per
66 pixel calculation of incidence vector for per pixel lighting purposes, which
67 would not otherwise be possible per pixel without GL_ARB_fragment_program or
68 GL_ARB_fragment_shader.
69
70
71
72 Terminology: 2D+1D Attenuation Texturing
73 A very crude approximation of light attenuation with distance which results
74 in cylindrical light shapes which fade vertically as a streak (some games
75 such as Doom3 allow this to be rotated to be less noticable in specific
76 cases), the technique is simply modulating lighting by two 2D textures (which
77 can be the same) on different axes of projection (XY and Z, typically), this
78 is the second best technique available without 3D Attenuation Texturing,
79 GL_ARB_fragment_program or GL_ARB_fragment_shader technology.
80
81
82
83 Terminology: 2D+1D Inverse Attenuation Texturing
84 A clever method described in papers on the Abducted engine, this has a squared
85 distance texture (bright on the outside, black in the middle), which is used
86 twice using GL_ADD blending, the result of this is used in an inverse modulate
87 (GL_ONE_MINUS_DST_ALPHA, GL_ZERO) to implement the equation
88 lighting*=(1-((X*X+Y*Y)+(Z*Z))) which is spherical (unlike 2D+1D attenuation
89 texturing).
90
91
92
93 Terminology: 3D Attenuation Texturing
94 A slightly crude approximation of light attenuation with distance, its flaws
95 are limited radius and resolution (performance tradeoffs).
96
97
98
99 Terminology: 3D Attenuation-Normalization Texturing
100 A 3D Attenuation Texture merged with a Normalization CubeMap, by making the
101 vectors shorter the lighting becomes darker, a very effective optimization of
102 diffuse lighting if 3D Attenuation Textures are already used.
103
104
105
106 Terminology: Light Cubemap Filtering
107 A technique for modeling non-uniform light distribution according to
108 direction, for example a lantern may use a cubemap to describe the light
109 emission pattern of the cage around the lantern (as well as soot buildup
110 discoloring the light in certain areas), often also used for softened grate
111 shadows and light shining through a stained glass window (done crudely by
112 texturing the lighting with a cubemap), another good example would be a disco
113 light.  This technique is used heavily in many games (Doom3 does not support
114 this however).
115
116
117
118 Terminology: Light Projection Filtering
119 A technique for modeling shadowing of light passing through translucent
120 surfaces, allowing stained glass windows and other effects to be done more
121 elegantly than possible with Light Cubemap Filtering by applying an occluder
122 texture to the lighting combined with a stencil light volume to limit the lit
123 area, this technique is used by Doom3 for spotlights and flashlights, among
124 other things, this can also be used more generally to render light passing
125 through multiple translucent occluders in a scene (using a light volume to
126 describe the area beyond the occluder, and thus mask off rendering of all
127 other areas).
128
129
130
131 Terminology: Doom3 Lighting
132 A combination of Stencil Shadow Volume, Per Pixel Lighting, Normalization
133 CubeMap, 2D+1D Attenuation Texturing, and Light Projection Filtering, as
134 demonstrated by the game Doom3.
135 */
136
137 #include "quakedef.h"
138 #include "r_shadow.h"
139 #include "cl_collision.h"
140 #include "portals.h"
141 #include "image.h"
142
143 extern void R_Shadow_EditLights_Init(void);
144
145 typedef enum r_shadow_rendermode_e
146 {
147         R_SHADOW_RENDERMODE_NONE,
148         R_SHADOW_RENDERMODE_ZPASS_STENCIL,
149         R_SHADOW_RENDERMODE_ZPASS_SEPARATESTENCIL,
150         R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE,
151         R_SHADOW_RENDERMODE_ZFAIL_STENCIL,
152         R_SHADOW_RENDERMODE_ZFAIL_SEPARATESTENCIL,
153         R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE,
154         R_SHADOW_RENDERMODE_LIGHT_VERTEX,
155         R_SHADOW_RENDERMODE_LIGHT_DOT3,
156         R_SHADOW_RENDERMODE_LIGHT_GLSL,
157         R_SHADOW_RENDERMODE_VISIBLEVOLUMES,
158         R_SHADOW_RENDERMODE_VISIBLELIGHTING,
159 }
160 r_shadow_rendermode_t;
161
162 r_shadow_rendermode_t r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
163 r_shadow_rendermode_t r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_NONE;
164 r_shadow_rendermode_t r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_NONE;
165 r_shadow_rendermode_t r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_NONE;
166
167 int maxshadowtriangles;
168 int *shadowelements;
169
170 int maxshadowvertices;
171 float *shadowvertex3f;
172
173 int maxshadowmark;
174 int numshadowmark;
175 int *shadowmark;
176 int *shadowmarklist;
177 int shadowmarkcount;
178
179 int maxvertexupdate;
180 int *vertexupdate;
181 int *vertexremap;
182 int vertexupdatenum;
183
184 int r_shadow_buffer_numleafpvsbytes;
185 unsigned char *r_shadow_buffer_visitingleafpvs;
186 unsigned char *r_shadow_buffer_leafpvs;
187 int *r_shadow_buffer_leaflist;
188
189 int r_shadow_buffer_numsurfacepvsbytes;
190 unsigned char *r_shadow_buffer_surfacepvs;
191 int *r_shadow_buffer_surfacelist;
192
193 int r_shadow_buffer_numshadowtrispvsbytes;
194 unsigned char *r_shadow_buffer_shadowtrispvs;
195 int r_shadow_buffer_numlighttrispvsbytes;
196 unsigned char *r_shadow_buffer_lighttrispvs;
197
198 rtexturepool_t *r_shadow_texturepool;
199 rtexture_t *r_shadow_attenuationgradienttexture;
200 rtexture_t *r_shadow_attenuation2dtexture;
201 rtexture_t *r_shadow_attenuation3dtexture;
202 rtexture_t *r_shadow_lightcorona;
203
204 // lights are reloaded when this changes
205 char r_shadow_mapname[MAX_QPATH];
206
207 // used only for light filters (cubemaps)
208 rtexturepool_t *r_shadow_filters_texturepool;
209
210 cvar_t r_shadow_bumpscale_basetexture = {0, "r_shadow_bumpscale_basetexture", "0", "generate fake bumpmaps from diffuse textures at this bumpyness, try 4 to match tenebrae, higher values increase depth, requires r_restart to take effect"};
211 cvar_t r_shadow_bumpscale_bumpmap = {0, "r_shadow_bumpscale_bumpmap", "4", "what magnitude to interpret _bump.tga textures as, higher values increase depth, requires r_restart to take effect"};
212 cvar_t r_shadow_debuglight = {0, "r_shadow_debuglight", "-1", "renders only one light, for level design purposes or debugging"};
213 cvar_t r_shadow_usenormalmap = {CVAR_SAVE, "r_shadow_usenormalmap", "1", "enables use of directional shading on lights"};
214 cvar_t r_shadow_gloss = {CVAR_SAVE, "r_shadow_gloss", "1", "0 disables gloss (specularity) rendering, 1 uses gloss if textures are found, 2 forces a flat metallic specular effect on everything without textures (similar to tenebrae)"};
215 cvar_t r_shadow_gloss2intensity = {0, "r_shadow_gloss2intensity", "0.125", "how bright the forced flat gloss should look if r_shadow_gloss is 2"};
216 cvar_t r_shadow_glossintensity = {0, "r_shadow_glossintensity", "1", "how bright textured glossmaps should look if r_shadow_gloss is 1 or 2"};
217 cvar_t r_shadow_glossexponent = {0, "r_shadow_glossexponent", "32", "how 'sharp' the gloss should appear (specular power)"};
218 cvar_t r_shadow_glossexact = {0, "r_shadow_glossexact", "1", "use exact reflection math for gloss (slightly slower, but should look a tad better)"};
219 cvar_t r_shadow_lightattenuationdividebias = {0, "r_shadow_lightattenuationdividebias", "1", "changes attenuation texture generation"};
220 cvar_t r_shadow_lightattenuationlinearscale = {0, "r_shadow_lightattenuationlinearscale", "2", "changes attenuation texture generation"};
221 cvar_t r_shadow_lightintensityscale = {0, "r_shadow_lightintensityscale", "1", "renders all world lights brighter or darker"};
222 cvar_t r_shadow_lightradiusscale = {0, "r_shadow_lightradiusscale", "1", "renders all world lights larger or smaller"};
223 cvar_t r_shadow_portallight = {0, "r_shadow_portallight", "1", "use portal culling to exactly determine lit triangles when compiling world lights"};
224 cvar_t r_shadow_projectdistance = {0, "r_shadow_projectdistance", "1000000", "how far to cast shadows"};
225 cvar_t r_shadow_frontsidecasting = {0, "r_shadow_frontsidecasting", "1", "whether to cast shadows from illuminated triangles (front side of model) or unlit triangles (back side of model)"};
226 cvar_t r_shadow_realtime_dlight = {CVAR_SAVE, "r_shadow_realtime_dlight", "1", "enables rendering of dynamic lights such as explosions and rocket light"};
227 cvar_t r_shadow_realtime_dlight_shadows = {CVAR_SAVE, "r_shadow_realtime_dlight_shadows", "1", "enables rendering of shadows from dynamic lights"};
228 cvar_t r_shadow_realtime_dlight_svbspculling = {0, "r_shadow_realtime_dlight_svbspculling", "0", "enables svbsp optimization on dynamic lights (very slow!)"};
229 cvar_t r_shadow_realtime_dlight_portalculling = {0, "r_shadow_realtime_dlight_portalculling", "0", "enables portal optimization on dynamic lights (slow!)"};
230 cvar_t r_shadow_realtime_world = {CVAR_SAVE, "r_shadow_realtime_world", "0", "enables rendering of full world lighting (whether loaded from the map, or a .rtlights file, or a .ent file, or a .lights file produced by hlight)"};
231 cvar_t r_shadow_realtime_world_lightmaps = {CVAR_SAVE, "r_shadow_realtime_world_lightmaps", "0", "brightness to render lightmaps when using full world lighting, try 0.5 for a tenebrae-like appearance"};
232 cvar_t r_shadow_realtime_world_shadows = {CVAR_SAVE, "r_shadow_realtime_world_shadows", "1", "enables rendering of shadows from world lights"};
233 cvar_t r_shadow_realtime_world_compile = {0, "r_shadow_realtime_world_compile", "1", "enables compilation of world lights for higher performance rendering"};
234 cvar_t r_shadow_realtime_world_compileshadow = {0, "r_shadow_realtime_world_compileshadow", "1", "enables compilation of shadows from world lights for higher performance rendering"};
235 cvar_t r_shadow_realtime_world_compilesvbsp = {0, "r_shadow_realtime_world_compilesvbsp", "1", "enables svbsp optimization during compilation"};
236 cvar_t r_shadow_realtime_world_compileportalculling = {0, "r_shadow_realtime_world_compileportalculling", "1", "enables portal-based culling optimization during compilation"};
237 cvar_t r_shadow_scissor = {0, "r_shadow_scissor", "1", "use scissor optimization of light rendering (restricts rendering to the portion of the screen affected by the light)"};
238 cvar_t r_shadow_culltriangles = {0, "r_shadow_culltriangles", "1", "performs more expensive tests to remove unnecessary triangles of lit surfaces"};
239 cvar_t r_shadow_polygonfactor = {0, "r_shadow_polygonfactor", "0", "how much to enlarge shadow volume polygons when rendering (should be 0!)"};
240 cvar_t r_shadow_polygonoffset = {0, "r_shadow_polygonoffset", "1", "how much to push shadow volumes into the distance when rendering, to reduce chances of zfighting artifacts (should not be less than 0)"};
241 cvar_t r_shadow_texture3d = {0, "r_shadow_texture3d", "1", "use 3D voxel textures for spherical attenuation rather than cylindrical (does not affect r_glsl lighting)"};
242 cvar_t r_coronas = {CVAR_SAVE, "r_coronas", "1", "brightness of corona flare effects around certain lights, 0 disables corona effects"};
243 cvar_t r_coronas_occlusionsizescale = {CVAR_SAVE, "r_coronas_occlusionsizescale", "0.1", "size of light source for corona occlusion checksm the proportion of hidden pixels controls corona intensity"};
244 cvar_t r_coronas_occlusionquery = {CVAR_SAVE, "r_coronas_occlusionquery", "1", "use GL_ARB_occlusion_query extension if supported (fades coronas according to visibility)"};
245 cvar_t gl_flashblend = {CVAR_SAVE, "gl_flashblend", "0", "render bright coronas for dynamic lights instead of actual lighting, fast but ugly"};
246 cvar_t gl_ext_separatestencil = {0, "gl_ext_separatestencil", "1", "make use of OpenGL 2.0 glStencilOpSeparate or GL_ATI_separate_stencil extension"};
247 cvar_t gl_ext_stenciltwoside = {0, "gl_ext_stenciltwoside", "1", "make use of GL_EXT_stenciltwoside extension (NVIDIA only)"};
248 cvar_t r_editlights = {0, "r_editlights", "0", "enables .rtlights file editing mode"};
249 cvar_t r_editlights_cursordistance = {0, "r_editlights_cursordistance", "1024", "maximum distance of cursor from eye"};
250 cvar_t r_editlights_cursorpushback = {0, "r_editlights_cursorpushback", "0", "how far to pull the cursor back toward the eye"};
251 cvar_t r_editlights_cursorpushoff = {0, "r_editlights_cursorpushoff", "4", "how far to push the cursor off the impacted surface"};
252 cvar_t r_editlights_cursorgrid = {0, "r_editlights_cursorgrid", "4", "snaps cursor to this grid size"};
253 cvar_t r_editlights_quakelightsizescale = {CVAR_SAVE, "r_editlights_quakelightsizescale", "1", "changes size of light entities loaded from a map"};
254
255 // note the table actually includes one more value, just to avoid the need to clamp the distance index due to minor math error
256 #define ATTENTABLESIZE 256
257 // 1D gradient, 2D circle and 3D sphere attenuation textures
258 #define ATTEN1DSIZE 32
259 #define ATTEN2DSIZE 64
260 #define ATTEN3DSIZE 32
261
262 static float r_shadow_attendividebias; // r_shadow_lightattenuationdividebias
263 static float r_shadow_attenlinearscale; // r_shadow_lightattenuationlinearscale
264 static float r_shadow_attentable[ATTENTABLESIZE+1];
265
266 rtlight_t *r_shadow_compilingrtlight;
267 static memexpandablearray_t r_shadow_worldlightsarray;
268 dlight_t *r_shadow_selectedlight;
269 dlight_t r_shadow_bufferlight;
270 vec3_t r_editlights_cursorlocation;
271
272 extern int con_vislines;
273
274 typedef struct cubemapinfo_s
275 {
276         char basename[64];
277         rtexture_t *texture;
278 }
279 cubemapinfo_t;
280
281 #define MAX_CUBEMAPS 256
282 static int numcubemaps;
283 static cubemapinfo_t cubemaps[MAX_CUBEMAPS];
284
285 void R_Shadow_UncompileWorldLights(void);
286 void R_Shadow_ClearWorldLights(void);
287 void R_Shadow_SaveWorldLights(void);
288 void R_Shadow_LoadWorldLights(void);
289 void R_Shadow_LoadLightsFile(void);
290 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void);
291 void R_Shadow_EditLights_Reload_f(void);
292 void R_Shadow_ValidateCvars(void);
293 static void R_Shadow_MakeTextures(void);
294
295 // VorteX: custom editor light sprites
296 #define EDLIGHTSPRSIZE                  8
297 cachepic_t *r_editlights_sprcursor;
298 cachepic_t *r_editlights_sprlight;
299 cachepic_t *r_editlights_sprnoshadowlight;
300 cachepic_t *r_editlights_sprcubemaplight;
301 cachepic_t *r_editlights_sprcubemapnoshadowlight;
302 cachepic_t *r_editlights_sprselection;
303
304 void r_shadow_start(void)
305 {
306         // allocate vertex processing arrays
307         numcubemaps = 0;
308         r_shadow_attenuationgradienttexture = NULL;
309         r_shadow_attenuation2dtexture = NULL;
310         r_shadow_attenuation3dtexture = NULL;
311         r_shadow_texturepool = NULL;
312         r_shadow_filters_texturepool = NULL;
313         R_Shadow_ValidateCvars();
314         R_Shadow_MakeTextures();
315         maxshadowtriangles = 0;
316         shadowelements = NULL;
317         maxshadowvertices = 0;
318         shadowvertex3f = NULL;
319         maxvertexupdate = 0;
320         vertexupdate = NULL;
321         vertexremap = NULL;
322         vertexupdatenum = 0;
323         maxshadowmark = 0;
324         numshadowmark = 0;
325         shadowmark = NULL;
326         shadowmarklist = NULL;
327         shadowmarkcount = 0;
328         r_shadow_buffer_numleafpvsbytes = 0;
329         r_shadow_buffer_visitingleafpvs = NULL;
330         r_shadow_buffer_leafpvs = NULL;
331         r_shadow_buffer_leaflist = NULL;
332         r_shadow_buffer_numsurfacepvsbytes = 0;
333         r_shadow_buffer_surfacepvs = NULL;
334         r_shadow_buffer_surfacelist = NULL;
335         r_shadow_buffer_numshadowtrispvsbytes = 0;
336         r_shadow_buffer_shadowtrispvs = NULL;
337         r_shadow_buffer_numlighttrispvsbytes = 0;
338         r_shadow_buffer_lighttrispvs = NULL;
339 }
340
341 void r_shadow_shutdown(void)
342 {
343         R_Shadow_UncompileWorldLights();
344         numcubemaps = 0;
345         r_shadow_attenuationgradienttexture = NULL;
346         r_shadow_attenuation2dtexture = NULL;
347         r_shadow_attenuation3dtexture = NULL;
348         R_FreeTexturePool(&r_shadow_texturepool);
349         R_FreeTexturePool(&r_shadow_filters_texturepool);
350         maxshadowtriangles = 0;
351         if (shadowelements)
352                 Mem_Free(shadowelements);
353         shadowelements = NULL;
354         if (shadowvertex3f)
355                 Mem_Free(shadowvertex3f);
356         shadowvertex3f = NULL;
357         maxvertexupdate = 0;
358         if (vertexupdate)
359                 Mem_Free(vertexupdate);
360         vertexupdate = NULL;
361         if (vertexremap)
362                 Mem_Free(vertexremap);
363         vertexremap = NULL;
364         vertexupdatenum = 0;
365         maxshadowmark = 0;
366         numshadowmark = 0;
367         if (shadowmark)
368                 Mem_Free(shadowmark);
369         shadowmark = NULL;
370         if (shadowmarklist)
371                 Mem_Free(shadowmarklist);
372         shadowmarklist = NULL;
373         shadowmarkcount = 0;
374         r_shadow_buffer_numleafpvsbytes = 0;
375         if (r_shadow_buffer_visitingleafpvs)
376                 Mem_Free(r_shadow_buffer_visitingleafpvs);
377         r_shadow_buffer_visitingleafpvs = NULL;
378         if (r_shadow_buffer_leafpvs)
379                 Mem_Free(r_shadow_buffer_leafpvs);
380         r_shadow_buffer_leafpvs = NULL;
381         if (r_shadow_buffer_leaflist)
382                 Mem_Free(r_shadow_buffer_leaflist);
383         r_shadow_buffer_leaflist = NULL;
384         r_shadow_buffer_numsurfacepvsbytes = 0;
385         if (r_shadow_buffer_surfacepvs)
386                 Mem_Free(r_shadow_buffer_surfacepvs);
387         r_shadow_buffer_surfacepvs = NULL;
388         if (r_shadow_buffer_surfacelist)
389                 Mem_Free(r_shadow_buffer_surfacelist);
390         r_shadow_buffer_surfacelist = NULL;
391         r_shadow_buffer_numshadowtrispvsbytes = 0;
392         if (r_shadow_buffer_shadowtrispvs)
393                 Mem_Free(r_shadow_buffer_shadowtrispvs);
394         r_shadow_buffer_numlighttrispvsbytes = 0;
395         if (r_shadow_buffer_lighttrispvs)
396                 Mem_Free(r_shadow_buffer_lighttrispvs);
397 }
398
399 void r_shadow_newmap(void)
400 {
401         if (cl.worldmodel && strncmp(cl.worldmodel->name, r_shadow_mapname, sizeof(r_shadow_mapname)))
402                 R_Shadow_EditLights_Reload_f();
403 }
404
405 void R_Shadow_Help_f(void)
406 {
407         Con_Printf(
408 "Documentation on r_shadow system:\n"
409 "Settings:\n"
410 "r_shadow_bumpscale_basetexture : base texture as bumpmap with this scale\n"
411 "r_shadow_bumpscale_bumpmap : depth scale for bumpmap conversion\n"
412 "r_shadow_debuglight : render only this light number (-1 = all)\n"
413 "r_shadow_gloss 0/1/2 : no gloss, gloss textures only, force gloss\n"
414 "r_shadow_gloss2intensity : brightness of forced gloss\n"
415 "r_shadow_glossintensity : brightness of textured gloss\n"
416 "r_shadow_lightattenuationlinearscale : used to generate attenuation texture\n"
417 "r_shadow_lightattenuationdividebias : used to generate attenuation texture\n"
418 "r_shadow_lightintensityscale : scale rendering brightness of all lights\n"
419 "r_shadow_lightradiusscale : scale rendering radius of all lights\n"
420 "r_shadow_portallight : use portal visibility for static light precomputation\n"
421 "r_shadow_projectdistance : shadow volume projection distance\n"
422 "r_shadow_realtime_dlight : use high quality dynamic lights in normal mode\n"
423 "r_shadow_realtime_dlight_shadows : cast shadows from dlights\n"
424 "r_shadow_realtime_world : use high quality world lighting mode\n"
425 "r_shadow_realtime_world_lightmaps : use lightmaps in addition to lights\n"
426 "r_shadow_realtime_world_shadows : cast shadows from world lights\n"
427 "r_shadow_realtime_world_compile : compile surface/visibility information\n"
428 "r_shadow_realtime_world_compileshadow : compile shadow geometry\n"
429 "r_shadow_scissor : use scissor optimization\n"
430 "r_shadow_polygonfactor : nudge shadow volumes closer/further\n"
431 "r_shadow_polygonoffset : nudge shadow volumes closer/further\n"
432 "r_shadow_texture3d : use 3d attenuation texture (if hardware supports)\n"
433 "r_showlighting : useful for performance testing; bright = slow!\n"
434 "r_showshadowvolumes : useful for performance testing; bright = slow!\n"
435 "Commands:\n"
436 "r_shadow_help : this help\n"
437         );
438 }
439
440 void R_Shadow_Init(void)
441 {
442         Cvar_RegisterVariable(&r_shadow_bumpscale_basetexture);
443         Cvar_RegisterVariable(&r_shadow_bumpscale_bumpmap);
444         Cvar_RegisterVariable(&r_shadow_usenormalmap);
445         Cvar_RegisterVariable(&r_shadow_debuglight);
446         Cvar_RegisterVariable(&r_shadow_gloss);
447         Cvar_RegisterVariable(&r_shadow_gloss2intensity);
448         Cvar_RegisterVariable(&r_shadow_glossintensity);
449         Cvar_RegisterVariable(&r_shadow_glossexponent);
450         Cvar_RegisterVariable(&r_shadow_glossexact);
451         Cvar_RegisterVariable(&r_shadow_lightattenuationdividebias);
452         Cvar_RegisterVariable(&r_shadow_lightattenuationlinearscale);
453         Cvar_RegisterVariable(&r_shadow_lightintensityscale);
454         Cvar_RegisterVariable(&r_shadow_lightradiusscale);
455         Cvar_RegisterVariable(&r_shadow_portallight);
456         Cvar_RegisterVariable(&r_shadow_projectdistance);
457         Cvar_RegisterVariable(&r_shadow_frontsidecasting);
458         Cvar_RegisterVariable(&r_shadow_realtime_dlight);
459         Cvar_RegisterVariable(&r_shadow_realtime_dlight_shadows);
460         Cvar_RegisterVariable(&r_shadow_realtime_dlight_svbspculling);
461         Cvar_RegisterVariable(&r_shadow_realtime_dlight_portalculling);
462         Cvar_RegisterVariable(&r_shadow_realtime_world);
463         Cvar_RegisterVariable(&r_shadow_realtime_world_lightmaps);
464         Cvar_RegisterVariable(&r_shadow_realtime_world_shadows);
465         Cvar_RegisterVariable(&r_shadow_realtime_world_compile);
466         Cvar_RegisterVariable(&r_shadow_realtime_world_compileshadow);
467         Cvar_RegisterVariable(&r_shadow_realtime_world_compilesvbsp);
468         Cvar_RegisterVariable(&r_shadow_realtime_world_compileportalculling);
469         Cvar_RegisterVariable(&r_shadow_scissor);
470         Cvar_RegisterVariable(&r_shadow_culltriangles);
471         Cvar_RegisterVariable(&r_shadow_polygonfactor);
472         Cvar_RegisterVariable(&r_shadow_polygonoffset);
473         Cvar_RegisterVariable(&r_shadow_texture3d);
474         Cvar_RegisterVariable(&r_coronas);
475         Cvar_RegisterVariable(&r_coronas_occlusionsizescale);
476         Cvar_RegisterVariable(&r_coronas_occlusionquery);
477         Cvar_RegisterVariable(&gl_flashblend);
478         Cvar_RegisterVariable(&gl_ext_separatestencil);
479         Cvar_RegisterVariable(&gl_ext_stenciltwoside);
480         if (gamemode == GAME_TENEBRAE)
481         {
482                 Cvar_SetValue("r_shadow_gloss", 2);
483                 Cvar_SetValue("r_shadow_bumpscale_basetexture", 4);
484         }
485         Cmd_AddCommand("r_shadow_help", R_Shadow_Help_f, "prints documentation on console commands and variables used by realtime lighting and shadowing system");
486         R_Shadow_EditLights_Init();
487         Mem_ExpandableArray_NewArray(&r_shadow_worldlightsarray, r_main_mempool, sizeof(dlight_t), 128);
488         maxshadowtriangles = 0;
489         shadowelements = NULL;
490         maxshadowvertices = 0;
491         shadowvertex3f = NULL;
492         maxvertexupdate = 0;
493         vertexupdate = NULL;
494         vertexremap = NULL;
495         vertexupdatenum = 0;
496         maxshadowmark = 0;
497         numshadowmark = 0;
498         shadowmark = NULL;
499         shadowmarklist = NULL;
500         shadowmarkcount = 0;
501         r_shadow_buffer_numleafpvsbytes = 0;
502         r_shadow_buffer_visitingleafpvs = NULL;
503         r_shadow_buffer_leafpvs = NULL;
504         r_shadow_buffer_leaflist = NULL;
505         r_shadow_buffer_numsurfacepvsbytes = 0;
506         r_shadow_buffer_surfacepvs = NULL;
507         r_shadow_buffer_surfacelist = NULL;
508         r_shadow_buffer_shadowtrispvs = NULL;
509         r_shadow_buffer_lighttrispvs = NULL;
510         R_RegisterModule("R_Shadow", r_shadow_start, r_shadow_shutdown, r_shadow_newmap);
511 }
512
513 matrix4x4_t matrix_attenuationxyz =
514 {
515         {
516                 {0.5, 0.0, 0.0, 0.5},
517                 {0.0, 0.5, 0.0, 0.5},
518                 {0.0, 0.0, 0.5, 0.5},
519                 {0.0, 0.0, 0.0, 1.0}
520         }
521 };
522
523 matrix4x4_t matrix_attenuationz =
524 {
525         {
526                 {0.0, 0.0, 0.5, 0.5},
527                 {0.0, 0.0, 0.0, 0.5},
528                 {0.0, 0.0, 0.0, 0.5},
529                 {0.0, 0.0, 0.0, 1.0}
530         }
531 };
532
533 void R_Shadow_ResizeShadowArrays(int numvertices, int numtriangles)
534 {
535         // make sure shadowelements is big enough for this volume
536         if (maxshadowtriangles < numtriangles)
537         {
538                 maxshadowtriangles = numtriangles;
539                 if (shadowelements)
540                         Mem_Free(shadowelements);
541                 shadowelements = (int *)Mem_Alloc(r_main_mempool, maxshadowtriangles * sizeof(int[24]));
542         }
543         // make sure shadowvertex3f is big enough for this volume
544         if (maxshadowvertices < numvertices)
545         {
546                 maxshadowvertices = numvertices;
547                 if (shadowvertex3f)
548                         Mem_Free(shadowvertex3f);
549                 shadowvertex3f = (float *)Mem_Alloc(r_main_mempool, maxshadowvertices * sizeof(float[6]));
550         }
551 }
552
553 static void R_Shadow_EnlargeLeafSurfaceTrisBuffer(int numleafs, int numsurfaces, int numshadowtriangles, int numlighttriangles)
554 {
555         int numleafpvsbytes = (((numleafs + 7) >> 3) + 255) & ~255;
556         int numsurfacepvsbytes = (((numsurfaces + 7) >> 3) + 255) & ~255;
557         int numshadowtrispvsbytes = (((numshadowtriangles + 7) >> 3) + 255) & ~255;
558         int numlighttrispvsbytes = (((numlighttriangles + 7) >> 3) + 255) & ~255;
559         if (r_shadow_buffer_numleafpvsbytes < numleafpvsbytes)
560         {
561                 if (r_shadow_buffer_visitingleafpvs)
562                         Mem_Free(r_shadow_buffer_visitingleafpvs);
563                 if (r_shadow_buffer_leafpvs)
564                         Mem_Free(r_shadow_buffer_leafpvs);
565                 if (r_shadow_buffer_leaflist)
566                         Mem_Free(r_shadow_buffer_leaflist);
567                 r_shadow_buffer_numleafpvsbytes = numleafpvsbytes;
568                 r_shadow_buffer_visitingleafpvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numleafpvsbytes);
569                 r_shadow_buffer_leafpvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numleafpvsbytes);
570                 r_shadow_buffer_leaflist = (int *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numleafpvsbytes * 8 * sizeof(*r_shadow_buffer_leaflist));
571         }
572         if (r_shadow_buffer_numsurfacepvsbytes < numsurfacepvsbytes)
573         {
574                 if (r_shadow_buffer_surfacepvs)
575                         Mem_Free(r_shadow_buffer_surfacepvs);
576                 if (r_shadow_buffer_surfacelist)
577                         Mem_Free(r_shadow_buffer_surfacelist);
578                 r_shadow_buffer_numsurfacepvsbytes = numsurfacepvsbytes;
579                 r_shadow_buffer_surfacepvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numsurfacepvsbytes);
580                 r_shadow_buffer_surfacelist = (int *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numsurfacepvsbytes * 8 * sizeof(*r_shadow_buffer_surfacelist));
581         }
582         if (r_shadow_buffer_numshadowtrispvsbytes < numshadowtrispvsbytes)
583         {
584                 if (r_shadow_buffer_shadowtrispvs)
585                         Mem_Free(r_shadow_buffer_shadowtrispvs);
586                 r_shadow_buffer_numshadowtrispvsbytes = numshadowtrispvsbytes;
587                 r_shadow_buffer_shadowtrispvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numshadowtrispvsbytes);
588         }
589         if (r_shadow_buffer_numlighttrispvsbytes < numlighttrispvsbytes)
590         {
591                 if (r_shadow_buffer_lighttrispvs)
592                         Mem_Free(r_shadow_buffer_lighttrispvs);
593                 r_shadow_buffer_numlighttrispvsbytes = numlighttrispvsbytes;
594                 r_shadow_buffer_lighttrispvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numlighttrispvsbytes);
595         }
596 }
597
598 void R_Shadow_PrepareShadowMark(int numtris)
599 {
600         // make sure shadowmark is big enough for this volume
601         if (maxshadowmark < numtris)
602         {
603                 maxshadowmark = numtris;
604                 if (shadowmark)
605                         Mem_Free(shadowmark);
606                 if (shadowmarklist)
607                         Mem_Free(shadowmarklist);
608                 shadowmark = (int *)Mem_Alloc(r_main_mempool, maxshadowmark * sizeof(*shadowmark));
609                 shadowmarklist = (int *)Mem_Alloc(r_main_mempool, maxshadowmark * sizeof(*shadowmarklist));
610                 shadowmarkcount = 0;
611         }
612         shadowmarkcount++;
613         // if shadowmarkcount wrapped we clear the array and adjust accordingly
614         if (shadowmarkcount == 0)
615         {
616                 shadowmarkcount = 1;
617                 memset(shadowmark, 0, maxshadowmark * sizeof(*shadowmark));
618         }
619         numshadowmark = 0;
620 }
621
622 static int R_Shadow_ConstructShadowVolume_ZFail(int innumvertices, int innumtris, const int *inelement3i, const int *inneighbor3i, const float *invertex3f, int *outnumvertices, int *outelement3i, float *outvertex3f, const float *projectorigin, const float *projectdirection, float projectdistance, int numshadowmarktris, const int *shadowmarktris)
623 {
624         int i, j;
625         int outtriangles = 0, outvertices = 0;
626         const int *element;
627         const float *vertex;
628         float ratio, direction[3], projectvector[3];
629
630         if (projectdirection)
631                 VectorScale(projectdirection, projectdistance, projectvector);
632         else
633                 VectorClear(projectvector);
634
635         // create the vertices
636         if (projectdirection)
637         {
638                 for (i = 0;i < numshadowmarktris;i++)
639                 {
640                         element = inelement3i + shadowmarktris[i] * 3;
641                         for (j = 0;j < 3;j++)
642                         {
643                                 if (vertexupdate[element[j]] != vertexupdatenum)
644                                 {
645                                         vertexupdate[element[j]] = vertexupdatenum;
646                                         vertexremap[element[j]] = outvertices;
647                                         vertex = invertex3f + element[j] * 3;
648                                         // project one copy of the vertex according to projectvector
649                                         VectorCopy(vertex, outvertex3f);
650                                         VectorAdd(vertex, projectvector, (outvertex3f + 3));
651                                         outvertex3f += 6;
652                                         outvertices += 2;
653                                 }
654                         }
655                 }
656         }
657         else
658         {
659                 for (i = 0;i < numshadowmarktris;i++)
660                 {
661                         element = inelement3i + shadowmarktris[i] * 3;
662                         for (j = 0;j < 3;j++)
663                         {
664                                 if (vertexupdate[element[j]] != vertexupdatenum)
665                                 {
666                                         vertexupdate[element[j]] = vertexupdatenum;
667                                         vertexremap[element[j]] = outvertices;
668                                         vertex = invertex3f + element[j] * 3;
669                                         // project one copy of the vertex to the sphere radius of the light
670                                         // (FIXME: would projecting it to the light box be better?)
671                                         VectorSubtract(vertex, projectorigin, direction);
672                                         ratio = projectdistance / VectorLength(direction);
673                                         VectorCopy(vertex, outvertex3f);
674                                         VectorMA(projectorigin, ratio, direction, (outvertex3f + 3));
675                                         outvertex3f += 6;
676                                         outvertices += 2;
677                                 }
678                         }
679                 }
680         }
681
682         if (r_shadow_frontsidecasting.integer)
683         {
684                 for (i = 0;i < numshadowmarktris;i++)
685                 {
686                         int remappedelement[3];
687                         int markindex;
688                         const int *neighbortriangle;
689
690                         markindex = shadowmarktris[i] * 3;
691                         element = inelement3i + markindex;
692                         neighbortriangle = inneighbor3i + markindex;
693                         // output the front and back triangles
694                         outelement3i[0] = vertexremap[element[0]];
695                         outelement3i[1] = vertexremap[element[1]];
696                         outelement3i[2] = vertexremap[element[2]];
697                         outelement3i[3] = vertexremap[element[2]] + 1;
698                         outelement3i[4] = vertexremap[element[1]] + 1;
699                         outelement3i[5] = vertexremap[element[0]] + 1;
700
701                         outelement3i += 6;
702                         outtriangles += 2;
703                         // output the sides (facing outward from this triangle)
704                         if (shadowmark[neighbortriangle[0]] != shadowmarkcount)
705                         {
706                                 remappedelement[0] = vertexremap[element[0]];
707                                 remappedelement[1] = vertexremap[element[1]];
708                                 outelement3i[0] = remappedelement[1];
709                                 outelement3i[1] = remappedelement[0];
710                                 outelement3i[2] = remappedelement[0] + 1;
711                                 outelement3i[3] = remappedelement[1];
712                                 outelement3i[4] = remappedelement[0] + 1;
713                                 outelement3i[5] = remappedelement[1] + 1;
714
715                                 outelement3i += 6;
716                                 outtriangles += 2;
717                         }
718                         if (shadowmark[neighbortriangle[1]] != shadowmarkcount)
719                         {
720                                 remappedelement[1] = vertexremap[element[1]];
721                                 remappedelement[2] = vertexremap[element[2]];
722                                 outelement3i[0] = remappedelement[2];
723                                 outelement3i[1] = remappedelement[1];
724                                 outelement3i[2] = remappedelement[1] + 1;
725                                 outelement3i[3] = remappedelement[2];
726                                 outelement3i[4] = remappedelement[1] + 1;
727                                 outelement3i[5] = remappedelement[2] + 1;
728
729                                 outelement3i += 6;
730                                 outtriangles += 2;
731                         }
732                         if (shadowmark[neighbortriangle[2]] != shadowmarkcount)
733                         {
734                                 remappedelement[0] = vertexremap[element[0]];
735                                 remappedelement[2] = vertexremap[element[2]];
736                                 outelement3i[0] = remappedelement[0];
737                                 outelement3i[1] = remappedelement[2];
738                                 outelement3i[2] = remappedelement[2] + 1;
739                                 outelement3i[3] = remappedelement[0];
740                                 outelement3i[4] = remappedelement[2] + 1;
741                                 outelement3i[5] = remappedelement[0] + 1;
742
743                                 outelement3i += 6;
744                                 outtriangles += 2;
745                         }
746                 }
747         }
748         else
749         {
750                 for (i = 0;i < numshadowmarktris;i++)
751                 {
752                         int remappedelement[3];
753                         int markindex;
754                         const int *neighbortriangle;
755
756                         markindex = shadowmarktris[i] * 3;
757                         element = inelement3i + markindex;
758                         neighbortriangle = inneighbor3i + markindex;
759                         // output the front and back triangles
760                         outelement3i[0] = vertexremap[element[2]];
761                         outelement3i[1] = vertexremap[element[1]];
762                         outelement3i[2] = vertexremap[element[0]];
763                         outelement3i[3] = vertexremap[element[0]] + 1;
764                         outelement3i[4] = vertexremap[element[1]] + 1;
765                         outelement3i[5] = vertexremap[element[2]] + 1;
766
767                         outelement3i += 6;
768                         outtriangles += 2;
769                         // output the sides (facing outward from this triangle)
770                         if (shadowmark[neighbortriangle[0]] != shadowmarkcount)
771                         {
772                                 remappedelement[0] = vertexremap[element[0]];
773                                 remappedelement[1] = vertexremap[element[1]];
774                                 outelement3i[0] = remappedelement[0];
775                                 outelement3i[1] = remappedelement[1];
776                                 outelement3i[2] = remappedelement[1] + 1;
777                                 outelement3i[3] = remappedelement[0];
778                                 outelement3i[4] = remappedelement[1] + 1;
779                                 outelement3i[5] = remappedelement[0] + 1;
780
781                                 outelement3i += 6;
782                                 outtriangles += 2;
783                         }
784                         if (shadowmark[neighbortriangle[1]] != shadowmarkcount)
785                         {
786                                 remappedelement[1] = vertexremap[element[1]];
787                                 remappedelement[2] = vertexremap[element[2]];
788                                 outelement3i[0] = remappedelement[1];
789                                 outelement3i[1] = remappedelement[2];
790                                 outelement3i[2] = remappedelement[2] + 1;
791                                 outelement3i[3] = remappedelement[1];
792                                 outelement3i[4] = remappedelement[2] + 1;
793                                 outelement3i[5] = remappedelement[1] + 1;
794
795                                 outelement3i += 6;
796                                 outtriangles += 2;
797                         }
798                         if (shadowmark[neighbortriangle[2]] != shadowmarkcount)
799                         {
800                                 remappedelement[0] = vertexremap[element[0]];
801                                 remappedelement[2] = vertexremap[element[2]];
802                                 outelement3i[0] = remappedelement[2];
803                                 outelement3i[1] = remappedelement[0];
804                                 outelement3i[2] = remappedelement[0] + 1;
805                                 outelement3i[3] = remappedelement[2];
806                                 outelement3i[4] = remappedelement[0] + 1;
807                                 outelement3i[5] = remappedelement[2] + 1;
808
809                                 outelement3i += 6;
810                                 outtriangles += 2;
811                         }
812                 }
813         }
814         if (outnumvertices)
815                 *outnumvertices = outvertices;
816         return outtriangles;
817 }
818
819 static int R_Shadow_ConstructShadowVolume_ZPass(int innumvertices, int innumtris, const int *inelement3i, const int *inneighbor3i, const float *invertex3f, int *outnumvertices, int *outelement3i, float *outvertex3f, const float *projectorigin, const float *projectdirection, float projectdistance, int numshadowmarktris, const int *shadowmarktris)
820 {
821         int i, j, k;
822         int outtriangles = 0, outvertices = 0;
823         const int *element;
824         const float *vertex;
825         float ratio, direction[3], projectvector[3];
826         qboolean side[4];
827
828         if (projectdirection)
829                 VectorScale(projectdirection, projectdistance, projectvector);
830         else
831                 VectorClear(projectvector);
832
833         for (i = 0;i < numshadowmarktris;i++)
834         {
835                 int remappedelement[3];
836                 int markindex;
837                 const int *neighbortriangle;
838
839                 markindex = shadowmarktris[i] * 3;
840                 neighbortriangle = inneighbor3i + markindex;
841                 side[0] = shadowmark[neighbortriangle[0]] == shadowmarkcount;
842                 side[1] = shadowmark[neighbortriangle[1]] == shadowmarkcount;
843                 side[2] = shadowmark[neighbortriangle[2]] == shadowmarkcount;
844                 if (side[0] + side[1] + side[2] == 0)
845                         continue;
846
847                 side[3] = side[0];
848                 element = inelement3i + markindex;
849
850                 // create the vertices
851                 for (j = 0;j < 3;j++)
852                 {
853                         if (side[j] + side[j+1] == 0)
854                                 continue;
855                         k = element[j];
856                         if (vertexupdate[k] != vertexupdatenum)
857                         {
858                                 vertexupdate[k] = vertexupdatenum;
859                                 vertexremap[k] = outvertices;
860                                 vertex = invertex3f + k * 3;
861                                 VectorCopy(vertex, outvertex3f);
862                                 if (projectdirection)
863                                 {
864                                         // project one copy of the vertex according to projectvector
865                                         VectorAdd(vertex, projectvector, (outvertex3f + 3));
866                                 }
867                                 else
868                                 {
869                                         // project one copy of the vertex to the sphere radius of the light
870                                         // (FIXME: would projecting it to the light box be better?)
871                                         VectorSubtract(vertex, projectorigin, direction);
872                                         ratio = projectdistance / VectorLength(direction);
873                                         VectorMA(projectorigin, ratio, direction, (outvertex3f + 3));
874                                 }
875                                 outvertex3f += 6;
876                                 outvertices += 2;
877                         }
878                 }
879
880                 // output the sides (facing outward from this triangle)
881                 if (!side[0])
882                 {
883                         remappedelement[0] = vertexremap[element[0]];
884                         remappedelement[1] = vertexremap[element[1]];
885                         outelement3i[0] = remappedelement[1];
886                         outelement3i[1] = remappedelement[0];
887                         outelement3i[2] = remappedelement[0] + 1;
888                         outelement3i[3] = remappedelement[1];
889                         outelement3i[4] = remappedelement[0] + 1;
890                         outelement3i[5] = remappedelement[1] + 1;
891
892                         outelement3i += 6;
893                         outtriangles += 2;
894                 }
895                 if (!side[1])
896                 {
897                         remappedelement[1] = vertexremap[element[1]];
898                         remappedelement[2] = vertexremap[element[2]];
899                         outelement3i[0] = remappedelement[2];
900                         outelement3i[1] = remappedelement[1];
901                         outelement3i[2] = remappedelement[1] + 1;
902                         outelement3i[3] = remappedelement[2];
903                         outelement3i[4] = remappedelement[1] + 1;
904                         outelement3i[5] = remappedelement[2] + 1;
905
906                         outelement3i += 6;
907                         outtriangles += 2;
908                 }
909                 if (!side[2])
910                 {
911                         remappedelement[0] = vertexremap[element[0]];
912                         remappedelement[2] = vertexremap[element[2]];
913                         outelement3i[0] = remappedelement[0];
914                         outelement3i[1] = remappedelement[2];
915                         outelement3i[2] = remappedelement[2] + 1;
916                         outelement3i[3] = remappedelement[0];
917                         outelement3i[4] = remappedelement[2] + 1;
918                         outelement3i[5] = remappedelement[0] + 1;
919
920                         outelement3i += 6;
921                         outtriangles += 2;
922                 }
923         }
924         if (outnumvertices)
925                 *outnumvertices = outvertices;
926         return outtriangles;
927 }
928
929 void R_Shadow_MarkVolumeFromBox(int firsttriangle, int numtris, const float *invertex3f, const int *elements, const vec3_t projectorigin, const vec3_t projectdirection, const vec3_t lightmins, const vec3_t lightmaxs, const vec3_t surfacemins, const vec3_t surfacemaxs)
930 {
931         int t, tend;
932         const int *e;
933         const float *v[3];
934         float normal[3];
935         if (!BoxesOverlap(lightmins, lightmaxs, surfacemins, surfacemaxs))
936                 return;
937         tend = firsttriangle + numtris;
938         if (BoxInsideBox(surfacemins, surfacemaxs, lightmins, lightmaxs))
939         {
940                 // surface box entirely inside light box, no box cull
941                 if (projectdirection)
942                 {
943                         for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
944                         {
945                                 TriangleNormal(invertex3f + e[0] * 3, invertex3f + e[1] * 3, invertex3f + e[2] * 3, normal);
946                                 if (r_shadow_frontsidecasting.integer == (DotProduct(normal, projectdirection) < 0))
947                                         shadowmarklist[numshadowmark++] = t;
948                         }
949                 }
950                 else
951                 {
952                         for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
953                                 if (r_shadow_frontsidecasting.integer == PointInfrontOfTriangle(projectorigin, invertex3f + e[0] * 3, invertex3f + e[1] * 3, invertex3f + e[2] * 3))
954                                         shadowmarklist[numshadowmark++] = t;
955                 }
956         }
957         else
958         {
959                 // surface box not entirely inside light box, cull each triangle
960                 if (projectdirection)
961                 {
962                         for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
963                         {
964                                 v[0] = invertex3f + e[0] * 3;
965                                 v[1] = invertex3f + e[1] * 3;
966                                 v[2] = invertex3f + e[2] * 3;
967                                 TriangleNormal(v[0], v[1], v[2], normal);
968                                 if (r_shadow_frontsidecasting.integer == (DotProduct(normal, projectdirection) < 0)
969                                  && TriangleOverlapsBox(v[0], v[1], v[2], lightmins, lightmaxs))
970                                         shadowmarklist[numshadowmark++] = t;
971                         }
972                 }
973                 else
974                 {
975                         for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
976                         {
977                                 v[0] = invertex3f + e[0] * 3;
978                                 v[1] = invertex3f + e[1] * 3;
979                                 v[2] = invertex3f + e[2] * 3;
980                                 if (r_shadow_frontsidecasting.integer == PointInfrontOfTriangle(projectorigin, v[0], v[1], v[2])
981                                  && TriangleOverlapsBox(v[0], v[1], v[2], lightmins, lightmaxs))
982                                         shadowmarklist[numshadowmark++] = t;
983                         }
984                 }
985         }
986 }
987
988 qboolean R_Shadow_UseZPass(vec3_t mins, vec3_t maxs)
989 {
990 #if 1
991         return false;
992 #else
993         if (r_shadow_compilingrtlight || !r_shadow_frontsidecasting.integer || !r_shadow_usezpassifpossible.integer)
994                 return false;
995         // check if the shadow volume intersects the near plane
996         //
997         // a ray between the eye and light origin may intersect the caster,
998         // indicating that the shadow may touch the eye location, however we must
999         // test the near plane (a polygon), not merely the eye location, so it is
1000         // easiest to enlarge the caster bounding shape slightly for this.
1001         // TODO
1002         return true;
1003 #endif
1004 }
1005
1006 void R_Shadow_VolumeFromList(int numverts, int numtris, const float *invertex3f, const int *elements, const int *neighbors, const vec3_t projectorigin, const vec3_t projectdirection, float projectdistance, int nummarktris, const int *marktris, vec3_t trismins, vec3_t trismaxs)
1007 {
1008         int i, tris, outverts;
1009         if (projectdistance < 0.1)
1010         {
1011                 Con_Printf("R_Shadow_Volume: projectdistance %f\n", projectdistance);
1012                 return;
1013         }
1014         if (!numverts || !nummarktris)
1015                 return;
1016         // make sure shadowelements is big enough for this volume
1017         if (maxshadowtriangles < nummarktris || maxshadowvertices < numverts)
1018                 R_Shadow_ResizeShadowArrays((numverts + 255) & ~255, (nummarktris + 255) & ~255);
1019
1020         if (maxvertexupdate < numverts)
1021         {
1022                 maxvertexupdate = numverts;
1023                 if (vertexupdate)
1024                         Mem_Free(vertexupdate);
1025                 if (vertexremap)
1026                         Mem_Free(vertexremap);
1027                 vertexupdate = (int *)Mem_Alloc(r_main_mempool, maxvertexupdate * sizeof(int));
1028                 vertexremap = (int *)Mem_Alloc(r_main_mempool, maxvertexupdate * sizeof(int));
1029                 vertexupdatenum = 0;
1030         }
1031         vertexupdatenum++;
1032         if (vertexupdatenum == 0)
1033         {
1034                 vertexupdatenum = 1;
1035                 memset(vertexupdate, 0, maxvertexupdate * sizeof(int));
1036                 memset(vertexremap, 0, maxvertexupdate * sizeof(int));
1037         }
1038
1039         for (i = 0;i < nummarktris;i++)
1040                 shadowmark[marktris[i]] = shadowmarkcount;
1041
1042         if (r_shadow_compilingrtlight)
1043         {
1044                 // if we're compiling an rtlight, capture the mesh
1045                 //tris = R_Shadow_ConstructShadowVolume_ZPass(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
1046                 //Mod_ShadowMesh_AddMesh(r_main_mempool, r_shadow_compilingrtlight->static_meshchain_shadow_zpass, NULL, NULL, NULL, shadowvertex3f, NULL, NULL, NULL, NULL, tris, shadowelements);
1047                 tris = R_Shadow_ConstructShadowVolume_ZFail(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
1048                 Mod_ShadowMesh_AddMesh(r_main_mempool, r_shadow_compilingrtlight->static_meshchain_shadow_zfail, NULL, NULL, NULL, shadowvertex3f, NULL, NULL, NULL, NULL, tris, shadowelements);
1049         }
1050         else
1051         {
1052                 // decide which type of shadow to generate and set stencil mode
1053                 R_Shadow_RenderMode_StencilShadowVolumes(R_Shadow_UseZPass(trismins, trismaxs));
1054                 // generate the sides or a solid volume, depending on type
1055                 if (r_shadow_rendermode >= R_SHADOW_RENDERMODE_ZPASS_STENCIL && r_shadow_rendermode <= R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE)
1056                         tris = R_Shadow_ConstructShadowVolume_ZPass(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
1057                 else
1058                         tris = R_Shadow_ConstructShadowVolume_ZFail(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
1059                 r_refdef.stats.lights_dynamicshadowtriangles += tris;
1060                 r_refdef.stats.lights_shadowtriangles += tris;
1061                 CHECKGLERROR
1062                 R_Mesh_VertexPointer(shadowvertex3f, 0, 0);
1063                 GL_LockArrays(0, outverts);
1064                 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZPASS_STENCIL)
1065                 {
1066                         // increment stencil if frontface is infront of depthbuffer
1067                         GL_CullFace(r_refdef.view.cullface_front);
1068                         qglStencilOp(GL_KEEP, GL_KEEP, GL_DECR);CHECKGLERROR
1069                         R_Mesh_Draw(0, outverts, 0, tris, shadowelements, NULL, 0, 0);
1070                         // decrement stencil if backface is infront of depthbuffer
1071                         GL_CullFace(r_refdef.view.cullface_back);
1072                         qglStencilOp(GL_KEEP, GL_KEEP, GL_INCR);CHECKGLERROR
1073                 }
1074                 else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZFAIL_STENCIL)
1075                 {
1076                         // decrement stencil if backface is behind depthbuffer
1077                         GL_CullFace(r_refdef.view.cullface_front);
1078                         qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
1079                         R_Mesh_Draw(0, outverts, 0, tris, shadowelements, NULL, 0, 0);
1080                         // increment stencil if frontface is behind depthbuffer
1081                         GL_CullFace(r_refdef.view.cullface_back);
1082                         qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR
1083                 }
1084                 R_Mesh_Draw(0, outverts, 0, tris, shadowelements, NULL, 0, 0);
1085                 GL_LockArrays(0, 0);
1086                 CHECKGLERROR
1087         }
1088 }
1089
1090 static void R_Shadow_MakeTextures_MakeCorona(void)
1091 {
1092         float dx, dy;
1093         int x, y, a;
1094         unsigned char pixels[32][32][4];
1095         for (y = 0;y < 32;y++)
1096         {
1097                 dy = (y - 15.5f) * (1.0f / 16.0f);
1098                 for (x = 0;x < 32;x++)
1099                 {
1100                         dx = (x - 15.5f) * (1.0f / 16.0f);
1101                         a = (int)(((1.0f / (dx * dx + dy * dy + 0.2f)) - (1.0f / (1.0f + 0.2))) * 32.0f / (1.0f / (1.0f + 0.2)));
1102                         a = bound(0, a, 255);
1103                         pixels[y][x][0] = a;
1104                         pixels[y][x][1] = a;
1105                         pixels[y][x][2] = a;
1106                         pixels[y][x][3] = 255;
1107                 }
1108         }
1109         r_shadow_lightcorona = R_LoadTexture2D(r_shadow_texturepool, "lightcorona", 32, 32, &pixels[0][0][0], TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_FORCELINEAR, NULL);
1110 }
1111
1112 static unsigned int R_Shadow_MakeTextures_SamplePoint(float x, float y, float z)
1113 {
1114         float dist = sqrt(x*x+y*y+z*z);
1115         float intensity = dist < 1 ? ((1.0f - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist)) : 0;
1116         // note this code could suffer byte order issues except that it is multiplying by an integer that reads the same both ways
1117         return (unsigned char)bound(0, intensity * 256.0f, 255) * 0x01010101;
1118 }
1119
1120 static void R_Shadow_MakeTextures(void)
1121 {
1122         int x, y, z;
1123         float intensity, dist;
1124         unsigned int *data;
1125         R_FreeTexturePool(&r_shadow_texturepool);
1126         r_shadow_texturepool = R_AllocTexturePool();
1127         r_shadow_attenlinearscale = r_shadow_lightattenuationlinearscale.value;
1128         r_shadow_attendividebias = r_shadow_lightattenuationdividebias.value;
1129         data = (unsigned int *)Mem_Alloc(tempmempool, max(max(ATTEN3DSIZE*ATTEN3DSIZE*ATTEN3DSIZE, ATTEN2DSIZE*ATTEN2DSIZE), ATTEN1DSIZE) * 4);
1130         // the table includes one additional value to avoid the need to clamp indexing due to minor math errors
1131         for (x = 0;x <= ATTENTABLESIZE;x++)
1132         {
1133                 dist = (x + 0.5f) * (1.0f / ATTENTABLESIZE) * (1.0f / 0.9375);
1134                 intensity = dist < 1 ? ((1.0f - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist)) : 0;
1135                 r_shadow_attentable[x] = bound(0, intensity, 1);
1136         }
1137         // 1D gradient texture
1138         for (x = 0;x < ATTEN1DSIZE;x++)
1139                 data[x] = R_Shadow_MakeTextures_SamplePoint((x + 0.5f) * (1.0f / ATTEN1DSIZE) * (1.0f / 0.9375), 0, 0);
1140         r_shadow_attenuationgradienttexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation1d", ATTEN1DSIZE, 1, (unsigned char *)data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCELINEAR, NULL);
1141         // 2D circle texture
1142         for (y = 0;y < ATTEN2DSIZE;y++)
1143                 for (x = 0;x < ATTEN2DSIZE;x++)
1144                         data[y*ATTEN2DSIZE+x] = R_Shadow_MakeTextures_SamplePoint(((x + 0.5f) * (2.0f / ATTEN2DSIZE) - 1.0f) * (1.0f / 0.9375), ((y + 0.5f) * (2.0f / ATTEN2DSIZE) - 1.0f) * (1.0f / 0.9375), 0);
1145         r_shadow_attenuation2dtexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation2d", ATTEN2DSIZE, ATTEN2DSIZE, (unsigned char *)data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCELINEAR, NULL);
1146         // 3D sphere texture
1147         if (r_shadow_texture3d.integer && gl_texture3d)
1148         {
1149                 for (z = 0;z < ATTEN3DSIZE;z++)
1150                         for (y = 0;y < ATTEN3DSIZE;y++)
1151                                 for (x = 0;x < ATTEN3DSIZE;x++)
1152                                         data[(z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x] = R_Shadow_MakeTextures_SamplePoint(((x + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375), ((y + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375), ((z + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375));
1153                 r_shadow_attenuation3dtexture = R_LoadTexture3D(r_shadow_texturepool, "attenuation3d", ATTEN3DSIZE, ATTEN3DSIZE, ATTEN3DSIZE, (unsigned char *)data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCELINEAR, NULL);
1154         }
1155         else
1156                 r_shadow_attenuation3dtexture = NULL;
1157         Mem_Free(data);
1158
1159         R_Shadow_MakeTextures_MakeCorona();
1160
1161         // Editor light sprites
1162         r_editlights_sprcursor = Draw_CachePic ("gfx/editlights/cursor");
1163         r_editlights_sprlight = Draw_CachePic ("gfx/editlights/light");
1164         r_editlights_sprnoshadowlight = Draw_CachePic ("gfx/editlights/noshadow");
1165         r_editlights_sprcubemaplight = Draw_CachePic ("gfx/editlights/cubemaplight");
1166         r_editlights_sprcubemapnoshadowlight = Draw_CachePic ("gfx/editlights/cubemapnoshadowlight");
1167         r_editlights_sprselection = Draw_CachePic ("gfx/editlights/selection");
1168 }
1169
1170 void R_Shadow_ValidateCvars(void)
1171 {
1172         if (r_shadow_texture3d.integer && !gl_texture3d)
1173                 Cvar_SetValueQuick(&r_shadow_texture3d, 0);
1174         if (gl_ext_separatestencil.integer && !gl_support_separatestencil)
1175                 Cvar_SetValueQuick(&gl_ext_separatestencil, 0);
1176         if (gl_ext_stenciltwoside.integer && !gl_support_stenciltwoside)
1177                 Cvar_SetValueQuick(&gl_ext_stenciltwoside, 0);
1178 }
1179
1180 void R_Shadow_RenderMode_Begin(void)
1181 {
1182         R_Shadow_ValidateCvars();
1183
1184         if (!r_shadow_attenuation2dtexture
1185          || (!r_shadow_attenuation3dtexture && r_shadow_texture3d.integer)
1186          || r_shadow_lightattenuationdividebias.value != r_shadow_attendividebias
1187          || r_shadow_lightattenuationlinearscale.value != r_shadow_attenlinearscale)
1188                 R_Shadow_MakeTextures();
1189
1190         CHECKGLERROR
1191         R_Mesh_ColorPointer(NULL, 0, 0);
1192         R_Mesh_ResetTextureState();
1193         GL_BlendFunc(GL_ONE, GL_ZERO);
1194         GL_DepthRange(0, 1);
1195         GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
1196         GL_DepthTest(true);
1197         GL_DepthMask(false);
1198         GL_Color(0, 0, 0, 1);
1199         GL_Scissor(r_refdef.view.x, r_refdef.view.y, r_refdef.view.width, r_refdef.view.height);
1200
1201         r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
1202
1203         if (gl_ext_separatestencil.integer)
1204         {
1205                 r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_SEPARATESTENCIL;
1206                 r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_SEPARATESTENCIL;
1207         }
1208         else if (gl_ext_stenciltwoside.integer)
1209         {
1210                 r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE;
1211                 r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE;
1212         }
1213         else
1214         {
1215                 r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_STENCIL;
1216                 r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_STENCIL;
1217         }
1218
1219         if (r_glsl.integer && gl_support_fragment_shader)
1220                 r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_GLSL;
1221         else if (gl_dot3arb && gl_texturecubemap && r_textureunits.integer >= 2 && gl_combine.integer && gl_stencil)
1222                 r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_DOT3;
1223         else
1224                 r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_VERTEX;
1225 }
1226
1227 void R_Shadow_RenderMode_ActiveLight(const rtlight_t *rtlight)
1228 {
1229         rsurface.rtlight = rtlight;
1230 }
1231
1232 void R_Shadow_RenderMode_Reset(void)
1233 {
1234         CHECKGLERROR
1235         if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE || r_shadow_rendermode == R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE)
1236         {
1237                 qglDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
1238         }
1239         R_Mesh_ColorPointer(NULL, 0, 0);
1240         R_Mesh_ResetTextureState();
1241         GL_DepthRange(0, 1);
1242         GL_DepthTest(true);
1243         GL_DepthMask(false);
1244         qglDepthFunc(GL_LEQUAL);CHECKGLERROR
1245         GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);CHECKGLERROR
1246         qglDisable(GL_STENCIL_TEST);CHECKGLERROR
1247         qglStencilMask(~0);CHECKGLERROR
1248         qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);CHECKGLERROR
1249         qglStencilFunc(GL_ALWAYS, 128, ~0);CHECKGLERROR
1250         GL_CullFace(r_refdef.view.cullface_back);
1251         GL_Color(1, 1, 1, 1);
1252         GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
1253         GL_BlendFunc(GL_ONE, GL_ZERO);
1254         R_SetupGenericShader(false);
1255 }
1256
1257 void R_Shadow_ClearStencil(void)
1258 {
1259         CHECKGLERROR
1260         GL_Clear(GL_STENCIL_BUFFER_BIT);
1261         r_refdef.stats.lights_clears++;
1262 }
1263
1264 void R_Shadow_RenderMode_StencilShadowVolumes(qboolean zpass)
1265 {
1266         r_shadow_rendermode_t mode = zpass ? r_shadow_shadowingrendermode_zpass : r_shadow_shadowingrendermode_zfail;
1267         if (r_shadow_rendermode == mode)
1268                 return;
1269         CHECKGLERROR
1270         R_Shadow_RenderMode_Reset();
1271         GL_ColorMask(0, 0, 0, 0);
1272         GL_PolygonOffset(r_refdef.shadowpolygonfactor, r_refdef.shadowpolygonoffset);CHECKGLERROR
1273         R_SetupDepthOrShadowShader();
1274         qglDepthFunc(GL_LESS);CHECKGLERROR
1275         qglEnable(GL_STENCIL_TEST);CHECKGLERROR
1276         r_shadow_rendermode = mode;
1277         switch(mode)
1278         {
1279         default:
1280                 break;
1281         case R_SHADOW_RENDERMODE_ZPASS_SEPARATESTENCIL:
1282                 GL_CullFace(GL_NONE);
1283                 qglStencilOpSeparate(r_refdef.view.cullface_front, GL_KEEP, GL_KEEP, GL_INCR);CHECKGLERROR
1284                 qglStencilOpSeparate(r_refdef.view.cullface_back, GL_KEEP, GL_KEEP, GL_DECR);CHECKGLERROR
1285                 break;
1286         case R_SHADOW_RENDERMODE_ZFAIL_SEPARATESTENCIL:
1287                 GL_CullFace(GL_NONE);
1288                 qglStencilOpSeparate(r_refdef.view.cullface_front, GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR
1289                 qglStencilOpSeparate(r_refdef.view.cullface_back, GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
1290                 break;
1291         case R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE:
1292                 GL_CullFace(GL_NONE);
1293                 qglEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
1294                 qglActiveStencilFaceEXT(r_refdef.view.cullface_front);CHECKGLERROR
1295                 qglStencilMask(~0);CHECKGLERROR
1296                 qglStencilOp(GL_KEEP, GL_KEEP, GL_INCR);CHECKGLERROR
1297                 qglActiveStencilFaceEXT(r_refdef.view.cullface_back);CHECKGLERROR
1298                 qglStencilMask(~0);CHECKGLERROR
1299                 qglStencilOp(GL_KEEP, GL_KEEP, GL_DECR);CHECKGLERROR
1300                 break;
1301         case R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE:
1302                 GL_CullFace(GL_NONE);
1303                 qglEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
1304                 qglActiveStencilFaceEXT(r_refdef.view.cullface_front);CHECKGLERROR
1305                 qglStencilMask(~0);CHECKGLERROR
1306                 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR
1307                 qglActiveStencilFaceEXT(r_refdef.view.cullface_back);CHECKGLERROR
1308                 qglStencilMask(~0);CHECKGLERROR
1309                 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
1310                 break;
1311         }
1312 }
1313
1314 void R_Shadow_RenderMode_Lighting(qboolean stenciltest, qboolean transparent)
1315 {
1316         CHECKGLERROR
1317         R_Shadow_RenderMode_Reset();
1318         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1319         if (!transparent)
1320         {
1321                 qglDepthFunc(GL_EQUAL);CHECKGLERROR
1322         }
1323         if (stenciltest)
1324         {
1325                 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
1326                 // only draw light where this geometry was already rendered AND the
1327                 // stencil is 128 (values other than this mean shadow)
1328                 qglStencilFunc(GL_EQUAL, 128, ~0);CHECKGLERROR
1329         }
1330         r_shadow_rendermode = r_shadow_lightingrendermode;
1331         // do global setup needed for the chosen lighting mode
1332         if (r_shadow_rendermode == R_SHADOW_RENDERMODE_LIGHT_GLSL)
1333         {
1334                 R_Mesh_TexBindCubeMap(GL20TU_CUBE, R_GetTexture(rsurface.rtlight->currentcubemap)); // light filter
1335                 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 0);
1336         }
1337         else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_LIGHT_VERTEX)
1338                 R_Mesh_ColorPointer(rsurface.array_color4f, 0, 0);
1339         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1340 }
1341
1342 void R_Shadow_RenderMode_VisibleShadowVolumes(void)
1343 {
1344         CHECKGLERROR
1345         R_Shadow_RenderMode_Reset();
1346         GL_BlendFunc(GL_ONE, GL_ONE);
1347         GL_DepthRange(0, 1);
1348         GL_DepthTest(r_showshadowvolumes.integer < 2);
1349         GL_Color(0.0, 0.0125 * r_refdef.view.colorscale, 0.1 * r_refdef.view.colorscale, 1);
1350         GL_PolygonOffset(r_refdef.shadowpolygonfactor, r_refdef.shadowpolygonoffset);CHECKGLERROR
1351         GL_CullFace(GL_NONE);
1352         r_shadow_rendermode = R_SHADOW_RENDERMODE_VISIBLEVOLUMES;
1353 }
1354
1355 void R_Shadow_RenderMode_VisibleLighting(qboolean stenciltest, qboolean transparent)
1356 {
1357         CHECKGLERROR
1358         R_Shadow_RenderMode_Reset();
1359         GL_BlendFunc(GL_ONE, GL_ONE);
1360         GL_DepthRange(0, 1);
1361         GL_DepthTest(r_showlighting.integer < 2);
1362         GL_Color(0.1 * r_refdef.view.colorscale, 0.0125 * r_refdef.view.colorscale, 0, 1);
1363         if (!transparent)
1364         {
1365                 qglDepthFunc(GL_EQUAL);CHECKGLERROR
1366         }
1367         if (stenciltest)
1368         {
1369                 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
1370                 qglStencilFunc(GL_EQUAL, 128, ~0);CHECKGLERROR
1371         }
1372         r_shadow_rendermode = R_SHADOW_RENDERMODE_VISIBLELIGHTING;
1373 }
1374
1375 void R_Shadow_RenderMode_End(void)
1376 {
1377         CHECKGLERROR
1378         R_Shadow_RenderMode_Reset();
1379         R_Shadow_RenderMode_ActiveLight(NULL);
1380         GL_DepthMask(true);
1381         GL_Scissor(r_refdef.view.x, r_refdef.view.y, r_refdef.view.width, r_refdef.view.height);
1382         r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
1383 }
1384
1385 int bboxedges[12][2] =
1386 {
1387         // top
1388         {0, 1}, // +X
1389         {0, 2}, // +Y
1390         {1, 3}, // Y, +X
1391         {2, 3}, // X, +Y
1392         // bottom
1393         {4, 5}, // +X
1394         {4, 6}, // +Y
1395         {5, 7}, // Y, +X
1396         {6, 7}, // X, +Y
1397         // verticals
1398         {0, 4}, // +Z
1399         {1, 5}, // X, +Z
1400         {2, 6}, // Y, +Z
1401         {3, 7}, // XY, +Z
1402 };
1403
1404 qboolean R_Shadow_ScissorForBBox(const float *mins, const float *maxs)
1405 {
1406         int i, ix1, iy1, ix2, iy2;
1407         float x1, y1, x2, y2;
1408         vec4_t v, v2;
1409         float vertex[20][3];
1410         int j, k;
1411         vec4_t plane4f;
1412         int numvertices;
1413         float corner[8][4];
1414         float dist[8];
1415         int sign[8];
1416         float f;
1417
1418         if (!r_shadow_scissor.integer)
1419                 return false;
1420
1421         // if view is inside the light box, just say yes it's visible
1422         if (BoxesOverlap(r_refdef.view.origin, r_refdef.view.origin, mins, maxs))
1423         {
1424                 GL_Scissor(r_refdef.view.x, r_refdef.view.y, r_refdef.view.width, r_refdef.view.height);
1425                 return false;
1426         }
1427
1428         x1 = y1 = x2 = y2 = 0;
1429
1430         // transform all corners that are infront of the nearclip plane
1431         VectorNegate(r_refdef.view.frustum[4].normal, plane4f);
1432         plane4f[3] = r_refdef.view.frustum[4].dist;
1433         numvertices = 0;
1434         for (i = 0;i < 8;i++)
1435         {
1436                 Vector4Set(corner[i], (i & 1) ? maxs[0] : mins[0], (i & 2) ? maxs[1] : mins[1], (i & 4) ? maxs[2] : mins[2], 1);
1437                 dist[i] = DotProduct4(corner[i], plane4f);
1438                 sign[i] = dist[i] > 0;
1439                 if (!sign[i])
1440                 {
1441                         VectorCopy(corner[i], vertex[numvertices]);
1442                         numvertices++;
1443                 }
1444         }
1445         // if some points are behind the nearclip, add clipped edge points to make
1446         // sure that the scissor boundary is complete
1447         if (numvertices > 0 && numvertices < 8)
1448         {
1449                 // add clipped edge points
1450                 for (i = 0;i < 12;i++)
1451                 {
1452                         j = bboxedges[i][0];
1453                         k = bboxedges[i][1];
1454                         if (sign[j] != sign[k])
1455                         {
1456                                 f = dist[j] / (dist[j] - dist[k]);
1457                                 VectorLerp(corner[j], f, corner[k], vertex[numvertices]);
1458                                 numvertices++;
1459                         }
1460                 }
1461         }
1462
1463         // if we have no points to check, the light is behind the view plane
1464         if (!numvertices)
1465                 return true;
1466
1467         // if we have some points to transform, check what screen area is covered
1468         x1 = y1 = x2 = y2 = 0;
1469         v[3] = 1.0f;
1470         //Con_Printf("%i vertices to transform...\n", numvertices);
1471         for (i = 0;i < numvertices;i++)
1472         {
1473                 VectorCopy(vertex[i], v);
1474                 GL_TransformToScreen(v, v2);
1475                 //Con_Printf("%.3f %.3f %.3f %.3f transformed to %.3f %.3f %.3f %.3f\n", v[0], v[1], v[2], v[3], v2[0], v2[1], v2[2], v2[3]);
1476                 if (i)
1477                 {
1478                         if (x1 > v2[0]) x1 = v2[0];
1479                         if (x2 < v2[0]) x2 = v2[0];
1480                         if (y1 > v2[1]) y1 = v2[1];
1481                         if (y2 < v2[1]) y2 = v2[1];
1482                 }
1483                 else
1484                 {
1485                         x1 = x2 = v2[0];
1486                         y1 = y2 = v2[1];
1487                 }
1488         }
1489
1490         // now convert the scissor rectangle to integer screen coordinates
1491         ix1 = (int)(x1 - 1.0f);
1492         iy1 = (int)(y1 - 1.0f);
1493         ix2 = (int)(x2 + 1.0f);
1494         iy2 = (int)(y2 + 1.0f);
1495         //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
1496
1497         // clamp it to the screen
1498         if (ix1 < r_refdef.view.x) ix1 = r_refdef.view.x;
1499         if (iy1 < r_refdef.view.y) iy1 = r_refdef.view.y;
1500         if (ix2 > r_refdef.view.x + r_refdef.view.width) ix2 = r_refdef.view.x + r_refdef.view.width;
1501         if (iy2 > r_refdef.view.y + r_refdef.view.height) iy2 = r_refdef.view.y + r_refdef.view.height;
1502
1503         // if it is inside out, it's not visible
1504         if (ix2 <= ix1 || iy2 <= iy1)
1505                 return true;
1506
1507         // the light area is visible, set up the scissor rectangle
1508         GL_Scissor(ix1, iy1, ix2 - ix1, iy2 - iy1);
1509         //qglScissor(ix1, iy1, ix2 - ix1, iy2 - iy1);CHECKGLERROR
1510         //qglEnable(GL_SCISSOR_TEST);CHECKGLERROR
1511         r_refdef.stats.lights_scissored++;
1512         return false;
1513 }
1514
1515 static void R_Shadow_RenderLighting_Light_Vertex_Shading(int firstvertex, int numverts, int numtriangles, const int *element3i, const float *diffusecolor, const float *ambientcolor)
1516 {
1517         float *vertex3f = rsurface.vertex3f + 3 * firstvertex;
1518         float *normal3f = rsurface.normal3f + 3 * firstvertex;
1519         float *color4f = rsurface.array_color4f + 4 * firstvertex;
1520         float dist, dot, distintensity, shadeintensity, v[3], n[3];
1521         if (r_textureunits.integer >= 3)
1522         {
1523                 if (VectorLength2(diffusecolor) > 0)
1524                 {
1525                         for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
1526                         {
1527                                 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
1528                                 Matrix4x4_Transform3x3(&rsurface.entitytolight, normal3f, n);
1529                                 if ((dot = DotProduct(n, v)) < 0)
1530                                 {
1531                                         shadeintensity = -dot / sqrt(VectorLength2(v) * VectorLength2(n));
1532                                         VectorMA(ambientcolor, shadeintensity, diffusecolor, color4f);
1533                                 }
1534                                 else
1535                                         VectorCopy(ambientcolor, color4f);
1536                                 if (r_refdef.fogenabled)
1537                                 {
1538                                         float f;
1539                                         f = FogPoint_Model(vertex3f);
1540                                         VectorScale(color4f, f, color4f);
1541                                 }
1542                                 color4f[3] = 1;
1543                         }
1544                 }
1545                 else
1546                 {
1547                         for (;numverts > 0;numverts--, vertex3f += 3, color4f += 4)
1548                         {
1549                                 VectorCopy(ambientcolor, color4f);
1550                                 if (r_refdef.fogenabled)
1551                                 {
1552                                         float f;
1553                                         Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
1554                                         f = FogPoint_Model(vertex3f);
1555                                         VectorScale(color4f, f, color4f);
1556                                 }
1557                                 color4f[3] = 1;
1558                         }
1559                 }
1560         }
1561         else if (r_textureunits.integer >= 2)
1562         {
1563                 if (VectorLength2(diffusecolor) > 0)
1564                 {
1565                         for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
1566                         {
1567                                 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
1568                                 if ((dist = fabs(v[2])) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
1569                                 {
1570                                         Matrix4x4_Transform3x3(&rsurface.entitytolight, normal3f, n);
1571                                         if ((dot = DotProduct(n, v)) < 0)
1572                                         {
1573                                                 shadeintensity = -dot / sqrt(VectorLength2(v) * VectorLength2(n));
1574                                                 color4f[0] = (ambientcolor[0] + shadeintensity * diffusecolor[0]) * distintensity;
1575                                                 color4f[1] = (ambientcolor[1] + shadeintensity * diffusecolor[1]) * distintensity;
1576                                                 color4f[2] = (ambientcolor[2] + shadeintensity * diffusecolor[2]) * distintensity;
1577                                         }
1578                                         else
1579                                         {
1580                                                 color4f[0] = ambientcolor[0] * distintensity;
1581                                                 color4f[1] = ambientcolor[1] * distintensity;
1582                                                 color4f[2] = ambientcolor[2] * distintensity;
1583                                         }
1584                                         if (r_refdef.fogenabled)
1585                                         {
1586                                                 float f;
1587                                                 f = FogPoint_Model(vertex3f);
1588                                                 VectorScale(color4f, f, color4f);
1589                                         }
1590                                 }
1591                                 else
1592                                         VectorClear(color4f);
1593                                 color4f[3] = 1;
1594                         }
1595                 }
1596                 else
1597                 {
1598                         for (;numverts > 0;numverts--, vertex3f += 3, color4f += 4)
1599                         {
1600                                 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
1601                                 if ((dist = fabs(v[2])) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
1602                                 {
1603                                         color4f[0] = ambientcolor[0] * distintensity;
1604                                         color4f[1] = ambientcolor[1] * distintensity;
1605                                         color4f[2] = ambientcolor[2] * distintensity;
1606                                         if (r_refdef.fogenabled)
1607                                         {
1608                                                 float f;
1609                                                 f = FogPoint_Model(vertex3f);
1610                                                 VectorScale(color4f, f, color4f);
1611                                         }
1612                                 }
1613                                 else
1614                                         VectorClear(color4f);
1615                                 color4f[3] = 1;
1616                         }
1617                 }
1618         }
1619         else
1620         {
1621                 if (VectorLength2(diffusecolor) > 0)
1622                 {
1623                         for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
1624                         {
1625                                 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
1626                                 if ((dist = VectorLength(v)) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
1627                                 {
1628                                         distintensity = (1 - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist);
1629                                         Matrix4x4_Transform3x3(&rsurface.entitytolight, normal3f, n);
1630                                         if ((dot = DotProduct(n, v)) < 0)
1631                                         {
1632                                                 shadeintensity = -dot / sqrt(VectorLength2(v) * VectorLength2(n));
1633                                                 color4f[0] = (ambientcolor[0] + shadeintensity * diffusecolor[0]) * distintensity;
1634                                                 color4f[1] = (ambientcolor[1] + shadeintensity * diffusecolor[1]) * distintensity;
1635                                                 color4f[2] = (ambientcolor[2] + shadeintensity * diffusecolor[2]) * distintensity;
1636                                         }
1637                                         else
1638                                         {
1639                                                 color4f[0] = ambientcolor[0] * distintensity;
1640                                                 color4f[1] = ambientcolor[1] * distintensity;
1641                                                 color4f[2] = ambientcolor[2] * distintensity;
1642                                         }
1643                                         if (r_refdef.fogenabled)
1644                                         {
1645                                                 float f;
1646                                                 f = FogPoint_Model(vertex3f);
1647                                                 VectorScale(color4f, f, color4f);
1648                                         }
1649                                 }
1650                                 else
1651                                         VectorClear(color4f);
1652                                 color4f[3] = 1;
1653                         }
1654                 }
1655                 else
1656                 {
1657                         for (;numverts > 0;numverts--, vertex3f += 3, color4f += 4)
1658                         {
1659                                 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
1660                                 if ((dist = VectorLength(v)) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
1661                                 {
1662                                         distintensity = (1 - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist);
1663                                         color4f[0] = ambientcolor[0] * distintensity;
1664                                         color4f[1] = ambientcolor[1] * distintensity;
1665                                         color4f[2] = ambientcolor[2] * distintensity;
1666                                         if (r_refdef.fogenabled)
1667                                         {
1668                                                 float f;
1669                                                 f = FogPoint_Model(vertex3f);
1670                                                 VectorScale(color4f, f, color4f);
1671                                         }
1672                                 }
1673                                 else
1674                                         VectorClear(color4f);
1675                                 color4f[3] = 1;
1676                         }
1677                 }
1678         }
1679 }
1680
1681 // TODO: use glTexGen instead of feeding vertices to texcoordpointer?
1682
1683 static void R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(int firstvertex, int numvertices, int numtriangles, const int *element3i)
1684 {
1685         int i;
1686         float       *out3f     = rsurface.array_texcoord3f + 3 * firstvertex;
1687         const float *vertex3f  = rsurface.vertex3f         + 3 * firstvertex;
1688         const float *svector3f = rsurface.svector3f        + 3 * firstvertex;
1689         const float *tvector3f = rsurface.tvector3f        + 3 * firstvertex;
1690         const float *normal3f  = rsurface.normal3f         + 3 * firstvertex;
1691         float lightdir[3];
1692         for (i = 0;i < numvertices;i++, vertex3f += 3, svector3f += 3, tvector3f += 3, normal3f += 3, out3f += 3)
1693         {
1694                 VectorSubtract(rsurface.entitylightorigin, vertex3f, lightdir);
1695                 // the cubemap normalizes this for us
1696                 out3f[0] = DotProduct(svector3f, lightdir);
1697                 out3f[1] = DotProduct(tvector3f, lightdir);
1698                 out3f[2] = DotProduct(normal3f, lightdir);
1699         }
1700 }
1701
1702 static void R_Shadow_GenTexCoords_Specular_NormalCubeMap(int firstvertex, int numvertices, int numtriangles, const int *element3i)
1703 {
1704         int i;
1705         float       *out3f     = rsurface.array_texcoord3f + 3 * firstvertex;
1706         const float *vertex3f  = rsurface.vertex3f         + 3 * firstvertex;
1707         const float *svector3f = rsurface.svector3f        + 3 * firstvertex;
1708         const float *tvector3f = rsurface.tvector3f        + 3 * firstvertex;
1709         const float *normal3f  = rsurface.normal3f         + 3 * firstvertex;
1710         float lightdir[3], eyedir[3], halfdir[3];
1711         for (i = 0;i < numvertices;i++, vertex3f += 3, svector3f += 3, tvector3f += 3, normal3f += 3, out3f += 3)
1712         {
1713                 VectorSubtract(rsurface.entitylightorigin, vertex3f, lightdir);
1714                 VectorNormalize(lightdir);
1715                 VectorSubtract(rsurface.modelorg, vertex3f, eyedir);
1716                 VectorNormalize(eyedir);
1717                 VectorAdd(lightdir, eyedir, halfdir);
1718                 // the cubemap normalizes this for us
1719                 out3f[0] = DotProduct(svector3f, halfdir);
1720                 out3f[1] = DotProduct(tvector3f, halfdir);
1721                 out3f[2] = DotProduct(normal3f, halfdir);
1722         }
1723 }
1724
1725 static void R_Shadow_RenderLighting_VisibleLighting(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int element3i_bufferobject, int element3s_bufferobject, const vec3_t lightcolorbase, const vec3_t lightcolorpants, const vec3_t lightcolorshirt, rtexture_t *basetexture, rtexture_t *pantstexture, rtexture_t *shirttexture, rtexture_t *normalmaptexture, rtexture_t *glosstexture, float ambientscale, float diffusescale, float specularscale, qboolean dopants, qboolean doshirt)
1726 {
1727         // used to display how many times a surface is lit for level design purposes
1728         R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
1729 }
1730
1731 static void R_Shadow_RenderLighting_Light_GLSL(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int element3i_bufferobject, int element3s_bufferobject, const vec3_t lightcolorbase, const vec3_t lightcolorpants, const vec3_t lightcolorshirt, rtexture_t *basetexture, rtexture_t *pantstexture, rtexture_t *shirttexture, rtexture_t *normalmaptexture, rtexture_t *glosstexture, float ambientscale, float diffusescale, float specularscale, qboolean dopants, qboolean doshirt)
1732 {
1733         // ARB2 GLSL shader path (GFFX5200, Radeon 9500)
1734         R_SetupSurfaceShader(lightcolorbase, false, ambientscale, diffusescale, specularscale, RSURFPASS_RTLIGHT);
1735         if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND))
1736                 R_Mesh_ColorPointer(rsurface.modellightmapcolor4f, rsurface.modellightmapcolor4f_bufferobject, rsurface.modellightmapcolor4f_bufferoffset);
1737         else
1738                 R_Mesh_ColorPointer(NULL, 0, 0);
1739         R_Mesh_TexMatrix(0, &rsurface.texture->currenttexmatrix);
1740         R_Mesh_TexBind(GL20TU_NORMAL, R_GetTexture(rsurface.texture->currentskinframe->nmap));
1741         R_Mesh_TexBind(GL20TU_COLOR, R_GetTexture(rsurface.texture->basetexture));
1742         R_Mesh_TexBind(GL20TU_GLOSS, R_GetTexture(rsurface.texture->glosstexture));
1743         if (rsurface.texture->backgroundcurrentskinframe)
1744         {
1745                 R_Mesh_TexBind(GL20TU_SECONDARY_NORMAL, R_GetTexture(rsurface.texture->backgroundcurrentskinframe->nmap));
1746                 R_Mesh_TexBind(GL20TU_SECONDARY_COLOR, R_GetTexture(rsurface.texture->backgroundbasetexture));
1747                 R_Mesh_TexBind(GL20TU_SECONDARY_GLOSS, R_GetTexture(rsurface.texture->backgroundglosstexture));
1748         }
1749         //R_Mesh_TexBindCubeMap(GL20TU_CUBE, R_GetTexture(rsurface.rtlight->currentcubemap));
1750         R_Mesh_TexBind(GL20TU_FOGMASK, R_GetTexture(r_texture_fogattenuation));
1751         if(rsurface.texture->colormapping)
1752         {
1753                 R_Mesh_TexBind(GL20TU_PANTS, R_GetTexture(rsurface.texture->currentskinframe->pants));
1754                 R_Mesh_TexBind(GL20TU_SHIRT, R_GetTexture(rsurface.texture->currentskinframe->shirt));
1755         }
1756         R_Mesh_TexBind(GL20TU_ATTENUATION, R_GetTexture(r_shadow_attenuationgradienttexture));
1757         R_Mesh_TexCoordPointer(0, 2, rsurface.texcoordtexture2f, rsurface.texcoordtexture2f_bufferobject, rsurface.texcoordtexture2f_bufferoffset);
1758         R_Mesh_TexCoordPointer(1, 3, rsurface.svector3f, rsurface.svector3f_bufferobject, rsurface.svector3f_bufferoffset);
1759         R_Mesh_TexCoordPointer(2, 3, rsurface.tvector3f, rsurface.tvector3f_bufferobject, rsurface.tvector3f_bufferoffset);
1760         R_Mesh_TexCoordPointer(3, 3, rsurface.normal3f, rsurface.normal3f_bufferobject, rsurface.normal3f_bufferoffset);
1761         if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
1762         {
1763                 qglDepthFunc(GL_EQUAL);CHECKGLERROR
1764         }
1765         R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
1766         if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
1767         {
1768                 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
1769         }
1770 }
1771
1772 static void R_Shadow_RenderLighting_Light_Dot3_Finalize(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int element3i_bufferobject, int element3s_bufferobject, float r, float g, float b)
1773 {
1774         // shared final code for all the dot3 layers
1775         int renders;
1776         GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 0);
1777         for (renders = 0;renders < 64 && (r > 0 || g > 0 || b > 0);renders++, r--, g--, b--)
1778         {
1779                 GL_Color(bound(0, r, 1), bound(0, g, 1), bound(0, b, 1), 1);
1780                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
1781         }
1782 }
1783
1784 static void R_Shadow_RenderLighting_Light_Dot3_AmbientPass(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int element3i_bufferobject, int element3s_bufferobject, const vec3_t lightcolorbase, rtexture_t *basetexture, float colorscale)
1785 {
1786         rmeshstate_t m;
1787         // colorscale accounts for how much we multiply the brightness
1788         // during combine.
1789         //
1790         // mult is how many times the final pass of the lighting will be
1791         // performed to get more brightness than otherwise possible.
1792         //
1793         // Limit mult to 64 for sanity sake.
1794         GL_Color(1,1,1,1);
1795         if (r_shadow_texture3d.integer && rsurface.rtlight->currentcubemap != r_texture_whitecube && r_textureunits.integer >= 4)
1796         {
1797                 // 3 3D combine path (Geforce3, Radeon 8500)
1798                 memset(&m, 0, sizeof(m));
1799                 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
1800                 m.pointer_texcoord3f[0] = rsurface.vertex3f;
1801                 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
1802                 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
1803                 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
1804                 m.tex[1] = R_GetTexture(basetexture);
1805                 m.pointer_texcoord[1] = rsurface.texcoordtexture2f;
1806                 m.pointer_texcoord_bufferobject[1] = rsurface.texcoordtexture2f_bufferobject;
1807                 m.pointer_texcoord_bufferoffset[1] = rsurface.texcoordtexture2f_bufferoffset;
1808                 m.texmatrix[1] = rsurface.texture->currenttexmatrix;
1809                 m.texcubemap[2] = R_GetTexture(rsurface.rtlight->currentcubemap);
1810                 m.pointer_texcoord3f[2] = rsurface.vertex3f;
1811                 m.pointer_texcoord_bufferobject[2] = rsurface.vertex3f_bufferobject;
1812                 m.pointer_texcoord_bufferoffset[2] = rsurface.vertex3f_bufferoffset;
1813                 m.texmatrix[2] = rsurface.entitytolight;
1814                 GL_BlendFunc(GL_ONE, GL_ONE);
1815         }
1816         else if (r_shadow_texture3d.integer && rsurface.rtlight->currentcubemap == r_texture_whitecube && r_textureunits.integer >= 2)
1817         {
1818                 // 2 3D combine path (Geforce3, original Radeon)
1819                 memset(&m, 0, sizeof(m));
1820                 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
1821                 m.pointer_texcoord3f[0] = rsurface.vertex3f;
1822                 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
1823                 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
1824                 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
1825                 m.tex[1] = R_GetTexture(basetexture);
1826                 m.pointer_texcoord[1] = rsurface.texcoordtexture2f;
1827                 m.pointer_texcoord_bufferobject[1] = rsurface.texcoordtexture2f_bufferobject;
1828                 m.pointer_texcoord_bufferoffset[1] = rsurface.texcoordtexture2f_bufferoffset;
1829                 m.texmatrix[1] = rsurface.texture->currenttexmatrix;
1830                 GL_BlendFunc(GL_ONE, GL_ONE);
1831         }
1832         else if (r_textureunits.integer >= 4 && rsurface.rtlight->currentcubemap != r_texture_whitecube)
1833         {
1834                 // 4 2D combine path (Geforce3, Radeon 8500)
1835                 memset(&m, 0, sizeof(m));
1836                 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
1837                 m.pointer_texcoord3f[0] = rsurface.vertex3f;
1838                 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
1839                 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
1840                 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
1841                 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1842                 m.pointer_texcoord3f[1] = rsurface.vertex3f;
1843                 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
1844                 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
1845                 m.texmatrix[1] = rsurface.entitytoattenuationz;
1846                 m.tex[2] = R_GetTexture(basetexture);
1847                 m.pointer_texcoord[2] = rsurface.texcoordtexture2f;
1848                 m.pointer_texcoord_bufferobject[2] = rsurface.texcoordtexture2f_bufferobject;
1849                 m.pointer_texcoord_bufferoffset[2] = rsurface.texcoordtexture2f_bufferoffset;
1850                 m.texmatrix[2] = rsurface.texture->currenttexmatrix;
1851                 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
1852                 {
1853                         m.texcubemap[3] = R_GetTexture(rsurface.rtlight->currentcubemap);
1854                         m.pointer_texcoord3f[3] = rsurface.vertex3f;
1855                         m.pointer_texcoord_bufferobject[3] = rsurface.vertex3f_bufferobject;
1856                         m.pointer_texcoord_bufferoffset[3] = rsurface.vertex3f_bufferoffset;
1857                         m.texmatrix[3] = rsurface.entitytolight;
1858                 }
1859                 GL_BlendFunc(GL_ONE, GL_ONE);
1860         }
1861         else if (r_textureunits.integer >= 3 && rsurface.rtlight->currentcubemap == r_texture_whitecube)
1862         {
1863                 // 3 2D combine path (Geforce3, original Radeon)
1864                 memset(&m, 0, sizeof(m));
1865                 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
1866                 m.pointer_texcoord3f[0] = rsurface.vertex3f;
1867                 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
1868                 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
1869                 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
1870                 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1871                 m.pointer_texcoord3f[1] = rsurface.vertex3f;
1872                 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
1873                 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
1874                 m.texmatrix[1] = rsurface.entitytoattenuationz;
1875                 m.tex[2] = R_GetTexture(basetexture);
1876                 m.pointer_texcoord[2] = rsurface.texcoordtexture2f;
1877                 m.pointer_texcoord_bufferobject[2] = rsurface.texcoordtexture2f_bufferobject;
1878                 m.pointer_texcoord_bufferoffset[2] = rsurface.texcoordtexture2f_bufferoffset;
1879                 m.texmatrix[2] = rsurface.texture->currenttexmatrix;
1880                 GL_BlendFunc(GL_ONE, GL_ONE);
1881         }
1882         else
1883         {
1884                 // 2/2/2 2D combine path (any dot3 card)
1885                 memset(&m, 0, sizeof(m));
1886                 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
1887                 m.pointer_texcoord3f[0] = rsurface.vertex3f;
1888                 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
1889                 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
1890                 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
1891                 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1892                 m.pointer_texcoord3f[1] = rsurface.vertex3f;
1893                 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
1894                 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
1895                 m.texmatrix[1] = rsurface.entitytoattenuationz;
1896                 R_Mesh_TextureState(&m);
1897                 GL_ColorMask(0,0,0,1);
1898                 GL_BlendFunc(GL_ONE, GL_ZERO);
1899                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
1900
1901                 // second pass
1902                 memset(&m, 0, sizeof(m));
1903                 m.tex[0] = R_GetTexture(basetexture);
1904                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
1905                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
1906                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
1907                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
1908                 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
1909                 {
1910                         m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
1911                         m.pointer_texcoord3f[1] = rsurface.vertex3f;
1912                         m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
1913                         m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
1914                         m.texmatrix[1] = rsurface.entitytolight;
1915                 }
1916                 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
1917         }
1918         // this final code is shared
1919         R_Mesh_TextureState(&m);
1920         R_Shadow_RenderLighting_Light_Dot3_Finalize(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase[0] * colorscale, lightcolorbase[1] * colorscale, lightcolorbase[2] * colorscale);
1921 }
1922
1923 static void R_Shadow_RenderLighting_Light_Dot3_DiffusePass(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int element3i_bufferobject, int element3s_bufferobject, const vec3_t lightcolorbase, rtexture_t *basetexture, rtexture_t *normalmaptexture, float colorscale)
1924 {
1925         rmeshstate_t m;
1926         // colorscale accounts for how much we multiply the brightness
1927         // during combine.
1928         //
1929         // mult is how many times the final pass of the lighting will be
1930         // performed to get more brightness than otherwise possible.
1931         //
1932         // Limit mult to 64 for sanity sake.
1933         GL_Color(1,1,1,1);
1934         // generate normalization cubemap texcoords
1935         R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(firstvertex, numvertices, numtriangles, element3i);
1936         if (r_shadow_texture3d.integer && r_textureunits.integer >= 4)
1937         {
1938                 // 3/2 3D combine path (Geforce3, Radeon 8500)
1939                 memset(&m, 0, sizeof(m));
1940                 m.tex[0] = R_GetTexture(normalmaptexture);
1941                 m.texcombinergb[0] = GL_REPLACE;
1942                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
1943                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
1944                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
1945                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
1946                 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
1947                 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1948                 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
1949                 m.pointer_texcoord_bufferobject[1] = 0;
1950                 m.pointer_texcoord_bufferoffset[1] = 0;
1951                 m.tex3d[2] = R_GetTexture(r_shadow_attenuation3dtexture);
1952                 m.pointer_texcoord3f[2] = rsurface.vertex3f;
1953                 m.pointer_texcoord_bufferobject[2] = rsurface.vertex3f_bufferobject;
1954                 m.pointer_texcoord_bufferoffset[2] = rsurface.vertex3f_bufferoffset;
1955                 m.texmatrix[2] = rsurface.entitytoattenuationxyz;
1956                 R_Mesh_TextureState(&m);
1957                 GL_ColorMask(0,0,0,1);
1958                 GL_BlendFunc(GL_ONE, GL_ZERO);
1959                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
1960
1961                 // second pass
1962                 memset(&m, 0, sizeof(m));
1963                 m.tex[0] = R_GetTexture(basetexture);
1964                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
1965                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
1966                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
1967                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
1968                 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
1969                 {
1970                         m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
1971                         m.pointer_texcoord3f[1] = rsurface.vertex3f;
1972                         m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
1973                         m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
1974                         m.texmatrix[1] = rsurface.entitytolight;
1975                 }
1976                 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
1977         }
1978         else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && rsurface.rtlight->currentcubemap != r_texture_whitecube)
1979         {
1980                 // 1/2/2 3D combine path (original Radeon)
1981                 memset(&m, 0, sizeof(m));
1982                 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
1983                 m.pointer_texcoord3f[0] = rsurface.vertex3f;
1984                 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
1985                 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
1986                 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
1987                 R_Mesh_TextureState(&m);
1988                 GL_ColorMask(0,0,0,1);
1989                 GL_BlendFunc(GL_ONE, GL_ZERO);
1990                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
1991
1992                 // second pass
1993                 memset(&m, 0, sizeof(m));
1994                 m.tex[0] = R_GetTexture(normalmaptexture);
1995                 m.texcombinergb[0] = GL_REPLACE;
1996                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
1997                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
1998                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
1999                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2000                 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
2001                 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2002                 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
2003                 m.pointer_texcoord_bufferobject[1] = 0;
2004                 m.pointer_texcoord_bufferoffset[1] = 0;
2005                 R_Mesh_TextureState(&m);
2006                 GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
2007                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2008
2009                 // second pass
2010                 memset(&m, 0, sizeof(m));
2011                 m.tex[0] = R_GetTexture(basetexture);
2012                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2013                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2014                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2015                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2016                 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2017                 {
2018                         m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
2019                         m.pointer_texcoord3f[1] = rsurface.vertex3f;
2020                         m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2021                         m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2022                         m.texmatrix[1] = rsurface.entitytolight;
2023                 }
2024                 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2025         }
2026         else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && rsurface.rtlight->currentcubemap == r_texture_whitecube)
2027         {
2028                 // 2/2 3D combine path (original Radeon)
2029                 memset(&m, 0, sizeof(m));
2030                 m.tex[0] = R_GetTexture(normalmaptexture);
2031                 m.texcombinergb[0] = GL_REPLACE;
2032                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2033                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2034                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2035                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2036                 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
2037                 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2038                 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
2039                 m.pointer_texcoord_bufferobject[1] = 0;
2040                 m.pointer_texcoord_bufferoffset[1] = 0;
2041                 R_Mesh_TextureState(&m);
2042                 GL_ColorMask(0,0,0,1);
2043                 GL_BlendFunc(GL_ONE, GL_ZERO);
2044                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2045
2046                 // second pass
2047                 memset(&m, 0, sizeof(m));
2048                 m.tex[0] = R_GetTexture(basetexture);
2049                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2050                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2051                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2052                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2053                 m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
2054                 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2055                 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2056                 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2057                 m.texmatrix[1] = rsurface.entitytoattenuationxyz;
2058                 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2059         }
2060         else if (r_textureunits.integer >= 4)
2061         {
2062                 // 4/2 2D combine path (Geforce3, Radeon 8500)
2063                 memset(&m, 0, sizeof(m));
2064                 m.tex[0] = R_GetTexture(normalmaptexture);
2065                 m.texcombinergb[0] = GL_REPLACE;
2066                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2067                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2068                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2069                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2070                 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
2071                 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2072                 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
2073                 m.pointer_texcoord_bufferobject[1] = 0;
2074                 m.pointer_texcoord_bufferoffset[1] = 0;
2075                 m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
2076                 m.pointer_texcoord3f[2] = rsurface.vertex3f;
2077                 m.pointer_texcoord_bufferobject[2] = rsurface.vertex3f_bufferobject;
2078                 m.pointer_texcoord_bufferoffset[2] = rsurface.vertex3f_bufferoffset;
2079                 m.texmatrix[2] = rsurface.entitytoattenuationxyz;
2080                 m.tex[3] = R_GetTexture(r_shadow_attenuation2dtexture);
2081                 m.pointer_texcoord3f[3] = rsurface.vertex3f;
2082                 m.pointer_texcoord_bufferobject[3] = rsurface.vertex3f_bufferobject;
2083                 m.pointer_texcoord_bufferoffset[3] = rsurface.vertex3f_bufferoffset;
2084                 m.texmatrix[3] = rsurface.entitytoattenuationz;
2085                 R_Mesh_TextureState(&m);
2086                 GL_ColorMask(0,0,0,1);
2087                 GL_BlendFunc(GL_ONE, GL_ZERO);
2088                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2089
2090                 // second pass
2091                 memset(&m, 0, sizeof(m));
2092                 m.tex[0] = R_GetTexture(basetexture);
2093                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2094                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2095                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2096                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2097                 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2098                 {
2099                         m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
2100                         m.pointer_texcoord3f[1] = rsurface.vertex3f;
2101                         m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2102                         m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2103                         m.texmatrix[1] = rsurface.entitytolight;
2104                 }
2105                 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2106         }
2107         else
2108         {
2109                 // 2/2/2 2D combine path (any dot3 card)
2110                 memset(&m, 0, sizeof(m));
2111                 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
2112                 m.pointer_texcoord3f[0] = rsurface.vertex3f;
2113                 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
2114                 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
2115                 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
2116                 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
2117                 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2118                 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
2119                 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
2120                 m.texmatrix[1] = rsurface.entitytoattenuationz;
2121                 R_Mesh_TextureState(&m);
2122                 GL_ColorMask(0,0,0,1);
2123                 GL_BlendFunc(GL_ONE, GL_ZERO);
2124                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2125
2126                 // second pass
2127                 memset(&m, 0, sizeof(m));
2128                 m.tex[0] = R_GetTexture(normalmaptexture);
2129                 m.texcombinergb[0] = GL_REPLACE;
2130                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2131                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2132                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2133                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2134                 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
2135                 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2136                 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
2137                 m.pointer_texcoord_bufferobject[1] = 0;
2138                 m.pointer_texcoord_bufferoffset[1] = 0;
2139                 R_Mesh_TextureState(&m);
2140                 GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
2141                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2142
2143                 // second pass
2144                 memset(&m, 0, sizeof(m));
2145                 m.tex[0] = R_GetTexture(basetexture);
2146                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2147                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2148                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2149                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2150                 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2151                 {
2152                         m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
2153                         m.pointer_texcoord3f[1] = rsurface.vertex3f;
2154                         m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2155                         m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2156                         m.texmatrix[1] = rsurface.entitytolight;
2157                 }
2158                 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2159         }
2160         // this final code is shared
2161         R_Mesh_TextureState(&m);
2162         R_Shadow_RenderLighting_Light_Dot3_Finalize(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase[0] * colorscale, lightcolorbase[1] * colorscale, lightcolorbase[2] * colorscale);
2163 }
2164
2165 static void R_Shadow_RenderLighting_Light_Dot3_SpecularPass(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int element3i_bufferobject, int element3s_bufferobject, const vec3_t lightcolorbase, rtexture_t *glosstexture, rtexture_t *normalmaptexture, float colorscale)
2166 {
2167         float glossexponent;
2168         rmeshstate_t m;
2169         // FIXME: detect blendsquare!
2170         //if (!gl_support_blendsquare)
2171         //      return;
2172         GL_Color(1,1,1,1);
2173         // generate normalization cubemap texcoords
2174         R_Shadow_GenTexCoords_Specular_NormalCubeMap(firstvertex, numvertices, numtriangles, element3i);
2175         if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && rsurface.rtlight->currentcubemap != r_texture_whitecube)
2176         {
2177                 // 2/0/0/1/2 3D combine blendsquare path
2178                 memset(&m, 0, sizeof(m));
2179                 m.tex[0] = R_GetTexture(normalmaptexture);
2180                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2181                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2182                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2183                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2184                 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
2185                 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2186                 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
2187                 m.pointer_texcoord_bufferobject[1] = 0;
2188                 m.pointer_texcoord_bufferoffset[1] = 0;
2189                 R_Mesh_TextureState(&m);
2190                 GL_ColorMask(0,0,0,1);
2191                 // this squares the result
2192                 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
2193                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2194
2195                 // second and third pass
2196                 R_Mesh_ResetTextureState();
2197                 // square alpha in framebuffer a few times to make it shiny
2198                 GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
2199                 for (glossexponent = 2;glossexponent * 2 <= r_shadow_glossexponent.value;glossexponent *= 2)
2200                         R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2201
2202                 // fourth pass
2203                 memset(&m, 0, sizeof(m));
2204                 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
2205                 m.pointer_texcoord3f[0] = rsurface.vertex3f;
2206                 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
2207                 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
2208                 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
2209                 R_Mesh_TextureState(&m);
2210                 GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
2211                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2212
2213                 // fifth pass
2214                 memset(&m, 0, sizeof(m));
2215                 m.tex[0] = R_GetTexture(glosstexture);
2216                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2217                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2218                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2219                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2220                 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2221                 {
2222                         m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
2223                         m.pointer_texcoord3f[1] = rsurface.vertex3f;
2224                         m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2225                         m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2226                         m.texmatrix[1] = rsurface.entitytolight;
2227                 }
2228                 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2229         }
2230         else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && rsurface.rtlight->currentcubemap == r_texture_whitecube /* && gl_support_blendsquare*/) // FIXME: detect blendsquare!
2231         {
2232                 // 2/0/0/2 3D combine blendsquare path
2233                 memset(&m, 0, sizeof(m));
2234                 m.tex[0] = R_GetTexture(normalmaptexture);
2235                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2236                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2237                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2238                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2239                 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
2240                 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2241                 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
2242                 m.pointer_texcoord_bufferobject[1] = 0;
2243                 m.pointer_texcoord_bufferoffset[1] = 0;
2244                 R_Mesh_TextureState(&m);
2245                 GL_ColorMask(0,0,0,1);
2246                 // this squares the result
2247                 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
2248                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2249
2250                 // second and third pass
2251                 R_Mesh_ResetTextureState();
2252                 // square alpha in framebuffer a few times to make it shiny
2253                 GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
2254                 for (glossexponent = 2;glossexponent * 2 <= r_shadow_glossexponent.value;glossexponent *= 2)
2255                         R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2256
2257                 // fourth pass
2258                 memset(&m, 0, sizeof(m));
2259                 m.tex[0] = R_GetTexture(glosstexture);
2260                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2261                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2262                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2263                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2264                 m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
2265                 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2266                 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2267                 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2268                 m.texmatrix[1] = rsurface.entitytoattenuationxyz;
2269                 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2270         }
2271         else
2272         {
2273                 // 2/0/0/2/2 2D combine blendsquare path
2274                 memset(&m, 0, sizeof(m));
2275                 m.tex[0] = R_GetTexture(normalmaptexture);
2276                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2277                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2278                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2279                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2280                 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
2281                 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2282                 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
2283                 m.pointer_texcoord_bufferobject[1] = 0;
2284                 m.pointer_texcoord_bufferoffset[1] = 0;
2285                 R_Mesh_TextureState(&m);
2286                 GL_ColorMask(0,0,0,1);
2287                 // this squares the result
2288                 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
2289                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2290
2291                 // second and third pass
2292                 R_Mesh_ResetTextureState();
2293                 // square alpha in framebuffer a few times to make it shiny
2294                 GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
2295                 for (glossexponent = 2;glossexponent * 2 <= r_shadow_glossexponent.value;glossexponent *= 2)
2296                         R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2297
2298                 // fourth pass
2299                 memset(&m, 0, sizeof(m));
2300                 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
2301                 m.pointer_texcoord3f[0] = rsurface.vertex3f;
2302                 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
2303                 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
2304                 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
2305                 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
2306                 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2307                 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2308                 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2309                 m.texmatrix[1] = rsurface.entitytoattenuationz;
2310                 R_Mesh_TextureState(&m);
2311                 GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
2312                 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2313
2314                 // fifth pass
2315                 memset(&m, 0, sizeof(m));
2316                 m.tex[0] = R_GetTexture(glosstexture);
2317                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2318                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2319                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2320                 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2321                 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2322                 {
2323                         m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
2324                         m.pointer_texcoord3f[1] = rsurface.vertex3f;
2325                         m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2326                         m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2327                         m.texmatrix[1] = rsurface.entitytolight;
2328                 }
2329                 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2330         }
2331         // this final code is shared
2332         R_Mesh_TextureState(&m);
2333         R_Shadow_RenderLighting_Light_Dot3_Finalize(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase[0] * colorscale, lightcolorbase[1] * colorscale, lightcolorbase[2] * colorscale);
2334 }
2335
2336 static void R_Shadow_RenderLighting_Light_Dot3(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int element3i_bufferobject, int element3s_bufferobject, const vec3_t lightcolorbase, const vec3_t lightcolorpants, const vec3_t lightcolorshirt, rtexture_t *basetexture, rtexture_t *pantstexture, rtexture_t *shirttexture, rtexture_t *normalmaptexture, rtexture_t *glosstexture, float ambientscale, float diffusescale, float specularscale, qboolean dopants, qboolean doshirt)
2337 {
2338         // ARB path (any Geforce, any Radeon)
2339         qboolean doambient = ambientscale > 0;
2340         qboolean dodiffuse = diffusescale > 0;
2341         qboolean dospecular = specularscale > 0;
2342         if (!doambient && !dodiffuse && !dospecular)
2343                 return;
2344         R_Mesh_ColorPointer(NULL, 0, 0);
2345         if (doambient)
2346                 R_Shadow_RenderLighting_Light_Dot3_AmbientPass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, basetexture, ambientscale * r_refdef.view.colorscale);
2347         if (dodiffuse)
2348                 R_Shadow_RenderLighting_Light_Dot3_DiffusePass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, basetexture, normalmaptexture, diffusescale * r_refdef.view.colorscale);
2349         if (dopants)
2350         {
2351                 if (doambient)
2352                         R_Shadow_RenderLighting_Light_Dot3_AmbientPass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorpants, pantstexture, ambientscale * r_refdef.view.colorscale);
2353                 if (dodiffuse)
2354                         R_Shadow_RenderLighting_Light_Dot3_DiffusePass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorpants, pantstexture, normalmaptexture, diffusescale * r_refdef.view.colorscale);
2355         }
2356         if (doshirt)
2357         {
2358                 if (doambient)
2359                         R_Shadow_RenderLighting_Light_Dot3_AmbientPass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorshirt, shirttexture, ambientscale * r_refdef.view.colorscale);
2360                 if (dodiffuse)
2361                         R_Shadow_RenderLighting_Light_Dot3_DiffusePass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorshirt, shirttexture, normalmaptexture, diffusescale * r_refdef.view.colorscale);
2362         }
2363         if (dospecular)
2364                 R_Shadow_RenderLighting_Light_Dot3_SpecularPass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, glosstexture, normalmaptexture, specularscale * r_refdef.view.colorscale);
2365 }
2366
2367 static void R_Shadow_RenderLighting_Light_Vertex_Pass(int firstvertex, int numvertices, int numtriangles, const int *element3i, vec3_t diffusecolor2, vec3_t ambientcolor2)
2368 {
2369         int renders;
2370         int i;
2371         int stop;
2372         int newfirstvertex;
2373         int newlastvertex;
2374         int newnumtriangles;
2375         int *newe;
2376         const int *e;
2377         float *c;
2378         int maxtriangles = 4096;
2379         int newelements[4096*3];
2380         R_Shadow_RenderLighting_Light_Vertex_Shading(firstvertex, numvertices, numtriangles, element3i, diffusecolor2, ambientcolor2);
2381         for (renders = 0;renders < 64;renders++)
2382         {
2383                 stop = true;
2384                 newfirstvertex = 0;
2385                 newlastvertex = 0;
2386                 newnumtriangles = 0;
2387                 newe = newelements;
2388                 // due to low fillrate on the cards this vertex lighting path is
2389                 // designed for, we manually cull all triangles that do not
2390                 // contain a lit vertex
2391                 // this builds batches of triangles from multiple surfaces and
2392                 // renders them at once
2393                 for (i = 0, e = element3i;i < numtriangles;i++, e += 3)
2394                 {
2395                         if (VectorLength2(rsurface.array_color4f + e[0] * 4) + VectorLength2(rsurface.array_color4f + e[1] * 4) + VectorLength2(rsurface.array_color4f + e[2] * 4) >= 0.01)
2396                         {
2397                                 if (newnumtriangles)
2398                                 {
2399                                         newfirstvertex = min(newfirstvertex, e[0]);
2400                                         newlastvertex  = max(newlastvertex, e[0]);
2401                                 }
2402                                 else
2403                                 {
2404                                         newfirstvertex = e[0];
2405                                         newlastvertex = e[0];
2406                                 }
2407                                 newfirstvertex = min(newfirstvertex, e[1]);
2408                                 newlastvertex  = max(newlastvertex, e[1]);
2409                                 newfirstvertex = min(newfirstvertex, e[2]);
2410                                 newlastvertex  = max(newlastvertex, e[2]);
2411                                 newe[0] = e[0];
2412                                 newe[1] = e[1];
2413                                 newe[2] = e[2];
2414                                 newnumtriangles++;
2415                                 newe += 3;
2416                                 if (newnumtriangles >= maxtriangles)
2417                                 {
2418                                         R_Mesh_Draw(newfirstvertex, newlastvertex - newfirstvertex + 1, 0, newnumtriangles, newelements, NULL, 0, 0);
2419                                         newnumtriangles = 0;
2420                                         newe = newelements;
2421                                         stop = false;
2422                                 }
2423                         }
2424                 }
2425                 if (newnumtriangles >= 1)
2426                 {
2427                         R_Mesh_Draw(newfirstvertex, newlastvertex - newfirstvertex + 1, 0, newnumtriangles, newelements, NULL, 0, 0);
2428                         stop = false;
2429                 }
2430                 // if we couldn't find any lit triangles, exit early
2431                 if (stop)
2432                         break;
2433                 // now reduce the intensity for the next overbright pass
2434                 // we have to clamp to 0 here incase the drivers have improper
2435                 // handling of negative colors
2436                 // (some old drivers even have improper handling of >1 color)
2437                 stop = true;
2438                 for (i = 0, c = rsurface.array_color4f + 4 * firstvertex;i < numvertices;i++, c += 4)
2439                 {
2440                         if (c[0] > 1 || c[1] > 1 || c[2] > 1)
2441                         {
2442                                 c[0] = max(0, c[0] - 1);
2443                                 c[1] = max(0, c[1] - 1);
2444                                 c[2] = max(0, c[2] - 1);
2445                                 stop = false;
2446                         }
2447                         else
2448                                 VectorClear(c);
2449                 }
2450                 // another check...
2451                 if (stop)
2452                         break;
2453         }
2454 }
2455
2456 static void R_Shadow_RenderLighting_Light_Vertex(int firstvertex, int numvertices, int numtriangles, const int *element3i, const vec3_t lightcolorbase, const vec3_t lightcolorpants, const vec3_t lightcolorshirt, rtexture_t *basetexture, rtexture_t *pantstexture, rtexture_t *shirttexture, rtexture_t *normalmaptexture, rtexture_t *glosstexture, float ambientscale, float diffusescale, float specularscale, qboolean dopants, qboolean doshirt)
2457 {
2458         // OpenGL 1.1 path (anything)
2459         float ambientcolorbase[3], diffusecolorbase[3];
2460         float ambientcolorpants[3], diffusecolorpants[3];
2461         float ambientcolorshirt[3], diffusecolorshirt[3];
2462         rmeshstate_t m;
2463         VectorScale(lightcolorbase, ambientscale * 2 * r_refdef.view.colorscale, ambientcolorbase);
2464         VectorScale(lightcolorbase, diffusescale * 2 * r_refdef.view.colorscale, diffusecolorbase);
2465         VectorScale(lightcolorpants, ambientscale * 2 * r_refdef.view.colorscale, ambientcolorpants);
2466         VectorScale(lightcolorpants, diffusescale * 2 * r_refdef.view.colorscale, diffusecolorpants);
2467         VectorScale(lightcolorshirt, ambientscale * 2 * r_refdef.view.colorscale, ambientcolorshirt);
2468         VectorScale(lightcolorshirt, diffusescale * 2 * r_refdef.view.colorscale, diffusecolorshirt);
2469         memset(&m, 0, sizeof(m));
2470         m.tex[0] = R_GetTexture(basetexture);
2471         m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2472         m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2473         m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2474         m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2475         if (r_textureunits.integer >= 2)
2476         {
2477                 // voodoo2 or TNT
2478                 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
2479                 m.texmatrix[1] = rsurface.entitytoattenuationxyz;
2480                 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2481                 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2482                 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2483                 if (r_textureunits.integer >= 3)
2484                 {
2485                         // Voodoo4 or Kyro (or Geforce3/Radeon with gl_combine off)
2486                         m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
2487                         m.texmatrix[2] = rsurface.entitytoattenuationz;
2488                         m.pointer_texcoord3f[2] = rsurface.vertex3f;
2489                         m.pointer_texcoord_bufferobject[2] = rsurface.vertex3f_bufferobject;
2490                         m.pointer_texcoord_bufferoffset[2] = rsurface.vertex3f_bufferoffset;
2491                 }
2492         }
2493         R_Mesh_TextureState(&m);
2494         //R_Mesh_TexBind(0, R_GetTexture(basetexture));
2495         R_Shadow_RenderLighting_Light_Vertex_Pass(firstvertex, numvertices, numtriangles, element3i, diffusecolorbase, ambientcolorbase);
2496         if (dopants)
2497         {
2498                 R_Mesh_TexBind(0, R_GetTexture(pantstexture));
2499                 R_Shadow_RenderLighting_Light_Vertex_Pass(firstvertex, numvertices, numtriangles, element3i, diffusecolorpants, ambientcolorpants);
2500         }
2501         if (doshirt)
2502         {
2503                 R_Mesh_TexBind(0, R_GetTexture(shirttexture));
2504                 R_Shadow_RenderLighting_Light_Vertex_Pass(firstvertex, numvertices, numtriangles, element3i, diffusecolorshirt, ambientcolorshirt);
2505         }
2506 }
2507
2508 extern cvar_t gl_lightmaps;
2509 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)
2510 {
2511         float ambientscale, diffusescale, specularscale;
2512         vec3_t lightcolorbase, lightcolorpants, lightcolorshirt;
2513         rtexture_t *nmap;
2514         // calculate colors to render this texture with
2515         lightcolorbase[0] = rsurface.rtlight->currentcolor[0] * rsurface.texture->dlightcolor[0];
2516         lightcolorbase[1] = rsurface.rtlight->currentcolor[1] * rsurface.texture->dlightcolor[1];
2517         lightcolorbase[2] = rsurface.rtlight->currentcolor[2] * rsurface.texture->dlightcolor[2];
2518         ambientscale = rsurface.rtlight->ambientscale;
2519         diffusescale = rsurface.rtlight->diffusescale;
2520         specularscale = rsurface.rtlight->specularscale * rsurface.texture->specularscale;
2521         if (!r_shadow_usenormalmap.integer)
2522         {
2523                 ambientscale += 1.0f * diffusescale;
2524                 diffusescale = 0;
2525                 specularscale = 0;
2526         }
2527         if ((ambientscale + diffusescale) * VectorLength2(lightcolorbase) + specularscale * VectorLength2(lightcolorbase) < (1.0f / 1048576.0f))
2528                 return;
2529         RSurf_SetupDepthAndCulling();
2530         nmap = rsurface.texture->currentskinframe->nmap;
2531         if (gl_lightmaps.integer)
2532                 nmap = r_texture_blanknormalmap;
2533         if (rsurface.texture->colormapping && !gl_lightmaps.integer)
2534         {
2535                 qboolean dopants = rsurface.texture->currentskinframe->pants != NULL && VectorLength2(rsurface.colormap_pantscolor) >= (1.0f / 1048576.0f);
2536                 qboolean doshirt = rsurface.texture->currentskinframe->shirt != NULL && VectorLength2(rsurface.colormap_shirtcolor) >= (1.0f / 1048576.0f);
2537                 if (dopants)
2538                 {
2539                         lightcolorpants[0] = lightcolorbase[0] * rsurface.colormap_pantscolor[0];
2540                         lightcolorpants[1] = lightcolorbase[1] * rsurface.colormap_pantscolor[1];
2541                         lightcolorpants[2] = lightcolorbase[2] * rsurface.colormap_pantscolor[2];
2542                 }
2543                 else
2544                         VectorClear(lightcolorpants);
2545                 if (doshirt)
2546                 {
2547                         lightcolorshirt[0] = lightcolorbase[0] * rsurface.colormap_shirtcolor[0];
2548                         lightcolorshirt[1] = lightcolorbase[1] * rsurface.colormap_shirtcolor[1];
2549                         lightcolorshirt[2] = lightcolorbase[2] * rsurface.colormap_shirtcolor[2];
2550                 }
2551                 else
2552                         VectorClear(lightcolorshirt);
2553                 switch (r_shadow_rendermode)
2554                 {
2555                 case R_SHADOW_RENDERMODE_VISIBLELIGHTING:
2556                         GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST) && !r_showdisabledepthtest.integer);
2557                         R_Shadow_RenderLighting_VisibleLighting(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, lightcolorpants, lightcolorshirt, rsurface.texture->basetexture, rsurface.texture->currentskinframe->pants, rsurface.texture->currentskinframe->shirt, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, dopants, doshirt);
2558                         break;
2559                 case R_SHADOW_RENDERMODE_LIGHT_GLSL:
2560                         R_Shadow_RenderLighting_Light_GLSL(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, lightcolorpants, lightcolorshirt, rsurface.texture->basetexture, rsurface.texture->currentskinframe->pants, rsurface.texture->currentskinframe->shirt, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, dopants, doshirt);
2561                         break;
2562                 case R_SHADOW_RENDERMODE_LIGHT_DOT3:
2563                         R_Shadow_RenderLighting_Light_Dot3(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, lightcolorpants, lightcolorshirt, rsurface.texture->basetexture, rsurface.texture->currentskinframe->pants, rsurface.texture->currentskinframe->shirt, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, dopants, doshirt);
2564                         break;
2565                 case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
2566                         R_Shadow_RenderLighting_Light_Vertex(firstvertex, numvertices, numtriangles, element3i + firsttriangle * 3, lightcolorbase, lightcolorpants, lightcolorshirt, rsurface.texture->basetexture, rsurface.texture->currentskinframe->pants, rsurface.texture->currentskinframe->shirt, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, dopants, doshirt);
2567                         break;
2568                 default:
2569                         Con_Printf("R_Shadow_RenderLighting: unknown r_shadow_rendermode %i\n", r_shadow_rendermode);
2570                         break;
2571                 }
2572         }
2573         else
2574         {
2575                 switch (r_shadow_rendermode)
2576                 {
2577                 case R_SHADOW_RENDERMODE_VISIBLELIGHTING:
2578                         GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST) && !r_showdisabledepthtest.integer);
2579                         R_Shadow_RenderLighting_VisibleLighting(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, vec3_origin, vec3_origin, rsurface.texture->basetexture, r_texture_black, r_texture_black, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, false, false);
2580                         break;
2581                 case R_SHADOW_RENDERMODE_LIGHT_GLSL:
2582                         R_Shadow_RenderLighting_Light_GLSL(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, vec3_origin, vec3_origin, rsurface.texture->basetexture, r_texture_black, r_texture_black, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, false, false);
2583                         break;
2584                 case R_SHADOW_RENDERMODE_LIGHT_DOT3:
2585                         R_Shadow_RenderLighting_Light_Dot3(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, vec3_origin, vec3_origin, rsurface.texture->basetexture, r_texture_black, r_texture_black, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, false, false);
2586                         break;
2587                 case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
2588                         R_Shadow_RenderLighting_Light_Vertex(firstvertex, numvertices, numtriangles, element3i + firsttriangle * 3, lightcolorbase, vec3_origin, vec3_origin, rsurface.texture->basetexture, r_texture_black, r_texture_black, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, false, false);
2589                         break;
2590                 default:
2591                         Con_Printf("R_Shadow_RenderLighting: unknown r_shadow_rendermode %i\n", r_shadow_rendermode);
2592                         break;
2593                 }
2594         }
2595 }
2596
2597 void R_RTLight_Update(rtlight_t *rtlight, int isstatic, matrix4x4_t *matrix, vec3_t color, int style, const char *cubemapname, qboolean shadow, vec_t corona, vec_t coronasizescale, vec_t ambientscale, vec_t diffusescale, vec_t specularscale, int flags)
2598 {
2599         matrix4x4_t tempmatrix = *matrix;
2600         Matrix4x4_Scale(&tempmatrix, r_shadow_lightradiusscale.value, 1);
2601
2602         // if this light has been compiled before, free the associated data
2603         R_RTLight_Uncompile(rtlight);
2604
2605         // clear it completely to avoid any lingering data
2606         memset(rtlight, 0, sizeof(*rtlight));
2607
2608         // copy the properties
2609         rtlight->matrix_lighttoworld = tempmatrix;
2610         Matrix4x4_Invert_Simple(&rtlight->matrix_worldtolight, &tempmatrix);
2611         Matrix4x4_OriginFromMatrix(&tempmatrix, rtlight->shadoworigin);
2612         rtlight->radius = Matrix4x4_ScaleFromMatrix(&tempmatrix);
2613         VectorCopy(color, rtlight->color);
2614         rtlight->cubemapname[0] = 0;
2615         if (cubemapname && cubemapname[0])
2616                 strlcpy(rtlight->cubemapname, cubemapname, sizeof(rtlight->cubemapname));
2617         rtlight->shadow = shadow;
2618         rtlight->corona = corona;
2619         rtlight->style = style;
2620         rtlight->isstatic = isstatic;
2621         rtlight->coronasizescale = coronasizescale;
2622         rtlight->ambientscale = ambientscale;
2623         rtlight->diffusescale = diffusescale;
2624         rtlight->specularscale = specularscale;
2625         rtlight->flags = flags;
2626
2627         // compute derived data
2628         //rtlight->cullradius = rtlight->radius;
2629         //rtlight->cullradius2 = rtlight->radius * rtlight->radius;
2630         rtlight->cullmins[0] = rtlight->shadoworigin[0] - rtlight->radius;
2631         rtlight->cullmins[1] = rtlight->shadoworigin[1] - rtlight->radius;
2632         rtlight->cullmins[2] = rtlight->shadoworigin[2] - rtlight->radius;
2633         rtlight->cullmaxs[0] = rtlight->shadoworigin[0] + rtlight->radius;
2634         rtlight->cullmaxs[1] = rtlight->shadoworigin[1] + rtlight->radius;
2635         rtlight->cullmaxs[2] = rtlight->shadoworigin[2] + rtlight->radius;
2636 }
2637
2638 // compiles rtlight geometry
2639 // (undone by R_FreeCompiledRTLight, which R_UpdateLight calls)
2640 void R_RTLight_Compile(rtlight_t *rtlight)
2641 {
2642         int i;
2643         int numsurfaces, numleafs, numleafpvsbytes, numshadowtrispvsbytes, numlighttrispvsbytes;
2644         int lighttris, shadowtris, shadowzpasstris, shadowzfailtris;
2645         entity_render_t *ent = r_refdef.scene.worldentity;
2646         dp_model_t *model = r_refdef.scene.worldmodel;
2647         unsigned char *data;
2648         shadowmesh_t *mesh;
2649
2650         // compile the light
2651         rtlight->compiled = true;
2652         rtlight->static_numleafs = 0;
2653         rtlight->static_numleafpvsbytes = 0;
2654         rtlight->static_leaflist = NULL;
2655         rtlight->static_leafpvs = NULL;
2656         rtlight->static_numsurfaces = 0;
2657         rtlight->static_surfacelist = NULL;
2658         rtlight->cullmins[0] = rtlight->shadoworigin[0] - rtlight->radius;
2659         rtlight->cullmins[1] = rtlight->shadoworigin[1] - rtlight->radius;
2660         rtlight->cullmins[2] = rtlight->shadoworigin[2] - rtlight->radius;
2661         rtlight->cullmaxs[0] = rtlight->shadoworigin[0] + rtlight->radius;
2662         rtlight->cullmaxs[1] = rtlight->shadoworigin[1] + rtlight->radius;
2663         rtlight->cullmaxs[2] = rtlight->shadoworigin[2] + rtlight->radius;
2664
2665         if (model && model->GetLightInfo)
2666         {
2667                 // this variable must be set for the CompileShadowVolume code
2668                 r_shadow_compilingrtlight = rtlight;
2669                 R_Shadow_EnlargeLeafSurfaceTrisBuffer(model->brush.num_leafs, model->num_surfaces, model->brush.shadowmesh ? model->brush.shadowmesh->numtriangles : model->surfmesh.num_triangles, model->surfmesh.num_triangles);
2670                 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);
2671                 numleafpvsbytes = (model->brush.num_leafs + 7) >> 3;
2672                 numshadowtrispvsbytes = ((model->brush.shadowmesh ? model->brush.shadowmesh->numtriangles : model->surfmesh.num_triangles) + 7) >> 3;
2673                 numlighttrispvsbytes = (model->surfmesh.num_triangles + 7) >> 3;
2674                 data = (unsigned char *)Mem_Alloc(r_main_mempool, sizeof(int) * numsurfaces + sizeof(int) * numleafs + numleafpvsbytes + numshadowtrispvsbytes + numlighttrispvsbytes);
2675                 rtlight->static_numsurfaces = numsurfaces;
2676                 rtlight->static_surfacelist = (int *)data;data += sizeof(int) * numsurfaces;
2677                 rtlight->static_numleafs = numleafs;
2678                 rtlight->static_leaflist = (int *)data;data += sizeof(int) * numleafs;
2679                 rtlight->static_numleafpvsbytes = numleafpvsbytes;
2680                 rtlight->static_leafpvs = (unsigned char *)data;data += numleafpvsbytes;
2681                 rtlight->static_numshadowtrispvsbytes = numshadowtrispvsbytes;
2682                 rtlight->static_shadowtrispvs = (unsigned char *)data;data += numshadowtrispvsbytes;
2683                 rtlight->static_numlighttrispvsbytes = numlighttrispvsbytes;
2684                 rtlight->static_lighttrispvs = (unsigned char *)data;data += numlighttrispvsbytes;
2685                 if (rtlight->static_numsurfaces)
2686                         memcpy(rtlight->static_surfacelist, r_shadow_buffer_surfacelist, rtlight->static_numsurfaces * sizeof(*rtlight->static_surfacelist));
2687                 if (rtlight->static_numleafs)
2688                         memcpy(rtlight->static_leaflist, r_shadow_buffer_leaflist, rtlight->static_numleafs * sizeof(*rtlight->static_leaflist));
2689                 if (rtlight->static_numleafpvsbytes)
2690                         memcpy(rtlight->static_leafpvs, r_shadow_buffer_leafpvs, rtlight->static_numleafpvsbytes);
2691                 if (rtlight->static_numshadowtrispvsbytes)
2692                         memcpy(rtlight->static_shadowtrispvs, r_shadow_buffer_shadowtrispvs, rtlight->static_numshadowtrispvsbytes);
2693                 if (rtlight->static_numlighttrispvsbytes)
2694                         memcpy(rtlight->static_lighttrispvs, r_shadow_buffer_lighttrispvs, rtlight->static_numlighttrispvsbytes);
2695                 if (model->CompileShadowVolume && rtlight->shadow)
2696                         model->CompileShadowVolume(ent, rtlight->shadoworigin, NULL, rtlight->radius, numsurfaces, r_shadow_buffer_surfacelist);
2697                 // now we're done compiling the rtlight
2698                 r_shadow_compilingrtlight = NULL;
2699         }
2700
2701
2702         // use smallest available cullradius - box radius or light radius
2703         //rtlight->cullradius = RadiusFromBoundsAndOrigin(rtlight->cullmins, rtlight->cullmaxs, rtlight->shadoworigin);
2704         //rtlight->cullradius = min(rtlight->cullradius, rtlight->radius);
2705
2706         shadowzpasstris = 0;
2707         if (rtlight->static_meshchain_shadow_zpass)
2708                 for (mesh = rtlight->static_meshchain_shadow_zpass;mesh;mesh = mesh->next)
2709                         shadowzpasstris += mesh->numtriangles;
2710
2711         shadowzfailtris = 0;
2712         if (rtlight->static_meshchain_shadow_zfail)
2713                 for (mesh = rtlight->static_meshchain_shadow_zfail;mesh;mesh = mesh->next)
2714                         shadowzfailtris += mesh->numtriangles;
2715
2716         lighttris = 0;
2717         if (rtlight->static_numlighttrispvsbytes)
2718                 for (i = 0;i < rtlight->static_numlighttrispvsbytes*8;i++)
2719                         if (CHECKPVSBIT(rtlight->static_lighttrispvs, i))
2720                                 lighttris++;
2721
2722         shadowtris = 0;
2723         if (rtlight->static_numlighttrispvsbytes)
2724                 for (i = 0;i < rtlight->static_numshadowtrispvsbytes*8;i++)
2725                         if (CHECKPVSBIT(rtlight->static_shadowtrispvs, i))
2726                                 shadowtris++;
2727
2728         if (developer.integer >= 10)
2729                 Con_Printf("static light built: %f %f %f : %f %f %f box, %i light triangles, %i shadow triangles, %i zpass/%i zfail compiled shadow volume triangles\n", rtlight->cullmins[0], rtlight->cullmins[1], rtlight->cullmins[2], rtlight->cullmaxs[0], rtlight->cullmaxs[1], rtlight->cullmaxs[2], lighttris, shadowtris, shadowzpasstris, shadowzfailtris);
2730 }
2731
2732 void R_RTLight_Uncompile(rtlight_t *rtlight)
2733 {
2734         if (rtlight->compiled)
2735         {
2736                 if (rtlight->static_meshchain_shadow_zpass)
2737                         Mod_ShadowMesh_Free(rtlight->static_meshchain_shadow_zpass);
2738                 rtlight->static_meshchain_shadow_zpass = NULL;
2739                 if (rtlight->static_meshchain_shadow_zfail)
2740                         Mod_ShadowMesh_Free(rtlight->static_meshchain_shadow_zfail);
2741                 rtlight->static_meshchain_shadow_zfail = NULL;
2742                 // these allocations are grouped
2743                 if (rtlight->static_surfacelist)
2744                         Mem_Free(rtlight->static_surfacelist);
2745                 rtlight->static_numleafs = 0;
2746                 rtlight->static_numleafpvsbytes = 0;
2747                 rtlight->static_leaflist = NULL;
2748                 rtlight->static_leafpvs = NULL;
2749                 rtlight->static_numsurfaces = 0;
2750                 rtlight->static_surfacelist = NULL;
2751                 rtlight->static_numshadowtrispvsbytes = 0;
2752                 rtlight->static_shadowtrispvs = NULL;
2753                 rtlight->static_numlighttrispvsbytes = 0;
2754                 rtlight->static_lighttrispvs = NULL;
2755                 rtlight->compiled = false;
2756         }
2757 }
2758
2759 void R_Shadow_UncompileWorldLights(void)
2760 {
2761         size_t lightindex;
2762         dlight_t *light;
2763         size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
2764         for (lightindex = 0;lightindex < range;lightindex++)
2765         {
2766                 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
2767                 if (!light)
2768                         continue;
2769                 R_RTLight_Uncompile(&light->rtlight);
2770         }
2771 }
2772
2773 void R_Shadow_ComputeShadowCasterCullingPlanes(rtlight_t *rtlight)
2774 {
2775         int i, j;
2776         mplane_t plane;
2777         // reset the count of frustum planes
2778         // see rsurface.rtlight_frustumplanes definition for how much this array
2779         // can hold
2780         rsurface.rtlight_numfrustumplanes = 0;
2781
2782         // haven't implemented a culling path for ortho rendering
2783         if (!r_refdef.view.useperspective)
2784         {
2785                 // check if the light is on screen and copy the 4 planes if it is
2786                 for (i = 0;i < 4;i++)
2787                         if (PlaneDiff(rtlight->shadoworigin, &r_refdef.view.frustum[i]) < -0.03125)
2788                                 break;
2789                 if (i == 4)
2790                         for (i = 0;i < 4;i++)
2791                                 rsurface.rtlight_frustumplanes[rsurface.rtlight_numfrustumplanes++] = r_refdef.view.frustum[i];
2792                 return;
2793         }
2794
2795 #if 1
2796         // generate a deformed frustum that includes the light origin, this is
2797         // used to cull shadow casting surfaces that can not possibly cast a
2798         // shadow onto the visible light-receiving surfaces, which can be a
2799         // performance gain
2800         //
2801         // if the light origin is onscreen the result will be 4 planes exactly
2802         // if the light origin is offscreen on only one axis the result will
2803         // be exactly 5 planes (split-side case)
2804         // if the light origin is offscreen on two axes the result will be
2805         // exactly 4 planes (stretched corner case)
2806         for (i = 0;i < 4;i++)
2807         {
2808                 // quickly reject standard frustum planes that put the light
2809                 // origin outside the frustum
2810                 if (PlaneDiff(rtlight->shadoworigin, &r_refdef.view.frustum[i]) < -0.03125)
2811                         continue;
2812                 // copy the plane
2813                 rsurface.rtlight_frustumplanes[rsurface.rtlight_numfrustumplanes++] = r_refdef.view.frustum[i];
2814         }
2815         // if all the standard frustum planes were accepted, the light is onscreen
2816         // otherwise we need to generate some more planes below...
2817         if (rsurface.rtlight_numfrustumplanes < 4)
2818         {
2819                 // at least one of the stock frustum planes failed, so we need to
2820                 // create one or two custom planes to enclose the light origin
2821                 for (i = 0;i < 4;i++)
2822                 {
2823                         // create a plane using the view origin and light origin, and a
2824                         // single point from the frustum corner set
2825                         TriangleNormal(r_refdef.view.origin, r_refdef.view.frustumcorner[i], rtlight->shadoworigin, plane.normal);
2826                         VectorNormalize(plane.normal);
2827                         plane.dist = DotProduct(r_refdef.view.origin, plane.normal);
2828                         // see if this plane is backwards and flip it if so
2829                         for (j = 0;j < 4;j++)
2830                                 if (j != i && DotProduct(r_refdef.view.frustumcorner[j], plane.normal) - plane.dist < -0.03125)
2831                                         break;
2832                         if (j < 4)
2833                         {
2834                                 VectorNegate(plane.normal, plane.normal);
2835                                 plane.dist *= -1;
2836                                 // flipped plane, test again to see if it is now valid
2837                                 for (j = 0;j < 4;j++)
2838                                         if (j != i && DotProduct(r_refdef.view.frustumcorner[j], plane.normal) - plane.dist < -0.03125)
2839                                                 break;
2840                                 // if the plane is still not valid, then it is dividing the
2841                                 // frustum and has to be rejected
2842                                 if (j < 4)
2843                                         continue;
2844                         }
2845                         // we have created a valid plane, compute extra info
2846                         PlaneClassify(&plane);
2847                         // copy the plane
2848                         rsurface.rtlight_frustumplanes[rsurface.rtlight_numfrustumplanes++] = plane;
2849 #if 1
2850                         // if we've found 5 frustum planes then we have constructed a
2851                         // proper split-side case and do not need to keep searching for
2852                         // planes to enclose the light origin
2853                         if (rsurface.rtlight_numfrustumplanes == 5)
2854                                 break;
2855 #endif
2856                 }
2857         }
2858 #endif
2859
2860 #if 0
2861         for (i = 0;i < rsurface.rtlight_numfrustumplanes;i++)
2862         {
2863                 plane = rsurface.rtlight_frustumplanes[i];
2864                 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));
2865         }
2866 #endif
2867
2868 #if 0
2869         // now add the light-space box planes if the light box is rotated, as any
2870         // caster outside the oriented light box is irrelevant (even if it passed
2871         // the worldspace light box, which is axial)
2872         if (rtlight->matrix_lighttoworld.m[0][0] != 1 || rtlight->matrix_lighttoworld.m[1][1] != 1 || rtlight->matrix_lighttoworld.m[2][2] != 1)
2873         {
2874                 for (i = 0;i < 6;i++)
2875                 {
2876                         vec3_t v;
2877                         VectorClear(v);
2878                         v[i >> 1] = (i & 1) ? -1 : 1;
2879                         Matrix4x4_Transform(&rtlight->matrix_lighttoworld, v, plane.normal);
2880                         VectorSubtract(plane.normal, rtlight->shadoworigin, plane.normal);
2881                         plane.dist = VectorNormalizeLength(plane.normal);
2882                         plane.dist += DotProduct(plane.normal, rtlight->shadoworigin);
2883                         rsurface.rtlight_frustumplanes[rsurface.rtlight_numfrustumplanes++] = plane;
2884                 }
2885         }
2886 #endif
2887
2888 #if 0
2889         // add the world-space reduced box planes
2890         for (i = 0;i < 6;i++)
2891         {
2892                 VectorClear(plane.normal);
2893                 plane.normal[i >> 1] = (i & 1) ? -1 : 1;
2894                 plane.dist = (i & 1) ? -rsurface.rtlight_cullmaxs[i >> 1] : rsurface.rtlight_cullmins[i >> 1];
2895                 rsurface.rtlight_frustumplanes[rsurface.rtlight_numfrustumplanes++] = plane;
2896         }
2897 #endif
2898
2899 #if 0
2900         {
2901         int j, oldnum;
2902         vec3_t points[8];
2903         vec_t bestdist;
2904         // reduce all plane distances to tightly fit the rtlight cull box, which
2905         // is in worldspace
2906         VectorSet(points[0], rsurface.rtlight_cullmins[0], rsurface.rtlight_cullmins[1], rsurface.rtlight_cullmins[2]);
2907         VectorSet(points[1], rsurface.rtlight_cullmaxs[0], rsurface.rtlight_cullmins[1], rsurface.rtlight_cullmins[2]);
2908         VectorSet(points[2], rsurface.rtlight_cullmins[0], rsurface.rtlight_cullmaxs[1], rsurface.rtlight_cullmins[2]);
2909         VectorSet(points[3], rsurface.rtlight_cullmaxs[0], rsurface.rtlight_cullmaxs[1], rsurface.rtlight_cullmins[2]);
2910         VectorSet(points[4], rsurface.rtlight_cullmins[0], rsurface.rtlight_cullmins[1], rsurface.rtlight_cullmaxs[2]);
2911         VectorSet(points[5], rsurface.rtlight_cullmaxs[0], rsurface.rtlight_cullmins[1], rsurface.rtlight_cullmaxs[2]);
2912         VectorSet(points[6], rsurface.rtlight_cullmins[0], rsurface.rtlight_cullmaxs[1], rsurface.rtlight_cullmaxs[2]);
2913         VectorSet(points[7], rsurface.rtlight_cullmaxs[0], rsurface.rtlight_cullmaxs[1], rsurface.rtlight_cullmaxs[2]);
2914         oldnum = rsurface.rtlight_numfrustumplanes;
2915         rsurface.rtlight_numfrustumplanes = 0;
2916         for (j = 0;j < oldnum;j++)
2917         {
2918                 // find the nearest point on the box to this plane
2919                 bestdist = DotProduct(rsurface.rtlight_frustumplanes[j].normal, points[0]);
2920                 for (i = 1;i < 8;i++)
2921                 {
2922                         dist = DotProduct(rsurface.rtlight_frustumplanes[j].normal, points[i]);
2923                         if (bestdist > dist)
2924                                 bestdist = dist;
2925                 }
2926                 Con_Printf("light %p %splane #%i %f %f %f : %f < %f\n", rtlight, rsurface.rtlight_frustumplanes[j].dist < bestdist + 0.03125 ? "^2" : "^1", j, rsurface.rtlight_frustumplanes[j].normal[0], rsurface.rtlight_frustumplanes[j].normal[1], rsurface.rtlight_frustumplanes[j].normal[2], rsurface.rtlight_frustumplanes[j].dist, bestdist);
2927                 // if the nearest point is near or behind the plane, we want this
2928                 // plane, otherwise the plane is useless as it won't cull anything
2929                 if (rsurface.rtlight_frustumplanes[j].dist < bestdist + 0.03125)
2930                 {
2931                         PlaneClassify(&rsurface.rtlight_frustumplanes[j]);
2932                         rsurface.rtlight_frustumplanes[rsurface.rtlight_numfrustumplanes++] = rsurface.rtlight_frustumplanes[j];
2933                 }
2934         }
2935         }
2936 #endif
2937 }
2938
2939 void R_Shadow_DrawWorldShadow(int numsurfaces, int *surfacelist, const unsigned char *trispvs)
2940 {
2941         qboolean zpass;
2942         shadowmesh_t *mesh;
2943         int t, tend;
2944         int surfacelistindex;
2945         msurface_t *surface;
2946
2947         RSurf_ActiveWorldEntity();
2948         if (rsurface.rtlight->compiled && r_shadow_realtime_world_compile.integer && r_shadow_realtime_world_compileshadow.integer)
2949         {
2950                 CHECKGLERROR
2951                 zpass = R_Shadow_UseZPass(r_refdef.scene.worldmodel->normalmins, r_refdef.scene.worldmodel->normalmaxs);
2952                 R_Shadow_RenderMode_StencilShadowVolumes(zpass);
2953                 mesh = zpass ? rsurface.rtlight->static_meshchain_shadow_zpass : rsurface.rtlight->static_meshchain_shadow_zfail;
2954                 for (;mesh;mesh = mesh->next)
2955                 {
2956                         r_refdef.stats.lights_shadowtriangles += mesh->numtriangles;
2957                         R_Mesh_VertexPointer(mesh->vertex3f, mesh->vbo, mesh->vbooffset_vertex3f);
2958                         GL_LockArrays(0, mesh->numverts);
2959                         if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZPASS_STENCIL)
2960                         {
2961                                 // increment stencil if frontface is infront of depthbuffer
2962                                 GL_CullFace(r_refdef.view.cullface_back);
2963                                 qglStencilOp(GL_KEEP, GL_KEEP, GL_INCR);CHECKGLERROR
2964                                 R_Mesh_Draw(0, mesh->numverts, 0, mesh->numtriangles, mesh->element3i, mesh->element3s, mesh->ebo3i, mesh->ebo3s);
2965                                 // decrement stencil if backface is infront of depthbuffer
2966                                 GL_CullFace(r_refdef.view.cullface_front);
2967                                 qglStencilOp(GL_KEEP, GL_KEEP, GL_DECR);CHECKGLERROR
2968                         }
2969                         else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZFAIL_STENCIL)
2970                         {
2971                                 // decrement stencil if backface is behind depthbuffer
2972                                 GL_CullFace(r_refdef.view.cullface_front);
2973                                 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
2974                                 R_Mesh_Draw(0, mesh->numverts, 0, mesh->numtriangles, mesh->element3i, mesh->element3s, mesh->ebo3i, mesh->ebo3s);
2975                                 // increment stencil if frontface is behind depthbuffer
2976                                 GL_CullFace(r_refdef.view.cullface_back);
2977                                 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR
2978                         }
2979                         R_Mesh_Draw(0, mesh->numverts, 0, mesh->numtriangles, mesh->element3i, mesh->element3s, mesh->ebo3i, mesh->ebo3s);
2980                         GL_LockArrays(0, 0);
2981                 }
2982                 CHECKGLERROR
2983         }
2984         else if (numsurfaces && r_refdef.scene.worldmodel->brush.shadowmesh && r_shadow_culltriangles.integer)
2985         {
2986                 R_Shadow_PrepareShadowMark(r_refdef.scene.worldmodel->brush.shadowmesh->numtriangles);
2987                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2988                 {
2989                         surface = r_refdef.scene.worldmodel->data_surfaces + surfacelist[surfacelistindex];
2990                         for (t = surface->num_firstshadowmeshtriangle, tend = t + surface->num_triangles;t < tend;t++)
2991                                 if (CHECKPVSBIT(trispvs, t))
2992                                         shadowmarklist[numshadowmark++] = t;
2993                 }
2994                 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);
2995         }
2996         else if (numsurfaces)
2997                 r_refdef.scene.worldmodel->DrawShadowVolume(r_refdef.scene.worldentity, rsurface.rtlight->shadoworigin, NULL, rsurface.rtlight->radius, numsurfaces, surfacelist, rsurface.rtlight_cullmins, rsurface.rtlight_cullmaxs);
2998 }
2999
3000 void R_Shadow_DrawEntityShadow(entity_render_t *ent)
3001 {
3002         vec3_t relativeshadoworigin, relativeshadowmins, relativeshadowmaxs;
3003         vec_t relativeshadowradius;
3004         RSurf_ActiveModelEntity(ent, false, false);
3005         Matrix4x4_Transform(&ent->inversematrix, rsurface.rtlight->shadoworigin, relativeshadoworigin);
3006         relativeshadowradius = rsurface.rtlight->radius / ent->scale;
3007         relativeshadowmins[0] = relativeshadoworigin[0] - relativeshadowradius;
3008         relativeshadowmins[1] = relativeshadoworigin[1] - relativeshadowradius;
3009         relativeshadowmins[2] = relativeshadoworigin[2] - relativeshadowradius;
3010         relativeshadowmaxs[0] = relativeshadoworigin[0] + relativeshadowradius;
3011         relativeshadowmaxs[1] = relativeshadoworigin[1] + relativeshadowradius;
3012         relativeshadowmaxs[2] = relativeshadoworigin[2] + relativeshadowradius;
3013         ent->model->DrawShadowVolume(ent, relativeshadoworigin, NULL, relativeshadowradius, ent->model->nummodelsurfaces, ent->model->surfacelist, relativeshadowmins, relativeshadowmaxs);
3014 }
3015
3016 void R_Shadow_SetupEntityLight(const entity_render_t *ent)
3017 {
3018         // set up properties for rendering light onto this entity
3019         RSurf_ActiveModelEntity(ent, true, true);
3020         GL_AlphaTest(false);
3021         Matrix4x4_Concat(&rsurface.entitytolight, &rsurface.rtlight->matrix_worldtolight, &ent->matrix);
3022         Matrix4x4_Concat(&rsurface.entitytoattenuationxyz, &matrix_attenuationxyz, &rsurface.entitytolight);
3023         Matrix4x4_Concat(&rsurface.entitytoattenuationz, &matrix_attenuationz, &rsurface.entitytolight);
3024         Matrix4x4_Transform(&ent->inversematrix, rsurface.rtlight->shadoworigin, rsurface.entitylightorigin);
3025         if (r_shadow_lightingrendermode == R_SHADOW_RENDERMODE_LIGHT_GLSL)
3026                 R_Mesh_TexMatrix(3, &rsurface.entitytolight);
3027 }
3028
3029 void R_Shadow_DrawWorldLight(int numsurfaces, int *surfacelist, const unsigned char *trispvs)
3030 {
3031         if (!r_refdef.scene.worldmodel->DrawLight)
3032                 return;
3033
3034         // set up properties for rendering light onto this entity
3035         RSurf_ActiveWorldEntity();
3036         GL_AlphaTest(false);
3037         rsurface.entitytolight = rsurface.rtlight->matrix_worldtolight;
3038         Matrix4x4_Concat(&rsurface.entitytoattenuationxyz, &matrix_attenuationxyz, &rsurface.entitytolight);
3039         Matrix4x4_Concat(&rsurface.entitytoattenuationz, &matrix_attenuationz, &rsurface.entitytolight);
3040         VectorCopy(rsurface.rtlight->shadoworigin, rsurface.entitylightorigin);
3041         if (r_shadow_lightingrendermode == R_SHADOW_RENDERMODE_LIGHT_GLSL)
3042                 R_Mesh_TexMatrix(3, &rsurface.entitytolight);
3043
3044         r_refdef.scene.worldmodel->DrawLight(r_refdef.scene.worldentity, numsurfaces, surfacelist, trispvs);
3045 }
3046
3047 void R_Shadow_DrawEntityLight(entity_render_t *ent)
3048 {
3049         dp_model_t *model = ent->model;
3050         if (!model->DrawLight)
3051                 return;
3052
3053         R_Shadow_SetupEntityLight(ent);
3054
3055         model->DrawLight(ent, model->nummodelsurfaces, model->surfacelist, NULL);
3056 }
3057
3058 void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
3059 {
3060         int i;
3061         float f;
3062         int numleafs, numsurfaces;
3063         int *leaflist, *surfacelist;
3064         unsigned char *leafpvs, *shadowtrispvs, *lighttrispvs;
3065         int numlightentities;
3066         int numlightentities_noselfshadow;
3067         int numshadowentities;
3068         int numshadowentities_noselfshadow;
3069         static entity_render_t *lightentities[MAX_EDICTS];
3070         static entity_render_t *lightentities_noselfshadow[MAX_EDICTS];
3071         static entity_render_t *shadowentities[MAX_EDICTS];
3072         static entity_render_t *shadowentities_noselfshadow[MAX_EDICTS];
3073
3074         // skip lights that don't light because of ambientscale+diffusescale+specularscale being 0 (corona only lights)
3075         // skip lights that are basically invisible (color 0 0 0)
3076         if (VectorLength2(rtlight->color) * (rtlight->ambientscale + rtlight->diffusescale + rtlight->specularscale) < (1.0f / 1048576.0f))
3077                 return;
3078
3079         // loading is done before visibility checks because loading should happen
3080         // all at once at the start of a level, not when it stalls gameplay.
3081         // (especially important to benchmarks)
3082         // compile light
3083         if (rtlight->isstatic && !rtlight->compiled && r_shadow_realtime_world_compile.integer)
3084                 R_RTLight_Compile(rtlight);
3085         // load cubemap
3086         rtlight->currentcubemap = rtlight->cubemapname[0] ? R_Shadow_Cubemap(rtlight->cubemapname) : r_texture_whitecube;
3087
3088         // look up the light style value at this time
3089         f = (rtlight->style >= 0 ? r_refdef.scene.rtlightstylevalue[rtlight->style] : 1) * r_shadow_lightintensityscale.value;
3090         VectorScale(rtlight->color, f, rtlight->currentcolor);
3091         /*
3092         if (rtlight->selected)
3093         {
3094                 f = 2 + sin(realtime * M_PI * 4.0);
3095                 VectorScale(rtlight->currentcolor, f, rtlight->currentcolor);
3096         }
3097         */
3098
3099         // if lightstyle is currently off, don't draw the light
3100         if (VectorLength2(rtlight->currentcolor) < (1.0f / 1048576.0f))
3101                 return;
3102
3103         // if the light box is offscreen, skip it
3104         if (R_CullBox(rtlight->cullmins, rtlight->cullmaxs))
3105                 return;
3106
3107         VectorCopy(rtlight->cullmins, rsurface.rtlight_cullmins);
3108         VectorCopy(rtlight->cullmaxs, rsurface.rtlight_cullmaxs);
3109
3110         if (rtlight->compiled && r_shadow_realtime_world_compile.integer)
3111         {
3112                 // compiled light, world available and can receive realtime lighting
3113                 // retrieve leaf information
3114                 numleafs = rtlight->static_numleafs;
3115                 leaflist = rtlight->static_leaflist;
3116                 leafpvs = rtlight->static_leafpvs;
3117                 numsurfaces = rtlight->static_numsurfaces;
3118                 surfacelist = rtlight->static_surfacelist;
3119                 shadowtrispvs = rtlight->static_shadowtrispvs;
3120                 lighttrispvs = rtlight->static_lighttrispvs;
3121         }
3122         else if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->GetLightInfo)
3123         {
3124                 // dynamic light, world available and can receive realtime lighting
3125                 // calculate lit surfaces and leafs
3126                 R_Shadow_EnlargeLeafSurfaceTrisBuffer(r_refdef.scene.worldmodel->brush.num_leafs, r_refdef.scene.worldmodel->num_surfaces, r_refdef.scene.worldmodel->brush.shadowmesh ? r_refdef.scene.worldmodel->brush.shadowmesh->numtriangles : r_refdef.scene.worldmodel->surfmesh.num_triangles, r_refdef.scene.worldmodel->surfmesh.num_triangles);
3127                 r_refdef.scene.worldmodel->GetLightInfo(r_refdef.scene.worldentity, rtlight->shadoworigin, rtlight->radius, rsurface.rtlight_cullmins, rsurface.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);
3128                 leaflist = r_shadow_buffer_leaflist;
3129                 leafpvs = r_shadow_buffer_leafpvs;
3130                 surfacelist = r_shadow_buffer_surfacelist;
3131                 shadowtrispvs = r_shadow_buffer_shadowtrispvs;
3132                 lighttrispvs = r_shadow_buffer_lighttrispvs;
3133                 // if the reduced leaf bounds are offscreen, skip it
3134                 if (R_CullBox(rsurface.rtlight_cullmins, rsurface.rtlight_cullmaxs))
3135                         return;
3136         }
3137         else
3138         {
3139                 // no world
3140                 numleafs = 0;
3141                 leaflist = NULL;
3142                 leafpvs = NULL;
3143                 numsurfaces = 0;
3144                 surfacelist = NULL;
3145                 shadowtrispvs = NULL;
3146                 lighttrispvs = NULL;
3147         }
3148         // check if light is illuminating any visible leafs
3149         if (numleafs)
3150         {
3151                 for (i = 0;i < numleafs;i++)
3152                         if (r_refdef.viewcache.world_leafvisible[leaflist[i]])
3153                                 break;
3154                 if (i == numleafs)
3155                         return;
3156         }
3157         // set up a scissor rectangle for this light
3158         if (R_Shadow_ScissorForBBox(rsurface.rtlight_cullmins, rsurface.rtlight_cullmaxs))
3159                 return;
3160
3161         R_Shadow_ComputeShadowCasterCullingPlanes(rtlight);
3162
3163         // make a list of lit entities and shadow casting entities
3164         numlightentities = 0;
3165         numlightentities_noselfshadow = 0;
3166         numshadowentities = 0;
3167         numshadowentities_noselfshadow = 0;
3168         // add dynamic entities that are lit by the light
3169         if (r_drawentities.integer)
3170         {
3171                 for (i = 0;i < r_refdef.scene.numentities;i++)
3172                 {
3173                         dp_model_t *model;
3174                         entity_render_t *ent = r_refdef.scene.entities[i];
3175                         vec3_t org;
3176                         if (!BoxesOverlap(ent->mins, ent->maxs, rsurface.rtlight_cullmins, rsurface.rtlight_cullmaxs))
3177                                 continue;
3178                         // skip the object entirely if it is not within the valid
3179                         // shadow-casting region (which includes the lit region)
3180                         if (R_CullBoxCustomPlanes(ent->mins, ent->maxs, rsurface.rtlight_numfrustumplanes, rsurface.rtlight_frustumplanes))
3181                                 continue;
3182                         if (!(model = ent->model))
3183                                 continue;
3184                         if (r_refdef.viewcache.entityvisible[i] && model->DrawLight && (ent->flags & RENDER_LIGHT))
3185                         {
3186                                 // this entity wants to receive light, is visible, and is
3187                                 // inside the light box
3188                                 // TODO: check if the surfaces in the model can receive light
3189                                 // so now check if it's in a leaf seen by the light
3190                                 if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.BoxTouchingLeafPVS && !r_refdef.scene.worldmodel->brush.BoxTouchingLeafPVS(r_refdef.scene.worldmodel, leafpvs, ent->mins, ent->maxs))
3191                                         continue;
3192                                 if (ent->flags & RENDER_NOSELFSHADOW)
3193                                         lightentities_noselfshadow[numlightentities_noselfshadow++] = ent;
3194                                 else
3195                                         lightentities[numlightentities++] = ent;
3196                                 // since it is lit, it probably also casts a shadow...
3197                                 // about the VectorDistance2 - light emitting entities should not cast their own shadow
3198                                 Matrix4x4_OriginFromMatrix(&ent->matrix, org);
3199                                 if ((ent->flags & RENDER_SHADOW) && model->DrawShadowVolume && VectorDistance2(org, rtlight->shadoworigin) > 0.1)
3200                                 {
3201                                         // note: exterior models without the RENDER_NOSELFSHADOW
3202                                         // flag still create a RENDER_NOSELFSHADOW shadow but
3203                                         // are lit normally, this means that they are
3204                                         // self-shadowing but do not shadow other
3205                                         // RENDER_NOSELFSHADOW entities such as the gun
3206                                         // (very weird, but keeps the player shadow off the gun)
3207                                         if (ent->flags & (RENDER_NOSELFSHADOW | RENDER_EXTERIORMODEL))
3208                                                 shadowentities_noselfshadow[numshadowentities_noselfshadow++] = ent;
3209                                         else
3210                                                 shadowentities[numshadowentities++] = ent;
3211                                 }
3212                         }
3213                         else if (ent->flags & RENDER_SHADOW)
3214                         {
3215                                 // this entity is not receiving light, but may still need to
3216                                 // cast a shadow...
3217                                 // TODO: check if the surfaces in the model can cast shadow
3218                                 // now check if it is in a leaf seen by the light
3219                                 if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.BoxTouchingLeafPVS && !r_refdef.scene.worldmodel->brush.BoxTouchingLeafPVS(r_refdef.scene.worldmodel, leafpvs, ent->mins, ent->maxs))
3220                                         continue;
3221                                 // about the VectorDistance2 - light emitting entities should not cast their own shadow
3222                                 Matrix4x4_OriginFromMatrix(&ent->matrix, org);
3223                                 if ((ent->flags & RENDER_SHADOW) && model->DrawShadowVolume && VectorDistance2(org, rtlight->shadoworigin) > 0.1)
3224                                 {
3225                                         if (ent->flags & (RENDER_NOSELFSHADOW | RENDER_EXTERIORMODEL))
3226                                                 shadowentities_noselfshadow[numshadowentities_noselfshadow++] = ent;
3227                                         else
3228                                                 shadowentities[numshadowentities++] = ent;
3229                                 }
3230                         }
3231                 }
3232         }
3233
3234         // return if there's nothing at all to light
3235         if (!numlightentities && !numsurfaces)
3236                 return;
3237
3238         // don't let sound skip if going slow
3239         if (r_refdef.scene.extraupdate)
3240                 S_ExtraUpdate ();
3241
3242         // make this the active rtlight for rendering purposes
3243         R_Shadow_RenderMode_ActiveLight(rtlight);
3244         // count this light in the r_speeds
3245         r_refdef.stats.lights++;
3246
3247         if (r_showshadowvolumes.integer && r_refdef.view.showdebug && numsurfaces + numshadowentities + numshadowentities_noselfshadow && rtlight->shadow && (rtlight->isstatic ? r_refdef.scene.rtworldshadows : r_refdef.scene.rtdlightshadows))
3248         {
3249                 // optionally draw visible shape of the shadow volumes
3250                 // for performance analysis by level designers
3251                 R_Shadow_RenderMode_VisibleShadowVolumes();
3252                 if (numsurfaces)
3253                         R_Shadow_DrawWorldShadow(numsurfaces, surfacelist, shadowtrispvs);
3254                 for (i = 0;i < numshadowentities;i++)
3255                         R_Shadow_DrawEntityShadow(shadowentities[i]);
3256                 for (i = 0;i < numshadowentities_noselfshadow;i++)
3257                         R_Shadow_DrawEntityShadow(shadowentities_noselfshadow[i]);
3258         }
3259
3260         if (gl_stencil && numsurfaces + numshadowentities + numshadowentities_noselfshadow && rtlight->shadow && (rtlight->isstatic ? r_refdef.scene.rtworldshadows : r_refdef.scene.rtdlightshadows))
3261         {
3262                 // draw stencil shadow volumes to mask off pixels that are in shadow
3263                 // so that they won't receive lighting
3264                 R_Shadow_ClearStencil();
3265                 if (numsurfaces)
3266                         R_Shadow_DrawWorldShadow(numsurfaces, surfacelist, shadowtrispvs);
3267                 for (i = 0;i < numshadowentities;i++)
3268                         R_Shadow_DrawEntityShadow(shadowentities[i]);
3269                 if (numlightentities_noselfshadow)
3270                 {
3271                         // draw lighting in the unmasked areas
3272                         R_Shadow_RenderMode_Lighting(true, false);
3273                         for (i = 0;i < numlightentities_noselfshadow;i++)
3274                                 R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]);
3275
3276                         // optionally draw the illuminated areas
3277                         // for performance analysis by level designers
3278                         if (r_showlighting.integer && r_refdef.view.showdebug)
3279                         {
3280                                 R_Shadow_RenderMode_VisibleLighting(!r_showdisabledepthtest.integer, false);
3281                                 for (i = 0;i < numlightentities_noselfshadow;i++)
3282                                         R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]);
3283                         }
3284                 }
3285                 for (i = 0;i < numshadowentities_noselfshadow;i++)
3286                         R_Shadow_DrawEntityShadow(shadowentities_noselfshadow[i]);
3287
3288                 if (numsurfaces + numlightentities)
3289                 {
3290                         // draw lighting in the unmasked areas
3291                         R_Shadow_RenderMode_Lighting(true, false);
3292                         if (numsurfaces)
3293                                 R_Shadow_DrawWorldLight(numsurfaces, surfacelist, lighttrispvs);
3294                         for (i = 0;i < numlightentities;i++)
3295                                 R_Shadow_DrawEntityLight(lightentities[i]);
3296
3297                         // optionally draw the illuminated areas
3298                         // for performance analysis by level designers
3299                         if (r_showlighting.integer && r_refdef.view.showdebug)
3300                         {
3301                                 R_Shadow_RenderMode_VisibleLighting(!r_showdisabledepthtest.integer, false);
3302                                 if (numsurfaces)
3303                                         R_Shadow_DrawWorldLight(numsurfaces, surfacelist, lighttrispvs);
3304                                 for (i = 0;i < numlightentities;i++)
3305                                         R_Shadow_DrawEntityLight(lightentities[i]);
3306                         }
3307                 }
3308         }
3309         else
3310         {
3311                 if (numsurfaces + numlightentities)
3312                 {
3313                         // draw lighting in the unmasked areas
3314                         R_Shadow_RenderMode_Lighting(false, false);
3315                         if (numsurfaces)
3316                                 R_Shadow_DrawWorldLight(numsurfaces, surfacelist, lighttrispvs);
3317                         for (i = 0;i < numlightentities;i++)
3318                                 R_Shadow_DrawEntityLight(lightentities[i]);
3319                         for (i = 0;i < numlightentities_noselfshadow;i++)
3320                                 R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]);
3321
3322                         // optionally draw the illuminated areas
3323                         // for performance analysis by level designers
3324                         if (r_showlighting.integer && r_refdef.view.showdebug)
3325                         {
3326                                 R_Shadow_RenderMode_VisibleLighting(false, false);
3327                                 if (numsurfaces)
3328                                         R_Shadow_DrawWorldLight(numsurfaces, surfacelist, lighttrispvs);
3329                                 for (i = 0;i < numlightentities;i++)
3330                                         R_Shadow_DrawEntityLight(lightentities[i]);
3331                                 for (i = 0;i < numlightentities_noselfshadow;i++)
3332                                         R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]);
3333                         }
3334                 }
3335         }
3336 }
3337
3338 void R_Shadow_DrawLightSprites(void);
3339 void R_ShadowVolumeLighting(qboolean visible)
3340 {
3341         int flag;
3342         int lnum;
3343         size_t lightindex;
3344         dlight_t *light;
3345         size_t range;
3346
3347         if (r_editlights.integer)
3348                 R_Shadow_DrawLightSprites();
3349
3350         R_Shadow_RenderMode_Begin();
3351
3352         flag = r_refdef.scene.rtworld ? LIGHTFLAG_REALTIMEMODE : LIGHTFLAG_NORMALMODE;
3353         if (r_shadow_debuglight.integer >= 0)
3354         {
3355                 lightindex = r_shadow_debuglight.integer;
3356                 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
3357                 if (light && (light->flags & flag))
3358                         R_DrawRTLight(&light->rtlight, visible);
3359         }
3360         else
3361         {
3362                 range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
3363                 for (lightindex = 0;lightindex < range;lightindex++)
3364                 {
3365                         light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
3366                         if (light && (light->flags & flag))
3367                                 R_DrawRTLight(&light->rtlight, visible);
3368                 }
3369         }
3370         if (r_refdef.scene.rtdlight)
3371                 for (lnum = 0;lnum < r_refdef.scene.numlights;lnum++)
3372                         R_DrawRTLight(r_refdef.scene.lights[lnum], visible);
3373
3374         R_Shadow_RenderMode_End();
3375 }
3376
3377 extern void R_SetupView(qboolean allowwaterclippingplane);
3378 extern cvar_t r_shadows;
3379 extern cvar_t r_shadows_throwdistance;
3380 void R_DrawModelShadows(void)
3381 {
3382         int i;
3383         float relativethrowdistance;
3384         entity_render_t *ent;
3385         vec3_t relativelightorigin;
3386         vec3_t relativelightdirection;
3387         vec3_t relativeshadowmins, relativeshadowmaxs;
3388         vec3_t tmp;
3389         float vertex3f[12];
3390
3391         if (!r_drawentities.integer || !gl_stencil)
3392                 return;
3393
3394         CHECKGLERROR
3395         GL_Scissor(r_refdef.view.x, r_refdef.view.y, r_refdef.view.width, r_refdef.view.height);
3396
3397         r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
3398
3399         if (gl_ext_separatestencil.integer)
3400         {
3401                 r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_SEPARATESTENCIL;
3402                 r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_SEPARATESTENCIL;
3403         }
3404         else if (gl_ext_stenciltwoside.integer)
3405         {
3406                 r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE;
3407                 r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE;
3408         }
3409         else
3410         {
3411                 r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_STENCIL;
3412                 r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_STENCIL;
3413         }
3414
3415         R_Shadow_ClearStencil();
3416
3417         for (i = 0;i < r_refdef.scene.numentities;i++)
3418         {
3419                 ent = r_refdef.scene.entities[i];
3420                 // cast shadows from anything that is not a submodel of the map
3421                 if (ent->model && ent->model->DrawShadowVolume != NULL && !ent->model->brush.submodel && (ent->flags & RENDER_SHADOW))
3422                 {
3423                         relativethrowdistance = r_shadows_throwdistance.value * Matrix4x4_ScaleFromMatrix(&ent->inversematrix);
3424                         VectorSet(relativeshadowmins, -relativethrowdistance, -relativethrowdistance, -relativethrowdistance);
3425                         VectorSet(relativeshadowmaxs, relativethrowdistance, relativethrowdistance, relativethrowdistance);
3426
3427                         if(r_shadows.integer == 2)
3428                         {
3429                                 // 2: simpler mode, throw shadows always DOWN
3430                                 VectorSet(tmp, 0, 0, -1);
3431                                 Matrix4x4_Transform3x3(&ent->inversematrix, tmp, relativelightdirection);
3432                         }
3433                         else
3434                         {
3435                                 if(ent->entitynumber != 0)
3436                                 {
3437                                         // networked entity - might be attached in some way (then we should use the parent's light direction, to not tear apart attached entities)
3438                                         int entnum, entnum2, recursion;
3439                                         entnum = entnum2 = ent->entitynumber;
3440                                         for(recursion = 32; recursion > 0; --recursion)
3441                                         {
3442                                                 entnum2 = cl.entities[entnum].state_current.tagentity;
3443                                                 if(entnum2 >= 1 && entnum2 < cl.num_entities && cl.entities_active[entnum2])
3444                                                         entnum = entnum2;
3445                                                 else
3446                                                         break;
3447                                         }
3448                                         if(recursion && recursion != 32) // if we followed a valid non-empty attachment chain
3449                                         {
3450                                                 VectorNegate(cl.entities[entnum].render.modellight_lightdir, relativelightdirection);
3451                                                 // transform into modelspace of OUR entity
3452                                                 Matrix4x4_Transform3x3(&cl.entities[entnum].render.matrix, relativelightdirection, tmp);
3453                                                 Matrix4x4_Transform3x3(&ent->inversematrix, tmp, relativelightdirection);
3454                                         }
3455                                         else
3456                                                 VectorNegate(ent->modellight_lightdir, relativelightdirection);
3457                                 }
3458                                 else
3459                                         VectorNegate(ent->modellight_lightdir, relativelightdirection);
3460                         }
3461
3462                         VectorScale(relativelightdirection, -relativethrowdistance, relativelightorigin);
3463                         RSurf_ActiveModelEntity(ent, false, false);
3464                         ent->model->DrawShadowVolume(ent, relativelightorigin, relativelightdirection, relativethrowdistance, ent->model->nummodelsurfaces, ent->model->surfacelist, relativeshadowmins, relativeshadowmaxs);
3465                 }
3466         }
3467
3468         // not really the right mode, but this will disable any silly stencil features
3469         R_Shadow_RenderMode_VisibleLighting(true, true);
3470
3471         // vertex coordinates for a quad that covers the screen exactly
3472         vertex3f[0] = 0;vertex3f[1] = 0;vertex3f[2] = 0;
3473         vertex3f[3] = 1;vertex3f[4] = 0;vertex3f[5] = 0;
3474         vertex3f[6] = 1;vertex3f[7] = 1;vertex3f[8] = 0;
3475         vertex3f[9] = 0;vertex3f[10] = 1;vertex3f[11] = 0;
3476
3477         // set up ortho view for rendering this pass
3478         GL_SetupView_Mode_Ortho(0, 0, 1, 1, -10, 100);
3479         GL_Scissor(r_refdef.view.x, r_refdef.view.y, r_refdef.view.width, r_refdef.view.height);
3480         GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
3481         GL_ScissorTest(true);
3482         R_Mesh_Matrix(&identitymatrix);
3483         R_Mesh_ResetTextureState();
3484         R_Mesh_VertexPointer(vertex3f, 0, 0);
3485         R_Mesh_ColorPointer(NULL, 0, 0);
3486
3487         // set up a 50% darkening blend on shadowed areas
3488         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3489         GL_DepthRange(0, 1);
3490         GL_DepthTest(false);
3491         GL_DepthMask(false);
3492         GL_PolygonOffset(0, 0);CHECKGLERROR
3493         GL_Color(0, 0, 0, 0.5);
3494         GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
3495         qglDepthFunc(GL_ALWAYS);CHECKGLERROR
3496         qglEnable(GL_STENCIL_TEST);CHECKGLERROR
3497         qglStencilMask(~0);CHECKGLERROR
3498         qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);CHECKGLERROR
3499         qglStencilFunc(GL_NOTEQUAL, 128, ~0);CHECKGLERROR
3500
3501         // apply the blend to the shadowed areas
3502         R_Mesh_Draw(0, 4, 0, 2, NULL, polygonelements, 0, 0);
3503
3504         // restoring the perspective view is done by R_RenderScene
3505         //R_SetupView(true);
3506
3507         // restore other state to normal
3508         R_Shadow_RenderMode_End();
3509 }
3510
3511 void R_BeginCoronaQuery(rtlight_t *rtlight, float scale, qboolean usequery)
3512 {
3513         float zdist;
3514         vec3_t centerorigin;
3515         // if it's too close, skip it
3516         if (VectorLength(rtlight->color) < (1.0f / 256.0f))
3517                 return;
3518         zdist = (DotProduct(rtlight->shadoworigin, r_refdef.view.forward) - DotProduct(r_refdef.view.origin, r_refdef.view.forward));
3519         if (zdist < 32)
3520                 return;
3521         if (usequery && r_numqueries + 2 <= r_maxqueries)
3522         {
3523                 rtlight->corona_queryindex_allpixels = r_queries[r_numqueries++];
3524                 rtlight->corona_queryindex_visiblepixels = r_queries[r_numqueries++];
3525                 VectorMA(r_refdef.view.origin, zdist, r_refdef.view.forward, centerorigin);
3526
3527                 CHECKGLERROR
3528                 // NOTE: we can't disable depth testing using R_DrawSprite's depthdisable argument, which calls GL_DepthTest, as that's broken in the ATI drivers
3529                 qglBeginQueryARB(GL_SAMPLES_PASSED_ARB, rtlight->corona_queryindex_allpixels);
3530                 qglDepthFunc(GL_ALWAYS);
3531                 R_DrawSprite(GL_ONE, GL_ZERO, r_shadow_lightcorona, NULL, false, false, centerorigin, r_refdef.view.right, r_refdef.view.up, scale, -scale, -scale, scale, 1, 1, 1, 1);
3532                 qglEndQueryARB(GL_SAMPLES_PASSED_ARB);
3533                 qglDepthFunc(GL_LEQUAL);
3534                 qglBeginQueryARB(GL_SAMPLES_PASSED_ARB, rtlight->corona_queryindex_visiblepixels);
3535                 R_DrawSprite(GL_ONE, GL_ZERO, r_shadow_lightcorona, NULL, false, false, rtlight->shadoworigin, r_refdef.view.right, r_refdef.view.up, scale, -scale, -scale, scale, 1, 1, 1, 1);
3536                 qglEndQueryARB(GL_SAMPLES_PASSED_ARB);
3537                 CHECKGLERROR
3538         }
3539         rtlight->corona_visibility = bound(0, (zdist - 32) / 32, 1);
3540 }
3541
3542 void R_DrawCorona(rtlight_t *rtlight, float cscale, float scale)
3543 {
3544         vec3_t color;
3545         GLint allpixels = 0, visiblepixels = 0;
3546         // now we have to check the query result
3547         if (rtlight->corona_queryindex_visiblepixels)
3548         {
3549                 CHECKGLERROR
3550                 qglGetQueryObjectivARB(rtlight->corona_queryindex_visiblepixels, GL_QUERY_RESULT_ARB, &visiblepixels);
3551                 qglGetQueryObjectivARB(rtlight->corona_queryindex_allpixels, GL_QUERY_RESULT_ARB, &allpixels);
3552                 CHECKGLERROR
3553                 //Con_Printf("%i of %i pixels\n", (int)visiblepixels, (int)allpixels);
3554                 if (visiblepixels < 1 || allpixels < 1)
3555                         return;
3556                 rtlight->corona_visibility *= bound(0, (float)visiblepixels / (float)allpixels, 1);
3557                 cscale *= rtlight->corona_visibility;
3558         }
3559         else
3560         {
3561                 // FIXME: these traces should scan all render entities instead of cl.world
3562                 if (CL_Move(r_refdef.view.origin, vec3_origin, vec3_origin, rtlight->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false).fraction < 1)
3563                         return;
3564         }
3565         VectorScale(rtlight->color, cscale, color);
3566         if (VectorLength(color) > (1.0f / 256.0f))
3567                 R_DrawSprite(GL_ONE, GL_ONE, r_shadow_lightcorona, NULL, true, false, rtlight->shadoworigin, r_refdef.view.right, r_refdef.view.up, scale, -scale, -scale, scale, color[0], color[1], color[2], 1);
3568 }
3569
3570 void R_DrawCoronas(void)
3571 {
3572         int i, flag;
3573         qboolean usequery;
3574         size_t lightindex;
3575         dlight_t *light;
3576         rtlight_t *rtlight;
3577         size_t range;
3578         if (r_coronas.value < (1.0f / 256.0f) && !gl_flashblend.integer)
3579                 return;
3580         if (r_waterstate.renderingscene)
3581                 return;
3582         flag = r_refdef.scene.rtworld ? LIGHTFLAG_REALTIMEMODE : LIGHTFLAG_NORMALMODE;
3583         R_Mesh_Matrix(&identitymatrix);
3584
3585         range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
3586
3587         // check occlusion of coronas
3588         // use GL_ARB_occlusion_query if available
3589         // otherwise use raytraces
3590         r_numqueries = 0;
3591         usequery = gl_support_arb_occlusion_query && r_coronas_occlusionquery.integer;
3592         if (usequery)
3593         {
3594                 GL_ColorMask(0,0,0,0);
3595                 if (r_maxqueries < (range + r_refdef.scene.numlights) * 2)
3596                 if (r_maxqueries < R_MAX_OCCLUSION_QUERIES)
3597                 {
3598                         i = r_maxqueries;
3599                         r_maxqueries = (range + r_refdef.scene.numlights) * 4;
3600                         r_maxqueries = min(r_maxqueries, R_MAX_OCCLUSION_QUERIES);
3601                         CHECKGLERROR
3602                         qglGenQueriesARB(r_maxqueries - i, r_queries + i);
3603                         CHECKGLERROR
3604                 }
3605         }
3606         for (lightindex = 0;lightindex < range;lightindex++)
3607         {
3608                 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
3609                 if (!light)
3610                         continue;
3611                 rtlight = &light->rtlight;
3612                 rtlight->corona_visibility = 0;
3613                 rtlight->corona_queryindex_visiblepixels = 0;
3614                 rtlight->corona_queryindex_allpixels = 0;
3615                 if (!(rtlight->flags & flag))
3616                         continue;
3617                 if (rtlight->corona <= 0)
3618                         continue;
3619                 if (r_shadow_debuglight.integer >= 0 && r_shadow_debuglight.integer != (int)lightindex)
3620                         continue;
3621                 R_BeginCoronaQuery(rtlight, rtlight->radius * rtlight->coronasizescale * r_coronas_occlusionsizescale.value, usequery);
3622         }
3623         for (i = 0;i < r_refdef.scene.numlights;i++)
3624         {
3625                 rtlight = r_refdef.scene.lights[i];
3626                 rtlight->corona_visibility = 0;
3627                 rtlight->corona_queryindex_visiblepixels = 0;
3628                 rtlight->corona_queryindex_allpixels = 0;
3629                 if (!(rtlight->flags & flag))
3630                         continue;
3631                 if (rtlight->corona <= 0)
3632                         continue;
3633                 R_BeginCoronaQuery(rtlight, rtlight->radius * rtlight->coronasizescale * r_coronas_occlusionsizescale.value, usequery);
3634         }
3635         if (usequery)
3636                 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
3637
3638         // now draw the coronas using the query data for intensity info
3639         for (lightindex = 0;lightindex < range;lightindex++)
3640         {
3641                 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
3642                 if (!light)
3643                         continue;
3644                 rtlight = &light->rtlight;
3645                 if (rtlight->corona_visibility <= 0)
3646                         continue;
3647                 R_DrawCorona(rtlight, rtlight->corona * r_coronas.value * 0.25f, rtlight->radius * rtlight->coronasizescale);
3648         }
3649         for (i = 0;i < r_refdef.scene.numlights;i++)
3650         {
3651                 rtlight = r_refdef.scene.lights[i];
3652                 if (rtlight->corona_visibility <= 0)
3653                         continue;
3654                 if (gl_flashblend.integer)
3655                         R_DrawCorona(rtlight, rtlight->corona, rtlight->radius * rtlight->coronasizescale * 2.0f);
3656                 else
3657                         R_DrawCorona(rtlight, rtlight->corona * r_coronas.value * 0.25f, rtlight->radius * rtlight->coronasizescale);
3658         }
3659 }
3660
3661
3662
3663 //static char *suffix[6] = {"ft", "bk", "rt", "lf", "up", "dn"};
3664 typedef struct suffixinfo_s
3665 {
3666         char *suffix;
3667         qboolean flipx, flipy, flipdiagonal;
3668 }
3669 suffixinfo_t;
3670 static suffixinfo_t suffix[3][6] =
3671 {
3672         {
3673                 {"px",   false, false, false},
3674                 {"nx",   false, false, false},
3675                 {"py",   false, false, false},
3676                 {"ny",   false, false, false},
3677                 {"pz",   false, false, false},
3678                 {"nz",   false, false, false}
3679         },
3680         {
3681                 {"posx", false, false, false},
3682                 {"negx", false, false, false},
3683                 {"posy", false, false, false},
3684                 {"negy", false, false, false},
3685                 {"posz", false, false, false},
3686                 {"negz", false, false, false}
3687         },
3688         {
3689                 {"rt",    true, false,  true},
3690                 {"lf",   false,  true,  true},
3691                 {"ft",    true,  true, false},
3692                 {"bk",   false, false, false},
3693                 {"up",    true, false,  true},
3694                 {"dn",    true, false,  true}
3695         }
3696 };
3697
3698 static int componentorder[4] = {0, 1, 2, 3};
3699
3700 rtexture_t *R_Shadow_LoadCubemap(const char *basename)
3701 {
3702         int i, j, cubemapsize;
3703         unsigned char *cubemappixels, *image_buffer;
3704         rtexture_t *cubemaptexture;
3705         char name[256];
3706         // must start 0 so the first loadimagepixels has no requested width/height
3707         cubemapsize = 0;
3708         cubemappixels = NULL;
3709         cubemaptexture = NULL;
3710         // keep trying different suffix groups (posx, px, rt) until one loads
3711         for (j = 0;j < 3 && !cubemappixels;j++)
3712         {
3713                 // load the 6 images in the suffix group
3714                 for (i = 0;i < 6;i++)
3715                 {
3716                         // generate an image name based on the base and and suffix
3717                         dpsnprintf(name, sizeof(name), "%s%s", basename, suffix[j][i].suffix);
3718                         // load it
3719                         if ((image_buffer = loadimagepixelsbgra(name, false, false)))
3720                         {
3721                                 // an image loaded, make sure width and height are equal
3722                                 if (image_width == image_height && (!cubemappixels || image_width == cubemapsize))
3723                                 {
3724                                         // if this is the first image to load successfully, allocate the cubemap memory
3725                                         if (!cubemappixels && image_width >= 1)
3726                                         {
3727                                                 cubemapsize = image_width;
3728                                                 // note this clears to black, so unavailable sides are black
3729                                                 cubemappixels = (unsigned char *)Mem_Alloc(tempmempool, 6*cubemapsize*cubemapsize*4);
3730                                         }
3731                                         // copy the image with any flipping needed by the suffix (px and posx types don't need flipping)
3732                                         if (cubemappixels)
3733                                                 Image_CopyMux(cubemappixels+i*cubemapsize*cubemapsize*4, image_buffer, cubemapsize, cubemapsize, suffix[j][i].flipx, suffix[j][i].flipy, suffix[j][i].flipdiagonal, 4, 4, componentorder);
3734                                 }
3735                                 else
3736                                         Con_Printf("Cubemap image \"%s\" (%ix%i) is not square, OpenGL requires square cubemaps.\n", name, image_width, image_height);
3737                                 // free the image
3738                                 Mem_Free(image_buffer);
3739                         }
3740                 }
3741         }
3742         // if a cubemap loaded, upload it
3743         if (cubemappixels)
3744         {
3745                 if (developer_loading.integer)
3746                         Con_Printf("loading cubemap \"%s\"\n", basename);
3747
3748                 if (!r_shadow_filters_texturepool)
3749                         r_shadow_filters_texturepool = R_AllocTexturePool();
3750                 cubemaptexture = R_LoadTextureCubeMap(r_shadow_filters_texturepool, basename, cubemapsize, cubemappixels, TEXTYPE_BGRA, TEXF_PRECACHE | (gl_texturecompression_lightcubemaps.integer ? TEXF_COMPRESS : 0) | TEXF_FORCELINEAR, NULL);
3751                 Mem_Free(cubemappixels);
3752         }
3753         else
3754         {
3755                 Con_DPrintf("failed to load cubemap \"%s\"\n", basename);
3756                 if (developer_loading.integer)
3757                 {
3758                         Con_Printf("(tried tried images ");
3759                         for (j = 0;j < 3;j++)
3760                                 for (i = 0;i < 6;i++)
3761                                         Con_Printf("%s\"%s%s.tga\"", j + i > 0 ? ", " : "", basename, suffix[j][i].suffix);
3762                         Con_Print(" and was unable to find any of them).\n");
3763                 }
3764         }
3765         return cubemaptexture;
3766 }
3767
3768 rtexture_t *R_Shadow_Cubemap(const char *basename)
3769 {
3770         int i;
3771         for (i = 0;i < numcubemaps;i++)
3772                 if (!strcasecmp(cubemaps[i].basename, basename))
3773                         return cubemaps[i].texture ? cubemaps[i].texture : r_texture_whitecube;
3774         if (i >= MAX_CUBEMAPS)
3775                 return r_texture_whitecube;
3776         numcubemaps++;
3777         strlcpy(cubemaps[i].basename, basename, sizeof(cubemaps[i].basename));
3778         cubemaps[i].texture = R_Shadow_LoadCubemap(cubemaps[i].basename);
3779         return cubemaps[i].texture;
3780 }
3781
3782 void R_Shadow_FreeCubemaps(void)
3783 {
3784         int i;
3785         for (i = 0;i < numcubemaps;i++)
3786         {
3787                 if (developer_loading.integer)
3788                         Con_Printf("unloading cubemap \"%s\"\n", cubemaps[i].basename);
3789                 if (cubemaps[i].texture)
3790                         R_FreeTexture(cubemaps[i].texture);
3791         }
3792
3793         numcubemaps = 0;
3794         R_FreeTexturePool(&r_shadow_filters_texturepool);
3795 }
3796
3797 dlight_t *R_Shadow_NewWorldLight(void)
3798 {
3799         return (dlight_t *)Mem_ExpandableArray_AllocRecord(&r_shadow_worldlightsarray);
3800 }
3801
3802 void R_Shadow_UpdateWorldLight(dlight_t *light, vec3_t origin, vec3_t angles, vec3_t color, vec_t radius, vec_t corona, int style, int shadowenable, const char *cubemapname, vec_t coronasizescale, vec_t ambientscale, vec_t diffusescale, vec_t specularscale, int flags)
3803 {
3804         matrix4x4_t matrix;
3805         // validate parameters
3806         if (style < 0 || style >= MAX_LIGHTSTYLES)
3807         {
3808                 Con_Printf("R_Shadow_NewWorldLight: invalid light style number %i, must be >= 0 and < %i\n", light->style, MAX_LIGHTSTYLES);
3809                 style = 0;
3810         }
3811         if (!cubemapname)
3812                 cubemapname = "";
3813
3814         // copy to light properties
3815         VectorCopy(origin, light->origin);
3816         light->angles[0] = angles[0] - 360 * floor(angles[0] / 360);
3817         light->angles[1] = angles[1] - 360 * floor(angles[1] / 360);
3818         light->angles[2] = angles[2] - 360 * floor(angles[2] / 360);
3819         light->color[0] = max(color[0], 0);
3820         light->color[1] = max(color[1], 0);
3821         light->color[2] = max(color[2], 0);
3822         light->radius = max(radius, 0);
3823         light->style = style;
3824         light->shadow = shadowenable;
3825         light->corona = corona;
3826         strlcpy(light->cubemapname, cubemapname, sizeof(light->cubemapname));
3827         light->coronasizescale = coronasizescale;
3828         light->ambientscale = ambientscale;
3829         light->diffusescale = diffusescale;
3830         light->specularscale = specularscale;
3831         light->flags = flags;
3832
3833         // update renderable light data
3834         Matrix4x4_CreateFromQuakeEntity(&matrix, light->origin[0], light->origin[1], light->origin[2], light->angles[0], light->angles[1], light->angles[2], light->radius);
3835         R_RTLight_Update(&light->rtlight, true, &matrix, light->color, light->style, light->cubemapname[0] ? light->cubemapname : NULL, light->shadow, light->corona, light->coronasizescale, light->ambientscale, light->diffusescale, light->specularscale, light->flags);
3836 }
3837
3838 void R_Shadow_FreeWorldLight(dlight_t *light)
3839 {
3840         if (r_shadow_selectedlight == light)
3841                 r_shadow_selectedlight = NULL;
3842         R_RTLight_Uncompile(&light->rtlight);
3843         Mem_ExpandableArray_FreeRecord(&r_shadow_worldlightsarray, light);
3844 }
3845
3846 void R_Shadow_ClearWorldLights(void)
3847 {
3848         size_t lightindex;
3849         dlight_t *light;
3850         size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
3851         for (lightindex = 0;lightindex < range;lightindex++)
3852         {
3853                 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
3854                 if (light)
3855                         R_Shadow_FreeWorldLight(light);
3856         }
3857         r_shadow_selectedlight = NULL;
3858         R_Shadow_FreeCubemaps();
3859 }
3860
3861 void R_Shadow_SelectLight(dlight_t *light)
3862 {
3863         if (r_shadow_selectedlight)
3864                 r_shadow_selectedlight->selected = false;
3865         r_shadow_selectedlight = light;
3866         if (r_shadow_selectedlight)
3867                 r_shadow_selectedlight->selected = true;
3868 }
3869
3870 void R_Shadow_DrawCursor_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
3871 {
3872         // this is never batched (there can be only one)
3873         R_DrawSprite(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, r_editlights_sprcursor->tex, r_editlights_sprcursor->tex, false, false, r_editlights_cursorlocation, r_refdef.view.right, r_refdef.view.up, EDLIGHTSPRSIZE, -EDLIGHTSPRSIZE, -EDLIGHTSPRSIZE, EDLIGHTSPRSIZE, 1, 1, 1, 1);
3874 }
3875
3876 void R_Shadow_DrawLightSprite_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
3877 {
3878         float intensity;
3879         float s;
3880         vec3_t spritecolor;
3881         cachepic_t *pic;
3882
3883         // this is never batched (due to the ent parameter changing every time)
3884         // so numsurfaces == 1 and surfacelist[0] == lightnumber
3885         const dlight_t *light = (dlight_t *)ent;
3886         s = EDLIGHTSPRSIZE;
3887         intensity = 0.5f;
3888         VectorScale(light->color, intensity, spritecolor);
3889         if (VectorLength(spritecolor) < 0.1732f)
3890                 VectorSet(spritecolor, 0.1f, 0.1f, 0.1f);
3891         if (VectorLength(spritecolor) > 1.0f)
3892                 VectorNormalize(spritecolor);
3893
3894         // draw light sprite
3895         if (light->cubemapname[0] && !light->shadow)
3896                 pic = r_editlights_sprcubemapnoshadowlight;
3897         else if (light->cubemapname[0])
3898                 pic = r_editlights_sprcubemaplight;
3899         else if (!light->shadow)
3900                 pic = r_editlights_sprnoshadowlight;
3901         else
3902                 pic = r_editlights_sprlight;
3903         R_DrawSprite(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, pic->tex, pic->tex, false, false, light->origin, r_refdef.view.right, r_refdef.view.up, s, -s, -s, s, spritecolor[0], spritecolor[1], spritecolor[2], 1);
3904         // draw selection sprite if light is selected
3905         if (light->selected)
3906                 R_DrawSprite(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, r_editlights_sprselection->tex, r_editlights_sprselection->tex, false, false, light->origin, r_refdef.view.right, r_refdef.view.up, s, -s, -s, s, 1, 1, 1, 1);
3907         // VorteX todo: add normalmode/realtime mode light overlay sprites?
3908 }
3909
3910 void R_Shadow_DrawLightSprites(void)
3911 {
3912         size_t lightindex;
3913         dlight_t *light;
3914         size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
3915         for (lightindex = 0;lightindex < range;lightindex++)
3916         {
3917                 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
3918                 if (light)
3919                         R_MeshQueue_AddTransparent(light->origin, R_Shadow_DrawLightSprite_TransparentCallback, (entity_render_t *)light, 5, &light->rtlight);
3920         }
3921         R_MeshQueue_AddTransparent(r_editlights_cursorlocation, R_Shadow_DrawCursor_TransparentCallback, NULL, 0, NULL);
3922 }
3923
3924 void R_Shadow_SelectLightInView(void)
3925 {
3926         float bestrating, rating, temp[3];
3927         dlight_t *best;
3928         size_t lightindex;
3929         dlight_t *light;
3930         size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
3931         best = NULL;
3932         bestrating = 0;
3933         for (lightindex = 0;lightindex < range;lightindex++)
3934         {
3935                 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
3936                 if (!light)
3937                         continue;
3938                 VectorSubtract(light->origin, r_refdef.view.origin, temp);
3939                 rating = (DotProduct(temp, r_refdef.view.forward) / sqrt(DotProduct(temp, temp)));
3940                 if (rating >= 0.95)
3941                 {
3942                         rating /= (1 + 0.0625f * sqrt(DotProduct(temp, temp)));
3943                         if (bestrating < rating && CL_Move(light->origin, vec3_origin, vec3_origin, r_refdef.view.origin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false).fraction == 1.0f)
3944                         {
3945                                 bestrating = rating;
3946                                 best = light;
3947                         }
3948                 }
3949         }
3950         R_Shadow_SelectLight(best);
3951 }
3952
3953 void R_Shadow_LoadWorldLights(void)
3954 {
3955         int n, a, style, shadow, flags;
3956         char tempchar, *lightsstring, *s, *t, name[MAX_QPATH], cubemapname[MAX_QPATH];
3957         float origin[3], radius, color[3], angles[3], corona, coronasizescale, ambientscale, diffusescale, specularscale;
3958         if (cl.worldmodel == NULL)
3959         {
3960                 Con_Print("No map loaded.\n");
3961                 return;
3962         }
3963         FS_StripExtension (cl.worldmodel->name, name, sizeof (name));
3964         strlcat (name, ".rtlights", sizeof (name));
3965         lightsstring = (char *)FS_LoadFile(name, tempmempool, false, NULL);
3966         if (lightsstring)
3967         {
3968                 s = lightsstring;
3969                 n = 0;
3970                 while (*s)
3971                 {
3972                         t = s;
3973                         /*
3974                         shadow = true;
3975                         for (;COM_Parse(t, true) && strcmp(
3976                         if (COM_Parse(t, true))
3977                         {
3978                                 if (com_token[0] == '!')
3979                                 {
3980                                         shadow = false;
3981                                         origin[0] = atof(com_token+1);
3982                                 }
3983                                 else
3984                                         origin[0] = atof(com_token);
3985                                 if (Com_Parse(t
3986                         }
3987                         */
3988                         t = s;
3989                         while (*s && *s != '\n' && *s != '\r')
3990                                 s++;
3991                         if (!*s)
3992                                 break;
3993                         tempchar = *s;
3994                         shadow = true;
3995                         // check for modifier flags
3996                         if (*t == '!')
3997                         {
3998                                 shadow = false;
3999                                 t++;
4000                         }
4001                         *s = 0;
4002 #if _MSC_VER >= 1400
4003 #define sscanf sscanf_s
4004 #endif
4005                         cubemapname[sizeof(cubemapname)-1] = 0;
4006 #if MAX_QPATH != 128
4007 #error update this code if MAX_QPATH changes
4008 #endif
4009                         a = sscanf(t, "%f %f %f %f %f %f %f %d %127s %f %f %f %f %f %f %f %f %i", &origin[0], &origin[1], &origin[2], &radius, &color[0], &color[1], &color[2], &style, cubemapname
4010 #if _MSC_VER >= 1400
4011 , sizeof(cubemapname)
4012 #endif
4013 , &corona, &angles[0], &angles[1], &angles[2], &coronasizescale, &ambientscale, &diffusescale, &specularscale, &flags);
4014                         *s = tempchar;
4015                         if (a < 18)
4016                                 flags = LIGHTFLAG_REALTIMEMODE;
4017                         if (a < 17)
4018                                 specularscale = 1;
4019                         if (a < 16)
4020                                 diffusescale = 1;
4021                         if (a < 15)
4022                                 ambientscale = 0;
4023                         if (a < 14)
4024                                 coronasizescale = 0.25f;
4025                         if (a < 13)
4026                                 VectorClear(angles);
4027                         if (a < 10)
4028                                 corona = 0;
4029                         if (a < 9 || !strcmp(cubemapname, "\"\""))
4030                                 cubemapname[0] = 0;
4031                         // remove quotes on cubemapname
4032                         if (cubemapname[0] == '"' && cubemapname[strlen(cubemapname) - 1] == '"')
4033                         {
4034                                 size_t namelen;
4035                                 namelen = strlen(cubemapname) - 2;
4036                                 memmove(cubemapname, cubemapname + 1, namelen);
4037                                 cubemapname[namelen] = '\0';
4038                         }
4039                         if (a < 8)
4040                         {
4041                                 Con_Printf("found %d parameters on line %i, should be 8 or more parameters (origin[0] origin[1] origin[2] radius color[0] color[1] color[2] style \"cubemapname\" corona angles[0] angles[1] angles[2] coronasizescale ambientscale diffusescale specularscale flags)\n", a, n + 1);
4042                                 break;
4043                         }
4044                         R_Shadow_UpdateWorldLight(R_Shadow_NewWorldLight(), origin, angles, color, radius, corona, style, shadow, cubemapname, coronasizescale, ambientscale, diffusescale, specularscale, flags);
4045                         if (*s == '\r')
4046                                 s++;
4047                         if (*s == '\n')
4048                                 s++;
4049                         n++;
4050                 }
4051                 if (*s)
4052                         Con_Printf("invalid rtlights file \"%s\"\n", name);
4053                 Mem_Free(lightsstring);
4054         }
4055 }
4056
4057 void R_Shadow_SaveWorldLights(void)
4058 {
4059         size_t lightindex;
4060         dlight_t *light;
4061         size_t bufchars, bufmaxchars;
4062         char *buf, *oldbuf;
4063         char name[MAX_QPATH];
4064         char line[MAX_INPUTLINE];
4065         size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked, assuming the dpsnprintf mess doesn't screw it up...
4066         // I hate lines which are 3 times my screen size :( --blub
4067         if (!range)
4068                 return;
4069         if (cl.worldmodel == NULL)
4070         {
4071                 Con_Print("No map loaded.\n");
4072                 return;
4073         }
4074         FS_StripExtension (cl.worldmodel->name, name, sizeof (name));
4075         strlcat (name, ".rtlights", sizeof (name));
4076         bufchars = bufmaxchars = 0;
4077         buf = NULL;
4078         for (lightindex = 0;lightindex < range;lightindex++)
4079         {
4080                 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
4081                 if (!light)
4082                         continue;
4083                 if (light->coronasizescale != 0.25f || light->ambientscale != 0 || light->diffusescale != 1 || light->specularscale != 1 || light->flags != LIGHTFLAG_REALTIMEMODE)
4084                         dpsnprintf(line, sizeof(line), "%s%f %f %f %f %f %f %f %d \"%s\" %f %f %f %f %f %f %f %f %i\n", light->shadow ? "" : "!", light->origin[0], light->origin[1], light->origin[2], light->radius, light->color[0], light->color[1], light->color[2], light->style, light->cubemapname, light->corona, light->angles[0], light->angles[1], light->angles[2], light->coronasizescale, light->ambientscale, light->diffusescale, light->specularscale, light->flags);
4085                 else if (light->cubemapname[0] || light->corona || light->angles[0] || light->angles[1] || light->angles[2])
4086                         dpsnprintf(line, sizeof(line), "%s%f %f %f %f %f %f %f %d \"%s\" %f %f %f %f\n", light->shadow ? "" : "!", light->origin[0], light->origin[1], light->origin[2], light->radius, light->color[0], light->color[1], light->color[2], light->style, light->cubemapname, light->corona, light->angles[0], light->angles[1], light->angles[2]);
4087                 else
4088                         dpsnprintf(line, sizeof(line), "%s%f %f %f %f %f %f %f %d\n", light->shadow ? "" : "!", light->origin[0], light->origin[1], light->origin[2], light->radius, light->color[0], light->color[1], light->color[2], light->style);
4089                 if (bufchars + strlen(line) > bufmaxchars)
4090                 {
4091                         bufmaxchars = bufchars + strlen(line) + 2048;
4092                         oldbuf = buf;
4093                         buf = (char *)Mem_Alloc(tempmempool, bufmaxchars);
4094                         if (oldbuf)
4095                         {
4096                                 if (bufchars)
4097                                         memcpy(buf, oldbuf, bufchars);
4098                                 Mem_Free(oldbuf);
4099                         }
4100                 }
4101                 if (strlen(line))
4102                 {
4103                         memcpy(buf + bufchars, line, strlen(line));
4104                         bufchars += strlen(line);
4105                 }
4106         }
4107         if (bufchars)
4108                 FS_WriteFile(name, buf, (fs_offset_t)bufchars);
4109         if (buf)
4110                 Mem_Free(buf);
4111 }
4112
4113 void R_Shadow_LoadLightsFile(void)
4114 {
4115         int n, a, style;
4116         char tempchar, *lightsstring, *s, *t, name[MAX_QPATH];
4117         float origin[3], radius, color[3], subtract, spotdir[3], spotcone, falloff, distbias;
4118         if (cl.worldmodel == NULL)
4119         {
4120                 Con_Print("No map loaded.\n");
4121                 return;
4122         }
4123         FS_StripExtension (cl.worldmodel->name, name, sizeof (name));
4124         strlcat (name, ".lights", sizeof (name));
4125         lightsstring = (char *)FS_LoadFile(name, tempmempool, false, NULL);
4126         if (lightsstring)
4127         {
4128                 s = lightsstring;
4129                 n = 0;
4130                 while (*s)
4131                 {
4132                         t = s;
4133                         while (*s && *s != '\n' && *s != '\r')
4134                                 s++;
4135                         if (!*s)
4136                                 break;
4137                         tempchar = *s;
4138                         *s = 0;
4139                         a = sscanf(t, "%f %f %f %f %f %f %f %f %f %f %f %f %f %d", &origin[0], &origin[1], &origin[2], &falloff, &color[0], &color[1], &color[2], &subtract, &spotdir[0], &spotdir[1], &spotdir[2], &spotcone, &distbias, &style);
4140                         *s = tempchar;
4141                         if (a < 14)
4142                         {
4143                                 Con_Printf("invalid lights file, found %d parameters on line %i, should be 14 parameters (origin[0] origin[1] origin[2] falloff light[0] light[1] light[2] subtract spotdir[0] spotdir[1] spotdir[2] spotcone distancebias style)\n", a, n + 1);
4144                                 break;
4145                         }
4146                         radius = sqrt(DotProduct(color, color) / (falloff * falloff * 8192.0f * 8192.0f));
4147                         radius = bound(15, radius, 4096);
4148                         VectorScale(color, (2.0f / (8388608.0f)), color);
4149                         R_Shadow_UpdateWorldLight(R_Shadow_NewWorldLight(), origin, vec3_origin, color, radius, 0, style, true, NULL, 0.25, 0, 1, 1, LIGHTFLAG_REALTIMEMODE);
4150                         if (*s == '\r')
4151                                 s++;
4152                         if (*s == '\n')
4153                                 s++;
4154                         n++;
4155                 }
4156                 if (*s)
4157                         Con_Printf("invalid lights file \"%s\"\n", name);
4158                 Mem_Free(lightsstring);
4159         }
4160 }
4161
4162 // tyrlite/hmap2 light types in the delay field
4163 typedef enum lighttype_e {LIGHTTYPE_MINUSX, LIGHTTYPE_RECIPX, LIGHTTYPE_RECIPXX, LIGHTTYPE_NONE, LIGHTTYPE_SUN, LIGHTTYPE_MINUSXX} lighttype_t;
4164
4165 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void)
4166 {
4167         int entnum, style, islight, skin, pflags, effects, type, n;
4168         char *entfiledata;
4169         const char *data;
4170         float origin[3], angles[3], radius, color[3], light[4], fadescale, lightscale, originhack[3], overridecolor[3], vec[4];
4171         char key[256], value[MAX_INPUTLINE];
4172
4173         if (cl.worldmodel == NULL)
4174         {
4175                 Con_Print("No map loaded.\n");
4176                 return;
4177         }
4178         // try to load a .ent file first
4179         FS_StripExtension (cl.worldmodel->name, key, sizeof (key));
4180         strlcat (key, ".ent", sizeof (key));
4181         data = entfiledata = (char *)FS_LoadFile(key, tempmempool, true, NULL);
4182         // and if that is not found, fall back to the bsp file entity string
4183         if (!data)
4184                 data = cl.worldmodel->brush.entities;
4185         if (!data)
4186                 return;
4187         for (entnum = 0;COM_ParseToken_Simple(&data, false, false) && com_token[0] == '{';entnum++)
4188         {
4189                 type = LIGHTTYPE_MINUSX;
4190                 origin[0] = origin[1] = origin[2] = 0;
4191                 originhack[0] = originhack[1] = originhack[2] = 0;
4192                 angles[0] = angles[1] = angles[2] = 0;
4193                 color[0] = color[1] = color[2] = 1;
4194                 light[0] = light[1] = light[2] = 1;light[3] = 300;
4195                 overridecolor[0] = overridecolor[1] = overridecolor[2] = 1;
4196                 fadescale = 1;
4197                 lightscale = 1;
4198                 style = 0;
4199                 skin = 0;
4200                 pflags = 0;
4201                 effects = 0;
4202                 islight = false;
4203                 while (1)
4204                 {
4205                         if (!COM_ParseToken_Simple(&data, false, false))
4206                                 break; // error
4207                         if (com_token[0] == '}')
4208                                 break; // end of entity
4209                         if (com_token[0] == '_')
4210                                 strlcpy(key, com_token + 1, sizeof(key));
4211                         else
4212                                 strlcpy(key, com_token, sizeof(key));
4213                         while (key[strlen(key)-1] == ' ') // remove trailing spaces
4214                                 key[strlen(key)-1] = 0;
4215                         if (!COM_ParseToken_Simple(&data, false, false))
4216                                 break; // error
4217                         strlcpy(value, com_token, sizeof(value));
4218
4219                         // now that we have the key pair worked out...
4220                         if (!strcmp("light", key))
4221                         {
4222                                 n = sscanf(value, "%f %f %f %f", &vec[0], &vec[1], &vec[2], &vec[3]);
4223                                 if (n == 1)
4224                                 {
4225                                         // quake
4226                                         light[0] = vec[0] * (1.0f / 256.0f);
4227                                         light[1] = vec[0] * (1.0f / 256.0f);
4228                                         light[2] = vec[0] * (1.0f / 256.0f);
4229                                         light[3] = vec[0];
4230                                 }
4231                                 else if (n == 4)
4232                                 {
4233                                         // halflife
4234                                         light[0] = vec[0] * (1.0f / 255.0f);
4235                                         light[1] = vec[1] * (1.0f / 255.0f);
4236                                         light[2] = vec[2] * (1.0f / 255.0f);
4237                                         light[3] = vec[3];
4238                                 }
4239                         }
4240                         else if (!strcmp("delay", key))
4241                                 type = atoi(value);
4242                         else if (!strcmp("origin", key))
4243                                 sscanf(value, "%f %f %f", &origin[0], &origin[1], &origin[2]);
4244                         else if (!strcmp("angle", key))
4245                                 angles[0] = 0, angles[1] = atof(value), angles[2] = 0;
4246                         else if (!strcmp("angles", key))
4247                                 sscanf(value, "%f %f %f", &angles[0], &angles[1], &angles[2]);
4248                         else if (!strcmp("color", key))
4249                                 sscanf(value, "%f %f %f", &color[0], &color[1], &color[2]);
4250                         else if (!strcmp("wait", key))
4251                                 fadescale = atof(value);
4252                         else if (!strcmp("classname", key))
4253                         {
4254                                 if (!strncmp(value, "light", 5))
4255                                 {
4256                                         islight = true;
4257                                         if (!strcmp(value, "light_fluoro"))
4258                                         {
4259                                                 originhack[0] = 0;
4260                                                 originhack[1] = 0;
4261                                                 originhack[2] = 0;
4262                                                 overridecolor[0] = 1;
4263                                                 overridecolor[1] = 1;
4264                                                 overridecolor[2] = 1;
4265                                         }
4266                                         if (!strcmp(value, "light_fluorospark"))
4267                                         {
4268                                                 originhack[0] = 0;
4269                                                 originhack[1] = 0;
4270                                                 originhack[2] = 0;
4271                                                 overridecolor[0] = 1;
4272                                                 overridecolor[1] = 1;
4273                                                 overridecolor[2] = 1;
4274                                         }
4275                                         if (!strcmp(value, "light_globe"))
4276                                         {
4277                                                 originhack[0] = 0;
4278                                                 originhack[1] = 0;
4279                                                 originhack[2] = 0;
4280                                                 overridecolor[0] = 1;
4281                                                 overridecolor[1] = 0.8;
4282                                                 overridecolor[2] = 0.4;
4283                                         }
4284                                         if (!strcmp(value, "light_flame_large_yellow"))
4285                                         {
4286                                                 originhack[0] = 0;
4287                                                 originhack[1] = 0;
4288                                                 originhack[2] = 0;
4289                                                 overridecolor[0] = 1;
4290                                                 overridecolor[1] = 0.5;
4291                                                 overridecolor[2] = 0.1;
4292                                         }
4293                                         if (!strcmp(value, "light_flame_small_yellow"))
4294                                         {
4295                                                 originhack[0] = 0;
4296                                                 originhack[1] = 0;
4297                                                 originhack[2] = 0;
4298                                                 overridecolor[0] = 1;
4299                                                 overridecolor[1] = 0.5;
4300                                                 overridecolor[2] = 0.1;
4301                                         }
4302                                         if (!strcmp(value, "light_torch_small_white"))
4303                                         {
4304                                                 originhack[0] = 0;
4305                                                 originhack[1] = 0;
4306                                                 originhack[2] = 0;
4307                                                 overridecolor[0] = 1;
4308                                                 overridecolor[1] = 0.5;
4309                                                 overridecolor[2] = 0.1;
4310                                         }
4311                                         if (!strcmp(value, "light_torch_small_walltorch"))
4312                                         {
4313                                                 originhack[0] = 0;
4314                                                 originhack[1] = 0;
4315                                                 originhack[2] = 0;
4316                                                 overridecolor[0] = 1;
4317                                                 overridecolor[1] = 0.5;
4318                                                 overridecolor[2] = 0.1;
4319                                         }
4320                                 }
4321                         }
4322                         else if (!strcmp("style", key))
4323                                 style = atoi(value);
4324                         else if (!strcmp("skin", key))
4325                                 skin = (int)atof(value);
4326                         else if (!strcmp("pflags", key))
4327                                 pflags = (int)atof(value);
4328                         else if (!strcmp("effects", key))
4329                                 effects = (int)atof(value);
4330                         else if (cl.worldmodel->type == mod_brushq3)
4331                         {
4332                                 if (!strcmp("scale", key))
4333                                         lightscale = atof(value);
4334                                 if (!strcmp("fade", key))
4335                                         fadescale = atof(value);
4336                         }
4337                 }
4338                 if (!islight)
4339                         continue;
4340                 if (lightscale <= 0)
4341                         lightscale = 1;
4342                 if (fadescale <= 0)
4343                         fadescale = 1;
4344                 if (color[0] == color[1] && color[0] == color[2])
4345                 {
4346                         color[0] *= overridecolor[0];
4347                         color[1] *= overridecolor[1];
4348                         color[2] *= overridecolor[2];
4349                 }
4350                 radius = light[3] * r_editlights_quakelightsizescale.value * lightscale / fadescale;
4351                 color[0] = color[0] * light[0];
4352                 color[1] = color[1] * light[1];
4353                 color[2] = color[2] * light[2];
4354                 switch (type)
4355                 {
4356                 case LIGHTTYPE_MINUSX:
4357                         break;
4358                 case LIGHTTYPE_RECIPX:
4359                         radius *= 2;
4360                         VectorScale(color, (1.0f / 16.0f), color);
4361                         break;
4362                 case LIGHTTYPE_RECIPXX:
4363                         radius *= 2;
4364                         VectorScale(color, (1.0f / 16.0f), color);
4365                         break;
4366                 default:
4367                 case LIGHTTYPE_NONE:
4368                         break;
4369                 case LIGHTTYPE_SUN:
4370                         break;
4371                 case LIGHTTYPE_MINUSXX:
4372                         break;
4373                 }
4374                 VectorAdd(origin, originhack, origin);
4375                 if (radius >= 1)
4376                         R_Shadow_UpdateWorldLight(R_Shadow_NewWorldLight(), origin, angles, color, radius, (pflags & PFLAGS_CORONA) != 0, style, (pflags & PFLAGS_NOSHADOW) == 0, skin >= 16 ? va("cubemaps/%i", skin) : NULL, 0.25, 0, 1, 1, LIGHTFLAG_REALTIMEMODE);
4377         }
4378         if (entfiledata)
4379                 Mem_Free(entfiledata);
4380 }
4381
4382
4383 void R_Shadow_SetCursorLocationForView(void)
4384 {
4385         vec_t dist, push;
4386         vec3_t dest, endpos;
4387         trace_t trace;
4388         VectorMA(r_refdef.view.origin, r_editlights_cursordistance.value, r_refdef.view.forward, dest);
4389         trace = CL_Move(r_refdef.view.origin, vec3_origin, vec3_origin, dest, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false);
4390         if (trace.fraction < 1)
4391         {
4392                 dist = trace.fraction * r_editlights_cursordistance.value;
4393                 push = r_editlights_cursorpushback.value;
4394                 if (push > dist)
4395                         push = dist;
4396                 push = -push;
4397                 VectorMA(trace.endpos, push, r_refdef.view.forward, endpos);
4398                 VectorMA(endpos, r_editlights_cursorpushoff.value, trace.plane.normal, endpos);
4399         }
4400         else
4401         {
4402                 VectorClear( endpos );
4403         }
4404         r_editlights_cursorlocation[0] = floor(endpos[0] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
4405         r_editlights_cursorlocation[1] = floor(endpos[1] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
4406         r_editlights_cursorlocation[2] = floor(endpos[2] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
4407 }
4408
4409 void R_Shadow_UpdateWorldLightSelection(void)
4410 {
4411         if (r_editlights.integer)
4412         {
4413                 R_Shadow_SetCursorLocationForView();
4414                 R_Shadow_SelectLightInView();
4415         }
4416         else
4417                 R_Shadow_SelectLight(NULL);
4418 }
4419
4420 void R_Shadow_EditLights_Clear_f(void)
4421 {
4422         R_Shadow_ClearWorldLights();
4423 }
4424
4425 void R_Shadow_EditLights_Reload_f(void)
4426 {
4427         if (!cl.worldmodel)
4428                 return;
4429         strlcpy(r_shadow_mapname, cl.worldmodel->name, sizeof(r_shadow_mapname));
4430         R_Shadow_ClearWorldLights();
4431         R_Shadow_LoadWorldLights();
4432         if (!Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray))
4433         {
4434                 R_Shadow_LoadLightsFile();
4435                 if (!Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray))
4436                         R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
4437         }
4438 }
4439
4440 void R_Shadow_EditLights_Save_f(void)
4441 {
4442         if (!cl.worldmodel)
4443                 return;
4444         R_Shadow_SaveWorldLights();
4445 }
4446
4447 void R_Shadow_EditLights_ImportLightEntitiesFromMap_f(void)
4448 {
4449         R_Shadow_ClearWorldLights();
4450         R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
4451 }
4452
4453 void R_Shadow_EditLights_ImportLightsFile_f(void)
4454 {
4455         R_Shadow_ClearWorldLights();
4456         R_Shadow_LoadLightsFile();
4457 }
4458
4459 void R_Shadow_EditLights_Spawn_f(void)
4460 {
4461         vec3_t color;
4462         if (!r_editlights.integer)
4463         {
4464                 Con_Print("Cannot spawn light when not in editing mode.  Set r_editlights to 1.\n");
4465                 return;
4466         }
4467         if (Cmd_Argc() != 1)
4468         {
4469                 Con_Print("r_editlights_spawn does not take parameters\n");
4470                 return;
4471         }
4472         color[0] = color[1] = color[2] = 1;
4473         R_Shadow_UpdateWorldLight(R_Shadow_NewWorldLight(), r_editlights_cursorlocation, vec3_origin, color, 200, 0, 0, true, NULL, 0.25, 0, 1, 1, LIGHTFLAG_REALTIMEMODE);
4474 }
4475
4476 void R_Shadow_EditLights_Edit_f(void)
4477 {
4478         vec3_t origin, angles, color;
4479         vec_t radius, corona, coronasizescale, ambientscale, diffusescale, specularscale;
4480         int style, shadows, flags, normalmode, realtimemode;
4481         char cubemapname[MAX_INPUTLINE];
4482         if (!r_editlights.integer)
4483         {
4484                 Con_Print("Cannot spawn light when not in editing mode.  Set r_editlights to 1.\n");
4485                 return;
4486         }
4487         if (!r_shadow_selectedlight)
4488         {
4489                 Con_Print("No selected light.\n");
4490                 return;
4491         }
4492         VectorCopy(r_shadow_selectedlight->origin, origin);
4493         VectorCopy(r_shadow_selectedlight->angles, angles);
4494         VectorCopy(r_shadow_selectedlight->color, color);
4495         radius = r_shadow_selectedlight->radius;
4496         style = r_shadow_selectedlight->style;
4497         if (r_shadow_selectedlight->cubemapname)
4498                 strlcpy(cubemapname, r_shadow_selectedlight->cubemapname, sizeof(cubemapname));
4499         else
4500                 cubemapname[0] = 0;
4501         shadows = r_shadow_selectedlight->shadow;
4502         corona = r_shadow_selectedlight->corona;
4503         coronasizescale = r_shadow_selectedlight->coronasizescale;
4504         ambientscale = r_shadow_selectedlight->ambientscale;
4505         diffusescale = r_shadow_selectedlight->diffusescale;
4506         specularscale = r_shadow_selectedlight->specularscale;
4507         flags = r_shadow_selectedlight->flags;
4508         normalmode = (flags & LIGHTFLAG_NORMALMODE) != 0;
4509         realtimemode = (flags & LIGHTFLAG_REALTIMEMODE) != 0;
4510         if (!strcmp(Cmd_Argv(1), "origin"))
4511         {
4512                 if (Cmd_Argc() != 5)
4513                 {
4514                         Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(1));
4515                         return;
4516                 }
4517                 origin[0] = atof(Cmd_Argv(2));
4518                 origin[1] = atof(Cmd_Argv(3));
4519                 origin[2] = atof(Cmd_Argv(4));
4520         }
4521         else if (!strcmp(Cmd_Argv(1), "originx"))
4522         {
4523                 if (Cmd_Argc() != 3)
4524                 {
4525                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4526                         return;
4527                 }
4528                 origin[0] = atof(Cmd_Argv(2));
4529         }
4530         else if (!strcmp(Cmd_Argv(1), "originy"))
4531         {
4532                 if (Cmd_Argc() != 3)
4533                 {
4534                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4535                         return;
4536                 }
4537                 origin[1] = atof(Cmd_Argv(2));
4538         }
4539         else if (!strcmp(Cmd_Argv(1), "originz"))
4540         {
4541                 if (Cmd_Argc() != 3)
4542                 {
4543                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4544                         return;
4545                 }
4546                 origin[2] = atof(Cmd_Argv(2));
4547         }
4548         else if (!strcmp(Cmd_Argv(1), "move"))
4549         {
4550                 if (Cmd_Argc() != 5)
4551                 {
4552                         Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(1));
4553                         return;
4554                 }
4555                 origin[0] += atof(Cmd_Argv(2));
4556                 origin[1] += atof(Cmd_Argv(3));
4557                 origin[2] += atof(Cmd_Argv(4));
4558         }
4559         else if (!strcmp(Cmd_Argv(1), "movex"))
4560         {
4561                 if (Cmd_Argc() != 3)
4562                 {
4563                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4564                         return;
4565                 }
4566                 origin[0] += atof(Cmd_Argv(2));
4567         }
4568         else if (!strcmp(Cmd_Argv(1), "movey"))
4569         {
4570                 if (Cmd_Argc() != 3)
4571                 {
4572                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4573                         return;
4574                 }
4575                 origin[1] += atof(Cmd_Argv(2));
4576         }
4577         else if (!strcmp(Cmd_Argv(1), "movez"))
4578         {
4579                 if (Cmd_Argc() != 3)
4580                 {
4581                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4582                         return;
4583                 }
4584                 origin[2] += atof(Cmd_Argv(2));
4585         }
4586         else if (!strcmp(Cmd_Argv(1), "angles"))
4587         {
4588                 if (Cmd_Argc() != 5)
4589                 {
4590                         Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(1));
4591                         return;
4592                 }
4593                 angles[0] = atof(Cmd_Argv(2));
4594                 angles[1] = atof(Cmd_Argv(3));
4595                 angles[2] = atof(Cmd_Argv(4));
4596         }
4597         else if (!strcmp(Cmd_Argv(1), "anglesx"))
4598         {
4599                 if (Cmd_Argc() != 3)
4600                 {
4601                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4602                         return;
4603                 }
4604                 angles[0] = atof(Cmd_Argv(2));
4605         }
4606         else if (!strcmp(Cmd_Argv(1), "anglesy"))
4607         {
4608                 if (Cmd_Argc() != 3)
4609                 {
4610                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4611                         return;
4612                 }
4613                 angles[1] = atof(Cmd_Argv(2));
4614         }
4615         else if (!strcmp(Cmd_Argv(1), "anglesz"))
4616         {
4617                 if (Cmd_Argc() != 3)
4618                 {
4619                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4620                         return;
4621                 }
4622                 angles[2] = atof(Cmd_Argv(2));
4623         }
4624         else if (!strcmp(Cmd_Argv(1), "color"))
4625         {
4626                 if (Cmd_Argc() != 5)
4627                 {
4628                         Con_Printf("usage: r_editlights_edit %s red green blue\n", Cmd_Argv(1));
4629                         return;
4630                 }
4631                 color[0] = atof(Cmd_Argv(2));
4632                 color[1] = atof(Cmd_Argv(3));
4633                 color[2] = atof(Cmd_Argv(4));
4634         }
4635         else if (!strcmp(Cmd_Argv(1), "radius"))
4636         {
4637                 if (Cmd_Argc() != 3)
4638                 {
4639                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4640                         return;
4641                 }
4642                 radius = atof(Cmd_Argv(2));
4643         }
4644         else if (!strcmp(Cmd_Argv(1), "colorscale"))
4645         {
4646                 if (Cmd_Argc() == 3)
4647                 {
4648                         double scale = atof(Cmd_Argv(2));
4649                         color[0] *= scale;
4650                         color[1] *= scale;
4651                         color[2] *= scale;
4652                 }
4653                 else
4654                 {
4655                         if (Cmd_Argc() != 5)
4656                         {
4657                                 Con_Printf("usage: r_editlights_edit %s red green blue  (OR grey instead of red green blue)\n", Cmd_Argv(1));
4658                                 return;
4659                         }
4660                         color[0] *= atof(Cmd_Argv(2));
4661                         color[1] *= atof(Cmd_Argv(3));
4662                         color[2] *= atof(Cmd_Argv(4));
4663                 }
4664         }
4665         else if (!strcmp(Cmd_Argv(1), "radiusscale") || !strcmp(Cmd_Argv(1), "sizescale"))
4666         {
4667                 if (Cmd_Argc() != 3)
4668                 {
4669                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4670                         return;
4671                 }
4672                 radius *= atof(Cmd_Argv(2));
4673         }
4674         else if (!strcmp(Cmd_Argv(1), "style"))
4675         {
4676                 if (Cmd_Argc() != 3)
4677                 {
4678                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4679                         return;
4680                 }
4681                 style = atoi(Cmd_Argv(2));
4682         }
4683         else if (!strcmp(Cmd_Argv(1), "cubemap"))
4684         {
4685                 if (Cmd_Argc() > 3)
4686                 {
4687                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4688                         return;
4689                 }
4690                 if (Cmd_Argc() == 3)
4691                         strlcpy(cubemapname, Cmd_Argv(2), sizeof(cubemapname));
4692                 else
4693                         cubemapname[0] = 0;
4694         }
4695         else if (!strcmp(Cmd_Argv(1), "shadows"))
4696         {
4697                 if (Cmd_Argc() != 3)
4698                 {
4699                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4700                         return;
4701                 }
4702                 shadows = Cmd_Argv(2)[0] == 'y' || Cmd_Argv(2)[0] == 'Y' || Cmd_Argv(2)[0] == 't' || atoi(Cmd_Argv(2));
4703         }
4704         else if (!strcmp(Cmd_Argv(1), "corona"))
4705         {
4706                 if (Cmd_Argc() != 3)
4707                 {
4708                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4709                         return;
4710                 }
4711                 corona = atof(Cmd_Argv(2));
4712         }
4713         else if (!strcmp(Cmd_Argv(1), "coronasize"))
4714         {
4715                 if (Cmd_Argc() != 3)
4716                 {
4717                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4718                         return;
4719                 }
4720                 coronasizescale = atof(Cmd_Argv(2));
4721         }
4722         else if (!strcmp(Cmd_Argv(1), "ambient"))
4723         {
4724                 if (Cmd_Argc() != 3)
4725                 {
4726                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4727                         return;
4728                 }
4729                 ambientscale = atof(Cmd_Argv(2));
4730         }
4731         else if (!strcmp(Cmd_Argv(1), "diffuse"))
4732         {
4733                 if (Cmd_Argc() != 3)
4734                 {
4735                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4736                         return;
4737                 }
4738                 diffusescale = atof(Cmd_Argv(2));
4739         }
4740         else if (!strcmp(Cmd_Argv(1), "specular"))
4741         {
4742                 if (Cmd_Argc() != 3)
4743                 {
4744                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4745                         return;
4746                 }
4747                 specularscale = atof(Cmd_Argv(2));
4748         }
4749         else if (!strcmp(Cmd_Argv(1), "normalmode"))
4750         {
4751                 if (Cmd_Argc() != 3)
4752                 {
4753                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4754                         return;
4755                 }
4756                 normalmode = Cmd_Argv(2)[0] == 'y' || Cmd_Argv(2)[0] == 'Y' || Cmd_Argv(2)[0] == 't' || atoi(Cmd_Argv(2));
4757         }
4758         else if (!strcmp(Cmd_Argv(1), "realtimemode"))
4759         {
4760                 if (Cmd_Argc() != 3)
4761                 {
4762                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4763                         return;
4764                 }
4765                 realtimemode = Cmd_Argv(2)[0] == 'y' || Cmd_Argv(2)[0] == 'Y' || Cmd_Argv(2)[0] == 't' || atoi(Cmd_Argv(2));
4766         }
4767         else
4768         {
4769                 Con_Print("usage: r_editlights_edit [property] [value]\n");
4770                 Con_Print("Selected light's properties:\n");
4771                 Con_Printf("Origin       : %f %f %f\n", r_shadow_selectedlight->origin[0], r_shadow_selectedlight->origin[1], r_shadow_selectedlight->origin[2]);
4772                 Con_Printf("Angles       : %f %f %f\n", r_shadow_selectedlight->angles[0], r_shadow_selectedlight->angles[1], r_shadow_selectedlight->angles[2]);
4773                 Con_Printf("Color        : %f %f %f\n", r_shadow_selectedlight->color[0], r_shadow_selectedlight->color[1], r_shadow_selectedlight->color[2]);
4774                 Con_Printf("Radius       : %f\n", r_shadow_selectedlight->radius);
4775                 Con_Printf("Corona       : %f\n", r_shadow_selectedlight->corona);
4776                 Con_Printf("Style        : %i\n", r_shadow_selectedlight->style);
4777                 Con_Printf("Shadows      : %s\n", r_shadow_selectedlight->shadow ? "yes" : "no");
4778                 Con_Printf("Cubemap      : %s\n", r_shadow_selectedlight->cubemapname);
4779                 Con_Printf("CoronaSize   : %f\n", r_shadow_selectedlight->coronasizescale);
4780                 Con_Printf("Ambient      : %f\n", r_shadow_selectedlight->ambientscale);
4781                 Con_Printf("Diffuse      : %f\n", r_shadow_selectedlight->diffusescale);
4782                 Con_Printf("Specular     : %f\n", r_shadow_selectedlight->specularscale);
4783                 Con_Printf("NormalMode   : %s\n", (r_shadow_selectedlight->flags & LIGHTFLAG_NORMALMODE) ? "yes" : "no");
4784                 Con_Printf("RealTimeMode : %s\n", (r_shadow_selectedlight->flags & LIGHTFLAG_REALTIMEMODE) ? "yes" : "no");
4785                 return;
4786         }
4787         flags = (normalmode ? LIGHTFLAG_NORMALMODE : 0) | (realtimemode ? LIGHTFLAG_REALTIMEMODE : 0);
4788         R_Shadow_UpdateWorldLight(r_shadow_selectedlight, origin, angles, color, radius, corona, style, shadows, cubemapname, coronasizescale, ambientscale, diffusescale, specularscale, flags);
4789 }
4790
4791 void R_Shadow_EditLights_EditAll_f(void)
4792 {
4793         size_t lightindex;
4794         dlight_t *light;
4795         size_t range;
4796
4797         if (!r_editlights.integer)
4798         {
4799                 Con_Print("Cannot edit lights when not in editing mode. Set r_editlights to 1.\n");
4800                 return;
4801         }
4802
4803         // EditLights doesn't seem to have a "remove" command or something so:
4804         range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
4805         for (lightindex = 0;lightindex < range;lightindex++)
4806         {
4807                 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
4808                 if (!light)
4809                         continue;
4810                 R_Shadow_SelectLight(light);
4811                 R_Shadow_EditLights_Edit_f();
4812         }
4813 }
4814
4815 void R_Shadow_EditLights_DrawSelectedLightProperties(void)
4816 {
4817         int lightnumber, lightcount;
4818         size_t lightindex, range;
4819         dlight_t *light;
4820         float x, y;
4821         char temp[256];
4822         if (!r_editlights.integer)
4823                 return;
4824         x = vid_conwidth.value - 240;
4825         y = 5;
4826         DrawQ_Pic(x-5, y-5, NULL, 250, 155, 0, 0, 0, 0.75, 0);
4827         lightnumber = -1;
4828         lightcount = 0;
4829         range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
4830         for (lightindex = 0;lightindex < range;lightindex++)
4831         {
4832                 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
4833                 if (!light)
4834                         continue;
4835                 if (light == r_shadow_selectedlight)
4836                         lightnumber = lightindex;
4837                 lightcount++;
4838         }
4839         dpsnprintf(temp, sizeof(temp), "Cursor origin: %.0f %.0f %.0f", r_editlights_cursorlocation[0], r_editlights_cursorlocation[1], r_editlights_cursorlocation[2]); DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, false);y += 8;
4840         dpsnprintf(temp, sizeof(temp), "Total lights : %i active (%i total)", lightcount, (int)Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray)); DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, false);y += 8;
4841         y += 8;
4842         if (r_shadow_selectedlight == NULL)
4843                 return;
4844         dpsnprintf(temp, sizeof(temp), "Light #%i properties:", lightnumber);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true);y += 8;
4845         dpsnprintf(temp, sizeof(temp), "Origin       : %.0f %.0f %.0f\n", r_shadow_selectedlight->origin[0], r_shadow_selectedlight->origin[1], r_shadow_selectedlight->origin[2]);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true);y += 8;
4846         dpsnprintf(temp, sizeof(temp), "Angles       : %.0f %.0f %.0f\n", r_shadow_selectedlight->angles[0], r_shadow_selectedlight->angles[1], r_shadow_selectedlight->angles[2]);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true);y += 8;
4847         dpsnprintf(temp, sizeof(temp), "Color        : %.2f %.2f %.2f\n", r_shadow_selectedlight->color[0], r_shadow_selectedlight->color[1], r_shadow_selectedlight->color[2]);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true);y += 8;
4848         dpsnprintf(temp, sizeof(temp), "Radius       : %.0f\n", r_shadow_selectedlight->radius);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true);y += 8;
4849         dpsnprintf(temp, sizeof(temp), "Corona       : %.0f\n", r_shadow_selectedlight->corona);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true);y += 8;
4850         dpsnprintf(temp, sizeof(temp), "Style        : %i\n", r_shadow_selectedlight->style);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true);y += 8;
4851         dpsnprintf(temp, sizeof(temp), "Shadows      : %s\n", r_shadow_selectedlight->shadow ? "yes" : "no");DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true);y += 8;
4852         dpsnprintf(temp, sizeof(temp), "Cubemap      : %s\n", r_shadow_selectedlight->cubemapname);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true);y += 8;
4853         dpsnprintf(temp, sizeof(temp), "CoronaSize   : %.2f\n", r_shadow_selectedlight->coronasizescale);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true);y += 8;
4854         dpsnprintf(temp, sizeof(temp), "Ambient      : %.2f\n", r_shadow_selectedlight->ambientscale);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true);y += 8;
4855         dpsnprintf(temp, sizeof(temp), "Diffuse      : %.2f\n", r_shadow_selectedlight->diffusescale);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true);y += 8;
4856         dpsnprintf(temp, sizeof(temp), "Specular     : %.2f\n", r_shadow_selectedlight->specularscale);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true);y += 8;
4857         dpsnprintf(temp, sizeof(temp), "NormalMode   : %s\n", (r_shadow_selectedlight->flags & LIGHTFLAG_NORMALMODE) ? "yes" : "no");DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true);y += 8;
4858         dpsnprintf(temp, sizeof(temp), "RealTimeMode : %s\n", (r_shadow_selectedlight->flags & LIGHTFLAG_REALTIMEMODE) ? "yes" : "no");DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true);y += 8;
4859 }
4860
4861 void R_Shadow_EditLights_ToggleShadow_f(void)
4862 {
4863         if (!r_editlights.integer)
4864         {
4865                 Con_Print("Cannot spawn light when not in editing mode.  Set r_editlights to 1.\n");
4866                 return;
4867         }
4868         if (!r_shadow_selectedlight)
4869         {
4870                 Con_Print("No selected light.\n");
4871                 return;
4872         }
4873         R_Shadow_UpdateWorldLight(r_shadow_selectedlight, r_shadow_selectedlight->origin, r_shadow_selectedlight->angles, r_shadow_selectedlight->color, r_shadow_selectedlight->radius, r_shadow_selectedlight->corona, r_shadow_selectedlight->style, !r_shadow_selectedlight->shadow, r_shadow_selectedlight->cubemapname, r_shadow_selectedlight->coronasizescale, r_shadow_selectedlight->ambientscale, r_shadow_selectedlight->diffusescale, r_shadow_selectedlight->specularscale, r_shadow_selectedlight->flags);
4874 }
4875
4876 void R_Shadow_EditLights_ToggleCorona_f(void)
4877 {
4878         if (!r_editlights.integer)
4879         {
4880                 Con_Print("Cannot spawn light when not in editing mode.  Set r_editlights to 1.\n");
4881                 return;
4882         }
4883         if (!r_shadow_selectedlight)
4884         {
4885                 Con_Print("No selected light.\n");
4886                 return;
4887         }
4888         R_Shadow_UpdateWorldLight(r_shadow_selectedlight, r_shadow_selectedlight->origin, r_shadow_selectedlight->angles, r_shadow_selectedlight->color, r_shadow_selectedlight->radius, !r_shadow_selectedlight->corona, r_shadow_selectedlight->style, r_shadow_selectedlight->shadow, r_shadow_selectedlight->cubemapname, r_shadow_selectedlight->coronasizescale, r_shadow_selectedlight->ambientscale, r_shadow_selectedlight->diffusescale, r_shadow_selectedlight->specularscale, r_shadow_selectedlight->flags);
4889 }
4890
4891 void R_Shadow_EditLights_Remove_f(void)
4892 {
4893         if (!r_editlights.integer)
4894         {
4895                 Con_Print("Cannot remove light when not in editing mode.  Set r_editlights to 1.\n");
4896                 return;
4897         }
4898         if (!r_shadow_selectedlight)
4899         {
4900                 Con_Print("No selected light.\n");
4901                 return;
4902         }
4903         R_Shadow_FreeWorldLight(r_shadow_selectedlight);
4904         r_shadow_selectedlight = NULL;
4905 }
4906
4907 void R_Shadow_EditLights_Help_f(void)
4908 {
4909         Con_Print(
4910 "Documentation on r_editlights system:\n"
4911 "Settings:\n"
4912 "r_editlights : enable/disable editing mode\n"
4913 "r_editlights_cursordistance : maximum distance of cursor from eye\n"
4914 "r_editlights_cursorpushback : push back cursor this far from surface\n"
4915 "r_editlights_cursorpushoff : push cursor off surface this far\n"
4916 "r_editlights_cursorgrid : snap cursor to grid of this size\n"
4917 "r_editlights_quakelightsizescale : imported quake light entity size scaling\n"
4918 "Commands:\n"
4919 "r_editlights_help : this help\n"
4920 "r_editlights_clear : remove all lights\n"
4921 "r_editlights_reload : reload .rtlights, .lights file, or entities\n"
4922 "r_editlights_save : save to .rtlights file\n"
4923 "r_editlights_spawn : create a light with default settings\n"
4924 "r_editlights_edit command : edit selected light - more documentation below\n"
4925 "r_editlights_remove : remove selected light\n"
4926 "r_editlights_toggleshadow : toggles on/off selected light's shadow property\n"
4927 "r_editlights_importlightentitiesfrommap : reload light entities\n"
4928 "r_editlights_importlightsfile : reload .light file (produced by hlight)\n"
4929 "Edit commands:\n"
4930 "origin x y z : set light location\n"
4931 "originx x: set x component of light location\n"
4932 "originy y: set y component of light location\n"
4933 "originz z: set z component of light location\n"
4934 "move x y z : adjust light location\n"
4935 "movex x: adjust x component of light location\n"
4936 "movey y: adjust y component of light location\n"
4937 "movez z: adjust z component of light location\n"
4938 "angles x y z : set light angles\n"
4939 "anglesx x: set x component of light angles\n"
4940 "anglesy y: set y component of light angles\n"
4941 "anglesz z: set z component of light angles\n"
4942 "color r g b : set color of light (can be brighter than 1 1 1)\n"
4943 "radius radius : set radius (size) of light\n"
4944 "colorscale grey : multiply color of light (1 does nothing)\n"
4945 "colorscale r g b : multiply color of light (1 1 1 does nothing)\n"
4946 "radiusscale scale : multiply radius (size) of light (1 does nothing)\n"
4947 "sizescale scale : multiply radius (size) of light (1 does nothing)\n"
4948 "style style : set lightstyle of light (flickering patterns, switches, etc)\n"
4949 "cubemap basename : set filter cubemap of light (not yet supported)\n"
4950 "shadows 1/0 : turn on/off shadows\n"
4951 "corona n : set corona intensity\n"
4952 "coronasize n : set corona size (0-1)\n"
4953 "ambient n : set ambient intensity (0-1)\n"
4954 "diffuse n : set diffuse intensity (0-1)\n"
4955 "specular n : set specular intensity (0-1)\n"
4956 "normalmode 1/0 : turn on/off rendering of this light in rtworld 0 mode\n"
4957 "realtimemode 1/0 : turn on/off rendering of this light in rtworld 1 mode\n"
4958 "<nothing> : print light properties to console\n"
4959         );
4960 }
4961
4962 void R_Shadow_EditLights_CopyInfo_f(void)
4963 {
4964         if (!r_editlights.integer)
4965         {
4966                 Con_Print("Cannot copy light info when not in editing mode.  Set r_editlights to 1.\n");
4967                 return;
4968         }
4969         if (!r_shadow_selectedlight)
4970         {
4971                 Con_Print("No selected light.\n");
4972                 return;
4973         }
4974         VectorCopy(r_shadow_selectedlight->angles, r_shadow_bufferlight.angles);
4975         VectorCopy(r_shadow_selectedlight->color, r_shadow_bufferlight.color);
4976         r_shadow_bufferlight.radius = r_shadow_selectedlight->radius;
4977         r_shadow_bufferlight.style = r_shadow_selectedlight->style;
4978         if (r_shadow_selectedlight->cubemapname)
4979                 strlcpy(r_shadow_bufferlight.cubemapname, r_shadow_selectedlight->cubemapname, sizeof(r_shadow_bufferlight.cubemapname));
4980         else
4981                 r_shadow_bufferlight.cubemapname[0] = 0;
4982         r_shadow_bufferlight.shadow = r_shadow_selectedlight->shadow;
4983         r_shadow_bufferlight.corona = r_shadow_selectedlight->corona;
4984         r_shadow_bufferlight.coronasizescale = r_shadow_selectedlight->coronasizescale;
4985         r_shadow_bufferlight.ambientscale = r_shadow_selectedlight->ambientscale;
4986         r_shadow_bufferlight.diffusescale = r_shadow_selectedlight->diffusescale;
4987         r_shadow_bufferlight.specularscale = r_shadow_selectedlight->specularscale;
4988         r_shadow_bufferlight.flags = r_shadow_selectedlight->flags;
4989 }
4990
4991 void R_Shadow_EditLights_PasteInfo_f(void)
4992 {
4993         if (!r_editlights.integer)
4994         {
4995                 Con_Print("Cannot paste light info when not in editing mode.  Set r_editlights to 1.\n");
4996                 return;
4997         }
4998         if (!r_shadow_selectedlight)
4999         {
5000                 Con_Print("No selected light.\n");
5001                 return;
5002         }
5003         R_Shadow_UpdateWorldLight(r_shadow_selectedlight, r_shadow_selectedlight->origin, r_shadow_bufferlight.angles, r_shadow_bufferlight.color, r_shadow_bufferlight.radius, r_shadow_bufferlight.corona, r_shadow_bufferlight.style, r_shadow_bufferlight.shadow, r_shadow_bufferlight.cubemapname, r_shadow_bufferlight.coronasizescale, r_shadow_bufferlight.ambientscale, r_shadow_bufferlight.diffusescale, r_shadow_bufferlight.specularscale, r_shadow_bufferlight.flags);
5004 }
5005
5006 void R_Shadow_EditLights_Init(void)
5007 {
5008         Cvar_RegisterVariable(&r_editlights);
5009         Cvar_RegisterVariable(&r_editlights_cursordistance);
5010         Cvar_RegisterVariable(&r_editlights_cursorpushback);
5011         Cvar_RegisterVariable(&r_editlights_cursorpushoff);
5012         Cvar_RegisterVariable(&r_editlights_cursorgrid);
5013         Cvar_RegisterVariable(&r_editlights_quakelightsizescale);
5014         Cmd_AddCommand("r_editlights_help", R_Shadow_EditLights_Help_f, "prints documentation on console commands and variables in rtlight editing system");
5015         Cmd_AddCommand("r_editlights_clear", R_Shadow_EditLights_Clear_f, "removes all world lights (let there be darkness!)");
5016         Cmd_AddCommand("r_editlights_reload", R_Shadow_EditLights_Reload_f, "reloads rtlights file (or imports from .lights file or .ent file or the map itself)");
5017         Cmd_AddCommand("r_editlights_save", R_Shadow_EditLights_Save_f, "save .rtlights file for current level");
5018         Cmd_AddCommand("r_editlights_spawn", R_Shadow_EditLights_Spawn_f, "creates a light with default properties (let there be light!)");
5019         Cmd_AddCommand("r_editlights_edit", R_Shadow_EditLights_Edit_f, "changes a property on the selected light");
5020         Cmd_AddCommand("r_editlights_editall", R_Shadow_EditLights_EditAll_f, "changes a property on ALL lights at once (tip: use radiusscale and colorscale to alter these properties)");
5021         Cmd_AddCommand("r_editlights_remove", R_Shadow_EditLights_Remove_f, "remove selected light");
5022         Cmd_AddCommand("r_editlights_toggleshadow", R_Shadow_EditLights_ToggleShadow_f, "toggle on/off the shadow option on the selected light");
5023         Cmd_AddCommand("r_editlights_togglecorona", R_Shadow_EditLights_ToggleCorona_f, "toggle on/off the corona option on the selected light");
5024         Cmd_AddCommand("r_editlights_importlightentitiesfrommap", R_Shadow_EditLights_ImportLightEntitiesFromMap_f, "load lights from .ent file or map entities (ignoring .rtlights or .lights file)");
5025         Cmd_AddCommand("r_editlights_importlightsfile", R_Shadow_EditLights_ImportLightsFile_f, "load lights from .lights file (ignoring .rtlights or .ent files and map entities)");
5026         Cmd_AddCommand("r_editlights_copyinfo", R_Shadow_EditLights_CopyInfo_f, "store a copy of all properties (except origin) of the selected light");
5027         Cmd_AddCommand("r_editlights_pasteinfo", R_Shadow_EditLights_PasteInfo_f, "apply the stored properties onto the selected light (making it exactly identical except for origin)");
5028 }
5029
5030
5031
5032 /*
5033 =============================================================================
5034
5035 LIGHT SAMPLING
5036
5037 =============================================================================
5038 */
5039
5040 void R_CompleteLightPoint(vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal, const vec3_t p, int dynamic)
5041 {
5042         VectorClear(diffusecolor);
5043         VectorClear(diffusenormal);
5044
5045         if (!r_fullbright.integer && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.LightPoint)
5046         {
5047                 ambientcolor[0] = ambientcolor[1] = ambientcolor[2] = r_refdef.scene.ambient * (2.0f / 128.0f);
5048                 r_refdef.scene.worldmodel->brush.LightPoint(r_refdef.scene.worldmodel, p, ambientcolor, diffusecolor, diffusenormal);
5049         }
5050         else
5051                 VectorSet(ambientcolor, 1, 1, 1);
5052
5053         if (dynamic)
5054         {
5055                 int i;
5056                 float f, v[3];
5057                 rtlight_t *light;
5058                 for (i = 0;i < r_refdef.scene.numlights;i++)
5059                 {
5060                         light = r_refdef.scene.lights[i];
5061                         Matrix4x4_Transform(&light->matrix_worldtolight, p, v);
5062                         f = 1 - VectorLength2(v);
5063                         if (f > 0 && CL_Move(p, vec3_origin, vec3_origin, light->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false).fraction == 1)
5064                                 VectorMA(ambientcolor, f, light->currentcolor, ambientcolor);
5065                 }
5066         }
5067 }