From: divverent Date: Thu, 15 Dec 2011 21:04:33 +0000 (+0000) Subject: r_water, MODE_REFRACTION: support alphaGen vertex for fading out water X-Git-Tag: xonotic-v0.6.0~102^2~3 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=2622546207b940ab27d8c7423e2d4af7aef58067;p=xonotic%2Fdarkplaces.git r_water, MODE_REFRACTION: support alphaGen vertex for fading out water git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@11616 d7cf8633-e32d-0410-b094-e92efae38249 --- diff --git a/gl_rmain.c b/gl_rmain.c index 4b0ca40c..8c88310c 100644 --- a/gl_rmain.c +++ b/gl_rmain.c @@ -2146,6 +2146,8 @@ void R_SetupShader_Surface(const vec3_t lightcolorbase, qboolean modellighting, if (rsurface.texture->currentmaterialflags & MATERIALFLAG_WATERSHADER) { mode = SHADERMODE_WATER; + if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX) + permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX; if((r_wateralpha.value < 1) && (rsurface.texture->currentmaterialflags & MATERIALFLAG_WATERALPHA)) { // this is the right thing to do for wateralpha @@ -2162,6 +2164,8 @@ void R_SetupShader_Surface(const vec3_t lightcolorbase, qboolean modellighting, else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFRACTION) { mode = SHADERMODE_REFRACTION; + if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX) + permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX; GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); blendfuncflags = R_BlendFuncFlags(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } diff --git a/shader_glsl.h b/shader_glsl.h index aa962c85..f968e3eb 100644 --- a/shader_glsl.h +++ b/shader_glsl.h @@ -416,6 +416,9 @@ "\n" "void main(void)\n" "{\n" +"#ifdef USEALPHAGENVERTEX\n" +" VertexColor = Attrib_Color;\n" +"#endif\n" " TexCoord = vec2(TexMatrix * Attrib_TexCoord0);\n" " gl_Position = ModelViewProjectionMatrix * Attrib_Position;\n" " ModelViewProjectionPosition = gl_Position;\n" @@ -444,12 +447,19 @@ " vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w);\n" " //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n" " vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n" +"#ifdef USEALPHAGENVERTEX\n" +" vec2 distort = DistortScaleRefractReflect.xy * VertexColor.a;\n" +" vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a);\n" +"#else\n" +" vec2 distort = DistortScaleRefractReflect.xy;\n" +" vec4 refractcolor = RefractColor;\n" +"#endif\n" " #ifdef USENORMALMAPSCROLLBLEND\n" " vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0);\n" " normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb;\n" -" vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * DistortScaleRefractReflect.xy;\n" +" vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(normal))).xy * distort;\n" " #else\n" -" vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * DistortScaleRefractReflect.xy;\n" +" vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(cast_myhalf3(dp_texture2D(Texture_Normal, TexCoord)) - cast_myhalf3(0.5))).xy * distort;\n" " #endif\n" " // FIXME temporary hack to detect the case that the reflection\n" " // gets blackened at edges due to leaving the area that contains actual\n" @@ -461,7 +471,7 @@ " f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05);\n" " f *= min(1.0, length(dp_texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05);\n" " ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f);\n" -" dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * RefractColor;\n" +" dp_FragColor = vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord).rgb, 1.0) * refractcolor;\n" "}\n" "#endif\n" "#else // !MODE_REFRACTION\n" @@ -479,6 +489,9 @@ "\n" "void main(void)\n" "{\n" +"#ifdef USEALPHAGENVERTEX\n" +" VertexColor = Attrib_Color;\n" +"#endif\n" " TexCoord = vec2(TexMatrix * Attrib_TexCoord0);\n" " vec3 EyeRelative = EyePosition - Attrib_Position.xyz;\n" " EyeVector.x = dot(EyeRelative, Attrib_TexCoord1.xyz);\n" @@ -516,12 +529,23 @@ " vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n" " //SafeScreenTexCoord = gl_FragCoord.xyxy * vec4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0);\n" " // slight water animation via 2 layer scrolling (todo: tweak)\n" +"#ifdef USEALPHAGENVERTEX\n" +" vec4 distort = DistortScaleRefractReflect * VertexColor.a;\n" +" float reflectoffset = ReflectOffset * VertexColor.a;\n" +" float reflectfactor = ReflectFactor * VertexColor.a;\n" +" vec4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), VertexColor.a);\n" +"#else\n" +" vec4 distort = DistortScaleRefractReflect;\n" +" float reflectoffset = ReflectOffset;\n" +" float reflectfactor = ReflectFactor;\n" +" vec4 refractcolor = RefractColor;\n" +"#endif\n" " #ifdef USENORMALMAPSCROLLBLEND\n" " vec3 normal = dp_texture2D(Texture_Normal, (TexCoord + vec2(0.08, 0.08)*ClientTime*NormalmapScrollBlend.x*0.5)*NormalmapScrollBlend.y).rgb - vec3(1.0);\n" " normal += dp_texture2D(Texture_Normal, (TexCoord + vec2(-0.06, -0.09)*ClientTime*NormalmapScrollBlend.x)*NormalmapScrollBlend.y*0.75).rgb;\n" -" vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * DistortScaleRefractReflect;\n" +" vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(normal) + vec3(0.15)).xyxy * distort;\n" " #else\n" -" vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * DistortScaleRefractReflect;\n" +" vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(dp_texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * distort;\n" " #endif\n" " // FIXME temporary hack to detect the case that the reflection\n" " // gets blackened at edges due to leaving the area that contains actual\n" @@ -538,8 +562,8 @@ " f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, 0.005)).rgb) / 0.002);\n" " f *= min(1.0, length(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.005, -0.005)).rgb) / 0.002);\n" " ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f);\n" -" float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset;\n" -" dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * RefractColor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel);\n" +" float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset;\n" +" dp_FragColor = mix(vec4(dp_texture2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, vec4(dp_texture2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel);\n" "}\n" "#endif\n" "#else // !MODE_WATER\n" diff --git a/shader_hlsl.h b/shader_hlsl.h index 78f291c6..d09307cf 100644 --- a/shader_hlsl.h +++ b/shader_hlsl.h @@ -384,15 +384,24 @@ "(\n" "float4 gl_Vertex : POSITION,\n" "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n" +"#ifdef USEALPHAGENVERTEX\n" +"float4 gl_Color : COLOR0,\n" +"#endif\n" "float4 gl_MultiTexCoord0 : TEXCOORD0,\n" "uniform float4x4 TexMatrix : register(c0),\n" "uniform float3 EyePosition : register(c24),\n" +"#ifdef USEALPHAGENVERTEX\n" +"out float4 gl_FrontColor : COLOR,\n" +"#endif\n" "out float4 gl_Position : POSITION,\n" "out float2 TexCoord : TEXCOORD0,\n" "out float3 EyeVector : TEXCOORD1,\n" "out float4 ModelViewProjectionPosition : TEXCOORD2\n" ")\n" "{\n" +"#ifdef USEALPHAGENVERTEX\n" +" gl_FrontColor = gl_Color;\n" +"#endif\n" " TexCoord = mul(TexMatrix, gl_MultiTexCoord0).xy;\n" " gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n" " ModelViewProjectionPosition = gl_Position;\n" @@ -405,6 +414,9 @@ "#ifdef FRAGMENT_SHADER\n" "void main\n" "(\n" +"#ifdef USEALPHAGENVERTEX\n" +"float4 gl_FrontColor : COLOR,\n" +"#endif\n" "float2 TexCoord : TEXCOORD0,\n" "float3 EyeVector : TEXCOORD1,\n" "float4 ModelViewProjectionPosition : TEXCOORD2,\n" @@ -421,7 +433,14 @@ " float2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w);\n" " //float2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(tex2D(Texture_Normal, TexCoord).rgb - float3(0.5,0.5,0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n" " float2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n" -" float2 ScreenTexCoord = SafeScreenTexCoord + normalize(tex2D(Texture_Normal, TexCoord).rgb - float3(0.5,0.5,0.5)).xy * DistortScaleRefractReflect.xy;\n" +"#ifdef USEALPHAGENVERTEX\n" +" float2 distort = DistortScaleRefractReflect.xy * gl_FrontColor.a;\n" +" float4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), gl_FrontColor.a);\n" +"#else\n" +" float2 distort = DistortScaleRefractReflect.xy;\n" +" float4 refractcolor = RefractColor;\n" +"#endif\n" +" float2 ScreenTexCoord = SafeScreenTexCoord + normalize(tex2D(Texture_Normal, TexCoord).rgb - float3(0.5,0.5,0.5)).xy * distort;\n" " // FIXME temporary hack to detect the case that the reflection\n" " // gets blackened at edges due to leaving the area that contains actual\n" " // content.\n" @@ -432,7 +451,7 @@ " f *= min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord + float2(-0.01, 0.01)).rgb) / 0.05);\n" " f *= min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord + float2(-0.01, -0.01)).rgb) / 0.05);\n" " ScreenTexCoord = lerp(SafeScreenTexCoord, ScreenTexCoord, f);\n" -" dp_FragColor = float4(tex2D(Texture_Refraction, ScreenTexCoord).rgb, 1) * RefractColor;\n" +" dp_FragColor = float4(tex2D(Texture_Refraction, ScreenTexCoord).rgb, 1) * refractcolor;\n" "}\n" "#endif\n" "#else // !MODE_REFRACTION\n" @@ -447,18 +466,27 @@ "(\n" "float4 gl_Vertex : POSITION,\n" "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n" +"#ifdef USEALPHAGENVERTEX\n" +"float4 gl_Color : COLOR0,\n" +"#endif\n" "float4 gl_MultiTexCoord0 : TEXCOORD0,\n" "float4 gl_MultiTexCoord1 : TEXCOORD1,\n" "float4 gl_MultiTexCoord2 : TEXCOORD2,\n" "float4 gl_MultiTexCoord3 : TEXCOORD3,\n" "uniform float4x4 TexMatrix : register(c0),\n" "uniform float3 EyePosition : register(c24),\n" +"#ifdef USEALPHAGENVERTEX\n" +"out float4 gl_FrontColor : COLOR,\n" +"#endif\n" "out float4 gl_Position : POSITION,\n" "out float2 TexCoord : TEXCOORD0,\n" "out float3 EyeVector : TEXCOORD1,\n" "out float4 ModelViewProjectionPosition : TEXCOORD2\n" ")\n" "{\n" +"#ifdef USEALPHAGENVERTEX\n" +" gl_FrontColor = gl_Color;\n" +"#endif\n" " TexCoord = mul(TexMatrix, gl_MultiTexCoord0).xy;\n" " float3 EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n" " EyeVector = float3(dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz), dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz), dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz));\n" @@ -473,6 +501,9 @@ "#ifdef FRAGMENT_SHADER\n" "void main\n" "(\n" +"#ifdef USEALPHAGENVERTEX\n" +"float4 gl_FrontColor : COLOR,\n" +"#endif\n" "float2 TexCoord : TEXCOORD0,\n" "float3 EyeVector : TEXCOORD1,\n" "float4 ModelViewProjectionPosition : TEXCOORD2,\n" @@ -493,7 +524,18 @@ " //float4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(tex2D(Texture_Normal, TexCoord).rgb - float3(0.5,0.5,0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n" " float4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n" " //SafeScreenTexCoord = gl_FragCoord.xyxy * float4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0);\n" -" float4 ScreenTexCoord = SafeScreenTexCoord + float2(normalize(tex2D(Texture_Normal, TexCoord).rgb - float3(0.5,0.5,0.5)).xy).xyxy * DistortScaleRefractReflect;\n" +"#ifdef USEALPHAGENVERTEX\n" +" float4 distort = DistortScaleRefractReflect * gl_FrontColor.a;\n" +" float reflectoffset = ReflectOffset * gl_FrontColor.a;\n" +" float reflectfactor = ReflectFactor * gl_FrontColor.a;\n" +" float4 refractcolor = mix(RefractColor, vec4(1.0, 1.0, 1.0, 1.0), gl_FrontColor.a);\n" +"#else\n" +" float4 distort = DistortScaleRefractReflect;\n" +" float reflectoffset = ReflectOffset;\n" +" float reflectfactor = ReflectFactor;\n" +" float4 refractcolor = RefractColor;\n" +"#endif\n" +" float4 ScreenTexCoord = SafeScreenTexCoord + float2(normalize(tex2D(Texture_Normal, TexCoord).rgb - float3(0.5,0.5,0.5)).xy).xyxy * distort;\n" " // FIXME temporary hack to detect the case that the reflection\n" " // gets blackened at edges due to leaving the area that contains actual\n" " // content.\n" @@ -509,8 +551,8 @@ " f *= min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord.zw + float2(-0.01, 0.01)).rgb) / 0.05);\n" " f *= min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord.zw + float2(-0.01, -0.01)).rgb) / 0.05);\n" " ScreenTexCoord.zw = lerp(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f);\n" -" float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset;\n" -" dp_FragColor = lerp(float4(tex2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * RefractColor, float4(tex2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel);\n" +" float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * reflectfactor + reflectoffset;\n" +" dp_FragColor = lerp(float4(tex2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * refractcolor, float4(tex2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel);\n" "}\n" "#endif\n" "#else // !MODE_WATER\n"