1
0
Fork 0

New water effect, first steps

This commit is contained in:
gral@who.net 2010-09-03 23:51:43 +02:00
parent da2998811a
commit db50149497
3 changed files with 237 additions and 87 deletions

View file

@ -2,6 +2,43 @@
<PropertyList> <PropertyList>
<name>Effects/water</name> <name>Effects/water</name>
<inherits-from>Effects/terrain-default</inherits-from> <inherits-from>Effects/terrain-default</inherits-from>
<parameters>
<texture n="0">
<image>Textures/Water/water-reflection.png</image>
<filter>linear-mipmap-linear</filter>
<wrap-s>repeat</wrap-s>
<wrap-t>repeat</wrap-t>
<internal-format>normalized</internal-format>
</texture>
<texture n="1">
<image>Textures/Water/water-refraction.png</image>
<filter>linear-mipmap-linear</filter>
<wrap-s>repeat</wrap-s>
<wrap-t>repeat</wrap-t>
<internal-format>normalized</internal-format>
</texture>
<texture n="2">
<image>Textures/Water/water-normalmap.png</image>
<filter>linear-mipmap-linear</filter>
<wrap-s>repeat</wrap-s>
<wrap-t>repeat</wrap-t>
<internal-format>normalized</internal-format>
</texture>
<texture n="3">
<image>Textures/Water/water-dudv.png</image>
<filter>linear-mipmap-linear</filter>
<wrap-s>repeat</wrap-s>
<wrap-t>repeat</wrap-t>
<internal-format>normalized</internal-format>
</texture>
<texture n="4">
<image>Textures/Water/water-depth.png</image>
<filter>linear-mipmap-linear</filter>
<wrap-s>repeat</wrap-s>
<wrap-t>repeat</wrap-t>
<internal-format>normalized</internal-format>
</texture>
</parameters>
<technique n="9"> <technique n="9">
<predicate> <predicate>
<and> <and>
@ -14,7 +51,7 @@
</less-equal> </less-equal>
<and> <and>
<extension-supported>GL_ARB_shader_objects</extension-supported> <extension-supported>GL_ARB_shader_objects</extension-supported>
<extension-supported>GL_ARB_shading_language_100</extension-supported> <extension-supported>GL_ARB_shading_language_120</extension-supported>
<extension-supported>GL_ARB_vertex_shader</extension-supported> <extension-supported>GL_ARB_vertex_shader</extension-supported>
<extension-supported>GL_ARB_fragment_shader</extension-supported> <extension-supported>GL_ARB_fragment_shader</extension-supported>
</and> </and>
@ -39,16 +76,91 @@
</render-bin> </render-bin>
<texture-unit> <texture-unit>
<unit>0</unit> <unit>0</unit>
<type>noise</type> <image><use>texture[0]/image</use></image>
<filter><use>texture[0]/filter</use></filter>
<wrap-s><use>texture[0]/wrap-s</use></wrap-s>
<wrap-t><use>texture[0]/wrap-t</use></wrap-t>
<internal-format>
<use>texture[0]/internal-format</use>
</internal-format>
</texture-unit>
<texture-unit>
<unit>1</unit>
<image><use>texture[1]/image</use></image>
<filter><use>texture[1]/filter</use></filter>
<wrap-s><use>texture[1]/wrap-s</use></wrap-s>
<wrap-t><use>texture[1]/wrap-t</use></wrap-t>
<internal-format>
<use>texture[1]/internal-format</use>
</internal-format>
</texture-unit>
<texture-unit>
<unit>2</unit>
<image><use>texture[2]/image</use></image>
<filter><use>texture[2]/filter</use></filter>
<wrap-s><use>texture[2]/wrap-s</use></wrap-s>
<wrap-t><use>texture[2]/wrap-t</use></wrap-t>
<internal-format>
<use>texture[2]/internal-format</use>
</internal-format>
</texture-unit>
<texture-unit>
<unit>3</unit>
<image><use>texture[3]/image</use></image>
<filter><use>texture[3]/filter</use></filter>
<wrap-s><use>texture[3]/wrap-s</use></wrap-s>
<wrap-t><use>texture[3]/wrap-t</use></wrap-t>
<internal-format>
<use>texture[3]/internal-format</use>
</internal-format>
</texture-unit>
<texture-unit>
<unit>4</unit>
<image><use>texture[4]/image</use></image>
<filter><use>texture[4]/filter</use></filter>
<wrap-s><use>texture[4]/wrap-s</use></wrap-s>
<wrap-t><use>texture[4]/wrap-t</use></wrap-t>
<internal-format>
<use>texture[4]/internal-format</use>
</internal-format>
</texture-unit> </texture-unit>
<program> <program>
<vertex-shader>Shaders/water.vert</vertex-shader> <vertex-shader>Shaders/water.vert</vertex-shader>
<fragment-shader>Shaders/water.frag</fragment-shader> <fragment-shader>Shaders/water.frag</fragment-shader>
</program> </program>
<uniform> <uniform>
<name>texture</name> <name>water_reflection</name>
<type>sampler-3d</type> <type>sampler-2d</type>
<value type="int">0</value></uniform> <value type="int">0</value>
</uniform>
<uniform>
<name>water_refraction</name>
<type>sampler-2d</type>
<value type="int">1</value>
</uniform>
<uniform>
<name>water_normalmap</name>
<type>sampler-2d</type>
<value type="int">2</value>
</uniform>
<uniform>
<name>water_dudvmap</name>
<type>sampler-2d</type>
<value type="int">3</value>
</uniform>
<uniform>
<name>water_depthmap</name>
<type>sampler-2d</type>
<value type="int">4</value>
</uniform>
<!--<uniform>
<name>waterColor</name>
<value type="float-vec4">0.0 0.0 1.0 1.0</value>
</uniform>
<uniform>
<name>waterDepth</name>
<value type="float-vec4">1.0 1.0 1.0 1.0</value>
</uniform>-->
</pass> </pass>
</technique> </technique>
</PropertyList> </PropertyList>

