1
0
Fork 0

Adapt water shader to Rembrandt

Signed-off-by: Emilian Huminiuc <emilianh@gmail.com>
This commit is contained in:
Emilian Huminiuc 2012-06-24 20:12:32 +03:00
parent c19d8f798a
commit 174f40d52b
4 changed files with 1413 additions and 2 deletions

View file

@ -138,8 +138,16 @@
</sea_b> </sea_b>
<!-- END fog include --> <!-- END fog include -->
<rembrandt>
<use>/sim/rendering/rembrandt/enabled</use>
</rembrandt>
</parameters> </parameters>
<generate>
<tangent type="int">6</tangent>
<binormal type="int">7</binormal>
</generate>
<technique n="3"> <technique n="3">
<predicate> <predicate>
@ -497,7 +505,782 @@
</pass> </pass>
</technique> </technique>
<technique n="6">
<predicate>
<and>
<property>/sim/rendering/shaders/water</property>
<property>/sim/rendering/rembrandt/enabled</property>
<less-equal>
<value type="float">4.0</value>
<float-property>/sim/rendering/shaders/water</float-property>
</less-equal>
<or>
<less-equal>
<value type="float">2.0</value>
<glversion/>
</less-equal>
<and>
<extension-supported>GL_ARB_shader_objects</extension-supported>
<extension-supported>GL_ARB_shading_language_100</extension-supported>
<extension-supported>GL_ARB_vertex_shader</extension-supported>
<extension-supported>GL_ARB_fragment_shader</extension-supported>
</and>
</or>
<extension-supported>GL_EXT_gpu_shader4</extension-supported>
</and>
</predicate>
<pass>
<lighting>false</lighting>
<material>
<ambient>
<use>material/ambient</use>
</ambient>
<diffuse>
<use>material/diffuse</use>
</diffuse>
<specular>
<use>material/specular</use>
</specular>
<emissive>
<use>material/emissive</use>
</emissive>
<shininess>
<use>material/shininess</use>
</shininess>
<color-mode>ambient-and-diffuse</color-mode>
</material>
<blend>
<use>transparent</use>
</blend>
<alpha-test>
<use>transparent</use>
</alpha-test>
<shade-model>smooth</shade-model>
<cull-face>back</cull-face>
<render-bin>
<bin-number>
<use>render-bin/bin-number</use>
</bin-number>
<bin-name>
<use>render-bin/bin-name</use>
</bin-name>
</render-bin>
<texture-unit>
<unit>0</unit>
<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>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>
<unit>5</unit>
<image>
<use>texture[5]/image</use>
</image>
<filter>
<use>texture[5]/filter</use>
</filter>
<wrap-s>
<use>texture[5]/wrap-s</use>
</wrap-s>
<wrap-t>
<use>texture[5]/wrap-t</use>
</wrap-t>
<internal-format>
<use>texture[5]/internal-format</use>
</internal-format>
</texture-unit>
<texture-unit>
<unit>6</unit>
<image>
<use>texture[6]/image</use>
</image>
<filter>
<use>texture[6]/filter</use>
</filter>
<wrap-s>
<use>texture[6]/wrap-s</use>
</wrap-s>
<wrap-t>
<use>texture[6]/wrap-t</use>
</wrap-t>
<internal-format>
<use>texture[6]/internal-format</use>
</internal-format>
</texture-unit>
<program>
<vertex-shader>Shaders/water.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>
<attribute>
<name>tangent</name>
<index>6</index>
</attribute>
<attribute>
<name>binormal</name>
<index>7</index>
</attribute>
</program>
<uniform>
<name>water_reflection</name>
<type>sampler-2d</type>
<value type="int">0</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_reflection_grey</name>
<type>sampler-2d</type>
<value type="int">4</value>
</uniform>
<uniform>
<name>sea_foam</name>
<type>sampler-2d</type>
<value type="int">5</value>
</uniform>
<uniform>
<name>perlin_normalmap</name>
<type>sampler-2d</type>
<value type="int">6</value>
</uniform>
<!-- normalmap is .dds-->
<uniform>
<name>normalmap_dds</name>
<type>float</type>
<value>
<use>normalmap_dds</use>
</value>
</uniform>
<uniform>
<name>saturation</name>
<type>float</type>
<!--<value>0.4</value>-->
<value>
<use>saturation</use>
</value>
</uniform>
<uniform>
<name>CloudCover0</name>
<type>float</type>
<value>
<use>cloud-cover[0]</use>
</value>
</uniform>
<uniform>
<name>CloudCover1</name>
<type>float</type>
<value>
<use>cloud-cover[1]</use>
</value>
</uniform>
<uniform>
<name>CloudCover2</name>
<type>float</type>
<value>
<use>cloud-cover[2]</use>
</value>
</uniform>
<uniform>
<name>CloudCover3</name>
<type>float</type>
<value>
<use>cloud-cover[3]</use>
</value>
</uniform>
<uniform>
<name>CloudCover4</name>
<type>float</type>
<value>
<use>cloud-cover[4]</use>
</value>
</uniform>
<uniform>
<name>Status</name>
<type>int</type>
<value>
<use>status</use>
</value>
<!--<value type="int">0</value> -->
</uniform>
<uniform>
<name>Overcast</name>
<type>float</type>
<value>
<use>overcast</use>
</value>
</uniform>
<uniform>
<name>WindE</name>
<type>float</type>
<value>
<use>windE</use>
</value>
</uniform>
<uniform>
<name>WindN</name>
<type>float</type>
<value>
<use>windN</use>
</value>
</uniform>
<uniform>
<name>WaveFreq</name>
<type>float</type>
<value>
<use>WaveFreq</use>
</value>
</uniform>
<uniform>
<name>WaveAmp</name>
<type>float</type>
<value>
<use>WaveAmp</use>
</value>
</uniform>
<uniform>
<name>WaveSharp</name>
<type>float</type>
<value>
<use>WaveSharp</use>
</value>
</uniform>
<uniform>
<name>WaveAngle</name>
<type>float</type>
<value>
<use>WaveAngle</use>
</value>
</uniform>
<uniform>
<name>WaveFactor</name>
<type>float</type>
<value>
<use>WaveFactor</use>
</value>
</uniform>
<uniform>
<name>WaveDAngle</name>
<type>float</type>
<value>
<use>WaveDAngle</use>
</value>
</uniform>
<!-- BEGIN fog include -->
<uniform>
<name>visibility</name>
<type>float</type>
<value>
<use>visibility</use>
</value>
</uniform>
<uniform>
<name>avisibility</name>
<type>float</type>
<value>
<use>avisibility</use>
</value>
</uniform>
<uniform>
<name>hazeLayerAltitude</name>
<type>float</type>
<value>
<use>lthickness</use>
</value>
</uniform>
<uniform>
<name>scattering</name>
<type>float</type>
<value>
<use>scattering</use>
</value>
</uniform>
<uniform>
<name>terminator</name>
<type>float</type>
<value>
<use>terminator</use>
</value>
</uniform>
<uniform>
<name>fogType</name>
<type>int</type>
<value>
<use>fogtype</use>
</value>
</uniform>
<!-- END fog include -->
<uniform>
<name>rembrandt_enabled</name>
<type>int</type>
<value>
<use>rembrandt</use>
</value>
</uniform>
</pass>
</technique>
<technique n="7">
<predicate>
<and>
<property>/sim/rendering/shaders/water</property>
<property>/sim/rendering/rembrandt/enabled</property>
<less-equal>
<value type="float">2.0</value>
<float-property>/sim/rendering/shaders/water</float-property>
</less-equal>
<or>
<less-equal>
<value type="float">2.0</value>
<glversion/>
</less-equal>
<and>
<extension-supported>GL_ARB_shader_objects</extension-supported>
<extension-supported>GL_ARB_shading_language_100</extension-supported>
<extension-supported>GL_ARB_vertex_shader</extension-supported>
<extension-supported>GL_ARB_fragment_shader</extension-supported>
</and>
</or>
<extension-supported>GL_EXT_gpu_shader4</extension-supported>
</and>
</predicate>
<pass>
<material>
<ambient>
<use>material/ambient</use>
</ambient>
<diffuse>
<use>material/diffuse</use>
</diffuse>
<specular>
<use>material/specular</use>
</specular>
<emissive>
<use>material/emissive</use>
</emissive>
<shininess>
<use>material/shininess</use>
</shininess>
<color-mode>ambient-and-diffuse</color-mode>
</material>
<blend>
<use>transparent</use>
</blend>
<alpha-test>
<use>transparent</use>
</alpha-test>
<shade-model>smooth</shade-model>
<cull-face>back</cull-face>
<render-bin>
<bin-number>
<use>render-bin/bin-number</use>
</bin-number>
<bin-name>
<use>render-bin/bin-name</use>
</bin-name>
</render-bin>
<texture-unit>
<unit>0</unit>
<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>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>
<unit>5</unit>
<image>
<use>texture[5]/image</use>
</image>
<filter>
<use>texture[5]/filter</use>
</filter>
<wrap-s>
<use>texture[5]/wrap-s</use>
</wrap-s>
<wrap-t>
<use>texture[5]/wrap-t</use>
</wrap-t>
<internal-format>
<use>texture[5]/internal-format</use>
</internal-format>
</texture-unit>
<texture-unit>
<unit>6</unit>
<image>
<use>texture[6]/image</use>
</image>
<filter>
<use>texture[6]/filter</use>
</filter>
<wrap-s>
<use>texture[6]/wrap-s</use>
</wrap-s>
<wrap-t>
<use>texture[6]/wrap-t</use>
</wrap-t>
<internal-format>
<use>texture[6]/internal-format</use>
</internal-format>
</texture-unit>
<program>
<vertex-shader>Shaders/water.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>
</program>
<uniform>
<name>water_reflection</name>
<type>sampler-2d</type>
<value type="int">0</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_reflection_grey</name>
<type>sampler-2d</type>
<value type="int">4</value>
</uniform>
<uniform>
<name>sea_foam</name>
<type>sampler-2d</type>
<value type="int">5</value>
</uniform>
<uniform>
<name>perlin_normalmap</name>
<type>sampler-2d</type>
<value type="int">6</value>
</uniform>
<!-- normalmap is .dds-->
<uniform>
<name>normalmap_dds</name>
<type>float</type>
<value>
<use>normalmap_dds</use>
</value>
</uniform>
<uniform>
<name>saturation</name>
<type>float</type>
<!--<value>0.4</value>-->
<value>
<use>saturation</use>
</value>
</uniform>
<uniform>
<name>CloudCover0</name>
<type>float</type>
<value>
<use>cloud-cover[0]</use>
</value>
</uniform>
<uniform>
<name>CloudCover1</name>
<type>float</type>
<value>
<use>cloud-cover[1]</use>
</value>
</uniform>
<uniform>
<name>CloudCover2</name>
<type>float</type>
<value>
<use>cloud-cover[2]</use>
</value>
</uniform>
<uniform>
<name>CloudCover3</name>
<type>float</type>
<value>
<use>cloud-cover[3]</use>
</value>
</uniform>
<uniform>
<name>CloudCover4</name>
<type>float</type>
<value>
<use>cloud-cover[4]</use>
</value>
</uniform>
<uniform>
<name>Status</name>
<type>int</type>
<value>
<use>status</use>
</value>
</uniform>
<uniform>
<name>Overcast</name>
<type>float</type>
<value>
<use>overcast</use>
</value>
</uniform>
<uniform>
<name>WindE</name>
<type>float</type>
<value>
<use>windE</use>
</value>
</uniform>
<uniform>
<name>WindN</name>
<type>float</type>
<value>
<use>windN</use>
</value>
</uniform>
<!--uniform> unused?
<name>WindFrom</name>
<type>float</type>
<value>
<use>wind-from</use>
</value>
</uniform-->
<uniform>
<name>WaveFreq</name>
<type>float</type>
<value>
<use>WaveFreq</use>
</value>
</uniform>
<uniform>
<name>WaveAmp</name>
<type>float</type>
<value>
<use>WaveAmp</use>
</value>
</uniform>
<uniform>
<name>WaveSharp</name>
<type>float</type>
<value>
<use>WaveSharp</use>
</value>
</uniform>
<uniform>
<name>WaveAngle</name>
<type>float</type>
<value>
<use>WaveAngle</use>
</value>
</uniform>
<uniform>
<name>WaveFactor</name>
<type>float</type>
<value>
<use>WaveFactor</use>
</value>
</uniform>
<uniform>
<name>WaveDAngle</name>
<type>float</type>
<value>
<use>WaveDAngle</use>
</value>
</uniform>
<!-- BEGIN fog include -->
<uniform>
<name>visibility</name>
<type>float</type>
<value>
<use>visibility</use>
</value>
</uniform>
<uniform>
<name>avisibility</name>
<type>float</type>
<value>
<use>avisibility</use>
</value>
</uniform>
<uniform>
<name>hazeLayerAltitude</name>
<type>float</type>
<value>
<use>lthickness</use>
</value>
</uniform>
<uniform>
<name>scattering</name>
<type>float</type>
<value>
<use>scattering</use>
</value>
</uniform>
<uniform>
<name>terminator</name>
<type>float</type>
<value>
<use>terminator</use>
</value>
</uniform>
<uniform>
<name>fogType</name>
<type>int</type>
<value>
<use>fogtype</use>
</value>
</uniform>
<!-- END fog include -->
<uniform>
<name>rembrandt_enabled</name>
<type>int</type>
<value>
<use>rembrandt</use>
</value>
</uniform>
</pass>
</technique>
<technique n="8"> <technique n="8">
<predicate> <predicate>
@ -863,6 +1646,13 @@
</value> </value>
</uniform> </uniform>
<!-- END fog include --> <!-- END fog include -->
<uniform>
<name>rembrandt_enabled</name>
<type>int</type>
<value>
<use>rembrandt</use>
</value>
</uniform>
</pass> </pass>
</technique> </technique>
<technique n="9"> <technique n="9">
@ -1234,6 +2024,13 @@
</value> </value>
</uniform> </uniform>
<!-- END fog include --> <!-- END fog include -->
<uniform>
<name>rembrandt_enabled</name>
<type>int</type>
<value>
<use>rembrandt</use>
</value>
</uniform>
</pass> </pass>
</technique> </technique>
</PropertyList> </PropertyList>

