1
0
Fork 0

Standardize fog

Signed-off-by: Vivian Meazza <vivian.meazza@lineone.net>
This commit is contained in:
Vivian Meazza 2011-12-01 21:03:40 +00:00
parent 4f4a918064
commit b1a777b12d
5 changed files with 150 additions and 53 deletions

View file

@ -1,12 +1,19 @@
// -*-C++-*-
// Ambient term comes in gl_Color.rgb.
#version 120
varying vec4 diffuse_term;
varying vec3 normal;
varying float fogCoord;
uniform sampler2D texture;
////fog "include" /////
uniform int fogType;
vec3 fog_Func(vec3 color, int type);
//////////////////////
float luminance(vec3 color)
{
return dot(vec3(0.212671, 0.715160, 0.072169), color);
@ -44,6 +51,7 @@ void main()
color = clamp(color, 0.0, 1.0);
texel = texture2D(texture, gl_TexCoord[0].st);
fragColor = color * texel + specular;
fogFactor = exp(-gl_Fog.density * gl_Fog.density * fogCoord * fogCoord);
gl_FragColor = mix(gl_Fog.color, fragColor, fogFactor);
}
fragColor.rgb = fog_Func(fragColor.rgb, fogType);
gl_FragColor = fragColor;
}

View file

@ -7,7 +7,7 @@
//
// Diffuse colors come from the gl_Color, ambient from the material. This is
// equivalent to osg::Material::DIFFUSE.
#version 120
#define MODE_OFF 0
#define MODE_DIFFUSE 1
#define MODE_AMBIENT_AND_DIFFUSE 2
@ -18,12 +18,17 @@
// bugs with gl_FrontFacing in the fragment shader.
varying vec4 diffuse_term;
varying vec3 normal;
varying float fogCoord;
uniform int colorMode;
////fog "include"////////
uniform int fogType;
void fog_Func(int type);
/////////////////////////
void main()
{
vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;
gl_Position = ftransform();
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
normal = gl_NormalMatrix * gl_Normal;
@ -51,5 +56,6 @@ void main()
// gl_FrontFacing in the fragment shader.
gl_FrontColor.rgb = constant_term.rgb; gl_FrontColor.a = 1.0;
gl_BackColor.rgb = constant_term.rgb; gl_BackColor.a = 0.0;
fogCoord = abs(ecPosition.z / ecPosition.w);
//fogCoord = abs(ecPosition.z / ecPosition.w);
fog_Func(fogType);
}

View file

@ -1,4 +1,5 @@
// -*-C++-*-
// © Vivian Meazza - 2011
// Shader that uses OpenGL state values to do per-pixel lighting
//
@ -8,6 +9,9 @@
// Diffuse colors come from the gl_Color, ambient from the material. This is
// equivalent to osg::Material::DIFFUSE.
#version 120
#define fps2kts 0.5925
#define MODE_OFF 0
#define MODE_DIFFUSE 1
#define MODE_AMBIENT_AND_DIFFUSE 2
@ -18,15 +22,69 @@
// bugs with gl_FrontFacing in the fragment shader.
varying vec4 diffuse_term;
varying vec3 normal;
varying float fogCoord;
//varying float fogCoord;
uniform int colorMode;
uniform float osg_SimulationTime;
uniform float WindSpeed, Offset, AmpFactor;
uniform float Offset, AmpFactor, WindE, WindN, spd, hdg;
uniform sampler3D Noise;
////fog "include"////////
uniform int fogType;
void fog_Func(int type);
/////////////////////////
/////// functions /////////
float normalize_range(float _val)
{
if (_val > 180.0)
return _val - 360.0;
else
return _val;
}
void relWind(out float rel_wind_speed_kts, out float rel_wind_from_rad)
{
//calculate speed north and east in kts
float speed_north_kts = cos(radians(hdg)) * spd ;
float speed_east_kts = sin(radians(hdg)) * spd ;
//calculate the relative wind speed north and east in kts
float rel_wind_speed_from_east_kts = WindE*fps2kts + speed_east_kts;
float rel_wind_speed_from_north_kts = WindN*fps2kts + speed_north_kts;
//combine relative speeds north and east to get relative windspeed in kts
rel_wind_speed_kts = sqrt(pow(abs(rel_wind_speed_from_east_kts), 2)
+ pow(abs(rel_wind_speed_from_north_kts), 2));
//calculate the relative wind direction
float rel_wind_from_deg = degrees(atan(rel_wind_speed_from_east_kts, rel_wind_speed_from_north_kts));
//rel_wind_from_rad = atan(rel_wind_speed_from_east_kts, rel_wind_speed_from_north_kts);
float rel_wind = rel_wind_from_deg - hdg;
rel_wind = normalize_range(rel_wind);
rel_wind_from_rad = radians(rel_wind);
}
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()
{
{
mat4 RotationMatrix;
float relWindspd=0;
float relWinddir=0;
// compute relative wind speed and direction
relWind (relWindspd, relWinddir);
// map noise vector
vec4 noisevec = texture3D(Noise, gl_Vertex.xyz);
@ -35,14 +93,18 @@ void main()
vec4 pos = gl_Vertex;
vec4 oldpos = gl_Vertex;
float freq = (10 * WindSpeed) + 10;
float freq = (10 * relWindspd) + 10;
pos.y = sin((pos.x * 5.0 + tsec * freq )/5.0) * 0.5 ;
pos.y += sin((pos.z * 5.0 + tsec * freq/2)/5.0) * 0.125 ;
pos.y *= pow(pos.x - Offset, 2.0) * AmpFactor;
//rotate the flag to align with relative wind
rotationmatrix(-relWinddir, RotationMatrix);
pos *= RotationMatrix;
gl_Position = gl_ModelViewProjectionMatrix * pos;
//do the colour and fog
vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
@ -52,28 +114,30 @@ void main()
if (colorMode == MODE_DIFFUSE) {
diffuse_color = gl_Color;
ambient_color = gl_FrontMaterial.ambient;
} else if (colorMode == MODE_AMBIENT_AND_DIFFUSE) {
diffuse_color = gl_Color;
ambient_color = gl_Color;
} else {
diffuse_color = gl_FrontMaterial.diffuse;
ambient_color = gl_FrontMaterial.ambient;
} else if (colorMode == MODE_AMBIENT_AND_DIFFUSE) {
diffuse_color = gl_Color;
ambient_color = gl_Color;
} else {
diffuse_color = gl_FrontMaterial.diffuse;
ambient_color = gl_FrontMaterial.ambient;
}
diffuse_term = diffuse_color * gl_LightSource[0].diffuse;
vec4 ambient_term = ambient_color * gl_LightSource[0].ambient;
// Super hack: if diffuse material alpha is less than 1, assume a
// transparency animation is at work
if (gl_FrontMaterial.diffuse.a < 1.0)
diffuse_term.a = gl_FrontMaterial.diffuse.a;
else
diffuse_term.a = gl_Color.a;
// Another hack for supporting two-sided lighting without using
// gl_FrontFacing in the fragment shader.
gl_FrontColor.rgb = ambient_term.rgb; gl_FrontColor.a = 0.0;
gl_BackColor.rgb = ambient_term.rgb; gl_FrontColor.a = 1.0;
// fogCoord = abs(ecPosition.z / ecPosition.w);
fog_Func(fogType);
}
diffuse_term = diffuse_color * gl_LightSource[0].diffuse;
vec4 ambient_term = ambient_color * gl_LightSource[0].ambient;
// Super hack: if diffuse material alpha is less than 1, assume a
// transparency animation is at work
if (gl_FrontMaterial.diffuse.a < 1.0)
diffuse_term.a = gl_FrontMaterial.diffuse.a;
else
diffuse_term.a = gl_Color.a;
// Another hack for supporting two-sided lighting without using
// gl_FrontFacing in the fragment shader.
gl_FrontColor.rgb = ambient_term.rgb; gl_FrontColor.a = 0.0;
gl_BackColor.rgb = ambient_term.rgb; gl_FrontColor.a = 1.0;
fogCoord = abs(ecPosition.z / ecPosition.w);
}

