From 2924a0a6e6f62043eb92c77db28434ef76b86461 Mon Sep 17 00:00:00 2001 From: divverent Date: Mon, 29 Aug 2011 14:02:58 +0000 Subject: [PATCH] make sure gloss never calculates 0^0 as that is undefined and leads to black stuff git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@11305 d7cf8633-e32d-0410-b094-e92efae38249 --- dpsoftrast.c | 6 ++++-- shader_glsl.h | 15 +++++++++------ shader_hlsl.h | 15 +++++++++------ 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/dpsoftrast.c b/dpsoftrast.c index 2c101fc2..d1cde7a9 100644 --- a/dpsoftrast.c +++ b/dpsoftrast.c @@ -107,6 +107,8 @@ static void *MM_CALLOC(size_t nmemb, size_t size) #define MM_FREE free #endif +#define DPSOFTRAST_FLT_MIN 0.000000000000000001f + typedef enum DPSOFTRAST_ARRAY_e { DPSOFTRAST_ARRAY_POSITION, @@ -3855,7 +3857,7 @@ void DPSOFTRAST_PixelShader_LightDirection(DPSOFTRAST_State_Thread *thread, cons specularnormal[2] = lightnormal[2] + eyenormal[2]; DPSOFTRAST_Vector3Normalize(specularnormal); - specular = DPSOFTRAST_Vector3Dot(surfacenormal, specularnormal);if (specular < 0.0f) specular = 0.0f; + specular = DPSOFTRAST_Vector3Dot(surfacenormal, specularnormal);if (specular < DPSOFTRAST_FLT_MIN) specular = DPSOFTRAST_FLT_MIN; } specular = pow(specular, SpecularPower * glosstex[3]); @@ -4264,7 +4266,7 @@ void DPSOFTRAST_PixelShader_LightSource(DPSOFTRAST_State_Thread *thread, const D specularnormal[2] = lightnormal[2] + eyenormal[2]; DPSOFTRAST_Vector3Normalize(specularnormal); - specular = DPSOFTRAST_Vector3Dot(surfacenormal, specularnormal);if (specular < 0.0f) specular = 0.0f; + specular = DPSOFTRAST_Vector3Dot(surfacenormal, specularnormal);if (specular < DPSOFTRAST_FLT_MIN) specular = DPSOFTRAST_FLT_MIN; } specular = pow(specular, SpecularPower * glosstex[3]); diff --git a/shader_glsl.h b/shader_glsl.h index cfc1c64a..0c01478e 100644 --- a/shader_glsl.h +++ b/shader_glsl.h @@ -43,6 +43,9 @@ "#define highp\n" "#endif\n" "\n" +"// not REALLY the smallest value (2^-62 would be correct), but short and any compiler failing at parsing this is broken\n" +"#define FLT_MIN 0.000000000000000001\n" +"\n" "#ifdef VERTEX_SHADER\n" "dp_attribute vec4 Attrib_Position; // vertex\n" "dp_attribute vec4 Attrib_Color; // color\n" @@ -978,10 +981,10 @@ " // calculate directional shading\n" " vec3 eyevector = position * -1.0;\n" "# ifdef USEEXACTSPECULARMATH\n" -" myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), SpecularPower * normalmap.a);\n" +" myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, FLT_MIN)), SpecularPower * normalmap.a);\n" "# else\n" " myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(eyevector)));\n" -" myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower * normalmap.a);\n" +" myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), FLT_MIN)), SpecularPower * normalmap.a);\n" "# endif\n" "#endif\n" "\n" @@ -1237,10 +1240,10 @@ " color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse);\n" "#ifdef USESPECULAR\n" "#ifdef USEEXACTSPECULARMATH\n" -" myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), SpecularPower * glosstex.a);\n" +" myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, FLT_MIN)), SpecularPower * glosstex.a);\n" "#else\n" " myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz)));\n" -" myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower * glosstex.a);\n" +" myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), FLT_MIN)), SpecularPower * glosstex.a);\n" "#endif\n" " color.rgb += glosstex.rgb * (specular * Color_Specular);\n" "#endif\n" @@ -1332,10 +1335,10 @@ " myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0));\n" "# ifdef USESPECULAR\n" "# ifdef USEEXACTSPECULARMATH\n" -" myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, 0.0)), SpecularPower * glosstex.a);\n" +" myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVectorFogDepth.xyz)))*-1.0, FLT_MIN)), SpecularPower * glosstex.a);\n" "# else\n" " myhalf3 specularnormal = normalize(lightnormal + cast_myhalf3(normalize(EyeVectorFogDepth.xyz)));\n" -" myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower * glosstex.a);\n" +" myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), FLT_MIN)), SpecularPower * glosstex.a);\n" "# endif\n" " color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor;\n" "# else\n" diff --git a/shader_hlsl.h b/shader_hlsl.h index 96cf8bab..73bd7dd8 100644 --- a/shader_hlsl.h +++ b/shader_hlsl.h @@ -2,6 +2,9 @@ "// written by Forest 'LordHavoc' Hale\n" "// shadowmapping enhancements by Lee 'eihrul' Salzman\n" "\n" +"// not REALLY the smallest value (2^-62 would be correct), but short and any compiler failing at parsing this is broken\n" +"#define FLT_MIN 0.000000000000000001\n" +"\n" "// FIXME: we need to get rid of ModelViewProjectionPosition to make room for the texcoord for this\n" "#if defined(USEREFLECTION)\n" "#undef USESHADOWMAPORTHO\n" @@ -944,10 +947,10 @@ " // calculate directional shading\n" " float3 eyevector = position * -1.0;\n" "# ifdef USEEXACTSPECULARMATH\n" -" half specular = half(pow(half(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), SpecularPower * normalmap.a));\n" +" half specular = half(pow(half(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, FLT_MIN)), SpecularPower * normalmap.a));\n" "# else\n" " half3 specularnormal = half3(normalize(lightnormal + half3(normalize(eyevector))));\n" -" half specular = half(pow(half(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower * normalmap.a));\n" +" half specular = half(pow(half(max(float(dot(surfacenormal, specularnormal)), FLT_MIN)), SpecularPower * normalmap.a));\n" "# endif\n" "#endif\n" "\n" @@ -1359,10 +1362,10 @@ " color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse);\n" "#ifdef USESPECULAR\n" "#ifdef USEEXACTSPECULARMATH\n" -" half specular = half(pow(half(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower * glosstex.a));\n" +" half specular = half(pow(half(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVector)))*-1.0, FLT_MIN)), SpecularPower * glosstex.a));\n" "#else\n" " half3 specularnormal = half3(normalize(lightnormal + half3(normalize(EyeVector))));\n" -" half specular = half(pow(half(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower * glosstex.a));\n" +" half specular = half(pow(half(max(float(dot(surfacenormal, specularnormal)), FLT_MIN)), SpecularPower * glosstex.a));\n" "#endif\n" " color.rgb += glosstex.rgb * (specular * Color_Specular);\n" "#endif\n" @@ -1481,10 +1484,10 @@ " half diffuse = half(max(float(dot(surfacenormal, lightnormal)), 0.0));\n" "# ifdef USESPECULAR\n" "# ifdef USEEXACTSPECULARMATH\n" -" half specular = half(pow(half(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower * glosstex.a));\n" +" half specular = half(pow(half(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVector)))*-1.0, FLT_MIN)), SpecularPower * glosstex.a));\n" "# else\n" " half3 specularnormal = half3(normalize(lightnormal + half3(normalize(EyeVector))));\n" -" half specular = half(pow(half(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower * glosstex.a));\n" +" half specular = half(pow(half(max(float(dot(surfacenormal, specularnormal)), FLT_MIN)), SpecularPower * glosstex.a));\n" "# endif\n" " color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor;\n" "# else\n" -- 2.39.2