1
0
Fork 0

fix water specular with Rembrandt

Signed-off-by: Emilian Huminiuc <emilianh@gmail.com>
This commit is contained in:
Emilian Huminiuc 2012-06-25 21:05:39 +03:00
parent 2b87e630bf
commit ce18b24ba9
5 changed files with 273 additions and 240 deletions

View file

@ -138,9 +138,6 @@
</sea_b>
<!-- END fog include -->
<rembrandt>
<use>/sim/rendering/rembrandt/enabled</use>
</rembrandt>
</parameters>
<generate>
@ -676,7 +673,7 @@
</texture-unit>
<program>
<vertex-shader>Shaders/water.vert</vertex-shader>
<vertex-shader>Shaders/water-gbuffer.vert</vertex-shader>
<fragment-shader>Shaders/water_sine-gbuffer.frag</fragment-shader>
<fragment-shader>Shaders/gbuffer-functions.frag</fragment-shader>
<fragment-shader>Shaders/gbuffer-encode.frag</fragment-shader>
@ -1064,7 +1061,7 @@
</texture-unit>
<program>
<vertex-shader>Shaders/water.vert</vertex-shader>
<vertex-shader>Shaders/water-gbuffer.vert</vertex-shader>
<fragment-shader>Shaders/water-gbuffer.frag</fragment-shader>
<fragment-shader>Shaders/gbuffer-functions.frag</fragment-shader>
<fragment-shader>Shaders/gbuffer-encode.frag</fragment-shader>

View file

@ -25,8 +25,8 @@ uniform int Status;
varying vec4 waterTex1; //moving texcoords
varying vec4 waterTex2; //moving texcoords
varying vec3 viewerdir;
// varying vec3 lightdir;
varying vec3 normal;
varying vec3 Vnormal;
varying vec3 VTangent;
varying vec3 VBinormal;
@ -56,13 +56,8 @@ void main(void)
// compute direction to viewer
vec3 E = normalize(viewerdir);
// compute direction to light source
//vec3 L = normalize(lightdir);
// half vector
//vec3 H = normalize(L + E);
vec3 Normal = normalize(normal);
vec3 vNormal = normalize(Vnormal);
const float water_shininess = 240.0;
@ -166,11 +161,14 @@ void main(void)
N0 *= windEffect_low;
N1 *= windEffect_low;
vec3 N = normalize(mix(Normal + N0, Normal + N1, mixFactor) * waveRoughness);
N = normalize(N.x * VTangent + N.y * VBinormal + N.z * normal);
if (normalmap_dds > 0)
N = -N; //dds fix
vec3 N2 = normalize(mix(N0, N1, mixFactor) * waveRoughness);
Normal = normalize(N2.x * VTangent + N2.y * VBinormal + N2.z * Normal);
vNormal = normalize(mix(vNormal + N0, vNormal + N1, mixFactor) * waveRoughness);
if (normalmap_dds > 0){
Normal = -Normal; //dds fix
vNormal = -vNormal;
}
// specular
//vec3 specular_color = vec3(gl_LightSource[0].diffuse)
// * pow(max(0.0, dot(N, H)), water_shininess) * 6.0;
@ -188,27 +186,18 @@ void main(void)
//vec4 ambient_light = gl_LightSource[0].diffuse;
vec4 finalColor = refl;
// if(cover < 1.5){
// specular = 0.0;
// }
float foamSlope = 0.10 + 0.1 * windScale;
vec4 foam_texel = texture2D(sea_foam, vec2(waterTex2 * tscale) * 25.0);
float waveSlope = N.g;
float waveSlope = vNormal.g;
if (windEffect >= 8.0)
if (waveSlope >= foamSlope){
finalColor = mix(finalColor, max(finalColor, finalColor + foam_texel), smoothstep(0.01, 0.50, N.g));
finalColor = mix(finalColor, max(finalColor, finalColor + foam_texel), smoothstep(0.01, 0.50, vNormal.g));
}
//finalColor *= ambient_light;
//gl_FragColor = mix(gl_Fog.color, finalColor, fogFactor);
//finalColor.rgb = fog_Func(finalColor.rgb, fogType);
//gl_FragColor = finalColor;
float emission = dot( gl_FrontLightModelProduct.sceneColor.rgb + gl_FrontMaterial.emission.rgb,
vec3( 0.3, 0.59, 0.11 ) );
encode_gbuffer(N, finalColor.rgb, 1, 1.0, water_shininess, emission, gl_FragCoord.z);
vec3( 0.3, 0.59, 0.11 )
);
float specular = smoothstep(0.0, 3.5, cover);
encode_gbuffer(Normal, finalColor.rgb, 1, specular, 128, emission, gl_FragCoord.z);
}

View file