View file

@ -31,21 +31,29 @@ attribute vec3 binormal;
uniform float canopy_height;
void main(void)
{
rawposIn = gl_Vertex;
ecPosIn = gl_ModelViewMatrix * gl_Vertex;
NormalIn = normalize(gl_Normal);
//rawTopIn = rawposIn + vec4(0.0, 0.0, canopy_height, 0.0);
//ecTopIn = gl_ModelViewMatrix * rawTopIn;
ecNormalIn = gl_NormalMatrix * NormalIn;
VTangentIn = gl_NormalMatrix * tangent;
VBinormalIn = gl_NormalMatrix * binormal;
////fog "include"////////
uniform int fogType;
gl_FrontColor = gl_Color;
gl_Position = ftransform();
//positionTopIn = gl_ModelViewProjectionMatrix * rawTopIn;
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
constantColorIn = gl_FrontMaterial.emission
+ gl_Color * (gl_LightModel.ambient + gl_LightSource[0].ambient);
}
void fog_Func(int type);
/////////////////////////
void main(void)
{
rawposIn = gl_Vertex;
ecPosIn = gl_ModelViewMatrix * gl_Vertex;
NormalIn = normalize(gl_Normal);
//rawTopIn = rawposIn + vec4(0.0, 0.0, canopy_height, 0.0);
//ecTopIn = gl_ModelViewMatrix * rawTopIn;
ecNormalIn = gl_NormalMatrix * NormalIn;
VTangentIn = gl_NormalMatrix * tangent;
VBinormalIn = gl_NormalMatrix * binormal;
gl_FrontColor = gl_Color;
gl_Position = ftransform();
//positionTopIn = gl_ModelViewProjectionMatrix * rawTopIn;
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
constantColorIn = gl_FrontMaterial.emission
+ gl_Color * (gl_LightModel.ambient + gl_LightSource[0].ambient);
fog_Func(fogType);
}

View file

@ -1,15 +1,26 @@
// -*-C++-*-
uniform sampler2D texture;
////fog "include" /////
uniform int fogType;
vec3 fog_Func(vec3 color, int type);
//////////////////////
void main()
{
vec4 color = vec4(1.0, 1.0, 1.0, 1.0);
vec4 texel;
vec4 fragColor;
vec4 finalColor;
texel = texture2D(texture, gl_TexCoord[0].st);
fragColor = color * texel;
gl_FragColor = fragColor;
finalColor.rgb = fog_Func(fragColor.rgb, fogType);
gl_FragColor = finalColor;
}