X-Git-Url: http://git.xonotic.org/?a=blobdiff_plain;f=r_shadow.c;h=44fd8f2935fa0e498771014f924f39d02ce44b95;hb=792cfa2c9cb08bf5f620214e1419d2d920969acb;hp=d300d50fcf32ff4118acbce1f01ea3f469c02904;hpb=2af7bf1a4eb9a5c9a1cfe76f9f8690523bafbd3e;p=xonotic%2Fdarkplaces.git diff --git a/r_shadow.c b/r_shadow.c index d300d50f..44fd8f29 100644 --- a/r_shadow.c +++ b/r_shadow.c @@ -314,18 +314,18 @@ cvar_t r_shadow_realtime_world_compileshadow = {0, "r_shadow_realtime_world_comp cvar_t r_shadow_realtime_world_compilesvbsp = {0, "r_shadow_realtime_world_compilesvbsp", "1", "enables svbsp optimization during compilation (slower than compileportalculling but more exact)"}; cvar_t r_shadow_realtime_world_compileportalculling = {0, "r_shadow_realtime_world_compileportalculling", "1", "enables portal-based culling optimization during compilation (overrides compilesvbsp)"}; cvar_t r_shadow_scissor = {0, "r_shadow_scissor", "1", "use scissor optimization of light rendering (restricts rendering to the portion of the screen affected by the light)"}; -cvar_t r_shadow_shadowmapping = {CVAR_SAVE, "r_shadow_shadowmapping", "1", "enables use of shadowmapping (depth texture sampling) instead of stencil shadow volumes, requires gl_fbo 1"}; +cvar_t r_shadow_shadowmapping = {CVAR_SAVE, "r_shadow_shadowmapping", "1", "enables use of shadowmapping (depth texture sampling) instead of stencil shadow volumes"}; cvar_t r_shadow_shadowmapping_filterquality = {CVAR_SAVE, "r_shadow_shadowmapping_filterquality", "-1", "shadowmap filter modes: -1 = auto-select, 0 = no filtering, 1 = bilinear, 2 = bilinear 2x2 blur (fast), 3 = 3x3 blur (moderate), 4 = 4x4 blur (slow)"}; cvar_t r_shadow_shadowmapping_useshadowsampler = {CVAR_SAVE, "r_shadow_shadowmapping_useshadowsampler", "1", "whether to use sampler2DShadow if available"}; cvar_t r_shadow_shadowmapping_depthbits = {CVAR_SAVE, "r_shadow_shadowmapping_depthbits", "24", "requested minimum shadowmap texture depth bits"}; cvar_t r_shadow_shadowmapping_vsdct = {CVAR_SAVE, "r_shadow_shadowmapping_vsdct", "1", "enables use of virtual shadow depth cube texture"}; cvar_t r_shadow_shadowmapping_minsize = {CVAR_SAVE, "r_shadow_shadowmapping_minsize", "32", "limit of shadowmap side size - must be at least r_shadow_shadowmapping_bordersize+2"}; cvar_t r_shadow_shadowmapping_maxsize = {CVAR_SAVE, "r_shadow_shadowmapping_maxsize", "512", "limit of shadowmap side size - can not be more than 1/8th of atlassize because lights store 6 sides (2x3 grid) and sometimes 12 sides (4x3 grid for shadows from EF_NOSELFSHADOW entities) and there are multiple lights..."}; -cvar_t r_shadow_shadowmapping_texturesize = { CVAR_SAVE, "r_shadow_shadowmapping_texturesize", "4096", "size of shadowmap atlas texture - all shadowmaps are packed into this texture at frame start"}; +cvar_t r_shadow_shadowmapping_texturesize = { CVAR_SAVE, "r_shadow_shadowmapping_texturesize", "8192", "size of shadowmap atlas texture - all shadowmaps are packed into this texture at frame start"}; cvar_t r_shadow_shadowmapping_precision = {CVAR_SAVE, "r_shadow_shadowmapping_precision", "1", "makes shadowmaps have a maximum resolution of this number of pixels per light source radius unit such that, for example, at precision 0.5 a light with radius 200 will have a maximum resolution of 100 pixels"}; //cvar_t r_shadow_shadowmapping_lod_bias = {CVAR_SAVE, "r_shadow_shadowmapping_lod_bias", "16", "shadowmap size bias"}; //cvar_t r_shadow_shadowmapping_lod_scale = {CVAR_SAVE, "r_shadow_shadowmapping_lod_scale", "128", "shadowmap size scaling parameter"}; -cvar_t r_shadow_shadowmapping_bordersize = {CVAR_SAVE, "r_shadow_shadowmapping_bordersize", "4", "shadowmap size bias for filtering"}; +cvar_t r_shadow_shadowmapping_bordersize = {CVAR_SAVE, "r_shadow_shadowmapping_bordersize", "5", "shadowmap size bias for filtering"}; cvar_t r_shadow_shadowmapping_nearclip = {CVAR_SAVE, "r_shadow_shadowmapping_nearclip", "1", "shadowmap nearclip in world units"}; cvar_t r_shadow_shadowmapping_bias = {CVAR_SAVE, "r_shadow_shadowmapping_bias", "0.03", "shadowmap bias parameter (this is multiplied by nearclip * 1024 / lodsize)"}; cvar_t r_shadow_shadowmapping_polygonfactor = {CVAR_SAVE, "r_shadow_shadowmapping_polygonfactor", "2", "slope-dependent shadowmapping bias"}; @@ -334,20 +334,26 @@ cvar_t r_shadow_sortsurfaces = {0, "r_shadow_sortsurfaces", "1", "improve perfor cvar_t r_shadow_polygonfactor = {0, "r_shadow_polygonfactor", "0", "how much to enlarge shadow volume polygons when rendering (should be 0!)"}; cvar_t r_shadow_polygonoffset = {0, "r_shadow_polygonoffset", "1", "how much to push shadow volumes into the distance when rendering, to reduce chances of zfighting artifacts (should not be less than 0)"}; cvar_t r_shadow_texture3d = {0, "r_shadow_texture3d", "1", "use 3D voxel textures for spherical attenuation rather than cylindrical (does not affect OpenGL 2.0 render path)"}; +cvar_t r_shadow_culllights_pvs = {CVAR_SAVE, "r_shadow_culllights_pvs", "1", "check if light overlaps any visible bsp leafs when determining if the light is visible"}; +cvar_t r_shadow_culllights_trace = {CVAR_SAVE, "r_shadow_culllights_trace", "1", "use raytraces from the eye to random places within light bounds to determine if the light is visible"}; +cvar_t r_shadow_culllights_trace_eyejitter = {CVAR_SAVE, "r_shadow_culllights_trace_eyejitter", "16", "offset eye location randomly by this much"}; +cvar_t r_shadow_culllights_trace_enlarge = {CVAR_SAVE, "r_shadow_culllights_trace_enlarge", "0.1", "make light bounds bigger by *1.0+enlarge"}; +cvar_t r_shadow_culllights_trace_samples = {CVAR_SAVE, "r_shadow_culllights_trace_samples", "16", "use this many traces to random positions (in addition to center trace)"}; +cvar_t r_shadow_culllights_trace_tempsamples = {CVAR_SAVE, "r_shadow_culllights_trace_tempsamples", "16", "use this many traces if the light was created by csqc (no inter-frame caching), -1 disables the check (to avoid flicker entirely)"}; +cvar_t r_shadow_culllights_trace_delay = {CVAR_SAVE, "r_shadow_culllights_trace_delay", "1", "light will be considered visible for this many seconds after any trace connects"}; cvar_t r_shadow_bouncegrid = {CVAR_SAVE, "r_shadow_bouncegrid", "0", "perform particle tracing for indirect lighting (Global Illumination / radiosity) using a 3D texture covering the scene, only active on levels with realtime lights active (r_shadow_realtime_world is usually required for these)"}; -cvar_t r_shadow_bouncegrid_blur = {CVAR_SAVE, "r_shadow_bouncegrid_blur", "1", "apply a 1-radius blur on bouncegrid to denoise it and deal with boundary issues with surfaces"}; +cvar_t r_shadow_bouncegrid_blur = {CVAR_SAVE, "r_shadow_bouncegrid_blur", "0", "apply a 1-radius blur on bouncegrid to denoise it and deal with boundary issues with surfaces"}; cvar_t r_shadow_bouncegrid_bounceanglediffuse = {CVAR_SAVE, "r_shadow_bouncegrid_bounceanglediffuse", "0", "use random bounce direction rather than true reflection, makes some corner areas dark"}; -cvar_t r_shadow_bouncegrid_dynamic_bounceminimumintensity = { CVAR_SAVE, "r_shadow_bouncegrid_dynamic_bounceminimumintensity", "0.01", "stop bouncing once intensity drops below this fraction of the original particle color" }; +cvar_t r_shadow_bouncegrid_dynamic_bounceminimumintensity = { CVAR_SAVE, "r_shadow_bouncegrid_dynamic_bounceminimumintensity", "0.05", "stop bouncing once intensity drops below this fraction of the original particle color" }; cvar_t r_shadow_bouncegrid_dynamic_culllightpaths = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_culllightpaths", "1", "skip accumulating light in the bouncegrid texture where the light paths are out of view (dynamic mode only)"}; -cvar_t r_shadow_bouncegrid_dynamic_dlightparticlemultiplier = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_dlightparticlemultiplier", "1", "if set to a high value like 16 this can make dlights look great, but 0 is recommended for performance reasons"}; cvar_t r_shadow_bouncegrid_dynamic_directionalshading = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_directionalshading", "0", "use diffuse shading rather than ambient, 3D texture becomes 8x as many pixels to hold the additional data"}; +cvar_t r_shadow_bouncegrid_dynamic_dlightparticlemultiplier = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_dlightparticlemultiplier", "1", "if set to a high value like 16 this can make dlights look great, but 0 is recommended for performance reasons"}; cvar_t r_shadow_bouncegrid_dynamic_hitmodels = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_hitmodels", "0", "enables hitting character model geometry (SLOW)"}; -cvar_t r_shadow_bouncegrid_dynamic_energyperphoton = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_energyperphoton", "10000", "amount of light that one photon should represent"}; -cvar_t r_shadow_bouncegrid_dynamic_lightradiusscale = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_lightradiusscale", "10", "particles stop at this fraction of light radius (can be more than 1)"}; -cvar_t r_shadow_bouncegrid_dynamic_maxbounce = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_maxbounce", "5", "maximum number of bounces for a particle (minimum is 0)"}; +cvar_t r_shadow_bouncegrid_dynamic_lightradiusscale = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_lightradiusscale", "2", "particles stop at this fraction of light radius (can be more than 1)"}; +cvar_t r_shadow_bouncegrid_dynamic_maxbounce = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_maxbounce", "2", "maximum number of bounces for a particle (minimum is 0)"}; cvar_t r_shadow_bouncegrid_dynamic_maxphotons = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_maxphotons", "25000", "upper bound on photons to shoot per update, divided proportionately between lights - normally the number of photons is calculated by energyperphoton"}; +cvar_t r_shadow_bouncegrid_dynamic_quality = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_quality", "1", "amount of photons that should be fired (this is multiplied by spacing^2 to make it adaptive with spacing changes)"}; cvar_t r_shadow_bouncegrid_dynamic_spacing = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_spacing", "64", "unit size of bouncegrid pixel"}; -cvar_t r_shadow_bouncegrid_dynamic_stablerandom = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_stablerandom", "1", "make particle distribution consistent from frame to frame"}; cvar_t r_shadow_bouncegrid_dynamic_updateinterval = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_updateinterval", "0", "update bouncegrid texture once per this many seconds, useful values are 0, 0.05, or 1000000"}; cvar_t r_shadow_bouncegrid_dynamic_x = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_x", "64", "maximum texture size of bouncegrid on X axis"}; cvar_t r_shadow_bouncegrid_dynamic_y = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_y", "64", "maximum texture size of bouncegrid on Y axis"}; @@ -355,17 +361,21 @@ cvar_t r_shadow_bouncegrid_dynamic_z = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_ cvar_t r_shadow_bouncegrid_floatcolors = {CVAR_SAVE, "r_shadow_bouncegrid_floatcolors", "1", "upload texture as RGBA16F (or RGBA32F when set to 2) rather than RGBA8 format - this gives more dynamic range and accuracy"}; cvar_t r_shadow_bouncegrid_includedirectlighting = {CVAR_SAVE, "r_shadow_bouncegrid_includedirectlighting", "0", "allows direct lighting to be recorded, not just indirect (gives an effect somewhat like r_shadow_realtime_world_lightmaps)"}; cvar_t r_shadow_bouncegrid_intensity = {CVAR_SAVE, "r_shadow_bouncegrid_intensity", "4", "overall brightness of bouncegrid texture"}; +cvar_t r_shadow_bouncegrid_lightpathsize_conespread = {CVAR_SAVE, "r_shadow_bouncegrid_lightpathsize_conespread", "0.015625", "increase lightpathsize over distance at this rate per grid cell"}; +cvar_t r_shadow_bouncegrid_lightpathsize_initial = {CVAR_SAVE, "r_shadow_bouncegrid_lightpathsize_initial", "0.5", "width (in grid cells) of the light path for accumulation of light in the bouncegrid texture"}; +cvar_t r_shadow_bouncegrid_normalizevectors = { CVAR_SAVE, "r_shadow_bouncegrid_normalizevectors", "1", "normalize random vectors (otherwise their length can vary, which dims the lighting further from the light)" }; cvar_t r_shadow_bouncegrid_particlebounceintensity = {CVAR_SAVE, "r_shadow_bouncegrid_particlebounceintensity", "2", "amount of energy carried over after each bounce, this is a multiplier of texture color and the result is clamped to 1 or less, to prevent adding energy on each bounce"}; cvar_t r_shadow_bouncegrid_particleintensity = {CVAR_SAVE, "r_shadow_bouncegrid_particleintensity", "0.25", "brightness of particles contributing to bouncegrid texture"}; +cvar_t r_shadow_bouncegrid_rng_seed = { CVAR_SAVE, "r_shadow_bouncegrid_rng_seed", "0", "0+ = use this number as RNG seed, -1 = use time instead for disco-like craziness in dynamic mode" }; +cvar_t r_shadow_bouncegrid_rng_type = { CVAR_SAVE, "r_shadow_bouncegrid_rng_type", "0", "0 = Lehmer 128bit RNG (slow but high quality), 1 = lhcheeserand 32bit RNG (quick)" }; cvar_t r_shadow_bouncegrid_sortlightpaths = {CVAR_SAVE, "r_shadow_bouncegrid_sortlightpaths", "1", "sort light paths before accumulating them into the bouncegrid texture, this reduces cpu cache misses"}; -cvar_t r_shadow_bouncegrid_lightpathsize = {CVAR_SAVE, "r_shadow_bouncegrid_lightpathsize", "1", "width of the light path for accumulation of light in the bouncegrid texture"}; cvar_t r_shadow_bouncegrid_static = {CVAR_SAVE, "r_shadow_bouncegrid_static", "1", "use static radiosity solution (high quality) rather than dynamic (splotchy)"}; cvar_t r_shadow_bouncegrid_static_bounceminimumintensity = { CVAR_SAVE, "r_shadow_bouncegrid_static_bounceminimumintensity", "0.01", "stop bouncing once intensity drops below this fraction of the original particle color" }; cvar_t r_shadow_bouncegrid_static_directionalshading = {CVAR_SAVE, "r_shadow_bouncegrid_static_directionalshading", "1", "whether to use directionalshading when in static mode"}; -cvar_t r_shadow_bouncegrid_static_energyperphoton = {CVAR_SAVE, "r_shadow_bouncegrid_static_energyperphoton", "10000", "amount of light that one photon should represent in static mode"}; -cvar_t r_shadow_bouncegrid_static_lightradiusscale = {CVAR_SAVE, "r_shadow_bouncegrid_static_lightradiusscale", "10", "particles stop at this fraction of light radius (can be more than 1) when in static mode"}; +cvar_t r_shadow_bouncegrid_static_lightradiusscale = {CVAR_SAVE, "r_shadow_bouncegrid_static_lightradiusscale", "2", "particles stop at this fraction of light radius (can be more than 1) when in static mode"}; cvar_t r_shadow_bouncegrid_static_maxbounce = {CVAR_SAVE, "r_shadow_bouncegrid_static_maxbounce", "5", "maximum number of bounces for a particle (minimum is 0) in static mode"}; cvar_t r_shadow_bouncegrid_static_maxphotons = {CVAR_SAVE, "r_shadow_bouncegrid_static_maxphotons", "250000", "upper bound on photons in static mode"}; +cvar_t r_shadow_bouncegrid_static_quality = { CVAR_SAVE, "r_shadow_bouncegrid_static_quality", "16", "amount of photons that should be fired (this is multiplied by spacing^2 to make it adaptive with spacing changes)" }; cvar_t r_shadow_bouncegrid_static_spacing = {CVAR_SAVE, "r_shadow_bouncegrid_static_spacing", "64", "unit size of bouncegrid pixel when in static mode"}; cvar_t r_coronas = {CVAR_SAVE, "r_coronas", "0", "brightness of corona flare effects around certain lights, 0 disables corona effects"}; cvar_t r_coronas_occlusionsizescale = {CVAR_SAVE, "r_coronas_occlusionsizescale", "0.1", "size of light source for corona occlusion checksum the proportion of hidden pixels controls corona intensity"}; @@ -461,19 +471,19 @@ static void R_Shadow_SetShadowMode(void) { if (!r_fb.usedepthtextures) r_shadow_shadowmappcf = 1; - else if((strstr(gl_vendor, "NVIDIA") || strstr(gl_renderer, "Radeon HD")) && vid.support.arb_shadow && r_shadow_shadowmapshadowsampler) + else if((strstr(gl_vendor, "NVIDIA") || strstr(gl_renderer, "Radeon HD")) && vid.support.arb_shadow && r_shadow_shadowmapshadowsampler) { r_shadow_shadowmapsampler = true; r_shadow_shadowmappcf = 1; } else if(vid.support.amd_texture_texture4 || vid.support.arb_texture_gather) r_shadow_shadowmappcf = 1; - else if((strstr(gl_vendor, "ATI") || strstr(gl_vendor, "Advanced Micro Devices")) && !strstr(gl_renderer, "Mesa") && !strstr(gl_version, "Mesa")) + else if((strstr(gl_vendor, "ATI") || strstr(gl_vendor, "Advanced Micro Devices")) && !strstr(gl_renderer, "Mesa") && !strstr(gl_version, "Mesa")) r_shadow_shadowmappcf = 1; - else + else r_shadow_shadowmapsampler = vid.support.arb_shadow && r_shadow_shadowmapshadowsampler; } - else + else { r_shadow_shadowmapsampler = vid.support.arb_shadow && r_shadow_shadowmapshadowsampler; switch (r_shadow_shadowmapfilterquality) @@ -730,20 +740,22 @@ static void r_shadow_shutdown(void) static void r_shadow_newmap(void) { r_shadow_bouncegrid_state.highpixels = NULL; - if (r_shadow_bouncegrid_state.blurpixels[0]) Mem_Free(r_shadow_bouncegrid_state.blurpixels[0]); r_shadow_bouncegrid_state.blurpixels[0] = NULL; - if (r_shadow_bouncegrid_state.blurpixels[1]) Mem_Free(r_shadow_bouncegrid_state.blurpixels[1]); r_shadow_bouncegrid_state.blurpixels[1] = NULL; - if (r_shadow_bouncegrid_state.u8pixels) Mem_Free(r_shadow_bouncegrid_state.u8pixels); r_shadow_bouncegrid_state.u8pixels = NULL; - if (r_shadow_bouncegrid_state.fp16pixels) Mem_Free(r_shadow_bouncegrid_state.fp16pixels); r_shadow_bouncegrid_state.fp16pixels = NULL; - if (r_shadow_bouncegrid_state.splatpaths) Mem_Free(r_shadow_bouncegrid_state.splatpaths); r_shadow_bouncegrid_state.splatpaths = NULL; + if (r_shadow_bouncegrid_state.blurpixels[0]) { Mem_Free(r_shadow_bouncegrid_state.blurpixels[0]); r_shadow_bouncegrid_state.blurpixels[0] = NULL; } + if (r_shadow_bouncegrid_state.blurpixels[1]) { Mem_Free(r_shadow_bouncegrid_state.blurpixels[1]); r_shadow_bouncegrid_state.blurpixels[1] = NULL; } + if (r_shadow_bouncegrid_state.u8pixels) { Mem_Free(r_shadow_bouncegrid_state.u8pixels); r_shadow_bouncegrid_state.u8pixels = NULL; } + if (r_shadow_bouncegrid_state.fp16pixels) { Mem_Free(r_shadow_bouncegrid_state.fp16pixels); r_shadow_bouncegrid_state.fp16pixels = NULL; } + if (r_shadow_bouncegrid_state.splatpaths) { Mem_Free(r_shadow_bouncegrid_state.splatpaths); r_shadow_bouncegrid_state.splatpaths = NULL; } + r_shadow_bouncegrid_state.maxsplatpaths = 0; - if (r_shadow_bouncegrid_state.texture) R_FreeTexture(r_shadow_bouncegrid_state.texture);r_shadow_bouncegrid_state.texture = NULL; - if (r_shadow_lightcorona) R_SkinFrame_MarkUsed(r_shadow_lightcorona); - if (r_editlights_sprcursor) R_SkinFrame_MarkUsed(r_editlights_sprcursor); - if (r_editlights_sprlight) R_SkinFrame_MarkUsed(r_editlights_sprlight); - if (r_editlights_sprnoshadowlight) R_SkinFrame_MarkUsed(r_editlights_sprnoshadowlight); - if (r_editlights_sprcubemaplight) R_SkinFrame_MarkUsed(r_editlights_sprcubemaplight); - if (r_editlights_sprcubemapnoshadowlight) R_SkinFrame_MarkUsed(r_editlights_sprcubemapnoshadowlight); - if (r_editlights_sprselection) R_SkinFrame_MarkUsed(r_editlights_sprselection); + + if (r_shadow_bouncegrid_state.texture) { R_FreeTexture(r_shadow_bouncegrid_state.texture);r_shadow_bouncegrid_state.texture = NULL; } + if (r_shadow_lightcorona) { R_SkinFrame_MarkUsed(r_shadow_lightcorona); } + if (r_editlights_sprcursor) { R_SkinFrame_MarkUsed(r_editlights_sprcursor); } + if (r_editlights_sprlight) { R_SkinFrame_MarkUsed(r_editlights_sprlight); } + if (r_editlights_sprnoshadowlight) { R_SkinFrame_MarkUsed(r_editlights_sprnoshadowlight); } + if (r_editlights_sprcubemaplight) { R_SkinFrame_MarkUsed(r_editlights_sprcubemaplight); } + if (r_editlights_sprcubemapnoshadowlight) { R_SkinFrame_MarkUsed(r_editlights_sprcubemapnoshadowlight); } + if (r_editlights_sprselection) { R_SkinFrame_MarkUsed(r_editlights_sprselection); } if (strncmp(cl.worldname, r_shadow_mapname, sizeof(r_shadow_mapname))) R_Shadow_EditLights_Reload_f(); } @@ -801,6 +813,13 @@ void R_Shadow_Init(void) Cvar_RegisterVariable(&r_shadow_polygonfactor); Cvar_RegisterVariable(&r_shadow_polygonoffset); Cvar_RegisterVariable(&r_shadow_texture3d); + Cvar_RegisterVariable(&r_shadow_culllights_pvs); + Cvar_RegisterVariable(&r_shadow_culllights_trace); + Cvar_RegisterVariable(&r_shadow_culllights_trace_eyejitter); + Cvar_RegisterVariable(&r_shadow_culllights_trace_enlarge); + Cvar_RegisterVariable(&r_shadow_culllights_trace_samples); + Cvar_RegisterVariable(&r_shadow_culllights_trace_tempsamples); + Cvar_RegisterVariable(&r_shadow_culllights_trace_delay); Cvar_RegisterVariable(&r_shadow_bouncegrid); Cvar_RegisterVariable(&r_shadow_bouncegrid_blur); Cvar_RegisterVariable(&r_shadow_bouncegrid_bounceanglediffuse); @@ -809,12 +828,11 @@ void R_Shadow_Init(void) Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_directionalshading); Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_dlightparticlemultiplier); Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_hitmodels); - Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_energyperphoton); Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_lightradiusscale); Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_maxbounce); Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_maxphotons); + Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_quality); Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_spacing); - Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_stablerandom); Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_updateinterval); Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_x); Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_y); @@ -822,17 +840,21 @@ void R_Shadow_Init(void) Cvar_RegisterVariable(&r_shadow_bouncegrid_floatcolors); Cvar_RegisterVariable(&r_shadow_bouncegrid_includedirectlighting); Cvar_RegisterVariable(&r_shadow_bouncegrid_intensity); - Cvar_RegisterVariable(&r_shadow_bouncegrid_lightpathsize); + Cvar_RegisterVariable(&r_shadow_bouncegrid_lightpathsize_conespread); + Cvar_RegisterVariable(&r_shadow_bouncegrid_lightpathsize_initial); + Cvar_RegisterVariable(&r_shadow_bouncegrid_normalizevectors); Cvar_RegisterVariable(&r_shadow_bouncegrid_particlebounceintensity); Cvar_RegisterVariable(&r_shadow_bouncegrid_particleintensity); + Cvar_RegisterVariable(&r_shadow_bouncegrid_rng_seed); + Cvar_RegisterVariable(&r_shadow_bouncegrid_rng_type); Cvar_RegisterVariable(&r_shadow_bouncegrid_sortlightpaths); Cvar_RegisterVariable(&r_shadow_bouncegrid_static); Cvar_RegisterVariable(&r_shadow_bouncegrid_static_bounceminimumintensity); Cvar_RegisterVariable(&r_shadow_bouncegrid_static_directionalshading); - Cvar_RegisterVariable(&r_shadow_bouncegrid_static_energyperphoton); Cvar_RegisterVariable(&r_shadow_bouncegrid_static_lightradiusscale); Cvar_RegisterVariable(&r_shadow_bouncegrid_static_maxbounce); Cvar_RegisterVariable(&r_shadow_bouncegrid_static_maxphotons); + Cvar_RegisterVariable(&r_shadow_bouncegrid_static_quality); Cvar_RegisterVariable(&r_shadow_bouncegrid_static_spacing); Cvar_RegisterVariable(&r_coronas); Cvar_RegisterVariable(&r_coronas_occlusionsizescale); @@ -1601,7 +1623,7 @@ static int R_Shadow_CullFrustumSides(rtlight_t *rtlight, float size, float borde int sides = 0x3F, masks[6] = { 3<<4, 3<<4, 3<<0, 3<<0, 3<<2, 3<<2 }; float scale = (size - 2*border)/size, len; float bias = border / (float)(size - border), dp, dn, ap, an; - // check if cone enclosing side would cross frustum plane + // check if cone enclosing side would cross frustum plane scale = 2 / (scale*scale + 2); Matrix4x4_OriginFromMatrix(&rtlight->matrix_lighttoworld, o); for (i = 0;i < 5;i++) @@ -2425,11 +2447,13 @@ typedef struct r_shadow_bouncegrid_splatpath_s vec3_t splatcolor; vec3_t splatdir; vec_t splatintensity; + vec_t splatsize_current; + vec_t splatsize_perstep; int remainingsplats; } r_shadow_bouncegrid_splatpath_t; -static void R_Shadow_BounceGrid_AddSplatPath(vec3_t originalstart, vec3_t originalend, vec3_t color) +static void R_Shadow_BounceGrid_AddSplatPath(vec3_t originalstart, vec3_t originalend, vec3_t color, vec_t distancetraveled) { int bestaxis; int numsplats; @@ -2509,6 +2533,8 @@ static void R_Shadow_BounceGrid_AddSplatPath(vec3_t originalstart, vec3_t origin VectorScale(diff, ilen, path->step); VectorCopy(color, path->splatcolor); VectorCopy(originaldir, path->splatdir); + path->splatsize_current = r_shadow_bouncegrid_state.settings.lightpathsize_initial + r_shadow_bouncegrid_state.settings.lightpathsize_conespread * distancetraveled * r_shadow_bouncegrid_state.ispacing[0]; + path->splatsize_perstep = r_shadow_bouncegrid_state.settings.lightpathsize_conespread; path->splatintensity = VectorLength(color); path->remainingsplats = numsplats; } @@ -2550,17 +2576,19 @@ static qboolean R_Shadow_BounceGrid_CheckEnable(int flag) static void R_Shadow_BounceGrid_GenerateSettings(r_shadow_bouncegrid_settings_t *settings) { qboolean s = r_shadow_bouncegrid_static.integer != 0; - float spacing = s ? r_shadow_bouncegrid_static_spacing.value : r_shadow_bouncegrid_dynamic_spacing.value; + float spacing = bound(1.0f, s ? r_shadow_bouncegrid_static_spacing.value : r_shadow_bouncegrid_dynamic_spacing.value, 1024.0f); + float quality = bound(0.0001f, (s ? r_shadow_bouncegrid_static_quality.value : r_shadow_bouncegrid_dynamic_quality.value), 1024.0f); float bounceminimumintensity = s ? r_shadow_bouncegrid_static_bounceminimumintensity.value : r_shadow_bouncegrid_dynamic_bounceminimumintensity.value; // prevent any garbage in alignment padded areas as we'll be using memcmp - memset(settings, 0, sizeof(*settings)); + memset(settings, 0, sizeof(*settings)); // build up a complete collection of the desired settings, so that memcmp can be used to compare parameters settings->staticmode = s; settings->blur = r_shadow_bouncegrid_blur.integer != 0; settings->floatcolors = bound(0, r_shadow_bouncegrid_floatcolors.integer, 2); - settings->lightpathsize = bound(1, r_shadow_bouncegrid_lightpathsize.integer, MAXBOUNCEGRIDSPLATSIZE); + settings->lightpathsize_initial = bound(0.0f, r_shadow_bouncegrid_lightpathsize_initial.value, 1024.0f); + settings->lightpathsize_conespread = bound(0.0f, r_shadow_bouncegrid_lightpathsize_conespread.value, 1024.0f); settings->bounceanglediffuse = r_shadow_bouncegrid_bounceanglediffuse.integer != 0; settings->directionalshading = (s ? r_shadow_bouncegrid_static_directionalshading.integer != 0 : r_shadow_bouncegrid_dynamic_directionalshading.integer != 0) && r_shadow_bouncegrid_state.allowdirectionalshading; settings->dlightparticlemultiplier = s ? 0 : r_shadow_bouncegrid_dynamic_dlightparticlemultiplier.value; @@ -2571,12 +2599,15 @@ static void R_Shadow_BounceGrid_GenerateSettings(r_shadow_bouncegrid_settings_t settings->particlebounceintensity = r_shadow_bouncegrid_particlebounceintensity.value; settings->particleintensity = r_shadow_bouncegrid_particleintensity.value * (settings->directionalshading ? 4.0f : 1.0f) * 16384 / (spacing * spacing) / 262144.0f; settings->maxphotons = s ? r_shadow_bouncegrid_static_maxphotons.integer : r_shadow_bouncegrid_dynamic_maxphotons.integer; - settings->energyperphoton = s ? r_shadow_bouncegrid_static_energyperphoton.value : r_shadow_bouncegrid_dynamic_energyperphoton.value; + settings->energyperphoton = spacing * spacing / quality; settings->spacing[0] = spacing; settings->spacing[1] = spacing; settings->spacing[2] = spacing; - settings->stablerandom = s ? 1 : r_shadow_bouncegrid_dynamic_stablerandom.integer; + settings->rng_type = r_shadow_bouncegrid_rng_type.integer; + settings->rng_seed = r_shadow_bouncegrid_rng_seed.integer; + settings->bounceminimumintensity2 = bounceminimumintensity * bounceminimumintensity; settings->bounceminimumintensity2 = bounceminimumintensity * bounceminimumintensity; + settings->normalizevectors = r_shadow_bouncegrid_normalizevectors.integer != 0; // bound the values for sanity settings->maxphotons = bound(1, settings->maxphotons, 25000000); @@ -2585,13 +2616,6 @@ static void R_Shadow_BounceGrid_GenerateSettings(r_shadow_bouncegrid_settings_t settings->spacing[0] = bound(1, settings->spacing[0], 512); settings->spacing[1] = bound(1, settings->spacing[1], 512); settings->spacing[2] = bound(1, settings->spacing[2], 512); - - // check if the ram requirements for blur would be excessive and disable it (increase lightpathsize to compensate) - if (spacing < 32 && settings->blur) - { - settings->blur = false; - settings->lightpathsize += 2; - } } static void R_Shadow_BounceGrid_UpdateSpacing(void) @@ -2618,8 +2642,54 @@ static void R_Shadow_BounceGrid_UpdateSpacing(void) // calculate texture size enclosing entire world bounds at the spacing if (r_refdef.scene.worldmodel) { - VectorMA(r_refdef.scene.worldmodel->normalmins, -2.0f, spacing, mins); - VectorMA(r_refdef.scene.worldmodel->normalmaxs, 2.0f, spacing, maxs); + int lightindex; + int range; + qboolean bounds_set = false; + dlight_t *light; + rtlight_t *rtlight; + + // calculate bounds enclosing world lights as they should be noticably tighter + // than the world bounds on maps with unlit monster containers (see e1m7 etc) + range = (unsigned int)Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked + for (lightindex = 0;lightindex < range;lightindex++) + { + const vec_t *rtlmins, *rtlmaxs; + + light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex); + if (!light) + continue; + + rtlight = &light->rtlight; + rtlmins = rtlight->cullmins; + rtlmaxs = rtlight->cullmaxs; + + if (!bounds_set) + { + VectorCopy(rtlmins, mins); + VectorCopy(rtlmaxs, maxs); + bounds_set = true; + } + else + { + mins[0] = min(mins[0], rtlmins[0]); + mins[1] = min(mins[1], rtlmins[1]); + mins[2] = min(mins[2], rtlmins[2]); + maxs[0] = max(maxs[0], rtlmaxs[0]); + maxs[1] = max(maxs[1], rtlmaxs[1]); + maxs[2] = max(maxs[2], rtlmaxs[2]); + } + } + + // limit to no larger than the world bounds + mins[0] = max(mins[0], r_refdef.scene.worldmodel->normalmins[0]); + mins[1] = max(mins[1], r_refdef.scene.worldmodel->normalmins[1]); + mins[2] = max(mins[2], r_refdef.scene.worldmodel->normalmins[2]); + maxs[0] = min(maxs[0], r_refdef.scene.worldmodel->normalmaxs[0]); + maxs[1] = min(maxs[1], r_refdef.scene.worldmodel->normalmaxs[1]); + maxs[2] = min(maxs[2], r_refdef.scene.worldmodel->normalmaxs[2]); + + VectorMA(mins, -2.0f, spacing, mins); + VectorMA(maxs, 2.0f, spacing, maxs); } else { @@ -2704,13 +2774,16 @@ static void R_Shadow_BounceGrid_UpdateSpacing(void) numpixels = r_shadow_bouncegrid_state.pixelsperband*r_shadow_bouncegrid_state.pixelbands; if (r_shadow_bouncegrid_state.numpixels != numpixels) { - if (r_shadow_bouncegrid_state.texture) R_FreeTexture(r_shadow_bouncegrid_state.texture);r_shadow_bouncegrid_state.texture = NULL; + if (r_shadow_bouncegrid_state.texture) { R_FreeTexture(r_shadow_bouncegrid_state.texture);r_shadow_bouncegrid_state.texture = NULL; } + r_shadow_bouncegrid_state.highpixels = NULL; - if (r_shadow_bouncegrid_state.blurpixels[0]) Mem_Free(r_shadow_bouncegrid_state.blurpixels[0]); r_shadow_bouncegrid_state.blurpixels[0] = NULL; - if (r_shadow_bouncegrid_state.blurpixels[1]) Mem_Free(r_shadow_bouncegrid_state.blurpixels[1]); r_shadow_bouncegrid_state.blurpixels[1] = NULL; - if (r_shadow_bouncegrid_state.u8pixels) Mem_Free(r_shadow_bouncegrid_state.u8pixels); r_shadow_bouncegrid_state.u8pixels = NULL; - if (r_shadow_bouncegrid_state.fp16pixels) Mem_Free(r_shadow_bouncegrid_state.fp16pixels); r_shadow_bouncegrid_state.fp16pixels = NULL; - if (r_shadow_bouncegrid_state.splatpaths) Mem_Free(r_shadow_bouncegrid_state.splatpaths); r_shadow_bouncegrid_state.splatpaths = NULL; + + if (r_shadow_bouncegrid_state.blurpixels[0]) { Mem_Free(r_shadow_bouncegrid_state.blurpixels[0]); r_shadow_bouncegrid_state.blurpixels[0] = NULL; } + if (r_shadow_bouncegrid_state.blurpixels[1]) { Mem_Free(r_shadow_bouncegrid_state.blurpixels[1]); r_shadow_bouncegrid_state.blurpixels[1] = NULL; } + if (r_shadow_bouncegrid_state.u8pixels) { Mem_Free(r_shadow_bouncegrid_state.u8pixels); r_shadow_bouncegrid_state.u8pixels = NULL; } + if (r_shadow_bouncegrid_state.fp16pixels) { Mem_Free(r_shadow_bouncegrid_state.fp16pixels); r_shadow_bouncegrid_state.fp16pixels = NULL; } + if (r_shadow_bouncegrid_state.splatpaths) { Mem_Free(r_shadow_bouncegrid_state.splatpaths); r_shadow_bouncegrid_state.splatpaths = NULL; } + r_shadow_bouncegrid_state.maxsplatpaths = 0; r_shadow_bouncegrid_state.numpixels = numpixels; } @@ -2748,7 +2821,7 @@ static void R_Shadow_BounceGrid_AssignPhotons(r_shadow_bouncegrid_settings_t *se dlight_t *light; rtlight_t *rtlight; normalphotonscaling = 1.0f / max(0.0001f, settings->energyperphoton); - for (lightindex = 0; lightindex < range2; lightindex++) + for (lightindex = 0;lightindex < range2;lightindex++) { if (lightindex < range) { @@ -2792,12 +2865,26 @@ static void R_Shadow_BounceGrid_AssignPhotons(r_shadow_bouncegrid_settings_t *se w = r_shadow_lightintensityscale.value * (rtlight->ambientscale + rtlight->diffusescale + rtlight->specularscale); if (!settings->staticmode) { - if (R_CullBox(cullmins, cullmaxs)) - continue; + // skip if the expanded light box does not touch any visible leafs if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.BoxTouchingVisibleLeafs && !r_refdef.scene.worldmodel->brush.BoxTouchingVisibleLeafs(r_refdef.scene.worldmodel, r_refdef.viewcache.world_leafvisible, cullmins, cullmaxs)) continue; + // skip if the expanded light box is not visible to traceline + // note that PrepareLight already did this check but for a smaller box, so we + // end up casting more traces per frame per light when using bouncegrid, which + // is probably fine (and they use the same timer) + if (r_shadow_culllights_trace.integer) + { + if (rtlight->trace_timer != realtime && R_CanSeeBox(rtlight->trace_timer == 0 ? r_shadow_culllights_trace_tempsamples.integer : r_shadow_culllights_trace_samples.integer, r_shadow_culllights_trace_eyejitter.value, r_shadow_culllights_trace_enlarge.value, r_refdef.view.origin, rtlight->cullmins, rtlight->cullmaxs)) + rtlight->trace_timer = realtime; + if (realtime - rtlight->trace_timer > r_shadow_culllights_trace_delay.value) + return; + } + // skip if expanded light box is offscreen + if (R_CullBox(cullmins, cullmaxs)) + continue; + // skip if overall light intensity is zero if (w * VectorLength2(rtlight->color) == 0.0f) continue; } @@ -2808,7 +2895,7 @@ static void R_Shadow_BounceGrid_AssignPhotons(r_shadow_bouncegrid_settings_t *se w *= ((rtlight->style >= 0 && rtlight->style < MAX_LIGHTSTYLES) ? r_refdef.scene.rtlightstylevalue[rtlight->style] : 1); VectorScale(rtlight->color, w, rtlight->bouncegrid_photoncolor); // skip lights that will emit no photons - if (VectorLength2(rtlight->bouncegrid_photoncolor) <= 0.0f) + if (!VectorLength2(rtlight->bouncegrid_photoncolor)) continue; // shoot particles from this light // use a calculation for the number of particles that will not @@ -2882,8 +2969,6 @@ static void R_Shadow_BounceGrid_ClearPixels(void) static void R_Shadow_BounceGrid_PerformSplats(void) { - int splatsize = r_shadow_bouncegrid_state.settings.lightpathsize; - int splatsize1 = splatsize + 1; r_shadow_bouncegrid_splatpath_t *splatpaths = r_shadow_bouncegrid_state.splatpaths; r_shadow_bouncegrid_splatpath_t *splatpath; float *highpixels = r_shadow_bouncegrid_state.highpixels; @@ -2892,12 +2977,10 @@ static void R_Shadow_BounceGrid_PerformSplats(void) vec3_t steppos; vec3_t stepdelta; vec3_t dir; - float texcorner[3]; - float texlerp[MAXBOUNCEGRIDSPLATSIZE1][3]; + vec_t lightpathsize_current; + vec_t lightpathsize_perstep; float splatcolor[32]; - float boxweight = 1.0f / (splatsize * splatsize * splatsize); int resolution[3]; - int tex[3]; int pixelsperband = r_shadow_bouncegrid_state.pixelsperband; int pixelbands = r_shadow_bouncegrid_state.pixelbands; int numsteps; @@ -2913,10 +2996,6 @@ static void R_Shadow_BounceGrid_PerformSplats(void) if (r_shadow_bouncegrid_sortlightpaths.integer) qsort(splatpaths, numsplatpaths, sizeof(*splatpaths), R_Shadow_BounceGrid_SplatPathCompare); - // the middle row/column/layer of each splat are full intensity - for (step = 1;step < splatsize;step++) - VectorSet(texlerp[step], 1.0f, 1.0f, 1.0f); - splatpath = splatpaths; for (splatindex = 0;splatindex < numsplatpaths;splatindex++, splatpath++) { @@ -2968,59 +3047,64 @@ static void R_Shadow_BounceGrid_PerformSplats(void) VectorCopy(splatpath->point, steppos); VectorCopy(splatpath->step, stepdelta); numsteps = splatpath->remainingsplats; + lightpathsize_current = splatpath->splatsize_current + 1.0f; // add 1.0 for the gradient fade around the sphere + lightpathsize_perstep = splatpath->splatsize_perstep; for (step = 0;step < numsteps;step++) { - r_refdef.stats[r_stat_bouncegrid_splats]++; - // figure out the min corner of the pixels we'll need to update - texcorner[0] = steppos[0] - (splatsize1 * 0.5f); - texcorner[1] = steppos[1] - (splatsize1 * 0.5f); - texcorner[2] = steppos[2] - (splatsize1 * 0.5f); - tex[0] = (int)floor(texcorner[0]); - tex[1] = (int)floor(texcorner[1]); - tex[2] = (int)floor(texcorner[2]); - // only update if it is within reasonable bounds - if (tex[0] >= 1 - && tex[1] >= 1 - && tex[2] >= 1 - && tex[0] < resolution[0] - splatsize1 - && tex[1] < resolution[1] - splatsize1 - && tex[2] < resolution[2] - splatsize1) + // the middle row/column/layer of each splat are full intensity + float splatmins[3]; + float splatmaxs[3]; + if (lightpathsize_current > MAXBOUNCEGRIDSPLATSIZE) + lightpathsize_current = MAXBOUNCEGRIDSPLATSIZE; + splatmins[0] = max(1.0f, steppos[0] - lightpathsize_current * 0.5f); + splatmins[1] = max(1.0f, steppos[1] - lightpathsize_current * 0.5f); + splatmins[2] = max(1.0f, steppos[2] - lightpathsize_current * 0.5f); + splatmaxs[0] = min(steppos[0] + lightpathsize_current * 0.5f, resolution[0] - 1.0f); + splatmaxs[1] = min(steppos[1] + lightpathsize_current * 0.5f, resolution[1] - 1.0f); + splatmaxs[2] = min(steppos[2] + lightpathsize_current * 0.5f, resolution[2] - 1.0f); + if (splatmaxs[0] > splatmins[0] && splatmaxs[1] > splatmins[1] && splatmaxs[2] > splatmins[2]) { // it is within bounds... do the real work now - int xi, yi, zi; - - // calculate the antialiased box edges - texlerp[splatsize][0] = texcorner[0] - tex[0]; - texlerp[splatsize][1] = texcorner[1] - tex[1]; - texlerp[splatsize][2] = texcorner[2] - tex[2]; - texlerp[0][0] = 1.0f - texlerp[splatsize][0]; - texlerp[0][1] = 1.0f - texlerp[splatsize][1]; - texlerp[0][2] = 1.0f - texlerp[splatsize][2]; - + int xi, yi, zi, band, row; + float pixelpos[3]; + float w; + float *p; + float colorscale = 1.0f / lightpathsize_current; + r_refdef.stats[r_stat_bouncegrid_splats]++; // accumulate light onto the pixels - for (zi = 0;zi < splatsize1;zi++) + for (zi = (int)floor(splatmins[2]);zi < splatmaxs[2];zi++) { - for (yi = 0;yi < splatsize1;yi++) + pixelpos[2] = zi + 0.5f; + for (yi = (int)floor(splatmins[1]); yi < splatmaxs[1]; yi++) { - int index = ((tex[2]+zi)*resolution[1]+tex[1]+yi)*resolution[0]+tex[0]; - for (xi = 0;xi < splatsize1;xi++, index++) + pixelpos[1] = yi + 0.5f; + row = (zi*resolution[1] + yi)*resolution[0]; + for (xi = (int)floor(splatmins[0]); xi < splatmaxs[0]; xi++) { - float w = texlerp[xi][0]*texlerp[yi][1]*texlerp[zi][2] * boxweight; - int band = 0; - float *p = highpixels + 4 * index + band * pixelsperband * 4; - for (;band < pixelbands;band++, p += pixelsperband * 4) + pixelpos[0] = xi + 0.5f; + // simple radial antialiased sphere - linear gradient fade over 1 pixel from the edge + w = lightpathsize_current - VectorDistance(pixelpos, steppos); + if (w > 0.0f) { - // add to the pixel color - p[0] += splatcolor[band*4+0] * w; - p[1] += splatcolor[band*4+1] * w; - p[2] += splatcolor[band*4+2] * w; - p[3] += splatcolor[band*4+3] * w; + if (w > 1.0f) + w = 1.0f; + w *= colorscale; + p = highpixels + 4 * (row + xi); + for (band = 0; band < pixelbands; band++, p += pixelsperband * 4) + { + // add to the pixel color + p[0] += splatcolor[band * 4 + 0] * w; + p[1] += splatcolor[band * 4 + 1] * w; + p[2] += splatcolor[band * 4 + 2] * w; + p[3] += splatcolor[band * 4 + 3] * w; + } } } } } } VectorAdd(steppos, stepdelta, steppos); + lightpathsize_current += lightpathsize_perstep; } } } @@ -3265,6 +3349,7 @@ static void R_Shadow_BounceGrid_TracePhotons(r_shadow_bouncegrid_settings_t sett int bouncecount; int hitsupercontentsmask; int skipsupercontentsmask; + int skipmaterialflagsmask; int maxbounce; int shootparticles; int shotparticles; @@ -3273,7 +3358,7 @@ static void R_Shadow_BounceGrid_TracePhotons(r_shadow_bouncegrid_settings_t sett //trace_t cliptrace2; //trace_t cliptrace3; unsigned int lightindex; - unsigned int seed = (unsigned int)(realtime * 1000.0f); + unsigned int seed; randomseed_t randomseed; vec3_t shotcolor; vec3_t baseshotcolor; @@ -3282,19 +3367,23 @@ static void R_Shadow_BounceGrid_TracePhotons(r_shadow_bouncegrid_settings_t sett vec3_t clipstart; vec3_t clipdiff; vec_t radius; + vec_t distancetraveled; vec_t s; rtlight_t *rtlight; - Math_RandomSeed_FromInt(&randomseed, seed); + // compute a seed for the unstable random modes + Math_RandomSeed_FromInts(&randomseed, 0, 0, 0, realtime * 1000.0); + seed = realtime * 1000.0; r_shadow_bouncegrid_state.numsplatpaths = 0; // figure out what we want to interact with if (settings.hitmodels) - hitsupercontentsmask = SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY;// | SUPERCONTENTS_LIQUIDSMASK; + hitsupercontentsmask = SUPERCONTENTS_SOLID;// | SUPERCONTENTS_LIQUIDSMASK; else hitsupercontentsmask = SUPERCONTENTS_SOLID;// | SUPERCONTENTS_LIQUIDSMASK; - skipsupercontentsmask = SUPERCONTENTS_SKY; // skipsurfaces with this contents, even if they also have SUPERCONTENTS_SOLID (basically this is for e1m5.bsp sky trick) + skipsupercontentsmask = 0; + skipmaterialflagsmask = MATERIALFLAGMASK_TRANSLUCENT; maxbounce = settings.maxbounce; for (lightindex = 0;lightindex < range2;lightindex++) @@ -3325,39 +3414,41 @@ static void R_Shadow_BounceGrid_TracePhotons(r_shadow_bouncegrid_settings_t sett r_refdef.stats[r_stat_bouncegrid_particles] += shootparticles; // we stop caring about bounces once the brightness goes below this fraction of the original intensity bounceminimumintensity2 = VectorLength(baseshotcolor) * settings.bounceminimumintensity2; - switch (settings.stablerandom) + + // for seeded random we start the RNG with the position of the light + if (settings.rng_seed >= 0) { - default: - break; - case 1: - Math_RandomSeed_FromInt(&randomseed, lightindex * 11937); - // prime the random number generator a bit - Math_crandomf(&randomseed); - break; - case 2: - seed = lightindex * 11937; - // prime the random number generator a bit - lhcheeserand(seed); - break; + union + { + unsigned int i[4]; + float f[4]; + } + u; + u.f[0] = rtlight->shadoworigin[0]; + u.f[1] = rtlight->shadoworigin[1]; + u.f[2] = rtlight->shadoworigin[2]; + u.f[3] = 1; + switch (settings.rng_type) + { + default: + case 0: + // we have to shift the seed provided by the user because the result must be odd + Math_RandomSeed_FromInts(&randomseed, u.i[0], u.i[1], u.i[2], u.i[3] ^ (settings.rng_seed << 1)); + break; + case 1: + seed = u.i[0] ^ u.i[1] ^ u.i[2] ^ u.i[3] ^ settings.rng_seed; + break; + } } + for (shotparticles = 0;shotparticles < shootparticles;shotparticles++) { VectorCopy(baseshotcolor, shotcolor); VectorCopy(rtlight->shadoworigin, clipstart); - switch (settings.stablerandom) + switch (settings.rng_type) { default: case 0: - VectorRandom(clipend); - if (settings.bounceanglediffuse) - { - // we want random to be stable, so we still have to do all the random we would have done - for (bouncecount = 0; bouncecount < maxbounce; bouncecount++) - VectorRandom(bouncerandom[bouncecount]); - } - break; - case -1: - case 1: VectorLehmerRandom(&randomseed, clipend); if (settings.bounceanglediffuse) { @@ -3366,8 +3457,7 @@ static void R_Shadow_BounceGrid_TracePhotons(r_shadow_bouncegrid_settings_t sett VectorLehmerRandom(&randomseed, bouncerandom[bouncecount]); } break; - case -2: - case 2: + case 1: VectorCheeseRandom(seed, clipend); if (settings.bounceanglediffuse) { @@ -3377,30 +3467,37 @@ static void R_Shadow_BounceGrid_TracePhotons(r_shadow_bouncegrid_settings_t sett } break; } + + // we want a uniform distribution spherically, not merely within the sphere + if (settings.normalizevectors) + VectorNormalize(clipend); + VectorMA(clipstart, radius, clipend, clipend); + distancetraveled = 0.0f; for (bouncecount = 0;;bouncecount++) { r_refdef.stats[r_stat_bouncegrid_traces]++; rtlight->bouncegrid_traces++; //r_refdef.scene.worldmodel->TraceLineAgainstSurfaces(r_refdef.scene.worldmodel, NULL, NULL, &cliptrace, clipstart, clipend, hitsupercontentsmask); //r_refdef.scene.worldmodel->TraceLine(r_refdef.scene.worldmodel, NULL, NULL, &cliptrace2, clipstart, clipend, hitsupercontentsmask); - if (settings.staticmode || settings.stablerandom <= 0) + if (settings.staticmode || settings.rng_seed < 0) { // static mode fires a LOT of rays but none of them are identical, so they are not cached // non-stable random in dynamic mode also never reuses a direction, so there's no reason to cache it - cliptrace = CL_TraceLine(clipstart, clipend, settings.staticmode ? MOVE_WORLDONLY : (settings.hitmodels ? MOVE_HITMODEL : MOVE_NOMONSTERS), NULL, hitsupercontentsmask, skipsupercontentsmask, collision_extendmovelength.value, true, false, NULL, true, true); + cliptrace = CL_TraceLine(clipstart, clipend, settings.staticmode ? MOVE_WORLDONLY : (settings.hitmodels ? MOVE_HITMODEL : MOVE_NOMONSTERS), NULL, hitsupercontentsmask, skipsupercontentsmask, skipmaterialflagsmask, collision_extendmovelength.value, true, false, NULL, true, true); } else { // dynamic mode fires many rays and most will match the cache from the previous frame - cliptrace = CL_Cache_TraceLineSurfaces(clipstart, clipend, settings.hitmodels ? MOVE_HITMODEL : MOVE_NOMONSTERS, hitsupercontentsmask, skipsupercontentsmask); + cliptrace = CL_Cache_TraceLineSurfaces(clipstart, clipend, settings.staticmode ? MOVE_WORLDONLY : (settings.hitmodels ? MOVE_HITMODEL : MOVE_NOMONSTERS), hitsupercontentsmask, skipsupercontentsmask, skipmaterialflagsmask); } if (bouncecount > 0 || settings.includedirectlighting) { vec3_t hitpos; VectorCopy(cliptrace.endpos, hitpos); - R_Shadow_BounceGrid_AddSplatPath(clipstart, hitpos, shotcolor); + R_Shadow_BounceGrid_AddSplatPath(clipstart, hitpos, shotcolor, distancetraveled); } + distancetraveled += VectorDistance(clipstart, cliptrace.endpos); s = VectorDistance(rtlight->shadoworigin, cliptrace.endpos); if (rtlight->bouncegrid_effectiveradius < s) rtlight->bouncegrid_effectiveradius = s; @@ -3787,10 +3884,10 @@ static void R_Shadow_RenderLighting_VisibleLighting(int texturenumsurfaces, cons RSurf_DrawBatch(); } -static void R_Shadow_RenderLighting_Light_GLSL(int texturenumsurfaces, const msurface_t **texturesurfacelist, const vec3_t lightcolor, float ambientscale, float diffusescale, float specularscale) +static void R_Shadow_RenderLighting_Light_GLSL(int texturenumsurfaces, const msurface_t **texturesurfacelist, const float ambientcolor[3], const float diffusecolor[3], const float specularcolor[3]) { // ARB2 GLSL shader path (GFFX5200, Radeon 9500) - R_SetupShader_Surface(lightcolor, false, ambientscale, diffusescale, specularscale, RSURFPASS_RTLIGHT, texturenumsurfaces, texturesurfacelist, NULL, false); + R_SetupShader_Surface(ambientcolor, diffusecolor, specularcolor, RSURFPASS_RTLIGHT, texturenumsurfaces, texturesurfacelist, NULL, false); RSurf_DrawBatch(); } @@ -3883,29 +3980,26 @@ static void R_Shadow_RenderLighting_Light_Vertex_Pass(int firstvertex, int numve } } -static void R_Shadow_RenderLighting_Light_Vertex(int texturenumsurfaces, const msurface_t **texturesurfacelist, const vec3_t lightcolor, float ambientscale, float diffusescale) +static void R_Shadow_RenderLighting_Light_Vertex(int texturenumsurfaces, const msurface_t **texturesurfacelist, const float ambientcolor[3], const float diffusecolor[3]) { // OpenGL 1.1 path (anything) float ambientcolorbase[3], diffusecolorbase[3]; float ambientcolorpants[3], diffusecolorpants[3]; float ambientcolorshirt[3], diffusecolorshirt[3]; - const float *surfacecolor = rsurface.texture->dlightcolor; - const float *surfacepants = rsurface.colormap_pantscolor; - const float *surfaceshirt = rsurface.colormap_shirtcolor; + const float *surfacepants = rsurface.texture->render_colormap_pants; + const float *surfaceshirt = rsurface.texture->render_colormap_shirt; rtexture_t *basetexture = rsurface.texture->basetexture; rtexture_t *pantstexture = rsurface.texture->pantstexture; rtexture_t *shirttexture = rsurface.texture->shirttexture; qboolean dopants = pantstexture && VectorLength2(surfacepants) >= (1.0f / 1048576.0f); qboolean doshirt = shirttexture && VectorLength2(surfaceshirt) >= (1.0f / 1048576.0f); - ambientscale *= 2 * r_refdef.view.colorscale; - diffusescale *= 2 * r_refdef.view.colorscale; - ambientcolorbase[0] = lightcolor[0] * ambientscale * surfacecolor[0];ambientcolorbase[1] = lightcolor[1] * ambientscale * surfacecolor[1];ambientcolorbase[2] = lightcolor[2] * ambientscale * surfacecolor[2]; - diffusecolorbase[0] = lightcolor[0] * diffusescale * surfacecolor[0];diffusecolorbase[1] = lightcolor[1] * diffusescale * surfacecolor[1];diffusecolorbase[2] = lightcolor[2] * diffusescale * surfacecolor[2]; + VectorCopy(ambientcolor, ambientcolorbase); + VectorCopy(diffusecolor, diffusecolorbase); ambientcolorpants[0] = ambientcolorbase[0] * surfacepants[0];ambientcolorpants[1] = ambientcolorbase[1] * surfacepants[1];ambientcolorpants[2] = ambientcolorbase[2] * surfacepants[2]; diffusecolorpants[0] = diffusecolorbase[0] * surfacepants[0];diffusecolorpants[1] = diffusecolorbase[1] * surfacepants[1];diffusecolorpants[2] = diffusecolorbase[2] * surfacepants[2]; ambientcolorshirt[0] = ambientcolorbase[0] * surfaceshirt[0];ambientcolorshirt[1] = ambientcolorbase[1] * surfaceshirt[1];ambientcolorshirt[2] = ambientcolorbase[2] * surfaceshirt[2]; diffusecolorshirt[0] = diffusecolorbase[0] * surfaceshirt[0];diffusecolorshirt[1] = diffusecolorbase[1] * surfaceshirt[1];diffusecolorshirt[2] = diffusecolorbase[2] * surfaceshirt[2]; - RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | (diffusescale > 0 ? BATCHNEED_ARRAY_NORMAL : 0) | BATCHNEED_ARRAY_TEXCOORD | BATCHNEED_NOGAPS, texturenumsurfaces, texturesurfacelist); + RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | (VectorLength2(diffusecolor) > 0 ? BATCHNEED_ARRAY_NORMAL : 0) | BATCHNEED_ARRAY_TEXCOORD | BATCHNEED_NOGAPS, texturenumsurfaces, texturesurfacelist); rsurface.passcolor4f = (float *)R_FrameData_Alloc((rsurface.batchfirstvertex + rsurface.batchnumvertices) * sizeof(float[4])); R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer, rsurface.batchvertex3f_bufferoffset); R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), rsurface.passcolor4f, 0, 0); @@ -3955,25 +4049,28 @@ static void R_Shadow_RenderLighting_Light_Vertex(int texturenumsurfaces, const m extern cvar_t gl_lightmaps; void R_Shadow_RenderLighting(int texturenumsurfaces, const msurface_t **texturesurfacelist) { - float ambientscale, diffusescale, specularscale; qboolean negated; - float lightcolor[3]; - VectorCopy(rsurface.rtlight->currentcolor, lightcolor); - ambientscale = rsurface.rtlight->ambientscale + rsurface.texture->rtlightambient; - diffusescale = rsurface.rtlight->diffusescale * max(0, 1.0 - rsurface.texture->rtlightambient); - specularscale = rsurface.rtlight->specularscale * rsurface.texture->specularscale; + float ambientcolor[3], diffusecolor[3], specularcolor[3]; + VectorM(rsurface.rtlight->ambientscale + rsurface.texture->rtlightambient, rsurface.texture->render_rtlight_diffuse, ambientcolor); + VectorM(rsurface.rtlight->diffusescale * max(0, 1.0 - rsurface.texture->rtlightambient), rsurface.texture->render_rtlight_diffuse, diffusecolor); + VectorM(rsurface.rtlight->specularscale, rsurface.texture->render_rtlight_specular, specularcolor); if (!r_shadow_usenormalmap.integer) { - ambientscale += 1.0f * diffusescale; - diffusescale = 0; - specularscale = 0; + VectorMAM(1.0f, ambientcolor, 1.0f, diffusecolor, ambientcolor); + VectorClear(diffusecolor); + VectorClear(specularcolor); } - if ((ambientscale + diffusescale) * VectorLength2(lightcolor) + specularscale * VectorLength2(lightcolor) < (1.0f / 1048576.0f)) + VectorMultiply(ambientcolor, rsurface.rtlight->currentcolor, ambientcolor); + VectorMultiply(diffusecolor, rsurface.rtlight->currentcolor, diffusecolor); + VectorMultiply(specularcolor, rsurface.rtlight->currentcolor, specularcolor); + if (VectorLength2(ambientcolor) + VectorLength2(diffusecolor) + VectorLength2(specularcolor) < (1.0f / 1048576.0f)) return; - negated = (lightcolor[0] + lightcolor[1] + lightcolor[2] < 0) && vid.support.ext_blend_subtract; + negated = (rsurface.rtlight->currentcolor[0] + rsurface.rtlight->currentcolor[1] + rsurface.rtlight->currentcolor[2] < 0) && vid.support.ext_blend_subtract; if(negated) { - VectorNegate(lightcolor, lightcolor); + VectorNegate(ambientcolor, ambientcolor); + VectorNegate(diffusecolor, diffusecolor); + VectorNegate(specularcolor, specularcolor); GL_BlendEquationSubtract(true); } RSurf_SetupDepthAndCulling(); @@ -3984,13 +4081,13 @@ void R_Shadow_RenderLighting(int texturenumsurfaces, const msurface_t **textures R_Shadow_RenderLighting_VisibleLighting(texturenumsurfaces, texturesurfacelist); break; case R_SHADOW_RENDERMODE_LIGHT_GLSL: - R_Shadow_RenderLighting_Light_GLSL(texturenumsurfaces, texturesurfacelist, lightcolor, ambientscale, diffusescale, specularscale); + R_Shadow_RenderLighting_Light_GLSL(texturenumsurfaces, texturesurfacelist, ambientcolor, diffusecolor, specularcolor); break; case R_SHADOW_RENDERMODE_LIGHT_VERTEX3DATTEN: case R_SHADOW_RENDERMODE_LIGHT_VERTEX2D1DATTEN: case R_SHADOW_RENDERMODE_LIGHT_VERTEX2DATTEN: case R_SHADOW_RENDERMODE_LIGHT_VERTEX: - R_Shadow_RenderLighting_Light_Vertex(texturenumsurfaces, texturesurfacelist, lightcolor, ambientscale, diffusescale); + R_Shadow_RenderLighting_Light_Vertex(texturenumsurfaces, texturesurfacelist, ambientcolor, diffusecolor); break; default: Con_Printf("R_Shadow_RenderLighting: unknown r_shadow_rendermode %i\n", r_shadow_rendermode); @@ -4076,7 +4173,7 @@ void R_RTLight_Compile(rtlight_t *rtlight) // this variable must be set for the CompileShadowVolume/CompileShadowMap code r_shadow_compilingrtlight = rtlight; R_FrameData_SetMark(); - model->GetLightInfo(ent, rtlight->shadoworigin, rtlight->radius, rtlight->cullmins, rtlight->cullmaxs, r_shadow_buffer_leaflist, r_shadow_buffer_leafpvs, &numleafs, r_shadow_buffer_surfacelist, r_shadow_buffer_surfacepvs, &numsurfaces, r_shadow_buffer_shadowtrispvs, r_shadow_buffer_lighttrispvs, r_shadow_buffer_visitingleafpvs, 0, NULL); + model->GetLightInfo(ent, rtlight->shadoworigin, rtlight->radius, rtlight->cullmins, rtlight->cullmaxs, r_shadow_buffer_leaflist, r_shadow_buffer_leafpvs, &numleafs, r_shadow_buffer_surfacelist, r_shadow_buffer_surfacepvs, &numsurfaces, r_shadow_buffer_shadowtrispvs, r_shadow_buffer_lighttrispvs, r_shadow_buffer_visitingleafpvs, 0, NULL, rtlight->shadow == 0); R_FrameData_ReturnToMark(); numleafpvsbytes = (model->brush.num_leafs + 7) >> 3; numshadowtrispvsbytes = ((model->brush.shadowmesh ? model->brush.shadowmesh->numtriangles : model->surfmesh.num_triangles) + 7) >> 3; @@ -4367,7 +4464,7 @@ static void R_Shadow_DrawWorldShadow_ShadowMap(int numsurfaces, int *surfacelist { shadowmesh_t *mesh; - RSurf_ActiveWorldEntity(); + RSurf_ActiveModelEntity(r_refdef.scene.worldentity, false, false, false); if (rsurface.rtlight->compiled && r_shadow_realtime_world_compile.integer && r_shadow_realtime_world_compileshadow.integer) { @@ -4387,7 +4484,7 @@ static void R_Shadow_DrawWorldShadow_ShadowMap(int numsurfaces, int *surfacelist else if (r_refdef.scene.worldentity->model) r_refdef.scene.worldmodel->DrawShadowMap(r_shadow_shadowmapside, r_refdef.scene.worldentity, rsurface.rtlight->shadoworigin, NULL, rsurface.rtlight->radius, numsurfaces, surfacelist, surfacesides, rsurface.rtlight->cached_cullmins, rsurface.rtlight->cached_cullmaxs); - rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity + rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity } static void R_Shadow_DrawWorldShadow_ShadowVolume(int numsurfaces, int *surfacelist, const unsigned char *trispvs) @@ -4402,7 +4499,7 @@ static void R_Shadow_DrawWorldShadow_ShadowVolume(int numsurfaces, int *surfacel if (r_refdef.scene.worldmodel->brush.shadowmesh ? !r_refdef.scene.worldmodel->brush.shadowmesh->neighbor3i : !r_refdef.scene.worldmodel->surfmesh.data_neighbor3i) return; - RSurf_ActiveWorldEntity(); + RSurf_ActiveModelEntity(r_refdef.scene.worldentity, false, false, false); if (rsurface.rtlight->compiled && r_shadow_realtime_world_compile.integer && r_shadow_realtime_world_compileshadow.integer) { @@ -4459,7 +4556,7 @@ static void R_Shadow_DrawWorldShadow_ShadowVolume(int numsurfaces, int *surfacel r_refdef.scene.worldmodel->DrawShadowVolume(r_refdef.scene.worldentity, rsurface.rtlight->shadoworigin, NULL, rsurface.rtlight->radius, numsurfaces, surfacelist, rsurface.rtlight->cached_cullmins, rsurface.rtlight->cached_cullmaxs); } - rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity + rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity } static void R_Shadow_DrawEntityShadow(entity_render_t *ent) @@ -4485,7 +4582,7 @@ static void R_Shadow_DrawEntityShadow(entity_render_t *ent) ent->model->DrawShadowVolume(ent, relativeshadoworigin, NULL, relativeshadowradius, ent->model->nummodelsurfaces, ent->model->sortedmodelsurfaces, relativeshadowmins, relativeshadowmaxs); break; } - rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity + rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity } void R_Shadow_SetupEntityLight(const entity_render_t *ent) @@ -4504,7 +4601,7 @@ static void R_Shadow_DrawWorldLight(int numsurfaces, int *surfacelist, const uns return; // set up properties for rendering light onto this entity - RSurf_ActiveWorldEntity(); + RSurf_ActiveModelEntity(r_refdef.scene.worldentity, false, false, false); rsurface.entitytolight = rsurface.rtlight->matrix_worldtolight; Matrix4x4_Concat(&rsurface.entitytoattenuationxyz, &matrix_attenuationxyz, &rsurface.entitytolight); Matrix4x4_Concat(&rsurface.entitytoattenuationz, &matrix_attenuationz, &rsurface.entitytolight); @@ -4512,7 +4609,7 @@ static void R_Shadow_DrawWorldLight(int numsurfaces, int *surfacelist, const uns r_refdef.scene.worldmodel->DrawLight(r_refdef.scene.worldentity, numsurfaces, surfacelist, lighttrispvs); - rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity + rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity } static void R_Shadow_DrawEntityLight(entity_render_t *ent) @@ -4525,7 +4622,7 @@ static void R_Shadow_DrawEntityLight(entity_render_t *ent) model->DrawLight(ent, model->nummodelsurfaces, model->sortedmodelsurfaces, NULL); - rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity + rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity } static void R_Shadow_PrepareLight(rtlight_t *rtlight) @@ -4594,7 +4691,7 @@ static void R_Shadow_PrepareLight(rtlight_t *rtlight) } */ - // if lightstyle is currently off, don't draw the light + // skip if lightstyle is currently off if (VectorLength2(rtlight->currentcolor) < (1.0f / 1048576.0f)) return; @@ -4602,10 +4699,27 @@ static void R_Shadow_PrepareLight(rtlight_t *rtlight) if (nolight) return; - // if the light box is offscreen, skip it + // skip if the light box is not touching any visible leafs + if (r_shadow_culllights_pvs.integer + && r_refdef.scene.worldmodel + && r_refdef.scene.worldmodel->brush.BoxTouchingVisibleLeafs + && !r_refdef.scene.worldmodel->brush.BoxTouchingVisibleLeafs(r_refdef.scene.worldmodel, r_refdef.viewcache.world_leafvisible, rtlight->cullmins, rtlight->cullmaxs)) + return; + + // skip if the light box is not visible to traceline + if (r_shadow_culllights_trace.integer) + { + if (rtlight->trace_timer != realtime && R_CanSeeBox(rtlight->trace_timer == 0 ? r_shadow_culllights_trace_tempsamples.integer : r_shadow_culllights_trace_samples.integer, r_shadow_culllights_trace_eyejitter.value, r_shadow_culllights_trace_enlarge.value, r_refdef.view.origin, rtlight->cullmins, rtlight->cullmaxs)) + rtlight->trace_timer = realtime; + if (realtime - rtlight->trace_timer > r_shadow_culllights_trace_delay.value) + return; + } + + // skip if the light box is off screen if (R_CullBox(rtlight->cullmins, rtlight->cullmaxs)) return; + // in the typical case this will be quickly replaced by GetLightInfo VectorCopy(rtlight->cullmins, rtlight->cached_cullmins); VectorCopy(rtlight->cullmaxs, rtlight->cached_cullmaxs); @@ -4632,7 +4746,7 @@ static void R_Shadow_PrepareLight(rtlight_t *rtlight) { // dynamic light, world available and can receive realtime lighting // calculate lit surfaces and leafs - r_refdef.scene.worldmodel->GetLightInfo(r_refdef.scene.worldentity, rtlight->shadoworigin, rtlight->radius, rtlight->cached_cullmins, rtlight->cached_cullmaxs, r_shadow_buffer_leaflist, r_shadow_buffer_leafpvs, &numleafs, r_shadow_buffer_surfacelist, r_shadow_buffer_surfacepvs, &numsurfaces, r_shadow_buffer_shadowtrispvs, r_shadow_buffer_lighttrispvs, r_shadow_buffer_visitingleafpvs, rtlight->cached_numfrustumplanes, rtlight->cached_frustumplanes); + r_refdef.scene.worldmodel->GetLightInfo(r_refdef.scene.worldentity, rtlight->shadoworigin, rtlight->radius, rtlight->cached_cullmins, rtlight->cached_cullmaxs, r_shadow_buffer_leaflist, r_shadow_buffer_leafpvs, &numleafs, r_shadow_buffer_surfacelist, r_shadow_buffer_surfacepvs, &numsurfaces, r_shadow_buffer_shadowtrispvs, r_shadow_buffer_lighttrispvs, r_shadow_buffer_visitingleafpvs, rtlight->cached_numfrustumplanes, rtlight->cached_frustumplanes, rtlight->shadow == 0); R_Shadow_ComputeShadowCasterCullingPlanes(rtlight); leaflist = r_shadow_buffer_leaflist; leafpvs = r_shadow_buffer_leafpvs; @@ -5458,7 +5572,7 @@ void R_Shadow_PrepareModelShadows(void) r_shadow_nummodelshadows = 0; r_shadow_shadowmapatlas_modelshadows_size = 0; - if (!r_refdef.scene.numentities || r_refdef.lightmapintensity <= 0.0f || r_shadows.integer <= 0) + if (!r_refdef.scene.numentities || r_refdef.scene.lightmapintensity <= 0.0f || r_shadows.integer <= 0) return; switch (r_shadow_shadowmode) @@ -5486,7 +5600,7 @@ void R_Shadow_PrepareModelShadows(void) return; } - size = 2 * r_shadow_shadowmapmaxsize; + size = r_shadow_shadowmaptexturesize / 4; scale = r_shadow_shadowmapping_precision.value * r_shadows_shadowmapscale.value; radius = 0.5f * size / scale; @@ -5584,7 +5698,7 @@ static void R_Shadow_DrawModelShadowMaps(void) VectorAdd(shadoworigin, r_refdef.view.origin, shadoworigin); dot1 = DotProduct(r_refdef.view.forward, shadowdir); dot2 = DotProduct(r_refdef.view.up, shadowdir); - if (fabs(dot1) <= fabs(dot2)) + if (fabs(dot1) <= fabs(dot2)) VectorMA(r_refdef.view.forward, -dot1, shadowdir, shadowforward); else VectorMA(r_refdef.view.up, -dot2, shadowdir, shadowforward); @@ -5626,7 +5740,7 @@ static void R_Shadow_DrawModelShadowMaps(void) relativeshadowmaxs[2] = relativelightorigin[2] + r_shadows_throwdistance.value * fabs(relativelightdirection[2]) + radius * (fabs(relativeforward[2]) + fabs(relativeright[2])); RSurf_ActiveModelEntity(ent, false, false, false); ent->model->DrawShadowMap(0, ent, relativelightorigin, relativelightdirection, relativethrowdistance, ent->model->nummodelsurfaces, ent->model->sortedmodelsurfaces, NULL, relativeshadowmins, relativeshadowmaxs); - rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity + rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity } #if 0 @@ -5644,7 +5758,7 @@ static void R_Shadow_DrawModelShadowMaps(void) Matrix4x4_Concat(&mvpmatrix, &r_refdef.view.viewport.projectmatrix, &r_refdef.view.viewport.viewmatrix); Matrix4x4_Invert_Full(&invmvpmatrix, &mvpmatrix); - Matrix4x4_CreateScale3(&scalematrix, size, -size, 1); + Matrix4x4_CreateScale3(&scalematrix, size, -size, 1); Matrix4x4_AdjustOrigin(&scalematrix, 0, size, -0.5f * bias); Matrix4x4_Concat(&texmatrix, &scalematrix, &shadowmatrix); Matrix4x4_Concat(&r_shadow_shadowmapmatrix, &texmatrix, &invmvpmatrix); @@ -5723,45 +5837,14 @@ void R_Shadow_DrawModelShadows(void) Matrix4x4_Transform3x3(&ent->inversematrix, shadowdir, relativelightdirection); else { - if(ent->entitynumber != 0) - { - if(ent->entitynumber >= MAX_EDICTS) // csqc entity - { - // FIXME handle this - VectorNegate(ent->modellight_lightdir, relativelightdirection); - } - else - { - // networked entity - might be attached in some way (then we should use the parent's light direction, to not tear apart attached entities) - int entnum, entnum2, recursion; - entnum = entnum2 = ent->entitynumber; - for(recursion = 32; recursion > 0; --recursion) - { - entnum2 = cl.entities[entnum].state_current.tagentity; - if(entnum2 >= 1 && entnum2 < cl.num_entities && cl.entities_active[entnum2]) - entnum = entnum2; - else - break; - } - if(recursion && recursion != 32) // if we followed a valid non-empty attachment chain - { - VectorNegate(cl.entities[entnum].render.modellight_lightdir, relativelightdirection); - // transform into modelspace of OUR entity - Matrix4x4_Transform3x3(&cl.entities[entnum].render.matrix, relativelightdirection, tmp); - Matrix4x4_Transform3x3(&ent->inversematrix, tmp, relativelightdirection); - } - else - VectorNegate(ent->modellight_lightdir, relativelightdirection); - } - } - else - VectorNegate(ent->modellight_lightdir, relativelightdirection); + VectorNegate(ent->render_modellight_lightdir, tmp); + Matrix4x4_Transform3x3(&ent->inversematrix, tmp, relativelightdirection); } VectorScale(relativelightdirection, -relativethrowdistance, relativelightorigin); RSurf_ActiveModelEntity(ent, false, false, false); ent->model->DrawShadowVolume(ent, relativelightorigin, relativelightdirection, relativethrowdistance, ent->model->nummodelsurfaces, ent->model->sortedmodelsurfaces, relativeshadowmins, relativeshadowmaxs); - rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity + rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity } // not really the right mode, but this will disable any silly stencil features @@ -5873,13 +5956,10 @@ static void R_DrawCorona(rtlight_t *rtlight, float cscale, float scale) { switch(vid.renderpath) { - case RENDERPATH_GL11: - case RENDERPATH_GL13: case RENDERPATH_GL20: case RENDERPATH_GLES1: case RENDERPATH_GLES2: #if defined(GL_SAMPLES_PASSED_ARB) && !defined(USE_GLES2) - CHECKGLERROR // See if we can use the GPU-side method to prevent implicit sync if (vid.support.arb_query_buffer_object) { #define BUFFER_OFFSET(i) ((GLint *)((unsigned char*)NULL + (i))) @@ -5894,13 +5974,23 @@ static void R_DrawCorona(rtlight_t *rtlight, float cscale, float scale) qglGetQueryObjectivARB(rtlight->corona_queryindex_allpixels, GL_QUERY_RESULT_ARB, BUFFER_OFFSET(4)); qglBindBufferBase(GL_UNIFORM_BUFFER, 0, r_shadow_occlusion_buf); occlude = MATERIALFLAG_OCCLUDE; - } else { - qglGetQueryObjectivARB(rtlight->corona_queryindex_visiblepixels, GL_QUERY_RESULT_ARB, &visiblepixels); - qglGetQueryObjectivARB(rtlight->corona_queryindex_allpixels, GL_QUERY_RESULT_ARB, &allpixels); - if (visiblepixels < 1 || allpixels < 1) - return; - rtlight->corona_visibility *= bound(0, (float)visiblepixels / (float)allpixels, 1); + cscale *= rtlight->corona_visibility; + CHECKGLERROR + break; } + // fallthrough +#else + return; +#endif + case RENDERPATH_GL11: + case RENDERPATH_GL13: +#if defined(GL_SAMPLES_PASSED_ARB) && !defined(USE_GLES2) + CHECKGLERROR + qglGetQueryObjectivARB(rtlight->corona_queryindex_visiblepixels, GL_QUERY_RESULT_ARB, &visiblepixels); + qglGetQueryObjectivARB(rtlight->corona_queryindex_allpixels, GL_QUERY_RESULT_ARB, &allpixels); + if (visiblepixels < 1 || allpixels < 1) + return; + rtlight->corona_visibility *= bound(0, (float)visiblepixels / (float)allpixels, 1); cscale *= rtlight->corona_visibility; CHECKGLERROR break; @@ -5925,8 +6015,7 @@ static void R_DrawCorona(rtlight_t *rtlight, float cscale, float scale) } else { - // FIXME: these traces should scan all render entities instead of cl.world - if (CL_TraceLine(r_refdef.view.origin, rtlight->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, SUPERCONTENTS_SKY, collision_extendmovelength.value, true, false, NULL, false, true).fraction < 1) + if (CL_Cache_TraceLineSurfaces(r_refdef.view.origin, rtlight->shadoworigin, MOVE_NORMAL, SUPERCONTENTS_SOLID, 0, MATERIALFLAGMASK_TRANSLUCENT).fraction < 1) return; } VectorScale(rtlight->currentcolor, cscale, color); @@ -5990,7 +6079,7 @@ void R_Shadow_DrawCoronas(void) qglGenQueriesARB(r_maxqueries - i, r_queries + i); CHECKGLERROR } - RSurf_ActiveWorldEntity(); + RSurf_ActiveModelEntity(r_refdef.scene.worldentity, false, false, false); GL_BlendFunc(GL_ONE, GL_ZERO); GL_CullFace(GL_NONE); GL_DepthMask(false); @@ -6259,7 +6348,7 @@ static void R_Shadow_SelectLightInView(void) if (rating >= 0.95) { rating /= (1 + 0.0625f * sqrt(DotProduct(temp, temp))); - if (bestrating < rating && CL_TraceLine(light->origin, r_refdef.view.origin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, SUPERCONTENTS_SKY, collision_extendmovelength.value, true, false, NULL, false, true).fraction == 1.0f) + if (bestrating < rating && CL_TraceLine(light->origin, r_refdef.view.origin, MOVE_NORMAL, NULL, SUPERCONTENTS_SOLID, 0, MATERIALFLAGMASK_TRANSLUCENT, collision_extendmovelength.value, true, false, NULL, false, true).fraction == 1.0f) { bestrating = rating; best = light; @@ -6709,7 +6798,7 @@ static void R_Shadow_SetCursorLocationForView(void) vec3_t dest, endpos; trace_t trace; VectorMA(r_refdef.view.origin, r_editlights_cursordistance.value, r_refdef.view.forward, dest); - trace = CL_TraceLine(r_refdef.view.origin, dest, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, SUPERCONTENTS_SKY, collision_extendmovelength.value, true, false, NULL, false, true); + trace = CL_TraceLine(r_refdef.view.origin, dest, MOVE_NORMAL, NULL, SUPERCONTENTS_SOLID, 0, MATERIALFLAGMASK_TRANSLUCENT, collision_extendmovelength.value, true, false, NULL, false, true); if (trace.fraction < 1) { dist = trace.fraction * r_editlights_cursordistance.value; @@ -7191,7 +7280,7 @@ void R_Shadow_EditLights_DrawSelectedLightProperties(void) return; x = vid_conwidth.value - 320; y = 5; - DrawQ_Pic(x-5, y-5, NULL, 250, 155, 0, 0, 0, 0.75, 0); + DrawQ_Pic(x-5, y-5, NULL, 250, 243, 0, 0, 0, 0.75, 0); lightnumber = -1; lightcount = 0; range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked @@ -7454,141 +7543,48 @@ LIGHT SAMPLING ============================================================================= */ -void R_LightPoint(float *color, const vec3_t p, const int flags) +void R_CompleteLightPoint(float *ambient, float *diffuse, float *lightdir, const vec3_t p, const int flags, float lightmapintensity, float ambientintensity) { - int i, numlights, flag; - float f, relativepoint[3], dist, dist2, lightradius2; - vec3_t diffuse, n; - rtlight_t *light; - dlight_t *dlight; - - if (r_fullbright.integer) - { - VectorSet(color, 1, 1, 1); - return; - } - - VectorClear(color); - - if (flags & LP_LIGHTMAP) - { - if (!r_fullbright.integer && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->lit && r_refdef.scene.worldmodel->brush.LightPoint) - { - VectorClear(diffuse); - r_refdef.scene.worldmodel->brush.LightPoint(r_refdef.scene.worldmodel, p, color, diffuse, n); - VectorAdd(color, diffuse, color); - } - else - VectorSet(color, 1, 1, 1); - color[0] += r_refdef.scene.ambient; - color[1] += r_refdef.scene.ambient; - color[2] += r_refdef.scene.ambient; - } - - if (flags & LP_RTWORLD) - { - flag = r_refdef.scene.rtworld ? LIGHTFLAG_REALTIMEMODE : LIGHTFLAG_NORMALMODE; - numlights = (int)Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); - for (i = 0; i < numlights; i++) - { - dlight = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, i); - if (!dlight) - continue; - light = &dlight->rtlight; - if (!(light->flags & flag)) - continue; - // sample - lightradius2 = light->radius * light->radius; - VectorSubtract(light->shadoworigin, p, relativepoint); - dist2 = VectorLength2(relativepoint); - if (dist2 >= lightradius2) - continue; - dist = sqrt(dist2) / light->radius; - f = dist < 1 ? (r_shadow_lightintensityscale.value * ((1.0f - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist))) : 0; - if (f <= 0) - continue; - // todo: add to both ambient and diffuse - if (!light->shadow || CL_TraceLine(p, light->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, SUPERCONTENTS_SKY, collision_extendmovelength.value, true, false, NULL, false, true).fraction == 1) - VectorMA(color, f, light->currentcolor, color); - } - } - if (flags & LP_DYNLIGHT) - { - // sample dlights - for (i = 0;i < r_refdef.scene.numlights;i++) - { - light = r_refdef.scene.lights[i]; - // sample - lightradius2 = light->radius * light->radius; - VectorSubtract(light->shadoworigin, p, relativepoint); - dist2 = VectorLength2(relativepoint); - if (dist2 >= lightradius2) - continue; - dist = sqrt(dist2) / light->radius; - f = dist < 1 ? (r_shadow_lightintensityscale.value * ((1.0f - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist))) : 0; - if (f <= 0) - continue; - // todo: add to both ambient and diffuse - if (!light->shadow || CL_TraceLine(p, light->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, SUPERCONTENTS_SKY, collision_extendmovelength.value, true, false, NULL, false, true).fraction == 1) - VectorMA(color, f, light->color, color); - } - } -} - -void R_CompleteLightPoint(vec3_t ambient, vec3_t diffuse, vec3_t lightdir, const vec3_t p, const int flags) -{ - int i, numlights, flag; + int i, numlights, flag, q; rtlight_t *light; dlight_t *dlight; float relativepoint[3]; float color[3]; - float dir[3]; float dist; float dist2; float intensity; - float sample[5*3]; + float sa[3], sx[3], sy[3], sz[3], sd[3]; float lightradius2; - if (r_fullbright.integer) - { - VectorSet(ambient, 1, 1, 1); - VectorClear(diffuse); - VectorClear(lightdir); - return; - } + // use first order spherical harmonics to combine directional lights + for (q = 0; q < 3; q++) + sa[q] = sx[q] = sy[q] = sz[q] = sd[q] = 0; - if (flags == LP_LIGHTMAP) + if (flags & LP_LIGHTMAP) { - VectorSet(ambient, r_refdef.scene.ambient, r_refdef.scene.ambient, r_refdef.scene.ambient); - VectorClear(diffuse); - VectorClear(lightdir); if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->lit && r_refdef.scene.worldmodel->brush.LightPoint) - r_refdef.scene.worldmodel->brush.LightPoint(r_refdef.scene.worldmodel, p, ambient, diffuse, lightdir); + { + float tempambient[3]; + for (q = 0; q < 3; q++) + tempambient[q] = color[q] = relativepoint[q] = 0; + r_refdef.scene.worldmodel->brush.LightPoint(r_refdef.scene.worldmodel, p, tempambient, color, relativepoint); + // calculate a weighted average light direction as well + intensity = VectorLength(color); + for (q = 0; q < 3; q++) + { + sa[q] += (0.5f * color[q] + tempambient[q]) * lightmapintensity; + sx[q] += (relativepoint[0] * color[q]) * lightmapintensity; + sy[q] += (relativepoint[1] * color[q]) * lightmapintensity; + sz[q] += (relativepoint[2] * color[q]) * lightmapintensity; + sd[q] += (intensity * relativepoint[q]) * lightmapintensity; + } + } else - VectorSet(ambient, 1, 1, 1); - return; - } - - memset(sample, 0, sizeof(sample)); - VectorSet(sample, r_refdef.scene.ambient, r_refdef.scene.ambient, r_refdef.scene.ambient); - - if ((flags & LP_LIGHTMAP) && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->lit && r_refdef.scene.worldmodel->brush.LightPoint) - { - vec3_t tempambient; - VectorClear(tempambient); - VectorClear(color); - VectorClear(relativepoint); - r_refdef.scene.worldmodel->brush.LightPoint(r_refdef.scene.worldmodel, p, tempambient, color, relativepoint); - VectorScale(tempambient, r_refdef.lightmapintensity, tempambient); - VectorScale(color, r_refdef.lightmapintensity, color); - VectorAdd(sample, tempambient, sample); - VectorMA(sample , 0.5f , color, sample ); - VectorMA(sample + 3, relativepoint[0], color, sample + 3); - VectorMA(sample + 6, relativepoint[1], color, sample + 6); - VectorMA(sample + 9, relativepoint[2], color, sample + 9); - // calculate a weighted average light direction as well - intensity = VectorLength(color); - VectorMA(sample + 12, intensity, relativepoint, sample + 12); + { + // unlit map - fullbright but scaled by lightmapintensity + for (q = 0; q < 3; q++) + sa[q] += lightmapintensity; + } } if (flags & LP_RTWORLD) @@ -7613,19 +7609,20 @@ void R_CompleteLightPoint(vec3_t ambient, vec3_t diffuse, vec3_t lightdir, const intensity = min(1.0f, (1.0f - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist)) * r_shadow_lightintensityscale.value; if (intensity <= 0.0f) continue; - if (light->shadow && CL_TraceLine(p, light->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, SUPERCONTENTS_SKY, collision_extendmovelength.value, true, false, NULL, false, true).fraction < 1) + if (light->shadow && CL_TraceLine(p, light->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, 0, MATERIALFLAGMASK_TRANSLUCENT, collision_extendmovelength.value, true, false, NULL, false, true).fraction < 1) continue; - // scale down intensity to add to both ambient and diffuse - //intensity *= 0.5f; + for (q = 0; q < 3; q++) + color[q] = light->currentcolor[q] * intensity; + intensity = VectorLength(color); VectorNormalize(relativepoint); - VectorScale(light->currentcolor, intensity, color); - VectorMA(sample , 0.5f , color, sample ); - VectorMA(sample + 3, relativepoint[0], color, sample + 3); - VectorMA(sample + 6, relativepoint[1], color, sample + 6); - VectorMA(sample + 9, relativepoint[2], color, sample + 9); - // calculate a weighted average light direction as well - intensity *= VectorLength(color); - VectorMA(sample + 12, intensity, relativepoint, sample + 12); + for (q = 0; q < 3; q++) + { + sa[q] += 0.5f * color[q]; + sx[q] += relativepoint[0] * color[q]; + sy[q] += relativepoint[1] * color[q]; + sz[q] += relativepoint[2] * color[q]; + sd[q] += intensity * relativepoint[q]; + } } // FIXME: sample bouncegrid too! } @@ -7646,32 +7643,32 @@ void R_CompleteLightPoint(vec3_t ambient, vec3_t diffuse, vec3_t lightdir, const intensity = (1.0f - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist) * r_shadow_lightintensityscale.value; if (intensity <= 0.0f) continue; - if (light->shadow && CL_TraceLine(p, light->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, SUPERCONTENTS_SKY, collision_extendmovelength.value, true, false, NULL, false, true).fraction < 1) + if (light->shadow && CL_TraceLine(p, light->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, 0, MATERIALFLAGMASK_TRANSLUCENT, collision_extendmovelength.value, true, false, NULL, false, true).fraction < 1) continue; - // scale down intensity to add to both ambient and diffuse - //intensity *= 0.5f; + for (q = 0; q < 3; q++) + color[q] = light->currentcolor[q] * intensity; + intensity = VectorLength(color); VectorNormalize(relativepoint); - VectorScale(light->currentcolor, intensity, color); - VectorMA(sample , 0.5f , color, sample ); - VectorMA(sample + 3, relativepoint[0], color, sample + 3); - VectorMA(sample + 6, relativepoint[1], color, sample + 6); - VectorMA(sample + 9, relativepoint[2], color, sample + 9); - // calculate a weighted average light direction as well - intensity *= VectorLength(color); - VectorMA(sample + 12, intensity, relativepoint, sample + 12); + for (q = 0; q < 3; q++) + { + sa[q] += 0.5f * color[q]; + sx[q] += relativepoint[0] * color[q]; + sy[q] += relativepoint[1] * color[q]; + sz[q] += relativepoint[2] * color[q]; + sd[q] += intensity * relativepoint[q]; + } } } - // calculate the direction we'll use to reduce the sample to a directional light source - VectorCopy(sample + 12, dir); - //VectorSet(dir, sample[3] + sample[4] + sample[5], sample[6] + sample[7] + sample[8], sample[9] + sample[10] + sample[11]); - VectorNormalize(dir); - // extract the diffuse color along the chosen direction and scale it - diffuse[0] = (dir[0]*sample[3] + dir[1]*sample[6] + dir[2]*sample[ 9] + sample[ 0]); - diffuse[1] = (dir[0]*sample[4] + dir[1]*sample[7] + dir[2]*sample[10] + sample[ 1]); - diffuse[2] = (dir[0]*sample[5] + dir[1]*sample[8] + dir[2]*sample[11] + sample[ 2]); - // subtract some of diffuse from ambient - VectorMA(sample, -0.333f, diffuse, ambient); - // store the normalized lightdir - VectorCopy(dir, lightdir); + // calculate the weighted-average light direction (bentnormal) + for (q = 0; q < 3; q++) + lightdir[q] = sd[q]; + VectorNormalize(lightdir); + for (q = 0; q < 3; q++) + { + // extract the diffuse color along the chosen direction and scale it + diffuse[q] = (lightdir[0] * sx[q] + lightdir[1] * sy[q] + lightdir[2] * sz[q]); + // subtract some of diffuse from ambient + ambient[q] = sa[q] + -0.333f * diffuse[q] + ambientintensity; + } }