diff options
author | SmileTheory <SmileTheory@gmail.com> | 2013-04-02 00:17:24 -0700 |
---|---|---|
committer | Tim Angus <tim@ngus.net> | 2013-05-03 16:07:36 +0100 |
commit | 4bc0dc5594ae2744090d27a5cea10a4f8e8b928d (patch) | |
tree | 21359a5f8bc6b503a3ec4d6f24602aba07631492 | |
parent | 3866046f497068d518fcb48ae9ba94c0bc7a0a72 (diff) |
Add sunlight/lightmap merging by multiply (r_sunlightMode 1) Merge additive sunlight and map lighting into one GLSL shader (r_sunlightMode 2) Change single lightmap blends to use lightall
-rw-r--r-- | src/renderergl2/glsl/lightall_fp.glsl | 182 | ||||
-rw-r--r-- | src/renderergl2/glsl/lightall_vp.glsl | 44 | ||||
-rw-r--r-- | src/renderergl2/glsl/shadowmask_fp.glsl | 2 | ||||
-rw-r--r-- | src/renderergl2/tr_glsl.c | 14 | ||||
-rw-r--r-- | src/renderergl2/tr_init.c | 8 | ||||
-rw-r--r-- | src/renderergl2/tr_local.h | 6 | ||||
-rw-r--r-- | src/renderergl2/tr_shade.c | 221 | ||||
-rw-r--r-- | src/renderergl2/tr_shader.c | 24 |
8 files changed, 195 insertions, 306 deletions
diff --git a/src/renderergl2/glsl/lightall_fp.glsl b/src/renderergl2/glsl/lightall_fp.glsl index ec9bb2dc..10f99946 100644 --- a/src/renderergl2/glsl/lightall_fp.glsl +++ b/src/renderergl2/glsl/lightall_fp.glsl @@ -27,11 +27,19 @@ uniform int u_TCGen0; #endif #if defined(USE_LIGHT_VECTOR) +uniform vec4 u_LightOrigin; uniform vec3 u_DirectedLight; uniform vec3 u_AmbientLight; uniform float u_LightRadius; #endif +#if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP) +uniform vec3 u_PrimaryLightColor; +uniform vec3 u_PrimaryLightAmbient; +uniform float u_PrimaryLightRadius; +#endif + + #if defined(USE_LIGHT) uniform vec2 u_MaterialInfo; #endif @@ -62,9 +70,14 @@ varying vec3 var_Bitangent; varying vec3 var_VertLight; #if defined(USE_LIGHT) && !defined(USE_DELUXEMAP) -varying vec3 var_WorldLight; +varying vec3 var_LightDirection; +#endif + +#if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP) +varying vec3 var_PrimaryLightDirection; #endif + #define EPSILON 0.00000001 #if defined(USE_PARALLAXMAP) @@ -199,14 +212,14 @@ float CalcSpecular(float NH, float NL, float NE, float EH, float fzero, float sh void main() { #if !defined(USE_FAST_LIGHT) && (defined(USE_LIGHT) || defined(USE_NORMALMAP)) - vec3 surfNormal = normalize(var_Normal); + vec3 surfN = normalize(var_Normal); #endif #if defined(USE_DELUXEMAP) - vec3 worldLight = 2.0 * texture2D(u_DeluxeMap, var_LightTex).xyz - vec3(1.0); - //worldLight += var_WorldLight * 0.0001; + vec3 L = 2.0 * texture2D(u_DeluxeMap, var_LightTex).xyz - vec3(1.0); + //L += var_LightDirection * 0.0001; #elif defined(USE_LIGHT) - vec3 worldLight = var_WorldLight; + vec3 L = var_LightDirection; #endif #if defined(USE_LIGHTMAP) @@ -214,101 +227,110 @@ void main() #if defined(RGBE_LIGHTMAP) lightSample.rgb *= exp2(lightSample.a * 255.0 - 128.0); #endif - vec3 directedLight = lightSample.rgb; + vec3 lightColor = lightSample.rgb; #elif defined(USE_LIGHT_VECTOR) && !defined(USE_FAST_LIGHT) #if defined(USE_INVSQRLIGHT) - float intensity = 1.0 / dot(worldLight, worldLight); + float intensity = 1.0 / dot(L, L); #else - float intensity = clamp((1.0 - dot(worldLight, worldLight) / (u_LightRadius * u_LightRadius)) * 1.07, 0.0, 1.0); + float intensity = clamp((1.0 - dot(L, L) / (u_LightRadius * u_LightRadius)) * 1.07, 0.0, 1.0); #endif - vec3 directedLight = u_DirectedLight * intensity; - vec3 ambientLight = u_AmbientLight; - - #if defined(USE_SHADOWMAP) - vec2 shadowTex = gl_FragCoord.xy * r_FBufScale; - directedLight *= texture2D(u_ShadowMap, shadowTex).r; - #endif + vec3 lightColor = u_DirectedLight * intensity; + vec3 ambientColor = u_AmbientLight; #elif defined(USE_LIGHT_VERTEX) && !defined(USE_FAST_LIGHT) - vec3 directedLight = var_VertLight; + vec3 lightColor = var_VertLight; #endif #if defined(USE_TCGEN) || defined(USE_NORMALMAP) || (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) - vec3 SampleToView = normalize(var_SampleToView); + vec3 E = normalize(var_SampleToView); #endif - vec2 tex = var_DiffuseTex; + vec2 texCoords = var_DiffuseTex; float ambientDiff = 1.0; #if defined(USE_NORMALMAP) #if defined(USE_VERT_TANGENT_SPACE) - vec3 tangent = var_Tangent; - vec3 bitangent = var_Bitangent; + mat3 tangentToWorld = mat3(var_Tangent, var_Bitangent, var_Normal); #else vec3 q0 = dFdx(var_Position); vec3 q1 = dFdy(var_Position); - vec2 st0 = dFdx(tex); - vec2 st1 = dFdy(tex); + vec2 st0 = dFdx(texCoords); + vec2 st1 = dFdy(texCoords); float dir = sign(st1.t * st0.s - st0.t * st1.s); - vec3 tangent = normalize( q0 * st1.t - q1 * st0.t) * dir; - vec3 bitangent = -normalize( q0 * st1.s - q1 * st0.s) * dir; - #endif + vec3 tangent = normalize(q0 * st1.t - q1 * st0.t) * dir; + vec3 bitangent = -normalize(q0 * st1.s - q1 * st0.s) * dir; mat3 tangentToWorld = mat3(tangent, bitangent, var_Normal); + #endif #if defined(USE_PARALLAXMAP) - vec3 offsetDir = normalize(SampleToView * tangentToWorld); - #if 0 - float height = SampleHeight(u_NormalMap, tex); - float pdist = 0.05 * height - (0.05 / 2.0); - #else + vec3 offsetDir = normalize(E * tangentToWorld); offsetDir.xy *= -0.05 / offsetDir.z; - float pdist = RayIntersectDisplaceMap(tex, offsetDir.xy, u_NormalMap); - #endif - tex += offsetDir.xy * pdist; + + texCoords += offsetDir.xy * RayIntersectDisplaceMap(texCoords, offsetDir.xy, u_NormalMap); #endif + vec3 texN; #if defined(SWIZZLE_NORMALMAP) - vec3 normal = 2.0 * texture2D(u_NormalMap, tex).agb - 1.0; + texN.xy = 2.0 * texture2D(u_NormalMap, texCoords).ag - 1.0; #else - vec3 normal = 2.0 * texture2D(u_NormalMap, tex).rgb - 1.0; + texN.xy = 2.0 * texture2D(u_NormalMap, texCoords).rg - 1.0; #endif - normal.z = sqrt(clamp(1.0 - dot(normal.xy, normal.xy), 0.0, 1.0)); - vec3 worldNormal = tangentToWorld * normal; + texN.z = sqrt(clamp(1.0 - dot(texN.xy, texN.xy), 0.0, 1.0)); + vec3 N = tangentToWorld * texN; #if defined(r_normalAmbient) - ambientDiff = 0.781341 * normal.z + 0.218659; + ambientDiff = 0.781341 * texN.z + 0.218659; #endif -#elif defined(USE_LIGHT) && !defined(USE_FAST_LIGHT) - vec3 worldNormal = surfNormal; +#elif defined(USE_LIGHT) && !defined(USE_FAST_LIGHT) + vec3 N = surfN; #endif #if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || (defined(USE_TCGEN) && defined(USE_NORMALMAP)) - worldNormal = normalize(worldNormal); + N = normalize(N); #endif #if defined(USE_TCGEN) && defined(USE_NORMALMAP) if (u_TCGen0 == TCGEN_ENVIRONMENT_MAPPED) { - tex = -reflect(normalize(SampleToView), worldNormal).yz * vec2(0.5, -0.5) + 0.5; + texCoords = -reflect(E, N).yz * vec2(0.5, -0.5) + 0.5; } #endif - vec4 diffuse = texture2D(u_DiffuseMap, tex); + vec4 diffuse = texture2D(u_DiffuseMap, texCoords); #if defined(USE_LIGHT) && defined(USE_FAST_LIGHT) #if defined(USE_LIGHTMAP) - diffuse.rgb *= directedLight; + diffuse.rgb *= lightColor; #endif #elif defined(USE_LIGHT) - worldLight = normalize(worldLight); + L = normalize(L); - float surfNL = clamp(dot(surfNormal, worldLight), 0.0, 1.0); + float surfNL = clamp(dot(surfN, L), 0.0, 1.0); + + #if defined(USE_SHADOWMAP) + vec2 shadowTex = gl_FragCoord.xy * r_FBufScale; + float shadowValue = texture2D(u_ShadowMap, shadowTex).r; + + // surfaces not facing the light are always shadowed + shadowValue *= step(0.0, dot(surfN, var_PrimaryLightDirection)); + + #if defined(SHADOWMAP_MODULATE) + //vec3 shadowColor = min(u_PrimaryLightAmbient, lightColor); + vec3 shadowColor = u_PrimaryLightAmbient * lightColor; + + #if 0 + // Only shadow when the world light is parallel to the primary light + shadowValue = 1.0 + (shadowValue - 1.0) * clamp(dot(L, var_PrimaryLightDirection), 0.0, 1.0); + #endif + lightColor = mix(shadowColor, lightColor, shadowValue); + #endif + #endif #if defined(USE_LIGHTMAP) || defined(USE_LIGHT_VERTEX) #if defined(USE_STANDARD_DELUXEMAP) // Standard deluxe mapping treats the light sample as fully directed // and doesn't compensate for light angle attenuation. - vec3 ambientLight = vec3(0.0); + vec3 ambientColor = vec3(0.0); #else // Separate the light sample into directed and ambient parts. // @@ -332,45 +354,71 @@ void main() #endif // Recover any unused light as ambient - vec3 ambientLight = directedLight; - directedLight *= directedScale; - ambientLight -= directedLight * surfNL; + vec3 ambientColor = lightColor; + lightColor *= directedScale; + ambientColor -= lightColor * surfNL; #endif #endif - - float NL = clamp(dot(worldNormal, worldLight), 0.0, 1.0); - float NE = clamp(dot(worldNormal, SampleToView), 0.0, 1.0); + + float NL = clamp(dot(N, L), 0.0, 1.0); + float NE = clamp(dot(N, E), 0.0, 1.0); float fzero = u_MaterialInfo.x; float shininess = u_MaterialInfo.y; #if defined(USE_SPECULARMAP) - vec4 specular = texture2D(u_SpecularMap, tex); + vec4 specular = texture2D(u_SpecularMap, texCoords); //specular.rgb = clamp(specular.rgb - diffuse.rgb, 0.0, 1.0); shininess *= specular.a; #endif - float directedDiff = NL * CalcDiffuse(worldNormal, worldLight, SampleToView, NE, NL, fzero, shininess); - diffuse.rgb *= directedLight * directedDiff + ambientDiff * ambientLight; + float diffuseIntensity = NL * CalcDiffuse(N, L, E, NE, NL, fzero, shininess); + #if defined(USE_PRIMARY_LIGHT) + vec3 L2 = var_PrimaryLightDirection; + + float NL2 = clamp(dot(N, L2), 0.0, 1.0); + float diffuseIntensity2 = NL2 * CalcDiffuse(N, L2, E, NE, NL2, fzero, shininess); + #if defined(USE_SHADOWMAP) + diffuseIntensity2 *= shadowValue; + #endif + + diffuse.rgb *= lightColor * diffuseIntensity + u_PrimaryLightColor * diffuseIntensity2 + ambientDiff * ambientColor; + #else + diffuse.rgb *= lightColor * diffuseIntensity + ambientDiff * ambientColor; + #endif #if defined(USE_SPECULARMAP) - vec3 halfAngle = normalize(worldLight + SampleToView); + vec3 H = normalize(L + E); - float EH = clamp(dot(SampleToView, halfAngle), 0.0, 1.0); - float NH = clamp(dot(worldNormal, halfAngle), 0.0, 1.0); + float EH = clamp(dot(E, H), 0.0, 1.0); + float NH = clamp(dot(N, H), 0.0, 1.0); - float directedSpec = NL * CalcSpecular(NH, NL, NE, EH, fzero, shininess); + float specularIntensity = NL * CalcSpecular(NH, NL, NE, EH, fzero, shininess); #if defined(r_normalAmbient) - vec3 ambientHalf = normalize(surfNormal + SampleToView); - float ambientSpec = max(dot(ambientHalf, worldNormal) + 0.5, 0.0); + vec3 ambientHalf = normalize(surfN + E); + float ambientSpec = max(dot(ambientHalf, N) + 0.5, 0.0); ambientSpec *= ambientSpec * 0.44; ambientSpec = pow(ambientSpec, shininess) * fzero; - specular.rgb *= directedSpec * directedLight + ambientSpec * ambientLight; - #else - specular.rgb *= directedSpec * directedLight; - #endif - #endif + #else + float ambientSpec = 0.0; + #endif + #if defined(USE_PRIMARY_LIGHT) + vec3 H2 = normalize(L2 + E); + float EH2 = clamp(dot(E, H2), 0.0, 1.0); + float NH2 = clamp(dot(N, H2), 0.0, 1.0); + + float specularIntensity2 = NL * CalcSpecular(NH2, NL2, NE, EH2, fzero, shininess); + + #if defined(USE_SHADOWMAP) + specularIntensity2 *= shadowValue; + #endif + + specular.rgb *= specularIntensity * lightColor + specularIntensity2 * u_PrimaryLightColor + ambientSpec * ambientColor; + #else + specular.rgb *= specularIntensity * lightColor + ambientSpec * ambientColor; + #endif + #endif #endif gl_FragColor = diffuse; diff --git a/src/renderergl2/glsl/lightall_vp.glsl b/src/renderergl2/glsl/lightall_vp.glsl index 4604b6ed..03775caf 100644 --- a/src/renderergl2/glsl/lightall_vp.glsl +++ b/src/renderergl2/glsl/lightall_vp.glsl @@ -61,6 +61,10 @@ uniform float u_LightRadius; #endif #endif +#if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP) +uniform vec4 u_PrimaryLightOrigin; +#endif + varying vec2 var_DiffuseTex; #if defined(USE_LIGHTMAP) @@ -91,7 +95,11 @@ varying vec3 var_VertLight; #endif #if defined(USE_LIGHT) && !defined(USE_DELUXEMAP) && !defined(USE_FAST_LIGHT) -varying vec3 var_WorldLight; +varying vec3 var_LightDirection; +#endif + +#if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP) +varying vec3 var_PrimaryLightDirection; #endif #if defined(USE_TCGEN) @@ -124,7 +132,7 @@ vec2 ModTexCoords(vec2 st, vec3 position, vec4 texMatrix, vec4 offTurb) float phase = offTurb.w; vec2 st2 = vec2(dot(st, texMatrix.xz), dot(st, texMatrix.yw)) + offTurb.xy; - vec3 offsetPos = position / 1024.0; + vec3 offsetPos = vec3(0); //position / 1024.0; offsetPos.x += offsetPos.z; vec2 texOffset = sin((offsetPos.xy + vec2(phase)) * 2.0 * M_PI); @@ -155,7 +163,7 @@ void main() gl_Position = u_ModelViewProjectionMatrix * position; #if (defined(USE_LIGHTMAP) || defined(USE_LIGHT_VERTEX)) && !defined(USE_DELUXEMAP) && !defined(USE_FAST_LIGHT) - vec3 worldLight = attr_LightDirection; + vec3 L = attr_LightDirection; #endif #if defined(USE_MODELMATRIX) @@ -167,7 +175,7 @@ void main() #endif #if defined(USE_LIGHTMAP) && !defined(USE_DELUXEMAP) && !defined(USE_FAST_LIGHT) - worldLight = (u_ModelMatrix * vec4(worldLight, 0.0)).xyz; + L = (u_ModelMatrix * vec4(L, 0.0)).xyz; #endif #endif @@ -175,24 +183,20 @@ void main() var_Position = position.xyz; #endif -#if defined(USE_TCGEN) || defined(USE_NORMALMAP) || defined(USE_LIGHT) && !defined(USE_FAST_LIGHT) - vec3 SampleToView = u_ViewOrigin - position.xyz; -#endif - #if defined(USE_TCGEN) || defined(USE_NORMALMAP) || (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) - var_SampleToView = SampleToView; + var_SampleToView = u_ViewOrigin - position.xyz; #endif #if defined(USE_TCGEN) - vec2 tex = GenTexCoords(u_TCGen0, position.xyz, normal, u_TCGen0Vector0, u_TCGen0Vector1); + vec2 texCoords = GenTexCoords(u_TCGen0, position.xyz, normal, u_TCGen0Vector0, u_TCGen0Vector1); #else - vec2 tex = attr_TexCoord0.st; + vec2 texCoords = attr_TexCoord0.st; #endif #if defined(USE_TCMOD) - var_DiffuseTex = ModTexCoords(tex, position.xyz, u_DiffuseTexMatrix, u_DiffuseTexOffTurb); + var_DiffuseTex = ModTexCoords(texCoords, position.xyz, u_DiffuseTexMatrix, u_DiffuseTexOffTurb); #else - var_DiffuseTex = tex; + var_DiffuseTex = texCoords; #endif #if defined(USE_LIGHTMAP) @@ -209,10 +213,10 @@ void main() #if defined(USE_LIGHT) && !defined(USE_DELUXEMAP) #if defined(USE_LIGHT_VECTOR) - vec3 worldLight = u_LightOrigin.xyz - (position.xyz * u_LightOrigin.w); + vec3 L = u_LightOrigin.xyz - (position.xyz * u_LightOrigin.w); #endif #if !defined(USE_FAST_LIGHT) - var_WorldLight = worldLight; + var_LightDirection = L; #endif #endif @@ -226,12 +230,16 @@ void main() #if defined(USE_LIGHT_VECTOR) && defined(USE_FAST_LIGHT) #if defined(USE_INVSQRLIGHT) - float intensity = 1.0 / dot(worldLight, worldLight); + float intensity = 1.0 / dot(L, L); #else - float intensity = clamp((1.0 - dot(worldLight, worldLight) / (u_LightRadius * u_LightRadius)) * 1.07, 0.0, 1.0); + float intensity = clamp((1.0 - dot(L, L) / (u_LightRadius * u_LightRadius)) * 1.07, 0.0, 1.0); #endif - float NL = clamp(dot(normal, normalize(worldLight)), 0.0, 1.0); + float NL = clamp(dot(normal, normalize(L)), 0.0, 1.0); var_Color.rgb *= u_DirectedLight * intensity * NL + u_AmbientLight; #endif + +#if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP) + var_PrimaryLightDirection = u_PrimaryLightOrigin.xyz - (position.xyz * u_PrimaryLightOrigin.w); +#endif } diff --git a/src/renderergl2/glsl/shadowmask_fp.glsl b/src/renderergl2/glsl/shadowmask_fp.glsl index b3a698c8..4bac5ccd 100644 --- a/src/renderergl2/glsl/shadowmask_fp.glsl +++ b/src/renderergl2/glsl/shadowmask_fp.glsl @@ -86,7 +86,7 @@ void main() vec4 shadowpos = u_ShadowMvp * biasPos; #if defined(USE_SHADOW_CASCADE) - const float fadeTo = 0.5; + const float fadeTo = 1.0; result = fadeTo; #else result = 0.0; diff --git a/src/renderergl2/tr_glsl.c b/src/renderergl2/tr_glsl.c index 163da908..7b9b180c 100644 --- a/src/renderergl2/tr_glsl.c +++ b/src/renderergl2/tr_glsl.c @@ -131,7 +131,12 @@ static uniformInfo_t uniformsInfo[] = { "u_InvTexRes", GLSL_VEC2 }, { "u_AutoExposureMinMax", GLSL_VEC2 }, - { "u_ToneMinAvgMaxLinear", GLSL_VEC3 } + { "u_ToneMinAvgMaxLinear", GLSL_VEC3 }, + + { "u_PrimaryLightOrigin", GLSL_VEC4 }, + { "u_PrimaryLightColor", GLSL_VEC3 }, + { "u_PrimaryLightAmbient", GLSL_VEC3 }, + { "u_PrimaryLightRadius", GLSL_FLOAT } }; @@ -1106,8 +1111,15 @@ void GLSL_InitGPUShaders(void) Q_strcat(extradefines, 1024, "#define USE_PARALLAXMAP\n"); if (i & LIGHTDEF_USE_SHADOWMAP) + { Q_strcat(extradefines, 1024, "#define USE_SHADOWMAP\n"); + if (r_sunlightMode->integer == 1) + Q_strcat(extradefines, 1024, "#define SHADOWMAP_MODULATE\n"); + else if (r_sunlightMode->integer == 2) + Q_strcat(extradefines, 1024, "#define USE_PRIMARY_LIGHT\n"); + } + if (i & LIGHTDEF_USE_TCGEN_AND_TCMOD) { Q_strcat(extradefines, 1024, "#define USE_TCGEN\n"); diff --git a/src/renderergl2/tr_init.c b/src/renderergl2/tr_init.c index 84d09b1d..a011bba4 100644 --- a/src/renderergl2/tr_init.c +++ b/src/renderergl2/tr_init.c @@ -145,6 +145,7 @@ cvar_t *r_forceSun; cvar_t *r_forceSunMapLightScale; cvar_t *r_forceSunLightScale; cvar_t *r_forceSunAmbientScale; +cvar_t *r_sunlightMode; cvar_t *r_drawSunRays; cvar_t *r_sunShadows; cvar_t *r_shadowFilter; @@ -1125,10 +1126,11 @@ void R_Register( void ) r_genNormalMaps = ri.Cvar_Get( "r_genNormalMaps", "0", CVAR_ARCHIVE | CVAR_LATCH ); r_forceSun = ri.Cvar_Get( "r_forceSun", "0", CVAR_CHEAT ); - r_forceSunMapLightScale = ri.Cvar_Get( "r_forceSunMapLightScale", "0.5", CVAR_CHEAT ); - r_forceSunLightScale = ri.Cvar_Get( "r_forceSunLightScale", "0.5", CVAR_CHEAT ); - r_forceSunAmbientScale = ri.Cvar_Get( "r_forceSunAmbientScale", "0.2", CVAR_CHEAT ); + r_forceSunMapLightScale = ri.Cvar_Get( "r_forceSunMapLightScale", "1.0", CVAR_CHEAT ); + r_forceSunLightScale = ri.Cvar_Get( "r_forceSunLightScale", "1.0", CVAR_CHEAT ); + r_forceSunAmbientScale = ri.Cvar_Get( "r_forceSunAmbientScale", "0.75", CVAR_CHEAT ); r_drawSunRays = ri.Cvar_Get( "r_drawSunRays", "0", CVAR_ARCHIVE | CVAR_LATCH ); + r_sunlightMode = ri.Cvar_Get( "r_sunlightMode", "1", CVAR_ARCHIVE | CVAR_LATCH ); r_sunShadows = ri.Cvar_Get( "r_sunShadows", "1", CVAR_ARCHIVE | CVAR_LATCH ); r_shadowFilter = ri.Cvar_Get( "r_shadowFilter", "1", CVAR_ARCHIVE | CVAR_LATCH ); diff --git a/src/renderergl2/tr_local.h b/src/renderergl2/tr_local.h index c550ca18..96f657e4 100644 --- a/src/renderergl2/tr_local.h +++ b/src/renderergl2/tr_local.h @@ -791,6 +791,11 @@ typedef enum UNIFORM_AUTOEXPOSUREMINMAX, UNIFORM_TONEMINAVGMAXLINEAR, + UNIFORM_PRIMARYLIGHTORIGIN, + UNIFORM_PRIMARYLIGHTCOLOR, + UNIFORM_PRIMARYLIGHTAMBIENT, + UNIFORM_PRIMARYLIGHTRADIUS, + UNIFORM_COUNT } uniform_t; @@ -1972,6 +1977,7 @@ extern cvar_t *r_forceSun; extern cvar_t *r_forceSunMapLightScale; extern cvar_t *r_forceSunLightScale; extern cvar_t *r_forceSunAmbientScale; +extern cvar_t *r_sunlightMode; extern cvar_t *r_drawSunRays; extern cvar_t *r_sunShadows; extern cvar_t *r_shadowFilter; diff --git a/src/renderergl2/tr_shade.c b/src/renderergl2/tr_shade.c index fbf11d6a..efbe69a3 100644 --- a/src/renderergl2/tr_shade.c +++ b/src/renderergl2/tr_shade.c @@ -879,209 +879,6 @@ static void ForwardDlight( void ) { } -static void ForwardSunlight( void ) { -// int l; - //vec3_t origin; - //float scale; - int stage; - int stageGlState[2]; - qboolean alphaOverride = qfalse; - - int deformGen; - vec5_t deformParams; - - vec4_t fogDistanceVector, fogDepthVector = {0, 0, 0, 0}; - float eyeT = 0; - - shaderCommands_t *input = &tess; - - ComputeDeformValues(&deformGen, deformParams); - - ComputeFogValues(fogDistanceVector, fogDepthVector, &eyeT); - - // deal with vertex alpha blended surfaces - if (input->xstages[0] && input->xstages[1] && - (input->xstages[1]->alphaGen == AGEN_VERTEX || input->xstages[1]->alphaGen == AGEN_ONE_MINUS_VERTEX)) - { - stageGlState[0] = input->xstages[0]->stateBits & (GLS_SRCBLEND_BITS | GLS_DSTBLEND_BITS); - - if (stageGlState[0] == 0 || stageGlState[0] == (GLS_SRCBLEND_ONE | GLS_DSTBLEND_ZERO)) - { - stageGlState[1] = input->xstages[1]->stateBits & (GLS_SRCBLEND_BITS | GLS_DSTBLEND_BITS); - - if (stageGlState[1] == (GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA)) - { - alphaOverride = qtrue; - stageGlState[0] = GLS_SRCBLEND_ONE_MINUS_SRC_ALPHA | GLS_DSTBLEND_ONE | GLS_DEPTHFUNC_EQUAL; - stageGlState[1] = GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE | GLS_DEPTHFUNC_EQUAL; - } - else if (stageGlState[1] == (GLS_SRCBLEND_ONE_MINUS_SRC_ALPHA | GLS_DSTBLEND_SRC_ALPHA)) - { - alphaOverride = qtrue; - stageGlState[0] = GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE | GLS_DEPTHFUNC_EQUAL; - stageGlState[1] = GLS_SRCBLEND_ONE_MINUS_SRC_ALPHA | GLS_DSTBLEND_ONE | GLS_DEPTHFUNC_EQUAL; - } - } - } - - if (!alphaOverride) - { - stageGlState[0] = - stageGlState[1] = GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE | GLS_DEPTHFUNC_EQUAL; - } - - for ( stage = 0; stage < 2 /*MAX_SHADER_STAGES */; stage++ ) - { - shaderStage_t *pStage = input->xstages[stage]; - shaderProgram_t *sp; - vec4_t vector; - matrix_t matrix; - - if ( !pStage ) - { - break; - } - - //VectorCopy( dl->transformed, origin ); - - //if (pStage->glslShaderGroup == tr.lightallShader) - { - int index = pStage->glslShaderIndex; - - index &= ~(LIGHTDEF_LIGHTTYPE_MASK | LIGHTDEF_USE_DELUXEMAP); - index |= LIGHTDEF_USE_LIGHT_VECTOR | LIGHTDEF_USE_SHADOWMAP; - - if (backEnd.currentEntity && backEnd.currentEntity != &tr.worldEntity) - { - index |= LIGHTDEF_ENTITY; - } - - sp = &tr.lightallShader[index]; - } - - backEnd.pc.c_lightallDraws++; - - GLSL_BindProgram(sp); - - GLSL_SetUniformMatrix16(sp, UNIFORM_MODELVIEWPROJECTIONMATRIX, glState.modelviewProjection); - GLSL_SetUniformVec3(sp, UNIFORM_VIEWORIGIN, backEnd.viewParms.or.origin); - - GLSL_SetUniformFloat(sp, UNIFORM_VERTEXLERP, glState.vertexAttribsInterpolation); - - GLSL_SetUniformInt(sp, UNIFORM_DEFORMGEN, deformGen); - if (deformGen != DGEN_NONE) - { - GLSL_SetUniformFloat5(sp, UNIFORM_DEFORMPARAMS, deformParams); - GLSL_SetUniformFloat(sp, UNIFORM_TIME, tess.shaderTime); - } - - if ( input->fogNum ) { - vec4_t fogColorMask; - - GLSL_SetUniformVec4(sp, UNIFORM_FOGDISTANCE, fogDistanceVector); - GLSL_SetUniformVec4(sp, UNIFORM_FOGDEPTH, fogDepthVector); - GLSL_SetUniformFloat(sp, UNIFORM_FOGEYET, eyeT); - - ComputeFogColorMask(pStage, fogColorMask); - - GLSL_SetUniformVec4(sp, UNIFORM_FOGCOLORMASK, fogColorMask); - } - - { - vec4_t baseColor; - vec4_t vertColor; - - ComputeShaderColors(pStage, baseColor, vertColor); - - if (alphaOverride) - { - if (input->xstages[1]->alphaGen == AGEN_VERTEX) - { - baseColor[3] = 0.0f; - vertColor[3] = 1.0f; - } - else if (input->xstages[1]->alphaGen == AGEN_ONE_MINUS_VERTEX) - { - baseColor[3] = 1.0f; - vertColor[3] = -1.0f; - } - } - - GLSL_SetUniformVec4(sp, UNIFORM_BASECOLOR, baseColor); - GLSL_SetUniformVec4(sp, UNIFORM_VERTCOLOR, vertColor); - } - - if (pStage->alphaGen == AGEN_PORTAL) - { - GLSL_SetUniformFloat(sp, UNIFORM_PORTALRANGE, tess.shader->portalRange); - } - - GLSL_SetUniformInt(sp, UNIFORM_COLORGEN, pStage->rgbGen); - GLSL_SetUniformInt(sp, UNIFORM_ALPHAGEN, pStage->alphaGen); - - GLSL_SetUniformVec3(sp, UNIFORM_DIRECTEDLIGHT, backEnd.refdef.sunCol); - GLSL_SetUniformVec3(sp, UNIFORM_AMBIENTLIGHT, backEnd.refdef.sunAmbCol); - - GLSL_SetUniformVec4(sp, UNIFORM_LIGHTORIGIN, backEnd.refdef.sunDir); - - GLSL_SetUniformFloat(sp, UNIFORM_LIGHTRADIUS, 9999999999.9f); - - GLSL_SetUniformVec2(sp, UNIFORM_MATERIALINFO, pStage->materialInfo); - - GL_State( stageGlState[stage] ); - - GLSL_SetUniformMatrix16(sp, UNIFORM_MODELMATRIX, backEnd.or.transformMatrix); - - if (pStage->bundle[TB_DIFFUSEMAP].image[0]) - R_BindAnimatedImageToTMU( &pStage->bundle[TB_DIFFUSEMAP], TB_DIFFUSEMAP); - - if (pStage->bundle[TB_NORMALMAP].image[0]) - R_BindAnimatedImageToTMU( &pStage->bundle[TB_NORMALMAP], TB_NORMALMAP); - - if (pStage->bundle[TB_SPECULARMAP].image[0]) - R_BindAnimatedImageToTMU( &pStage->bundle[TB_SPECULARMAP], TB_SPECULARMAP); - - /* - { - GL_BindToTMU(tr.sunShadowDepthImage[0], TB_SHADOWMAP); - GL_BindToTMU(tr.sunShadowDepthImage[1], TB_SHADOWMAP2); - GL_BindToTMU(tr.sunShadowDepthImage[2], TB_SHADOWMAP3); - GLSL_SetUniformMatrix16(sp, UNIFORM_SHADOWMVP, backEnd.refdef.sunShadowMvp[0]); - GLSL_SetUniformMatrix16(sp, UNIFORM_SHADOWMVP2, backEnd.refdef.sunShadowMvp[1]); - GLSL_SetUniformMatrix16(sp, UNIFORM_SHADOWMVP3, backEnd.refdef.sunShadowMvp[2]); - } - */ - GL_BindToTMU(tr.screenShadowImage, TB_SHADOWMAP); - - ComputeTexMatrix( pStage, TB_DIFFUSEMAP, matrix ); - - VectorSet4(vector, matrix[0], matrix[1], matrix[4], matrix[5]); - GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX, vector); - - VectorSet4(vector, matrix[8], matrix[9], matrix[12], matrix[13]); - GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXOFFTURB, vector); - - GLSL_SetUniformInt(sp, UNIFORM_TCGEN0, pStage->bundle[0].tcGen); - - // - // draw - // - - if (input->multiDrawPrimitives) - { - R_DrawMultiElementsVBO(input->multiDrawPrimitives, input->multiDrawMinIndex, input->multiDrawMaxIndex, input->multiDrawNumIndexes, input->multiDrawFirstIndex); - } - else - { - R_DrawElementsVBO(input->numIndexes, input->firstIndex, input->minIndex, input->maxIndex); - } - - backEnd.pc.c_totalIndexes += tess.numIndexes; - backEnd.pc.c_dlightIndexes += tess.numIndexes; - } -} - - static void ProjectPshadowVBOGLSL( void ) { int l; vec3_t origin; @@ -1330,7 +1127,7 @@ static void RB_IterateStagesGeneric( shaderCommands_t *input ) index |= LIGHTDEF_ENTITY; } - if ((backEnd.viewParms.flags & VPF_USESUNLIGHT) && ((index & LIGHTDEF_USE_LIGHTMAP) || (index & LIGHTDEF_USE_LIGHT_VERTEX))) + if (r_sunlightMode->integer && (backEnd.viewParms.flags & VPF_USESUNLIGHT) && (index & LIGHTDEF_LIGHTTYPE_MASK)) { index |= LIGHTDEF_USE_SHADOWMAP; } @@ -1498,12 +1295,12 @@ static void RB_IterateStagesGeneric( shaderCommands_t *input ) { int i; - if ((backEnd.viewParms.flags & VPF_USESUNLIGHT) && ((pStage->glslShaderIndex & LIGHTDEF_USE_LIGHTMAP) || (pStage->glslShaderIndex & LIGHTDEF_USE_LIGHT_VERTEX))) + if ((backEnd.viewParms.flags & VPF_USESUNLIGHT) && (pStage->glslShaderIndex & LIGHTDEF_LIGHTTYPE_MASK)) { GL_BindToTMU(tr.screenShadowImage, TB_SHADOWMAP); - GLSL_SetUniformVec3(sp, GENERIC_UNIFORM_AMBIENTLIGHT, backEnd.refdef.sunAmbCol); - GLSL_SetUniformVec3(sp, GENERIC_UNIFORM_DIRECTEDLIGHT, backEnd.refdef.sunCol); - GLSL_SetUniformVec4(sp, GENERIC_UNIFORM_LIGHTORIGIN, backEnd.refdef.sunDir); + GLSL_SetUniformVec3(sp, UNIFORM_PRIMARYLIGHTAMBIENT, backEnd.refdef.sunAmbCol); + GLSL_SetUniformVec3(sp, UNIFORM_PRIMARYLIGHTCOLOR, backEnd.refdef.sunCol); + GLSL_SetUniformVec4(sp, UNIFORM_PRIMARYLIGHTORIGIN, backEnd.refdef.sunDir); } if ((r_lightmap->integer == 1 || r_lightmap->integer == 2) && pStage->bundle[TB_LIGHTMAP].image[0]) @@ -1796,14 +1593,6 @@ void RB_StageIteratorGeneric( void ) } } -#if 0 - if ((backEnd.viewParms.flags & VPF_USESUNLIGHT) && tess.shader->sort <= SS_OPAQUE - //if ((tr.sunShadows || r_forceSunlight->value > 0.0f) && tess.shader->sort <= SS_OPAQUE - && !(tess.shader->surfaceFlags & (SURF_NODLIGHT | SURF_SKY) ) && tess.xstages[0]->glslShaderGroup == tr.lightallShader) { - ForwardSunlight(); - } -#endif - // // now do fog // diff --git a/src/renderergl2/tr_shader.c b/src/renderergl2/tr_shader.c index 6a42f4cb..7eebf787 100644 --- a/src/renderergl2/tr_shader.c +++ b/src/renderergl2/tr_shader.c @@ -2494,6 +2494,30 @@ static qboolean CollapseStagesToGLSL(void) if (numStages == i && i >= 2 && CollapseMultitexture()) numStages--; + // convert any remaining lightmap stages to a lighting pass with a white texture + // only do this with r_sunlightMode non-zero, as it's only for correct shadows. + if (r_sunlightMode->integer) + { + for (i = 0; i < MAX_SHADER_STAGES; i++) + { + shaderStage_t *pStage = &stages[i]; + + if (!pStage->active) + continue; + + if (pStage->bundle[TB_DIFFUSEMAP].isLightmap) + { + pStage->glslShaderGroup = tr.lightallShader; + pStage->glslShaderIndex = LIGHTDEF_USE_LIGHTMAP; + if (r_deluxeMapping->integer && tr.worldDeluxeMapping) + pStage->glslShaderIndex |= LIGHTDEF_USE_DELUXEMAP; + pStage->bundle[TB_LIGHTMAP] = pStage->bundle[TB_DIFFUSEMAP]; + pStage->bundle[TB_DIFFUSEMAP].image[0] = tr.whiteImage; + pStage->bundle[TB_DIFFUSEMAP].isLightmap = qfalse; + } + } + } + return numStages; } |