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)
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.
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).
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).
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).
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
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.
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.
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.
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
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).
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.
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
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
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.
137 #include "quakedef.h"
138 #include "r_shadow.h"
139 #include "cl_collision.h"
143 extern void R_Shadow_EditLights_Init(void);
145 typedef enum r_shadow_rendermode_e
147 R_SHADOW_RENDERMODE_NONE,
148 R_SHADOW_RENDERMODE_STENCIL,
149 R_SHADOW_RENDERMODE_SEPARATESTENCIL,
150 R_SHADOW_RENDERMODE_STENCILTWOSIDE,
151 R_SHADOW_RENDERMODE_LIGHT_VERTEX,
152 R_SHADOW_RENDERMODE_LIGHT_DOT3,
153 R_SHADOW_RENDERMODE_LIGHT_GLSL,
154 R_SHADOW_RENDERMODE_VISIBLEVOLUMES,
155 R_SHADOW_RENDERMODE_VISIBLELIGHTING,
157 r_shadow_rendermode_t;
159 r_shadow_rendermode_t r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
160 r_shadow_rendermode_t r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_NONE;
161 r_shadow_rendermode_t r_shadow_shadowingrendermode = R_SHADOW_RENDERMODE_NONE;
163 int maxshadowtriangles;
166 int maxshadowvertices;
167 float *shadowvertex3f;
180 int r_shadow_buffer_numleafpvsbytes;
181 unsigned char *r_shadow_buffer_visitingleafpvs;
182 unsigned char *r_shadow_buffer_leafpvs;
183 int *r_shadow_buffer_leaflist;
185 int r_shadow_buffer_numsurfacepvsbytes;
186 unsigned char *r_shadow_buffer_surfacepvs;
187 int *r_shadow_buffer_surfacelist;
189 int r_shadow_buffer_numshadowtrispvsbytes;
190 unsigned char *r_shadow_buffer_shadowtrispvs;
191 int r_shadow_buffer_numlighttrispvsbytes;
192 unsigned char *r_shadow_buffer_lighttrispvs;
194 rtexturepool_t *r_shadow_texturepool;
195 rtexture_t *r_shadow_attenuationgradienttexture;
196 rtexture_t *r_shadow_attenuation2dtexture;
197 rtexture_t *r_shadow_attenuation3dtexture;
198 rtexture_t *r_shadow_lightcorona;
200 // lights are reloaded when this changes
201 char r_shadow_mapname[MAX_QPATH];
203 // used only for light filters (cubemaps)
204 rtexturepool_t *r_shadow_filters_texturepool;
206 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"};
207 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"};
208 cvar_t r_shadow_debuglight = {0, "r_shadow_debuglight", "-1", "renders only one light, for level design purposes or debugging"};
209 cvar_t r_shadow_usenormalmap = {CVAR_SAVE, "r_shadow_usenormalmap", "1", "enables use of directional shading on lights"};
210 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)"};
211 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"};
212 cvar_t r_shadow_glossintensity = {0, "r_shadow_glossintensity", "1", "how bright textured glossmaps should look if r_shadow_gloss is 1 or 2"};
213 cvar_t r_shadow_glossexponent = {0, "r_shadow_glossexponent", "32", "how 'sharp' the gloss should appear (specular power)"};
214 cvar_t r_shadow_glossexact = {0, "r_shadow_glossexact", "1", "use exact reflection math for gloss (slightly slower, but should look a tad better)"};
215 cvar_t r_shadow_lightattenuationdividebias = {0, "r_shadow_lightattenuationdividebias", "1", "changes attenuation texture generation"};
216 cvar_t r_shadow_lightattenuationlinearscale = {0, "r_shadow_lightattenuationlinearscale", "2", "changes attenuation texture generation"};
217 cvar_t r_shadow_lightintensityscale = {0, "r_shadow_lightintensityscale", "1", "renders all world lights brighter or darker"};
218 cvar_t r_shadow_lightradiusscale = {0, "r_shadow_lightradiusscale", "1", "renders all world lights larger or smaller"};
219 cvar_t r_shadow_portallight = {0, "r_shadow_portallight", "1", "use portal culling to exactly determine lit triangles when compiling world lights"};
220 cvar_t r_shadow_projectdistance = {0, "r_shadow_projectdistance", "1000000", "how far to cast shadows"};
221 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)"};
222 cvar_t r_shadow_realtime_dlight = {CVAR_SAVE, "r_shadow_realtime_dlight", "1", "enables rendering of dynamic lights such as explosions and rocket light"};
223 cvar_t r_shadow_realtime_dlight_shadows = {CVAR_SAVE, "r_shadow_realtime_dlight_shadows", "1", "enables rendering of shadows from dynamic lights"};
224 cvar_t r_shadow_realtime_dlight_svbspculling = {0, "r_shadow_realtime_dlight_svbspculling", "0", "enables svbsp optimization on dynamic lights (very slow!)"};
225 cvar_t r_shadow_realtime_dlight_portalculling = {0, "r_shadow_realtime_dlight_portalculling", "0", "enables portal optimization on dynamic lights (slow!)"};
226 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)"};
227 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"};
228 cvar_t r_shadow_realtime_world_shadows = {CVAR_SAVE, "r_shadow_realtime_world_shadows", "1", "enables rendering of shadows from world lights"};
229 cvar_t r_shadow_realtime_world_compile = {0, "r_shadow_realtime_world_compile", "1", "enables compilation of world lights for higher performance rendering"};
230 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"};
231 cvar_t r_shadow_realtime_world_compilesvbsp = {0, "r_shadow_realtime_world_compilesvbsp", "1", "enables svbsp optimization during compilation"};
232 cvar_t r_shadow_realtime_world_compileportalculling = {0, "r_shadow_realtime_world_compileportalculling", "1", "enables portal-based culling optimization during compilation"};
233 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)"};
234 cvar_t r_shadow_culltriangles = {0, "r_shadow_culltriangles", "1", "performs more expensive tests to remove unnecessary triangles of lit surfaces"};
235 cvar_t r_shadow_polygonfactor = {0, "r_shadow_polygonfactor", "0", "how much to enlarge shadow volume polygons when rendering (should be 0!)"};
236 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)"};
237 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)"};
238 cvar_t r_coronas = {CVAR_SAVE, "r_coronas", "1", "brightness of corona flare effects around certain lights, 0 disables corona effects"};
239 cvar_t gl_flashblend = {CVAR_SAVE, "gl_flashblend", "0", "render bright coronas for dynamic lights instead of actual lighting, fast but ugly"};
240 cvar_t gl_ext_separatestencil = {0, "gl_ext_separatestencil", "1", "make use of OpenGL 2.0 glStencilOpSeparate or GL_ATI_separate_stencil extension"};
241 cvar_t gl_ext_stenciltwoside = {0, "gl_ext_stenciltwoside", "1", "make use of GL_EXT_stenciltwoside extension (NVIDIA only)"};
242 cvar_t r_editlights = {0, "r_editlights", "0", "enables .rtlights file editing mode"};
243 cvar_t r_editlights_cursordistance = {0, "r_editlights_cursordistance", "1024", "maximum distance of cursor from eye"};
244 cvar_t r_editlights_cursorpushback = {0, "r_editlights_cursorpushback", "0", "how far to pull the cursor back toward the eye"};
245 cvar_t r_editlights_cursorpushoff = {0, "r_editlights_cursorpushoff", "4", "how far to push the cursor off the impacted surface"};
246 cvar_t r_editlights_cursorgrid = {0, "r_editlights_cursorgrid", "4", "snaps cursor to this grid size"};
247 cvar_t r_editlights_quakelightsizescale = {CVAR_SAVE, "r_editlights_quakelightsizescale", "1", "changes size of light entities loaded from a map"};
249 // note the table actually includes one more value, just to avoid the need to clamp the distance index due to minor math error
250 #define ATTENTABLESIZE 256
251 // 1D gradient, 2D circle and 3D sphere attenuation textures
252 #define ATTEN1DSIZE 32
253 #define ATTEN2DSIZE 64
254 #define ATTEN3DSIZE 32
256 static float r_shadow_attendividebias; // r_shadow_lightattenuationdividebias
257 static float r_shadow_attenlinearscale; // r_shadow_lightattenuationlinearscale
258 static float r_shadow_attentable[ATTENTABLESIZE+1];
260 rtlight_t *r_shadow_compilingrtlight;
261 static memexpandablearray_t r_shadow_worldlightsarray;
262 dlight_t *r_shadow_selectedlight;
263 dlight_t r_shadow_bufferlight;
264 vec3_t r_editlights_cursorlocation;
266 extern int con_vislines;
268 typedef struct cubemapinfo_s
275 #define MAX_CUBEMAPS 256
276 static int numcubemaps;
277 static cubemapinfo_t cubemaps[MAX_CUBEMAPS];
279 void R_Shadow_UncompileWorldLights(void);
280 void R_Shadow_ClearWorldLights(void);
281 void R_Shadow_SaveWorldLights(void);
282 void R_Shadow_LoadWorldLights(void);
283 void R_Shadow_LoadLightsFile(void);
284 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void);
285 void R_Shadow_EditLights_Reload_f(void);
286 void R_Shadow_ValidateCvars(void);
287 static void R_Shadow_MakeTextures(void);
289 // VorteX: custom editor light sprites
290 #define EDLIGHTSPRSIZE 8
291 cachepic_t *r_editlights_sprcursor;
292 cachepic_t *r_editlights_sprlight;
293 cachepic_t *r_editlights_sprnoshadowlight;
294 cachepic_t *r_editlights_sprcubemaplight;
295 cachepic_t *r_editlights_sprcubemapnoshadowlight;
296 cachepic_t *r_editlights_sprselection;
298 void r_shadow_start(void)
300 // allocate vertex processing arrays
302 r_shadow_attenuationgradienttexture = NULL;
303 r_shadow_attenuation2dtexture = NULL;
304 r_shadow_attenuation3dtexture = NULL;
305 r_shadow_texturepool = NULL;
306 r_shadow_filters_texturepool = NULL;
307 R_Shadow_ValidateCvars();
308 R_Shadow_MakeTextures();
309 maxshadowtriangles = 0;
310 shadowelements = NULL;
311 maxshadowvertices = 0;
312 shadowvertex3f = NULL;
320 shadowmarklist = NULL;
322 r_shadow_buffer_numleafpvsbytes = 0;
323 r_shadow_buffer_visitingleafpvs = NULL;
324 r_shadow_buffer_leafpvs = NULL;
325 r_shadow_buffer_leaflist = NULL;
326 r_shadow_buffer_numsurfacepvsbytes = 0;
327 r_shadow_buffer_surfacepvs = NULL;
328 r_shadow_buffer_surfacelist = NULL;
329 r_shadow_buffer_numshadowtrispvsbytes = 0;
330 r_shadow_buffer_shadowtrispvs = NULL;
331 r_shadow_buffer_numlighttrispvsbytes = 0;
332 r_shadow_buffer_lighttrispvs = NULL;
335 void r_shadow_shutdown(void)
337 R_Shadow_UncompileWorldLights();
339 r_shadow_attenuationgradienttexture = NULL;
340 r_shadow_attenuation2dtexture = NULL;
341 r_shadow_attenuation3dtexture = NULL;
342 R_FreeTexturePool(&r_shadow_texturepool);
343 R_FreeTexturePool(&r_shadow_filters_texturepool);
344 maxshadowtriangles = 0;
346 Mem_Free(shadowelements);
347 shadowelements = NULL;
349 Mem_Free(shadowvertex3f);
350 shadowvertex3f = NULL;
353 Mem_Free(vertexupdate);
356 Mem_Free(vertexremap);
362 Mem_Free(shadowmark);
365 Mem_Free(shadowmarklist);
366 shadowmarklist = NULL;
368 r_shadow_buffer_numleafpvsbytes = 0;
369 if (r_shadow_buffer_visitingleafpvs)
370 Mem_Free(r_shadow_buffer_visitingleafpvs);
371 r_shadow_buffer_visitingleafpvs = NULL;
372 if (r_shadow_buffer_leafpvs)
373 Mem_Free(r_shadow_buffer_leafpvs);
374 r_shadow_buffer_leafpvs = NULL;
375 if (r_shadow_buffer_leaflist)
376 Mem_Free(r_shadow_buffer_leaflist);
377 r_shadow_buffer_leaflist = NULL;
378 r_shadow_buffer_numsurfacepvsbytes = 0;
379 if (r_shadow_buffer_surfacepvs)
380 Mem_Free(r_shadow_buffer_surfacepvs);
381 r_shadow_buffer_surfacepvs = NULL;
382 if (r_shadow_buffer_surfacelist)
383 Mem_Free(r_shadow_buffer_surfacelist);
384 r_shadow_buffer_surfacelist = NULL;
385 r_shadow_buffer_numshadowtrispvsbytes = 0;
386 if (r_shadow_buffer_shadowtrispvs)
387 Mem_Free(r_shadow_buffer_shadowtrispvs);
388 r_shadow_buffer_numlighttrispvsbytes = 0;
389 if (r_shadow_buffer_lighttrispvs)
390 Mem_Free(r_shadow_buffer_lighttrispvs);
393 void r_shadow_newmap(void)
395 if (cl.worldmodel && strncmp(cl.worldmodel->name, r_shadow_mapname, sizeof(r_shadow_mapname)))
396 R_Shadow_EditLights_Reload_f();
399 void R_Shadow_Help_f(void)
402 "Documentation on r_shadow system:\n"
404 "r_shadow_bumpscale_basetexture : base texture as bumpmap with this scale\n"
405 "r_shadow_bumpscale_bumpmap : depth scale for bumpmap conversion\n"
406 "r_shadow_debuglight : render only this light number (-1 = all)\n"
407 "r_shadow_gloss 0/1/2 : no gloss, gloss textures only, force gloss\n"
408 "r_shadow_gloss2intensity : brightness of forced gloss\n"
409 "r_shadow_glossintensity : brightness of textured gloss\n"
410 "r_shadow_lightattenuationlinearscale : used to generate attenuation texture\n"
411 "r_shadow_lightattenuationdividebias : used to generate attenuation texture\n"
412 "r_shadow_lightintensityscale : scale rendering brightness of all lights\n"
413 "r_shadow_lightradiusscale : scale rendering radius of all lights\n"
414 "r_shadow_portallight : use portal visibility for static light precomputation\n"
415 "r_shadow_projectdistance : shadow volume projection distance\n"
416 "r_shadow_realtime_dlight : use high quality dynamic lights in normal mode\n"
417 "r_shadow_realtime_dlight_shadows : cast shadows from dlights\n"
418 "r_shadow_realtime_world : use high quality world lighting mode\n"
419 "r_shadow_realtime_world_lightmaps : use lightmaps in addition to lights\n"
420 "r_shadow_realtime_world_shadows : cast shadows from world lights\n"
421 "r_shadow_realtime_world_compile : compile surface/visibility information\n"
422 "r_shadow_realtime_world_compileshadow : compile shadow geometry\n"
423 "r_shadow_scissor : use scissor optimization\n"
424 "r_shadow_polygonfactor : nudge shadow volumes closer/further\n"
425 "r_shadow_polygonoffset : nudge shadow volumes closer/further\n"
426 "r_shadow_texture3d : use 3d attenuation texture (if hardware supports)\n"
427 "r_showlighting : useful for performance testing; bright = slow!\n"
428 "r_showshadowvolumes : useful for performance testing; bright = slow!\n"
430 "r_shadow_help : this help\n"
434 void R_Shadow_Init(void)
436 Cvar_RegisterVariable(&r_shadow_bumpscale_basetexture);
437 Cvar_RegisterVariable(&r_shadow_bumpscale_bumpmap);
438 Cvar_RegisterVariable(&r_shadow_usenormalmap);
439 Cvar_RegisterVariable(&r_shadow_debuglight);
440 Cvar_RegisterVariable(&r_shadow_gloss);
441 Cvar_RegisterVariable(&r_shadow_gloss2intensity);
442 Cvar_RegisterVariable(&r_shadow_glossintensity);
443 Cvar_RegisterVariable(&r_shadow_glossexponent);
444 Cvar_RegisterVariable(&r_shadow_glossexact);
445 Cvar_RegisterVariable(&r_shadow_lightattenuationdividebias);
446 Cvar_RegisterVariable(&r_shadow_lightattenuationlinearscale);
447 Cvar_RegisterVariable(&r_shadow_lightintensityscale);
448 Cvar_RegisterVariable(&r_shadow_lightradiusscale);
449 Cvar_RegisterVariable(&r_shadow_portallight);
450 Cvar_RegisterVariable(&r_shadow_projectdistance);
451 Cvar_RegisterVariable(&r_shadow_frontsidecasting);
452 Cvar_RegisterVariable(&r_shadow_realtime_dlight);
453 Cvar_RegisterVariable(&r_shadow_realtime_dlight_shadows);
454 Cvar_RegisterVariable(&r_shadow_realtime_dlight_svbspculling);
455 Cvar_RegisterVariable(&r_shadow_realtime_dlight_portalculling);
456 Cvar_RegisterVariable(&r_shadow_realtime_world);
457 Cvar_RegisterVariable(&r_shadow_realtime_world_lightmaps);
458 Cvar_RegisterVariable(&r_shadow_realtime_world_shadows);
459 Cvar_RegisterVariable(&r_shadow_realtime_world_compile);
460 Cvar_RegisterVariable(&r_shadow_realtime_world_compileshadow);
461 Cvar_RegisterVariable(&r_shadow_realtime_world_compilesvbsp);
462 Cvar_RegisterVariable(&r_shadow_realtime_world_compileportalculling);
463 Cvar_RegisterVariable(&r_shadow_scissor);
464 Cvar_RegisterVariable(&r_shadow_culltriangles);
465 Cvar_RegisterVariable(&r_shadow_polygonfactor);
466 Cvar_RegisterVariable(&r_shadow_polygonoffset);
467 Cvar_RegisterVariable(&r_shadow_texture3d);
468 Cvar_RegisterVariable(&r_coronas);
469 Cvar_RegisterVariable(&gl_flashblend);
470 Cvar_RegisterVariable(&gl_ext_separatestencil);
471 Cvar_RegisterVariable(&gl_ext_stenciltwoside);
472 if (gamemode == GAME_TENEBRAE)
474 Cvar_SetValue("r_shadow_gloss", 2);
475 Cvar_SetValue("r_shadow_bumpscale_basetexture", 4);
477 Cmd_AddCommand("r_shadow_help", R_Shadow_Help_f, "prints documentation on console commands and variables used by realtime lighting and shadowing system");
478 R_Shadow_EditLights_Init();
479 Mem_ExpandableArray_NewArray(&r_shadow_worldlightsarray, r_main_mempool, sizeof(dlight_t), 128);
480 maxshadowtriangles = 0;
481 shadowelements = NULL;
482 maxshadowvertices = 0;
483 shadowvertex3f = NULL;
491 shadowmarklist = NULL;
493 r_shadow_buffer_numleafpvsbytes = 0;
494 r_shadow_buffer_visitingleafpvs = NULL;
495 r_shadow_buffer_leafpvs = NULL;
496 r_shadow_buffer_leaflist = NULL;
497 r_shadow_buffer_numsurfacepvsbytes = 0;
498 r_shadow_buffer_surfacepvs = NULL;
499 r_shadow_buffer_surfacelist = NULL;
500 r_shadow_buffer_shadowtrispvs = NULL;
501 r_shadow_buffer_lighttrispvs = NULL;
502 R_RegisterModule("R_Shadow", r_shadow_start, r_shadow_shutdown, r_shadow_newmap);
505 matrix4x4_t matrix_attenuationxyz =
508 {0.5, 0.0, 0.0, 0.5},
509 {0.0, 0.5, 0.0, 0.5},
510 {0.0, 0.0, 0.5, 0.5},
515 matrix4x4_t matrix_attenuationz =
518 {0.0, 0.0, 0.5, 0.5},
519 {0.0, 0.0, 0.0, 0.5},
520 {0.0, 0.0, 0.0, 0.5},
525 void R_Shadow_ResizeShadowArrays(int numvertices, int numtriangles)
527 // make sure shadowelements is big enough for this volume
528 if (maxshadowtriangles < numtriangles)
530 maxshadowtriangles = numtriangles;
532 Mem_Free(shadowelements);
533 shadowelements = (int *)Mem_Alloc(r_main_mempool, maxshadowtriangles * sizeof(int[24]));
535 // make sure shadowvertex3f is big enough for this volume
536 if (maxshadowvertices < numvertices)
538 maxshadowvertices = numvertices;
540 Mem_Free(shadowvertex3f);
541 shadowvertex3f = (float *)Mem_Alloc(r_main_mempool, maxshadowvertices * sizeof(float[6]));
545 static void R_Shadow_EnlargeLeafSurfaceTrisBuffer(int numleafs, int numsurfaces, int numshadowtriangles, int numlighttriangles)
547 int numleafpvsbytes = (((numleafs + 7) >> 3) + 255) & ~255;
548 int numsurfacepvsbytes = (((numsurfaces + 7) >> 3) + 255) & ~255;
549 int numshadowtrispvsbytes = (((numshadowtriangles + 7) >> 3) + 255) & ~255;
550 int numlighttrispvsbytes = (((numlighttriangles + 7) >> 3) + 255) & ~255;
551 if (r_shadow_buffer_numleafpvsbytes < numleafpvsbytes)
553 if (r_shadow_buffer_visitingleafpvs)
554 Mem_Free(r_shadow_buffer_visitingleafpvs);
555 if (r_shadow_buffer_leafpvs)
556 Mem_Free(r_shadow_buffer_leafpvs);
557 if (r_shadow_buffer_leaflist)
558 Mem_Free(r_shadow_buffer_leaflist);
559 r_shadow_buffer_numleafpvsbytes = numleafpvsbytes;
560 r_shadow_buffer_visitingleafpvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numleafpvsbytes);
561 r_shadow_buffer_leafpvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numleafpvsbytes);
562 r_shadow_buffer_leaflist = (int *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numleafpvsbytes * 8 * sizeof(*r_shadow_buffer_leaflist));
564 if (r_shadow_buffer_numsurfacepvsbytes < numsurfacepvsbytes)
566 if (r_shadow_buffer_surfacepvs)
567 Mem_Free(r_shadow_buffer_surfacepvs);
568 if (r_shadow_buffer_surfacelist)
569 Mem_Free(r_shadow_buffer_surfacelist);
570 r_shadow_buffer_numsurfacepvsbytes = numsurfacepvsbytes;
571 r_shadow_buffer_surfacepvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numsurfacepvsbytes);
572 r_shadow_buffer_surfacelist = (int *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numsurfacepvsbytes * 8 * sizeof(*r_shadow_buffer_surfacelist));
574 if (r_shadow_buffer_numshadowtrispvsbytes < numshadowtrispvsbytes)
576 if (r_shadow_buffer_shadowtrispvs)
577 Mem_Free(r_shadow_buffer_shadowtrispvs);
578 r_shadow_buffer_numshadowtrispvsbytes = numshadowtrispvsbytes;
579 r_shadow_buffer_shadowtrispvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numshadowtrispvsbytes);
581 if (r_shadow_buffer_numlighttrispvsbytes < numlighttrispvsbytes)
583 if (r_shadow_buffer_lighttrispvs)
584 Mem_Free(r_shadow_buffer_lighttrispvs);
585 r_shadow_buffer_numlighttrispvsbytes = numlighttrispvsbytes;
586 r_shadow_buffer_lighttrispvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numlighttrispvsbytes);
590 void R_Shadow_PrepareShadowMark(int numtris)
592 // make sure shadowmark is big enough for this volume
593 if (maxshadowmark < numtris)
595 maxshadowmark = numtris;
597 Mem_Free(shadowmark);
599 Mem_Free(shadowmarklist);
600 shadowmark = (int *)Mem_Alloc(r_main_mempool, maxshadowmark * sizeof(*shadowmark));
601 shadowmarklist = (int *)Mem_Alloc(r_main_mempool, maxshadowmark * sizeof(*shadowmarklist));
605 // if shadowmarkcount wrapped we clear the array and adjust accordingly
606 if (shadowmarkcount == 0)
609 memset(shadowmark, 0, maxshadowmark * sizeof(*shadowmark));
614 int R_Shadow_ConstructShadowVolume(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)
617 int outtriangles = 0, outvertices = 0;
620 float ratio, direction[3], projectvector[3];
622 if (projectdirection)
623 VectorScale(projectdirection, projectdistance, projectvector);
625 VectorClear(projectvector);
627 if (maxvertexupdate < innumvertices)
629 maxvertexupdate = innumvertices;
631 Mem_Free(vertexupdate);
633 Mem_Free(vertexremap);
634 vertexupdate = (int *)Mem_Alloc(r_main_mempool, maxvertexupdate * sizeof(int));
635 vertexremap = (int *)Mem_Alloc(r_main_mempool, maxvertexupdate * sizeof(int));
639 if (vertexupdatenum == 0)
642 memset(vertexupdate, 0, maxvertexupdate * sizeof(int));
643 memset(vertexremap, 0, maxvertexupdate * sizeof(int));
646 for (i = 0;i < numshadowmarktris;i++)
647 shadowmark[shadowmarktris[i]] = shadowmarkcount;
649 // create the vertices
650 if (projectdirection)
652 for (i = 0;i < numshadowmarktris;i++)
654 element = inelement3i + shadowmarktris[i] * 3;
655 for (j = 0;j < 3;j++)
657 if (vertexupdate[element[j]] != vertexupdatenum)
659 vertexupdate[element[j]] = vertexupdatenum;
660 vertexremap[element[j]] = outvertices;
661 vertex = invertex3f + element[j] * 3;
662 // project one copy of the vertex according to projectvector
663 VectorCopy(vertex, outvertex3f);
664 VectorAdd(vertex, projectvector, (outvertex3f + 3));
673 for (i = 0;i < numshadowmarktris;i++)
675 element = inelement3i + shadowmarktris[i] * 3;
676 for (j = 0;j < 3;j++)
678 if (vertexupdate[element[j]] != vertexupdatenum)
680 vertexupdate[element[j]] = vertexupdatenum;
681 vertexremap[element[j]] = outvertices;
682 vertex = invertex3f + element[j] * 3;
683 // project one copy of the vertex to the sphere radius of the light
684 // (FIXME: would projecting it to the light box be better?)
685 VectorSubtract(vertex, projectorigin, direction);
686 ratio = projectdistance / VectorLength(direction);
687 VectorCopy(vertex, outvertex3f);
688 VectorMA(projectorigin, ratio, direction, (outvertex3f + 3));
696 if (r_shadow_frontsidecasting.integer)
698 for (i = 0;i < numshadowmarktris;i++)
700 int remappedelement[3];
702 const int *neighbortriangle;
704 markindex = shadowmarktris[i] * 3;
705 element = inelement3i + markindex;
706 neighbortriangle = inneighbor3i + markindex;
707 // output the front and back triangles
708 outelement3i[0] = vertexremap[element[0]];
709 outelement3i[1] = vertexremap[element[1]];
710 outelement3i[2] = vertexremap[element[2]];
711 outelement3i[3] = vertexremap[element[2]] + 1;
712 outelement3i[4] = vertexremap[element[1]] + 1;
713 outelement3i[5] = vertexremap[element[0]] + 1;
717 // output the sides (facing outward from this triangle)
718 if (shadowmark[neighbortriangle[0]] != shadowmarkcount)
720 remappedelement[0] = vertexremap[element[0]];
721 remappedelement[1] = vertexremap[element[1]];
722 outelement3i[0] = remappedelement[1];
723 outelement3i[1] = remappedelement[0];
724 outelement3i[2] = remappedelement[0] + 1;
725 outelement3i[3] = remappedelement[1];
726 outelement3i[4] = remappedelement[0] + 1;
727 outelement3i[5] = remappedelement[1] + 1;
732 if (shadowmark[neighbortriangle[1]] != shadowmarkcount)
734 remappedelement[1] = vertexremap[element[1]];
735 remappedelement[2] = vertexremap[element[2]];
736 outelement3i[0] = remappedelement[2];
737 outelement3i[1] = remappedelement[1];
738 outelement3i[2] = remappedelement[1] + 1;
739 outelement3i[3] = remappedelement[2];
740 outelement3i[4] = remappedelement[1] + 1;
741 outelement3i[5] = remappedelement[2] + 1;
746 if (shadowmark[neighbortriangle[2]] != shadowmarkcount)
748 remappedelement[0] = vertexremap[element[0]];
749 remappedelement[2] = vertexremap[element[2]];
750 outelement3i[0] = remappedelement[0];
751 outelement3i[1] = remappedelement[2];
752 outelement3i[2] = remappedelement[2] + 1;
753 outelement3i[3] = remappedelement[0];
754 outelement3i[4] = remappedelement[2] + 1;
755 outelement3i[5] = remappedelement[0] + 1;
764 for (i = 0;i < numshadowmarktris;i++)
766 int remappedelement[3];
768 const int *neighbortriangle;
770 markindex = shadowmarktris[i] * 3;
771 element = inelement3i + markindex;
772 neighbortriangle = inneighbor3i + markindex;
773 // output the front and back triangles
774 outelement3i[0] = vertexremap[element[2]];
775 outelement3i[1] = vertexremap[element[1]];
776 outelement3i[2] = vertexremap[element[0]];
777 outelement3i[3] = vertexremap[element[0]] + 1;
778 outelement3i[4] = vertexremap[element[1]] + 1;
779 outelement3i[5] = vertexremap[element[2]] + 1;
783 // output the sides (facing outward from this triangle)
784 if (shadowmark[neighbortriangle[0]] != shadowmarkcount)
786 remappedelement[0] = vertexremap[element[0]];
787 remappedelement[1] = vertexremap[element[1]];
788 outelement3i[0] = remappedelement[0];
789 outelement3i[1] = remappedelement[1];
790 outelement3i[2] = remappedelement[1] + 1;
791 outelement3i[3] = remappedelement[0];
792 outelement3i[4] = remappedelement[1] + 1;
793 outelement3i[5] = remappedelement[0] + 1;
798 if (shadowmark[neighbortriangle[1]] != shadowmarkcount)
800 remappedelement[1] = vertexremap[element[1]];
801 remappedelement[2] = vertexremap[element[2]];
802 outelement3i[0] = remappedelement[1];
803 outelement3i[1] = remappedelement[2];
804 outelement3i[2] = remappedelement[2] + 1;
805 outelement3i[3] = remappedelement[1];
806 outelement3i[4] = remappedelement[2] + 1;
807 outelement3i[5] = remappedelement[1] + 1;
812 if (shadowmark[neighbortriangle[2]] != shadowmarkcount)
814 remappedelement[0] = vertexremap[element[0]];
815 remappedelement[2] = vertexremap[element[2]];
816 outelement3i[0] = remappedelement[2];
817 outelement3i[1] = remappedelement[0];
818 outelement3i[2] = remappedelement[0] + 1;
819 outelement3i[3] = remappedelement[2];
820 outelement3i[4] = remappedelement[0] + 1;
821 outelement3i[5] = remappedelement[2] + 1;
829 *outnumvertices = outvertices;
833 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)
839 if (!BoxesOverlap(lightmins, lightmaxs, surfacemins, surfacemaxs))
841 tend = firsttriangle + numtris;
842 if (BoxInsideBox(surfacemins, surfacemaxs, lightmins, lightmaxs))
844 // surface box entirely inside light box, no box cull
845 if (projectdirection)
847 for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
849 TriangleNormal(invertex3f + e[0] * 3, invertex3f + e[1] * 3, invertex3f + e[2] * 3, normal);
850 if (r_shadow_frontsidecasting.integer == (DotProduct(normal, projectdirection) < 0))
851 shadowmarklist[numshadowmark++] = t;
856 for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
857 if (r_shadow_frontsidecasting.integer == PointInfrontOfTriangle(projectorigin, invertex3f + e[0] * 3, invertex3f + e[1] * 3, invertex3f + e[2] * 3))
858 shadowmarklist[numshadowmark++] = t;
863 // surface box not entirely inside light box, cull each triangle
864 if (projectdirection)
866 for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
868 v[0] = invertex3f + e[0] * 3;
869 v[1] = invertex3f + e[1] * 3;
870 v[2] = invertex3f + e[2] * 3;
871 TriangleNormal(v[0], v[1], v[2], normal);
872 if (r_shadow_frontsidecasting.integer == (DotProduct(normal, projectdirection) < 0)
873 && TriangleOverlapsBox(v[0], v[1], v[2], lightmins, lightmaxs))
874 shadowmarklist[numshadowmark++] = t;
879 for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
881 v[0] = invertex3f + e[0] * 3;
882 v[1] = invertex3f + e[1] * 3;
883 v[2] = invertex3f + e[2] * 3;
884 if (r_shadow_frontsidecasting.integer == PointInfrontOfTriangle(projectorigin, v[0], v[1], v[2])
885 && TriangleOverlapsBox(v[0], v[1], v[2], lightmins, lightmaxs))
886 shadowmarklist[numshadowmark++] = t;
892 static void R_Shadow_RenderVolume(int numvertices, int numtriangles, const float *vertex3f, const int *element3i)
894 if (r_shadow_compilingrtlight)
896 // if we're compiling an rtlight, capture the mesh
897 Mod_ShadowMesh_AddMesh(r_main_mempool, r_shadow_compilingrtlight->static_meshchain_shadow, NULL, NULL, NULL, vertex3f, NULL, NULL, NULL, NULL, numtriangles, element3i);
900 r_refdef.stats.lights_shadowtriangles += numtriangles;
902 R_Mesh_VertexPointer(vertex3f, 0, 0);
903 GL_LockArrays(0, numvertices);
904 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_STENCIL)
906 // decrement stencil if backface is behind depthbuffer
907 GL_CullFace(r_refdef.view.cullface_front);
908 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
909 R_Mesh_Draw(0, numvertices, 0, numtriangles, element3i, NULL, 0, 0);
910 // increment stencil if frontface is behind depthbuffer
911 GL_CullFace(r_refdef.view.cullface_back);
912 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR
914 R_Mesh_Draw(0, numvertices, 0, numtriangles, element3i, NULL, 0, 0);
919 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)
922 if (projectdistance < 0.1)
924 Con_Printf("R_Shadow_Volume: projectdistance %f\n", projectdistance);
927 if (!numverts || !nummarktris)
929 // make sure shadowelements is big enough for this volume
930 if (maxshadowtriangles < nummarktris || maxshadowvertices < numverts)
931 R_Shadow_ResizeShadowArrays((numverts + 255) & ~255, (nummarktris + 255) & ~255);
932 tris = R_Shadow_ConstructShadowVolume(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
933 r_refdef.stats.lights_dynamicshadowtriangles += tris;
934 R_Shadow_RenderVolume(outverts, tris, shadowvertex3f, shadowelements);
937 static void R_Shadow_MakeTextures_MakeCorona(void)
941 unsigned char pixels[32][32][4];
942 for (y = 0;y < 32;y++)
944 dy = (y - 15.5f) * (1.0f / 16.0f);
945 for (x = 0;x < 32;x++)
947 dx = (x - 15.5f) * (1.0f / 16.0f);
948 a = (int)(((1.0f / (dx * dx + dy * dy + 0.2f)) - (1.0f / (1.0f + 0.2))) * 32.0f / (1.0f / (1.0f + 0.2)));
949 a = bound(0, a, 255);
953 pixels[y][x][3] = 255;
956 r_shadow_lightcorona = R_LoadTexture2D(r_shadow_texturepool, "lightcorona", 32, 32, &pixels[0][0][0], TEXTYPE_BGRA, TEXF_PRECACHE, NULL);
959 static unsigned int R_Shadow_MakeTextures_SamplePoint(float x, float y, float z)
961 float dist = sqrt(x*x+y*y+z*z);
962 float intensity = dist < 1 ? ((1.0f - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist)) : 0;
963 // note this code could suffer byte order issues except that it is multiplying by an integer that reads the same both ways
964 return (unsigned char)bound(0, intensity * 256.0f, 255) * 0x01010101;
967 static void R_Shadow_MakeTextures(void)
970 float intensity, dist;
972 R_FreeTexturePool(&r_shadow_texturepool);
973 r_shadow_texturepool = R_AllocTexturePool();
974 r_shadow_attenlinearscale = r_shadow_lightattenuationlinearscale.value;
975 r_shadow_attendividebias = r_shadow_lightattenuationdividebias.value;
976 data = (unsigned int *)Mem_Alloc(tempmempool, max(max(ATTEN3DSIZE*ATTEN3DSIZE*ATTEN3DSIZE, ATTEN2DSIZE*ATTEN2DSIZE), ATTEN1DSIZE) * 4);
977 // the table includes one additional value to avoid the need to clamp indexing due to minor math errors
978 for (x = 0;x <= ATTENTABLESIZE;x++)
980 dist = (x + 0.5f) * (1.0f / ATTENTABLESIZE) * (1.0f / 0.9375);
981 intensity = dist < 1 ? ((1.0f - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist)) : 0;
982 r_shadow_attentable[x] = bound(0, intensity, 1);
984 // 1D gradient texture
985 for (x = 0;x < ATTEN1DSIZE;x++)
986 data[x] = R_Shadow_MakeTextures_SamplePoint((x + 0.5f) * (1.0f / ATTEN1DSIZE) * (1.0f / 0.9375), 0, 0);
987 r_shadow_attenuationgradienttexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation1d", ATTEN1DSIZE, 1, (unsigned char *)data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA, NULL);
989 for (y = 0;y < ATTEN2DSIZE;y++)
990 for (x = 0;x < ATTEN2DSIZE;x++)
991 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);
992 r_shadow_attenuation2dtexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation2d", ATTEN2DSIZE, ATTEN2DSIZE, (unsigned char *)data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA, NULL);
994 if (r_shadow_texture3d.integer && gl_texture3d)
996 for (z = 0;z < ATTEN3DSIZE;z++)
997 for (y = 0;y < ATTEN3DSIZE;y++)
998 for (x = 0;x < ATTEN3DSIZE;x++)
999 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));
1000 r_shadow_attenuation3dtexture = R_LoadTexture3D(r_shadow_texturepool, "attenuation3d", ATTEN3DSIZE, ATTEN3DSIZE, ATTEN3DSIZE, (unsigned char *)data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA, NULL);
1003 r_shadow_attenuation3dtexture = NULL;
1006 R_Shadow_MakeTextures_MakeCorona();
1008 // Editor light sprites
1009 r_editlights_sprcursor = Draw_CachePic ("gfx/editlights/cursor");
1010 r_editlights_sprlight = Draw_CachePic ("gfx/editlights/light");
1011 r_editlights_sprnoshadowlight = Draw_CachePic ("gfx/editlights/noshadow");
1012 r_editlights_sprcubemaplight = Draw_CachePic ("gfx/editlights/cubemaplight");
1013 r_editlights_sprcubemapnoshadowlight = Draw_CachePic ("gfx/editlights/cubemapnoshadowlight");
1014 r_editlights_sprselection = Draw_CachePic ("gfx/editlights/selection");
1017 void R_Shadow_ValidateCvars(void)
1019 if (r_shadow_texture3d.integer && !gl_texture3d)
1020 Cvar_SetValueQuick(&r_shadow_texture3d, 0);
1021 if (gl_ext_separatestencil.integer && !gl_support_separatestencil)
1022 Cvar_SetValueQuick(&gl_ext_separatestencil, 0);
1023 if (gl_ext_stenciltwoside.integer && !gl_support_stenciltwoside)
1024 Cvar_SetValueQuick(&gl_ext_stenciltwoside, 0);
1027 void R_Shadow_RenderMode_Begin(void)
1029 R_Shadow_ValidateCvars();
1031 if (!r_shadow_attenuation2dtexture
1032 || (!r_shadow_attenuation3dtexture && r_shadow_texture3d.integer)
1033 || r_shadow_lightattenuationdividebias.value != r_shadow_attendividebias
1034 || r_shadow_lightattenuationlinearscale.value != r_shadow_attenlinearscale)
1035 R_Shadow_MakeTextures();
1038 R_Mesh_ColorPointer(NULL, 0, 0);
1039 R_Mesh_ResetTextureState();
1040 GL_BlendFunc(GL_ONE, GL_ZERO);
1041 GL_DepthRange(0, 1);
1042 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
1044 GL_DepthMask(false);
1045 GL_Color(0, 0, 0, 1);
1046 GL_Scissor(r_refdef.view.x, r_refdef.view.y, r_refdef.view.width, r_refdef.view.height);
1048 r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
1050 if (gl_ext_separatestencil.integer)
1051 r_shadow_shadowingrendermode = R_SHADOW_RENDERMODE_SEPARATESTENCIL;
1052 else if (gl_ext_stenciltwoside.integer)
1053 r_shadow_shadowingrendermode = R_SHADOW_RENDERMODE_STENCILTWOSIDE;
1055 r_shadow_shadowingrendermode = R_SHADOW_RENDERMODE_STENCIL;
1057 if (r_glsl.integer && gl_support_fragment_shader)
1058 r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_GLSL;
1059 else if (gl_dot3arb && gl_texturecubemap && r_textureunits.integer >= 2 && gl_combine.integer && gl_stencil)
1060 r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_DOT3;
1062 r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_VERTEX;
1065 void R_Shadow_RenderMode_ActiveLight(const rtlight_t *rtlight)
1067 rsurface.rtlight = rtlight;
1070 void R_Shadow_RenderMode_Reset(void)
1073 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_STENCILTWOSIDE)
1075 qglDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
1077 R_Mesh_ColorPointer(NULL, 0, 0);
1078 R_Mesh_ResetTextureState();
1079 GL_DepthRange(0, 1);
1081 GL_DepthMask(false);
1082 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
1083 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);CHECKGLERROR
1084 qglDisable(GL_STENCIL_TEST);CHECKGLERROR
1085 qglStencilMask(~0);CHECKGLERROR
1086 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);CHECKGLERROR
1087 qglStencilFunc(GL_ALWAYS, 128, ~0);CHECKGLERROR
1088 GL_CullFace(r_refdef.view.cullface_back);
1089 GL_Color(1, 1, 1, 1);
1090 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
1091 GL_BlendFunc(GL_ONE, GL_ZERO);
1092 R_SetupGenericShader(false);
1095 void R_Shadow_RenderMode_StencilShadowVolumes(qboolean clearstencil)
1098 R_Shadow_RenderMode_Reset();
1099 GL_ColorMask(0, 0, 0, 0);
1100 GL_PolygonOffset(r_refdef.shadowpolygonfactor, r_refdef.shadowpolygonoffset);CHECKGLERROR
1101 R_SetupDepthOrShadowShader();
1102 qglDepthFunc(GL_LESS);CHECKGLERROR
1103 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
1104 r_shadow_rendermode = r_shadow_shadowingrendermode;
1105 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_SEPARATESTENCIL)
1107 GL_CullFace(GL_NONE);
1108 qglStencilOpSeparate(r_refdef.view.cullface_front, GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR
1109 qglStencilOpSeparate(r_refdef.view.cullface_back, GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
1111 else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_STENCILTWOSIDE)
1113 GL_CullFace(GL_NONE);
1114 qglEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
1115 qglActiveStencilFaceEXT(r_refdef.view.cullface_front);CHECKGLERROR
1116 qglStencilMask(~0);CHECKGLERROR
1117 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR
1118 qglActiveStencilFaceEXT(r_refdef.view.cullface_back);CHECKGLERROR
1119 qglStencilMask(~0);CHECKGLERROR
1120 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
1123 GL_Clear(GL_STENCIL_BUFFER_BIT);
1124 r_refdef.stats.lights_clears++;
1127 void R_Shadow_RenderMode_Lighting(qboolean stenciltest, qboolean transparent)
1130 R_Shadow_RenderMode_Reset();
1131 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1134 qglDepthFunc(GL_EQUAL);CHECKGLERROR
1138 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
1139 // only draw light where this geometry was already rendered AND the
1140 // stencil is 128 (values other than this mean shadow)
1141 qglStencilFunc(GL_EQUAL, 128, ~0);CHECKGLERROR
1143 r_shadow_rendermode = r_shadow_lightingrendermode;
1144 // do global setup needed for the chosen lighting mode
1145 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_LIGHT_GLSL)
1147 R_Mesh_TexBindCubeMap(GL20TU_CUBE, R_GetTexture(rsurface.rtlight->currentcubemap)); // light filter
1148 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 0);
1150 else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_LIGHT_VERTEX)
1151 R_Mesh_ColorPointer(rsurface.array_color4f, 0, 0);
1152 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1155 void R_Shadow_RenderMode_VisibleShadowVolumes(void)
1158 R_Shadow_RenderMode_Reset();
1159 GL_BlendFunc(GL_ONE, GL_ONE);
1160 GL_DepthRange(0, 1);
1161 GL_DepthTest(r_showshadowvolumes.integer < 2);
1162 GL_Color(0.0, 0.0125 * r_refdef.view.colorscale, 0.1 * r_refdef.view.colorscale, 1);
1163 GL_PolygonOffset(r_refdef.shadowpolygonfactor, r_refdef.shadowpolygonoffset);CHECKGLERROR
1164 GL_CullFace(GL_NONE);
1165 r_shadow_rendermode = R_SHADOW_RENDERMODE_VISIBLEVOLUMES;
1168 void R_Shadow_RenderMode_VisibleLighting(qboolean stenciltest, qboolean transparent)
1171 R_Shadow_RenderMode_Reset();
1172 GL_BlendFunc(GL_ONE, GL_ONE);
1173 GL_DepthRange(0, 1);
1174 GL_DepthTest(r_showlighting.integer < 2);
1175 GL_Color(0.1 * r_refdef.view.colorscale, 0.0125 * r_refdef.view.colorscale, 0, 1);
1178 qglDepthFunc(GL_EQUAL);CHECKGLERROR
1182 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
1183 qglStencilFunc(GL_EQUAL, 128, ~0);CHECKGLERROR
1185 r_shadow_rendermode = R_SHADOW_RENDERMODE_VISIBLELIGHTING;
1188 void R_Shadow_RenderMode_End(void)
1191 R_Shadow_RenderMode_Reset();
1192 R_Shadow_RenderMode_ActiveLight(NULL);
1194 GL_Scissor(r_refdef.view.x, r_refdef.view.y, r_refdef.view.width, r_refdef.view.height);
1195 r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
1198 int bboxedges[12][2] =
1217 qboolean R_Shadow_ScissorForBBox(const float *mins, const float *maxs)
1219 int i, ix1, iy1, ix2, iy2;
1220 float x1, y1, x2, y2;
1222 float vertex[20][3];
1231 if (!r_shadow_scissor.integer)
1234 // if view is inside the light box, just say yes it's visible
1235 if (BoxesOverlap(r_refdef.view.origin, r_refdef.view.origin, mins, maxs))
1237 GL_Scissor(r_refdef.view.x, r_refdef.view.y, r_refdef.view.width, r_refdef.view.height);
1241 x1 = y1 = x2 = y2 = 0;
1243 // transform all corners that are infront of the nearclip plane
1244 VectorNegate(r_refdef.view.frustum[4].normal, plane4f);
1245 plane4f[3] = r_refdef.view.frustum[4].dist;
1247 for (i = 0;i < 8;i++)
1249 Vector4Set(corner[i], (i & 1) ? maxs[0] : mins[0], (i & 2) ? maxs[1] : mins[1], (i & 4) ? maxs[2] : mins[2], 1);
1250 dist[i] = DotProduct4(corner[i], plane4f);
1251 sign[i] = dist[i] > 0;
1254 VectorCopy(corner[i], vertex[numvertices]);
1258 // if some points are behind the nearclip, add clipped edge points to make
1259 // sure that the scissor boundary is complete
1260 if (numvertices > 0 && numvertices < 8)
1262 // add clipped edge points
1263 for (i = 0;i < 12;i++)
1265 j = bboxedges[i][0];
1266 k = bboxedges[i][1];
1267 if (sign[j] != sign[k])
1269 f = dist[j] / (dist[j] - dist[k]);
1270 VectorLerp(corner[j], f, corner[k], vertex[numvertices]);
1276 // if we have no points to check, the light is behind the view plane
1280 // if we have some points to transform, check what screen area is covered
1281 x1 = y1 = x2 = y2 = 0;
1283 //Con_Printf("%i vertices to transform...\n", numvertices);
1284 for (i = 0;i < numvertices;i++)
1286 VectorCopy(vertex[i], v);
1287 GL_TransformToScreen(v, v2);
1288 //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]);
1291 if (x1 > v2[0]) x1 = v2[0];
1292 if (x2 < v2[0]) x2 = v2[0];
1293 if (y1 > v2[1]) y1 = v2[1];
1294 if (y2 < v2[1]) y2 = v2[1];
1303 // now convert the scissor rectangle to integer screen coordinates
1304 ix1 = (int)(x1 - 1.0f);
1305 iy1 = (int)(y1 - 1.0f);
1306 ix2 = (int)(x2 + 1.0f);
1307 iy2 = (int)(y2 + 1.0f);
1308 //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
1310 // clamp it to the screen
1311 if (ix1 < r_refdef.view.x) ix1 = r_refdef.view.x;
1312 if (iy1 < r_refdef.view.y) iy1 = r_refdef.view.y;
1313 if (ix2 > r_refdef.view.x + r_refdef.view.width) ix2 = r_refdef.view.x + r_refdef.view.width;
1314 if (iy2 > r_refdef.view.y + r_refdef.view.height) iy2 = r_refdef.view.y + r_refdef.view.height;
1316 // if it is inside out, it's not visible
1317 if (ix2 <= ix1 || iy2 <= iy1)
1320 // the light area is visible, set up the scissor rectangle
1321 GL_Scissor(ix1, iy1, ix2 - ix1, iy2 - iy1);
1322 //qglScissor(ix1, iy1, ix2 - ix1, iy2 - iy1);CHECKGLERROR
1323 //qglEnable(GL_SCISSOR_TEST);CHECKGLERROR
1324 r_refdef.stats.lights_scissored++;
1328 static void R_Shadow_RenderLighting_Light_Vertex_Shading(int firstvertex, int numverts, int numtriangles, const int *element3i, const float *diffusecolor, const float *ambientcolor)
1330 float *vertex3f = rsurface.vertex3f + 3 * firstvertex;
1331 float *normal3f = rsurface.normal3f + 3 * firstvertex;
1332 float *color4f = rsurface.array_color4f + 4 * firstvertex;
1333 float dist, dot, distintensity, shadeintensity, v[3], n[3];
1334 if (r_textureunits.integer >= 3)
1336 if (VectorLength2(diffusecolor) > 0)
1338 for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
1340 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
1341 Matrix4x4_Transform3x3(&rsurface.entitytolight, normal3f, n);
1342 if ((dot = DotProduct(n, v)) < 0)
1344 shadeintensity = -dot / sqrt(VectorLength2(v) * VectorLength2(n));
1345 VectorMA(ambientcolor, shadeintensity, diffusecolor, color4f);
1348 VectorCopy(ambientcolor, color4f);
1349 if (r_refdef.fogenabled)
1352 f = FogPoint_Model(vertex3f);
1353 VectorScale(color4f, f, color4f);
1360 for (;numverts > 0;numverts--, vertex3f += 3, color4f += 4)
1362 VectorCopy(ambientcolor, color4f);
1363 if (r_refdef.fogenabled)
1366 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
1367 f = FogPoint_Model(vertex3f);
1368 VectorScale(color4f, f, color4f);
1374 else if (r_textureunits.integer >= 2)
1376 if (VectorLength2(diffusecolor) > 0)
1378 for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
1380 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
1381 if ((dist = fabs(v[2])) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
1383 Matrix4x4_Transform3x3(&rsurface.entitytolight, normal3f, n);
1384 if ((dot = DotProduct(n, v)) < 0)
1386 shadeintensity = -dot / sqrt(VectorLength2(v) * VectorLength2(n));
1387 color4f[0] = (ambientcolor[0] + shadeintensity * diffusecolor[0]) * distintensity;
1388 color4f[1] = (ambientcolor[1] + shadeintensity * diffusecolor[1]) * distintensity;
1389 color4f[2] = (ambientcolor[2] + shadeintensity * diffusecolor[2]) * distintensity;
1393 color4f[0] = ambientcolor[0] * distintensity;
1394 color4f[1] = ambientcolor[1] * distintensity;
1395 color4f[2] = ambientcolor[2] * distintensity;
1397 if (r_refdef.fogenabled)
1400 f = FogPoint_Model(vertex3f);
1401 VectorScale(color4f, f, color4f);
1405 VectorClear(color4f);
1411 for (;numverts > 0;numverts--, vertex3f += 3, color4f += 4)
1413 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
1414 if ((dist = fabs(v[2])) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
1416 color4f[0] = ambientcolor[0] * distintensity;
1417 color4f[1] = ambientcolor[1] * distintensity;
1418 color4f[2] = ambientcolor[2] * distintensity;
1419 if (r_refdef.fogenabled)
1422 f = FogPoint_Model(vertex3f);
1423 VectorScale(color4f, f, color4f);
1427 VectorClear(color4f);
1434 if (VectorLength2(diffusecolor) > 0)
1436 for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
1438 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
1439 if ((dist = VectorLength(v)) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
1441 distintensity = (1 - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist);
1442 Matrix4x4_Transform3x3(&rsurface.entitytolight, normal3f, n);
1443 if ((dot = DotProduct(n, v)) < 0)
1445 shadeintensity = -dot / sqrt(VectorLength2(v) * VectorLength2(n));
1446 color4f[0] = (ambientcolor[0] + shadeintensity * diffusecolor[0]) * distintensity;
1447 color4f[1] = (ambientcolor[1] + shadeintensity * diffusecolor[1]) * distintensity;
1448 color4f[2] = (ambientcolor[2] + shadeintensity * diffusecolor[2]) * distintensity;
1452 color4f[0] = ambientcolor[0] * distintensity;
1453 color4f[1] = ambientcolor[1] * distintensity;
1454 color4f[2] = ambientcolor[2] * distintensity;
1456 if (r_refdef.fogenabled)
1459 f = FogPoint_Model(vertex3f);
1460 VectorScale(color4f, f, color4f);
1464 VectorClear(color4f);
1470 for (;numverts > 0;numverts--, vertex3f += 3, color4f += 4)
1472 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
1473 if ((dist = VectorLength(v)) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
1475 distintensity = (1 - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist);
1476 color4f[0] = ambientcolor[0] * distintensity;
1477 color4f[1] = ambientcolor[1] * distintensity;
1478 color4f[2] = ambientcolor[2] * distintensity;
1479 if (r_refdef.fogenabled)
1482 f = FogPoint_Model(vertex3f);
1483 VectorScale(color4f, f, color4f);
1487 VectorClear(color4f);
1494 // TODO: use glTexGen instead of feeding vertices to texcoordpointer?
1496 static void R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(int firstvertex, int numvertices, int numtriangles, const int *element3i)
1499 float *out3f = rsurface.array_texcoord3f + 3 * firstvertex;
1500 const float *vertex3f = rsurface.vertex3f + 3 * firstvertex;
1501 const float *svector3f = rsurface.svector3f + 3 * firstvertex;
1502 const float *tvector3f = rsurface.tvector3f + 3 * firstvertex;
1503 const float *normal3f = rsurface.normal3f + 3 * firstvertex;
1505 for (i = 0;i < numvertices;i++, vertex3f += 3, svector3f += 3, tvector3f += 3, normal3f += 3, out3f += 3)
1507 VectorSubtract(rsurface.entitylightorigin, vertex3f, lightdir);
1508 // the cubemap normalizes this for us
1509 out3f[0] = DotProduct(svector3f, lightdir);
1510 out3f[1] = DotProduct(tvector3f, lightdir);
1511 out3f[2] = DotProduct(normal3f, lightdir);
1515 static void R_Shadow_GenTexCoords_Specular_NormalCubeMap(int firstvertex, int numvertices, int numtriangles, const int *element3i)
1518 float *out3f = rsurface.array_texcoord3f + 3 * firstvertex;
1519 const float *vertex3f = rsurface.vertex3f + 3 * firstvertex;
1520 const float *svector3f = rsurface.svector3f + 3 * firstvertex;
1521 const float *tvector3f = rsurface.tvector3f + 3 * firstvertex;
1522 const float *normal3f = rsurface.normal3f + 3 * firstvertex;
1523 float lightdir[3], eyedir[3], halfdir[3];
1524 for (i = 0;i < numvertices;i++, vertex3f += 3, svector3f += 3, tvector3f += 3, normal3f += 3, out3f += 3)
1526 VectorSubtract(rsurface.entitylightorigin, vertex3f, lightdir);
1527 VectorNormalize(lightdir);
1528 VectorSubtract(rsurface.modelorg, vertex3f, eyedir);
1529 VectorNormalize(eyedir);
1530 VectorAdd(lightdir, eyedir, halfdir);
1531 // the cubemap normalizes this for us
1532 out3f[0] = DotProduct(svector3f, halfdir);
1533 out3f[1] = DotProduct(tvector3f, halfdir);
1534 out3f[2] = DotProduct(normal3f, halfdir);
1538 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)
1540 // used to display how many times a surface is lit for level design purposes
1541 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
1544 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)
1546 // ARB2 GLSL shader path (GFFX5200, Radeon 9500)
1547 R_SetupSurfaceShader(lightcolorbase, false, ambientscale, diffusescale, specularscale, RSURFPASS_RTLIGHT);
1548 if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND))
1549 R_Mesh_ColorPointer(rsurface.modellightmapcolor4f, rsurface.modellightmapcolor4f_bufferobject, rsurface.modellightmapcolor4f_bufferoffset);
1551 R_Mesh_ColorPointer(NULL, 0, 0);
1552 R_Mesh_TexMatrix(0, &rsurface.texture->currenttexmatrix);
1553 R_Mesh_TexBind(GL20TU_NORMAL, R_GetTexture(rsurface.texture->currentskinframe->nmap));
1554 R_Mesh_TexBind(GL20TU_COLOR, R_GetTexture(rsurface.texture->basetexture));
1555 R_Mesh_TexBind(GL20TU_GLOSS, R_GetTexture(rsurface.texture->glosstexture));
1556 if (rsurface.texture->backgroundcurrentskinframe)
1558 R_Mesh_TexBind(GL20TU_SECONDARY_NORMAL, R_GetTexture(rsurface.texture->backgroundcurrentskinframe->nmap));
1559 R_Mesh_TexBind(GL20TU_SECONDARY_COLOR, R_GetTexture(rsurface.texture->backgroundbasetexture));
1560 R_Mesh_TexBind(GL20TU_SECONDARY_GLOSS, R_GetTexture(rsurface.texture->backgroundglosstexture));
1562 //R_Mesh_TexBindCubeMap(GL20TU_CUBE, R_GetTexture(rsurface.rtlight->currentcubemap));
1563 R_Mesh_TexBind(GL20TU_FOGMASK, R_GetTexture(r_texture_fogattenuation));
1564 if(rsurface.texture->colormapping)
1566 R_Mesh_TexBind(GL20TU_PANTS, R_GetTexture(rsurface.texture->currentskinframe->pants));
1567 R_Mesh_TexBind(GL20TU_SHIRT, R_GetTexture(rsurface.texture->currentskinframe->shirt));
1569 R_Mesh_TexBind(GL20TU_ATTENUATION, R_GetTexture(r_shadow_attenuationgradienttexture));
1570 R_Mesh_TexCoordPointer(0, 2, rsurface.texcoordtexture2f, rsurface.texcoordtexture2f_bufferobject, rsurface.texcoordtexture2f_bufferoffset);
1571 R_Mesh_TexCoordPointer(1, 3, rsurface.svector3f, rsurface.svector3f_bufferobject, rsurface.svector3f_bufferoffset);
1572 R_Mesh_TexCoordPointer(2, 3, rsurface.tvector3f, rsurface.tvector3f_bufferobject, rsurface.tvector3f_bufferoffset);
1573 R_Mesh_TexCoordPointer(3, 3, rsurface.normal3f, rsurface.normal3f_bufferobject, rsurface.normal3f_bufferoffset);
1574 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
1576 qglDepthFunc(GL_EQUAL);CHECKGLERROR
1578 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
1579 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
1581 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
1585 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)
1587 // shared final code for all the dot3 layers
1589 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 0);
1590 for (renders = 0;renders < 64 && (r > 0 || g > 0 || b > 0);renders++, r--, g--, b--)
1592 GL_Color(bound(0, r, 1), bound(0, g, 1), bound(0, b, 1), 1);
1593 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
1597 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)
1600 // colorscale accounts for how much we multiply the brightness
1603 // mult is how many times the final pass of the lighting will be
1604 // performed to get more brightness than otherwise possible.
1606 // Limit mult to 64 for sanity sake.
1608 if (r_shadow_texture3d.integer && rsurface.rtlight->currentcubemap != r_texture_whitecube && r_textureunits.integer >= 4)
1610 // 3 3D combine path (Geforce3, Radeon 8500)
1611 memset(&m, 0, sizeof(m));
1612 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
1613 m.pointer_texcoord3f[0] = rsurface.vertex3f;
1614 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
1615 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
1616 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
1617 m.tex[1] = R_GetTexture(basetexture);
1618 m.pointer_texcoord[1] = rsurface.texcoordtexture2f;
1619 m.pointer_texcoord_bufferobject[1] = rsurface.texcoordtexture2f_bufferobject;
1620 m.pointer_texcoord_bufferoffset[1] = rsurface.texcoordtexture2f_bufferoffset;
1621 m.texmatrix[1] = rsurface.texture->currenttexmatrix;
1622 m.texcubemap[2] = R_GetTexture(rsurface.rtlight->currentcubemap);
1623 m.pointer_texcoord3f[2] = rsurface.vertex3f;
1624 m.pointer_texcoord_bufferobject[2] = rsurface.vertex3f_bufferobject;
1625 m.pointer_texcoord_bufferoffset[2] = rsurface.vertex3f_bufferoffset;
1626 m.texmatrix[2] = rsurface.entitytolight;
1627 GL_BlendFunc(GL_ONE, GL_ONE);
1629 else if (r_shadow_texture3d.integer && rsurface.rtlight->currentcubemap == r_texture_whitecube && r_textureunits.integer >= 2)
1631 // 2 3D combine path (Geforce3, original Radeon)
1632 memset(&m, 0, sizeof(m));
1633 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
1634 m.pointer_texcoord3f[0] = rsurface.vertex3f;
1635 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
1636 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
1637 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
1638 m.tex[1] = R_GetTexture(basetexture);
1639 m.pointer_texcoord[1] = rsurface.texcoordtexture2f;
1640 m.pointer_texcoord_bufferobject[1] = rsurface.texcoordtexture2f_bufferobject;
1641 m.pointer_texcoord_bufferoffset[1] = rsurface.texcoordtexture2f_bufferoffset;
1642 m.texmatrix[1] = rsurface.texture->currenttexmatrix;
1643 GL_BlendFunc(GL_ONE, GL_ONE);
1645 else if (r_textureunits.integer >= 4 && rsurface.rtlight->currentcubemap != r_texture_whitecube)
1647 // 4 2D combine path (Geforce3, Radeon 8500)
1648 memset(&m, 0, sizeof(m));
1649 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
1650 m.pointer_texcoord3f[0] = rsurface.vertex3f;
1651 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
1652 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
1653 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
1654 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1655 m.pointer_texcoord3f[1] = rsurface.vertex3f;
1656 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
1657 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
1658 m.texmatrix[1] = rsurface.entitytoattenuationz;
1659 m.tex[2] = R_GetTexture(basetexture);
1660 m.pointer_texcoord[2] = rsurface.texcoordtexture2f;
1661 m.pointer_texcoord_bufferobject[2] = rsurface.texcoordtexture2f_bufferobject;
1662 m.pointer_texcoord_bufferoffset[2] = rsurface.texcoordtexture2f_bufferoffset;
1663 m.texmatrix[2] = rsurface.texture->currenttexmatrix;
1664 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
1666 m.texcubemap[3] = R_GetTexture(rsurface.rtlight->currentcubemap);
1667 m.pointer_texcoord3f[3] = rsurface.vertex3f;
1668 m.pointer_texcoord_bufferobject[3] = rsurface.vertex3f_bufferobject;
1669 m.pointer_texcoord_bufferoffset[3] = rsurface.vertex3f_bufferoffset;
1670 m.texmatrix[3] = rsurface.entitytolight;
1672 GL_BlendFunc(GL_ONE, GL_ONE);
1674 else if (r_textureunits.integer >= 3 && rsurface.rtlight->currentcubemap == r_texture_whitecube)
1676 // 3 2D combine path (Geforce3, original Radeon)
1677 memset(&m, 0, sizeof(m));
1678 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
1679 m.pointer_texcoord3f[0] = rsurface.vertex3f;
1680 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
1681 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
1682 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
1683 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1684 m.pointer_texcoord3f[1] = rsurface.vertex3f;
1685 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
1686 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
1687 m.texmatrix[1] = rsurface.entitytoattenuationz;
1688 m.tex[2] = R_GetTexture(basetexture);
1689 m.pointer_texcoord[2] = rsurface.texcoordtexture2f;
1690 m.pointer_texcoord_bufferobject[2] = rsurface.texcoordtexture2f_bufferobject;
1691 m.pointer_texcoord_bufferoffset[2] = rsurface.texcoordtexture2f_bufferoffset;
1692 m.texmatrix[2] = rsurface.texture->currenttexmatrix;
1693 GL_BlendFunc(GL_ONE, GL_ONE);
1697 // 2/2/2 2D combine path (any dot3 card)
1698 memset(&m, 0, sizeof(m));
1699 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
1700 m.pointer_texcoord3f[0] = rsurface.vertex3f;
1701 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
1702 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
1703 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
1704 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1705 m.pointer_texcoord3f[1] = rsurface.vertex3f;
1706 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
1707 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
1708 m.texmatrix[1] = rsurface.entitytoattenuationz;
1709 R_Mesh_TextureState(&m);
1710 GL_ColorMask(0,0,0,1);
1711 GL_BlendFunc(GL_ONE, GL_ZERO);
1712 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
1715 memset(&m, 0, sizeof(m));
1716 m.tex[0] = R_GetTexture(basetexture);
1717 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
1718 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
1719 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
1720 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
1721 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
1723 m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
1724 m.pointer_texcoord3f[1] = rsurface.vertex3f;
1725 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
1726 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
1727 m.texmatrix[1] = rsurface.entitytolight;
1729 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
1731 // this final code is shared
1732 R_Mesh_TextureState(&m);
1733 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);
1736 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)
1739 // colorscale accounts for how much we multiply the brightness
1742 // mult is how many times the final pass of the lighting will be
1743 // performed to get more brightness than otherwise possible.
1745 // Limit mult to 64 for sanity sake.
1747 // generate normalization cubemap texcoords
1748 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(firstvertex, numvertices, numtriangles, element3i);
1749 if (r_shadow_texture3d.integer && r_textureunits.integer >= 4)
1751 // 3/2 3D combine path (Geforce3, Radeon 8500)
1752 memset(&m, 0, sizeof(m));
1753 m.tex[0] = R_GetTexture(normalmaptexture);
1754 m.texcombinergb[0] = GL_REPLACE;
1755 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
1756 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
1757 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
1758 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
1759 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
1760 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1761 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
1762 m.pointer_texcoord_bufferobject[1] = 0;
1763 m.pointer_texcoord_bufferoffset[1] = 0;
1764 m.tex3d[2] = R_GetTexture(r_shadow_attenuation3dtexture);
1765 m.pointer_texcoord3f[2] = rsurface.vertex3f;
1766 m.pointer_texcoord_bufferobject[2] = rsurface.vertex3f_bufferobject;
1767 m.pointer_texcoord_bufferoffset[2] = rsurface.vertex3f_bufferoffset;
1768 m.texmatrix[2] = rsurface.entitytoattenuationxyz;
1769 R_Mesh_TextureState(&m);
1770 GL_ColorMask(0,0,0,1);
1771 GL_BlendFunc(GL_ONE, GL_ZERO);
1772 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
1775 memset(&m, 0, sizeof(m));
1776 m.tex[0] = R_GetTexture(basetexture);
1777 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
1778 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
1779 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
1780 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
1781 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
1783 m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
1784 m.pointer_texcoord3f[1] = rsurface.vertex3f;
1785 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
1786 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
1787 m.texmatrix[1] = rsurface.entitytolight;
1789 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
1791 else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && rsurface.rtlight->currentcubemap != r_texture_whitecube)
1793 // 1/2/2 3D combine path (original Radeon)
1794 memset(&m, 0, sizeof(m));
1795 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
1796 m.pointer_texcoord3f[0] = rsurface.vertex3f;
1797 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
1798 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
1799 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
1800 R_Mesh_TextureState(&m);
1801 GL_ColorMask(0,0,0,1);
1802 GL_BlendFunc(GL_ONE, GL_ZERO);
1803 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
1806 memset(&m, 0, sizeof(m));
1807 m.tex[0] = R_GetTexture(normalmaptexture);
1808 m.texcombinergb[0] = GL_REPLACE;
1809 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
1810 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
1811 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
1812 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
1813 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
1814 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1815 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
1816 m.pointer_texcoord_bufferobject[1] = 0;
1817 m.pointer_texcoord_bufferoffset[1] = 0;
1818 R_Mesh_TextureState(&m);
1819 GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
1820 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
1823 memset(&m, 0, sizeof(m));
1824 m.tex[0] = R_GetTexture(basetexture);
1825 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
1826 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
1827 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
1828 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
1829 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
1831 m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
1832 m.pointer_texcoord3f[1] = rsurface.vertex3f;
1833 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
1834 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
1835 m.texmatrix[1] = rsurface.entitytolight;
1837 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
1839 else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && rsurface.rtlight->currentcubemap == r_texture_whitecube)
1841 // 2/2 3D combine path (original Radeon)
1842 memset(&m, 0, sizeof(m));
1843 m.tex[0] = R_GetTexture(normalmaptexture);
1844 m.texcombinergb[0] = GL_REPLACE;
1845 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
1846 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
1847 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
1848 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
1849 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
1850 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1851 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
1852 m.pointer_texcoord_bufferobject[1] = 0;
1853 m.pointer_texcoord_bufferoffset[1] = 0;
1854 R_Mesh_TextureState(&m);
1855 GL_ColorMask(0,0,0,1);
1856 GL_BlendFunc(GL_ONE, GL_ZERO);
1857 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
1860 memset(&m, 0, sizeof(m));
1861 m.tex[0] = R_GetTexture(basetexture);
1862 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
1863 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
1864 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
1865 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
1866 m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
1867 m.pointer_texcoord3f[1] = rsurface.vertex3f;
1868 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
1869 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
1870 m.texmatrix[1] = rsurface.entitytoattenuationxyz;
1871 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
1873 else if (r_textureunits.integer >= 4)
1875 // 4/2 2D combine path (Geforce3, Radeon 8500)
1876 memset(&m, 0, sizeof(m));
1877 m.tex[0] = R_GetTexture(normalmaptexture);
1878 m.texcombinergb[0] = GL_REPLACE;
1879 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
1880 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
1881 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
1882 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
1883 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
1884 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1885 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
1886 m.pointer_texcoord_bufferobject[1] = 0;
1887 m.pointer_texcoord_bufferoffset[1] = 0;
1888 m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
1889 m.pointer_texcoord3f[2] = rsurface.vertex3f;
1890 m.pointer_texcoord_bufferobject[2] = rsurface.vertex3f_bufferobject;
1891 m.pointer_texcoord_bufferoffset[2] = rsurface.vertex3f_bufferoffset;
1892 m.texmatrix[2] = rsurface.entitytoattenuationxyz;
1893 m.tex[3] = R_GetTexture(r_shadow_attenuation2dtexture);
1894 m.pointer_texcoord3f[3] = rsurface.vertex3f;
1895 m.pointer_texcoord_bufferobject[3] = rsurface.vertex3f_bufferobject;
1896 m.pointer_texcoord_bufferoffset[3] = rsurface.vertex3f_bufferoffset;
1897 m.texmatrix[3] = rsurface.entitytoattenuationz;
1898 R_Mesh_TextureState(&m);
1899 GL_ColorMask(0,0,0,1);
1900 GL_BlendFunc(GL_ONE, GL_ZERO);
1901 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
1904 memset(&m, 0, sizeof(m));
1905 m.tex[0] = R_GetTexture(basetexture);
1906 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
1907 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
1908 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
1909 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
1910 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
1912 m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
1913 m.pointer_texcoord3f[1] = rsurface.vertex3f;
1914 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
1915 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
1916 m.texmatrix[1] = rsurface.entitytolight;
1918 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
1922 // 2/2/2 2D combine path (any dot3 card)
1923 memset(&m, 0, sizeof(m));
1924 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
1925 m.pointer_texcoord3f[0] = rsurface.vertex3f;
1926 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
1927 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
1928 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
1929 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1930 m.pointer_texcoord3f[1] = rsurface.vertex3f;
1931 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
1932 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
1933 m.texmatrix[1] = rsurface.entitytoattenuationz;
1934 R_Mesh_TextureState(&m);
1935 GL_ColorMask(0,0,0,1);
1936 GL_BlendFunc(GL_ONE, GL_ZERO);
1937 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
1940 memset(&m, 0, sizeof(m));
1941 m.tex[0] = R_GetTexture(normalmaptexture);
1942 m.texcombinergb[0] = GL_REPLACE;
1943 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
1944 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
1945 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
1946 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
1947 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
1948 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1949 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
1950 m.pointer_texcoord_bufferobject[1] = 0;
1951 m.pointer_texcoord_bufferoffset[1] = 0;
1952 R_Mesh_TextureState(&m);
1953 GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
1954 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
1957 memset(&m, 0, sizeof(m));
1958 m.tex[0] = R_GetTexture(basetexture);
1959 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
1960 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
1961 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
1962 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
1963 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
1965 m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
1966 m.pointer_texcoord3f[1] = rsurface.vertex3f;
1967 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
1968 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
1969 m.texmatrix[1] = rsurface.entitytolight;
1971 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
1973 // this final code is shared
1974 R_Mesh_TextureState(&m);
1975 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);
1978 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)
1980 float glossexponent;
1982 // FIXME: detect blendsquare!
1983 //if (!gl_support_blendsquare)
1986 // generate normalization cubemap texcoords
1987 R_Shadow_GenTexCoords_Specular_NormalCubeMap(firstvertex, numvertices, numtriangles, element3i);
1988 if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && rsurface.rtlight->currentcubemap != r_texture_whitecube)
1990 // 2/0/0/1/2 3D combine blendsquare path
1991 memset(&m, 0, sizeof(m));
1992 m.tex[0] = R_GetTexture(normalmaptexture);
1993 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
1994 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
1995 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
1996 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
1997 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
1998 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1999 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
2000 m.pointer_texcoord_bufferobject[1] = 0;
2001 m.pointer_texcoord_bufferoffset[1] = 0;
2002 R_Mesh_TextureState(&m);
2003 GL_ColorMask(0,0,0,1);
2004 // this squares the result
2005 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
2006 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2008 // second and third pass
2009 R_Mesh_ResetTextureState();
2010 // square alpha in framebuffer a few times to make it shiny
2011 GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
2012 for (glossexponent = 2;glossexponent * 2 <= r_shadow_glossexponent.value;glossexponent *= 2)
2013 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2016 memset(&m, 0, sizeof(m));
2017 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
2018 m.pointer_texcoord3f[0] = rsurface.vertex3f;
2019 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
2020 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
2021 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
2022 R_Mesh_TextureState(&m);
2023 GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
2024 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2027 memset(&m, 0, sizeof(m));
2028 m.tex[0] = R_GetTexture(glosstexture);
2029 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2030 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2031 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2032 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2033 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2035 m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
2036 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2037 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2038 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2039 m.texmatrix[1] = rsurface.entitytolight;
2041 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2043 else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && rsurface.rtlight->currentcubemap == r_texture_whitecube /* && gl_support_blendsquare*/) // FIXME: detect blendsquare!
2045 // 2/0/0/2 3D combine blendsquare path
2046 memset(&m, 0, sizeof(m));
2047 m.tex[0] = R_GetTexture(normalmaptexture);
2048 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2049 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2050 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2051 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2052 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
2053 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2054 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
2055 m.pointer_texcoord_bufferobject[1] = 0;
2056 m.pointer_texcoord_bufferoffset[1] = 0;
2057 R_Mesh_TextureState(&m);
2058 GL_ColorMask(0,0,0,1);
2059 // this squares the result
2060 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
2061 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2063 // second and third pass
2064 R_Mesh_ResetTextureState();
2065 // square alpha in framebuffer a few times to make it shiny
2066 GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
2067 for (glossexponent = 2;glossexponent * 2 <= r_shadow_glossexponent.value;glossexponent *= 2)
2068 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2071 memset(&m, 0, sizeof(m));
2072 m.tex[0] = R_GetTexture(glosstexture);
2073 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2074 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2075 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2076 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2077 m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
2078 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2079 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2080 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2081 m.texmatrix[1] = rsurface.entitytoattenuationxyz;
2082 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2086 // 2/0/0/2/2 2D combine blendsquare path
2087 memset(&m, 0, sizeof(m));
2088 m.tex[0] = R_GetTexture(normalmaptexture);
2089 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2090 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2091 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2092 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2093 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
2094 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2095 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
2096 m.pointer_texcoord_bufferobject[1] = 0;
2097 m.pointer_texcoord_bufferoffset[1] = 0;
2098 R_Mesh_TextureState(&m);
2099 GL_ColorMask(0,0,0,1);
2100 // this squares the result
2101 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
2102 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2104 // second and third pass
2105 R_Mesh_ResetTextureState();
2106 // square alpha in framebuffer a few times to make it shiny
2107 GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
2108 for (glossexponent = 2;glossexponent * 2 <= r_shadow_glossexponent.value;glossexponent *= 2)
2109 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2112 memset(&m, 0, sizeof(m));
2113 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
2114 m.pointer_texcoord3f[0] = rsurface.vertex3f;
2115 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
2116 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
2117 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
2118 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
2119 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2120 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2121 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2122 m.texmatrix[1] = rsurface.entitytoattenuationz;
2123 R_Mesh_TextureState(&m);
2124 GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
2125 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2128 memset(&m, 0, sizeof(m));
2129 m.tex[0] = R_GetTexture(glosstexture);
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 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2136 m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
2137 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2138 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2139 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2140 m.texmatrix[1] = rsurface.entitytolight;
2142 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2144 // this final code is shared
2145 R_Mesh_TextureState(&m);
2146 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);
2149 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)
2151 // ARB path (any Geforce, any Radeon)
2152 qboolean doambient = ambientscale > 0;
2153 qboolean dodiffuse = diffusescale > 0;
2154 qboolean dospecular = specularscale > 0;
2155 if (!doambient && !dodiffuse && !dospecular)
2157 R_Mesh_ColorPointer(NULL, 0, 0);
2159 R_Shadow_RenderLighting_Light_Dot3_AmbientPass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, basetexture, ambientscale * r_refdef.view.colorscale);
2161 R_Shadow_RenderLighting_Light_Dot3_DiffusePass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, basetexture, normalmaptexture, diffusescale * r_refdef.view.colorscale);
2165 R_Shadow_RenderLighting_Light_Dot3_AmbientPass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorpants, pantstexture, ambientscale * r_refdef.view.colorscale);
2167 R_Shadow_RenderLighting_Light_Dot3_DiffusePass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorpants, pantstexture, normalmaptexture, diffusescale * r_refdef.view.colorscale);
2172 R_Shadow_RenderLighting_Light_Dot3_AmbientPass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorshirt, shirttexture, ambientscale * r_refdef.view.colorscale);
2174 R_Shadow_RenderLighting_Light_Dot3_DiffusePass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorshirt, shirttexture, normalmaptexture, diffusescale * r_refdef.view.colorscale);
2177 R_Shadow_RenderLighting_Light_Dot3_SpecularPass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, glosstexture, normalmaptexture, specularscale * r_refdef.view.colorscale);
2180 static void R_Shadow_RenderLighting_Light_Vertex_Pass(int firstvertex, int numvertices, int numtriangles, const int *element3i, vec3_t diffusecolor2, vec3_t ambientcolor2)
2187 int newnumtriangles;
2191 int maxtriangles = 4096;
2192 int newelements[4096*3];
2193 R_Shadow_RenderLighting_Light_Vertex_Shading(firstvertex, numvertices, numtriangles, element3i, diffusecolor2, ambientcolor2);
2194 for (renders = 0;renders < 64;renders++)
2199 newnumtriangles = 0;
2201 // due to low fillrate on the cards this vertex lighting path is
2202 // designed for, we manually cull all triangles that do not
2203 // contain a lit vertex
2204 // this builds batches of triangles from multiple surfaces and
2205 // renders them at once
2206 for (i = 0, e = element3i;i < numtriangles;i++, e += 3)
2208 if (VectorLength2(rsurface.array_color4f + e[0] * 4) + VectorLength2(rsurface.array_color4f + e[1] * 4) + VectorLength2(rsurface.array_color4f + e[2] * 4) >= 0.01)
2210 if (newnumtriangles)
2212 newfirstvertex = min(newfirstvertex, e[0]);
2213 newlastvertex = max(newlastvertex, e[0]);
2217 newfirstvertex = e[0];
2218 newlastvertex = e[0];
2220 newfirstvertex = min(newfirstvertex, e[1]);
2221 newlastvertex = max(newlastvertex, e[1]);
2222 newfirstvertex = min(newfirstvertex, e[2]);
2223 newlastvertex = max(newlastvertex, e[2]);
2229 if (newnumtriangles >= maxtriangles)
2231 R_Mesh_Draw(newfirstvertex, newlastvertex - newfirstvertex + 1, 0, newnumtriangles, newelements, NULL, 0, 0);
2232 newnumtriangles = 0;
2238 if (newnumtriangles >= 1)
2240 R_Mesh_Draw(newfirstvertex, newlastvertex - newfirstvertex + 1, 0, newnumtriangles, newelements, NULL, 0, 0);
2243 // if we couldn't find any lit triangles, exit early
2246 // now reduce the intensity for the next overbright pass
2247 // we have to clamp to 0 here incase the drivers have improper
2248 // handling of negative colors
2249 // (some old drivers even have improper handling of >1 color)
2251 for (i = 0, c = rsurface.array_color4f + 4 * firstvertex;i < numvertices;i++, c += 4)
2253 if (c[0] > 1 || c[1] > 1 || c[2] > 1)
2255 c[0] = max(0, c[0] - 1);
2256 c[1] = max(0, c[1] - 1);
2257 c[2] = max(0, c[2] - 1);
2269 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)
2271 // OpenGL 1.1 path (anything)
2272 float ambientcolorbase[3], diffusecolorbase[3];
2273 float ambientcolorpants[3], diffusecolorpants[3];
2274 float ambientcolorshirt[3], diffusecolorshirt[3];
2276 VectorScale(lightcolorbase, ambientscale * 2 * r_refdef.view.colorscale, ambientcolorbase);
2277 VectorScale(lightcolorbase, diffusescale * 2 * r_refdef.view.colorscale, diffusecolorbase);
2278 VectorScale(lightcolorpants, ambientscale * 2 * r_refdef.view.colorscale, ambientcolorpants);
2279 VectorScale(lightcolorpants, diffusescale * 2 * r_refdef.view.colorscale, diffusecolorpants);
2280 VectorScale(lightcolorshirt, ambientscale * 2 * r_refdef.view.colorscale, ambientcolorshirt);
2281 VectorScale(lightcolorshirt, diffusescale * 2 * r_refdef.view.colorscale, diffusecolorshirt);
2282 memset(&m, 0, sizeof(m));
2283 m.tex[0] = R_GetTexture(basetexture);
2284 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2285 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2286 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2287 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2288 if (r_textureunits.integer >= 2)
2291 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
2292 m.texmatrix[1] = rsurface.entitytoattenuationxyz;
2293 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2294 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2295 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2296 if (r_textureunits.integer >= 3)
2298 // Voodoo4 or Kyro (or Geforce3/Radeon with gl_combine off)
2299 m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
2300 m.texmatrix[2] = rsurface.entitytoattenuationz;
2301 m.pointer_texcoord3f[2] = rsurface.vertex3f;
2302 m.pointer_texcoord_bufferobject[2] = rsurface.vertex3f_bufferobject;
2303 m.pointer_texcoord_bufferoffset[2] = rsurface.vertex3f_bufferoffset;
2306 R_Mesh_TextureState(&m);
2307 //R_Mesh_TexBind(0, R_GetTexture(basetexture));
2308 R_Shadow_RenderLighting_Light_Vertex_Pass(firstvertex, numvertices, numtriangles, element3i, diffusecolorbase, ambientcolorbase);
2311 R_Mesh_TexBind(0, R_GetTexture(pantstexture));
2312 R_Shadow_RenderLighting_Light_Vertex_Pass(firstvertex, numvertices, numtriangles, element3i, diffusecolorpants, ambientcolorpants);
2316 R_Mesh_TexBind(0, R_GetTexture(shirttexture));
2317 R_Shadow_RenderLighting_Light_Vertex_Pass(firstvertex, numvertices, numtriangles, element3i, diffusecolorshirt, ambientcolorshirt);
2321 extern cvar_t gl_lightmaps;
2322 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)
2324 float ambientscale, diffusescale, specularscale;
2325 vec3_t lightcolorbase, lightcolorpants, lightcolorshirt;
2327 // calculate colors to render this texture with
2328 lightcolorbase[0] = rsurface.rtlight->currentcolor[0] * rsurface.texture->dlightcolor[0];
2329 lightcolorbase[1] = rsurface.rtlight->currentcolor[1] * rsurface.texture->dlightcolor[1];
2330 lightcolorbase[2] = rsurface.rtlight->currentcolor[2] * rsurface.texture->dlightcolor[2];
2331 ambientscale = rsurface.rtlight->ambientscale;
2332 diffusescale = rsurface.rtlight->diffusescale;
2333 specularscale = rsurface.rtlight->specularscale * rsurface.texture->specularscale;
2334 if (!r_shadow_usenormalmap.integer)
2336 ambientscale += 1.0f * diffusescale;
2340 if ((ambientscale + diffusescale) * VectorLength2(lightcolorbase) + specularscale * VectorLength2(lightcolorbase) < (1.0f / 1048576.0f))
2342 RSurf_SetupDepthAndCulling();
2343 nmap = rsurface.texture->currentskinframe->nmap;
2344 if (gl_lightmaps.integer)
2345 nmap = r_texture_blanknormalmap;
2346 if (rsurface.texture->colormapping && !gl_lightmaps.integer)
2348 qboolean dopants = rsurface.texture->currentskinframe->pants != NULL && VectorLength2(rsurface.colormap_pantscolor) >= (1.0f / 1048576.0f);
2349 qboolean doshirt = rsurface.texture->currentskinframe->shirt != NULL && VectorLength2(rsurface.colormap_shirtcolor) >= (1.0f / 1048576.0f);
2352 lightcolorpants[0] = lightcolorbase[0] * rsurface.colormap_pantscolor[0];
2353 lightcolorpants[1] = lightcolorbase[1] * rsurface.colormap_pantscolor[1];
2354 lightcolorpants[2] = lightcolorbase[2] * rsurface.colormap_pantscolor[2];
2357 VectorClear(lightcolorpants);
2360 lightcolorshirt[0] = lightcolorbase[0] * rsurface.colormap_shirtcolor[0];
2361 lightcolorshirt[1] = lightcolorbase[1] * rsurface.colormap_shirtcolor[1];
2362 lightcolorshirt[2] = lightcolorbase[2] * rsurface.colormap_shirtcolor[2];
2365 VectorClear(lightcolorshirt);
2366 switch (r_shadow_rendermode)
2368 case R_SHADOW_RENDERMODE_VISIBLELIGHTING:
2369 GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST) && !r_showdisabledepthtest.integer);
2370 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);
2372 case R_SHADOW_RENDERMODE_LIGHT_GLSL:
2373 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);
2375 case R_SHADOW_RENDERMODE_LIGHT_DOT3:
2376 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);
2378 case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
2379 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);
2382 Con_Printf("R_Shadow_RenderLighting: unknown r_shadow_rendermode %i\n", r_shadow_rendermode);
2388 switch (r_shadow_rendermode)
2390 case R_SHADOW_RENDERMODE_VISIBLELIGHTING:
2391 GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST) && !r_showdisabledepthtest.integer);
2392 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);
2394 case R_SHADOW_RENDERMODE_LIGHT_GLSL:
2395 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);
2397 case R_SHADOW_RENDERMODE_LIGHT_DOT3:
2398 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);
2400 case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
2401 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);
2404 Con_Printf("R_Shadow_RenderLighting: unknown r_shadow_rendermode %i\n", r_shadow_rendermode);
2410 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)
2412 matrix4x4_t tempmatrix = *matrix;
2413 Matrix4x4_Scale(&tempmatrix, r_shadow_lightradiusscale.value, 1);
2415 // if this light has been compiled before, free the associated data
2416 R_RTLight_Uncompile(rtlight);
2418 // clear it completely to avoid any lingering data
2419 memset(rtlight, 0, sizeof(*rtlight));
2421 // copy the properties
2422 rtlight->matrix_lighttoworld = tempmatrix;
2423 Matrix4x4_Invert_Simple(&rtlight->matrix_worldtolight, &tempmatrix);
2424 Matrix4x4_OriginFromMatrix(&tempmatrix, rtlight->shadoworigin);
2425 rtlight->radius = Matrix4x4_ScaleFromMatrix(&tempmatrix);
2426 VectorCopy(color, rtlight->color);
2427 rtlight->cubemapname[0] = 0;
2428 if (cubemapname && cubemapname[0])
2429 strlcpy(rtlight->cubemapname, cubemapname, sizeof(rtlight->cubemapname));
2430 rtlight->shadow = shadow;
2431 rtlight->corona = corona;
2432 rtlight->style = style;
2433 rtlight->isstatic = isstatic;
2434 rtlight->coronasizescale = coronasizescale;
2435 rtlight->ambientscale = ambientscale;
2436 rtlight->diffusescale = diffusescale;
2437 rtlight->specularscale = specularscale;
2438 rtlight->flags = flags;
2440 // compute derived data
2441 //rtlight->cullradius = rtlight->radius;
2442 //rtlight->cullradius2 = rtlight->radius * rtlight->radius;
2443 rtlight->cullmins[0] = rtlight->shadoworigin[0] - rtlight->radius;
2444 rtlight->cullmins[1] = rtlight->shadoworigin[1] - rtlight->radius;
2445 rtlight->cullmins[2] = rtlight->shadoworigin[2] - rtlight->radius;
2446 rtlight->cullmaxs[0] = rtlight->shadoworigin[0] + rtlight->radius;
2447 rtlight->cullmaxs[1] = rtlight->shadoworigin[1] + rtlight->radius;
2448 rtlight->cullmaxs[2] = rtlight->shadoworigin[2] + rtlight->radius;
2451 // compiles rtlight geometry
2452 // (undone by R_FreeCompiledRTLight, which R_UpdateLight calls)
2453 void R_RTLight_Compile(rtlight_t *rtlight)
2456 int numsurfaces, numleafs, numleafpvsbytes, numshadowtrispvsbytes, numlighttrispvsbytes;
2457 int lighttris, shadowtris, shadowmeshes, shadowmeshtris;
2458 entity_render_t *ent = r_refdef.scene.worldentity;
2459 dp_model_t *model = r_refdef.scene.worldmodel;
2460 unsigned char *data;
2462 // compile the light
2463 rtlight->compiled = true;
2464 rtlight->static_numleafs = 0;
2465 rtlight->static_numleafpvsbytes = 0;
2466 rtlight->static_leaflist = NULL;
2467 rtlight->static_leafpvs = NULL;
2468 rtlight->static_numsurfaces = 0;
2469 rtlight->static_surfacelist = NULL;
2470 rtlight->cullmins[0] = rtlight->shadoworigin[0] - rtlight->radius;
2471 rtlight->cullmins[1] = rtlight->shadoworigin[1] - rtlight->radius;
2472 rtlight->cullmins[2] = rtlight->shadoworigin[2] - rtlight->radius;
2473 rtlight->cullmaxs[0] = rtlight->shadoworigin[0] + rtlight->radius;
2474 rtlight->cullmaxs[1] = rtlight->shadoworigin[1] + rtlight->radius;
2475 rtlight->cullmaxs[2] = rtlight->shadoworigin[2] + rtlight->radius;
2477 if (model && model->GetLightInfo)
2479 // this variable must be set for the CompileShadowVolume code
2480 r_shadow_compilingrtlight = rtlight;
2481 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);
2482 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);
2483 numleafpvsbytes = (model->brush.num_leafs + 7) >> 3;
2484 numshadowtrispvsbytes = ((model->brush.shadowmesh ? model->brush.shadowmesh->numtriangles : model->surfmesh.num_triangles) + 7) >> 3;
2485 numlighttrispvsbytes = (model->surfmesh.num_triangles + 7) >> 3;
2486 data = (unsigned char *)Mem_Alloc(r_main_mempool, sizeof(int) * numsurfaces + sizeof(int) * numleafs + numleafpvsbytes + numshadowtrispvsbytes + numlighttrispvsbytes);
2487 rtlight->static_numsurfaces = numsurfaces;
2488 rtlight->static_surfacelist = (int *)data;data += sizeof(int) * numsurfaces;
2489 rtlight->static_numleafs = numleafs;
2490 rtlight->static_leaflist = (int *)data;data += sizeof(int) * numleafs;
2491 rtlight->static_numleafpvsbytes = numleafpvsbytes;
2492 rtlight->static_leafpvs = (unsigned char *)data;data += numleafpvsbytes;
2493 rtlight->static_numshadowtrispvsbytes = numshadowtrispvsbytes;
2494 rtlight->static_shadowtrispvs = (unsigned char *)data;data += numshadowtrispvsbytes;
2495 rtlight->static_numlighttrispvsbytes = numlighttrispvsbytes;
2496 rtlight->static_lighttrispvs = (unsigned char *)data;data += numlighttrispvsbytes;
2497 if (rtlight->static_numsurfaces)
2498 memcpy(rtlight->static_surfacelist, r_shadow_buffer_surfacelist, rtlight->static_numsurfaces * sizeof(*rtlight->static_surfacelist));
2499 if (rtlight->static_numleafs)
2500 memcpy(rtlight->static_leaflist, r_shadow_buffer_leaflist, rtlight->static_numleafs * sizeof(*rtlight->static_leaflist));
2501 if (rtlight->static_numleafpvsbytes)
2502 memcpy(rtlight->static_leafpvs, r_shadow_buffer_leafpvs, rtlight->static_numleafpvsbytes);
2503 if (rtlight->static_numshadowtrispvsbytes)
2504 memcpy(rtlight->static_shadowtrispvs, r_shadow_buffer_shadowtrispvs, rtlight->static_numshadowtrispvsbytes);
2505 if (rtlight->static_numlighttrispvsbytes)
2506 memcpy(rtlight->static_lighttrispvs, r_shadow_buffer_lighttrispvs, rtlight->static_numlighttrispvsbytes);
2507 if (model->CompileShadowVolume && rtlight->shadow)
2508 model->CompileShadowVolume(ent, rtlight->shadoworigin, NULL, rtlight->radius, numsurfaces, r_shadow_buffer_surfacelist);
2509 // now we're done compiling the rtlight
2510 r_shadow_compilingrtlight = NULL;
2514 // use smallest available cullradius - box radius or light radius
2515 //rtlight->cullradius = RadiusFromBoundsAndOrigin(rtlight->cullmins, rtlight->cullmaxs, rtlight->shadoworigin);
2516 //rtlight->cullradius = min(rtlight->cullradius, rtlight->radius);
2520 if (rtlight->static_meshchain_shadow)
2523 for (mesh = rtlight->static_meshchain_shadow;mesh;mesh = mesh->next)
2526 shadowmeshtris += mesh->numtriangles;
2531 if (rtlight->static_numlighttrispvsbytes)
2532 for (i = 0;i < rtlight->static_numlighttrispvsbytes*8;i++)
2533 if (CHECKPVSBIT(rtlight->static_lighttrispvs, i))
2537 if (rtlight->static_numlighttrispvsbytes)
2538 for (i = 0;i < rtlight->static_numshadowtrispvsbytes*8;i++)
2539 if (CHECKPVSBIT(rtlight->static_shadowtrispvs, i))
2542 if (developer.integer >= 10)
2543 Con_Printf("static light built: %f %f %f : %f %f %f box, %i light triangles, %i shadow triangles, %i compiled shadow volume triangles (in %i meshes)\n", rtlight->cullmins[0], rtlight->cullmins[1], rtlight->cullmins[2], rtlight->cullmaxs[0], rtlight->cullmaxs[1], rtlight->cullmaxs[2], lighttris, shadowtris, shadowmeshtris, shadowmeshes);
2546 void R_RTLight_Uncompile(rtlight_t *rtlight)
2548 if (rtlight->compiled)
2550 if (rtlight->static_meshchain_shadow)
2551 Mod_ShadowMesh_Free(rtlight->static_meshchain_shadow);
2552 rtlight->static_meshchain_shadow = NULL;
2553 // these allocations are grouped
2554 if (rtlight->static_surfacelist)
2555 Mem_Free(rtlight->static_surfacelist);
2556 rtlight->static_numleafs = 0;
2557 rtlight->static_numleafpvsbytes = 0;
2558 rtlight->static_leaflist = NULL;
2559 rtlight->static_leafpvs = NULL;
2560 rtlight->static_numsurfaces = 0;
2561 rtlight->static_surfacelist = NULL;
2562 rtlight->static_numshadowtrispvsbytes = 0;
2563 rtlight->static_shadowtrispvs = NULL;
2564 rtlight->static_numlighttrispvsbytes = 0;
2565 rtlight->static_lighttrispvs = NULL;
2566 rtlight->compiled = false;
2570 void R_Shadow_UncompileWorldLights(void)
2574 size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
2575 for (lightindex = 0;lightindex < range;lightindex++)
2577 light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
2580 R_RTLight_Uncompile(&light->rtlight);
2584 void R_Shadow_ComputeShadowCasterCullingPlanes(rtlight_t *rtlight)
2588 // reset the count of frustum planes
2589 // see rsurface.rtlight_frustumplanes definition for how much this array
2591 rsurface.rtlight_numfrustumplanes = 0;
2593 // haven't implemented a culling path for ortho rendering
2594 if (!r_refdef.view.useperspective)
2596 // check if the light is on screen and copy the 4 planes if it is
2597 for (i = 0;i < 4;i++)
2598 if (PlaneDiff(rtlight->shadoworigin, &r_refdef.view.frustum[i]) < -0.03125)
2601 for (i = 0;i < 4;i++)
2602 rsurface.rtlight_frustumplanes[rsurface.rtlight_numfrustumplanes++] = r_refdef.view.frustum[i];
2607 // generate a deformed frustum that includes the light origin, this is
2608 // used to cull shadow casting surfaces that can not possibly cast a
2609 // shadow onto the visible light-receiving surfaces, which can be a
2612 // if the light origin is onscreen the result will be 4 planes exactly
2613 // if the light origin is offscreen on only one axis the result will
2614 // be exactly 5 planes (split-side case)
2615 // if the light origin is offscreen on two axes the result will be
2616 // exactly 4 planes (stretched corner case)
2617 for (i = 0;i < 4;i++)
2619 // quickly reject standard frustum planes that put the light
2620 // origin outside the frustum
2621 if (PlaneDiff(rtlight->shadoworigin, &r_refdef.view.frustum[i]) < -0.03125)
2624 rsurface.rtlight_frustumplanes[rsurface.rtlight_numfrustumplanes++] = r_refdef.view.frustum[i];
2626 // if all the standard frustum planes were accepted, the light is onscreen
2627 // otherwise we need to generate some more planes below...
2628 if (rsurface.rtlight_numfrustumplanes < 4)
2630 // at least one of the stock frustum planes failed, so we need to
2631 // create one or two custom planes to enclose the light origin
2632 for (i = 0;i < 4;i++)
2634 // create a plane using the view origin and light origin, and a
2635 // single point from the frustum corner set
2636 TriangleNormal(r_refdef.view.origin, r_refdef.view.frustumcorner[i], rtlight->shadoworigin, plane.normal);
2637 VectorNormalize(plane.normal);
2638 plane.dist = DotProduct(r_refdef.view.origin, plane.normal);
2639 // see if this plane is backwards and flip it if so
2640 for (j = 0;j < 4;j++)
2641 if (j != i && DotProduct(r_refdef.view.frustumcorner[j], plane.normal) - plane.dist < -0.03125)
2645 VectorNegate(plane.normal, plane.normal);
2647 // flipped plane, test again to see if it is now valid
2648 for (j = 0;j < 4;j++)
2649 if (j != i && DotProduct(r_refdef.view.frustumcorner[j], plane.normal) - plane.dist < -0.03125)
2651 // if the plane is still not valid, then it is dividing the
2652 // frustum and has to be rejected
2656 // we have created a valid plane, compute extra info
2657 PlaneClassify(&plane);
2659 rsurface.rtlight_frustumplanes[rsurface.rtlight_numfrustumplanes++] = plane;
2661 // if we've found 5 frustum planes then we have constructed a
2662 // proper split-side case and do not need to keep searching for
2663 // planes to enclose the light origin
2664 if (rsurface.rtlight_numfrustumplanes == 5)
2672 for (i = 0;i < rsurface.rtlight_numfrustumplanes;i++)
2674 plane = rsurface.rtlight_frustumplanes[i];
2675 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));
2680 // now add the light-space box planes if the light box is rotated, as any
2681 // caster outside the oriented light box is irrelevant (even if it passed
2682 // the worldspace light box, which is axial)
2683 if (rtlight->matrix_lighttoworld.m[0][0] != 1 || rtlight->matrix_lighttoworld.m[1][1] != 1 || rtlight->matrix_lighttoworld.m[2][2] != 1)
2685 for (i = 0;i < 6;i++)
2689 v[i >> 1] = (i & 1) ? -1 : 1;
2690 Matrix4x4_Transform(&rtlight->matrix_lighttoworld, v, plane.normal);
2691 VectorSubtract(plane.normal, rtlight->shadoworigin, plane.normal);
2692 plane.dist = VectorNormalizeLength(plane.normal);
2693 plane.dist += DotProduct(plane.normal, rtlight->shadoworigin);
2694 rsurface.rtlight_frustumplanes[rsurface.rtlight_numfrustumplanes++] = plane;
2700 // add the world-space reduced box planes
2701 for (i = 0;i < 6;i++)
2703 VectorClear(plane.normal);
2704 plane.normal[i >> 1] = (i & 1) ? -1 : 1;
2705 plane.dist = (i & 1) ? -rsurface.rtlight_cullmaxs[i >> 1] : rsurface.rtlight_cullmins[i >> 1];
2706 rsurface.rtlight_frustumplanes[rsurface.rtlight_numfrustumplanes++] = plane;
2715 // reduce all plane distances to tightly fit the rtlight cull box, which
2717 VectorSet(points[0], rsurface.rtlight_cullmins[0], rsurface.rtlight_cullmins[1], rsurface.rtlight_cullmins[2]);
2718 VectorSet(points[1], rsurface.rtlight_cullmaxs[0], rsurface.rtlight_cullmins[1], rsurface.rtlight_cullmins[2]);
2719 VectorSet(points[2], rsurface.rtlight_cullmins[0], rsurface.rtlight_cullmaxs[1], rsurface.rtlight_cullmins[2]);
2720 VectorSet(points[3], rsurface.rtlight_cullmaxs[0], rsurface.rtlight_cullmaxs[1], rsurface.rtlight_cullmins[2]);
2721 VectorSet(points[4], rsurface.rtlight_cullmins[0], rsurface.rtlight_cullmins[1], rsurface.rtlight_cullmaxs[2]);
2722 VectorSet(points[5], rsurface.rtlight_cullmaxs[0], rsurface.rtlight_cullmins[1], rsurface.rtlight_cullmaxs[2]);
2723 VectorSet(points[6], rsurface.rtlight_cullmins[0], rsurface.rtlight_cullmaxs[1], rsurface.rtlight_cullmaxs[2]);
2724 VectorSet(points[7], rsurface.rtlight_cullmaxs[0], rsurface.rtlight_cullmaxs[1], rsurface.rtlight_cullmaxs[2]);
2725 oldnum = rsurface.rtlight_numfrustumplanes;
2726 rsurface.rtlight_numfrustumplanes = 0;
2727 for (j = 0;j < oldnum;j++)
2729 // find the nearest point on the box to this plane
2730 bestdist = DotProduct(rsurface.rtlight_frustumplanes[j].normal, points[0]);
2731 for (i = 1;i < 8;i++)
2733 dist = DotProduct(rsurface.rtlight_frustumplanes[j].normal, points[i]);
2734 if (bestdist > dist)
2737 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);
2738 // if the nearest point is near or behind the plane, we want this
2739 // plane, otherwise the plane is useless as it won't cull anything
2740 if (rsurface.rtlight_frustumplanes[j].dist < bestdist + 0.03125)
2742 PlaneClassify(&rsurface.rtlight_frustumplanes[j]);
2743 rsurface.rtlight_frustumplanes[rsurface.rtlight_numfrustumplanes++] = rsurface.rtlight_frustumplanes[j];
2750 void R_Shadow_DrawWorldShadow(int numsurfaces, int *surfacelist, const unsigned char *trispvs)
2752 RSurf_ActiveWorldEntity();
2753 if (rsurface.rtlight->compiled && r_shadow_realtime_world_compile.integer && r_shadow_realtime_world_compileshadow.integer)
2757 for (mesh = rsurface.rtlight->static_meshchain_shadow;mesh;mesh = mesh->next)
2759 r_refdef.stats.lights_shadowtriangles += mesh->numtriangles;
2760 R_Mesh_VertexPointer(mesh->vertex3f, mesh->vbo, mesh->vbooffset_vertex3f);
2761 GL_LockArrays(0, mesh->numverts);
2762 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_STENCIL)
2764 // decrement stencil if backface is behind depthbuffer
2765 GL_CullFace(r_refdef.view.cullface_front);
2766 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
2767 R_Mesh_Draw(0, mesh->numverts, 0, mesh->numtriangles, mesh->element3i, mesh->element3s, mesh->ebo3i, mesh->ebo3s);
2768 // increment stencil if frontface is behind depthbuffer
2769 GL_CullFace(r_refdef.view.cullface_back);
2770 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR
2772 R_Mesh_Draw(0, mesh->numverts, 0, mesh->numtriangles, mesh->element3i, mesh->element3s, mesh->ebo3i, mesh->ebo3s);
2773 GL_LockArrays(0, 0);
2777 else if (numsurfaces && r_refdef.scene.worldmodel->brush.shadowmesh && r_shadow_culltriangles.integer)
2780 int surfacelistindex;
2781 msurface_t *surface;
2782 R_Shadow_PrepareShadowMark(r_refdef.scene.worldmodel->brush.shadowmesh->numtriangles);
2783 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2785 surface = r_refdef.scene.worldmodel->data_surfaces + surfacelist[surfacelistindex];
2786 for (t = surface->num_firstshadowmeshtriangle, tend = t + surface->num_triangles;t < tend;t++)
2787 if (CHECKPVSBIT(trispvs, t))
2788 shadowmarklist[numshadowmark++] = t;
2790 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);
2792 else if (numsurfaces)
2793 r_refdef.scene.worldmodel->DrawShadowVolume(r_refdef.scene.worldentity, rsurface.rtlight->shadoworigin, NULL, rsurface.rtlight->radius, numsurfaces, surfacelist, rsurface.rtlight_cullmins, rsurface.rtlight_cullmaxs);
2796 void R_Shadow_DrawEntityShadow(entity_render_t *ent)
2798 vec3_t relativeshadoworigin, relativeshadowmins, relativeshadowmaxs;
2799 vec_t relativeshadowradius;
2800 RSurf_ActiveModelEntity(ent, false, false);
2801 Matrix4x4_Transform(&ent->inversematrix, rsurface.rtlight->shadoworigin, relativeshadoworigin);
2802 relativeshadowradius = rsurface.rtlight->radius / ent->scale;
2803 relativeshadowmins[0] = relativeshadoworigin[0] - relativeshadowradius;
2804 relativeshadowmins[1] = relativeshadoworigin[1] - relativeshadowradius;
2805 relativeshadowmins[2] = relativeshadoworigin[2] - relativeshadowradius;
2806 relativeshadowmaxs[0] = relativeshadoworigin[0] + relativeshadowradius;
2807 relativeshadowmaxs[1] = relativeshadoworigin[1] + relativeshadowradius;
2808 relativeshadowmaxs[2] = relativeshadoworigin[2] + relativeshadowradius;
2809 ent->model->DrawShadowVolume(ent, relativeshadoworigin, NULL, relativeshadowradius, ent->model->nummodelsurfaces, ent->model->surfacelist, relativeshadowmins, relativeshadowmaxs);
2812 void R_Shadow_SetupEntityLight(const entity_render_t *ent)
2814 // set up properties for rendering light onto this entity
2815 RSurf_ActiveModelEntity(ent, true, true);
2816 GL_AlphaTest(false);
2817 Matrix4x4_Concat(&rsurface.entitytolight, &rsurface.rtlight->matrix_worldtolight, &ent->matrix);
2818 Matrix4x4_Concat(&rsurface.entitytoattenuationxyz, &matrix_attenuationxyz, &rsurface.entitytolight);
2819 Matrix4x4_Concat(&rsurface.entitytoattenuationz, &matrix_attenuationz, &rsurface.entitytolight);
2820 Matrix4x4_Transform(&ent->inversematrix, rsurface.rtlight->shadoworigin, rsurface.entitylightorigin);
2821 if (r_shadow_lightingrendermode == R_SHADOW_RENDERMODE_LIGHT_GLSL)
2822 R_Mesh_TexMatrix(3, &rsurface.entitytolight);
2825 void R_Shadow_DrawWorldLight(int numsurfaces, int *surfacelist, const unsigned char *trispvs)
2827 if (!r_refdef.scene.worldmodel->DrawLight)
2830 // set up properties for rendering light onto this entity
2831 RSurf_ActiveWorldEntity();
2832 GL_AlphaTest(false);
2833 rsurface.entitytolight = rsurface.rtlight->matrix_worldtolight;
2834 Matrix4x4_Concat(&rsurface.entitytoattenuationxyz, &matrix_attenuationxyz, &rsurface.entitytolight);
2835 Matrix4x4_Concat(&rsurface.entitytoattenuationz, &matrix_attenuationz, &rsurface.entitytolight);
2836 VectorCopy(rsurface.rtlight->shadoworigin, rsurface.entitylightorigin);
2837 if (r_shadow_lightingrendermode == R_SHADOW_RENDERMODE_LIGHT_GLSL)
2838 R_Mesh_TexMatrix(3, &rsurface.entitytolight);
2840 r_refdef.scene.worldmodel->DrawLight(r_refdef.scene.worldentity, numsurfaces, surfacelist, trispvs);
2843 void R_Shadow_DrawEntityLight(entity_render_t *ent)
2845 dp_model_t *model = ent->model;
2846 if (!model->DrawLight)
2849 R_Shadow_SetupEntityLight(ent);
2851 model->DrawLight(ent, model->nummodelsurfaces, model->surfacelist, NULL);
2854 void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
2858 int numleafs, numsurfaces;
2859 int *leaflist, *surfacelist;
2860 unsigned char *leafpvs, *shadowtrispvs, *lighttrispvs;
2861 int numlightentities;
2862 int numlightentities_noselfshadow;
2863 int numshadowentities;
2864 int numshadowentities_noselfshadow;
2865 static entity_render_t *lightentities[MAX_EDICTS];
2866 static entity_render_t *lightentities_noselfshadow[MAX_EDICTS];
2867 static entity_render_t *shadowentities[MAX_EDICTS];
2868 static entity_render_t *shadowentities_noselfshadow[MAX_EDICTS];
2870 // skip lights that don't light because of ambientscale+diffusescale+specularscale being 0 (corona only lights)
2871 // skip lights that are basically invisible (color 0 0 0)
2872 if (VectorLength2(rtlight->color) * (rtlight->ambientscale + rtlight->diffusescale + rtlight->specularscale) < (1.0f / 1048576.0f))
2875 // loading is done before visibility checks because loading should happen
2876 // all at once at the start of a level, not when it stalls gameplay.
2877 // (especially important to benchmarks)
2879 if (rtlight->isstatic && !rtlight->compiled && r_shadow_realtime_world_compile.integer)
2880 R_RTLight_Compile(rtlight);
2882 rtlight->currentcubemap = rtlight->cubemapname[0] ? R_Shadow_Cubemap(rtlight->cubemapname) : r_texture_whitecube;
2884 // look up the light style value at this time
2885 f = (rtlight->style >= 0 ? r_refdef.scene.rtlightstylevalue[rtlight->style] : 1) * r_shadow_lightintensityscale.value;
2886 VectorScale(rtlight->color, f, rtlight->currentcolor);
2888 if (rtlight->selected)
2890 f = 2 + sin(realtime * M_PI * 4.0);
2891 VectorScale(rtlight->currentcolor, f, rtlight->currentcolor);
2895 // if lightstyle is currently off, don't draw the light
2896 if (VectorLength2(rtlight->currentcolor) < (1.0f / 1048576.0f))
2899 // if the light box is offscreen, skip it
2900 if (R_CullBox(rtlight->cullmins, rtlight->cullmaxs))
2903 VectorCopy(rtlight->cullmins, rsurface.rtlight_cullmins);
2904 VectorCopy(rtlight->cullmaxs, rsurface.rtlight_cullmaxs);
2906 if (rtlight->compiled && r_shadow_realtime_world_compile.integer)
2908 // compiled light, world available and can receive realtime lighting
2909 // retrieve leaf information
2910 numleafs = rtlight->static_numleafs;
2911 leaflist = rtlight->static_leaflist;
2912 leafpvs = rtlight->static_leafpvs;
2913 numsurfaces = rtlight->static_numsurfaces;
2914 surfacelist = rtlight->static_surfacelist;
2915 shadowtrispvs = rtlight->static_shadowtrispvs;
2916 lighttrispvs = rtlight->static_lighttrispvs;
2918 else if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->GetLightInfo)
2920 // dynamic light, world available and can receive realtime lighting
2921 // calculate lit surfaces and leafs
2922 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);
2923 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);
2924 leaflist = r_shadow_buffer_leaflist;
2925 leafpvs = r_shadow_buffer_leafpvs;
2926 surfacelist = r_shadow_buffer_surfacelist;
2927 shadowtrispvs = r_shadow_buffer_shadowtrispvs;
2928 lighttrispvs = r_shadow_buffer_lighttrispvs;
2929 // if the reduced leaf bounds are offscreen, skip it
2930 if (R_CullBox(rsurface.rtlight_cullmins, rsurface.rtlight_cullmaxs))
2941 shadowtrispvs = NULL;
2942 lighttrispvs = NULL;
2944 // check if light is illuminating any visible leafs
2947 for (i = 0;i < numleafs;i++)
2948 if (r_refdef.viewcache.world_leafvisible[leaflist[i]])
2953 // set up a scissor rectangle for this light
2954 if (R_Shadow_ScissorForBBox(rsurface.rtlight_cullmins, rsurface.rtlight_cullmaxs))
2957 R_Shadow_ComputeShadowCasterCullingPlanes(rtlight);
2959 // make a list of lit entities and shadow casting entities
2960 numlightentities = 0;
2961 numlightentities_noselfshadow = 0;
2962 numshadowentities = 0;
2963 numshadowentities_noselfshadow = 0;
2964 // add dynamic entities that are lit by the light
2965 if (r_drawentities.integer)
2967 for (i = 0;i < r_refdef.scene.numentities;i++)
2970 entity_render_t *ent = r_refdef.scene.entities[i];
2972 if (!BoxesOverlap(ent->mins, ent->maxs, rsurface.rtlight_cullmins, rsurface.rtlight_cullmaxs))
2974 // skip the object entirely if it is not within the valid
2975 // shadow-casting region (which includes the lit region)
2976 if (R_CullBoxCustomPlanes(ent->mins, ent->maxs, rsurface.rtlight_numfrustumplanes, rsurface.rtlight_frustumplanes))
2978 if (!(model = ent->model))
2980 if (r_refdef.viewcache.entityvisible[i] && model->DrawLight && (ent->flags & RENDER_LIGHT))
2982 // this entity wants to receive light, is visible, and is
2983 // inside the light box
2984 // TODO: check if the surfaces in the model can receive light
2985 // so now check if it's in a leaf seen by the light
2986 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))
2988 if (ent->flags & RENDER_NOSELFSHADOW)
2989 lightentities_noselfshadow[numlightentities_noselfshadow++] = ent;
2991 lightentities[numlightentities++] = ent;
2992 // since it is lit, it probably also casts a shadow...
2993 // about the VectorDistance2 - light emitting entities should not cast their own shadow
2994 Matrix4x4_OriginFromMatrix(&ent->matrix, org);
2995 if ((ent->flags & RENDER_SHADOW) && model->DrawShadowVolume && VectorDistance2(org, rtlight->shadoworigin) > 0.1)
2997 // note: exterior models without the RENDER_NOSELFSHADOW
2998 // flag still create a RENDER_NOSELFSHADOW shadow but
2999 // are lit normally, this means that they are
3000 // self-shadowing but do not shadow other
3001 // RENDER_NOSELFSHADOW entities such as the gun
3002 // (very weird, but keeps the player shadow off the gun)
3003 if (ent->flags & (RENDER_NOSELFSHADOW | RENDER_EXTERIORMODEL))
3004 shadowentities_noselfshadow[numshadowentities_noselfshadow++] = ent;
3006 shadowentities[numshadowentities++] = ent;
3009 else if (ent->flags & RENDER_SHADOW)
3011 // this entity is not receiving light, but may still need to
3013 // TODO: check if the surfaces in the model can cast shadow
3014 // now check if it is in a leaf seen by the light
3015 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))
3017 // about the VectorDistance2 - light emitting entities should not cast their own shadow
3018 Matrix4x4_OriginFromMatrix(&ent->matrix, org);
3019 if ((ent->flags & RENDER_SHADOW) && model->DrawShadowVolume && VectorDistance2(org, rtlight->shadoworigin) > 0.1)
3021 if (ent->flags & (RENDER_NOSELFSHADOW | RENDER_EXTERIORMODEL))
3022 shadowentities_noselfshadow[numshadowentities_noselfshadow++] = ent;
3024 shadowentities[numshadowentities++] = ent;
3030 // return if there's nothing at all to light
3031 if (!numlightentities && !numsurfaces)
3034 // don't let sound skip if going slow
3035 if (r_refdef.scene.extraupdate)
3038 // make this the active rtlight for rendering purposes
3039 R_Shadow_RenderMode_ActiveLight(rtlight);
3040 // count this light in the r_speeds
3041 r_refdef.stats.lights++;
3043 if (r_showshadowvolumes.integer && r_refdef.view.showdebug && numsurfaces + numshadowentities + numshadowentities_noselfshadow && rtlight->shadow && (rtlight->isstatic ? r_refdef.scene.rtworldshadows : r_refdef.scene.rtdlightshadows))
3045 // optionally draw visible shape of the shadow volumes
3046 // for performance analysis by level designers
3047 R_Shadow_RenderMode_VisibleShadowVolumes();
3049 R_Shadow_DrawWorldShadow(numsurfaces, surfacelist, shadowtrispvs);
3050 for (i = 0;i < numshadowentities;i++)
3051 R_Shadow_DrawEntityShadow(shadowentities[i]);
3052 for (i = 0;i < numshadowentities_noselfshadow;i++)
3053 R_Shadow_DrawEntityShadow(shadowentities_noselfshadow[i]);
3056 if (gl_stencil && numsurfaces + numshadowentities + numshadowentities_noselfshadow && rtlight->shadow && (rtlight->isstatic ? r_refdef.scene.rtworldshadows : r_refdef.scene.rtdlightshadows))
3058 // draw stencil shadow volumes to mask off pixels that are in shadow
3059 // so that they won't receive lighting
3060 R_Shadow_RenderMode_StencilShadowVolumes(true);
3062 R_Shadow_DrawWorldShadow(numsurfaces, surfacelist, shadowtrispvs);
3063 for (i = 0;i < numshadowentities;i++)
3064 R_Shadow_DrawEntityShadow(shadowentities[i]);
3065 if (numlightentities_noselfshadow)
3067 // draw lighting in the unmasked areas
3068 R_Shadow_RenderMode_Lighting(true, false);
3069 for (i = 0;i < numlightentities_noselfshadow;i++)
3070 R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]);
3072 // optionally draw the illuminated areas
3073 // for performance analysis by level designers
3074 if (r_showlighting.integer && r_refdef.view.showdebug)
3076 R_Shadow_RenderMode_VisibleLighting(!r_showdisabledepthtest.integer, false);
3077 for (i = 0;i < numlightentities_noselfshadow;i++)
3078 R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]);
3081 R_Shadow_RenderMode_StencilShadowVolumes(false);
3083 for (i = 0;i < numshadowentities_noselfshadow;i++)
3084 R_Shadow_DrawEntityShadow(shadowentities_noselfshadow[i]);
3086 if (numsurfaces + numlightentities)
3088 // draw lighting in the unmasked areas
3089 R_Shadow_RenderMode_Lighting(true, false);
3091 R_Shadow_DrawWorldLight(numsurfaces, surfacelist, lighttrispvs);
3092 for (i = 0;i < numlightentities;i++)
3093 R_Shadow_DrawEntityLight(lightentities[i]);
3095 // optionally draw the illuminated areas
3096 // for performance analysis by level designers
3097 if (r_showlighting.integer && r_refdef.view.showdebug)
3099 R_Shadow_RenderMode_VisibleLighting(!r_showdisabledepthtest.integer, false);
3101 R_Shadow_DrawWorldLight(numsurfaces, surfacelist, lighttrispvs);
3102 for (i = 0;i < numlightentities;i++)
3103 R_Shadow_DrawEntityLight(lightentities[i]);
3109 if (numsurfaces + numlightentities)
3111 // draw lighting in the unmasked areas
3112 R_Shadow_RenderMode_Lighting(false, false);
3114 R_Shadow_DrawWorldLight(numsurfaces, surfacelist, lighttrispvs);
3115 for (i = 0;i < numlightentities;i++)
3116 R_Shadow_DrawEntityLight(lightentities[i]);
3117 for (i = 0;i < numlightentities_noselfshadow;i++)
3118 R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]);
3120 // optionally draw the illuminated areas
3121 // for performance analysis by level designers
3122 if (r_showlighting.integer && r_refdef.view.showdebug)
3124 R_Shadow_RenderMode_VisibleLighting(false, false);
3126 R_Shadow_DrawWorldLight(numsurfaces, surfacelist, lighttrispvs);
3127 for (i = 0;i < numlightentities;i++)
3128 R_Shadow_DrawEntityLight(lightentities[i]);
3129 for (i = 0;i < numlightentities_noselfshadow;i++)
3130 R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]);
3136 void R_Shadow_DrawLightSprites(void);
3137 void R_ShadowVolumeLighting(qboolean visible)
3145 if (r_editlights.integer)
3146 R_Shadow_DrawLightSprites();
3148 R_Shadow_RenderMode_Begin();
3150 flag = r_refdef.scene.rtworld ? LIGHTFLAG_REALTIMEMODE : LIGHTFLAG_NORMALMODE;
3151 if (r_shadow_debuglight.integer >= 0)
3153 lightindex = r_shadow_debuglight.integer;
3154 light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
3155 if (light && (light->flags & flag))
3156 R_DrawRTLight(&light->rtlight, visible);
3160 range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
3161 for (lightindex = 0;lightindex < range;lightindex++)
3163 light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
3164 if (light && (light->flags & flag))
3165 R_DrawRTLight(&light->rtlight, visible);
3168 if (r_refdef.scene.rtdlight)
3169 for (lnum = 0;lnum < r_refdef.scene.numlights;lnum++)
3170 R_DrawRTLight(&r_refdef.scene.lights[lnum], visible);
3172 R_Shadow_RenderMode_End();
3175 extern void R_SetupView(qboolean allowwaterclippingplane);
3176 extern cvar_t r_shadows;
3177 extern cvar_t r_shadows_throwdistance;
3178 void R_DrawModelShadows(void)
3181 float relativethrowdistance;
3182 entity_render_t *ent;
3183 vec3_t relativelightorigin;
3184 vec3_t relativelightdirection;
3185 vec3_t relativeshadowmins, relativeshadowmaxs;
3189 if (!r_drawentities.integer || !gl_stencil)
3193 GL_Scissor(r_refdef.view.x, r_refdef.view.y, r_refdef.view.width, r_refdef.view.height);
3195 r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
3197 if (gl_ext_separatestencil.integer)
3198 r_shadow_shadowingrendermode = R_SHADOW_RENDERMODE_SEPARATESTENCIL;
3199 else if (gl_ext_stenciltwoside.integer)
3200 r_shadow_shadowingrendermode = R_SHADOW_RENDERMODE_STENCILTWOSIDE;
3202 r_shadow_shadowingrendermode = R_SHADOW_RENDERMODE_STENCIL;
3204 R_Shadow_RenderMode_StencilShadowVolumes(true);
3206 for (i = 0;i < r_refdef.scene.numentities;i++)
3208 ent = r_refdef.scene.entities[i];
3209 // cast shadows from anything that is not a submodel of the map
3210 if (ent->model && ent->model->DrawShadowVolume != NULL && !ent->model->brush.submodel && (ent->flags & RENDER_SHADOW))
3212 relativethrowdistance = r_shadows_throwdistance.value * Matrix4x4_ScaleFromMatrix(&ent->inversematrix);
3213 VectorSet(relativeshadowmins, -relativethrowdistance, -relativethrowdistance, -relativethrowdistance);
3214 VectorSet(relativeshadowmaxs, relativethrowdistance, relativethrowdistance, relativethrowdistance);
3216 if(r_shadows.integer == 2)
3218 // 2: simpler mode, throw shadows always DOWN
3219 VectorSet(tmp, 0, 0, -1);
3220 Matrix4x4_Transform3x3(&ent->inversematrix, tmp, relativelightdirection);
3224 if(ent->entitynumber != 0)
3226 // networked entity - might be attached in some way (then we should use the parent's light direction, to not tear apart attached entities)
3227 int entnum, entnum2, recursion;
3228 entnum = entnum2 = ent->entitynumber;
3229 for(recursion = 32; recursion > 0; --recursion)
3231 entnum2 = cl.entities[entnum].state_current.tagentity;
3232 if(entnum2 >= 1 && entnum2 < cl.num_entities && cl.entities_active[entnum2])
3237 if(recursion && recursion != 32) // if we followed a valid non-empty attachment chain
3239 VectorNegate(cl.entities[entnum].render.modellight_lightdir, relativelightdirection);
3240 // transform into modelspace of OUR entity
3241 Matrix4x4_Transform3x3(&cl.entities[entnum].render.matrix, relativelightdirection, tmp);
3242 Matrix4x4_Transform3x3(&ent->inversematrix, tmp, relativelightdirection);
3245 VectorNegate(ent->modellight_lightdir, relativelightdirection);
3248 VectorNegate(ent->modellight_lightdir, relativelightdirection);
3251 VectorScale(relativelightdirection, -relativethrowdistance, relativelightorigin);
3252 RSurf_ActiveModelEntity(ent, false, false);
3253 ent->model->DrawShadowVolume(ent, relativelightorigin, relativelightdirection, relativethrowdistance, ent->model->nummodelsurfaces, ent->model->surfacelist, relativeshadowmins, relativeshadowmaxs);
3257 // not really the right mode, but this will disable any silly stencil features
3258 R_Shadow_RenderMode_VisibleLighting(true, true);
3260 // vertex coordinates for a quad that covers the screen exactly
3261 vertex3f[0] = 0;vertex3f[1] = 0;vertex3f[2] = 0;
3262 vertex3f[3] = 1;vertex3f[4] = 0;vertex3f[5] = 0;
3263 vertex3f[6] = 1;vertex3f[7] = 1;vertex3f[8] = 0;
3264 vertex3f[9] = 0;vertex3f[10] = 1;vertex3f[11] = 0;
3266 // set up ortho view for rendering this pass
3267 GL_SetupView_Mode_Ortho(0, 0, 1, 1, -10, 100);
3268 GL_Scissor(r_refdef.view.x, r_refdef.view.y, r_refdef.view.width, r_refdef.view.height);
3269 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
3270 GL_ScissorTest(true);
3271 R_Mesh_Matrix(&identitymatrix);
3272 R_Mesh_ResetTextureState();
3273 R_Mesh_VertexPointer(vertex3f, 0, 0);
3274 R_Mesh_ColorPointer(NULL, 0, 0);
3276 // set up a 50% darkening blend on shadowed areas
3277 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3278 GL_DepthRange(0, 1);
3279 GL_DepthTest(false);
3280 GL_DepthMask(false);
3281 GL_PolygonOffset(0, 0);CHECKGLERROR
3282 GL_Color(0, 0, 0, 0.5);
3283 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
3284 qglDepthFunc(GL_ALWAYS);CHECKGLERROR
3285 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
3286 qglStencilMask(~0);CHECKGLERROR
3287 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);CHECKGLERROR
3288 qglStencilFunc(GL_NOTEQUAL, 128, ~0);CHECKGLERROR
3290 // apply the blend to the shadowed areas
3291 R_Mesh_Draw(0, 4, 0, 2, NULL, polygonelements, 0, 0);
3293 // restoring the perspective view is done by R_RenderScene
3294 //R_SetupView(true);
3296 // restore other state to normal
3297 R_Shadow_RenderMode_End();
3300 void R_DrawCoronas(void)
3303 float cscale, scale;
3308 if (r_coronas.value < (1.0f / 256.0f) && !gl_flashblend.integer)
3310 R_Mesh_Matrix(&identitymatrix);
3311 flag = r_refdef.scene.rtworld ? LIGHTFLAG_REALTIMEMODE : LIGHTFLAG_NORMALMODE;
3312 // FIXME: these traces should scan all render entities instead of cl.world
3313 range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
3314 for (lightindex = 0;lightindex < range;lightindex++)
3316 light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
3319 rtlight = &light->rtlight;
3320 if (!(rtlight->flags & flag))
3322 if (rtlight->corona * r_coronas.value <= 0)
3324 if (r_shadow_debuglight.integer >= 0 && r_shadow_debuglight.integer != (int)lightindex)
3326 cscale = rtlight->corona * r_coronas.value* 0.25f;
3327 scale = rtlight->radius * rtlight->coronasizescale;
3328 if (VectorDistance2(rtlight->shadoworigin, r_refdef.view.origin) < 16.0f * 16.0f)
3330 if (CL_Move(r_refdef.view.origin, vec3_origin, vec3_origin, rtlight->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false).fraction < 1)
3332 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, rtlight->color[0] * cscale, rtlight->color[1] * cscale, rtlight->color[2] * cscale, 1);
3334 for (i = 0;i < r_refdef.scene.numlights;i++)
3336 rtlight = &r_refdef.scene.lights[i];
3337 if (!(rtlight->flags & flag))
3339 if (rtlight->corona <= 0)
3341 if (VectorDistance2(rtlight->shadoworigin, r_refdef.view.origin) < 32.0f * 32.0f)
3343 if (gl_flashblend.integer)
3345 cscale = rtlight->corona * 1.0f;
3346 scale = rtlight->radius * rtlight->coronasizescale * 2.0f;
3350 cscale = rtlight->corona * r_coronas.value* 0.25f;
3351 scale = rtlight->radius * rtlight->coronasizescale;
3353 if (VectorLength(rtlight->color) * cscale < (1.0f / 256.0f))
3355 if (CL_Move(r_refdef.view.origin, vec3_origin, vec3_origin, rtlight->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false).fraction < 1)
3357 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, rtlight->color[0] * cscale, rtlight->color[1] * cscale, rtlight->color[2] * cscale, 1);
3363 //static char *suffix[6] = {"ft", "bk", "rt", "lf", "up", "dn"};
3364 typedef struct suffixinfo_s
3367 qboolean flipx, flipy, flipdiagonal;
3370 static suffixinfo_t suffix[3][6] =
3373 {"px", false, false, false},
3374 {"nx", false, false, false},
3375 {"py", false, false, false},
3376 {"ny", false, false, false},
3377 {"pz", false, false, false},
3378 {"nz", false, false, false}
3381 {"posx", false, false, false},
3382 {"negx", false, false, false},
3383 {"posy", false, false, false},
3384 {"negy", false, false, false},
3385 {"posz", false, false, false},
3386 {"negz", false, false, false}
3389 {"rt", true, false, true},
3390 {"lf", false, true, true},
3391 {"ft", true, true, false},
3392 {"bk", false, false, false},
3393 {"up", true, false, true},
3394 {"dn", true, false, true}
3398 static int componentorder[4] = {0, 1, 2, 3};
3400 rtexture_t *R_Shadow_LoadCubemap(const char *basename)
3402 int i, j, cubemapsize;
3403 unsigned char *cubemappixels, *image_buffer;
3404 rtexture_t *cubemaptexture;
3406 // must start 0 so the first loadimagepixels has no requested width/height
3408 cubemappixels = NULL;
3409 cubemaptexture = NULL;
3410 // keep trying different suffix groups (posx, px, rt) until one loads
3411 for (j = 0;j < 3 && !cubemappixels;j++)
3413 // load the 6 images in the suffix group
3414 for (i = 0;i < 6;i++)
3416 // generate an image name based on the base and and suffix
3417 dpsnprintf(name, sizeof(name), "%s%s", basename, suffix[j][i].suffix);
3419 if ((image_buffer = loadimagepixelsbgra(name, false, false)))
3421 // an image loaded, make sure width and height are equal
3422 if (image_width == image_height && (!cubemappixels || image_width == cubemapsize))
3424 // if this is the first image to load successfully, allocate the cubemap memory
3425 if (!cubemappixels && image_width >= 1)
3427 cubemapsize = image_width;
3428 // note this clears to black, so unavailable sides are black
3429 cubemappixels = (unsigned char *)Mem_Alloc(tempmempool, 6*cubemapsize*cubemapsize*4);
3431 // copy the image with any flipping needed by the suffix (px and posx types don't need flipping)
3433 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);
3436 Con_Printf("Cubemap image \"%s\" (%ix%i) is not square, OpenGL requires square cubemaps.\n", name, image_width, image_height);
3438 Mem_Free(image_buffer);
3442 // if a cubemap loaded, upload it
3445 if (developer_loading.integer)
3446 Con_Printf("loading cubemap \"%s\"\n", basename);
3448 if (!r_shadow_filters_texturepool)
3449 r_shadow_filters_texturepool = R_AllocTexturePool();
3450 cubemaptexture = R_LoadTextureCubeMap(r_shadow_filters_texturepool, basename, cubemapsize, cubemappixels, TEXTYPE_BGRA, TEXF_PRECACHE | (gl_texturecompression_lightcubemaps.integer ? TEXF_COMPRESS : 0), NULL);
3451 Mem_Free(cubemappixels);
3455 Con_DPrintf("failed to load cubemap \"%s\"\n", basename);
3456 if (developer_loading.integer)
3458 Con_Printf("(tried tried images ");
3459 for (j = 0;j < 3;j++)
3460 for (i = 0;i < 6;i++)
3461 Con_Printf("%s\"%s%s.tga\"", j + i > 0 ? ", " : "", basename, suffix[j][i].suffix);
3462 Con_Print(" and was unable to find any of them).\n");
3465 return cubemaptexture;
3468 rtexture_t *R_Shadow_Cubemap(const char *basename)
3471 for (i = 0;i < numcubemaps;i++)
3472 if (!strcasecmp(cubemaps[i].basename, basename))
3473 return cubemaps[i].texture ? cubemaps[i].texture : r_texture_whitecube;
3474 if (i >= MAX_CUBEMAPS)
3475 return r_texture_whitecube;
3477 strlcpy(cubemaps[i].basename, basename, sizeof(cubemaps[i].basename));
3478 cubemaps[i].texture = R_Shadow_LoadCubemap(cubemaps[i].basename);
3479 return cubemaps[i].texture;
3482 void R_Shadow_FreeCubemaps(void)
3485 for (i = 0;i < numcubemaps;i++)
3487 if (developer_loading.integer)
3488 Con_Printf("unloading cubemap \"%s\"\n", cubemaps[i].basename);
3489 if (cubemaps[i].texture)
3490 R_FreeTexture(cubemaps[i].texture);
3494 R_FreeTexturePool(&r_shadow_filters_texturepool);
3497 dlight_t *R_Shadow_NewWorldLight(void)
3499 return (dlight_t *)Mem_ExpandableArray_AllocRecord(&r_shadow_worldlightsarray);
3502 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)
3505 // validate parameters
3506 if (style < 0 || style >= MAX_LIGHTSTYLES)
3508 Con_Printf("R_Shadow_NewWorldLight: invalid light style number %i, must be >= 0 and < %i\n", light->style, MAX_LIGHTSTYLES);
3514 // copy to light properties
3515 VectorCopy(origin, light->origin);
3516 light->angles[0] = angles[0] - 360 * floor(angles[0] / 360);
3517 light->angles[1] = angles[1] - 360 * floor(angles[1] / 360);
3518 light->angles[2] = angles[2] - 360 * floor(angles[2] / 360);
3519 light->color[0] = max(color[0], 0);
3520 light->color[1] = max(color[1], 0);
3521 light->color[2] = max(color[2], 0);
3522 light->radius = max(radius, 0);
3523 light->style = style;
3524 light->shadow = shadowenable;
3525 light->corona = corona;
3526 strlcpy(light->cubemapname, cubemapname, sizeof(light->cubemapname));
3527 light->coronasizescale = coronasizescale;
3528 light->ambientscale = ambientscale;
3529 light->diffusescale = diffusescale;
3530 light->specularscale = specularscale;
3531 light->flags = flags;
3533 // update renderable light data
3534 Matrix4x4_CreateFromQuakeEntity(&matrix, light->origin[0], light->origin[1], light->origin[2], light->angles[0], light->angles[1], light->angles[2], light->radius);
3535 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);
3538 void R_Shadow_FreeWorldLight(dlight_t *light)
3540 if (r_shadow_selectedlight == light)
3541 r_shadow_selectedlight = NULL;
3542 R_RTLight_Uncompile(&light->rtlight);
3543 Mem_ExpandableArray_FreeRecord(&r_shadow_worldlightsarray, light);
3546 void R_Shadow_ClearWorldLights(void)
3550 size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
3551 for (lightindex = 0;lightindex < range;lightindex++)
3553 light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
3555 R_Shadow_FreeWorldLight(light);
3557 r_shadow_selectedlight = NULL;
3558 R_Shadow_FreeCubemaps();
3561 void R_Shadow_SelectLight(dlight_t *light)
3563 if (r_shadow_selectedlight)
3564 r_shadow_selectedlight->selected = false;
3565 r_shadow_selectedlight = light;
3566 if (r_shadow_selectedlight)
3567 r_shadow_selectedlight->selected = true;
3570 void R_Shadow_DrawCursor_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
3572 // this is never batched (there can be only one)
3573 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);
3576 void R_Shadow_DrawLightSprite_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
3583 // this is never batched (due to the ent parameter changing every time)
3584 // so numsurfaces == 1 and surfacelist[0] == lightnumber
3585 const dlight_t *light = (dlight_t *)ent;
3588 VectorScale(light->color, intensity, spritecolor);
3589 if (VectorLength(spritecolor) < 0.1732f)
3590 VectorSet(spritecolor, 0.1f, 0.1f, 0.1f);
3591 if (VectorLength(spritecolor) > 1.0f)
3592 VectorNormalize(spritecolor);
3594 // draw light sprite
3595 if (light->cubemapname[0] && !light->shadow)
3596 pic = r_editlights_sprcubemapnoshadowlight;
3597 else if (light->cubemapname[0])
3598 pic = r_editlights_sprcubemaplight;
3599 else if (!light->shadow)
3600 pic = r_editlights_sprnoshadowlight;
3602 pic = r_editlights_sprlight;
3603 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);
3604 // draw selection sprite if light is selected
3605 if (light->selected)
3606 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);
3607 // VorteX todo: add normalmode/realtime mode light overlay sprites?
3610 void R_Shadow_DrawLightSprites(void)
3614 size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
3615 for (lightindex = 0;lightindex < range;lightindex++)
3617 light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
3619 R_MeshQueue_AddTransparent(light->origin, R_Shadow_DrawLightSprite_TransparentCallback, (entity_render_t *)light, 5, &light->rtlight);
3621 R_MeshQueue_AddTransparent(r_editlights_cursorlocation, R_Shadow_DrawCursor_TransparentCallback, NULL, 0, NULL);
3624 void R_Shadow_SelectLightInView(void)
3626 float bestrating, rating, temp[3];
3630 size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
3633 for (lightindex = 0;lightindex < range;lightindex++)
3635 light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
3638 VectorSubtract(light->origin, r_refdef.view.origin, temp);
3639 rating = (DotProduct(temp, r_refdef.view.forward) / sqrt(DotProduct(temp, temp)));
3642 rating /= (1 + 0.0625f * sqrt(DotProduct(temp, temp)));
3643 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)
3645 bestrating = rating;
3650 R_Shadow_SelectLight(best);
3653 void R_Shadow_LoadWorldLights(void)
3655 int n, a, style, shadow, flags;
3656 char tempchar, *lightsstring, *s, *t, name[MAX_QPATH], cubemapname[MAX_QPATH];
3657 float origin[3], radius, color[3], angles[3], corona, coronasizescale, ambientscale, diffusescale, specularscale;
3658 if (cl.worldmodel == NULL)
3660 Con_Print("No map loaded.\n");
3663 FS_StripExtension (cl.worldmodel->name, name, sizeof (name));
3664 strlcat (name, ".rtlights", sizeof (name));
3665 lightsstring = (char *)FS_LoadFile(name, tempmempool, false, NULL);
3675 for (;COM_Parse(t, true) && strcmp(
3676 if (COM_Parse(t, true))
3678 if (com_token[0] == '!')
3681 origin[0] = atof(com_token+1);
3684 origin[0] = atof(com_token);
3689 while (*s && *s != '\n' && *s != '\r')
3695 // check for modifier flags
3702 #if _MSC_VER >= 1400
3703 #define sscanf sscanf_s
3705 cubemapname[sizeof(cubemapname)-1] = 0;
3706 #if MAX_QPATH != 128
3707 #error update this code if MAX_QPATH changes
3709 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
3710 #if _MSC_VER >= 1400
3711 , sizeof(cubemapname)
3713 , &corona, &angles[0], &angles[1], &angles[2], &coronasizescale, &ambientscale, &diffusescale, &specularscale, &flags);
3716 flags = LIGHTFLAG_REALTIMEMODE;
3724 coronasizescale = 0.25f;
3726 VectorClear(angles);
3729 if (a < 9 || !strcmp(cubemapname, "\"\""))
3731 // remove quotes on cubemapname
3732 if (cubemapname[0] == '"' && cubemapname[strlen(cubemapname) - 1] == '"')
3735 namelen = strlen(cubemapname) - 2;
3736 memmove(cubemapname, cubemapname + 1, namelen);
3737 cubemapname[namelen] = '\0';
3741 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);
3744 R_Shadow_UpdateWorldLight(R_Shadow_NewWorldLight(), origin, angles, color, radius, corona, style, shadow, cubemapname, coronasizescale, ambientscale, diffusescale, specularscale, flags);
3752 Con_Printf("invalid rtlights file \"%s\"\n", name);
3753 Mem_Free(lightsstring);
3757 void R_Shadow_SaveWorldLights(void)
3761 size_t bufchars, bufmaxchars;
3763 char name[MAX_QPATH];
3764 char line[MAX_INPUTLINE];
3765 size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked, assuming the dpsnprintf mess doesn't screw it up...
3766 // I hate lines which are 3 times my screen size :( --blub
3769 if (cl.worldmodel == NULL)
3771 Con_Print("No map loaded.\n");
3774 FS_StripExtension (cl.worldmodel->name, name, sizeof (name));
3775 strlcat (name, ".rtlights", sizeof (name));
3776 bufchars = bufmaxchars = 0;
3778 for (lightindex = 0;lightindex < range;lightindex++)
3780 light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
3783 if (light->coronasizescale != 0.25f || light->ambientscale != 0 || light->diffusescale != 1 || light->specularscale != 1 || light->flags != LIGHTFLAG_REALTIMEMODE)
3784 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);
3785 else if (light->cubemapname[0] || light->corona || light->angles[0] || light->angles[1] || light->angles[2])
3786 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]);
3788 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);
3789 if (bufchars + strlen(line) > bufmaxchars)
3791 bufmaxchars = bufchars + strlen(line) + 2048;
3793 buf = (char *)Mem_Alloc(tempmempool, bufmaxchars);
3797 memcpy(buf, oldbuf, bufchars);
3803 memcpy(buf + bufchars, line, strlen(line));
3804 bufchars += strlen(line);
3808 FS_WriteFile(name, buf, (fs_offset_t)bufchars);
3813 void R_Shadow_LoadLightsFile(void)
3816 char tempchar, *lightsstring, *s, *t, name[MAX_QPATH];
3817 float origin[3], radius, color[3], subtract, spotdir[3], spotcone, falloff, distbias;
3818 if (cl.worldmodel == NULL)
3820 Con_Print("No map loaded.\n");
3823 FS_StripExtension (cl.worldmodel->name, name, sizeof (name));
3824 strlcat (name, ".lights", sizeof (name));
3825 lightsstring = (char *)FS_LoadFile(name, tempmempool, false, NULL);
3833 while (*s && *s != '\n' && *s != '\r')
3839 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);
3843 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);
3846 radius = sqrt(DotProduct(color, color) / (falloff * falloff * 8192.0f * 8192.0f));
3847 radius = bound(15, radius, 4096);
3848 VectorScale(color, (2.0f / (8388608.0f)), color);
3849 R_Shadow_UpdateWorldLight(R_Shadow_NewWorldLight(), origin, vec3_origin, color, radius, 0, style, true, NULL, 0.25, 0, 1, 1, LIGHTFLAG_REALTIMEMODE);
3857 Con_Printf("invalid lights file \"%s\"\n", name);
3858 Mem_Free(lightsstring);
3862 // tyrlite/hmap2 light types in the delay field
3863 typedef enum lighttype_e {LIGHTTYPE_MINUSX, LIGHTTYPE_RECIPX, LIGHTTYPE_RECIPXX, LIGHTTYPE_NONE, LIGHTTYPE_SUN, LIGHTTYPE_MINUSXX} lighttype_t;
3865 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void)
3867 int entnum, style, islight, skin, pflags, effects, type, n;
3870 float origin[3], angles[3], radius, color[3], light[4], fadescale, lightscale, originhack[3], overridecolor[3], vec[4];
3871 char key[256], value[MAX_INPUTLINE];
3873 if (cl.worldmodel == NULL)
3875 Con_Print("No map loaded.\n");
3878 // try to load a .ent file first
3879 FS_StripExtension (cl.worldmodel->name, key, sizeof (key));
3880 strlcat (key, ".ent", sizeof (key));
3881 data = entfiledata = (char *)FS_LoadFile(key, tempmempool, true, NULL);
3882 // and if that is not found, fall back to the bsp file entity string
3884 data = cl.worldmodel->brush.entities;
3887 for (entnum = 0;COM_ParseToken_Simple(&data, false, false) && com_token[0] == '{';entnum++)
3889 type = LIGHTTYPE_MINUSX;
3890 origin[0] = origin[1] = origin[2] = 0;
3891 originhack[0] = originhack[1] = originhack[2] = 0;
3892 angles[0] = angles[1] = angles[2] = 0;
3893 color[0] = color[1] = color[2] = 1;
3894 light[0] = light[1] = light[2] = 1;light[3] = 300;
3895 overridecolor[0] = overridecolor[1] = overridecolor[2] = 1;
3905 if (!COM_ParseToken_Simple(&data, false, false))
3907 if (com_token[0] == '}')
3908 break; // end of entity
3909 if (com_token[0] == '_')
3910 strlcpy(key, com_token + 1, sizeof(key));
3912 strlcpy(key, com_token, sizeof(key));
3913 while (key[strlen(key)-1] == ' ') // remove trailing spaces
3914 key[strlen(key)-1] = 0;
3915 if (!COM_ParseToken_Simple(&data, false, false))
3917 strlcpy(value, com_token, sizeof(value));
3919 // now that we have the key pair worked out...
3920 if (!strcmp("light", key))
3922 n = sscanf(value, "%f %f %f %f", &vec[0], &vec[1], &vec[2], &vec[3]);
3926 light[0] = vec[0] * (1.0f / 256.0f);
3927 light[1] = vec[0] * (1.0f / 256.0f);
3928 light[2] = vec[0] * (1.0f / 256.0f);
3934 light[0] = vec[0] * (1.0f / 255.0f);
3935 light[1] = vec[1] * (1.0f / 255.0f);
3936 light[2] = vec[2] * (1.0f / 255.0f);
3940 else if (!strcmp("delay", key))
3942 else if (!strcmp("origin", key))
3943 sscanf(value, "%f %f %f", &origin[0], &origin[1], &origin[2]);
3944 else if (!strcmp("angle", key))
3945 angles[0] = 0, angles[1] = atof(value), angles[2] = 0;
3946 else if (!strcmp("angles", key))
3947 sscanf(value, "%f %f %f", &angles[0], &angles[1], &angles[2]);
3948 else if (!strcmp("color", key))
3949 sscanf(value, "%f %f %f", &color[0], &color[1], &color[2]);
3950 else if (!strcmp("wait", key))
3951 fadescale = atof(value);
3952 else if (!strcmp("classname", key))
3954 if (!strncmp(value, "light", 5))
3957 if (!strcmp(value, "light_fluoro"))
3962 overridecolor[0] = 1;
3963 overridecolor[1] = 1;
3964 overridecolor[2] = 1;
3966 if (!strcmp(value, "light_fluorospark"))
3971 overridecolor[0] = 1;
3972 overridecolor[1] = 1;
3973 overridecolor[2] = 1;
3975 if (!strcmp(value, "light_globe"))
3980 overridecolor[0] = 1;
3981 overridecolor[1] = 0.8;
3982 overridecolor[2] = 0.4;
3984 if (!strcmp(value, "light_flame_large_yellow"))
3989 overridecolor[0] = 1;
3990 overridecolor[1] = 0.5;
3991 overridecolor[2] = 0.1;
3993 if (!strcmp(value, "light_flame_small_yellow"))
3998 overridecolor[0] = 1;
3999 overridecolor[1] = 0.5;
4000 overridecolor[2] = 0.1;
4002 if (!strcmp(value, "light_torch_small_white"))
4007 overridecolor[0] = 1;
4008 overridecolor[1] = 0.5;
4009 overridecolor[2] = 0.1;
4011 if (!strcmp(value, "light_torch_small_walltorch"))
4016 overridecolor[0] = 1;
4017 overridecolor[1] = 0.5;
4018 overridecolor[2] = 0.1;
4022 else if (!strcmp("style", key))
4023 style = atoi(value);
4024 else if (!strcmp("skin", key))
4025 skin = (int)atof(value);
4026 else if (!strcmp("pflags", key))
4027 pflags = (int)atof(value);
4028 else if (!strcmp("effects", key))
4029 effects = (int)atof(value);
4030 else if (cl.worldmodel->type == mod_brushq3)
4032 if (!strcmp("scale", key))
4033 lightscale = atof(value);
4034 if (!strcmp("fade", key))
4035 fadescale = atof(value);
4040 if (lightscale <= 0)
4044 if (color[0] == color[1] && color[0] == color[2])
4046 color[0] *= overridecolor[0];
4047 color[1] *= overridecolor[1];
4048 color[2] *= overridecolor[2];
4050 radius = light[3] * r_editlights_quakelightsizescale.value * lightscale / fadescale;
4051 color[0] = color[0] * light[0];
4052 color[1] = color[1] * light[1];
4053 color[2] = color[2] * light[2];
4056 case LIGHTTYPE_MINUSX:
4058 case LIGHTTYPE_RECIPX:
4060 VectorScale(color, (1.0f / 16.0f), color);
4062 case LIGHTTYPE_RECIPXX:
4064 VectorScale(color, (1.0f / 16.0f), color);
4067 case LIGHTTYPE_NONE:
4071 case LIGHTTYPE_MINUSXX:
4074 VectorAdd(origin, originhack, origin);
4076 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);
4079 Mem_Free(entfiledata);
4083 void R_Shadow_SetCursorLocationForView(void)
4086 vec3_t dest, endpos;
4088 VectorMA(r_refdef.view.origin, r_editlights_cursordistance.value, r_refdef.view.forward, dest);
4089 trace = CL_Move(r_refdef.view.origin, vec3_origin, vec3_origin, dest, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false);
4090 if (trace.fraction < 1)
4092 dist = trace.fraction * r_editlights_cursordistance.value;
4093 push = r_editlights_cursorpushback.value;
4097 VectorMA(trace.endpos, push, r_refdef.view.forward, endpos);
4098 VectorMA(endpos, r_editlights_cursorpushoff.value, trace.plane.normal, endpos);
4102 VectorClear( endpos );
4104 r_editlights_cursorlocation[0] = floor(endpos[0] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
4105 r_editlights_cursorlocation[1] = floor(endpos[1] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
4106 r_editlights_cursorlocation[2] = floor(endpos[2] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
4109 void R_Shadow_UpdateWorldLightSelection(void)
4111 if (r_editlights.integer)
4113 R_Shadow_SetCursorLocationForView();
4114 R_Shadow_SelectLightInView();
4117 R_Shadow_SelectLight(NULL);
4120 void R_Shadow_EditLights_Clear_f(void)
4122 R_Shadow_ClearWorldLights();
4125 void R_Shadow_EditLights_Reload_f(void)
4129 strlcpy(r_shadow_mapname, cl.worldmodel->name, sizeof(r_shadow_mapname));
4130 R_Shadow_ClearWorldLights();
4131 R_Shadow_LoadWorldLights();
4132 if (!Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray))
4134 R_Shadow_LoadLightsFile();
4135 if (!Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray))
4136 R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
4140 void R_Shadow_EditLights_Save_f(void)
4144 R_Shadow_SaveWorldLights();
4147 void R_Shadow_EditLights_ImportLightEntitiesFromMap_f(void)
4149 R_Shadow_ClearWorldLights();
4150 R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
4153 void R_Shadow_EditLights_ImportLightsFile_f(void)
4155 R_Shadow_ClearWorldLights();
4156 R_Shadow_LoadLightsFile();
4159 void R_Shadow_EditLights_Spawn_f(void)
4162 if (!r_editlights.integer)
4164 Con_Print("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
4167 if (Cmd_Argc() != 1)
4169 Con_Print("r_editlights_spawn does not take parameters\n");
4172 color[0] = color[1] = color[2] = 1;
4173 R_Shadow_UpdateWorldLight(R_Shadow_NewWorldLight(), r_editlights_cursorlocation, vec3_origin, color, 200, 0, 0, true, NULL, 0.25, 0, 1, 1, LIGHTFLAG_REALTIMEMODE);
4176 void R_Shadow_EditLights_Edit_f(void)
4178 vec3_t origin, angles, color;
4179 vec_t radius, corona, coronasizescale, ambientscale, diffusescale, specularscale;
4180 int style, shadows, flags, normalmode, realtimemode;
4181 char cubemapname[MAX_INPUTLINE];
4182 if (!r_editlights.integer)
4184 Con_Print("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
4187 if (!r_shadow_selectedlight)
4189 Con_Print("No selected light.\n");
4192 VectorCopy(r_shadow_selectedlight->origin, origin);
4193 VectorCopy(r_shadow_selectedlight->angles, angles);
4194 VectorCopy(r_shadow_selectedlight->color, color);
4195 radius = r_shadow_selectedlight->radius;
4196 style = r_shadow_selectedlight->style;
4197 if (r_shadow_selectedlight->cubemapname)
4198 strlcpy(cubemapname, r_shadow_selectedlight->cubemapname, sizeof(cubemapname));
4201 shadows = r_shadow_selectedlight->shadow;
4202 corona = r_shadow_selectedlight->corona;
4203 coronasizescale = r_shadow_selectedlight->coronasizescale;
4204 ambientscale = r_shadow_selectedlight->ambientscale;
4205 diffusescale = r_shadow_selectedlight->diffusescale;
4206 specularscale = r_shadow_selectedlight->specularscale;
4207 flags = r_shadow_selectedlight->flags;
4208 normalmode = (flags & LIGHTFLAG_NORMALMODE) != 0;
4209 realtimemode = (flags & LIGHTFLAG_REALTIMEMODE) != 0;
4210 if (!strcmp(Cmd_Argv(1), "origin"))
4212 if (Cmd_Argc() != 5)
4214 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(1));
4217 origin[0] = atof(Cmd_Argv(2));
4218 origin[1] = atof(Cmd_Argv(3));
4219 origin[2] = atof(Cmd_Argv(4));
4221 else if (!strcmp(Cmd_Argv(1), "originx"))
4223 if (Cmd_Argc() != 3)
4225 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4228 origin[0] = atof(Cmd_Argv(2));
4230 else if (!strcmp(Cmd_Argv(1), "originy"))
4232 if (Cmd_Argc() != 3)
4234 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4237 origin[1] = atof(Cmd_Argv(2));
4239 else if (!strcmp(Cmd_Argv(1), "originz"))
4241 if (Cmd_Argc() != 3)
4243 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4246 origin[2] = atof(Cmd_Argv(2));
4248 else if (!strcmp(Cmd_Argv(1), "move"))
4250 if (Cmd_Argc() != 5)
4252 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(1));
4255 origin[0] += atof(Cmd_Argv(2));
4256 origin[1] += atof(Cmd_Argv(3));
4257 origin[2] += atof(Cmd_Argv(4));
4259 else if (!strcmp(Cmd_Argv(1), "movex"))
4261 if (Cmd_Argc() != 3)
4263 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4266 origin[0] += atof(Cmd_Argv(2));
4268 else if (!strcmp(Cmd_Argv(1), "movey"))
4270 if (Cmd_Argc() != 3)
4272 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4275 origin[1] += atof(Cmd_Argv(2));
4277 else if (!strcmp(Cmd_Argv(1), "movez"))
4279 if (Cmd_Argc() != 3)
4281 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4284 origin[2] += atof(Cmd_Argv(2));
4286 else if (!strcmp(Cmd_Argv(1), "angles"))
4288 if (Cmd_Argc() != 5)
4290 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(1));
4293 angles[0] = atof(Cmd_Argv(2));
4294 angles[1] = atof(Cmd_Argv(3));
4295 angles[2] = atof(Cmd_Argv(4));
4297 else if (!strcmp(Cmd_Argv(1), "anglesx"))
4299 if (Cmd_Argc() != 3)
4301 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4304 angles[0] = atof(Cmd_Argv(2));
4306 else if (!strcmp(Cmd_Argv(1), "anglesy"))
4308 if (Cmd_Argc() != 3)
4310 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4313 angles[1] = atof(Cmd_Argv(2));
4315 else if (!strcmp(Cmd_Argv(1), "anglesz"))
4317 if (Cmd_Argc() != 3)
4319 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4322 angles[2] = atof(Cmd_Argv(2));
4324 else if (!strcmp(Cmd_Argv(1), "color"))
4326 if (Cmd_Argc() != 5)
4328 Con_Printf("usage: r_editlights_edit %s red green blue\n", Cmd_Argv(1));
4331 color[0] = atof(Cmd_Argv(2));
4332 color[1] = atof(Cmd_Argv(3));
4333 color[2] = atof(Cmd_Argv(4));
4335 else if (!strcmp(Cmd_Argv(1), "radius"))
4337 if (Cmd_Argc() != 3)
4339 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4342 radius = atof(Cmd_Argv(2));
4344 else if (!strcmp(Cmd_Argv(1), "colorscale"))
4346 if (Cmd_Argc() == 3)
4348 double scale = atof(Cmd_Argv(2));
4355 if (Cmd_Argc() != 5)
4357 Con_Printf("usage: r_editlights_edit %s red green blue (OR grey instead of red green blue)\n", Cmd_Argv(1));
4360 color[0] *= atof(Cmd_Argv(2));
4361 color[1] *= atof(Cmd_Argv(3));
4362 color[2] *= atof(Cmd_Argv(4));
4365 else if (!strcmp(Cmd_Argv(1), "radiusscale") || !strcmp(Cmd_Argv(1), "sizescale"))
4367 if (Cmd_Argc() != 3)
4369 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4372 radius *= atof(Cmd_Argv(2));
4374 else if (!strcmp(Cmd_Argv(1), "style"))
4376 if (Cmd_Argc() != 3)
4378 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4381 style = atoi(Cmd_Argv(2));
4383 else if (!strcmp(Cmd_Argv(1), "cubemap"))
4387 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4390 if (Cmd_Argc() == 3)
4391 strlcpy(cubemapname, Cmd_Argv(2), sizeof(cubemapname));
4395 else if (!strcmp(Cmd_Argv(1), "shadows"))
4397 if (Cmd_Argc() != 3)
4399 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4402 shadows = Cmd_Argv(2)[0] == 'y' || Cmd_Argv(2)[0] == 'Y' || Cmd_Argv(2)[0] == 't' || atoi(Cmd_Argv(2));
4404 else if (!strcmp(Cmd_Argv(1), "corona"))
4406 if (Cmd_Argc() != 3)
4408 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4411 corona = atof(Cmd_Argv(2));
4413 else if (!strcmp(Cmd_Argv(1), "coronasize"))
4415 if (Cmd_Argc() != 3)
4417 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4420 coronasizescale = atof(Cmd_Argv(2));
4422 else if (!strcmp(Cmd_Argv(1), "ambient"))
4424 if (Cmd_Argc() != 3)
4426 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4429 ambientscale = atof(Cmd_Argv(2));
4431 else if (!strcmp(Cmd_Argv(1), "diffuse"))
4433 if (Cmd_Argc() != 3)
4435 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4438 diffusescale = atof(Cmd_Argv(2));
4440 else if (!strcmp(Cmd_Argv(1), "specular"))
4442 if (Cmd_Argc() != 3)
4444 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4447 specularscale = atof(Cmd_Argv(2));
4449 else if (!strcmp(Cmd_Argv(1), "normalmode"))
4451 if (Cmd_Argc() != 3)
4453 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4456 normalmode = Cmd_Argv(2)[0] == 'y' || Cmd_Argv(2)[0] == 'Y' || Cmd_Argv(2)[0] == 't' || atoi(Cmd_Argv(2));
4458 else if (!strcmp(Cmd_Argv(1), "realtimemode"))
4460 if (Cmd_Argc() != 3)
4462 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
4465 realtimemode = Cmd_Argv(2)[0] == 'y' || Cmd_Argv(2)[0] == 'Y' || Cmd_Argv(2)[0] == 't' || atoi(Cmd_Argv(2));
4469 Con_Print("usage: r_editlights_edit [property] [value]\n");
4470 Con_Print("Selected light's properties:\n");
4471 Con_Printf("Origin : %f %f %f\n", r_shadow_selectedlight->origin[0], r_shadow_selectedlight->origin[1], r_shadow_selectedlight->origin[2]);
4472 Con_Printf("Angles : %f %f %f\n", r_shadow_selectedlight->angles[0], r_shadow_selectedlight->angles[1], r_shadow_selectedlight->angles[2]);
4473 Con_Printf("Color : %f %f %f\n", r_shadow_selectedlight->color[0], r_shadow_selectedlight->color[1], r_shadow_selectedlight->color[2]);
4474 Con_Printf("Radius : %f\n", r_shadow_selectedlight->radius);
4475 Con_Printf("Corona : %f\n", r_shadow_selectedlight->corona);
4476 Con_Printf("Style : %i\n", r_shadow_selectedlight->style);
4477 Con_Printf("Shadows : %s\n", r_shadow_selectedlight->shadow ? "yes" : "no");
4478 Con_Printf("Cubemap : %s\n", r_shadow_selectedlight->cubemapname);
4479 Con_Printf("CoronaSize : %f\n", r_shadow_selectedlight->coronasizescale);
4480 Con_Printf("Ambient : %f\n", r_shadow_selectedlight->ambientscale);
4481 Con_Printf("Diffuse : %f\n", r_shadow_selectedlight->diffusescale);
4482 Con_Printf("Specular : %f\n", r_shadow_selectedlight->specularscale);
4483 Con_Printf("NormalMode : %s\n", (r_shadow_selectedlight->flags & LIGHTFLAG_NORMALMODE) ? "yes" : "no");
4484 Con_Printf("RealTimeMode : %s\n", (r_shadow_selectedlight->flags & LIGHTFLAG_REALTIMEMODE) ? "yes" : "no");
4487 flags = (normalmode ? LIGHTFLAG_NORMALMODE : 0) | (realtimemode ? LIGHTFLAG_REALTIMEMODE : 0);
4488 R_Shadow_UpdateWorldLight(r_shadow_selectedlight, origin, angles, color, radius, corona, style, shadows, cubemapname, coronasizescale, ambientscale, diffusescale, specularscale, flags);
4491 void R_Shadow_EditLights_EditAll_f(void)
4497 if (!r_editlights.integer)
4499 Con_Print("Cannot edit lights when not in editing mode. Set r_editlights to 1.\n");
4503 // EditLights doesn't seem to have a "remove" command or something so:
4504 range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
4505 for (lightindex = 0;lightindex < range;lightindex++)
4507 light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
4510 R_Shadow_SelectLight(light);
4511 R_Shadow_EditLights_Edit_f();
4515 void R_Shadow_EditLights_DrawSelectedLightProperties(void)
4517 int lightnumber, lightcount;
4518 size_t lightindex, range;
4522 if (!r_editlights.integer)
4524 x = vid_conwidth.value - 240;
4526 DrawQ_Pic(x-5, y-5, NULL, 250, 155, 0, 0, 0, 0.75, 0);
4529 range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
4530 for (lightindex = 0;lightindex < range;lightindex++)
4532 light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
4535 if (light == r_shadow_selectedlight)
4536 lightnumber = lightindex;
4539 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;
4540 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;
4542 if (r_shadow_selectedlight == NULL)
4544 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;
4545 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;
4546 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;
4547 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;
4548 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;
4549 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;
4550 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;
4551 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;
4552 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;
4553 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;
4554 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;
4555 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;
4556 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;
4557 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;
4558 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;
4561 void R_Shadow_EditLights_ToggleShadow_f(void)
4563 if (!r_editlights.integer)
4565 Con_Print("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
4568 if (!r_shadow_selectedlight)
4570 Con_Print("No selected light.\n");
4573 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);
4576 void R_Shadow_EditLights_ToggleCorona_f(void)
4578 if (!r_editlights.integer)
4580 Con_Print("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
4583 if (!r_shadow_selectedlight)
4585 Con_Print("No selected light.\n");
4588 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);
4591 void R_Shadow_EditLights_Remove_f(void)
4593 if (!r_editlights.integer)
4595 Con_Print("Cannot remove light when not in editing mode. Set r_editlights to 1.\n");
4598 if (!r_shadow_selectedlight)
4600 Con_Print("No selected light.\n");
4603 R_Shadow_FreeWorldLight(r_shadow_selectedlight);
4604 r_shadow_selectedlight = NULL;
4607 void R_Shadow_EditLights_Help_f(void)
4610 "Documentation on r_editlights system:\n"
4612 "r_editlights : enable/disable editing mode\n"
4613 "r_editlights_cursordistance : maximum distance of cursor from eye\n"
4614 "r_editlights_cursorpushback : push back cursor this far from surface\n"
4615 "r_editlights_cursorpushoff : push cursor off surface this far\n"
4616 "r_editlights_cursorgrid : snap cursor to grid of this size\n"
4617 "r_editlights_quakelightsizescale : imported quake light entity size scaling\n"
4619 "r_editlights_help : this help\n"
4620 "r_editlights_clear : remove all lights\n"
4621 "r_editlights_reload : reload .rtlights, .lights file, or entities\n"
4622 "r_editlights_save : save to .rtlights file\n"
4623 "r_editlights_spawn : create a light with default settings\n"
4624 "r_editlights_edit command : edit selected light - more documentation below\n"
4625 "r_editlights_remove : remove selected light\n"
4626 "r_editlights_toggleshadow : toggles on/off selected light's shadow property\n"
4627 "r_editlights_importlightentitiesfrommap : reload light entities\n"
4628 "r_editlights_importlightsfile : reload .light file (produced by hlight)\n"
4630 "origin x y z : set light location\n"
4631 "originx x: set x component of light location\n"
4632 "originy y: set y component of light location\n"
4633 "originz z: set z component of light location\n"
4634 "move x y z : adjust light location\n"
4635 "movex x: adjust x component of light location\n"
4636 "movey y: adjust y component of light location\n"
4637 "movez z: adjust z component of light location\n"
4638 "angles x y z : set light angles\n"
4639 "anglesx x: set x component of light angles\n"
4640 "anglesy y: set y component of light angles\n"
4641 "anglesz z: set z component of light angles\n"
4642 "color r g b : set color of light (can be brighter than 1 1 1)\n"
4643 "radius radius : set radius (size) of light\n"
4644 "colorscale grey : multiply color of light (1 does nothing)\n"
4645 "colorscale r g b : multiply color of light (1 1 1 does nothing)\n"
4646 "radiusscale scale : multiply radius (size) of light (1 does nothing)\n"
4647 "sizescale scale : multiply radius (size) of light (1 does nothing)\n"
4648 "style style : set lightstyle of light (flickering patterns, switches, etc)\n"
4649 "cubemap basename : set filter cubemap of light (not yet supported)\n"
4650 "shadows 1/0 : turn on/off shadows\n"
4651 "corona n : set corona intensity\n"
4652 "coronasize n : set corona size (0-1)\n"
4653 "ambient n : set ambient intensity (0-1)\n"
4654 "diffuse n : set diffuse intensity (0-1)\n"
4655 "specular n : set specular intensity (0-1)\n"
4656 "normalmode 1/0 : turn on/off rendering of this light in rtworld 0 mode\n"
4657 "realtimemode 1/0 : turn on/off rendering of this light in rtworld 1 mode\n"
4658 "<nothing> : print light properties to console\n"
4662 void R_Shadow_EditLights_CopyInfo_f(void)
4664 if (!r_editlights.integer)
4666 Con_Print("Cannot copy light info when not in editing mode. Set r_editlights to 1.\n");
4669 if (!r_shadow_selectedlight)
4671 Con_Print("No selected light.\n");
4674 VectorCopy(r_shadow_selectedlight->angles, r_shadow_bufferlight.angles);
4675 VectorCopy(r_shadow_selectedlight->color, r_shadow_bufferlight.color);
4676 r_shadow_bufferlight.radius = r_shadow_selectedlight->radius;
4677 r_shadow_bufferlight.style = r_shadow_selectedlight->style;
4678 if (r_shadow_selectedlight->cubemapname)
4679 strlcpy(r_shadow_bufferlight.cubemapname, r_shadow_selectedlight->cubemapname, sizeof(r_shadow_bufferlight.cubemapname));
4681 r_shadow_bufferlight.cubemapname[0] = 0;
4682 r_shadow_bufferlight.shadow = r_shadow_selectedlight->shadow;
4683 r_shadow_bufferlight.corona = r_shadow_selectedlight->corona;
4684 r_shadow_bufferlight.coronasizescale = r_shadow_selectedlight->coronasizescale;
4685 r_shadow_bufferlight.ambientscale = r_shadow_selectedlight->ambientscale;
4686 r_shadow_bufferlight.diffusescale = r_shadow_selectedlight->diffusescale;
4687 r_shadow_bufferlight.specularscale = r_shadow_selectedlight->specularscale;
4688 r_shadow_bufferlight.flags = r_shadow_selectedlight->flags;
4691 void R_Shadow_EditLights_PasteInfo_f(void)
4693 if (!r_editlights.integer)
4695 Con_Print("Cannot paste light info when not in editing mode. Set r_editlights to 1.\n");
4698 if (!r_shadow_selectedlight)
4700 Con_Print("No selected light.\n");
4703 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);
4706 void R_Shadow_EditLights_Init(void)
4708 Cvar_RegisterVariable(&r_editlights);
4709 Cvar_RegisterVariable(&r_editlights_cursordistance);
4710 Cvar_RegisterVariable(&r_editlights_cursorpushback);
4711 Cvar_RegisterVariable(&r_editlights_cursorpushoff);
4712 Cvar_RegisterVariable(&r_editlights_cursorgrid);
4713 Cvar_RegisterVariable(&r_editlights_quakelightsizescale);
4714 Cmd_AddCommand("r_editlights_help", R_Shadow_EditLights_Help_f, "prints documentation on console commands and variables in rtlight editing system");
4715 Cmd_AddCommand("r_editlights_clear", R_Shadow_EditLights_Clear_f, "removes all world lights (let there be darkness!)");
4716 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)");
4717 Cmd_AddCommand("r_editlights_save", R_Shadow_EditLights_Save_f, "save .rtlights file for current level");
4718 Cmd_AddCommand("r_editlights_spawn", R_Shadow_EditLights_Spawn_f, "creates a light with default properties (let there be light!)");
4719 Cmd_AddCommand("r_editlights_edit", R_Shadow_EditLights_Edit_f, "changes a property on the selected light");
4720 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)");
4721 Cmd_AddCommand("r_editlights_remove", R_Shadow_EditLights_Remove_f, "remove selected light");
4722 Cmd_AddCommand("r_editlights_toggleshadow", R_Shadow_EditLights_ToggleShadow_f, "toggle on/off the shadow option on the selected light");
4723 Cmd_AddCommand("r_editlights_togglecorona", R_Shadow_EditLights_ToggleCorona_f, "toggle on/off the corona option on the selected light");
4724 Cmd_AddCommand("r_editlights_importlightentitiesfrommap", R_Shadow_EditLights_ImportLightEntitiesFromMap_f, "load lights from .ent file or map entities (ignoring .rtlights or .lights file)");
4725 Cmd_AddCommand("r_editlights_importlightsfile", R_Shadow_EditLights_ImportLightsFile_f, "load lights from .lights file (ignoring .rtlights or .ent files and map entities)");
4726 Cmd_AddCommand("r_editlights_copyinfo", R_Shadow_EditLights_CopyInfo_f, "store a copy of all properties (except origin) of the selected light");
4727 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)");
4733 =============================================================================
4737 =============================================================================
4740 void R_CompleteLightPoint(vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal, const vec3_t p, int dynamic)
4742 VectorClear(diffusecolor);
4743 VectorClear(diffusenormal);
4745 if (!r_fullbright.integer && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.LightPoint)
4747 ambientcolor[0] = ambientcolor[1] = ambientcolor[2] = r_refdef.scene.ambient * (2.0f / 128.0f);
4748 r_refdef.scene.worldmodel->brush.LightPoint(r_refdef.scene.worldmodel, p, ambientcolor, diffusecolor, diffusenormal);
4751 VectorSet(ambientcolor, 1, 1, 1);
4758 for (i = 0;i < r_refdef.scene.numlights;i++)
4760 light = &r_refdef.scene.lights[i];
4761 Matrix4x4_Transform(&light->matrix_worldtolight, p, v);
4762 f = 1 - VectorLength2(v);
4763 if (f > 0 && CL_Move(p, vec3_origin, vec3_origin, light->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false).fraction == 1)
4764 VectorMA(ambientcolor, f, light->currentcolor, ambientcolor);