213
Shaders/water-gbuffer.frag Normal file
View file

@ -0,0 +1,213 @@
// 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
// Optimisation - 2012-5-05
// Based on ideas by Thorsten Renk
// © Emilian Huminiuc and Vivian Meazza
#version 120
uniform sampler2D water_normalmap;
uniform sampler2D water_reflection;
uniform sampler2D water_dudvmap;
uniform sampler2D water_reflection_grey;
uniform sampler2D sea_foam;
uniform sampler2D perlin_normalmap;
uniform float saturation, Overcast, WindE, WindN;
uniform float CloudCover0, CloudCover1, CloudCover2, CloudCover3, 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 ;
uniform float normalmap_dds;
/////// 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,
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)
{
const vec4 sca = vec4(0.005, 0.005, 0.005, 0.005);
const vec4 sca2 = vec4(0.02, 0.02, 0.02, 0.02);
const vec4 tscale = vec4(0.25, 0.25, 0.25, 0.25);
mat4 RotationMatrix;
// 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);
const float water_shininess = 240.0;
// approximate cloud cover
float cover = 0.0;
//bool Status = true;
float windEffect = sqrt( WindE*WindE + WindN*WindN ) * 0.6; //wind speed in kt
float windScale = 15.0/(3.0 + windEffect); //wave scale
float windEffect_low = 0.3 + 0.7 * smoothstep(0.0, 5.0, windEffect); //low windspeed wave filter
float waveRoughness = 0.05 + smoothstep(0.0, 20.0, windEffect); //wave roughness filter
float mixFactor = 0.75 - 0.15 * smoothstep(0.0, 40.0, windEffect);
mixFactor = clamp(mixFactor, 0.3, 0.8);
if (Status == 1){
cover = min(min(min(min(CloudCover0, CloudCover1),CloudCover2),CloudCover3),CloudCover4);
} else {
// hack to allow for Overcast not to be set by Local Weather
if (Overcast == 0){
cover = 5;
} else {
cover = Overcast * 5;
}
}
vec4 viewt = vec4(-E, 0.0) * 0.6;
vec4 disdis = texture2D(water_dudvmap, vec2(waterTex2 * tscale)* windScale) * 2.0 - 1.0;
vec4 dist = texture2D(water_dudvmap, vec2(waterTex1 + disdis*sca2)* windScale) * 2.0 - 1.0;
dist *= (0.6 + 0.5 * smoothstep(0.0, 15.0, windEffect));
vec4 fdist = normalize(dist);
if (normalmap_dds > 0)
fdist = -fdist; //dds fix
fdist *= sca;
//normalmaps
rotationmatrix(radians(3.0 * windScale + 0.6 * sin(waterTex1.s * 0.2)), RotationMatrix);
vec4 nmap = texture2D(water_normalmap, vec2(waterTex1* RotationMatrix + disdis * sca2) * windScale) * 2.0 - 1.0;
vec4 nmap1 = texture2D(perlin_normalmap, vec2(waterTex1/** RotationMatrix*/ + disdis * sca2) * windScale) * 2.0 - 1.0;
rotationmatrix(radians(-2.0 * windScale -0.4 * sin(waterTex1.s * 0.32)), RotationMatrix);
nmap += texture2D(water_normalmap, vec2(waterTex1* RotationMatrix + disdis * sca2) * windScale * 1.5) * 2.0 - 1.0;
//nmap1 += texture2D(perlin_normalmap, vec2(waterTex1* RotationMatrix + disdis * sca2) * windScale) * 2.0 - 1.0;
rotationmatrix(radians(1.5 * windScale + 0.3 * sin(waterTex1.s * 0.16)), RotationMatrix);
nmap += texture2D(water_normalmap, vec2(waterTex1* RotationMatrix + disdis * sca2) * windScale * 2.1) * 2.0 - 1.0;
rotationmatrix(radians(-0.5 * windScale - 0.45 * sin(waterTex1.s * 0.28)), RotationMatrix);
nmap += texture2D(water_normalmap, vec2(waterTex1* RotationMatrix + disdis * sca2) * windScale * 0.8) * 2.0 - 1.0;
rotationmatrix(radians(-1.2 * windScale - 0.35 * sin(waterTex1.s * 0.28)), RotationMatrix);
nmap += texture2D(water_normalmap, vec2(waterTex2 * RotationMatrix* tscale) * windScale * 1.7) * 2.0 - 1.0;
nmap1 += texture2D(perlin_normalmap, vec2(waterTex2/** RotationMatrix*/ * tscale) * windScale) * 2.0 - 1.0;
nmap *= windEffect_low;
nmap1 *= windEffect_low;
// mix water and noise, modulated by factor
vec4 vNorm = normalize(mix(nmap, nmap1, mixFactor) * waveRoughness);
if (normalmap_dds > 0)
vNorm = -vNorm; //dds fix
//load reflection
vec4 tmp = vec4(lightdir, 0.0);
vec4 refTex = texture2D(water_reflection, vec2(tmp + waterTex1) * 32.0) ;
vec4 refTexGrey = texture2D(water_reflection_grey, vec2(tmp + waterTex1) * 32.0) ;
vec4 refl ;
// cover = 0;
if(cover >= 1.5){
refl = normalize(refTex);
refl.a = 1.0;
}
else
{
refl = normalize(refTexGrey);
refl.r *= (0.75 + 0.15 * cover);
refl.g *= (0.80 + 0.15 * cover);
refl.b *= (0.875 + 0.125 * cover);
refl.a = 1.0;
}
rotationmatrix(radians(2.1* windScale + 0.25 * sin(waterTex1.s *0.14)), RotationMatrix);
vec3 N0 = vec3(texture2D(water_normalmap, vec2(waterTex1* RotationMatrix + disdis * sca2) * windScale * 1.15) * 2.0 - 1.0);
vec3 N1 = vec3(texture2D(perlin_normalmap, vec2(waterTex1/** RotationMatrix*/ + disdis * sca) * windScale) * 2.0 - 1.0);
rotationmatrix(radians(-1.5 * windScale -0.32 * sin(waterTex1.s *0.24)), RotationMatrix);
N0 += vec3(texture2D(water_normalmap, vec2(waterTex2* RotationMatrix * tscale) * windScale * 1.8) * 2.0 - 1.0);
N1 += vec3(texture2D(perlin_normalmap, vec2(waterTex2/** RotationMatrix*/ * tscale) * windScale) * 2.0 - 1.0);
rotationmatrix(radians(3.8 * windScale + 0.45 * sin(waterTex1.s *0.32)), RotationMatrix);
N0 += vec3(texture2D(water_normalmap, vec2(waterTex2 * RotationMatrix * (tscale + sca2)) * windScale * 0.85) * 2.0 - 1.0);
N1 += vec3(texture2D(perlin_normalmap, vec2(waterTex2/** RotationMatrix*/ * (tscale + sca2)) * windScale) * 2.0 - 1.0);
rotationmatrix(radians(-2.8 * windScale - 0.38 * sin(waterTex1.s * 0.26)), RotationMatrix);
N0 += vec3(texture2D(water_normalmap, vec2(waterTex1 * RotationMatrix + disdis * sca2) * windScale * 2.1) * 2.0 - 1.0);
N1 += vec3(texture2D(perlin_normalmap, vec2(waterTex1 /** RotationMatrix*/ + disdis * sca) * windScale) * 2.0 - 1.0);
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
// specular
//vec3 specular_color = vec3(gl_LightSource[0].diffuse)
// * pow(max(0.0, dot(N, H)), water_shininess) * 6.0;
//vec4 specular = vec4(specular_color, 0.5);
//specular = specular * saturation * 0.3 ;
float specular = saturation * 0.3;
//calculate fresnel
vec4 invfres = vec4( dot(vNorm, viewt) );
vec4 fres = vec4(1.0) + invfres;
refl *= fres;
//calculate final colour
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;
if (windEffect >= 8.0)
if (waveSlope >= foamSlope){
finalColor = mix(finalColor, max(finalColor, finalColor + foam_texel), smoothstep(0.01, 0.50, N.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,
vec3( 0.3, 0.59, 0.11 ) );
encode_gbuffer(N, finalColor.rgb, 1, specular, water_shininess, emission, gl_FragCoord.z);
}

View file

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

View file

@ -0,0 +1,388 @@
// 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
// Optimisation - 2012-5-05
// Based on ideas by Thorsten Renk
// Emilian Huminiuc and Vivian Meazza
#version 120
uniform sampler2D water_normalmap;
uniform sampler2D water_reflection;
uniform sampler2D water_dudvmap;
uniform sampler2D water_reflection_grey;
uniform sampler2D sea_foam;
uniform sampler2D perlin_normalmap;
uniform sampler3D Noise;
uniform float saturation, Overcast, WindE, WindN;
uniform float CloudCover0, CloudCover1, CloudCover2, CloudCover3, 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 ;
uniform float WaveAngle ;
uniform float WaveFactor ;
uniform float WaveDAngle ;
uniform float normalmap_dds;
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,
sin( angle ), cos( angle ), 0.0, 0.0,
0.0 , 0.0 , 1.0, 0.0,
0.0 , 0.0 , 0.0, 1.0 );
}
// wave functions ///////////////////////
struct Wave {
float freq; // 2*PI / wavelength
float amp; // amplitude
float phase; // speed * 2*PI / wavelength
vec2 dir;
};
Wave wave0 = Wave(1.0, 1.0, 0.5, vec2(0.97, 0.25));
Wave wave1 = Wave(2.0, 0.5, 1.3, vec2(0.97, -0.25));
Wave wave2 = Wave(1.0, 1.0, 0.6, vec2(0.95, -0.3));
Wave wave3 = Wave(2.0, 0.5, 1.4, vec2(0.99, 0.1));
float evaluateWave(in Wave w, vec2 pos, float t)
{
return w.amp * sin( dot(w.dir, pos) * w.freq + t * w.phase);
}
// derivative of wave function
float evaluateWaveDeriv(Wave w, vec2 pos, float t)
{
return w.freq * w.amp * cos( dot(w.dir, pos)*w.freq + t*w.phase);
}
// sharp wave functions
float evaluateWaveSharp(Wave w, vec2 pos, float t, float k)
{
return w.amp * pow(sin( dot(w.dir, pos)*w.freq + t*w.phase)* 0.5 + 0.5 , k);
}
float evaluateWaveDerivSharp(Wave w, vec2 pos, float t, float k)
{
return k*w.freq*w.amp * pow(sin( dot(w.dir, pos)*w.freq + t*w.phase)* 0.5 + 0.5 , k - 1) * cos( dot(w.dir, pos)*w.freq + t*w.phase);
}
void sumWaves(float angle, float dangle, float windScale, float factor, out float ddx, float ddy)
{
mat4 RotationMatrix;
float deriv;
vec4 P = waterTex1 * 1024.0;
rotationmatrix(radians(angle + dangle * windScale + 0.6 * sin(P.x * factor)), RotationMatrix);
P *= RotationMatrix;
P.y += evaluateWave(wave0, P.xz, osg_SimulationTime);
deriv = evaluateWaveDeriv(wave0, P.xz, osg_SimulationTime );
ddx = deriv * wave0.dir.x;
ddy = deriv * wave0.dir.y;
P.y += evaluateWave(wave1, P.xz, osg_SimulationTime);
deriv = evaluateWaveDeriv(wave1, P.xz, osg_SimulationTime);
ddx += deriv * wave1.dir.x;
ddy += deriv * wave1.dir.y;
P.y += evaluateWaveSharp(wave2, P.xz, osg_SimulationTime, WaveSharp);
deriv = evaluateWaveDerivSharp(wave2, P.xz, osg_SimulationTime, WaveSharp);
ddx += deriv * wave2.dir.x;
ddy += deriv * wave2.dir.y;
P.y += evaluateWaveSharp(wave3, P.xz, osg_SimulationTime, WaveSharp);
deriv = evaluateWaveDerivSharp(wave3, P.xz, osg_SimulationTime, WaveSharp);
ddx += deriv * wave3.dir.x;
ddy += deriv * wave3.dir.y;
}
void main(void)
{
const vec4 sca = vec4(0.005, 0.005, 0.005, 0.005);
const vec4 sca2 = vec4(0.02, 0.02, 0.02, 0.02);
const vec4 tscale = vec4(0.25, 0.25, 0.25, 0.25);
mat4 RotationMatrix;
// 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);
const float water_shininess = 240.0;
// float range = gl_ProjectionMatrix[3].z/(gl_FragCoord.z * -2.0 + 1.0 - gl_ProjectionMatrix[2].z);
// approximate cloud cover
float cover = 0.0;
//bool Status = true;
float windEffect = sqrt( WindE*WindE + WindN*WindN ) * 0.6; //wind speed in kt
float windScale = 15.0/(3.0 + windEffect); //wave scale
float windEffect_low = 0.3 + 0.7 * smoothstep(0.0, 5.0, windEffect); //low windspeed wave filter
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
// Test data
//float WaveFreq =1.0;
//float WaveAmp = 1000.0;
//float WaveSharp = 10.0;
vec4 ddxVec = vec4(0.0);
vec4 ddyVec = vec4(0.0);
int detailFlag = 0;
//uncomment to test
//range = -20000;
//if (range > -15000 || dot(Normal,H) > 0.95 ) {
float ddx = 0.0, ddy = 0.0;
float ddx1 = 0.0, ddy1 = 0.0;
float ddx2 = 0.0, ddy2 = 0.0;
float ddx3 = 0.0, ddy3 = 0.0;
float waveamp;
float angle = 0.0;
wave0.freq = WaveFreq ;
wave0.amp = WaveAmp;
wave0.dir = vec2(cos(radians(angle)), sin(radians(angle)));
angle -= 45;
wave1.freq = WaveFreq * 2.0 ;
wave1.amp = WaveAmp * 1.25;
wave1.dir = vec2(cos(radians(angle)), sin(radians(angle)));
angle += 30;
wave2.freq = WaveFreq * 3.5;
wave2.amp = WaveAmp * 0.75;
wave2.dir = vec2(cos(radians(angle)), sin(radians(angle)));
angle -= 50;
wave3.freq = WaveFreq * 3.0 ;
wave3.amp = WaveAmp * 0.75;
wave3.dir = vec2(cos(radians(angle)), sin(radians(angle)));
// sum waves
ddx = 0.0, ddy = 0.0;
sumWaves(WaveAngle, -1.5, windScale, WaveFactor, ddx, ddy);
ddx1 = 0.0, ddy1 = 0.0;
sumWaves(WaveAngle, 1.5, windScale, WaveFactor, ddx1, ddy1);
//reset the waves
angle = 0.0;
waveamp = WaveAmp * 0.75;
wave0.freq = WaveFreq ;
wave0.amp = waveamp;
wave0.dir = vec2(cos(radians(angle)), sin(radians(angle)));
angle -= 20;
wave1.freq = WaveFreq * 2.0 ;
wave1.amp = waveamp * 1.25;
wave1.dir = vec2(cos(radians(angle)), sin(radians(angle)));
angle += 35;
wave2.freq = WaveFreq * 3.5;
wave2.amp = waveamp * 0.75;
wave2.dir = vec2(cos(radians(angle)), sin(radians(angle)));
angle -= 45;
wave3.freq = WaveFreq * 3.0 ;
wave3.amp = waveamp * 0.75;
wave3.dir = vec2(cos(radians(angle)), sin(radians(angle)));
// sum waves
ddx2 = 0.0, ddy2 = 0.0;
sumWaves(WaveAngle + WaveDAngle, -1.5, windScale, WaveFactor, ddx2, ddy2);
ddx3 = 0.0, ddy3 = 0.0;
sumWaves(WaveAngle + WaveDAngle, 1.5, windScale, WaveFactor, ddx3, ddy3);
ddxVec = vec4(ddx, ddx1, ddx2, ddx3);
ddyVec = vec4(ddy, ddy1, ddy2, ddy3);
//toggle detailFlag
detailFlag = 1;
//} // end sine stuff
float ddxSum = dot(ddxVec, AllOnes);
float ddySum = dot(ddyVec, AllOnes);
if (Status == 1){
cover = min(min(min(min(CloudCover0, CloudCover1),CloudCover2),CloudCover3),CloudCover4);
} else {
// hack to allow for Overcast not to be set by Local Weather
if (Overcast == 0){
cover = 5;
} else {
cover = Overcast * 5;
}
}
// vec4 viewt = normalize(waterTex4);
vec4 viewt = vec4(-E, 0.0) * 0.6;
vec4 disdis = texture2D(water_dudvmap, vec2(waterTex2 * tscale)* windScale) * 2.0 - 1.0;
//normalmaps
vec4 nmap = texture2D(water_normalmap, vec2(waterTex1 + disdis * sca2) * windScale) * 2.0 - 1.0;
vec4 nmap1 = texture2D(perlin_normalmap, vec2(waterTex1 + disdis * sca2) * windScale) * 2.0 - 1.0;
rotationmatrix(radians(3.0 * sin(osg_SimulationTime * 0.0075)), RotationMatrix);
nmap += texture2D(water_normalmap, vec2(waterTex2 * RotationMatrix * tscale) * windScale) * 2.0 - 1.0;
nmap1 += texture2D(perlin_normalmap, vec2(waterTex2 * RotationMatrix * tscale) * windScale) * 2.0 - 1.0;
nmap *= windEffect_low;
nmap1 *= windEffect_low;
// mix water and noise, modulated by factor
vec4 vNorm = normalize(mix(nmap, nmap1, mixFactor) * waveRoughness);
vNorm.r += ddxSum;
if (normalmap_dds > 0)
vNorm = -vNorm; //dds fix
//load reflection
//vec4 tmp = vec4(lightdir, 0.0);
vec4 tmp = vec4(0.0);
vec4 refTex = texture2D(water_reflection, vec2(tmp + waterTex1) * 32.0) ;
vec4 refTexGrey = texture2D(water_reflection_grey, vec2(tmp + waterTex1) * 32.0) ;
vec4 refl ;
// Test data
// cover = 0;
if(cover >= 1.5){
refl = normalize(refTex);
refl.a = 1.0;
}
else
{
refl = normalize(refTexGrey);
refl.r *= (0.75 + 0.15 * cover);
refl.g *= (0.80 + 0.15 * cover);
refl.b *= (0.875 + 0.125 * cover);
refl.a = 1.0;
}
vec3 N0 = vec3(texture2D(water_normalmap, vec2(waterTex1 + disdis * sca2) * windScale) * 2.0 - 1.0);
vec3 N1 = vec3(texture2D(perlin_normalmap, vec2(waterTex1 + disdis * sca) * windScale) * 2.0 - 1.0);
N0 += vec3(texture2D(water_normalmap, vec2(waterTex1 * tscale) * windScale) * 2.0 - 1.0);
N1 += vec3(texture2D(perlin_normalmap, vec2(waterTex2 * tscale) * windScale) * 2.0 - 1.0);
rotationmatrix(radians(2.0 * sin(osg_SimulationTime * 0.005)), RotationMatrix);
N0 += vec3(texture2D(water_normalmap, vec2(waterTex2 * RotationMatrix * (tscale + sca2)) * windScale) * 2.0 - 1.0);
N1 += vec3(texture2D(perlin_normalmap, vec2(waterTex2 * RotationMatrix * (tscale + sca2)) * windScale) * 2.0 - 1.0);
rotationmatrix(radians(-4.0 * sin(osg_SimulationTime * 0.003)), RotationMatrix);
N0 += vec3(texture2D(water_normalmap, vec2(waterTex1 * RotationMatrix + disdis * sca2) * windScale) * 2.0 - 1.0);
N1 += vec3(texture2D(perlin_normalmap, vec2(waterTex1 * RotationMatrix + disdis * sca) * windScale) * 2.0 - 1.0);
//if(detailFlag > 0)
//{
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
//}
// 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;
//calculate fresnel
vec4 invfres = vec4( dot(vNorm, viewt) );
vec4 fres = vec4(1.0) + invfres;
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;
if (windEffect >= 8.0)
if (waveSlope >= foamSlope){
finalColor = mix( finalColor,
max(finalColor, finalColor + foam_texel),
smoothstep(0.01, 0.50, Normal.g)
);
}
// } // end range
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);
}