3 Terminology: Stencil Shadow Volume (sometimes called Stencil Shadows)
4 An extrusion of the lit faces, beginning at the original geometry and ending
5 further from the light source than the original geometry (presumably at least
6 as far as the light's radius, if the light has a radius at all), capped at
7 both front and back to avoid any problems (extrusion from dark faces also
8 works but has a different set of problems)
10 This is normally rendered using Carmack's Reverse technique, in which
11 backfaces behind zbuffer (zfail) increment the stencil, and frontfaces behind
12 zbuffer (zfail) decrement the stencil, the result is a stencil value of zero
13 where shadows did not intersect the visible geometry, suitable as a stencil
14 mask for rendering lighting everywhere but shadow.
16 In our case to hopefully avoid the Creative Labs patent, we draw the backfaces
17 as decrement and the frontfaces as increment, and we redefine the DepthFunc to
18 GL_LESS (the patent uses GL_GEQUAL) which causes zfail when behind surfaces
19 and zpass when infront (the patent draws where zpass with a GL_GEQUAL test),
20 additionally we clear stencil to 128 to avoid the need for the unclamped
21 incr/decr extension (not related to patent).
24 This algorithm may be covered by Creative's patent (US Patent #6384822),
25 however that patent is quite specific about increment on backfaces and
26 decrement on frontfaces where zpass with GL_GEQUAL depth test, which is
27 opposite this implementation and partially opposite Carmack's Reverse paper
28 (which uses GL_LESS, but increments on backfaces and decrements on frontfaces).
32 Terminology: Stencil Light Volume (sometimes called Light Volumes)
33 Similar to a Stencil Shadow Volume, but inverted; rather than containing the
34 areas in shadow it contains the areas in light, this can only be built
35 quickly for certain limited cases (such as portal visibility from a point),
36 but is quite useful for some effects (sunlight coming from sky polygons is
37 one possible example, translucent occluders is another example).
41 Terminology: Optimized Stencil Shadow Volume
42 A Stencil Shadow Volume that has been processed sufficiently to ensure it has
43 no duplicate coverage of areas (no need to shadow an area twice), often this
44 greatly improves performance but is an operation too costly to use on moving
45 lights (however completely optimal Stencil Light Volumes can be constructed
50 Terminology: Per Pixel Lighting (sometimes abbreviated PPL)
51 Per pixel evaluation of lighting equations, at a bare minimum this involves
52 DOT3 shading of diffuse lighting (per pixel dotproduct of negated incidence
53 vector and surface normal, using a texture of the surface bumps, called a
54 NormalMap) if supported by hardware; in our case there is support for cards
55 which are incapable of DOT3, the quality is quite poor however. Additionally
56 it is desirable to have specular evaluation per pixel, per vertex
57 normalization of specular halfangle vectors causes noticable distortion but
58 is unavoidable on hardware without GL_ARB_fragment_program or
59 GL_ARB_fragment_shader.
63 Terminology: Normalization CubeMap
64 A cubemap containing normalized dot3-encoded (vectors of length 1 or less
65 encoded as RGB colors) for any possible direction, this technique allows per
66 pixel calculation of incidence vector for per pixel lighting purposes, which
67 would not otherwise be possible per pixel without GL_ARB_fragment_program or
68 GL_ARB_fragment_shader.
72 Terminology: 2D+1D Attenuation Texturing
73 A very crude approximation of light attenuation with distance which results
74 in cylindrical light shapes which fade vertically as a streak (some games
75 such as Doom3 allow this to be rotated to be less noticable in specific
76 cases), the technique is simply modulating lighting by two 2D textures (which
77 can be the same) on different axes of projection (XY and Z, typically), this
78 is the second best technique available without 3D Attenuation Texturing,
79 GL_ARB_fragment_program or GL_ARB_fragment_shader technology.
83 Terminology: 2D+1D Inverse Attenuation Texturing
84 A clever method described in papers on the Abducted engine, this has a squared
85 distance texture (bright on the outside, black in the middle), which is used
86 twice using GL_ADD blending, the result of this is used in an inverse modulate
87 (GL_ONE_MINUS_DST_ALPHA, GL_ZERO) to implement the equation
88 lighting*=(1-((X*X+Y*Y)+(Z*Z))) which is spherical (unlike 2D+1D attenuation
93 Terminology: 3D Attenuation Texturing
94 A slightly crude approximation of light attenuation with distance, its flaws
95 are limited radius and resolution (performance tradeoffs).
99 Terminology: 3D Attenuation-Normalization Texturing
100 A 3D Attenuation Texture merged with a Normalization CubeMap, by making the
101 vectors shorter the lighting becomes darker, a very effective optimization of
102 diffuse lighting if 3D Attenuation Textures are already used.
106 Terminology: Light Cubemap Filtering
107 A technique for modeling non-uniform light distribution according to
108 direction, for example a lantern may use a cubemap to describe the light
109 emission pattern of the cage around the lantern (as well as soot buildup
110 discoloring the light in certain areas), often also used for softened grate
111 shadows and light shining through a stained glass window (done crudely by
112 texturing the lighting with a cubemap), another good example would be a disco
113 light. This technique is used heavily in many games (Doom3 does not support
118 Terminology: Light Projection Filtering
119 A technique for modeling shadowing of light passing through translucent
120 surfaces, allowing stained glass windows and other effects to be done more
121 elegantly than possible with Light Cubemap Filtering by applying an occluder
122 texture to the lighting combined with a stencil light volume to limit the lit
123 area, this technique is used by Doom3 for spotlights and flashlights, among
124 other things, this can also be used more generally to render light passing
125 through multiple translucent occluders in a scene (using a light volume to
126 describe the area beyond the occluder, and thus mask off rendering of all
131 Terminology: Doom3 Lighting
132 A combination of Stencil Shadow Volume, Per Pixel Lighting, Normalization
133 CubeMap, 2D+1D Attenuation Texturing, and Light Projection Filtering, as
134 demonstrated by the game Doom3.
137 #include "quakedef.h"
138 #include "r_shadow.h"
139 #include "cl_collision.h"
143 extern void R_Shadow_EditLights_Init(void);
145 typedef enum r_shadow_rendermode_e
147 R_SHADOW_RENDERMODE_NONE,
148 R_SHADOW_RENDERMODE_STENCIL,
149 R_SHADOW_RENDERMODE_STENCILTWOSIDE,
150 R_SHADOW_RENDERMODE_LIGHT_VERTEX,
151 R_SHADOW_RENDERMODE_LIGHT_DOT3,
152 R_SHADOW_RENDERMODE_LIGHT_GLSL,
153 R_SHADOW_RENDERMODE_VISIBLEVOLUMES,
154 R_SHADOW_RENDERMODE_VISIBLELIGHTING,
156 r_shadow_rendermode_t;
158 r_shadow_rendermode_t r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
159 r_shadow_rendermode_t r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_NONE;
160 r_shadow_rendermode_t r_shadow_shadowingrendermode = R_SHADOW_RENDERMODE_NONE;
162 int maxshadowtriangles;
165 int maxshadowvertices;
166 float *shadowvertex3f;
179 int r_shadow_buffer_numleafpvsbytes;
180 unsigned char *r_shadow_buffer_leafpvs;
181 int *r_shadow_buffer_leaflist;
183 int r_shadow_buffer_numsurfacepvsbytes;
184 unsigned char *r_shadow_buffer_surfacepvs;
185 int *r_shadow_buffer_surfacelist;
187 rtexturepool_t *r_shadow_texturepool;
188 rtexture_t *r_shadow_attenuation2dtexture;
189 rtexture_t *r_shadow_attenuation3dtexture;
191 // lights are reloaded when this changes
192 char r_shadow_mapname[MAX_QPATH];
194 // used only for light filters (cubemaps)
195 rtexturepool_t *r_shadow_filters_texturepool;
197 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"};
198 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"};
199 cvar_t r_shadow_debuglight = {0, "r_shadow_debuglight", "-1", "renders only one light, for level design purposes or debugging"};
200 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)"};
201 cvar_t r_shadow_gloss2intensity = {0, "r_shadow_gloss2intensity", "0.25", "how bright the forced flat gloss should look if r_shadow_gloss is 2"};
202 cvar_t r_shadow_glossintensity = {0, "r_shadow_glossintensity", "1", "how bright textured glossmaps should look if r_shadow_gloss is 1 or 2"};
203 cvar_t r_shadow_lightattenuationpower = {0, "r_shadow_lightattenuationpower", "0.5", "changes attenuation texture generation (does not affect r_glsl lighting)"};
204 cvar_t r_shadow_lightattenuationscale = {0, "r_shadow_lightattenuationscale", "1", "changes attenuation texture generation (does not affect r_glsl lighting)"};
205 cvar_t r_shadow_lightintensityscale = {0, "r_shadow_lightintensityscale", "1", "renders all world lights brighter or darker"};
206 cvar_t r_shadow_portallight = {0, "r_shadow_portallight", "1", "use portal culling to exactly determine lit triangles when compiling world lights"};
207 cvar_t r_shadow_projectdistance = {0, "r_shadow_projectdistance", "1000000", "how far to cast shadows"};
208 cvar_t r_shadow_realtime_dlight = {CVAR_SAVE, "r_shadow_realtime_dlight", "1", "enables rendering of dynamic lights such as explosions and rocket light"};
209 cvar_t r_shadow_realtime_dlight_shadows = {CVAR_SAVE, "r_shadow_realtime_dlight_shadows", "1", "enables rendering of shadows from dynamic lights"};
210 cvar_t r_shadow_realtime_dlight_portalculling = {0, "r_shadow_realtime_dlight_portalculling", "0", "enables portal culling optimizations on dynamic lights (slow! you probably don't want this!)"};
211 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)"};
212 cvar_t r_shadow_realtime_world_dlightshadows = {CVAR_SAVE, "r_shadow_realtime_world_dlightshadows", "1", "enables shadows from dynamic lights when using full world lighting"};
213 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"};
214 cvar_t r_shadow_realtime_world_shadows = {CVAR_SAVE, "r_shadow_realtime_world_shadows", "1", "enables rendering of shadows from world lights"};
215 cvar_t r_shadow_realtime_world_compile = {0, "r_shadow_realtime_world_compile", "1", "enables compilation of world lights for higher performance rendering"};
216 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"};
217 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)"};
218 cvar_t r_shadow_shadow_polygonfactor = {0, "r_shadow_shadow_polygonfactor", "0", "how much to enlarge shadow volume polygons when rendering (should be 0!)"};
219 cvar_t r_shadow_shadow_polygonoffset = {0, "r_shadow_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)"};
220 cvar_t r_shadow_texture3d = {0, "r_shadow_texture3d", "1", "use 3D voxel textures for spherical attenuation rather than cylindrical (does not affect r_glsl lighting)"};
221 cvar_t gl_ext_stenciltwoside = {0, "gl_ext_stenciltwoside", "1", "make use of GL_EXT_stenciltwoside extension (NVIDIA only)"};
222 cvar_t r_editlights = {0, "r_editlights", "0", "enables .rtlights file editing mode"};
223 cvar_t r_editlights_cursordistance = {0, "r_editlights_cursordistance", "1024", "maximum distance of cursor from eye"};
224 cvar_t r_editlights_cursorpushback = {0, "r_editlights_cursorpushback", "0", "how far to pull the cursor back toward the eye"};
225 cvar_t r_editlights_cursorpushoff = {0, "r_editlights_cursorpushoff", "4", "how far to push the cursor off the impacted surface"};
226 cvar_t r_editlights_cursorgrid = {0, "r_editlights_cursorgrid", "4", "snaps cursor to this grid size"};
227 cvar_t r_editlights_quakelightsizescale = {CVAR_SAVE, "r_editlights_quakelightsizescale", "1", "changes size of light entities loaded from a map"};
229 float r_shadow_attenpower, r_shadow_attenscale;
231 rtlight_t *r_shadow_compilingrtlight;
232 dlight_t *r_shadow_worldlightchain;
233 dlight_t *r_shadow_selectedlight;
234 dlight_t r_shadow_bufferlight;
235 vec3_t r_editlights_cursorlocation;
237 extern int con_vislines;
239 typedef struct cubemapinfo_s
246 #define MAX_CUBEMAPS 256
247 static int numcubemaps;
248 static cubemapinfo_t cubemaps[MAX_CUBEMAPS];
250 void R_Shadow_UncompileWorldLights(void);
251 void R_Shadow_ClearWorldLights(void);
252 void R_Shadow_SaveWorldLights(void);
253 void R_Shadow_LoadWorldLights(void);
254 void R_Shadow_LoadLightsFile(void);
255 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void);
256 void R_Shadow_EditLights_Reload_f(void);
257 void R_Shadow_ValidateCvars(void);
258 static void R_Shadow_MakeTextures(void);
259 void R_Shadow_DrawWorldLightShadowVolume(matrix4x4_t *matrix, dlight_t *light);
261 void r_shadow_start(void)
263 // allocate vertex processing arrays
265 r_shadow_attenuation2dtexture = NULL;
266 r_shadow_attenuation3dtexture = NULL;
267 r_shadow_texturepool = NULL;
268 r_shadow_filters_texturepool = NULL;
269 R_Shadow_ValidateCvars();
270 R_Shadow_MakeTextures();
271 maxshadowtriangles = 0;
272 shadowelements = NULL;
273 maxshadowvertices = 0;
274 shadowvertex3f = NULL;
282 shadowmarklist = NULL;
284 r_shadow_buffer_numleafpvsbytes = 0;
285 r_shadow_buffer_leafpvs = NULL;
286 r_shadow_buffer_leaflist = NULL;
287 r_shadow_buffer_numsurfacepvsbytes = 0;
288 r_shadow_buffer_surfacepvs = NULL;
289 r_shadow_buffer_surfacelist = NULL;
292 void r_shadow_shutdown(void)
294 R_Shadow_UncompileWorldLights();
296 r_shadow_attenuation2dtexture = NULL;
297 r_shadow_attenuation3dtexture = NULL;
298 R_FreeTexturePool(&r_shadow_texturepool);
299 R_FreeTexturePool(&r_shadow_filters_texturepool);
300 maxshadowtriangles = 0;
302 Mem_Free(shadowelements);
303 shadowelements = NULL;
305 Mem_Free(shadowvertex3f);
306 shadowvertex3f = NULL;
309 Mem_Free(vertexupdate);
312 Mem_Free(vertexremap);
318 Mem_Free(shadowmark);
321 Mem_Free(shadowmarklist);
322 shadowmarklist = NULL;
324 r_shadow_buffer_numleafpvsbytes = 0;
325 if (r_shadow_buffer_leafpvs)
326 Mem_Free(r_shadow_buffer_leafpvs);
327 r_shadow_buffer_leafpvs = NULL;
328 if (r_shadow_buffer_leaflist)
329 Mem_Free(r_shadow_buffer_leaflist);
330 r_shadow_buffer_leaflist = NULL;
331 r_shadow_buffer_numsurfacepvsbytes = 0;
332 if (r_shadow_buffer_surfacepvs)
333 Mem_Free(r_shadow_buffer_surfacepvs);
334 r_shadow_buffer_surfacepvs = NULL;
335 if (r_shadow_buffer_surfacelist)
336 Mem_Free(r_shadow_buffer_surfacelist);
337 r_shadow_buffer_surfacelist = NULL;
340 void r_shadow_newmap(void)
344 void R_Shadow_Help_f(void)
347 "Documentation on r_shadow system:\n"
349 "r_shadow_bumpscale_basetexture : base texture as bumpmap with this scale\n"
350 "r_shadow_bumpscale_bumpmap : depth scale for bumpmap conversion\n"
351 "r_shadow_debuglight : render only this light number (-1 = all)\n"
352 "r_shadow_gloss 0/1/2 : no gloss, gloss textures only, force gloss\n"
353 "r_shadow_gloss2intensity : brightness of forced gloss\n"
354 "r_shadow_glossintensity : brightness of textured gloss\n"
355 "r_shadow_lightattenuationpower : used to generate attenuation texture\n"
356 "r_shadow_lightattenuationscale : used to generate attenuation texture\n"
357 "r_shadow_lightintensityscale : scale rendering brightness of all lights\n"
358 "r_shadow_portallight : use portal visibility for static light precomputation\n"
359 "r_shadow_projectdistance : shadow volume projection distance\n"
360 "r_shadow_realtime_dlight : use high quality dynamic lights in normal mode\n"
361 "r_shadow_realtime_dlight_shadows : cast shadows from dlights\n"
362 "r_shadow_realtime_dlight_portalculling : work hard to reduce graphics work\n"
363 "r_shadow_realtime_world : use high quality world lighting mode\n"
364 "r_shadow_realtime_world_dlightshadows : cast shadows from dlights\n"
365 "r_shadow_realtime_world_lightmaps : use lightmaps in addition to lights\n"
366 "r_shadow_realtime_world_shadows : cast shadows from world lights\n"
367 "r_shadow_realtime_world_compile : compile surface/visibility information\n"
368 "r_shadow_realtime_world_compileshadow : compile shadow geometry\n"
369 "r_shadow_scissor : use scissor optimization\n"
370 "r_shadow_shadow_polygonfactor : nudge shadow volumes closer/further\n"
371 "r_shadow_shadow_polygonoffset : nudge shadow volumes closer/further\n"
372 "r_shadow_texture3d : use 3d attenuation texture (if hardware supports)\n"
373 "r_showlighting : useful for performance testing; bright = slow!\n"
374 "r_showshadowvolumes : useful for performance testing; bright = slow!\n"
376 "r_shadow_help : this help\n"
380 void R_Shadow_Init(void)
382 Cvar_RegisterVariable(&r_shadow_bumpscale_basetexture);
383 Cvar_RegisterVariable(&r_shadow_bumpscale_bumpmap);
384 Cvar_RegisterVariable(&r_shadow_debuglight);
385 Cvar_RegisterVariable(&r_shadow_gloss);
386 Cvar_RegisterVariable(&r_shadow_gloss2intensity);
387 Cvar_RegisterVariable(&r_shadow_glossintensity);
388 Cvar_RegisterVariable(&r_shadow_lightattenuationpower);
389 Cvar_RegisterVariable(&r_shadow_lightattenuationscale);
390 Cvar_RegisterVariable(&r_shadow_lightintensityscale);
391 Cvar_RegisterVariable(&r_shadow_portallight);
392 Cvar_RegisterVariable(&r_shadow_projectdistance);
393 Cvar_RegisterVariable(&r_shadow_realtime_dlight);
394 Cvar_RegisterVariable(&r_shadow_realtime_dlight_shadows);
395 Cvar_RegisterVariable(&r_shadow_realtime_dlight_portalculling);
396 Cvar_RegisterVariable(&r_shadow_realtime_world);
397 Cvar_RegisterVariable(&r_shadow_realtime_world_dlightshadows);
398 Cvar_RegisterVariable(&r_shadow_realtime_world_lightmaps);
399 Cvar_RegisterVariable(&r_shadow_realtime_world_shadows);
400 Cvar_RegisterVariable(&r_shadow_realtime_world_compile);
401 Cvar_RegisterVariable(&r_shadow_realtime_world_compileshadow);
402 Cvar_RegisterVariable(&r_shadow_scissor);
403 Cvar_RegisterVariable(&r_shadow_shadow_polygonfactor);
404 Cvar_RegisterVariable(&r_shadow_shadow_polygonoffset);
405 Cvar_RegisterVariable(&r_shadow_texture3d);
406 Cvar_RegisterVariable(&gl_ext_stenciltwoside);
407 if (gamemode == GAME_TENEBRAE)
409 Cvar_SetValue("r_shadow_gloss", 2);
410 Cvar_SetValue("r_shadow_bumpscale_basetexture", 4);
412 Cmd_AddCommand("r_shadow_help", R_Shadow_Help_f, "prints documentation on console commands and variables used by realtime lighting and shadowing system");
413 R_Shadow_EditLights_Init();
414 r_shadow_worldlightchain = NULL;
415 maxshadowtriangles = 0;
416 shadowelements = NULL;
417 maxshadowvertices = 0;
418 shadowvertex3f = NULL;
426 shadowmarklist = NULL;
428 r_shadow_buffer_numleafpvsbytes = 0;
429 r_shadow_buffer_leafpvs = NULL;
430 r_shadow_buffer_leaflist = NULL;
431 r_shadow_buffer_numsurfacepvsbytes = 0;
432 r_shadow_buffer_surfacepvs = NULL;
433 r_shadow_buffer_surfacelist = NULL;
434 R_RegisterModule("R_Shadow", r_shadow_start, r_shadow_shutdown, r_shadow_newmap);
437 matrix4x4_t matrix_attenuationxyz =
440 {0.5, 0.0, 0.0, 0.5},
441 {0.0, 0.5, 0.0, 0.5},
442 {0.0, 0.0, 0.5, 0.5},
447 matrix4x4_t matrix_attenuationz =
450 {0.0, 0.0, 0.5, 0.5},
451 {0.0, 0.0, 0.0, 0.5},
452 {0.0, 0.0, 0.0, 0.5},
457 void R_Shadow_ResizeShadowArrays(int numvertices, int numtriangles)
459 // make sure shadowelements is big enough for this volume
460 if (maxshadowtriangles < numtriangles)
462 maxshadowtriangles = numtriangles;
464 Mem_Free(shadowelements);
465 shadowelements = (int *)Mem_Alloc(r_main_mempool, maxshadowtriangles * sizeof(int[24]));
467 // make sure shadowvertex3f is big enough for this volume
468 if (maxshadowvertices < numvertices)
470 maxshadowvertices = numvertices;
472 Mem_Free(shadowvertex3f);
473 shadowvertex3f = (float *)Mem_Alloc(r_main_mempool, maxshadowvertices * sizeof(float[6]));
477 static void R_Shadow_EnlargeLeafSurfaceBuffer(int numleafs, int numsurfaces)
479 int numleafpvsbytes = (((numleafs + 7) >> 3) + 255) & ~255;
480 int numsurfacepvsbytes = (((numsurfaces + 7) >> 3) + 255) & ~255;
481 if (r_shadow_buffer_numleafpvsbytes < numleafpvsbytes)
483 if (r_shadow_buffer_leafpvs)
484 Mem_Free(r_shadow_buffer_leafpvs);
485 if (r_shadow_buffer_leaflist)
486 Mem_Free(r_shadow_buffer_leaflist);
487 r_shadow_buffer_numleafpvsbytes = numleafpvsbytes;
488 r_shadow_buffer_leafpvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numleafpvsbytes);
489 r_shadow_buffer_leaflist = (int *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numleafpvsbytes * 8 * sizeof(*r_shadow_buffer_leaflist));
491 if (r_shadow_buffer_numsurfacepvsbytes < numsurfacepvsbytes)
493 if (r_shadow_buffer_surfacepvs)
494 Mem_Free(r_shadow_buffer_surfacepvs);
495 if (r_shadow_buffer_surfacelist)
496 Mem_Free(r_shadow_buffer_surfacelist);
497 r_shadow_buffer_numsurfacepvsbytes = numsurfacepvsbytes;
498 r_shadow_buffer_surfacepvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numsurfacepvsbytes);
499 r_shadow_buffer_surfacelist = (int *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numsurfacepvsbytes * 8 * sizeof(*r_shadow_buffer_surfacelist));
503 void R_Shadow_PrepareShadowMark(int numtris)
505 // make sure shadowmark is big enough for this volume
506 if (maxshadowmark < numtris)
508 maxshadowmark = numtris;
510 Mem_Free(shadowmark);
512 Mem_Free(shadowmarklist);
513 shadowmark = (int *)Mem_Alloc(r_main_mempool, maxshadowmark * sizeof(*shadowmark));
514 shadowmarklist = (int *)Mem_Alloc(r_main_mempool, maxshadowmark * sizeof(*shadowmarklist));
518 // if shadowmarkcount wrapped we clear the array and adjust accordingly
519 if (shadowmarkcount == 0)
522 memset(shadowmark, 0, maxshadowmark * sizeof(*shadowmark));
527 int R_Shadow_ConstructShadowVolume(int innumvertices, int innumtris, const int *inelement3i, const int *inneighbor3i, const float *invertex3f, int *outnumvertices, int *outelement3i, float *outvertex3f, const float *projectorigin, float projectdistance, int numshadowmarktris, const int *shadowmarktris)
530 int outtriangles = 0, outvertices = 0;
534 if (maxvertexupdate < innumvertices)
536 maxvertexupdate = innumvertices;
538 Mem_Free(vertexupdate);
540 Mem_Free(vertexremap);
541 vertexupdate = (int *)Mem_Alloc(r_main_mempool, maxvertexupdate * sizeof(int));
542 vertexremap = (int *)Mem_Alloc(r_main_mempool, maxvertexupdate * sizeof(int));
546 if (vertexupdatenum == 0)
549 memset(vertexupdate, 0, maxvertexupdate * sizeof(int));
550 memset(vertexremap, 0, maxvertexupdate * sizeof(int));
553 for (i = 0;i < numshadowmarktris;i++)
554 shadowmark[shadowmarktris[i]] = shadowmarkcount;
556 for (i = 0;i < numshadowmarktris;i++)
558 element = inelement3i + shadowmarktris[i] * 3;
559 // make sure the vertices are created
560 for (j = 0;j < 3;j++)
562 if (vertexupdate[element[j]] != vertexupdatenum)
564 float ratio, direction[3];
565 vertexupdate[element[j]] = vertexupdatenum;
566 vertexremap[element[j]] = outvertices;
567 vertex = invertex3f + element[j] * 3;
568 // project one copy of the vertex to the sphere radius of the light
569 // (FIXME: would projecting it to the light box be better?)
570 VectorSubtract(vertex, projectorigin, direction);
571 ratio = projectdistance / VectorLength(direction);
572 VectorCopy(vertex, outvertex3f);
573 VectorMA(projectorigin, ratio, direction, (outvertex3f + 3));
580 for (i = 0;i < numshadowmarktris;i++)
582 int remappedelement[3];
584 const int *neighbortriangle;
586 markindex = shadowmarktris[i] * 3;
587 element = inelement3i + markindex;
588 neighbortriangle = inneighbor3i + markindex;
589 // output the front and back triangles
590 outelement3i[0] = vertexremap[element[0]];
591 outelement3i[1] = vertexremap[element[1]];
592 outelement3i[2] = vertexremap[element[2]];
593 outelement3i[3] = vertexremap[element[2]] + 1;
594 outelement3i[4] = vertexremap[element[1]] + 1;
595 outelement3i[5] = vertexremap[element[0]] + 1;
599 // output the sides (facing outward from this triangle)
600 if (shadowmark[neighbortriangle[0]] != shadowmarkcount)
602 remappedelement[0] = vertexremap[element[0]];
603 remappedelement[1] = vertexremap[element[1]];
604 outelement3i[0] = remappedelement[1];
605 outelement3i[1] = remappedelement[0];
606 outelement3i[2] = remappedelement[0] + 1;
607 outelement3i[3] = remappedelement[1];
608 outelement3i[4] = remappedelement[0] + 1;
609 outelement3i[5] = remappedelement[1] + 1;
614 if (shadowmark[neighbortriangle[1]] != shadowmarkcount)
616 remappedelement[1] = vertexremap[element[1]];
617 remappedelement[2] = vertexremap[element[2]];
618 outelement3i[0] = remappedelement[2];
619 outelement3i[1] = remappedelement[1];
620 outelement3i[2] = remappedelement[1] + 1;
621 outelement3i[3] = remappedelement[2];
622 outelement3i[4] = remappedelement[1] + 1;
623 outelement3i[5] = remappedelement[2] + 1;
628 if (shadowmark[neighbortriangle[2]] != shadowmarkcount)
630 remappedelement[0] = vertexremap[element[0]];
631 remappedelement[2] = vertexremap[element[2]];
632 outelement3i[0] = remappedelement[0];
633 outelement3i[1] = remappedelement[2];
634 outelement3i[2] = remappedelement[2] + 1;
635 outelement3i[3] = remappedelement[0];
636 outelement3i[4] = remappedelement[2] + 1;
637 outelement3i[5] = remappedelement[0] + 1;
644 *outnumvertices = outvertices;
648 void R_Shadow_VolumeFromList(int numverts, int numtris, const float *invertex3f, const int *elements, const int *neighbors, const vec3_t projectorigin, float projectdistance, int nummarktris, const int *marktris)
651 if (projectdistance < 0.1)
653 Con_Printf("R_Shadow_Volume: projectdistance %f\n");
656 if (!numverts || !nummarktris)
658 // make sure shadowelements is big enough for this volume
659 if (maxshadowtriangles < nummarktris || maxshadowvertices < numverts)
660 R_Shadow_ResizeShadowArrays((numverts + 255) & ~255, (nummarktris + 255) & ~255);
661 tris = R_Shadow_ConstructShadowVolume(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdistance, nummarktris, marktris);
662 renderstats.lights_dynamicshadowtriangles += tris;
663 R_Shadow_RenderVolume(outverts, tris, shadowvertex3f, shadowelements);
666 void R_Shadow_MarkVolumeFromBox(int firsttriangle, int numtris, const float *invertex3f, const int *elements, const vec3_t projectorigin, const vec3_t lightmins, const vec3_t lightmaxs, const vec3_t surfacemins, const vec3_t surfacemaxs)
671 if (!BoxesOverlap(lightmins, lightmaxs, surfacemins, surfacemaxs))
673 tend = firsttriangle + numtris;
674 if (surfacemins[0] >= lightmins[0] && surfacemaxs[0] <= lightmaxs[0]
675 && surfacemins[1] >= lightmins[1] && surfacemaxs[1] <= lightmaxs[1]
676 && surfacemins[2] >= lightmins[2] && surfacemaxs[2] <= lightmaxs[2])
678 // surface box entirely inside light box, no box cull
679 for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
680 if (PointInfrontOfTriangle(projectorigin, invertex3f + e[0] * 3, invertex3f + e[1] * 3, invertex3f + e[2] * 3))
681 shadowmarklist[numshadowmark++] = t;
685 // surface box not entirely inside light box, cull each triangle
686 for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
688 v[0] = invertex3f + e[0] * 3;
689 v[1] = invertex3f + e[1] * 3;
690 v[2] = invertex3f + e[2] * 3;
691 if (PointInfrontOfTriangle(projectorigin, v[0], v[1], v[2])
692 && lightmaxs[0] > min(v[0][0], min(v[1][0], v[2][0]))
693 && lightmins[0] < max(v[0][0], max(v[1][0], v[2][0]))
694 && lightmaxs[1] > min(v[0][1], min(v[1][1], v[2][1]))
695 && lightmins[1] < max(v[0][1], max(v[1][1], v[2][1]))
696 && lightmaxs[2] > min(v[0][2], min(v[1][2], v[2][2]))
697 && lightmins[2] < max(v[0][2], max(v[1][2], v[2][2])))
698 shadowmarklist[numshadowmark++] = t;
703 void R_Shadow_RenderVolume(int numvertices, int numtriangles, const float *vertex3f, const int *element3i)
706 if (r_shadow_compilingrtlight)
708 // if we're compiling an rtlight, capture the mesh
709 Mod_ShadowMesh_AddMesh(r_main_mempool, r_shadow_compilingrtlight->static_meshchain_shadow, NULL, NULL, NULL, vertex3f, NULL, NULL, NULL, NULL, numtriangles, element3i);
712 renderstats.lights_shadowtriangles += numtriangles;
713 memset(&m, 0, sizeof(m));
714 m.pointer_vertex = vertex3f;
716 GL_LockArrays(0, numvertices);
717 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_STENCIL)
719 // decrement stencil if backface is behind depthbuffer
720 qglCullFace(GL_BACK); // quake is backwards, this culls front faces
721 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
722 R_Mesh_Draw(0, numvertices, numtriangles, element3i);
723 // increment stencil if frontface is behind depthbuffer
724 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
725 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
727 R_Mesh_Draw(0, numvertices, numtriangles, element3i);
731 static void R_Shadow_MakeTextures(void)
734 float v[3], intensity;
736 R_FreeTexturePool(&r_shadow_texturepool);
737 r_shadow_texturepool = R_AllocTexturePool();
738 r_shadow_attenpower = r_shadow_lightattenuationpower.value;
739 r_shadow_attenscale = r_shadow_lightattenuationscale.value;
740 #define ATTEN2DSIZE 64
741 #define ATTEN3DSIZE 32
742 data = (unsigned char *)Mem_Alloc(tempmempool, max(ATTEN3DSIZE*ATTEN3DSIZE*ATTEN3DSIZE*4, ATTEN2DSIZE*ATTEN2DSIZE*4));
743 for (y = 0;y < ATTEN2DSIZE;y++)
745 for (x = 0;x < ATTEN2DSIZE;x++)
747 v[0] = ((x + 0.5f) * (2.0f / ATTEN2DSIZE) - 1.0f) * (1.0f / 0.9375);
748 v[1] = ((y + 0.5f) * (2.0f / ATTEN2DSIZE) - 1.0f) * (1.0f / 0.9375);
750 intensity = 1.0f - sqrt(DotProduct(v, v));
752 intensity = pow(intensity, r_shadow_attenpower) * r_shadow_attenscale * 256.0f;
753 d = (int)bound(0, intensity, 255);
754 data[(y*ATTEN2DSIZE+x)*4+0] = d;
755 data[(y*ATTEN2DSIZE+x)*4+1] = d;
756 data[(y*ATTEN2DSIZE+x)*4+2] = d;
757 data[(y*ATTEN2DSIZE+x)*4+3] = d;
760 r_shadow_attenuation2dtexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation2d", ATTEN2DSIZE, ATTEN2DSIZE, data, TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA, NULL);
761 if (r_shadow_texture3d.integer)
763 for (z = 0;z < ATTEN3DSIZE;z++)
765 for (y = 0;y < ATTEN3DSIZE;y++)
767 for (x = 0;x < ATTEN3DSIZE;x++)
769 v[0] = ((x + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375);
770 v[1] = ((y + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375);
771 v[2] = ((z + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375);
772 intensity = 1.0f - sqrt(DotProduct(v, v));
774 intensity = pow(intensity, r_shadow_attenpower) * r_shadow_attenscale * 256.0f;
775 d = (int)bound(0, intensity, 255);
776 data[((z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x)*4+0] = d;
777 data[((z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x)*4+1] = d;
778 data[((z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x)*4+2] = d;
779 data[((z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x)*4+3] = d;
783 r_shadow_attenuation3dtexture = R_LoadTexture3D(r_shadow_texturepool, "attenuation3d", ATTEN3DSIZE, ATTEN3DSIZE, ATTEN3DSIZE, data, TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA, NULL);
788 void R_Shadow_ValidateCvars(void)
790 if (r_shadow_texture3d.integer && !gl_texture3d)
791 Cvar_SetValueQuick(&r_shadow_texture3d, 0);
792 if (gl_ext_stenciltwoside.integer && !gl_support_stenciltwoside)
793 Cvar_SetValueQuick(&gl_ext_stenciltwoside, 0);
796 // light currently being rendered
797 rtlight_t *r_shadow_rtlight;
799 // this is the location of the eye in entity space
800 vec3_t r_shadow_entityeyeorigin;
801 // this is the location of the light in entity space
802 vec3_t r_shadow_entitylightorigin;
803 // this transforms entity coordinates to light filter cubemap coordinates
804 // (also often used for other purposes)
805 matrix4x4_t r_shadow_entitytolight;
806 // based on entitytolight this transforms -1 to +1 to 0 to 1 for purposes
807 // of attenuation texturing in full 3D (Z result often ignored)
808 matrix4x4_t r_shadow_entitytoattenuationxyz;
809 // this transforms only the Z to S, and T is always 0.5
810 matrix4x4_t r_shadow_entitytoattenuationz;
812 void R_Shadow_RenderMode_Begin(void)
816 R_Shadow_ValidateCvars();
818 if (!r_shadow_attenuation2dtexture
819 || (!r_shadow_attenuation3dtexture && r_shadow_texture3d.integer)
820 || r_shadow_lightattenuationpower.value != r_shadow_attenpower
821 || r_shadow_lightattenuationscale.value != r_shadow_attenscale)
822 R_Shadow_MakeTextures();
824 memset(&m, 0, sizeof(m));
826 GL_BlendFunc(GL_ONE, GL_ZERO);
829 GL_Color(0, 0, 0, 1);
830 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
831 qglEnable(GL_CULL_FACE);
832 GL_Scissor(r_view_x, r_view_y, r_view_width, r_view_height);
834 r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
836 if (gl_ext_stenciltwoside.integer)
837 r_shadow_shadowingrendermode = R_SHADOW_RENDERMODE_STENCILTWOSIDE;
839 r_shadow_shadowingrendermode = R_SHADOW_RENDERMODE_STENCIL;
841 if (r_glsl.integer && gl_support_fragment_shader)
842 r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_GLSL;
843 else if (gl_dot3arb && gl_texturecubemap && r_textureunits.integer >= 2 && gl_combine.integer && gl_stencil)
844 r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_DOT3;
846 r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_VERTEX;
849 void R_Shadow_RenderMode_ActiveLight(rtlight_t *rtlight)
851 r_shadow_rtlight = rtlight;
854 void R_Shadow_RenderMode_Reset(void)
857 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_LIGHT_GLSL)
859 qglUseProgramObjectARB(0);
860 // HACK HACK HACK: work around for bug in NVIDIAI 6xxx drivers that causes GL_OUT_OF_MEMORY and/or software rendering
861 qglBegin(GL_TRIANGLES);
865 else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_STENCILTWOSIDE)
866 qglDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
867 memset(&m, 0, sizeof(m));
871 void R_Shadow_RenderMode_StencilShadowVolumes(void)
873 R_Shadow_RenderMode_Reset();
874 GL_Color(1, 1, 1, 1);
875 GL_ColorMask(0, 0, 0, 0);
876 GL_BlendFunc(GL_ONE, GL_ZERO);
879 qglPolygonOffset(r_shadowpolygonfactor, r_shadowpolygonoffset);
880 qglDepthFunc(GL_LESS);
881 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
882 qglEnable(GL_STENCIL_TEST);
883 qglStencilFunc(GL_ALWAYS, 128, ~0);
884 r_shadow_rendermode = r_shadow_shadowingrendermode;
885 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_STENCILTWOSIDE)
887 qglDisable(GL_CULL_FACE);
888 qglEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);
889 qglActiveStencilFaceEXT(GL_BACK); // quake is backwards, this is front faces
891 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
892 qglActiveStencilFaceEXT(GL_FRONT); // quake is backwards, this is back faces
894 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
898 qglEnable(GL_CULL_FACE);
900 // this is changed by every shadow render so its value here is unimportant
901 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
903 GL_Clear(GL_STENCIL_BUFFER_BIT);
904 renderstats.lights_clears++;
907 void R_Shadow_RenderMode_Lighting(qboolean stenciltest, qboolean transparent)
909 R_Shadow_RenderMode_Reset();
910 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
913 qglPolygonOffset(r_polygonfactor, r_polygonoffset);
914 //qglDisable(GL_POLYGON_OFFSET_FILL);
915 GL_Color(1, 1, 1, 1);
916 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
918 qglDepthFunc(GL_LEQUAL);
920 qglDepthFunc(GL_EQUAL);
921 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
922 qglEnable(GL_CULL_FACE);
924 qglEnable(GL_STENCIL_TEST);
926 qglDisable(GL_STENCIL_TEST);
928 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
929 // only draw light where this geometry was already rendered AND the
930 // stencil is 128 (values other than this mean shadow)
931 qglStencilFunc(GL_EQUAL, 128, ~0);
932 r_shadow_rendermode = r_shadow_lightingrendermode;
933 // do global setup needed for the chosen lighting mode
934 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_LIGHT_GLSL)
936 R_Mesh_TexBind(0, R_GetTexture(r_texture_blanknormalmap)); // normal
937 R_Mesh_TexBind(1, R_GetTexture(r_texture_white)); // diffuse
938 R_Mesh_TexBind(2, R_GetTexture(r_texture_white)); // gloss
939 R_Mesh_TexBindCubeMap(3, R_GetTexture(r_shadow_rtlight->currentcubemap)); // light filter
940 R_Mesh_TexBind(4, R_GetTexture(r_texture_fogattenuation)); // fog
941 R_Mesh_TexBind(5, R_GetTexture(r_texture_white)); // pants
942 R_Mesh_TexBind(6, R_GetTexture(r_texture_white)); // shirt
943 //R_Mesh_TexMatrix(3, r_shadow_entitytolight); // light filter matrix
944 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
945 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 0);
950 void R_Shadow_RenderMode_VisibleShadowVolumes(void)
952 R_Shadow_RenderMode_Reset();
953 GL_BlendFunc(GL_ONE, GL_ONE);
955 GL_DepthTest(!r_showdisabledepthtest.integer);
956 qglPolygonOffset(r_polygonfactor, r_polygonoffset);
957 GL_Color(0.0, 0.0125, 0.1, 1);
958 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
959 qglDepthFunc(GL_GEQUAL);
960 qglCullFace(GL_FRONT); // this culls back
961 qglDisable(GL_CULL_FACE);
962 qglDisable(GL_STENCIL_TEST);
963 r_shadow_rendermode = R_SHADOW_RENDERMODE_VISIBLEVOLUMES;
966 void R_Shadow_RenderMode_VisibleLighting(qboolean stenciltest, qboolean transparent)
968 R_Shadow_RenderMode_Reset();
969 GL_BlendFunc(GL_ONE, GL_ONE);
971 GL_DepthTest(!r_showdisabledepthtest.integer);
972 qglPolygonOffset(r_polygonfactor, r_polygonoffset);
973 GL_Color(0.1, 0.0125, 0, 1);
974 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
976 qglDepthFunc(GL_LEQUAL);
978 qglDepthFunc(GL_EQUAL);
979 qglCullFace(GL_FRONT); // this culls back
980 qglEnable(GL_CULL_FACE);
982 qglEnable(GL_STENCIL_TEST);
984 qglDisable(GL_STENCIL_TEST);
985 r_shadow_rendermode = R_SHADOW_RENDERMODE_VISIBLELIGHTING;
988 void R_Shadow_RenderMode_End(void)
990 R_Shadow_RenderMode_Reset();
991 R_Shadow_RenderMode_ActiveLight(NULL);
992 GL_BlendFunc(GL_ONE, GL_ZERO);
995 qglPolygonOffset(r_polygonfactor, r_polygonoffset);
996 //qglDisable(GL_POLYGON_OFFSET_FILL);
997 GL_Color(1, 1, 1, 1);
998 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
999 GL_Scissor(r_view_x, r_view_y, r_view_width, r_view_height);
1000 qglDepthFunc(GL_LEQUAL);
1001 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
1002 qglEnable(GL_CULL_FACE);
1003 qglDisable(GL_STENCIL_TEST);
1004 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
1005 if (gl_support_stenciltwoside)
1006 qglDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
1008 qglStencilFunc(GL_ALWAYS, 128, ~0);
1009 r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
1012 qboolean R_Shadow_ScissorForBBox(const float *mins, const float *maxs)
1014 int i, ix1, iy1, ix2, iy2;
1015 float x1, y1, x2, y2;
1018 mplane_t planes[11];
1019 float vertex3f[256*3];
1021 // if view is inside the light box, just say yes it's visible
1022 if (BoxesOverlap(r_vieworigin, r_vieworigin, mins, maxs))
1024 GL_Scissor(r_view_x, r_view_y, r_view_width, r_view_height);
1028 // create a temporary brush describing the area the light can affect in worldspace
1029 VectorNegate(frustum[0].normal, planes[ 0].normal);planes[ 0].dist = -frustum[0].dist;
1030 VectorNegate(frustum[1].normal, planes[ 1].normal);planes[ 1].dist = -frustum[1].dist;
1031 VectorNegate(frustum[2].normal, planes[ 2].normal);planes[ 2].dist = -frustum[2].dist;
1032 VectorNegate(frustum[3].normal, planes[ 3].normal);planes[ 3].dist = -frustum[3].dist;
1033 VectorNegate(frustum[4].normal, planes[ 4].normal);planes[ 4].dist = -frustum[4].dist;
1034 VectorSet (planes[ 5].normal, 1, 0, 0); planes[ 5].dist = maxs[0];
1035 VectorSet (planes[ 6].normal, -1, 0, 0); planes[ 6].dist = -mins[0];
1036 VectorSet (planes[ 7].normal, 0, 1, 0); planes[ 7].dist = maxs[1];
1037 VectorSet (planes[ 8].normal, 0, -1, 0); planes[ 8].dist = -mins[1];
1038 VectorSet (planes[ 9].normal, 0, 0, 1); planes[ 9].dist = maxs[2];
1039 VectorSet (planes[10].normal, 0, 0, -1); planes[10].dist = -mins[2];
1041 // turn the brush into a mesh
1042 memset(&mesh, 0, sizeof(rmesh_t));
1043 mesh.maxvertices = 256;
1044 mesh.vertex3f = vertex3f;
1045 mesh.epsilon2 = (1.0f / (32.0f * 32.0f));
1046 R_Mesh_AddBrushMeshFromPlanes(&mesh, 11, planes);
1048 // if that mesh is empty, the light is not visible at all
1049 if (!mesh.numvertices)
1052 if (!r_shadow_scissor.integer)
1055 // if that mesh is not empty, check what area of the screen it covers
1056 x1 = y1 = x2 = y2 = 0;
1058 for (i = 0;i < mesh.numvertices;i++)
1060 VectorCopy(mesh.vertex3f + i * 3, v);
1061 GL_TransformToScreen(v, v2);
1062 //Con_Printf("%.3f %.3f %.3f %.3f transformed to %.3f %.3f %.3f %.3f\n", v[0], v[1], v[2], v[3], v2[0], v2[1], v2[2], v2[3]);
1065 if (x1 > v2[0]) x1 = v2[0];
1066 if (x2 < v2[0]) x2 = v2[0];
1067 if (y1 > v2[1]) y1 = v2[1];
1068 if (y2 < v2[1]) y2 = v2[1];
1077 // now convert the scissor rectangle to integer screen coordinates
1078 ix1 = (int)(x1 - 1.0f);
1079 iy1 = (int)(y1 - 1.0f);
1080 ix2 = (int)(x2 + 1.0f);
1081 iy2 = (int)(y2 + 1.0f);
1082 //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
1084 // clamp it to the screen
1085 if (ix1 < r_view_x) ix1 = r_view_x;
1086 if (iy1 < r_view_y) iy1 = r_view_y;
1087 if (ix2 > r_view_x + r_view_width) ix2 = r_view_x + r_view_width;
1088 if (iy2 > r_view_y + r_view_height) iy2 = r_view_y + r_view_height;
1090 // if it is inside out, it's not visible
1091 if (ix2 <= ix1 || iy2 <= iy1)
1094 // the light area is visible, set up the scissor rectangle
1095 GL_Scissor(ix1, vid.height - iy2, ix2 - ix1, iy2 - iy1);
1096 //qglScissor(ix1, iy1, ix2 - ix1, iy2 - iy1);
1097 //qglEnable(GL_SCISSOR_TEST);
1098 renderstats.lights_scissored++;
1102 static void R_Shadow_RenderSurfacesLighting_Light_Vertex_Shading(const msurface_t *surface, const float *diffusecolor, const float *ambientcolor)
1104 int numverts = surface->num_vertices;
1105 float *vertex3f = rsurface_vertex3f + 3 * surface->num_firstvertex;
1106 float *normal3f = rsurface_normal3f + 3 * surface->num_firstvertex;
1107 float *color4f = rsurface_array_color4f + 4 * surface->num_firstvertex;
1108 float dist, dot, distintensity, shadeintensity, v[3], n[3];
1109 if (r_textureunits.integer >= 3)
1111 for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
1113 Matrix4x4_Transform(&r_shadow_entitytolight, vertex3f, v);
1114 Matrix4x4_Transform3x3(&r_shadow_entitytolight, normal3f, n);
1115 if ((dot = DotProduct(n, v)) < 0)
1117 shadeintensity = -dot / sqrt(VectorLength2(v) * VectorLength2(n));
1118 color4f[0] = (ambientcolor[0] + shadeintensity * diffusecolor[0]);
1119 color4f[1] = (ambientcolor[1] + shadeintensity * diffusecolor[1]);
1120 color4f[2] = (ambientcolor[2] + shadeintensity * diffusecolor[2]);
1123 float f = VERTEXFOGTABLE(VectorDistance(v, r_shadow_entityeyeorigin));
1124 VectorScale(color4f, f, color4f);
1128 VectorClear(color4f);
1132 else if (r_textureunits.integer >= 2)
1134 for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
1136 Matrix4x4_Transform(&r_shadow_entitytolight, vertex3f, v);
1137 if ((dist = fabs(v[2])) < 1)
1139 distintensity = pow(1 - dist, r_shadow_attenpower) * r_shadow_attenscale;
1140 Matrix4x4_Transform3x3(&r_shadow_entitytolight, normal3f, n);
1141 if ((dot = DotProduct(n, v)) < 0)
1143 shadeintensity = -dot / sqrt(VectorLength2(v) * VectorLength2(n));
1144 color4f[0] = (ambientcolor[0] + shadeintensity * diffusecolor[0]) * distintensity;
1145 color4f[1] = (ambientcolor[1] + shadeintensity * diffusecolor[1]) * distintensity;
1146 color4f[2] = (ambientcolor[2] + shadeintensity * diffusecolor[2]) * distintensity;
1150 color4f[0] = ambientcolor[0] * distintensity;
1151 color4f[1] = ambientcolor[1] * distintensity;
1152 color4f[2] = ambientcolor[2] * distintensity;
1156 float f = VERTEXFOGTABLE(VectorDistance(v, r_shadow_entityeyeorigin));
1157 VectorScale(color4f, f, color4f);
1161 VectorClear(color4f);
1167 for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
1169 Matrix4x4_Transform(&r_shadow_entitytolight, vertex3f, v);
1170 if ((dist = DotProduct(v, v)) < 1)
1173 distintensity = pow(1 - dist, r_shadow_attenpower) * r_shadow_attenscale;
1174 Matrix4x4_Transform3x3(&r_shadow_entitytolight, normal3f, n);
1175 if ((dot = DotProduct(n, v)) < 0)
1177 shadeintensity = -dot / sqrt(VectorLength2(v) * VectorLength2(n));
1178 color4f[0] = (ambientcolor[0] + shadeintensity * diffusecolor[0]) * distintensity;
1179 color4f[1] = (ambientcolor[1] + shadeintensity * diffusecolor[1]) * distintensity;
1180 color4f[2] = (ambientcolor[2] + shadeintensity * diffusecolor[2]) * distintensity;
1184 color4f[0] = ambientcolor[0] * distintensity;
1185 color4f[1] = ambientcolor[1] * distintensity;
1186 color4f[2] = ambientcolor[2] * distintensity;
1190 float f = VERTEXFOGTABLE(VectorDistance(v, r_shadow_entityeyeorigin));
1191 VectorScale(color4f, f, color4f);
1195 VectorClear(color4f);
1201 // TODO: use glTexGen instead of feeding vertices to texcoordpointer?
1203 static void R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(float *out3f, int numverts, const float *vertex3f, const float *svector3f, const float *tvector3f, const float *normal3f, const vec3_t relativelightorigin)
1207 for (i = 0;i < numverts;i++, vertex3f += 3, svector3f += 3, tvector3f += 3, normal3f += 3, out3f += 3)
1209 VectorSubtract(relativelightorigin, vertex3f, lightdir);
1210 // the cubemap normalizes this for us
1211 out3f[0] = DotProduct(svector3f, lightdir);
1212 out3f[1] = DotProduct(tvector3f, lightdir);
1213 out3f[2] = DotProduct(normal3f, lightdir);
1217 static void R_Shadow_GenTexCoords_Specular_NormalCubeMap(float *out3f, int numverts, const float *vertex3f, const float *svector3f, const float *tvector3f, const float *normal3f, const vec3_t relativelightorigin, const vec3_t relativeeyeorigin)
1220 float lightdir[3], eyedir[3], halfdir[3];
1221 for (i = 0;i < numverts;i++, vertex3f += 3, svector3f += 3, tvector3f += 3, normal3f += 3, out3f += 3)
1223 VectorSubtract(relativelightorigin, vertex3f, lightdir);
1224 VectorNormalize(lightdir);
1225 VectorSubtract(relativeeyeorigin, vertex3f, eyedir);
1226 VectorNormalize(eyedir);
1227 VectorAdd(lightdir, eyedir, halfdir);
1228 // the cubemap normalizes this for us
1229 out3f[0] = DotProduct(svector3f, halfdir);
1230 out3f[1] = DotProduct(tvector3f, halfdir);
1231 out3f[2] = DotProduct(normal3f, halfdir);
1235 static void R_Shadow_RenderSurfacesLighting_VisibleLighting(const entity_render_t *ent, const texture_t *texture, int numsurfaces, msurface_t **surfacelist, const vec3_t lightcolorbase, const vec3_t lightcolorpants, const vec3_t lightcolorshirt, rtexture_t *basetexture, rtexture_t *pantstexture, rtexture_t *shirttexture, rtexture_t *normalmaptexture, rtexture_t *glosstexture, float specularscale, qboolean dopants, qboolean doshirt)
1237 // used to display how many times a surface is lit for level design purposes
1238 int surfacelistindex;
1239 model_t *model = ent->model;
1241 GL_Color(0.1, 0.025, 0, 1);
1242 memset(&m, 0, sizeof(m));
1244 RSurf_PrepareVerticesForBatch(ent, texture, r_shadow_entityeyeorigin, false, false, numsurfaces, surfacelist);
1245 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1247 const msurface_t *surface = surfacelist[surfacelistindex];
1248 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1249 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, model->surfmesh.data_element3i + 3 * surface->num_firsttriangle);
1250 GL_LockArrays(0, 0);
1254 static void R_Shadow_RenderSurfacesLighting_Light_GLSL(const entity_render_t *ent, const texture_t *texture, int numsurfaces, msurface_t **surfacelist, const vec3_t lightcolorbase, const vec3_t lightcolorpants, const vec3_t lightcolorshirt, rtexture_t *basetexture, rtexture_t *pantstexture, rtexture_t *shirttexture, rtexture_t *normalmaptexture, rtexture_t *glosstexture, float specularscale, qboolean dopants, qboolean doshirt)
1256 // ARB2 GLSL shader path (GFFX5200, Radeon 9500)
1257 int surfacelistindex;
1258 model_t *model = ent->model;
1259 RSurf_PrepareVerticesForBatch(ent, texture, r_shadow_entityeyeorigin, true, true, numsurfaces, surfacelist);
1260 R_SetupSurfaceShader(ent, texture, r_shadow_entityeyeorigin, lightcolorbase, false);
1261 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1263 const msurface_t *surface = surfacelist[surfacelistindex];
1264 const int *elements = model->surfmesh.data_element3i + surface->num_firsttriangle * 3;
1265 R_Mesh_TexCoordPointer(0, 2, model->surfmesh.data_texcoordtexture2f);
1266 R_Mesh_TexCoordPointer(1, 3, rsurface_svector3f);
1267 R_Mesh_TexCoordPointer(2, 3, rsurface_tvector3f);
1268 R_Mesh_TexCoordPointer(3, 3, rsurface_normal3f);
1269 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1270 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
1271 GL_LockArrays(0, 0);
1275 static void R_Shadow_RenderSurfacesLighting_Light_Dot3_AmbientPass(const entity_render_t *ent, const texture_t *texture, const msurface_t *surface, const vec3_t lightcolorbase, rtexture_t *basetexture, float colorscale)
1278 model_t *model = ent->model;
1281 const int *elements = model->surfmesh.data_element3i + surface->num_firsttriangle * 3;
1283 // colorscale accounts for how much we multiply the brightness
1286 // mult is how many times the final pass of the lighting will be
1287 // performed to get more brightness than otherwise possible.
1289 // Limit mult to 64 for sanity sake.
1290 if (r_shadow_texture3d.integer && r_shadow_rtlight->currentcubemap != r_texture_whitecube && r_textureunits.integer >= 4)
1292 // 3 3D combine path (Geforce3, Radeon 8500)
1293 memset(&m, 0, sizeof(m));
1294 m.pointer_vertex = rsurface_vertex3f;
1295 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
1296 m.pointer_texcoord3f[0] = rsurface_vertex3f;
1297 m.texmatrix[0] = r_shadow_entitytoattenuationxyz;
1298 m.tex[1] = R_GetTexture(basetexture);
1299 m.pointer_texcoord[1] = model->surfmesh.data_texcoordtexture2f;
1300 m.texmatrix[1] = texture->currenttexmatrix;
1301 m.texcubemap[2] = R_GetTexture(r_shadow_rtlight->currentcubemap);
1302 m.pointer_texcoord3f[2] = rsurface_vertex3f;
1303 m.texmatrix[2] = r_shadow_entitytolight;
1304 GL_BlendFunc(GL_ONE, GL_ONE);
1306 else if (r_shadow_texture3d.integer && r_shadow_rtlight->currentcubemap == r_texture_whitecube && r_textureunits.integer >= 2)
1308 // 2 3D combine path (Geforce3, original Radeon)
1309 memset(&m, 0, sizeof(m));
1310 m.pointer_vertex = rsurface_vertex3f;
1311 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
1312 m.pointer_texcoord3f[0] = rsurface_vertex3f;
1313 m.texmatrix[0] = r_shadow_entitytoattenuationxyz;
1314 m.tex[1] = R_GetTexture(basetexture);
1315 m.pointer_texcoord[1] = model->surfmesh.data_texcoordtexture2f;
1316 m.texmatrix[1] = texture->currenttexmatrix;
1317 GL_BlendFunc(GL_ONE, GL_ONE);
1319 else if (r_textureunits.integer >= 4 && r_shadow_rtlight->currentcubemap != r_texture_whitecube)
1321 // 4 2D combine path (Geforce3, Radeon 8500)
1322 memset(&m, 0, sizeof(m));
1323 m.pointer_vertex = rsurface_vertex3f;
1324 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
1325 m.pointer_texcoord3f[0] = rsurface_vertex3f;
1326 m.texmatrix[0] = r_shadow_entitytoattenuationxyz;
1327 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1328 m.pointer_texcoord3f[1] = rsurface_vertex3f;
1329 m.texmatrix[1] = r_shadow_entitytoattenuationz;
1330 m.tex[2] = R_GetTexture(basetexture);
1331 m.pointer_texcoord[2] = model->surfmesh.data_texcoordtexture2f;
1332 m.texmatrix[2] = texture->currenttexmatrix;
1333 if (r_shadow_rtlight->currentcubemap != r_texture_whitecube)
1335 m.texcubemap[3] = R_GetTexture(r_shadow_rtlight->currentcubemap);
1336 m.pointer_texcoord3f[3] = rsurface_vertex3f;
1337 m.texmatrix[3] = r_shadow_entitytolight;
1339 GL_BlendFunc(GL_ONE, GL_ONE);
1341 else if (r_textureunits.integer >= 3 && r_shadow_rtlight->currentcubemap == r_texture_whitecube)
1343 // 3 2D combine path (Geforce3, original Radeon)
1344 memset(&m, 0, sizeof(m));
1345 m.pointer_vertex = rsurface_vertex3f;
1346 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
1347 m.pointer_texcoord3f[0] = rsurface_vertex3f;
1348 m.texmatrix[0] = r_shadow_entitytoattenuationxyz;
1349 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1350 m.pointer_texcoord3f[1] = rsurface_vertex3f;
1351 m.texmatrix[1] = r_shadow_entitytoattenuationz;
1352 m.tex[2] = R_GetTexture(basetexture);
1353 m.pointer_texcoord[2] = model->surfmesh.data_texcoordtexture2f;
1354 m.texmatrix[2] = texture->currenttexmatrix;
1355 GL_BlendFunc(GL_ONE, GL_ONE);
1359 // 2/2/2 2D combine path (any dot3 card)
1360 memset(&m, 0, sizeof(m));
1361 m.pointer_vertex = rsurface_vertex3f;
1362 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
1363 m.pointer_texcoord3f[0] = rsurface_vertex3f;
1364 m.texmatrix[0] = r_shadow_entitytoattenuationxyz;
1365 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1366 m.pointer_texcoord3f[1] = rsurface_vertex3f;
1367 m.texmatrix[1] = r_shadow_entitytoattenuationz;
1369 GL_ColorMask(0,0,0,1);
1370 GL_BlendFunc(GL_ONE, GL_ZERO);
1371 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1372 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
1373 GL_LockArrays(0, 0);
1375 memset(&m, 0, sizeof(m));
1376 m.pointer_vertex = rsurface_vertex3f;
1377 m.tex[0] = R_GetTexture(basetexture);
1378 m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
1379 m.texmatrix[0] = texture->currenttexmatrix;
1380 if (r_shadow_rtlight->currentcubemap != r_texture_whitecube)
1382 m.texcubemap[1] = R_GetTexture(r_shadow_rtlight->currentcubemap);
1383 m.pointer_texcoord3f[1] = rsurface_vertex3f;
1384 m.texmatrix[1] = r_shadow_entitytolight;
1386 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
1388 // this final code is shared
1390 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 0);
1391 VectorScale(lightcolorbase, colorscale, color2);
1392 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1393 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1395 GL_Color(bound(0, color2[0], 1), bound(0, color2[1], 1), bound(0, color2[2], 1), 1);
1396 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
1398 GL_LockArrays(0, 0);
1401 static void R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(const entity_render_t *ent, const texture_t *texture, const msurface_t *surface, const vec3_t lightcolorbase, rtexture_t *basetexture, rtexture_t *normalmaptexture, float colorscale)
1404 model_t *model = ent->model;
1407 const int *elements = model->surfmesh.data_element3i + surface->num_firsttriangle * 3;
1409 // colorscale accounts for how much we multiply the brightness
1412 // mult is how many times the final pass of the lighting will be
1413 // performed to get more brightness than otherwise possible.
1415 // Limit mult to 64 for sanity sake.
1416 if (r_shadow_texture3d.integer && r_textureunits.integer >= 4)
1418 // 3/2 3D combine path (Geforce3, Radeon 8500)
1419 memset(&m, 0, sizeof(m));
1420 m.pointer_vertex = rsurface_vertex3f;
1421 m.tex[0] = R_GetTexture(normalmaptexture);
1422 m.texcombinergb[0] = GL_REPLACE;
1423 m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
1424 m.texmatrix[0] = texture->currenttexmatrix;
1425 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
1426 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1427 m.pointer_texcoord3f[1] = rsurface_array_texcoord3f;
1428 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(rsurface_array_texcoord3f + 3 * surface->num_firstvertex, surface->num_vertices, rsurface_vertex3f + 3 * surface->num_firstvertex, rsurface_svector3f + 3 * surface->num_firstvertex, rsurface_tvector3f + 3 * surface->num_firstvertex, rsurface_normal3f + 3 * surface->num_firstvertex, r_shadow_entitylightorigin);
1429 m.tex3d[2] = R_GetTexture(r_shadow_attenuation3dtexture);
1430 m.pointer_texcoord3f[2] = rsurface_vertex3f;
1431 m.texmatrix[2] = r_shadow_entitytoattenuationxyz;
1433 GL_ColorMask(0,0,0,1);
1434 GL_BlendFunc(GL_ONE, GL_ZERO);
1435 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1436 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
1437 GL_LockArrays(0, 0);
1439 memset(&m, 0, sizeof(m));
1440 m.pointer_vertex = rsurface_vertex3f;
1441 m.tex[0] = R_GetTexture(basetexture);
1442 m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
1443 m.texmatrix[0] = texture->currenttexmatrix;
1444 if (r_shadow_rtlight->currentcubemap != r_texture_whitecube)
1446 m.texcubemap[1] = R_GetTexture(r_shadow_rtlight->currentcubemap);
1447 m.pointer_texcoord3f[1] = rsurface_vertex3f;
1448 m.texmatrix[1] = r_shadow_entitytolight;
1450 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
1452 else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && r_shadow_rtlight->currentcubemap != r_texture_whitecube)
1454 // 1/2/2 3D combine path (original Radeon)
1455 memset(&m, 0, sizeof(m));
1456 m.pointer_vertex = rsurface_vertex3f;
1457 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
1458 m.pointer_texcoord3f[0] = rsurface_vertex3f;
1459 m.texmatrix[0] = r_shadow_entitytoattenuationxyz;
1461 GL_ColorMask(0,0,0,1);
1462 GL_BlendFunc(GL_ONE, GL_ZERO);
1463 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1464 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
1465 GL_LockArrays(0, 0);
1467 memset(&m, 0, sizeof(m));
1468 m.pointer_vertex = rsurface_vertex3f;
1469 m.tex[0] = R_GetTexture(normalmaptexture);
1470 m.texcombinergb[0] = GL_REPLACE;
1471 m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
1472 m.texmatrix[0] = texture->currenttexmatrix;
1473 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
1474 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1475 m.pointer_texcoord3f[1] = rsurface_array_texcoord3f;
1476 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(rsurface_array_texcoord3f + 3 * surface->num_firstvertex, surface->num_vertices, rsurface_vertex3f + 3 * surface->num_firstvertex, rsurface_svector3f + 3 * surface->num_firstvertex, rsurface_tvector3f + 3 * surface->num_firstvertex, rsurface_normal3f + 3 * surface->num_firstvertex, r_shadow_entitylightorigin);
1478 GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
1479 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1480 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
1481 GL_LockArrays(0, 0);
1483 memset(&m, 0, sizeof(m));
1484 m.pointer_vertex = rsurface_vertex3f;
1485 m.tex[0] = R_GetTexture(basetexture);
1486 m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
1487 m.texmatrix[0] = texture->currenttexmatrix;
1488 if (r_shadow_rtlight->currentcubemap != r_texture_whitecube)
1490 m.texcubemap[1] = R_GetTexture(r_shadow_rtlight->currentcubemap);
1491 m.pointer_texcoord3f[1] = rsurface_vertex3f;
1492 m.texmatrix[1] = r_shadow_entitytolight;
1494 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
1496 else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && r_shadow_rtlight->currentcubemap == r_texture_whitecube)
1498 // 2/2 3D combine path (original Radeon)
1499 memset(&m, 0, sizeof(m));
1500 m.pointer_vertex = rsurface_vertex3f;
1501 m.tex[0] = R_GetTexture(normalmaptexture);
1502 m.texcombinergb[0] = GL_REPLACE;
1503 m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
1504 m.texmatrix[0] = texture->currenttexmatrix;
1505 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
1506 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1507 m.pointer_texcoord3f[1] = rsurface_array_texcoord3f;
1508 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(rsurface_array_texcoord3f + 3 * surface->num_firstvertex, surface->num_vertices, rsurface_vertex3f + 3 * surface->num_firstvertex, rsurface_svector3f + 3 * surface->num_firstvertex, rsurface_tvector3f + 3 * surface->num_firstvertex, rsurface_normal3f + 3 * surface->num_firstvertex, r_shadow_entitylightorigin);
1510 GL_ColorMask(0,0,0,1);
1511 GL_BlendFunc(GL_ONE, GL_ZERO);
1512 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1513 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
1514 GL_LockArrays(0, 0);
1516 memset(&m, 0, sizeof(m));
1517 m.pointer_vertex = rsurface_vertex3f;
1518 m.tex[0] = R_GetTexture(basetexture);
1519 m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
1520 m.texmatrix[0] = texture->currenttexmatrix;
1521 m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
1522 m.pointer_texcoord3f[1] = rsurface_vertex3f;
1523 m.texmatrix[1] = r_shadow_entitytoattenuationxyz;
1524 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
1526 else if (r_textureunits.integer >= 4)
1528 // 4/2 2D combine path (Geforce3, Radeon 8500)
1529 memset(&m, 0, sizeof(m));
1530 m.pointer_vertex = rsurface_vertex3f;
1531 m.tex[0] = R_GetTexture(normalmaptexture);
1532 m.texcombinergb[0] = GL_REPLACE;
1533 m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
1534 m.texmatrix[0] = texture->currenttexmatrix;
1535 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
1536 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1537 m.pointer_texcoord3f[1] = rsurface_array_texcoord3f;
1538 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(rsurface_array_texcoord3f + 3 * surface->num_firstvertex, surface->num_vertices, rsurface_vertex3f + 3 * surface->num_firstvertex, rsurface_svector3f + 3 * surface->num_firstvertex, rsurface_tvector3f + 3 * surface->num_firstvertex, rsurface_normal3f + 3 * surface->num_firstvertex, r_shadow_entitylightorigin);
1539 m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
1540 m.pointer_texcoord3f[2] = rsurface_vertex3f;
1541 m.texmatrix[2] = r_shadow_entitytoattenuationxyz;
1542 m.tex[3] = R_GetTexture(r_shadow_attenuation2dtexture);
1543 m.pointer_texcoord3f[3] = rsurface_vertex3f;
1544 m.texmatrix[3] = r_shadow_entitytoattenuationz;
1546 GL_ColorMask(0,0,0,1);
1547 GL_BlendFunc(GL_ONE, GL_ZERO);
1548 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1549 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
1550 GL_LockArrays(0, 0);
1552 memset(&m, 0, sizeof(m));
1553 m.pointer_vertex = rsurface_vertex3f;
1554 m.tex[0] = R_GetTexture(basetexture);
1555 m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
1556 m.texmatrix[0] = texture->currenttexmatrix;
1557 if (r_shadow_rtlight->currentcubemap != r_texture_whitecube)
1559 m.texcubemap[1] = R_GetTexture(r_shadow_rtlight->currentcubemap);
1560 m.pointer_texcoord3f[1] = rsurface_vertex3f;
1561 m.texmatrix[1] = r_shadow_entitytolight;
1563 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
1567 // 2/2/2 2D combine path (any dot3 card)
1568 memset(&m, 0, sizeof(m));
1569 m.pointer_vertex = rsurface_vertex3f;
1570 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
1571 m.pointer_texcoord3f[0] = rsurface_vertex3f;
1572 m.texmatrix[0] = r_shadow_entitytoattenuationxyz;
1573 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1574 m.pointer_texcoord3f[1] = rsurface_vertex3f;
1575 m.texmatrix[1] = r_shadow_entitytoattenuationz;
1577 GL_ColorMask(0,0,0,1);
1578 GL_BlendFunc(GL_ONE, GL_ZERO);
1579 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1580 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
1581 GL_LockArrays(0, 0);
1583 memset(&m, 0, sizeof(m));
1584 m.pointer_vertex = rsurface_vertex3f;
1585 m.tex[0] = R_GetTexture(normalmaptexture);
1586 m.texcombinergb[0] = GL_REPLACE;
1587 m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
1588 m.texmatrix[0] = texture->currenttexmatrix;
1589 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
1590 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1591 m.pointer_texcoord3f[1] = rsurface_array_texcoord3f;
1592 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(rsurface_array_texcoord3f + 3 * surface->num_firstvertex, surface->num_vertices, rsurface_vertex3f + 3 * surface->num_firstvertex, rsurface_svector3f + 3 * surface->num_firstvertex, rsurface_tvector3f + 3 * surface->num_firstvertex, rsurface_normal3f + 3 * surface->num_firstvertex, r_shadow_entitylightorigin);
1594 GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
1595 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1596 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
1597 GL_LockArrays(0, 0);
1599 memset(&m, 0, sizeof(m));
1600 m.pointer_vertex = rsurface_vertex3f;
1601 m.tex[0] = R_GetTexture(basetexture);
1602 m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
1603 m.texmatrix[0] = texture->currenttexmatrix;
1604 if (r_shadow_rtlight->currentcubemap != r_texture_whitecube)
1606 m.texcubemap[1] = R_GetTexture(r_shadow_rtlight->currentcubemap);
1607 m.pointer_texcoord3f[1] = rsurface_vertex3f;
1608 m.texmatrix[1] = r_shadow_entitytolight;
1610 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
1612 // this final code is shared
1614 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 0);
1615 VectorScale(lightcolorbase, colorscale, color2);
1616 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1617 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1619 GL_Color(bound(0, color2[0], 1), bound(0, color2[1], 1), bound(0, color2[2], 1), 1);
1620 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
1622 GL_LockArrays(0, 0);
1625 static void R_Shadow_RenderSurfacesLighting_Light_Dot3_SpecularPass(const entity_render_t *ent, const texture_t *texture, const msurface_t *surface, const vec3_t lightcolorbase, rtexture_t *glosstexture, rtexture_t *normalmaptexture, float colorscale)
1628 model_t *model = ent->model;
1631 const int *elements = model->surfmesh.data_element3i + surface->num_firsttriangle * 3;
1632 // FIXME: detect blendsquare!
1633 //if (!gl_support_blendsquare)
1636 if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && r_shadow_rtlight->currentcubemap != r_texture_whitecube /* && gl_support_blendsquare*/) // FIXME: detect blendsquare!
1638 // 2/0/0/1/2 3D combine blendsquare path
1639 memset(&m, 0, sizeof(m));
1640 m.pointer_vertex = rsurface_vertex3f;
1641 m.tex[0] = R_GetTexture(normalmaptexture);
1642 m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
1643 m.texmatrix[0] = texture->currenttexmatrix;
1644 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
1645 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1646 m.pointer_texcoord3f[1] = rsurface_array_texcoord3f;
1647 R_Shadow_GenTexCoords_Specular_NormalCubeMap(rsurface_array_texcoord3f + 3 * surface->num_firstvertex, surface->num_vertices, rsurface_vertex3f + 3 * surface->num_firstvertex, rsurface_svector3f + 3 * surface->num_firstvertex, rsurface_tvector3f + 3 * surface->num_firstvertex, rsurface_normal3f + 3 * surface->num_firstvertex, r_shadow_entitylightorigin, r_shadow_entityeyeorigin);
1649 GL_ColorMask(0,0,0,1);
1650 // this squares the result
1651 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
1652 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1653 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
1654 GL_LockArrays(0, 0);
1656 memset(&m, 0, sizeof(m));
1657 m.pointer_vertex = rsurface_vertex3f;
1659 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1660 // square alpha in framebuffer a few times to make it shiny
1661 GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
1662 // these comments are a test run through this math for intensity 0.5
1663 // 0.5 * 0.5 = 0.25 (done by the BlendFunc earlier)
1664 // 0.25 * 0.25 = 0.0625 (this is another pass)
1665 // 0.0625 * 0.0625 = 0.00390625 (this is another pass)
1666 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
1667 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
1668 GL_LockArrays(0, 0);
1670 memset(&m, 0, sizeof(m));
1671 m.pointer_vertex = rsurface_vertex3f;
1672 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
1673 m.pointer_texcoord3f[0] = rsurface_vertex3f;
1674 m.texmatrix[0] = r_shadow_entitytoattenuationxyz;
1676 GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
1677 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1678 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
1679 GL_LockArrays(0, 0);
1681 memset(&m, 0, sizeof(m));
1682 m.pointer_vertex = rsurface_vertex3f;
1683 m.tex[0] = R_GetTexture(glosstexture);
1684 m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
1685 m.texmatrix[0] = texture->currenttexmatrix;
1686 if (r_shadow_rtlight->currentcubemap != r_texture_whitecube)
1688 m.texcubemap[1] = R_GetTexture(r_shadow_rtlight->currentcubemap);
1689 m.pointer_texcoord3f[1] = rsurface_vertex3f;
1690 m.texmatrix[1] = r_shadow_entitytolight;
1692 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
1694 else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && r_shadow_rtlight->currentcubemap == r_texture_whitecube /* && gl_support_blendsquare*/) // FIXME: detect blendsquare!
1696 // 2/0/0/2 3D combine blendsquare path
1697 memset(&m, 0, sizeof(m));
1698 m.pointer_vertex = rsurface_vertex3f;
1699 m.tex[0] = R_GetTexture(normalmaptexture);
1700 m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
1701 m.texmatrix[0] = texture->currenttexmatrix;
1702 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
1703 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1704 m.pointer_texcoord3f[1] = rsurface_array_texcoord3f;
1705 R_Shadow_GenTexCoords_Specular_NormalCubeMap(rsurface_array_texcoord3f + 3 * surface->num_firstvertex, surface->num_vertices, rsurface_vertex3f + 3 * surface->num_firstvertex, rsurface_svector3f + 3 * surface->num_firstvertex, rsurface_tvector3f + 3 * surface->num_firstvertex, rsurface_normal3f + 3 * surface->num_firstvertex, r_shadow_entitylightorigin, r_shadow_entityeyeorigin);
1707 GL_ColorMask(0,0,0,1);
1708 // this squares the result
1709 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
1710 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1711 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
1712 GL_LockArrays(0, 0);
1714 memset(&m, 0, sizeof(m));
1715 m.pointer_vertex = rsurface_vertex3f;
1717 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1718 // square alpha in framebuffer a few times to make it shiny
1719 GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
1720 // these comments are a test run through this math for intensity 0.5
1721 // 0.5 * 0.5 = 0.25 (done by the BlendFunc earlier)
1722 // 0.25 * 0.25 = 0.0625 (this is another pass)
1723 // 0.0625 * 0.0625 = 0.00390625 (this is another pass)
1724 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
1725 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
1726 GL_LockArrays(0, 0);
1728 memset(&m, 0, sizeof(m));
1729 m.pointer_vertex = rsurface_vertex3f;
1730 m.tex[0] = R_GetTexture(glosstexture);
1731 m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
1732 m.texmatrix[0] = texture->currenttexmatrix;
1733 m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
1734 m.pointer_texcoord3f[1] = rsurface_vertex3f;
1735 m.texmatrix[1] = r_shadow_entitytoattenuationxyz;
1736 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
1740 // 2/0/0/2/2 2D combine blendsquare path
1741 memset(&m, 0, sizeof(m));
1742 m.pointer_vertex = rsurface_vertex3f;
1743 m.tex[0] = R_GetTexture(normalmaptexture);
1744 m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
1745 m.texmatrix[0] = texture->currenttexmatrix;
1746 m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
1747 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1748 m.pointer_texcoord3f[1] = rsurface_array_texcoord3f;
1749 R_Shadow_GenTexCoords_Specular_NormalCubeMap(rsurface_array_texcoord3f + 3 * surface->num_firstvertex, surface->num_vertices, rsurface_vertex3f + 3 * surface->num_firstvertex, rsurface_svector3f + 3 * surface->num_firstvertex, rsurface_tvector3f + 3 * surface->num_firstvertex, rsurface_normal3f + 3 * surface->num_firstvertex, r_shadow_entitylightorigin, r_shadow_entityeyeorigin);
1751 GL_ColorMask(0,0,0,1);
1752 // this squares the result
1753 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
1754 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1755 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
1756 GL_LockArrays(0, 0);
1758 memset(&m, 0, sizeof(m));
1759 m.pointer_vertex = rsurface_vertex3f;
1761 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1762 // square alpha in framebuffer a few times to make it shiny
1763 GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
1764 // these comments are a test run through this math for intensity 0.5
1765 // 0.5 * 0.5 = 0.25 (done by the BlendFunc earlier)
1766 // 0.25 * 0.25 = 0.0625 (this is another pass)
1767 // 0.0625 * 0.0625 = 0.00390625 (this is another pass)
1768 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
1769 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
1770 GL_LockArrays(0, 0);
1772 memset(&m, 0, sizeof(m));
1773 m.pointer_vertex = rsurface_vertex3f;
1774 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
1775 m.pointer_texcoord3f[0] = rsurface_vertex3f;
1776 m.texmatrix[0] = r_shadow_entitytoattenuationxyz;
1777 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1778 m.pointer_texcoord3f[1] = rsurface_vertex3f;
1779 m.texmatrix[1] = r_shadow_entitytoattenuationz;
1781 GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
1782 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1783 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
1784 GL_LockArrays(0, 0);
1786 memset(&m, 0, sizeof(m));
1787 m.pointer_vertex = rsurface_vertex3f;
1788 m.tex[0] = R_GetTexture(glosstexture);
1789 m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
1790 m.texmatrix[0] = texture->currenttexmatrix;
1791 if (r_shadow_rtlight->currentcubemap != r_texture_whitecube)
1793 m.texcubemap[1] = R_GetTexture(r_shadow_rtlight->currentcubemap);
1794 m.pointer_texcoord3f[1] = rsurface_vertex3f;
1795 m.texmatrix[1] = r_shadow_entitytolight;
1797 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
1800 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 0);
1801 VectorScale(lightcolorbase, colorscale, color2);
1802 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1803 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1805 GL_Color(bound(0, color2[0], 1), bound(0, color2[1], 1), bound(0, color2[2], 1), 1);
1806 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
1808 GL_LockArrays(0, 0);
1811 static void R_Shadow_RenderSurfacesLighting_Light_Dot3(const entity_render_t *ent, const texture_t *texture, int numsurfaces, msurface_t **surfacelist, const vec3_t lightcolorbase, const vec3_t lightcolorpants, const vec3_t lightcolorshirt, rtexture_t *basetexture, rtexture_t *pantstexture, rtexture_t *shirttexture, rtexture_t *normalmaptexture, rtexture_t *glosstexture, float specularscale, qboolean dopants, qboolean doshirt)
1813 // ARB path (any Geforce, any Radeon)
1814 int surfacelistindex;
1815 qboolean doambient = r_shadow_rtlight->ambientscale > 0;
1816 qboolean dodiffuse = r_shadow_rtlight->diffusescale > 0;
1817 qboolean dospecular = specularscale > 0;
1818 if (!doambient && !dodiffuse && !dospecular)
1820 RSurf_PrepareVerticesForBatch(ent, texture, r_shadow_entityeyeorigin, true, true, numsurfaces, surfacelist);
1821 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1823 const msurface_t *surface = surfacelist[surfacelistindex];
1825 R_Shadow_RenderSurfacesLighting_Light_Dot3_AmbientPass(ent, texture, surface, lightcolorbase, basetexture, r_shadow_rtlight->ambientscale);
1827 R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(ent, texture, surface, lightcolorbase, basetexture, normalmaptexture, r_shadow_rtlight->diffusescale);
1831 R_Shadow_RenderSurfacesLighting_Light_Dot3_AmbientPass(ent, texture, surface, lightcolorpants, pantstexture, r_shadow_rtlight->ambientscale);
1833 R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(ent, texture, surface, lightcolorpants, pantstexture, normalmaptexture, r_shadow_rtlight->diffusescale);
1838 R_Shadow_RenderSurfacesLighting_Light_Dot3_AmbientPass(ent, texture, surface, lightcolorshirt, shirttexture, r_shadow_rtlight->ambientscale);
1840 R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(ent, texture, surface, lightcolorshirt, shirttexture, normalmaptexture, r_shadow_rtlight->diffusescale);
1843 R_Shadow_RenderSurfacesLighting_Light_Dot3_SpecularPass(ent, texture, surface, lightcolorbase, glosstexture, normalmaptexture, specularscale);
1847 void R_Shadow_RenderSurfacesLighting_Light_Vertex_Pass(const model_t *model, const msurface_t *surface, vec3_t diffusecolor2, vec3_t ambientcolor2)
1850 const int *elements = model->surfmesh.data_element3i + surface->num_firsttriangle * 3;
1851 R_Shadow_RenderSurfacesLighting_Light_Vertex_Shading(surface, diffusecolor2, ambientcolor2);
1852 for (renders = 0;renders < 64 && (ambientcolor2[0] > renders || ambientcolor2[1] > renders || ambientcolor2[2] > renders || diffusecolor2[0] > renders || diffusecolor2[1] > renders || diffusecolor2[2] > renders);renders++)
1857 // due to low fillrate on the cards this vertex lighting path is
1858 // designed for, we manually cull all triangles that do not
1859 // contain a lit vertex
1862 int newnumtriangles;
1864 int newelements[3072];
1866 newnumtriangles = 0;
1868 for (i = 0, e = elements;i < surface->num_triangles;i++, e += 3)
1870 if (newnumtriangles >= 1024)
1872 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1873 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, newnumtriangles, newelements);
1874 GL_LockArrays(0, 0);
1875 newnumtriangles = 0;
1878 if (VectorLength2(rsurface_array_color4f + e[0] * 4) + VectorLength2(rsurface_array_color4f + e[1] * 4) + VectorLength2(rsurface_array_color4f + e[2] * 4) >= 0.01)
1888 if (newnumtriangles >= 1)
1890 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1891 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, newnumtriangles, newelements);
1892 GL_LockArrays(0, 0);
1898 for (i = 0, c = rsurface_array_color4f + 4 * surface->num_firstvertex;i < surface->num_vertices;i++, c += 4)
1899 if (VectorLength2(c))
1903 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1904 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
1905 GL_LockArrays(0, 0);
1907 // now reduce the intensity for the next overbright pass
1908 for (i = 0, c = rsurface_array_color4f + 4 * surface->num_firstvertex;i < surface->num_vertices;i++, c += 4)
1910 c[0] = max(0, c[0] - 1);
1911 c[1] = max(0, c[1] - 1);
1912 c[2] = max(0, c[2] - 1);
1917 static void R_Shadow_RenderSurfacesLighting_Light_Vertex(const entity_render_t *ent, const texture_t *texture, int numsurfaces, msurface_t **surfacelist, const vec3_t lightcolorbase, const vec3_t lightcolorpants, const vec3_t lightcolorshirt, rtexture_t *basetexture, rtexture_t *pantstexture, rtexture_t *shirttexture, rtexture_t *normalmaptexture, rtexture_t *glosstexture, float specularscale, qboolean dopants, qboolean doshirt)
1919 int surfacelistindex;
1920 model_t *model = ent->model;
1921 float ambientcolorbase[3], diffusecolorbase[3];
1922 float ambientcolorpants[3], diffusecolorpants[3];
1923 float ambientcolorshirt[3], diffusecolorshirt[3];
1925 VectorScale(lightcolorbase, r_shadow_rtlight->ambientscale * 2, ambientcolorbase);
1926 VectorScale(lightcolorbase, r_shadow_rtlight->diffusescale * 2, diffusecolorbase);
1927 VectorScale(lightcolorpants, r_shadow_rtlight->ambientscale * 2, ambientcolorpants);
1928 VectorScale(lightcolorpants, r_shadow_rtlight->diffusescale * 2, diffusecolorpants);
1929 VectorScale(lightcolorshirt, r_shadow_rtlight->ambientscale * 2, ambientcolorshirt);
1930 VectorScale(lightcolorshirt, r_shadow_rtlight->diffusescale * 2, diffusecolorshirt);
1931 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1932 memset(&m, 0, sizeof(m));
1933 m.tex[0] = R_GetTexture(basetexture);
1934 if (r_textureunits.integer >= 2)
1937 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1938 m.texmatrix[1] = r_shadow_entitytoattenuationxyz;
1939 if (r_textureunits.integer >= 3)
1941 // Geforce3/Radeon class but not using dot3
1942 m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
1943 m.texmatrix[2] = r_shadow_entitytoattenuationz;
1946 m.pointer_color = rsurface_array_color4f;
1948 RSurf_PrepareVerticesForBatch(ent, texture, r_shadow_entityeyeorigin, true, false, numsurfaces, surfacelist);
1949 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1951 const msurface_t *surface = surfacelist[surfacelistindex];
1952 // OpenGL 1.1 path (anything)
1953 R_Mesh_TexCoordPointer(0, 2, model->surfmesh.data_texcoordtexture2f);
1954 R_Mesh_TexMatrix(0, &texture->currenttexmatrix);
1955 if (r_textureunits.integer >= 2)
1958 R_Mesh_TexCoordPointer(1, 3, rsurface_vertex3f);
1959 if (r_textureunits.integer >= 3)
1961 // Voodoo4 or Kyro (or Geforce3/Radeon with gl_combine off)
1962 R_Mesh_TexCoordPointer(2, 3, rsurface_vertex3f);
1965 R_Mesh_TexBind(0, R_GetTexture(basetexture));
1966 R_Shadow_RenderSurfacesLighting_Light_Vertex_Pass(model, surface, diffusecolorbase, ambientcolorbase);
1969 R_Mesh_TexBind(0, R_GetTexture(pantstexture));
1970 R_Shadow_RenderSurfacesLighting_Light_Vertex_Pass(model, surface, diffusecolorpants, ambientcolorpants);
1974 R_Mesh_TexBind(0, R_GetTexture(shirttexture));
1975 R_Shadow_RenderSurfacesLighting_Light_Vertex_Pass(model, surface, diffusecolorshirt, ambientcolorshirt);
1980 void R_Shadow_RenderSurfacesLighting(const entity_render_t *ent, const texture_t *texture, int numsurfaces, msurface_t **surfacelist)
1982 // FIXME: support MATERIALFLAG_NODEPTHTEST
1983 vec3_t lightcolorbase, lightcolorpants, lightcolorshirt;
1984 // calculate colors to render this texture with
1985 lightcolorbase[0] = r_shadow_rtlight->currentcolor[0] * ent->colormod[0] * texture->currentalpha;
1986 lightcolorbase[1] = r_shadow_rtlight->currentcolor[1] * ent->colormod[1] * texture->currentalpha;
1987 lightcolorbase[2] = r_shadow_rtlight->currentcolor[2] * ent->colormod[2] * texture->currentalpha;
1988 if ((r_shadow_rtlight->ambientscale + r_shadow_rtlight->diffusescale) * VectorLength2(lightcolorbase) + (r_shadow_rtlight->specularscale * texture->specularscale) * VectorLength2(lightcolorbase) < (1.0f / 1048576.0f))
1990 if ((texture->textureflags & Q3TEXTUREFLAG_TWOSIDED) || (ent->flags & RENDER_NOCULLFACE))
1991 qglDisable(GL_CULL_FACE);
1993 qglEnable(GL_CULL_FACE);
1994 if (texture->colormapping)
1996 qboolean dopants = texture->skin.pants != NULL && VectorLength2(ent->colormap_pantscolor) >= (1.0f / 1048576.0f);
1997 qboolean doshirt = texture->skin.shirt != NULL && VectorLength2(ent->colormap_shirtcolor) >= (1.0f / 1048576.0f);
2000 lightcolorpants[0] = lightcolorbase[0] * ent->colormap_pantscolor[0];
2001 lightcolorpants[1] = lightcolorbase[1] * ent->colormap_pantscolor[1];
2002 lightcolorpants[2] = lightcolorbase[2] * ent->colormap_pantscolor[2];
2005 VectorClear(lightcolorpants);
2008 lightcolorshirt[0] = lightcolorbase[0] * ent->colormap_shirtcolor[0];
2009 lightcolorshirt[1] = lightcolorbase[1] * ent->colormap_shirtcolor[1];
2010 lightcolorshirt[2] = lightcolorbase[2] * ent->colormap_shirtcolor[2];
2013 VectorClear(lightcolorshirt);
2014 switch (r_shadow_rendermode)
2016 case R_SHADOW_RENDERMODE_VISIBLELIGHTING:
2017 R_Shadow_RenderSurfacesLighting_VisibleLighting(ent, texture, numsurfaces, surfacelist, lightcolorbase, lightcolorpants, lightcolorshirt, texture->basetexture, texture->skin.pants, texture->skin.shirt, texture->skin.nmap, texture->glosstexture, r_shadow_rtlight->specularscale * texture->specularscale, dopants, doshirt);
2019 case R_SHADOW_RENDERMODE_LIGHT_GLSL:
2020 R_Shadow_RenderSurfacesLighting_Light_GLSL(ent, texture, numsurfaces, surfacelist, lightcolorbase, lightcolorpants, lightcolorshirt, texture->basetexture, texture->skin.pants, texture->skin.shirt, texture->skin.nmap, texture->glosstexture, r_shadow_rtlight->specularscale * texture->specularscale, dopants, doshirt);
2022 case R_SHADOW_RENDERMODE_LIGHT_DOT3:
2023 R_Shadow_RenderSurfacesLighting_Light_Dot3(ent, texture, numsurfaces, surfacelist, lightcolorbase, lightcolorpants, lightcolorshirt, texture->basetexture, texture->skin.pants, texture->skin.shirt, texture->skin.nmap, texture->glosstexture, r_shadow_rtlight->specularscale * texture->specularscale, dopants, doshirt);
2025 case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
2026 R_Shadow_RenderSurfacesLighting_Light_Vertex(ent, texture, numsurfaces, surfacelist, lightcolorbase, lightcolorpants, lightcolorshirt, texture->basetexture, texture->skin.pants, texture->skin.shirt, texture->skin.nmap, texture->glosstexture, r_shadow_rtlight->specularscale * texture->specularscale, dopants, doshirt);
2029 Con_Printf("R_Shadow_RenderSurfacesLighting: unknown r_shadow_rendermode %i\n", r_shadow_rendermode);
2035 switch (r_shadow_rendermode)
2037 case R_SHADOW_RENDERMODE_VISIBLELIGHTING:
2038 R_Shadow_RenderSurfacesLighting_VisibleLighting(ent, texture, numsurfaces, surfacelist, lightcolorbase, vec3_origin, vec3_origin, texture->basetexture, r_texture_black, r_texture_black, texture->skin.nmap, texture->glosstexture, r_shadow_rtlight->specularscale * texture->specularscale, false, false);
2040 case R_SHADOW_RENDERMODE_LIGHT_GLSL:
2041 R_Shadow_RenderSurfacesLighting_Light_GLSL(ent, texture, numsurfaces, surfacelist, lightcolorbase, vec3_origin, vec3_origin, texture->basetexture, r_texture_black, r_texture_black, texture->skin.nmap, texture->glosstexture, r_shadow_rtlight->specularscale * texture->specularscale, false, false);
2043 case R_SHADOW_RENDERMODE_LIGHT_DOT3:
2044 R_Shadow_RenderSurfacesLighting_Light_Dot3(ent, texture, numsurfaces, surfacelist, lightcolorbase, vec3_origin, vec3_origin, texture->basetexture, r_texture_black, r_texture_black, texture->skin.nmap, texture->glosstexture, r_shadow_rtlight->specularscale * texture->specularscale, false, false);
2046 case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
2047 R_Shadow_RenderSurfacesLighting_Light_Vertex(ent, texture, numsurfaces, surfacelist, lightcolorbase, vec3_origin, vec3_origin, texture->basetexture, r_texture_black, r_texture_black, texture->skin.nmap, texture->glosstexture, r_shadow_rtlight->specularscale * texture->specularscale, false, false);
2050 Con_Printf("R_Shadow_RenderSurfacesLighting: unknown r_shadow_rendermode %i\n", r_shadow_rendermode);
2056 void R_RTLight_Update(dlight_t *light, int isstatic)
2060 rtlight_t *rtlight = &light->rtlight;
2061 R_RTLight_Uncompile(rtlight);
2062 memset(rtlight, 0, sizeof(*rtlight));
2064 VectorCopy(light->origin, rtlight->shadoworigin);
2065 VectorCopy(light->color, rtlight->color);
2066 rtlight->radius = light->radius;
2067 //rtlight->cullradius = rtlight->radius;
2068 //rtlight->cullradius2 = rtlight->radius * rtlight->radius;
2069 rtlight->cullmins[0] = rtlight->shadoworigin[0] - rtlight->radius;
2070 rtlight->cullmins[1] = rtlight->shadoworigin[1] - rtlight->radius;
2071 rtlight->cullmins[2] = rtlight->shadoworigin[2] - rtlight->radius;
2072 rtlight->cullmaxs[0] = rtlight->shadoworigin[0] + rtlight->radius;
2073 rtlight->cullmaxs[1] = rtlight->shadoworigin[1] + rtlight->radius;
2074 rtlight->cullmaxs[2] = rtlight->shadoworigin[2] + rtlight->radius;
2075 rtlight->cubemapname[0] = 0;
2076 if (light->cubemapname[0])
2077 strcpy(rtlight->cubemapname, light->cubemapname);
2078 else if (light->cubemapnum > 0)
2079 sprintf(rtlight->cubemapname, "cubemaps/%i", light->cubemapnum);
2080 rtlight->shadow = light->shadow;
2081 rtlight->corona = light->corona;
2082 rtlight->style = light->style;
2083 rtlight->isstatic = isstatic;
2084 rtlight->coronasizescale = light->coronasizescale;
2085 rtlight->ambientscale = light->ambientscale;
2086 rtlight->diffusescale = light->diffusescale;
2087 rtlight->specularscale = light->specularscale;
2088 rtlight->flags = light->flags;
2089 Matrix4x4_Invert_Simple(&rtlight->matrix_worldtolight, &light->matrix);
2090 // ConcatScale won't work here because this needs to scale rotate and
2091 // translate, not just rotate
2092 scale = 1.0f / rtlight->radius;
2093 for (k = 0;k < 3;k++)
2094 for (j = 0;j < 4;j++)
2095 rtlight->matrix_worldtolight.m[k][j] *= scale;
2098 // compiles rtlight geometry
2099 // (undone by R_FreeCompiledRTLight, which R_UpdateLight calls)
2100 void R_RTLight_Compile(rtlight_t *rtlight)
2102 int shadowmeshes, shadowtris, numleafs, numleafpvsbytes, numsurfaces;
2103 entity_render_t *ent = r_refdef.worldentity;
2104 model_t *model = r_refdef.worldmodel;
2105 unsigned char *data;
2107 // compile the light
2108 rtlight->compiled = true;
2109 rtlight->static_numleafs = 0;
2110 rtlight->static_numleafpvsbytes = 0;
2111 rtlight->static_leaflist = NULL;
2112 rtlight->static_leafpvs = NULL;
2113 rtlight->static_numsurfaces = 0;
2114 rtlight->static_surfacelist = NULL;
2115 rtlight->cullmins[0] = rtlight->shadoworigin[0] - rtlight->radius;
2116 rtlight->cullmins[1] = rtlight->shadoworigin[1] - rtlight->radius;
2117 rtlight->cullmins[2] = rtlight->shadoworigin[2] - rtlight->radius;
2118 rtlight->cullmaxs[0] = rtlight->shadoworigin[0] + rtlight->radius;
2119 rtlight->cullmaxs[1] = rtlight->shadoworigin[1] + rtlight->radius;
2120 rtlight->cullmaxs[2] = rtlight->shadoworigin[2] + rtlight->radius;
2122 if (model && model->GetLightInfo)
2124 // this variable must be set for the CompileShadowVolume code
2125 r_shadow_compilingrtlight = rtlight;
2126 R_Shadow_EnlargeLeafSurfaceBuffer(model->brush.num_leafs, model->num_surfaces);
2127 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);
2128 numleafpvsbytes = (model->brush.num_leafs + 7) >> 3;
2129 data = (unsigned char *)Mem_Alloc(r_main_mempool, sizeof(int) * numleafs + numleafpvsbytes + sizeof(int) * numsurfaces);
2130 rtlight->static_numleafs = numleafs;
2131 rtlight->static_numleafpvsbytes = numleafpvsbytes;
2132 rtlight->static_leaflist = (int *)data;data += sizeof(int) * numleafs;
2133 rtlight->static_leafpvs = (unsigned char *)data;data += numleafpvsbytes;
2134 rtlight->static_numsurfaces = numsurfaces;
2135 rtlight->static_surfacelist = (int *)data;data += sizeof(int) * numsurfaces;
2137 memcpy(rtlight->static_leaflist, r_shadow_buffer_leaflist, rtlight->static_numleafs * sizeof(*rtlight->static_leaflist));
2138 if (numleafpvsbytes)
2139 memcpy(rtlight->static_leafpvs, r_shadow_buffer_leafpvs, rtlight->static_numleafpvsbytes);
2141 memcpy(rtlight->static_surfacelist, r_shadow_buffer_surfacelist, rtlight->static_numsurfaces * sizeof(*rtlight->static_surfacelist));
2142 if (model->CompileShadowVolume && rtlight->shadow)
2143 model->CompileShadowVolume(ent, rtlight->shadoworigin, rtlight->radius, numsurfaces, r_shadow_buffer_surfacelist);
2144 // now we're done compiling the rtlight
2145 r_shadow_compilingrtlight = NULL;
2149 // use smallest available cullradius - box radius or light radius
2150 //rtlight->cullradius = RadiusFromBoundsAndOrigin(rtlight->cullmins, rtlight->cullmaxs, rtlight->shadoworigin);
2151 //rtlight->cullradius = min(rtlight->cullradius, rtlight->radius);
2155 if (rtlight->static_meshchain_shadow)
2158 for (mesh = rtlight->static_meshchain_shadow;mesh;mesh = mesh->next)
2161 shadowtris += mesh->numtriangles;
2165 if (developer.integer >= 10)
2166 Con_Printf("static light built: %f %f %f : %f %f %f box, %i shadow volume triangles (in %i meshes)\n", rtlight->cullmins[0], rtlight->cullmins[1], rtlight->cullmins[2], rtlight->cullmaxs[0], rtlight->cullmaxs[1], rtlight->cullmaxs[2], shadowtris, shadowmeshes);
2169 void R_RTLight_Uncompile(rtlight_t *rtlight)
2171 if (rtlight->compiled)
2173 if (rtlight->static_meshchain_shadow)
2174 Mod_ShadowMesh_Free(rtlight->static_meshchain_shadow);
2175 rtlight->static_meshchain_shadow = NULL;
2176 // these allocations are grouped
2177 if (rtlight->static_leaflist)
2178 Mem_Free(rtlight->static_leaflist);
2179 rtlight->static_numleafs = 0;
2180 rtlight->static_numleafpvsbytes = 0;
2181 rtlight->static_leaflist = NULL;
2182 rtlight->static_leafpvs = NULL;
2183 rtlight->static_numsurfaces = 0;
2184 rtlight->static_surfacelist = NULL;
2185 rtlight->compiled = false;
2189 void R_Shadow_UncompileWorldLights(void)
2192 for (light = r_shadow_worldlightchain;light;light = light->next)
2193 R_RTLight_Uncompile(&light->rtlight);
2196 void R_Shadow_DrawEntityShadow(entity_render_t *ent, int numsurfaces, int *surfacelist)
2198 model_t *model = ent->model;
2199 vec3_t relativeshadoworigin, relativeshadowmins, relativeshadowmaxs;
2200 vec_t relativeshadowradius;
2201 if (ent == r_refdef.worldentity)
2203 if (r_shadow_rtlight->compiled && r_shadow_realtime_world_compile.integer && r_shadow_realtime_world_compileshadow.integer)
2206 R_Mesh_Matrix(&ent->matrix);
2207 for (mesh = r_shadow_rtlight->static_meshchain_shadow;mesh;mesh = mesh->next)
2209 renderstats.lights_shadowtriangles += mesh->numtriangles;
2210 R_Mesh_VertexPointer(mesh->vertex3f);
2211 GL_LockArrays(0, mesh->numverts);
2212 if (r_shadow_rendermode == R_SHADOW_RENDERMODE_STENCIL)
2214 // decrement stencil if backface is behind depthbuffer
2215 qglCullFace(GL_BACK); // quake is backwards, this culls front faces
2216 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
2217 R_Mesh_Draw(0, mesh->numverts, mesh->numtriangles, mesh->element3i);
2218 // increment stencil if frontface is behind depthbuffer
2219 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
2220 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
2222 R_Mesh_Draw(0, mesh->numverts, mesh->numtriangles, mesh->element3i);
2223 GL_LockArrays(0, 0);
2226 else if (numsurfaces)
2228 R_Mesh_Matrix(&ent->matrix);
2229 model->DrawShadowVolume(ent, r_shadow_rtlight->shadoworigin, r_shadow_rtlight->radius, numsurfaces, surfacelist, r_shadow_rtlight->cullmins, r_shadow_rtlight->cullmaxs);
2234 Matrix4x4_Transform(&ent->inversematrix, r_shadow_rtlight->shadoworigin, relativeshadoworigin);
2235 relativeshadowradius = r_shadow_rtlight->radius / ent->scale;
2236 relativeshadowmins[0] = relativeshadoworigin[0] - relativeshadowradius;
2237 relativeshadowmins[1] = relativeshadoworigin[1] - relativeshadowradius;
2238 relativeshadowmins[2] = relativeshadoworigin[2] - relativeshadowradius;
2239 relativeshadowmaxs[0] = relativeshadoworigin[0] + relativeshadowradius;
2240 relativeshadowmaxs[1] = relativeshadoworigin[1] + relativeshadowradius;
2241 relativeshadowmaxs[2] = relativeshadoworigin[2] + relativeshadowradius;
2242 R_Mesh_Matrix(&ent->matrix);
2243 model->DrawShadowVolume(ent, relativeshadoworigin, relativeshadowradius, model->nummodelsurfaces, model->surfacelist, relativeshadowmins, relativeshadowmaxs);
2247 void R_Shadow_SetupEntityLight(const entity_render_t *ent)
2249 // set up properties for rendering light onto this entity
2250 Matrix4x4_Concat(&r_shadow_entitytolight, &r_shadow_rtlight->matrix_worldtolight, &ent->matrix);
2251 Matrix4x4_Concat(&r_shadow_entitytoattenuationxyz, &matrix_attenuationxyz, &r_shadow_entitytolight);
2252 Matrix4x4_Concat(&r_shadow_entitytoattenuationz, &matrix_attenuationz, &r_shadow_entitytolight);
2253 Matrix4x4_Transform(&ent->inversematrix, r_shadow_rtlight->shadoworigin, r_shadow_entitylightorigin);
2254 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, r_shadow_entityeyeorigin);
2255 R_Mesh_Matrix(&ent->matrix);
2258 void R_Shadow_DrawEntityLight(entity_render_t *ent, int numsurfaces, int *surfacelist)
2260 model_t *model = ent->model;
2261 if (!model->DrawLight)
2263 R_Shadow_SetupEntityLight(ent);
2264 if (ent == r_refdef.worldentity)
2265 model->DrawLight(ent, numsurfaces, surfacelist);
2267 model->DrawLight(ent, model->nummodelsurfaces, model->surfacelist);
2270 void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
2274 int numleafs, numsurfaces;
2275 int *leaflist, *surfacelist;
2276 unsigned char *leafpvs;
2277 int numlightentities;
2278 int numshadowentities;
2279 entity_render_t *lightentities[MAX_EDICTS];
2280 entity_render_t *shadowentities[MAX_EDICTS];
2282 // skip lights that don't light because of ambientscale+diffusescale+specularscale being 0 (corona only lights)
2283 // skip lights that are basically invisible (color 0 0 0)
2284 if (VectorLength2(rtlight->color) * (rtlight->ambientscale + rtlight->diffusescale + rtlight->specularscale) < (1.0f / 1048576.0f))
2287 // loading is done before visibility checks because loading should happen
2288 // all at once at the start of a level, not when it stalls gameplay.
2289 // (especially important to benchmarks)
2291 if (rtlight->isstatic && !rtlight->compiled && r_shadow_realtime_world_compile.integer)
2292 R_RTLight_Compile(rtlight);
2294 rtlight->currentcubemap = rtlight->cubemapname[0] ? R_Shadow_Cubemap(rtlight->cubemapname) : r_texture_whitecube;
2296 // look up the light style value at this time
2297 f = (rtlight->style >= 0 ? r_refdef.lightstylevalue[rtlight->style] : 128) * (1.0f / 256.0f) * r_shadow_lightintensityscale.value;
2298 VectorScale(rtlight->color, f, rtlight->currentcolor);
2300 if (rtlight->selected)
2302 f = 2 + sin(realtime * M_PI * 4.0);
2303 VectorScale(rtlight->currentcolor, f, rtlight->currentcolor);
2307 // if lightstyle is currently off, don't draw the light
2308 if (VectorLength2(rtlight->currentcolor) < (1.0f / 1048576.0f))
2311 // if the light box is offscreen, skip it
2312 if (R_CullBox(rtlight->cullmins, rtlight->cullmaxs))
2315 if (rtlight->compiled && r_shadow_realtime_world_compile.integer)
2317 // compiled light, world available and can receive realtime lighting
2318 // retrieve leaf information
2319 numleafs = rtlight->static_numleafs;
2320 leaflist = rtlight->static_leaflist;
2321 leafpvs = rtlight->static_leafpvs;
2322 numsurfaces = rtlight->static_numsurfaces;
2323 surfacelist = rtlight->static_surfacelist;
2325 else if (r_refdef.worldmodel && r_refdef.worldmodel->GetLightInfo)
2327 // dynamic light, world available and can receive realtime lighting
2328 // calculate lit surfaces and leafs
2329 R_Shadow_EnlargeLeafSurfaceBuffer(r_refdef.worldmodel->brush.num_leafs, r_refdef.worldmodel->num_surfaces);
2330 r_refdef.worldmodel->GetLightInfo(r_refdef.worldentity, 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);
2331 leaflist = r_shadow_buffer_leaflist;
2332 leafpvs = r_shadow_buffer_leafpvs;
2333 surfacelist = r_shadow_buffer_surfacelist;
2334 // if the reduced leaf bounds are offscreen, skip it
2335 if (R_CullBox(rtlight->cullmins, rtlight->cullmaxs))
2347 // check if light is illuminating any visible leafs
2350 for (i = 0;i < numleafs;i++)
2351 if (r_worldleafvisible[leaflist[i]])
2356 // set up a scissor rectangle for this light
2357 if (R_Shadow_ScissorForBBox(rtlight->cullmins, rtlight->cullmaxs))
2360 // make a list of lit entities and shadow casting entities
2361 numlightentities = 0;
2362 numshadowentities = 0;
2363 // don't count the world unless some surfaces are actually lit
2366 lightentities[numlightentities++] = r_refdef.worldentity;
2367 shadowentities[numshadowentities++] = r_refdef.worldentity;
2369 // add dynamic entities that are lit by the light
2370 if (r_drawentities.integer)
2372 for (i = 0;i < r_refdef.numentities;i++)
2375 entity_render_t *ent = r_refdef.entities[i];
2376 if (BoxesOverlap(ent->mins, ent->maxs, rtlight->cullmins, rtlight->cullmaxs)
2377 && (model = ent->model)
2378 && !(ent->flags & RENDER_TRANSPARENT)
2379 && (r_refdef.worldmodel == NULL || r_refdef.worldmodel->brush.BoxTouchingLeafPVS == NULL || r_refdef.worldmodel->brush.BoxTouchingLeafPVS(r_refdef.worldmodel, leafpvs, ent->mins, ent->maxs)))
2381 // about the VectorDistance2 - light emitting entities should not cast their own shadow
2382 if ((ent->flags & RENDER_SHADOW) && model->DrawShadowVolume && VectorDistance2(ent->origin, rtlight->shadoworigin) > 0.1)
2383 shadowentities[numshadowentities++] = ent;
2384 if (ent->visframe == r_framecount && (ent->flags & RENDER_LIGHT) && model->DrawLight)
2385 lightentities[numlightentities++] = ent;
2390 // return if there's nothing at all to light
2391 if (!numlightentities)
2394 // don't let sound skip if going slow
2395 if (r_refdef.extraupdate)
2398 // make this the active rtlight for rendering purposes
2399 R_Shadow_RenderMode_ActiveLight(rtlight);
2400 // count this light in the r_speeds
2401 renderstats.lights++;
2404 if (numshadowentities && rtlight->shadow && (rtlight->isstatic ? r_rtworldshadows : r_rtdlightshadows))
2406 // draw stencil shadow volumes to mask off pixels that are in shadow
2407 // so that they won't receive lighting
2411 R_Shadow_RenderMode_StencilShadowVolumes();
2412 for (i = 0;i < numshadowentities;i++)
2413 R_Shadow_DrawEntityShadow(shadowentities[i], numsurfaces, surfacelist);
2416 // optionally draw visible shape of the shadow volumes
2417 // for performance analysis by level designers
2418 if (r_showshadowvolumes.integer)
2420 R_Shadow_RenderMode_VisibleShadowVolumes();
2421 for (i = 0;i < numshadowentities;i++)
2422 R_Shadow_DrawEntityShadow(shadowentities[i], numsurfaces, surfacelist);
2426 if (numlightentities)
2428 // draw lighting in the unmasked areas
2429 R_Shadow_RenderMode_Lighting(usestencil, false);
2430 for (i = 0;i < numlightentities;i++)
2431 R_Shadow_DrawEntityLight(lightentities[i], numsurfaces, surfacelist);
2433 // optionally draw the illuminated areas
2434 // for performance analysis by level designers
2435 if (r_showlighting.integer)
2437 R_Shadow_RenderMode_VisibleLighting(usestencil && !r_showdisabledepthtest.integer, false);
2438 for (i = 0;i < numlightentities;i++)
2439 R_Shadow_DrawEntityLight(lightentities[i], numsurfaces, surfacelist);
2444 void R_ShadowVolumeLighting(qboolean visible)
2449 if (r_refdef.worldmodel && strncmp(r_refdef.worldmodel->name, r_shadow_mapname, sizeof(r_shadow_mapname)))
2450 R_Shadow_EditLights_Reload_f();
2452 R_Shadow_RenderMode_Begin();
2454 flag = r_rtworld ? LIGHTFLAG_REALTIMEMODE : LIGHTFLAG_NORMALMODE;
2455 if (r_shadow_debuglight.integer >= 0)
2457 for (lnum = 0, light = r_shadow_worldlightchain;light;lnum++, light = light->next)
2458 if (lnum == r_shadow_debuglight.integer && (light->flags & flag))
2459 R_DrawRTLight(&light->rtlight, visible);
2462 for (lnum = 0, light = r_shadow_worldlightchain;light;lnum++, light = light->next)
2463 if (light->flags & flag)
2464 R_DrawRTLight(&light->rtlight, visible);
2466 for (lnum = 0;lnum < r_refdef.numlights;lnum++)
2467 R_DrawRTLight(&r_refdef.lights[lnum]->rtlight, visible);
2469 R_Shadow_RenderMode_End();
2472 //static char *suffix[6] = {"ft", "bk", "rt", "lf", "up", "dn"};
2473 typedef struct suffixinfo_s
2476 qboolean flipx, flipy, flipdiagonal;
2479 static suffixinfo_t suffix[3][6] =
2482 {"px", false, false, false},
2483 {"nx", false, false, false},
2484 {"py", false, false, false},
2485 {"ny", false, false, false},
2486 {"pz", false, false, false},
2487 {"nz", false, false, false}
2490 {"posx", false, false, false},
2491 {"negx", false, false, false},
2492 {"posy", false, false, false},
2493 {"negy", false, false, false},
2494 {"posz", false, false, false},
2495 {"negz", false, false, false}
2498 {"rt", true, false, true},
2499 {"lf", false, true, true},
2500 {"ft", true, true, false},
2501 {"bk", false, false, false},
2502 {"up", true, false, true},
2503 {"dn", true, false, true}
2507 static int componentorder[4] = {0, 1, 2, 3};
2509 rtexture_t *R_Shadow_LoadCubemap(const char *basename)
2511 int i, j, cubemapsize;
2512 unsigned char *cubemappixels, *image_rgba;
2513 rtexture_t *cubemaptexture;
2515 // must start 0 so the first loadimagepixels has no requested width/height
2517 cubemappixels = NULL;
2518 cubemaptexture = NULL;
2519 // keep trying different suffix groups (posx, px, rt) until one loads
2520 for (j = 0;j < 3 && !cubemappixels;j++)
2522 // load the 6 images in the suffix group
2523 for (i = 0;i < 6;i++)
2525 // generate an image name based on the base and and suffix
2526 dpsnprintf(name, sizeof(name), "%s%s", basename, suffix[j][i].suffix);
2528 if ((image_rgba = loadimagepixels(name, false, cubemapsize, cubemapsize)))
2530 // an image loaded, make sure width and height are equal
2531 if (image_width == image_height)
2533 // if this is the first image to load successfully, allocate the cubemap memory
2534 if (!cubemappixels && image_width >= 1)
2536 cubemapsize = image_width;
2537 // note this clears to black, so unavailable sides are black
2538 cubemappixels = (unsigned char *)Mem_Alloc(tempmempool, 6*cubemapsize*cubemapsize*4);
2540 // copy the image with any flipping needed by the suffix (px and posx types don't need flipping)
2542 Image_CopyMux(cubemappixels+i*cubemapsize*cubemapsize*4, image_rgba, cubemapsize, cubemapsize, suffix[j][i].flipx, suffix[j][i].flipy, suffix[j][i].flipdiagonal, 4, 4, componentorder);
2545 Con_Printf("Cubemap image \"%s\" (%ix%i) is not square, OpenGL requires square cubemaps.\n", name, image_width, image_height);
2547 Mem_Free(image_rgba);
2551 // if a cubemap loaded, upload it
2554 if (!r_shadow_filters_texturepool)
2555 r_shadow_filters_texturepool = R_AllocTexturePool();
2556 cubemaptexture = R_LoadTextureCubeMap(r_shadow_filters_texturepool, basename, cubemapsize, cubemappixels, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
2557 Mem_Free(cubemappixels);
2561 Con_Printf("Failed to load Cubemap \"%s\", tried ", basename);
2562 for (j = 0;j < 3;j++)
2563 for (i = 0;i < 6;i++)
2564 Con_Printf("%s\"%s%s.tga\"", j + i > 0 ? ", " : "", basename, suffix[j][i].suffix);
2565 Con_Print(" and was unable to find any of them.\n");
2567 return cubemaptexture;
2570 rtexture_t *R_Shadow_Cubemap(const char *basename)
2573 for (i = 0;i < numcubemaps;i++)
2574 if (!strcasecmp(cubemaps[i].basename, basename))
2575 return cubemaps[i].texture;
2576 if (i >= MAX_CUBEMAPS)
2577 return r_texture_whitecube;
2579 strcpy(cubemaps[i].basename, basename);
2580 cubemaps[i].texture = R_Shadow_LoadCubemap(cubemaps[i].basename);
2581 if (!cubemaps[i].texture)
2582 cubemaps[i].texture = r_texture_whitecube;
2583 return cubemaps[i].texture;
2586 void R_Shadow_FreeCubemaps(void)
2589 R_FreeTexturePool(&r_shadow_filters_texturepool);
2592 dlight_t *R_Shadow_NewWorldLight(void)
2595 light = (dlight_t *)Mem_Alloc(r_main_mempool, sizeof(dlight_t));
2596 light->next = r_shadow_worldlightchain;
2597 r_shadow_worldlightchain = light;
2601 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)
2603 VectorCopy(origin, light->origin);
2604 light->angles[0] = angles[0] - 360 * floor(angles[0] / 360);
2605 light->angles[1] = angles[1] - 360 * floor(angles[1] / 360);
2606 light->angles[2] = angles[2] - 360 * floor(angles[2] / 360);
2607 light->color[0] = max(color[0], 0);
2608 light->color[1] = max(color[1], 0);
2609 light->color[2] = max(color[2], 0);
2610 light->radius = max(radius, 0);
2611 light->style = style;
2612 if (light->style < 0 || light->style >= MAX_LIGHTSTYLES)
2614 Con_Printf("R_Shadow_NewWorldLight: invalid light style number %i, must be >= 0 and < %i\n", light->style, MAX_LIGHTSTYLES);
2617 light->shadow = shadowenable;
2618 light->corona = corona;
2621 strlcpy(light->cubemapname, cubemapname, sizeof(light->cubemapname));
2622 light->coronasizescale = coronasizescale;
2623 light->ambientscale = ambientscale;
2624 light->diffusescale = diffusescale;
2625 light->specularscale = specularscale;
2626 light->flags = flags;
2627 Matrix4x4_CreateFromQuakeEntity(&light->matrix, light->origin[0], light->origin[1], light->origin[2], light->angles[0], light->angles[1], light->angles[2], 1);
2629 R_RTLight_Update(light, true);
2632 void R_Shadow_FreeWorldLight(dlight_t *light)
2634 dlight_t **lightpointer;
2635 R_RTLight_Uncompile(&light->rtlight);
2636 for (lightpointer = &r_shadow_worldlightchain;*lightpointer && *lightpointer != light;lightpointer = &(*lightpointer)->next);
2637 if (*lightpointer != light)
2638 Sys_Error("R_Shadow_FreeWorldLight: light not linked into chain");
2639 *lightpointer = light->next;
2643 void R_Shadow_ClearWorldLights(void)
2645 while (r_shadow_worldlightchain)
2646 R_Shadow_FreeWorldLight(r_shadow_worldlightchain);
2647 r_shadow_selectedlight = NULL;
2648 R_Shadow_FreeCubemaps();
2651 void R_Shadow_SelectLight(dlight_t *light)
2653 if (r_shadow_selectedlight)
2654 r_shadow_selectedlight->selected = false;
2655 r_shadow_selectedlight = light;
2656 if (r_shadow_selectedlight)
2657 r_shadow_selectedlight->selected = true;
2660 void R_Shadow_DrawCursor_TransparentCallback(const entity_render_t *ent, int surfacenumber, const rtlight_t *rtlight)
2662 float scale = r_editlights_cursorgrid.value * 0.5f;
2663 R_DrawSprite(GL_SRC_ALPHA, GL_ONE, r_crosshairs[1]->tex, NULL, false, r_editlights_cursorlocation, r_viewright, r_viewup, scale, -scale, -scale, scale, 1, 1, 1, 0.5f);
2666 void R_Shadow_DrawLightSprite_TransparentCallback(const entity_render_t *ent, int surfacenumber, const rtlight_t *rtlight)
2669 const dlight_t *light = (dlight_t *)ent;
2671 if (light->selected)
2672 intensity = 0.75 + 0.25 * sin(realtime * M_PI * 4.0);
2675 R_DrawSprite(GL_SRC_ALPHA, GL_ONE, r_crosshairs[surfacenumber]->tex, NULL, false, light->origin, r_viewright, r_viewup, 8, -8, -8, 8, intensity, intensity, intensity, 0.5);
2678 void R_Shadow_DrawLightSprites(void)
2683 for (i = 0, light = r_shadow_worldlightchain;light;i++, light = light->next)
2684 R_MeshQueue_AddTransparent(light->origin, R_Shadow_DrawLightSprite_TransparentCallback, (entity_render_t *)light, 1+(i % 5), &light->rtlight);
2685 R_MeshQueue_AddTransparent(r_editlights_cursorlocation, R_Shadow_DrawCursor_TransparentCallback, NULL, 0, NULL);
2688 void R_Shadow_SelectLightInView(void)
2690 float bestrating, rating, temp[3];
2691 dlight_t *best, *light;
2694 for (light = r_shadow_worldlightchain;light;light = light->next)
2696 VectorSubtract(light->origin, r_vieworigin, temp);
2697 rating = (DotProduct(temp, r_viewforward) / sqrt(DotProduct(temp, temp)));
2700 rating /= (1 + 0.0625f * sqrt(DotProduct(temp, temp)));
2701 if (bestrating < rating && CL_TraceBox(light->origin, vec3_origin, vec3_origin, r_vieworigin, true, NULL, SUPERCONTENTS_SOLID, false).fraction == 1.0f)
2703 bestrating = rating;
2708 R_Shadow_SelectLight(best);
2711 void R_Shadow_LoadWorldLights(void)
2713 int n, a, style, shadow, flags;
2714 char tempchar, *lightsstring, *s, *t, name[MAX_QPATH], cubemapname[MAX_QPATH];
2715 float origin[3], radius, color[3], angles[3], corona, coronasizescale, ambientscale, diffusescale, specularscale;
2716 if (r_refdef.worldmodel == NULL)
2718 Con_Print("No map loaded.\n");
2721 FS_StripExtension (r_refdef.worldmodel->name, name, sizeof (name));
2722 strlcat (name, ".rtlights", sizeof (name));
2723 lightsstring = (char *)FS_LoadFile(name, tempmempool, false, NULL);
2733 for (;COM_Parse(t, true) && strcmp(
2734 if (COM_Parse(t, true))
2736 if (com_token[0] == '!')
2739 origin[0] = atof(com_token+1);
2742 origin[0] = atof(com_token);
2747 while (*s && *s != '\n' && *s != '\r')
2753 // check for modifier flags
2760 a = sscanf(t, "%f %f %f %f %f %f %f %d %s %f %f %f %f %f %f %f %f %i", &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);
2763 flags = LIGHTFLAG_REALTIMEMODE;
2771 coronasizescale = 0.25f;
2773 VectorClear(angles);
2776 if (a < 9 || !strcmp(cubemapname, "\"\""))
2778 // remove quotes on cubemapname
2779 if (cubemapname[0] == '"' && cubemapname[strlen(cubemapname) - 1] == '"')
2781 cubemapname[strlen(cubemapname)-1] = 0;
2782 strcpy(cubemapname, cubemapname + 1);
2786 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);
2789 R_Shadow_UpdateWorldLight(R_Shadow_NewWorldLight(), origin, angles, color, radius, corona, style, shadow, cubemapname, coronasizescale, ambientscale, diffusescale, specularscale, flags);
2797 Con_Printf("invalid rtlights file \"%s\"\n", name);
2798 Mem_Free(lightsstring);
2802 void R_Shadow_SaveWorldLights(void)
2805 size_t bufchars, bufmaxchars;
2807 char name[MAX_QPATH];
2808 char line[MAX_INPUTLINE];
2809 if (!r_shadow_worldlightchain)
2811 if (r_refdef.worldmodel == NULL)
2813 Con_Print("No map loaded.\n");
2816 FS_StripExtension (r_refdef.worldmodel->name, name, sizeof (name));
2817 strlcat (name, ".rtlights", sizeof (name));
2818 bufchars = bufmaxchars = 0;
2820 for (light = r_shadow_worldlightchain;light;light = light->next)
2822 if (light->coronasizescale != 0.25f || light->ambientscale != 0 || light->diffusescale != 1 || light->specularscale != 1 || light->flags != LIGHTFLAG_REALTIMEMODE)
2823 sprintf(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);
2824 else if (light->cubemapname[0] || light->corona || light->angles[0] || light->angles[1] || light->angles[2])
2825 sprintf(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]);
2827 sprintf(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);
2828 if (bufchars + strlen(line) > bufmaxchars)
2830 bufmaxchars = bufchars + strlen(line) + 2048;
2832 buf = (char *)Mem_Alloc(tempmempool, bufmaxchars);
2836 memcpy(buf, oldbuf, bufchars);
2842 memcpy(buf + bufchars, line, strlen(line));
2843 bufchars += strlen(line);
2847 FS_WriteFile(name, buf, (fs_offset_t)bufchars);
2852 void R_Shadow_LoadLightsFile(void)
2855 char tempchar, *lightsstring, *s, *t, name[MAX_QPATH];
2856 float origin[3], radius, color[3], subtract, spotdir[3], spotcone, falloff, distbias;
2857 if (r_refdef.worldmodel == NULL)
2859 Con_Print("No map loaded.\n");
2862 FS_StripExtension (r_refdef.worldmodel->name, name, sizeof (name));
2863 strlcat (name, ".lights", sizeof (name));
2864 lightsstring = (char *)FS_LoadFile(name, tempmempool, false, NULL);
2872 while (*s && *s != '\n' && *s != '\r')
2878 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);
2882 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);
2885 radius = sqrt(DotProduct(color, color) / (falloff * falloff * 8192.0f * 8192.0f));
2886 radius = bound(15, radius, 4096);
2887 VectorScale(color, (2.0f / (8388608.0f)), color);
2888 R_Shadow_UpdateWorldLight(R_Shadow_NewWorldLight(), origin, vec3_origin, color, radius, 0, style, true, NULL, 0.25, 0, 1, 1, LIGHTFLAG_REALTIMEMODE);
2896 Con_Printf("invalid lights file \"%s\"\n", name);
2897 Mem_Free(lightsstring);
2901 // tyrlite/hmap2 light types in the delay field
2902 typedef enum lighttype_e {LIGHTTYPE_MINUSX, LIGHTTYPE_RECIPX, LIGHTTYPE_RECIPXX, LIGHTTYPE_NONE, LIGHTTYPE_SUN, LIGHTTYPE_MINUSXX} lighttype_t;
2904 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void)
2906 int entnum, style, islight, skin, pflags, effects, type, n;
2909 float origin[3], angles[3], radius, color[3], light[4], fadescale, lightscale, originhack[3], overridecolor[3], vec[4];
2910 char key[256], value[MAX_INPUTLINE];
2912 if (r_refdef.worldmodel == NULL)
2914 Con_Print("No map loaded.\n");
2917 // try to load a .ent file first
2918 FS_StripExtension (r_refdef.worldmodel->name, key, sizeof (key));
2919 strlcat (key, ".ent", sizeof (key));
2920 data = entfiledata = (char *)FS_LoadFile(key, tempmempool, true, NULL);
2921 // and if that is not found, fall back to the bsp file entity string
2923 data = r_refdef.worldmodel->brush.entities;
2926 for (entnum = 0;COM_ParseToken(&data, false) && com_token[0] == '{';entnum++)
2928 type = LIGHTTYPE_MINUSX;
2929 origin[0] = origin[1] = origin[2] = 0;
2930 originhack[0] = originhack[1] = originhack[2] = 0;
2931 angles[0] = angles[1] = angles[2] = 0;
2932 color[0] = color[1] = color[2] = 1;
2933 light[0] = light[1] = light[2] = 1;light[3] = 300;
2934 overridecolor[0] = overridecolor[1] = overridecolor[2] = 1;
2944 if (!COM_ParseToken(&data, false))
2946 if (com_token[0] == '}')
2947 break; // end of entity
2948 if (com_token[0] == '_')
2949 strcpy(key, com_token + 1);
2951 strcpy(key, com_token);
2952 while (key[strlen(key)-1] == ' ') // remove trailing spaces
2953 key[strlen(key)-1] = 0;
2954 if (!COM_ParseToken(&data, false))
2956 strcpy(value, com_token);
2958 // now that we have the key pair worked out...
2959 if (!strcmp("light", key))
2961 n = sscanf(value, "%f %f %f %f", &vec[0], &vec[1], &vec[2], &vec[3]);
2965 light[0] = vec[0] * (1.0f / 256.0f);
2966 light[1] = vec[0] * (1.0f / 256.0f);
2967 light[2] = vec[0] * (1.0f / 256.0f);
2973 light[0] = vec[0] * (1.0f / 255.0f);
2974 light[1] = vec[1] * (1.0f / 255.0f);
2975 light[2] = vec[2] * (1.0f / 255.0f);
2979 else if (!strcmp("delay", key))
2981 else if (!strcmp("origin", key))
2982 sscanf(value, "%f %f %f", &origin[0], &origin[1], &origin[2]);
2983 else if (!strcmp("angle", key))
2984 angles[0] = 0, angles[1] = atof(value), angles[2] = 0;
2985 else if (!strcmp("angles", key))
2986 sscanf(value, "%f %f %f", &angles[0], &angles[1], &angles[2]);
2987 else if (!strcmp("color", key))
2988 sscanf(value, "%f %f %f", &color[0], &color[1], &color[2]);
2989 else if (!strcmp("wait", key))
2990 fadescale = atof(value);
2991 else if (!strcmp("classname", key))
2993 if (!strncmp(value, "light", 5))
2996 if (!strcmp(value, "light_fluoro"))
3001 overridecolor[0] = 1;
3002 overridecolor[1] = 1;
3003 overridecolor[2] = 1;
3005 if (!strcmp(value, "light_fluorospark"))
3010 overridecolor[0] = 1;
3011 overridecolor[1] = 1;
3012 overridecolor[2] = 1;
3014 if (!strcmp(value, "light_globe"))
3019 overridecolor[0] = 1;
3020 overridecolor[1] = 0.8;
3021 overridecolor[2] = 0.4;
3023 if (!strcmp(value, "light_flame_large_yellow"))
3028 overridecolor[0] = 1;
3029 overridecolor[1] = 0.5;
3030 overridecolor[2] = 0.1;
3032 if (!strcmp(value, "light_flame_small_yellow"))
3037 overridecolor[0] = 1;
3038 overridecolor[1] = 0.5;
3039 overridecolor[2] = 0.1;
3041 if (!strcmp(value, "light_torch_small_white"))
3046 overridecolor[0] = 1;
3047 overridecolor[1] = 0.5;
3048 overridecolor[2] = 0.1;
3050 if (!strcmp(value, "light_torch_small_walltorch"))
3055 overridecolor[0] = 1;
3056 overridecolor[1] = 0.5;
3057 overridecolor[2] = 0.1;
3061 else if (!strcmp("style", key))
3062 style = atoi(value);
3063 else if (!strcmp("skin", key))
3064 skin = (int)atof(value);
3065 else if (!strcmp("pflags", key))
3066 pflags = (int)atof(value);
3067 else if (!strcmp("effects", key))
3068 effects = (int)atof(value);
3069 else if (r_refdef.worldmodel->type == mod_brushq3)
3071 if (!strcmp("scale", key))
3072 lightscale = atof(value);
3073 if (!strcmp("fade", key))
3074 fadescale = atof(value);
3079 if (lightscale <= 0)
3083 if (color[0] == color[1] && color[0] == color[2])
3085 color[0] *= overridecolor[0];
3086 color[1] *= overridecolor[1];
3087 color[2] *= overridecolor[2];
3089 radius = light[3] * r_editlights_quakelightsizescale.value * lightscale / fadescale;
3090 color[0] = color[0] * light[0];
3091 color[1] = color[1] * light[1];
3092 color[2] = color[2] * light[2];
3095 case LIGHTTYPE_MINUSX:
3097 case LIGHTTYPE_RECIPX:
3099 VectorScale(color, (1.0f / 16.0f), color);
3101 case LIGHTTYPE_RECIPXX:
3103 VectorScale(color, (1.0f / 16.0f), color);
3106 case LIGHTTYPE_NONE:
3110 case LIGHTTYPE_MINUSXX:
3113 VectorAdd(origin, originhack, origin);
3115 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);
3118 Mem_Free(entfiledata);
3122 void R_Shadow_SetCursorLocationForView(void)
3125 vec3_t dest, endpos;
3127 VectorMA(r_vieworigin, r_editlights_cursordistance.value, r_viewforward, dest);
3128 trace = CL_TraceBox(r_vieworigin, vec3_origin, vec3_origin, dest, true, NULL, SUPERCONTENTS_SOLID, false);
3129 if (trace.fraction < 1)
3131 dist = trace.fraction * r_editlights_cursordistance.value;
3132 push = r_editlights_cursorpushback.value;
3136 VectorMA(trace.endpos, push, r_viewforward, endpos);
3137 VectorMA(endpos, r_editlights_cursorpushoff.value, trace.plane.normal, endpos);
3141 VectorClear( endpos );
3143 r_editlights_cursorlocation[0] = floor(endpos[0] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
3144 r_editlights_cursorlocation[1] = floor(endpos[1] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
3145 r_editlights_cursorlocation[2] = floor(endpos[2] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
3148 void R_Shadow_UpdateWorldLightSelection(void)
3150 if (r_editlights.integer)
3152 R_Shadow_SetCursorLocationForView();
3153 R_Shadow_SelectLightInView();
3154 R_Shadow_DrawLightSprites();
3157 R_Shadow_SelectLight(NULL);
3160 void R_Shadow_EditLights_Clear_f(void)
3162 R_Shadow_ClearWorldLights();
3165 void R_Shadow_EditLights_Reload_f(void)
3167 if (!r_refdef.worldmodel)
3169 strlcpy(r_shadow_mapname, r_refdef.worldmodel->name, sizeof(r_shadow_mapname));
3170 R_Shadow_ClearWorldLights();
3171 R_Shadow_LoadWorldLights();
3172 if (r_shadow_worldlightchain == NULL)
3174 R_Shadow_LoadLightsFile();
3175 if (r_shadow_worldlightchain == NULL)
3176 R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
3180 void R_Shadow_EditLights_Save_f(void)
3182 if (!r_refdef.worldmodel)
3184 R_Shadow_SaveWorldLights();
3187 void R_Shadow_EditLights_ImportLightEntitiesFromMap_f(void)
3189 R_Shadow_ClearWorldLights();
3190 R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
3193 void R_Shadow_EditLights_ImportLightsFile_f(void)
3195 R_Shadow_ClearWorldLights();
3196 R_Shadow_LoadLightsFile();
3199 void R_Shadow_EditLights_Spawn_f(void)
3202 if (!r_editlights.integer)
3204 Con_Print("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
3207 if (Cmd_Argc() != 1)
3209 Con_Print("r_editlights_spawn does not take parameters\n");
3212 color[0] = color[1] = color[2] = 1;
3213 R_Shadow_UpdateWorldLight(R_Shadow_NewWorldLight(), r_editlights_cursorlocation, vec3_origin, color, 200, 0, 0, true, NULL, 0.25, 0, 1, 1, LIGHTFLAG_REALTIMEMODE);
3216 void R_Shadow_EditLights_Edit_f(void)
3218 vec3_t origin, angles, color;
3219 vec_t radius, corona, coronasizescale, ambientscale, diffusescale, specularscale;
3220 int style, shadows, flags, normalmode, realtimemode;
3221 char cubemapname[MAX_INPUTLINE];
3222 if (!r_editlights.integer)
3224 Con_Print("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
3227 if (!r_shadow_selectedlight)
3229 Con_Print("No selected light.\n");
3232 VectorCopy(r_shadow_selectedlight->origin, origin);
3233 VectorCopy(r_shadow_selectedlight->angles, angles);
3234 VectorCopy(r_shadow_selectedlight->color, color);
3235 radius = r_shadow_selectedlight->radius;
3236 style = r_shadow_selectedlight->style;
3237 if (r_shadow_selectedlight->cubemapname)
3238 strlcpy(cubemapname, r_shadow_selectedlight->cubemapname, sizeof(cubemapname));
3241 shadows = r_shadow_selectedlight->shadow;
3242 corona = r_shadow_selectedlight->corona;
3243 coronasizescale = r_shadow_selectedlight->coronasizescale;
3244 ambientscale = r_shadow_selectedlight->ambientscale;
3245 diffusescale = r_shadow_selectedlight->diffusescale;
3246 specularscale = r_shadow_selectedlight->specularscale;
3247 flags = r_shadow_selectedlight->flags;
3248 normalmode = (flags & LIGHTFLAG_NORMALMODE) != 0;
3249 realtimemode = (flags & LIGHTFLAG_REALTIMEMODE) != 0;
3250 if (!strcmp(Cmd_Argv(1), "origin"))
3252 if (Cmd_Argc() != 5)
3254 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(1));
3257 origin[0] = atof(Cmd_Argv(2));
3258 origin[1] = atof(Cmd_Argv(3));
3259 origin[2] = atof(Cmd_Argv(4));
3261 else if (!strcmp(Cmd_Argv(1), "originx"))
3263 if (Cmd_Argc() != 3)
3265 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3268 origin[0] = atof(Cmd_Argv(2));
3270 else if (!strcmp(Cmd_Argv(1), "originy"))
3272 if (Cmd_Argc() != 3)
3274 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3277 origin[1] = atof(Cmd_Argv(2));
3279 else if (!strcmp(Cmd_Argv(1), "originz"))
3281 if (Cmd_Argc() != 3)
3283 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3286 origin[2] = atof(Cmd_Argv(2));
3288 else if (!strcmp(Cmd_Argv(1), "move"))
3290 if (Cmd_Argc() != 5)
3292 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(1));
3295 origin[0] += atof(Cmd_Argv(2));
3296 origin[1] += atof(Cmd_Argv(3));
3297 origin[2] += atof(Cmd_Argv(4));
3299 else if (!strcmp(Cmd_Argv(1), "movex"))
3301 if (Cmd_Argc() != 3)
3303 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3306 origin[0] += atof(Cmd_Argv(2));
3308 else if (!strcmp(Cmd_Argv(1), "movey"))
3310 if (Cmd_Argc() != 3)
3312 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3315 origin[1] += atof(Cmd_Argv(2));
3317 else if (!strcmp(Cmd_Argv(1), "movez"))
3319 if (Cmd_Argc() != 3)
3321 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3324 origin[2] += atof(Cmd_Argv(2));
3326 else if (!strcmp(Cmd_Argv(1), "angles"))
3328 if (Cmd_Argc() != 5)
3330 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(1));
3333 angles[0] = atof(Cmd_Argv(2));
3334 angles[1] = atof(Cmd_Argv(3));
3335 angles[2] = atof(Cmd_Argv(4));
3337 else if (!strcmp(Cmd_Argv(1), "anglesx"))
3339 if (Cmd_Argc() != 3)
3341 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3344 angles[0] = atof(Cmd_Argv(2));
3346 else if (!strcmp(Cmd_Argv(1), "anglesy"))
3348 if (Cmd_Argc() != 3)
3350 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3353 angles[1] = atof(Cmd_Argv(2));
3355 else if (!strcmp(Cmd_Argv(1), "anglesz"))
3357 if (Cmd_Argc() != 3)
3359 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3362 angles[2] = atof(Cmd_Argv(2));
3364 else if (!strcmp(Cmd_Argv(1), "color"))
3366 if (Cmd_Argc() != 5)
3368 Con_Printf("usage: r_editlights_edit %s red green blue\n", Cmd_Argv(1));
3371 color[0] = atof(Cmd_Argv(2));
3372 color[1] = atof(Cmd_Argv(3));
3373 color[2] = atof(Cmd_Argv(4));
3375 else if (!strcmp(Cmd_Argv(1), "radius"))
3377 if (Cmd_Argc() != 3)
3379 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3382 radius = atof(Cmd_Argv(2));
3384 else if (!strcmp(Cmd_Argv(1), "colorscale"))
3386 if (Cmd_Argc() == 3)
3388 double scale = atof(Cmd_Argv(2));
3395 if (Cmd_Argc() != 5)
3397 Con_Printf("usage: r_editlights_edit %s red green blue (OR grey instead of red green blue)\n", Cmd_Argv(1));
3400 color[0] *= atof(Cmd_Argv(2));
3401 color[1] *= atof(Cmd_Argv(3));
3402 color[2] *= atof(Cmd_Argv(4));
3405 else if (!strcmp(Cmd_Argv(1), "radiusscale") || !strcmp(Cmd_Argv(1), "sizescale"))
3407 if (Cmd_Argc() != 3)
3409 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3412 radius *= atof(Cmd_Argv(2));
3414 else if (!strcmp(Cmd_Argv(1), "style"))
3416 if (Cmd_Argc() != 3)
3418 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3421 style = atoi(Cmd_Argv(2));
3423 else if (!strcmp(Cmd_Argv(1), "cubemap"))
3427 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3430 if (Cmd_Argc() == 3)
3431 strcpy(cubemapname, Cmd_Argv(2));
3435 else if (!strcmp(Cmd_Argv(1), "shadows"))
3437 if (Cmd_Argc() != 3)
3439 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3442 shadows = Cmd_Argv(2)[0] == 'y' || Cmd_Argv(2)[0] == 'Y' || Cmd_Argv(2)[0] == 't' || atoi(Cmd_Argv(2));
3444 else if (!strcmp(Cmd_Argv(1), "corona"))
3446 if (Cmd_Argc() != 3)
3448 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3451 corona = atof(Cmd_Argv(2));
3453 else if (!strcmp(Cmd_Argv(1), "coronasize"))
3455 if (Cmd_Argc() != 3)
3457 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3460 coronasizescale = atof(Cmd_Argv(2));
3462 else if (!strcmp(Cmd_Argv(1), "ambient"))
3464 if (Cmd_Argc() != 3)
3466 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3469 ambientscale = atof(Cmd_Argv(2));
3471 else if (!strcmp(Cmd_Argv(1), "diffuse"))
3473 if (Cmd_Argc() != 3)
3475 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3478 diffusescale = atof(Cmd_Argv(2));
3480 else if (!strcmp(Cmd_Argv(1), "specular"))
3482 if (Cmd_Argc() != 3)
3484 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3487 specularscale = atof(Cmd_Argv(2));
3489 else if (!strcmp(Cmd_Argv(1), "normalmode"))
3491 if (Cmd_Argc() != 3)
3493 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3496 normalmode = Cmd_Argv(2)[0] == 'y' || Cmd_Argv(2)[0] == 'Y' || Cmd_Argv(2)[0] == 't' || atoi(Cmd_Argv(2));
3498 else if (!strcmp(Cmd_Argv(1), "realtimemode"))
3500 if (Cmd_Argc() != 3)
3502 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3505 realtimemode = Cmd_Argv(2)[0] == 'y' || Cmd_Argv(2)[0] == 'Y' || Cmd_Argv(2)[0] == 't' || atoi(Cmd_Argv(2));
3509 Con_Print("usage: r_editlights_edit [property] [value]\n");
3510 Con_Print("Selected light's properties:\n");
3511 Con_Printf("Origin : %f %f %f\n", r_shadow_selectedlight->origin[0], r_shadow_selectedlight->origin[1], r_shadow_selectedlight->origin[2]);
3512 Con_Printf("Angles : %f %f %f\n", r_shadow_selectedlight->angles[0], r_shadow_selectedlight->angles[1], r_shadow_selectedlight->angles[2]);
3513 Con_Printf("Color : %f %f %f\n", r_shadow_selectedlight->color[0], r_shadow_selectedlight->color[1], r_shadow_selectedlight->color[2]);
3514 Con_Printf("Radius : %f\n", r_shadow_selectedlight->radius);
3515 Con_Printf("Corona : %f\n", r_shadow_selectedlight->corona);
3516 Con_Printf("Style : %i\n", r_shadow_selectedlight->style);
3517 Con_Printf("Shadows : %s\n", r_shadow_selectedlight->shadow ? "yes" : "no");
3518 Con_Printf("Cubemap : %s\n", r_shadow_selectedlight->cubemapname);
3519 Con_Printf("CoronaSize : %f\n", r_shadow_selectedlight->coronasizescale);
3520 Con_Printf("Ambient : %f\n", r_shadow_selectedlight->ambientscale);
3521 Con_Printf("Diffuse : %f\n", r_shadow_selectedlight->diffusescale);
3522 Con_Printf("Specular : %f\n", r_shadow_selectedlight->specularscale);
3523 Con_Printf("NormalMode : %s\n", (r_shadow_selectedlight->flags & LIGHTFLAG_NORMALMODE) ? "yes" : "no");
3524 Con_Printf("RealTimeMode : %s\n", (r_shadow_selectedlight->flags & LIGHTFLAG_REALTIMEMODE) ? "yes" : "no");
3527 flags = (normalmode ? LIGHTFLAG_NORMALMODE : 0) | (realtimemode ? LIGHTFLAG_REALTIMEMODE : 0);
3528 R_Shadow_UpdateWorldLight(r_shadow_selectedlight, origin, angles, color, radius, corona, style, shadows, cubemapname, coronasizescale, ambientscale, diffusescale, specularscale, flags);
3531 void R_Shadow_EditLights_EditAll_f(void)
3535 if (!r_editlights.integer)
3537 Con_Print("Cannot edit lights when not in editing mode. Set r_editlights to 1.\n");
3541 for (light = r_shadow_worldlightchain;light;light = light->next)
3543 R_Shadow_SelectLight(light);
3544 R_Shadow_EditLights_Edit_f();
3548 void R_Shadow_EditLights_DrawSelectedLightProperties(void)
3550 int lightnumber, lightcount;
3554 if (!r_editlights.integer)
3560 for (lightcount = 0, light = r_shadow_worldlightchain;light;lightcount++, light = light->next)
3561 if (light == r_shadow_selectedlight)
3562 lightnumber = lightcount;
3563 sprintf(temp, "Cursor %f %f %f Total Lights %i", r_editlights_cursorlocation[0], r_editlights_cursorlocation[1], r_editlights_cursorlocation[2], lightcount);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
3564 if (r_shadow_selectedlight == NULL)
3566 sprintf(temp, "Light #%i properties", lightnumber);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
3567 sprintf(temp, "Origin : %f %f %f\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);y += 8;
3568 sprintf(temp, "Angles : %f %f %f\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);y += 8;
3569 sprintf(temp, "Color : %f %f %f\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);y += 8;
3570 sprintf(temp, "Radius : %f\n", r_shadow_selectedlight->radius);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
3571 sprintf(temp, "Corona : %f\n", r_shadow_selectedlight->corona);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
3572 sprintf(temp, "Style : %i\n", r_shadow_selectedlight->style);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
3573 sprintf(temp, "Shadows : %s\n", r_shadow_selectedlight->shadow ? "yes" : "no");DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
3574 sprintf(temp, "Cubemap : %s\n", r_shadow_selectedlight->cubemapname);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
3575 sprintf(temp, "CoronaSize : %f\n", r_shadow_selectedlight->coronasizescale);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
3576 sprintf(temp, "Ambient : %f\n", r_shadow_selectedlight->ambientscale);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
3577 sprintf(temp, "Diffuse : %f\n", r_shadow_selectedlight->diffusescale);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
3578 sprintf(temp, "Specular : %f\n", r_shadow_selectedlight->specularscale);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
3579 sprintf(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);y += 8;
3580 sprintf(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);y += 8;
3583 void R_Shadow_EditLights_ToggleShadow_f(void)
3585 if (!r_editlights.integer)
3587 Con_Print("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
3590 if (!r_shadow_selectedlight)
3592 Con_Print("No selected light.\n");
3595 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);
3598 void R_Shadow_EditLights_ToggleCorona_f(void)
3600 if (!r_editlights.integer)
3602 Con_Print("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
3605 if (!r_shadow_selectedlight)
3607 Con_Print("No selected light.\n");
3610 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);
3613 void R_Shadow_EditLights_Remove_f(void)
3615 if (!r_editlights.integer)
3617 Con_Print("Cannot remove light when not in editing mode. Set r_editlights to 1.\n");
3620 if (!r_shadow_selectedlight)
3622 Con_Print("No selected light.\n");
3625 R_Shadow_FreeWorldLight(r_shadow_selectedlight);
3626 r_shadow_selectedlight = NULL;
3629 void R_Shadow_EditLights_Help_f(void)
3632 "Documentation on r_editlights system:\n"
3634 "r_editlights : enable/disable editing mode\n"
3635 "r_editlights_cursordistance : maximum distance of cursor from eye\n"
3636 "r_editlights_cursorpushback : push back cursor this far from surface\n"
3637 "r_editlights_cursorpushoff : push cursor off surface this far\n"
3638 "r_editlights_cursorgrid : snap cursor to grid of this size\n"
3639 "r_editlights_quakelightsizescale : imported quake light entity size scaling\n"
3641 "r_editlights_help : this help\n"
3642 "r_editlights_clear : remove all lights\n"
3643 "r_editlights_reload : reload .rtlights, .lights file, or entities\n"
3644 "r_editlights_save : save to .rtlights file\n"
3645 "r_editlights_spawn : create a light with default settings\n"
3646 "r_editlights_edit command : edit selected light - more documentation below\n"
3647 "r_editlights_remove : remove selected light\n"
3648 "r_editlights_toggleshadow : toggles on/off selected light's shadow property\n"
3649 "r_editlights_importlightentitiesfrommap : reload light entities\n"
3650 "r_editlights_importlightsfile : reload .light file (produced by hlight)\n"
3652 "origin x y z : set light location\n"
3653 "originx x: set x component of light location\n"
3654 "originy y: set y component of light location\n"
3655 "originz z: set z component of light location\n"
3656 "move x y z : adjust light location\n"
3657 "movex x: adjust x component of light location\n"
3658 "movey y: adjust y component of light location\n"
3659 "movez z: adjust z component of light location\n"
3660 "angles x y z : set light angles\n"
3661 "anglesx x: set x component of light angles\n"
3662 "anglesy y: set y component of light angles\n"
3663 "anglesz z: set z component of light angles\n"
3664 "color r g b : set color of light (can be brighter than 1 1 1)\n"
3665 "radius radius : set radius (size) of light\n"
3666 "colorscale grey : multiply color of light (1 does nothing)\n"
3667 "colorscale r g b : multiply color of light (1 1 1 does nothing)\n"
3668 "radiusscale scale : multiply radius (size) of light (1 does nothing)\n"
3669 "sizescale scale : multiply radius (size) of light (1 does nothing)\n"
3670 "style style : set lightstyle of light (flickering patterns, switches, etc)\n"
3671 "cubemap basename : set filter cubemap of light (not yet supported)\n"
3672 "shadows 1/0 : turn on/off shadows\n"
3673 "corona n : set corona intensity\n"
3674 "coronasize n : set corona size (0-1)\n"
3675 "ambient n : set ambient intensity (0-1)\n"
3676 "diffuse n : set diffuse intensity (0-1)\n"
3677 "specular n : set specular intensity (0-1)\n"
3678 "normalmode 1/0 : turn on/off rendering of this light in rtworld 0 mode\n"
3679 "realtimemode 1/0 : turn on/off rendering of this light in rtworld 1 mode\n"
3680 "<nothing> : print light properties to console\n"
3684 void R_Shadow_EditLights_CopyInfo_f(void)
3686 if (!r_editlights.integer)
3688 Con_Print("Cannot copy light info when not in editing mode. Set r_editlights to 1.\n");
3691 if (!r_shadow_selectedlight)
3693 Con_Print("No selected light.\n");
3696 VectorCopy(r_shadow_selectedlight->angles, r_shadow_bufferlight.angles);
3697 VectorCopy(r_shadow_selectedlight->color, r_shadow_bufferlight.color);
3698 r_shadow_bufferlight.radius = r_shadow_selectedlight->radius;
3699 r_shadow_bufferlight.style = r_shadow_selectedlight->style;
3700 if (r_shadow_selectedlight->cubemapname)
3701 strcpy(r_shadow_bufferlight.cubemapname, r_shadow_selectedlight->cubemapname);
3703 r_shadow_bufferlight.cubemapname[0] = 0;
3704 r_shadow_bufferlight.shadow = r_shadow_selectedlight->shadow;
3705 r_shadow_bufferlight.corona = r_shadow_selectedlight->corona;
3706 r_shadow_bufferlight.coronasizescale = r_shadow_selectedlight->coronasizescale;
3707 r_shadow_bufferlight.ambientscale = r_shadow_selectedlight->ambientscale;
3708 r_shadow_bufferlight.diffusescale = r_shadow_selectedlight->diffusescale;
3709 r_shadow_bufferlight.specularscale = r_shadow_selectedlight->specularscale;
3710 r_shadow_bufferlight.flags = r_shadow_selectedlight->flags;
3713 void R_Shadow_EditLights_PasteInfo_f(void)
3715 if (!r_editlights.integer)
3717 Con_Print("Cannot paste light info when not in editing mode. Set r_editlights to 1.\n");
3720 if (!r_shadow_selectedlight)
3722 Con_Print("No selected light.\n");
3725 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);
3728 void R_Shadow_EditLights_Init(void)
3730 Cvar_RegisterVariable(&r_editlights);
3731 Cvar_RegisterVariable(&r_editlights_cursordistance);
3732 Cvar_RegisterVariable(&r_editlights_cursorpushback);
3733 Cvar_RegisterVariable(&r_editlights_cursorpushoff);
3734 Cvar_RegisterVariable(&r_editlights_cursorgrid);
3735 Cvar_RegisterVariable(&r_editlights_quakelightsizescale);
3736 Cmd_AddCommand("r_editlights_help", R_Shadow_EditLights_Help_f, "prints documentation on console commands and variables in rtlight editing system");
3737 Cmd_AddCommand("r_editlights_clear", R_Shadow_EditLights_Clear_f, "removes all world lights (let there be darkness!)");
3738 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)");
3739 Cmd_AddCommand("r_editlights_save", R_Shadow_EditLights_Save_f, "save .rtlights file for current level");
3740 Cmd_AddCommand("r_editlights_spawn", R_Shadow_EditLights_Spawn_f, "creates a light with default properties (let there be light!)");
3741 Cmd_AddCommand("r_editlights_edit", R_Shadow_EditLights_Edit_f, "changes a property on the selected light");
3742 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)");
3743 Cmd_AddCommand("r_editlights_remove", R_Shadow_EditLights_Remove_f, "remove selected light");
3744 Cmd_AddCommand("r_editlights_toggleshadow", R_Shadow_EditLights_ToggleShadow_f, "toggle on/off the shadow option on the selected light");
3745 Cmd_AddCommand("r_editlights_togglecorona", R_Shadow_EditLights_ToggleCorona_f, "toggle on/off the corona option on the selected light");
3746 Cmd_AddCommand("r_editlights_importlightentitiesfrommap", R_Shadow_EditLights_ImportLightEntitiesFromMap_f, "load lights from .ent file or map entities (ignoring .rtlights or .lights file)");
3747 Cmd_AddCommand("r_editlights_importlightsfile", R_Shadow_EditLights_ImportLightsFile_f, "load lights from .lights file (ignoring .rtlights or .ent files and map entities)");
3748 Cmd_AddCommand("r_editlights_copyinfo", R_Shadow_EditLights_CopyInfo_f, "store a copy of all properties (except origin) of the selected light");
3749 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)");