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"
142 #include "dpsoftrast.h"
146 extern LPDIRECT3DDEVICE9 vid_d3d9dev;
149 extern void R_Shadow_EditLights_Init(void);
151 typedef enum r_shadow_rendermode_e
153 R_SHADOW_RENDERMODE_NONE,
154 R_SHADOW_RENDERMODE_ZPASS_STENCIL,
155 R_SHADOW_RENDERMODE_ZPASS_SEPARATESTENCIL,
156 R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE,
157 R_SHADOW_RENDERMODE_ZFAIL_STENCIL,
158 R_SHADOW_RENDERMODE_ZFAIL_SEPARATESTENCIL,
159 R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE,
160 R_SHADOW_RENDERMODE_LIGHT_VERTEX,
161 R_SHADOW_RENDERMODE_LIGHT_VERTEX2DATTEN,
162 R_SHADOW_RENDERMODE_LIGHT_VERTEX2D1DATTEN,
163 R_SHADOW_RENDERMODE_LIGHT_VERTEX3DATTEN,
164 R_SHADOW_RENDERMODE_LIGHT_GLSL,
165 R_SHADOW_RENDERMODE_VISIBLEVOLUMES,
166 R_SHADOW_RENDERMODE_VISIBLELIGHTING,
167 R_SHADOW_RENDERMODE_SHADOWMAP2D
169 r_shadow_rendermode_t;
171 typedef enum r_shadow_shadowmode_e
173 R_SHADOW_SHADOWMODE_STENCIL,
174 R_SHADOW_SHADOWMODE_SHADOWMAP2D
176 r_shadow_shadowmode_t;
178 r_shadow_rendermode_t r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
179 r_shadow_rendermode_t r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_NONE;
180 r_shadow_rendermode_t r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_NONE;
181 r_shadow_rendermode_t r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_NONE;
182 qboolean r_shadow_usingshadowmap2d;
183 qboolean r_shadow_usingshadowmaportho;
184 int r_shadow_shadowmapside;
185 float r_shadow_shadowmap_texturescale[2];
186 float r_shadow_shadowmap_parameters[4];
188 int r_shadow_drawbuffer;
189 int r_shadow_readbuffer;
191 int r_shadow_cullface_front, r_shadow_cullface_back;
192 GLuint r_shadow_fbo2d;
193 r_shadow_shadowmode_t r_shadow_shadowmode;
194 int r_shadow_shadowmapfilterquality;
195 int r_shadow_shadowmapdepthbits;
196 int r_shadow_shadowmapmaxsize;
197 qboolean r_shadow_shadowmapvsdct;
198 qboolean r_shadow_shadowmapsampler;
199 int r_shadow_shadowmappcf;
200 int r_shadow_shadowmapborder;
201 matrix4x4_t r_shadow_shadowmapmatrix;
202 int r_shadow_lightscissor[4];
203 qboolean r_shadow_usingdeferredprepass;
205 int maxshadowtriangles;
208 int maxshadowvertices;
209 float *shadowvertex3f;
219 unsigned char *shadowsides;
220 int *shadowsideslist;
227 int r_shadow_buffer_numleafpvsbytes;
228 unsigned char *r_shadow_buffer_visitingleafpvs;
229 unsigned char *r_shadow_buffer_leafpvs;
230 int *r_shadow_buffer_leaflist;
232 int r_shadow_buffer_numsurfacepvsbytes;
233 unsigned char *r_shadow_buffer_surfacepvs;
234 int *r_shadow_buffer_surfacelist;
235 unsigned char *r_shadow_buffer_surfacesides;
237 int r_shadow_buffer_numshadowtrispvsbytes;
238 unsigned char *r_shadow_buffer_shadowtrispvs;
239 int r_shadow_buffer_numlighttrispvsbytes;
240 unsigned char *r_shadow_buffer_lighttrispvs;
242 rtexturepool_t *r_shadow_texturepool;
243 rtexture_t *r_shadow_attenuationgradienttexture;
244 rtexture_t *r_shadow_attenuation2dtexture;
245 rtexture_t *r_shadow_attenuation3dtexture;
246 skinframe_t *r_shadow_lightcorona;
247 rtexture_t *r_shadow_shadowmap2dtexture;
248 rtexture_t *r_shadow_shadowmap2dcolortexture;
249 rtexture_t *r_shadow_shadowmapvsdcttexture;
250 int r_shadow_shadowmapsize; // changes for each light based on distance
251 int r_shadow_shadowmaplod; // changes for each light based on distance
253 GLuint r_shadow_prepassgeometryfbo;
254 GLuint r_shadow_prepasslightingdiffusespecularfbo;
255 GLuint r_shadow_prepasslightingdiffusefbo;
256 int r_shadow_prepass_width;
257 int r_shadow_prepass_height;
258 rtexture_t *r_shadow_prepassgeometrydepthtexture;
259 rtexture_t *r_shadow_prepassgeometrydepthcolortexture;
260 rtexture_t *r_shadow_prepassgeometrynormalmaptexture;
261 rtexture_t *r_shadow_prepasslightingdiffusetexture;
262 rtexture_t *r_shadow_prepasslightingspeculartexture;
264 // lights are reloaded when this changes
265 char r_shadow_mapname[MAX_QPATH];
267 // used only for light filters (cubemaps)
268 rtexturepool_t *r_shadow_filters_texturepool;
270 static const GLenum r_shadow_prepasslightingdrawbuffers[2] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT};
272 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"};
273 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"};
274 cvar_t r_shadow_debuglight = {0, "r_shadow_debuglight", "-1", "renders only one light, for level design purposes or debugging"};
275 cvar_t r_shadow_deferred = {CVAR_SAVE, "r_shadow_deferred", "0", "uses image-based lighting instead of geometry-based lighting, the method used renders a depth image and a normalmap image, renders lights into separate diffuse and specular images, and then combines this into the normal rendering, requires r_shadow_shadowmapping"};
276 cvar_t r_shadow_deferred_8bitrange = {CVAR_SAVE, "r_shadow_deferred_8bitrange", "2", "dynamic range of image-based lighting when using 32bit color (does not apply to fp)"};
277 //cvar_t r_shadow_deferred_fp = {CVAR_SAVE, "r_shadow_deferred_fp", "0", "use 16bit (1) or 32bit (2) floating point for accumulation of image-based lighting"};
278 cvar_t r_shadow_usebihculling = {0, "r_shadow_usebihculling", "1", "use BIH (Bounding Interval Hierarchy) for culling lit surfaces instead of BSP (Binary Space Partitioning)"};
279 cvar_t r_shadow_usenormalmap = {CVAR_SAVE, "r_shadow_usenormalmap", "1", "enables use of directional shading on lights"};
280 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)"};
281 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"};
282 cvar_t r_shadow_glossintensity = {0, "r_shadow_glossintensity", "1", "how bright textured glossmaps should look if r_shadow_gloss is 1 or 2"};
283 cvar_t r_shadow_glossexponent = {0, "r_shadow_glossexponent", "32", "how 'sharp' the gloss should appear (specular power)"};
284 cvar_t r_shadow_gloss2exponent = {0, "r_shadow_gloss2exponent", "32", "same as r_shadow_glossexponent but for forced gloss (gloss 2) surfaces"};
285 cvar_t r_shadow_glossexact = {0, "r_shadow_glossexact", "0", "use exact reflection math for gloss (slightly slower, but should look a tad better)"};
286 cvar_t r_shadow_lightattenuationdividebias = {0, "r_shadow_lightattenuationdividebias", "1", "changes attenuation texture generation"};
287 cvar_t r_shadow_lightattenuationlinearscale = {0, "r_shadow_lightattenuationlinearscale", "2", "changes attenuation texture generation"};
288 cvar_t r_shadow_lightintensityscale = {0, "r_shadow_lightintensityscale", "1", "renders all world lights brighter or darker"};
289 cvar_t r_shadow_lightradiusscale = {0, "r_shadow_lightradiusscale", "1", "renders all world lights larger or smaller"};
290 cvar_t r_shadow_projectdistance = {0, "r_shadow_projectdistance", "0", "how far to cast shadows"};
291 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)"};
292 cvar_t r_shadow_realtime_dlight = {CVAR_SAVE, "r_shadow_realtime_dlight", "1", "enables rendering of dynamic lights such as explosions and rocket light"};
293 cvar_t r_shadow_realtime_dlight_shadows = {CVAR_SAVE, "r_shadow_realtime_dlight_shadows", "1", "enables rendering of shadows from dynamic lights"};
294 cvar_t r_shadow_realtime_dlight_svbspculling = {0, "r_shadow_realtime_dlight_svbspculling", "0", "enables svbsp optimization on dynamic lights (very slow!)"};
295 cvar_t r_shadow_realtime_dlight_portalculling = {0, "r_shadow_realtime_dlight_portalculling", "0", "enables portal optimization on dynamic lights (slow!)"};
296 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)"};
297 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"};
298 cvar_t r_shadow_realtime_world_shadows = {CVAR_SAVE, "r_shadow_realtime_world_shadows", "1", "enables rendering of shadows from world lights"};
299 cvar_t r_shadow_realtime_world_compile = {0, "r_shadow_realtime_world_compile", "1", "enables compilation of world lights for higher performance rendering"};
300 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"};
301 cvar_t r_shadow_realtime_world_compilesvbsp = {0, "r_shadow_realtime_world_compilesvbsp", "1", "enables svbsp optimization during compilation (slower than compileportalculling but more exact)"};
302 cvar_t r_shadow_realtime_world_compileportalculling = {0, "r_shadow_realtime_world_compileportalculling", "1", "enables portal-based culling optimization during compilation (overrides compilesvbsp)"};
303 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)"};
304 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"};
305 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)"};
306 cvar_t r_shadow_shadowmapping_depthbits = {CVAR_SAVE, "r_shadow_shadowmapping_depthbits", "24", "requested minimum shadowmap texture depth bits"};
307 cvar_t r_shadow_shadowmapping_vsdct = {CVAR_SAVE, "r_shadow_shadowmapping_vsdct", "1", "enables use of virtual shadow depth cube texture"};
308 cvar_t r_shadow_shadowmapping_minsize = {CVAR_SAVE, "r_shadow_shadowmapping_minsize", "32", "shadowmap size limit"};
309 cvar_t r_shadow_shadowmapping_maxsize = {CVAR_SAVE, "r_shadow_shadowmapping_maxsize", "512", "shadowmap size limit"};
310 cvar_t r_shadow_shadowmapping_precision = {CVAR_SAVE, "r_shadow_shadowmapping_precision", "1", "makes shadowmaps have a maximum resolution of this number of pixels per light source radius unit such that, for example, at precision 0.5 a light with radius 200 will have a maximum resolution of 100 pixels"};
311 //cvar_t r_shadow_shadowmapping_lod_bias = {CVAR_SAVE, "r_shadow_shadowmapping_lod_bias", "16", "shadowmap size bias"};
312 //cvar_t r_shadow_shadowmapping_lod_scale = {CVAR_SAVE, "r_shadow_shadowmapping_lod_scale", "128", "shadowmap size scaling parameter"};
313 cvar_t r_shadow_shadowmapping_bordersize = {CVAR_SAVE, "r_shadow_shadowmapping_bordersize", "4", "shadowmap size bias for filtering"};
314 cvar_t r_shadow_shadowmapping_nearclip = {CVAR_SAVE, "r_shadow_shadowmapping_nearclip", "1", "shadowmap nearclip in world units"};
315 cvar_t r_shadow_shadowmapping_bias = {CVAR_SAVE, "r_shadow_shadowmapping_bias", "0.03", "shadowmap bias parameter (this is multiplied by nearclip * 1024 / lodsize)"};
316 cvar_t r_shadow_shadowmapping_polygonfactor = {CVAR_SAVE, "r_shadow_shadowmapping_polygonfactor", "2", "slope-dependent shadowmapping bias"};
317 cvar_t r_shadow_shadowmapping_polygonoffset = {CVAR_SAVE, "r_shadow_shadowmapping_polygonoffset", "0", "constant shadowmapping bias"};
318 cvar_t r_shadow_sortsurfaces = {0, "r_shadow_sortsurfaces", "1", "improve performance by sorting illuminated surfaces by texture"};
319 cvar_t r_shadow_polygonfactor = {0, "r_shadow_polygonfactor", "0", "how much to enlarge shadow volume polygons when rendering (should be 0!)"};
320 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)"};
321 cvar_t r_shadow_texture3d = {0, "r_shadow_texture3d", "1", "use 3D voxel textures for spherical attenuation rather than cylindrical (does not affect OpenGL 2.0 render path)"};
322 cvar_t r_shadow_particletrace = {CVAR_SAVE, "r_shadow_particletrace", "0", "perform particle tracing for indirect lighting (Global Illumination / radiosity), requires r_shadow_deferred 1, requires r_shadow_realtime_world 1, EXTREMELY SLOW"};
323 cvar_t r_shadow_particletrace_intensity = {CVAR_SAVE, "r_shadow_particletrace_intensity", "128", "overall brightness of particle traced radiosity"};
324 cvar_t r_shadow_particletrace_size = {CVAR_SAVE, "r_shadow_particletrace_size", "32", "particles produce bounce lights of this radius"};
325 cvar_t r_shadow_particletrace_radiusscale = {CVAR_SAVE, "r_shadow_particletrace_radiusscale", "1", "particles stop at this fraction of light radius"};
326 cvar_t r_shadow_particletrace_maxbounce = {CVAR_SAVE, "r_shadow_particletrace_maxbounce", "1", "maximum number of bounces for a particle (minimum is 1)"};
327 cvar_t r_shadow_particletrace_bounceintensity = {CVAR_SAVE, "r_shadow_particletrace_bounceintensity", "1", "amount of energy carried over after each bounce"};
328 cvar_t r_shadow_particletrace_particlespacing = {CVAR_SAVE, "r_shadow_particletrace_particlespacing", "0.25", "overlap setting in terms of particle size, this affects how many particles are used"};
329 cvar_t r_shadow_particletrace_updatepercentage = {CVAR_SAVE, "r_shadow_particletrace_updatepercentage", "0.01", "update this fraction of the particles of a light each frame (0 = best performance)"};
330 cvar_t r_shadow_bouncegrid = {CVAR_SAVE, "r_shadow_bouncegrid", "0", "perform particle tracing for indirect lighting (Global Illumination / radiosity) using a 3D texture covering the scene, requires r_shadow_realtime_world 1"};
331 cvar_t r_shadow_bouncegrid_dlightparticlemultiplier = {CVAR_SAVE, "r_shadow_bouncegrid_dlightparticlemultiplier", "0", "if set to a high value like 16 this can make dlights look great, but 0 is recommended for performance reasons"};
332 cvar_t r_shadow_bouncegrid_hitmodels = {CVAR_SAVE, "r_shadow_bouncegrid_hitmodels", "0", "enables hitting character model geometry (SLOW)"};
333 cvar_t r_shadow_bouncegrid_intensity = {CVAR_SAVE, "r_shadow_bouncegrid_intensity", "1", "overall brightness of bouncegrid texture"};
334 cvar_t r_shadow_bouncegrid_lightradiusscale = {CVAR_SAVE, "r_shadow_bouncegrid_lightradiusscale", "2", "particles stop at this fraction of light radius (can be more than 1)"};
335 cvar_t r_shadow_bouncegrid_maxbounce = {CVAR_SAVE, "r_shadow_bouncegrid_maxbounce", "16", "maximum number of bounces for a particle (minimum is 1)"};
336 cvar_t r_shadow_bouncegrid_particlebounceintensity = {CVAR_SAVE, "r_shadow_bouncegrid_particlebounceintensity", "1", "amount of energy carried over after each bounce"};
337 cvar_t r_shadow_bouncegrid_particleintensity = {CVAR_SAVE, "r_shadow_bouncegrid_particleintensity", "16", "brightness of particles contributing to bouncegrid texture"};
338 cvar_t r_shadow_bouncegrid_particlespacing = {CVAR_SAVE, "r_shadow_bouncegrid_particlespacing", "32", "emit one particle per this many units (squared) of radius (squared)"};
339 cvar_t r_shadow_bouncegrid_spacingx = {CVAR_SAVE, "r_shadow_bouncegrid_spacingx", "64", "unit size of bouncegrid pixel on X axis"};
340 cvar_t r_shadow_bouncegrid_spacingy = {CVAR_SAVE, "r_shadow_bouncegrid_spacingy", "64", "unit size of bouncegrid pixel on Y axis"};
341 cvar_t r_shadow_bouncegrid_spacingz = {CVAR_SAVE, "r_shadow_bouncegrid_spacingz", "64", "unit size of bouncegrid pixel on Z axis"};
342 cvar_t r_shadow_bouncegrid_stablerandom = {CVAR_SAVE, "r_shadow_bouncegrid_stablerandom", "1", "make particle distribution consistent from frame to frame"};
343 cvar_t r_shadow_bouncegrid_updateinterval = {CVAR_SAVE, "r_shadow_bouncegrid_updateinterval", "0", "update bouncegrid texture once per this many seconds, useful values are 0, 0.05, or 1000000"};
344 cvar_t r_shadow_bouncegrid_x = {CVAR_SAVE, "r_shadow_bouncegrid_x", "128", "maximum texture size of bouncegrid on X axis"};
345 cvar_t r_shadow_bouncegrid_y = {CVAR_SAVE, "r_shadow_bouncegrid_y", "128", "maximum texture size of bouncegrid on Y axis"};
346 cvar_t r_shadow_bouncegrid_z = {CVAR_SAVE, "r_shadow_bouncegrid_z", "64", "maximum texture size of bouncegrid on Z axis"};
347 cvar_t r_coronas = {CVAR_SAVE, "r_coronas", "1", "brightness of corona flare effects around certain lights, 0 disables corona effects"};
348 cvar_t r_coronas_occlusionsizescale = {CVAR_SAVE, "r_coronas_occlusionsizescale", "0.1", "size of light source for corona occlusion checksum the proportion of hidden pixels controls corona intensity"};
349 cvar_t r_coronas_occlusionquery = {CVAR_SAVE, "r_coronas_occlusionquery", "1", "use GL_ARB_occlusion_query extension if supported (fades coronas according to visibility)"};
350 cvar_t gl_flashblend = {CVAR_SAVE, "gl_flashblend", "0", "render bright coronas for dynamic lights instead of actual lighting, fast but ugly"};
351 cvar_t gl_ext_separatestencil = {0, "gl_ext_separatestencil", "1", "make use of OpenGL 2.0 glStencilOpSeparate or GL_ATI_separate_stencil extension"};
352 cvar_t gl_ext_stenciltwoside = {0, "gl_ext_stenciltwoside", "1", "make use of GL_EXT_stenciltwoside extension (NVIDIA only)"};
353 cvar_t r_editlights = {0, "r_editlights", "0", "enables .rtlights file editing mode"};
354 cvar_t r_editlights_cursordistance = {0, "r_editlights_cursordistance", "1024", "maximum distance of cursor from eye"};
355 cvar_t r_editlights_cursorpushback = {0, "r_editlights_cursorpushback", "0", "how far to pull the cursor back toward the eye"};
356 cvar_t r_editlights_cursorpushoff = {0, "r_editlights_cursorpushoff", "4", "how far to push the cursor off the impacted surface"};
357 cvar_t r_editlights_cursorgrid = {0, "r_editlights_cursorgrid", "4", "snaps cursor to this grid size"};
358 cvar_t r_editlights_quakelightsizescale = {CVAR_SAVE, "r_editlights_quakelightsizescale", "1", "changes size of light entities loaded from a map"};
360 rtexture_t *r_shadow_bouncegridtexture;
361 matrix4x4_t r_shadow_bouncegridmatrix;
362 vec_t r_shadow_bouncegridintensity;
363 static double r_shadow_bouncegridtime;
364 static int r_shadow_bouncegridresolution[3];
366 // note the table actually includes one more value, just to avoid the need to clamp the distance index due to minor math error
367 #define ATTENTABLESIZE 256
368 // 1D gradient, 2D circle and 3D sphere attenuation textures
369 #define ATTEN1DSIZE 32
370 #define ATTEN2DSIZE 64
371 #define ATTEN3DSIZE 32
373 static float r_shadow_attendividebias; // r_shadow_lightattenuationdividebias
374 static float r_shadow_attenlinearscale; // r_shadow_lightattenuationlinearscale
375 static float r_shadow_attentable[ATTENTABLESIZE+1];
377 rtlight_t *r_shadow_compilingrtlight;
378 static memexpandablearray_t r_shadow_worldlightsarray;
379 dlight_t *r_shadow_selectedlight;
380 dlight_t r_shadow_bufferlight;
381 vec3_t r_editlights_cursorlocation;
382 qboolean r_editlights_lockcursor;
384 extern int con_vislines;
386 void R_Shadow_UncompileWorldLights(void);
387 void R_Shadow_ClearWorldLights(void);
388 void R_Shadow_SaveWorldLights(void);
389 void R_Shadow_LoadWorldLights(void);
390 void R_Shadow_LoadLightsFile(void);
391 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void);
392 void R_Shadow_EditLights_Reload_f(void);
393 void R_Shadow_ValidateCvars(void);
394 static void R_Shadow_MakeTextures(void);
396 #define EDLIGHTSPRSIZE 8
397 skinframe_t *r_editlights_sprcursor;
398 skinframe_t *r_editlights_sprlight;
399 skinframe_t *r_editlights_sprnoshadowlight;
400 skinframe_t *r_editlights_sprcubemaplight;
401 skinframe_t *r_editlights_sprcubemapnoshadowlight;
402 skinframe_t *r_editlights_sprselection;
404 void R_Shadow_SetShadowMode(void)
406 r_shadow_shadowmapmaxsize = bound(1, r_shadow_shadowmapping_maxsize.integer, (int)vid.maxtexturesize_2d / 4);
407 r_shadow_shadowmapvsdct = r_shadow_shadowmapping_vsdct.integer != 0 && vid.renderpath == RENDERPATH_GL20;
408 r_shadow_shadowmapfilterquality = r_shadow_shadowmapping_filterquality.integer;
409 r_shadow_shadowmapdepthbits = r_shadow_shadowmapping_depthbits.integer;
410 r_shadow_shadowmapborder = bound(0, r_shadow_shadowmapping_bordersize.integer, 16);
411 r_shadow_shadowmaplod = -1;
412 r_shadow_shadowmapsize = 0;
413 r_shadow_shadowmapsampler = false;
414 r_shadow_shadowmappcf = 0;
415 r_shadow_shadowmode = R_SHADOW_SHADOWMODE_STENCIL;
416 if ((r_shadow_shadowmapping.integer || r_shadow_deferred.integer) && vid.support.ext_framebuffer_object)
418 switch(vid.renderpath)
420 case RENDERPATH_GL20:
421 if(r_shadow_shadowmapfilterquality < 0)
423 if(vid.support.amd_texture_texture4 || vid.support.arb_texture_gather)
424 r_shadow_shadowmappcf = 1;
425 else if(strstr(gl_vendor, "NVIDIA") || strstr(gl_renderer, "Radeon HD"))
427 r_shadow_shadowmapsampler = vid.support.arb_shadow;
428 r_shadow_shadowmappcf = 1;
430 else if(strstr(gl_vendor, "ATI"))
431 r_shadow_shadowmappcf = 1;
433 r_shadow_shadowmapsampler = vid.support.arb_shadow;
437 switch (r_shadow_shadowmapfilterquality)
440 r_shadow_shadowmapsampler = vid.support.arb_shadow;
443 r_shadow_shadowmapsampler = vid.support.arb_shadow;
444 r_shadow_shadowmappcf = 1;
447 r_shadow_shadowmappcf = 1;
450 r_shadow_shadowmappcf = 2;
454 r_shadow_shadowmode = R_SHADOW_SHADOWMODE_SHADOWMAP2D;
456 case RENDERPATH_D3D9:
457 case RENDERPATH_D3D10:
458 case RENDERPATH_D3D11:
459 case RENDERPATH_SOFT:
460 r_shadow_shadowmapsampler = false;
461 r_shadow_shadowmappcf = 1;
462 r_shadow_shadowmode = R_SHADOW_SHADOWMODE_SHADOWMAP2D;
464 case RENDERPATH_GL13:
466 case RENDERPATH_GL11:
468 case RENDERPATH_GLES2:
474 qboolean R_Shadow_ShadowMappingEnabled(void)
476 switch (r_shadow_shadowmode)
478 case R_SHADOW_SHADOWMODE_SHADOWMAP2D:
485 void R_Shadow_FreeShadowMaps(void)
487 R_Shadow_SetShadowMode();
489 R_Mesh_DestroyFramebufferObject(r_shadow_fbo2d);
493 if (r_shadow_shadowmap2dtexture)
494 R_FreeTexture(r_shadow_shadowmap2dtexture);
495 r_shadow_shadowmap2dtexture = NULL;
497 if (r_shadow_shadowmap2dcolortexture)
498 R_FreeTexture(r_shadow_shadowmap2dcolortexture);
499 r_shadow_shadowmap2dcolortexture = NULL;
501 if (r_shadow_shadowmapvsdcttexture)
502 R_FreeTexture(r_shadow_shadowmapvsdcttexture);
503 r_shadow_shadowmapvsdcttexture = NULL;
506 void r_shadow_start(void)
508 // allocate vertex processing arrays
509 r_shadow_bouncegridtexture = NULL;
510 r_shadow_attenuationgradienttexture = NULL;
511 r_shadow_attenuation2dtexture = NULL;
512 r_shadow_attenuation3dtexture = NULL;
513 r_shadow_shadowmode = R_SHADOW_SHADOWMODE_STENCIL;
514 r_shadow_shadowmap2dtexture = NULL;
515 r_shadow_shadowmap2dcolortexture = NULL;
516 r_shadow_shadowmapvsdcttexture = NULL;
517 r_shadow_shadowmapmaxsize = 0;
518 r_shadow_shadowmapsize = 0;
519 r_shadow_shadowmaplod = 0;
520 r_shadow_shadowmapfilterquality = -1;
521 r_shadow_shadowmapdepthbits = 0;
522 r_shadow_shadowmapvsdct = false;
523 r_shadow_shadowmapsampler = false;
524 r_shadow_shadowmappcf = 0;
527 R_Shadow_FreeShadowMaps();
529 r_shadow_texturepool = NULL;
530 r_shadow_filters_texturepool = NULL;
531 R_Shadow_ValidateCvars();
532 R_Shadow_MakeTextures();
533 maxshadowtriangles = 0;
534 shadowelements = NULL;
535 maxshadowvertices = 0;
536 shadowvertex3f = NULL;
544 shadowmarklist = NULL;
549 shadowsideslist = NULL;
550 r_shadow_buffer_numleafpvsbytes = 0;
551 r_shadow_buffer_visitingleafpvs = NULL;
552 r_shadow_buffer_leafpvs = NULL;
553 r_shadow_buffer_leaflist = NULL;
554 r_shadow_buffer_numsurfacepvsbytes = 0;
555 r_shadow_buffer_surfacepvs = NULL;
556 r_shadow_buffer_surfacelist = NULL;
557 r_shadow_buffer_surfacesides = NULL;
558 r_shadow_buffer_numshadowtrispvsbytes = 0;
559 r_shadow_buffer_shadowtrispvs = NULL;
560 r_shadow_buffer_numlighttrispvsbytes = 0;
561 r_shadow_buffer_lighttrispvs = NULL;
563 r_shadow_usingdeferredprepass = false;
564 r_shadow_prepass_width = r_shadow_prepass_height = 0;
567 static void R_Shadow_FreeDeferred(void);
568 void r_shadow_shutdown(void)
571 R_Shadow_UncompileWorldLights();
573 R_Shadow_FreeShadowMaps();
575 r_shadow_usingdeferredprepass = false;
576 if (r_shadow_prepass_width)
577 R_Shadow_FreeDeferred();
578 r_shadow_prepass_width = r_shadow_prepass_height = 0;
581 r_shadow_bouncegridtexture = NULL;
582 r_shadow_attenuationgradienttexture = NULL;
583 r_shadow_attenuation2dtexture = NULL;
584 r_shadow_attenuation3dtexture = NULL;
585 R_FreeTexturePool(&r_shadow_texturepool);
586 R_FreeTexturePool(&r_shadow_filters_texturepool);
587 maxshadowtriangles = 0;
589 Mem_Free(shadowelements);
590 shadowelements = NULL;
592 Mem_Free(shadowvertex3f);
593 shadowvertex3f = NULL;
596 Mem_Free(vertexupdate);
599 Mem_Free(vertexremap);
605 Mem_Free(shadowmark);
608 Mem_Free(shadowmarklist);
609 shadowmarklist = NULL;
614 Mem_Free(shadowsides);
617 Mem_Free(shadowsideslist);
618 shadowsideslist = NULL;
619 r_shadow_buffer_numleafpvsbytes = 0;
620 if (r_shadow_buffer_visitingleafpvs)
621 Mem_Free(r_shadow_buffer_visitingleafpvs);
622 r_shadow_buffer_visitingleafpvs = NULL;
623 if (r_shadow_buffer_leafpvs)
624 Mem_Free(r_shadow_buffer_leafpvs);
625 r_shadow_buffer_leafpvs = NULL;
626 if (r_shadow_buffer_leaflist)
627 Mem_Free(r_shadow_buffer_leaflist);
628 r_shadow_buffer_leaflist = NULL;
629 r_shadow_buffer_numsurfacepvsbytes = 0;
630 if (r_shadow_buffer_surfacepvs)
631 Mem_Free(r_shadow_buffer_surfacepvs);
632 r_shadow_buffer_surfacepvs = NULL;
633 if (r_shadow_buffer_surfacelist)
634 Mem_Free(r_shadow_buffer_surfacelist);
635 r_shadow_buffer_surfacelist = NULL;
636 if (r_shadow_buffer_surfacesides)
637 Mem_Free(r_shadow_buffer_surfacesides);
638 r_shadow_buffer_surfacesides = NULL;
639 r_shadow_buffer_numshadowtrispvsbytes = 0;
640 if (r_shadow_buffer_shadowtrispvs)
641 Mem_Free(r_shadow_buffer_shadowtrispvs);
642 r_shadow_buffer_numlighttrispvsbytes = 0;
643 if (r_shadow_buffer_lighttrispvs)
644 Mem_Free(r_shadow_buffer_lighttrispvs);
647 void r_shadow_newmap(void)
649 if (r_shadow_bouncegridtexture) R_FreeTexture(r_shadow_bouncegridtexture);r_shadow_bouncegridtexture = NULL;
650 if (r_shadow_lightcorona) R_SkinFrame_MarkUsed(r_shadow_lightcorona);
651 if (r_editlights_sprcursor) R_SkinFrame_MarkUsed(r_editlights_sprcursor);
652 if (r_editlights_sprlight) R_SkinFrame_MarkUsed(r_editlights_sprlight);
653 if (r_editlights_sprnoshadowlight) R_SkinFrame_MarkUsed(r_editlights_sprnoshadowlight);
654 if (r_editlights_sprcubemaplight) R_SkinFrame_MarkUsed(r_editlights_sprcubemaplight);
655 if (r_editlights_sprcubemapnoshadowlight) R_SkinFrame_MarkUsed(r_editlights_sprcubemapnoshadowlight);
656 if (r_editlights_sprselection) R_SkinFrame_MarkUsed(r_editlights_sprselection);
657 if (strncmp(cl.worldname, r_shadow_mapname, sizeof(r_shadow_mapname)))
658 R_Shadow_EditLights_Reload_f();
661 void R_Shadow_Init(void)
663 Cvar_RegisterVariable(&r_shadow_bumpscale_basetexture);
664 Cvar_RegisterVariable(&r_shadow_bumpscale_bumpmap);
665 Cvar_RegisterVariable(&r_shadow_usebihculling);
666 Cvar_RegisterVariable(&r_shadow_usenormalmap);
667 Cvar_RegisterVariable(&r_shadow_debuglight);
668 Cvar_RegisterVariable(&r_shadow_deferred);
669 Cvar_RegisterVariable(&r_shadow_deferred_8bitrange);
670 // Cvar_RegisterVariable(&r_shadow_deferred_fp);
671 Cvar_RegisterVariable(&r_shadow_gloss);
672 Cvar_RegisterVariable(&r_shadow_gloss2intensity);
673 Cvar_RegisterVariable(&r_shadow_glossintensity);
674 Cvar_RegisterVariable(&r_shadow_glossexponent);
675 Cvar_RegisterVariable(&r_shadow_gloss2exponent);
676 Cvar_RegisterVariable(&r_shadow_glossexact);
677 Cvar_RegisterVariable(&r_shadow_lightattenuationdividebias);
678 Cvar_RegisterVariable(&r_shadow_lightattenuationlinearscale);
679 Cvar_RegisterVariable(&r_shadow_lightintensityscale);
680 Cvar_RegisterVariable(&r_shadow_lightradiusscale);
681 Cvar_RegisterVariable(&r_shadow_projectdistance);
682 Cvar_RegisterVariable(&r_shadow_frontsidecasting);
683 Cvar_RegisterVariable(&r_shadow_realtime_dlight);
684 Cvar_RegisterVariable(&r_shadow_realtime_dlight_shadows);
685 Cvar_RegisterVariable(&r_shadow_realtime_dlight_svbspculling);
686 Cvar_RegisterVariable(&r_shadow_realtime_dlight_portalculling);
687 Cvar_RegisterVariable(&r_shadow_realtime_world);
688 Cvar_RegisterVariable(&r_shadow_realtime_world_lightmaps);
689 Cvar_RegisterVariable(&r_shadow_realtime_world_shadows);
690 Cvar_RegisterVariable(&r_shadow_realtime_world_compile);
691 Cvar_RegisterVariable(&r_shadow_realtime_world_compileshadow);
692 Cvar_RegisterVariable(&r_shadow_realtime_world_compilesvbsp);
693 Cvar_RegisterVariable(&r_shadow_realtime_world_compileportalculling);
694 Cvar_RegisterVariable(&r_shadow_scissor);
695 Cvar_RegisterVariable(&r_shadow_shadowmapping);
696 Cvar_RegisterVariable(&r_shadow_shadowmapping_vsdct);
697 Cvar_RegisterVariable(&r_shadow_shadowmapping_filterquality);
698 Cvar_RegisterVariable(&r_shadow_shadowmapping_depthbits);
699 Cvar_RegisterVariable(&r_shadow_shadowmapping_precision);
700 Cvar_RegisterVariable(&r_shadow_shadowmapping_maxsize);
701 Cvar_RegisterVariable(&r_shadow_shadowmapping_minsize);
702 // Cvar_RegisterVariable(&r_shadow_shadowmapping_lod_bias);
703 // Cvar_RegisterVariable(&r_shadow_shadowmapping_lod_scale);
704 Cvar_RegisterVariable(&r_shadow_shadowmapping_bordersize);
705 Cvar_RegisterVariable(&r_shadow_shadowmapping_nearclip);
706 Cvar_RegisterVariable(&r_shadow_shadowmapping_bias);
707 Cvar_RegisterVariable(&r_shadow_shadowmapping_polygonfactor);
708 Cvar_RegisterVariable(&r_shadow_shadowmapping_polygonoffset);
709 Cvar_RegisterVariable(&r_shadow_sortsurfaces);
710 Cvar_RegisterVariable(&r_shadow_polygonfactor);
711 Cvar_RegisterVariable(&r_shadow_polygonoffset);
712 Cvar_RegisterVariable(&r_shadow_texture3d);
713 Cvar_RegisterVariable(&r_shadow_particletrace);
714 Cvar_RegisterVariable(&r_shadow_particletrace_intensity);
715 Cvar_RegisterVariable(&r_shadow_particletrace_size);
716 Cvar_RegisterVariable(&r_shadow_particletrace_radiusscale);
717 Cvar_RegisterVariable(&r_shadow_particletrace_maxbounce);
718 Cvar_RegisterVariable(&r_shadow_particletrace_bounceintensity);
719 Cvar_RegisterVariable(&r_shadow_particletrace_particlespacing);
720 Cvar_RegisterVariable(&r_shadow_particletrace_updatepercentage);
721 Cvar_RegisterVariable(&r_shadow_bouncegrid);
722 Cvar_RegisterVariable(&r_shadow_bouncegrid_dlightparticlemultiplier);
723 Cvar_RegisterVariable(&r_shadow_bouncegrid_hitmodels);
724 Cvar_RegisterVariable(&r_shadow_bouncegrid_intensity);
725 Cvar_RegisterVariable(&r_shadow_bouncegrid_lightradiusscale);
726 Cvar_RegisterVariable(&r_shadow_bouncegrid_maxbounce);
727 Cvar_RegisterVariable(&r_shadow_bouncegrid_particlebounceintensity);
728 Cvar_RegisterVariable(&r_shadow_bouncegrid_particleintensity);
729 Cvar_RegisterVariable(&r_shadow_bouncegrid_particlespacing);
730 Cvar_RegisterVariable(&r_shadow_bouncegrid_spacingx);
731 Cvar_RegisterVariable(&r_shadow_bouncegrid_spacingy);
732 Cvar_RegisterVariable(&r_shadow_bouncegrid_spacingz);
733 Cvar_RegisterVariable(&r_shadow_bouncegrid_stablerandom);
734 Cvar_RegisterVariable(&r_shadow_bouncegrid_updateinterval);
735 Cvar_RegisterVariable(&r_shadow_bouncegrid_x);
736 Cvar_RegisterVariable(&r_shadow_bouncegrid_y);
737 Cvar_RegisterVariable(&r_shadow_bouncegrid_z);
738 Cvar_RegisterVariable(&r_coronas);
739 Cvar_RegisterVariable(&r_coronas_occlusionsizescale);
740 Cvar_RegisterVariable(&r_coronas_occlusionquery);
741 Cvar_RegisterVariable(&gl_flashblend);
742 Cvar_RegisterVariable(&gl_ext_separatestencil);
743 Cvar_RegisterVariable(&gl_ext_stenciltwoside);
744 if (gamemode == GAME_TENEBRAE)
746 Cvar_SetValue("r_shadow_gloss", 2);
747 Cvar_SetValue("r_shadow_bumpscale_basetexture", 4);
749 R_Shadow_EditLights_Init();
750 Mem_ExpandableArray_NewArray(&r_shadow_worldlightsarray, r_main_mempool, sizeof(dlight_t), 128);
751 maxshadowtriangles = 0;
752 shadowelements = NULL;
753 maxshadowvertices = 0;
754 shadowvertex3f = NULL;
762 shadowmarklist = NULL;
767 shadowsideslist = NULL;
768 r_shadow_buffer_numleafpvsbytes = 0;
769 r_shadow_buffer_visitingleafpvs = NULL;
770 r_shadow_buffer_leafpvs = NULL;
771 r_shadow_buffer_leaflist = NULL;
772 r_shadow_buffer_numsurfacepvsbytes = 0;
773 r_shadow_buffer_surfacepvs = NULL;
774 r_shadow_buffer_surfacelist = NULL;
775 r_shadow_buffer_surfacesides = NULL;
776 r_shadow_buffer_shadowtrispvs = NULL;
777 r_shadow_buffer_lighttrispvs = NULL;
778 R_RegisterModule("R_Shadow", r_shadow_start, r_shadow_shutdown, r_shadow_newmap, NULL, NULL);
781 matrix4x4_t matrix_attenuationxyz =
784 {0.5, 0.0, 0.0, 0.5},
785 {0.0, 0.5, 0.0, 0.5},
786 {0.0, 0.0, 0.5, 0.5},
791 matrix4x4_t matrix_attenuationz =
794 {0.0, 0.0, 0.5, 0.5},
795 {0.0, 0.0, 0.0, 0.5},
796 {0.0, 0.0, 0.0, 0.5},
801 void R_Shadow_ResizeShadowArrays(int numvertices, int numtriangles, int vertscale, int triscale)
803 numvertices = ((numvertices + 255) & ~255) * vertscale;
804 numtriangles = ((numtriangles + 255) & ~255) * triscale;
805 // make sure shadowelements is big enough for this volume
806 if (maxshadowtriangles < numtriangles)
808 maxshadowtriangles = numtriangles;
810 Mem_Free(shadowelements);
811 shadowelements = (int *)Mem_Alloc(r_main_mempool, maxshadowtriangles * sizeof(int[3]));
813 // make sure shadowvertex3f is big enough for this volume
814 if (maxshadowvertices < numvertices)
816 maxshadowvertices = numvertices;
818 Mem_Free(shadowvertex3f);
819 shadowvertex3f = (float *)Mem_Alloc(r_main_mempool, maxshadowvertices * sizeof(float[3]));
823 static void R_Shadow_EnlargeLeafSurfaceTrisBuffer(int numleafs, int numsurfaces, int numshadowtriangles, int numlighttriangles)
825 int numleafpvsbytes = (((numleafs + 7) >> 3) + 255) & ~255;
826 int numsurfacepvsbytes = (((numsurfaces + 7) >> 3) + 255) & ~255;
827 int numshadowtrispvsbytes = (((numshadowtriangles + 7) >> 3) + 255) & ~255;
828 int numlighttrispvsbytes = (((numlighttriangles + 7) >> 3) + 255) & ~255;
829 if (r_shadow_buffer_numleafpvsbytes < numleafpvsbytes)
831 if (r_shadow_buffer_visitingleafpvs)
832 Mem_Free(r_shadow_buffer_visitingleafpvs);
833 if (r_shadow_buffer_leafpvs)
834 Mem_Free(r_shadow_buffer_leafpvs);
835 if (r_shadow_buffer_leaflist)
836 Mem_Free(r_shadow_buffer_leaflist);
837 r_shadow_buffer_numleafpvsbytes = numleafpvsbytes;
838 r_shadow_buffer_visitingleafpvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numleafpvsbytes);
839 r_shadow_buffer_leafpvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numleafpvsbytes);
840 r_shadow_buffer_leaflist = (int *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numleafpvsbytes * 8 * sizeof(*r_shadow_buffer_leaflist));
842 if (r_shadow_buffer_numsurfacepvsbytes < numsurfacepvsbytes)
844 if (r_shadow_buffer_surfacepvs)
845 Mem_Free(r_shadow_buffer_surfacepvs);
846 if (r_shadow_buffer_surfacelist)
847 Mem_Free(r_shadow_buffer_surfacelist);
848 if (r_shadow_buffer_surfacesides)
849 Mem_Free(r_shadow_buffer_surfacesides);
850 r_shadow_buffer_numsurfacepvsbytes = numsurfacepvsbytes;
851 r_shadow_buffer_surfacepvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numsurfacepvsbytes);
852 r_shadow_buffer_surfacelist = (int *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numsurfacepvsbytes * 8 * sizeof(*r_shadow_buffer_surfacelist));
853 r_shadow_buffer_surfacesides = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numsurfacepvsbytes * 8 * sizeof(*r_shadow_buffer_surfacelist));
855 if (r_shadow_buffer_numshadowtrispvsbytes < numshadowtrispvsbytes)
857 if (r_shadow_buffer_shadowtrispvs)
858 Mem_Free(r_shadow_buffer_shadowtrispvs);
859 r_shadow_buffer_numshadowtrispvsbytes = numshadowtrispvsbytes;
860 r_shadow_buffer_shadowtrispvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numshadowtrispvsbytes);
862 if (r_shadow_buffer_numlighttrispvsbytes < numlighttrispvsbytes)
864 if (r_shadow_buffer_lighttrispvs)
865 Mem_Free(r_shadow_buffer_lighttrispvs);
866 r_shadow_buffer_numlighttrispvsbytes = numlighttrispvsbytes;
867 r_shadow_buffer_lighttrispvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numlighttrispvsbytes);
871 void R_Shadow_PrepareShadowMark(int numtris)
873 // make sure shadowmark is big enough for this volume
874 if (maxshadowmark < numtris)
876 maxshadowmark = numtris;
878 Mem_Free(shadowmark);
880 Mem_Free(shadowmarklist);
881 shadowmark = (int *)Mem_Alloc(r_main_mempool, maxshadowmark * sizeof(*shadowmark));
882 shadowmarklist = (int *)Mem_Alloc(r_main_mempool, maxshadowmark * sizeof(*shadowmarklist));
886 // if shadowmarkcount wrapped we clear the array and adjust accordingly
887 if (shadowmarkcount == 0)
890 memset(shadowmark, 0, maxshadowmark * sizeof(*shadowmark));
895 void R_Shadow_PrepareShadowSides(int numtris)
897 if (maxshadowsides < numtris)
899 maxshadowsides = numtris;
901 Mem_Free(shadowsides);
903 Mem_Free(shadowsideslist);
904 shadowsides = (unsigned char *)Mem_Alloc(r_main_mempool, maxshadowsides * sizeof(*shadowsides));
905 shadowsideslist = (int *)Mem_Alloc(r_main_mempool, maxshadowsides * sizeof(*shadowsideslist));
910 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)
913 int outtriangles = 0, outvertices = 0;
916 float ratio, direction[3], projectvector[3];
918 if (projectdirection)
919 VectorScale(projectdirection, projectdistance, projectvector);
921 VectorClear(projectvector);
923 // create the vertices
924 if (projectdirection)
926 for (i = 0;i < numshadowmarktris;i++)
928 element = inelement3i + shadowmarktris[i] * 3;
929 for (j = 0;j < 3;j++)
931 if (vertexupdate[element[j]] != vertexupdatenum)
933 vertexupdate[element[j]] = vertexupdatenum;
934 vertexremap[element[j]] = outvertices;
935 vertex = invertex3f + element[j] * 3;
936 // project one copy of the vertex according to projectvector
937 VectorCopy(vertex, outvertex3f);
938 VectorAdd(vertex, projectvector, (outvertex3f + 3));
947 for (i = 0;i < numshadowmarktris;i++)
949 element = inelement3i + shadowmarktris[i] * 3;
950 for (j = 0;j < 3;j++)
952 if (vertexupdate[element[j]] != vertexupdatenum)
954 vertexupdate[element[j]] = vertexupdatenum;
955 vertexremap[element[j]] = outvertices;
956 vertex = invertex3f + element[j] * 3;
957 // project one copy of the vertex to the sphere radius of the light
958 // (FIXME: would projecting it to the light box be better?)
959 VectorSubtract(vertex, projectorigin, direction);
960 ratio = projectdistance / VectorLength(direction);
961 VectorCopy(vertex, outvertex3f);
962 VectorMA(projectorigin, ratio, direction, (outvertex3f + 3));
970 if (r_shadow_frontsidecasting.integer)
972 for (i = 0;i < numshadowmarktris;i++)
974 int remappedelement[3];
976 const int *neighbortriangle;
978 markindex = shadowmarktris[i] * 3;
979 element = inelement3i + markindex;
980 neighbortriangle = inneighbor3i + markindex;
981 // output the front and back triangles
982 outelement3i[0] = vertexremap[element[0]];
983 outelement3i[1] = vertexremap[element[1]];
984 outelement3i[2] = vertexremap[element[2]];
985 outelement3i[3] = vertexremap[element[2]] + 1;
986 outelement3i[4] = vertexremap[element[1]] + 1;
987 outelement3i[5] = vertexremap[element[0]] + 1;
991 // output the sides (facing outward from this triangle)
992 if (shadowmark[neighbortriangle[0]] != shadowmarkcount)
994 remappedelement[0] = vertexremap[element[0]];
995 remappedelement[1] = vertexremap[element[1]];
996 outelement3i[0] = remappedelement[1];
997 outelement3i[1] = remappedelement[0];
998 outelement3i[2] = remappedelement[0] + 1;
999 outelement3i[3] = remappedelement[1];
1000 outelement3i[4] = remappedelement[0] + 1;
1001 outelement3i[5] = remappedelement[1] + 1;
1006 if (shadowmark[neighbortriangle[1]] != shadowmarkcount)
1008 remappedelement[1] = vertexremap[element[1]];
1009 remappedelement[2] = vertexremap[element[2]];
1010 outelement3i[0] = remappedelement[2];
1011 outelement3i[1] = remappedelement[1];
1012 outelement3i[2] = remappedelement[1] + 1;
1013 outelement3i[3] = remappedelement[2];
1014 outelement3i[4] = remappedelement[1] + 1;
1015 outelement3i[5] = remappedelement[2] + 1;
1020 if (shadowmark[neighbortriangle[2]] != shadowmarkcount)
1022 remappedelement[0] = vertexremap[element[0]];
1023 remappedelement[2] = vertexremap[element[2]];
1024 outelement3i[0] = remappedelement[0];
1025 outelement3i[1] = remappedelement[2];
1026 outelement3i[2] = remappedelement[2] + 1;
1027 outelement3i[3] = remappedelement[0];
1028 outelement3i[4] = remappedelement[2] + 1;
1029 outelement3i[5] = remappedelement[0] + 1;
1038 for (i = 0;i < numshadowmarktris;i++)
1040 int remappedelement[3];
1042 const int *neighbortriangle;
1044 markindex = shadowmarktris[i] * 3;
1045 element = inelement3i + markindex;
1046 neighbortriangle = inneighbor3i + markindex;
1047 // output the front and back triangles
1048 outelement3i[0] = vertexremap[element[2]];
1049 outelement3i[1] = vertexremap[element[1]];
1050 outelement3i[2] = vertexremap[element[0]];
1051 outelement3i[3] = vertexremap[element[0]] + 1;
1052 outelement3i[4] = vertexremap[element[1]] + 1;
1053 outelement3i[5] = vertexremap[element[2]] + 1;
1057 // output the sides (facing outward from this triangle)
1058 if (shadowmark[neighbortriangle[0]] != shadowmarkcount)
1060 remappedelement[0] = vertexremap[element[0]];
1061 remappedelement[1] = vertexremap[element[1]];
1062 outelement3i[0] = remappedelement[0];
1063 outelement3i[1] = remappedelement[1];
1064 outelement3i[2] = remappedelement[1] + 1;
1065 outelement3i[3] = remappedelement[0];
1066 outelement3i[4] = remappedelement[1] + 1;
1067 outelement3i[5] = remappedelement[0] + 1;
1072 if (shadowmark[neighbortriangle[1]] != shadowmarkcount)
1074 remappedelement[1] = vertexremap[element[1]];
1075 remappedelement[2] = vertexremap[element[2]];
1076 outelement3i[0] = remappedelement[1];
1077 outelement3i[1] = remappedelement[2];
1078 outelement3i[2] = remappedelement[2] + 1;
1079 outelement3i[3] = remappedelement[1];
1080 outelement3i[4] = remappedelement[2] + 1;
1081 outelement3i[5] = remappedelement[1] + 1;
1086 if (shadowmark[neighbortriangle[2]] != shadowmarkcount)
1088 remappedelement[0] = vertexremap[element[0]];
1089 remappedelement[2] = vertexremap[element[2]];
1090 outelement3i[0] = remappedelement[2];
1091 outelement3i[1] = remappedelement[0];
1092 outelement3i[2] = remappedelement[0] + 1;
1093 outelement3i[3] = remappedelement[2];
1094 outelement3i[4] = remappedelement[0] + 1;
1095 outelement3i[5] = remappedelement[2] + 1;
1103 *outnumvertices = outvertices;
1104 return outtriangles;
1107 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)
1110 int outtriangles = 0, outvertices = 0;
1112 const float *vertex;
1113 float ratio, direction[3], projectvector[3];
1116 if (projectdirection)
1117 VectorScale(projectdirection, projectdistance, projectvector);
1119 VectorClear(projectvector);
1121 for (i = 0;i < numshadowmarktris;i++)
1123 int remappedelement[3];
1125 const int *neighbortriangle;
1127 markindex = shadowmarktris[i] * 3;
1128 neighbortriangle = inneighbor3i + markindex;
1129 side[0] = shadowmark[neighbortriangle[0]] == shadowmarkcount;
1130 side[1] = shadowmark[neighbortriangle[1]] == shadowmarkcount;
1131 side[2] = shadowmark[neighbortriangle[2]] == shadowmarkcount;
1132 if (side[0] + side[1] + side[2] == 0)
1136 element = inelement3i + markindex;
1138 // create the vertices
1139 for (j = 0;j < 3;j++)
1141 if (side[j] + side[j+1] == 0)
1144 if (vertexupdate[k] != vertexupdatenum)
1146 vertexupdate[k] = vertexupdatenum;
1147 vertexremap[k] = outvertices;
1148 vertex = invertex3f + k * 3;
1149 VectorCopy(vertex, outvertex3f);
1150 if (projectdirection)
1152 // project one copy of the vertex according to projectvector
1153 VectorAdd(vertex, projectvector, (outvertex3f + 3));
1157 // project one copy of the vertex to the sphere radius of the light
1158 // (FIXME: would projecting it to the light box be better?)
1159 VectorSubtract(vertex, projectorigin, direction);
1160 ratio = projectdistance / VectorLength(direction);
1161 VectorMA(projectorigin, ratio, direction, (outvertex3f + 3));
1168 // output the sides (facing outward from this triangle)
1171 remappedelement[0] = vertexremap[element[0]];
1172 remappedelement[1] = vertexremap[element[1]];
1173 outelement3i[0] = remappedelement[1];
1174 outelement3i[1] = remappedelement[0];
1175 outelement3i[2] = remappedelement[0] + 1;
1176 outelement3i[3] = remappedelement[1];
1177 outelement3i[4] = remappedelement[0] + 1;
1178 outelement3i[5] = remappedelement[1] + 1;
1185 remappedelement[1] = vertexremap[element[1]];
1186 remappedelement[2] = vertexremap[element[2]];
1187 outelement3i[0] = remappedelement[2];
1188 outelement3i[1] = remappedelement[1];
1189 outelement3i[2] = remappedelement[1] + 1;
1190 outelement3i[3] = remappedelement[2];
1191 outelement3i[4] = remappedelement[1] + 1;
1192 outelement3i[5] = remappedelement[2] + 1;
1199 remappedelement[0] = vertexremap[element[0]];
1200 remappedelement[2] = vertexremap[element[2]];
1201 outelement3i[0] = remappedelement[0];
1202 outelement3i[1] = remappedelement[2];
1203 outelement3i[2] = remappedelement[2] + 1;
1204 outelement3i[3] = remappedelement[0];
1205 outelement3i[4] = remappedelement[2] + 1;
1206 outelement3i[5] = remappedelement[0] + 1;
1213 *outnumvertices = outvertices;
1214 return outtriangles;
1217 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)
1223 if (!BoxesOverlap(lightmins, lightmaxs, surfacemins, surfacemaxs))
1225 tend = firsttriangle + numtris;
1226 if (BoxInsideBox(surfacemins, surfacemaxs, lightmins, lightmaxs))
1228 // surface box entirely inside light box, no box cull
1229 if (projectdirection)
1231 for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
1233 TriangleNormal(invertex3f + e[0] * 3, invertex3f + e[1] * 3, invertex3f + e[2] * 3, normal);
1234 if (r_shadow_frontsidecasting.integer == (DotProduct(normal, projectdirection) < 0))
1235 shadowmarklist[numshadowmark++] = t;
1240 for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
1241 if (r_shadow_frontsidecasting.integer == PointInfrontOfTriangle(projectorigin, invertex3f + e[0] * 3, invertex3f + e[1] * 3, invertex3f + e[2] * 3))
1242 shadowmarklist[numshadowmark++] = t;
1247 // surface box not entirely inside light box, cull each triangle
1248 if (projectdirection)
1250 for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
1252 v[0] = invertex3f + e[0] * 3;
1253 v[1] = invertex3f + e[1] * 3;
1254 v[2] = invertex3f + e[2] * 3;
1255 TriangleNormal(v[0], v[1], v[2], normal);
1256 if (r_shadow_frontsidecasting.integer == (DotProduct(normal, projectdirection) < 0)
1257 && TriangleOverlapsBox(v[0], v[1], v[2], lightmins, lightmaxs))
1258 shadowmarklist[numshadowmark++] = t;
1263 for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
1265 v[0] = invertex3f + e[0] * 3;
1266 v[1] = invertex3f + e[1] * 3;
1267 v[2] = invertex3f + e[2] * 3;
1268 if (r_shadow_frontsidecasting.integer == PointInfrontOfTriangle(projectorigin, v[0], v[1], v[2])
1269 && TriangleOverlapsBox(v[0], v[1], v[2], lightmins, lightmaxs))
1270 shadowmarklist[numshadowmark++] = t;
1276 qboolean R_Shadow_UseZPass(vec3_t mins, vec3_t maxs)
1281 if (r_shadow_compilingrtlight || !r_shadow_frontsidecasting.integer || !r_shadow_usezpassifpossible.integer)
1283 // check if the shadow volume intersects the near plane
1285 // a ray between the eye and light origin may intersect the caster,
1286 // indicating that the shadow may touch the eye location, however we must
1287 // test the near plane (a polygon), not merely the eye location, so it is
1288 // easiest to enlarge the caster bounding shape slightly for this.
1294 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)
1296 int i, tris, outverts;
1297 if (projectdistance < 0.1)
1299 Con_Printf("R_Shadow_Volume: projectdistance %f\n", projectdistance);
1302 if (!numverts || !nummarktris)
1304 // make sure shadowelements is big enough for this volume
1305 if (maxshadowtriangles < nummarktris*8 || maxshadowvertices < numverts*2)
1306 R_Shadow_ResizeShadowArrays(numverts, nummarktris, 2, 8);
1308 if (maxvertexupdate < numverts)
1310 maxvertexupdate = numverts;
1312 Mem_Free(vertexupdate);
1314 Mem_Free(vertexremap);
1315 vertexupdate = (int *)Mem_Alloc(r_main_mempool, maxvertexupdate * sizeof(int));
1316 vertexremap = (int *)Mem_Alloc(r_main_mempool, maxvertexupdate * sizeof(int));
1317 vertexupdatenum = 0;
1320 if (vertexupdatenum == 0)
1322 vertexupdatenum = 1;
1323 memset(vertexupdate, 0, maxvertexupdate * sizeof(int));
1324 memset(vertexremap, 0, maxvertexupdate * sizeof(int));
1327 for (i = 0;i < nummarktris;i++)
1328 shadowmark[marktris[i]] = shadowmarkcount;
1330 if (r_shadow_compilingrtlight)
1332 // if we're compiling an rtlight, capture the mesh
1333 //tris = R_Shadow_ConstructShadowVolume_ZPass(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
1334 //Mod_ShadowMesh_AddMesh(r_main_mempool, r_shadow_compilingrtlight->static_meshchain_shadow_zpass, NULL, NULL, NULL, shadowvertex3f, NULL, NULL, NULL, NULL, tris, shadowelements);
1335 tris = R_Shadow_ConstructShadowVolume_ZFail(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
1336 Mod_ShadowMesh_AddMesh(r_main_mempool, r_shadow_compilingrtlight->static_meshchain_shadow_zfail, NULL, NULL, NULL, shadowvertex3f, NULL, NULL, NULL, NULL, tris, shadowelements);
1338 else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_VISIBLEVOLUMES)
1340 tris = R_Shadow_ConstructShadowVolume_ZFail(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
1341 R_Mesh_PrepareVertices_Vertex3f(outverts, shadowvertex3f, NULL);
1342 R_Mesh_Draw(0, outverts, 0, tris, shadowelements, NULL, 0, NULL, NULL, 0);
1346 // decide which type of shadow to generate and set stencil mode
1347 R_Shadow_RenderMode_StencilShadowVolumes(R_Shadow_UseZPass(trismins, trismaxs));
1348 // generate the sides or a solid volume, depending on type
1349 if (r_shadow_rendermode >= R_SHADOW_RENDERMODE_ZPASS_STENCIL && r_shadow_rendermode <= R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE)
1350 tris = R_Shadow_ConstructShadowVolume_ZPass(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
1352 tris = R_Shadow_ConstructShadowVolume_ZFail(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
1353 r_refdef.stats.lights_dynamicshadowtriangles += tris;
1354 r_refdef.stats.lights_shadowtriangles += tris;
1355 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZPASS_STENCIL)
1357 // increment stencil if frontface is infront of depthbuffer
1358 GL_CullFace(r_refdef.view.cullface_front);
1359 R_SetStencil(true, 255, GL_KEEP, GL_KEEP, GL_DECR, GL_ALWAYS, 128, 255);
1360 R_Mesh_Draw(0, outverts, 0, tris, shadowelements, NULL, 0, NULL, NULL, 0);
1361 // decrement stencil if backface is infront of depthbuffer
1362 GL_CullFace(r_refdef.view.cullface_back);
1363 R_SetStencil(true, 255, GL_KEEP, GL_KEEP, GL_INCR, GL_ALWAYS, 128, 255);
1365 else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZFAIL_STENCIL)
1367 // decrement stencil if backface is behind depthbuffer
1368 GL_CullFace(r_refdef.view.cullface_front);
1369 R_SetStencil(true, 255, GL_KEEP, GL_DECR, GL_KEEP, GL_ALWAYS, 128, 255);
1370 R_Mesh_Draw(0, outverts, 0, tris, shadowelements, NULL, 0, NULL, NULL, 0);
1371 // increment stencil if frontface is behind depthbuffer
1372 GL_CullFace(r_refdef.view.cullface_back);
1373 R_SetStencil(true, 255, GL_KEEP, GL_INCR, GL_KEEP, GL_ALWAYS, 128, 255);
1375 R_Mesh_PrepareVertices_Vertex3f(outverts, shadowvertex3f, NULL);
1376 R_Mesh_Draw(0, outverts, 0, tris, shadowelements, NULL, 0, NULL, NULL, 0);
1380 int R_Shadow_CalcTriangleSideMask(const vec3_t p1, const vec3_t p2, const vec3_t p3, float bias)
1382 // p1, p2, p3 are in the cubemap's local coordinate system
1383 // bias = border/(size - border)
1386 float dp1 = p1[0] + p1[1], dn1 = p1[0] - p1[1], ap1 = fabs(dp1), an1 = fabs(dn1),
1387 dp2 = p2[0] + p2[1], dn2 = p2[0] - p2[1], ap2 = fabs(dp2), an2 = fabs(dn2),
1388 dp3 = p3[0] + p3[1], dn3 = p3[0] - p3[1], ap3 = fabs(dp3), an3 = fabs(dn3);
1389 if(ap1 > bias*an1 && ap2 > bias*an2 && ap3 > bias*an3)
1391 | (dp1 >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2))
1392 | (dp2 >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2))
1393 | (dp3 >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2));
1394 if(an1 > bias*ap1 && an2 > bias*ap2 && an3 > bias*ap3)
1396 | (dn1 >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2))
1397 | (dn2 >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2))
1398 | (dn3 >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2));
1400 dp1 = p1[1] + p1[2], dn1 = p1[1] - p1[2], ap1 = fabs(dp1), an1 = fabs(dn1),
1401 dp2 = p2[1] + p2[2], dn2 = p2[1] - p2[2], ap2 = fabs(dp2), an2 = fabs(dn2),
1402 dp3 = p3[1] + p3[2], dn3 = p3[1] - p3[2], ap3 = fabs(dp3), an3 = fabs(dn3);
1403 if(ap1 > bias*an1 && ap2 > bias*an2 && ap3 > bias*an3)
1405 | (dp1 >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4))
1406 | (dp2 >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4))
1407 | (dp3 >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4));
1408 if(an1 > bias*ap1 && an2 > bias*ap2 && an3 > bias*ap3)
1410 | (dn1 >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4))
1411 | (dn2 >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4))
1412 | (dn3 >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4));
1414 dp1 = p1[2] + p1[0], dn1 = p1[2] - p1[0], ap1 = fabs(dp1), an1 = fabs(dn1),
1415 dp2 = p2[2] + p2[0], dn2 = p2[2] - p2[0], ap2 = fabs(dp2), an2 = fabs(dn2),
1416 dp3 = p3[2] + p3[0], dn3 = p3[2] - p3[0], ap3 = fabs(dp3), an3 = fabs(dn3);
1417 if(ap1 > bias*an1 && ap2 > bias*an2 && ap3 > bias*an3)
1419 | (dp1 >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0))
1420 | (dp2 >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0))
1421 | (dp3 >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0));
1422 if(an1 > bias*ap1 && an2 > bias*ap2 && an3 > bias*ap3)
1424 | (dn1 >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0))
1425 | (dn2 >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0))
1426 | (dn3 >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0));
1431 int R_Shadow_CalcBBoxSideMask(const vec3_t mins, const vec3_t maxs, const matrix4x4_t *worldtolight, const matrix4x4_t *radiustolight, float bias)
1433 vec3_t center, radius, lightcenter, lightradius, pmin, pmax;
1434 float dp1, dn1, ap1, an1, dp2, dn2, ap2, an2;
1437 VectorSubtract(maxs, mins, radius);
1438 VectorScale(radius, 0.5f, radius);
1439 VectorAdd(mins, radius, center);
1440 Matrix4x4_Transform(worldtolight, center, lightcenter);
1441 Matrix4x4_Transform3x3(radiustolight, radius, lightradius);
1442 VectorSubtract(lightcenter, lightradius, pmin);
1443 VectorAdd(lightcenter, lightradius, pmax);
1445 dp1 = pmax[0] + pmax[1], dn1 = pmax[0] - pmin[1], ap1 = fabs(dp1), an1 = fabs(dn1),
1446 dp2 = pmin[0] + pmin[1], dn2 = pmin[0] - pmax[1], ap2 = fabs(dp2), an2 = fabs(dn2);
1447 if(ap1 > bias*an1 && ap2 > bias*an2)
1449 | (dp1 >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2))
1450 | (dp2 >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2));
1451 if(an1 > bias*ap1 && an2 > bias*ap2)
1453 | (dn1 >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2))
1454 | (dn2 >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2));
1456 dp1 = pmax[1] + pmax[2], dn1 = pmax[1] - pmin[2], ap1 = fabs(dp1), an1 = fabs(dn1),
1457 dp2 = pmin[1] + pmin[2], dn2 = pmin[1] - pmax[2], ap2 = fabs(dp2), an2 = fabs(dn2);
1458 if(ap1 > bias*an1 && ap2 > bias*an2)
1460 | (dp1 >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4))
1461 | (dp2 >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4));
1462 if(an1 > bias*ap1 && an2 > bias*ap2)
1464 | (dn1 >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4))
1465 | (dn2 >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4));
1467 dp1 = pmax[2] + pmax[0], dn1 = pmax[2] - pmin[0], ap1 = fabs(dp1), an1 = fabs(dn1),
1468 dp2 = pmin[2] + pmin[0], dn2 = pmin[2] - pmax[0], ap2 = fabs(dp2), an2 = fabs(dn2);
1469 if(ap1 > bias*an1 && ap2 > bias*an2)
1471 | (dp1 >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0))
1472 | (dp2 >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0));
1473 if(an1 > bias*ap1 && an2 > bias*ap2)
1475 | (dn1 >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0))
1476 | (dn2 >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0));
1481 #define R_Shadow_CalcEntitySideMask(ent, worldtolight, radiustolight, bias) R_Shadow_CalcBBoxSideMask((ent)->mins, (ent)->maxs, worldtolight, radiustolight, bias)
1483 int R_Shadow_CalcSphereSideMask(const vec3_t p, float radius, float bias)
1485 // p is in the cubemap's local coordinate system
1486 // bias = border/(size - border)
1487 float dxyp = p[0] + p[1], dxyn = p[0] - p[1], axyp = fabs(dxyp), axyn = fabs(dxyn);
1488 float dyzp = p[1] + p[2], dyzn = p[1] - p[2], ayzp = fabs(dyzp), ayzn = fabs(dyzn);
1489 float dzxp = p[2] + p[0], dzxn = p[2] - p[0], azxp = fabs(dzxp), azxn = fabs(dzxn);
1491 if(axyp > bias*axyn + radius) mask &= dxyp < 0 ? ~((1<<0)|(1<<2)) : ~((2<<0)|(2<<2));
1492 if(axyn > bias*axyp + radius) mask &= dxyn < 0 ? ~((1<<0)|(2<<2)) : ~((2<<0)|(1<<2));
1493 if(ayzp > bias*ayzn + radius) mask &= dyzp < 0 ? ~((1<<2)|(1<<4)) : ~((2<<2)|(2<<4));
1494 if(ayzn > bias*ayzp + radius) mask &= dyzn < 0 ? ~((1<<2)|(2<<4)) : ~((2<<2)|(1<<4));
1495 if(azxp > bias*azxn + radius) mask &= dzxp < 0 ? ~((1<<4)|(1<<0)) : ~((2<<4)|(2<<0));
1496 if(azxn > bias*azxp + radius) mask &= dzxn < 0 ? ~((1<<4)|(2<<0)) : ~((2<<4)|(1<<0));
1500 int R_Shadow_CullFrustumSides(rtlight_t *rtlight, float size, float border)
1504 int sides = 0x3F, masks[6] = { 3<<4, 3<<4, 3<<0, 3<<0, 3<<2, 3<<2 };
1505 float scale = (size - 2*border)/size, len;
1506 float bias = border / (float)(size - border), dp, dn, ap, an;
1507 // check if cone enclosing side would cross frustum plane
1508 scale = 2 / (scale*scale + 2);
1509 for (i = 0;i < 5;i++)
1511 if (PlaneDiff(rtlight->shadoworigin, &r_refdef.view.frustum[i]) > -0.03125)
1513 Matrix4x4_Transform3x3(&rtlight->matrix_worldtolight, r_refdef.view.frustum[i].normal, n);
1514 len = scale*VectorLength2(n);
1515 if(n[0]*n[0] > len) sides &= n[0] < 0 ? ~(1<<0) : ~(2 << 0);
1516 if(n[1]*n[1] > len) sides &= n[1] < 0 ? ~(1<<2) : ~(2 << 2);
1517 if(n[2]*n[2] > len) sides &= n[2] < 0 ? ~(1<<4) : ~(2 << 4);
1519 if (PlaneDiff(rtlight->shadoworigin, &r_refdef.view.frustum[4]) >= r_refdef.farclip - r_refdef.nearclip + 0.03125)
1521 Matrix4x4_Transform3x3(&rtlight->matrix_worldtolight, r_refdef.view.frustum[4].normal, n);
1522 len = scale*VectorLength(n);
1523 if(n[0]*n[0] > len) sides &= n[0] >= 0 ? ~(1<<0) : ~(2 << 0);
1524 if(n[1]*n[1] > len) sides &= n[1] >= 0 ? ~(1<<2) : ~(2 << 2);
1525 if(n[2]*n[2] > len) sides &= n[2] >= 0 ? ~(1<<4) : ~(2 << 4);
1527 // this next test usually clips off more sides than the former, but occasionally clips fewer/different ones, so do both and combine results
1528 // check if frustum corners/origin cross plane sides
1530 // infinite version, assumes frustum corners merely give direction and extend to infinite distance
1531 Matrix4x4_Transform(&rtlight->matrix_worldtolight, r_refdef.view.origin, p);
1532 dp = p[0] + p[1], dn = p[0] - p[1], ap = fabs(dp), an = fabs(dn);
1533 masks[0] |= ap <= bias*an ? 0x3F : (dp >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2));
1534 masks[1] |= an <= bias*ap ? 0x3F : (dn >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2));
1535 dp = p[1] + p[2], dn = p[1] - p[2], ap = fabs(dp), an = fabs(dn);
1536 masks[2] |= ap <= bias*an ? 0x3F : (dp >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4));
1537 masks[3] |= an <= bias*ap ? 0x3F : (dn >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4));
1538 dp = p[2] + p[0], dn = p[2] - p[0], ap = fabs(dp), an = fabs(dn);
1539 masks[4] |= ap <= bias*an ? 0x3F : (dp >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0));
1540 masks[5] |= an <= bias*ap ? 0x3F : (dn >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0));
1541 for (i = 0;i < 4;i++)
1543 Matrix4x4_Transform(&rtlight->matrix_worldtolight, r_refdef.view.frustumcorner[i], n);
1544 VectorSubtract(n, p, n);
1545 dp = n[0] + n[1], dn = n[0] - n[1], ap = fabs(dp), an = fabs(dn);
1546 if(ap > 0) masks[0] |= dp >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2);
1547 if(an > 0) masks[1] |= dn >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2);
1548 dp = n[1] + n[2], dn = n[1] - n[2], ap = fabs(dp), an = fabs(dn);
1549 if(ap > 0) masks[2] |= dp >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4);
1550 if(an > 0) masks[3] |= dn >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4);
1551 dp = n[2] + n[0], dn = n[2] - n[0], ap = fabs(dp), an = fabs(dn);
1552 if(ap > 0) masks[4] |= dp >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0);
1553 if(an > 0) masks[5] |= dn >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0);
1556 // finite version, assumes corners are a finite distance from origin dependent on far plane
1557 for (i = 0;i < 5;i++)
1559 Matrix4x4_Transform(&rtlight->matrix_worldtolight, !i ? r_refdef.view.origin : r_refdef.view.frustumcorner[i-1], p);
1560 dp = p[0] + p[1], dn = p[0] - p[1], ap = fabs(dp), an = fabs(dn);
1561 masks[0] |= ap <= bias*an ? 0x3F : (dp >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2));
1562 masks[1] |= an <= bias*ap ? 0x3F : (dn >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2));
1563 dp = p[1] + p[2], dn = p[1] - p[2], ap = fabs(dp), an = fabs(dn);
1564 masks[2] |= ap <= bias*an ? 0x3F : (dp >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4));
1565 masks[3] |= an <= bias*ap ? 0x3F : (dn >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4));
1566 dp = p[2] + p[0], dn = p[2] - p[0], ap = fabs(dp), an = fabs(dn);
1567 masks[4] |= ap <= bias*an ? 0x3F : (dp >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0));
1568 masks[5] |= an <= bias*ap ? 0x3F : (dn >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0));
1571 return sides & masks[0] & masks[1] & masks[2] & masks[3] & masks[4] & masks[5];
1574 int R_Shadow_ChooseSidesFromBox(int firsttriangle, int numtris, const float *invertex3f, const int *elements, const matrix4x4_t *worldtolight, const vec3_t projectorigin, const vec3_t projectdirection, const vec3_t lightmins, const vec3_t lightmaxs, const vec3_t surfacemins, const vec3_t surfacemaxs, int *totals)
1582 int mask, surfacemask = 0;
1583 if (!BoxesOverlap(lightmins, lightmaxs, surfacemins, surfacemaxs))
1585 bias = r_shadow_shadowmapborder / (float)(r_shadow_shadowmapmaxsize - r_shadow_shadowmapborder);
1586 tend = firsttriangle + numtris;
1587 if (BoxInsideBox(surfacemins, surfacemaxs, lightmins, lightmaxs))
1589 // surface box entirely inside light box, no box cull
1590 if (projectdirection)
1592 for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
1594 v[0] = invertex3f + e[0] * 3, v[1] = invertex3f + e[1] * 3, v[2] = invertex3f + e[2] * 3;
1595 TriangleNormal(v[0], v[1], v[2], normal);
1596 if (r_shadow_frontsidecasting.integer == (DotProduct(normal, projectdirection) < 0))
1598 Matrix4x4_Transform(worldtolight, v[0], p[0]), Matrix4x4_Transform(worldtolight, v[1], p[1]), Matrix4x4_Transform(worldtolight, v[2], p[2]);
1599 mask = R_Shadow_CalcTriangleSideMask(p[0], p[1], p[2], bias);
1600 surfacemask |= mask;
1603 totals[0] += mask&1, totals[1] += (mask>>1)&1, totals[2] += (mask>>2)&1, totals[3] += (mask>>3)&1, totals[4] += (mask>>4)&1, totals[5] += mask>>5;
1604 shadowsides[numshadowsides] = mask;
1605 shadowsideslist[numshadowsides++] = t;
1612 for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
1614 v[0] = invertex3f + e[0] * 3, v[1] = invertex3f + e[1] * 3, v[2] = invertex3f + e[2] * 3;
1615 if (r_shadow_frontsidecasting.integer == PointInfrontOfTriangle(projectorigin, v[0], v[1], v[2]))
1617 Matrix4x4_Transform(worldtolight, v[0], p[0]), Matrix4x4_Transform(worldtolight, v[1], p[1]), Matrix4x4_Transform(worldtolight, v[2], p[2]);
1618 mask = R_Shadow_CalcTriangleSideMask(p[0], p[1], p[2], bias);
1619 surfacemask |= mask;
1622 totals[0] += mask&1, totals[1] += (mask>>1)&1, totals[2] += (mask>>2)&1, totals[3] += (mask>>3)&1, totals[4] += (mask>>4)&1, totals[5] += mask>>5;
1623 shadowsides[numshadowsides] = mask;
1624 shadowsideslist[numshadowsides++] = t;
1632 // surface box not entirely inside light box, cull each triangle
1633 if (projectdirection)
1635 for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
1637 v[0] = invertex3f + e[0] * 3, v[1] = invertex3f + e[1] * 3, v[2] = invertex3f + e[2] * 3;
1638 TriangleNormal(v[0], v[1], v[2], normal);
1639 if (r_shadow_frontsidecasting.integer == (DotProduct(normal, projectdirection) < 0)
1640 && TriangleOverlapsBox(v[0], v[1], v[2], lightmins, lightmaxs))
1642 Matrix4x4_Transform(worldtolight, v[0], p[0]), Matrix4x4_Transform(worldtolight, v[1], p[1]), Matrix4x4_Transform(worldtolight, v[2], p[2]);
1643 mask = R_Shadow_CalcTriangleSideMask(p[0], p[1], p[2], bias);
1644 surfacemask |= mask;
1647 totals[0] += mask&1, totals[1] += (mask>>1)&1, totals[2] += (mask>>2)&1, totals[3] += (mask>>3)&1, totals[4] += (mask>>4)&1, totals[5] += mask>>5;
1648 shadowsides[numshadowsides] = mask;
1649 shadowsideslist[numshadowsides++] = t;
1656 for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
1658 v[0] = invertex3f + e[0] * 3, v[1] = invertex3f + e[1] * 3, v[2] = invertex3f + e[2] * 3;
1659 if (r_shadow_frontsidecasting.integer == PointInfrontOfTriangle(projectorigin, v[0], v[1], v[2])
1660 && TriangleOverlapsBox(v[0], v[1], v[2], lightmins, lightmaxs))
1662 Matrix4x4_Transform(worldtolight, v[0], p[0]), Matrix4x4_Transform(worldtolight, v[1], p[1]), Matrix4x4_Transform(worldtolight, v[2], p[2]);
1663 mask = R_Shadow_CalcTriangleSideMask(p[0], p[1], p[2], bias);
1664 surfacemask |= mask;
1667 totals[0] += mask&1, totals[1] += (mask>>1)&1, totals[2] += (mask>>2)&1, totals[3] += (mask>>3)&1, totals[4] += (mask>>4)&1, totals[5] += mask>>5;
1668 shadowsides[numshadowsides] = mask;
1669 shadowsideslist[numshadowsides++] = t;
1678 void R_Shadow_ShadowMapFromList(int numverts, int numtris, const float *vertex3f, const int *elements, int numsidetris, const int *sidetotals, const unsigned char *sides, const int *sidetris)
1680 int i, j, outtriangles = 0;
1681 int *outelement3i[6];
1682 if (!numverts || !numsidetris || !r_shadow_compilingrtlight)
1684 outtriangles = sidetotals[0] + sidetotals[1] + sidetotals[2] + sidetotals[3] + sidetotals[4] + sidetotals[5];
1685 // make sure shadowelements is big enough for this mesh
1686 if (maxshadowtriangles < outtriangles)
1687 R_Shadow_ResizeShadowArrays(0, outtriangles, 0, 1);
1689 // compute the offset and size of the separate index lists for each cubemap side
1691 for (i = 0;i < 6;i++)
1693 outelement3i[i] = shadowelements + outtriangles * 3;
1694 r_shadow_compilingrtlight->static_meshchain_shadow_shadowmap->sideoffsets[i] = outtriangles;
1695 r_shadow_compilingrtlight->static_meshchain_shadow_shadowmap->sidetotals[i] = sidetotals[i];
1696 outtriangles += sidetotals[i];
1699 // gather up the (sparse) triangles into separate index lists for each cubemap side
1700 for (i = 0;i < numsidetris;i++)
1702 const int *element = elements + sidetris[i] * 3;
1703 for (j = 0;j < 6;j++)
1705 if (sides[i] & (1 << j))
1707 outelement3i[j][0] = element[0];
1708 outelement3i[j][1] = element[1];
1709 outelement3i[j][2] = element[2];
1710 outelement3i[j] += 3;
1715 Mod_ShadowMesh_AddMesh(r_main_mempool, r_shadow_compilingrtlight->static_meshchain_shadow_shadowmap, NULL, NULL, NULL, vertex3f, NULL, NULL, NULL, NULL, outtriangles, shadowelements);
1718 static void R_Shadow_MakeTextures_MakeCorona(void)
1722 unsigned char pixels[32][32][4];
1723 for (y = 0;y < 32;y++)
1725 dy = (y - 15.5f) * (1.0f / 16.0f);
1726 for (x = 0;x < 32;x++)
1728 dx = (x - 15.5f) * (1.0f / 16.0f);
1729 a = (int)(((1.0f / (dx * dx + dy * dy + 0.2f)) - (1.0f / (1.0f + 0.2))) * 32.0f / (1.0f / (1.0f + 0.2)));
1730 a = bound(0, a, 255);
1731 pixels[y][x][0] = a;
1732 pixels[y][x][1] = a;
1733 pixels[y][x][2] = a;
1734 pixels[y][x][3] = 255;
1737 r_shadow_lightcorona = R_SkinFrame_LoadInternalBGRA("lightcorona", TEXF_FORCELINEAR, &pixels[0][0][0], 32, 32);
1740 static unsigned int R_Shadow_MakeTextures_SamplePoint(float x, float y, float z)
1742 float dist = sqrt(x*x+y*y+z*z);
1743 float intensity = dist < 1 ? ((1.0f - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist)) : 0;
1744 // note this code could suffer byte order issues except that it is multiplying by an integer that reads the same both ways
1745 return (unsigned char)bound(0, intensity * 256.0f, 255) * 0x01010101;
1748 static void R_Shadow_MakeTextures(void)
1751 float intensity, dist;
1753 R_Shadow_FreeShadowMaps();
1754 R_FreeTexturePool(&r_shadow_texturepool);
1755 r_shadow_texturepool = R_AllocTexturePool();
1756 r_shadow_attenlinearscale = r_shadow_lightattenuationlinearscale.value;
1757 r_shadow_attendividebias = r_shadow_lightattenuationdividebias.value;
1758 data = (unsigned int *)Mem_Alloc(tempmempool, max(max(ATTEN3DSIZE*ATTEN3DSIZE*ATTEN3DSIZE, ATTEN2DSIZE*ATTEN2DSIZE), ATTEN1DSIZE) * 4);
1759 // the table includes one additional value to avoid the need to clamp indexing due to minor math errors
1760 for (x = 0;x <= ATTENTABLESIZE;x++)
1762 dist = (x + 0.5f) * (1.0f / ATTENTABLESIZE) * (1.0f / 0.9375);
1763 intensity = dist < 1 ? ((1.0f - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist)) : 0;
1764 r_shadow_attentable[x] = bound(0, intensity, 1);
1766 // 1D gradient texture
1767 for (x = 0;x < ATTEN1DSIZE;x++)
1768 data[x] = R_Shadow_MakeTextures_SamplePoint((x + 0.5f) * (1.0f / ATTEN1DSIZE) * (1.0f / 0.9375), 0, 0);
1769 r_shadow_attenuationgradienttexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation1d", ATTEN1DSIZE, 1, (unsigned char *)data, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCELINEAR, -1, NULL);
1770 // 2D circle texture
1771 for (y = 0;y < ATTEN2DSIZE;y++)
1772 for (x = 0;x < ATTEN2DSIZE;x++)
1773 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);
1774 r_shadow_attenuation2dtexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation2d", ATTEN2DSIZE, ATTEN2DSIZE, (unsigned char *)data, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCELINEAR, -1, NULL);
1775 // 3D sphere texture
1776 if (r_shadow_texture3d.integer && vid.support.ext_texture_3d)
1778 for (z = 0;z < ATTEN3DSIZE;z++)
1779 for (y = 0;y < ATTEN3DSIZE;y++)
1780 for (x = 0;x < ATTEN3DSIZE;x++)
1781 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));
1782 r_shadow_attenuation3dtexture = R_LoadTexture3D(r_shadow_texturepool, "attenuation3d", ATTEN3DSIZE, ATTEN3DSIZE, ATTEN3DSIZE, (unsigned char *)data, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCELINEAR, -1, NULL);
1785 r_shadow_attenuation3dtexture = NULL;
1788 R_Shadow_MakeTextures_MakeCorona();
1790 // Editor light sprites
1791 r_editlights_sprcursor = R_SkinFrame_LoadInternal8bit("gfx/editlights/cursor", TEXF_ALPHA | TEXF_CLAMP, (const unsigned char *)
1808 , 16, 16, palette_bgra_embeddedpic, palette_bgra_embeddedpic);
1809 r_editlights_sprlight = R_SkinFrame_LoadInternal8bit("gfx/editlights/light", TEXF_ALPHA | TEXF_CLAMP, (const unsigned char *)
1826 , 16, 16, palette_bgra_embeddedpic, palette_bgra_embeddedpic);
1827 r_editlights_sprnoshadowlight = R_SkinFrame_LoadInternal8bit("gfx/editlights/noshadow", TEXF_ALPHA | TEXF_CLAMP, (const unsigned char *)
1844 , 16, 16, palette_bgra_embeddedpic, palette_bgra_embeddedpic);
1845 r_editlights_sprcubemaplight = R_SkinFrame_LoadInternal8bit("gfx/editlights/cubemaplight", TEXF_ALPHA | TEXF_CLAMP, (const unsigned char *)
1862 , 16, 16, palette_bgra_embeddedpic, palette_bgra_embeddedpic);
1863 r_editlights_sprcubemapnoshadowlight = R_SkinFrame_LoadInternal8bit("gfx/editlights/cubemapnoshadowlight", TEXF_ALPHA | TEXF_CLAMP, (const unsigned char *)
1880 , 16, 16, palette_bgra_embeddedpic, palette_bgra_embeddedpic);
1881 r_editlights_sprselection = R_SkinFrame_LoadInternal8bit("gfx/editlights/selection", TEXF_ALPHA | TEXF_CLAMP, (unsigned char *)
1898 , 16, 16, palette_bgra_embeddedpic, palette_bgra_embeddedpic);
1901 void R_Shadow_ValidateCvars(void)
1903 if (r_shadow_texture3d.integer && !vid.support.ext_texture_3d)
1904 Cvar_SetValueQuick(&r_shadow_texture3d, 0);
1905 if (gl_ext_separatestencil.integer && !vid.support.ati_separate_stencil)
1906 Cvar_SetValueQuick(&gl_ext_separatestencil, 0);
1907 if (gl_ext_stenciltwoside.integer && !vid.support.ext_stencil_two_side)
1908 Cvar_SetValueQuick(&gl_ext_stenciltwoside, 0);
1911 void R_Shadow_RenderMode_Begin(void)
1917 R_Shadow_ValidateCvars();
1919 if (!r_shadow_attenuation2dtexture
1920 || (!r_shadow_attenuation3dtexture && r_shadow_texture3d.integer)
1921 || r_shadow_lightattenuationdividebias.value != r_shadow_attendividebias
1922 || r_shadow_lightattenuationlinearscale.value != r_shadow_attenlinearscale)
1923 R_Shadow_MakeTextures();
1926 R_Mesh_ResetTextureState();
1927 GL_BlendFunc(GL_ONE, GL_ZERO);
1928 GL_DepthRange(0, 1);
1929 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
1931 GL_DepthMask(false);
1932 GL_Color(0, 0, 0, 1);
1933 GL_Scissor(r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
1935 r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
1937 if (gl_ext_separatestencil.integer && vid.support.ati_separate_stencil)
1939 r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_SEPARATESTENCIL;
1940 r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_SEPARATESTENCIL;
1942 else if (gl_ext_stenciltwoside.integer && vid.support.ext_stencil_two_side)
1944 r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE;
1945 r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE;
1949 r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_STENCIL;
1950 r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_STENCIL;
1953 switch(vid.renderpath)
1955 case RENDERPATH_GL20:
1956 case RENDERPATH_D3D9:
1957 case RENDERPATH_D3D10:
1958 case RENDERPATH_D3D11:
1959 case RENDERPATH_SOFT:
1960 case RENDERPATH_GLES2:
1961 r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_GLSL;
1963 case RENDERPATH_GL13:
1964 case RENDERPATH_GL11:
1965 if (r_textureunits.integer >= 2 && vid.texunits >= 2 && r_shadow_texture3d.integer && r_shadow_attenuation3dtexture)
1966 r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_VERTEX3DATTEN;
1967 else if (r_textureunits.integer >= 3 && vid.texunits >= 3)
1968 r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_VERTEX2D1DATTEN;
1969 else if (r_textureunits.integer >= 2 && vid.texunits >= 2)
1970 r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_VERTEX2DATTEN;
1972 r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_VERTEX;
1978 qglGetIntegerv(GL_DRAW_BUFFER, &drawbuffer);CHECKGLERROR
1979 qglGetIntegerv(GL_READ_BUFFER, &readbuffer);CHECKGLERROR
1980 r_shadow_drawbuffer = drawbuffer;
1981 r_shadow_readbuffer = readbuffer;
1983 r_shadow_cullface_front = r_refdef.view.cullface_front;
1984 r_shadow_cullface_back = r_refdef.view.cullface_back;
1987 void R_Shadow_RenderMode_ActiveLight(const rtlight_t *rtlight)
1989 rsurface.rtlight = rtlight;
1992 void R_Shadow_RenderMode_Reset(void)
1994 R_Mesh_ResetRenderTargets();
1995 R_SetViewport(&r_refdef.view.viewport);
1996 GL_Scissor(r_shadow_lightscissor[0], r_shadow_lightscissor[1], r_shadow_lightscissor[2], r_shadow_lightscissor[3]);
1997 R_Mesh_ResetTextureState();
1998 GL_DepthRange(0, 1);
2000 GL_DepthMask(false);
2001 GL_DepthFunc(GL_LEQUAL);
2002 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);CHECKGLERROR
2003 r_refdef.view.cullface_front = r_shadow_cullface_front;
2004 r_refdef.view.cullface_back = r_shadow_cullface_back;
2005 GL_CullFace(r_refdef.view.cullface_back);
2006 GL_Color(1, 1, 1, 1);
2007 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
2008 GL_BlendFunc(GL_ONE, GL_ZERO);
2009 R_SetupShader_Generic(NULL, NULL, GL_MODULATE, 1);
2010 r_shadow_usingshadowmap2d = false;
2011 r_shadow_usingshadowmaportho = false;
2012 R_SetStencil(false, 255, GL_KEEP, GL_KEEP, GL_KEEP, GL_ALWAYS, 128, 255);
2015 void R_Shadow_ClearStencil(void)
2017 GL_Clear(GL_STENCIL_BUFFER_BIT, NULL, 1.0f, 128);
2018 r_refdef.stats.lights_clears++;
2021 void R_Shadow_RenderMode_StencilShadowVolumes(qboolean zpass)
2023 r_shadow_rendermode_t mode = zpass ? r_shadow_shadowingrendermode_zpass : r_shadow_shadowingrendermode_zfail;
2024 if (r_shadow_rendermode == mode)
2026 R_Shadow_RenderMode_Reset();
2027 GL_DepthFunc(GL_LESS);
2028 GL_ColorMask(0, 0, 0, 0);
2029 GL_PolygonOffset(r_refdef.shadowpolygonfactor, r_refdef.shadowpolygonoffset);CHECKGLERROR
2030 GL_CullFace(GL_NONE);
2031 R_SetupShader_DepthOrShadow();
2032 r_shadow_rendermode = mode;
2037 case R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE:
2038 case R_SHADOW_RENDERMODE_ZPASS_SEPARATESTENCIL:
2039 R_SetStencilSeparate(true, 255, GL_KEEP, GL_KEEP, GL_INCR, GL_KEEP, GL_KEEP, GL_DECR, GL_ALWAYS, GL_ALWAYS, 128, 255);
2041 case R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE:
2042 case R_SHADOW_RENDERMODE_ZFAIL_SEPARATESTENCIL:
2043 R_SetStencilSeparate(true, 255, GL_KEEP, GL_INCR, GL_KEEP, GL_KEEP, GL_DECR, GL_KEEP, GL_ALWAYS, GL_ALWAYS, 128, 255);
2048 static void R_Shadow_MakeVSDCT(void)
2050 // maps to a 2x3 texture rectangle with normalized coordinates
2055 // stores abs(dir.xy), offset.xy/2.5
2056 unsigned char data[4*6] =
2058 255, 0, 0x33, 0x33, // +X: <1, 0>, <0.5, 0.5>
2059 255, 0, 0x99, 0x33, // -X: <1, 0>, <1.5, 0.5>
2060 0, 255, 0x33, 0x99, // +Y: <0, 1>, <0.5, 1.5>
2061 0, 255, 0x99, 0x99, // -Y: <0, 1>, <1.5, 1.5>
2062 0, 0, 0x33, 0xFF, // +Z: <0, 0>, <0.5, 2.5>
2063 0, 0, 0x99, 0xFF, // -Z: <0, 0>, <1.5, 2.5>
2065 r_shadow_shadowmapvsdcttexture = R_LoadTextureCubeMap(r_shadow_texturepool, "shadowmapvsdct", 1, data, TEXTYPE_RGBA, TEXF_FORCENEAREST | TEXF_CLAMP | TEXF_ALPHA, -1, NULL);
2068 static void R_Shadow_MakeShadowMap(int side, int size)
2070 switch (r_shadow_shadowmode)
2072 case R_SHADOW_SHADOWMODE_SHADOWMAP2D:
2073 if (r_shadow_shadowmap2dtexture) return;
2074 r_shadow_shadowmap2dtexture = R_LoadTextureShadowMap2D(r_shadow_texturepool, "shadowmap", size*2, size*(vid.support.arb_texture_non_power_of_two ? 3 : 4), r_shadow_shadowmapdepthbits, r_shadow_shadowmapsampler);
2075 r_shadow_shadowmap2dcolortexture = NULL;
2076 switch(vid.renderpath)
2079 case RENDERPATH_D3D9:
2080 r_shadow_shadowmap2dcolortexture = R_LoadTexture2D(r_shadow_texturepool, "shadowmaprendertarget", size*2, size*(vid.support.arb_texture_non_power_of_two ? 3 : 4), NULL, TEXTYPE_BGRA, TEXF_RENDERTARGET | TEXF_FORCENEAREST | TEXF_CLAMP | TEXF_ALPHA, -1, NULL);
2081 r_shadow_fbo2d = R_Mesh_CreateFramebufferObject(r_shadow_shadowmap2dtexture, r_shadow_shadowmap2dcolortexture, NULL, NULL, NULL);
2085 r_shadow_fbo2d = R_Mesh_CreateFramebufferObject(r_shadow_shadowmap2dtexture, NULL, NULL, NULL, NULL);
2093 // render depth into the fbo, do not render color at all
2094 // validate the fbo now
2098 qglDrawBuffer(GL_NONE);CHECKGLERROR
2099 qglReadBuffer(GL_NONE);CHECKGLERROR
2100 status = qglCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);CHECKGLERROR
2101 if (status != GL_FRAMEBUFFER_COMPLETE_EXT && (r_shadow_shadowmapping.integer || r_shadow_deferred.integer))
2103 Con_Printf("R_Shadow_MakeShadowMap: glCheckFramebufferStatusEXT returned %i\n", status);
2104 Cvar_SetValueQuick(&r_shadow_shadowmapping, 0);
2105 Cvar_SetValueQuick(&r_shadow_deferred, 0);
2110 void R_Shadow_RenderMode_ShadowMap(int side, int clear, int size)
2112 float nearclip, farclip, bias;
2113 r_viewport_t viewport;
2116 float clearcolor[4];
2117 nearclip = r_shadow_shadowmapping_nearclip.value / rsurface.rtlight->radius;
2119 bias = r_shadow_shadowmapping_bias.value * nearclip * (1024.0f / size);// * rsurface.rtlight->radius;
2120 r_shadow_shadowmap_parameters[1] = -nearclip * farclip / (farclip - nearclip) - 0.5f * bias;
2121 r_shadow_shadowmap_parameters[3] = 0.5f + 0.5f * (farclip + nearclip) / (farclip - nearclip);
2122 r_shadow_shadowmapside = side;
2123 r_shadow_shadowmapsize = size;
2125 r_shadow_shadowmap_parameters[0] = 0.5f * (size - r_shadow_shadowmapborder);
2126 r_shadow_shadowmap_parameters[2] = r_shadow_shadowmapvsdct ? 2.5f*size : size;
2127 R_Viewport_InitRectSideView(&viewport, &rsurface.rtlight->matrix_lighttoworld, side, size, r_shadow_shadowmapborder, nearclip, farclip, NULL);
2128 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAP2D) goto init_done;
2130 // complex unrolled cube approach (more flexible)
2131 if (r_shadow_shadowmapvsdct && !r_shadow_shadowmapvsdcttexture)
2132 R_Shadow_MakeVSDCT();
2133 if (!r_shadow_shadowmap2dtexture)
2134 R_Shadow_MakeShadowMap(side, r_shadow_shadowmapmaxsize);
2135 if (r_shadow_shadowmap2dtexture) fbo = r_shadow_fbo2d;
2136 r_shadow_shadowmap_texturescale[0] = 1.0f / R_TextureWidth(r_shadow_shadowmap2dtexture);
2137 r_shadow_shadowmap_texturescale[1] = 1.0f / R_TextureHeight(r_shadow_shadowmap2dtexture);
2138 r_shadow_rendermode = R_SHADOW_RENDERMODE_SHADOWMAP2D;
2140 R_Mesh_ResetTextureState();
2141 R_Mesh_ResetRenderTargets();
2142 R_Shadow_RenderMode_Reset();
2145 R_Mesh_SetRenderTargets(fbo, r_shadow_shadowmap2dtexture, r_shadow_shadowmap2dcolortexture, NULL, NULL, NULL);
2146 R_SetupShader_DepthOrShadow();
2149 R_SetupShader_ShowDepth();
2150 GL_PolygonOffset(r_shadow_shadowmapping_polygonfactor.value, r_shadow_shadowmapping_polygonoffset.value);
2155 R_SetViewport(&viewport);
2156 flipped = (side & 1) ^ (side >> 2);
2157 r_refdef.view.cullface_front = flipped ? r_shadow_cullface_back : r_shadow_cullface_front;
2158 r_refdef.view.cullface_back = flipped ? r_shadow_cullface_front : r_shadow_cullface_back;
2159 switch(vid.renderpath)
2161 case RENDERPATH_GL11:
2162 case RENDERPATH_GL13:
2163 case RENDERPATH_GL20:
2164 case RENDERPATH_SOFT:
2165 case RENDERPATH_GLES2:
2166 GL_CullFace(r_refdef.view.cullface_back);
2167 // OpenGL lets us scissor larger than the viewport, so go ahead and clear all views at once
2168 if ((clear & ((2 << side) - 1)) == (1 << side)) // only clear if the side is the first in the mask
2170 // get tightest scissor rectangle that encloses all viewports in the clear mask
2171 int x1 = clear & 0x15 ? 0 : size;
2172 int x2 = clear & 0x2A ? 2 * size : size;
2173 int y1 = clear & 0x03 ? 0 : (clear & 0xC ? size : 2 * size);
2174 int y2 = clear & 0x30 ? 3 * size : (clear & 0xC ? 2 * size : size);
2175 GL_Scissor(x1, y1, x2 - x1, y2 - y1);
2176 GL_Clear(GL_DEPTH_BUFFER_BIT, NULL, 1.0f, 0);
2178 GL_Scissor(viewport.x, viewport.y, viewport.width, viewport.height);
2180 case RENDERPATH_D3D9:
2181 case RENDERPATH_D3D10:
2182 case RENDERPATH_D3D11:
2183 Vector4Set(clearcolor, 1,1,1,1);
2184 // completely different meaning than in OpenGL path
2185 r_shadow_shadowmap_parameters[1] = 0;
2186 r_shadow_shadowmap_parameters[3] = -bias;
2187 // we invert the cull mode because we flip the projection matrix
2188 // NOTE: this actually does nothing because the DrawShadowMap code sets it to doublesided...
2189 GL_CullFace(r_refdef.view.cullface_front);
2190 // D3D considers it an error to use a scissor larger than the viewport... clear just this view
2191 GL_Scissor(viewport.x, viewport.y, viewport.width, viewport.height);
2192 if (r_shadow_shadowmapsampler)
2194 GL_ColorMask(0,0,0,0);
2196 GL_Clear(GL_DEPTH_BUFFER_BIT, clearcolor, 1.0f, 0);
2200 GL_ColorMask(1,1,1,1);
2202 GL_Clear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT, clearcolor, 1.0f, 0);
2208 void R_Shadow_RenderMode_Lighting(qboolean stenciltest, qboolean transparent, qboolean shadowmapping)
2210 R_Mesh_ResetTextureState();
2211 R_Mesh_ResetRenderTargets();
2214 r_shadow_lightscissor[0] = r_refdef.view.viewport.x;
2215 r_shadow_lightscissor[1] = r_refdef.view.viewport.y;
2216 r_shadow_lightscissor[2] = r_refdef.view.viewport.width;
2217 r_shadow_lightscissor[3] = r_refdef.view.viewport.height;
2219 R_Shadow_RenderMode_Reset();
2220 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2222 GL_DepthFunc(GL_EQUAL);
2223 // do global setup needed for the chosen lighting mode
2224 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_LIGHT_GLSL)
2225 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 0);
2226 r_shadow_usingshadowmap2d = shadowmapping;
2227 r_shadow_rendermode = r_shadow_lightingrendermode;
2228 // only draw light where this geometry was already rendered AND the
2229 // stencil is 128 (values other than this mean shadow)
2231 R_SetStencil(true, 255, GL_KEEP, GL_KEEP, GL_KEEP, GL_EQUAL, 128, 255);
2233 R_SetStencil(false, 255, GL_KEEP, GL_KEEP, GL_KEEP, GL_ALWAYS, 128, 255);
2236 static const unsigned short bboxelements[36] =
2246 static const float bboxpoints[8][3] =
2258 void R_Shadow_RenderMode_DrawDeferredLight(qboolean stenciltest, qboolean shadowmapping)
2261 float vertex3f[8*3];
2262 const matrix4x4_t *matrix = &rsurface.rtlight->matrix_lighttoworld;
2263 // do global setup needed for the chosen lighting mode
2264 R_Shadow_RenderMode_Reset();
2265 r_shadow_rendermode = r_shadow_lightingrendermode;
2266 R_EntityMatrix(&identitymatrix);
2267 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2268 // only draw light where this geometry was already rendered AND the
2269 // stencil is 128 (values other than this mean shadow)
2270 R_SetStencil(stenciltest, 255, GL_KEEP, GL_KEEP, GL_KEEP, GL_EQUAL, 128, 255);
2271 R_Mesh_SetRenderTargets(r_shadow_prepasslightingdiffusespecularfbo, r_shadow_prepassgeometrydepthtexture, r_shadow_prepasslightingdiffusetexture, r_shadow_prepasslightingspeculartexture, NULL, NULL);
2273 r_shadow_usingshadowmap2d = shadowmapping;
2275 // render the lighting
2276 R_SetupShader_DeferredLight(rsurface.rtlight);
2277 for (i = 0;i < 8;i++)
2278 Matrix4x4_Transform(matrix, bboxpoints[i], vertex3f + i*3);
2279 GL_ColorMask(1,1,1,1);
2280 GL_DepthMask(false);
2281 GL_DepthRange(0, 1);
2282 GL_PolygonOffset(0, 0);
2284 GL_DepthFunc(GL_GREATER);
2285 GL_CullFace(r_refdef.view.cullface_back);
2286 R_Mesh_PrepareVertices_Vertex3f(8, vertex3f, NULL);
2287 R_Mesh_Draw(0, 8, 0, 12, NULL, NULL, 0, bboxelements, NULL, 0);
2290 static void R_Shadow_UpdateBounceGridTexture(void)
2292 #define MAXBOUNCEGRIDPARTICLESPERLIGHT 1048576
2294 int flag = r_refdef.scene.rtworld ? LIGHTFLAG_REALTIMEMODE : LIGHTFLAG_NORMALMODE;
2298 int hitsupercontentsmask;
2306 unsigned char *pixel;
2307 unsigned char *pixels;
2308 unsigned int lightindex;
2310 unsigned int range1;
2311 unsigned int range2;
2312 unsigned int seed = (unsigned int)(realtime * 1000.0f);
2314 vec3_t baseshotcolor;
2325 vec_t lightintensity;
2327 qboolean isstatic = r_shadow_bouncegrid_updateinterval.value > 1.0f;
2329 if (!r_shadow_bouncegrid.integer || !vid.support.ext_texture_3d)
2331 if (r_shadow_bouncegridtexture)
2333 R_FreeTexture(r_shadow_bouncegridtexture);
2334 r_shadow_bouncegridtexture = NULL;
2338 if (r_refdef.scene.worldmodel)
2340 VectorSet(spacing, bound(1, r_shadow_bouncegrid_spacingx.value, 512), bound(1, r_shadow_bouncegrid_spacingy.value, 512), bound(1, r_shadow_bouncegrid_spacingz.value, 512));
2341 VectorMA(r_refdef.scene.worldmodel->normalmins, -2.0f, spacing, mins);
2342 VectorMA(r_refdef.scene.worldmodel->normalmaxs, 2.0f, spacing, maxs);
2343 VectorSubtract(maxs, mins, size);
2344 resolution[0] = (int)floor(size[0] / spacing[0] + 0.5f);
2345 resolution[1] = (int)floor(size[1] / spacing[1] + 0.5f);
2346 resolution[2] = (int)floor(size[2] / spacing[2] + 0.5f);
2347 resolution[0] = min(resolution[0], bound(4, r_shadow_bouncegrid_x.integer, (int)vid.maxtexturesize_3d));
2348 resolution[1] = min(resolution[1], bound(4, r_shadow_bouncegrid_y.integer, (int)vid.maxtexturesize_3d));
2349 resolution[2] = min(resolution[2], bound(4, r_shadow_bouncegrid_z.integer, (int)vid.maxtexturesize_3d));
2350 spacing[0] = size[0] / resolution[0];
2351 spacing[1] = size[1] / resolution[1];
2352 spacing[2] = size[2] / resolution[2];
2353 ispacing[0] = 1.0f / spacing[0];
2354 ispacing[1] = 1.0f / spacing[1];
2355 ispacing[2] = 1.0f / spacing[2];
2359 VectorSet(resolution, bound(4, r_shadow_bouncegrid_x.integer, (int)vid.maxtexturesize_3d), bound(4, r_shadow_bouncegrid_y.integer, (int)vid.maxtexturesize_3d), bound(4, r_shadow_bouncegrid_z.integer, (int)vid.maxtexturesize_3d));
2360 VectorSet(spacing, bound(1, r_shadow_bouncegrid_spacingx.value, 512), bound(1, r_shadow_bouncegrid_spacingy.value, 512), bound(1, r_shadow_bouncegrid_spacingz.value, 512));
2361 VectorMultiply(resolution, spacing, size);
2362 ispacing[0] = 1.0f / spacing[0];
2363 ispacing[1] = 1.0f / spacing[1];
2364 ispacing[2] = 1.0f / spacing[2];
2365 mins[0] = floor(r_refdef.view.origin[0] * ispacing[0] + 0.5f) * spacing[0] - 0.5f * size[0];
2366 mins[1] = floor(r_refdef.view.origin[1] * ispacing[1] + 0.5f) * spacing[1] - 0.5f * size[1];
2367 mins[2] = floor(r_refdef.view.origin[2] * ispacing[2] + 0.5f) * spacing[2] - 0.5f * size[2];
2368 VectorAdd(mins, size, maxs);
2370 memset(m, 0, sizeof(m));
2371 m[0] = 1.0f / size[0];
2372 m[3] = -mins[0] * m[0];
2373 m[5] = 1.0f / size[1];
2374 m[7] = -mins[1] * m[5];
2375 m[10] = 1.0f / size[2];
2376 m[11] = -mins[2] * m[10];
2378 Matrix4x4_FromArrayFloatD3D(&r_shadow_bouncegridmatrix, m);
2379 r_shadow_bouncegridintensity = r_shadow_bouncegrid_intensity.value;
2380 if (r_shadow_bouncegridtexture && realtime < r_shadow_bouncegridtime + r_shadow_bouncegrid_updateinterval.value && resolution[0] == r_shadow_bouncegridresolution[0] && resolution[1] == r_shadow_bouncegridresolution[1] && resolution[2] == r_shadow_bouncegridresolution[2])
2382 numpixels = resolution[0]*resolution[1]*resolution[2];
2383 // allocate pixels for this update...
2384 pixels = Mem_Alloc(r_main_mempool, numpixels * sizeof(unsigned char[4]));
2385 // figure out what we want to interact with
2386 if (r_shadow_bouncegrid_hitmodels.integer)
2387 hitsupercontentsmask = SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_LIQUIDSMASK;
2389 hitsupercontentsmask = SUPERCONTENTS_SOLID | SUPERCONTENTS_LIQUIDSMASK;
2390 // iterate world rtlights
2391 range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
2392 range1 = isstatic ? 0 : r_refdef.scene.numlights;
2393 range2 = range + range1;
2394 for (lightindex = 0;lightindex < range2;lightindex++)
2398 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
2399 if (!light || !(light->flags & flag))
2401 rtlight = &light->rtlight;
2402 // when static, we skip styled lights because they tend to change...
2403 if (rtlight->style > 0)
2405 VectorScale(rtlight->color, (rtlight->ambientscale + rtlight->diffusescale + rtlight->specularscale) * (rtlight->style >= 0 ? r_refdef.scene.rtlightstylevalue[rtlight->style] : 1), lightcolor);
2409 if (lightindex < range)
2411 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
2412 rtlight = &light->rtlight;
2415 rtlight = r_refdef.scene.lights[lightindex - range];
2416 // draw only visible lights (major speedup)
2419 VectorScale(rtlight->currentcolor, rtlight->ambientscale + rtlight->diffusescale + rtlight->specularscale, lightcolor);
2421 if (!VectorLength2(lightcolor))
2423 // shoot particles from this light
2424 // use a calculation for the number of particles that will not
2425 // vary with lightstyle, otherwise we get randomized particle
2426 // distribution, the seeded random is only consistent for a
2427 // consistent number of particles on this light...
2428 radius = rtlight->radius * bound(0.0001f, r_shadow_bouncegrid_lightradiusscale.value, 1024.0f);
2429 s = rtlight->radius / bound(1.0f, r_shadow_bouncegrid_particlespacing.value, 1048576.0f);
2430 lightintensity = VectorLength(rtlight->color) * rtlight->ambientscale + rtlight->diffusescale + rtlight->specularscale;
2431 if (lightindex >= range)
2432 lightintensity *= r_shadow_bouncegrid_dlightparticlemultiplier.value;
2433 shootparticles = (int)bound(0, lightintensity * s *s, MAXBOUNCEGRIDPARTICLESPERLIGHT);
2434 if (!shootparticles)
2436 s = 255.0f * r_shadow_bouncegrid_particleintensity.value / shootparticles;
2437 VectorScale(lightcolor, s, baseshotcolor);
2438 maxbounce = bound(1, r_shadow_bouncegrid_maxbounce.integer, 16);
2439 for (shotparticles = 0;shotparticles < shootparticles;shotparticles++)
2441 if (r_shadow_bouncegrid_stablerandom.integer > 0)
2442 seed = lightindex * 11937 + shotparticles;
2443 VectorCopy(baseshotcolor, shotcolor);
2444 VectorCopy(rtlight->shadoworigin, clipstart);
2445 if (r_shadow_bouncegrid_stablerandom.integer < 0)
2446 VectorRandom(clipend);
2448 VectorCheeseRandom(clipend);
2449 VectorMA(clipstart, radius, clipend, clipend);
2450 bouncelimit = 1 + (rtlight->particlecache_updateparticle % maxbounce);
2451 for (bouncecount = 0;;bouncecount++)
2453 cliptrace = CL_TraceLine(clipstart, clipend, r_shadow_bouncegrid_hitmodels.integer ? MOVE_HITMODEL : MOVE_NOMONSTERS, NULL, hitsupercontentsmask, true, false, NULL, true);
2454 //Collision_ClipLineToWorld(&cliptrace, cl.worldmodel, clipstart, clipend, hitsupercontentsmask);
2455 if (cliptrace.fraction >= 1.0f)
2457 if (VectorLength2(shotcolor) < 1.0f)
2459 if (bouncecount > 0)
2461 // figure out which texture pixel this is in
2462 tex[0] = (int)((cliptrace.endpos[0] - mins[0]) * ispacing[0]);
2463 tex[1] = (int)((cliptrace.endpos[1] - mins[1]) * ispacing[1]);
2464 tex[2] = (int)((cliptrace.endpos[2] - mins[2]) * ispacing[2]);
2465 if (tex[0] >= 1 && tex[1] >= 1 && tex[2] >= 1 && tex[0] < resolution[0] - 1 && tex[1] < resolution[1] - 1 && tex[2] < resolution[2] - 1)
2467 // it is within bounds...
2468 pixel = pixels + 4 * ((tex[2]*resolution[1]+tex[1])*resolution[0]+tex[0]);
2469 // add to the pixel color
2470 c[0] = pixel[0] + (int)shotcolor[2];
2471 c[1] = pixel[1] + (int)shotcolor[1];
2472 c[2] = pixel[2] + (int)shotcolor[0];
2473 pixel[0] = (unsigned char)min(c[0], 255);
2474 pixel[1] = (unsigned char)min(c[1], 255);
2475 pixel[2] = (unsigned char)min(c[2], 255);
2479 if (bouncecount >= bouncelimit)
2481 // scale down shot color by bounce intensity and texture color
2482 VectorScale(shotcolor, r_shadow_bouncegrid_particlebounceintensity.value, shotcolor);
2483 if (cliptrace.hittexture && cliptrace.hittexture->currentskinframe)
2484 VectorMultiply(shotcolor, rsurface.texture->currentskinframe->avgcolor, shotcolor);
2485 // reflect the remaining portion of the line across plane normal
2486 //VectorSubtract(clipend, cliptrace.endpos, clipdiff);
2487 //VectorReflect(clipdiff, 1.0, cliptrace.plane.normal, clipend);
2488 // random direction, primarily along plane normal
2489 s = VectorDistance(cliptrace.endpos, clipend);
2490 if (r_shadow_bouncegrid_stablerandom.integer < 0)
2491 VectorRandom(clipend);
2493 VectorCheeseRandom(clipend);
2494 VectorMA(cliptrace.plane.normal, 0.95f, clipend, clipend);
2495 VectorNormalize(clipend);
2496 VectorScale(clipend, s, clipend);
2497 // calculate the new line start and end
2498 VectorCopy(cliptrace.endpos, clipstart);
2499 VectorAdd(clipstart, clipend, clipend);
2503 if (r_shadow_bouncegridtexture && r_shadow_bouncegridresolution[0] == resolution[0] && r_shadow_bouncegridresolution[1] == resolution[1] && r_shadow_bouncegridresolution[2] == resolution[2])
2504 R_UpdateTexture(r_shadow_bouncegridtexture, pixels, 0, 0, 0, resolution[0], resolution[1], resolution[2]);
2507 VectorCopy(resolution, r_shadow_bouncegridresolution);
2508 if (r_shadow_bouncegridtexture)
2509 R_FreeTexture(r_shadow_bouncegridtexture);
2510 r_shadow_bouncegridtexture = R_LoadTexture3D(r_shadow_texturepool, "bouncegrid", resolution[0], resolution[1], resolution[2], pixels, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCELINEAR, 0, NULL);
2513 r_shadow_bouncegridtime = realtime;
2516 #define MAXPARTICLESPERLIGHT 262144
2517 #define MAXLIGHTSPERDRAW 1024
2519 static void R_Shadow_RenderParticlesForLight(rtlight_t *rtlight)
2525 int hitsupercontentsmask;
2528 int shootparticles = 0;
2531 unsigned int seed = 0;
2532 static unsigned short bouncelight_elements[MAXLIGHTSPERDRAW*36];
2533 static float vertex3f[MAXLIGHTSPERDRAW*24];
2534 static float lightorigin4f[MAXLIGHTSPERDRAW*32];
2535 static float color4f[MAXLIGHTSPERDRAW*32];
2536 float scaledpoints[8][3];
2540 rtlight_particle_t *p;
2541 vec_t wantparticles = 0;
2545 vec_t iparticlesize;
2551 vec3_t currentcolor;
2556 if (!rtlight->draw || !rtlight->isstatic || !r_shadow_usingdeferredprepass)
2558 if (r_shadow_particletrace.integer)
2560 radius = rtlight->radius * bound(0.0001f, r_shadow_particletrace_radiusscale.value, 1.0f) - r_shadow_particletrace_size.value;
2561 s = rtlight->radius / bound(1.0f, r_shadow_particletrace_particlespacing.value * r_shadow_particletrace_size.value, 1048576.0f);
2562 wantparticles = s*s;
2563 n = (int)bound(0, wantparticles, MAXPARTICLESPERLIGHT);
2567 shootparticles = (int)(n * r_shadow_particletrace_updatepercentage.value);
2568 if ((n && !rtlight->particlecache_particles) || rtlight->particlecache_maxparticles != n)
2570 if (rtlight->particlecache_particles)
2571 Mem_Free(rtlight->particlecache_particles);
2572 rtlight->particlecache_particles = NULL;
2573 rtlight->particlecache_numparticles = 0;
2574 rtlight->particlecache_maxparticles = n;
2575 rtlight->particlecache_updateparticle = 0;
2576 if (rtlight->particlecache_maxparticles)
2577 rtlight->particlecache_particles = Mem_Alloc(r_main_mempool, rtlight->particlecache_maxparticles * sizeof(*rtlight->particlecache_particles));
2578 shootparticles = n * 16;
2581 if (!rtlight->particlecache_maxparticles)
2584 // if (rtlight->particlecache_numparticles < rtlight->particlecache_maxparticles)
2585 // shootparticles = rtlight->particlecache_maxparticles;
2587 // if (rtlight->particlecache_numparticles >= rtlight->particlecache_maxparticles)
2588 // shootparticles = 0;
2590 maxbounce = bound(1, r_shadow_particletrace_maxbounce.integer, 16);
2591 r_refdef.stats.lights_bouncelightsupdated += shootparticles;
2592 for (shotparticles = 0;shotparticles < shootparticles;shotparticles++)
2594 seed = rtlight->particlecache_updateparticle;
2595 VectorSet(shotcolor, 1.0f, 1.0f, 1.0f);
2596 VectorCopy(rtlight->shadoworigin, clipstart);
2597 VectorRandom(clipend);
2598 VectorMA(clipstart, radius, clipend, clipend);
2599 hitsupercontentsmask = SUPERCONTENTS_SOLID | SUPERCONTENTS_LIQUIDSMASK;
2600 bouncelimit = 1 + (rtlight->particlecache_updateparticle % maxbounce);
2601 for (bouncecount = 0;;bouncecount++)
2603 cliptrace = CL_TraceLine(clipstart, clipend, MOVE_NOMONSTERS, NULL, hitsupercontentsmask, true, false, NULL, true);
2604 //Collision_ClipLineToWorld(&cliptrace, cl.worldmodel, clipstart, clipend, hitsupercontentsmask);
2605 if (cliptrace.fraction >= 1.0f)
2607 if (VectorLength2(shotcolor) < (1.0f / 262144.0f))
2609 if (bouncecount >= bouncelimit)
2611 VectorCopy(cliptrace.endpos, rtlight->particlecache_particles[rtlight->particlecache_updateparticle].origin);
2612 VectorCopy(shotcolor, rtlight->particlecache_particles[rtlight->particlecache_updateparticle].color);
2613 rtlight->particlecache_updateparticle++;
2614 if (rtlight->particlecache_numparticles < rtlight->particlecache_updateparticle)
2615 rtlight->particlecache_numparticles = rtlight->particlecache_updateparticle;
2616 if (rtlight->particlecache_updateparticle >= rtlight->particlecache_maxparticles)
2618 rtlight->particlecache_updateparticle = 0;
2619 shotparticles = shootparticles;
2623 // scale down shot color by bounce intensity and texture color
2624 VectorScale(shotcolor, r_shadow_particletrace_bounceintensity.value, shotcolor);
2625 if (cliptrace.hittexture && cliptrace.hittexture->currentskinframe)
2626 VectorMultiply(shotcolor, rsurface.texture->currentskinframe->avgcolor, shotcolor);
2627 // reflect the remaining portion of the line across plane normal
2628 //VectorSubtract(clipend, cliptrace.endpos, clipdiff);
2629 //VectorReflect(clipdiff, 1.0, cliptrace.plane.normal, clipend);
2630 // random direction, primarily along plane normal
2631 s = VectorDistance(cliptrace.endpos, clipend);
2632 VectorRandom(clipend);
2633 VectorMA(cliptrace.plane.normal, 0.95f, clipend, clipend);
2634 VectorNormalize(clipend);
2635 VectorScale(clipend, s, clipend);
2636 // calculate the new line start and end
2637 VectorCopy(cliptrace.endpos, clipstart);
2638 VectorAdd(clipstart, clipend, clipend);
2642 if (!rtlight->particlecache_numparticles)
2645 // render the particles as deferred lights
2646 // do global setup needed for the chosen lighting mode
2647 R_Shadow_RenderMode_Reset();
2648 r_shadow_rendermode = r_shadow_lightingrendermode;
2649 r_shadow_usingshadowmap2d = false;
2650 R_EntityMatrix(&identitymatrix);
2651 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2652 // only draw light where this geometry was already rendered AND the
2653 // stencil is 128 (values other than this mean shadow)
2654 R_SetStencil(false, 255, GL_KEEP, GL_KEEP, GL_KEEP, GL_EQUAL, 128, 255);
2655 R_Mesh_SetRenderTargets(r_shadow_prepasslightingdiffusefbo, r_shadow_prepassgeometrydepthtexture, r_shadow_prepasslightingdiffusetexture, NULL, NULL, NULL);
2656 R_SetupShader_DeferredBounceLight();
2657 GL_ColorMask(1,1,1,1);
2658 GL_DepthMask(false);
2659 GL_DepthRange(0, 1);
2660 GL_PolygonOffset(0, 0);
2662 GL_DepthFunc(GL_GREATER);
2663 GL_CullFace(r_refdef.view.cullface_back);
2664 s = r_shadow_particletrace_intensity.value / (float)rtlight->particlecache_numparticles;
2665 VectorScale(rtlight->currentcolor, s, currentcolor);
2666 particlesize = bound(0.0001f, r_shadow_particletrace_size.value, 1024.0f);
2667 iparticlesize = 1.0f / particlesize;
2668 // VectorScale(r_refdef.view.forward, particlesize, offset);
2669 // VectorScale(r_refdef.view.left, -particlesize, right);
2670 // VectorScale(r_refdef.view.up, particlesize, up);
2671 org[3] = iparticlesize;
2674 lo4f = lightorigin4f;
2677 if (!bouncelight_elements[1])
2678 for (i = 0;i < MAXLIGHTSPERDRAW;i++)
2679 for (j = 0;j < 36;j++)
2680 bouncelight_elements[i*36+j] = i*8+bboxelements[j];
2681 for (j = 0;j < 8;j++)
2682 VectorScale(bboxpoints[j], particlesize, scaledpoints[j]);
2683 r_refdef.stats.lights_bouncelightscounted += rtlight->particlecache_numparticles;
2684 for (j = 0, p = rtlight->particlecache_particles, n = rtlight->particlecache_numparticles;j < n;j++, p++)
2686 VectorCopy(p->origin, org);
2687 // org[3] is set above
2688 VectorMultiply(p->color, currentcolor, color);
2689 // color[3] is set above
2690 VectorAdd(scaledpoints[0], org, v3f + 0);
2691 VectorAdd(scaledpoints[1], org, v3f + 3);
2692 VectorAdd(scaledpoints[2], org, v3f + 6);
2693 VectorAdd(scaledpoints[3], org, v3f + 9);
2694 VectorAdd(scaledpoints[4], org, v3f + 12);
2695 VectorAdd(scaledpoints[5], org, v3f + 15);
2696 VectorAdd(scaledpoints[6], org, v3f + 18);
2697 VectorAdd(scaledpoints[7], org, v3f + 21);
2698 Vector4Copy(org, lo4f + 0);
2699 Vector4Copy(org, lo4f + 4);
2700 Vector4Copy(org, lo4f + 8);
2701 Vector4Copy(org, lo4f + 12);
2702 Vector4Copy(org, lo4f + 16);
2703 Vector4Copy(org, lo4f + 20);
2704 Vector4Copy(org, lo4f + 24);
2705 Vector4Copy(org, lo4f + 28);
2706 Vector4Copy(color, c4f + 0);
2707 Vector4Copy(color, c4f + 4);
2708 Vector4Copy(color, c4f + 8);
2709 Vector4Copy(color, c4f + 12);
2710 Vector4Copy(color, c4f + 16);
2711 Vector4Copy(color, c4f + 20);
2712 Vector4Copy(color, c4f + 24);
2713 Vector4Copy(color, c4f + 28);
2718 if (batchcount >= MAXLIGHTSPERDRAW)
2720 r_refdef.stats.lights_bouncelightsdrawn += batchcount;
2721 R_Mesh_PrepareVertices_BounceLight_Arrays(batchcount*8, vertex3f, color4f, lightorigin4f);
2722 R_Mesh_Draw(0, batchcount*8, 0, batchcount*12, NULL, NULL, 0, bouncelight_elements, NULL, 0);
2724 lo4f = lightorigin4f;
2731 r_refdef.stats.lights_bouncelightsdrawn += batchcount;
2732 R_Mesh_PrepareVertices_BounceLight_Arrays(batchcount*8, vertex3f, color4f, lightorigin4f);
2733 R_Mesh_Draw(0, batchcount*8, 0, batchcount*12, NULL, NULL, 0, bouncelight_elements, NULL, 0);
2735 lo4f = lightorigin4f;
2741 void R_Shadow_RenderMode_VisibleShadowVolumes(void)
2743 R_Shadow_RenderMode_Reset();
2744 GL_BlendFunc(GL_ONE, GL_ONE);
2745 GL_DepthRange(0, 1);
2746 GL_DepthTest(r_showshadowvolumes.integer < 2);
2747 GL_Color(0.0, 0.0125 * r_refdef.view.colorscale, 0.1 * r_refdef.view.colorscale, 1);
2748 GL_PolygonOffset(r_refdef.shadowpolygonfactor, r_refdef.shadowpolygonoffset);CHECKGLERROR
2749 GL_CullFace(GL_NONE);
2750 r_shadow_rendermode = R_SHADOW_RENDERMODE_VISIBLEVOLUMES;
2753 void R_Shadow_RenderMode_VisibleLighting(qboolean stenciltest, qboolean transparent)
2755 R_Shadow_RenderMode_Reset();
2756 GL_BlendFunc(GL_ONE, GL_ONE);
2757 GL_DepthRange(0, 1);
2758 GL_DepthTest(r_showlighting.integer < 2);
2759 GL_Color(0.1 * r_refdef.view.colorscale, 0.0125 * r_refdef.view.colorscale, 0, 1);
2761 GL_DepthFunc(GL_EQUAL);
2762 R_SetStencil(stenciltest, 255, GL_KEEP, GL_KEEP, GL_KEEP, GL_EQUAL, 128, 255);
2763 r_shadow_rendermode = R_SHADOW_RENDERMODE_VISIBLELIGHTING;
2766 void R_Shadow_RenderMode_End(void)
2768 R_Shadow_RenderMode_Reset();
2769 R_Shadow_RenderMode_ActiveLight(NULL);
2771 GL_Scissor(r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
2772 r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
2775 int bboxedges[12][2] =
2794 qboolean R_Shadow_ScissorForBBox(const float *mins, const float *maxs)
2796 if (!r_shadow_scissor.integer)
2798 r_shadow_lightscissor[0] = r_refdef.view.viewport.x;
2799 r_shadow_lightscissor[1] = r_refdef.view.viewport.y;
2800 r_shadow_lightscissor[2] = r_refdef.view.viewport.width;
2801 r_shadow_lightscissor[3] = r_refdef.view.viewport.height;
2804 if(R_ScissorForBBox(mins, maxs, r_shadow_lightscissor))
2805 return true; // invisible
2806 if(r_shadow_lightscissor[0] != r_refdef.view.viewport.x
2807 || r_shadow_lightscissor[1] != r_refdef.view.viewport.y
2808 || r_shadow_lightscissor[2] != r_refdef.view.viewport.width
2809 || r_shadow_lightscissor[3] != r_refdef.view.viewport.height)
2810 r_refdef.stats.lights_scissored++;
2814 static void R_Shadow_RenderLighting_Light_Vertex_Shading(int firstvertex, int numverts, const float *diffusecolor, const float *ambientcolor)
2817 const float *vertex3f;
2818 const float *normal3f;
2820 float dist, dot, distintensity, shadeintensity, v[3], n[3];
2821 switch (r_shadow_rendermode)
2823 case R_SHADOW_RENDERMODE_LIGHT_VERTEX3DATTEN:
2824 case R_SHADOW_RENDERMODE_LIGHT_VERTEX2D1DATTEN:
2825 if (VectorLength2(diffusecolor) > 0)
2827 for (i = 0, vertex3f = rsurface.batchvertex3f + 3*firstvertex, normal3f = rsurface.batchnormal3f + 3*firstvertex, color4f = rsurface.passcolor4f + 4 * firstvertex;i < numverts;i++, vertex3f += 3, normal3f += 3, color4f += 4)
2829 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
2830 Matrix4x4_Transform3x3(&rsurface.entitytolight, normal3f, n);
2831 if ((dot = DotProduct(n, v)) < 0)
2833 shadeintensity = -dot / sqrt(VectorLength2(v) * VectorLength2(n));
2834 VectorMA(ambientcolor, shadeintensity, diffusecolor, color4f);
2837 VectorCopy(ambientcolor, color4f);
2838 if (r_refdef.fogenabled)
2841 f = RSurf_FogVertex(vertex3f);
2842 VectorScale(color4f, f, color4f);
2849 for (i = 0, vertex3f = rsurface.batchvertex3f + 3*firstvertex, color4f = rsurface.passcolor4f + 4 * firstvertex;i < numverts;i++, vertex3f += 3, color4f += 4)
2851 VectorCopy(ambientcolor, color4f);
2852 if (r_refdef.fogenabled)
2855 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
2856 f = RSurf_FogVertex(vertex3f);
2857 VectorScale(color4f + 4*i, f, color4f);
2863 case R_SHADOW_RENDERMODE_LIGHT_VERTEX2DATTEN:
2864 if (VectorLength2(diffusecolor) > 0)
2866 for (i = 0, vertex3f = rsurface.batchvertex3f + 3*firstvertex, normal3f = rsurface.batchnormal3f + 3*firstvertex, color4f = rsurface.passcolor4f + 4 * firstvertex;i < numverts;i++, vertex3f += 3, normal3f += 3, color4f += 4)
2868 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
2869 if ((dist = fabs(v[2])) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
2871 Matrix4x4_Transform3x3(&rsurface.entitytolight, normal3f, n);
2872 if ((dot = DotProduct(n, v)) < 0)
2874 shadeintensity = -dot / sqrt(VectorLength2(v) * VectorLength2(n));
2875 color4f[0] = (ambientcolor[0] + shadeintensity * diffusecolor[0]) * distintensity;
2876 color4f[1] = (ambientcolor[1] + shadeintensity * diffusecolor[1]) * distintensity;
2877 color4f[2] = (ambientcolor[2] + shadeintensity * diffusecolor[2]) * distintensity;
2881 color4f[0] = ambientcolor[0] * distintensity;
2882 color4f[1] = ambientcolor[1] * distintensity;
2883 color4f[2] = ambientcolor[2] * distintensity;
2885 if (r_refdef.fogenabled)
2888 f = RSurf_FogVertex(vertex3f);
2889 VectorScale(color4f, f, color4f);
2893 VectorClear(color4f);
2899 for (i = 0, vertex3f = rsurface.batchvertex3f + 3*firstvertex, color4f = rsurface.passcolor4f + 4 * firstvertex;i < numverts;i++, vertex3f += 3, color4f += 4)
2901 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
2902 if ((dist = fabs(v[2])) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
2904 color4f[0] = ambientcolor[0] * distintensity;
2905 color4f[1] = ambientcolor[1] * distintensity;
2906 color4f[2] = ambientcolor[2] * distintensity;
2907 if (r_refdef.fogenabled)
2910 f = RSurf_FogVertex(vertex3f);
2911 VectorScale(color4f, f, color4f);
2915 VectorClear(color4f);
2920 case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
2921 if (VectorLength2(diffusecolor) > 0)
2923 for (i = 0, vertex3f = rsurface.batchvertex3f + 3*firstvertex, normal3f = rsurface.batchnormal3f + 3*firstvertex, color4f = rsurface.passcolor4f + 4 * firstvertex;i < numverts;i++, vertex3f += 3, normal3f += 3, color4f += 4)
2925 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
2926 if ((dist = VectorLength(v)) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
2928 distintensity = (1 - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist);
2929 Matrix4x4_Transform3x3(&rsurface.entitytolight, normal3f, n);
2930 if ((dot = DotProduct(n, v)) < 0)
2932 shadeintensity = -dot / sqrt(VectorLength2(v) * VectorLength2(n));
2933 color4f[0] = (ambientcolor[0] + shadeintensity * diffusecolor[0]) * distintensity;
2934 color4f[1] = (ambientcolor[1] + shadeintensity * diffusecolor[1]) * distintensity;
2935 color4f[2] = (ambientcolor[2] + shadeintensity * diffusecolor[2]) * distintensity;
2939 color4f[0] = ambientcolor[0] * distintensity;
2940 color4f[1] = ambientcolor[1] * distintensity;
2941 color4f[2] = ambientcolor[2] * distintensity;
2943 if (r_refdef.fogenabled)
2946 f = RSurf_FogVertex(vertex3f);
2947 VectorScale(color4f, f, color4f);
2951 VectorClear(color4f);
2957 for (i = 0, vertex3f = rsurface.batchvertex3f + 3*firstvertex, color4f = rsurface.passcolor4f + 4 * firstvertex;i < numverts;i++, vertex3f += 3, color4f += 4)
2959 Matrix4x4_Transform(&rsurface.entitytolight, vertex3f, v);
2960 if ((dist = VectorLength(v)) < 1 && (distintensity = r_shadow_attentable[(int)(dist * ATTENTABLESIZE)]))
2962 distintensity = (1 - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist);
2963 color4f[0] = ambientcolor[0] * distintensity;
2964 color4f[1] = ambientcolor[1] * distintensity;
2965 color4f[2] = ambientcolor[2] * distintensity;
2966 if (r_refdef.fogenabled)
2969 f = RSurf_FogVertex(vertex3f);
2970 VectorScale(color4f, f, color4f);
2974 VectorClear(color4f);
2984 static void R_Shadow_RenderLighting_VisibleLighting(int texturenumsurfaces, const msurface_t **texturesurfacelist)
2986 // used to display how many times a surface is lit for level design purposes
2987 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_NOGAPS, texturenumsurfaces, texturesurfacelist);
2988 R_Mesh_PrepareVertices_Generic_Arrays(rsurface.batchnumvertices, rsurface.batchvertex3f, NULL, NULL);
2992 static void R_Shadow_RenderLighting_Light_GLSL(int texturenumsurfaces, const msurface_t **texturesurfacelist, const vec3_t lightcolor, float ambientscale, float diffusescale, float specularscale)
2994 // ARB2 GLSL shader path (GFFX5200, Radeon 9500)
2995 R_SetupShader_Surface(lightcolor, false, ambientscale, diffusescale, specularscale, RSURFPASS_RTLIGHT, texturenumsurfaces, texturesurfacelist, NULL);
2996 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
2997 GL_DepthFunc(GL_EQUAL);
2999 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
3000 GL_DepthFunc(GL_LEQUAL);
3003 static void R_Shadow_RenderLighting_Light_Vertex_Pass(int firstvertex, int numvertices, int numtriangles, const int *element3i, vec3_t diffusecolor2, vec3_t ambientcolor2)
3010 int newnumtriangles;
3014 int maxtriangles = 4096;
3015 static int newelements[4096*3];
3016 R_Shadow_RenderLighting_Light_Vertex_Shading(firstvertex, numvertices, diffusecolor2, ambientcolor2);
3017 for (renders = 0;renders < 4;renders++)
3022 newnumtriangles = 0;
3024 // due to low fillrate on the cards this vertex lighting path is
3025 // designed for, we manually cull all triangles that do not
3026 // contain a lit vertex
3027 // this builds batches of triangles from multiple surfaces and
3028 // renders them at once
3029 for (i = 0, e = element3i;i < numtriangles;i++, e += 3)
3031 if (VectorLength2(rsurface.passcolor4f + e[0] * 4) + VectorLength2(rsurface.passcolor4f + e[1] * 4) + VectorLength2(rsurface.passcolor4f + e[2] * 4) >= 0.01)
3033 if (newnumtriangles)
3035 newfirstvertex = min(newfirstvertex, e[0]);
3036 newlastvertex = max(newlastvertex, e[0]);
3040 newfirstvertex = e[0];
3041 newlastvertex = e[0];
3043 newfirstvertex = min(newfirstvertex, e[1]);
3044 newlastvertex = max(newlastvertex, e[1]);
3045 newfirstvertex = min(newfirstvertex, e[2]);
3046 newlastvertex = max(newlastvertex, e[2]);
3052 if (newnumtriangles >= maxtriangles)
3054 R_Mesh_Draw(newfirstvertex, newlastvertex - newfirstvertex + 1, 0, newnumtriangles, newelements, NULL, 0, NULL, NULL, 0);
3055 newnumtriangles = 0;
3061 if (newnumtriangles >= 1)
3063 R_Mesh_Draw(newfirstvertex, newlastvertex - newfirstvertex + 1, 0, newnumtriangles, newelements, NULL, 0, NULL, NULL, 0);
3066 // if we couldn't find any lit triangles, exit early
3069 // now reduce the intensity for the next overbright pass
3070 // we have to clamp to 0 here incase the drivers have improper
3071 // handling of negative colors
3072 // (some old drivers even have improper handling of >1 color)
3074 for (i = 0, c = rsurface.passcolor4f + 4 * firstvertex;i < numvertices;i++, c += 4)
3076 if (c[0] > 1 || c[1] > 1 || c[2] > 1)
3078 c[0] = max(0, c[0] - 1);
3079 c[1] = max(0, c[1] - 1);
3080 c[2] = max(0, c[2] - 1);
3092 static void R_Shadow_RenderLighting_Light_Vertex(int texturenumsurfaces, const msurface_t **texturesurfacelist, const vec3_t lightcolor, float ambientscale, float diffusescale)
3094 // OpenGL 1.1 path (anything)
3095 float ambientcolorbase[3], diffusecolorbase[3];
3096 float ambientcolorpants[3], diffusecolorpants[3];
3097 float ambientcolorshirt[3], diffusecolorshirt[3];
3098 const float *surfacecolor = rsurface.texture->dlightcolor;
3099 const float *surfacepants = rsurface.colormap_pantscolor;
3100 const float *surfaceshirt = rsurface.colormap_shirtcolor;
3101 rtexture_t *basetexture = rsurface.texture->basetexture;
3102 rtexture_t *pantstexture = rsurface.texture->pantstexture;
3103 rtexture_t *shirttexture = rsurface.texture->shirttexture;
3104 qboolean dopants = pantstexture && VectorLength2(surfacepants) >= (1.0f / 1048576.0f);
3105 qboolean doshirt = shirttexture && VectorLength2(surfaceshirt) >= (1.0f / 1048576.0f);
3106 ambientscale *= 2 * r_refdef.view.colorscale;
3107 diffusescale *= 2 * r_refdef.view.colorscale;
3108 ambientcolorbase[0] = lightcolor[0] * ambientscale * surfacecolor[0];ambientcolorbase[1] = lightcolor[1] * ambientscale * surfacecolor[1];ambientcolorbase[2] = lightcolor[2] * ambientscale * surfacecolor[2];
3109 diffusecolorbase[0] = lightcolor[0] * diffusescale * surfacecolor[0];diffusecolorbase[1] = lightcolor[1] * diffusescale * surfacecolor[1];diffusecolorbase[2] = lightcolor[2] * diffusescale * surfacecolor[2];
3110 ambientcolorpants[0] = ambientcolorbase[0] * surfacepants[0];ambientcolorpants[1] = ambientcolorbase[1] * surfacepants[1];ambientcolorpants[2] = ambientcolorbase[2] * surfacepants[2];
3111 diffusecolorpants[0] = diffusecolorbase[0] * surfacepants[0];diffusecolorpants[1] = diffusecolorbase[1] * surfacepants[1];diffusecolorpants[2] = diffusecolorbase[2] * surfacepants[2];
3112 ambientcolorshirt[0] = ambientcolorbase[0] * surfaceshirt[0];ambientcolorshirt[1] = ambientcolorbase[1] * surfaceshirt[1];ambientcolorshirt[2] = ambientcolorbase[2] * surfaceshirt[2];
3113 diffusecolorshirt[0] = diffusecolorbase[0] * surfaceshirt[0];diffusecolorshirt[1] = diffusecolorbase[1] * surfaceshirt[1];diffusecolorshirt[2] = diffusecolorbase[2] * surfaceshirt[2];
3114 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | (diffusescale > 0 ? BATCHNEED_ARRAY_NORMAL : 0) | BATCHNEED_ARRAY_TEXCOORD | BATCHNEED_NOGAPS, texturenumsurfaces, texturesurfacelist);
3115 rsurface.passcolor4f = (float *)R_FrameData_Alloc((rsurface.batchfirstvertex + rsurface.batchnumvertices) * sizeof(float[4]));
3116 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer, rsurface.batchvertex3f_bufferoffset);
3117 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), rsurface.passcolor4f, 0, 0);
3118 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), rsurface.batchtexcoordtexture2f, rsurface.batchtexcoordtexture2f_vertexbuffer, rsurface.batchtexcoordtexture2f_bufferoffset);
3119 R_Mesh_TexBind(0, basetexture);
3120 R_Mesh_TexMatrix(0, &rsurface.texture->currenttexmatrix);
3121 R_Mesh_TexCombine(0, GL_MODULATE, GL_MODULATE, 1, 1);
3122 switch(r_shadow_rendermode)
3124 case R_SHADOW_RENDERMODE_LIGHT_VERTEX3DATTEN:
3125 R_Mesh_TexBind(1, r_shadow_attenuation3dtexture);
3126 R_Mesh_TexMatrix(1, &rsurface.entitytoattenuationxyz);
3127 R_Mesh_TexCombine(1, GL_MODULATE, GL_MODULATE, 1, 1);
3128 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT, sizeof(float[3]), rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer, rsurface.batchvertex3f_bufferoffset);
3130 case R_SHADOW_RENDERMODE_LIGHT_VERTEX2D1DATTEN:
3131 R_Mesh_TexBind(2, r_shadow_attenuation2dtexture);
3132 R_Mesh_TexMatrix(2, &rsurface.entitytoattenuationz);
3133 R_Mesh_TexCombine(2, GL_MODULATE, GL_MODULATE, 1, 1);
3134 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT, sizeof(float[3]), rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer, rsurface.batchvertex3f_bufferoffset);
3136 case R_SHADOW_RENDERMODE_LIGHT_VERTEX2DATTEN:
3137 R_Mesh_TexBind(1, r_shadow_attenuation2dtexture);
3138 R_Mesh_TexMatrix(1, &rsurface.entitytoattenuationxyz);
3139 R_Mesh_TexCombine(1, GL_MODULATE, GL_MODULATE, 1, 1);
3140 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT, sizeof(float[3]), rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer, rsurface.batchvertex3f_bufferoffset);
3142 case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
3147 //R_Mesh_TexBind(0, basetexture);
3148 R_Shadow_RenderLighting_Light_Vertex_Pass(rsurface.batchfirstvertex, rsurface.batchnumvertices, rsurface.batchnumtriangles, rsurface.batchelement3i + 3*rsurface.batchfirsttriangle, diffusecolorbase, ambientcolorbase);
3151 R_Mesh_TexBind(0, pantstexture);
3152 R_Shadow_RenderLighting_Light_Vertex_Pass(rsurface.batchfirstvertex, rsurface.batchnumvertices, rsurface.batchnumtriangles, rsurface.batchelement3i + 3*rsurface.batchfirsttriangle, diffusecolorpants, ambientcolorpants);
3156 R_Mesh_TexBind(0, shirttexture);
3157 R_Shadow_RenderLighting_Light_Vertex_Pass(rsurface.batchfirstvertex, rsurface.batchnumvertices, rsurface.batchnumtriangles, rsurface.batchelement3i + 3*rsurface.batchfirsttriangle, diffusecolorshirt, ambientcolorshirt);
3161 extern cvar_t gl_lightmaps;
3162 void R_Shadow_RenderLighting(int texturenumsurfaces, const msurface_t **texturesurfacelist)
3164 float ambientscale, diffusescale, specularscale;
3166 float lightcolor[3];
3167 VectorCopy(rsurface.rtlight->currentcolor, lightcolor);
3168 ambientscale = rsurface.rtlight->ambientscale;
3169 diffusescale = rsurface.rtlight->diffusescale;
3170 specularscale = rsurface.rtlight->specularscale * rsurface.texture->specularscale;
3171 if (!r_shadow_usenormalmap.integer)
3173 ambientscale += 1.0f * diffusescale;
3177 if ((ambientscale + diffusescale) * VectorLength2(lightcolor) + specularscale * VectorLength2(lightcolor) < (1.0f / 1048576.0f))
3179 negated = (lightcolor[0] + lightcolor[1] + lightcolor[2] < 0) && vid.support.ext_blend_subtract;
3182 VectorNegate(lightcolor, lightcolor);
3183 switch(vid.renderpath)
3185 case RENDERPATH_GL11:
3186 case RENDERPATH_GL13:
3187 case RENDERPATH_GL20:
3188 case RENDERPATH_GLES2:
3189 qglBlendEquationEXT(GL_FUNC_REVERSE_SUBTRACT_EXT);
3191 case RENDERPATH_D3D9:
3193 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_BLENDOP, D3DBLENDOP_SUBTRACT);
3196 case RENDERPATH_D3D10:
3197 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3199 case RENDERPATH_D3D11:
3200 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3202 case RENDERPATH_SOFT:
3203 DPSOFTRAST_BlendSubtract(true);
3207 RSurf_SetupDepthAndCulling();
3208 switch (r_shadow_rendermode)
3210 case R_SHADOW_RENDERMODE_VISIBLELIGHTING:
3211 GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST) && !r_showdisabledepthtest.integer);
3212 R_Shadow_RenderLighting_VisibleLighting(texturenumsurfaces, texturesurfacelist);
3214 case R_SHADOW_RENDERMODE_LIGHT_GLSL:
3215 R_Shadow_RenderLighting_Light_GLSL(texturenumsurfaces, texturesurfacelist, lightcolor, ambientscale, diffusescale, specularscale);
3217 case R_SHADOW_RENDERMODE_LIGHT_VERTEX3DATTEN:
3218 case R_SHADOW_RENDERMODE_LIGHT_VERTEX2D1DATTEN:
3219 case R_SHADOW_RENDERMODE_LIGHT_VERTEX2DATTEN:
3220 case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
3221 R_Shadow_RenderLighting_Light_Vertex(texturenumsurfaces, texturesurfacelist, lightcolor, ambientscale, diffusescale);
3224 Con_Printf("R_Shadow_RenderLighting: unknown r_shadow_rendermode %i\n", r_shadow_rendermode);
3229 switch(vid.renderpath)
3231 case RENDERPATH_GL11:
3232 case RENDERPATH_GL13:
3233 case RENDERPATH_GL20:
3234 case RENDERPATH_GLES2:
3235 qglBlendEquationEXT(GL_FUNC_ADD_EXT);
3237 case RENDERPATH_D3D9:
3239 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_BLENDOP, D3DBLENDOP_ADD);
3242 case RENDERPATH_D3D10:
3243 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3245 case RENDERPATH_D3D11:
3246 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3248 case RENDERPATH_SOFT:
3249 DPSOFTRAST_BlendSubtract(false);
3255 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)
3257 matrix4x4_t tempmatrix = *matrix;
3258 Matrix4x4_Scale(&tempmatrix, r_shadow_lightradiusscale.value, 1);
3260 // if this light has been compiled before, free the associated data
3261 R_RTLight_Uncompile(rtlight);
3263 // clear it completely to avoid any lingering data
3264 memset(rtlight, 0, sizeof(*rtlight));
3266 // copy the properties
3267 rtlight->matrix_lighttoworld = tempmatrix;
3268 Matrix4x4_Invert_Simple(&rtlight->matrix_worldtolight, &tempmatrix);
3269 Matrix4x4_OriginFromMatrix(&tempmatrix, rtlight->shadoworigin);
3270 rtlight->radius = Matrix4x4_ScaleFromMatrix(&tempmatrix);
3271 VectorCopy(color, rtlight->color);
3272 rtlight->cubemapname[0] = 0;
3273 if (cubemapname && cubemapname[0])
3274 strlcpy(rtlight->cubemapname, cubemapname, sizeof(rtlight->cubemapname));
3275 rtlight->shadow = shadow;
3276 rtlight->corona = corona;
3277 rtlight->style = style;
3278 rtlight->isstatic = isstatic;
3279 rtlight->coronasizescale = coronasizescale;
3280 rtlight->ambientscale = ambientscale;
3281 rtlight->diffusescale = diffusescale;
3282 rtlight->specularscale = specularscale;
3283 rtlight->flags = flags;
3285 // compute derived data
3286 //rtlight->cullradius = rtlight->radius;
3287 //rtlight->cullradius2 = rtlight->radius * rtlight->radius;
3288 rtlight->cullmins[0] = rtlight->shadoworigin[0] - rtlight->radius;
3289 rtlight->cullmins[1] = rtlight->shadoworigin[1] - rtlight->radius;
3290 rtlight->cullmins[2] = rtlight->shadoworigin[2] - rtlight->radius;
3291 rtlight->cullmaxs[0] = rtlight->shadoworigin[0] + rtlight->radius;
3292 rtlight->cullmaxs[1] = rtlight->shadoworigin[1] + rtlight->radius;
3293 rtlight->cullmaxs[2] = rtlight->shadoworigin[2] + rtlight->radius;
3296 // compiles rtlight geometry
3297 // (undone by R_FreeCompiledRTLight, which R_UpdateLight calls)
3298 void R_RTLight_Compile(rtlight_t *rtlight)
3301 int numsurfaces, numleafs, numleafpvsbytes, numshadowtrispvsbytes, numlighttrispvsbytes;
3302 int lighttris, shadowtris, shadowzpasstris, shadowzfailtris;
3303 entity_render_t *ent = r_refdef.scene.worldentity;
3304 dp_model_t *model = r_refdef.scene.worldmodel;
3305 unsigned char *data;
3308 // compile the light
3309 rtlight->compiled = true;
3310 rtlight->shadowmode = rtlight->shadow ? (int)r_shadow_shadowmode : -1;
3311 rtlight->static_numleafs = 0;
3312 rtlight->static_numleafpvsbytes = 0;
3313 rtlight->static_leaflist = NULL;
3314 rtlight->static_leafpvs = NULL;
3315 rtlight->static_numsurfaces = 0;
3316 rtlight->static_surfacelist = NULL;
3317 rtlight->static_shadowmap_receivers = 0x3F;
3318 rtlight->static_shadowmap_casters = 0x3F;
3319 rtlight->cullmins[0] = rtlight->shadoworigin[0] - rtlight->radius;
3320 rtlight->cullmins[1] = rtlight->shadoworigin[1] - rtlight->radius;
3321 rtlight->cullmins[2] = rtlight->shadoworigin[2] - rtlight->radius;
3322 rtlight->cullmaxs[0] = rtlight->shadoworigin[0] + rtlight->radius;
3323 rtlight->cullmaxs[1] = rtlight->shadoworigin[1] + rtlight->radius;
3324 rtlight->cullmaxs[2] = rtlight->shadoworigin[2] + rtlight->radius;
3326 if (model && model->GetLightInfo)
3328 // this variable must be set for the CompileShadowVolume/CompileShadowMap code
3329 r_shadow_compilingrtlight = rtlight;
3330 R_FrameData_SetMark();
3331 model->GetLightInfo(ent, rtlight->shadoworigin, rtlight->radius, rtlight->cullmins, rtlight->cullmaxs, r_shadow_buffer_leaflist, r_shadow_buffer_leafpvs, &numleafs, r_shadow_buffer_surfacelist, r_shadow_buffer_surfacepvs, &numsurfaces, r_shadow_buffer_shadowtrispvs, r_shadow_buffer_lighttrispvs, r_shadow_buffer_visitingleafpvs, 0, NULL);
3332 R_FrameData_ReturnToMark();
3333 numleafpvsbytes = (model->brush.num_leafs + 7) >> 3;
3334 numshadowtrispvsbytes = ((model->brush.shadowmesh ? model->brush.shadowmesh->numtriangles : model->surfmesh.num_triangles) + 7) >> 3;
3335 numlighttrispvsbytes = (model->surfmesh.num_triangles + 7) >> 3;
3336 data = (unsigned char *)Mem_Alloc(r_main_mempool, sizeof(int) * numsurfaces + sizeof(int) * numleafs + numleafpvsbytes + numshadowtrispvsbytes + numlighttrispvsbytes);
3337 rtlight->static_numsurfaces = numsurfaces;
3338 rtlight->static_surfacelist = (int *)data;data += sizeof(int) * numsurfaces;
3339 rtlight->static_numleafs = numleafs;
3340 rtlight->static_leaflist = (int *)data;data += sizeof(int) * numleafs;
3341 rtlight->static_numleafpvsbytes = numleafpvsbytes;
3342 rtlight->static_leafpvs = (unsigned char *)data;data += numleafpvsbytes;
3343 rtlight->static_numshadowtrispvsbytes = numshadowtrispvsbytes;
3344 rtlight->static_shadowtrispvs = (unsigned char *)data;data += numshadowtrispvsbytes;
3345 rtlight->static_numlighttrispvsbytes = numlighttrispvsbytes;
3346 rtlight->static_lighttrispvs = (unsigned char *)data;data += numlighttrispvsbytes;
3347 if (rtlight->static_numsurfaces)
3348 memcpy(rtlight->static_surfacelist, r_shadow_buffer_surfacelist, rtlight->static_numsurfaces * sizeof(*rtlight->static_surfacelist));
3349 if (rtlight->static_numleafs)
3350 memcpy(rtlight->static_leaflist, r_shadow_buffer_leaflist, rtlight->static_numleafs * sizeof(*rtlight->static_leaflist));
3351 if (rtlight->static_numleafpvsbytes)
3352 memcpy(rtlight->static_leafpvs, r_shadow_buffer_leafpvs, rtlight->static_numleafpvsbytes);
3353 if (rtlight->static_numshadowtrispvsbytes)
3354 memcpy(rtlight->static_shadowtrispvs, r_shadow_buffer_shadowtrispvs, rtlight->static_numshadowtrispvsbytes);
3355 if (rtlight->static_numlighttrispvsbytes)
3356 memcpy(rtlight->static_lighttrispvs, r_shadow_buffer_lighttrispvs, rtlight->static_numlighttrispvsbytes);
3357 R_FrameData_SetMark();
3358 switch (rtlight->shadowmode)
3360 case R_SHADOW_SHADOWMODE_SHADOWMAP2D:
3361 if (model->CompileShadowMap && rtlight->shadow)
3362 model->CompileShadowMap(ent, rtlight->shadoworigin, NULL, rtlight->radius, numsurfaces, r_shadow_buffer_surfacelist);
3365 if (model->CompileShadowVolume && rtlight->shadow)
3366 model->CompileShadowVolume(ent, rtlight->shadoworigin, NULL, rtlight->radius, numsurfaces, r_shadow_buffer_surfacelist);
3369 R_FrameData_ReturnToMark();
3370 // now we're done compiling the rtlight
3371 r_shadow_compilingrtlight = NULL;
3375 // use smallest available cullradius - box radius or light radius
3376 //rtlight->cullradius = RadiusFromBoundsAndOrigin(rtlight->cullmins, rtlight->cullmaxs, rtlight->shadoworigin);
3377 //rtlight->cullradius = min(rtlight->cullradius, rtlight->radius);
3379 shadowzpasstris = 0;
3380 if (rtlight->static_meshchain_shadow_zpass)
3381 for (mesh = rtlight->static_meshchain_shadow_zpass;mesh;mesh = mesh->next)
3382 shadowzpasstris += mesh->numtriangles;
3384 shadowzfailtris = 0;
3385 if (rtlight->static_meshchain_shadow_zfail)
3386 for (mesh = rtlight->static_meshchain_shadow_zfail;mesh;mesh = mesh->next)
3387 shadowzfailtris += mesh->numtriangles;
3390 if (rtlight->static_numlighttrispvsbytes)
3391 for (i = 0;i < rtlight->static_numlighttrispvsbytes*8;i++)
3392 if (CHECKPVSBIT(rtlight->static_lighttrispvs, i))
3396 if (rtlight->static_numlighttrispvsbytes)
3397 for (i = 0;i < rtlight->static_numshadowtrispvsbytes*8;i++)
3398 if (CHECKPVSBIT(rtlight->static_shadowtrispvs, i))
3401 if (developer_extra.integer)
3402 Con_DPrintf("static light built: %f %f %f : %f %f %f box, %i light triangles, %i shadow triangles, %i zpass/%i zfail compiled shadow volume triangles\n", rtlight->cullmins[0], rtlight->cullmins[1], rtlight->cullmins[2], rtlight->cullmaxs[0], rtlight->cullmaxs[1], rtlight->cullmaxs[2], lighttris, shadowtris, shadowzpasstris, shadowzfailtris);
3405 void R_RTLight_Uncompile(rtlight_t *rtlight)
3407 if (rtlight->compiled)
3409 if (rtlight->static_meshchain_shadow_zpass)
3410 Mod_ShadowMesh_Free(rtlight->static_meshchain_shadow_zpass);
3411 rtlight->static_meshchain_shadow_zpass = NULL;
3412 if (rtlight->static_meshchain_shadow_zfail)
3413 Mod_ShadowMesh_Free(rtlight->static_meshchain_shadow_zfail);
3414 rtlight->static_meshchain_shadow_zfail = NULL;
3415 if (rtlight->static_meshchain_shadow_shadowmap)
3416 Mod_ShadowMesh_Free(rtlight->static_meshchain_shadow_shadowmap);
3417 rtlight->static_meshchain_shadow_shadowmap = NULL;
3418 // these allocations are grouped
3419 if (rtlight->static_surfacelist)
3420 Mem_Free(rtlight->static_surfacelist);
3421 rtlight->static_numleafs = 0;
3422 rtlight->static_numleafpvsbytes = 0;
3423 rtlight->static_leaflist = NULL;
3424 rtlight->static_leafpvs = NULL;
3425 rtlight->static_numsurfaces = 0;
3426 rtlight->static_surfacelist = NULL;
3427 rtlight->static_numshadowtrispvsbytes = 0;
3428 rtlight->static_shadowtrispvs = NULL;
3429 rtlight->static_numlighttrispvsbytes = 0;
3430 rtlight->static_lighttrispvs = NULL;
3431 rtlight->compiled = false;
3435 void R_Shadow_UncompileWorldLights(void)
3439 size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
3440 for (lightindex = 0;lightindex < range;lightindex++)
3442 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
3445 R_RTLight_Uncompile(&light->rtlight);
3449 void R_Shadow_ComputeShadowCasterCullingPlanes(rtlight_t *rtlight)
3453 // reset the count of frustum planes
3454 // see rtlight->cached_frustumplanes definition for how much this array
3456 rtlight->cached_numfrustumplanes = 0;
3458 // haven't implemented a culling path for ortho rendering
3459 if (!r_refdef.view.useperspective)
3461 // check if the light is on screen and copy the 4 planes if it is
3462 for (i = 0;i < 4;i++)
3463 if (PlaneDiff(rtlight->shadoworigin, &r_refdef.view.frustum[i]) < -0.03125)
3466 for (i = 0;i < 4;i++)
3467 rtlight->cached_frustumplanes[rtlight->cached_numfrustumplanes++] = r_refdef.view.frustum[i];
3472 // generate a deformed frustum that includes the light origin, this is
3473 // used to cull shadow casting surfaces that can not possibly cast a
3474 // shadow onto the visible light-receiving surfaces, which can be a
3477 // if the light origin is onscreen the result will be 4 planes exactly
3478 // if the light origin is offscreen on only one axis the result will
3479 // be exactly 5 planes (split-side case)
3480 // if the light origin is offscreen on two axes the result will be
3481 // exactly 4 planes (stretched corner case)
3482 for (i = 0;i < 4;i++)
3484 // quickly reject standard frustum planes that put the light
3485 // origin outside the frustum
3486 if (PlaneDiff(rtlight->shadoworigin, &r_refdef.view.frustum[i]) < -0.03125)
3489 rtlight->cached_frustumplanes[rtlight->cached_numfrustumplanes++] = r_refdef.view.frustum[i];
3491 // if all the standard frustum planes were accepted, the light is onscreen
3492 // otherwise we need to generate some more planes below...
3493 if (rtlight->cached_numfrustumplanes < 4)
3495 // at least one of the stock frustum planes failed, so we need to
3496 // create one or two custom planes to enclose the light origin
3497 for (i = 0;i < 4;i++)
3499 // create a plane using the view origin and light origin, and a
3500 // single point from the frustum corner set
3501 TriangleNormal(r_refdef.view.origin, r_refdef.view.frustumcorner[i], rtlight->shadoworigin, plane.normal);
3502 VectorNormalize(plane.normal);
3503 plane.dist = DotProduct(r_refdef.view.origin, plane.normal);
3504 // see if this plane is backwards and flip it if so
3505 for (j = 0;j < 4;j++)
3506 if (j != i && DotProduct(r_refdef.view.frustumcorner[j], plane.normal) - plane.dist < -0.03125)
3510 VectorNegate(plane.normal, plane.normal);
3512 // flipped plane, test again to see if it is now valid
3513 for (j = 0;j < 4;j++)
3514 if (j != i && DotProduct(r_refdef.view.frustumcorner[j], plane.normal) - plane.dist < -0.03125)
3516 // if the plane is still not valid, then it is dividing the
3517 // frustum and has to be rejected
3521 // we have created a valid plane, compute extra info
3522 PlaneClassify(&plane);
3524 rtlight->cached_frustumplanes[rtlight->cached_numfrustumplanes++] = plane;
3526 // if we've found 5 frustum planes then we have constructed a
3527 // proper split-side case and do not need to keep searching for
3528 // planes to enclose the light origin
3529 if (rtlight->cached_numfrustumplanes == 5)
3537 for (i = 0;i < rtlight->cached_numfrustumplanes;i++)
3539 plane = rtlight->cached_frustumplanes[i];
3540 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));
3545 // now add the light-space box planes if the light box is rotated, as any
3546 // caster outside the oriented light box is irrelevant (even if it passed
3547 // the worldspace light box, which is axial)
3548 if (rtlight->matrix_lighttoworld.m[0][0] != 1 || rtlight->matrix_lighttoworld.m[1][1] != 1 || rtlight->matrix_lighttoworld.m[2][2] != 1)
3550 for (i = 0;i < 6;i++)
3554 v[i >> 1] = (i & 1) ? -1 : 1;
3555 Matrix4x4_Transform(&rtlight->matrix_lighttoworld, v, plane.normal);
3556 VectorSubtract(plane.normal, rtlight->shadoworigin, plane.normal);
3557 plane.dist = VectorNormalizeLength(plane.normal);
3558 plane.dist += DotProduct(plane.normal, rtlight->shadoworigin);
3559 rtlight->cached_frustumplanes[rtlight->cached_numfrustumplanes++] = plane;
3565 // add the world-space reduced box planes
3566 for (i = 0;i < 6;i++)
3568 VectorClear(plane.normal);
3569 plane.normal[i >> 1] = (i & 1) ? -1 : 1;
3570 plane.dist = (i & 1) ? -rtlight->cached_cullmaxs[i >> 1] : rtlight->cached_cullmins[i >> 1];
3571 rtlight->cached_frustumplanes[rtlight->cached_numfrustumplanes++] = plane;
3580 // reduce all plane distances to tightly fit the rtlight cull box, which
3582 VectorSet(points[0], rtlight->cached_cullmins[0], rtlight->cached_cullmins[1], rtlight->cached_cullmins[2]);
3583 VectorSet(points[1], rtlight->cached_cullmaxs[0], rtlight->cached_cullmins[1], rtlight->cached_cullmins[2]);
3584 VectorSet(points[2], rtlight->cached_cullmins[0], rtlight->cached_cullmaxs[1], rtlight->cached_cullmins[2]);
3585 VectorSet(points[3], rtlight->cached_cullmaxs[0], rtlight->cached_cullmaxs[1], rtlight->cached_cullmins[2]);
3586 VectorSet(points[4], rtlight->cached_cullmins[0], rtlight->cached_cullmins[1], rtlight->cached_cullmaxs[2]);
3587 VectorSet(points[5], rtlight->cached_cullmaxs[0], rtlight->cached_cullmins[1], rtlight->cached_cullmaxs[2]);
3588 VectorSet(points[6], rtlight->cached_cullmins[0], rtlight->cached_cullmaxs[1], rtlight->cached_cullmaxs[2]);
3589 VectorSet(points[7], rtlight->cached_cullmaxs[0], rtlight->cached_cullmaxs[1], rtlight->cached_cullmaxs[2]);
3590 oldnum = rtlight->cached_numfrustumplanes;
3591 rtlight->cached_numfrustumplanes = 0;
3592 for (j = 0;j < oldnum;j++)
3594 // find the nearest point on the box to this plane
3595 bestdist = DotProduct(rtlight->cached_frustumplanes[j].normal, points[0]);
3596 for (i = 1;i < 8;i++)
3598 dist = DotProduct(rtlight->cached_frustumplanes[j].normal, points[i]);
3599 if (bestdist > dist)
3602 Con_Printf("light %p %splane #%i %f %f %f : %f < %f\n", rtlight, rtlight->cached_frustumplanes[j].dist < bestdist + 0.03125 ? "^2" : "^1", j, rtlight->cached_frustumplanes[j].normal[0], rtlight->cached_frustumplanes[j].normal[1], rtlight->cached_frustumplanes[j].normal[2], rtlight->cached_frustumplanes[j].dist, bestdist);
3603 // if the nearest point is near or behind the plane, we want this
3604 // plane, otherwise the plane is useless as it won't cull anything
3605 if (rtlight->cached_frustumplanes[j].dist < bestdist + 0.03125)
3607 PlaneClassify(&rtlight->cached_frustumplanes[j]);
3608 rtlight->cached_frustumplanes[rtlight->cached_numfrustumplanes++] = rtlight->cached_frustumplanes[j];
3615 void R_Shadow_DrawWorldShadow_ShadowMap(int numsurfaces, int *surfacelist, const unsigned char *trispvs, const unsigned char *surfacesides)
3619 RSurf_ActiveWorldEntity();
3621 if (rsurface.rtlight->compiled && r_shadow_realtime_world_compile.integer && r_shadow_realtime_world_compileshadow.integer)
3624 GL_CullFace(GL_NONE);
3625 mesh = rsurface.rtlight->static_meshchain_shadow_shadowmap;
3626 for (;mesh;mesh = mesh->next)
3628 if (!mesh->sidetotals[r_shadow_shadowmapside])
3630 r_refdef.stats.lights_shadowtriangles += mesh->sidetotals[r_shadow_shadowmapside];
3631 if (mesh->vertex3fbuffer)
3632 R_Mesh_PrepareVertices_Vertex3f(mesh->numverts, mesh->vertex3f, mesh->vertex3fbuffer);
3634 R_Mesh_PrepareVertices_Vertex3f(mesh->numverts, mesh->vertex3f, mesh->vbo_vertexbuffer);
3635 R_Mesh_Draw(0, mesh->numverts, mesh->sideoffsets[r_shadow_shadowmapside], mesh->sidetotals[r_shadow_shadowmapside], mesh->element3i, mesh->element3i_indexbuffer, mesh->element3i_bufferoffset, mesh->element3s, mesh->element3s_indexbuffer, mesh->element3s_bufferoffset);
3639 else if (r_refdef.scene.worldentity->model)
3640 r_refdef.scene.worldmodel->DrawShadowMap(r_shadow_shadowmapside, r_refdef.scene.worldentity, rsurface.rtlight->shadoworigin, NULL, rsurface.rtlight->radius, numsurfaces, surfacelist, surfacesides, rsurface.rtlight->cached_cullmins, rsurface.rtlight->cached_cullmaxs);
3642 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
3645 void R_Shadow_DrawWorldShadow_ShadowVolume(int numsurfaces, int *surfacelist, const unsigned char *trispvs)
3647 qboolean zpass = false;
3650 int surfacelistindex;
3651 msurface_t *surface;
3653 // if triangle neighbors are disabled, shadowvolumes are disabled
3654 if (r_refdef.scene.worldmodel->brush.shadowmesh ? !r_refdef.scene.worldmodel->brush.shadowmesh->neighbor3i : !r_refdef.scene.worldmodel->surfmesh.data_neighbor3i)
3657 RSurf_ActiveWorldEntity();
3659 if (rsurface.rtlight->compiled && r_shadow_realtime_world_compile.integer && r_shadow_realtime_world_compileshadow.integer)
3662 if (r_shadow_rendermode != R_SHADOW_RENDERMODE_VISIBLEVOLUMES)
3664 zpass = R_Shadow_UseZPass(r_refdef.scene.worldmodel->normalmins, r_refdef.scene.worldmodel->normalmaxs);
3665 R_Shadow_RenderMode_StencilShadowVolumes(zpass);
3667 mesh = zpass ? rsurface.rtlight->static_meshchain_shadow_zpass : rsurface.rtlight->static_meshchain_shadow_zfail;
3668 for (;mesh;mesh = mesh->next)
3670 r_refdef.stats.lights_shadowtriangles += mesh->numtriangles;
3671 if (mesh->vertex3fbuffer)
3672 R_Mesh_PrepareVertices_Vertex3f(mesh->numverts, mesh->vertex3f, mesh->vertex3fbuffer);
3674 R_Mesh_PrepareVertices_Vertex3f(mesh->numverts, mesh->vertex3f, mesh->vbo_vertexbuffer);
3675 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZPASS_STENCIL)
3677 // increment stencil if frontface is infront of depthbuffer
3678 GL_CullFace(r_refdef.view.cullface_back);
3679 R_SetStencil(true, 255, GL_KEEP, GL_KEEP, GL_INCR, GL_ALWAYS, 128, 255);
3680 R_Mesh_Draw(0, mesh->numverts, 0, mesh->numtriangles, mesh->element3i, mesh->element3i_indexbuffer, mesh->element3i_bufferoffset, mesh->element3s, mesh->element3s_indexbuffer, mesh->element3s_bufferoffset);
3681 // decrement stencil if backface is infront of depthbuffer
3682 GL_CullFace(r_refdef.view.cullface_front);
3683 R_SetStencil(true, 255, GL_KEEP, GL_KEEP, GL_DECR, GL_ALWAYS, 128, 255);
3685 else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZFAIL_STENCIL)
3687 // decrement stencil if backface is behind depthbuffer
3688 GL_CullFace(r_refdef.view.cullface_front);
3689 R_SetStencil(true, 255, GL_KEEP, GL_DECR, GL_KEEP, GL_ALWAYS, 128, 255);
3690 R_Mesh_Draw(0, mesh->numverts, 0, mesh->numtriangles, mesh->element3i, mesh->element3i_indexbuffer, mesh->element3i_bufferoffset, mesh->element3s, mesh->element3s_indexbuffer, mesh->element3s_bufferoffset);
3691 // increment stencil if frontface is behind depthbuffer
3692 GL_CullFace(r_refdef.view.cullface_back);
3693 R_SetStencil(true, 255, GL_KEEP, GL_INCR, GL_KEEP, GL_ALWAYS, 128, 255);
3695 R_Mesh_Draw(0, mesh->numverts, 0, mesh->numtriangles, mesh->element3i, mesh->element3i_indexbuffer, mesh->element3i_bufferoffset, mesh->element3s, mesh->element3s_indexbuffer, mesh->element3s_bufferoffset);
3699 else if (numsurfaces && r_refdef.scene.worldmodel->brush.shadowmesh)
3701 // use the shadow trispvs calculated earlier by GetLightInfo to cull world triangles on this dynamic light
3702 R_Shadow_PrepareShadowMark(r_refdef.scene.worldmodel->brush.shadowmesh->numtriangles);
3703 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
3705 surface = r_refdef.scene.worldmodel->data_surfaces + surfacelist[surfacelistindex];
3706 for (t = surface->num_firstshadowmeshtriangle, tend = t + surface->num_triangles;t < tend;t++)
3707 if (CHECKPVSBIT(trispvs, t))
3708 shadowmarklist[numshadowmark++] = t;
3710 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);
3712 else if (numsurfaces)
3714 r_refdef.scene.worldmodel->DrawShadowVolume(r_refdef.scene.worldentity, rsurface.rtlight->shadoworigin, NULL, rsurface.rtlight->radius, numsurfaces, surfacelist, rsurface.rtlight->cached_cullmins, rsurface.rtlight->cached_cullmaxs);
3717 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
3720 void R_Shadow_DrawEntityShadow(entity_render_t *ent)
3722 vec3_t relativeshadoworigin, relativeshadowmins, relativeshadowmaxs;
3723 vec_t relativeshadowradius;
3724 RSurf_ActiveModelEntity(ent, false, false, false);
3725 Matrix4x4_Transform(&ent->inversematrix, rsurface.rtlight->shadoworigin, relativeshadoworigin);
3726 // we need to re-init the shader for each entity because the matrix changed
3727 relativeshadowradius = rsurface.rtlight->radius / ent->scale;
3728 relativeshadowmins[0] = relativeshadoworigin[0] - relativeshadowradius;
3729 relativeshadowmins[1] = relativeshadoworigin[1] - relativeshadowradius;
3730 relativeshadowmins[2] = relativeshadoworigin[2] - relativeshadowradius;
3731 relativeshadowmaxs[0] = relativeshadoworigin[0] + relativeshadowradius;
3732 relativeshadowmaxs[1] = relativeshadoworigin[1] + relativeshadowradius;
3733 relativeshadowmaxs[2] = relativeshadoworigin[2] + relativeshadowradius;
3734 switch (r_shadow_rendermode)
3736 case R_SHADOW_RENDERMODE_SHADOWMAP2D:
3737 ent->model->DrawShadowMap(r_shadow_shadowmapside, ent, relativeshadoworigin, NULL, relativeshadowradius, ent->model->nummodelsurfaces, ent->model->sortedmodelsurfaces, NULL, relativeshadowmins, relativeshadowmaxs);
3740 ent->model->DrawShadowVolume(ent, relativeshadoworigin, NULL, relativeshadowradius, ent->model->nummodelsurfaces, ent->model->sortedmodelsurfaces, relativeshadowmins, relativeshadowmaxs);
3743 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
3746 void R_Shadow_SetupEntityLight(const entity_render_t *ent)
3748 // set up properties for rendering light onto this entity
3749 RSurf_ActiveModelEntity(ent, true, true, false);
3750 Matrix4x4_Concat(&rsurface.entitytolight, &rsurface.rtlight->matrix_worldtolight, &ent->matrix);
3751 Matrix4x4_Concat(&rsurface.entitytoattenuationxyz, &matrix_attenuationxyz, &rsurface.entitytolight);
3752 Matrix4x4_Concat(&rsurface.entitytoattenuationz, &matrix_attenuationz, &rsurface.entitytolight);
3753 Matrix4x4_Transform(&ent->inversematrix, rsurface.rtlight->shadoworigin, rsurface.entitylightorigin);
3756 void R_Shadow_DrawWorldLight(int numsurfaces, int *surfacelist, const unsigned char *lighttrispvs)
3758 if (!r_refdef.scene.worldmodel->DrawLight)
3761 // set up properties for rendering light onto this entity
3762 RSurf_ActiveWorldEntity();
3763 rsurface.entitytolight = rsurface.rtlight->matrix_worldtolight;
3764 Matrix4x4_Concat(&rsurface.entitytoattenuationxyz, &matrix_attenuationxyz, &rsurface.entitytolight);
3765 Matrix4x4_Concat(&rsurface.entitytoattenuationz, &matrix_attenuationz, &rsurface.entitytolight);
3766 VectorCopy(rsurface.rtlight->shadoworigin, rsurface.entitylightorigin);
3768 r_refdef.scene.worldmodel->DrawLight(r_refdef.scene.worldentity, numsurfaces, surfacelist, lighttrispvs);
3770 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
3773 void R_Shadow_DrawEntityLight(entity_render_t *ent)
3775 dp_model_t *model = ent->model;
3776 if (!model->DrawLight)
3779 R_Shadow_SetupEntityLight(ent);
3781 model->DrawLight(ent, model->nummodelsurfaces, model->sortedmodelsurfaces, NULL);
3783 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
3786 void R_Shadow_PrepareLight(rtlight_t *rtlight)
3790 int numleafs, numsurfaces;
3791 int *leaflist, *surfacelist;
3792 unsigned char *leafpvs;
3793 unsigned char *shadowtrispvs;
3794 unsigned char *lighttrispvs;
3795 //unsigned char *surfacesides;
3796 int numlightentities;
3797 int numlightentities_noselfshadow;
3798 int numshadowentities;
3799 int numshadowentities_noselfshadow;
3800 static entity_render_t *lightentities[MAX_EDICTS];
3801 static entity_render_t *lightentities_noselfshadow[MAX_EDICTS];
3802 static entity_render_t *shadowentities[MAX_EDICTS];
3803 static entity_render_t *shadowentities_noselfshadow[MAX_EDICTS];
3806 rtlight->draw = false;
3808 // skip lights that don't light because of ambientscale+diffusescale+specularscale being 0 (corona only lights)
3809 // skip lights that are basically invisible (color 0 0 0)
3810 nolight = VectorLength2(rtlight->color) * (rtlight->ambientscale + rtlight->diffusescale + rtlight->specularscale) < (1.0f / 1048576.0f);
3812 // loading is done before visibility checks because loading should happen
3813 // all at once at the start of a level, not when it stalls gameplay.
3814 // (especially important to benchmarks)
3816 if (rtlight->isstatic && !nolight && (!rtlight->compiled || (rtlight->shadow && rtlight->shadowmode != (int)r_shadow_shadowmode)) && r_shadow_realtime_world_compile.integer)
3818 if (rtlight->compiled)
3819 R_RTLight_Uncompile(rtlight);
3820 R_RTLight_Compile(rtlight);
3824 rtlight->currentcubemap = rtlight->cubemapname[0] ? R_GetCubemap(rtlight->cubemapname) : r_texture_whitecube;
3826 // look up the light style value at this time
3827 f = (rtlight->style >= 0 ? r_refdef.scene.rtlightstylevalue[rtlight->style] : 1) * r_shadow_lightintensityscale.value;
3828 VectorScale(rtlight->color, f, rtlight->currentcolor);
3830 if (rtlight->selected)
3832 f = 2 + sin(realtime * M_PI * 4.0);
3833 VectorScale(rtlight->currentcolor, f, rtlight->currentcolor);
3837 // if lightstyle is currently off, don't draw the light
3838 if (VectorLength2(rtlight->currentcolor) < (1.0f / 1048576.0f))
3841 // skip processing on corona-only lights
3845 // if the light box is offscreen, skip it
3846 if (R_CullBox(rtlight->cullmins, rtlight->cullmaxs))
3849 VectorCopy(rtlight->cullmins, rtlight->cached_cullmins);
3850 VectorCopy(rtlight->cullmaxs, rtlight->cached_cullmaxs);
3852 R_Shadow_ComputeShadowCasterCullingPlanes(rtlight);
3854 if (rtlight->compiled && r_shadow_realtime_world_compile.integer)
3856 // compiled light, world available and can receive realtime lighting
3857 // retrieve leaf information
3858 numleafs = rtlight->static_numleafs;
3859 leaflist = rtlight->static_leaflist;
3860 leafpvs = rtlight->static_leafpvs;
3861 numsurfaces = rtlight->static_numsurfaces;
3862 surfacelist = rtlight->static_surfacelist;
3863 //surfacesides = NULL;
3864 shadowtrispvs = rtlight->static_shadowtrispvs;
3865 lighttrispvs = rtlight->static_lighttrispvs;
3867 else if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->GetLightInfo)
3869 // dynamic light, world available and can receive realtime lighting
3870 // calculate lit surfaces and leafs
3871 r_refdef.scene.worldmodel->GetLightInfo(r_refdef.scene.worldentity, rtlight->shadoworigin, rtlight->radius, rtlight->cached_cullmins, rtlight->cached_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, rtlight->cached_numfrustumplanes, rtlight->cached_frustumplanes);
3872 R_Shadow_ComputeShadowCasterCullingPlanes(rtlight);
3873 leaflist = r_shadow_buffer_leaflist;
3874 leafpvs = r_shadow_buffer_leafpvs;
3875 surfacelist = r_shadow_buffer_surfacelist;
3876 //surfacesides = r_shadow_buffer_surfacesides;
3877 shadowtrispvs = r_shadow_buffer_shadowtrispvs;
3878 lighttrispvs = r_shadow_buffer_lighttrispvs;
3879 // if the reduced leaf bounds are offscreen, skip it
3880 if (R_CullBox(rtlight->cached_cullmins, rtlight->cached_cullmaxs))
3891 //surfacesides = NULL;
3892 shadowtrispvs = NULL;
3893 lighttrispvs = NULL;
3895 // check if light is illuminating any visible leafs
3898 for (i = 0;i < numleafs;i++)
3899 if (r_refdef.viewcache.world_leafvisible[leaflist[i]])
3905 // make a list of lit entities and shadow casting entities
3906 numlightentities = 0;
3907 numlightentities_noselfshadow = 0;
3908 numshadowentities = 0;
3909 numshadowentities_noselfshadow = 0;
3911 // add dynamic entities that are lit by the light
3912 for (i = 0;i < r_refdef.scene.numentities;i++)
3915 entity_render_t *ent = r_refdef.scene.entities[i];
3917 if (!BoxesOverlap(ent->mins, ent->maxs, rtlight->cached_cullmins, rtlight->cached_cullmaxs))
3919 // skip the object entirely if it is not within the valid
3920 // shadow-casting region (which includes the lit region)
3921 if (R_CullBoxCustomPlanes(ent->mins, ent->maxs, rtlight->cached_numfrustumplanes, rtlight->cached_frustumplanes))
3923 if (!(model = ent->model))
3925 if (r_refdef.viewcache.entityvisible[i] && model->DrawLight && (ent->flags & RENDER_LIGHT))
3927 // this entity wants to receive light, is visible, and is
3928 // inside the light box
3929 // TODO: check if the surfaces in the model can receive light
3930 // so now check if it's in a leaf seen by the light
3931 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))
3933 if (ent->flags & RENDER_NOSELFSHADOW)
3934 lightentities_noselfshadow[numlightentities_noselfshadow++] = ent;
3936 lightentities[numlightentities++] = ent;
3937 // since it is lit, it probably also casts a shadow...
3938 // about the VectorDistance2 - light emitting entities should not cast their own shadow
3939 Matrix4x4_OriginFromMatrix(&ent->matrix, org);
3940 if ((ent->flags & RENDER_SHADOW) && model->DrawShadowVolume && VectorDistance2(org, rtlight->shadoworigin) > 0.1)
3942 // note: exterior models without the RENDER_NOSELFSHADOW
3943 // flag still create a RENDER_NOSELFSHADOW shadow but
3944 // are lit normally, this means that they are
3945 // self-shadowing but do not shadow other
3946 // RENDER_NOSELFSHADOW entities such as the gun
3947 // (very weird, but keeps the player shadow off the gun)
3948 if (ent->flags & (RENDER_NOSELFSHADOW | RENDER_EXTERIORMODEL))
3949 shadowentities_noselfshadow[numshadowentities_noselfshadow++] = ent;
3951 shadowentities[numshadowentities++] = ent;
3954 else if (ent->flags & RENDER_SHADOW)
3956 // this entity is not receiving light, but may still need to
3958 // TODO: check if the surfaces in the model can cast shadow
3959 // now check if it is in a leaf seen by the light
3960 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))
3962 // about the VectorDistance2 - light emitting entities should not cast their own shadow
3963 Matrix4x4_OriginFromMatrix(&ent->matrix, org);
3964 if ((ent->flags & RENDER_SHADOW) && model->DrawShadowVolume && VectorDistance2(org, rtlight->shadoworigin) > 0.1)
3966 if (ent->flags & (RENDER_NOSELFSHADOW | RENDER_EXTERIORMODEL))
3967 shadowentities_noselfshadow[numshadowentities_noselfshadow++] = ent;
3969 shadowentities[numshadowentities++] = ent;
3974 // return if there's nothing at all to light
3975 if (numsurfaces + numlightentities + numlightentities_noselfshadow == 0)
3978 // count this light in the r_speeds
3979 r_refdef.stats.lights++;
3981 // flag it as worth drawing later
3982 rtlight->draw = true;
3984 // cache all the animated entities that cast a shadow but are not visible
3985 for (i = 0;i < numshadowentities;i++)
3986 if (!shadowentities[i]->animcache_vertex3f)
3987 R_AnimCache_GetEntity(shadowentities[i], false, false);
3988 for (i = 0;i < numshadowentities_noselfshadow;i++)
3989 if (!shadowentities_noselfshadow[i]->animcache_vertex3f)
3990 R_AnimCache_GetEntity(shadowentities_noselfshadow[i], false, false);
3992 // allocate some temporary memory for rendering this light later in the frame
3993 // reusable buffers need to be copied, static data can be used as-is
3994 rtlight->cached_numlightentities = numlightentities;
3995 rtlight->cached_numlightentities_noselfshadow = numlightentities_noselfshadow;
3996 rtlight->cached_numshadowentities = numshadowentities;
3997 rtlight->cached_numshadowentities_noselfshadow = numshadowentities_noselfshadow;
3998 rtlight->cached_numsurfaces = numsurfaces;
3999 rtlight->cached_lightentities = (entity_render_t**)R_FrameData_Store(numlightentities*sizeof(entity_render_t*), (void*)lightentities);
4000 rtlight->cached_lightentities_noselfshadow = (entity_render_t**)R_FrameData_Store(numlightentities_noselfshadow*sizeof(entity_render_t*), (void*)lightentities_noselfshadow);
4001 rtlight->cached_shadowentities = (entity_render_t**)R_FrameData_Store(numshadowentities*sizeof(entity_render_t*), (void*)shadowentities);
4002 rtlight->cached_shadowentities_noselfshadow = (entity_render_t**)R_FrameData_Store(numshadowentities_noselfshadow*sizeof(entity_render_t *), (void*)shadowentities_noselfshadow);
4003 if (shadowtrispvs == r_shadow_buffer_shadowtrispvs)
4005 int numshadowtrispvsbytes = (((r_refdef.scene.worldmodel->brush.shadowmesh ? r_refdef.scene.worldmodel->brush.shadowmesh->numtriangles : r_refdef.scene.worldmodel->surfmesh.num_triangles) + 7) >> 3);
4006 int numlighttrispvsbytes = ((r_refdef.scene.worldmodel->surfmesh.num_triangles + 7) >> 3);
4007 rtlight->cached_shadowtrispvs = (unsigned char *)R_FrameData_Store(numshadowtrispvsbytes, shadowtrispvs);
4008 rtlight->cached_lighttrispvs = (unsigned char *)R_FrameData_Store(numlighttrispvsbytes, lighttrispvs);
4009 rtlight->cached_surfacelist = (int*)R_FrameData_Store(numsurfaces*sizeof(int), (void*)surfacelist);
4013 // compiled light data
4014 rtlight->cached_shadowtrispvs = shadowtrispvs;
4015 rtlight->cached_lighttrispvs = lighttrispvs;
4016 rtlight->cached_surfacelist = surfacelist;
4020 void R_Shadow_DrawLight(rtlight_t *rtlight)
4024 unsigned char *shadowtrispvs, *lighttrispvs, *surfacesides;
4025 int numlightentities;
4026 int numlightentities_noselfshadow;
4027 int numshadowentities;
4028 int numshadowentities_noselfshadow;
4029 entity_render_t **lightentities;
4030 entity_render_t **lightentities_noselfshadow;
4031 entity_render_t **shadowentities;
4032 entity_render_t **shadowentities_noselfshadow;
4034 static unsigned char entitysides[MAX_EDICTS];
4035 static unsigned char entitysides_noselfshadow[MAX_EDICTS];
4036 vec3_t nearestpoint;
4038 qboolean castshadows;
4041 // check if we cached this light this frame (meaning it is worth drawing)
4045 numlightentities = rtlight->cached_numlightentities;
4046 numlightentities_noselfshadow = rtlight->cached_numlightentities_noselfshadow;
4047 numshadowentities = rtlight->cached_numshadowentities;
4048 numshadowentities_noselfshadow = rtlight->cached_numshadowentities_noselfshadow;
4049 numsurfaces = rtlight->cached_numsurfaces;
4050 lightentities = rtlight->cached_lightentities;
4051 lightentities_noselfshadow = rtlight->cached_lightentities_noselfshadow;
4052 shadowentities = rtlight->cached_shadowentities;
4053 shadowentities_noselfshadow = rtlight->cached_shadowentities_noselfshadow;
4054 shadowtrispvs = rtlight->cached_shadowtrispvs;
4055 lighttrispvs = rtlight->cached_lighttrispvs;
4056 surfacelist = rtlight->cached_surfacelist;
4058 // set up a scissor rectangle for this light
4059 if (R_Shadow_ScissorForBBox(rtlight->cached_cullmins, rtlight->cached_cullmaxs))
4062 // don't let sound skip if going slow
4063 if (r_refdef.scene.extraupdate)
4066 // make this the active rtlight for rendering purposes
4067 R_Shadow_RenderMode_ActiveLight(rtlight);
4069 if (r_showshadowvolumes.integer && r_refdef.view.showdebug && numsurfaces + numshadowentities + numshadowentities_noselfshadow && rtlight->shadow && (rtlight->isstatic ? r_refdef.scene.rtworldshadows : r_refdef.scene.rtdlightshadows))
4071 // optionally draw visible shape of the shadow volumes
4072 // for performance analysis by level designers
4073 R_Shadow_RenderMode_VisibleShadowVolumes();
4075 R_Shadow_DrawWorldShadow_ShadowVolume(numsurfaces, surfacelist, shadowtrispvs);
4076 for (i = 0;i < numshadowentities;i++)
4077 R_Shadow_DrawEntityShadow(shadowentities[i]);
4078 for (i = 0;i < numshadowentities_noselfshadow;i++)
4079 R_Shadow_DrawEntityShadow(shadowentities_noselfshadow[i]);
4080 R_Shadow_RenderMode_VisibleLighting(false, false);
4083 if (r_showlighting.integer && r_refdef.view.showdebug && numsurfaces + numlightentities + numlightentities_noselfshadow)
4085 // optionally draw the illuminated areas
4086 // for performance analysis by level designers
4087 R_Shadow_RenderMode_VisibleLighting(false, false);
4089 R_Shadow_DrawWorldLight(numsurfaces, surfacelist, lighttrispvs);
4090 for (i = 0;i < numlightentities;i++)
4091 R_Shadow_DrawEntityLight(lightentities[i]);
4092 for (i = 0;i < numlightentities_noselfshadow;i++)
4093 R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]);
4096 castshadows = numsurfaces + numshadowentities + numshadowentities_noselfshadow > 0 && rtlight->shadow && (rtlight->isstatic ? r_refdef.scene.rtworldshadows : r_refdef.scene.rtdlightshadows);
4098 nearestpoint[0] = bound(rtlight->cullmins[0], r_refdef.view.origin[0], rtlight->cullmaxs[0]);
4099 nearestpoint[1] = bound(rtlight->cullmins[1], r_refdef.view.origin[1], rtlight->cullmaxs[1]);
4100 nearestpoint[2] = bound(rtlight->cullmins[2], r_refdef.view.origin[2], rtlight->cullmaxs[2]);
4101 distance = VectorDistance(nearestpoint, r_refdef.view.origin);
4103 lodlinear = (rtlight->radius * r_shadow_shadowmapping_precision.value) / sqrt(max(1.0f, distance/rtlight->radius));
4104 //lodlinear = (int)(r_shadow_shadowmapping_lod_bias.value + r_shadow_shadowmapping_lod_scale.value * rtlight->radius / max(1.0f, distance));
4105 lodlinear = bound(r_shadow_shadowmapping_minsize.integer, lodlinear, r_shadow_shadowmapmaxsize);
4107 if (castshadows && r_shadow_shadowmode == R_SHADOW_SHADOWMODE_SHADOWMAP2D)
4113 int receivermask = 0;
4114 matrix4x4_t radiustolight = rtlight->matrix_worldtolight;
4115 Matrix4x4_Abs(&radiustolight);
4117 r_shadow_shadowmaplod = 0;
4118 for (i = 1;i < R_SHADOW_SHADOWMAP_NUMCUBEMAPS;i++)
4119 if ((r_shadow_shadowmapmaxsize >> i) > lodlinear)
4120 r_shadow_shadowmaplod = i;
4122 size = bound(r_shadow_shadowmapborder, lodlinear, r_shadow_shadowmapmaxsize);
4124 borderbias = r_shadow_shadowmapborder / (float)(size - r_shadow_shadowmapborder);
4126 surfacesides = NULL;
4129 if (rtlight->compiled && r_shadow_realtime_world_compile.integer && r_shadow_realtime_world_compileshadow.integer)
4131 castermask = rtlight->static_shadowmap_casters;
4132 receivermask = rtlight->static_shadowmap_receivers;
4136 surfacesides = r_shadow_buffer_surfacesides;
4137 for(i = 0;i < numsurfaces;i++)
4139 msurface_t *surface = r_refdef.scene.worldmodel->data_surfaces + surfacelist[i];
4140 surfacesides[i] = R_Shadow_CalcBBoxSideMask(surface->mins, surface->maxs, &rtlight->matrix_worldtolight, &radiustolight, borderbias);
4141 castermask |= surfacesides[i];
4142 receivermask |= surfacesides[i];
4146 if (receivermask < 0x3F)
4148 for (i = 0;i < numlightentities;i++)
4149 receivermask |= R_Shadow_CalcEntitySideMask(lightentities[i], &rtlight->matrix_worldtolight, &radiustolight, borderbias);
4150 if (receivermask < 0x3F)
4151 for(i = 0; i < numlightentities_noselfshadow;i++)
4152 receivermask |= R_Shadow_CalcEntitySideMask(lightentities_noselfshadow[i], &rtlight->matrix_worldtolight, &radiustolight, borderbias);
4155 receivermask &= R_Shadow_CullFrustumSides(rtlight, size, r_shadow_shadowmapborder);
4159 for (i = 0;i < numshadowentities;i++)
4160 castermask |= (entitysides[i] = R_Shadow_CalcEntitySideMask(shadowentities[i], &rtlight->matrix_worldtolight, &radiustolight, borderbias));
4161 for (i = 0;i < numshadowentities_noselfshadow;i++)
4162 castermask |= (entitysides_noselfshadow[i] = R_Shadow_CalcEntitySideMask(shadowentities_noselfshadow[i], &rtlight->matrix_worldtolight, &radiustolight, borderbias));
4165 //Con_Printf("distance %f lodlinear %i (lod %i) size %i\n", distance, lodlinear, r_shadow_shadowmaplod, size);
4167 // render shadow casters into 6 sided depth texture
4168 for (side = 0;side < 6;side++) if (receivermask & (1 << side))
4170 R_Shadow_RenderMode_ShadowMap(side, receivermask, size);
4171 if (! (castermask & (1 << side))) continue;
4173 R_Shadow_DrawWorldShadow_ShadowMap(numsurfaces, surfacelist, shadowtrispvs, surfacesides);
4174 for (i = 0;i < numshadowentities;i++) if (entitysides[i] & (1 << side))
4175 R_Shadow_DrawEntityShadow(shadowentities[i]);
4178 if (numlightentities_noselfshadow)
4180 // render lighting using the depth texture as shadowmap
4181 // draw lighting in the unmasked areas
4182 R_Shadow_RenderMode_Lighting(false, false, true);
4183 for (i = 0;i < numlightentities_noselfshadow;i++)
4184 R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]);
4187 // render shadow casters into 6 sided depth texture
4188 if (numshadowentities_noselfshadow)
4190 for (side = 0;side < 6;side++) if ((receivermask & castermask) & (1 << side))
4192 R_Shadow_RenderMode_ShadowMap(side, 0, size);
4193 for (i = 0;i < numshadowentities_noselfshadow;i++) if (entitysides_noselfshadow[i] & (1 << side))
4194 R_Shadow_DrawEntityShadow(shadowentities_noselfshadow[i]);
4198 // render lighting using the depth texture as shadowmap
4199 // draw lighting in the unmasked areas
4200 R_Shadow_RenderMode_Lighting(false, false, true);
4201 // draw lighting in the unmasked areas
4203 R_Shadow_DrawWorldLight(numsurfaces, surfacelist, lighttrispvs);
4204 for (i = 0;i < numlightentities;i++)
4205 R_Shadow_DrawEntityLight(lightentities[i]);
4207 else if (castshadows && vid.stencil)
4209 // draw stencil shadow volumes to mask off pixels that are in shadow
4210 // so that they won't receive lighting
4211 GL_Scissor(r_shadow_lightscissor[0], r_shadow_lightscissor[1], r_shadow_lightscissor[2], r_shadow_lightscissor[3]);
4212 R_Shadow_ClearStencil();
4215 R_Shadow_DrawWorldShadow_ShadowVolume(numsurfaces, surfacelist, shadowtrispvs);
4216 for (i = 0;i < numshadowentities;i++)
4217 R_Shadow_DrawEntityShadow(shadowentities[i]);
4219 // draw lighting in the unmasked areas
4220 R_Shadow_RenderMode_Lighting(true, false, false);
4221 for (i = 0;i < numlightentities_noselfshadow;i++)
4222 R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]);
4224 for (i = 0;i < numshadowentities_noselfshadow;i++)
4225 R_Shadow_DrawEntityShadow(shadowentities_noselfshadow[i]);
4227 // draw lighting in the unmasked areas
4228 R_Shadow_RenderMode_Lighting(true, false, false);
4230 R_Shadow_DrawWorldLight(numsurfaces, surfacelist, lighttrispvs);
4231 for (i = 0;i < numlightentities;i++)
4232 R_Shadow_DrawEntityLight(lightentities[i]);
4236 // draw lighting in the unmasked areas
4237 R_Shadow_RenderMode_Lighting(false, false, false);
4239 R_Shadow_DrawWorldLight(numsurfaces, surfacelist, lighttrispvs);
4240 for (i = 0;i < numlightentities;i++)
4241 R_Shadow_DrawEntityLight(lightentities[i]);
4242 for (i = 0;i < numlightentities_noselfshadow;i++)
4243 R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]);
4246 if (r_shadow_usingdeferredprepass)
4248 // when rendering deferred lighting, we simply rasterize the box
4249 if (castshadows && r_shadow_shadowmode == R_SHADOW_SHADOWMODE_SHADOWMAP2D)
4250 R_Shadow_RenderMode_DrawDeferredLight(false, true);
4251 else if (castshadows && vid.stencil)
4252 R_Shadow_RenderMode_DrawDeferredLight(true, false);
4254 R_Shadow_RenderMode_DrawDeferredLight(false, false);
4257 if (r_shadow_particletrace.integer)
4258 R_Shadow_RenderParticlesForLight(rtlight);
4261 static void R_Shadow_FreeDeferred(void)
4263 R_Mesh_DestroyFramebufferObject(r_shadow_prepassgeometryfbo);
4264 r_shadow_prepassgeometryfbo = 0;
4266 R_Mesh_DestroyFramebufferObject(r_shadow_prepasslightingdiffusespecularfbo);
4267 r_shadow_prepasslightingdiffusespecularfbo = 0;
4269 R_Mesh_DestroyFramebufferObject(r_shadow_prepasslightingdiffusefbo);
4270 r_shadow_prepasslightingdiffusefbo = 0;
4272 if (r_shadow_prepassgeometrydepthtexture)
4273 R_FreeTexture(r_shadow_prepassgeometrydepthtexture);
4274 r_shadow_prepassgeometrydepthtexture = NULL;
4276 if (r_shadow_prepassgeometrydepthcolortexture)
4277 R_FreeTexture(r_shadow_prepassgeometrydepthcolortexture);
4278 r_shadow_prepassgeometrydepthcolortexture = NULL;
4280 if (r_shadow_prepassgeometrynormalmaptexture)
4281 R_FreeTexture(r_shadow_prepassgeometrynormalmaptexture);
4282 r_shadow_prepassgeometrynormalmaptexture = NULL;
4284 if (r_shadow_prepasslightingdiffusetexture)
4285 R_FreeTexture(r_shadow_prepasslightingdiffusetexture);
4286 r_shadow_prepasslightingdiffusetexture = NULL;
4288 if (r_shadow_prepasslightingspeculartexture)
4289 R_FreeTexture(r_shadow_prepasslightingspeculartexture);
4290 r_shadow_prepasslightingspeculartexture = NULL;
4293 void R_Shadow_DrawPrepass(void)
4301 entity_render_t *ent;
4302 float clearcolor[4];
4304 R_Mesh_ResetTextureState();
4306 GL_ColorMask(1,1,1,1);
4307 GL_BlendFunc(GL_ONE, GL_ZERO);
4310 R_Mesh_SetRenderTargets(r_shadow_prepassgeometryfbo, r_shadow_prepassgeometrydepthtexture, r_shadow_prepassgeometrynormalmaptexture, r_shadow_prepassgeometrydepthcolortexture, NULL, NULL);
4311 Vector4Set(clearcolor, 0.5f,0.5f,0.5f,1.0f);
4312 GL_Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, clearcolor, 1.0f, 0);
4313 if (r_timereport_active)
4314 R_TimeReport("prepasscleargeom");
4316 if (cl.csqc_vidvars.drawworld && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->DrawPrepass)
4317 r_refdef.scene.worldmodel->DrawPrepass(r_refdef.scene.worldentity);
4318 if (r_timereport_active)
4319 R_TimeReport("prepassworld");
4321 for (i = 0;i < r_refdef.scene.numentities;i++)
4323 if (!r_refdef.viewcache.entityvisible[i])
4325 ent = r_refdef.scene.entities[i];
4326 if (ent->model && ent->model->DrawPrepass != NULL)
4327 ent->model->DrawPrepass(ent);
4330 if (r_timereport_active)
4331 R_TimeReport("prepassmodels");
4333 GL_DepthMask(false);
4334 GL_ColorMask(1,1,1,1);
4337 R_Mesh_SetRenderTargets(r_shadow_prepasslightingdiffusespecularfbo, r_shadow_prepassgeometrydepthtexture, r_shadow_prepasslightingdiffusetexture, r_shadow_prepasslightingspeculartexture, NULL, NULL);
4338 Vector4Set(clearcolor, 0, 0, 0, 0);
4339 GL_Clear(GL_COLOR_BUFFER_BIT, clearcolor, 1.0f, 0);
4340 if (r_timereport_active)
4341 R_TimeReport("prepassclearlit");
4343 R_Shadow_RenderMode_Begin();
4345 flag = r_refdef.scene.rtworld ? LIGHTFLAG_REALTIMEMODE : LIGHTFLAG_NORMALMODE;
4346 if (r_shadow_debuglight.integer >= 0)
4348 lightindex = r_shadow_debuglight.integer;
4349 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
4350 if (light && (light->flags & flag) && light->rtlight.draw)
4351 R_Shadow_DrawLight(&light->rtlight);
4355 range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
4356 for (lightindex = 0;lightindex < range;lightindex++)
4358 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
4359 if (light && (light->flags & flag) && light->rtlight.draw)
4360 R_Shadow_DrawLight(&light->rtlight);
4363 if (r_refdef.scene.rtdlight)
4364 for (lnum = 0;lnum < r_refdef.scene.numlights;lnum++)
4365 if (r_refdef.scene.lights[lnum]->draw)
4366 R_Shadow_DrawLight(r_refdef.scene.lights[lnum]);
4368 R_Mesh_ResetRenderTargets();
4370 R_Shadow_RenderMode_End();
4372 if (r_timereport_active)
4373 R_TimeReport("prepasslights");
4376 void R_Shadow_DrawLightSprites(void);
4377 void R_Shadow_PrepareLights(void)
4387 if (r_shadow_shadowmapmaxsize != bound(1, r_shadow_shadowmapping_maxsize.integer, (int)vid.maxtexturesize_2d / 4) ||
4388 (r_shadow_shadowmode != R_SHADOW_SHADOWMODE_STENCIL) != (r_shadow_shadowmapping.integer || r_shadow_deferred.integer) ||
4389 r_shadow_shadowmapvsdct != (r_shadow_shadowmapping_vsdct.integer != 0 && vid.renderpath == RENDERPATH_GL20) ||
4390 r_shadow_shadowmapfilterquality != r_shadow_shadowmapping_filterquality.integer ||
4391 r_shadow_shadowmapdepthbits != r_shadow_shadowmapping_depthbits.integer ||
4392 r_shadow_shadowmapborder != bound(0, r_shadow_shadowmapping_bordersize.integer, 16))
4393 R_Shadow_FreeShadowMaps();
4395 r_shadow_usingshadowmaportho = false;
4397 switch (vid.renderpath)
4399 case RENDERPATH_GL20:
4400 case RENDERPATH_D3D9:
4401 case RENDERPATH_D3D10:
4402 case RENDERPATH_D3D11:
4403 case RENDERPATH_SOFT:
4404 case RENDERPATH_GLES2:
4405 if (!r_shadow_deferred.integer || r_shadow_shadowmode == R_SHADOW_SHADOWMODE_STENCIL || !vid.support.ext_framebuffer_object || vid.maxdrawbuffers < 2)
4407 r_shadow_usingdeferredprepass = false;
4408 if (r_shadow_prepass_width)
4409 R_Shadow_FreeDeferred();
4410 r_shadow_prepass_width = r_shadow_prepass_height = 0;
4414 if (r_shadow_prepass_width != vid.width || r_shadow_prepass_height != vid.height)
4416 R_Shadow_FreeDeferred();
4418 r_shadow_usingdeferredprepass = true;
4419 r_shadow_prepass_width = vid.width;
4420 r_shadow_prepass_height = vid.height;
4421 r_shadow_prepassgeometrydepthtexture = R_LoadTextureShadowMap2D(r_shadow_texturepool, "prepassgeometrydepthmap", vid.width, vid.height, 24, false);
4422 switch (vid.renderpath)
4424 case RENDERPATH_D3D9:
4425 r_shadow_prepassgeometrydepthcolortexture = R_LoadTexture2D(r_shadow_texturepool, "prepassgeometrydepthcolormap", vid.width, vid.height, NULL, TEXTYPE_COLORBUFFER, TEXF_RENDERTARGET | TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCENEAREST, -1, NULL);
4430 r_shadow_prepassgeometrynormalmaptexture = R_LoadTexture2D(r_shadow_texturepool, "prepassgeometrynormalmap", vid.width, vid.height, NULL, TEXTYPE_COLORBUFFER, TEXF_RENDERTARGET | TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCENEAREST, -1, NULL);
4431 r_shadow_prepasslightingdiffusetexture = R_LoadTexture2D(r_shadow_texturepool, "prepasslightingdiffuse", vid.width, vid.height, NULL, TEXTYPE_COLORBUFFER, TEXF_RENDERTARGET | TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCENEAREST, -1, NULL);
4432 r_shadow_prepasslightingspeculartexture = R_LoadTexture2D(r_shadow_texturepool, "prepasslightingspecular", vid.width, vid.height, NULL, TEXTYPE_COLORBUFFER, TEXF_RENDERTARGET | TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCENEAREST, -1, NULL);
4434 // set up the geometry pass fbo (depth + normalmap)
4435 r_shadow_prepassgeometryfbo = R_Mesh_CreateFramebufferObject(r_shadow_prepassgeometrydepthtexture, r_shadow_prepassgeometrynormalmaptexture, NULL, NULL, NULL);
4436 R_Mesh_SetRenderTargets(r_shadow_prepassgeometryfbo, r_shadow_prepassgeometrydepthtexture, r_shadow_prepassgeometrynormalmaptexture, r_shadow_prepassgeometrydepthcolortexture, NULL, NULL);
4437 // render depth into one texture and normalmap into the other
4438 if (qglDrawBuffersARB)
4440 qglDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);CHECKGLERROR
4441 qglReadBuffer(GL_NONE);CHECKGLERROR
4442 status = qglCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);CHECKGLERROR
4443 if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
4445 Con_Printf("R_PrepareRTLights: glCheckFramebufferStatusEXT returned %i\n", status);
4446 Cvar_SetValueQuick(&r_shadow_deferred, 0);
4447 r_shadow_usingdeferredprepass = false;
4451 // set up the lighting pass fbo (diffuse + specular)
4452 r_shadow_prepasslightingdiffusespecularfbo = R_Mesh_CreateFramebufferObject(r_shadow_prepassgeometrydepthtexture, r_shadow_prepasslightingdiffusetexture, r_shadow_prepasslightingspeculartexture, NULL, NULL);
4453 R_Mesh_SetRenderTargets(r_shadow_prepasslightingdiffusespecularfbo, r_shadow_prepassgeometrydepthtexture, r_shadow_prepasslightingdiffusetexture, r_shadow_prepasslightingspeculartexture, NULL, NULL);
4454 // render diffuse into one texture and specular into another,
4455 // with depth and normalmap bound as textures,
4456 // with depth bound as attachment as well
4457 if (qglDrawBuffersARB)
4459 qglDrawBuffersARB(2, r_shadow_prepasslightingdrawbuffers);CHECKGLERROR
4460 qglReadBuffer(GL_NONE);CHECKGLERROR
4461 status = qglCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);CHECKGLERROR
4462 if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
4464 Con_Printf("R_PrepareRTLights: glCheckFramebufferStatusEXT returned %i\n", status);
4465 Cvar_SetValueQuick(&r_shadow_deferred, 0);
4466 r_shadow_usingdeferredprepass = false;
4470 // set up the lighting pass fbo (diffuse)
4471 r_shadow_prepasslightingdiffusefbo = R_Mesh_CreateFramebufferObject(r_shadow_prepassgeometrydepthtexture, r_shadow_prepasslightingdiffusetexture, NULL, NULL, NULL);
4472 R_Mesh_SetRenderTargets(r_shadow_prepasslightingdiffusefbo, r_shadow_prepassgeometrydepthtexture, r_shadow_prepasslightingdiffusetexture, NULL, NULL, NULL);
4473 // render diffuse into one texture,
4474 // with depth and normalmap bound as textures,
4475 // with depth bound as attachment as well
4476 if (qglDrawBuffersARB)
4478 qglDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);CHECKGLERROR
4479 qglReadBuffer(GL_NONE);CHECKGLERROR
4480 status = qglCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);CHECKGLERROR
4481 if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
4483 Con_Printf("R_PrepareRTLights: glCheckFramebufferStatusEXT returned %i\n", status);
4484 Cvar_SetValueQuick(&r_shadow_deferred, 0);
4485 r_shadow_usingdeferredprepass = false;
4490 case RENDERPATH_GL13:
4491 case RENDERPATH_GL11:
4492 r_shadow_usingdeferredprepass = false;
4496 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);
4498 flag = r_refdef.scene.rtworld ? LIGHTFLAG_REALTIMEMODE : LIGHTFLAG_NORMALMODE;
4499 if (r_shadow_debuglight.integer >= 0)
4501 lightindex = r_shadow_debuglight.integer;
4502 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
4503 if (light && (light->flags & flag))
4504 R_Shadow_PrepareLight(&light->rtlight);
4508 range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
4509 for (lightindex = 0;lightindex < range;lightindex++)
4511 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
4512 if (light && (light->flags & flag))
4513 R_Shadow_PrepareLight(&light->rtlight);
4516 if (r_refdef.scene.rtdlight)
4518 for (lnum = 0;lnum < r_refdef.scene.numlights;lnum++)
4519 R_Shadow_PrepareLight(r_refdef.scene.lights[lnum]);
4521 else if(gl_flashblend.integer)
4523 for (lnum = 0;lnum < r_refdef.scene.numlights;lnum++)
4525 rtlight_t *rtlight = r_refdef.scene.lights[lnum];
4526 f = (rtlight->style >= 0 ? r_refdef.scene.lightstylevalue[rtlight->style] : 1) * r_shadow_lightintensityscale.value;
4527 VectorScale(rtlight->color, f, rtlight->currentcolor);
4531 if (r_editlights.integer)
4532 R_Shadow_DrawLightSprites();
4534 R_Shadow_UpdateBounceGridTexture();
4537 void R_Shadow_DrawLights(void)
4545 R_Shadow_RenderMode_Begin();
4547 flag = r_refdef.scene.rtworld ? LIGHTFLAG_REALTIMEMODE : LIGHTFLAG_NORMALMODE;
4548 if (r_shadow_debuglight.integer >= 0)
4550 lightindex = r_shadow_debuglight.integer;
4551 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
4552 if (light && (light->flags & flag))
4553 R_Shadow_DrawLight(&light->rtlight);
4557 range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
4558 for (lightindex = 0;lightindex < range;lightindex++)
4560 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
4561 if (light && (light->flags & flag))
4562 R_Shadow_DrawLight(&light->rtlight);
4565 if (r_refdef.scene.rtdlight)
4566 for (lnum = 0;lnum < r_refdef.scene.numlights;lnum++)
4567 R_Shadow_DrawLight(r_refdef.scene.lights[lnum]);
4569 R_Shadow_RenderMode_End();
4572 extern const float r_screenvertex3f[12];
4573 extern void R_SetupView(qboolean allowwaterclippingplane);
4574 extern void R_ResetViewRendering3D(void);
4575 extern void R_ResetViewRendering2D(void);
4576 extern cvar_t r_shadows;
4577 extern cvar_t r_shadows_darken;
4578 extern cvar_t r_shadows_drawafterrtlighting;
4579 extern cvar_t r_shadows_castfrombmodels;
4580 extern cvar_t r_shadows_throwdistance;
4581 extern cvar_t r_shadows_throwdirection;
4582 extern cvar_t r_shadows_focus;
4583 extern cvar_t r_shadows_shadowmapscale;
4585 void R_Shadow_PrepareModelShadows(void)
4588 float scale, size, radius, dot1, dot2;
4589 vec3_t shadowdir, shadowforward, shadowright, shadoworigin, shadowfocus, shadowmins, shadowmaxs;
4590 entity_render_t *ent;
4592 if (!r_refdef.scene.numentities)
4595 switch (r_shadow_shadowmode)
4597 case R_SHADOW_SHADOWMODE_SHADOWMAP2D:
4598 if (r_shadows.integer >= 2)
4601 case R_SHADOW_SHADOWMODE_STENCIL:
4602 for (i = 0;i < r_refdef.scene.numentities;i++)
4604 ent = r_refdef.scene.entities[i];
4605 if (!ent->animcache_vertex3f && ent->model && ent->model->DrawShadowVolume != NULL && (!ent->model->brush.submodel || r_shadows_castfrombmodels.integer) && (ent->flags & RENDER_SHADOW))
4606 R_AnimCache_GetEntity(ent, false, false);
4613 size = 2*r_shadow_shadowmapmaxsize;
4614 scale = r_shadow_shadowmapping_precision.value * r_shadows_shadowmapscale.value;
4615 radius = 0.5f * size / scale;
4617 Math_atov(r_shadows_throwdirection.string, shadowdir);
4618 VectorNormalize(shadowdir);
4619 dot1 = DotProduct(r_refdef.view.forward, shadowdir);
4620 dot2 = DotProduct(r_refdef.view.up, shadowdir);
4621 if (fabs(dot1) <= fabs(dot2))
4622 VectorMA(r_refdef.view.forward, -dot1, shadowdir, shadowforward);
4624 VectorMA(r_refdef.view.up, -dot2, shadowdir, shadowforward);
4625 VectorNormalize(shadowforward);
4626 CrossProduct(shadowdir, shadowforward, shadowright);
4627 Math_atov(r_shadows_focus.string, shadowfocus);
4628 VectorM(shadowfocus[0], r_refdef.view.right, shadoworigin);
4629 VectorMA(shadoworigin, shadowfocus[1], r_refdef.view.up, shadoworigin);
4630 VectorMA(shadoworigin, -shadowfocus[2], r_refdef.view.forward, shadoworigin);
4631 VectorAdd(shadoworigin, r_refdef.view.origin, shadoworigin);
4632 if (shadowfocus[0] || shadowfocus[1] || shadowfocus[2])
4634 VectorMA(shadoworigin, (1.0f - fabs(dot1)) * radius, shadowforward, shadoworigin);
4636 shadowmins[0] = shadoworigin[0] - r_shadows_throwdistance.value * fabs(shadowdir[0]) - radius * (fabs(shadowforward[0]) + fabs(shadowright[0]));
4637 shadowmins[1] = shadoworigin[1] - r_shadows_throwdistance.value * fabs(shadowdir[1]) - radius * (fabs(shadowforward[1]) + fabs(shadowright[1]));
4638 shadowmins[2] = shadoworigin[2] - r_shadows_throwdistance.value * fabs(shadowdir[2]) - radius * (fabs(shadowforward[2]) + fabs(shadowright[2]));
4639 shadowmaxs[0] = shadoworigin[0] + r_shadows_throwdistance.value * fabs(shadowdir[0]) + radius * (fabs(shadowforward[0]) + fabs(shadowright[0]));
4640 shadowmaxs[1] = shadoworigin[1] + r_shadows_throwdistance.value * fabs(shadowdir[1]) + radius * (fabs(shadowforward[1]) + fabs(shadowright[1]));
4641 shadowmaxs[2] = shadoworigin[2] + r_shadows_throwdistance.value * fabs(shadowdir[2]) + radius * (fabs(shadowforward[2]) + fabs(shadowright[2]));
4643 for (i = 0;i < r_refdef.scene.numentities;i++)
4645 ent = r_refdef.scene.entities[i];
4646 if (!BoxesOverlap(ent->mins, ent->maxs, shadowmins, shadowmaxs))
4648 // cast shadows from anything of the map (submodels are optional)
4649 if (!ent->animcache_vertex3f && ent->model && ent->model->DrawShadowMap != NULL && (!ent->model->brush.submodel || r_shadows_castfrombmodels.integer) && (ent->flags & RENDER_SHADOW))
4650 R_AnimCache_GetEntity(ent, false, false);
4654 void R_DrawModelShadowMaps(void)
4657 float relativethrowdistance, scale, size, radius, nearclip, farclip, bias, dot1, dot2;
4658 entity_render_t *ent;
4659 vec3_t relativelightorigin;
4660 vec3_t relativelightdirection, relativeforward, relativeright;
4661 vec3_t relativeshadowmins, relativeshadowmaxs;
4662 vec3_t shadowdir, shadowforward, shadowright, shadoworigin, shadowfocus;
4664 matrix4x4_t shadowmatrix, cameramatrix, mvpmatrix, invmvpmatrix, scalematrix, texmatrix;
4665 r_viewport_t viewport;
4667 float clearcolor[4];
4669 if (!r_refdef.scene.numentities)
4672 switch (r_shadow_shadowmode)
4674 case R_SHADOW_SHADOWMODE_SHADOWMAP2D:
4680 R_ResetViewRendering3D();
4681 R_Shadow_RenderMode_Begin();
4682 R_Shadow_RenderMode_ActiveLight(NULL);
4684 switch (r_shadow_shadowmode)
4686 case R_SHADOW_SHADOWMODE_SHADOWMAP2D:
4687 if (!r_shadow_shadowmap2dtexture)
4688 R_Shadow_MakeShadowMap(0, r_shadow_shadowmapmaxsize);
4689 fbo = r_shadow_fbo2d;
4690 r_shadow_shadowmap_texturescale[0] = 1.0f / R_TextureWidth(r_shadow_shadowmap2dtexture);
4691 r_shadow_shadowmap_texturescale[1] = 1.0f / R_TextureHeight(r_shadow_shadowmap2dtexture);
4692 r_shadow_rendermode = R_SHADOW_RENDERMODE_SHADOWMAP2D;
4698 size = 2*r_shadow_shadowmapmaxsize;
4699 scale = (r_shadow_shadowmapping_precision.value * r_shadows_shadowmapscale.value) / size;
4700 radius = 0.5f / scale;
4701 nearclip = -r_shadows_throwdistance.value;
4702 farclip = r_shadows_throwdistance.value;
4703 bias = r_shadow_shadowmapping_bias.value * r_shadow_shadowmapping_nearclip.value / (2 * r_shadows_throwdistance.value) * (1024.0f / size);
4705 r_shadow_shadowmap_parameters[0] = size;
4706 r_shadow_shadowmap_parameters[1] = size;
4707 r_shadow_shadowmap_parameters[2] = 1.0;
4708 r_shadow_shadowmap_parameters[3] = bound(0.0f, 1.0f - r_shadows_darken.value, 1.0f);
4710 Math_atov(r_shadows_throwdirection.string, shadowdir);
4711 VectorNormalize(shadowdir);
4712 Math_atov(r_shadows_focus.string, shadowfocus);
4713 VectorM(shadowfocus[0], r_refdef.view.right, shadoworigin);
4714 VectorMA(shadoworigin, shadowfocus[1], r_refdef.view.up, shadoworigin);
4715 VectorMA(shadoworigin, -shadowfocus[2], r_refdef.view.forward, shadoworigin);
4716 VectorAdd(shadoworigin, r_refdef.view.origin, shadoworigin);
4717 dot1 = DotProduct(r_refdef.view.forward, shadowdir);
4718 dot2 = DotProduct(r_refdef.view.up, shadowdir);
4719 if (fabs(dot1) <= fabs(dot2))
4720 VectorMA(r_refdef.view.forward, -dot1, shadowdir, shadowforward);
4722 VectorMA(r_refdef.view.up, -dot2, shadowdir, shadowforward);
4723 VectorNormalize(shadowforward);
4724 VectorM(scale, shadowforward, &m[0]);
4725 if (shadowfocus[0] || shadowfocus[1] || shadowfocus[2])
4727 m[3] = fabs(dot1) * 0.5f - DotProduct(shadoworigin, &m[0]);
4728 CrossProduct(shadowdir, shadowforward, shadowright);
4729 VectorM(scale, shadowright, &m[4]);
4730 m[7] = 0.5f - DotProduct(shadoworigin, &m[4]);
4731 VectorM(1.0f / (farclip - nearclip), shadowdir, &m[8]);
4732 m[11] = 0.5f - DotProduct(shadoworigin, &m[8]);
4733 Matrix4x4_FromArray12FloatD3D(&shadowmatrix, m);
4734 Matrix4x4_Invert_Full(&cameramatrix, &shadowmatrix);
4735 R_Viewport_InitOrtho(&viewport, &cameramatrix, 0, 0, size, size, 0, 0, 1, 1, 0, -1, NULL);
4737 VectorMA(shadoworigin, (1.0f - fabs(dot1)) * radius, shadowforward, shadoworigin);
4739 R_Mesh_SetRenderTargets(fbo, r_shadow_shadowmap2dtexture, r_shadow_shadowmap2dcolortexture, NULL, NULL, NULL);
4740 R_SetupShader_DepthOrShadow();
4741 GL_PolygonOffset(r_shadow_shadowmapping_polygonfactor.value, r_shadow_shadowmapping_polygonoffset.value);
4744 R_SetViewport(&viewport);
4745 GL_Scissor(viewport.x, viewport.y, min(viewport.width + r_shadow_shadowmapborder, 2*r_shadow_shadowmapmaxsize), viewport.height + r_shadow_shadowmapborder);
4746 Vector4Set(clearcolor, 1,1,1,1);
4747 // in D3D9 we have to render to a color texture shadowmap
4748 // in GL we render directly to a depth texture only
4749 if (r_shadow_shadowmap2dtexture)
4750 GL_Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, clearcolor, 1.0f, 0);
4752 GL_Clear(GL_DEPTH_BUFFER_BIT, clearcolor, 1.0f, 0);
4753 // render into a slightly restricted region so that the borders of the
4754 // shadowmap area fade away, rather than streaking across everything
4755 // outside the usable area
4756 GL_Scissor(viewport.x + r_shadow_shadowmapborder, viewport.y + r_shadow_shadowmapborder, viewport.width - 2*r_shadow_shadowmapborder, viewport.height - 2*r_shadow_shadowmapborder);
4760 R_Mesh_ResetRenderTargets();
4761 R_SetupShader_ShowDepth();
4762 GL_ColorMask(1,1,1,1);
4763 GL_Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, clearcolor, 1.0f, 0);
4766 for (i = 0;i < r_refdef.scene.numentities;i++)
4768 ent = r_refdef.scene.entities[i];
4770 // cast shadows from anything of the map (submodels are optional)
4771 if (ent->model && ent->model->DrawShadowMap != NULL && (!ent->model->brush.submodel || r_shadows_castfrombmodels.integer) && (ent->flags & RENDER_SHADOW))
4773 relativethrowdistance = r_shadows_throwdistance.value * Matrix4x4_ScaleFromMatrix(&ent->inversematrix);
4774 Matrix4x4_Transform(&ent->inversematrix, shadoworigin, relativelightorigin);
4775 Matrix4x4_Transform3x3(&ent->inversematrix, shadowdir, relativelightdirection);
4776 Matrix4x4_Transform3x3(&ent->inversematrix, shadowforward, relativeforward);
4777 Matrix4x4_Transform3x3(&ent->inversematrix, shadowright, relativeright);
4778 relativeshadowmins[0] = relativelightorigin[0] - r_shadows_throwdistance.value * fabs(relativelightdirection[0]) - radius * (fabs(relativeforward[0]) + fabs(relativeright[0]));
4779 relativeshadowmins[1] = relativelightorigin[1] - r_shadows_throwdistance.value * fabs(relativelightdirection[1]) - radius * (fabs(relativeforward[1]) + fabs(relativeright[1]));
4780 relativeshadowmins[2] = relativelightorigin[2] - r_shadows_throwdistance.value * fabs(relativelightdirection[2]) - radius * (fabs(relativeforward[2]) + fabs(relativeright[2]));
4781 relativeshadowmaxs[0] = relativelightorigin[0] + r_shadows_throwdistance.value * fabs(relativelightdirection[0]) + radius * (fabs(relativeforward[0]) + fabs(relativeright[0]));
4782 relativeshadowmaxs[1] = relativelightorigin[1] + r_shadows_throwdistance.value * fabs(relativelightdirection[1]) + radius * (fabs(relativeforward[1]) + fabs(relativeright[1]));
4783 relativeshadowmaxs[2] = relativelightorigin[2] + r_shadows_throwdistance.value * fabs(relativelightdirection[2]) + radius * (fabs(relativeforward[2]) + fabs(relativeright[2]));
4784 RSurf_ActiveModelEntity(ent, false, false, false);
4785 ent->model->DrawShadowMap(0, ent, relativelightorigin, relativelightdirection, relativethrowdistance, ent->model->nummodelsurfaces, ent->model->sortedmodelsurfaces, NULL, relativeshadowmins, relativeshadowmaxs);
4786 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
4793 unsigned char *rawpixels = Z_Malloc(viewport.width*viewport.height*4);
4795 qglReadPixels(viewport.x, viewport.y, viewport.width, viewport.height, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, rawpixels);
4797 Image_WriteTGABGRA("r_shadows_2.tga", viewport.width, viewport.height, rawpixels);
4798 Cvar_SetValueQuick(&r_test, 0);
4803 R_Shadow_RenderMode_End();
4805 Matrix4x4_Concat(&mvpmatrix, &r_refdef.view.viewport.projectmatrix, &r_refdef.view.viewport.viewmatrix);
4806 Matrix4x4_Invert_Full(&invmvpmatrix, &mvpmatrix);
4807 Matrix4x4_CreateScale3(&scalematrix, size, -size, 1);
4808 Matrix4x4_AdjustOrigin(&scalematrix, 0, size, -0.5f * bias);
4809 Matrix4x4_Concat(&texmatrix, &scalematrix, &shadowmatrix);
4810 Matrix4x4_Concat(&r_shadow_shadowmapmatrix, &texmatrix, &invmvpmatrix);
4812 switch (vid.renderpath)
4814 case RENDERPATH_GL11:
4815 case RENDERPATH_GL13:
4816 case RENDERPATH_GL20:
4817 case RENDERPATH_SOFT:
4818 case RENDERPATH_GLES2:
4820 case RENDERPATH_D3D9:
4821 case RENDERPATH_D3D10:
4822 case RENDERPATH_D3D11:
4823 #ifdef OPENGL_ORIENTATION
4824 r_shadow_shadowmapmatrix.m[0][0] *= -1.0f;
4825 r_shadow_shadowmapmatrix.m[0][1] *= -1.0f;
4826 r_shadow_shadowmapmatrix.m[0][2] *= -1.0f;
4827 r_shadow_shadowmapmatrix.m[0][3] *= -1.0f;
4829 r_shadow_shadowmapmatrix.m[0][0] *= -1.0f;
4830 r_shadow_shadowmapmatrix.m[1][0] *= -1.0f;
4831 r_shadow_shadowmapmatrix.m[2][0] *= -1.0f;
4832 r_shadow_shadowmapmatrix.m[3][0] *= -1.0f;
4837 r_shadow_usingshadowmaportho = true;
4838 switch (r_shadow_shadowmode)
4840 case R_SHADOW_SHADOWMODE_SHADOWMAP2D:
4841 r_shadow_usingshadowmap2d = true;
4848 void R_DrawModelShadows(void)
4851 float relativethrowdistance;
4852 entity_render_t *ent;
4853 vec3_t relativelightorigin;
4854 vec3_t relativelightdirection;
4855 vec3_t relativeshadowmins, relativeshadowmaxs;
4856 vec3_t tmp, shadowdir;
4858 if (!r_refdef.scene.numentities || !vid.stencil || (r_shadow_shadowmode != R_SHADOW_SHADOWMODE_STENCIL && r_shadows.integer != 1))
4861 R_ResetViewRendering3D();
4862 //GL_Scissor(r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
4863 //GL_Scissor(r_refdef.view.x, vid.height - r_refdef.view.height - r_refdef.view.y, r_refdef.view.width, r_refdef.view.height);
4864 R_Shadow_RenderMode_Begin();
4865 R_Shadow_RenderMode_ActiveLight(NULL);
4866 r_shadow_lightscissor[0] = r_refdef.view.x;
4867 r_shadow_lightscissor[1] = vid.height - r_refdef.view.y - r_refdef.view.height;
4868 r_shadow_lightscissor[2] = r_refdef.view.width;
4869 r_shadow_lightscissor[3] = r_refdef.view.height;
4870 R_Shadow_RenderMode_StencilShadowVolumes(false);
4873 if (r_shadows.integer == 2)
4875 Math_atov(r_shadows_throwdirection.string, shadowdir);
4876 VectorNormalize(shadowdir);
4879 R_Shadow_ClearStencil();
4881 for (i = 0;i < r_refdef.scene.numentities;i++)
4883 ent = r_refdef.scene.entities[i];
4885 // cast shadows from anything of the map (submodels are optional)
4886 if (ent->model && ent->model->DrawShadowVolume != NULL && (!ent->model->brush.submodel || r_shadows_castfrombmodels.integer) && (ent->flags & RENDER_SHADOW))
4888 relativethrowdistance = r_shadows_throwdistance.value * Matrix4x4_ScaleFromMatrix(&ent->inversematrix);
4889 VectorSet(relativeshadowmins, -relativethrowdistance, -relativethrowdistance, -relativethrowdistance);
4890 VectorSet(relativeshadowmaxs, relativethrowdistance, relativethrowdistance, relativethrowdistance);
4891 if (r_shadows.integer == 2) // 2: simpler mode, throw shadows always in same direction
4892 Matrix4x4_Transform3x3(&ent->inversematrix, shadowdir, relativelightdirection);
4895 if(ent->entitynumber != 0)
4897 if(ent->entitynumber >= MAX_EDICTS) // csqc entity
4899 // FIXME handle this
4900 VectorNegate(ent->modellight_lightdir, relativelightdirection);
4904 // networked entity - might be attached in some way (then we should use the parent's light direction, to not tear apart attached entities)
4905 int entnum, entnum2, recursion;
4906 entnum = entnum2 = ent->entitynumber;
4907 for(recursion = 32; recursion > 0; --recursion)
4909 entnum2 = cl.entities[entnum].state_current.tagentity;
4910 if(entnum2 >= 1 && entnum2 < cl.num_entities && cl.entities_active[entnum2])
4915 if(recursion && recursion != 32) // if we followed a valid non-empty attachment chain
4917 VectorNegate(cl.entities[entnum].render.modellight_lightdir, relativelightdirection);
4918 // transform into modelspace of OUR entity
4919 Matrix4x4_Transform3x3(&cl.entities[entnum].render.matrix, relativelightdirection, tmp);
4920 Matrix4x4_Transform3x3(&ent->inversematrix, tmp, relativelightdirection);
4923 VectorNegate(ent->modellight_lightdir, relativelightdirection);
4927 VectorNegate(ent->modellight_lightdir, relativelightdirection);
4930 VectorScale(relativelightdirection, -relativethrowdistance, relativelightorigin);
4931 RSurf_ActiveModelEntity(ent, false, false, false);
4932 ent->model->DrawShadowVolume(ent, relativelightorigin, relativelightdirection, relativethrowdistance, ent->model->nummodelsurfaces, ent->model->sortedmodelsurfaces, relativeshadowmins, relativeshadowmaxs);
4933 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
4937 // not really the right mode, but this will disable any silly stencil features
4938 R_Shadow_RenderMode_End();
4940 // set up ortho view for rendering this pass
4941 //GL_Scissor(r_refdef.view.x, vid.height - r_refdef.view.height - r_refdef.view.y, r_refdef.view.width, r_refdef.view.height);
4942 //GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
4943 //GL_ScissorTest(true);
4944 //R_EntityMatrix(&identitymatrix);
4945 //R_Mesh_ResetTextureState();
4946 R_ResetViewRendering2D();
4948 // set up a darkening blend on shadowed areas
4949 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
4950 //GL_DepthRange(0, 1);
4951 //GL_DepthTest(false);
4952 //GL_DepthMask(false);
4953 //GL_PolygonOffset(0, 0);CHECKGLERROR
4954 GL_Color(0, 0, 0, r_shadows_darken.value);
4955 //GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
4956 //GL_DepthFunc(GL_ALWAYS);
4957 R_SetStencil(true, 255, GL_KEEP, GL_KEEP, GL_KEEP, GL_NOTEQUAL, 128, 255);
4959 // apply the blend to the shadowed areas
4960 R_Mesh_PrepareVertices_Generic_Arrays(4, r_screenvertex3f, NULL, NULL);
4961 R_SetupShader_Generic(NULL, NULL, GL_MODULATE, 1);
4962 R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
4964 // restore the viewport
4965 R_SetViewport(&r_refdef.view.viewport);
4967 // restore other state to normal
4968 //R_Shadow_RenderMode_End();
4971 void R_BeginCoronaQuery(rtlight_t *rtlight, float scale, qboolean usequery)
4974 vec3_t centerorigin;
4976 // if it's too close, skip it
4977 if (VectorLength(rtlight->currentcolor) < (1.0f / 256.0f))
4979 zdist = (DotProduct(rtlight->shadoworigin, r_refdef.view.forward) - DotProduct(r_refdef.view.origin, r_refdef.view.forward));
4982 if (usequery && r_numqueries + 2 <= r_maxqueries)
4984 rtlight->corona_queryindex_allpixels = r_queries[r_numqueries++];
4985 rtlight->corona_queryindex_visiblepixels = r_queries[r_numqueries++];
4986 // we count potential samples in the middle of the screen, we count actual samples at the light location, this allows counting potential samples of off-screen lights
4987 VectorMA(r_refdef.view.origin, zdist, r_refdef.view.forward, centerorigin);
4989 switch(vid.renderpath)
4991 case RENDERPATH_GL20:
4992 case RENDERPATH_GL13:
4993 case RENDERPATH_GL11:
4994 case RENDERPATH_GLES2:
4996 // NOTE: GL_DEPTH_TEST must be enabled or ATI won't count samples, so use GL_DepthFunc instead
4997 qglBeginQueryARB(GL_SAMPLES_PASSED_ARB, rtlight->corona_queryindex_allpixels);
4998 GL_DepthFunc(GL_ALWAYS);
4999 R_CalcSprite_Vertex3f(vertex3f, centerorigin, r_refdef.view.right, r_refdef.view.up, scale, -scale, -scale, scale);
5000 R_Mesh_PrepareVertices_Vertex3f(4, vertex3f, NULL);
5001 R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
5002 qglEndQueryARB(GL_SAMPLES_PASSED_ARB);
5003 GL_DepthFunc(GL_LEQUAL);
5004 qglBeginQueryARB(GL_SAMPLES_PASSED_ARB, rtlight->corona_queryindex_visiblepixels);
5005 R_CalcSprite_Vertex3f(vertex3f, rtlight->shadoworigin, r_refdef.view.right, r_refdef.view.up, scale, -scale, -scale, scale);
5006 R_Mesh_PrepareVertices_Vertex3f(4, vertex3f, NULL);
5007 R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
5008 qglEndQueryARB(GL_SAMPLES_PASSED_ARB);
5011 case RENDERPATH_D3D9:
5012 Con_DPrintf("FIXME D3D9 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
5014 case RENDERPATH_D3D10:
5015 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
5017 case RENDERPATH_D3D11:
5018 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
5020 case RENDERPATH_SOFT:
5021 //Con_DPrintf("FIXME SOFT %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
5025 rtlight->corona_visibility = bound(0, (zdist - 32) / 32, 1);
5028 static float spritetexcoord2f[4*2] = {0, 1, 0, 0, 1, 0, 1, 1};
5030 void R_DrawCorona(rtlight_t *rtlight, float cscale, float scale)
5033 GLint allpixels = 0, visiblepixels = 0;
5034 // now we have to check the query result
5035 if (rtlight->corona_queryindex_visiblepixels)
5037 switch(vid.renderpath)
5039 case RENDERPATH_GL20:
5040 case RENDERPATH_GL13:
5041 case RENDERPATH_GL11:
5042 case RENDERPATH_GLES2:
5044 qglGetQueryObjectivARB(rtlight->corona_queryindex_visiblepixels, GL_QUERY_RESULT_ARB, &visiblepixels);
5045 qglGetQueryObjectivARB(rtlight->corona_queryindex_allpixels, GL_QUERY_RESULT_ARB, &allpixels);
5048 case RENDERPATH_D3D9:
5049 Con_DPrintf("FIXME D3D9 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
5051 case RENDERPATH_D3D10:
5052 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
5054 case RENDERPATH_D3D11:
5055 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
5057 case RENDERPATH_SOFT:
5058 //Con_DPrintf("FIXME SOFT %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
5061 //Con_Printf("%i of %i pixels\n", (int)visiblepixels, (int)allpixels);
5062 if (visiblepixels < 1 || allpixels < 1)
5064 rtlight->corona_visibility *= bound(0, (float)visiblepixels / (float)allpixels, 1);
5065 cscale *= rtlight->corona_visibility;
5069 // FIXME: these traces should scan all render entities instead of cl.world
5070 if (CL_TraceLine(r_refdef.view.origin, rtlight->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false).fraction < 1)
5073 VectorScale(rtlight->currentcolor, cscale, color);
5074 if (VectorLength(color) > (1.0f / 256.0f))
5077 qboolean negated = (color[0] + color[1] + color[2] < 0) && vid.support.ext_blend_subtract;
5080 VectorNegate(color, color);
5081 switch(vid.renderpath)
5083 case RENDERPATH_GL11:
5084 case RENDERPATH_GL13:
5085 case RENDERPATH_GL20:
5086 case RENDERPATH_GLES2:
5087 qglBlendEquationEXT(GL_FUNC_REVERSE_SUBTRACT_EXT);
5089 case RENDERPATH_D3D9:
5091 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_BLENDOP, D3DBLENDOP_SUBTRACT);
5094 case RENDERPATH_D3D10:
5095 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
5097 case RENDERPATH_D3D11:
5098 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
5100 case RENDERPATH_SOFT:
5101 DPSOFTRAST_BlendSubtract(true);
5105 R_CalcSprite_Vertex3f(vertex3f, rtlight->shadoworigin, r_refdef.view.right, r_refdef.view.up, scale, -scale, -scale, scale);
5106 RSurf_ActiveCustomEntity(&identitymatrix, &identitymatrix, RENDER_NODEPTHTEST, 0, color[0], color[1], color[2], 1, 4, vertex3f, spritetexcoord2f, NULL, NULL, NULL, NULL, 2, polygonelement3i, polygonelement3s, false, false);
5107 R_DrawCustomSurface(r_shadow_lightcorona, &identitymatrix, MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_FULLBRIGHT | MATERIALFLAG_NOCULLFACE, 0, 4, 0, 2, false, false);
5110 switch(vid.renderpath)
5112 case RENDERPATH_GL11:
5113 case RENDERPATH_GL13:
5114 case RENDERPATH_GL20:
5115 case RENDERPATH_GLES2:
5116 qglBlendEquationEXT(GL_FUNC_ADD_EXT);
5118 case RENDERPATH_D3D9:
5120 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_BLENDOP, D3DBLENDOP_ADD);
5123 case RENDERPATH_D3D10:
5124 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
5126 case RENDERPATH_D3D11:
5127 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
5129 case RENDERPATH_SOFT:
5130 DPSOFTRAST_BlendSubtract(false);
5137 void R_Shadow_DrawCoronas(void)
5140 qboolean usequery = false;
5145 if (r_coronas.value < (1.0f / 256.0f) && !gl_flashblend.integer)
5147 if (r_waterstate.renderingscene)
5149 flag = r_refdef.scene.rtworld ? LIGHTFLAG_REALTIMEMODE : LIGHTFLAG_NORMALMODE;
5150 R_EntityMatrix(&identitymatrix);
5152 range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
5154 // check occlusion of coronas
5155 // use GL_ARB_occlusion_query if available
5156 // otherwise use raytraces
5158 switch (vid.renderpath)
5160 case RENDERPATH_GL11:
5161 case RENDERPATH_GL13:
5162 case RENDERPATH_GL20:
5163 case RENDERPATH_GLES2:
5164 usequery = vid.support.arb_occlusion_query && r_coronas_occlusionquery.integer;
5167 GL_ColorMask(0,0,0,0);
5168 if (r_maxqueries < (range + r_refdef.scene.numlights) * 2)
5169 if (r_maxqueries < MAX_OCCLUSION_QUERIES)
5172 r_maxqueries = (range + r_refdef.scene.numlights) * 4;
5173 r_maxqueries = min(r_maxqueries, MAX_OCCLUSION_QUERIES);
5175 qglGenQueriesARB(r_maxqueries - i, r_queries + i);
5178 RSurf_ActiveWorldEntity();
5179 GL_BlendFunc(GL_ONE, GL_ZERO);
5180 GL_CullFace(GL_NONE);
5181 GL_DepthMask(false);
5182 GL_DepthRange(0, 1);
5183 GL_PolygonOffset(0, 0);
5185 R_Mesh_ResetTextureState();
5186 R_SetupShader_Generic(NULL, NULL, GL_MODULATE, 1);
5189 case RENDERPATH_D3D9:
5191 //Con_DPrintf("FIXME D3D9 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
5193 case RENDERPATH_D3D10:
5194 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
5196 case RENDERPATH_D3D11:
5197 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
5199 case RENDERPATH_SOFT:
5201 //Con_DPrintf("FIXME SOFT %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
5204 for (lightindex = 0;lightindex < range;lightindex++)
5206 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
5209 rtlight = &light->rtlight;
5210 rtlight->corona_visibility = 0;
5211 rtlight->corona_queryindex_visiblepixels = 0;
5212 rtlight->corona_queryindex_allpixels = 0;
5213 if (!(rtlight->flags & flag))
5215 if (rtlight->corona <= 0)
5217 if (r_shadow_debuglight.integer >= 0 && r_shadow_debuglight.integer != (int)lightindex)
5219 R_BeginCoronaQuery(rtlight, rtlight->radius * rtlight->coronasizescale * r_coronas_occlusionsizescale.value, usequery);
5221 for (i = 0;i < r_refdef.scene.numlights;i++)
5223 rtlight = r_refdef.scene.lights[i];
5224 rtlight->corona_visibility = 0;
5225 rtlight->corona_queryindex_visiblepixels = 0;
5226 rtlight->corona_queryindex_allpixels = 0;
5227 if (!(rtlight->flags & flag))
5229 if (rtlight->corona <= 0)
5231 R_BeginCoronaQuery(rtlight, rtlight->radius * rtlight->coronasizescale * r_coronas_occlusionsizescale.value, usequery);
5234 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
5236 // now draw the coronas using the query data for intensity info
5237 for (lightindex = 0;lightindex < range;lightindex++)
5239 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
5242 rtlight = &light->rtlight;
5243 if (rtlight->corona_visibility <= 0)
5245 R_DrawCorona(rtlight, rtlight->corona * r_coronas.value * 0.25f, rtlight->radius * rtlight->coronasizescale);
5247 for (i = 0;i < r_refdef.scene.numlights;i++)
5249 rtlight = r_refdef.scene.lights[i];
5250 if (rtlight->corona_visibility <= 0)
5252 if (gl_flashblend.integer)
5253 R_DrawCorona(rtlight, rtlight->corona, rtlight->radius * rtlight->coronasizescale * 2.0f);
5255 R_DrawCorona(rtlight, rtlight->corona * r_coronas.value * 0.25f, rtlight->radius * rtlight->coronasizescale);
5261 dlight_t *R_Shadow_NewWorldLight(void)
5263 return (dlight_t *)Mem_ExpandableArray_AllocRecord(&r_shadow_worldlightsarray);
5266 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)
5269 // validate parameters
5270 if (style < 0 || style >= MAX_LIGHTSTYLES)
5272 Con_Printf("R_Shadow_NewWorldLight: invalid light style number %i, must be >= 0 and < %i\n", light->style, MAX_LIGHTSTYLES);
5278 // copy to light properties
5279 VectorCopy(origin, light->origin);
5280 light->angles[0] = angles[0] - 360 * floor(angles[0] / 360);
5281 light->angles[1] = angles[1] - 360 * floor(angles[1] / 360);
5282 light->angles[2] = angles[2] - 360 * floor(angles[2] / 360);
5284 light->color[0] = max(color[0], 0);
5285 light->color[1] = max(color[1], 0);
5286 light->color[2] = max(color[2], 0);
5288 light->color[0] = color[0];
5289 light->color[1] = color[1];
5290 light->color[2] = color[2];
5291 light->radius = max(radius, 0);
5292 light->style = style;
5293 light->shadow = shadowenable;
5294 light->corona = corona;
5295 strlcpy(light->cubemapname, cubemapname, sizeof(light->cubemapname));
5296 light->coronasizescale = coronasizescale;
5297 light->ambientscale = ambientscale;
5298 light->diffusescale = diffusescale;
5299 light->specularscale = specularscale;
5300 light->flags = flags;
5302 // update renderable light data
5303 Matrix4x4_CreateFromQuakeEntity(&matrix, light->origin[0], light->origin[1], light->origin[2], light->angles[0], light->angles[1], light->angles[2], light->radius);
5304 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);
5307 void R_Shadow_FreeWorldLight(dlight_t *light)
5309 if (r_shadow_selectedlight == light)
5310 r_shadow_selectedlight = NULL;
5311 R_RTLight_Uncompile(&light->rtlight);
5312 Mem_ExpandableArray_FreeRecord(&r_shadow_worldlightsarray, light);
5315 void R_Shadow_ClearWorldLights(void)
5319 size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
5320 for (lightindex = 0;lightindex < range;lightindex++)
5322 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
5324 R_Shadow_FreeWorldLight(light);
5326 r_shadow_selectedlight = NULL;
5329 void R_Shadow_SelectLight(dlight_t *light)
5331 if (r_shadow_selectedlight)
5332 r_shadow_selectedlight->selected = false;
5333 r_shadow_selectedlight = light;
5334 if (r_shadow_selectedlight)
5335 r_shadow_selectedlight->selected = true;
5338 void R_Shadow_DrawCursor_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
5340 // this is never batched (there can be only one)
5342 R_CalcSprite_Vertex3f(vertex3f, r_editlights_cursorlocation, r_refdef.view.right, r_refdef.view.up, EDLIGHTSPRSIZE, -EDLIGHTSPRSIZE, -EDLIGHTSPRSIZE, EDLIGHTSPRSIZE);
5343 RSurf_ActiveCustomEntity(&identitymatrix, &identitymatrix, 0, 0, 1, 1, 1, 1, 4, vertex3f, spritetexcoord2f, NULL, NULL, NULL, NULL, 2, polygonelement3i, polygonelement3s, false, false);
5344 R_DrawCustomSurface(r_editlights_sprcursor, &identitymatrix, MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_FULLBRIGHT | MATERIALFLAG_NOCULLFACE, 0, 4, 0, 2, false, false);
5347 void R_Shadow_DrawLightSprite_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
5352 skinframe_t *skinframe;
5355 // this is never batched (due to the ent parameter changing every time)
5356 // so numsurfaces == 1 and surfacelist[0] == lightnumber
5357 const dlight_t *light = (dlight_t *)ent;
5360 R_CalcSprite_Vertex3f(vertex3f, light->origin, r_refdef.view.right, r_refdef.view.up, s, -s, -s, s);
5363 VectorScale(light->color, intensity, spritecolor);
5364 if (VectorLength(spritecolor) < 0.1732f)
5365 VectorSet(spritecolor, 0.1f, 0.1f, 0.1f);
5366 if (VectorLength(spritecolor) > 1.0f)
5367 VectorNormalize(spritecolor);
5369 // draw light sprite
5370 if (light->cubemapname[0] && !light->shadow)
5371 skinframe = r_editlights_sprcubemapnoshadowlight;
5372 else if (light->cubemapname[0])
5373 skinframe = r_editlights_sprcubemaplight;
5374 else if (!light->shadow)
5375 skinframe = r_editlights_sprnoshadowlight;
5377 skinframe = r_editlights_sprlight;
5379 RSurf_ActiveCustomEntity(&identitymatrix, &identitymatrix, 0, 0, spritecolor[0], spritecolor[1], spritecolor[2], 1, 4, vertex3f, spritetexcoord2f, NULL, NULL, NULL, NULL, 2, polygonelement3i, polygonelement3s, false, false);
5380 R_DrawCustomSurface(skinframe, &identitymatrix, MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_FULLBRIGHT | MATERIALFLAG_NOCULLFACE, 0, 4, 0, 2, false, false);
5382 // draw selection sprite if light is selected
5383 if (light->selected)
5385 RSurf_ActiveCustomEntity(&identitymatrix, &identitymatrix, 0, 0, 1, 1, 1, 1, 4, vertex3f, spritetexcoord2f, NULL, NULL, NULL, NULL, 2, polygonelement3i, polygonelement3s, false, false);
5386 R_DrawCustomSurface(r_editlights_sprselection, &identitymatrix, MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_FULLBRIGHT | MATERIALFLAG_NOCULLFACE, 0, 4, 0, 2, false, false);
5387 // VorteX todo: add normalmode/realtime mode light overlay sprites?
5391 void R_Shadow_DrawLightSprites(void)
5395 size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
5396 for (lightindex = 0;lightindex < range;lightindex++)
5398 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
5400 R_MeshQueue_AddTransparent(light->origin, R_Shadow_DrawLightSprite_TransparentCallback, (entity_render_t *)light, 5, &light->rtlight);
5402 if (!r_editlights_lockcursor)
5403 R_MeshQueue_AddTransparent(r_editlights_cursorlocation, R_Shadow_DrawCursor_TransparentCallback, NULL, 0, NULL);
5406 int R_Shadow_GetRTLightInfo(unsigned int lightindex, float *origin, float *radius, float *color)
5411 range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray);
5412 if (lightindex >= range)
5414 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
5417 rtlight = &light->rtlight;
5418 //if (!(rtlight->flags & flag))
5420 VectorCopy(rtlight->shadoworigin, origin);
5421 *radius = rtlight->radius;
5422 VectorCopy(rtlight->color, color);
5426 void R_Shadow_SelectLightInView(void)
5428 float bestrating, rating, temp[3];
5432 size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
5436 if (r_editlights_lockcursor)
5438 for (lightindex = 0;lightindex < range;lightindex++)
5440 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
5443 VectorSubtract(light->origin, r_refdef.view.origin, temp);
5444 rating = (DotProduct(temp, r_refdef.view.forward) / sqrt(DotProduct(temp, temp)));
5447 rating /= (1 + 0.0625f * sqrt(DotProduct(temp, temp)));
5448 if (bestrating < rating && CL_TraceLine(light->origin, r_refdef.view.origin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false).fraction == 1.0f)
5450 bestrating = rating;
5455 R_Shadow_SelectLight(best);
5458 void R_Shadow_LoadWorldLights(void)
5460 int n, a, style, shadow, flags;
5461 char tempchar, *lightsstring, *s, *t, name[MAX_QPATH], cubemapname[MAX_QPATH];
5462 float origin[3], radius, color[3], angles[3], corona, coronasizescale, ambientscale, diffusescale, specularscale;
5463 if (cl.worldmodel == NULL)
5465 Con_Print("No map loaded.\n");
5468 dpsnprintf(name, sizeof(name), "%s.rtlights", cl.worldnamenoextension);
5469 lightsstring = (char *)FS_LoadFile(name, tempmempool, false, NULL);
5479 for (;COM_Parse(t, true) && strcmp(
5480 if (COM_Parse(t, true))
5482 if (com_token[0] == '!')
5485 origin[0] = atof(com_token+1);
5488 origin[0] = atof(com_token);
5493 while (*s && *s != '\n' && *s != '\r')
5499 // check for modifier flags
5506 #if _MSC_VER >= 1400
5507 #define sscanf sscanf_s
5509 cubemapname[sizeof(cubemapname)-1] = 0;
5510 #if MAX_QPATH != 128
5511 #error update this code if MAX_QPATH changes
5513 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
5514 #if _MSC_VER >= 1400
5515 , sizeof(cubemapname)
5517 , &corona, &angles[0], &angles[1], &angles[2], &coronasizescale, &ambientscale, &diffusescale, &specularscale, &flags);
5520 flags = LIGHTFLAG_REALTIMEMODE;
5528 coronasizescale = 0.25f;
5530 VectorClear(angles);
5533 if (a < 9 || !strcmp(cubemapname, "\"\""))
5535 // remove quotes on cubemapname
5536 if (cubemapname[0] == '"' && cubemapname[strlen(cubemapname) - 1] == '"')
5539 namelen = strlen(cubemapname) - 2;
5540 memmove(cubemapname, cubemapname + 1, namelen);
5541 cubemapname[namelen] = '\0';
5545 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);
5548 R_Shadow_UpdateWorldLight(R_Shadow_NewWorldLight(), origin, angles, color, radius, corona, style, shadow, cubemapname, coronasizescale, ambientscale, diffusescale, specularscale, flags);
5556 Con_Printf("invalid rtlights file \"%s\"\n", name);
5557 Mem_Free(lightsstring);
5561 void R_Shadow_SaveWorldLights(void)
5565 size_t bufchars, bufmaxchars;
5567 char name[MAX_QPATH];
5568 char line[MAX_INPUTLINE];
5569 size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked, assuming the dpsnprintf mess doesn't screw it up...
5570 // I hate lines which are 3 times my screen size :( --blub
5573 if (cl.worldmodel == NULL)
5575 Con_Print("No map loaded.\n");
5578 dpsnprintf(name, sizeof(name), "%s.rtlights", cl.worldnamenoextension);
5579 bufchars = bufmaxchars = 0;
5581 for (lightindex = 0;lightindex < range;lightindex++)
5583 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
5586 if (light->coronasizescale != 0.25f || light->ambientscale != 0 || light->diffusescale != 1 || light->specularscale != 1 || light->flags != LIGHTFLAG_REALTIMEMODE)
5587 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);
5588 else if (light->cubemapname[0] || light->corona || light->angles[0] || light->angles[1] || light->angles[2])
5589 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]);
5591 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);
5592 if (bufchars + strlen(line) > bufmaxchars)
5594 bufmaxchars = bufchars + strlen(line) + 2048;
5596 buf = (char *)Mem_Alloc(tempmempool, bufmaxchars);
5600 memcpy(buf, oldbuf, bufchars);
5606 memcpy(buf + bufchars, line, strlen(line));
5607 bufchars += strlen(line);
5611 FS_WriteFile(name, buf, (fs_offset_t)bufchars);
5616 void R_Shadow_LoadLightsFile(void)
5619 char tempchar, *lightsstring, *s, *t, name[MAX_QPATH];
5620 float origin[3], radius, color[3], subtract, spotdir[3], spotcone, falloff, distbias;
5621 if (cl.worldmodel == NULL)
5623 Con_Print("No map loaded.\n");
5626 dpsnprintf(name, sizeof(name), "%s.lights", cl.worldnamenoextension);
5627 lightsstring = (char *)FS_LoadFile(name, tempmempool, false, NULL);
5635 while (*s && *s != '\n' && *s != '\r')
5641 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);
5645 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);
5648 radius = sqrt(DotProduct(color, color) / (falloff * falloff * 8192.0f * 8192.0f));
5649 radius = bound(15, radius, 4096);
5650 VectorScale(color, (2.0f / (8388608.0f)), color);
5651 R_Shadow_UpdateWorldLight(R_Shadow_NewWorldLight(), origin, vec3_origin, color, radius, 0, style, true, NULL, 0.25, 0, 1, 1, LIGHTFLAG_REALTIMEMODE);
5659 Con_Printf("invalid lights file \"%s\"\n", name);
5660 Mem_Free(lightsstring);
5664 // tyrlite/hmap2 light types in the delay field
5665 typedef enum lighttype_e {LIGHTTYPE_MINUSX, LIGHTTYPE_RECIPX, LIGHTTYPE_RECIPXX, LIGHTTYPE_NONE, LIGHTTYPE_SUN, LIGHTTYPE_MINUSXX} lighttype_t;
5667 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void)
5679 float origin[3], angles[3], radius, color[3], light[4], fadescale, lightscale, originhack[3], overridecolor[3], vec[4];
5680 char key[256], value[MAX_INPUTLINE];
5682 if (cl.worldmodel == NULL)
5684 Con_Print("No map loaded.\n");
5687 // try to load a .ent file first
5688 dpsnprintf(key, sizeof(key), "%s.ent", cl.worldnamenoextension);
5689 data = entfiledata = (char *)FS_LoadFile(key, tempmempool, true, NULL);
5690 // and if that is not found, fall back to the bsp file entity string
5692 data = cl.worldmodel->brush.entities;
5695 for (entnum = 0;COM_ParseToken_Simple(&data, false, false) && com_token[0] == '{';entnum++)
5697 type = LIGHTTYPE_MINUSX;
5698 origin[0] = origin[1] = origin[2] = 0;
5699 originhack[0] = originhack[1] = originhack[2] = 0;
5700 angles[0] = angles[1] = angles[2] = 0;
5701 color[0] = color[1] = color[2] = 1;
5702 light[0] = light[1] = light[2] = 1;light[3] = 300;
5703 overridecolor[0] = overridecolor[1] = overridecolor[2] = 1;
5713 if (!COM_ParseToken_Simple(&data, false, false))
5715 if (com_token[0] == '}')
5716 break; // end of entity
5717 if (com_token[0] == '_')
5718 strlcpy(key, com_token + 1, sizeof(key));
5720 strlcpy(key, com_token, sizeof(key));
5721 while (key[strlen(key)-1] == ' ') // remove trailing spaces
5722 key[strlen(key)-1] = 0;
5723 if (!COM_ParseToken_Simple(&data, false, false))
5725 strlcpy(value, com_token, sizeof(value));
5727 // now that we have the key pair worked out...
5728 if (!strcmp("light", key))
5730 n = sscanf(value, "%f %f %f %f", &vec[0], &vec[1], &vec[2], &vec[3]);
5734 light[0] = vec[0] * (1.0f / 256.0f);
5735 light[1] = vec[0] * (1.0f / 256.0f);
5736 light[2] = vec[0] * (1.0f / 256.0f);
5742 light[0] = vec[0] * (1.0f / 255.0f);
5743 light[1] = vec[1] * (1.0f / 255.0f);
5744 light[2] = vec[2] * (1.0f / 255.0f);
5748 else if (!strcmp("delay", key))
5750 else if (!strcmp("origin", key))
5751 sscanf(value, "%f %f %f", &origin[0], &origin[1], &origin[2]);
5752 else if (!strcmp("angle", key))
5753 angles[0] = 0, angles[1] = atof(value), angles[2] = 0;
5754 else if (!strcmp("angles", key))
5755 sscanf(value, "%f %f %f", &angles[0], &angles[1], &angles[2]);
5756 else if (!strcmp("color", key))
5757 sscanf(value, "%f %f %f", &color[0], &color[1], &color[2]);
5758 else if (!strcmp("wait", key))
5759 fadescale = atof(value);
5760 else if (!strcmp("classname", key))
5762 if (!strncmp(value, "light", 5))
5765 if (!strcmp(value, "light_fluoro"))
5770 overridecolor[0] = 1;
5771 overridecolor[1] = 1;
5772 overridecolor[2] = 1;
5774 if (!strcmp(value, "light_fluorospark"))
5779 overridecolor[0] = 1;
5780 overridecolor[1] = 1;
5781 overridecolor[2] = 1;
5783 if (!strcmp(value, "light_globe"))
5788 overridecolor[0] = 1;
5789 overridecolor[1] = 0.8;
5790 overridecolor[2] = 0.4;
5792 if (!strcmp(value, "light_flame_large_yellow"))
5797 overridecolor[0] = 1;
5798 overridecolor[1] = 0.5;
5799 overridecolor[2] = 0.1;
5801 if (!strcmp(value, "light_flame_small_yellow"))
5806 overridecolor[0] = 1;
5807 overridecolor[1] = 0.5;
5808 overridecolor[2] = 0.1;
5810 if (!strcmp(value, "light_torch_small_white"))
5815 overridecolor[0] = 1;
5816 overridecolor[1] = 0.5;
5817 overridecolor[2] = 0.1;
5819 if (!strcmp(value, "light_torch_small_walltorch"))
5824 overridecolor[0] = 1;
5825 overridecolor[1] = 0.5;
5826 overridecolor[2] = 0.1;
5830 else if (!strcmp("style", key))
5831 style = atoi(value);
5832 else if (!strcmp("skin", key))
5833 skin = (int)atof(value);
5834 else if (!strcmp("pflags", key))
5835 pflags = (int)atof(value);
5836 //else if (!strcmp("effects", key))
5837 // effects = (int)atof(value);
5838 else if (cl.worldmodel->type == mod_brushq3)
5840 if (!strcmp("scale", key))
5841 lightscale = atof(value);
5842 if (!strcmp("fade", key))
5843 fadescale = atof(value);
5848 if (lightscale <= 0)
5852 if (color[0] == color[1] && color[0] == color[2])
5854 color[0] *= overridecolor[0];
5855 color[1] *= overridecolor[1];
5856 color[2] *= overridecolor[2];
5858 radius = light[3] * r_editlights_quakelightsizescale.value * lightscale / fadescale;
5859 color[0] = color[0] * light[0];
5860 color[1] = color[1] * light[1];
5861 color[2] = color[2] * light[2];
5864 case LIGHTTYPE_MINUSX:
5866 case LIGHTTYPE_RECIPX:
5868 VectorScale(color, (1.0f / 16.0f), color);
5870 case LIGHTTYPE_RECIPXX:
5872 VectorScale(color, (1.0f / 16.0f), color);
5875 case LIGHTTYPE_NONE:
5879 case LIGHTTYPE_MINUSXX:
5882 VectorAdd(origin, originhack, origin);
5884 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);
5887 Mem_Free(entfiledata);
5891 void R_Shadow_SetCursorLocationForView(void)
5894 vec3_t dest, endpos;
5896 VectorMA(r_refdef.view.origin, r_editlights_cursordistance.value, r_refdef.view.forward, dest);
5897 trace = CL_TraceLine(r_refdef.view.origin, dest, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false);
5898 if (trace.fraction < 1)
5900 dist = trace.fraction * r_editlights_cursordistance.value;
5901 push = r_editlights_cursorpushback.value;
5905 VectorMA(trace.endpos, push, r_refdef.view.forward, endpos);
5906 VectorMA(endpos, r_editlights_cursorpushoff.value, trace.plane.normal, endpos);
5910 VectorClear( endpos );
5912 r_editlights_cursorlocation[0] = floor(endpos[0] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
5913 r_editlights_cursorlocation[1] = floor(endpos[1] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
5914 r_editlights_cursorlocation[2] = floor(endpos[2] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
5917 void R_Shadow_UpdateWorldLightSelection(void)
5919 if (r_editlights.integer)
5921 R_Shadow_SetCursorLocationForView();
5922 R_Shadow_SelectLightInView();
5925 R_Shadow_SelectLight(NULL);
5928 void R_Shadow_EditLights_Clear_f(void)
5930 R_Shadow_ClearWorldLights();
5933 void R_Shadow_EditLights_Reload_f(void)
5937 strlcpy(r_shadow_mapname, cl.worldname, sizeof(r_shadow_mapname));
5938 R_Shadow_ClearWorldLights();
5939 R_Shadow_LoadWorldLights();
5940 if (!Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray))
5942 R_Shadow_LoadLightsFile();
5943 if (!Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray))
5944 R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
5948 void R_Shadow_EditLights_Save_f(void)
5952 R_Shadow_SaveWorldLights();
5955 void R_Shadow_EditLights_ImportLightEntitiesFromMap_f(void)
5957 R_Shadow_ClearWorldLights();
5958 R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
5961 void R_Shadow_EditLights_ImportLightsFile_f(void)
5963 R_Shadow_ClearWorldLights();
5964 R_Shadow_LoadLightsFile();
5967 void R_Shadow_EditLights_Spawn_f(void)
5970 if (!r_editlights.integer)
5972 Con_Print("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
5975 if (Cmd_Argc() != 1)
5977 Con_Print("r_editlights_spawn does not take parameters\n");
5980 color[0] = color[1] = color[2] = 1;
5981 R_Shadow_UpdateWorldLight(R_Shadow_NewWorldLight(), r_editlights_cursorlocation, vec3_origin, color, 200, 0, 0, true, NULL, 0.25, 0, 1, 1, LIGHTFLAG_REALTIMEMODE);
5984 void R_Shadow_EditLights_Edit_f(void)
5986 vec3_t origin, angles, color;
5987 vec_t radius, corona, coronasizescale, ambientscale, diffusescale, specularscale;
5988 int style, shadows, flags, normalmode, realtimemode;
5989 char cubemapname[MAX_INPUTLINE];
5990 if (!r_editlights.integer)
5992 Con_Print("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
5995 if (!r_shadow_selectedlight)
5997 Con_Print("No selected light.\n");
6000 VectorCopy(r_shadow_selectedlight->origin, origin);
6001 VectorCopy(r_shadow_selectedlight->angles, angles);
6002 VectorCopy(r_shadow_selectedlight->color, color);
6003 radius = r_shadow_selectedlight->radius;
6004 style = r_shadow_selectedlight->style;
6005 if (r_shadow_selectedlight->cubemapname)
6006 strlcpy(cubemapname, r_shadow_selectedlight->cubemapname, sizeof(cubemapname));
6009 shadows = r_shadow_selectedlight->shadow;
6010 corona = r_shadow_selectedlight->corona;
6011 coronasizescale = r_shadow_selectedlight->coronasizescale;
6012 ambientscale = r_shadow_selectedlight->ambientscale;
6013 diffusescale = r_shadow_selectedlight->diffusescale;
6014 specularscale = r_shadow_selectedlight->specularscale;
6015 flags = r_shadow_selectedlight->flags;
6016 normalmode = (flags & LIGHTFLAG_NORMALMODE) != 0;
6017 realtimemode = (flags & LIGHTFLAG_REALTIMEMODE) != 0;
6018 if (!strcmp(Cmd_Argv(1), "origin"))
6020 if (Cmd_Argc() != 5)
6022 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(1));
6025 origin[0] = atof(Cmd_Argv(2));
6026 origin[1] = atof(Cmd_Argv(3));
6027 origin[2] = atof(Cmd_Argv(4));
6029 else if (!strcmp(Cmd_Argv(1), "originx"))
6031 if (Cmd_Argc() != 3)
6033 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
6036 origin[0] = atof(Cmd_Argv(2));
6038 else if (!strcmp(Cmd_Argv(1), "originy"))
6040 if (Cmd_Argc() != 3)
6042 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
6045 origin[1] = atof(Cmd_Argv(2));
6047 else if (!strcmp(Cmd_Argv(1), "originz"))
6049 if (Cmd_Argc() != 3)
6051 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
6054 origin[2] = atof(Cmd_Argv(2));
6056 else if (!strcmp(Cmd_Argv(1), "move"))
6058 if (Cmd_Argc() != 5)
6060 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(1));
6063 origin[0] += atof(Cmd_Argv(2));
6064 origin[1] += atof(Cmd_Argv(3));
6065 origin[2] += atof(Cmd_Argv(4));
6067 else if (!strcmp(Cmd_Argv(1), "movex"))
6069 if (Cmd_Argc() != 3)
6071 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
6074 origin[0] += atof(Cmd_Argv(2));
6076 else if (!strcmp(Cmd_Argv(1), "movey"))
6078 if (Cmd_Argc() != 3)
6080 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
6083 origin[1] += atof(Cmd_Argv(2));
6085 else if (!strcmp(Cmd_Argv(1), "movez"))
6087 if (Cmd_Argc() != 3)
6089 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
6092 origin[2] += atof(Cmd_Argv(2));
6094 else if (!strcmp(Cmd_Argv(1), "angles"))
6096 if (Cmd_Argc() != 5)
6098 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(1));
6101 angles[0] = atof(Cmd_Argv(2));
6102 angles[1] = atof(Cmd_Argv(3));
6103 angles[2] = atof(Cmd_Argv(4));
6105 else if (!strcmp(Cmd_Argv(1), "anglesx"))
6107 if (Cmd_Argc() != 3)
6109 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
6112 angles[0] = atof(Cmd_Argv(2));
6114 else if (!strcmp(Cmd_Argv(1), "anglesy"))
6116 if (Cmd_Argc() != 3)
6118 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
6121 angles[1] = atof(Cmd_Argv(2));
6123 else if (!strcmp(Cmd_Argv(1), "anglesz"))
6125 if (Cmd_Argc() != 3)
6127 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
6130 angles[2] = atof(Cmd_Argv(2));
6132 else if (!strcmp(Cmd_Argv(1), "color"))
6134 if (Cmd_Argc() != 5)
6136 Con_Printf("usage: r_editlights_edit %s red green blue\n", Cmd_Argv(1));
6139 color[0] = atof(Cmd_Argv(2));
6140 color[1] = atof(Cmd_Argv(3));
6141 color[2] = atof(Cmd_Argv(4));
6143 else if (!strcmp(Cmd_Argv(1), "radius"))
6145 if (Cmd_Argc() != 3)
6147 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
6150 radius = atof(Cmd_Argv(2));
6152 else if (!strcmp(Cmd_Argv(1), "colorscale"))
6154 if (Cmd_Argc() == 3)
6156 double scale = atof(Cmd_Argv(2));
6163 if (Cmd_Argc() != 5)
6165 Con_Printf("usage: r_editlights_edit %s red green blue (OR grey instead of red green blue)\n", Cmd_Argv(1));
6168 color[0] *= atof(Cmd_Argv(2));
6169 color[1] *= atof(Cmd_Argv(3));
6170 color[2] *= atof(Cmd_Argv(4));
6173 else if (!strcmp(Cmd_Argv(1), "radiusscale") || !strcmp(Cmd_Argv(1), "sizescale"))
6175 if (Cmd_Argc() != 3)
6177 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
6180 radius *= atof(Cmd_Argv(2));
6182 else if (!strcmp(Cmd_Argv(1), "style"))
6184 if (Cmd_Argc() != 3)
6186 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
6189 style = atoi(Cmd_Argv(2));
6191 else if (!strcmp(Cmd_Argv(1), "cubemap"))
6195 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
6198 if (Cmd_Argc() == 3)
6199 strlcpy(cubemapname, Cmd_Argv(2), sizeof(cubemapname));
6203 else if (!strcmp(Cmd_Argv(1), "shadows"))
6205 if (Cmd_Argc() != 3)
6207 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
6210 shadows = Cmd_Argv(2)[0] == 'y' || Cmd_Argv(2)[0] == 'Y' || Cmd_Argv(2)[0] == 't' || atoi(Cmd_Argv(2));
6212 else if (!strcmp(Cmd_Argv(1), "corona"))
6214 if (Cmd_Argc() != 3)
6216 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
6219 corona = atof(Cmd_Argv(2));
6221 else if (!strcmp(Cmd_Argv(1), "coronasize"))
6223 if (Cmd_Argc() != 3)
6225 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
6228 coronasizescale = atof(Cmd_Argv(2));
6230 else if (!strcmp(Cmd_Argv(1), "ambient"))
6232 if (Cmd_Argc() != 3)
6234 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
6237 ambientscale = atof(Cmd_Argv(2));
6239 else if (!strcmp(Cmd_Argv(1), "diffuse"))
6241 if (Cmd_Argc() != 3)
6243 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
6246 diffusescale = atof(Cmd_Argv(2));
6248 else if (!strcmp(Cmd_Argv(1), "specular"))
6250 if (Cmd_Argc() != 3)
6252 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
6255 specularscale = atof(Cmd_Argv(2));
6257 else if (!strcmp(Cmd_Argv(1), "normalmode"))
6259 if (Cmd_Argc() != 3)
6261 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
6264 normalmode = Cmd_Argv(2)[0] == 'y' || Cmd_Argv(2)[0] == 'Y' || Cmd_Argv(2)[0] == 't' || atoi(Cmd_Argv(2));
6266 else if (!strcmp(Cmd_Argv(1), "realtimemode"))
6268 if (Cmd_Argc() != 3)
6270 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
6273 realtimemode = Cmd_Argv(2)[0] == 'y' || Cmd_Argv(2)[0] == 'Y' || Cmd_Argv(2)[0] == 't' || atoi(Cmd_Argv(2));
6277 Con_Print("usage: r_editlights_edit [property] [value]\n");
6278 Con_Print("Selected light's properties:\n");
6279 Con_Printf("Origin : %f %f %f\n", r_shadow_selectedlight->origin[0], r_shadow_selectedlight->origin[1], r_shadow_selectedlight->origin[2]);
6280 Con_Printf("Angles : %f %f %f\n", r_shadow_selectedlight->angles[0], r_shadow_selectedlight->angles[1], r_shadow_selectedlight->angles[2]);
6281 Con_Printf("Color : %f %f %f\n", r_shadow_selectedlight->color[0], r_shadow_selectedlight->color[1], r_shadow_selectedlight->color[2]);
6282 Con_Printf("Radius : %f\n", r_shadow_selectedlight->radius);
6283 Con_Printf("Corona : %f\n", r_shadow_selectedlight->corona);
6284 Con_Printf("Style : %i\n", r_shadow_selectedlight->style);
6285 Con_Printf("Shadows : %s\n", r_shadow_selectedlight->shadow ? "yes" : "no");
6286 Con_Printf("Cubemap : %s\n", r_shadow_selectedlight->cubemapname);
6287 Con_Printf("CoronaSize : %f\n", r_shadow_selectedlight->coronasizescale);
6288 Con_Printf("Ambient : %f\n", r_shadow_selectedlight->ambientscale);
6289 Con_Printf("Diffuse : %f\n", r_shadow_selectedlight->diffusescale);
6290 Con_Printf("Specular : %f\n", r_shadow_selectedlight->specularscale);
6291 Con_Printf("NormalMode : %s\n", (r_shadow_selectedlight->flags & LIGHTFLAG_NORMALMODE) ? "yes" : "no");
6292 Con_Printf("RealTimeMode : %s\n", (r_shadow_selectedlight->flags & LIGHTFLAG_REALTIMEMODE) ? "yes" : "no");
6295 flags = (normalmode ? LIGHTFLAG_NORMALMODE : 0) | (realtimemode ? LIGHTFLAG_REALTIMEMODE : 0);
6296 R_Shadow_UpdateWorldLight(r_shadow_selectedlight, origin, angles, color, radius, corona, style, shadows, cubemapname, coronasizescale, ambientscale, diffusescale, specularscale, flags);
6299 void R_Shadow_EditLights_EditAll_f(void)
6302 dlight_t *light, *oldselected;
6305 if (!r_editlights.integer)
6307 Con_Print("Cannot edit lights when not in editing mode. Set r_editlights to 1.\n");
6311 oldselected = r_shadow_selectedlight;
6312 // EditLights doesn't seem to have a "remove" command or something so:
6313 range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
6314 for (lightindex = 0;lightindex < range;lightindex++)
6316 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
6319 R_Shadow_SelectLight(light);
6320 R_Shadow_EditLights_Edit_f();
6322 // return to old selected (to not mess editing once selection is locked)
6323 R_Shadow_SelectLight(oldselected);
6326 void R_Shadow_EditLights_DrawSelectedLightProperties(void)
6328 int lightnumber, lightcount;
6329 size_t lightindex, range;
6333 if (!r_editlights.integer)
6335 x = vid_conwidth.value - 240;
6337 DrawQ_Pic(x-5, y-5, NULL, 250, 155, 0, 0, 0, 0.75, 0);
6340 range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
6341 for (lightindex = 0;lightindex < range;lightindex++)
6343 light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
6346 if (light == r_shadow_selectedlight)
6347 lightnumber = lightindex;
6350 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, FONT_DEFAULT);y += 8;
6351 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, FONT_DEFAULT);y += 8;
6353 if (r_shadow_selectedlight == NULL)
6355 dpsnprintf(temp, sizeof(temp), "Light #%i properties:", lightnumber);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true, FONT_DEFAULT);y += 8;
6356 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, FONT_DEFAULT);y += 8;
6357 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, FONT_DEFAULT);y += 8;
6358 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, FONT_DEFAULT);y += 8;
6359 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, FONT_DEFAULT);y += 8;
6360 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, FONT_DEFAULT);y += 8;
6361 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, FONT_DEFAULT);y += 8;
6362 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, FONT_DEFAULT);y += 8;
6363 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, FONT_DEFAULT);y += 8;
6364 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, FONT_DEFAULT);y += 8;
6365 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, FONT_DEFAULT);y += 8;
6366 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, FONT_DEFAULT);y += 8;
6367 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, FONT_DEFAULT);y += 8;
6368 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, FONT_DEFAULT);y += 8;
6369 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, FONT_DEFAULT);y += 8;
6372 void R_Shadow_EditLights_ToggleShadow_f(void)
6374 if (!r_editlights.integer)
6376 Con_Print("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
6379 if (!r_shadow_selectedlight)
6381 Con_Print("No selected light.\n");
6384 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);
6387 void R_Shadow_EditLights_ToggleCorona_f(void)
6389 if (!r_editlights.integer)
6391 Con_Print("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
6394 if (!r_shadow_selectedlight)
6396 Con_Print("No selected light.\n");
6399 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);
6402 void R_Shadow_EditLights_Remove_f(void)
6404 if (!r_editlights.integer)
6406 Con_Print("Cannot remove light when not in editing mode. Set r_editlights to 1.\n");
6409 if (!r_shadow_selectedlight)
6411 Con_Print("No selected light.\n");
6414 R_Shadow_FreeWorldLight(r_shadow_selectedlight);
6415 r_shadow_selectedlight = NULL;
6418 void R_Shadow_EditLights_Help_f(void)
6421 "Documentation on r_editlights system:\n"
6423 "r_editlights : enable/disable editing mode\n"
6424 "r_editlights_cursordistance : maximum distance of cursor from eye\n"
6425 "r_editlights_cursorpushback : push back cursor this far from surface\n"
6426 "r_editlights_cursorpushoff : push cursor off surface this far\n"
6427 "r_editlights_cursorgrid : snap cursor to grid of this size\n"
6428 "r_editlights_quakelightsizescale : imported quake light entity size scaling\n"
6430 "r_editlights_help : this help\n"
6431 "r_editlights_clear : remove all lights\n"
6432 "r_editlights_reload : reload .rtlights, .lights file, or entities\n"
6433 "r_editlights_lock : lock selection to current light, if already locked - unlock\n"
6434 "r_editlights_save : save to .rtlights file\n"
6435 "r_editlights_spawn : create a light with default settings\n"
6436 "r_editlights_edit command : edit selected light - more documentation below\n"
6437 "r_editlights_remove : remove selected light\n"
6438 "r_editlights_toggleshadow : toggles on/off selected light's shadow property\n"
6439 "r_editlights_importlightentitiesfrommap : reload light entities\n"
6440 "r_editlights_importlightsfile : reload .light file (produced by hlight)\n"
6442 "origin x y z : set light location\n"
6443 "originx x: set x component of light location\n"
6444 "originy y: set y component of light location\n"
6445 "originz z: set z component of light location\n"
6446 "move x y z : adjust light location\n"
6447 "movex x: adjust x component of light location\n"
6448 "movey y: adjust y component of light location\n"
6449 "movez z: adjust z component of light location\n"
6450 "angles x y z : set light angles\n"
6451 "anglesx x: set x component of light angles\n"
6452 "anglesy y: set y component of light angles\n"
6453 "anglesz z: set z component of light angles\n"
6454 "color r g b : set color of light (can be brighter than 1 1 1)\n"
6455 "radius radius : set radius (size) of light\n"
6456 "colorscale grey : multiply color of light (1 does nothing)\n"
6457 "colorscale r g b : multiply color of light (1 1 1 does nothing)\n"
6458 "radiusscale scale : multiply radius (size) of light (1 does nothing)\n"
6459 "sizescale scale : multiply radius (size) of light (1 does nothing)\n"
6460 "style style : set lightstyle of light (flickering patterns, switches, etc)\n"
6461 "cubemap basename : set filter cubemap of light (not yet supported)\n"
6462 "shadows 1/0 : turn on/off shadows\n"
6463 "corona n : set corona intensity\n"
6464 "coronasize n : set corona size (0-1)\n"
6465 "ambient n : set ambient intensity (0-1)\n"
6466 "diffuse n : set diffuse intensity (0-1)\n"
6467 "specular n : set specular intensity (0-1)\n"
6468 "normalmode 1/0 : turn on/off rendering of this light in rtworld 0 mode\n"
6469 "realtimemode 1/0 : turn on/off rendering of this light in rtworld 1 mode\n"
6470 "<nothing> : print light properties to console\n"
6474 void R_Shadow_EditLights_CopyInfo_f(void)
6476 if (!r_editlights.integer)
6478 Con_Print("Cannot copy light info when not in editing mode. Set r_editlights to 1.\n");
6481 if (!r_shadow_selectedlight)
6483 Con_Print("No selected light.\n");
6486 VectorCopy(r_shadow_selectedlight->angles, r_shadow_bufferlight.angles);
6487 VectorCopy(r_shadow_selectedlight->color, r_shadow_bufferlight.color);
6488 r_shadow_bufferlight.radius = r_shadow_selectedlight->radius;
6489 r_shadow_bufferlight.style = r_shadow_selectedlight->style;
6490 if (r_shadow_selectedlight->cubemapname)
6491 strlcpy(r_shadow_bufferlight.cubemapname, r_shadow_selectedlight->cubemapname, sizeof(r_shadow_bufferlight.cubemapname));
6493 r_shadow_bufferlight.cubemapname[0] = 0;
6494 r_shadow_bufferlight.shadow = r_shadow_selectedlight->shadow;
6495 r_shadow_bufferlight.corona = r_shadow_selectedlight->corona;
6496 r_shadow_bufferlight.coronasizescale = r_shadow_selectedlight->coronasizescale;
6497 r_shadow_bufferlight.ambientscale = r_shadow_selectedlight->ambientscale;
6498 r_shadow_bufferlight.diffusescale = r_shadow_selectedlight->diffusescale;
6499 r_shadow_bufferlight.specularscale = r_shadow_selectedlight->specularscale;
6500 r_shadow_bufferlight.flags = r_shadow_selectedlight->flags;
6503 void R_Shadow_EditLights_PasteInfo_f(void)
6505 if (!r_editlights.integer)
6507 Con_Print("Cannot paste light info when not in editing mode. Set r_editlights to 1.\n");
6510 if (!r_shadow_selectedlight)
6512 Con_Print("No selected light.\n");
6515 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);
6518 void R_Shadow_EditLights_Lock_f(void)
6520 if (!r_editlights.integer)
6522 Con_Print("Cannot lock on light when not in editing mode. Set r_editlights to 1.\n");
6525 if (r_editlights_lockcursor)
6527 r_editlights_lockcursor = false;
6530 if (!r_shadow_selectedlight)
6532 Con_Print("No selected light to lock on.\n");
6535 r_editlights_lockcursor = true;
6538 void R_Shadow_EditLights_Init(void)
6540 Cvar_RegisterVariable(&r_editlights);
6541 Cvar_RegisterVariable(&r_editlights_cursordistance);
6542 Cvar_RegisterVariable(&r_editlights_cursorpushback);
6543 Cvar_RegisterVariable(&r_editlights_cursorpushoff);
6544 Cvar_RegisterVariable(&r_editlights_cursorgrid);
6545 Cvar_RegisterVariable(&r_editlights_quakelightsizescale);
6546 Cmd_AddCommand("r_editlights_help", R_Shadow_EditLights_Help_f, "prints documentation on console commands and variables in rtlight editing system");
6547 Cmd_AddCommand("r_editlights_clear", R_Shadow_EditLights_Clear_f, "removes all world lights (let there be darkness!)");
6548 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)");
6549 Cmd_AddCommand("r_editlights_save", R_Shadow_EditLights_Save_f, "save .rtlights file for current level");
6550 Cmd_AddCommand("r_editlights_spawn", R_Shadow_EditLights_Spawn_f, "creates a light with default properties (let there be light!)");
6551 Cmd_AddCommand("r_editlights_edit", R_Shadow_EditLights_Edit_f, "changes a property on the selected light");
6552 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)");
6553 Cmd_AddCommand("r_editlights_remove", R_Shadow_EditLights_Remove_f, "remove selected light");
6554 Cmd_AddCommand("r_editlights_toggleshadow", R_Shadow_EditLights_ToggleShadow_f, "toggle on/off the shadow option on the selected light");
6555 Cmd_AddCommand("r_editlights_togglecorona", R_Shadow_EditLights_ToggleCorona_f, "toggle on/off the corona option on the selected light");
6556 Cmd_AddCommand("r_editlights_importlightentitiesfrommap", R_Shadow_EditLights_ImportLightEntitiesFromMap_f, "load lights from .ent file or map entities (ignoring .rtlights or .lights file)");
6557 Cmd_AddCommand("r_editlights_importlightsfile", R_Shadow_EditLights_ImportLightsFile_f, "load lights from .lights file (ignoring .rtlights or .ent files and map entities)");
6558 Cmd_AddCommand("r_editlights_copyinfo", R_Shadow_EditLights_CopyInfo_f, "store a copy of all properties (except origin) of the selected light");
6559 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)");
6560 Cmd_AddCommand("r_editlights_lock", R_Shadow_EditLights_Lock_f, "lock selection to current light, if already locked - unlock");
6566 =============================================================================
6570 =============================================================================
6573 void R_CompleteLightPoint(vec3_t ambient, vec3_t diffuse, vec3_t lightdir, const vec3_t p, const int flags)
6575 int i, numlights, flag;
6578 float relativepoint[3];
6587 if (r_fullbright.integer)
6589 VectorSet(ambient, 1, 1, 1);
6590 VectorClear(diffuse);
6591 VectorClear(lightdir);
6595 if (flags & LP_LIGHTMAP)
6597 VectorSet(ambient, r_refdef.scene.ambient, r_refdef.scene.ambient, r_refdef.scene.ambient);
6598 VectorClear(diffuse);
6599 VectorClear(lightdir);
6600 if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.LightPoint)
6601 r_refdef.scene.worldmodel->brush.LightPoint(r_refdef.scene.worldmodel, p, ambient, diffuse, lightdir);
6605 memset(sample, 0, sizeof(sample));
6606 VectorSet(sample, r_refdef.scene.ambient, r_refdef.scene.ambient, r_refdef.scene.ambient);
6608 if ((flags & LP_LIGHTMAP) && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.LightPoint)
6611 VectorClear(tempambient);
6613 VectorClear(relativepoint);
6614 r_refdef.scene.worldmodel->brush.LightPoint(r_refdef.scene.worldmodel, p, tempambient, color, relativepoint);
6615 VectorScale(tempambient, r_refdef.lightmapintensity, tempambient);
6616 VectorScale(color, r_refdef.lightmapintensity, color);
6617 VectorAdd(sample, tempambient, sample);
6618 VectorMA(sample , 0.5f , color, sample );
6619 VectorMA(sample + 3, relativepoint[0], color, sample + 3);
6620 VectorMA(sample + 6, relativepoint[1], color, sample + 6);
6621 VectorMA(sample + 9, relativepoint[2], color, sample + 9);
6622 // calculate a weighted average light direction as well
6623 intensity = VectorLength(color);
6624 VectorMA(sample + 12, intensity, relativepoint, sample + 12);
6627 if (flags & LP_RTWORLD)
6629 flag = r_refdef.scene.rtworld ? LIGHTFLAG_REALTIMEMODE : LIGHTFLAG_NORMALMODE;
6630 numlights = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray);
6631 for (i = 0; i < numlights; i++)
6633 dlight = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, i);
6636 light = &dlight->rtlight;
6637 if (!(light->flags & flag))
6640 lightradius2 = light->radius * light->radius;
6641 VectorSubtract(light->shadoworigin, p, relativepoint);
6642 dist2 = VectorLength2(relativepoint);
6643 if (dist2 >= lightradius2)
6645 dist = sqrt(dist2) / light->radius;
6646 intensity = min(1.0f, (1.0f - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist)) * r_shadow_lightintensityscale.value;
6647 if (intensity <= 0.0f)
6649 if (light->shadow && CL_TraceLine(p, light->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false).fraction < 1)
6651 // scale down intensity to add to both ambient and diffuse
6652 //intensity *= 0.5f;
6653 VectorNormalize(relativepoint);
6654 VectorScale(light->currentcolor, intensity, color);
6655 VectorMA(sample , 0.5f , color, sample );
6656 VectorMA(sample + 3, relativepoint[0], color, sample + 3);
6657 VectorMA(sample + 6, relativepoint[1], color, sample + 6);
6658 VectorMA(sample + 9, relativepoint[2], color, sample + 9);
6659 // calculate a weighted average light direction as well
6660 intensity *= VectorLength(color);
6661 VectorMA(sample + 12, intensity, relativepoint, sample + 12);
6665 if (flags & LP_DYNLIGHT)
6668 for (i = 0;i < r_refdef.scene.numlights;i++)
6670 light = r_refdef.scene.lights[i];
6672 lightradius2 = light->radius * light->radius;
6673 VectorSubtract(light->shadoworigin, p, relativepoint);
6674 dist2 = VectorLength2(relativepoint);
6675 if (dist2 >= lightradius2)
6677 dist = sqrt(dist2) / light->radius;
6678 intensity = (1.0f - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist) * r_shadow_lightintensityscale.value;
6679 if (intensity <= 0.0f)
6681 if (light->shadow && CL_TraceLine(p, light->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false).fraction < 1)
6683 // scale down intensity to add to both ambient and diffuse
6684 //intensity *= 0.5f;
6685 VectorNormalize(relativepoint);
6686 VectorScale(light->currentcolor, intensity, color);
6687 VectorMA(sample , 0.5f , color, sample );
6688 VectorMA(sample + 3, relativepoint[0], color, sample + 3);
6689 VectorMA(sample + 6, relativepoint[1], color, sample + 6);
6690 VectorMA(sample + 9, relativepoint[2], color, sample + 9);
6691 // calculate a weighted average light direction as well
6692 intensity *= VectorLength(color);
6693 VectorMA(sample + 12, intensity, relativepoint, sample + 12);
6697 // calculate the direction we'll use to reduce the sample to a directional light source
6698 VectorCopy(sample + 12, dir);
6699 //VectorSet(dir, sample[3] + sample[4] + sample[5], sample[6] + sample[7] + sample[8], sample[9] + sample[10] + sample[11]);
6700 VectorNormalize(dir);
6701 // extract the diffuse color along the chosen direction and scale it
6702 diffuse[0] = (dir[0]*sample[3] + dir[1]*sample[6] + dir[2]*sample[ 9] + sample[ 0]);
6703 diffuse[1] = (dir[0]*sample[4] + dir[1]*sample[7] + dir[2]*sample[10] + sample[ 1]);
6704 diffuse[2] = (dir[0]*sample[5] + dir[1]*sample[8] + dir[2]*sample[11] + sample[ 2]);
6705 // subtract some of diffuse from ambient
6706 VectorMA(sample, -0.333f, diffuse, ambient);
6707 // store the normalized lightdir
6708 VectorCopy(dir, lightdir);