View file

@ -1,86 +1,91 @@
#version 120
varying vec4 rawpos; uniform sampler2D water_normalmap;
varying vec4 ecPosition; uniform sampler2D water_reflection;
varying vec3 VNormal; uniform sampler2D water_refraction;
varying vec3 Normal; uniform sampler2D water_dudvmap;
varying vec3 lightVec; uniform sampler2D water_depthmap;
//uniform vec4 waterColor, waterDepth;
uniform sampler3D NoiseTex; vec4 waterColor = vec4(0.0,0.2,1.0,1.0);
uniform float osg_SimulationTime; vec4 waterDepth = vec4(0.0,0.2,1.0,1.0);
//const float scale = 1.0; varying vec4 waterTex0; //lightpos
varying vec4 waterTex1; //moving texcoords
varying vec4 waterTex2; //moving texcoords
varying vec4 waterTex3; //for projection
varying vec4 waterTex4; //viewts
void main (void) //unit 0 = water_reflection
//unit 1 = water_refraction
//unit 2 = water_normalmap
//unit 3 = water_dudvmap
//unit 4 = water_depthmap
void main(void)
{ {
vec4 noisevecS = texture3D(NoiseTex, (rawpos.xyz)*0.0126); const vec4 sca = vec4(0.005, 0.005, 0.005, 0.005);
vec4 nvLS = texture3D(NoiseTex, (rawpos.xyz)*-0.0003323417); const vec4 sca2 = vec4(0.02, 0.02, 0.02, 0.02);
const vec4 tscale = vec4(0.25, 0.25, 0.25, 0.25);
const vec4 two = vec4(2.0, 2.0, 2.0, 1.0);
const vec4 mone = vec4(-1.0, -1.0, -1.0, 1.0);
vec4 noisevec = texture3D(NoiseTex, (rawpos.xyz)*0.00423+vec3(0.0,0.0,osg_SimulationTime*0.035217)); const vec4 ofive = vec4(0.5,0.5,0.5,1.0);
vec4 nvL = texture3D(NoiseTex, (rawpos.xyz)*0.001223417+(0.0,0.0,osg_SimulationTime*-0.0212));
float fogFactor; const float exponent = 64.0;
if (gl_Fog.density == 1.0)
{
fogFactor=1.0;
}
else
{
float fogCoord = ecPosition.z;
const float LOG2 = 1.442695;
fogFactor = exp2(-gl_Fog.density * gl_Fog.density * fogCoord * fogCoord * LOG2);
fogFactor = clamp(fogFactor, 0.0, 1.0);
}
float a=1.0; vec4 lightTS = normalize(waterTex0);
float n=0.00; vec4 viewt = normalize(waterTex4);
n += nvLS[0]*a; vec4 disdis = texture2D(water_dudvmap, vec2(waterTex2 * tscale));
a/=2.0; vec4 dist = texture2D(water_dudvmap, vec2(waterTex1 + disdis*sca2));
n += nvLS[1]*a; vec4 fdist = dist;
a/=2.0; fdist = fdist * two + mone;
n += nvLS[2]*a; fdist = normalize(fdist);
a/=2.0; fdist *= sca;
n += nvLS[3]*a;
a=4.0; //load normalmap
float na=n; vec4 nmap = texture2D(water_normalmap, vec2(waterTex1 + disdis*sca2));
na += nvL[0]*1.1; nmap = (nmap-ofive) * two;
a*=1.2; vec4 vNorm = nmap;
na += nvL[1]*a; vNorm = normalize(nmap);
a*=1.2;
na += nvL[2]*a;
a*=1.2;
na += nvL[3]*a;
a=2.0;
na += noisevec[0]*a*0.2;
a*=1.2;
na += noisevec[1]*a;
a*=1.2;
na += noisevec[2]*a;
a*=1.2;
na += noisevec[3]*a;
vec4 c1; //get projective texcoords
c1 = asin(vec4(smoothstep(0.0, 2.2, n), smoothstep(-0.1, 2.10, n), smoothstep(-0.2, 2.0, n), 1.0)); vec4 tmp = vec4(1.0 / waterTex3.w);
vec4 temp = tmp;
vec3 Eye = normalize(-ecPosition.xyz); vec4 projCoord = waterTex3 * tmp;
vec3 Reflected = normalize(reflect(-normalize(lightVec), normalize(VNormal+vec3(0.0,0.0,na*0.10-0.24)))); projCoord += vec4(1.0);
projCoord *= vec4(0.5);
tmp = projCoord + fdist;
tmp = clamp(tmp, 0.001, 0.999);
vec3 bump = normalize(VNormal+vec3(0.0, 0.0, na)-0.9); //load reflection,refraction and depth texture
vec3 bumped = max(normalize(refract(lightVec, normalize(bump), 0.16)), 0.0); vec4 refTex = texture2D(water_reflection, vec2(tmp));
vec4 refl = refTex;
vec4 refr = texture2D(water_refraction, vec2(tmp));
vec4 wdepth = texture2D(water_depthmap, vec2(tmp));
vec4 ambientColor = gl_LightSource[0].ambient; wdepth = vec4(pow(wdepth.x, 4.0));
vec4 light = ambientColor; vec4 invdepth = 1.0 - wdepth;
c1 *= light;
float bumpFact = (bumped.r+bumped.g+bumped.b); //calculate specular highlight
float ReflectedEye = max(dot(Reflected, Eye), 0.0); vec4 vRef = normalize(reflect(-lightTS, vNorm));
float eyeFact = pow(ReflectedEye, 20.0); float stemp =max(0.0, dot(viewt, vRef) );
c1 += 0.3 * gl_LightSource[0].diffuse * (1.0-eyeFact) * bumpFact*bumpFact; stemp = pow(stemp, exponent);
c1 += 0.4 * gl_LightSource[0].diffuse * eyeFact * 3*bumpFact; vec4 specular = vec4(stemp);
eyeFact = pow(eyeFact, 20.0);
c1 += gl_LightSource[0].specular * eyeFact * 4*bumpFact;
vec4 finalColor = c1; //calculate fresnel and inverted fresnel
gl_FragColor = mix(gl_Fog.color, finalColor, fogFactor); vec4 invfres = vec4( dot(vNorm, viewt) );
vec4 fres = vec4(1.0) -invfres ;
//calculate reflection and refraction
refr *= invfres;
refr *= invdepth;
temp = waterColor * wdepth * invfres;
refr += temp;
refl *= fres;
//add reflection and refraction
tmp = refr + refl;
gl_FragColor = tmp + specular;
} }