@ -0,0 +1,67 @@
// This shader is mostly an adaptation of the shader found at
// http://www.bonzaisoftware.com/water_tut.html and its glsl conversion
// available at http://forum.bonzaisoftware.com/viewthread.php?tid=10
// © Michael Horsch - 2005
// Major update and revisions - 2011-10-07
// © Emilian Huminiuc and Vivian Meazza
#version 120
varying vec4 waterTex1;
varying vec4 waterTex2;
varying vec3 viewerdir;
varying vec3 lightdir;
varying vec3 normal;
varying vec3 VTangent;
varying vec3 VBinormal;
uniform float osg_SimulationTime;
uniform float WindE, WindN;
uniform int rembrandt_enabled;
attribute vec3 tangent;
attribute vec3 binormal;
/////// functions /////////
void rotationmatrix(in float angle, out mat4 rotmat)
{
rotmat = mat4( cos( angle ), -sin( angle ), 0.0, 0.0,
sin( angle ), cos( angle ), 0.0, 0.0,
0.0 , 0.0 , 1.0, 0.0,
0.0 , 0.0 , 0.0, 1.0 );
}
void main(void)
{
mat4 RotationMatrix;
normal = gl_NormalMatrix * gl_Normal;
VTangent = normalize(gl_NormalMatrix * tangent);
VBinormal = normalize(gl_NormalMatrix * binormal);
viewerdir = vec3(gl_ModelViewMatrixInverse[3]) - vec3(gl_Vertex);
vec4 t1 = vec4(0.0, osg_SimulationTime * 0.005217, 0.0, 0.0);
vec4 t2 = vec4(0.0, osg_SimulationTime * -0.0012, 0.0, 0.0);
float Angle;
float windFactor = sqrt(pow(abs(WindE),2)+pow(abs(WindN),2)) * 0.05;
if (WindN == 0.0 && WindE == 0.0) {
Angle = 0.0;
}else{
Angle = atan(-WindN, WindE) - atan(1.0);
}
rotationmatrix(Angle, RotationMatrix);
waterTex1 = gl_MultiTexCoord0 * RotationMatrix - t1 * windFactor;
rotationmatrix(Angle, RotationMatrix);
waterTex2 = gl_MultiTexCoord0 * RotationMatrix - t2 * windFactor;
// fog_Func(fogType);
gl_Position = ftransform();
}

View file

