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 rendered using Carmack's Reverse technique, in which backfaces behind
11 zbuffer (zfail) increment the stencil, and frontfaces behind zbuffer (zfail)
12 decrement the stencil, the result is a stencil value of zero where shadows
13 did not intersect the visible geometry, suitable as a stencil mask for
14 rendering lighting everywhere but shadow.
16 In our case we use a biased stencil clear of 128 to avoid requiring the
17 stencil wrap extension (but probably should support it), and to address
18 Creative's patent on this sort of technology we also draw the frontfaces
19 first, and backfaces second (decrement, increment).
22 This algorithm may be covered by Creative's patent (US Patent #6384822)
23 on Carmack's Reverse paper (which I have not read), however that patent
24 seems to be about drawing a stencil shadow from a model in an otherwise
25 unshadowed scene, where as realtime lighting technology draws light where
30 Terminology: Stencil Light Volume (sometimes called Light Volumes)
31 Similar to a Stencil Shadow Volume, but inverted; rather than containing the
32 areas in shadow it contanis the areas in light, this can only be built
33 quickly for certain limited cases (such as portal visibility from a point),
34 but is quite useful for some effects (sunlight coming from sky polygons is
35 one possible example, translucent occluders is another example).
39 Terminology: Optimized Stencil Shadow Volume
40 A Stencil Shadow Volume that has been processed sufficiently to ensure it has
41 no duplicate coverage of areas (no need to shadow an area twice), often this
42 greatly improves performance but is an operation too costly to use on moving
43 lights (however completely optimal Stencil Light Volumes can be constructed
48 Terminology: Per Pixel Lighting (sometimes abbreviated PPL)
49 Per pixel evaluation of lighting equations, at a bare minimum this involves
50 DOT3 shading of diffuse lighting (per pixel dotproduct of negated incidence
51 vector and surface normal, using a texture of the surface bumps, called a
52 NormalMap) if supported by hardware; in our case there is support for cards
53 which are incapable of DOT3, the quality is quite poor however. Additionally
54 it is desirable to have specular evaluation per pixel, per vertex
55 normalization of specular halfangle vectors causes noticable distortion but
56 is unavoidable on hardware without GL_ARB_fragment_program.
60 Terminology: Normalization CubeMap
61 A cubemap containing normalized dot3-encoded (vectors of length 1 or less
62 encoded as RGB colors) for any possible direction, this technique allows per
63 pixel calculation of incidence vector for per pixel lighting purposes, which
64 would not otherwise be possible per pixel without GL_ARB_fragment_program.
68 Terminology: 2D Attenuation Texturing
69 A very crude approximation of light attenuation with distance which results
70 in cylindrical light shapes which fade vertically as a streak (some games
71 such as Doom3 allow this to be rotated to be less noticable in specific
72 cases), the technique is simply modulating lighting by two 2D textures (which
73 can be the same) on different axes of projection (XY and Z, typically), this
74 is the best technique available without 3D Attenuation Texturing or
75 GL_ARB_fragment_program technology.
79 Terminology: 3D Attenuation Texturing
80 A slightly crude approximation of light attenuation with distance, its flaws
81 are limited radius and resolution (performance tradeoffs).
85 Terminology: 3D Attenuation-Normalization Texturing
86 A 3D Attenuation Texture merged with a Normalization CubeMap, by making the
87 vectors shorter the lighting becomes darker, a very effective optimization of
88 diffuse lighting if 3D Attenuation Textures are already used.
92 Terminology: Light Cubemap Filtering
93 A technique for modeling non-uniform light distribution according to
94 direction, for example projecting a stained glass window image onto a wall,
95 this is done by texturing the lighting with a cubemap.
99 Terminology: Light Projection Filtering
100 A technique for modeling shadowing of light passing through translucent
101 surfaces, allowing stained glass windows and other effects to be done more
102 elegantly than possible with Light Cubemap Filtering by applying an occluder
103 texture to the lighting combined with a stencil light volume to limit the lit
104 area (this allows evaluating multiple translucent occluders in a scene).
108 Terminology: Doom3 Lighting
109 A combination of Stencil Shadow Volume, Per Pixel Lighting, Normalization
110 CubeMap, 2D Attenuation Texturing, and Light Filtering, as demonstrated by
111 the (currently upcoming) game Doom3.
114 #include "quakedef.h"
115 #include "r_shadow.h"
116 #include "cl_collision.h"
120 extern void R_Shadow_EditLights_Init(void);
122 #define SHADOWSTAGE_NONE 0
123 #define SHADOWSTAGE_STENCIL 1
124 #define SHADOWSTAGE_LIGHT 2
125 #define SHADOWSTAGE_STENCILTWOSIDE 3
127 int r_shadowstage = SHADOWSTAGE_NONE;
129 mempool_t *r_shadow_mempool;
131 int maxshadowelements;
145 int r_shadow_buffer_numclusterpvsbytes;
146 qbyte *r_shadow_buffer_clusterpvs;
147 int *r_shadow_buffer_clusterlist;
149 int r_shadow_buffer_numsurfacepvsbytes;
150 qbyte *r_shadow_buffer_surfacepvs;
151 int *r_shadow_buffer_surfacelist;
153 rtexturepool_t *r_shadow_texturepool;
154 rtexture_t *r_shadow_normalcubetexture;
155 rtexture_t *r_shadow_attenuation2dtexture;
156 rtexture_t *r_shadow_attenuation3dtexture;
157 rtexture_t *r_shadow_blankbumptexture;
158 rtexture_t *r_shadow_blankglosstexture;
159 rtexture_t *r_shadow_blankwhitetexture;
160 rtexture_t *r_shadow_blankwhitecubetexture;
161 rtexture_t *r_shadow_blankblacktexture;
163 // lights are reloaded when this changes
164 char r_shadow_mapname[MAX_QPATH];
166 // used only for light filters (cubemaps)
167 rtexturepool_t *r_shadow_filters_texturepool;
169 cvar_t r_shadow_bumpscale_basetexture = {0, "r_shadow_bumpscale_basetexture", "0"};
170 cvar_t r_shadow_bumpscale_bumpmap = {0, "r_shadow_bumpscale_bumpmap", "4"};
171 cvar_t r_shadow_cull = {0, "r_shadow_cull", "1"};
172 cvar_t r_shadow_debuglight = {0, "r_shadow_debuglight", "-1"};
173 cvar_t r_shadow_gloss = {CVAR_SAVE, "r_shadow_gloss", "1"};
174 cvar_t r_shadow_gloss2intensity = {0, "r_shadow_gloss2intensity", "0.25"};
175 cvar_t r_shadow_glossintensity = {0, "r_shadow_glossintensity", "1"};
176 cvar_t r_shadow_lightattenuationpower = {0, "r_shadow_lightattenuationpower", "0.5"};
177 cvar_t r_shadow_lightattenuationscale = {0, "r_shadow_lightattenuationscale", "1"};
178 cvar_t r_shadow_lightintensityscale = {0, "r_shadow_lightintensityscale", "1"};
179 cvar_t r_shadow_portallight = {0, "r_shadow_portallight", "1"};
180 cvar_t r_shadow_projectdistance = {0, "r_shadow_projectdistance", "1000000"};
181 cvar_t r_shadow_realtime_dlight = {CVAR_SAVE, "r_shadow_realtime_dlight", "1"};
182 cvar_t r_shadow_realtime_dlight_shadows = {CVAR_SAVE, "r_shadow_realtime_dlight_shadows", "0"};
183 cvar_t r_shadow_realtime_world = {CVAR_SAVE, "r_shadow_realtime_world", "0"};
184 cvar_t r_shadow_realtime_world_dlightshadows = {CVAR_SAVE, "r_shadow_realtime_world_dlightshadows", "1"};
185 cvar_t r_shadow_realtime_world_lightmaps = {CVAR_SAVE, "r_shadow_realtime_world_lightmaps", "0"};
186 cvar_t r_shadow_realtime_world_shadows = {CVAR_SAVE, "r_shadow_realtime_world_shadows", "1"};
187 cvar_t r_shadow_scissor = {0, "r_shadow_scissor", "1"};
188 cvar_t r_shadow_shadow_polygonfactor = {0, "r_shadow_shadow_polygonfactor", "0"};
189 cvar_t r_shadow_shadow_polygonoffset = {0, "r_shadow_shadow_polygonoffset", "1"};
190 cvar_t r_shadow_singlepassvolumegeneration = {0, "r_shadow_singlepassvolumegeneration", "1"};
191 cvar_t r_shadow_staticworldlights = {0, "r_shadow_staticworldlights", "1"};
192 cvar_t r_shadow_texture3d = {0, "r_shadow_texture3d", "1"};
193 cvar_t r_shadow_visiblevolumes = {0, "r_shadow_visiblevolumes", "0"};
194 cvar_t r_shadow_glsl = {0, "r_shadow_glsl", "1"};
195 cvar_t r_shadow_glsl_offsetmapping = {0, "r_shadow_glsl_offsetmapping", "1"};
196 cvar_t r_shadow_glsl_offsetmapping_scale = {0, "r_shadow_glsl_offsetmapping_scale", "0.04"};
197 cvar_t r_shadow_glsl_offsetmapping_bias = {0, "r_shadow_glsl_offsetmapping_bias", "-0.04"};
198 cvar_t gl_ext_stenciltwoside = {0, "gl_ext_stenciltwoside", "1"};
199 cvar_t r_editlights = {0, "r_editlights", "0"};
200 cvar_t r_editlights_cursordistance = {0, "r_editlights_distance", "1024"};
201 cvar_t r_editlights_cursorpushback = {0, "r_editlights_pushback", "0"};
202 cvar_t r_editlights_cursorpushoff = {0, "r_editlights_pushoff", "4"};
203 cvar_t r_editlights_cursorgrid = {0, "r_editlights_grid", "4"};
204 cvar_t r_editlights_quakelightsizescale = {CVAR_SAVE, "r_editlights_quakelightsizescale", "0.8"};
205 cvar_t r_editlights_rtlightssizescale = {CVAR_SAVE, "r_editlights_rtlightssizescale", "0.7"};
206 cvar_t r_editlights_rtlightscolorscale = {CVAR_SAVE, "r_editlights_rtlightscolorscale", "2"};
208 float r_shadow_attenpower, r_shadow_attenscale;
210 rtlight_t *r_shadow_compilingrtlight;
211 dlight_t *r_shadow_worldlightchain;
212 dlight_t *r_shadow_selectedlight;
213 dlight_t r_shadow_bufferlight;
214 vec3_t r_editlights_cursorlocation;
216 rtexture_t *lighttextures[5];
218 extern int con_vislines;
220 typedef struct cubemapinfo_s
227 #define MAX_CUBEMAPS 256
228 static int numcubemaps;
229 static cubemapinfo_t cubemaps[MAX_CUBEMAPS];
231 #define SHADERPERMUTATION_SPECULAR (1<<0)
232 #define SHADERPERMUTATION_FOG (1<<1)
233 #define SHADERPERMUTATION_CUBEFILTER (1<<2)
234 #define SHADERPERMUTATION_OFFSETMAPPING (1<<3)
235 #define SHADERPERMUTATION_COUNT (1<<4)
237 GLhandleARB r_shadow_program_light[SHADERPERMUTATION_COUNT];
239 void R_Shadow_UncompileWorldLights(void);
240 void R_Shadow_ClearWorldLights(void);
241 void R_Shadow_SaveWorldLights(void);
242 void R_Shadow_LoadWorldLights(void);
243 void R_Shadow_LoadLightsFile(void);
244 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void);
245 void R_Shadow_EditLights_Reload_f(void);
246 void R_Shadow_ValidateCvars(void);
247 static void R_Shadow_MakeTextures(void);
248 void R_Shadow_DrawWorldLightShadowVolume(matrix4x4_t *matrix, dlight_t *light);
250 const char *builtinshader_light_vert =
251 "// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader\n"
252 "// written by Forest 'LordHavoc' Hale\n"
254 "uniform vec3 LightPosition;\n"
256 "varying vec2 TexCoord;\n"
257 "varying vec3 CubeVector;\n"
258 "varying vec3 LightVector;\n"
260 "#if defined(USESPECULAR) || defined(USEFOG) || defined(USEOFFSETMAPPING)\n"
261 "uniform vec3 EyePosition;\n"
262 "varying vec3 EyeVector;\n"
265 "// TODO: get rid of tangentt (texcoord2) and use a crossproduct to regenerate it from tangents (texcoord1) and normal (texcoord3)\n"
269 " // copy the surface texcoord\n"
270 " TexCoord = gl_MultiTexCoord0.st;\n"
272 " // transform vertex position into light attenuation/cubemap space\n"
273 " // (-1 to +1 across the light box)\n"
274 " CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex);\n"
276 " // transform unnormalized light direction into tangent space\n"
277 " // (we use unnormalized to ensure that it interpolates correctly and then\n"
278 " // normalize it per pixel)\n"
279 " vec3 lightminusvertex = LightPosition - gl_Vertex.xyz;\n"
280 " LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz);\n"
281 " LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz);\n"
282 " LightVector.z = -dot(lightminusvertex, gl_MultiTexCoord3.xyz);\n"
284 "#if defined(USESPECULAR) || defined(USEFOG) || defined(USEOFFSETMAPPING)\n"
285 " // transform unnormalized eye direction into tangent space\n"
286 " vec3 eyeminusvertex = EyePosition - gl_Vertex.xyz;\n"
287 " EyeVector.x = dot(eyeminusvertex, gl_MultiTexCoord1.xyz);\n"
288 " EyeVector.y = dot(eyeminusvertex, gl_MultiTexCoord2.xyz);\n"
289 " EyeVector.z = -dot(eyeminusvertex, gl_MultiTexCoord3.xyz);\n"
292 " // transform vertex to camera space, using ftransform to match non-VS\n"
294 " gl_Position = ftransform();\n"
298 const char *builtinshader_light_frag =
299 "// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader\n"
300 "// written by Forest 'LordHavoc' Hale\n"
302 "uniform vec3 LightColor;\n"
304 "#ifdef USEOFFSETMAPPING\n"
305 "uniform float OffsetMapping_Scale;\n"
306 "uniform float OffsetMapping_Bias;\n"
308 "#ifdef USESPECULAR\n"
309 "uniform float SpecularPower;\n"
312 "uniform float FogRangeRecip;\n"
314 "uniform float AmbientScale;\n"
315 "uniform float DiffuseScale;\n"
316 "#ifdef USESPECULAR\n"
317 "uniform float SpecularScale;\n"
320 "uniform sampler2D Texture_Normal;\n"
321 "uniform sampler2D Texture_Color;\n"
322 "#ifdef USESPECULAR\n"
323 "uniform sampler2D Texture_Gloss;\n"
325 "#ifdef USECUBEFILTER\n"
326 "uniform samplerCube Texture_Cube;\n"
329 "uniform sampler2D Texture_FogMask;\n"
332 "varying vec2 TexCoord;\n"
333 "varying vec3 CubeVector;\n"
334 "varying vec3 LightVector;\n"
335 "#if defined(USESPECULAR) || defined(USEFOG) || defined(USEOFFSETMAPPING)\n"
336 "varying vec3 EyeVector;\n"
343 " // the attenuation is (1-(x*x+y*y+z*z)) which gives a large bright\n"
344 " // center and sharp falloff at the edge, this is about the most efficient\n"
345 " // we can get away with as far as providing illumination.\n"
347 " // pow(1-(x*x+y*y+z*z), 4) is far more realistic but needs large lights to\n"
348 " // provide significant illumination, large = slow = pain.\n"
349 " float colorscale = clamp(1.0 - dot(CubeVector, CubeVector), 0.0, 1.0);\n"
353 " colorscale *= texture2D(Texture_FogMask, vec2(length(EyeVector)*FogRangeRecip, 0)).x;\n"
356 "#ifdef USEOFFSETMAPPING\n"
357 " vec2 OffsetVector = normalize(EyeVector).xy * vec2(-1, 1);\n"
358 " vec2 TexCoordOffset = TexCoord + OffsetVector * (OffsetMapping_Bias + OffsetMapping_Scale * texture2D(Texture_Normal, TexCoord).w);\n"
359 " TexCoordOffset += OffsetVector * (OffsetMapping_Bias + OffsetMapping_Scale * texture2D(Texture_Normal, TexCoordOffset).w);\n"
360 " TexCoordOffset += OffsetVector * (OffsetMapping_Bias + OffsetMapping_Scale * texture2D(Texture_Normal, TexCoordOffset).w);\n"
361 " TexCoordOffset += OffsetVector * (OffsetMapping_Bias + OffsetMapping_Scale * texture2D(Texture_Normal, TexCoordOffset).w);\n"
362 "#define TexCoord TexCoordOffset\n"
365 " // get the texels - with a blendmap we'd need to blend multiple here\n"
366 " vec3 surfacenormal = -1.0 + 2.0 * vec3(texture2D(Texture_Normal, TexCoord));\n"
367 " vec3 colortexel = vec3(texture2D(Texture_Color, TexCoord));\n"
368 "#ifdef USESPECULAR\n"
369 " vec3 glosstexel = vec3(texture2D(Texture_Gloss, TexCoord));\n"
372 " // calculate shading\n"
373 " vec3 diffusenormal = normalize(LightVector);\n"
374 " vec3 color = colortexel * (AmbientScale + DiffuseScale * clamp(dot(surfacenormal, diffusenormal), 0.0, 1.0));\n"
375 "#ifdef USESPECULAR\n"
376 " color += glosstexel * (SpecularScale * pow(clamp(dot(surfacenormal, normalize(diffusenormal + normalize(EyeVector))), 0.0, 1.0), SpecularPower));\n"
379 "#ifdef USECUBEFILTER\n"
380 " // apply light cubemap filter\n"
381 " color *= vec3(textureCube(Texture_Cube, CubeVector));\n"
384 " // calculate fragment color\n"
385 " gl_FragColor = vec4(LightColor * color * colorscale, 1);\n"
389 void r_shadow_start(void)
392 // allocate vertex processing arrays
394 r_shadow_normalcubetexture = NULL;
395 r_shadow_attenuation2dtexture = NULL;
396 r_shadow_attenuation3dtexture = NULL;
397 r_shadow_blankbumptexture = NULL;
398 r_shadow_blankglosstexture = NULL;
399 r_shadow_blankwhitetexture = NULL;
400 r_shadow_blankwhitecubetexture = NULL;
401 r_shadow_blankblacktexture = NULL;
402 r_shadow_texturepool = NULL;
403 r_shadow_filters_texturepool = NULL;
404 R_Shadow_ValidateCvars();
405 R_Shadow_MakeTextures();
406 maxshadowelements = 0;
407 shadowelements = NULL;
415 shadowmarklist = NULL;
417 r_shadow_buffer_numclusterpvsbytes = 0;
418 r_shadow_buffer_clusterpvs = NULL;
419 r_shadow_buffer_clusterlist = NULL;
420 r_shadow_buffer_numsurfacepvsbytes = 0;
421 r_shadow_buffer_surfacepvs = NULL;
422 r_shadow_buffer_surfacelist = NULL;
423 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
424 r_shadow_program_light[i] = 0;
425 if (gl_support_fragment_shader)
427 char *vertstring, *fragstring;
428 int vertstrings_count;
429 int fragstrings_count;
430 const char *vertstrings_list[SHADERPERMUTATION_COUNT];
431 const char *fragstrings_list[SHADERPERMUTATION_COUNT];
432 vertstring = FS_LoadFile("glsl/light.vert", tempmempool, false);
433 fragstring = FS_LoadFile("glsl/light.frag", tempmempool, false);
434 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
436 vertstrings_count = 0;
437 fragstrings_count = 0;
438 if (i & SHADERPERMUTATION_SPECULAR)
440 vertstrings_list[vertstrings_count++] = "#define USESPECULAR\n";
441 fragstrings_list[fragstrings_count++] = "#define USESPECULAR\n";
443 if (i & SHADERPERMUTATION_FOG)
445 vertstrings_list[vertstrings_count++] = "#define USEFOG\n";
446 fragstrings_list[fragstrings_count++] = "#define USEFOG\n";
448 if (i & SHADERPERMUTATION_CUBEFILTER)
450 vertstrings_list[vertstrings_count++] = "#define USECUBEFILTER\n";
451 fragstrings_list[fragstrings_count++] = "#define USECUBEFILTER\n";
453 if (i & SHADERPERMUTATION_OFFSETMAPPING)
455 vertstrings_list[vertstrings_count++] = "#define USEOFFSETMAPPING\n";
456 fragstrings_list[fragstrings_count++] = "#define USEOFFSETMAPPING\n";
458 vertstrings_list[vertstrings_count++] = vertstring ? vertstring : builtinshader_light_vert;
459 fragstrings_list[fragstrings_count++] = fragstring ? fragstring : builtinshader_light_frag;
460 r_shadow_program_light[i] = GL_Backend_CompileProgram(vertstrings_count, vertstrings_list, fragstrings_count, fragstrings_list);
461 if (!r_shadow_program_light[i])
463 Con_Printf("permutation %s %s %s %s failed for shader %s, some features may not work properly!\n", i & 1 ? "specular" : "", i & 1 ? "fog" : "", i & 1 ? "cubefilter" : "", i & 1 ? "offsetmapping" : "", "glsl/light");
466 qglUseProgramObjectARB(r_shadow_program_light[i]);
467 qglUniform1iARB(qglGetUniformLocationARB(r_shadow_program_light[i], "Texture_Normal"), 0);CHECKGLERROR
468 qglUniform1iARB(qglGetUniformLocationARB(r_shadow_program_light[i], "Texture_Color"), 1);CHECKGLERROR
469 if (i & SHADERPERMUTATION_SPECULAR)
471 qglUniform1iARB(qglGetUniformLocationARB(r_shadow_program_light[i], "Texture_Gloss"), 2);CHECKGLERROR
473 if (i & SHADERPERMUTATION_CUBEFILTER)
475 qglUniform1iARB(qglGetUniformLocationARB(r_shadow_program_light[i], "Texture_Cube"), 3);CHECKGLERROR
477 if (i & SHADERPERMUTATION_FOG)
479 qglUniform1iARB(qglGetUniformLocationARB(r_shadow_program_light[i], "Texture_FogMask"), 4);CHECKGLERROR
482 qglUseProgramObjectARB(0);
484 Mem_Free(fragstring);
486 Mem_Free(vertstring);
490 void r_shadow_shutdown(void)
493 R_Shadow_UncompileWorldLights();
494 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
496 if (r_shadow_program_light[i])
498 GL_Backend_FreeProgram(r_shadow_program_light[i]);
499 r_shadow_program_light[i] = 0;
503 r_shadow_normalcubetexture = NULL;
504 r_shadow_attenuation2dtexture = NULL;
505 r_shadow_attenuation3dtexture = NULL;
506 r_shadow_blankbumptexture = NULL;
507 r_shadow_blankglosstexture = NULL;
508 r_shadow_blankwhitetexture = NULL;
509 r_shadow_blankwhitecubetexture = NULL;
510 r_shadow_blankblacktexture = NULL;
511 R_FreeTexturePool(&r_shadow_texturepool);
512 R_FreeTexturePool(&r_shadow_filters_texturepool);
513 maxshadowelements = 0;
515 Mem_Free(shadowelements);
516 shadowelements = NULL;
519 Mem_Free(vertexupdate);
522 Mem_Free(vertexremap);
528 Mem_Free(shadowmark);
531 Mem_Free(shadowmarklist);
532 shadowmarklist = NULL;
534 r_shadow_buffer_numclusterpvsbytes = 0;
535 if (r_shadow_buffer_clusterpvs)
536 Mem_Free(r_shadow_buffer_clusterpvs);
537 r_shadow_buffer_clusterpvs = NULL;
538 if (r_shadow_buffer_clusterlist)
539 Mem_Free(r_shadow_buffer_clusterlist);
540 r_shadow_buffer_clusterlist = NULL;
541 r_shadow_buffer_numsurfacepvsbytes = 0;
542 if (r_shadow_buffer_surfacepvs)
543 Mem_Free(r_shadow_buffer_surfacepvs);
544 r_shadow_buffer_surfacepvs = NULL;
545 if (r_shadow_buffer_surfacelist)
546 Mem_Free(r_shadow_buffer_surfacelist);
547 r_shadow_buffer_surfacelist = NULL;
550 void r_shadow_newmap(void)
554 void R_Shadow_Help_f(void)
557 "Documentation on r_shadow system:\n"
559 "r_shadow_bumpscale_basetexture : base texture as bumpmap with this scale\n"
560 "r_shadow_bumpscale_bumpmap : depth scale for bumpmap conversion\n"
561 "r_shadow_debuglight : render only this light number (-1 = all)\n"
562 "r_shadow_gloss 0/1/2 : no gloss, gloss textures only, force gloss\n"
563 "r_shadow_gloss2intensity : brightness of forced gloss\n"
564 "r_shadow_glossintensity : brightness of textured gloss\n"
565 "r_shadow_lightattenuationpower : used to generate attenuation texture\n"
566 "r_shadow_lightattenuationscale : used to generate attenuation texture\n"
567 "r_shadow_lightintensityscale : scale rendering brightness of all lights\n"
568 "r_shadow_portallight : use portal visibility for static light precomputation\n"
569 "r_shadow_projectdistance : shadow volume projection distance\n"
570 "r_shadow_realtime_dlight : use high quality dynamic lights in normal mode\n"
571 "r_shadow_realtime_dlight_shadows : cast shadows from dlights\n"
572 "r_shadow_realtime_world : use high quality world lighting mode\n"
573 "r_shadow_realtime_world_dlightshadows : cast shadows from dlights\n"
574 "r_shadow_realtime_world_lightmaps : use lightmaps in addition to lights\n"
575 "r_shadow_realtime_world_shadows : cast shadows from world lights\n"
576 "r_shadow_glsl : use OpenGL Shading Language for lighting\n"
577 "r_shadow_glsl_offsetmapping : enables Offset Mapping bumpmap enhancement\n"
578 "r_shadow_glsl_offsetmapping_scale : controls depth of Offset Mapping\n"
579 "r_shadow_glsl_offsetmapping_bias : should be negative half of scale\n"
580 "r_shadow_scissor : use scissor optimization\n"
581 "r_shadow_shadow_polygonfactor : nudge shadow volumes closer/further\n"
582 "r_shadow_shadow_polygonoffset : nudge shadow volumes closer/further\n"
583 "r_shadow_singlepassvolumegeneration : selects shadow volume algorithm\n"
584 "r_shadow_texture3d : use 3d attenuation texture (if hardware supports)\n"
585 "r_shadow_visiblevolumes : useful for performance testing; bright = slow!\n"
587 "r_shadow_help : this help\n"
591 void R_Shadow_Init(void)
593 Cvar_RegisterVariable(&r_shadow_bumpscale_basetexture);
594 Cvar_RegisterVariable(&r_shadow_bumpscale_bumpmap);
595 Cvar_RegisterVariable(&r_shadow_cull);
596 Cvar_RegisterVariable(&r_shadow_debuglight);
597 Cvar_RegisterVariable(&r_shadow_gloss);
598 Cvar_RegisterVariable(&r_shadow_gloss2intensity);
599 Cvar_RegisterVariable(&r_shadow_glossintensity);
600 Cvar_RegisterVariable(&r_shadow_lightattenuationpower);
601 Cvar_RegisterVariable(&r_shadow_lightattenuationscale);
602 Cvar_RegisterVariable(&r_shadow_lightintensityscale);
603 Cvar_RegisterVariable(&r_shadow_portallight);
604 Cvar_RegisterVariable(&r_shadow_projectdistance);
605 Cvar_RegisterVariable(&r_shadow_realtime_dlight);
606 Cvar_RegisterVariable(&r_shadow_realtime_dlight_shadows);
607 Cvar_RegisterVariable(&r_shadow_realtime_world);
608 Cvar_RegisterVariable(&r_shadow_realtime_world_dlightshadows);
609 Cvar_RegisterVariable(&r_shadow_realtime_world_lightmaps);
610 Cvar_RegisterVariable(&r_shadow_realtime_world_shadows);
611 Cvar_RegisterVariable(&r_shadow_scissor);
612 Cvar_RegisterVariable(&r_shadow_shadow_polygonfactor);
613 Cvar_RegisterVariable(&r_shadow_shadow_polygonoffset);
614 Cvar_RegisterVariable(&r_shadow_singlepassvolumegeneration);
615 Cvar_RegisterVariable(&r_shadow_staticworldlights);
616 Cvar_RegisterVariable(&r_shadow_texture3d);
617 Cvar_RegisterVariable(&r_shadow_visiblevolumes);
618 Cvar_RegisterVariable(&r_shadow_glsl);
619 Cvar_RegisterVariable(&r_shadow_glsl_offsetmapping);
620 Cvar_RegisterVariable(&r_shadow_glsl_offsetmapping_scale);
621 Cvar_RegisterVariable(&r_shadow_glsl_offsetmapping_bias);
622 Cvar_RegisterVariable(&gl_ext_stenciltwoside);
623 if (gamemode == GAME_TENEBRAE)
625 Cvar_SetValue("r_shadow_gloss", 2);
626 Cvar_SetValue("r_shadow_bumpscale_basetexture", 4);
628 Cmd_AddCommand("r_shadow_help", R_Shadow_Help_f);
629 R_Shadow_EditLights_Init();
630 r_shadow_mempool = Mem_AllocPool("R_Shadow", 0, NULL);
631 r_shadow_worldlightchain = NULL;
632 maxshadowelements = 0;
633 shadowelements = NULL;
641 shadowmarklist = NULL;
643 r_shadow_buffer_numclusterpvsbytes = 0;
644 r_shadow_buffer_clusterpvs = NULL;
645 r_shadow_buffer_clusterlist = NULL;
646 r_shadow_buffer_numsurfacepvsbytes = 0;
647 r_shadow_buffer_surfacepvs = NULL;
648 r_shadow_buffer_surfacelist = NULL;
649 R_RegisterModule("R_Shadow", r_shadow_start, r_shadow_shutdown, r_shadow_newmap);
652 matrix4x4_t matrix_attenuationxyz =
655 {0.5, 0.0, 0.0, 0.5},
656 {0.0, 0.5, 0.0, 0.5},
657 {0.0, 0.0, 0.5, 0.5},
662 matrix4x4_t matrix_attenuationz =
665 {0.0, 0.0, 0.5, 0.5},
666 {0.0, 0.0, 0.0, 0.5},
667 {0.0, 0.0, 0.0, 0.5},
672 int *R_Shadow_ResizeShadowElements(int numtris)
674 // make sure shadowelements is big enough for this volume
675 if (maxshadowelements < numtris * 24)
677 maxshadowelements = numtris * 24;
679 Mem_Free(shadowelements);
680 shadowelements = Mem_Alloc(r_shadow_mempool, maxshadowelements * sizeof(int));
682 return shadowelements;
685 void R_Shadow_EnlargeClusterBuffer(int numclusters)
687 int numclusterpvsbytes = (((numclusters + 7) >> 3) + 255) & ~255;
688 if (r_shadow_buffer_numclusterpvsbytes < numclusterpvsbytes)
690 if (r_shadow_buffer_clusterpvs)
691 Mem_Free(r_shadow_buffer_clusterpvs);
692 if (r_shadow_buffer_clusterlist)
693 Mem_Free(r_shadow_buffer_clusterlist);
694 r_shadow_buffer_numclusterpvsbytes = numclusterpvsbytes;
695 r_shadow_buffer_clusterpvs = Mem_Alloc(r_shadow_mempool, r_shadow_buffer_numclusterpvsbytes);
696 r_shadow_buffer_clusterlist = Mem_Alloc(r_shadow_mempool, r_shadow_buffer_numclusterpvsbytes * 8 * sizeof(*r_shadow_buffer_clusterlist));
700 void R_Shadow_EnlargeSurfaceBuffer(int numsurfaces)
702 int numsurfacepvsbytes = (((numsurfaces + 7) >> 3) + 255) & ~255;
703 if (r_shadow_buffer_numsurfacepvsbytes < numsurfacepvsbytes)
705 if (r_shadow_buffer_surfacepvs)
706 Mem_Free(r_shadow_buffer_surfacepvs);
707 if (r_shadow_buffer_surfacelist)
708 Mem_Free(r_shadow_buffer_surfacelist);
709 r_shadow_buffer_numsurfacepvsbytes = numsurfacepvsbytes;
710 r_shadow_buffer_surfacepvs = Mem_Alloc(r_shadow_mempool, r_shadow_buffer_numsurfacepvsbytes);
711 r_shadow_buffer_surfacelist = Mem_Alloc(r_shadow_mempool, r_shadow_buffer_numsurfacepvsbytes * 8 * sizeof(*r_shadow_buffer_surfacelist));
715 void R_Shadow_PrepareShadowMark(int numtris)
717 // make sure shadowmark is big enough for this volume
718 if (maxshadowmark < numtris)
720 maxshadowmark = numtris;
722 Mem_Free(shadowmark);
724 Mem_Free(shadowmarklist);
725 shadowmark = Mem_Alloc(r_shadow_mempool, maxshadowmark * sizeof(*shadowmark));
726 shadowmarklist = Mem_Alloc(r_shadow_mempool, maxshadowmark * sizeof(*shadowmarklist));
730 // if shadowmarkcount wrapped we clear the array and adjust accordingly
731 if (shadowmarkcount == 0)
734 memset(shadowmark, 0, maxshadowmark * sizeof(*shadowmark));
739 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)
741 int i, j, tris = 0, vr[3], t, outvertices = 0;
746 if (maxvertexupdate < innumvertices)
748 maxvertexupdate = innumvertices;
750 Mem_Free(vertexupdate);
752 Mem_Free(vertexremap);
753 vertexupdate = Mem_Alloc(r_shadow_mempool, maxvertexupdate * sizeof(int));
754 vertexremap = Mem_Alloc(r_shadow_mempool, maxvertexupdate * sizeof(int));
758 if (vertexupdatenum == 0)
761 memset(vertexupdate, 0, maxvertexupdate * sizeof(int));
762 memset(vertexremap, 0, maxvertexupdate * sizeof(int));
765 for (i = 0;i < numshadowmarktris;i++)
766 shadowmark[shadowmarktris[i]] = shadowmarkcount;
768 for (i = 0;i < numshadowmarktris;i++)
770 t = shadowmarktris[i];
771 e = inelement3i + t * 3;
772 // make sure the vertices are created
773 for (j = 0;j < 3;j++)
775 if (vertexupdate[e[j]] != vertexupdatenum)
777 vertexupdate[e[j]] = vertexupdatenum;
778 vertexremap[e[j]] = outvertices;
779 v = invertex3f + e[j] * 3;
780 // project one copy of the vertex to the sphere radius of the light
781 // (FIXME: would projecting it to the light box be better?)
782 VectorSubtract(v, projectorigin, temp);
783 f = projectdistance / VectorLength(temp);
784 VectorCopy(v, outvertex3f);
785 VectorMA(projectorigin, f, temp, (outvertex3f + 3));
792 for (i = 0;i < numshadowmarktris;i++)
794 t = shadowmarktris[i];
795 e = inelement3i + t * 3;
796 n = inneighbor3i + t * 3;
797 // output the front and back triangles
798 outelement3i[0] = vertexremap[e[0]];
799 outelement3i[1] = vertexremap[e[1]];
800 outelement3i[2] = vertexremap[e[2]];
801 outelement3i[3] = vertexremap[e[2]] + 1;
802 outelement3i[4] = vertexremap[e[1]] + 1;
803 outelement3i[5] = vertexremap[e[0]] + 1;
806 // output the sides (facing outward from this triangle)
807 if (shadowmark[n[0]] != shadowmarkcount)
809 vr[0] = vertexremap[e[0]];
810 vr[1] = vertexremap[e[1]];
811 outelement3i[0] = vr[1];
812 outelement3i[1] = vr[0];
813 outelement3i[2] = vr[0] + 1;
814 outelement3i[3] = vr[1];
815 outelement3i[4] = vr[0] + 1;
816 outelement3i[5] = vr[1] + 1;
820 if (shadowmark[n[1]] != shadowmarkcount)
822 vr[1] = vertexremap[e[1]];
823 vr[2] = vertexremap[e[2]];
824 outelement3i[0] = vr[2];
825 outelement3i[1] = vr[1];
826 outelement3i[2] = vr[1] + 1;
827 outelement3i[3] = vr[2];
828 outelement3i[4] = vr[1] + 1;
829 outelement3i[5] = vr[2] + 1;
833 if (shadowmark[n[2]] != shadowmarkcount)
835 vr[0] = vertexremap[e[0]];
836 vr[2] = vertexremap[e[2]];
837 outelement3i[0] = vr[0];
838 outelement3i[1] = vr[2];
839 outelement3i[2] = vr[2] + 1;
840 outelement3i[3] = vr[0];
841 outelement3i[4] = vr[2] + 1;
842 outelement3i[5] = vr[0] + 1;
848 *outnumvertices = outvertices;
852 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)
855 if (projectdistance < 0.1)
857 Con_Printf("R_Shadow_Volume: projectdistance %f\n");
860 if (!numverts || !nummarktris)
862 // make sure shadowelements is big enough for this volume
863 if (maxshadowelements < nummarktris * 24)
864 R_Shadow_ResizeShadowElements((nummarktris + 256) * 24);
865 tris = R_Shadow_ConstructShadowVolume(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, varray_vertex3f2, projectorigin, projectdistance, nummarktris, marktris);
866 R_Shadow_RenderVolume(outverts, tris, varray_vertex3f2, shadowelements);
869 void R_Shadow_MarkVolumeFromBox(int firsttriangle, int numtris, const float *invertex3f, const int *elements, const vec3_t projectorigin, vec3_t lightmins, vec3_t lightmaxs, vec3_t surfacemins, vec3_t surfacemaxs)
874 if (!BoxesOverlap(lightmins, lightmaxs, surfacemins, surfacemaxs))
876 tend = firsttriangle + numtris;
877 if (surfacemins[0] >= lightmins[0] && surfacemaxs[0] <= lightmaxs[0]
878 && surfacemins[1] >= lightmins[1] && surfacemaxs[1] <= lightmaxs[1]
879 && surfacemins[2] >= lightmins[2] && surfacemaxs[2] <= lightmaxs[2])
881 // surface box entirely inside light box, no box cull
882 for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
883 if (PointInfrontOfTriangle(projectorigin, invertex3f + e[0] * 3, invertex3f + e[1] * 3, invertex3f + e[2] * 3))
884 shadowmarklist[numshadowmark++] = t;
888 // surface box not entirely inside light box, cull each triangle
889 for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
891 v[0] = invertex3f + e[0] * 3;
892 v[1] = invertex3f + e[1] * 3;
893 v[2] = invertex3f + e[2] * 3;
894 if (PointInfrontOfTriangle(projectorigin, v[0], v[1], v[2])
895 && lightmaxs[0] > min(v[0][0], min(v[1][0], v[2][0]))
896 && lightmins[0] < max(v[0][0], max(v[1][0], v[2][0]))
897 && lightmaxs[1] > min(v[0][1], min(v[1][1], v[2][1]))
898 && lightmins[1] < max(v[0][1], max(v[1][1], v[2][1]))
899 && lightmaxs[2] > min(v[0][2], min(v[1][2], v[2][2]))
900 && lightmins[2] < max(v[0][2], max(v[1][2], v[2][2])))
901 shadowmarklist[numshadowmark++] = t;
906 void R_Shadow_RenderVolume(int numvertices, int numtriangles, const float *vertex3f, const int *element3i)
909 if (r_shadow_compilingrtlight)
911 // if we're compiling an rtlight, capture the mesh
912 Mod_ShadowMesh_AddMesh(r_shadow_mempool, r_shadow_compilingrtlight->static_meshchain_shadow, NULL, NULL, NULL, vertex3f, NULL, NULL, NULL, NULL, numtriangles, element3i);
915 memset(&m, 0, sizeof(m));
916 m.pointer_vertex = vertex3f;
918 GL_LockArrays(0, numvertices);
919 if (r_shadowstage == SHADOWSTAGE_STENCIL)
921 // increment stencil if backface is behind depthbuffer
922 qglCullFace(GL_BACK); // quake is backwards, this culls front faces
923 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
924 R_Mesh_Draw(numvertices, numtriangles, element3i);
926 c_rt_shadowtris += numtriangles;
927 // decrement stencil if frontface is behind depthbuffer
928 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
929 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
931 R_Mesh_Draw(numvertices, numtriangles, element3i);
933 c_rt_shadowtris += numtriangles;
937 static void R_Shadow_MakeTextures(void)
939 int x, y, z, d, side;
940 float v[3], s, t, intensity;
942 R_FreeTexturePool(&r_shadow_texturepool);
943 r_shadow_texturepool = R_AllocTexturePool();
944 r_shadow_attenpower = r_shadow_lightattenuationpower.value;
945 r_shadow_attenscale = r_shadow_lightattenuationscale.value;
947 #define ATTEN2DSIZE 64
948 #define ATTEN3DSIZE 32
949 data = Mem_Alloc(tempmempool, max(6*NORMSIZE*NORMSIZE*4, max(ATTEN3DSIZE*ATTEN3DSIZE*ATTEN3DSIZE*4, ATTEN2DSIZE*ATTEN2DSIZE*4)));
950 data[0] = 128; // normal X
951 data[1] = 128; // normal Y
952 data[2] = 255; // normal Z
953 data[3] = 128; // height
954 r_shadow_blankbumptexture = R_LoadTexture2D(r_shadow_texturepool, "blankbump", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
959 r_shadow_blankglosstexture = R_LoadTexture2D(r_shadow_texturepool, "blankgloss", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
964 r_shadow_blankwhitetexture = R_LoadTexture2D(r_shadow_texturepool, "blankwhite", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
969 r_shadow_blankblacktexture = R_LoadTexture2D(r_shadow_texturepool, "blankblack", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
970 r_shadow_blankwhitecubetexture = NULL;
971 r_shadow_normalcubetexture = NULL;
972 if (gl_texturecubemap)
974 data[ 0] = 255;data[ 1] = 255;data[ 2] = 255;data[ 3] = 255;
975 data[ 4] = 255;data[ 5] = 255;data[ 6] = 255;data[ 7] = 255;
976 data[ 8] = 255;data[ 9] = 255;data[10] = 255;data[11] = 255;
977 data[12] = 255;data[13] = 255;data[14] = 255;data[15] = 255;
978 data[16] = 255;data[17] = 255;data[18] = 255;data[19] = 255;
979 data[20] = 255;data[21] = 255;data[22] = 255;data[23] = 255;
980 r_shadow_blankwhitecubetexture = R_LoadTextureCubeMap(r_shadow_texturepool, "blankwhitecube", 1, data, TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP, NULL);
981 for (side = 0;side < 6;side++)
983 for (y = 0;y < NORMSIZE;y++)
985 for (x = 0;x < NORMSIZE;x++)
987 s = (x + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
988 t = (y + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
1022 intensity = 127.0f / sqrt(DotProduct(v, v));
1023 data[((side*NORMSIZE+y)*NORMSIZE+x)*4+0] = 128.0f + intensity * v[0];
1024 data[((side*NORMSIZE+y)*NORMSIZE+x)*4+1] = 128.0f + intensity * v[1];
1025 data[((side*NORMSIZE+y)*NORMSIZE+x)*4+2] = 128.0f + intensity * v[2];
1026 data[((side*NORMSIZE+y)*NORMSIZE+x)*4+3] = 255;
1030 r_shadow_normalcubetexture = R_LoadTextureCubeMap(r_shadow_texturepool, "normalcube", NORMSIZE, data, TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP, NULL);
1032 for (y = 0;y < ATTEN2DSIZE;y++)
1034 for (x = 0;x < ATTEN2DSIZE;x++)
1036 v[0] = ((x + 0.5f) * (2.0f / ATTEN2DSIZE) - 1.0f) * (1.0f / 0.9375);
1037 v[1] = ((y + 0.5f) * (2.0f / ATTEN2DSIZE) - 1.0f) * (1.0f / 0.9375);
1039 intensity = 1.0f - sqrt(DotProduct(v, v));
1041 intensity = pow(intensity, r_shadow_attenpower) * r_shadow_attenscale * 256.0f;
1042 d = bound(0, intensity, 255);
1043 data[(y*ATTEN2DSIZE+x)*4+0] = d;
1044 data[(y*ATTEN2DSIZE+x)*4+1] = d;
1045 data[(y*ATTEN2DSIZE+x)*4+2] = d;
1046 data[(y*ATTEN2DSIZE+x)*4+3] = d;
1049 r_shadow_attenuation2dtexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation2d", ATTEN2DSIZE, ATTEN2DSIZE, data, TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA, NULL);
1050 if (r_shadow_texture3d.integer)
1052 for (z = 0;z < ATTEN3DSIZE;z++)
1054 for (y = 0;y < ATTEN3DSIZE;y++)
1056 for (x = 0;x < ATTEN3DSIZE;x++)
1058 v[0] = ((x + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375);
1059 v[1] = ((y + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375);
1060 v[2] = ((z + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375);
1061 intensity = 1.0f - sqrt(DotProduct(v, v));
1063 intensity = pow(intensity, r_shadow_attenpower) * r_shadow_attenscale * 256.0f;
1064 d = bound(0, intensity, 255);
1065 data[((z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x)*4+0] = d;
1066 data[((z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x)*4+1] = d;
1067 data[((z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x)*4+2] = d;
1068 data[((z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x)*4+3] = d;
1072 r_shadow_attenuation3dtexture = R_LoadTexture3D(r_shadow_texturepool, "attenuation3d", ATTEN3DSIZE, ATTEN3DSIZE, ATTEN3DSIZE, data, TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA, NULL);
1077 void R_Shadow_ValidateCvars(void)
1079 if (r_shadow_texture3d.integer && !gl_texture3d)
1080 Cvar_SetValueQuick(&r_shadow_texture3d, 0);
1081 if (gl_ext_stenciltwoside.integer && !gl_support_stenciltwoside)
1082 Cvar_SetValueQuick(&gl_ext_stenciltwoside, 0);
1085 void R_Shadow_Stage_Begin(void)
1089 R_Shadow_ValidateCvars();
1091 if (!r_shadow_attenuation2dtexture
1092 || (!r_shadow_attenuation3dtexture && r_shadow_texture3d.integer)
1093 || r_shadow_lightattenuationpower.value != r_shadow_attenpower
1094 || r_shadow_lightattenuationscale.value != r_shadow_attenscale)
1095 R_Shadow_MakeTextures();
1097 memset(&m, 0, sizeof(m));
1098 GL_BlendFunc(GL_ONE, GL_ZERO);
1099 GL_DepthMask(false);
1102 GL_Color(0, 0, 0, 1);
1103 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
1104 qglEnable(GL_CULL_FACE);
1105 GL_Scissor(r_view_x, r_view_y, r_view_width, r_view_height);
1106 r_shadowstage = SHADOWSTAGE_NONE;
1109 void R_Shadow_Stage_ShadowVolumes(void)
1112 memset(&m, 0, sizeof(m));
1114 GL_Color(1, 1, 1, 1);
1115 GL_ColorMask(0, 0, 0, 0);
1116 GL_BlendFunc(GL_ONE, GL_ZERO);
1117 GL_DepthMask(false);
1119 qglPolygonOffset(r_shadow_shadow_polygonfactor.value, r_shadow_shadow_polygonoffset.value);
1120 //if (r_shadow_shadow_polygonoffset.value != 0)
1122 // qglPolygonOffset(r_shadow_shadow_polygonfactor.value, r_shadow_shadow_polygonoffset.value);
1123 // qglEnable(GL_POLYGON_OFFSET_FILL);
1126 // qglDisable(GL_POLYGON_OFFSET_FILL);
1127 qglDepthFunc(GL_LESS);
1128 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
1129 qglEnable(GL_STENCIL_TEST);
1130 qglStencilFunc(GL_ALWAYS, 128, ~0);
1131 if (gl_ext_stenciltwoside.integer)
1133 r_shadowstage = SHADOWSTAGE_STENCILTWOSIDE;
1134 qglDisable(GL_CULL_FACE);
1135 qglEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);
1136 qglActiveStencilFaceEXT(GL_BACK); // quake is backwards, this is front faces
1138 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
1139 qglActiveStencilFaceEXT(GL_FRONT); // quake is backwards, this is back faces
1141 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
1145 r_shadowstage = SHADOWSTAGE_STENCIL;
1146 qglEnable(GL_CULL_FACE);
1148 // this is changed by every shadow render so its value here is unimportant
1149 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
1151 GL_Clear(GL_STENCIL_BUFFER_BIT);
1153 // LordHavoc note: many shadow volumes reside entirely inside the world
1154 // (that is to say they are entirely bounded by their lit surfaces),
1155 // which can be optimized by handling things as an inverted light volume,
1156 // with the shadow boundaries of the world being simulated by an altered
1157 // (129) bias to stencil clearing on such lights
1158 // FIXME: generate inverted light volumes for use as shadow volumes and
1159 // optimize for them as noted above
1162 void R_Shadow_Stage_Light(int shadowtest)
1165 memset(&m, 0, sizeof(m));
1167 GL_BlendFunc(GL_ONE, GL_ONE);
1168 GL_DepthMask(false);
1170 qglPolygonOffset(0, 0);
1171 //qglDisable(GL_POLYGON_OFFSET_FILL);
1172 GL_Color(1, 1, 1, 1);
1173 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
1174 qglDepthFunc(GL_EQUAL);
1175 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
1176 qglEnable(GL_CULL_FACE);
1178 qglEnable(GL_STENCIL_TEST);
1180 qglDisable(GL_STENCIL_TEST);
1181 if (gl_support_stenciltwoside)
1182 qglDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
1184 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
1185 // only draw light where this geometry was already rendered AND the
1186 // stencil is 128 (values other than this mean shadow)
1187 qglStencilFunc(GL_EQUAL, 128, ~0);
1188 r_shadowstage = SHADOWSTAGE_LIGHT;
1192 void R_Shadow_Stage_End(void)
1195 memset(&m, 0, sizeof(m));
1197 GL_BlendFunc(GL_ONE, GL_ZERO);
1200 qglPolygonOffset(0, 0);
1201 //qglDisable(GL_POLYGON_OFFSET_FILL);
1202 GL_Color(1, 1, 1, 1);
1203 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
1204 GL_Scissor(r_view_x, r_view_y, r_view_width, r_view_height);
1205 qglDepthFunc(GL_LEQUAL);
1206 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
1207 qglDisable(GL_STENCIL_TEST);
1208 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
1209 if (gl_support_stenciltwoside)
1210 qglDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
1212 qglStencilFunc(GL_ALWAYS, 128, ~0);
1213 r_shadowstage = SHADOWSTAGE_NONE;
1216 int R_Shadow_ScissorForBBox(const float *mins, const float *maxs)
1218 int i, ix1, iy1, ix2, iy2;
1219 float x1, y1, x2, y2, x, y, f;
1220 vec3_t smins, smaxs;
1222 if (!r_shadow_scissor.integer)
1224 // if view is inside the box, just say yes it's visible
1225 if (BoxesOverlap(r_vieworigin, r_vieworigin, mins, maxs))
1227 GL_Scissor(r_view_x, r_view_y, r_view_width, r_view_height);
1230 for (i = 0;i < 3;i++)
1232 if (r_viewforward[i] >= 0)
1243 f = DotProduct(r_viewforward, r_vieworigin) + 1;
1244 if (DotProduct(r_viewforward, v2) <= f)
1246 // entirely behind nearclip plane
1249 if (DotProduct(r_viewforward, v) >= f)
1251 // entirely infront of nearclip plane
1252 x1 = y1 = x2 = y2 = 0;
1253 for (i = 0;i < 8;i++)
1255 v[0] = (i & 1) ? mins[0] : maxs[0];
1256 v[1] = (i & 2) ? mins[1] : maxs[1];
1257 v[2] = (i & 4) ? mins[2] : maxs[2];
1259 GL_TransformToScreen(v, v2);
1260 //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]);
1279 // clipped by nearclip plane
1280 // this is nasty and crude...
1281 // create viewspace bbox
1282 for (i = 0;i < 8;i++)
1284 v[0] = ((i & 1) ? mins[0] : maxs[0]) - r_vieworigin[0];
1285 v[1] = ((i & 2) ? mins[1] : maxs[1]) - r_vieworigin[1];
1286 v[2] = ((i & 4) ? mins[2] : maxs[2]) - r_vieworigin[2];
1287 v2[0] = -DotProduct(v, r_viewleft);
1288 v2[1] = DotProduct(v, r_viewup);
1289 v2[2] = DotProduct(v, r_viewforward);
1292 if (smins[0] > v2[0]) smins[0] = v2[0];
1293 if (smaxs[0] < v2[0]) smaxs[0] = v2[0];
1294 if (smins[1] > v2[1]) smins[1] = v2[1];
1295 if (smaxs[1] < v2[1]) smaxs[1] = v2[1];
1296 if (smins[2] > v2[2]) smins[2] = v2[2];
1297 if (smaxs[2] < v2[2]) smaxs[2] = v2[2];
1301 smins[0] = smaxs[0] = v2[0];
1302 smins[1] = smaxs[1] = v2[1];
1303 smins[2] = smaxs[2] = v2[2];
1306 // now we have a bbox in viewspace
1307 // clip it to the view plane
1310 // return true if that culled the box
1311 if (smins[2] >= smaxs[2])
1313 // ok some of it is infront of the view, transform each corner back to
1314 // worldspace and then to screenspace and make screen rect
1315 // initialize these variables just to avoid compiler warnings
1316 x1 = y1 = x2 = y2 = 0;
1317 for (i = 0;i < 8;i++)
1319 v2[0] = (i & 1) ? smins[0] : smaxs[0];
1320 v2[1] = (i & 2) ? smins[1] : smaxs[1];
1321 v2[2] = (i & 4) ? smins[2] : smaxs[2];
1322 v[0] = v2[0] * -r_viewleft[0] + v2[1] * r_viewup[0] + v2[2] * r_viewforward[0] + r_vieworigin[0];
1323 v[1] = v2[0] * -r_viewleft[1] + v2[1] * r_viewup[1] + v2[2] * r_viewforward[1] + r_vieworigin[1];
1324 v[2] = v2[0] * -r_viewleft[2] + v2[1] * r_viewup[2] + v2[2] * r_viewforward[2] + r_vieworigin[2];
1326 GL_TransformToScreen(v, v2);
1327 //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]);
1344 // this code doesn't handle boxes with any points behind view properly
1345 x1 = 1000;x2 = -1000;
1346 y1 = 1000;y2 = -1000;
1347 for (i = 0;i < 8;i++)
1349 v[0] = (i & 1) ? mins[0] : maxs[0];
1350 v[1] = (i & 2) ? mins[1] : maxs[1];
1351 v[2] = (i & 4) ? mins[2] : maxs[2];
1353 GL_TransformToScreen(v, v2);
1354 //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]);
1372 //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
1373 if (ix1 < r_view_x) ix1 = r_view_x;
1374 if (iy1 < r_view_y) iy1 = r_view_y;
1375 if (ix2 > r_view_x + r_view_width) ix2 = r_view_x + r_view_width;
1376 if (iy2 > r_view_y + r_view_height) iy2 = r_view_y + r_view_height;
1377 if (ix2 <= ix1 || iy2 <= iy1)
1379 // set up the scissor rectangle
1380 GL_Scissor(ix1, vid.realheight - iy2, ix2 - ix1, iy2 - iy1);
1381 //qglScissor(ix1, iy1, ix2 - ix1, iy2 - iy1);
1382 //qglEnable(GL_SCISSOR_TEST);
1387 static void R_Shadow_VertexShadingWithXYZAttenuation(int numverts, const float *vertex3f, const float *normal3f, const float *lightcolor, const matrix4x4_t *m)
1389 float *color4f = varray_color4f;
1390 float dist, dot, intensity, v[3], n[3];
1391 for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
1393 Matrix4x4_Transform(m, vertex3f, v);
1394 if ((dist = DotProduct(v, v)) < 1)
1396 Matrix4x4_Transform3x3(m, normal3f, n);
1397 if ((dot = DotProduct(n, v)) > 0)
1400 intensity = dot / sqrt(VectorLength2(v) * VectorLength2(n));
1401 intensity *= pow(1 - dist, r_shadow_attenpower) * r_shadow_attenscale;
1402 VectorScale(lightcolor, intensity, color4f);
1407 VectorClear(color4f);
1413 VectorClear(color4f);
1419 static void R_Shadow_VertexShadingWithZAttenuation(int numverts, const float *vertex3f, const float *normal3f, const float *lightcolor, const matrix4x4_t *m)
1421 float *color4f = varray_color4f;
1422 float dist, dot, intensity, v[3], n[3];
1423 for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
1425 Matrix4x4_Transform(m, vertex3f, v);
1426 if ((dist = fabs(v[2])) < 1)
1428 Matrix4x4_Transform3x3(m, normal3f, n);
1429 if ((dot = DotProduct(n, v)) > 0)
1431 intensity = dot / sqrt(VectorLength2(v) * VectorLength2(n));
1432 intensity *= pow(1 - dist, r_shadow_attenpower) * r_shadow_attenscale;
1433 VectorScale(lightcolor, intensity, color4f);
1438 VectorClear(color4f);
1444 VectorClear(color4f);
1450 static void R_Shadow_VertexShading(int numverts, const float *vertex3f, const float *normal3f, const float *lightcolor, const matrix4x4_t *m)
1452 float *color4f = varray_color4f;
1453 float dot, intensity, v[3], n[3];
1454 for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
1456 Matrix4x4_Transform(m, vertex3f, v);
1457 Matrix4x4_Transform3x3(m, normal3f, n);
1458 if ((dot = DotProduct(n, v)) > 0)
1460 intensity = dot / sqrt(VectorLength2(v) * VectorLength2(n));
1461 VectorScale(lightcolor, intensity, color4f);
1466 VectorClear(color4f);
1472 static void R_Shadow_VertexNoShadingWithXYZAttenuation(int numverts, const float *vertex3f, const float *lightcolor, const matrix4x4_t *m)
1474 float *color4f = varray_color4f;
1475 float dist, intensity, v[3];
1476 for (;numverts > 0;numverts--, vertex3f += 3, color4f += 4)
1478 Matrix4x4_Transform(m, vertex3f, v);
1479 if ((dist = DotProduct(v, v)) < 1)
1482 intensity = pow(1 - dist, r_shadow_attenpower) * r_shadow_attenscale;
1483 VectorScale(lightcolor, intensity, color4f);
1488 VectorClear(color4f);
1494 static void R_Shadow_VertexNoShadingWithZAttenuation(int numverts, const float *vertex3f, const float *lightcolor, const matrix4x4_t *m)
1496 float *color4f = varray_color4f;
1497 float dist, intensity, v[3];
1498 for (;numverts > 0;numverts--, vertex3f += 3, color4f += 4)
1500 Matrix4x4_Transform(m, vertex3f, v);
1501 if ((dist = fabs(v[2])) < 1)
1503 intensity = pow(1 - dist, r_shadow_attenpower) * r_shadow_attenscale;
1504 VectorScale(lightcolor, intensity, color4f);
1509 VectorClear(color4f);
1515 // TODO: use glTexGen instead of feeding vertices to texcoordpointer?
1516 #define USETEXMATRIX
1518 #ifndef USETEXMATRIX
1519 // this should be done in a texture matrix or vertex program when possible, but here's code to do it manually
1520 // if hardware texcoord manipulation is not available (or not suitable, this would really benefit from 3DNow! or SSE
1521 static void R_Shadow_Transform_Vertex3f_TexCoord3f(float *tc3f, int numverts, const float *vertex3f, const matrix4x4_t *matrix)
1525 tc3f[0] = vertex3f[0] * matrix->m[0][0] + vertex3f[1] * matrix->m[0][1] + vertex3f[2] * matrix->m[0][2] + matrix->m[0][3];
1526 tc3f[1] = vertex3f[0] * matrix->m[1][0] + vertex3f[1] * matrix->m[1][1] + vertex3f[2] * matrix->m[1][2] + matrix->m[1][3];
1527 tc3f[2] = vertex3f[0] * matrix->m[2][0] + vertex3f[1] * matrix->m[2][1] + vertex3f[2] * matrix->m[2][2] + matrix->m[2][3];
1534 static void R_Shadow_Transform_Vertex3f_TexCoord2f(float *tc2f, int numverts, const float *vertex3f, const matrix4x4_t *matrix)
1538 tc2f[0] = vertex3f[0] * matrix->m[0][0] + vertex3f[1] * matrix->m[0][1] + vertex3f[2] * matrix->m[0][2] + matrix->m[0][3];
1539 tc2f[1] = vertex3f[0] * matrix->m[1][0] + vertex3f[1] * matrix->m[1][1] + vertex3f[2] * matrix->m[1][2] + matrix->m[1][3];
1547 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)
1551 for (i = 0;i < numverts;i++, vertex3f += 3, svector3f += 3, tvector3f += 3, normal3f += 3, out3f += 3)
1553 VectorSubtract(vertex3f, relativelightorigin, lightdir);
1554 // the cubemap normalizes this for us
1555 out3f[0] = DotProduct(svector3f, lightdir);
1556 out3f[1] = DotProduct(tvector3f, lightdir);
1557 out3f[2] = DotProduct(normal3f, lightdir);
1561 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)
1564 float lightdir[3], eyedir[3], halfdir[3];
1565 for (i = 0;i < numverts;i++, vertex3f += 3, svector3f += 3, tvector3f += 3, normal3f += 3, out3f += 3)
1567 VectorSubtract(vertex3f, relativelightorigin, lightdir);
1568 VectorNormalizeFast(lightdir);
1569 VectorSubtract(vertex3f, relativeeyeorigin, eyedir);
1570 VectorNormalizeFast(eyedir);
1571 VectorAdd(lightdir, eyedir, halfdir);
1572 // the cubemap normalizes this for us
1573 out3f[0] = DotProduct(svector3f, halfdir);
1574 out3f[1] = DotProduct(tvector3f, halfdir);
1575 out3f[2] = DotProduct(normal3f, halfdir);
1579 void R_Shadow_RenderLighting(int numverts, int numtriangles, const int *elements, const float *vertex3f, const float *svector3f, const float *tvector3f, const float *normal3f, const float *texcoord2f, const float *relativelightorigin, const float *relativeeyeorigin, const float *lightcolor, const matrix4x4_t *matrix_modeltolight, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz, rtexture_t *basetexture, rtexture_t *bumptexture, rtexture_t *glosstexture, rtexture_t *lightcubemap, vec_t ambientscale, vec_t diffusescale, vec_t specularscale)
1582 float color[3], color2[3], colorscale;
1584 // FIXME: support EF_NODEPTHTEST
1585 GL_DepthMask(false);
1588 bumptexture = r_shadow_blankbumptexture;
1589 specularscale *= r_shadow_glossintensity.value;
1592 if (r_shadow_gloss.integer >= 2)
1594 glosstexture = r_shadow_blankglosstexture;
1595 specularscale *= r_shadow_gloss2intensity.value;
1599 glosstexture = r_shadow_blankblacktexture;
1603 if (r_shadow_gloss.integer < 1)
1606 lightcubemap = r_shadow_blankwhitecubetexture;
1607 if (ambientscale + diffusescale + specularscale < 0.01)
1609 if (r_shadow_glsl.integer && r_shadow_program_light[0])
1611 unsigned int perm, prog;
1612 // GLSL shader path (GFFX5200, Radeon 9500)
1613 memset(&m, 0, sizeof(m));
1614 m.pointer_vertex = vertex3f;
1615 m.pointer_texcoord[0] = texcoord2f;
1616 m.pointer_texcoord3f[1] = svector3f;
1617 m.pointer_texcoord3f[2] = tvector3f;
1618 m.pointer_texcoord3f[3] = normal3f;
1619 m.tex[0] = R_GetTexture(bumptexture);
1620 m.tex[1] = R_GetTexture(basetexture);
1621 m.tex[2] = R_GetTexture(glosstexture);
1622 m.texcubemap[3] = R_GetTexture(lightcubemap);
1623 // TODO: support fog (after renderer is converted to texture fog)
1624 m.tex[4] = R_GetTexture(r_shadow_blankwhitetexture);
1625 m.texmatrix[3] = *matrix_modeltolight;
1627 GL_BlendFunc(GL_ONE, GL_ONE);
1628 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 0);
1631 // only add a feature to the permutation if that permutation exists
1632 // (otherwise it might end up not using a shader at all, which looks
1633 // worse than using less features)
1634 if (specularscale && r_shadow_program_light[perm | SHADERPERMUTATION_SPECULAR])
1635 perm |= SHADERPERMUTATION_SPECULAR;
1636 //if (fog && r_shadow_program_light[perm | SHADERPERMUTATION_FOG])
1637 // perm |= SHADERPERMUTATION_FOG;
1638 if (lightcubemap && r_shadow_program_light[perm | SHADERPERMUTATION_CUBEFILTER])
1639 perm |= SHADERPERMUTATION_CUBEFILTER;
1640 if (r_shadow_glsl_offsetmapping.integer && r_shadow_program_light[perm | SHADERPERMUTATION_OFFSETMAPPING])
1641 perm |= SHADERPERMUTATION_OFFSETMAPPING;
1642 prog = r_shadow_program_light[perm];
1643 qglUseProgramObjectARB(prog);CHECKGLERROR
1644 // TODO: support fog (after renderer is converted to texture fog)
1645 if (perm & SHADERPERMUTATION_FOG)
1647 qglUniform1fARB(qglGetUniformLocationARB(prog, "FogRangeRecip"), 0);CHECKGLERROR
1649 qglUniform1fARB(qglGetUniformLocationARB(prog, "AmbientScale"), ambientscale);CHECKGLERROR
1650 qglUniform1fARB(qglGetUniformLocationARB(prog, "DiffuseScale"), diffusescale);CHECKGLERROR
1651 if (perm & SHADERPERMUTATION_SPECULAR)
1653 qglUniform1fARB(qglGetUniformLocationARB(prog, "SpecularPower"), 8);CHECKGLERROR
1654 qglUniform1fARB(qglGetUniformLocationARB(prog, "SpecularScale"), specularscale);CHECKGLERROR
1656 qglUniform3fARB(qglGetUniformLocationARB(prog, "LightColor"), lightcolor[0], lightcolor[1], lightcolor[2]);CHECKGLERROR
1657 qglUniform3fARB(qglGetUniformLocationARB(prog, "LightPosition"), relativelightorigin[0], relativelightorigin[1], relativelightorigin[2]);CHECKGLERROR
1658 if (perm & (SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_FOG | SHADERPERMUTATION_OFFSETMAPPING))
1660 qglUniform3fARB(qglGetUniformLocationARB(prog, "EyePosition"), relativeeyeorigin[0], relativeeyeorigin[1], relativeeyeorigin[2]);CHECKGLERROR
1662 if (perm & SHADERPERMUTATION_OFFSETMAPPING)
1664 // these are * 0.25 because the offsetmapping shader does the process 4 times
1665 qglUniform1fARB(qglGetUniformLocationARB(prog, "OffsetMapping_Scale"), r_shadow_glsl_offsetmapping_scale.value * 0.25);CHECKGLERROR
1666 qglUniform1fARB(qglGetUniformLocationARB(prog, "OffsetMapping_Bias"), r_shadow_glsl_offsetmapping_bias.value * 0.25);CHECKGLERROR
1669 GL_LockArrays(0, numverts);
1670 R_Mesh_Draw(numverts, numtriangles, elements);
1672 c_rt_lighttris += numtriangles;
1673 GL_LockArrays(0, 0);
1674 qglUseProgramObjectARB(0);
1675 // HACK HACK HACK: work around for stupid NVIDIA bug that causes GL_OUT_OF_MEMORY and/or software rendering
1676 qglBegin(GL_TRIANGLES);
1680 else if (gl_dot3arb && gl_texturecubemap && gl_combine.integer && gl_stencil)
1683 bumptexture = r_shadow_blankbumptexture;
1685 glosstexture = r_shadow_blankglosstexture;
1689 colorscale = ambientscale;
1690 // colorscale accounts for how much we multiply the brightness
1693 // mult is how many times the final pass of the lighting will be
1694 // performed to get more brightness than otherwise possible.
1696 // Limit mult to 64 for sanity sake.
1697 if (r_shadow_texture3d.integer && lightcubemap && r_textureunits.integer >= 4)
1699 // 3 3D combine path (Geforce3, Radeon 8500)
1700 memset(&m, 0, sizeof(m));
1701 m.pointer_vertex = vertex3f;
1702 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
1704 m.pointer_texcoord3f[0] = vertex3f;
1705 m.texmatrix[0] = *matrix_modeltoattenuationxyz;
1707 m.pointer_texcoord3f[0] = varray_texcoord3f[0];
1708 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
1710 m.tex[1] = R_GetTexture(basetexture);
1711 m.pointer_texcoord[1] = texcoord2f;
1712 m.texcubemap[2] = R_GetTexture(lightcubemap);
1714 m.pointer_texcoord3f[2] = vertex3f;
1715 m.texmatrix[2] = *matrix_modeltolight;
1717 m.pointer_texcoord3f[2] = varray_texcoord3f[2];
1718 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[2], numverts, vertex3f, matrix_modeltolight);
1720 GL_BlendFunc(GL_ONE, GL_ONE);
1722 else if (r_shadow_texture3d.integer && !lightcubemap && r_textureunits.integer >= 2)
1724 // 2 3D combine path (Geforce3, original Radeon)
1725 memset(&m, 0, sizeof(m));
1726 m.pointer_vertex = vertex3f;
1727 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
1729 m.pointer_texcoord3f[0] = vertex3f;
1730 m.texmatrix[0] = *matrix_modeltoattenuationxyz;
1732 m.pointer_texcoord3f[0] = varray_texcoord3f[0];
1733 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
1735 m.tex[1] = R_GetTexture(basetexture);
1736 m.pointer_texcoord[1] = texcoord2f;
1737 GL_BlendFunc(GL_ONE, GL_ONE);
1739 else if (r_textureunits.integer >= 4 && lightcubemap)
1741 // 4 2D combine path (Geforce3, Radeon 8500)
1742 memset(&m, 0, sizeof(m));
1743 m.pointer_vertex = vertex3f;
1744 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
1746 m.pointer_texcoord3f[0] = vertex3f;
1747 m.texmatrix[0] = *matrix_modeltoattenuationxyz;
1749 m.pointer_texcoord[0] = varray_texcoord2f[0];
1750 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
1752 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1754 m.pointer_texcoord3f[1] = vertex3f;
1755 m.texmatrix[1] = *matrix_modeltoattenuationz;
1757 m.pointer_texcoord[1] = varray_texcoord2f[1];
1758 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[1], numverts, vertex3f, matrix_modeltoattenuationz);
1760 m.tex[2] = R_GetTexture(basetexture);
1761 m.pointer_texcoord[2] = texcoord2f;
1764 m.texcubemap[3] = R_GetTexture(lightcubemap);
1766 m.pointer_texcoord3f[3] = vertex3f;
1767 m.texmatrix[3] = *matrix_modeltolight;
1769 m.pointer_texcoord3f[3] = varray_texcoord3f[3];
1770 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[3], numverts, vertex3f, matrix_modeltolight);
1773 GL_BlendFunc(GL_ONE, GL_ONE);
1775 else if (r_textureunits.integer >= 3 && !lightcubemap)
1777 // 3 2D combine path (Geforce3, original Radeon)
1778 memset(&m, 0, sizeof(m));
1779 m.pointer_vertex = vertex3f;
1780 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
1782 m.pointer_texcoord3f[0] = vertex3f;
1783 m.texmatrix[0] = *matrix_modeltoattenuationxyz;
1785 m.pointer_texcoord[0] = varray_texcoord2f[0];
1786 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
1788 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1790 m.pointer_texcoord3f[1] = vertex3f;
1791 m.texmatrix[1] = *matrix_modeltoattenuationz;
1793 m.pointer_texcoord[1] = varray_texcoord2f[1];
1794 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[1], numverts, vertex3f, matrix_modeltoattenuationz);
1796 m.tex[2] = R_GetTexture(basetexture);
1797 m.pointer_texcoord[2] = texcoord2f;
1798 GL_BlendFunc(GL_ONE, GL_ONE);
1802 // 2/2/2 2D combine path (any dot3 card)
1803 memset(&m, 0, sizeof(m));
1804 m.pointer_vertex = vertex3f;
1805 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
1807 m.pointer_texcoord3f[0] = vertex3f;
1808 m.texmatrix[0] = *matrix_modeltoattenuationxyz;
1810 m.pointer_texcoord[0] = varray_texcoord2f[0];
1811 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
1813 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1815 m.pointer_texcoord3f[1] = vertex3f;
1816 m.texmatrix[1] = *matrix_modeltoattenuationz;
1818 m.pointer_texcoord[1] = varray_texcoord2f[1];
1819 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[1], numverts, vertex3f, matrix_modeltoattenuationz);
1822 GL_ColorMask(0,0,0,1);
1823 GL_BlendFunc(GL_ONE, GL_ZERO);
1824 GL_LockArrays(0, numverts);
1825 R_Mesh_Draw(numverts, numtriangles, elements);
1826 GL_LockArrays(0, 0);
1828 c_rt_lighttris += numtriangles;
1830 memset(&m, 0, sizeof(m));
1831 m.pointer_vertex = vertex3f;
1832 m.tex[0] = R_GetTexture(basetexture);
1833 m.pointer_texcoord[0] = texcoord2f;
1836 m.texcubemap[1] = R_GetTexture(lightcubemap);
1838 m.pointer_texcoord3f[1] = vertex3f;
1839 m.texmatrix[1] = *matrix_modeltolight;
1841 m.pointer_texcoord3f[1] = varray_texcoord3f[1];
1842 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltolight);
1845 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
1847 // this final code is shared
1849 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 0);
1850 VectorScale(lightcolor, colorscale, color2);
1851 GL_LockArrays(0, numverts);
1852 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1854 GL_Color(bound(0, color2[0], 1), bound(0, color2[1], 1), bound(0, color2[2], 1), 1);
1855 R_Mesh_Draw(numverts, numtriangles, elements);
1857 c_rt_lighttris += numtriangles;
1859 GL_LockArrays(0, 0);
1864 colorscale = diffusescale;
1865 // colorscale accounts for how much we multiply the brightness
1868 // mult is how many times the final pass of the lighting will be
1869 // performed to get more brightness than otherwise possible.
1871 // Limit mult to 64 for sanity sake.
1872 if (r_shadow_texture3d.integer && r_textureunits.integer >= 4)
1874 // 3/2 3D combine path (Geforce3, Radeon 8500)
1875 memset(&m, 0, sizeof(m));
1876 m.pointer_vertex = vertex3f;
1877 m.tex[0] = R_GetTexture(bumptexture);
1878 m.texcombinergb[0] = GL_REPLACE;
1879 m.pointer_texcoord[0] = texcoord2f;
1880 m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
1881 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1882 m.pointer_texcoord3f[1] = varray_texcoord3f[1];
1883 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin);
1884 m.tex3d[2] = R_GetTexture(r_shadow_attenuation3dtexture);
1886 m.pointer_texcoord3f[2] = vertex3f;
1887 m.texmatrix[2] = *matrix_modeltoattenuationxyz;
1889 m.pointer_texcoord3f[2] = varray_texcoord3f[2];
1890 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[2], numverts, vertex3f, matrix_modeltoattenuationxyz);
1893 GL_ColorMask(0,0,0,1);
1894 GL_BlendFunc(GL_ONE, GL_ZERO);
1895 GL_LockArrays(0, numverts);
1896 R_Mesh_Draw(numverts, numtriangles, elements);
1897 GL_LockArrays(0, 0);
1899 c_rt_lighttris += numtriangles;
1901 memset(&m, 0, sizeof(m));
1902 m.pointer_vertex = vertex3f;
1903 m.tex[0] = R_GetTexture(basetexture);
1904 m.pointer_texcoord[0] = texcoord2f;
1907 m.texcubemap[1] = R_GetTexture(lightcubemap);
1909 m.pointer_texcoord3f[1] = vertex3f;
1910 m.texmatrix[1] = *matrix_modeltolight;
1912 m.pointer_texcoord3f[1] = varray_texcoord3f[1];
1913 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltolight);
1916 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
1918 else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && lightcubemap)
1920 // 1/2/2 3D combine path (original Radeon)
1921 memset(&m, 0, sizeof(m));
1922 m.pointer_vertex = vertex3f;
1923 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
1925 m.pointer_texcoord3f[0] = vertex3f;
1926 m.texmatrix[0] = *matrix_modeltoattenuationxyz;
1928 m.pointer_texcoord3f[0] = varray_texcoord3f[0];
1929 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
1932 GL_ColorMask(0,0,0,1);
1933 GL_BlendFunc(GL_ONE, GL_ZERO);
1934 GL_LockArrays(0, numverts);
1935 R_Mesh_Draw(numverts, numtriangles, elements);
1936 GL_LockArrays(0, 0);
1938 c_rt_lighttris += numtriangles;
1940 memset(&m, 0, sizeof(m));
1941 m.pointer_vertex = vertex3f;
1942 m.tex[0] = R_GetTexture(bumptexture);
1943 m.texcombinergb[0] = GL_REPLACE;
1944 m.pointer_texcoord[0] = texcoord2f;
1945 m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
1946 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1947 m.pointer_texcoord3f[1] = varray_texcoord3f[1];
1948 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin);
1950 GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
1951 GL_LockArrays(0, numverts);
1952 R_Mesh_Draw(numverts, numtriangles, elements);
1953 GL_LockArrays(0, 0);
1955 c_rt_lighttris += numtriangles;
1957 memset(&m, 0, sizeof(m));
1958 m.pointer_vertex = vertex3f;
1959 m.tex[0] = R_GetTexture(basetexture);
1960 m.pointer_texcoord[0] = texcoord2f;
1963 m.texcubemap[1] = R_GetTexture(lightcubemap);
1965 m.pointer_texcoord3f[1] = vertex3f;
1966 m.texmatrix[1] = *matrix_modeltolight;
1968 m.pointer_texcoord3f[1] = varray_texcoord3f[1];
1969 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltolight);
1972 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
1974 else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && !lightcubemap)
1976 // 2/2 3D combine path (original Radeon)
1977 memset(&m, 0, sizeof(m));
1978 m.pointer_vertex = vertex3f;
1979 m.tex[0] = R_GetTexture(bumptexture);
1980 m.texcombinergb[0] = GL_REPLACE;
1981 m.pointer_texcoord[0] = texcoord2f;
1982 m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
1983 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1984 m.pointer_texcoord3f[1] = varray_texcoord3f[1];
1985 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin);
1987 GL_ColorMask(0,0,0,1);
1988 GL_BlendFunc(GL_ONE, GL_ZERO);
1989 GL_LockArrays(0, numverts);
1990 R_Mesh_Draw(numverts, numtriangles, elements);
1991 GL_LockArrays(0, 0);
1993 c_rt_lighttris += numtriangles;
1995 memset(&m, 0, sizeof(m));
1996 m.pointer_vertex = vertex3f;
1997 m.tex[0] = R_GetTexture(basetexture);
1998 m.pointer_texcoord[0] = texcoord2f;
1999 m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
2001 m.pointer_texcoord3f[1] = vertex3f;
2002 m.texmatrix[1] = *matrix_modeltoattenuationxyz;
2004 m.pointer_texcoord3f[1] = varray_texcoord3f[1];
2005 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltoattenuationxyz);
2007 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2009 else if (r_textureunits.integer >= 4)
2011 // 4/2 2D combine path (Geforce3, Radeon 8500)
2012 memset(&m, 0, sizeof(m));
2013 m.pointer_vertex = vertex3f;
2014 m.tex[0] = R_GetTexture(bumptexture);
2015 m.texcombinergb[0] = GL_REPLACE;
2016 m.pointer_texcoord[0] = texcoord2f;
2017 m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
2018 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2019 m.pointer_texcoord3f[1] = varray_texcoord3f[1];
2020 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin);
2021 m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
2023 m.pointer_texcoord3f[2] = vertex3f;
2024 m.texmatrix[2] = *matrix_modeltoattenuationxyz;
2026 m.pointer_texcoord[2] = varray_texcoord2f[2];
2027 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[2], numverts, vertex3f, matrix_modeltoattenuationxyz);
2029 m.tex[3] = R_GetTexture(r_shadow_attenuation2dtexture);
2031 m.pointer_texcoord3f[3] = vertex3f;
2032 m.texmatrix[3] = *matrix_modeltoattenuationz;
2034 m.pointer_texcoord[3] = varray_texcoord2f[3];
2035 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[3], numverts, vertex3f, matrix_modeltoattenuationz);
2038 GL_ColorMask(0,0,0,1);
2039 GL_BlendFunc(GL_ONE, GL_ZERO);
2040 GL_LockArrays(0, numverts);
2041 R_Mesh_Draw(numverts, numtriangles, elements);
2042 GL_LockArrays(0, 0);
2044 c_rt_lighttris += numtriangles;
2046 memset(&m, 0, sizeof(m));
2047 m.pointer_vertex = vertex3f;
2048 m.tex[0] = R_GetTexture(basetexture);
2049 m.pointer_texcoord[0] = texcoord2f;
2052 m.texcubemap[1] = R_GetTexture(lightcubemap);
2054 m.pointer_texcoord3f[1] = vertex3f;
2055 m.texmatrix[1] = *matrix_modeltolight;
2057 m.pointer_texcoord3f[1] = varray_texcoord3f[1];
2058 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltolight);
2061 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2065 // 2/2/2 2D combine path (any dot3 card)
2066 memset(&m, 0, sizeof(m));
2067 m.pointer_vertex = vertex3f;
2068 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
2070 m.pointer_texcoord3f[0] = vertex3f;
2071 m.texmatrix[0] = *matrix_modeltoattenuationxyz;
2073 m.pointer_texcoord[0] = varray_texcoord2f[0];
2074 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
2076 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
2078 m.pointer_texcoord3f[1] = vertex3f;
2079 m.texmatrix[1] = *matrix_modeltoattenuationz;
2081 m.pointer_texcoord[1] = varray_texcoord2f[1];
2082 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[1], numverts, vertex3f, matrix_modeltoattenuationz);
2085 GL_ColorMask(0,0,0,1);
2086 GL_BlendFunc(GL_ONE, GL_ZERO);
2087 GL_LockArrays(0, numverts);
2088 R_Mesh_Draw(numverts, numtriangles, elements);
2089 GL_LockArrays(0, 0);
2091 c_rt_lighttris += numtriangles;
2093 memset(&m, 0, sizeof(m));
2094 m.pointer_vertex = vertex3f;
2095 m.tex[0] = R_GetTexture(bumptexture);
2096 m.texcombinergb[0] = GL_REPLACE;
2097 m.pointer_texcoord[0] = texcoord2f;
2098 m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
2099 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2100 m.pointer_texcoord3f[1] = varray_texcoord3f[1];
2101 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin);
2103 GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
2104 GL_LockArrays(0, numverts);
2105 R_Mesh_Draw(numverts, numtriangles, elements);
2106 GL_LockArrays(0, 0);
2108 c_rt_lighttris += numtriangles;
2110 memset(&m, 0, sizeof(m));
2111 m.pointer_vertex = vertex3f;
2112 m.tex[0] = R_GetTexture(basetexture);
2113 m.pointer_texcoord[0] = texcoord2f;
2116 m.texcubemap[1] = R_GetTexture(lightcubemap);
2118 m.pointer_texcoord3f[1] = vertex3f;
2119 m.texmatrix[1] = *matrix_modeltolight;
2121 m.pointer_texcoord3f[1] = varray_texcoord3f[1];
2122 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltolight);
2125 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2127 // this final code is shared
2129 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 0);
2130 VectorScale(lightcolor, colorscale, color2);
2131 GL_LockArrays(0, numverts);
2132 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
2134 GL_Color(bound(0, color2[0], 1), bound(0, color2[1], 1), bound(0, color2[2], 1), 1);
2135 R_Mesh_Draw(numverts, numtriangles, elements);
2137 c_rt_lighttris += numtriangles;
2139 GL_LockArrays(0, 0);
2141 if (specularscale && glosstexture != r_shadow_blankblacktexture)
2143 // FIXME: detect blendsquare!
2144 //if (gl_support_blendsquare)
2146 colorscale = specularscale;
2148 if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && lightcubemap /*&& gl_support_blendsquare*/) // FIXME: detect blendsquare!
2150 // 2/0/0/1/2 3D combine blendsquare path
2151 memset(&m, 0, sizeof(m));
2152 m.pointer_vertex = vertex3f;
2153 m.tex[0] = R_GetTexture(bumptexture);
2154 m.pointer_texcoord[0] = texcoord2f;
2155 m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
2156 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2157 m.pointer_texcoord3f[1] = varray_texcoord3f[1];
2158 R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin, relativeeyeorigin);
2160 GL_ColorMask(0,0,0,1);
2161 // this squares the result
2162 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
2163 GL_LockArrays(0, numverts);
2164 R_Mesh_Draw(numverts, numtriangles, elements);
2165 GL_LockArrays(0, 0);
2167 c_rt_lighttris += numtriangles;
2169 memset(&m, 0, sizeof(m));
2170 m.pointer_vertex = vertex3f;
2172 GL_LockArrays(0, numverts);
2173 // square alpha in framebuffer a few times to make it shiny
2174 GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
2175 // these comments are a test run through this math for intensity 0.5
2176 // 0.5 * 0.5 = 0.25 (done by the BlendFunc earlier)
2177 // 0.25 * 0.25 = 0.0625 (this is another pass)
2178 // 0.0625 * 0.0625 = 0.00390625 (this is another pass)
2179 R_Mesh_Draw(numverts, numtriangles, elements);
2181 c_rt_lighttris += numtriangles;
2182 R_Mesh_Draw(numverts, numtriangles, elements);
2184 c_rt_lighttris += numtriangles;
2185 GL_LockArrays(0, 0);
2187 memset(&m, 0, sizeof(m));
2188 m.pointer_vertex = vertex3f;
2189 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
2191 m.pointer_texcoord3f[0] = vertex3f;
2192 m.texmatrix[0] = *matrix_modeltoattenuationxyz;
2194 m.pointer_texcoord3f[0] = varray_texcoord3f[0];
2195 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
2198 GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
2199 GL_LockArrays(0, numverts);
2200 R_Mesh_Draw(numverts, numtriangles, elements);
2201 GL_LockArrays(0, 0);
2203 c_rt_lighttris += numtriangles;
2205 memset(&m, 0, sizeof(m));
2206 m.pointer_vertex = vertex3f;
2207 m.tex[0] = R_GetTexture(glosstexture);
2208 m.pointer_texcoord[0] = texcoord2f;
2211 m.texcubemap[1] = R_GetTexture(lightcubemap);
2213 m.pointer_texcoord3f[1] = vertex3f;
2214 m.texmatrix[1] = *matrix_modeltolight;
2216 m.pointer_texcoord3f[1] = varray_texcoord3f[1];
2217 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltolight);
2220 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2222 else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && !lightcubemap /*&& gl_support_blendsquare*/) // FIXME: detect blendsquare!
2224 // 2/0/0/2 3D combine blendsquare path
2225 memset(&m, 0, sizeof(m));
2226 m.pointer_vertex = vertex3f;
2227 m.tex[0] = R_GetTexture(bumptexture);
2228 m.pointer_texcoord[0] = texcoord2f;
2229 m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
2230 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2231 m.pointer_texcoord3f[1] = varray_texcoord3f[1];
2232 R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin, relativeeyeorigin);
2234 GL_ColorMask(0,0,0,1);
2235 // this squares the result
2236 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
2237 GL_LockArrays(0, numverts);
2238 R_Mesh_Draw(numverts, numtriangles, elements);
2239 GL_LockArrays(0, 0);
2241 c_rt_lighttris += numtriangles;
2243 memset(&m, 0, sizeof(m));
2244 m.pointer_vertex = vertex3f;
2246 GL_LockArrays(0, numverts);
2247 // square alpha in framebuffer a few times to make it shiny
2248 GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
2249 // these comments are a test run through this math for intensity 0.5
2250 // 0.5 * 0.5 = 0.25 (done by the BlendFunc earlier)
2251 // 0.25 * 0.25 = 0.0625 (this is another pass)
2252 // 0.0625 * 0.0625 = 0.00390625 (this is another pass)
2253 R_Mesh_Draw(numverts, numtriangles, elements);
2255 c_rt_lighttris += numtriangles;
2256 R_Mesh_Draw(numverts, numtriangles, elements);
2258 c_rt_lighttris += numtriangles;
2259 GL_LockArrays(0, 0);
2261 memset(&m, 0, sizeof(m));
2262 m.pointer_vertex = vertex3f;
2263 m.tex[0] = R_GetTexture(glosstexture);
2264 m.pointer_texcoord[0] = texcoord2f;
2265 m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
2267 m.pointer_texcoord3f[1] = vertex3f;
2268 m.texmatrix[1] = *matrix_modeltoattenuationxyz;
2270 m.pointer_texcoord3f[1] = varray_texcoord3f[1];
2271 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltoattenuationxyz);
2273 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2277 // 2/0/0/2/2 2D combine blendsquare path
2278 memset(&m, 0, sizeof(m));
2279 m.pointer_vertex = vertex3f;
2280 m.tex[0] = R_GetTexture(bumptexture);
2281 m.pointer_texcoord[0] = texcoord2f;
2282 m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
2283 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
2284 m.pointer_texcoord3f[1] = varray_texcoord3f[1];
2285 R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin, relativeeyeorigin);
2287 GL_ColorMask(0,0,0,1);
2288 // this squares the result
2289 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
2290 GL_LockArrays(0, numverts);
2291 R_Mesh_Draw(numverts, numtriangles, elements);
2292 GL_LockArrays(0, 0);
2294 c_rt_lighttris += numtriangles;
2296 memset(&m, 0, sizeof(m));
2297 m.pointer_vertex = vertex3f;
2299 GL_LockArrays(0, numverts);
2300 // square alpha in framebuffer a few times to make it shiny
2301 GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
2302 // these comments are a test run through this math for intensity 0.5
2303 // 0.5 * 0.5 = 0.25 (done by the BlendFunc earlier)
2304 // 0.25 * 0.25 = 0.0625 (this is another pass)
2305 // 0.0625 * 0.0625 = 0.00390625 (this is another pass)
2306 R_Mesh_Draw(numverts, numtriangles, elements);
2308 c_rt_lighttris += numtriangles;
2309 R_Mesh_Draw(numverts, numtriangles, elements);
2311 c_rt_lighttris += numtriangles;
2312 GL_LockArrays(0, 0);
2314 memset(&m, 0, sizeof(m));
2315 m.pointer_vertex = vertex3f;
2316 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
2318 m.pointer_texcoord3f[0] = vertex3f;
2319 m.texmatrix[0] = *matrix_modeltoattenuationxyz;
2321 m.pointer_texcoord[0] = varray_texcoord2f[0];
2322 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
2324 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
2326 m.pointer_texcoord3f[1] = vertex3f;
2327 m.texmatrix[1] = *matrix_modeltoattenuationz;
2329 m.pointer_texcoord[1] = varray_texcoord2f[1];
2330 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[1], numverts, vertex3f, matrix_modeltoattenuationz);
2333 GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
2334 GL_LockArrays(0, numverts);
2335 R_Mesh_Draw(numverts, numtriangles, elements);
2336 GL_LockArrays(0, 0);
2338 c_rt_lighttris += numtriangles;
2340 memset(&m, 0, sizeof(m));
2341 m.pointer_vertex = vertex3f;
2342 m.tex[0] = R_GetTexture(glosstexture);
2343 m.pointer_texcoord[0] = texcoord2f;
2346 m.texcubemap[1] = R_GetTexture(lightcubemap);
2348 m.pointer_texcoord3f[1] = vertex3f;
2349 m.texmatrix[1] = *matrix_modeltolight;
2351 m.pointer_texcoord3f[1] = varray_texcoord3f[1];
2352 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltolight);
2355 GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
2358 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 0);
2359 VectorScale(lightcolor, colorscale, color2);
2360 GL_LockArrays(0, numverts);
2361 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
2363 GL_Color(bound(0, color2[0], 1), bound(0, color2[1], 1), bound(0, color2[2], 1), 1);
2364 R_Mesh_Draw(numverts, numtriangles, elements);
2366 c_rt_lighttris += numtriangles;
2368 GL_LockArrays(0, 0);
2376 GL_BlendFunc(GL_ONE, GL_ONE);
2377 VectorScale(lightcolor, ambientscale, color2);
2378 memset(&m, 0, sizeof(m));
2379 m.pointer_vertex = vertex3f;
2380 m.tex[0] = R_GetTexture(basetexture);
2381 m.pointer_texcoord[0] = texcoord2f;
2382 if (r_textureunits.integer >= 2)
2385 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
2387 m.pointer_texcoord3f[1] = vertex3f;
2388 m.texmatrix[1] = *matrix_modeltoattenuationxyz;
2390 m.pointer_texcoord[1] = varray_texcoord2f[1];
2391 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[1], numverts, vertex3f, matrix_modeltoattenuationxyz);
2393 if (r_textureunits.integer >= 3)
2395 // Geforce3/Radeon class but not using dot3
2396 m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
2398 m.pointer_texcoord3f[2] = vertex3f;
2399 m.texmatrix[2] = *matrix_modeltoattenuationz;
2401 m.pointer_texcoord[2] = varray_texcoord2f[2];
2402 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[2], numverts, vertex3f, matrix_modeltoattenuationz);
2406 if (r_textureunits.integer >= 3)
2407 m.pointer_color = NULL;
2409 m.pointer_color = varray_color4f;
2411 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
2413 color[0] = bound(0, color2[0], 1);
2414 color[1] = bound(0, color2[1], 1);
2415 color[2] = bound(0, color2[2], 1);
2416 if (r_textureunits.integer >= 3)
2417 GL_Color(color[0], color[1], color[2], 1);
2418 else if (r_textureunits.integer >= 2)
2419 R_Shadow_VertexNoShadingWithZAttenuation(numverts, vertex3f, color, matrix_modeltolight);
2421 R_Shadow_VertexNoShadingWithXYZAttenuation(numverts, vertex3f, color, matrix_modeltolight);
2422 GL_LockArrays(0, numverts);
2423 R_Mesh_Draw(numverts, numtriangles, elements);
2424 GL_LockArrays(0, 0);
2426 c_rt_lighttris += numtriangles;
2431 GL_BlendFunc(GL_ONE, GL_ONE);
2432 VectorScale(lightcolor, diffusescale, color2);
2433 memset(&m, 0, sizeof(m));
2434 m.pointer_vertex = vertex3f;
2435 m.pointer_color = varray_color4f;
2436 m.tex[0] = R_GetTexture(basetexture);
2437 m.pointer_texcoord[0] = texcoord2f;
2438 if (r_textureunits.integer >= 2)
2441 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
2443 m.pointer_texcoord3f[1] = vertex3f;
2444 m.texmatrix[1] = *matrix_modeltoattenuationxyz;
2446 m.pointer_texcoord[1] = varray_texcoord2f[1];
2447 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[1], numverts, vertex3f, matrix_modeltoattenuationxyz);
2449 if (r_textureunits.integer >= 3)
2451 // Geforce3/Radeon class but not using dot3
2452 m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
2454 m.pointer_texcoord3f[2] = vertex3f;
2455 m.texmatrix[2] = *matrix_modeltoattenuationz;
2457 m.pointer_texcoord[2] = varray_texcoord2f[2];
2458 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[2], numverts, vertex3f, matrix_modeltoattenuationz);
2463 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
2465 color[0] = bound(0, color2[0], 1);
2466 color[1] = bound(0, color2[1], 1);
2467 color[2] = bound(0, color2[2], 1);
2468 if (r_textureunits.integer >= 3)
2469 R_Shadow_VertexShading(numverts, vertex3f, normal3f, color, matrix_modeltolight);
2470 else if (r_textureunits.integer >= 2)
2471 R_Shadow_VertexShadingWithZAttenuation(numverts, vertex3f, normal3f, color, matrix_modeltolight);
2473 R_Shadow_VertexShadingWithXYZAttenuation(numverts, vertex3f, normal3f, color, matrix_modeltolight);
2474 GL_LockArrays(0, numverts);
2475 R_Mesh_Draw(numverts, numtriangles, elements);
2476 GL_LockArrays(0, 0);
2478 c_rt_lighttris += numtriangles;
2484 void R_RTLight_UpdateFromDLight(rtlight_t *rtlight, const dlight_t *light, int isstatic)
2488 R_RTLight_Uncompile(rtlight);
2489 memset(rtlight, 0, sizeof(*rtlight));
2491 VectorCopy(light->origin, rtlight->shadoworigin);
2492 VectorCopy(light->color, rtlight->color);
2493 rtlight->radius = light->radius;
2494 //rtlight->cullradius = rtlight->radius;
2495 //rtlight->cullradius2 = rtlight->radius * rtlight->radius;
2496 rtlight->cullmins[0] = rtlight->shadoworigin[0] - rtlight->radius;
2497 rtlight->cullmins[1] = rtlight->shadoworigin[1] - rtlight->radius;
2498 rtlight->cullmins[2] = rtlight->shadoworigin[2] - rtlight->radius;
2499 rtlight->cullmaxs[0] = rtlight->shadoworigin[0] + rtlight->radius;
2500 rtlight->cullmaxs[1] = rtlight->shadoworigin[1] + rtlight->radius;
2501 rtlight->cullmaxs[2] = rtlight->shadoworigin[2] + rtlight->radius;
2502 rtlight->cubemapname[0] = 0;
2503 if (light->cubemapname[0])
2504 strcpy(rtlight->cubemapname, light->cubemapname);
2505 else if (light->cubemapnum > 0)
2506 sprintf(rtlight->cubemapname, "cubemaps/%i", light->cubemapnum);
2507 rtlight->shadow = light->shadow;
2508 rtlight->corona = light->corona;
2509 rtlight->style = light->style;
2510 rtlight->isstatic = isstatic;
2511 rtlight->coronasizescale = light->coronasizescale;
2512 rtlight->ambientscale = light->ambientscale;
2513 rtlight->diffusescale = light->diffusescale;
2514 rtlight->specularscale = light->specularscale;
2515 rtlight->flags = light->flags;
2516 Matrix4x4_Invert_Simple(&rtlight->matrix_worldtolight, &light->matrix);
2517 // ConcatScale won't work here because this needs to scale rotate and
2518 // translate, not just rotate
2519 scale = 1.0f / rtlight->radius;
2520 for (k = 0;k < 3;k++)
2521 for (j = 0;j < 4;j++)
2522 rtlight->matrix_worldtolight.m[k][j] *= scale;
2523 Matrix4x4_Concat(&rtlight->matrix_worldtoattenuationxyz, &matrix_attenuationxyz, &rtlight->matrix_worldtolight);
2524 Matrix4x4_Concat(&rtlight->matrix_worldtoattenuationz, &matrix_attenuationz, &rtlight->matrix_worldtolight);
2526 rtlight->lightmap_cullradius = bound(0, rtlight->radius, 2048.0f);
2527 rtlight->lightmap_cullradius2 = rtlight->lightmap_cullradius * rtlight->lightmap_cullradius;
2528 VectorScale(rtlight->color, rtlight->radius * (rtlight->style >= 0 ? d_lightstylevalue[rtlight->style] : 128) * 0.125f, rtlight->lightmap_light);
2529 rtlight->lightmap_subtract = 1.0f / rtlight->lightmap_cullradius2;
2532 // compiles rtlight geometry
2533 // (undone by R_FreeCompiledRTLight, which R_UpdateLight calls)
2534 void R_RTLight_Compile(rtlight_t *rtlight)
2536 int shadowmeshes, shadowtris, lightmeshes, lighttris, numclusters, numsurfaces;
2537 entity_render_t *ent = &cl_entities[0].render;
2538 model_t *model = ent->model;
2540 // compile the light
2541 rtlight->compiled = true;
2542 rtlight->static_numclusters = 0;
2543 rtlight->static_numclusterpvsbytes = 0;
2544 rtlight->static_clusterlist = NULL;
2545 rtlight->static_clusterpvs = NULL;
2546 rtlight->cullmins[0] = rtlight->shadoworigin[0] - rtlight->radius;
2547 rtlight->cullmins[1] = rtlight->shadoworigin[1] - rtlight->radius;
2548 rtlight->cullmins[2] = rtlight->shadoworigin[2] - rtlight->radius;
2549 rtlight->cullmaxs[0] = rtlight->shadoworigin[0] + rtlight->radius;
2550 rtlight->cullmaxs[1] = rtlight->shadoworigin[1] + rtlight->radius;
2551 rtlight->cullmaxs[2] = rtlight->shadoworigin[2] + rtlight->radius;
2553 if (model && model->GetLightInfo)
2555 // this variable directs the DrawShadowVolume and DrawLight code to capture into the mesh chain instead of rendering
2556 r_shadow_compilingrtlight = rtlight;
2557 R_Shadow_EnlargeClusterBuffer(model->brush.num_pvsclusters);
2558 R_Shadow_EnlargeSurfaceBuffer(model->nummodelsurfaces);
2559 model->GetLightInfo(ent, rtlight->shadoworigin, rtlight->radius, rtlight->cullmins, rtlight->cullmaxs, r_shadow_buffer_clusterlist, r_shadow_buffer_clusterpvs, &numclusters, r_shadow_buffer_surfacelist, r_shadow_buffer_surfacepvs, &numsurfaces);
2560 rtlight->static_numclusterpvsbytes = (model->brush.num_pvsclusters + 7) >> 3;
2561 rtlight->static_clusterpvs = Mem_Alloc(r_shadow_mempool, rtlight->static_numclusterpvsbytes);
2564 rtlight->static_numclusters = numclusters;
2565 rtlight->static_clusterlist = Mem_Alloc(r_shadow_mempool, rtlight->static_numclusters * sizeof(*rtlight->static_clusterlist));
2566 memcpy(rtlight->static_clusterlist, r_shadow_buffer_clusterlist, rtlight->static_numclusters * sizeof(*rtlight->static_clusterlist));
2567 memcpy(rtlight->static_clusterpvs, r_shadow_buffer_clusterpvs, rtlight->static_numclusterpvsbytes);
2569 if (model->DrawShadowVolume && rtlight->shadow)
2571 rtlight->static_meshchain_shadow = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768, 32768, NULL, NULL, NULL, false, false, true);
2572 model->DrawShadowVolume(ent, rtlight->shadoworigin, rtlight->radius, numsurfaces, r_shadow_buffer_surfacelist);
2573 rtlight->static_meshchain_shadow = Mod_ShadowMesh_Finish(r_shadow_mempool, rtlight->static_meshchain_shadow, false, false);
2575 if (model->DrawLight)
2577 rtlight->static_meshchain_light = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768, 32768, NULL, NULL, NULL, true, false, true);
2578 model->DrawLight(ent, rtlight->shadoworigin, vec3_origin, rtlight->radius, vec3_origin, &r_identitymatrix, &r_identitymatrix, &r_identitymatrix, NULL, 0, 0, 0, numsurfaces, r_shadow_buffer_surfacelist);
2579 rtlight->static_meshchain_light = Mod_ShadowMesh_Finish(r_shadow_mempool, rtlight->static_meshchain_light, true, false);
2581 // switch back to rendering when DrawShadowVolume or DrawLight is called
2582 r_shadow_compilingrtlight = NULL;
2586 // use smallest available cullradius - box radius or light radius
2587 //rtlight->cullradius = RadiusFromBoundsAndOrigin(rtlight->cullmins, rtlight->cullmaxs, rtlight->shadoworigin);
2588 //rtlight->cullradius = min(rtlight->cullradius, rtlight->radius);
2592 if (rtlight->static_meshchain_shadow)
2595 for (mesh = rtlight->static_meshchain_shadow;mesh;mesh = mesh->next)
2598 shadowtris += mesh->numtriangles;
2604 if (rtlight->static_meshchain_light)
2607 for (mesh = rtlight->static_meshchain_light;mesh;mesh = mesh->next)
2610 lighttris += mesh->numtriangles;
2614 Con_DPrintf("static light built: %f %f %f : %f %f %f box, %i shadow volume triangles (in %i meshes), %i light 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, lighttris, lightmeshes);
2617 void R_RTLight_Uncompile(rtlight_t *rtlight)
2619 if (rtlight->compiled)
2621 if (rtlight->static_meshchain_shadow)
2622 Mod_ShadowMesh_Free(rtlight->static_meshchain_shadow);
2623 rtlight->static_meshchain_shadow = NULL;
2624 if (rtlight->static_meshchain_light)
2625 Mod_ShadowMesh_Free(rtlight->static_meshchain_light);
2626 rtlight->static_meshchain_light = NULL;
2627 if (rtlight->static_clusterlist)
2628 Mem_Free(rtlight->static_clusterlist);
2629 rtlight->static_clusterlist = NULL;
2630 if (rtlight->static_clusterpvs)
2631 Mem_Free(rtlight->static_clusterpvs);
2632 rtlight->static_clusterpvs = NULL;
2633 rtlight->static_numclusters = 0;
2634 rtlight->static_numclusterpvsbytes = 0;
2635 rtlight->compiled = false;
2639 void R_Shadow_UncompileWorldLights(void)
2642 for (light = r_shadow_worldlightchain;light;light = light->next)
2643 R_RTLight_Uncompile(&light->rtlight);
2646 void R_DrawRTLight(rtlight_t *rtlight, int visiblevolumes)
2648 int i, shadow, usestencil;
2649 entity_render_t *ent;
2651 vec3_t relativelightorigin, relativeeyeorigin, lightcolor, lightcolor2;
2652 rtexture_t *cubemaptexture;
2653 matrix4x4_t matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz;
2654 int numclusters, numsurfaces;
2655 int *clusterlist, *surfacelist;
2657 vec3_t cullmins, cullmaxs;
2661 // skip lights that don't light (corona only lights)
2662 if (rtlight->ambientscale + rtlight->diffusescale + rtlight->specularscale < 0.01)
2665 f = (rtlight->style >= 0 ? d_lightstylevalue[rtlight->style] : 128) * (1.0f / 256.0f) * r_shadow_lightintensityscale.value;
2666 VectorScale(rtlight->color, f, lightcolor);
2667 if (VectorLength2(lightcolor) < 0.01)
2670 if (rtlight->selected)
2672 f = 2 + sin(realtime * M_PI * 4.0);
2673 VectorScale(lightcolor, f, lightcolor);
2677 // loading is done before visibility checks because loading should happen
2678 // all at once at the start of a level, not when it stalls gameplay.
2679 // (especially important to benchmarks)
2680 if (rtlight->isstatic && !rtlight->compiled && r_shadow_staticworldlights.integer)
2681 R_RTLight_Compile(rtlight);
2682 if (rtlight->cubemapname[0])
2683 cubemaptexture = R_Shadow_Cubemap(rtlight->cubemapname);
2685 cubemaptexture = NULL;
2687 cullmins[0] = rtlight->shadoworigin[0] - rtlight->radius;
2688 cullmins[1] = rtlight->shadoworigin[1] - rtlight->radius;
2689 cullmins[2] = rtlight->shadoworigin[2] - rtlight->radius;
2690 cullmaxs[0] = rtlight->shadoworigin[0] + rtlight->radius;
2691 cullmaxs[1] = rtlight->shadoworigin[1] + rtlight->radius;
2692 cullmaxs[2] = rtlight->shadoworigin[2] + rtlight->radius;
2693 if (rtlight->style >= 0 && d_lightstylevalue[rtlight->style] <= 0)
2700 if (rtlight->compiled && r_shadow_staticworldlights.integer)
2702 // compiled light, world available and can receive realtime lighting
2703 // retrieve cluster information
2704 numclusters = rtlight->static_numclusters;
2705 clusterlist = rtlight->static_clusterlist;
2706 clusterpvs = rtlight->static_clusterpvs;
2707 VectorCopy(rtlight->cullmins, cullmins);
2708 VectorCopy(rtlight->cullmaxs, cullmaxs);
2710 else if (r_refdef.worldmodel && r_refdef.worldmodel->GetLightInfo)
2712 // dynamic light, world available and can receive realtime lighting
2713 // if the light box is offscreen, skip it right away
2714 if (R_CullBox(cullmins, cullmaxs))
2716 // calculate lit surfaces and clusters
2717 R_Shadow_EnlargeClusterBuffer(r_refdef.worldmodel->brush.num_pvsclusters);
2718 R_Shadow_EnlargeSurfaceBuffer(r_refdef.worldmodel->nummodelsurfaces);
2719 r_refdef.worldmodel->GetLightInfo(&cl_entities[0].render, rtlight->shadoworigin, rtlight->radius, cullmins, cullmaxs, r_shadow_buffer_clusterlist, r_shadow_buffer_clusterpvs, &numclusters, r_shadow_buffer_surfacelist, r_shadow_buffer_surfacepvs, &numsurfaces);
2720 clusterlist = r_shadow_buffer_clusterlist;
2721 clusterpvs = r_shadow_buffer_clusterpvs;
2722 surfacelist = r_shadow_buffer_surfacelist;
2724 // if the reduced cluster bounds are offscreen, skip it
2725 if (R_CullBox(cullmins, cullmaxs))
2727 // check if light is illuminating any visible clusters
2730 for (i = 0;i < numclusters;i++)
2731 if (CHECKPVSBIT(r_pvsbits, clusterlist[i]))
2733 if (i == numclusters)
2736 // set up a scissor rectangle for this light
2737 if (R_Shadow_ScissorForBBox(cullmins, cullmaxs))
2740 shadow = rtlight->shadow && (rtlight->isstatic ? r_rtworldshadows : r_rtdlightshadows);
2743 if (shadow && (gl_stencil || visiblevolumes))
2745 if (!visiblevolumes)
2747 R_Shadow_Stage_ShadowVolumes();
2750 ent = &cl_entities[0].render;
2751 if (r_shadow_staticworldlights.integer && rtlight->compiled)
2753 memset(&m, 0, sizeof(m));
2754 R_Mesh_Matrix(&ent->matrix);
2755 for (mesh = rtlight->static_meshchain_shadow;mesh;mesh = mesh->next)
2757 m.pointer_vertex = mesh->vertex3f;
2759 GL_LockArrays(0, mesh->numverts);
2760 if (r_shadowstage == SHADOWSTAGE_STENCIL)
2762 // increment stencil if backface is behind depthbuffer
2763 qglCullFace(GL_BACK); // quake is backwards, this culls front faces
2764 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
2765 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
2766 c_rtcached_shadowmeshes++;
2767 c_rtcached_shadowtris += mesh->numtriangles;
2768 // decrement stencil if frontface is behind depthbuffer
2769 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
2770 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
2772 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
2773 c_rtcached_shadowmeshes++;
2774 c_rtcached_shadowtris += mesh->numtriangles;
2775 GL_LockArrays(0, 0);
2778 else if (numsurfaces)
2780 Matrix4x4_Transform(&ent->inversematrix, rtlight->shadoworigin, relativelightorigin);
2781 ent->model->DrawShadowVolume(ent, relativelightorigin, rtlight->radius, numsurfaces, surfacelist);
2783 if (r_drawentities.integer)
2785 for (i = 0;i < r_refdef.numentities;i++)
2787 ent = r_refdef.entities[i];
2789 if (r_shadow_cull.integer)
2791 if (!BoxesOverlap(ent->mins, ent->maxs, cullmins, cullmaxs))
2793 if (r_refdef.worldmodel != NULL && r_refdef.worldmodel->brush.BoxTouchingPVS != NULL && !r_refdef.worldmodel->brush.BoxTouchingPVS(r_refdef.worldmodel, clusterpvs, ent->mins, ent->maxs))
2796 if (!(ent->flags & RENDER_SHADOW) || !ent->model || !ent->model->DrawShadowVolume)
2798 Matrix4x4_Transform(&ent->inversematrix, rtlight->shadoworigin, relativelightorigin);
2799 // light emitting entities should not cast their own shadow
2800 if (VectorLength2(relativelightorigin) < 0.1)
2802 ent->model->DrawShadowVolume(ent, relativelightorigin, rtlight->radius, ent->model->nummodelsurfaces, ent->model->surfacelist);
2807 if (!visiblevolumes)
2809 R_Shadow_Stage_Light(usestencil);
2811 ent = &cl_entities[0].render;
2812 if (ent->model && ent->model->DrawLight && (ent->flags & RENDER_LIGHT))
2814 lightcolor2[0] = lightcolor[0] * ent->colormod[0] * ent->alpha;
2815 lightcolor2[1] = lightcolor[1] * ent->colormod[1] * ent->alpha;
2816 lightcolor2[2] = lightcolor[2] * ent->colormod[2] * ent->alpha;
2817 Matrix4x4_Transform(&ent->inversematrix, rtlight->shadoworigin, relativelightorigin);
2818 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, relativeeyeorigin);
2819 Matrix4x4_Concat(&matrix_modeltolight, &rtlight->matrix_worldtolight, &ent->matrix);
2820 Matrix4x4_Concat(&matrix_modeltoattenuationxyz, &rtlight->matrix_worldtoattenuationxyz, &ent->matrix);
2821 Matrix4x4_Concat(&matrix_modeltoattenuationz, &rtlight->matrix_worldtoattenuationz, &ent->matrix);
2822 if (r_shadow_staticworldlights.integer && rtlight->compiled)
2824 R_Mesh_Matrix(&ent->matrix);
2825 for (mesh = rtlight->static_meshchain_light;mesh;mesh = mesh->next)
2826 R_Shadow_RenderLighting(mesh->numverts, mesh->numtriangles, mesh->element3i, mesh->vertex3f, mesh->svector3f, mesh->tvector3f, mesh->normal3f, mesh->texcoord2f, relativelightorigin, relativeeyeorigin, lightcolor2, &matrix_modeltolight, &matrix_modeltoattenuationxyz, &matrix_modeltoattenuationz, mesh->map_diffuse, mesh->map_normal, mesh->map_specular, cubemaptexture, rtlight->ambientscale, rtlight->diffusescale, rtlight->specularscale);
2829 ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, rtlight->radius, lightcolor2, &matrix_modeltolight, &matrix_modeltoattenuationxyz, &matrix_modeltoattenuationz, cubemaptexture, rtlight->ambientscale, rtlight->diffusescale, rtlight->specularscale, numsurfaces, surfacelist);
2831 if (r_drawentities.integer)
2833 for (i = 0;i < r_refdef.numentities;i++)
2835 ent = r_refdef.entities[i];
2836 // can't draw transparent entity lighting here because
2837 // transparent meshes are deferred for later
2838 if (ent->visframe == r_framecount && BoxesOverlap(ent->mins, ent->maxs, cullmins, cullmaxs) && ent->model && ent->model->DrawLight && (ent->flags & (RENDER_LIGHT | RENDER_TRANSPARENT)) == RENDER_LIGHT)
2840 lightcolor2[0] = lightcolor[0] * ent->colormod[0] * ent->alpha;
2841 lightcolor2[1] = lightcolor[1] * ent->colormod[1] * ent->alpha;
2842 lightcolor2[2] = lightcolor[2] * ent->colormod[2] * ent->alpha;
2843 Matrix4x4_Transform(&ent->inversematrix, rtlight->shadoworigin, relativelightorigin);
2844 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, relativeeyeorigin);
2845 Matrix4x4_Concat(&matrix_modeltolight, &rtlight->matrix_worldtolight, &ent->matrix);
2846 Matrix4x4_Concat(&matrix_modeltoattenuationxyz, &rtlight->matrix_worldtoattenuationxyz, &ent->matrix);
2847 Matrix4x4_Concat(&matrix_modeltoattenuationz, &rtlight->matrix_worldtoattenuationz, &ent->matrix);
2848 ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, rtlight->radius, lightcolor2, &matrix_modeltolight, &matrix_modeltoattenuationxyz, &matrix_modeltoattenuationz, cubemaptexture, rtlight->ambientscale, rtlight->diffusescale, rtlight->specularscale, ent->model->nummodelsurfaces, ent->model->surfacelist);
2855 void R_ShadowVolumeLighting(int visiblevolumes)
2861 if (r_refdef.worldmodel && strncmp(r_refdef.worldmodel->name, r_shadow_mapname, sizeof(r_shadow_mapname)))
2862 R_Shadow_EditLights_Reload_f();
2866 memset(&m, 0, sizeof(m));
2869 GL_BlendFunc(GL_ONE, GL_ONE);
2870 GL_DepthMask(false);
2871 GL_DepthTest(r_shadow_visiblevolumes.integer < 2);
2872 qglDisable(GL_CULL_FACE);
2873 GL_Color(0.0, 0.0125, 0.1, 1);
2876 R_Shadow_Stage_Begin();
2877 flag = r_rtworld ? LIGHTFLAG_REALTIMEMODE : LIGHTFLAG_NORMALMODE;
2878 if (r_shadow_debuglight.integer >= 0)
2880 for (lnum = 0, light = r_shadow_worldlightchain;light;lnum++, light = light->next)
2881 if (lnum == r_shadow_debuglight.integer && (light->flags & flag))
2882 R_DrawRTLight(&light->rtlight, visiblevolumes);
2885 for (lnum = 0, light = r_shadow_worldlightchain;light;lnum++, light = light->next)
2886 if (light->flags & flag)
2887 R_DrawRTLight(&light->rtlight, visiblevolumes);
2889 for (lnum = 0, light = r_dlight;lnum < r_numdlights;lnum++, light++)
2890 R_DrawRTLight(&light->rtlight, visiblevolumes);
2894 qglEnable(GL_CULL_FACE);
2895 GL_Scissor(r_view_x, r_view_y, r_view_width, r_view_height);
2898 R_Shadow_Stage_End();
2901 //static char *suffix[6] = {"ft", "bk", "rt", "lf", "up", "dn"};
2902 typedef struct suffixinfo_s
2905 qboolean flipx, flipy, flipdiagonal;
2908 static suffixinfo_t suffix[3][6] =
2911 {"px", false, false, false},
2912 {"nx", false, false, false},
2913 {"py", false, false, false},
2914 {"ny", false, false, false},
2915 {"pz", false, false, false},
2916 {"nz", false, false, false}
2919 {"posx", false, false, false},
2920 {"negx", false, false, false},
2921 {"posy", false, false, false},
2922 {"negy", false, false, false},
2923 {"posz", false, false, false},
2924 {"negz", false, false, false}
2927 {"rt", true, false, true},
2928 {"lf", false, true, true},
2929 {"ft", true, true, false},
2930 {"bk", false, false, false},
2931 {"up", true, false, true},
2932 {"dn", true, false, true}
2936 static int componentorder[4] = {0, 1, 2, 3};
2938 rtexture_t *R_Shadow_LoadCubemap(const char *basename)
2940 int i, j, cubemapsize;
2941 qbyte *cubemappixels, *image_rgba;
2942 rtexture_t *cubemaptexture;
2944 // must start 0 so the first loadimagepixels has no requested width/height
2946 cubemappixels = NULL;
2947 cubemaptexture = NULL;
2948 // keep trying different suffix groups (posx, px, rt) until one loads
2949 for (j = 0;j < 3 && !cubemappixels;j++)
2951 // load the 6 images in the suffix group
2952 for (i = 0;i < 6;i++)
2954 // generate an image name based on the base and and suffix
2955 snprintf(name, sizeof(name), "%s%s", basename, suffix[j][i].suffix);
2957 if ((image_rgba = loadimagepixels(name, false, cubemapsize, cubemapsize)))
2959 // an image loaded, make sure width and height are equal
2960 if (image_width == image_height)
2962 // if this is the first image to load successfully, allocate the cubemap memory
2963 if (!cubemappixels && image_width >= 1)
2965 cubemapsize = image_width;
2966 // note this clears to black, so unavailable sides are black
2967 cubemappixels = Mem_Alloc(tempmempool, 6*cubemapsize*cubemapsize*4);
2969 // copy the image with any flipping needed by the suffix (px and posx types don't need flipping)
2971 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);
2974 Con_Printf("Cubemap image \"%s\" (%ix%i) is not square, OpenGL requires square cubemaps.\n", name, image_width, image_height);
2976 Mem_Free(image_rgba);
2980 // if a cubemap loaded, upload it
2983 if (!r_shadow_filters_texturepool)
2984 r_shadow_filters_texturepool = R_AllocTexturePool();
2985 cubemaptexture = R_LoadTextureCubeMap(r_shadow_filters_texturepool, basename, cubemapsize, cubemappixels, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
2986 Mem_Free(cubemappixels);
2990 Con_Printf("Failed to load Cubemap \"%s\", tried ", basename);
2991 for (j = 0;j < 3;j++)
2992 for (i = 0;i < 6;i++)
2993 Con_Printf("%s\"%s%s.tga\"", j + i > 0 ? ", " : "", basename, suffix[j][i].suffix);
2994 Con_Print(" and was unable to find any of them.\n");
2996 return cubemaptexture;
2999 rtexture_t *R_Shadow_Cubemap(const char *basename)
3002 for (i = 0;i < numcubemaps;i++)
3003 if (!strcasecmp(cubemaps[i].basename, basename))
3004 return cubemaps[i].texture;
3005 if (i >= MAX_CUBEMAPS)
3008 strcpy(cubemaps[i].basename, basename);
3009 cubemaps[i].texture = R_Shadow_LoadCubemap(cubemaps[i].basename);
3010 return cubemaps[i].texture;
3013 void R_Shadow_FreeCubemaps(void)
3016 R_FreeTexturePool(&r_shadow_filters_texturepool);
3019 dlight_t *R_Shadow_NewWorldLight(void)
3022 light = Mem_Alloc(r_shadow_mempool, sizeof(dlight_t));
3023 light->next = r_shadow_worldlightchain;
3024 r_shadow_worldlightchain = light;
3028 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)
3030 VectorCopy(origin, light->origin);
3031 light->angles[0] = angles[0] - 360 * floor(angles[0] / 360);
3032 light->angles[1] = angles[1] - 360 * floor(angles[1] / 360);
3033 light->angles[2] = angles[2] - 360 * floor(angles[2] / 360);
3034 light->color[0] = max(color[0], 0);
3035 light->color[1] = max(color[1], 0);
3036 light->color[2] = max(color[2], 0);
3037 light->radius = max(radius, 0);
3038 light->style = style;
3039 if (light->style < 0 || light->style >= MAX_LIGHTSTYLES)
3041 Con_Printf("R_Shadow_NewWorldLight: invalid light style number %i, must be >= 0 and < %i\n", light->style, MAX_LIGHTSTYLES);
3044 light->shadow = shadowenable;
3045 light->corona = corona;
3048 strlcpy(light->cubemapname, cubemapname, strlen(light->cubemapname));
3049 light->coronasizescale = coronasizescale;
3050 light->ambientscale = ambientscale;
3051 light->diffusescale = diffusescale;
3052 light->specularscale = specularscale;
3053 light->flags = flags;
3054 Matrix4x4_CreateFromQuakeEntity(&light->matrix, light->origin[0], light->origin[1], light->origin[2], light->angles[0], light->angles[1], light->angles[2], 1);
3056 R_RTLight_UpdateFromDLight(&light->rtlight, light, true);
3059 void R_Shadow_FreeWorldLight(dlight_t *light)
3061 dlight_t **lightpointer;
3062 R_RTLight_Uncompile(&light->rtlight);
3063 for (lightpointer = &r_shadow_worldlightchain;*lightpointer && *lightpointer != light;lightpointer = &(*lightpointer)->next);
3064 if (*lightpointer != light)
3065 Sys_Error("R_Shadow_FreeWorldLight: light not linked into chain\n");
3066 *lightpointer = light->next;
3070 void R_Shadow_ClearWorldLights(void)
3072 while (r_shadow_worldlightchain)
3073 R_Shadow_FreeWorldLight(r_shadow_worldlightchain);
3074 r_shadow_selectedlight = NULL;
3075 R_Shadow_FreeCubemaps();
3078 void R_Shadow_SelectLight(dlight_t *light)
3080 if (r_shadow_selectedlight)
3081 r_shadow_selectedlight->selected = false;
3082 r_shadow_selectedlight = light;
3083 if (r_shadow_selectedlight)
3084 r_shadow_selectedlight->selected = true;
3087 void R_Shadow_DrawCursorCallback(const void *calldata1, int calldata2)
3089 float scale = r_editlights_cursorgrid.value * 0.5f;
3090 R_DrawSprite(GL_SRC_ALPHA, GL_ONE, lighttextures[0], false, r_editlights_cursorlocation, r_viewright, r_viewup, scale, -scale, -scale, scale, 1, 1, 1, 0.5f);
3093 void R_Shadow_DrawLightSpriteCallback(const void *calldata1, int calldata2)
3096 const dlight_t *light;
3099 if (light->selected)
3100 intensity = 0.75 + 0.25 * sin(realtime * M_PI * 4.0);
3103 R_DrawSprite(GL_SRC_ALPHA, GL_ONE, lighttextures[calldata2], false, light->origin, r_viewright, r_viewup, 8, -8, -8, 8, intensity, intensity, intensity, 0.5);
3106 void R_Shadow_DrawLightSprites(void)
3112 for (i = 0;i < 5;i++)
3114 lighttextures[i] = NULL;
3115 if ((pic = Draw_CachePic(va("gfx/crosshair%i.tga", i + 1))))
3116 lighttextures[i] = pic->tex;
3119 for (i = 0, light = r_shadow_worldlightchain;light;i++, light = light->next)
3120 R_MeshQueue_AddTransparent(light->origin, R_Shadow_DrawLightSpriteCallback, light, i % 5);
3121 R_MeshQueue_AddTransparent(r_editlights_cursorlocation, R_Shadow_DrawCursorCallback, NULL, 0);
3124 void R_Shadow_SelectLightInView(void)
3126 float bestrating, rating, temp[3];
3127 dlight_t *best, *light;
3130 for (light = r_shadow_worldlightchain;light;light = light->next)
3132 VectorSubtract(light->origin, r_vieworigin, temp);
3133 rating = (DotProduct(temp, r_viewforward) / sqrt(DotProduct(temp, temp)));
3136 rating /= (1 + 0.0625f * sqrt(DotProduct(temp, temp)));
3137 if (bestrating < rating && CL_TraceLine(light->origin, r_vieworigin, NULL, NULL, true, NULL, SUPERCONTENTS_SOLID) == 1.0f)
3139 bestrating = rating;
3144 R_Shadow_SelectLight(best);
3147 void R_Shadow_LoadWorldLights(void)
3149 int n, a, style, shadow, flags;
3150 char tempchar, *lightsstring, *s, *t, name[MAX_QPATH], cubemapname[MAX_QPATH];
3151 float origin[3], radius, color[3], angles[3], corona, coronasizescale, ambientscale, diffusescale, specularscale;
3152 if (r_refdef.worldmodel == NULL)
3154 Con_Print("No map loaded.\n");
3157 FS_StripExtension (r_refdef.worldmodel->name, name, sizeof (name));
3158 strlcat (name, ".rtlights", sizeof (name));
3159 lightsstring = FS_LoadFile(name, tempmempool, false);
3169 for (;COM_Parse(t, true) && strcmp(
3170 if (COM_Parse(t, true))
3172 if (com_token[0] == '!')
3175 origin[0] = atof(com_token+1);
3178 origin[0] = atof(com_token);
3183 while (*s && *s != '\n' && *s != '\r')
3189 // check for modifier flags
3196 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);
3199 flags = LIGHTFLAG_REALTIMEMODE;
3207 coronasizescale = 0.25f;
3209 VectorClear(angles);
3212 if (a < 9 || !strcmp(cubemapname, "\"\""))
3214 // remove quotes on cubemapname
3215 if (cubemapname[0] == '"' && cubemapname[strlen(cubemapname) - 1] == '"')
3217 cubemapname[strlen(cubemapname)-1] = 0;
3218 strcpy(cubemapname, cubemapname + 1);
3222 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);
3225 VectorScale(color, r_editlights_rtlightscolorscale.value, color);
3226 radius *= r_editlights_rtlightssizescale.value;
3227 R_Shadow_UpdateWorldLight(R_Shadow_NewWorldLight(), origin, angles, color, radius, corona, style, shadow, cubemapname, coronasizescale, ambientscale, diffusescale, specularscale, flags);
3235 Con_Printf("invalid rtlights file \"%s\"\n", name);
3236 Mem_Free(lightsstring);
3240 void R_Shadow_SaveWorldLights(void)
3243 int bufchars, bufmaxchars;
3245 char name[MAX_QPATH];
3247 if (!r_shadow_worldlightchain)
3249 if (r_refdef.worldmodel == NULL)
3251 Con_Print("No map loaded.\n");
3254 FS_StripExtension (r_refdef.worldmodel->name, name, sizeof (name));
3255 strlcat (name, ".rtlights", sizeof (name));
3256 bufchars = bufmaxchars = 0;
3258 for (light = r_shadow_worldlightchain;light;light = light->next)
3260 if (light->coronasizescale != 0.25f || light->ambientscale != 0 || light->diffusescale != 1 || light->specularscale != 1 || light->flags != LIGHTFLAG_REALTIMEMODE)
3261 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 / r_editlights_rtlightssizescale.value, light->color[0] / r_editlights_rtlightscolorscale.value, light->color[1] / r_editlights_rtlightscolorscale.value, light->color[2] / r_editlights_rtlightscolorscale.value, 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);
3262 else if (light->cubemapname[0] || light->corona || light->angles[0] || light->angles[1] || light->angles[2])
3263 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 / r_editlights_rtlightssizescale.value, light->color[0] / r_editlights_rtlightscolorscale.value, light->color[1] / r_editlights_rtlightscolorscale.value, light->color[2] / r_editlights_rtlightscolorscale.value, light->style, light->cubemapname, light->corona, light->angles[0], light->angles[1], light->angles[2]);
3265 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 / r_editlights_rtlightssizescale.value, light->color[0] / r_editlights_rtlightscolorscale.value, light->color[1] / r_editlights_rtlightscolorscale.value, light->color[2] / r_editlights_rtlightscolorscale.value, light->style);
3266 if (bufchars + (int) strlen(line) > bufmaxchars)
3268 bufmaxchars = bufchars + strlen(line) + 2048;
3270 buf = Mem_Alloc(r_shadow_mempool, bufmaxchars);
3274 memcpy(buf, oldbuf, bufchars);
3280 memcpy(buf + bufchars, line, strlen(line));
3281 bufchars += strlen(line);
3285 FS_WriteFile(name, buf, bufchars);
3290 void R_Shadow_LoadLightsFile(void)
3293 char tempchar, *lightsstring, *s, *t, name[MAX_QPATH];
3294 float origin[3], radius, color[3], subtract, spotdir[3], spotcone, falloff, distbias;
3295 if (r_refdef.worldmodel == NULL)
3297 Con_Print("No map loaded.\n");
3300 FS_StripExtension (r_refdef.worldmodel->name, name, sizeof (name));
3301 strlcat (name, ".lights", sizeof (name));
3302 lightsstring = FS_LoadFile(name, tempmempool, false);
3310 while (*s && *s != '\n' && *s != '\r')
3316 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);
3320 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);
3323 radius = sqrt(DotProduct(color, color) / (falloff * falloff * 8192.0f * 8192.0f));
3324 radius = bound(15, radius, 4096);
3325 VectorScale(color, (2.0f / (8388608.0f)), color);
3326 R_Shadow_UpdateWorldLight(R_Shadow_NewWorldLight(), origin, vec3_origin, color, radius, 0, style, true, NULL, 0.25, 0, 1, 1, LIGHTFLAG_REALTIMEMODE);
3334 Con_Printf("invalid lights file \"%s\"\n", name);
3335 Mem_Free(lightsstring);
3339 // tyrlite/hmap2 light types in the delay field
3340 typedef enum lighttype_e {LIGHTTYPE_MINUSX, LIGHTTYPE_RECIPX, LIGHTTYPE_RECIPXX, LIGHTTYPE_NONE, LIGHTTYPE_SUN, LIGHTTYPE_MINUSXX} lighttype_t;
3342 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void)
3344 int entnum, style, islight, skin, pflags, effects, type, n;
3347 float origin[3], angles[3], radius, color[3], light[4], fadescale, lightscale, originhack[3], overridecolor[3], vec[4];
3348 char key[256], value[1024];
3350 if (r_refdef.worldmodel == NULL)
3352 Con_Print("No map loaded.\n");
3355 // try to load a .ent file first
3356 FS_StripExtension (r_refdef.worldmodel->name, key, sizeof (key));
3357 strlcat (key, ".ent", sizeof (key));
3358 data = entfiledata = FS_LoadFile(key, tempmempool, true);
3359 // and if that is not found, fall back to the bsp file entity string
3361 data = r_refdef.worldmodel->brush.entities;
3364 for (entnum = 0;COM_ParseToken(&data, false) && com_token[0] == '{';entnum++)
3366 type = LIGHTTYPE_MINUSX;
3367 origin[0] = origin[1] = origin[2] = 0;
3368 originhack[0] = originhack[1] = originhack[2] = 0;
3369 angles[0] = angles[1] = angles[2] = 0;
3370 color[0] = color[1] = color[2] = 1;
3371 light[0] = light[1] = light[2] = 1;light[3] = 300;
3372 overridecolor[0] = overridecolor[1] = overridecolor[2] = 1;
3382 if (!COM_ParseToken(&data, false))
3384 if (com_token[0] == '}')
3385 break; // end of entity
3386 if (com_token[0] == '_')
3387 strcpy(key, com_token + 1);
3389 strcpy(key, com_token);
3390 while (key[strlen(key)-1] == ' ') // remove trailing spaces
3391 key[strlen(key)-1] = 0;
3392 if (!COM_ParseToken(&data, false))
3394 strcpy(value, com_token);
3396 // now that we have the key pair worked out...
3397 if (!strcmp("light", key))
3399 n = sscanf(value, "%f %f %f %f", &vec[0], &vec[1], &vec[2], &vec[3]);
3403 light[0] = vec[0] * (1.0f / 256.0f);
3404 light[1] = vec[0] * (1.0f / 256.0f);
3405 light[2] = vec[0] * (1.0f / 256.0f);
3411 light[0] = vec[0] * (1.0f / 255.0f);
3412 light[1] = vec[1] * (1.0f / 255.0f);
3413 light[2] = vec[2] * (1.0f / 255.0f);
3417 else if (!strcmp("delay", key))
3419 else if (!strcmp("origin", key))
3420 sscanf(value, "%f %f %f", &origin[0], &origin[1], &origin[2]);
3421 else if (!strcmp("angle", key))
3422 angles[0] = 0, angles[1] = atof(value), angles[2] = 0;
3423 else if (!strcmp("angles", key))
3424 sscanf(value, "%f %f %f", &angles[0], &angles[1], &angles[2]);
3425 else if (!strcmp("color", key))
3426 sscanf(value, "%f %f %f", &color[0], &color[1], &color[2]);
3427 else if (!strcmp("wait", key))
3428 fadescale = atof(value);
3429 else if (!strcmp("classname", key))
3431 if (!strncmp(value, "light", 5))
3434 if (!strcmp(value, "light_fluoro"))
3439 overridecolor[0] = 1;
3440 overridecolor[1] = 1;
3441 overridecolor[2] = 1;
3443 if (!strcmp(value, "light_fluorospark"))
3448 overridecolor[0] = 1;
3449 overridecolor[1] = 1;
3450 overridecolor[2] = 1;
3452 if (!strcmp(value, "light_globe"))
3457 overridecolor[0] = 1;
3458 overridecolor[1] = 0.8;
3459 overridecolor[2] = 0.4;
3461 if (!strcmp(value, "light_flame_large_yellow"))
3466 overridecolor[0] = 1;
3467 overridecolor[1] = 0.5;
3468 overridecolor[2] = 0.1;
3470 if (!strcmp(value, "light_flame_small_yellow"))
3475 overridecolor[0] = 1;
3476 overridecolor[1] = 0.5;
3477 overridecolor[2] = 0.1;
3479 if (!strcmp(value, "light_torch_small_white"))
3484 overridecolor[0] = 1;
3485 overridecolor[1] = 0.5;
3486 overridecolor[2] = 0.1;
3488 if (!strcmp(value, "light_torch_small_walltorch"))
3493 overridecolor[0] = 1;
3494 overridecolor[1] = 0.5;
3495 overridecolor[2] = 0.1;
3499 else if (!strcmp("style", key))
3500 style = atoi(value);
3501 else if (r_refdef.worldmodel->type == mod_brushq3)
3503 if (!strcmp("scale", key))
3504 lightscale = atof(value);
3505 if (!strcmp("fade", key))
3506 fadescale = atof(value);
3508 else if (!strcmp("skin", key))
3509 skin = (int)atof(value);
3510 else if (!strcmp("pflags", key))
3511 pflags = (int)atof(value);
3512 else if (!strcmp("effects", key))
3513 effects = (int)atof(value);
3517 if (lightscale <= 0)
3521 if (color[0] == color[1] && color[0] == color[2])
3523 color[0] *= overridecolor[0];
3524 color[1] *= overridecolor[1];
3525 color[2] *= overridecolor[2];
3527 radius = light[3] * r_editlights_quakelightsizescale.value * lightscale / fadescale;
3528 color[0] = color[0] * light[0];
3529 color[1] = color[1] * light[1];
3530 color[2] = color[2] * light[2];
3533 case LIGHTTYPE_MINUSX:
3535 case LIGHTTYPE_RECIPX:
3537 VectorScale(color, (1.0f / 16.0f), color);
3539 case LIGHTTYPE_RECIPXX:
3541 VectorScale(color, (1.0f / 16.0f), color);
3544 case LIGHTTYPE_NONE:
3548 case LIGHTTYPE_MINUSXX:
3551 VectorAdd(origin, originhack, origin);
3553 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);
3556 Mem_Free(entfiledata);
3560 void R_Shadow_SetCursorLocationForView(void)
3562 vec_t dist, push, frac;
3563 vec3_t dest, endpos, normal;
3564 VectorMA(r_vieworigin, r_editlights_cursordistance.value, r_viewforward, dest);
3565 frac = CL_TraceLine(r_vieworigin, dest, endpos, normal, true, NULL, SUPERCONTENTS_SOLID);
3568 dist = frac * r_editlights_cursordistance.value;
3569 push = r_editlights_cursorpushback.value;
3573 VectorMA(endpos, push, r_viewforward, endpos);
3574 VectorMA(endpos, r_editlights_cursorpushoff.value, normal, endpos);
3576 r_editlights_cursorlocation[0] = floor(endpos[0] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
3577 r_editlights_cursorlocation[1] = floor(endpos[1] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
3578 r_editlights_cursorlocation[2] = floor(endpos[2] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
3581 void R_Shadow_UpdateWorldLightSelection(void)
3583 if (r_editlights.integer)
3585 R_Shadow_SetCursorLocationForView();
3586 R_Shadow_SelectLightInView();
3587 R_Shadow_DrawLightSprites();
3590 R_Shadow_SelectLight(NULL);
3593 void R_Shadow_EditLights_Clear_f(void)
3595 R_Shadow_ClearWorldLights();
3598 void R_Shadow_EditLights_Reload_f(void)
3600 if (!r_refdef.worldmodel)
3602 strlcpy(r_shadow_mapname, r_refdef.worldmodel->name, sizeof(r_shadow_mapname));
3603 R_Shadow_ClearWorldLights();
3604 R_Shadow_LoadWorldLights();
3605 if (r_shadow_worldlightchain == NULL)
3607 R_Shadow_LoadLightsFile();
3608 if (r_shadow_worldlightchain == NULL)
3609 R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
3613 void R_Shadow_EditLights_Save_f(void)
3615 if (!r_refdef.worldmodel)
3617 R_Shadow_SaveWorldLights();
3620 void R_Shadow_EditLights_ImportLightEntitiesFromMap_f(void)
3622 R_Shadow_ClearWorldLights();
3623 R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
3626 void R_Shadow_EditLights_ImportLightsFile_f(void)
3628 R_Shadow_ClearWorldLights();
3629 R_Shadow_LoadLightsFile();
3632 void R_Shadow_EditLights_Spawn_f(void)
3635 if (!r_editlights.integer)
3637 Con_Print("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
3640 if (Cmd_Argc() != 1)
3642 Con_Print("r_editlights_spawn does not take parameters\n");
3645 color[0] = color[1] = color[2] = 1;
3646 R_Shadow_UpdateWorldLight(R_Shadow_NewWorldLight(), r_editlights_cursorlocation, vec3_origin, color, 200, 0, 0, true, NULL, 0.25, 0, 1, 1, LIGHTFLAG_REALTIMEMODE);
3649 void R_Shadow_EditLights_Edit_f(void)
3651 vec3_t origin, angles, color;
3652 vec_t radius, corona, coronasizescale, ambientscale, diffusescale, specularscale;
3653 int style, shadows, flags, normalmode, realtimemode;
3654 char cubemapname[1024];
3655 if (!r_editlights.integer)
3657 Con_Print("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
3660 if (!r_shadow_selectedlight)
3662 Con_Print("No selected light.\n");
3665 VectorCopy(r_shadow_selectedlight->origin, origin);
3666 VectorCopy(r_shadow_selectedlight->angles, angles);
3667 VectorCopy(r_shadow_selectedlight->color, color);
3668 radius = r_shadow_selectedlight->radius;
3669 style = r_shadow_selectedlight->style;
3670 if (r_shadow_selectedlight->cubemapname)
3671 strcpy(cubemapname, r_shadow_selectedlight->cubemapname);
3674 shadows = r_shadow_selectedlight->shadow;
3675 corona = r_shadow_selectedlight->corona;
3676 coronasizescale = r_shadow_selectedlight->coronasizescale;
3677 ambientscale = r_shadow_selectedlight->ambientscale;
3678 diffusescale = r_shadow_selectedlight->diffusescale;
3679 specularscale = r_shadow_selectedlight->specularscale;
3680 flags = r_shadow_selectedlight->flags;
3681 normalmode = (flags & LIGHTFLAG_NORMALMODE) != 0;
3682 realtimemode = (flags & LIGHTFLAG_REALTIMEMODE) != 0;
3683 if (!strcmp(Cmd_Argv(1), "origin"))
3685 if (Cmd_Argc() != 5)
3687 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(1));
3690 origin[0] = atof(Cmd_Argv(2));
3691 origin[1] = atof(Cmd_Argv(3));
3692 origin[2] = atof(Cmd_Argv(4));
3694 else if (!strcmp(Cmd_Argv(1), "originx"))
3696 if (Cmd_Argc() != 3)
3698 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3701 origin[0] = atof(Cmd_Argv(2));
3703 else if (!strcmp(Cmd_Argv(1), "originy"))
3705 if (Cmd_Argc() != 3)
3707 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3710 origin[1] = atof(Cmd_Argv(2));
3712 else if (!strcmp(Cmd_Argv(1), "originz"))
3714 if (Cmd_Argc() != 3)
3716 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3719 origin[2] = atof(Cmd_Argv(2));
3721 else if (!strcmp(Cmd_Argv(1), "move"))
3723 if (Cmd_Argc() != 5)
3725 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(1));
3728 origin[0] += atof(Cmd_Argv(2));
3729 origin[1] += atof(Cmd_Argv(3));
3730 origin[2] += atof(Cmd_Argv(4));
3732 else if (!strcmp(Cmd_Argv(1), "movex"))
3734 if (Cmd_Argc() != 3)
3736 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3739 origin[0] += atof(Cmd_Argv(2));
3741 else if (!strcmp(Cmd_Argv(1), "movey"))
3743 if (Cmd_Argc() != 3)
3745 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3748 origin[1] += atof(Cmd_Argv(2));
3750 else if (!strcmp(Cmd_Argv(1), "movez"))
3752 if (Cmd_Argc() != 3)
3754 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3757 origin[2] += atof(Cmd_Argv(2));
3759 else if (!strcmp(Cmd_Argv(1), "angles"))
3761 if (Cmd_Argc() != 5)
3763 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(1));
3766 angles[0] = atof(Cmd_Argv(2));
3767 angles[1] = atof(Cmd_Argv(3));
3768 angles[2] = atof(Cmd_Argv(4));
3770 else if (!strcmp(Cmd_Argv(1), "anglesx"))
3772 if (Cmd_Argc() != 3)
3774 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3777 angles[0] = atof(Cmd_Argv(2));
3779 else if (!strcmp(Cmd_Argv(1), "anglesy"))
3781 if (Cmd_Argc() != 3)
3783 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3786 angles[1] = atof(Cmd_Argv(2));
3788 else if (!strcmp(Cmd_Argv(1), "anglesz"))
3790 if (Cmd_Argc() != 3)
3792 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3795 angles[2] = atof(Cmd_Argv(2));
3797 else if (!strcmp(Cmd_Argv(1), "color"))
3799 if (Cmd_Argc() != 5)
3801 Con_Printf("usage: r_editlights_edit %s red green blue\n", Cmd_Argv(1));
3804 color[0] = atof(Cmd_Argv(2));
3805 color[1] = atof(Cmd_Argv(3));
3806 color[2] = atof(Cmd_Argv(4));
3808 else if (!strcmp(Cmd_Argv(1), "radius"))
3810 if (Cmd_Argc() != 3)
3812 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3815 radius = atof(Cmd_Argv(2));
3817 else if (!strcmp(Cmd_Argv(1), "style"))
3819 if (Cmd_Argc() != 3)
3821 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3824 style = atoi(Cmd_Argv(2));
3826 else if (!strcmp(Cmd_Argv(1), "cubemap"))
3830 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3833 if (Cmd_Argc() == 3)
3834 strcpy(cubemapname, Cmd_Argv(2));
3838 else if (!strcmp(Cmd_Argv(1), "shadows"))
3840 if (Cmd_Argc() != 3)
3842 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3845 shadows = Cmd_Argv(2)[0] == 'y' || Cmd_Argv(2)[0] == 'Y' || Cmd_Argv(2)[0] == 't' || atoi(Cmd_Argv(2));
3847 else if (!strcmp(Cmd_Argv(1), "corona"))
3849 if (Cmd_Argc() != 3)
3851 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3854 corona = atof(Cmd_Argv(2));
3856 else if (!strcmp(Cmd_Argv(1), "coronasize"))
3858 if (Cmd_Argc() != 3)
3860 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3863 coronasizescale = atof(Cmd_Argv(2));
3865 else if (!strcmp(Cmd_Argv(1), "ambient"))
3867 if (Cmd_Argc() != 3)
3869 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3872 ambientscale = atof(Cmd_Argv(2));
3874 else if (!strcmp(Cmd_Argv(1), "diffuse"))
3876 if (Cmd_Argc() != 3)
3878 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3881 diffusescale = atof(Cmd_Argv(2));
3883 else if (!strcmp(Cmd_Argv(1), "specular"))
3885 if (Cmd_Argc() != 3)
3887 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3890 specularscale = atof(Cmd_Argv(2));
3892 else if (!strcmp(Cmd_Argv(1), "normalmode"))
3894 if (Cmd_Argc() != 3)
3896 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3899 normalmode = Cmd_Argv(2)[0] == 'y' || Cmd_Argv(2)[0] == 'Y' || Cmd_Argv(2)[0] == 't' || atoi(Cmd_Argv(2));
3901 else if (!strcmp(Cmd_Argv(1), "realtimemode"))
3903 if (Cmd_Argc() != 3)
3905 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
3908 realtimemode = Cmd_Argv(2)[0] == 'y' || Cmd_Argv(2)[0] == 'Y' || Cmd_Argv(2)[0] == 't' || atoi(Cmd_Argv(2));
3912 Con_Print("usage: r_editlights_edit [property] [value]\n");
3913 Con_Print("Selected light's properties:\n");
3914 Con_Printf("Origin : %f %f %f\n", r_shadow_selectedlight->origin[0], r_shadow_selectedlight->origin[1], r_shadow_selectedlight->origin[2]);
3915 Con_Printf("Angles : %f %f %f\n", r_shadow_selectedlight->angles[0], r_shadow_selectedlight->angles[1], r_shadow_selectedlight->angles[2]);
3916 Con_Printf("Color : %f %f %f\n", r_shadow_selectedlight->color[0], r_shadow_selectedlight->color[1], r_shadow_selectedlight->color[2]);
3917 Con_Printf("Radius : %f\n", r_shadow_selectedlight->radius);
3918 Con_Printf("Corona : %f\n", r_shadow_selectedlight->corona);
3919 Con_Printf("Style : %i\n", r_shadow_selectedlight->style);
3920 Con_Printf("Shadows : %s\n", r_shadow_selectedlight->shadow ? "yes" : "no");
3921 Con_Printf("Cubemap : %s\n", r_shadow_selectedlight->cubemapname);
3922 Con_Printf("CoronaSize : %f\n", r_shadow_selectedlight->coronasizescale);
3923 Con_Printf("Ambient : %f\n", r_shadow_selectedlight->ambientscale);
3924 Con_Printf("Diffuse : %f\n", r_shadow_selectedlight->diffusescale);
3925 Con_Printf("Specular : %f\n", r_shadow_selectedlight->specularscale);
3926 Con_Printf("NormalMode : %s\n", (r_shadow_selectedlight->flags & LIGHTFLAG_NORMALMODE) ? "yes" : "no");
3927 Con_Printf("RealTimeMode : %s\n", (r_shadow_selectedlight->flags & LIGHTFLAG_REALTIMEMODE) ? "yes" : "no");
3930 flags = (normalmode ? LIGHTFLAG_NORMALMODE : 0) | (realtimemode ? LIGHTFLAG_REALTIMEMODE : 0);
3931 R_Shadow_UpdateWorldLight(r_shadow_selectedlight, origin, angles, color, radius, corona, style, shadows, cubemapname, coronasizescale, ambientscale, diffusescale, specularscale, flags);
3934 void R_Shadow_EditLights_EditAll_f(void)
3938 if (!r_editlights.integer)
3940 Con_Print("Cannot edit lights when not in editing mode. Set r_editlights to 1.\n");
3944 for (light = r_shadow_worldlightchain;light;light = light->next)
3946 R_Shadow_SelectLight(light);
3947 R_Shadow_EditLights_Edit_f();
3951 void R_Shadow_EditLights_DrawSelectedLightProperties(void)
3953 int lightnumber, lightcount;
3957 if (!r_editlights.integer)
3963 for (lightcount = 0, light = r_shadow_worldlightchain;light;lightcount++, light = light->next)
3964 if (light == r_shadow_selectedlight)
3965 lightnumber = lightcount;
3966 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;
3967 if (r_shadow_selectedlight == NULL)
3969 sprintf(temp, "Light #%i properties", lightnumber);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
3970 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;
3971 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;
3972 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;
3973 sprintf(temp, "Radius : %f\n", r_shadow_selectedlight->radius);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
3974 sprintf(temp, "Corona : %f\n", r_shadow_selectedlight->corona);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
3975 sprintf(temp, "Style : %i\n", r_shadow_selectedlight->style);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
3976 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;
3977 sprintf(temp, "Cubemap : %s\n", r_shadow_selectedlight->cubemapname);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
3978 sprintf(temp, "CoronaSize : %f\n", r_shadow_selectedlight->coronasizescale);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
3979 sprintf(temp, "Ambient : %f\n", r_shadow_selectedlight->ambientscale);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
3980 sprintf(temp, "Diffuse : %f\n", r_shadow_selectedlight->diffusescale);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
3981 sprintf(temp, "Specular : %f\n", r_shadow_selectedlight->specularscale);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
3982 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;
3983 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;
3986 void R_Shadow_EditLights_ToggleShadow_f(void)
3988 if (!r_editlights.integer)
3990 Con_Print("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
3993 if (!r_shadow_selectedlight)
3995 Con_Print("No selected light.\n");
3998 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);
4001 void R_Shadow_EditLights_ToggleCorona_f(void)
4003 if (!r_editlights.integer)
4005 Con_Print("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
4008 if (!r_shadow_selectedlight)
4010 Con_Print("No selected light.\n");
4013 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);
4016 void R_Shadow_EditLights_Remove_f(void)
4018 if (!r_editlights.integer)
4020 Con_Print("Cannot remove light when not in editing mode. Set r_editlights to 1.\n");
4023 if (!r_shadow_selectedlight)
4025 Con_Print("No selected light.\n");
4028 R_Shadow_FreeWorldLight(r_shadow_selectedlight);
4029 r_shadow_selectedlight = NULL;
4032 void R_Shadow_EditLights_Help_f(void)
4035 "Documentation on r_editlights system:\n"
4037 "r_editlights : enable/disable editing mode\n"
4038 "r_editlights_cursordistance : maximum distance of cursor from eye\n"
4039 "r_editlights_cursorpushback : push back cursor this far from surface\n"
4040 "r_editlights_cursorpushoff : push cursor off surface this far\n"
4041 "r_editlights_cursorgrid : snap cursor to grid of this size\n"
4042 "r_editlights_quakelightsizescale : imported quake light entity size scaling\n"
4043 "r_editlights_rtlightssizescale : imported rtlight size scaling\n"
4044 "r_editlights_rtlightscolorscale : imported rtlight color scaling\n"
4046 "r_editlights_help : this help\n"
4047 "r_editlights_clear : remove all lights\n"
4048 "r_editlights_reload : reload .rtlights, .lights file, or entities\n"
4049 "r_editlights_save : save to .rtlights file\n"
4050 "r_editlights_spawn : create a light with default settings\n"
4051 "r_editlights_edit command : edit selected light - more documentation below\n"
4052 "r_editlights_remove : remove selected light\n"
4053 "r_editlights_toggleshadow : toggles on/off selected light's shadow property\n"
4054 "r_editlights_importlightentitiesfrommap : reload light entities\n"
4055 "r_editlights_importlightsfile : reload .light file (produced by hlight)\n"
4057 "origin x y z : set light location\n"
4058 "originx x: set x component of light location\n"
4059 "originy y: set y component of light location\n"
4060 "originz z: set z component of light location\n"
4061 "move x y z : adjust light location\n"
4062 "movex x: adjust x component of light location\n"
4063 "movey y: adjust y component of light location\n"
4064 "movez z: adjust z component of light location\n"
4065 "angles x y z : set light angles\n"
4066 "anglesx x: set x component of light angles\n"
4067 "anglesy y: set y component of light angles\n"
4068 "anglesz z: set z component of light angles\n"
4069 "color r g b : set color of light (can be brighter than 1 1 1)\n"
4070 "radius radius : set radius (size) of light\n"
4071 "style style : set lightstyle of light (flickering patterns, switches, etc)\n"
4072 "cubemap basename : set filter cubemap of light (not yet supported)\n"
4073 "shadows 1/0 : turn on/off shadows\n"
4074 "corona n : set corona intensity\n"
4075 "coronasize n : set corona size (0-1)\n"
4076 "ambient n : set ambient intensity (0-1)\n"
4077 "diffuse n : set diffuse intensity (0-1)\n"
4078 "specular n : set specular intensity (0-1)\n"
4079 "normalmode 1/0 : turn on/off rendering of this light in rtworld 0 mode\n"
4080 "realtimemode 1/0 : turn on/off rendering of this light in rtworld 1 mode\n"
4081 "<nothing> : print light properties to console\n"
4085 void R_Shadow_EditLights_CopyInfo_f(void)
4087 if (!r_editlights.integer)
4089 Con_Print("Cannot copy light info when not in editing mode. Set r_editlights to 1.\n");
4092 if (!r_shadow_selectedlight)
4094 Con_Print("No selected light.\n");
4097 VectorCopy(r_shadow_selectedlight->angles, r_shadow_bufferlight.angles);
4098 VectorCopy(r_shadow_selectedlight->color, r_shadow_bufferlight.color);
4099 r_shadow_bufferlight.radius = r_shadow_selectedlight->radius;
4100 r_shadow_bufferlight.style = r_shadow_selectedlight->style;
4101 if (r_shadow_selectedlight->cubemapname)
4102 strcpy(r_shadow_bufferlight.cubemapname, r_shadow_selectedlight->cubemapname);
4104 r_shadow_bufferlight.cubemapname[0] = 0;
4105 r_shadow_bufferlight.shadow = r_shadow_selectedlight->shadow;
4106 r_shadow_bufferlight.corona = r_shadow_selectedlight->corona;
4107 r_shadow_bufferlight.coronasizescale = r_shadow_selectedlight->coronasizescale;
4108 r_shadow_bufferlight.ambientscale = r_shadow_selectedlight->ambientscale;
4109 r_shadow_bufferlight.diffusescale = r_shadow_selectedlight->diffusescale;
4110 r_shadow_bufferlight.specularscale = r_shadow_selectedlight->specularscale;
4111 r_shadow_bufferlight.flags = r_shadow_selectedlight->flags;
4114 void R_Shadow_EditLights_PasteInfo_f(void)
4116 if (!r_editlights.integer)
4118 Con_Print("Cannot paste light info when not in editing mode. Set r_editlights to 1.\n");
4121 if (!r_shadow_selectedlight)
4123 Con_Print("No selected light.\n");
4126 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);
4129 void R_Shadow_EditLights_Init(void)
4131 Cvar_RegisterVariable(&r_editlights);
4132 Cvar_RegisterVariable(&r_editlights_cursordistance);
4133 Cvar_RegisterVariable(&r_editlights_cursorpushback);
4134 Cvar_RegisterVariable(&r_editlights_cursorpushoff);
4135 Cvar_RegisterVariable(&r_editlights_cursorgrid);
4136 Cvar_RegisterVariable(&r_editlights_quakelightsizescale);
4137 Cvar_RegisterVariable(&r_editlights_rtlightssizescale);
4138 Cvar_RegisterVariable(&r_editlights_rtlightscolorscale);
4139 Cmd_AddCommand("r_editlights_help", R_Shadow_EditLights_Help_f);
4140 Cmd_AddCommand("r_editlights_clear", R_Shadow_EditLights_Clear_f);
4141 Cmd_AddCommand("r_editlights_reload", R_Shadow_EditLights_Reload_f);
4142 Cmd_AddCommand("r_editlights_save", R_Shadow_EditLights_Save_f);
4143 Cmd_AddCommand("r_editlights_spawn", R_Shadow_EditLights_Spawn_f);
4144 Cmd_AddCommand("r_editlights_edit", R_Shadow_EditLights_Edit_f);
4145 Cmd_AddCommand("r_editlights_editall", R_Shadow_EditLights_EditAll_f);
4146 Cmd_AddCommand("r_editlights_remove", R_Shadow_EditLights_Remove_f);
4147 Cmd_AddCommand("r_editlights_toggleshadow", R_Shadow_EditLights_ToggleShadow_f);
4148 Cmd_AddCommand("r_editlights_togglecorona", R_Shadow_EditLights_ToggleCorona_f);
4149 Cmd_AddCommand("r_editlights_importlightentitiesfrommap", R_Shadow_EditLights_ImportLightEntitiesFromMap_f);
4150 Cmd_AddCommand("r_editlights_importlightsfile", R_Shadow_EditLights_ImportLightsFile_f);
4151 Cmd_AddCommand("r_editlights_copyinfo", R_Shadow_EditLights_CopyInfo_f);
4152 Cmd_AddCommand("r_editlights_pasteinfo", R_Shadow_EditLights_PasteInfo_f);