View file

@ -1,15 +1,48 @@
varying vec4 rawpos;
varying vec4 ecPosition; varying vec4 waterTex0;
varying vec3 VNormal; varying vec4 waterTex1;
varying vec3 Normal; varying vec4 waterTex2;
varying vec3 lightVec; varying vec4 waterTex3;
varying vec4 waterTex4;
uniform vec4 viewpos, lightpos;
uniform float time, time2;
//unit 0 = water_reflection
//unit 1 = water_refraction
//unit 2 = water_normalmap
//unit 3 = water_dudvmap
//unit 4 = water_depthmap
void main(void) void main(void)
{ {
rawpos = gl_Vertex; vec4 mpos, temp;
ecPosition = gl_ModelViewMatrix * gl_Vertex; vec4 tangent = vec4(1.0, 0.0, 0.0, 0.0);
VNormal = normalize(gl_NormalMatrix * gl_Normal); vec4 norm = vec4(0.0, 1.0, 0.0, 0.0);
Normal = normalize(gl_Normal); vec4 binormal = vec4(0.0, 0.0, 1.0, 0.0);
lightVec = normalize(gl_LightSource[0].position.xyz/* - ecPosition*/);
gl_Position = ftransform(); mat4 mvp = gl_ModelViewProjectionMatrix;
mat4 mtx = gl_TextureMatrix[0];
temp = viewpos - gl_Vertex;
waterTex4.x = dot(temp, tangent);
waterTex4.y = dot(temp, binormal);
waterTex4.z = dot(temp, norm);
waterTex4.w = 0.0;
temp = lightpos - gl_Vertex;
waterTex0.x = dot(temp, tangent);
waterTex0.y = dot(temp, binormal);
waterTex0.z = dot(temp, norm);
waterTex0.w = 0.0;
mpos = mvp * gl_Vertex;
vec4 t1 = vec4(0.0, -time, 0.0,0.0);
vec4 t2 = vec4(0.0, -time2, 0.0,0.0);
waterTex1 = gl_MultiTexCoord0 + t1;
waterTex2 = gl_MultiTexCoord0 + t2;
waterTex3 = mpos;
gl_Position = ftransform();
} }