@ -14,15 +14,8 @@ varying vec3 viewerdir;
varying vec3 lightdir;
varying vec3 normal;
varying vec3 VTangent;
varying vec3 VBinormal;
uniform float osg_SimulationTime;
uniform float WindE, WindN;
uniform int rembrandt_enabled;
attribute vec3 tangent;
attribute vec3 binormal;
/////// functions /////////
@ -37,15 +30,7 @@ void rotationmatrix(in float angle, out mat4 rotmat)
void main(void)
{
mat4 RotationMatrix;
if (rembrandt_enabled > 0){
normal = gl_NormalMatrix * gl_Normal;
} else {
normal = normalize(gl_Normal);
}
VTangent = normalize(gl_NormalMatrix * tangent);
VBinormal = normalize(gl_NormalMatrix * binormal);
viewerdir = vec3(gl_ModelViewMatrixInverse[3]) - vec3(gl_Vertex);
lightdir = normalize(vec3(gl_ModelViewMatrixInverse * gl_LightSource[0].position));

View file

@ -19,20 +19,17 @@ uniform sampler2D perlin_normalmap;
uniform sampler3D Noise ;
uniform float saturation, Overcast, WindE, WindN;
uniform float CloudCover0, CloudCover1, CloudCover2, CloudCover3, CloudCover4;
uniform float saturation ;
uniform float Overcast ;
uniform float WindE ;
uniform float WindN ;
uniform float CloudCover0 ;
uniform float CloudCover1 ;
uniform float CloudCover2 ;
uniform float CloudCover3 ;
uniform float CloudCover4 ;
uniform float osg_SimulationTime ;
uniform int Status;
varying vec4 waterTex1; //moving texcoords
varying vec4 waterTex2; //moving texcoords
varying vec3 viewerdir;
varying vec3 lightdir;
varying vec3 normal;
varying vec3 VTangent;
varying vec3 VBinormal;
uniform float WaveFreq ;
uniform float WaveAmp ;
uniform float WaveSharp ;
@ -40,12 +37,25 @@ uniform float WaveAngle ;
uniform float WaveFactor ;
uniform float WaveDAngle ;
uniform float normalmap_dds ;
uniform int Status ;
varying vec4 waterTex1 ; //moving texcoords
varying vec4 waterTex2 ; //moving texcoords
varying vec3 viewerdir ;
varying vec3 lightdir ;
varying vec3 normal ;
varying vec3 Vnormal ;
varying vec3 VTangent ;
varying vec3 VBinormal ;
const vec4 AllOnes = vec4(1.0);
/////// functions /////////
void encode_gbuffer(vec3 normal, vec3 color, int mId, float specular, float shininess, float emission, float depth);
void rotationmatrix(in float angle, out mat4 rotmat)
{
rotmat = mat4( cos( angle ), -sin( angle ), 0.0, 0.0,
@ -133,14 +143,12 @@ void main(void)
// compute direction to light source
//vec3 L = normalize(lightdir);
// half vector
//vec3 H = normalize(L + E);
vec3 Normal = normalize(normal) ;
const float water_shininess = 240.0;
vec3 vNormal = normalize(Vnormal) ;
const float water_shininess = 128.0 ;
// float range = gl_ProjectionMatrix[3].z/(gl_FragCoord.z * -2.0 + 1.0 - gl_ProjectionMatrix[2].z);
@ -154,7 +162,6 @@ void main(void)
float waveRoughness = 0.01 + smoothstep(0.0, 40.0, windEffect) ; //wave roughness filter
float mixFactor = 0.2 + 0.02 * smoothstep(0.0, 50.0, windEffect);
//mixFactor = 0.2;
mixFactor = clamp(mixFactor, 0.3, 0.8);
// sine waves
@ -242,7 +249,7 @@ void main(void)
ddyVec = vec4(ddy, ddy1, ddy2, ddy3) ;
//toggle detailFlag
detailFlag = 1;
//detailFlag = 1 ;
// } // end sine stuff
float ddxSum = dot(ddxVec, AllOnes) ;
@ -279,8 +286,8 @@ void main(void)
vec4 vNorm = normalize(mix(nmap, nmap1, mixFactor) * waveRoughness) ;
vNorm.r += ddxSum ;
if (normalmap_dds > 0)
vNorm = -vNorm; //dds fix
if (normalmap_dds > 0)//dds fix
vNorm = -vNorm ;
//load reflection
//vec4 tmp = vec4(lightdir, 0.0);
@ -323,23 +330,21 @@ void main(void)
// {
N0 *= windEffect_low;
N1 *= windEffect_low;
//N0.r += (ddx + ddx1 + ddx2 + ddx3);
//N0.g += (ddy + ddy1 + ddy2 + ddy3);
N0.r += ddxSum;
N0.g += ddySum;
Normal = normalize(mix(Normal + N0, Normal + N1, mixFactor) * waveRoughness);
Normal = normalize(Normal.x * VTangent + Normal.y * VBinormal + Normal.z * normal);
if (normalmap_dds > 0)
Normal = -Normal; //dds fix
vec3 N2 = normalize(mix(N0, N1, mixFactor) * waveRoughness);
Normal = normalize(N2.x * VTangent + N2.y * VBinormal + N2.z * Normal);
vNormal = normalize(mix(vNormal + N0, vNormal + N1, mixFactor) * waveRoughness);
if (normalmap_dds > 0){ //dds fix
Normal = -Normal;
vNormal = -vNormal;
}
// }
// specular
// vec3 specular_color = vec3(1.0) * pow(max(0.0, dot(Normal, H)), water_shininess) * 6.0;
// vec4 specular = vec4(specular_color, 0.5);
//
//specular_color *= saturation * 0.3 ;
//float specular = saturation * 0.3;
@ -349,29 +354,21 @@ void main(void)
refl *= fres;
//calculate final colour
//vec4 ambient_light = gl_LightSource[0].diffuse;
vec4 finalColor = refl;
// if(cover >= 1.5){
// // specular = 0.0;
// // } else {
// finalColor.rgb = finalColor.rgb + specular_color;
// }
//add foam
vec4 foam_texel = texture2D(sea_foam, vec2(waterTex2 * tscale) * 25.0);
// if (range > -10000.0){
float foamSlope = 0.1 + 0.1 * windScale;
//float waveSlope = mix(N0.g, N1.g, 0.25);
float waveSlope = Normal.g;
float waveSlope = vNormal.g;
if (windEffect >= 8.0)
if (waveSlope >= foamSlope){
finalColor = mix( finalColor,
max(finalColor, finalColor + foam_texel),
smoothstep(0.01, 0.50, Normal.g)
smoothstep(0.01, 0.50, vNormal.g)
);
}
@ -381,8 +378,6 @@ void main(void)
float emission = dot( gl_FrontLightModelProduct.sceneColor.rgb + gl_FrontMaterial.emission.rgb,
vec3( 0.3, 0.59, 0.11 )
);
// float emission = dot( gl_FrontLightModelProduct.sceneColor.rgb + specular_color,
// vec3( 0.3, 0.59, 0.11 )
// );
encode_gbuffer(Normal, finalColor.rgb, 1, 1.0, water_shininess, emission, gl_FragCoord.z);
float specular = smoothstep(0.0, 3.5, cover);
encode_gbuffer(Normal, finalColor.rgb, 1, specular, water_shininess, emission, gl_FragCoord.z);
}