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 #define R_SHADOW_SHADOWMAP_NUMCUBEMAPS 8
145 extern void R_Shadow_EditLights_Init(void);
147 typedef enum r_shadow_rendermode_e
149 R_SHADOW_RENDERMODE_NONE,
150 R_SHADOW_RENDERMODE_ZPASS_STENCIL,
151 R_SHADOW_RENDERMODE_ZPASS_SEPARATESTENCIL,
152 R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE,
153 R_SHADOW_RENDERMODE_ZFAIL_STENCIL,
154 R_SHADOW_RENDERMODE_ZFAIL_SEPARATESTENCIL,
155 R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE,
156 R_SHADOW_RENDERMODE_LIGHT_VERTEX,
157 R_SHADOW_RENDERMODE_LIGHT_DOT3,
158 R_SHADOW_RENDERMODE_LIGHT_GLSL,
159 R_SHADOW_RENDERMODE_VISIBLEVOLUMES,
160 R_SHADOW_RENDERMODE_VISIBLELIGHTING,
161 R_SHADOW_RENDERMODE_SHADOWMAPRECTANGLE,
162 R_SHADOW_RENDERMODE_SHADOWMAP2D,
163 R_SHADOW_RENDERMODE_SHADOWMAPCUBESIDE,
165 r_shadow_rendermode_t;
167 r_shadow_rendermode_t r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
168 r_shadow_rendermode_t r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_NONE;
169 r_shadow_rendermode_t r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_NONE;
170 r_shadow_rendermode_t r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_NONE;
171 qboolean r_shadow_usingshadowmaprect;
172 qboolean r_shadow_usingshadowmap2d;
173 qboolean r_shadow_usingshadowmapcube;
174 float r_shadow_shadowmap_texturescale[4];
175 float r_shadow_shadowmap_parameters[4];
176 int r_shadow_drawbuffer;
177 int r_shadow_readbuffer;
178 GLuint r_shadow_fborectangle;
179 GLuint r_shadow_fbocubeside[R_SHADOW_SHADOWMAP_NUMCUBEMAPS][6];
180 GLuint r_shadow_fbo2d;
181 int r_shadow_shadowmode;
182 int r_shadow_shadowmapfilterquality;
183 int r_shadow_shadowmaptexturetype;
184 int r_shadow_shadowmapmaxsize;
185 qboolean r_shadow_shadowmapvsdct;
186 qboolean r_shadow_shadowmapsampler;
187 int r_shadow_shadowmappcf;
188 int r_shadow_shadowmapborder;
189 int r_shadow_lightscissor[4];
191 int maxshadowtriangles;
194 int maxshadowvertices;
195 float *shadowvertex3f;
208 int r_shadow_buffer_numleafpvsbytes;
209 unsigned char *r_shadow_buffer_visitingleafpvs;
210 unsigned char *r_shadow_buffer_leafpvs;
211 int *r_shadow_buffer_leaflist;
213 int r_shadow_buffer_numsurfacepvsbytes;
214 unsigned char *r_shadow_buffer_surfacepvs;
215 int *r_shadow_buffer_surfacelist;
217 int r_shadow_buffer_numshadowtrispvsbytes;
218 unsigned char *r_shadow_buffer_shadowtrispvs;
219 int r_shadow_buffer_numlighttrispvsbytes;
220 unsigned char *r_shadow_buffer_lighttrispvs;
222 rtexturepool_t *r_shadow_texturepool;
223 rtexture_t *r_shadow_attenuationgradienttexture;
224 rtexture_t *r_shadow_attenuation2dtexture;
225 rtexture_t *r_shadow_attenuation3dtexture;
226 rtexture_t *r_shadow_lightcorona;
227 rtexture_t *r_shadow_shadowmaprectangletexture;
228 rtexture_t *r_shadow_shadowmap2dtexture;
229 rtexture_t *r_shadow_shadowmapcubetexture[R_SHADOW_SHADOWMAP_NUMCUBEMAPS];
230 rtexture_t *r_shadow_shadowmapvsdcttexture;
231 int r_shadow_shadowmapsize; // changes for each light based on distance
232 int r_shadow_shadowmaplod; // changes for each light based on distance
234 // lights are reloaded when this changes
235 char r_shadow_mapname[MAX_QPATH];
237 // used only for light filters (cubemaps)
238 rtexturepool_t *r_shadow_filters_texturepool;
240 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"};
241 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"};
242 cvar_t r_shadow_debuglight = {0, "r_shadow_debuglight", "-1", "renders only one light, for level design purposes or debugging"};
243 cvar_t r_shadow_usenormalmap = {CVAR_SAVE, "r_shadow_usenormalmap", "1", "enables use of directional shading on lights"};
244 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)"};
245 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"};
246 cvar_t r_shadow_glossintensity = {0, "r_shadow_glossintensity", "1", "how bright textured glossmaps should look if r_shadow_gloss is 1 or 2"};
247 cvar_t r_shadow_glossexponent = {0, "r_shadow_glossexponent", "32", "how 'sharp' the gloss should appear (specular power)"};
248 cvar_t r_shadow_glossexact = {0, "r_shadow_glossexact", "0", "use exact reflection math for gloss (slightly slower, but should look a tad better)"};
249 cvar_t r_shadow_lightattenuationdividebias = {0, "r_shadow_lightattenuationdividebias", "1", "changes attenuation texture generation"};
250 cvar_t r_shadow_lightattenuationlinearscale = {0, "r_shadow_lightattenuationlinearscale", "2", "changes attenuation texture generation"};
251 cvar_t r_shadow_lightintensityscale = {0, "r_shadow_lightintensityscale", "1", "renders all world lights brighter or darker"};
252 cvar_t r_shadow_lightradiusscale = {0, "r_shadow_lightradiusscale", "1", "renders all world lights larger or smaller"};
253 cvar_t r_shadow_portallight = {0, "r_shadow_portallight", "1", "use portal culling to exactly determine lit triangles when compiling world lights"};
254 cvar_t r_shadow_projectdistance = {0, "r_shadow_projectdistance", "1000000", "how far to cast shadows"};
255 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)"};
256 cvar_t r_shadow_realtime_dlight = {CVAR_SAVE, "r_shadow_realtime_dlight", "1", "enables rendering of dynamic lights such as explosions and rocket light"};
257 cvar_t r_shadow_realtime_dlight_shadows = {CVAR_SAVE, "r_shadow_realtime_dlight_shadows", "1", "enables rendering of shadows from dynamic lights"};
258 cvar_t r_shadow_realtime_dlight_svbspculling = {0, "r_shadow_realtime_dlight_svbspculling", "0", "enables svbsp optimization on dynamic lights (very slow!)"};
259 cvar_t r_shadow_realtime_dlight_portalculling = {0, "r_shadow_realtime_dlight_portalculling", "0", "enables portal optimization on dynamic lights (slow!)"};
260 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)"};
261 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"};
262 cvar_t r_shadow_realtime_world_shadows = {CVAR_SAVE, "r_shadow_realtime_world_shadows", "1", "enables rendering of shadows from world lights"};
263 cvar_t r_shadow_realtime_world_compile = {0, "r_shadow_realtime_world_compile", "1", "enables compilation of world lights for higher performance rendering"};
264 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"};
265 cvar_t r_shadow_realtime_world_compilesvbsp = {0, "r_shadow_realtime_world_compilesvbsp", "1", "enables svbsp optimization during compilation"};
266 cvar_t r_shadow_realtime_world_compileportalculling = {0, "r_shadow_realtime_world_compileportalculling", "1", "enables portal-based culling optimization during compilation"};
267 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)"};
268 cvar_t r_shadow_shadowmapping = {CVAR_SAVE, "r_shadow_shadowmapping", "0", "enables use of shadowmapping (depth texture sampling) instead of stencil shadow volumes, requires gl_fbo 1"};
269 cvar_t r_shadow_shadowmapping_texturetype = {CVAR_SAVE, "r_shadow_shadowmapping_texturetype", "0", "shadowmap texture types: 0 = auto-select, 1 = 2D, 2 = rectangle, 3 = cubemap"};
270 cvar_t r_shadow_shadowmapping_filterquality = {CVAR_SAVE, "r_shadow_shadowmapping_filterquality", "-1", "shadowmap filter modes: -1 = auto-select, 0 = no filtering, 1 = bilinear, 2 = bilinear 2x2 blur (fast), 3 = 3x3 blur (moderate), 4 = 4x4 blur (slow)"};
271 cvar_t r_shadow_shadowmapping_vsdct = {CVAR_SAVE, "r_shadow_shadowmapping_vsdct", "1", "enables use of virtual shadow depth cube texture"};
272 cvar_t r_shadow_shadowmapping_minsize = {CVAR_SAVE, "r_shadow_shadowmapping_minsize", "32", "shadowmap size limit"};
273 cvar_t r_shadow_shadowmapping_maxsize = {CVAR_SAVE, "r_shadow_shadowmapping_maxsize", "512", "shadowmap size limit"};
274 cvar_t r_shadow_shadowmapping_lod_bias = {CVAR_SAVE, "r_shadow_shadowmapping_lod_bias", "16", "shadowmap size bias"};
275 cvar_t r_shadow_shadowmapping_lod_scale = {CVAR_SAVE, "r_shadow_shadowmapping_lod_scale", "128", "shadowmap size scaling parameter"};
276 cvar_t r_shadow_shadowmapping_bordersize = {CVAR_SAVE, "r_shadow_shadowmapping_bordersize", "4", "shadowmap size bias for filtering"};
277 cvar_t r_shadow_shadowmapping_nearclip = {CVAR_SAVE, "r_shadow_shadowmapping_nearclip", "1", "shadowmap nearclip in world units"};
278 cvar_t r_shadow_shadowmapping_bias = {CVAR_SAVE, "r_shadow_shadowmapping_bias", "0.03", "shadowmap bias parameter (this is multiplied by nearclip * 1024 / lodsize)"};
279 cvar_t r_shadow_culltriangles = {0, "r_shadow_culltriangles", "1", "performs more expensive tests to remove unnecessary triangles of lit surfaces"};
280 cvar_t r_shadow_polygonfactor = {0, "r_shadow_polygonfactor", "0", "how much to enlarge shadow volume polygons when rendering (should be 0!)"};
281 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)"};
282 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)"};
283 cvar_t r_coronas = {CVAR_SAVE, "r_coronas", "1", "brightness of corona flare effects around certain lights, 0 disables corona effects"};
284 cvar_t r_coronas_occlusionsizescale = {CVAR_SAVE, "r_coronas_occlusionsizescale", "0.1", "size of light source for corona occlusion checksm the proportion of hidden pixels controls corona intensity"};
285 cvar_t r_coronas_occlusionquery = {CVAR_SAVE, "r_coronas_occlusionquery", "1", "use GL_ARB_occlusion_query extension if supported (fades coronas according to visibility)"};
286 cvar_t gl_flashblend = {CVAR_SAVE, "gl_flashblend", "0", "render bright coronas for dynamic lights instead of actual lighting, fast but ugly"};
287 cvar_t gl_ext_separatestencil = {0, "gl_ext_separatestencil", "1", "make use of OpenGL 2.0 glStencilOpSeparate or GL_ATI_separate_stencil extension"};
288 cvar_t gl_ext_stenciltwoside = {0, "gl_ext_stenciltwoside", "1", "make use of GL_EXT_stenciltwoside extension (NVIDIA only)"};
289 cvar_t r_editlights = {0, "r_editlights", "0", "enables .rtlights file editing mode"};
290 cvar_t r_editlights_cursordistance = {0, "r_editlights_cursordistance", "1024", "maximum distance of cursor from eye"};
291 cvar_t r_editlights_cursorpushback = {0, "r_editlights_cursorpushback", "0", "how far to pull the cursor back toward the eye"};
292 cvar_t r_editlights_cursorpushoff = {0, "r_editlights_cursorpushoff", "4", "how far to push the cursor off the impacted surface"};
293 cvar_t r_editlights_cursorgrid = {0, "r_editlights_cursorgrid", "4", "snaps cursor to this grid size"};
294 cvar_t r_editlights_quakelightsizescale = {CVAR_SAVE, "r_editlights_quakelightsizescale", "1", "changes size of light entities loaded from a map"};
296 // note the table actually includes one more value, just to avoid the need to clamp the distance index due to minor math error
297 #define ATTENTABLESIZE 256
298 // 1D gradient, 2D circle and 3D sphere attenuation textures
299 #define ATTEN1DSIZE 32
300 #define ATTEN2DSIZE 64
301 #define ATTEN3DSIZE 32
303 static float r_shadow_attendividebias; // r_shadow_lightattenuationdividebias
304 static float r_shadow_attenlinearscale; // r_shadow_lightattenuationlinearscale
305 static float r_shadow_attentable[ATTENTABLESIZE+1];
307 rtlight_t *r_shadow_compilingrtlight;
308 static memexpandablearray_t r_shadow_worldlightsarray;
309 dlight_t *r_shadow_selectedlight;
310 dlight_t r_shadow_bufferlight;
311 vec3_t r_editlights_cursorlocation;
313 extern int con_vislines;
315 typedef struct cubemapinfo_s
322 #define MAX_CUBEMAPS 256
323 static int numcubemaps;
324 static cubemapinfo_t cubemaps[MAX_CUBEMAPS];
326 void R_Shadow_UncompileWorldLights(void);
327 void R_Shadow_ClearWorldLights(void);
328 void R_Shadow_SaveWorldLights(void);
329 void R_Shadow_LoadWorldLights(void);
330 void R_Shadow_LoadLightsFile(void);
331 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void);
332 void R_Shadow_EditLights_Reload_f(void);
333 void R_Shadow_ValidateCvars(void);
334 static void R_Shadow_MakeTextures(void);
336 // VorteX: custom editor light sprites
337 #define EDLIGHTSPRSIZE 8
338 cachepic_t *r_editlights_sprcursor;
339 cachepic_t *r_editlights_sprlight;
340 cachepic_t *r_editlights_sprnoshadowlight;
341 cachepic_t *r_editlights_sprcubemaplight;
342 cachepic_t *r_editlights_sprcubemapnoshadowlight;
343 cachepic_t *r_editlights_sprselection;
345 void R_Shadow_SetShadowMode(void)
347 r_shadow_shadowmapmaxsize = bound(1, r_shadow_shadowmapping_maxsize.integer, 2048);
348 r_shadow_shadowmapvsdct = r_shadow_shadowmapping_vsdct.integer != 0;
349 r_shadow_shadowmapfilterquality = r_shadow_shadowmapping_filterquality.integer;
350 r_shadow_shadowmaptexturetype = r_shadow_shadowmapping_texturetype.integer;
351 r_shadow_shadowmapborder = bound(0, r_shadow_shadowmapping_bordersize.integer, 16);
352 r_shadow_shadowmaplod = -1;
353 r_shadow_shadowmapsampler = false;
354 r_shadow_shadowmappcf = 0;
355 r_shadow_shadowmode = 0;
356 if(r_shadow_shadowmapping.integer)
358 if(r_shadow_shadowmapfilterquality < 0)
360 if(strstr(gl_vendor, "NVIDIA"))
362 r_shadow_shadowmapsampler = gl_support_arb_shadow;
363 r_shadow_shadowmappcf = 1;
365 else if(gl_support_amd_texture_texture4 || gl_support_arb_texture_gather)
366 r_shadow_shadowmappcf = 1;
367 else if(strstr(gl_vendor, "ATI"))
368 r_shadow_shadowmappcf = 1;
370 r_shadow_shadowmapsampler = gl_support_arb_shadow;
374 switch (r_shadow_shadowmapfilterquality)
377 r_shadow_shadowmapsampler = gl_support_arb_shadow;
380 r_shadow_shadowmapsampler = gl_support_arb_shadow;
381 r_shadow_shadowmappcf = 1;
384 r_shadow_shadowmappcf = 1;
387 r_shadow_shadowmappcf = 2;
391 r_shadow_shadowmode = r_shadow_shadowmaptexturetype;
392 if(r_shadow_shadowmode <= 0)
394 if((gl_support_amd_texture_texture4 || gl_support_arb_texture_gather) && r_shadow_shadowmappcf && !r_shadow_shadowmapsampler)
395 r_shadow_shadowmode = 1;
396 else if(gl_texturerectangle)
397 r_shadow_shadowmode = 2;
399 r_shadow_shadowmode = 1;
404 void R_Shadow_FreeShadowMaps(void)
408 R_Shadow_SetShadowMode();
410 if (r_shadow_fborectangle)
411 qglDeleteFramebuffersEXT(1, &r_shadow_fborectangle);
412 r_shadow_fborectangle = 0;
416 qglDeleteFramebuffersEXT(1, &r_shadow_fbo2d);
419 for (i = 0;i < R_SHADOW_SHADOWMAP_NUMCUBEMAPS;i++)
420 if (r_shadow_fbocubeside[i][0])
421 qglDeleteFramebuffersEXT(6, r_shadow_fbocubeside[i]);
422 memset(r_shadow_fbocubeside, 0, sizeof(r_shadow_fbocubeside));
425 if (r_shadow_shadowmaprectangletexture)
426 R_FreeTexture(r_shadow_shadowmaprectangletexture);
427 r_shadow_shadowmaprectangletexture = NULL;
429 if (r_shadow_shadowmap2dtexture)
430 R_FreeTexture(r_shadow_shadowmap2dtexture);
431 r_shadow_shadowmap2dtexture = NULL;
433 for (i = 0;i < R_SHADOW_SHADOWMAP_NUMCUBEMAPS;i++)
434 if (r_shadow_shadowmapcubetexture[i])
435 R_FreeTexture(r_shadow_shadowmapcubetexture[i]);
436 memset(r_shadow_shadowmapcubetexture, 0, sizeof(r_shadow_shadowmapcubetexture));
438 if (r_shadow_shadowmapvsdcttexture)
439 R_FreeTexture(r_shadow_shadowmapvsdcttexture);
440 r_shadow_shadowmapvsdcttexture = NULL;
445 void r_shadow_start(void)
447 // allocate vertex processing arrays
449 r_shadow_attenuationgradienttexture = NULL;
450 r_shadow_attenuation2dtexture = NULL;
451 r_shadow_attenuation3dtexture = NULL;
452 r_shadow_shadowmode = 0;
453 r_shadow_shadowmaprectangletexture = NULL;
454 r_shadow_shadowmap2dtexture = NULL;
455 memset(r_shadow_shadowmapcubetexture, 0, sizeof(r_shadow_shadowmapcubetexture));
456 r_shadow_shadowmapvsdcttexture = NULL;
457 r_shadow_shadowmapmaxsize = 0;
458 r_shadow_shadowmapsize = 0;
459 r_shadow_shadowmaplod = 0;
460 r_shadow_shadowmapfilterquality = 0;
461 r_shadow_shadowmaptexturetype = 0;
462 r_shadow_shadowmapvsdct = false;
463 r_shadow_shadowmapsampler = false;
464 r_shadow_shadowmappcf = 0;
465 r_shadow_fborectangle = 0;
467 memset(r_shadow_fbocubeside, 0, sizeof(r_shadow_fbocubeside));
469 R_Shadow_FreeShadowMaps();
471 r_shadow_texturepool = NULL;
472 r_shadow_filters_texturepool = NULL;
473 R_Shadow_ValidateCvars();
474 R_Shadow_MakeTextures();
475 maxshadowtriangles = 0;
476 shadowelements = NULL;
477 maxshadowvertices = 0;
478 shadowvertex3f = NULL;
486 shadowmarklist = NULL;
488 r_shadow_buffer_numleafpvsbytes = 0;
489 r_shadow_buffer_visitingleafpvs = NULL;
490 r_shadow_buffer_leafpvs = NULL;
491 r_shadow_buffer_leaflist = NULL;
492 r_shadow_buffer_numsurfacepvsbytes = 0;
493 r_shadow_buffer_surfacepvs = NULL;
494 r_shadow_buffer_surfacelist = NULL;
495 r_shadow_buffer_numshadowtrispvsbytes = 0;
496 r_shadow_buffer_shadowtrispvs = NULL;
497 r_shadow_buffer_numlighttrispvsbytes = 0;
498 r_shadow_buffer_lighttrispvs = NULL;
501 void r_shadow_shutdown(void)
504 R_Shadow_UncompileWorldLights();
506 R_Shadow_FreeShadowMaps();
510 r_shadow_attenuationgradienttexture = NULL;
511 r_shadow_attenuation2dtexture = NULL;
512 r_shadow_attenuation3dtexture = NULL;
513 R_FreeTexturePool(&r_shadow_texturepool);
514 R_FreeTexturePool(&r_shadow_filters_texturepool);
515 maxshadowtriangles = 0;
517 Mem_Free(shadowelements);
518 shadowelements = NULL;
520 Mem_Free(shadowvertex3f);
521 shadowvertex3f = NULL;
524 Mem_Free(vertexupdate);
527 Mem_Free(vertexremap);
533 Mem_Free(shadowmark);
536 Mem_Free(shadowmarklist);
537 shadowmarklist = NULL;
539 r_shadow_buffer_numleafpvsbytes = 0;
540 if (r_shadow_buffer_visitingleafpvs)
541 Mem_Free(r_shadow_buffer_visitingleafpvs);
542 r_shadow_buffer_visitingleafpvs = NULL;
543 if (r_shadow_buffer_leafpvs)
544 Mem_Free(r_shadow_buffer_leafpvs);
545 r_shadow_buffer_leafpvs = NULL;
546 if (r_shadow_buffer_leaflist)
547 Mem_Free(r_shadow_buffer_leaflist);
548 r_shadow_buffer_leaflist = NULL;
549 r_shadow_buffer_numsurfacepvsbytes = 0;
550 if (r_shadow_buffer_surfacepvs)
551 Mem_Free(r_shadow_buffer_surfacepvs);
552 r_shadow_buffer_surfacepvs = NULL;
553 if (r_shadow_buffer_surfacelist)
554 Mem_Free(r_shadow_buffer_surfacelist);
555 r_shadow_buffer_surfacelist = NULL;
556 r_shadow_buffer_numshadowtrispvsbytes = 0;
557 if (r_shadow_buffer_shadowtrispvs)
558 Mem_Free(r_shadow_buffer_shadowtrispvs);
559 r_shadow_buffer_numlighttrispvsbytes = 0;
560 if (r_shadow_buffer_lighttrispvs)
561 Mem_Free(r_shadow_buffer_lighttrispvs);
564 void r_shadow_newmap(void)
566 if (cl.worldmodel && strncmp(cl.worldmodel->name, r_shadow_mapname, sizeof(r_shadow_mapname)))
567 R_Shadow_EditLights_Reload_f();
570 void R_Shadow_Help_f(void)
573 "Documentation on r_shadow system:\n"
575 "r_shadow_bumpscale_basetexture : base texture as bumpmap with this scale\n"
576 "r_shadow_bumpscale_bumpmap : depth scale for bumpmap conversion\n"
577 "r_shadow_debuglight : render only this light number (-1 = all)\n"
578 "r_shadow_gloss 0/1/2 : no gloss, gloss textures only, force gloss\n"
579 "r_shadow_gloss2intensity : brightness of forced gloss\n"
580 "r_shadow_glossintensity : brightness of textured gloss\n"
581 "r_shadow_lightattenuationlinearscale : used to generate attenuation texture\n"
582 "r_shadow_lightattenuationdividebias : used to generate attenuation texture\n"
583 "r_shadow_lightintensityscale : scale rendering brightness of all lights\n"
584 "r_shadow_lightradiusscale : scale rendering radius of all lights\n"
585 "r_shadow_portallight : use portal visibility for static light precomputation\n"
586 "r_shadow_projectdistance : shadow volume projection distance\n"
587 "r_shadow_realtime_dlight : use high quality dynamic lights in normal mode\n"
588 "r_shadow_realtime_dlight_shadows : cast shadows from dlights\n"
589 "r_shadow_realtime_world : use high quality world lighting mode\n"
590 "r_shadow_realtime_world_lightmaps : use lightmaps in addition to lights\n"
591 "r_shadow_realtime_world_shadows : cast shadows from world lights\n"
592 "r_shadow_realtime_world_compile : compile surface/visibility information\n"
593 "r_shadow_realtime_world_compileshadow : compile shadow geometry\n"
594 "r_shadow_scissor : use scissor optimization\n"
595 "r_shadow_polygonfactor : nudge shadow volumes closer/further\n"
596 "r_shadow_polygonoffset : nudge shadow volumes closer/further\n"
597 "r_shadow_texture3d : use 3d attenuation texture (if hardware supports)\n"
598 "r_showlighting : useful for performance testing; bright = slow!\n"
599 "r_showshadowvolumes : useful for performance testing; bright = slow!\n"
601 "r_shadow_help : this help\n"
605 void R_Shadow_Init(void)
607 Cvar_RegisterVariable(&r_shadow_bumpscale_basetexture);
608 Cvar_RegisterVariable(&r_shadow_bumpscale_bumpmap);
609 Cvar_RegisterVariable(&r_shadow_usenormalmap);
610 Cvar_RegisterVariable(&r_shadow_debuglight);
611 Cvar_RegisterVariable(&r_shadow_gloss);
612 Cvar_RegisterVariable(&r_shadow_gloss2intensity);
613 Cvar_RegisterVariable(&r_shadow_glossintensity);
614 Cvar_RegisterVariable(&r_shadow_glossexponent);
615 Cvar_RegisterVariable(&r_shadow_glossexact);
616 Cvar_RegisterVariable(&r_shadow_lightattenuationdividebias);
617 Cvar_RegisterVariable(&r_shadow_lightattenuationlinearscale);
618 Cvar_RegisterVariable(&r_shadow_lightintensityscale);
619 Cvar_RegisterVariable(&r_shadow_lightradiusscale);
620 Cvar_RegisterVariable(&r_shadow_portallight);
621 Cvar_RegisterVariable(&r_shadow_projectdistance);
622 Cvar_RegisterVariable(&r_shadow_frontsidecasting);
623 Cvar_RegisterVariable(&r_shadow_realtime_dlight);
624 Cvar_RegisterVariable(&r_shadow_realtime_dlight_shadows);
625 Cvar_RegisterVariable(&r_shadow_realtime_dlight_svbspculling);
626 Cvar_RegisterVariable(&r_shadow_realtime_dlight_portalculling);
627 Cvar_RegisterVariable(&r_shadow_realtime_world);
628 Cvar_RegisterVariable(&r_shadow_realtime_world_lightmaps);
629 Cvar_RegisterVariable(&r_shadow_realtime_world_shadows);
630 Cvar_RegisterVariable(&r_shadow_realtime_world_compile);
631 Cvar_RegisterVariable(&r_shadow_realtime_world_compileshadow);
632 Cvar_RegisterVariable(&r_shadow_realtime_world_compilesvbsp);
633 Cvar_RegisterVariable(&r_shadow_realtime_world_compileportalculling);
634 Cvar_RegisterVariable(&r_shadow_scissor);
635 Cvar_RegisterVariable(&r_shadow_shadowmapping);
636 Cvar_RegisterVariable(&r_shadow_shadowmapping_vsdct);
637 Cvar_RegisterVariable(&r_shadow_shadowmapping_texturetype);
638 Cvar_RegisterVariable(&r_shadow_shadowmapping_filterquality);
639 Cvar_RegisterVariable(&r_shadow_shadowmapping_maxsize);
640 Cvar_RegisterVariable(&r_shadow_shadowmapping_minsize);
641 Cvar_RegisterVariable(&r_shadow_shadowmapping_lod_bias);
642 Cvar_RegisterVariable(&r_shadow_shadowmapping_lod_scale);
643 Cvar_RegisterVariable(&r_shadow_shadowmapping_bordersize);
644 Cvar_RegisterVariable(&r_shadow_shadowmapping_nearclip);
645 Cvar_RegisterVariable(&r_shadow_shadowmapping_bias);
646 Cvar_RegisterVariable(&r_shadow_culltriangles);
647 Cvar_RegisterVariable(&r_shadow_polygonfactor);
648 Cvar_RegisterVariable(&r_shadow_polygonoffset);
649 Cvar_RegisterVariable(&r_shadow_texture3d);
650 Cvar_RegisterVariable(&r_coronas);
651 Cvar_RegisterVariable(&r_coronas_occlusionsizescale);
652 Cvar_RegisterVariable(&r_coronas_occlusionquery);
653 Cvar_RegisterVariable(&gl_flashblend);
654 Cvar_RegisterVariable(&gl_ext_separatestencil);
655 Cvar_RegisterVariable(&gl_ext_stenciltwoside);
656 if (gamemode == GAME_TENEBRAE)
658 Cvar_SetValue("r_shadow_gloss", 2);
659 Cvar_SetValue("r_shadow_bumpscale_basetexture", 4);
661 Cmd_AddCommand("r_shadow_help", R_Shadow_Help_f, "prints documentation on console commands and variables used by realtime lighting and shadowing system");
662 R_Shadow_EditLights_Init();
663 Mem_ExpandableArray_NewArray(&r_shadow_worldlightsarray, r_main_mempool, sizeof(dlight_t), 128);
664 maxshadowtriangles = 0;
665 shadowelements = NULL;
666 maxshadowvertices = 0;
667 shadowvertex3f = NULL;
675 shadowmarklist = NULL;
677 r_shadow_buffer_numleafpvsbytes = 0;
678 r_shadow_buffer_visitingleafpvs = NULL;
679 r_shadow_buffer_leafpvs = NULL;
680 r_shadow_buffer_leaflist = NULL;
681 r_shadow_buffer_numsurfacepvsbytes = 0;
682 r_shadow_buffer_surfacepvs = NULL;
683 r_shadow_buffer_surfacelist = NULL;
684 r_shadow_buffer_shadowtrispvs = NULL;
685 r_shadow_buffer_lighttrispvs = NULL;
686 R_RegisterModule("R_Shadow", r_shadow_start, r_shadow_shutdown, r_shadow_newmap);
689 matrix4x4_t matrix_attenuationxyz =
692 {0.5, 0.0, 0.0, 0.5},
693 {0.0, 0.5, 0.0, 0.5},
694 {0.0, 0.0, 0.5, 0.5},
699 matrix4x4_t matrix_attenuationz =
702 {0.0, 0.0, 0.5, 0.5},
703 {0.0, 0.0, 0.0, 0.5},
704 {0.0, 0.0, 0.0, 0.5},
709 void R_Shadow_ResizeShadowArrays(int numvertices, int numtriangles)
711 // make sure shadowelements is big enough for this volume
712 if (maxshadowtriangles < numtriangles)
714 maxshadowtriangles = numtriangles;
716 Mem_Free(shadowelements);
717 shadowelements = (int *)Mem_Alloc(r_main_mempool, maxshadowtriangles * sizeof(int[24]));
719 // make sure shadowvertex3f is big enough for this volume
720 if (maxshadowvertices < numvertices)
722 maxshadowvertices = numvertices;
724 Mem_Free(shadowvertex3f);
725 shadowvertex3f = (float *)Mem_Alloc(r_main_mempool, maxshadowvertices * sizeof(float[6]));
729 static void R_Shadow_EnlargeLeafSurfaceTrisBuffer(int numleafs, int numsurfaces, int numshadowtriangles, int numlighttriangles)
731 int numleafpvsbytes = (((numleafs + 7) >> 3) + 255) & ~255;
732 int numsurfacepvsbytes = (((numsurfaces + 7) >> 3) + 255) & ~255;
733 int numshadowtrispvsbytes = (((numshadowtriangles + 7) >> 3) + 255) & ~255;
734 int numlighttrispvsbytes = (((numlighttriangles + 7) >> 3) + 255) & ~255;
735 if (r_shadow_buffer_numleafpvsbytes < numleafpvsbytes)
737 if (r_shadow_buffer_visitingleafpvs)
738 Mem_Free(r_shadow_buffer_visitingleafpvs);
739 if (r_shadow_buffer_leafpvs)
740 Mem_Free(r_shadow_buffer_leafpvs);
741 if (r_shadow_buffer_leaflist)
742 Mem_Free(r_shadow_buffer_leaflist);
743 r_shadow_buffer_numleafpvsbytes = numleafpvsbytes;
744 r_shadow_buffer_visitingleafpvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numleafpvsbytes);
745 r_shadow_buffer_leafpvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numleafpvsbytes);
746 r_shadow_buffer_leaflist = (int *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numleafpvsbytes * 8 * sizeof(*r_shadow_buffer_leaflist));
748 if (r_shadow_buffer_numsurfacepvsbytes < numsurfacepvsbytes)
750 if (r_shadow_buffer_surfacepvs)
751 Mem_Free(r_shadow_buffer_surfacepvs);
752 if (r_shadow_buffer_surfacelist)
753 Mem_Free(r_shadow_buffer_surfacelist);
754 r_shadow_buffer_numsurfacepvsbytes = numsurfacepvsbytes;
755 r_shadow_buffer_surfacepvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numsurfacepvsbytes);
756 r_shadow_buffer_surfacelist = (int *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numsurfacepvsbytes * 8 * sizeof(*r_shadow_buffer_surfacelist));
758 if (r_shadow_buffer_numshadowtrispvsbytes < numshadowtrispvsbytes)
760 if (r_shadow_buffer_shadowtrispvs)
761 Mem_Free(r_shadow_buffer_shadowtrispvs);
762 r_shadow_buffer_numshadowtrispvsbytes = numshadowtrispvsbytes;
763 r_shadow_buffer_shadowtrispvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numshadowtrispvsbytes);
765 if (r_shadow_buffer_numlighttrispvsbytes < numlighttrispvsbytes)
767 if (r_shadow_buffer_lighttrispvs)
768 Mem_Free(r_shadow_buffer_lighttrispvs);
769 r_shadow_buffer_numlighttrispvsbytes = numlighttrispvsbytes;
770 r_shadow_buffer_lighttrispvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numlighttrispvsbytes);
774 void R_Shadow_PrepareShadowMark(int numtris)
776 // make sure shadowmark is big enough for this volume
777 if (maxshadowmark < numtris)
779 maxshadowmark = numtris;
781 Mem_Free(shadowmark);
783 Mem_Free(shadowmarklist);
784 shadowmark = (int *)Mem_Alloc(r_main_mempool, maxshadowmark * sizeof(*shadowmark));
785 shadowmarklist = (int *)Mem_Alloc(r_main_mempool, maxshadowmark * sizeof(*shadowmarklist));
789 // if shadowmarkcount wrapped we clear the array and adjust accordingly
790 if (shadowmarkcount == 0)
793 memset(shadowmark, 0, maxshadowmark * sizeof(*shadowmark));
798 static int R_Shadow_ConstructShadowVolume_ZFail(int innumvertices, int innumtris, const int *inelement3i, const int *inneighbor3i, const float *invertex3f, int *outnumvertices, int *outelement3i, float *outvertex3f, const float *projectorigin, const float *projectdirection, float projectdistance, int numshadowmarktris, const int *shadowmarktris)
801 int outtriangles = 0, outvertices = 0;
804 float ratio, direction[3], projectvector[3];
806 if (projectdirection)
807 VectorScale(projectdirection, projectdistance, projectvector);
809 VectorClear(projectvector);
811 // create the vertices
812 if (projectdirection)
814 for (i = 0;i < numshadowmarktris;i++)
816 element = inelement3i + shadowmarktris[i] * 3;
817 for (j = 0;j < 3;j++)
819 if (vertexupdate[element[j]] != vertexupdatenum)
821 vertexupdate[element[j]] = vertexupdatenum;
822 vertexremap[element[j]] = outvertices;
823 vertex = invertex3f + element[j] * 3;
824 // project one copy of the vertex according to projectvector
825 VectorCopy(vertex, outvertex3f);
826 VectorAdd(vertex, projectvector, (outvertex3f + 3));
835 for (i = 0;i < numshadowmarktris;i++)
837 element = inelement3i + shadowmarktris[i] * 3;
838 for (j = 0;j < 3;j++)
840 if (vertexupdate[element[j]] != vertexupdatenum)
842 vertexupdate[element[j]] = vertexupdatenum;
843 vertexremap[element[j]] = outvertices;
844 vertex = invertex3f + element[j] * 3;
845 // project one copy of the vertex to the sphere radius of the light
846 // (FIXME: would projecting it to the light box be better?)
847 VectorSubtract(vertex, projectorigin, direction);
848 ratio = projectdistance / VectorLength(direction);
849 VectorCopy(vertex, outvertex3f);
850 VectorMA(projectorigin, ratio, direction, (outvertex3f + 3));
858 if (r_shadow_frontsidecasting.integer)
860 for (i = 0;i < numshadowmarktris;i++)
862 int remappedelement[3];
864 const int *neighbortriangle;
866 markindex = shadowmarktris[i] * 3;
867 element = inelement3i + markindex;
868 neighbortriangle = inneighbor3i + markindex;
869 // output the front and back triangles
870 outelement3i[0] = vertexremap[element[0]];
871 outelement3i[1] = vertexremap[element[1]];
872 outelement3i[2] = vertexremap[element[2]];
873 outelement3i[3] = vertexremap[element[2]] + 1;
874 outelement3i[4] = vertexremap[element[1]] + 1;
875 outelement3i[5] = vertexremap[element[0]] + 1;
879 // output the sides (facing outward from this triangle)
880 if (shadowmark[neighbortriangle[0]] != shadowmarkcount)
882 remappedelement[0] = vertexremap[element[0]];
883 remappedelement[1] = vertexremap[element[1]];
884 outelement3i[0] = remappedelement[1];
885 outelement3i[1] = remappedelement[0];
886 outelement3i[2] = remappedelement[0] + 1;
887 outelement3i[3] = remappedelement[1];
888 outelement3i[4] = remappedelement[0] + 1;
889 outelement3i[5] = remappedelement[1] + 1;
894 if (shadowmark[neighbortriangle[1]] != shadowmarkcount)
896 remappedelement[1] = vertexremap[element[1]];
897 remappedelement[2] = vertexremap[element[2]];
898 outelement3i[0] = remappedelement[2];
899 outelement3i[1] = remappedelement[1];
900 outelement3i[2] = remappedelement[1] + 1;
901 outelement3i[3] = remappedelement[2];
902 outelement3i[4] = remappedelement[1] + 1;
903 outelement3i[5] = remappedelement[2] + 1;
908 if (shadowmark[neighbortriangle[2]] != shadowmarkcount)
910 remappedelement[0] = vertexremap[element[0]];
911 remappedelement[2] = vertexremap[element[2]];
912 outelement3i[0] = remappedelement[0];
913 outelement3i[1] = remappedelement[2];
914 outelement3i[2] = remappedelement[2] + 1;
915 outelement3i[3] = remappedelement[0];
916 outelement3i[4] = remappedelement[2] + 1;
917 outelement3i[5] = remappedelement[0] + 1;
926 for (i = 0;i < numshadowmarktris;i++)
928 int remappedelement[3];
930 const int *neighbortriangle;
932 markindex = shadowmarktris[i] * 3;
933 element = inelement3i + markindex;
934 neighbortriangle = inneighbor3i + markindex;
935 // output the front and back triangles
936 outelement3i[0] = vertexremap[element[2]];
937 outelement3i[1] = vertexremap[element[1]];
938 outelement3i[2] = vertexremap[element[0]];
939 outelement3i[3] = vertexremap[element[0]] + 1;
940 outelement3i[4] = vertexremap[element[1]] + 1;
941 outelement3i[5] = vertexremap[element[2]] + 1;
945 // output the sides (facing outward from this triangle)
946 if (shadowmark[neighbortriangle[0]] != shadowmarkcount)
948 remappedelement[0] = vertexremap[element[0]];
949 remappedelement[1] = vertexremap[element[1]];
950 outelement3i[0] = remappedelement[0];
951 outelement3i[1] = remappedelement[1];
952 outelement3i[2] = remappedelement[1] + 1;
953 outelement3i[3] = remappedelement[0];
954 outelement3i[4] = remappedelement[1] + 1;
955 outelement3i[5] = remappedelement[0] + 1;
960 if (shadowmark[neighbortriangle[1]] != shadowmarkcount)
962 remappedelement[1] = vertexremap[element[1]];
963 remappedelement[2] = vertexremap[element[2]];
964 outelement3i[0] = remappedelement[1];
965 outelement3i[1] = remappedelement[2];
966 outelement3i[2] = remappedelement[2] + 1;
967 outelement3i[3] = remappedelement[1];
968 outelement3i[4] = remappedelement[2] + 1;
969 outelement3i[5] = remappedelement[1] + 1;
974 if (shadowmark[neighbortriangle[2]] != shadowmarkcount)
976 remappedelement[0] = vertexremap[element[0]];
977 remappedelement[2] = vertexremap[element[2]];
978 outelement3i[0] = remappedelement[2];
979 outelement3i[1] = remappedelement[0];
980 outelement3i[2] = remappedelement[0] + 1;
981 outelement3i[3] = remappedelement[2];
982 outelement3i[4] = remappedelement[0] + 1;
983 outelement3i[5] = remappedelement[2] + 1;
991 *outnumvertices = outvertices;
995 static int R_Shadow_ConstructShadowVolume_ZPass(int innumvertices, int innumtris, const int *inelement3i, const int *inneighbor3i, const float *invertex3f, int *outnumvertices, int *outelement3i, float *outvertex3f, const float *projectorigin, const float *projectdirection, float projectdistance, int numshadowmarktris, const int *shadowmarktris)
998 int outtriangles = 0, outvertices = 0;
1000 const float *vertex;
1001 float ratio, direction[3], projectvector[3];
1004 if (projectdirection)
1005 VectorScale(projectdirection, projectdistance, projectvector);
1007 VectorClear(projectvector);
1009 for (i = 0;i < numshadowmarktris;i++)
1011 int remappedelement[3];
1013 const int *neighbortriangle;
1015 markindex = shadowmarktris[i] * 3;
1016 neighbortriangle = inneighbor3i + markindex;
1017 side[0] = shadowmark[neighbortriangle[0]] == shadowmarkcount;
1018 side[1] = shadowmark[neighbortriangle[1]] == shadowmarkcount;
1019 side[2] = shadowmark[neighbortriangle[2]] == shadowmarkcount;
1020 if (side[0] + side[1] + side[2] == 0)
1024 element = inelement3i + markindex;
1026 // create the vertices
1027 for (j = 0;j < 3;j++)
1029 if (side[j] + side[j+1] == 0)
1032 if (vertexupdate[k] != vertexupdatenum)
1034 vertexupdate[k] = vertexupdatenum;
1035 vertexremap[k] = outvertices;
1036 vertex = invertex3f + k * 3;
1037 VectorCopy(vertex, outvertex3f);
1038 if (projectdirection)
1040 // project one copy of the vertex according to projectvector
1041 VectorAdd(vertex, projectvector, (outvertex3f + 3));
1045 // project one copy of the vertex to the sphere radius of the light
1046 // (FIXME: would projecting it to the light box be better?)
1047 VectorSubtract(vertex, projectorigin, direction);
1048 ratio = projectdistance / VectorLength(direction);
1049 VectorMA(projectorigin, ratio, direction, (outvertex3f + 3));
1056 // output the sides (facing outward from this triangle)
1059 remappedelement[0] = vertexremap[element[0]];
1060 remappedelement[1] = vertexremap[element[1]];
1061 outelement3i[0] = remappedelement[1];
1062 outelement3i[1] = remappedelement[0];
1063 outelement3i[2] = remappedelement[0] + 1;
1064 outelement3i[3] = remappedelement[1];
1065 outelement3i[4] = remappedelement[0] + 1;
1066 outelement3i[5] = remappedelement[1] + 1;
1073 remappedelement[1] = vertexremap[element[1]];
1074 remappedelement[2] = vertexremap[element[2]];
1075 outelement3i[0] = remappedelement[2];
1076 outelement3i[1] = remappedelement[1];
1077 outelement3i[2] = remappedelement[1] + 1;
1078 outelement3i[3] = remappedelement[2];
1079 outelement3i[4] = remappedelement[1] + 1;
1080 outelement3i[5] = remappedelement[2] + 1;
1087 remappedelement[0] = vertexremap[element[0]];
1088 remappedelement[2] = vertexremap[element[2]];
1089 outelement3i[0] = remappedelement[0];
1090 outelement3i[1] = remappedelement[2];
1091 outelement3i[2] = remappedelement[2] + 1;
1092 outelement3i[3] = remappedelement[0];
1093 outelement3i[4] = remappedelement[2] + 1;
1094 outelement3i[5] = remappedelement[0] + 1;
1101 *outnumvertices = outvertices;
1102 return outtriangles;
1105 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)
1111 if (!BoxesOverlap(lightmins, lightmaxs, surfacemins, surfacemaxs))
1113 tend = firsttriangle + numtris;
1114 if (BoxInsideBox(surfacemins, surfacemaxs, lightmins, lightmaxs))
1116 // surface box entirely inside light box, no box cull
1117 if (projectdirection)
1119 for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
1121 TriangleNormal(invertex3f + e[0] * 3, invertex3f + e[1] * 3, invertex3f + e[2] * 3, normal);
1122 if (r_shadow_frontsidecasting.integer == (DotProduct(normal, projectdirection) < 0))
1123 shadowmarklist[numshadowmark++] = t;
1128 for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
1129 if (r_shadow_frontsidecasting.integer == PointInfrontOfTriangle(projectorigin, invertex3f + e[0] * 3, invertex3f + e[1] * 3, invertex3f + e[2] * 3))
1130 shadowmarklist[numshadowmark++] = t;
1135 // surface box not entirely inside light box, cull each triangle
1136 if (projectdirection)
1138 for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
1140 v[0] = invertex3f + e[0] * 3;
1141 v[1] = invertex3f + e[1] * 3;
1142 v[2] = invertex3f + e[2] * 3;
1143 TriangleNormal(v[0], v[1], v[2], normal);
1144 if (r_shadow_frontsidecasting.integer == (DotProduct(normal, projectdirection) < 0)
1145 && TriangleOverlapsBox(v[0], v[1], v[2], lightmins, lightmaxs))
1146 shadowmarklist[numshadowmark++] = t;
1151 for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
1153 v[0] = invertex3f + e[0] * 3;
1154 v[1] = invertex3f + e[1] * 3;
1155 v[2] = invertex3f + e[2] * 3;
1156 if (r_shadow_frontsidecasting.integer == PointInfrontOfTriangle(projectorigin, v[0], v[1], v[2])
1157 && TriangleOverlapsBox(v[0], v[1], v[2], lightmins, lightmaxs))
1158 shadowmarklist[numshadowmark++] = t;
1164 qboolean R_Shadow_UseZPass(vec3_t mins, vec3_t maxs)
1169 if (r_shadow_compilingrtlight || !r_shadow_frontsidecasting.integer || !r_shadow_usezpassifpossible.integer)
1171 // check if the shadow volume intersects the near plane
1173 // a ray between the eye and light origin may intersect the caster,
1174 // indicating that the shadow may touch the eye location, however we must
1175 // test the near plane (a polygon), not merely the eye location, so it is
1176 // easiest to enlarge the caster bounding shape slightly for this.
1182 void R_Shadow_VolumeFromList(int numverts, int numtris, const float *invertex3f, const int *elements, const int *neighbors, const vec3_t projectorigin, const vec3_t projectdirection, float projectdistance, int nummarktris, const int *marktris, vec3_t trismins, vec3_t trismaxs)
1184 int i, tris, outverts;
1185 if (projectdistance < 0.1)
1187 Con_Printf("R_Shadow_Volume: projectdistance %f\n", projectdistance);
1190 if (!numverts || !nummarktris)
1192 // make sure shadowelements is big enough for this volume
1193 if (maxshadowtriangles < nummarktris || maxshadowvertices < numverts)
1194 R_Shadow_ResizeShadowArrays((numverts + 255) & ~255, (nummarktris + 255) & ~255);
1196 if (maxvertexupdate < numverts)
1198 maxvertexupdate = numverts;
1200 Mem_Free(vertexupdate);
1202 Mem_Free(vertexremap);
1203 vertexupdate = (int *)Mem_Alloc(r_main_mempool, maxvertexupdate * sizeof(int));
1204 vertexremap = (int *)Mem_Alloc(r_main_mempool, maxvertexupdate * sizeof(int));
1205 vertexupdatenum = 0;
1208 if (vertexupdatenum == 0)
1210 vertexupdatenum = 1;
1211 memset(vertexupdate, 0, maxvertexupdate * sizeof(int));
1212 memset(vertexremap, 0, maxvertexupdate * sizeof(int));
1215 for (i = 0;i < nummarktris;i++)
1216 shadowmark[marktris[i]] = shadowmarkcount;
1218 if (r_shadow_compilingrtlight)
1220 // if we're compiling an rtlight, capture the mesh
1221 //tris = R_Shadow_ConstructShadowVolume_ZPass(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
1222 //Mod_ShadowMesh_AddMesh(r_main_mempool, r_shadow_compilingrtlight->static_meshchain_shadow_zpass, NULL, NULL, NULL, shadowvertex3f, NULL, NULL, NULL, NULL, tris, shadowelements);
1223 tris = R_Shadow_ConstructShadowVolume_ZFail(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
1224 Mod_ShadowMesh_AddMesh(r_main_mempool, r_shadow_compilingrtlight->static_meshchain_shadow_zfail, NULL, NULL, NULL, shadowvertex3f, NULL, NULL, NULL, NULL, tris, shadowelements);
1228 // decide which type of shadow to generate and set stencil mode
1229 R_Shadow_RenderMode_StencilShadowVolumes(R_Shadow_UseZPass(trismins, trismaxs));
1230 // generate the sides or a solid volume, depending on type
1231 if (r_shadow_rendermode >= R_SHADOW_RENDERMODE_ZPASS_STENCIL && r_shadow_rendermode <= R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE)
1232 tris = R_Shadow_ConstructShadowVolume_ZPass(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
1234 tris = R_Shadow_ConstructShadowVolume_ZFail(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
1235 r_refdef.stats.lights_dynamicshadowtriangles += tris;
1236 r_refdef.stats.lights_shadowtriangles += tris;
1238 R_Mesh_VertexPointer(shadowvertex3f, 0, 0);
1239 GL_LockArrays(0, outverts);
1240 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZPASS_STENCIL)
1242 // increment stencil if frontface is infront of depthbuffer
1243 GL_CullFace(r_refdef.view.cullface_front);
1244 qglStencilOp(GL_KEEP, GL_KEEP, GL_DECR);CHECKGLERROR
1245 R_Mesh_Draw(0, outverts, 0, tris, shadowelements, NULL, 0, 0);
1246 // decrement stencil if backface is infront of depthbuffer
1247 GL_CullFace(r_refdef.view.cullface_back);
1248 qglStencilOp(GL_KEEP, GL_KEEP, GL_INCR);CHECKGLERROR
1250 else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZFAIL_STENCIL)
1252 // decrement stencil if backface is behind depthbuffer
1253 GL_CullFace(r_refdef.view.cullface_front);
1254 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
1255 R_Mesh_Draw(0, outverts, 0, tris, shadowelements, NULL, 0, 0);
1256 // increment stencil if frontface is behind depthbuffer
1257 GL_CullFace(r_refdef.view.cullface_back);
1258 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR
1260 R_Mesh_Draw(0, outverts, 0, tris, shadowelements, NULL, 0, 0);
1261 GL_LockArrays(0, 0);
1266 void R_Shadow_ShadowMapFromList(int numverts, int numtris, const float *vertex3f, int vertex3f_bufferobject, int vertex3f_bufferoffset, const int *elements, int nummarktris, const int *marktris)
1268 int i, tris = nummarktris;
1271 if (!numverts || !nummarktris)
1273 // make sure shadowelements is big enough for this mesh
1274 if (maxshadowtriangles < nummarktris || maxshadowvertices < numverts)
1275 R_Shadow_ResizeShadowArrays((numverts + 255) & ~255, (nummarktris + 255) & ~255);
1277 // gather up the (sparse) triangles into one array
1278 outelement3i = shadowelements;
1279 for (i = 0;i < nummarktris;i++)
1281 element = elements + marktris[i] * 3;
1282 outelement3i[0] = element[0];
1283 outelement3i[1] = element[1];
1284 outelement3i[2] = element[2];
1288 r_refdef.stats.lights_dynamicshadowtriangles += tris;
1289 r_refdef.stats.lights_shadowtriangles += tris;
1290 R_Mesh_VertexPointer(vertex3f, vertex3f_bufferobject, vertex3f_bufferoffset);
1291 R_Mesh_Draw(0, numverts, 0, tris, shadowelements, NULL, 0, 0);
1294 static void R_Shadow_MakeTextures_MakeCorona(void)
1298 unsigned char pixels[32][32][4];
1299 for (y = 0;y < 32;y++)
1301 dy = (y - 15.5f) * (1.0f / 16.0f);
1302 for (x = 0;x < 32;x++)
1304 dx = (x - 15.5f) * (1.0f / 16.0f);
1305 a = (int)(((1.0f / (dx * dx + dy * dy + 0.2f)) - (1.0f / (1.0f + 0.2))) * 32.0f / (1.0f / (1.0f + 0.2)));
1306 a = bound(0, a, 255);
1307 pixels[y][x][0] = a;
1308 pixels[y][x][1] = a;
1309 pixels[y][x][2] = a;
1310 pixels[y][x][3] = 255;
1313 r_shadow_lightcorona = R_LoadTexture2D(r_shadow_texturepool, "lightcorona", 32, 32, &pixels[0][0][0], TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_FORCELINEAR, NULL);
1316 static unsigned int R_Shadow_MakeTextures_SamplePoint(float x, float y, float z)
1318 float dist = sqrt(x*x+y*y+z*z);
1319 float intensity = dist < 1 ? ((1.0f - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist)) : 0;
1320 // note this code could suffer byte order issues except that it is multiplying by an integer that reads the same both ways
1321 return (unsigned char)bound(0, intensity * 256.0f, 255) * 0x01010101;
1324 static void R_Shadow_MakeTextures(void)
1327 float intensity, dist;
1329 R_FreeTexturePool(&r_shadow_texturepool);
1330 r_shadow_texturepool = R_AllocTexturePool();
1331 r_shadow_attenlinearscale = r_shadow_lightattenuationlinearscale.value;
1332 r_shadow_attendividebias = r_shadow_lightattenuationdividebias.value;
1333 data = (unsigned int *)Mem_Alloc(tempmempool, max(max(ATTEN3DSIZE*ATTEN3DSIZE*ATTEN3DSIZE, ATTEN2DSIZE*ATTEN2DSIZE), ATTEN1DSIZE) * 4);
1334 // the table includes one additional value to avoid the need to clamp indexing due to minor math errors
1335 for (x = 0;x <= ATTENTABLESIZE;x++)
1337 dist = (x + 0.5f) * (1.0f / ATTENTABLESIZE) * (1.0f / 0.9375);
1338 intensity = dist < 1 ? ((1.0f - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist)) : 0;
1339 r_shadow_attentable[x] = bound(0, intensity, 1);
1341 // 1D gradient texture
1342 for (x = 0;x < ATTEN1DSIZE;x++)
1343 data[x] = R_Shadow_MakeTextures_SamplePoint((x + 0.5f) * (1.0f / ATTEN1DSIZE) * (1.0f / 0.9375), 0, 0);
1344 r_shadow_attenuationgradienttexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation1d", ATTEN1DSIZE, 1, (unsigned char *)data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCELINEAR, NULL);
1345 // 2D circle texture
1346 for (y = 0;y < ATTEN2DSIZE;y++)
1347 for (x = 0;x < ATTEN2DSIZE;x++)
1348 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);
1349 r_shadow_attenuation2dtexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation2d", ATTEN2DSIZE, ATTEN2DSIZE, (unsigned char *)data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCELINEAR, NULL);
1350 // 3D sphere texture
1351 if (r_shadow_texture3d.integer && gl_texture3d)
1353 for (z = 0;z < ATTEN3DSIZE;z++)
1354 for (y = 0;y < ATTEN3DSIZE;y++)
1355 for (x = 0;x < ATTEN3DSIZE;x++)
1356 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));
1357 r_shadow_attenuation3dtexture = R_LoadTexture3D(r_shadow_texturepool, "attenuation3d", ATTEN3DSIZE, ATTEN3DSIZE, ATTEN3DSIZE, (unsigned char *)data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCELINEAR, NULL);
1360 r_shadow_attenuation3dtexture = NULL;
1363 R_Shadow_MakeTextures_MakeCorona();
1365 // Editor light sprites
1366 r_editlights_sprcursor = Draw_CachePic ("gfx/editlights/cursor");
1367 r_editlights_sprlight = Draw_CachePic ("gfx/editlights/light");
1368 r_editlights_sprnoshadowlight = Draw_CachePic ("gfx/editlights/noshadow");
1369 r_editlights_sprcubemaplight = Draw_CachePic ("gfx/editlights/cubemaplight");
1370 r_editlights_sprcubemapnoshadowlight = Draw_CachePic ("gfx/editlights/cubemapnoshadowlight");
1371 r_editlights_sprselection = Draw_CachePic ("gfx/editlights/selection");
1374 void R_Shadow_ValidateCvars(void)
1376 if (r_shadow_texture3d.integer && !gl_texture3d)
1377 Cvar_SetValueQuick(&r_shadow_texture3d, 0);
1378 if (gl_ext_separatestencil.integer && !gl_support_separatestencil)
1379 Cvar_SetValueQuick(&gl_ext_separatestencil, 0);
1380 if (gl_ext_stenciltwoside.integer && !gl_support_stenciltwoside)
1381 Cvar_SetValueQuick(&gl_ext_stenciltwoside, 0);
1384 void R_Shadow_RenderMode_Begin(void)
1388 R_Shadow_ValidateCvars();
1390 if (!r_shadow_attenuation2dtexture
1391 || (!r_shadow_attenuation3dtexture && r_shadow_texture3d.integer)
1392 || r_shadow_lightattenuationdividebias.value != r_shadow_attendividebias
1393 || r_shadow_lightattenuationlinearscale.value != r_shadow_attenlinearscale)
1394 R_Shadow_MakeTextures();
1397 R_Mesh_ColorPointer(NULL, 0, 0);
1398 R_Mesh_ResetTextureState();
1399 GL_BlendFunc(GL_ONE, GL_ZERO);
1400 GL_DepthRange(0, 1);
1401 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
1403 GL_DepthMask(false);
1404 GL_Color(0, 0, 0, 1);
1405 GL_Scissor(r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
1407 r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
1409 if (gl_ext_separatestencil.integer)
1411 r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_SEPARATESTENCIL;
1412 r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_SEPARATESTENCIL;
1414 else if (gl_ext_stenciltwoside.integer)
1416 r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE;
1417 r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE;
1421 r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_STENCIL;
1422 r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_STENCIL;
1425 if (r_glsl.integer && gl_support_fragment_shader)
1426 r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_GLSL;
1427 else if (gl_dot3arb && gl_texturecubemap && r_textureunits.integer >= 2 && gl_combine.integer && gl_stencil)
1428 r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_DOT3;
1430 r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_VERTEX;
1433 qglGetIntegerv(GL_DRAW_BUFFER, &drawbuffer);CHECKGLERROR
1434 qglGetIntegerv(GL_READ_BUFFER, &readbuffer);CHECKGLERROR
1435 r_shadow_drawbuffer = drawbuffer;
1436 r_shadow_readbuffer = readbuffer;
1439 void R_Shadow_RenderMode_ActiveLight(const rtlight_t *rtlight)
1441 rsurface.rtlight = rtlight;
1444 void R_Shadow_RenderMode_Reset(void)
1447 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE || r_shadow_rendermode == R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE)
1449 qglDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
1451 if (gl_support_ext_framebuffer_object)
1453 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);CHECKGLERROR
1455 qglDrawBuffer(r_shadow_drawbuffer);CHECKGLERROR
1456 qglReadBuffer(r_shadow_readbuffer);CHECKGLERROR
1457 R_SetViewport(&r_refdef.view.viewport);
1458 GL_Scissor(r_shadow_lightscissor[0], r_shadow_lightscissor[1], r_shadow_lightscissor[2], r_shadow_lightscissor[3]);
1459 R_Mesh_ColorPointer(NULL, 0, 0);
1460 R_Mesh_ResetTextureState();
1461 GL_DepthRange(0, 1);
1463 GL_DepthMask(false);
1464 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
1465 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);CHECKGLERROR
1466 qglDisable(GL_STENCIL_TEST);CHECKGLERROR
1467 qglStencilMask(~0);CHECKGLERROR
1468 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);CHECKGLERROR
1469 qglStencilFunc(GL_ALWAYS, 128, ~0);CHECKGLERROR
1470 GL_CullFace(r_refdef.view.cullface_back);
1471 GL_Color(1, 1, 1, 1);
1472 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
1473 GL_BlendFunc(GL_ONE, GL_ZERO);
1474 R_SetupGenericShader(false);
1475 r_shadow_usingshadowmaprect = false;
1476 r_shadow_usingshadowmapcube = false;
1477 r_shadow_usingshadowmap2d = false;
1481 void R_Shadow_ClearStencil(void)
1484 GL_Clear(GL_STENCIL_BUFFER_BIT);
1485 r_refdef.stats.lights_clears++;
1488 void R_Shadow_RenderMode_StencilShadowVolumes(qboolean zpass)
1490 r_shadow_rendermode_t mode = zpass ? r_shadow_shadowingrendermode_zpass : r_shadow_shadowingrendermode_zfail;
1491 if (r_shadow_rendermode == mode)
1494 R_Shadow_RenderMode_Reset();
1495 GL_ColorMask(0, 0, 0, 0);
1496 GL_PolygonOffset(r_refdef.shadowpolygonfactor, r_refdef.shadowpolygonoffset);CHECKGLERROR
1497 R_SetupDepthOrShadowShader();
1498 qglDepthFunc(GL_LESS);CHECKGLERROR
1499 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
1500 r_shadow_rendermode = mode;
1505 case R_SHADOW_RENDERMODE_ZPASS_SEPARATESTENCIL:
1506 GL_CullFace(GL_NONE);
1507 qglStencilOpSeparate(r_refdef.view.cullface_front, GL_KEEP, GL_KEEP, GL_INCR);CHECKGLERROR
1508 qglStencilOpSeparate(r_refdef.view.cullface_back, GL_KEEP, GL_KEEP, GL_DECR);CHECKGLERROR
1510 case R_SHADOW_RENDERMODE_ZFAIL_SEPARATESTENCIL:
1511 GL_CullFace(GL_NONE);
1512 qglStencilOpSeparate(r_refdef.view.cullface_front, GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR
1513 qglStencilOpSeparate(r_refdef.view.cullface_back, GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
1515 case R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE:
1516 GL_CullFace(GL_NONE);
1517 qglEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
1518 qglActiveStencilFaceEXT(r_refdef.view.cullface_front);CHECKGLERROR
1519 qglStencilMask(~0);CHECKGLERROR
1520 qglStencilOp(GL_KEEP, GL_KEEP, GL_INCR);CHECKGLERROR
1521 qglActiveStencilFaceEXT(r_refdef.view.cullface_back);CHECKGLERROR
1522 qglStencilMask(~0);CHECKGLERROR
1523 qglStencilOp(GL_KEEP, GL_KEEP, GL_DECR);CHECKGLERROR
1525 case R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE:
1526 GL_CullFace(GL_NONE);
1527 qglEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
1528 qglActiveStencilFaceEXT(r_refdef.view.cullface_front);CHECKGLERROR
1529 qglStencilMask(~0);CHECKGLERROR
1530 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR
1531 qglActiveStencilFaceEXT(r_refdef.view.cullface_back);CHECKGLERROR
1532 qglStencilMask(~0);CHECKGLERROR
1533 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
1538 static void R_Shadow_MakeVSDCT(void)
1540 // maps to a 2x3 texture rectangle with normalized coordinates
1545 // stores abs(dir.xy), offset.xy/2.5
1546 unsigned char data[4*6] =
1548 255, 0, 0x33, 0x33, // +X: <1, 0>, <0.5, 0.5>
1549 255, 0, 0x99, 0x33, // -X: <1, 0>, <1.5, 0.5>
1550 0, 255, 0x33, 0x99, // +Y: <0, 1>, <0.5, 1.5>
1551 0, 255, 0x99, 0x99, // -Y: <0, 1>, <1.5, 1.5>
1552 0, 0, 0x33, 0xFF, // +Z: <0, 0>, <0.5, 2.5>
1553 0, 0, 0x99, 0xFF, // -Z: <0, 0>, <1.5, 2.5>
1555 r_shadow_shadowmapvsdcttexture = R_LoadTextureCubeMap(r_shadow_texturepool, "shadowmapvsdct", 1, data, TEXTYPE_RGBA, TEXF_ALWAYSPRECACHE | TEXF_FORCELINEAR | TEXF_CLAMP | TEXF_ALPHA, NULL);
1558 void R_Shadow_RenderMode_ShadowMap(int side, qboolean clear, int size)
1563 float nearclip, farclip, bias;
1564 r_viewport_t viewport;
1566 maxsize = r_shadow_shadowmapmaxsize;
1567 nearclip = r_shadow_shadowmapping_nearclip.value / rsurface.rtlight->radius;
1569 bias = r_shadow_shadowmapping_bias.value * nearclip * (1024.0f / size);// * rsurface.rtlight->radius;
1570 r_shadow_shadowmap_parameters[2] = 0.5f + 0.5f * (farclip + nearclip) / (farclip - nearclip);
1571 r_shadow_shadowmap_parameters[3] = -nearclip * farclip / (farclip - nearclip) - 0.5f * bias;
1572 if (r_shadow_shadowmode == 1)
1574 // complex unrolled cube approach (more flexible)
1575 if (r_shadow_shadowmapvsdct && !r_shadow_shadowmapvsdcttexture)
1576 R_Shadow_MakeVSDCT();
1577 if (!r_shadow_shadowmap2dtexture)
1580 int w = maxsize*2, h = gl_support_arb_texture_non_power_of_two ? maxsize*3 : maxsize*4;
1581 r_shadow_shadowmap2dtexture = R_LoadTextureShadowMap2D(r_shadow_texturepool, "shadowmap", w, h, r_shadow_shadowmapsampler);
1582 qglGenFramebuffersEXT(1, &r_shadow_fbo2d);CHECKGLERROR
1583 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r_shadow_fbo2d);CHECKGLERROR
1584 qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, R_GetTexture(r_shadow_shadowmap2dtexture), 0);CHECKGLERROR
1588 R_Shadow_RenderMode_Reset();
1589 if (r_shadow_shadowmap2dtexture)
1591 // render depth into the fbo, do not render color at all
1592 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r_shadow_fbo2d);CHECKGLERROR
1593 qglDrawBuffer(GL_NONE);CHECKGLERROR
1594 qglReadBuffer(GL_NONE);CHECKGLERROR
1595 status = qglCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);CHECKGLERROR
1596 if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
1598 Con_Printf("R_Shadow_RenderMode_ShadowMap: glCheckFramebufferStatusEXT returned %i\n", status);
1599 Cvar_SetValueQuick(&r_shadow_shadowmapping, 0);
1601 R_SetupDepthOrShadowShader();
1605 R_SetupShowDepthShader();
1606 qglClearColor(1,1,1,1);CHECKGLERROR
1608 R_Viewport_InitRectSideView(&viewport, &rsurface.rtlight->matrix_lighttoworld, side, size, r_shadow_shadowmapborder, nearclip, farclip, NULL);
1609 r_shadow_shadowmap_texturescale[0] = 1.0f / R_TextureWidth(r_shadow_shadowmap2dtexture);
1610 r_shadow_shadowmap_texturescale[1] = 1.0f / R_TextureHeight(r_shadow_shadowmap2dtexture);
1611 r_shadow_shadowmap_parameters[0] = 0.5f * (size - r_shadow_shadowmapborder);
1612 r_shadow_shadowmap_parameters[1] = r_shadow_shadowmapvsdct ? 2.5f*size : size;
1613 r_shadow_rendermode = R_SHADOW_RENDERMODE_SHADOWMAP2D;
1615 else if (r_shadow_shadowmode == 2)
1617 // complex unrolled cube approach (more flexible)
1618 if (r_shadow_shadowmapvsdct && !r_shadow_shadowmapvsdcttexture)
1619 R_Shadow_MakeVSDCT();
1620 if (!r_shadow_shadowmaprectangletexture)
1623 r_shadow_shadowmaprectangletexture = R_LoadTextureShadowMapRectangle(r_shadow_texturepool, "shadowmap", maxsize*2, maxsize*3, r_shadow_shadowmapsampler);
1624 qglGenFramebuffersEXT(1, &r_shadow_fborectangle);CHECKGLERROR
1625 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r_shadow_fborectangle);CHECKGLERROR
1626 qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_RECTANGLE_ARB, R_GetTexture(r_shadow_shadowmaprectangletexture), 0);CHECKGLERROR
1630 R_Shadow_RenderMode_Reset();
1631 if (r_shadow_shadowmaprectangletexture)
1633 // render depth into the fbo, do not render color at all
1634 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r_shadow_fborectangle);CHECKGLERROR
1635 qglDrawBuffer(GL_NONE);CHECKGLERROR
1636 qglReadBuffer(GL_NONE);CHECKGLERROR
1637 status = qglCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);CHECKGLERROR
1638 if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
1640 Con_Printf("R_Shadow_RenderMode_ShadowMap: glCheckFramebufferStatusEXT returned %i\n", status);
1641 Cvar_SetValueQuick(&r_shadow_shadowmapping, 0);
1643 R_SetupDepthOrShadowShader();
1647 R_SetupShowDepthShader();
1648 qglClearColor(1,1,1,1);CHECKGLERROR
1650 R_Viewport_InitRectSideView(&viewport, &rsurface.rtlight->matrix_lighttoworld, side, size, r_shadow_shadowmapborder, nearclip, farclip, NULL);
1651 r_shadow_shadowmap_texturescale[0] = 1.0f;
1652 r_shadow_shadowmap_texturescale[1] = 1.0f;
1653 r_shadow_shadowmap_parameters[0] = 0.5f * (size - r_shadow_shadowmapborder);
1654 r_shadow_shadowmap_parameters[1] = r_shadow_shadowmapvsdct ? 2.5f*size : size;
1655 r_shadow_rendermode = R_SHADOW_RENDERMODE_SHADOWMAPRECTANGLE;
1657 else if (r_shadow_shadowmode == 3)
1659 // simple cube approach
1660 if (!r_shadow_shadowmapcubetexture[r_shadow_shadowmaplod])
1663 r_shadow_shadowmapcubetexture[r_shadow_shadowmaplod] = R_LoadTextureShadowMapCube(r_shadow_texturepool, "shadowmapcube", bound(1, maxsize >> r_shadow_shadowmaplod, 2048), r_shadow_shadowmapsampler);
1664 qglGenFramebuffersEXT(6, r_shadow_fbocubeside[r_shadow_shadowmaplod]);CHECKGLERROR
1665 for (i = 0;i < 6;i++)
1667 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r_shadow_fbocubeside[r_shadow_shadowmaplod][i]);CHECKGLERROR
1668 qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + i, R_GetTexture(r_shadow_shadowmapcubetexture[r_shadow_shadowmaplod]), 0);CHECKGLERROR
1673 R_Shadow_RenderMode_Reset();
1674 if (r_shadow_shadowmapcubetexture[r_shadow_shadowmaplod])
1676 // render depth into the fbo, do not render color at all
1677 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r_shadow_fbocubeside[r_shadow_shadowmaplod][side]);CHECKGLERROR
1678 qglDrawBuffer(GL_NONE);CHECKGLERROR
1679 qglReadBuffer(GL_NONE);CHECKGLERROR
1680 status = qglCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);CHECKGLERROR
1681 if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
1683 Con_Printf("R_Shadow_RenderMode_ShadowMap: glCheckFramebufferStatusEXT returned %i\n", status);
1684 Cvar_SetValueQuick(&r_shadow_shadowmapping, 0);
1686 R_SetupDepthOrShadowShader();
1690 R_SetupShowDepthShader();
1691 qglClearColor(1,1,1,1);CHECKGLERROR
1693 R_Viewport_InitCubeSideView(&viewport, &rsurface.rtlight->matrix_lighttoworld, side, size, nearclip, farclip, NULL);
1694 r_shadow_shadowmap_texturescale[0] = 1.0f / R_TextureWidth(r_shadow_shadowmapcubetexture[r_shadow_shadowmaplod]);
1695 r_shadow_shadowmap_texturescale[1] = 1.0f / R_TextureWidth(r_shadow_shadowmapcubetexture[r_shadow_shadowmaplod]);
1696 r_shadow_shadowmap_parameters[0] = 1.0f;
1697 r_shadow_shadowmap_parameters[1] = 1.0f;
1698 r_shadow_rendermode = R_SHADOW_RENDERMODE_SHADOWMAPCUBESIDE;
1700 r_shadow_shadowmap_texturescale[2] = 1.0f / r_shadow_shadowmap_texturescale[0];
1701 r_shadow_shadowmap_texturescale[3] = 1.0f / r_shadow_shadowmap_texturescale[1];
1703 R_SetViewport(&viewport);
1704 GL_PolygonOffset(0, 0);
1705 GL_CullFace(GL_NONE); // quake is backwards
1706 GL_Scissor(viewport.x, viewport.y, viewport.width, viewport.height);
1709 qglClearDepth(1);CHECKGLERROR
1712 qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
1716 void R_Shadow_RenderMode_Lighting(qboolean stenciltest, qboolean transparent, qboolean shadowmapping)
1719 R_Shadow_RenderMode_Reset();
1720 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1723 qglDepthFunc(GL_EQUAL);CHECKGLERROR
1727 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
1728 // only draw light where this geometry was already rendered AND the
1729 // stencil is 128 (values other than this mean shadow)
1730 qglStencilFunc(GL_EQUAL, 128, ~0);CHECKGLERROR
1732 r_shadow_rendermode = r_shadow_lightingrendermode;
1733 // do global setup needed for the chosen lighting mode
1734 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_LIGHT_GLSL)
1736 R_Mesh_TexBindCubeMap(GL20TU_CUBE, R_GetTexture(rsurface.rtlight->currentcubemap)); // light filter
1737 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 0);
1741 if (r_shadow_shadowmode == 1)
1743 r_shadow_usingshadowmap2d = true;
1744 R_Mesh_TexBind(GL20TU_SHADOWMAP2D, R_GetTexture(r_shadow_shadowmap2dtexture));
1747 else if (r_shadow_shadowmode == 2)
1749 r_shadow_usingshadowmaprect = true;
1750 R_Mesh_TexBindRectangle(GL20TU_SHADOWMAPRECT, R_GetTexture(r_shadow_shadowmaprectangletexture));
1753 else if (r_shadow_shadowmode == 3)
1755 r_shadow_usingshadowmapcube = true;
1756 R_Mesh_TexBindCubeMap(GL20TU_SHADOWMAPCUBE, R_GetTexture(r_shadow_shadowmapcubetexture[r_shadow_shadowmaplod]));
1760 if (r_shadow_shadowmapvsdct && (r_shadow_usingshadowmap2d || r_shadow_usingshadowmaprect))
1762 R_Mesh_TexBindCubeMap(GL20TU_CUBEPROJECTION, R_GetTexture(r_shadow_shadowmapvsdcttexture));
1767 else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_LIGHT_VERTEX)
1768 R_Mesh_ColorPointer(rsurface.array_color4f, 0, 0);
1769 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1773 void R_Shadow_RenderMode_VisibleShadowVolumes(void)
1776 R_Shadow_RenderMode_Reset();
1777 GL_BlendFunc(GL_ONE, GL_ONE);
1778 GL_DepthRange(0, 1);
1779 GL_DepthTest(r_showshadowvolumes.integer < 2);
1780 GL_Color(0.0, 0.0125 * r_refdef.view.colorscale, 0.1 * r_refdef.view.colorscale, 1);
1781 GL_PolygonOffset(r_refdef.shadowpolygonfactor, r_refdef.shadowpolygonoffset);CHECKGLERROR
1782 GL_CullFace(GL_NONE);
1783 r_shadow_rendermode = R_SHADOW_RENDERMODE_VISIBLEVOLUMES;
1786 void R_Shadow_RenderMode_VisibleLighting(qboolean stenciltest, qboolean transparent)
1789 R_Shadow_RenderMode_Reset();
1790 GL_BlendFunc(GL_ONE, GL_ONE);
1791 GL_DepthRange(0, 1);
1792 GL_DepthTest(r_showlighting.integer < 2);
1793 GL_Color(0.1 * r_refdef.view.colorscale, 0.0125 * r_refdef.view.colorscale, 0, 1);
1796 qglDepthFunc(GL_EQUAL);CHECKGLERROR
1800 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
1801 qglStencilFunc(GL_EQUAL, 128, ~0);CHECKGLERROR
1803 r_shadow_rendermode = R_SHADOW_RENDERMODE_VISIBLELIGHTING;
1806 void R_Shadow_RenderMode_End(void)
1809 R_Shadow_RenderMode_Reset();
1810 R_Shadow_RenderMode_ActiveLight(NULL);
1812 GL_Scissor(r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
1813 r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
1816 int bboxedges[12][2] =
1835 qboolean R_Shadow_ScissorForBBox(const float *mins, const float *maxs)
1837 int i, ix1, iy1, ix2, iy2;
1838 float x1, y1, x2, y2;
1840 float vertex[20][3];
1849 r_shadow_lightscissor[0] = r_refdef.view.viewport.x;
1850 r_shadow_lightscissor[1] = r_refdef.view.viewport.y;
1851 r_shadow_lightscissor[2] = r_refdef.view.viewport.width;
1852 r_shadow_lightscissor[3] = r_refdef.view.viewport.height;
1854 if (!r_shadow_scissor.integer)
1857 // if view is inside the light box, just say yes it's visible
1858 if (BoxesOverlap(r_refdef.view.origin, r_refdef.view.origin, mins, maxs))
1861 x1 = y1 = x2 = y2 = 0;
1863 // transform all corners that are infront of the nearclip plane
1864 VectorNegate(r_refdef.view.frustum[4].normal, plane4f);
1865 plane4f[3] = r_refdef.view.frustum[4].dist;
1867 for (i = 0;i < 8;i++)
1869 Vector4Set(corner[i], (i & 1) ? maxs[0] : mins[0], (i & 2) ? maxs[1] : mins[1], (i & 4) ? maxs[2] : mins[2], 1);
1870 dist[i] = DotProduct4(corner[i], plane4f);
1871 sign[i] = dist[i] > 0;
1874 VectorCopy(corner[i], vertex[numvertices]);
1878 // if some points are behind the nearclip, add clipped edge points to make
1879 // sure that the scissor boundary is complete
1880 if (numvertices > 0 && numvertices < 8)
1882 // add clipped edge points
1883 for (i = 0;i < 12;i++)
1885 j = bboxedges[i][0];
1886 k = bboxedges[i][1];
1887 if (sign[j] != sign[k])
1889 f = dist[j] / (dist[j] - dist[k]);
1890 VectorLerp(corner[j], f, corner[k], vertex[numvertices]);
1896 // if we have no points to check, the light is behind the view plane
1900 // if we have some points to transform, check what screen area is covered
1901 x1 = y1 = x2 = y2 = 0;
1903 //Con_Printf("%i vertices to transform...\n", numvertices);
1904 for (i = 0;i < numvertices;i++)
1906 VectorCopy(vertex[i], v);
1907 R_Viewport_TransformToScreen(&r_refdef.view.viewport, v, v2);
1908 //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]);
1911 if (x1 > v2[0]) x1 = v2[0];
1912 if (x2 < v2[0]) x2 = v2[0];
1913 if (y1 > v2[1]) y1 = v2[1];
1914 if (y2 < v2[1]) y2 = v2[1];
1923 // now convert the scissor rectangle to integer screen coordinates
1924 ix1 = (int)(x1 - 1.0f);
1925 iy1 = vid.height - (int)(y2 - 1.0f);
1926 ix2 = (int)(x2 + 1.0f);
1927 iy2 = vid.height - (int)(y1 + 1.0f);
1928 //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
1930 // clamp it to the screen
1931 if (ix1 < r_refdef.view.viewport.x) ix1 = r_refdef.view.viewport.x;
1932 if (iy1 < r_refdef.view.viewport.y) iy1 = r_refdef.view.viewport.y;
1933 if (ix2 > r_refdef.view.viewport.x + r_refdef.view.viewport.width) ix2 = r_refdef.view.viewport.x + r_refdef.view.viewport.width;
1934 if (iy2 > r_refdef.view.viewport.y + r_refdef.view.viewport.height) iy2 = r_refdef.view.viewport.y + r_refdef.view.viewport.height;
1936 // if it is inside out, it's not visible
1937 if (ix2 <= ix1 || iy2 <= iy1)
1940 // the light area is visible, set up the scissor rectangle
1941 r_shadow_lightscissor[0] = ix1;
1942 r_shadow_lightscissor[1] = iy1;
1943 r_shadow_lightscissor[2] = ix2 - ix1;
1944 r_shadow_lightscissor[3] = iy2 - iy1;
1946 r_refdef.stats.lights_scissored++;
1950 static void R_Shadow_RenderLighting_Light_Vertex_Shading(int firstvertex, int numverts, int numtriangles, const int *element3i, const float *diffusecolor, const float *ambientcolor)
1952 float *vertex3f = rsurface.vertex3f + 3 * firstvertex;
1953 float *normal3f = rsurface.normal3f + 3 * firstvertex;
1954 float *color4f = rsurface.array_color4f + 4 * firstvertex;
1955 float dist, dot, distintensity, shadeintensity, v[3], n[3];
1956 if (r_textureunits.integer >= 3)
1958 if (VectorLength2(diffusecolor) > 0)
1960 for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
1962 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
1963 Matrix4x4_Transform3x3(&rsurface.entitytolight, normal3f, n);
1964 if ((dot = DotProduct(n, v)) < 0)
1966 shadeintensity = -dot / sqrt(VectorLength2(v) * VectorLength2(n));
1967 VectorMA(ambientcolor, shadeintensity, diffusecolor, color4f);
1970 VectorCopy(ambientcolor, color4f);
1971 if (r_refdef.fogenabled)
1974 f = FogPoint_Model(vertex3f);
1975 VectorScale(color4f, f, color4f);
1982 for (;numverts > 0;numverts--, vertex3f += 3, color4f += 4)
1984 VectorCopy(ambientcolor, color4f);
1985 if (r_refdef.fogenabled)
1988 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
1989 f = FogPoint_Model(vertex3f);
1990 VectorScale(color4f, f, color4f);
1996 else if (r_textureunits.integer >= 2)
1998 if (VectorLength2(diffusecolor) > 0)
2000 for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
2002 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
2003 if ((dist = fabs(v[2])) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
2005 Matrix4x4_Transform3x3(&rsurface.entitytolight, normal3f, n);
2006 if ((dot = DotProduct(n, v)) < 0)
2008 shadeintensity = -dot / sqrt(VectorLength2(v) * VectorLength2(n));
2009 color4f[0] = (ambientcolor[0] + shadeintensity * diffusecolor[0]) * distintensity;
2010 color4f[1] = (ambientcolor[1] + shadeintensity * diffusecolor[1]) * distintensity;
2011 color4f[2] = (ambientcolor[2] + shadeintensity * diffusecolor[2]) * distintensity;
2015 color4f[0] = ambientcolor[0] * distintensity;
2016 color4f[1] = ambientcolor[1] * distintensity;
2017 color4f[2] = ambientcolor[2] * distintensity;
2019 if (r_refdef.fogenabled)
2022 f = FogPoint_Model(vertex3f);
2023 VectorScale(color4f, f, color4f);
2027 VectorClear(color4f);
2033 for (;numverts > 0;numverts--, vertex3f += 3, color4f += 4)
2035 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
2036 if ((dist = fabs(v[2])) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
2038 color4f[0] = ambientcolor[0] * distintensity;
2039 color4f[1] = ambientcolor[1] * distintensity;
2040 color4f[2] = ambientcolor[2] * distintensity;
2041 if (r_refdef.fogenabled)
2044 f = FogPoint_Model(vertex3f);
2045 VectorScale(color4f, f, color4f);
2049 VectorClear(color4f);
2056 if (VectorLength2(diffusecolor) > 0)
2058 for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
2060 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
2061 if ((dist = VectorLength(v)) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
2063 distintensity = (1 - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist);
2064 Matrix4x4_Transform3x3(&rsurface.entitytolight, normal3f, n);
2065 if ((dot = DotProduct(n, v)) < 0)
2067 shadeintensity = -dot / sqrt(VectorLength2(v) * VectorLength2(n));
2068 color4f[0] = (ambientcolor[0] + shadeintensity * diffusecolor[0]) * distintensity;
2069 color4f[1] = (ambientcolor[1] + shadeintensity * diffusecolor[1]) * distintensity;
2070 color4f[2] = (ambientcolor[2] + shadeintensity * diffusecolor[2]) * distintensity;
2074 color4f[0] = ambientcolor[0] * distintensity;
2075 color4f[1] = ambientcolor[1] * distintensity;
2076 color4f[2] = ambientcolor[2] * distintensity;
2078 if (r_refdef.fogenabled)
2081 f = FogPoint_Model(vertex3f);
2082 VectorScale(color4f, f, color4f);
2086 VectorClear(color4f);
2092 for (;numverts > 0;numverts--, vertex3f += 3, color4f += 4)
2094 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
2095 if ((dist = VectorLength(v)) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
2097 distintensity = (1 - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist);
2098 color4f[0] = ambientcolor[0] * distintensity;
2099 color4f[1] = ambientcolor[1] * distintensity;
2100 color4f[2] = ambientcolor[2] * distintensity;
2101 if (r_refdef.fogenabled)
2104 f = FogPoint_Model(vertex3f);
2105 VectorScale(color4f, f, color4f);
2109 VectorClear(color4f);
2116 // TODO: use glTexGen instead of feeding vertices to texcoordpointer?
2118 static void R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(int firstvertex, int numvertices, int numtriangles, const int *element3i)
2121 float *out3f = rsurface.array_texcoord3f + 3 * firstvertex;
2122 const float *vertex3f = rsurface.vertex3f + 3 * firstvertex;
2123 const float *svector3f = rsurface.svector3f + 3 * firstvertex;
2124 const float *tvector3f = rsurface.tvector3f + 3 * firstvertex;
2125 const float *normal3f = rsurface.normal3f + 3 * firstvertex;
2127 for (i = 0;i < numvertices;i++, vertex3f += 3, svector3f += 3, tvector3f += 3, normal3f += 3, out3f += 3)
2129 VectorSubtract(rsurface.entitylightorigin, vertex3f, lightdir);
2130 // the cubemap normalizes this for us
2131 out3f[0] = DotProduct(svector3f, lightdir);
2132 out3f[1] = DotProduct(tvector3f, lightdir);
2133 out3f[2] = DotProduct(normal3f, lightdir);
2137 static void R_Shadow_GenTexCoords_Specular_NormalCubeMap(int firstvertex, int numvertices, int numtriangles, const int *element3i)
2140 float *out3f = rsurface.array_texcoord3f + 3 * firstvertex;
2141 const float *vertex3f = rsurface.vertex3f + 3 * firstvertex;
2142 const float *svector3f = rsurface.svector3f + 3 * firstvertex;
2143 const float *tvector3f = rsurface.tvector3f + 3 * firstvertex;
2144 const float *normal3f = rsurface.normal3f + 3 * firstvertex;
2145 float lightdir[3], eyedir[3], halfdir[3];
2146 for (i = 0;i < numvertices;i++, vertex3f += 3, svector3f += 3, tvector3f += 3, normal3f += 3, out3f += 3)
2148 VectorSubtract(rsurface.entitylightorigin, vertex3f, lightdir);
2149 VectorNormalize(lightdir);
2150 VectorSubtract(rsurface.modelorg, vertex3f, eyedir);
2151 VectorNormalize(eyedir);
2152 VectorAdd(lightdir, eyedir, halfdir);
2153 // the cubemap normalizes this for us
2154 out3f[0] = DotProduct(svector3f, halfdir);
2155 out3f[1] = DotProduct(tvector3f, halfdir);
2156 out3f[2] = DotProduct(normal3f, halfdir);
2160 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)
2162 // used to display how many times a surface is lit for level design purposes
2163 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2166 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)
2168 // ARB2 GLSL shader path (GFFX5200, Radeon 9500)
2169 R_SetupSurfaceShader(lightcolorbase, false, ambientscale, diffusescale, specularscale, RSURFPASS_RTLIGHT);
2170 if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND))
2171 R_Mesh_ColorPointer(rsurface.modellightmapcolor4f, rsurface.modellightmapcolor4f_bufferobject, rsurface.modellightmapcolor4f_bufferoffset);
2173 R_Mesh_ColorPointer(NULL, 0, 0);
2174 R_Mesh_TexMatrix(0, &rsurface.texture->currenttexmatrix);
2175 R_Mesh_TexMatrix(1, &rsurface.texture->currentbackgroundtexmatrix);
2176 R_Mesh_TexBind(GL20TU_NORMAL, R_GetTexture(rsurface.texture->currentskinframe->nmap));
2177 R_Mesh_TexBind(GL20TU_COLOR, R_GetTexture(rsurface.texture->basetexture));
2178 R_Mesh_TexBind(GL20TU_GLOSS, R_GetTexture(rsurface.texture->glosstexture));
2179 if (rsurface.texture->backgroundcurrentskinframe)
2181 R_Mesh_TexBind(GL20TU_SECONDARY_NORMAL, R_GetTexture(rsurface.texture->backgroundcurrentskinframe->nmap));
2182 R_Mesh_TexBind(GL20TU_SECONDARY_COLOR, R_GetTexture(rsurface.texture->backgroundbasetexture));
2183 R_Mesh_TexBind(GL20TU_SECONDARY_GLOSS, R_GetTexture(rsurface.texture->backgroundglosstexture));
2184 R_Mesh_TexBind(GL20TU_SECONDARY_GLOW, R_GetTexture(rsurface.texture->backgroundcurrentskinframe->glow));
2186 //R_Mesh_TexBindCubeMap(GL20TU_CUBE, R_GetTexture(rsurface.rtlight->currentcubemap));
2187 R_Mesh_TexBind(GL20TU_FOGMASK, R_GetTexture(r_texture_fogattenuation));
2188 if(rsurface.texture->colormapping)
2190 R_Mesh_TexBind(GL20TU_PANTS, R_GetTexture(rsurface.texture->currentskinframe->pants));
2191 R_Mesh_TexBind(GL20TU_SHIRT, R_GetTexture(rsurface.texture->currentskinframe->shirt));
2193 R_Mesh_TexBind(GL20TU_ATTENUATION, R_GetTexture(r_shadow_attenuationgradienttexture));
2194 R_Mesh_TexCoordPointer(0, 2, rsurface.texcoordtexture2f, rsurface.texcoordtexture2f_bufferobject, rsurface.texcoordtexture2f_bufferoffset);
2195 R_Mesh_TexCoordPointer(1, 3, rsurface.svector3f, rsurface.svector3f_bufferobject, rsurface.svector3f_bufferoffset);
2196 R_Mesh_TexCoordPointer(2, 3, rsurface.tvector3f, rsurface.tvector3f_bufferobject, rsurface.tvector3f_bufferoffset);
2197 R_Mesh_TexCoordPointer(3, 3, rsurface.normal3f, rsurface.normal3f_bufferobject, rsurface.normal3f_bufferoffset);
2198 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
2200 qglDepthFunc(GL_EQUAL);CHECKGLERROR
2202 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2203 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
2205 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
2209 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)
2211 // shared final code for all the dot3 layers
2213 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 0);
2214 for (renders = 0;renders < 64 && (r > 0 || g > 0 || b > 0);renders++, r--, g--, b--)
2216 GL_Color(bound(0, r, 1), bound(0, g, 1), bound(0, b, 1), 1);
2217 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2221 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)
2224 // colorscale accounts for how much we multiply the brightness
2227 // mult is how many times the final pass of the lighting will be
2228 // performed to get more brightness than otherwise possible.
2230 // Limit mult to 64 for sanity sake.
2232 if (r_shadow_texture3d.integer && rsurface.rtlight->currentcubemap != r_texture_whitecube && r_textureunits.integer >= 4)
2234 // 3 3D combine path (Geforce3, Radeon 8500)
2235 memset(&m, 0, sizeof(m));
2236 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
2237 m.pointer_texcoord3f[0] = rsurface.vertex3f;
2238 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
2239 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
2240 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
2241 m.tex[1] = R_GetTexture(basetexture);
2242 m.pointer_texcoord[1] = rsurface.texcoordtexture2f;
2243 m.pointer_texcoord_bufferobject[1] = rsurface.texcoordtexture2f_bufferobject;
2244 m.pointer_texcoord_bufferoffset[1] = rsurface.texcoordtexture2f_bufferoffset;
2245 m.texmatrix[1] = rsurface.texture->currenttexmatrix;
2246 m.texcubemap[2] = R_GetTexture(rsurface.rtlight->currentcubemap);
2247 m.pointer_texcoord3f[2] = rsurface.vertex3f;
2248 m.pointer_texcoord_bufferobject[2] = rsurface.vertex3f_bufferobject;
2249 m.pointer_texcoord_bufferoffset[2] = rsurface.vertex3f_bufferoffset;
2250 m.texmatrix[2] = rsurface.entitytolight;
2251 GL_BlendFunc(GL_ONE, GL_ONE);
2253 else if (r_shadow_texture3d.integer && rsurface.rtlight->currentcubemap == r_texture_whitecube && r_textureunits.integer >= 2)
2255 // 2 3D combine path (Geforce3, original Radeon)
2256 memset(&m, 0, sizeof(m));
2257 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
2258 m.pointer_texcoord3f[0] = rsurface.vertex3f;
2259 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
2260 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
2261 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
2262 m.tex[1] = R_GetTexture(basetexture);
2263 m.pointer_texcoord[1] = rsurface.texcoordtexture2f;
2264 m.pointer_texcoord_bufferobject[1] = rsurface.texcoordtexture2f_bufferobject;
2265 m.pointer_texcoord_bufferoffset[1] = rsurface.texcoordtexture2f_bufferoffset;
2266 m.texmatrix[1] = rsurface.texture->currenttexmatrix;
2267 GL_BlendFunc(GL_ONE, GL_ONE);
2269 else if (r_textureunits.integer >= 4 && rsurface.rtlight->currentcubemap != r_texture_whitecube)
2271 // 4 2D combine path (Geforce3, Radeon 8500)
2272 memset(&m, 0, sizeof(m));
2273 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
2274 m.pointer_texcoord3f[0] = rsurface.vertex3f;
2275 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
2276 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
2277 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
2278 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
2279 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2280 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2281 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2282 m.texmatrix[1] = rsurface.entitytoattenuationz;
2283 m.tex[2] = R_GetTexture(basetexture);
2284 m.pointer_texcoord[2] = rsurface.texcoordtexture2f;
2285 m.pointer_texcoord_bufferobject[2] = rsurface.texcoordtexture2f_bufferobject;
2286 m.pointer_texcoord_bufferoffset[2] = rsurface.texcoordtexture2f_bufferoffset;
2287 m.texmatrix[2] = rsurface.texture->currenttexmatrix;
2288 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2290 m.texcubemap[3] = R_GetTexture(rsurface.rtlight->currentcubemap);
2291 m.pointer_texcoord3f[3] = rsurface.vertex3f;
2292 m.pointer_texcoord_bufferobject[3] = rsurface.vertex3f_bufferobject;
2293 m.pointer_texcoord_bufferoffset[3] = rsurface.vertex3f_bufferoffset;
2294 m.texmatrix[3] = rsurface.entitytolight;
2296 GL_BlendFunc(GL_ONE, GL_ONE);
2298 else if (r_textureunits.integer >= 3 && rsurface.rtlight->currentcubemap == r_texture_whitecube)
2300 // 3 2D combine path (Geforce3, original Radeon)
2301 memset(&m, 0, sizeof(m));
2302 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
2303 m.pointer_texcoord3f[0] = rsurface.vertex3f;
2304 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
2305 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
2306 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
2307 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
2308 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2309 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2310 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2311 m.texmatrix[1] = rsurface.entitytoattenuationz;
2312 m.tex[2] = R_GetTexture(basetexture);
2313 m.pointer_texcoord[2] = rsurface.texcoordtexture2f;
2314 m.pointer_texcoord_bufferobject[2] = rsurface.texcoordtexture2f_bufferobject;
2315 m.pointer_texcoord_bufferoffset[2] = rsurface.texcoordtexture2f_bufferoffset;
2316 m.texmatrix[2] = rsurface.texture->currenttexmatrix;
2317 GL_BlendFunc(GL_ONE, GL_ONE);
2321 // 2/2/2 2D combine path (any dot3 card)
2322 memset(&m, 0, sizeof(m));
2323 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
2324 m.pointer_texcoord3f[0] = rsurface.vertex3f;
2325 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
2326 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
2327 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
2328 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
2329 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2330 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2331 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2332 m.texmatrix[1] = rsurface.entitytoattenuationz;
2333 R_Mesh_TextureState(&m);
2334 GL_ColorMask(0,0,0,1);
2335 GL_BlendFunc(GL_ONE, GL_ZERO);
2336 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2339 memset(&m, 0, sizeof(m));
2340 m.tex[0] = R_GetTexture(basetexture);
2341 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2342 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2343 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2344 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2345 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2347 m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
2348 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2349 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2350 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2351 m.texmatrix[1] = rsurface.entitytolight;
2353 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2355 // this final code is shared
2356 R_Mesh_TextureState(&m);
2357 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);
2360 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)
2363 // colorscale accounts for how much we multiply the brightness
2366 // mult is how many times the final pass of the lighting will be
2367 // performed to get more brightness than otherwise possible.
2369 // Limit mult to 64 for sanity sake.
2371 // generate normalization cubemap texcoords
2372 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(firstvertex, numvertices, numtriangles, element3i);
2373 if (r_shadow_texture3d.integer && r_textureunits.integer >= 4)
2375 // 3/2 3D combine path (Geforce3, Radeon 8500)
2376 memset(&m, 0, sizeof(m));
2377 m.tex[0] = R_GetTexture(normalmaptexture);
2378 m.texcombinergb[0] = GL_REPLACE;
2379 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2380 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2381 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2382 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2383 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
2384 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2385 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
2386 m.pointer_texcoord_bufferobject[1] = 0;
2387 m.pointer_texcoord_bufferoffset[1] = 0;
2388 m.tex3d[2] = R_GetTexture(r_shadow_attenuation3dtexture);
2389 m.pointer_texcoord3f[2] = rsurface.vertex3f;
2390 m.pointer_texcoord_bufferobject[2] = rsurface.vertex3f_bufferobject;
2391 m.pointer_texcoord_bufferoffset[2] = rsurface.vertex3f_bufferoffset;
2392 m.texmatrix[2] = rsurface.entitytoattenuationxyz;
2393 R_Mesh_TextureState(&m);
2394 GL_ColorMask(0,0,0,1);
2395 GL_BlendFunc(GL_ONE, GL_ZERO);
2396 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2399 memset(&m, 0, sizeof(m));
2400 m.tex[0] = R_GetTexture(basetexture);
2401 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2402 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2403 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2404 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2405 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2407 m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
2408 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2409 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2410 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2411 m.texmatrix[1] = rsurface.entitytolight;
2413 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2415 else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && rsurface.rtlight->currentcubemap != r_texture_whitecube)
2417 // 1/2/2 3D combine path (original Radeon)
2418 memset(&m, 0, sizeof(m));
2419 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
2420 m.pointer_texcoord3f[0] = rsurface.vertex3f;
2421 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
2422 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
2423 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
2424 R_Mesh_TextureState(&m);
2425 GL_ColorMask(0,0,0,1);
2426 GL_BlendFunc(GL_ONE, GL_ZERO);
2427 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2430 memset(&m, 0, sizeof(m));
2431 m.tex[0] = R_GetTexture(normalmaptexture);
2432 m.texcombinergb[0] = GL_REPLACE;
2433 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2434 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2435 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2436 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2437 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
2438 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2439 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
2440 m.pointer_texcoord_bufferobject[1] = 0;
2441 m.pointer_texcoord_bufferoffset[1] = 0;
2442 R_Mesh_TextureState(&m);
2443 GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
2444 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2447 memset(&m, 0, sizeof(m));
2448 m.tex[0] = R_GetTexture(basetexture);
2449 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2450 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2451 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2452 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2453 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2455 m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
2456 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2457 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2458 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2459 m.texmatrix[1] = rsurface.entitytolight;
2461 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2463 else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && rsurface.rtlight->currentcubemap == r_texture_whitecube)
2465 // 2/2 3D combine path (original Radeon)
2466 memset(&m, 0, sizeof(m));
2467 m.tex[0] = R_GetTexture(normalmaptexture);
2468 m.texcombinergb[0] = GL_REPLACE;
2469 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2470 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2471 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2472 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2473 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
2474 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2475 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
2476 m.pointer_texcoord_bufferobject[1] = 0;
2477 m.pointer_texcoord_bufferoffset[1] = 0;
2478 R_Mesh_TextureState(&m);
2479 GL_ColorMask(0,0,0,1);
2480 GL_BlendFunc(GL_ONE, GL_ZERO);
2481 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2484 memset(&m, 0, sizeof(m));
2485 m.tex[0] = R_GetTexture(basetexture);
2486 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2487 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2488 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2489 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2490 m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
2491 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2492 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2493 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2494 m.texmatrix[1] = rsurface.entitytoattenuationxyz;
2495 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2497 else if (r_textureunits.integer >= 4)
2499 // 4/2 2D combine path (Geforce3, Radeon 8500)
2500 memset(&m, 0, sizeof(m));
2501 m.tex[0] = R_GetTexture(normalmaptexture);
2502 m.texcombinergb[0] = GL_REPLACE;
2503 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2504 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2505 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2506 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2507 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
2508 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2509 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
2510 m.pointer_texcoord_bufferobject[1] = 0;
2511 m.pointer_texcoord_bufferoffset[1] = 0;
2512 m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
2513 m.pointer_texcoord3f[2] = rsurface.vertex3f;
2514 m.pointer_texcoord_bufferobject[2] = rsurface.vertex3f_bufferobject;
2515 m.pointer_texcoord_bufferoffset[2] = rsurface.vertex3f_bufferoffset;
2516 m.texmatrix[2] = rsurface.entitytoattenuationxyz;
2517 m.tex[3] = R_GetTexture(r_shadow_attenuation2dtexture);
2518 m.pointer_texcoord3f[3] = rsurface.vertex3f;
2519 m.pointer_texcoord_bufferobject[3] = rsurface.vertex3f_bufferobject;
2520 m.pointer_texcoord_bufferoffset[3] = rsurface.vertex3f_bufferoffset;
2521 m.texmatrix[3] = rsurface.entitytoattenuationz;
2522 R_Mesh_TextureState(&m);
2523 GL_ColorMask(0,0,0,1);
2524 GL_BlendFunc(GL_ONE, GL_ZERO);
2525 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2528 memset(&m, 0, sizeof(m));
2529 m.tex[0] = R_GetTexture(basetexture);
2530 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2531 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2532 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2533 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2534 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2536 m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
2537 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2538 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2539 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2540 m.texmatrix[1] = rsurface.entitytolight;
2542 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2546 // 2/2/2 2D combine path (any dot3 card)
2547 memset(&m, 0, sizeof(m));
2548 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
2549 m.pointer_texcoord3f[0] = rsurface.vertex3f;
2550 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
2551 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
2552 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
2553 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
2554 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2555 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
2556 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
2557 m.texmatrix[1] = rsurface.entitytoattenuationz;
2558 R_Mesh_TextureState(&m);
2559 GL_ColorMask(0,0,0,1);
2560 GL_BlendFunc(GL_ONE, GL_ZERO);
2561 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2564 memset(&m, 0, sizeof(m));
2565 m.tex[0] = R_GetTexture(normalmaptexture);
2566 m.texcombinergb[0] = GL_REPLACE;
2567 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2568 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2569 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2570 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2571 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
2572 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2573 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
2574 m.pointer_texcoord_bufferobject[1] = 0;
2575 m.pointer_texcoord_bufferoffset[1] = 0;
2576 R_Mesh_TextureState(&m);
2577 GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
2578 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2581 memset(&m, 0, sizeof(m));
2582 m.tex[0] = R_GetTexture(basetexture);
2583 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2584 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2585 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2586 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2587 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2589 m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
2590 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2591 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2592 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2593 m.texmatrix[1] = rsurface.entitytolight;
2595 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2597 // this final code is shared
2598 R_Mesh_TextureState(&m);
2599 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);
2602 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)
2604 float glossexponent;
2606 // FIXME: detect blendsquare!
2607 //if (!gl_support_blendsquare)
2610 // generate normalization cubemap texcoords
2611 R_Shadow_GenTexCoords_Specular_NormalCubeMap(firstvertex, numvertices, numtriangles, element3i);
2612 if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && rsurface.rtlight->currentcubemap != r_texture_whitecube)
2614 // 2/0/0/1/2 3D combine blendsquare path
2615 memset(&m, 0, sizeof(m));
2616 m.tex[0] = R_GetTexture(normalmaptexture);
2617 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2618 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2619 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2620 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2621 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
2622 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2623 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
2624 m.pointer_texcoord_bufferobject[1] = 0;
2625 m.pointer_texcoord_bufferoffset[1] = 0;
2626 R_Mesh_TextureState(&m);
2627 GL_ColorMask(0,0,0,1);
2628 // this squares the result
2629 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
2630 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2632 // second and third pass
2633 R_Mesh_ResetTextureState();
2634 // square alpha in framebuffer a few times to make it shiny
2635 GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
2636 for (glossexponent = 2;glossexponent * 2 <= r_shadow_glossexponent.value;glossexponent *= 2)
2637 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2640 memset(&m, 0, sizeof(m));
2641 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
2642 m.pointer_texcoord3f[0] = rsurface.vertex3f;
2643 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
2644 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
2645 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
2646 R_Mesh_TextureState(&m);
2647 GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
2648 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2651 memset(&m, 0, sizeof(m));
2652 m.tex[0] = R_GetTexture(glosstexture);
2653 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2654 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2655 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2656 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2657 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2659 m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
2660 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2661 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2662 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2663 m.texmatrix[1] = rsurface.entitytolight;
2665 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2667 else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && rsurface.rtlight->currentcubemap == r_texture_whitecube /* && gl_support_blendsquare*/) // FIXME: detect blendsquare!
2669 // 2/0/0/2 3D combine blendsquare path
2670 memset(&m, 0, sizeof(m));
2671 m.tex[0] = R_GetTexture(normalmaptexture);
2672 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2673 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2674 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2675 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2676 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
2677 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2678 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
2679 m.pointer_texcoord_bufferobject[1] = 0;
2680 m.pointer_texcoord_bufferoffset[1] = 0;
2681 R_Mesh_TextureState(&m);
2682 GL_ColorMask(0,0,0,1);
2683 // this squares the result
2684 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
2685 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2687 // second and third pass
2688 R_Mesh_ResetTextureState();
2689 // square alpha in framebuffer a few times to make it shiny
2690 GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
2691 for (glossexponent = 2;glossexponent * 2 <= r_shadow_glossexponent.value;glossexponent *= 2)
2692 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2695 memset(&m, 0, sizeof(m));
2696 m.tex[0] = R_GetTexture(glosstexture);
2697 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2698 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2699 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2700 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2701 m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
2702 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2703 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2704 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2705 m.texmatrix[1] = rsurface.entitytoattenuationxyz;
2706 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2710 // 2/0/0/2/2 2D combine blendsquare path
2711 memset(&m, 0, sizeof(m));
2712 m.tex[0] = R_GetTexture(normalmaptexture);
2713 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2714 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2715 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2716 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2717 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
2718 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2719 m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
2720 m.pointer_texcoord_bufferobject[1] = 0;
2721 m.pointer_texcoord_bufferoffset[1] = 0;
2722 R_Mesh_TextureState(&m);
2723 GL_ColorMask(0,0,0,1);
2724 // this squares the result
2725 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
2726 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2728 // second and third pass
2729 R_Mesh_ResetTextureState();
2730 // square alpha in framebuffer a few times to make it shiny
2731 GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
2732 for (glossexponent = 2;glossexponent * 2 <= r_shadow_glossexponent.value;glossexponent *= 2)
2733 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2736 memset(&m, 0, sizeof(m));
2737 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
2738 m.pointer_texcoord3f[0] = rsurface.vertex3f;
2739 m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
2740 m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
2741 m.texmatrix[0] = rsurface.entitytoattenuationxyz;
2742 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
2743 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2744 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2745 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2746 m.texmatrix[1] = rsurface.entitytoattenuationz;
2747 R_Mesh_TextureState(&m);
2748 GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
2749 R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
2752 memset(&m, 0, sizeof(m));
2753 m.tex[0] = R_GetTexture(glosstexture);
2754 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2755 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2756 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2757 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2758 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2760 m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
2761 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2762 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2763 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2764 m.texmatrix[1] = rsurface.entitytolight;
2766 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2768 // this final code is shared
2769 R_Mesh_TextureState(&m);
2770 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);
2773 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)
2775 // ARB path (any Geforce, any Radeon)
2776 qboolean doambient = ambientscale > 0;
2777 qboolean dodiffuse = diffusescale > 0;
2778 qboolean dospecular = specularscale > 0;
2779 if (!doambient && !dodiffuse && !dospecular)
2781 R_Mesh_ColorPointer(NULL, 0, 0);
2783 R_Shadow_RenderLighting_Light_Dot3_AmbientPass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, basetexture, ambientscale * r_refdef.view.colorscale);
2785 R_Shadow_RenderLighting_Light_Dot3_DiffusePass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, basetexture, normalmaptexture, diffusescale * r_refdef.view.colorscale);
2789 R_Shadow_RenderLighting_Light_Dot3_AmbientPass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorpants, pantstexture, ambientscale * r_refdef.view.colorscale);
2791 R_Shadow_RenderLighting_Light_Dot3_DiffusePass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorpants, pantstexture, normalmaptexture, diffusescale * r_refdef.view.colorscale);
2796 R_Shadow_RenderLighting_Light_Dot3_AmbientPass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorshirt, shirttexture, ambientscale * r_refdef.view.colorscale);
2798 R_Shadow_RenderLighting_Light_Dot3_DiffusePass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorshirt, shirttexture, normalmaptexture, diffusescale * r_refdef.view.colorscale);
2801 R_Shadow_RenderLighting_Light_Dot3_SpecularPass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, glosstexture, normalmaptexture, specularscale * r_refdef.view.colorscale);
2804 static void R_Shadow_RenderLighting_Light_Vertex_Pass(int firstvertex, int numvertices, int numtriangles, const int *element3i, vec3_t diffusecolor2, vec3_t ambientcolor2)
2811 int newnumtriangles;
2815 int maxtriangles = 4096;
2816 int newelements[4096*3];
2817 R_Shadow_RenderLighting_Light_Vertex_Shading(firstvertex, numvertices, numtriangles, element3i, diffusecolor2, ambientcolor2);
2818 for (renders = 0;renders < 64;renders++)
2823 newnumtriangles = 0;
2825 // due to low fillrate on the cards this vertex lighting path is
2826 // designed for, we manually cull all triangles that do not
2827 // contain a lit vertex
2828 // this builds batches of triangles from multiple surfaces and
2829 // renders them at once
2830 for (i = 0, e = element3i;i < numtriangles;i++, e += 3)
2832 if (VectorLength2(rsurface.array_color4f + e[0] * 4) + VectorLength2(rsurface.array_color4f + e[1] * 4) + VectorLength2(rsurface.array_color4f + e[2] * 4) >= 0.01)
2834 if (newnumtriangles)
2836 newfirstvertex = min(newfirstvertex, e[0]);
2837 newlastvertex = max(newlastvertex, e[0]);
2841 newfirstvertex = e[0];
2842 newlastvertex = e[0];
2844 newfirstvertex = min(newfirstvertex, e[1]);
2845 newlastvertex = max(newlastvertex, e[1]);
2846 newfirstvertex = min(newfirstvertex, e[2]);
2847 newlastvertex = max(newlastvertex, e[2]);
2853 if (newnumtriangles >= maxtriangles)
2855 R_Mesh_Draw(newfirstvertex, newlastvertex - newfirstvertex + 1, 0, newnumtriangles, newelements, NULL, 0, 0);
2856 newnumtriangles = 0;
2862 if (newnumtriangles >= 1)
2864 R_Mesh_Draw(newfirstvertex, newlastvertex - newfirstvertex + 1, 0, newnumtriangles, newelements, NULL, 0, 0);
2867 // if we couldn't find any lit triangles, exit early
2870 // now reduce the intensity for the next overbright pass
2871 // we have to clamp to 0 here incase the drivers have improper
2872 // handling of negative colors
2873 // (some old drivers even have improper handling of >1 color)
2875 for (i = 0, c = rsurface.array_color4f + 4 * firstvertex;i < numvertices;i++, c += 4)
2877 if (c[0] > 1 || c[1] > 1 || c[2] > 1)
2879 c[0] = max(0, c[0] - 1);
2880 c[1] = max(0, c[1] - 1);
2881 c[2] = max(0, c[2] - 1);
2893 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)
2895 // OpenGL 1.1 path (anything)
2896 float ambientcolorbase[3], diffusecolorbase[3];
2897 float ambientcolorpants[3], diffusecolorpants[3];
2898 float ambientcolorshirt[3], diffusecolorshirt[3];
2900 VectorScale(lightcolorbase, ambientscale * 2 * r_refdef.view.colorscale, ambientcolorbase);
2901 VectorScale(lightcolorbase, diffusescale * 2 * r_refdef.view.colorscale, diffusecolorbase);
2902 VectorScale(lightcolorpants, ambientscale * 2 * r_refdef.view.colorscale, ambientcolorpants);
2903 VectorScale(lightcolorpants, diffusescale * 2 * r_refdef.view.colorscale, diffusecolorpants);
2904 VectorScale(lightcolorshirt, ambientscale * 2 * r_refdef.view.colorscale, ambientcolorshirt);
2905 VectorScale(lightcolorshirt, diffusescale * 2 * r_refdef.view.colorscale, diffusecolorshirt);
2906 memset(&m, 0, sizeof(m));
2907 m.tex[0] = R_GetTexture(basetexture);
2908 m.texmatrix[0] = rsurface.texture->currenttexmatrix;
2909 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
2910 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
2911 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
2912 if (r_textureunits.integer >= 2)
2915 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
2916 m.texmatrix[1] = rsurface.entitytoattenuationxyz;
2917 m.pointer_texcoord3f[1] = rsurface.vertex3f;
2918 m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
2919 m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
2920 if (r_textureunits.integer >= 3)
2922 // Voodoo4 or Kyro (or Geforce3/Radeon with gl_combine off)
2923 m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
2924 m.texmatrix[2] = rsurface.entitytoattenuationz;
2925 m.pointer_texcoord3f[2] = rsurface.vertex3f;
2926 m.pointer_texcoord_bufferobject[2] = rsurface.vertex3f_bufferobject;
2927 m.pointer_texcoord_bufferoffset[2] = rsurface.vertex3f_bufferoffset;
2930 R_Mesh_TextureState(&m);
2931 //R_Mesh_TexBind(0, R_GetTexture(basetexture));
2932 R_Shadow_RenderLighting_Light_Vertex_Pass(firstvertex, numvertices, numtriangles, element3i, diffusecolorbase, ambientcolorbase);
2935 R_Mesh_TexBind(0, R_GetTexture(pantstexture));
2936 R_Shadow_RenderLighting_Light_Vertex_Pass(firstvertex, numvertices, numtriangles, element3i, diffusecolorpants, ambientcolorpants);
2940 R_Mesh_TexBind(0, R_GetTexture(shirttexture));
2941 R_Shadow_RenderLighting_Light_Vertex_Pass(firstvertex, numvertices, numtriangles, element3i, diffusecolorshirt, ambientcolorshirt);
2945 extern cvar_t gl_lightmaps;
2946 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)
2948 float ambientscale, diffusescale, specularscale;
2949 vec3_t lightcolorbase, lightcolorpants, lightcolorshirt;
2951 // calculate colors to render this texture with
2952 lightcolorbase[0] = rsurface.rtlight->currentcolor[0] * rsurface.texture->dlightcolor[0];
2953 lightcolorbase[1] = rsurface.rtlight->currentcolor[1] * rsurface.texture->dlightcolor[1];
2954 lightcolorbase[2] = rsurface.rtlight->currentcolor[2] * rsurface.texture->dlightcolor[2];
2955 ambientscale = rsurface.rtlight->ambientscale;
2956 diffusescale = rsurface.rtlight->diffusescale;
2957 specularscale = rsurface.rtlight->specularscale * rsurface.texture->specularscale;
2958 if (!r_shadow_usenormalmap.integer)
2960 ambientscale += 1.0f * diffusescale;
2964 if ((ambientscale + diffusescale) * VectorLength2(lightcolorbase) + specularscale * VectorLength2(lightcolorbase) < (1.0f / 1048576.0f))
2966 RSurf_SetupDepthAndCulling();
2967 nmap = rsurface.texture->currentskinframe->nmap;
2968 if (gl_lightmaps.integer)
2969 nmap = r_texture_blanknormalmap;
2970 if (rsurface.texture->colormapping && !gl_lightmaps.integer)
2972 qboolean dopants = rsurface.texture->currentskinframe->pants != NULL && VectorLength2(rsurface.colormap_pantscolor) >= (1.0f / 1048576.0f);
2973 qboolean doshirt = rsurface.texture->currentskinframe->shirt != NULL && VectorLength2(rsurface.colormap_shirtcolor) >= (1.0f / 1048576.0f);
2976 lightcolorpants[0] = lightcolorbase[0] * rsurface.colormap_pantscolor[0];
2977 lightcolorpants[1] = lightcolorbase[1] * rsurface.colormap_pantscolor[1];
2978 lightcolorpants[2] = lightcolorbase[2] * rsurface.colormap_pantscolor[2];
2981 VectorClear(lightcolorpants);
2984 lightcolorshirt[0] = lightcolorbase[0] * rsurface.colormap_shirtcolor[0];
2985 lightcolorshirt[1] = lightcolorbase[1] * rsurface.colormap_shirtcolor[1];
2986 lightcolorshirt[2] = lightcolorbase[2] * rsurface.colormap_shirtcolor[2];
2989 VectorClear(lightcolorshirt);
2990 switch (r_shadow_rendermode)
2992 case R_SHADOW_RENDERMODE_VISIBLELIGHTING:
2993 GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST) && !r_showdisabledepthtest.integer);
2994 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);
2996 case R_SHADOW_RENDERMODE_LIGHT_GLSL:
2997 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);
2999 case R_SHADOW_RENDERMODE_LIGHT_DOT3:
3000 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);
3002 case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
3003 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);
3006 Con_Printf("R_Shadow_RenderLighting: unknown r_shadow_rendermode %i\n", r_shadow_rendermode);
3012 switch (r_shadow_rendermode)
3014 case R_SHADOW_RENDERMODE_VISIBLELIGHTING:
3015 GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST) && !r_showdisabledepthtest.integer);
3016 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);
3018 case R_SHADOW_RENDERMODE_LIGHT_GLSL:
3019 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);
3021 case R_SHADOW_RENDERMODE_LIGHT_DOT3:
3022 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);
3024 case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
3025 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);
3028 Con_Printf("R_Shadow_RenderLighting: unknown r_shadow_rendermode %i\n", r_shadow_rendermode);
3034 void R_RTLight_Update(rtlight_t *rtlight, int isstatic, matrix4x4_t *matrix, vec3_t color, int style, const char *cubemapname, int shadow, vec_t corona, vec_t coronasizescale, vec_t ambientscale, vec_t diffusescale, vec_t specularscale, int flags)
3036 matrix4x4_t tempmatrix = *matrix;
3037 Matrix4x4_Scale(&tempmatrix, r_shadow_lightradiusscale.value, 1);
3039 // if this light has been compiled before, free the associated data
3040 R_RTLight_Uncompile(rtlight);
3042 // clear it completely to avoid any lingering data
3043 memset(rtlight, 0, sizeof(*rtlight));
3045 // copy the properties
3046 rtlight->matrix_lighttoworld = tempmatrix;
3047 Matrix4x4_Invert_Simple(&rtlight->matrix_worldtolight, &tempmatrix);
3048 Matrix4x4_OriginFromMatrix(&tempmatrix, rtlight->shadoworigin);
3049 rtlight->radius = Matrix4x4_ScaleFromMatrix(&tempmatrix);
3050 VectorCopy(color, rtlight->color);
3051 rtlight->cubemapname[0] = 0;
3052 if (cubemapname && cubemapname[0])
3053 strlcpy(rtlight->cubemapname, cubemapname, sizeof(rtlight->cubemapname));
3054 rtlight->shadow = shadow;
3055 rtlight->corona = corona;
3056 rtlight->style = style;
3057 rtlight->isstatic = isstatic;
3058 rtlight->coronasizescale = coronasizescale;
3059 rtlight->ambientscale = ambientscale;
3060 rtlight->diffusescale = diffusescale;
3061 rtlight->specularscale = specularscale;
3062 rtlight->flags = flags;
3064 // compute derived data
3065 //rtlight->cullradius = rtlight->radius;
3066 //rtlight->cullradius2 = rtlight->radius * rtlight->radius;
3067 rtlight->cullmins[0] = rtlight->shadoworigin[0] - rtlight->radius;
3068 rtlight->cullmins[1] = rtlight->shadoworigin[1] - rtlight->radius;
3069 rtlight->cullmins[2] = rtlight->shadoworigin[2] - rtlight->radius;
3070 rtlight->cullmaxs[0] = rtlight->shadoworigin[0] + rtlight->radius;
3071 rtlight->cullmaxs[1] = rtlight->shadoworigin[1] + rtlight->radius;
3072 rtlight->cullmaxs[2] = rtlight->shadoworigin[2] + rtlight->radius;
3075 // compiles rtlight geometry
3076 // (undone by R_FreeCompiledRTLight, which R_UpdateLight calls)
3077 void R_RTLight_Compile(rtlight_t *rtlight)
3080 int numsurfaces, numleafs, numleafpvsbytes, numshadowtrispvsbytes, numlighttrispvsbytes;
3081 int lighttris, shadowtris, shadowzpasstris, shadowzfailtris;
3082 entity_render_t *ent = r_refdef.scene.worldentity;
3083 dp_model_t *model = r_refdef.scene.worldmodel;
3084 unsigned char *data;
3087 // compile the light
3088 rtlight->compiled = true;
3089 rtlight->static_numleafs = 0;
3090 rtlight->static_numleafpvsbytes = 0;
3091 rtlight->static_leaflist = NULL;
3092 rtlight->static_leafpvs = NULL;
3093 rtlight->static_numsurfaces = 0;
3094 rtlight->static_surfacelist = NULL;
3095 rtlight->cullmins[0] = rtlight->shadoworigin[0] - rtlight->radius;
3096 rtlight->cullmins[1] = rtlight->shadoworigin[1] - rtlight->radius;
3097 rtlight->cullmins[2] = rtlight->shadoworigin[2] - rtlight->radius;
3098 rtlight->cullmaxs[0] = rtlight->shadoworigin[0] + rtlight->radius;
3099 rtlight->cullmaxs[1] = rtlight->shadoworigin[1] + rtlight->radius;
3100 rtlight->cullmaxs[2] = rtlight->shadoworigin[2] + rtlight->radius;
3102 if (model && model->GetLightInfo)
3104 // this variable must be set for the CompileShadowVolume code
3105 r_shadow_compilingrtlight = rtlight;
3106 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);
3107 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);
3108 numleafpvsbytes = (model->brush.num_leafs + 7) >> 3;
3109 numshadowtrispvsbytes = ((model->brush.shadowmesh ? model->brush.shadowmesh->numtriangles : model->surfmesh.num_triangles) + 7) >> 3;
3110 numlighttrispvsbytes = (model->surfmesh.num_triangles + 7) >> 3;
3111 data = (unsigned char *)Mem_Alloc(r_main_mempool, sizeof(int) * numsurfaces + sizeof(int) * numleafs + numleafpvsbytes + numshadowtrispvsbytes + numlighttrispvsbytes);
3112 rtlight->static_numsurfaces = numsurfaces;
3113 rtlight->static_surfacelist = (int *)data;data += sizeof(int) * numsurfaces;
3114 rtlight->static_numleafs = numleafs;
3115 rtlight->static_leaflist = (int *)data;data += sizeof(int) * numleafs;
3116 rtlight->static_numleafpvsbytes = numleafpvsbytes;
3117 rtlight->static_leafpvs = (unsigned char *)data;data += numleafpvsbytes;
3118 rtlight->static_numshadowtrispvsbytes = numshadowtrispvsbytes;
3119 rtlight->static_shadowtrispvs = (unsigned char *)data;data += numshadowtrispvsbytes;
3120 rtlight->static_numlighttrispvsbytes = numlighttrispvsbytes;
3121 rtlight->static_lighttrispvs = (unsigned char *)data;data += numlighttrispvsbytes;
3122 if (rtlight->static_numsurfaces)
3123 memcpy(rtlight->static_surfacelist, r_shadow_buffer_surfacelist, rtlight->static_numsurfaces * sizeof(*rtlight->static_surfacelist));
3124 if (rtlight->static_numleafs)
3125 memcpy(rtlight->static_leaflist, r_shadow_buffer_leaflist, rtlight->static_numleafs * sizeof(*rtlight->static_leaflist));
3126 if (rtlight->static_numleafpvsbytes)
3127 memcpy(rtlight->static_leafpvs, r_shadow_buffer_leafpvs, rtlight->static_numleafpvsbytes);
3128 if (rtlight->static_numshadowtrispvsbytes)
3129 memcpy(rtlight->static_shadowtrispvs, r_shadow_buffer_shadowtrispvs, rtlight->static_numshadowtrispvsbytes);
3130 if (rtlight->static_numlighttrispvsbytes)
3131 memcpy(rtlight->static_lighttrispvs, r_shadow_buffer_lighttrispvs, rtlight->static_numlighttrispvsbytes);
3132 if (model->CompileShadowVolume && rtlight->shadow)
3133 model->CompileShadowVolume(ent, rtlight->shadoworigin, NULL, rtlight->radius, numsurfaces, r_shadow_buffer_surfacelist);
3134 // now we're done compiling the rtlight
3135 r_shadow_compilingrtlight = NULL;
3139 // use smallest available cullradius - box radius or light radius
3140 //rtlight->cullradius = RadiusFromBoundsAndOrigin(rtlight->cullmins, rtlight->cullmaxs, rtlight->shadoworigin);
3141 //rtlight->cullradius = min(rtlight->cullradius, rtlight->radius);
3143 shadowzpasstris = 0;
3144 if (rtlight->static_meshchain_shadow_zpass)
3145 for (mesh = rtlight->static_meshchain_shadow_zpass;mesh;mesh = mesh->next)
3146 shadowzpasstris += mesh->numtriangles;
3148 shadowzfailtris = 0;
3149 if (rtlight->static_meshchain_shadow_zfail)
3150 for (mesh = rtlight->static_meshchain_shadow_zfail;mesh;mesh = mesh->next)
3151 shadowzfailtris += mesh->numtriangles;
3154 if (rtlight->static_numlighttrispvsbytes)
3155 for (i = 0;i < rtlight->static_numlighttrispvsbytes*8;i++)
3156 if (CHECKPVSBIT(rtlight->static_lighttrispvs, i))
3160 if (rtlight->static_numlighttrispvsbytes)
3161 for (i = 0;i < rtlight->static_numshadowtrispvsbytes*8;i++)
3162 if (CHECKPVSBIT(rtlight->static_shadowtrispvs, i))
3165 if (developer.integer >= 10)
3166 Con_Printf("static light built: %f %f %f : %f %f %f box, %i light triangles, %i shadow triangles, %i zpass/%i zfail compiled shadow volume triangles\n", rtlight->cullmins[0], rtlight->cullmins[1], rtlight->cullmins[2], rtlight->cullmaxs[0], rtlight->cullmaxs[1], rtlight->cullmaxs[2], lighttris, shadowtris, shadowzpasstris, shadowzfailtris);
3169 void R_RTLight_Uncompile(rtlight_t *rtlight)
3171 if (rtlight->compiled)
3173 if (rtlight->static_meshchain_shadow_zpass)
3174 Mod_ShadowMesh_Free(rtlight->static_meshchain_shadow_zpass);
3175 rtlight->static_meshchain_shadow_zpass = NULL;
3176 if (rtlight->static_meshchain_shadow_zfail)
3177 Mod_ShadowMesh_Free(rtlight->static_meshchain_shadow_zfail);
3178 rtlight->static_meshchain_shadow_zfail = NULL;
3179 // these allocations are grouped
3180 if (rtlight->static_surfacelist)
3181 Mem_Free(rtlight->static_surfacelist);
3182 rtlight->static_numleafs = 0;
3183 rtlight->static_numleafpvsbytes = 0;
3184 rtlight->static_leaflist = NULL;
3185 rtlight->static_leafpvs = NULL;
3186 rtlight->static_numsurfaces = 0;
3187 rtlight->static_surfacelist = NULL;
3188 rtlight->static_numshadowtrispvsbytes = 0;
3189 rtlight->static_shadowtrispvs = NULL;
3190 rtlight->static_numlighttrispvsbytes = 0;
3191 rtlight->static_lighttrispvs = NULL;
3192 rtlight->compiled = false;
3196 void R_Shadow_UncompileWorldLights(void)
3200 size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
3201 for (lightindex = 0;lightindex < range;lightindex++)
3203 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
3206 R_RTLight_Uncompile(&light->rtlight);
3210 void R_Shadow_ComputeShadowCasterCullingPlanes(rtlight_t *rtlight)
3214 // reset the count of frustum planes
3215 // see rsurface.rtlight_frustumplanes definition for how much this array
3217 rsurface.rtlight_numfrustumplanes = 0;
3219 // haven't implemented a culling path for ortho rendering
3220 if (!r_refdef.view.useperspective)
3222 // check if the light is on screen and copy the 4 planes if it is
3223 for (i = 0;i < 4;i++)
3224 if (PlaneDiff(rtlight->shadoworigin, &r_refdef.view.frustum[i]) < -0.03125)
3227 for (i = 0;i < 4;i++)
3228 rsurface.rtlight_frustumplanes[rsurface.rtlight_numfrustumplanes++] = r_refdef.view.frustum[i];
3233 // generate a deformed frustum that includes the light origin, this is
3234 // used to cull shadow casting surfaces that can not possibly cast a
3235 // shadow onto the visible light-receiving surfaces, which can be a
3238 // if the light origin is onscreen the result will be 4 planes exactly
3239 // if the light origin is offscreen on only one axis the result will
3240 // be exactly 5 planes (split-side case)
3241 // if the light origin is offscreen on two axes the result will be
3242 // exactly 4 planes (stretched corner case)
3243 for (i = 0;i < 4;i++)
3245 // quickly reject standard frustum planes that put the light
3246 // origin outside the frustum
3247 if (PlaneDiff(rtlight->shadoworigin, &r_refdef.view.frustum[i]) < -0.03125)
3250 rsurface.rtlight_frustumplanes[rsurface.rtlight_numfrustumplanes++] = r_refdef.view.frustum[i];
3252 // if all the standard frustum planes were accepted, the light is onscreen
3253 // otherwise we need to generate some more planes below...
3254 if (rsurface.rtlight_numfrustumplanes < 4)
3256 // at least one of the stock frustum planes failed, so we need to
3257 // create one or two custom planes to enclose the light origin
3258 for (i = 0;i < 4;i++)
3260 // create a plane using the view origin and light origin, and a
3261 // single point from the frustum corner set
3262 TriangleNormal(r_refdef.view.origin, r_refdef.view.frustumcorner[i], rtlight->shadoworigin, plane.normal);
3263 VectorNormalize(plane.normal);
3264 plane.dist = DotProduct(r_refdef.view.origin, plane.normal);
3265 // see if this plane is backwards and flip it if so
3266 for (j = 0;j < 4;j++)
3267 if (j != i && DotProduct(r_refdef.view.frustumcorner[j], plane.normal) - plane.dist < -0.03125)
3271 VectorNegate(plane.normal, plane.normal);
3273 // flipped plane, test again to see if it is now valid
3274 for (j = 0;j < 4;j++)
3275 if (j != i && DotProduct(r_refdef.view.frustumcorner[j], plane.normal) - plane.dist < -0.03125)
3277 // if the plane is still not valid, then it is dividing the
3278 // frustum and has to be rejected
3282 // we have created a valid plane, compute extra info
3283 PlaneClassify(&plane);
3285 rsurface.rtlight_frustumplanes[rsurface.rtlight_numfrustumplanes++] = plane;
3287 // if we've found 5 frustum planes then we have constructed a
3288 // proper split-side case and do not need to keep searching for
3289 // planes to enclose the light origin
3290 if (rsurface.rtlight_numfrustumplanes == 5)
3298 for (i = 0;i < rsurface.rtlight_numfrustumplanes;i++)
3300 plane = rsurface.rtlight_frustumplanes[i];
3301 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));
3306 // now add the light-space box planes if the light box is rotated, as any
3307 // caster outside the oriented light box is irrelevant (even if it passed
3308 // the worldspace light box, which is axial)
3309 if (rtlight->matrix_lighttoworld.m[0][0] != 1 || rtlight->matrix_lighttoworld.m[1][1] != 1 || rtlight->matrix_lighttoworld.m[2][2] != 1)
3311 for (i = 0;i < 6;i++)
3315 v[i >> 1] = (i & 1) ? -1 : 1;
3316 Matrix4x4_Transform(&rtlight->matrix_lighttoworld, v, plane.normal);
3317 VectorSubtract(plane.normal, rtlight->shadoworigin, plane.normal);
3318 plane.dist = VectorNormalizeLength(plane.normal);
3319 plane.dist += DotProduct(plane.normal, rtlight->shadoworigin);
3320 rsurface.rtlight_frustumplanes[rsurface.rtlight_numfrustumplanes++] = plane;
3326 // add the world-space reduced box planes
3327 for (i = 0;i < 6;i++)
3329 VectorClear(plane.normal);
3330 plane.normal[i >> 1] = (i & 1) ? -1 : 1;
3331 plane.dist = (i & 1) ? -rsurface.rtlight_cullmaxs[i >> 1] : rsurface.rtlight_cullmins[i >> 1];
3332 rsurface.rtlight_frustumplanes[rsurface.rtlight_numfrustumplanes++] = plane;
3341 // reduce all plane distances to tightly fit the rtlight cull box, which
3343 VectorSet(points[0], rsurface.rtlight_cullmins[0], rsurface.rtlight_cullmins[1], rsurface.rtlight_cullmins[2]);
3344 VectorSet(points[1], rsurface.rtlight_cullmaxs[0], rsurface.rtlight_cullmins[1], rsurface.rtlight_cullmins[2]);
3345 VectorSet(points[2], rsurface.rtlight_cullmins[0], rsurface.rtlight_cullmaxs[1], rsurface.rtlight_cullmins[2]);
3346 VectorSet(points[3], rsurface.rtlight_cullmaxs[0], rsurface.rtlight_cullmaxs[1], rsurface.rtlight_cullmins[2]);
3347 VectorSet(points[4], rsurface.rtlight_cullmins[0], rsurface.rtlight_cullmins[1], rsurface.rtlight_cullmaxs[2]);
3348 VectorSet(points[5], rsurface.rtlight_cullmaxs[0], rsurface.rtlight_cullmins[1], rsurface.rtlight_cullmaxs[2]);
3349 VectorSet(points[6], rsurface.rtlight_cullmins[0], rsurface.rtlight_cullmaxs[1], rsurface.rtlight_cullmaxs[2]);
3350 VectorSet(points[7], rsurface.rtlight_cullmaxs[0], rsurface.rtlight_cullmaxs[1], rsurface.rtlight_cullmaxs[2]);
3351 oldnum = rsurface.rtlight_numfrustumplanes;
3352 rsurface.rtlight_numfrustumplanes = 0;
3353 for (j = 0;j < oldnum;j++)
3355 // find the nearest point on the box to this plane
3356 bestdist = DotProduct(rsurface.rtlight_frustumplanes[j].normal, points[0]);
3357 for (i = 1;i < 8;i++)
3359 dist = DotProduct(rsurface.rtlight_frustumplanes[j].normal, points[i]);
3360 if (bestdist > dist)
3363 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);
3364 // if the nearest point is near or behind the plane, we want this
3365 // plane, otherwise the plane is useless as it won't cull anything
3366 if (rsurface.rtlight_frustumplanes[j].dist < bestdist + 0.03125)
3368 PlaneClassify(&rsurface.rtlight_frustumplanes[j]);
3369 rsurface.rtlight_frustumplanes[rsurface.rtlight_numfrustumplanes++] = rsurface.rtlight_frustumplanes[j];
3376 void R_Shadow_DrawWorldShadow(int numsurfaces, int *surfacelist, const unsigned char *trispvs)
3381 int surfacelistindex;
3382 msurface_t *surface;
3384 RSurf_ActiveWorldEntity();
3385 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAPRECTANGLE || r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAPCUBESIDE || r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAP2D)
3387 if (r_refdef.scene.worldentity->model)
3388 r_refdef.scene.worldmodel->DrawShadowMap(r_refdef.scene.worldentity, rsurface.rtlight->shadoworigin, NULL, rsurface.rtlight->radius, numsurfaces, surfacelist, rsurface.rtlight_cullmins, rsurface.rtlight_cullmaxs);
3389 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
3393 if (rsurface.rtlight->compiled && r_shadow_realtime_world_compile.integer && r_shadow_realtime_world_compileshadow.integer)
3396 zpass = R_Shadow_UseZPass(r_refdef.scene.worldmodel->normalmins, r_refdef.scene.worldmodel->normalmaxs);
3397 R_Shadow_RenderMode_StencilShadowVolumes(zpass);
3398 mesh = zpass ? rsurface.rtlight->static_meshchain_shadow_zpass : rsurface.rtlight->static_meshchain_shadow_zfail;
3399 for (;mesh;mesh = mesh->next)
3401 r_refdef.stats.lights_shadowtriangles += mesh->numtriangles;
3402 R_Mesh_VertexPointer(mesh->vertex3f, mesh->vbo, mesh->vbooffset_vertex3f);
3403 GL_LockArrays(0, mesh->numverts);
3404 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZPASS_STENCIL)
3406 // increment stencil if frontface is infront of depthbuffer
3407 GL_CullFace(r_refdef.view.cullface_back);
3408 qglStencilOp(GL_KEEP, GL_KEEP, GL_INCR);CHECKGLERROR
3409 R_Mesh_Draw(0, mesh->numverts, 0, mesh->numtriangles, mesh->element3i, mesh->element3s, mesh->ebo3i, mesh->ebo3s);
3410 // decrement stencil if backface is infront of depthbuffer
3411 GL_CullFace(r_refdef.view.cullface_front);
3412 qglStencilOp(GL_KEEP, GL_KEEP, GL_DECR);CHECKGLERROR
3414 else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZFAIL_STENCIL)
3416 // decrement stencil if backface is behind depthbuffer
3417 GL_CullFace(r_refdef.view.cullface_front);
3418 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
3419 R_Mesh_Draw(0, mesh->numverts, 0, mesh->numtriangles, mesh->element3i, mesh->element3s, mesh->ebo3i, mesh->ebo3s);
3420 // increment stencil if frontface is behind depthbuffer
3421 GL_CullFace(r_refdef.view.cullface_back);
3422 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR
3424 R_Mesh_Draw(0, mesh->numverts, 0, mesh->numtriangles, mesh->element3i, mesh->element3s, mesh->ebo3i, mesh->ebo3s);
3425 GL_LockArrays(0, 0);
3429 else if (numsurfaces && r_refdef.scene.worldmodel->brush.shadowmesh && r_shadow_culltriangles.integer)
3431 R_Shadow_PrepareShadowMark(r_refdef.scene.worldmodel->brush.shadowmesh->numtriangles);
3432 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
3434 surface = r_refdef.scene.worldmodel->data_surfaces + surfacelist[surfacelistindex];
3435 for (t = surface->num_firstshadowmeshtriangle, tend = t + surface->num_triangles;t < tend;t++)
3436 if (CHECKPVSBIT(trispvs, t))
3437 shadowmarklist[numshadowmark++] = t;
3439 R_Shadow_VolumeFromList(r_refdef.scene.worldmodel->brush.shadowmesh->numverts, r_refdef.scene.worldmodel->brush.shadowmesh->numtriangles, r_refdef.scene.worldmodel->brush.shadowmesh->vertex3f, r_refdef.scene.worldmodel->brush.shadowmesh->element3i, r_refdef.scene.worldmodel->brush.shadowmesh->neighbor3i, rsurface.rtlight->shadoworigin, NULL, rsurface.rtlight->radius + r_refdef.scene.worldmodel->radius*2 + r_shadow_projectdistance.value, numshadowmark, shadowmarklist, r_refdef.scene.worldmodel->normalmins, r_refdef.scene.worldmodel->normalmaxs);
3441 else if (numsurfaces)
3442 r_refdef.scene.worldmodel->DrawShadowVolume(r_refdef.scene.worldentity, rsurface.rtlight->shadoworigin, NULL, rsurface.rtlight->radius, numsurfaces, surfacelist, rsurface.rtlight_cullmins, rsurface.rtlight_cullmaxs);
3444 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
3447 void R_Shadow_DrawEntityShadow(entity_render_t *ent)
3449 vec3_t relativeshadoworigin, relativeshadowmins, relativeshadowmaxs;
3450 vec_t relativeshadowradius;
3451 RSurf_ActiveModelEntity(ent, false, false);
3452 Matrix4x4_Transform(&ent->inversematrix, rsurface.rtlight->shadoworigin, relativeshadoworigin);
3453 relativeshadowradius = rsurface.rtlight->radius / ent->scale;
3454 relativeshadowmins[0] = relativeshadoworigin[0] - relativeshadowradius;
3455 relativeshadowmins[1] = relativeshadoworigin[1] - relativeshadowradius;
3456 relativeshadowmins[2] = relativeshadoworigin[2] - relativeshadowradius;
3457 relativeshadowmaxs[0] = relativeshadoworigin[0] + relativeshadowradius;
3458 relativeshadowmaxs[1] = relativeshadoworigin[1] + relativeshadowradius;
3459 relativeshadowmaxs[2] = relativeshadoworigin[2] + relativeshadowradius;
3460 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAPRECTANGLE || r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAPCUBESIDE || r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAP2D)
3461 ent->model->DrawShadowMap(ent, relativeshadoworigin, NULL, relativeshadowradius, ent->model->nummodelsurfaces, ent->model->sortedmodelsurfaces, relativeshadowmins, relativeshadowmaxs);
3463 ent->model->DrawShadowVolume(ent, relativeshadoworigin, NULL, relativeshadowradius, ent->model->nummodelsurfaces, ent->model->sortedmodelsurfaces, relativeshadowmins, relativeshadowmaxs);
3464 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
3467 void R_Shadow_SetupEntityLight(const entity_render_t *ent)
3469 // set up properties for rendering light onto this entity
3470 RSurf_ActiveModelEntity(ent, true, true);
3471 GL_AlphaTest(false);
3472 Matrix4x4_Concat(&rsurface.entitytolight, &rsurface.rtlight->matrix_worldtolight, &ent->matrix);
3473 Matrix4x4_Concat(&rsurface.entitytoattenuationxyz, &matrix_attenuationxyz, &rsurface.entitytolight);
3474 Matrix4x4_Concat(&rsurface.entitytoattenuationz, &matrix_attenuationz, &rsurface.entitytolight);
3475 Matrix4x4_Transform(&ent->inversematrix, rsurface.rtlight->shadoworigin, rsurface.entitylightorigin);
3476 if (r_shadow_lightingrendermode == R_SHADOW_RENDERMODE_LIGHT_GLSL)
3477 R_Mesh_TexMatrix(3, &rsurface.entitytolight);
3480 void R_Shadow_DrawWorldLight(int numsurfaces, int *surfacelist, const unsigned char *trispvs)
3482 if (!r_refdef.scene.worldmodel->DrawLight)
3485 // set up properties for rendering light onto this entity
3486 RSurf_ActiveWorldEntity();
3487 GL_AlphaTest(false);
3488 rsurface.entitytolight = rsurface.rtlight->matrix_worldtolight;
3489 Matrix4x4_Concat(&rsurface.entitytoattenuationxyz, &matrix_attenuationxyz, &rsurface.entitytolight);
3490 Matrix4x4_Concat(&rsurface.entitytoattenuationz, &matrix_attenuationz, &rsurface.entitytolight);
3491 VectorCopy(rsurface.rtlight->shadoworigin, rsurface.entitylightorigin);
3492 if (r_shadow_lightingrendermode == R_SHADOW_RENDERMODE_LIGHT_GLSL)
3493 R_Mesh_TexMatrix(3, &rsurface.entitytolight);
3495 r_refdef.scene.worldmodel->DrawLight(r_refdef.scene.worldentity, numsurfaces, surfacelist, trispvs);
3497 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
3500 void R_Shadow_DrawEntityLight(entity_render_t *ent)
3502 dp_model_t *model = ent->model;
3503 if (!model->DrawLight)
3506 R_Shadow_SetupEntityLight(ent);
3508 model->DrawLight(ent, model->nummodelsurfaces, model->sortedmodelsurfaces, NULL);
3510 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
3514 {{ 0, 0, 0}, "px", true, true, true},
3515 {{ 0, 90, 0}, "py", false, true, false},
3516 {{ 0, 180, 0}, "nx", false, false, true},
3517 {{ 0, 270, 0}, "ny", true, false, false},
3518 {{-90, 180, 0}, "pz", false, false, true},
3519 {{ 90, 180, 0}, "nz", false, false, true}
3522 static const double shadowviewmat16[6][4][4] =
3562 void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
3566 int numleafs, numsurfaces;
3567 int *leaflist, *surfacelist;
3568 unsigned char *leafpvs, *shadowtrispvs, *lighttrispvs;
3569 int numlightentities;
3570 int numlightentities_noselfshadow;
3571 int numshadowentities;
3572 int numshadowentities_noselfshadow;
3573 static entity_render_t *lightentities[MAX_EDICTS];
3574 static entity_render_t *lightentities_noselfshadow[MAX_EDICTS];
3575 static entity_render_t *shadowentities[MAX_EDICTS];
3576 static entity_render_t *shadowentities_noselfshadow[MAX_EDICTS];
3577 vec3_t nearestpoint;
3579 qboolean castshadows;
3582 // skip lights that don't light because of ambientscale+diffusescale+specularscale being 0 (corona only lights)
3583 // skip lights that are basically invisible (color 0 0 0)
3584 if (VectorLength2(rtlight->color) * (rtlight->ambientscale + rtlight->diffusescale + rtlight->specularscale) < (1.0f / 1048576.0f))
3587 // loading is done before visibility checks because loading should happen
3588 // all at once at the start of a level, not when it stalls gameplay.
3589 // (especially important to benchmarks)
3591 if (rtlight->isstatic && !rtlight->compiled && r_shadow_realtime_world_compile.integer)
3592 R_RTLight_Compile(rtlight);
3594 rtlight->currentcubemap = rtlight->cubemapname[0] ? R_Shadow_Cubemap(rtlight->cubemapname) : r_texture_whitecube;
3596 // look up the light style value at this time
3597 f = (rtlight->style >= 0 ? r_refdef.scene.rtlightstylevalue[rtlight->style] : 1) * r_shadow_lightintensityscale.value;
3598 VectorScale(rtlight->color, f, rtlight->currentcolor);
3600 if (rtlight->selected)
3602 f = 2 + sin(realtime * M_PI * 4.0);
3603 VectorScale(rtlight->currentcolor, f, rtlight->currentcolor);
3607 // if lightstyle is currently off, don't draw the light
3608 if (VectorLength2(rtlight->currentcolor) < (1.0f / 1048576.0f))
3611 // if the light box is offscreen, skip it
3612 if (R_CullBox(rtlight->cullmins, rtlight->cullmaxs))
3615 VectorCopy(rtlight->cullmins, rsurface.rtlight_cullmins);
3616 VectorCopy(rtlight->cullmaxs, rsurface.rtlight_cullmaxs);
3618 if (rtlight->compiled && r_shadow_realtime_world_compile.integer)
3620 // compiled light, world available and can receive realtime lighting
3621 // retrieve leaf information
3622 numleafs = rtlight->static_numleafs;
3623 leaflist = rtlight->static_leaflist;
3624 leafpvs = rtlight->static_leafpvs;
3625 numsurfaces = rtlight->static_numsurfaces;
3626 surfacelist = rtlight->static_surfacelist;
3627 shadowtrispvs = rtlight->static_shadowtrispvs;
3628 lighttrispvs = rtlight->static_lighttrispvs;
3630 else if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->GetLightInfo)
3632 // dynamic light, world available and can receive realtime lighting
3633 // calculate lit surfaces and leafs
3634 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);
3635 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);
3636 leaflist = r_shadow_buffer_leaflist;
3637 leafpvs = r_shadow_buffer_leafpvs;
3638 surfacelist = r_shadow_buffer_surfacelist;
3639 shadowtrispvs = r_shadow_buffer_shadowtrispvs;
3640 lighttrispvs = r_shadow_buffer_lighttrispvs;
3641 // if the reduced leaf bounds are offscreen, skip it
3642 if (R_CullBox(rsurface.rtlight_cullmins, rsurface.rtlight_cullmaxs))
3653 shadowtrispvs = NULL;
3654 lighttrispvs = NULL;
3656 // check if light is illuminating any visible leafs
3659 for (i = 0;i < numleafs;i++)
3660 if (r_refdef.viewcache.world_leafvisible[leaflist[i]])
3665 // set up a scissor rectangle for this light
3666 if (R_Shadow_ScissorForBBox(rsurface.rtlight_cullmins, rsurface.rtlight_cullmaxs))
3669 R_Shadow_ComputeShadowCasterCullingPlanes(rtlight);
3671 // make a list of lit entities and shadow casting entities
3672 numlightentities = 0;
3673 numlightentities_noselfshadow = 0;
3674 numshadowentities = 0;
3675 numshadowentities_noselfshadow = 0;
3676 // add dynamic entities that are lit by the light
3677 if (r_drawentities.integer)
3679 for (i = 0;i < r_refdef.scene.numentities;i++)
3682 entity_render_t *ent = r_refdef.scene.entities[i];
3684 if (!BoxesOverlap(ent->mins, ent->maxs, rsurface.rtlight_cullmins, rsurface.rtlight_cullmaxs))
3686 // skip the object entirely if it is not within the valid
3687 // shadow-casting region (which includes the lit region)
3688 if (R_CullBoxCustomPlanes(ent->mins, ent->maxs, rsurface.rtlight_numfrustumplanes, rsurface.rtlight_frustumplanes))
3690 if (!(model = ent->model))
3692 if (r_refdef.viewcache.entityvisible[i] && model->DrawLight && (ent->flags & RENDER_LIGHT))
3694 // this entity wants to receive light, is visible, and is
3695 // inside the light box
3696 // TODO: check if the surfaces in the model can receive light
3697 // so now check if it's in a leaf seen by the light
3698 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))
3700 if (ent->flags & RENDER_NOSELFSHADOW)
3701 lightentities_noselfshadow[numlightentities_noselfshadow++] = ent;
3703 lightentities[numlightentities++] = ent;
3704 // since it is lit, it probably also casts a shadow...
3705 // about the VectorDistance2 - light emitting entities should not cast their own shadow
3706 Matrix4x4_OriginFromMatrix(&ent->matrix, org);
3707 if ((ent->flags & RENDER_SHADOW) && model->DrawShadowVolume && VectorDistance2(org, rtlight->shadoworigin) > 0.1)
3709 // note: exterior models without the RENDER_NOSELFSHADOW
3710 // flag still create a RENDER_NOSELFSHADOW shadow but
3711 // are lit normally, this means that they are
3712 // self-shadowing but do not shadow other
3713 // RENDER_NOSELFSHADOW entities such as the gun
3714 // (very weird, but keeps the player shadow off the gun)
3715 if (ent->flags & (RENDER_NOSELFSHADOW | RENDER_EXTERIORMODEL))
3716 shadowentities_noselfshadow[numshadowentities_noselfshadow++] = ent;
3718 shadowentities[numshadowentities++] = ent;
3721 else if (ent->flags & RENDER_SHADOW)
3723 // this entity is not receiving light, but may still need to
3725 // TODO: check if the surfaces in the model can cast shadow
3726 // now check if it is in a leaf seen by the light
3727 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))
3729 // about the VectorDistance2 - light emitting entities should not cast their own shadow
3730 Matrix4x4_OriginFromMatrix(&ent->matrix, org);
3731 if ((ent->flags & RENDER_SHADOW) && model->DrawShadowVolume && VectorDistance2(org, rtlight->shadoworigin) > 0.1)
3733 if (ent->flags & (RENDER_NOSELFSHADOW | RENDER_EXTERIORMODEL))
3734 shadowentities_noselfshadow[numshadowentities_noselfshadow++] = ent;
3736 shadowentities[numshadowentities++] = ent;
3742 // return if there's nothing at all to light
3743 if (!numlightentities && !numsurfaces)
3746 // don't let sound skip if going slow
3747 if (r_refdef.scene.extraupdate)
3750 // make this the active rtlight for rendering purposes
3751 R_Shadow_RenderMode_ActiveLight(rtlight);
3752 // count this light in the r_speeds
3753 r_refdef.stats.lights++;
3755 if (r_showshadowvolumes.integer && r_refdef.view.showdebug && numsurfaces + numshadowentities + numshadowentities_noselfshadow && rtlight->shadow && (rtlight->isstatic ? r_refdef.scene.rtworldshadows : r_refdef.scene.rtdlightshadows))
3757 // optionally draw visible shape of the shadow volumes
3758 // for performance analysis by level designers
3759 R_Shadow_RenderMode_VisibleShadowVolumes();
3761 R_Shadow_DrawWorldShadow(numsurfaces, surfacelist, shadowtrispvs);
3762 for (i = 0;i < numshadowentities;i++)
3763 R_Shadow_DrawEntityShadow(shadowentities[i]);
3764 for (i = 0;i < numshadowentities_noselfshadow;i++)
3765 R_Shadow_DrawEntityShadow(shadowentities_noselfshadow[i]);
3768 if (r_showlighting.integer && r_refdef.view.showdebug && numsurfaces + numlightentities + numlightentities_noselfshadow)
3770 // optionally draw the illuminated areas
3771 // for performance analysis by level designers
3772 R_Shadow_RenderMode_VisibleLighting(false, false);
3774 R_Shadow_DrawWorldLight(numsurfaces, surfacelist, lighttrispvs);
3775 for (i = 0;i < numlightentities;i++)
3776 R_Shadow_DrawEntityLight(lightentities[i]);
3777 for (i = 0;i < numlightentities_noselfshadow;i++)
3778 R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]);
3781 castshadows = numsurfaces + numshadowentities + numshadowentities_noselfshadow > 0 && rtlight->shadow && (rtlight->isstatic ? r_refdef.scene.rtworldshadows : r_refdef.scene.rtdlightshadows);
3783 nearestpoint[0] = bound(rtlight->cullmins[0], r_refdef.view.origin[0], rtlight->cullmaxs[0]);
3784 nearestpoint[1] = bound(rtlight->cullmins[1], r_refdef.view.origin[1], rtlight->cullmaxs[1]);
3785 nearestpoint[2] = bound(rtlight->cullmins[2], r_refdef.view.origin[2], rtlight->cullmaxs[2]);
3786 distance = VectorDistance(nearestpoint, r_refdef.view.origin);
3787 lodlinear = (int)(r_shadow_shadowmapping_lod_bias.value + r_shadow_shadowmapping_lod_scale.value * rtlight->radius / max(1.0f, distance));
3788 lodlinear = bound(r_shadow_shadowmapping_minsize.integer, lodlinear, r_shadow_shadowmapping_maxsize.integer);
3790 if (castshadows && r_shadow_shadowmode >= 1 && r_shadow_shadowmode <= 3 && r_glsl.integer && gl_support_fragment_shader)
3795 r_shadow_shadowmaplod = 0;
3796 for (i = 1;i < R_SHADOW_SHADOWMAP_NUMCUBEMAPS;i++)
3797 if ((r_shadow_shadowmapping_maxsize.integer >> i) > lodlinear)
3798 r_shadow_shadowmaplod = i;
3800 size = r_shadow_shadowmode == 3 ? r_shadow_shadowmapping_maxsize.integer >> r_shadow_shadowmaplod : lodlinear;
3801 size = bound(1, size, 2048);
3803 //Con_Printf("distance %f lodlinear %i (lod %i) size %i\n", distance, lodlinear, r_shadow_shadowmaplod, size);
3805 // render shadow casters into 6 sided depth texture
3806 for (side = 0;side < 6;side++)
3808 R_Shadow_RenderMode_ShadowMap(side, true, size);
3810 R_Shadow_DrawWorldShadow(numsurfaces, surfacelist, shadowtrispvs);
3811 for (i = 0;i < numshadowentities;i++)
3812 R_Shadow_DrawEntityShadow(shadowentities[i]);
3815 if (numlightentities_noselfshadow)
3817 // render lighting using the depth texture as shadowmap
3818 // draw lighting in the unmasked areas
3819 R_Shadow_RenderMode_Lighting(false, false, true);
3820 for (i = 0;i < numlightentities_noselfshadow;i++)
3821 R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]);
3824 // render shadow casters into 6 sided depth texture
3825 for (side = 0;side < 6;side++)
3827 R_Shadow_RenderMode_ShadowMap(side, false, size);
3828 for (i = 0;i < numshadowentities_noselfshadow;i++)
3829 R_Shadow_DrawEntityShadow(shadowentities_noselfshadow[i]);
3832 // render lighting using the depth texture as shadowmap
3833 // draw lighting in the unmasked areas
3834 R_Shadow_RenderMode_Lighting(false, false, true);
3835 // draw lighting in the unmasked areas
3837 R_Shadow_DrawWorldLight(numsurfaces, surfacelist, lighttrispvs);
3838 for (i = 0;i < numlightentities;i++)
3839 R_Shadow_DrawEntityLight(lightentities[i]);
3841 else if (castshadows && gl_stencil)
3843 // draw stencil shadow volumes to mask off pixels that are in shadow
3844 // so that they won't receive lighting
3845 GL_Scissor(r_shadow_lightscissor[0], r_shadow_lightscissor[1], r_shadow_lightscissor[2], r_shadow_lightscissor[3]);
3846 R_Shadow_ClearStencil();
3848 R_Shadow_DrawWorldShadow(numsurfaces, surfacelist, shadowtrispvs);
3849 for (i = 0;i < numshadowentities;i++)
3850 R_Shadow_DrawEntityShadow(shadowentities[i]);
3851 if (numlightentities_noselfshadow)
3853 // draw lighting in the unmasked areas
3854 R_Shadow_RenderMode_Lighting(true, false, false);
3855 for (i = 0;i < numlightentities_noselfshadow;i++)
3856 R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]);
3858 // optionally draw the illuminated areas
3859 // for performance analysis by level designers
3860 if (r_showlighting.integer && r_refdef.view.showdebug)
3862 R_Shadow_RenderMode_VisibleLighting(!r_showdisabledepthtest.integer, false);
3863 for (i = 0;i < numlightentities_noselfshadow;i++)
3864 R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]);
3867 for (i = 0;i < numshadowentities_noselfshadow;i++)
3868 R_Shadow_DrawEntityShadow(shadowentities_noselfshadow[i]);
3870 if (numsurfaces + numlightentities)
3872 // draw lighting in the unmasked areas
3873 R_Shadow_RenderMode_Lighting(true, false, false);
3875 R_Shadow_DrawWorldLight(numsurfaces, surfacelist, lighttrispvs);
3876 for (i = 0;i < numlightentities;i++)
3877 R_Shadow_DrawEntityLight(lightentities[i]);
3882 if (numsurfaces + numlightentities)
3884 // draw lighting in the unmasked areas
3885 R_Shadow_RenderMode_Lighting(false, false, false);
3887 R_Shadow_DrawWorldLight(numsurfaces, surfacelist, lighttrispvs);
3888 for (i = 0;i < numlightentities;i++)
3889 R_Shadow_DrawEntityLight(lightentities[i]);
3890 for (i = 0;i < numlightentities_noselfshadow;i++)
3891 R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]);
3896 void R_Shadow_DrawLightSprites(void);
3897 void R_ShadowVolumeLighting(qboolean visible)
3905 if (r_shadow_shadowmapmaxsize != bound(1, r_shadow_shadowmapping_maxsize.integer, 2048) ||
3906 (r_shadow_shadowmode != 0) != (r_shadow_shadowmapping.integer != 0) ||
3907 r_shadow_shadowmapvsdct != (r_shadow_shadowmapping_vsdct.integer != 0) ||
3908 r_shadow_shadowmaptexturetype != r_shadow_shadowmapping_texturetype.integer ||
3909 r_shadow_shadowmapfilterquality != r_shadow_shadowmapping_filterquality.integer ||
3910 r_shadow_shadowmapborder != bound(0, r_shadow_shadowmapping_bordersize.integer, 16))
3911 R_Shadow_FreeShadowMaps();
3913 if (r_editlights.integer)
3914 R_Shadow_DrawLightSprites();
3916 R_Shadow_RenderMode_Begin();
3918 flag = r_refdef.scene.rtworld ? LIGHTFLAG_REALTIMEMODE : LIGHTFLAG_NORMALMODE;
3919 if (r_shadow_debuglight.integer >= 0)
3921 lightindex = r_shadow_debuglight.integer;
3922 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
3923 if (light && (light->flags & flag))
3924 R_DrawRTLight(&light->rtlight, visible);
3928 range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
3929 for (lightindex = 0;lightindex < range;lightindex++)
3931 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
3932 if (light && (light->flags & flag))
3933 R_DrawRTLight(&light->rtlight, visible);
3936 if (r_refdef.scene.rtdlight)
3937 for (lnum = 0;lnum < r_refdef.scene.numlights;lnum++)
3938 R_DrawRTLight(r_refdef.scene.lights[lnum], visible);
3940 R_Shadow_RenderMode_End();
3943 extern const float r_screenvertex3f[12];
3944 extern void R_SetupView(qboolean allowwaterclippingplane);
3945 extern void R_ResetViewRendering3D(void);
3946 extern void R_ResetViewRendering2D(void);
3947 extern cvar_t r_shadows;
3948 extern cvar_t r_shadows_darken;
3949 extern cvar_t r_shadows_drawafterrtlighting;
3950 extern cvar_t r_shadows_castfrombmodels;
3951 extern cvar_t r_shadows_throwdistance;
3952 extern cvar_t r_shadows_throwdirection;
3953 void R_DrawModelShadows(void)
3956 float relativethrowdistance;
3957 entity_render_t *ent;
3958 vec3_t relativelightorigin;
3959 vec3_t relativelightdirection;
3960 vec3_t relativeshadowmins, relativeshadowmaxs;
3961 vec3_t tmp, shadowdir;
3963 if (!r_drawentities.integer || !gl_stencil)
3967 R_ResetViewRendering3D();
3968 //GL_Scissor(r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
3969 //GL_Scissor(r_refdef.view.x, vid.height - r_refdef.view.height - r_refdef.view.y, r_refdef.view.width, r_refdef.view.height);
3970 R_Shadow_RenderMode_Begin();
3971 R_Shadow_RenderMode_ActiveLight(NULL);
3972 r_shadow_lightscissor[0] = r_refdef.view.x;
3973 r_shadow_lightscissor[1] = vid.height - r_refdef.view.y - r_refdef.view.height;
3974 r_shadow_lightscissor[2] = r_refdef.view.width;
3975 r_shadow_lightscissor[3] = r_refdef.view.height;
3976 R_Shadow_RenderMode_StencilShadowVolumes(false);
3979 if (r_shadows.integer == 2)
3981 Math_atov(r_shadows_throwdirection.string, shadowdir);
3982 VectorNormalize(shadowdir);
3985 R_Shadow_ClearStencil();
3987 for (i = 0;i < r_refdef.scene.numentities;i++)
3989 ent = r_refdef.scene.entities[i];
3991 // cast shadows from anything of the map (submodels are optional)
3992 if (ent->model && ent->model->DrawShadowVolume != NULL && (!ent->model->brush.submodel || r_shadows_castfrombmodels.integer) && (ent->flags & RENDER_SHADOW))
3994 relativethrowdistance = r_shadows_throwdistance.value * Matrix4x4_ScaleFromMatrix(&ent->inversematrix);
3995 VectorSet(relativeshadowmins, -relativethrowdistance, -relativethrowdistance, -relativethrowdistance);
3996 VectorSet(relativeshadowmaxs, relativethrowdistance, relativethrowdistance, relativethrowdistance);
3997 if (r_shadows.integer == 2) // 2: simpler mode, throw shadows always in same direction
3998 Matrix4x4_Transform3x3(&ent->inversematrix, shadowdir, relativelightdirection);
4001 if(ent->entitynumber != 0)
4003 // networked entity - might be attached in some way (then we should use the parent's light direction, to not tear apart attached entities)
4004 int entnum, entnum2, recursion;
4005 entnum = entnum2 = ent->entitynumber;
4006 for(recursion = 32; recursion > 0; --recursion)
4008 entnum2 = cl.entities[entnum].state_current.tagentity;
4009 if(entnum2 >= 1 && entnum2 < cl.num_entities && cl.entities_active[entnum2])
4014 if(recursion && recursion != 32) // if we followed a valid non-empty attachment chain
4016 VectorNegate(cl.entities[entnum].render.modellight_lightdir, relativelightdirection);
4017 // transform into modelspace of OUR entity
4018 Matrix4x4_Transform3x3(&cl.entities[entnum].render.matrix, relativelightdirection, tmp);
4019 Matrix4x4_Transform3x3(&ent->inversematrix, tmp, relativelightdirection);
4022 VectorNegate(ent->modellight_lightdir, relativelightdirection);
4025 VectorNegate(ent->modellight_lightdir, relativelightdirection);
4028 VectorScale(relativelightdirection, -relativethrowdistance, relativelightorigin);
4029 RSurf_ActiveModelEntity(ent, false, false);
4030 ent->model->DrawShadowVolume(ent, relativelightorigin, relativelightdirection, relativethrowdistance, ent->model->nummodelsurfaces, ent->model->sortedmodelsurfaces, relativeshadowmins, relativeshadowmaxs);
4031 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
4035 // not really the right mode, but this will disable any silly stencil features
4036 R_Shadow_RenderMode_End();
4038 // set up ortho view for rendering this pass
4039 //GL_Scissor(r_refdef.view.x, vid.height - r_refdef.view.height - r_refdef.view.y, r_refdef.view.width, r_refdef.view.height);
4040 //GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
4041 //GL_ScissorTest(true);
4042 //R_Mesh_Matrix(&identitymatrix);
4043 //R_Mesh_ResetTextureState();
4044 R_ResetViewRendering2D();
4045 R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
4046 R_Mesh_ColorPointer(NULL, 0, 0);
4047 R_SetupGenericShader(false);
4049 // set up a darkening blend on shadowed areas
4050 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
4051 //GL_DepthRange(0, 1);
4052 //GL_DepthTest(false);
4053 //GL_DepthMask(false);
4054 //GL_PolygonOffset(0, 0);CHECKGLERROR
4055 GL_Color(0, 0, 0, r_shadows_darken.value);
4056 //GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
4057 //qglDepthFunc(GL_ALWAYS);CHECKGLERROR
4058 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
4059 qglStencilMask(~0);CHECKGLERROR
4060 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);CHECKGLERROR
4061 qglStencilFunc(GL_NOTEQUAL, 128, ~0);CHECKGLERROR
4063 // apply the blend to the shadowed areas
4064 R_Mesh_Draw(0, 4, 0, 2, NULL, polygonelements, 0, 0);
4066 // restore the viewport
4067 R_SetViewport(&r_refdef.view.viewport);
4069 // restore other state to normal
4070 //R_Shadow_RenderMode_End();
4073 void R_BeginCoronaQuery(rtlight_t *rtlight, float scale, qboolean usequery)
4076 vec3_t centerorigin;
4077 // if it's too close, skip it
4078 if (VectorLength(rtlight->color) < (1.0f / 256.0f))
4080 zdist = (DotProduct(rtlight->shadoworigin, r_refdef.view.forward) - DotProduct(r_refdef.view.origin, r_refdef.view.forward));
4083 if (usequery && r_numqueries + 2 <= r_maxqueries)
4085 rtlight->corona_queryindex_allpixels = r_queries[r_numqueries++];
4086 rtlight->corona_queryindex_visiblepixels = r_queries[r_numqueries++];
4087 VectorMA(r_refdef.view.origin, zdist, r_refdef.view.forward, centerorigin);
4090 // NOTE: we can't disable depth testing using R_DrawSprite's depthdisable argument, which calls GL_DepthTest, as that's broken in the ATI drivers
4091 qglBeginQueryARB(GL_SAMPLES_PASSED_ARB, rtlight->corona_queryindex_allpixels);
4092 qglDepthFunc(GL_ALWAYS);
4093 R_DrawSprite(GL_ONE, GL_ZERO, r_shadow_lightcorona, NULL, false, false, centerorigin, r_refdef.view.right, r_refdef.view.up, scale, -scale, -scale, scale, 1, 1, 1, 1);
4094 qglEndQueryARB(GL_SAMPLES_PASSED_ARB);
4095 qglDepthFunc(GL_LEQUAL);
4096 qglBeginQueryARB(GL_SAMPLES_PASSED_ARB, rtlight->corona_queryindex_visiblepixels);
4097 R_DrawSprite(GL_ONE, GL_ZERO, r_shadow_lightcorona, NULL, false, false, rtlight->shadoworigin, r_refdef.view.right, r_refdef.view.up, scale, -scale, -scale, scale, 1, 1, 1, 1);
4098 qglEndQueryARB(GL_SAMPLES_PASSED_ARB);
4101 rtlight->corona_visibility = bound(0, (zdist - 32) / 32, 1);
4104 void R_DrawCorona(rtlight_t *rtlight, float cscale, float scale)
4107 GLint allpixels = 0, visiblepixels = 0;
4108 // now we have to check the query result
4109 if (rtlight->corona_queryindex_visiblepixels)
4112 qglGetQueryObjectivARB(rtlight->corona_queryindex_visiblepixels, GL_QUERY_RESULT_ARB, &visiblepixels);
4113 qglGetQueryObjectivARB(rtlight->corona_queryindex_allpixels, GL_QUERY_RESULT_ARB, &allpixels);
4115 //Con_Printf("%i of %i pixels\n", (int)visiblepixels, (int)allpixels);
4116 if (visiblepixels < 1 || allpixels < 1)
4118 rtlight->corona_visibility *= bound(0, (float)visiblepixels / (float)allpixels, 1);
4119 cscale *= rtlight->corona_visibility;
4123 // FIXME: these traces should scan all render entities instead of cl.world
4124 if (CL_Move(r_refdef.view.origin, vec3_origin, vec3_origin, rtlight->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false).fraction < 1)
4127 VectorScale(rtlight->color, cscale, color);
4128 if (VectorLength(color) > (1.0f / 256.0f))
4129 R_DrawSprite(GL_ONE, GL_ONE, r_shadow_lightcorona, NULL, true, false, rtlight->shadoworigin, r_refdef.view.right, r_refdef.view.up, scale, -scale, -scale, scale, color[0], color[1], color[2], 1);
4132 void R_DrawCoronas(void)
4140 if (r_coronas.value < (1.0f / 256.0f) && !gl_flashblend.integer)
4142 if (r_waterstate.renderingscene)
4144 flag = r_refdef.scene.rtworld ? LIGHTFLAG_REALTIMEMODE : LIGHTFLAG_NORMALMODE;
4145 R_Mesh_Matrix(&identitymatrix);
4147 range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
4149 // check occlusion of coronas
4150 // use GL_ARB_occlusion_query if available
4151 // otherwise use raytraces
4153 usequery = gl_support_arb_occlusion_query && r_coronas_occlusionquery.integer;
4156 GL_ColorMask(0,0,0,0);
4157 if (r_maxqueries < (range + r_refdef.scene.numlights) * 2)
4158 if (r_maxqueries < R_MAX_OCCLUSION_QUERIES)
4161 r_maxqueries = (range + r_refdef.scene.numlights) * 4;
4162 r_maxqueries = min(r_maxqueries, R_MAX_OCCLUSION_QUERIES);
4164 qglGenQueriesARB(r_maxqueries - i, r_queries + i);
4168 for (lightindex = 0;lightindex < range;lightindex++)
4170 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
4173 rtlight = &light->rtlight;
4174 rtlight->corona_visibility = 0;
4175 rtlight->corona_queryindex_visiblepixels = 0;
4176 rtlight->corona_queryindex_allpixels = 0;
4177 if (!(rtlight->flags & flag))
4179 if (rtlight->corona <= 0)
4181 if (r_shadow_debuglight.integer >= 0 && r_shadow_debuglight.integer != (int)lightindex)
4183 R_BeginCoronaQuery(rtlight, rtlight->radius * rtlight->coronasizescale * r_coronas_occlusionsizescale.value, usequery);
4185 for (i = 0;i < r_refdef.scene.numlights;i++)
4187 rtlight = r_refdef.scene.lights[i];
4188 rtlight->corona_visibility = 0;
4189 rtlight->corona_queryindex_visiblepixels = 0;
4190 rtlight->corona_queryindex_allpixels = 0;
4191 if (!(rtlight->flags & flag))
4193 if (rtlight->corona <= 0)
4195 R_BeginCoronaQuery(rtlight, rtlight->radius * rtlight->coronasizescale * r_coronas_occlusionsizescale.value, usequery);
4198 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
4200 // now draw the coronas using the query data for intensity info
4201 for (lightindex = 0;lightindex < range;lightindex++)
4203 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
4206 rtlight = &light->rtlight;
4207 if (rtlight->corona_visibility <= 0)
4209 R_DrawCorona(rtlight, rtlight->corona * r_coronas.value * 0.25f, rtlight->radius * rtlight->coronasizescale);
4211 for (i = 0;i < r_refdef.scene.numlights;i++)
4213 rtlight = r_refdef.scene.lights[i];
4214 if (rtlight->corona_visibility <= 0)
4216 if (gl_flashblend.integer)
4217 R_DrawCorona(rtlight, rtlight->corona, rtlight->radius * rtlight->coronasizescale * 2.0f);
4219 R_DrawCorona(rtlight, rtlight->corona * r_coronas.value * 0.25f, rtlight->radius * rtlight->coronasizescale);
4225 //static char *suffix[6] = {"ft", "bk", "rt", "lf", "up", "dn"};
4226 typedef struct suffixinfo_s
4229 qboolean flipx, flipy, flipdiagonal;
4232 static suffixinfo_t suffix[3][6] =
4235 {"px", false, false, false},
4236 {"nx", false, false, false},
4237 {"py", false, false, false},
4238 {"ny", false, false, false},
4239 {"pz", false, false, false},
4240 {"nz", false, false, false}
4243 {"posx", false, false, false},
4244 {"negx", false, false, false},
4245 {"posy", false, false, false},
4246 {"negy", false, false, false},
4247 {"posz", false, false, false},
4248 {"negz", false, false, false}
4251 {"rt", true, false, true},
4252 {"lf", false, true, true},
4253 {"ft", true, true, false},
4254 {"bk", false, false, false},
4255 {"up", true, false, true},
4256 {"dn", true, false, true}
4260 static int componentorder[4] = {0, 1, 2, 3};
4262 rtexture_t *R_Shadow_LoadCubemap(const char *basename)
4264 int i, j, cubemapsize;
4265 unsigned char *cubemappixels, *image_buffer;
4266 rtexture_t *cubemaptexture;
4268 // must start 0 so the first loadimagepixels has no requested width/height
4270 cubemappixels = NULL;
4271 cubemaptexture = NULL;
4272 // keep trying different suffix groups (posx, px, rt) until one loads
4273 for (j = 0;j < 3 && !cubemappixels;j++)
4275 // load the 6 images in the suffix group
4276 for (i = 0;i < 6;i++)
4278 // generate an image name based on the base and and suffix
4279 dpsnprintf(name, sizeof(name), "%s%s", basename, suffix[j][i].suffix);
4281 if ((image_buffer = loadimagepixelsbgra(name, false, false)))
4283 // an image loaded, make sure width and height are equal
4284 if (image_width == image_height && (!cubemappixels || image_width == cubemapsize))
4286 // if this is the first image to load successfully, allocate the cubemap memory
4287 if (!cubemappixels && image_width >= 1)
4289 cubemapsize = image_width;
4290 // note this clears to black, so unavailable sides are black
4291 cubemappixels = (unsigned char *)Mem_Alloc(tempmempool, 6*cubemapsize*cubemapsize*4);
4293 // copy the image with any flipping needed by the suffix (px and posx types don't need flipping)
4295 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);
4298 Con_Printf("Cubemap image \"%s\" (%ix%i) is not square, OpenGL requires square cubemaps.\n", name, image_width, image_height);
4300 Mem_Free(image_buffer);
4304 // if a cubemap loaded, upload it
4307 if (developer_loading.integer)
4308 Con_Printf("loading cubemap \"%s\"\n", basename);
4310 if (!r_shadow_filters_texturepool)
4311 r_shadow_filters_texturepool = R_AllocTexturePool();
4312 cubemaptexture = R_LoadTextureCubeMap(r_shadow_filters_texturepool, basename, cubemapsize, cubemappixels, TEXTYPE_BGRA, TEXF_PRECACHE | (gl_texturecompression_lightcubemaps.integer ? TEXF_COMPRESS : 0) | TEXF_FORCELINEAR, NULL);
4313 Mem_Free(cubemappixels);
4317 Con_DPrintf("failed to load cubemap \"%s\"\n", basename);
4318 if (developer_loading.integer)
4320 Con_Printf("(tried tried images ");
4321 for (j = 0;j < 3;j++)
4322 for (i = 0;i < 6;i++)
4323 Con_Printf("%s\"%s%s.tga\"", j + i > 0 ? ", " : "", basename, suffix[j][i].suffix);
4324 Con_Print(" and was unable to find any of them).\n");
4327 return cubemaptexture;
4330 rtexture_t *R_Shadow_Cubemap(const char *basename)
4333 for (i = 0;i < numcubemaps;i++)
4334 if (!strcasecmp(cubemaps[i].basename, basename))
4335 return cubemaps[i].texture ? cubemaps[i].texture : r_texture_whitecube;
4336 if (i >= MAX_CUBEMAPS)
4337 return r_texture_whitecube;
4339 strlcpy(cubemaps[i].basename, basename, sizeof(cubemaps[i].basename));
4340 cubemaps[i].texture = R_Shadow_LoadCubemap(cubemaps[i].basename);
4341 return cubemaps[i].texture;
4344 void R_Shadow_FreeCubemaps(void)
4347 for (i = 0;i < numcubemaps;i++)
4349 if (developer_loading.integer)
4350 Con_Printf("unloading cubemap \"%s\"\n", cubemaps[i].basename);
4351 if (cubemaps[i].texture)
4352 R_FreeTexture(cubemaps[i].texture);
4356 R_FreeTexturePool(&r_shadow_filters_texturepool);
4359 dlight_t *R_Shadow_NewWorldLight(void)
4361 return (dlight_t *)Mem_ExpandableArray_AllocRecord(&r_shadow_worldlightsarray);
4364 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)
4367 // validate parameters
4368 if (style < 0 || style >= MAX_LIGHTSTYLES)
4370 Con_Printf("R_Shadow_NewWorldLight: invalid light style number %i, must be >= 0 and < %i\n", light->style, MAX_LIGHTSTYLES);
4376 // copy to light properties
4377 VectorCopy(origin, light->origin);
4378 light->angles[0] = angles[0] - 360 * floor(angles[0] / 360);
4379 light->angles[1] = angles[1] - 360 * floor(angles[1] / 360);
4380 light->angles[2] = angles[2] - 360 * floor(angles[2] / 360);
4381 light->color[0] = max(color[0], 0);
4382 light->color[1] = max(color[1], 0);
4383 light->color[2] = max(color[2], 0);
4384 light->radius = max(radius, 0);
4385 light->style = style;
4386 light->shadow = shadowenable;
4387 light->corona = corona;
4388 strlcpy(light->cubemapname, cubemapname, sizeof(light->cubemapname));
4389 light->coronasizescale = coronasizescale;
4390 light->ambientscale = ambientscale;
4391 light->diffusescale = diffusescale;
4392 light->specularscale = specularscale;
4393 light->flags = flags;
4395 // update renderable light data
4396 Matrix4x4_CreateFromQuakeEntity(&matrix, light->origin[0], light->origin[1], light->origin[2], light->angles[0], light->angles[1], light->angles[2], light->radius);
4397 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);
4400 void R_Shadow_FreeWorldLight(dlight_t *light)
4402 if (r_shadow_selectedlight == light)
4403 r_shadow_selectedlight = NULL;
4404 R_RTLight_Uncompile(&light->rtlight);
4405 Mem_ExpandableArray_FreeRecord(&r_shadow_worldlightsarray, light);
4408 void R_Shadow_ClearWorldLights(void)
4412 size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
4413 for (lightindex = 0;lightindex < range;lightindex++)
4415 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
4417 R_Shadow_FreeWorldLight(light);
4419 r_shadow_selectedlight = NULL;
4420 R_Shadow_FreeCubemaps();
4423 void R_Shadow_SelectLight(dlight_t *light)
4425 if (r_shadow_selectedlight)
4426 r_shadow_selectedlight->selected = false;
4427 r_shadow_selectedlight = light;
4428 if (r_shadow_selectedlight)
4429 r_shadow_selectedlight->selected = true;
4432 void R_Shadow_DrawCursor_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
4434 // this is never batched (there can be only one)
4435 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);
4438 void R_Shadow_DrawLightSprite_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
4445 // this is never batched (due to the ent parameter changing every time)
4446 // so numsurfaces == 1 and surfacelist[0] == lightnumber
4447 const dlight_t *light = (dlight_t *)ent;
4450 VectorScale(light->color, intensity, spritecolor);
4451 if (VectorLength(spritecolor) < 0.1732f)
4452 VectorSet(spritecolor, 0.1f, 0.1f, 0.1f);
4453 if (VectorLength(spritecolor) > 1.0f)
4454 VectorNormalize(spritecolor);
4456 // draw light sprite
4457 if (light->cubemapname[0] && !light->shadow)
4458 pic = r_editlights_sprcubemapnoshadowlight;
4459 else if (light->cubemapname[0])
4460 pic = r_editlights_sprcubemaplight;
4461 else if (!light->shadow)
4462 pic = r_editlights_sprnoshadowlight;
4464 pic = r_editlights_sprlight;
4465 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);
4466 // draw selection sprite if light is selected
4467 if (light->selected)
4468 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);
4469 // VorteX todo: add normalmode/realtime mode light overlay sprites?
4472 void R_Shadow_DrawLightSprites(void)
4476 size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
4477 for (lightindex = 0;lightindex < range;lightindex++)
4479 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
4481 R_MeshQueue_AddTransparent(light->origin, R_Shadow_DrawLightSprite_TransparentCallback, (entity_render_t *)light, 5, &light->rtlight);
4483 R_MeshQueue_AddTransparent(r_editlights_cursorlocation, R_Shadow_DrawCursor_TransparentCallback, NULL, 0, NULL);
4486 void R_Shadow_SelectLightInView(void)
4488 float bestrating, rating, temp[3];
4492 size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
4495 for (lightindex = 0;lightindex < range;lightindex++)
4497 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
4500 VectorSubtract(light->origin, r_refdef.view.origin, temp);
4501 rating = (DotProduct(temp, r_refdef.view.forward) / sqrt(DotProduct(temp, temp)));
4504 rating /= (1 + 0.0625f * sqrt(DotProduct(temp, temp)));
4505 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)
4507 bestrating = rating;
4512 R_Shadow_SelectLight(best);
4515 void R_Shadow_LoadWorldLights(void)
4517 int n, a, style, shadow, flags;
4518 char tempchar, *lightsstring, *s, *t, name[MAX_QPATH], cubemapname[MAX_QPATH];
4519 float origin[3], radius, color[3], angles[3], corona, coronasizescale, ambientscale, diffusescale, specularscale;
4520 if (cl.worldmodel == NULL)
4522 Con_Print("No map loaded.\n");
4525 FS_StripExtension (cl.worldmodel->name, name, sizeof (name));
4526 strlcat (name, ".rtlights", sizeof (name));
4527 lightsstring = (char *)FS_LoadFile(name, tempmempool, false, NULL);
4537 for (;COM_Parse(t, true) && strcmp(
4538 if (COM_Parse(t, true))
4540 if (com_token[0] == '!')
4543 origin[0] = atof(com_token+1);
4546 origin[0] = atof(com_token);
4551 while (*s && *s != '\n' && *s != '\r')
4557 // check for modifier flags
4564 #if _MSC_VER >= 1400
4565 #define sscanf sscanf_s
4567 cubemapname[sizeof(cubemapname)-1] = 0;
4568 #if MAX_QPATH != 128
4569 #error update this code if MAX_QPATH changes
4571 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
4572 #if _MSC_VER >= 1400
4573 , sizeof(cubemapname)
4575 , &corona, &angles[0], &angles[1], &angles[2], &coronasizescale, &ambientscale, &diffusescale, &specularscale, &flags);
4578 flags = LIGHTFLAG_REALTIMEMODE;
4586 coronasizescale = 0.25f;
4588 VectorClear(angles);
4591 if (a < 9 || !strcmp(cubemapname, "\"\""))
4593 // remove quotes on cubemapname
4594 if (cubemapname[0] == '"' && cubemapname[strlen(cubemapname) - 1] == '"')
4597 namelen = strlen(cubemapname) - 2;
4598 memmove(cubemapname, cubemapname + 1, namelen);
4599 cubemapname[namelen] = '\0';
4603 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);
4606 R_Shadow_UpdateWorldLight(R_Shadow_NewWorldLight(), origin, angles, color, radius, corona, style, shadow, cubemapname, coronasizescale, ambientscale, diffusescale, specularscale, flags);
4614 Con_Printf("invalid rtlights file \"%s\"\n", name);
4615 Mem_Free(lightsstring);
4619 void R_Shadow_SaveWorldLights(void)
4623 size_t bufchars, bufmaxchars;
4625 char name[MAX_QPATH];
4626 char line[MAX_INPUTLINE];
4627 size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked, assuming the dpsnprintf mess doesn't screw it up...
4628 // I hate lines which are 3 times my screen size :( --blub
4631 if (cl.worldmodel == NULL)
4633 Con_Print("No map loaded.\n");
4636 FS_StripExtension (cl.worldmodel->name, name, sizeof (name));
4637 strlcat (name, ".rtlights", sizeof (name));
4638 bufchars = bufmaxchars = 0;
4640 for (lightindex = 0;lightindex < range;lightindex++)
4642 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
4645 if (light->coronasizescale != 0.25f || light->ambientscale != 0 || light->diffusescale != 1 || light->specularscale != 1 || light->flags != LIGHTFLAG_REALTIMEMODE)
4646 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);
4647 else if (light->cubemapname[0] || light->corona || light->angles[0] || light->angles[1] || light->angles[2])
4648 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]);
4650 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);
4651 if (bufchars + strlen(line) > bufmaxchars)
4653 bufmaxchars = bufchars + strlen(line) + 2048;
4655 buf = (char *)Mem_Alloc(tempmempool, bufmaxchars);
4659 memcpy(buf, oldbuf, bufchars);
4665 memcpy(buf + bufchars, line, strlen(line));
4666 bufchars += strlen(line);
4670 FS_WriteFile(name, buf, (fs_offset_t)bufchars);
4675 void R_Shadow_LoadLightsFile(void)
4678 char tempchar, *lightsstring, *s, *t, name[MAX_QPATH];
4679 float origin[3], radius, color[3], subtract, spotdir[3], spotcone, falloff, distbias;
4680 if (cl.worldmodel == NULL)
4682 Con_Print("No map loaded.\n");
4685 FS_StripExtension (cl.worldmodel->name, name, sizeof (name));
4686 strlcat (name, ".lights", sizeof (name));
4687 lightsstring = (char *)FS_LoadFile(name, tempmempool, false, NULL);
4695 while (*s && *s != '\n' && *s != '\r')
4701 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);
4705 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);
4708 radius = sqrt(DotProduct(color, color) / (falloff * falloff * 8192.0f * 8192.0f));
4709 radius = bound(15, radius, 4096);
4710 VectorScale(color, (2.0f / (8388608.0f)), color);
4711 R_Shadow_UpdateWorldLight(R_Shadow_NewWorldLight(), origin, vec3_origin, color, radius, 0, style, true, NULL, 0.25, 0, 1, 1, LIGHTFLAG_REALTIMEMODE);
4719 Con_Printf("invalid lights file \"%s\"\n", name);
4720 Mem_Free(lightsstring);
4724 // tyrlite/hmap2 light types in the delay field
4725 typedef enum lighttype_e {LIGHTTYPE_MINUSX, LIGHTTYPE_RECIPX, LIGHTTYPE_RECIPXX, LIGHTTYPE_NONE, LIGHTTYPE_SUN, LIGHTTYPE_MINUSXX} lighttype_t;
4727 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void)
4729 int entnum, style, islight, skin, pflags, effects, type, n;
4732 float origin[3], angles[3], radius, color[3], light[4], fadescale, lightscale, originhack[3], overridecolor[3], vec[4];
4733 char key[256], value[MAX_INPUTLINE];
4735 if (cl.worldmodel == NULL)
4737 Con_Print("No map loaded.\n");
4740 // try to load a .ent file first
4741 FS_StripExtension (cl.worldmodel->name, key, sizeof (key));
4742 strlcat (key, ".ent", sizeof (key));
4743 data = entfiledata = (char *)FS_LoadFile(key, tempmempool, true, NULL);
4744 // and if that is not found, fall back to the bsp file entity string
4746 data = cl.worldmodel->brush.entities;
4749 for (entnum = 0;COM_ParseToken_Simple(&data, false, false) && com_token[0] == '{';entnum++)
4751 type = LIGHTTYPE_MINUSX;
4752 origin[0] = origin[1] = origin[2] = 0;
4753 originhack[0] = originhack[1] = originhack[2] = 0;
4754 angles[0] = angles[1] = angles[2] = 0;
4755 color[0] = color[1] = color[2] = 1;
4756 light[0] = light[1] = light[2] = 1;light[3] = 300;
4757 overridecolor[0] = overridecolor[1] = overridecolor[2] = 1;
4767 if (!COM_ParseToken_Simple(&data, false, false))
4769 if (com_token[0] == '}')
4770 break; // end of entity
4771 if (com_token[0] == '_')
4772 strlcpy(key, com_token + 1, sizeof(key));
4774 strlcpy(key, com_token, sizeof(key));
4775 while (key[strlen(key)-1] == ' ') // remove trailing spaces
4776 key[strlen(key)-1] = 0;
4777 if (!COM_ParseToken_Simple(&data, false, false))
4779 strlcpy(value, com_token, sizeof(value));
4781 // now that we have the key pair worked out...
4782 if (!strcmp("light", key))
4784 n = sscanf(value, "%f %f %f %f", &vec[0], &vec[1], &vec[2], &vec[3]);
4788 light[0] = vec[0] * (1.0f / 256.0f);
4789 light[1] = vec[0] * (1.0f / 256.0f);
4790 light[2] = vec[0] * (1.0f / 256.0f);
4796 light[0] = vec[0] * (1.0f / 255.0f);
4797 light[1] = vec[1] * (1.0f / 255.0f);
4798 light[2] = vec[2] * (1.0f / 255.0f);
4802 else if (!strcmp("delay", key))
4804 else if (!strcmp("origin", key))
4805 sscanf(value, "%f %f %f", &origin[0], &origin[1], &origin[2]);
4806 else if (!strcmp("angle", key))
4807 angles[0] = 0, angles[1] = atof(value), angles[2] = 0;
4808 else if (!strcmp("angles", key))
4809 sscanf(value, "%f %f %f", &angles[0], &angles[1], &angles[2]);
4810 else if (!strcmp("color", key))
4811 sscanf(value, "%f %f %f", &color[0], &color[1], &color[2]);
4812 else if (!strcmp("wait", key))
4813 fadescale = atof(value);
4814 else if (!strcmp("classname", key))
4816 if (!strncmp(value, "light", 5))
4819 if (!strcmp(value, "light_fluoro"))
4824 overridecolor[0] = 1;
4825 overridecolor[1] = 1;
4826 overridecolor[2] = 1;
4828 if (!strcmp(value, "light_fluorospark"))
4833 overridecolor[0] = 1;
4834 overridecolor[1] = 1;
4835 overridecolor[2] = 1;
4837 if (!strcmp(value, "light_globe"))
4842 overridecolor[0] = 1;
4843 overridecolor[1] = 0.8;
4844 overridecolor[2] = 0.4;
4846 if (!strcmp(value, "light_flame_large_yellow"))
4851 overridecolor[0] = 1;
4852 overridecolor[1] = 0.5;
4853 overridecolor[2] = 0.1;
4855 if (!strcmp(value, "light_flame_small_yellow"))
4860 overridecolor[0] = 1;
4861 overridecolor[1] = 0.5;
4862 overridecolor[2] = 0.1;
4864 if (!strcmp(value, "light_torch_small_white"))
4869 overridecolor[0] = 1;
4870 overridecolor[1] = 0.5;
4871 overridecolor[2] = 0.1;
4873 if (!strcmp(value, "light_torch_small_walltorch"))
4878 overridecolor[0] = 1;
4879 overridecolor[1] = 0.5;
4880 overridecolor[2] = 0.1;
4884 else if (!strcmp("style", key))
4885 style = atoi(value);
4886 else if (!strcmp("skin", key))
4887 skin = (int)atof(value);
4888 else if (!strcmp("pflags", key))
4889 pflags = (int)atof(value);
4890 else if (!strcmp("effects", key))
4891 effects = (int)atof(value);
4892 else if (cl.worldmodel->type == mod_brushq3)
4894 if (!strcmp("scale", key))
4895 lightscale = atof(value);
4896 if (!strcmp("fade", key))
4897 fadescale = atof(value);
4902 if (lightscale <= 0)
4906 if (color[0] == color[1] && color[0] == color[2])
4908 color[0] *= overridecolor[0];
4909 color[1] *= overridecolor[1];
4910 color[2] *= overridecolor[2];
4912 radius = light[3] * r_editlights_quakelightsizescale.value * lightscale / fadescale;
4913 color[0] = color[0] * light[0];
4914 color[1] = color[1] * light[1];
4915 color[2] = color[2] * light[2];
4918 case LIGHTTYPE_MINUSX:
4920 case LIGHTTYPE_RECIPX:
4922 VectorScale(color, (1.0f / 16.0f), color);
4924 case LIGHTTYPE_RECIPXX:
4926 VectorScale(color, (1.0f / 16.0f), color);
4929 case LIGHTTYPE_NONE:
4933 case LIGHTTYPE_MINUSXX:
4936 VectorAdd(origin, originhack, origin);
4938 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);
4941 Mem_Free(entfiledata);
4945 void R_Shadow_SetCursorLocationForView(void)
4948 vec3_t dest, endpos;
4950 VectorMA(r_refdef.view.origin, r_editlights_cursordistance.value, r_refdef.view.forward, dest);
4951 trace = CL_Move(r_refdef.view.origin, vec3_origin, vec3_origin, dest, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false);
4952 if (trace.fraction < 1)
4954 dist = trace.fraction * r_editlights_cursordistance.value;
4955 push = r_editlights_cursorpushback.value;
4959 VectorMA(trace.endpos, push, r_refdef.view.forward, endpos);
4960 VectorMA(endpos, r_editlights_cursorpushoff.value, trace.plane.normal, endpos);
4964 VectorClear( endpos );
4966 r_editlights_cursorlocation[0] = floor(endpos[0] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
4967 r_editlights_cursorlocation[1] = floor(endpos[1] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
4968 r_editlights_cursorlocation[2] = floor(endpos[2] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
4971 void R_Shadow_UpdateWorldLightSelection(void)
4973 if (r_editlights.integer)
4975 R_Shadow_SetCursorLocationForView();
4976 R_Shadow_SelectLightInView();
4979 R_Shadow_SelectLight(NULL);
4982 void R_Shadow_EditLights_Clear_f(void)
4984 R_Shadow_ClearWorldLights();
4987 void R_Shadow_EditLights_Reload_f(void)
4991 strlcpy(r_shadow_mapname, cl.worldmodel->name, sizeof(r_shadow_mapname));
4992 R_Shadow_ClearWorldLights();
4993 R_Shadow_LoadWorldLights();
4994 if (!Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray))
4996 R_Shadow_LoadLightsFile();
4997 if (!Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray))
4998 R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
5002 void R_Shadow_EditLights_Save_f(void)
5006 R_Shadow_SaveWorldLights();
5009 void R_Shadow_EditLights_ImportLightEntitiesFromMap_f(void)
5011 R_Shadow_ClearWorldLights();
5012 R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
5015 void R_Shadow_EditLights_ImportLightsFile_f(void)
5017 R_Shadow_ClearWorldLights();
5018 R_Shadow_LoadLightsFile();
5021 void R_Shadow_EditLights_Spawn_f(void)
5024 if (!r_editlights.integer)
5026 Con_Print("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
5029 if (Cmd_Argc() != 1)
5031 Con_Print("r_editlights_spawn does not take parameters\n");
5034 color[0] = color[1] = color[2] = 1;
5035 R_Shadow_UpdateWorldLight(R_Shadow_NewWorldLight(), r_editlights_cursorlocation, vec3_origin, color, 200, 0, 0, true, NULL, 0.25, 0, 1, 1, LIGHTFLAG_REALTIMEMODE);
5038 void R_Shadow_EditLights_Edit_f(void)
5040 vec3_t origin, angles, color;
5041 vec_t radius, corona, coronasizescale, ambientscale, diffusescale, specularscale;
5042 int style, shadows, flags, normalmode, realtimemode;
5043 char cubemapname[MAX_INPUTLINE];
5044 if (!r_editlights.integer)
5046 Con_Print("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
5049 if (!r_shadow_selectedlight)
5051 Con_Print("No selected light.\n");
5054 VectorCopy(r_shadow_selectedlight->origin, origin);
5055 VectorCopy(r_shadow_selectedlight->angles, angles);
5056 VectorCopy(r_shadow_selectedlight->color, color);
5057 radius = r_shadow_selectedlight->radius;
5058 style = r_shadow_selectedlight->style;
5059 if (r_shadow_selectedlight->cubemapname)
5060 strlcpy(cubemapname, r_shadow_selectedlight->cubemapname, sizeof(cubemapname));
5063 shadows = r_shadow_selectedlight->shadow;
5064 corona = r_shadow_selectedlight->corona;
5065 coronasizescale = r_shadow_selectedlight->coronasizescale;
5066 ambientscale = r_shadow_selectedlight->ambientscale;
5067 diffusescale = r_shadow_selectedlight->diffusescale;
5068 specularscale = r_shadow_selectedlight->specularscale;
5069 flags = r_shadow_selectedlight->flags;
5070 normalmode = (flags & LIGHTFLAG_NORMALMODE) != 0;
5071 realtimemode = (flags & LIGHTFLAG_REALTIMEMODE) != 0;
5072 if (!strcmp(Cmd_Argv(1), "origin"))
5074 if (Cmd_Argc() != 5)
5076 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(1));
5079 origin[0] = atof(Cmd_Argv(2));
5080 origin[1] = atof(Cmd_Argv(3));
5081 origin[2] = atof(Cmd_Argv(4));
5083 else if (!strcmp(Cmd_Argv(1), "originx"))
5085 if (Cmd_Argc() != 3)
5087 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
5090 origin[0] = atof(Cmd_Argv(2));
5092 else if (!strcmp(Cmd_Argv(1), "originy"))
5094 if (Cmd_Argc() != 3)
5096 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
5099 origin[1] = atof(Cmd_Argv(2));
5101 else if (!strcmp(Cmd_Argv(1), "originz"))
5103 if (Cmd_Argc() != 3)
5105 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
5108 origin[2] = atof(Cmd_Argv(2));
5110 else if (!strcmp(Cmd_Argv(1), "move"))
5112 if (Cmd_Argc() != 5)
5114 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(1));
5117 origin[0] += atof(Cmd_Argv(2));
5118 origin[1] += atof(Cmd_Argv(3));
5119 origin[2] += atof(Cmd_Argv(4));
5121 else if (!strcmp(Cmd_Argv(1), "movex"))
5123 if (Cmd_Argc() != 3)
5125 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
5128 origin[0] += atof(Cmd_Argv(2));
5130 else if (!strcmp(Cmd_Argv(1), "movey"))
5132 if (Cmd_Argc() != 3)
5134 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
5137 origin[1] += atof(Cmd_Argv(2));
5139 else if (!strcmp(Cmd_Argv(1), "movez"))
5141 if (Cmd_Argc() != 3)
5143 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
5146 origin[2] += atof(Cmd_Argv(2));
5148 else if (!strcmp(Cmd_Argv(1), "angles"))
5150 if (Cmd_Argc() != 5)
5152 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(1));
5155 angles[0] = atof(Cmd_Argv(2));
5156 angles[1] = atof(Cmd_Argv(3));
5157 angles[2] = atof(Cmd_Argv(4));
5159 else if (!strcmp(Cmd_Argv(1), "anglesx"))
5161 if (Cmd_Argc() != 3)
5163 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
5166 angles[0] = atof(Cmd_Argv(2));
5168 else if (!strcmp(Cmd_Argv(1), "anglesy"))
5170 if (Cmd_Argc() != 3)
5172 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
5175 angles[1] = atof(Cmd_Argv(2));
5177 else if (!strcmp(Cmd_Argv(1), "anglesz"))
5179 if (Cmd_Argc() != 3)
5181 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
5184 angles[2] = atof(Cmd_Argv(2));
5186 else if (!strcmp(Cmd_Argv(1), "color"))
5188 if (Cmd_Argc() != 5)
5190 Con_Printf("usage: r_editlights_edit %s red green blue\n", Cmd_Argv(1));
5193 color[0] = atof(Cmd_Argv(2));
5194 color[1] = atof(Cmd_Argv(3));
5195 color[2] = atof(Cmd_Argv(4));
5197 else if (!strcmp(Cmd_Argv(1), "radius"))
5199 if (Cmd_Argc() != 3)
5201 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
5204 radius = atof(Cmd_Argv(2));
5206 else if (!strcmp(Cmd_Argv(1), "colorscale"))
5208 if (Cmd_Argc() == 3)
5210 double scale = atof(Cmd_Argv(2));
5217 if (Cmd_Argc() != 5)
5219 Con_Printf("usage: r_editlights_edit %s red green blue (OR grey instead of red green blue)\n", Cmd_Argv(1));
5222 color[0] *= atof(Cmd_Argv(2));
5223 color[1] *= atof(Cmd_Argv(3));
5224 color[2] *= atof(Cmd_Argv(4));
5227 else if (!strcmp(Cmd_Argv(1), "radiusscale") || !strcmp(Cmd_Argv(1), "sizescale"))
5229 if (Cmd_Argc() != 3)
5231 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
5234 radius *= atof(Cmd_Argv(2));
5236 else if (!strcmp(Cmd_Argv(1), "style"))
5238 if (Cmd_Argc() != 3)
5240 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
5243 style = atoi(Cmd_Argv(2));
5245 else if (!strcmp(Cmd_Argv(1), "cubemap"))
5249 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
5252 if (Cmd_Argc() == 3)
5253 strlcpy(cubemapname, Cmd_Argv(2), sizeof(cubemapname));
5257 else if (!strcmp(Cmd_Argv(1), "shadows"))
5259 if (Cmd_Argc() != 3)
5261 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
5264 shadows = Cmd_Argv(2)[0] == 'y' || Cmd_Argv(2)[0] == 'Y' || Cmd_Argv(2)[0] == 't' || atoi(Cmd_Argv(2));
5266 else if (!strcmp(Cmd_Argv(1), "corona"))
5268 if (Cmd_Argc() != 3)
5270 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
5273 corona = atof(Cmd_Argv(2));
5275 else if (!strcmp(Cmd_Argv(1), "coronasize"))
5277 if (Cmd_Argc() != 3)
5279 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
5282 coronasizescale = atof(Cmd_Argv(2));
5284 else if (!strcmp(Cmd_Argv(1), "ambient"))
5286 if (Cmd_Argc() != 3)
5288 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
5291 ambientscale = atof(Cmd_Argv(2));
5293 else if (!strcmp(Cmd_Argv(1), "diffuse"))
5295 if (Cmd_Argc() != 3)
5297 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
5300 diffusescale = atof(Cmd_Argv(2));
5302 else if (!strcmp(Cmd_Argv(1), "specular"))
5304 if (Cmd_Argc() != 3)
5306 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
5309 specularscale = atof(Cmd_Argv(2));
5311 else if (!strcmp(Cmd_Argv(1), "normalmode"))
5313 if (Cmd_Argc() != 3)
5315 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
5318 normalmode = Cmd_Argv(2)[0] == 'y' || Cmd_Argv(2)[0] == 'Y' || Cmd_Argv(2)[0] == 't' || atoi(Cmd_Argv(2));
5320 else if (!strcmp(Cmd_Argv(1), "realtimemode"))
5322 if (Cmd_Argc() != 3)
5324 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
5327 realtimemode = Cmd_Argv(2)[0] == 'y' || Cmd_Argv(2)[0] == 'Y' || Cmd_Argv(2)[0] == 't' || atoi(Cmd_Argv(2));
5331 Con_Print("usage: r_editlights_edit [property] [value]\n");
5332 Con_Print("Selected light's properties:\n");
5333 Con_Printf("Origin : %f %f %f\n", r_shadow_selectedlight->origin[0], r_shadow_selectedlight->origin[1], r_shadow_selectedlight->origin[2]);
5334 Con_Printf("Angles : %f %f %f\n", r_shadow_selectedlight->angles[0], r_shadow_selectedlight->angles[1], r_shadow_selectedlight->angles[2]);
5335 Con_Printf("Color : %f %f %f\n", r_shadow_selectedlight->color[0], r_shadow_selectedlight->color[1], r_shadow_selectedlight->color[2]);
5336 Con_Printf("Radius : %f\n", r_shadow_selectedlight->radius);
5337 Con_Printf("Corona : %f\n", r_shadow_selectedlight->corona);
5338 Con_Printf("Style : %i\n", r_shadow_selectedlight->style);
5339 Con_Printf("Shadows : %s\n", r_shadow_selectedlight->shadow ? "yes" : "no");
5340 Con_Printf("Cubemap : %s\n", r_shadow_selectedlight->cubemapname);
5341 Con_Printf("CoronaSize : %f\n", r_shadow_selectedlight->coronasizescale);
5342 Con_Printf("Ambient : %f\n", r_shadow_selectedlight->ambientscale);
5343 Con_Printf("Diffuse : %f\n", r_shadow_selectedlight->diffusescale);
5344 Con_Printf("Specular : %f\n", r_shadow_selectedlight->specularscale);
5345 Con_Printf("NormalMode : %s\n", (r_shadow_selectedlight->flags & LIGHTFLAG_NORMALMODE) ? "yes" : "no");
5346 Con_Printf("RealTimeMode : %s\n", (r_shadow_selectedlight->flags & LIGHTFLAG_REALTIMEMODE) ? "yes" : "no");
5349 flags = (normalmode ? LIGHTFLAG_NORMALMODE : 0) | (realtimemode ? LIGHTFLAG_REALTIMEMODE : 0);
5350 R_Shadow_UpdateWorldLight(r_shadow_selectedlight, origin, angles, color, radius, corona, style, shadows, cubemapname, coronasizescale, ambientscale, diffusescale, specularscale, flags);
5353 void R_Shadow_EditLights_EditAll_f(void)
5359 if (!r_editlights.integer)
5361 Con_Print("Cannot edit lights when not in editing mode. Set r_editlights to 1.\n");
5365 // EditLights doesn't seem to have a "remove" command or something so:
5366 range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
5367 for (lightindex = 0;lightindex < range;lightindex++)
5369 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
5372 R_Shadow_SelectLight(light);
5373 R_Shadow_EditLights_Edit_f();
5377 void R_Shadow_EditLights_DrawSelectedLightProperties(void)
5379 int lightnumber, lightcount;
5380 size_t lightindex, range;
5384 if (!r_editlights.integer)
5386 x = vid_conwidth.value - 240;
5388 DrawQ_Pic(x-5, y-5, NULL, 250, 155, 0, 0, 0, 0.75, 0);
5391 range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
5392 for (lightindex = 0;lightindex < range;lightindex++)
5394 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
5397 if (light == r_shadow_selectedlight)
5398 lightnumber = lightindex;
5401 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;
5402 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;
5404 if (r_shadow_selectedlight == NULL)
5406 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;
5407 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;
5408 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;
5409 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;
5410 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;
5411 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;
5412 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;
5413 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;
5414 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;
5415 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;
5416 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;
5417 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;
5418 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;
5419 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;
5420 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;
5423 void R_Shadow_EditLights_ToggleShadow_f(void)
5425 if (!r_editlights.integer)
5427 Con_Print("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
5430 if (!r_shadow_selectedlight)
5432 Con_Print("No selected light.\n");
5435 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);
5438 void R_Shadow_EditLights_ToggleCorona_f(void)
5440 if (!r_editlights.integer)
5442 Con_Print("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
5445 if (!r_shadow_selectedlight)
5447 Con_Print("No selected light.\n");
5450 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);
5453 void R_Shadow_EditLights_Remove_f(void)
5455 if (!r_editlights.integer)
5457 Con_Print("Cannot remove light when not in editing mode. Set r_editlights to 1.\n");
5460 if (!r_shadow_selectedlight)
5462 Con_Print("No selected light.\n");
5465 R_Shadow_FreeWorldLight(r_shadow_selectedlight);
5466 r_shadow_selectedlight = NULL;
5469 void R_Shadow_EditLights_Help_f(void)
5472 "Documentation on r_editlights system:\n"
5474 "r_editlights : enable/disable editing mode\n"
5475 "r_editlights_cursordistance : maximum distance of cursor from eye\n"
5476 "r_editlights_cursorpushback : push back cursor this far from surface\n"
5477 "r_editlights_cursorpushoff : push cursor off surface this far\n"
5478 "r_editlights_cursorgrid : snap cursor to grid of this size\n"
5479 "r_editlights_quakelightsizescale : imported quake light entity size scaling\n"
5481 "r_editlights_help : this help\n"
5482 "r_editlights_clear : remove all lights\n"
5483 "r_editlights_reload : reload .rtlights, .lights file, or entities\n"
5484 "r_editlights_save : save to .rtlights file\n"
5485 "r_editlights_spawn : create a light with default settings\n"
5486 "r_editlights_edit command : edit selected light - more documentation below\n"
5487 "r_editlights_remove : remove selected light\n"
5488 "r_editlights_toggleshadow : toggles on/off selected light's shadow property\n"
5489 "r_editlights_importlightentitiesfrommap : reload light entities\n"
5490 "r_editlights_importlightsfile : reload .light file (produced by hlight)\n"
5492 "origin x y z : set light location\n"
5493 "originx x: set x component of light location\n"
5494 "originy y: set y component of light location\n"
5495 "originz z: set z component of light location\n"
5496 "move x y z : adjust light location\n"
5497 "movex x: adjust x component of light location\n"
5498 "movey y: adjust y component of light location\n"
5499 "movez z: adjust z component of light location\n"
5500 "angles x y z : set light angles\n"
5501 "anglesx x: set x component of light angles\n"
5502 "anglesy y: set y component of light angles\n"
5503 "anglesz z: set z component of light angles\n"
5504 "color r g b : set color of light (can be brighter than 1 1 1)\n"
5505 "radius radius : set radius (size) of light\n"
5506 "colorscale grey : multiply color of light (1 does nothing)\n"
5507 "colorscale r g b : multiply color of light (1 1 1 does nothing)\n"
5508 "radiusscale scale : multiply radius (size) of light (1 does nothing)\n"
5509 "sizescale scale : multiply radius (size) of light (1 does nothing)\n"
5510 "style style : set lightstyle of light (flickering patterns, switches, etc)\n"
5511 "cubemap basename : set filter cubemap of light (not yet supported)\n"
5512 "shadows 1/0 : turn on/off shadows\n"
5513 "corona n : set corona intensity\n"
5514 "coronasize n : set corona size (0-1)\n"
5515 "ambient n : set ambient intensity (0-1)\n"
5516 "diffuse n : set diffuse intensity (0-1)\n"
5517 "specular n : set specular intensity (0-1)\n"
5518 "normalmode 1/0 : turn on/off rendering of this light in rtworld 0 mode\n"
5519 "realtimemode 1/0 : turn on/off rendering of this light in rtworld 1 mode\n"
5520 "<nothing> : print light properties to console\n"
5524 void R_Shadow_EditLights_CopyInfo_f(void)
5526 if (!r_editlights.integer)
5528 Con_Print("Cannot copy light info when not in editing mode. Set r_editlights to 1.\n");
5531 if (!r_shadow_selectedlight)
5533 Con_Print("No selected light.\n");
5536 VectorCopy(r_shadow_selectedlight->angles, r_shadow_bufferlight.angles);
5537 VectorCopy(r_shadow_selectedlight->color, r_shadow_bufferlight.color);
5538 r_shadow_bufferlight.radius = r_shadow_selectedlight->radius;
5539 r_shadow_bufferlight.style = r_shadow_selectedlight->style;
5540 if (r_shadow_selectedlight->cubemapname)
5541 strlcpy(r_shadow_bufferlight.cubemapname, r_shadow_selectedlight->cubemapname, sizeof(r_shadow_bufferlight.cubemapname));
5543 r_shadow_bufferlight.cubemapname[0] = 0;
5544 r_shadow_bufferlight.shadow = r_shadow_selectedlight->shadow;
5545 r_shadow_bufferlight.corona = r_shadow_selectedlight->corona;
5546 r_shadow_bufferlight.coronasizescale = r_shadow_selectedlight->coronasizescale;
5547 r_shadow_bufferlight.ambientscale = r_shadow_selectedlight->ambientscale;
5548 r_shadow_bufferlight.diffusescale = r_shadow_selectedlight->diffusescale;
5549 r_shadow_bufferlight.specularscale = r_shadow_selectedlight->specularscale;
5550 r_shadow_bufferlight.flags = r_shadow_selectedlight->flags;
5553 void R_Shadow_EditLights_PasteInfo_f(void)
5555 if (!r_editlights.integer)
5557 Con_Print("Cannot paste light info when not in editing mode. Set r_editlights to 1.\n");
5560 if (!r_shadow_selectedlight)
5562 Con_Print("No selected light.\n");
5565 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);
5568 void R_Shadow_EditLights_Init(void)
5570 Cvar_RegisterVariable(&r_editlights);
5571 Cvar_RegisterVariable(&r_editlights_cursordistance);
5572 Cvar_RegisterVariable(&r_editlights_cursorpushback);
5573 Cvar_RegisterVariable(&r_editlights_cursorpushoff);
5574 Cvar_RegisterVariable(&r_editlights_cursorgrid);
5575 Cvar_RegisterVariable(&r_editlights_quakelightsizescale);
5576 Cmd_AddCommand("r_editlights_help", R_Shadow_EditLights_Help_f, "prints documentation on console commands and variables in rtlight editing system");
5577 Cmd_AddCommand("r_editlights_clear", R_Shadow_EditLights_Clear_f, "removes all world lights (let there be darkness!)");
5578 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)");
5579 Cmd_AddCommand("r_editlights_save", R_Shadow_EditLights_Save_f, "save .rtlights file for current level");
5580 Cmd_AddCommand("r_editlights_spawn", R_Shadow_EditLights_Spawn_f, "creates a light with default properties (let there be light!)");
5581 Cmd_AddCommand("r_editlights_edit", R_Shadow_EditLights_Edit_f, "changes a property on the selected light");
5582 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)");
5583 Cmd_AddCommand("r_editlights_remove", R_Shadow_EditLights_Remove_f, "remove selected light");
5584 Cmd_AddCommand("r_editlights_toggleshadow", R_Shadow_EditLights_ToggleShadow_f, "toggle on/off the shadow option on the selected light");
5585 Cmd_AddCommand("r_editlights_togglecorona", R_Shadow_EditLights_ToggleCorona_f, "toggle on/off the corona option on the selected light");
5586 Cmd_AddCommand("r_editlights_importlightentitiesfrommap", R_Shadow_EditLights_ImportLightEntitiesFromMap_f, "load lights from .ent file or map entities (ignoring .rtlights or .lights file)");
5587 Cmd_AddCommand("r_editlights_importlightsfile", R_Shadow_EditLights_ImportLightsFile_f, "load lights from .lights file (ignoring .rtlights or .ent files and map entities)");
5588 Cmd_AddCommand("r_editlights_copyinfo", R_Shadow_EditLights_CopyInfo_f, "store a copy of all properties (except origin) of the selected light");
5589 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)");
5595 =============================================================================
5599 =============================================================================
5602 void R_CompleteLightPoint(vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal, const vec3_t p, int dynamic)
5604 VectorClear(diffusecolor);
5605 VectorClear(diffusenormal);
5607 if (!r_fullbright.integer && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.LightPoint)
5609 ambientcolor[0] = ambientcolor[1] = ambientcolor[2] = r_refdef.scene.ambient * (2.0f / 128.0f);
5610 r_refdef.scene.worldmodel->brush.LightPoint(r_refdef.scene.worldmodel, p, ambientcolor, diffusecolor, diffusenormal);
5613 VectorSet(ambientcolor, 1, 1, 1);
5620 for (i = 0;i < r_refdef.scene.numlights;i++)
5622 light = r_refdef.scene.lights[i];
5623 Matrix4x4_Transform(&light->matrix_worldtolight, p, v);
5624 f = 1 - VectorLength2(v);
5625 if (f > 0 && CL_Move(p, vec3_origin, vec3_origin, light->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false).fraction == 1)
5626 VectorMA(ambientcolor, f, light->currentcolor, ambientcolor);