PDA

View Full Version : exp2 Fog and direction in GLSL



Nick's a Novice
05-21-2006, 01:47 AM
Hi, I have a giant single quad that I'm using as a surface of water. I'm using the orange books implementation of the wobble shader from chapter 16 to create a really cool effect. I need to add fog on the top of this shader. I tried doing this the way the orange book recommends with ecPosition.z for the fog frag coord but this only seems to work properly if I'm facing a certain direction. When you face that direction it's amazing, but any other way and the fog is far too thick. Here are my shaders, can anyone help?
vs:

void main(void)
{
gl_Position = ftransform();

vec3 ecPosition = vec3(gl_ModelViewMatrix * gl_Vertex);
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_FogFragCoord = abs(ecPosition.z);
} fs:

const float C_PI = 3.1415;
const float C_2PI = 2.0 * C_PI;
const float C_2PI_I = 1.0 / (2.0 * C_PI);
const float C_PI_2 = C_PI / 2.0;

uniform float StartRad;
uniform vec2 Freq;
uniform vec2 Amplitude;
float fog;

uniform sampler2D WobbleTex;
uniform int mode;

void main (void)
{
if (mode==1)
{
vec2 perturb;
float rad;
vec4 color;

// Compute a perturbation factor for the x-direction
rad = (gl_TexCoord[0].s + gl_TexCoord[0].t - 1.0 + StartRad) * Freq.x;

// Wrap to -2.0*PI, 2*PI
rad = rad * C_2PI_I;
rad = fract(rad);
rad = rad * C_2PI;

// Center in -PI, PI
if (rad > C_PI) rad = rad - C_2PI;
if (rad < -C_PI) rad = rad + C_2PI;

// Center in -PI/2, PI/2
if (rad > C_PI_2) rad = C_PI - rad;
if (rad < -C_PI_2) rad = -C_PI - rad;

perturb.x = (rad - (rad * rad * rad / 6.0)) * Amplitude.x;

// Now compute a perturbation factor for the y-direction
rad = (gl_TexCoord[0].s - gl_TexCoord[0].t + StartRad) * Freq.y;

// Wrap to -2*PI, 2*PI
rad = rad * C_2PI_I;
rad = fract(rad);
rad = rad * C_2PI;

// Center in -PI, PI
if (rad > C_PI) rad = rad - C_2PI;
if (rad < -C_PI) rad = rad + C_2PI;

// Center in -PI/2, PI/2
if (rad > C_PI_2) rad = C_PI - rad;
if (rad < -C_PI_2) rad = -C_PI - rad;

perturb.y = (rad - (rad * rad * rad / 6.0)) * Amplitude.y;

color = texture2D(WobbleTex, perturb + gl_TexCoord[0].st);
color = vec4(mix(color.rgb,vec3(0.0, 0.08, 0.2),0.72), 0.72);
fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);
fog = clamp(fog, 0.0, 1.0);
color.rgb = mix(vec3(gl_Fog.color),color.rgb, fog);
gl_FragColor = color;
}
else
discard;
}