hi,
I have a shader that computes a displacement map to be applied to a VBO.
In the main() function you can see I use a texture (heightmap), and add some sine waves for effect.
Here is the problem :
void main()
{
float d = amplitude * displace( gl_TexCoord[0].xy );
float c = texture2D(tex, gl_TexCoord[0]).r;
gl_FragColor = vec4(d , 0.0, 0.0, 1.0 );
}
The above works. The wave texture (d) is generated, c (the heightmap) is not used.
void main()
{
float d = amplitude * displace( gl_TexCoord[0].xy );
float c = texture2D(tex, gl_TexCoord[0]).r;
gl_FragColor = vec4(c, 0.0, 0.0, 1.0 );
}
The above works. c (the heightmap) is successfully used to the texture.
void main()
{
float d = amplitude * displace( gl_TexCoord[0].xy );
float c = texture2D(tex, gl_TexCoord[0]).r;
gl_FragColor = vec4( c + d, 0.0, 0.0, 1.0 );
}
The above does not work. Only d (the sine waves) is taken into account, somehow c (the heightmap) is completely ignored.
Here is the full code :
#version 120
uniform float time;
uniform float amplitude;
uniform sampler2D tex;
float wave( float period )
{
return sin( period * 6.283185 );
}
// calculate displacement based on uv coordinate
float displace( vec2 uv )
{
// large up and down movement
float d = wave( (uv.x * 0.5) - time * 0.01 );
// add a large wave from left to right
d -= 0.9 * wave( (uv.x * 0.9) - time * 0.04 );
// add diagonal waves from back to front
d -= 0.25 * wave( ((uv.x + uv.y) * 2.2) - time * 0.05 );
// add additional waves for increased complexity
d += 0.25 * wave( (uv.y * 1.2) - time * 0.01 );
d -= 0.15 * wave( ((uv.y + uv.x) * 2.8) - time * 0.09 );
d += 0.15 * wave( ((uv.y - uv.x) * 1.9) - time * 0.08 );
d += 0.20 * wave( (uv.y * 2.5) - time * 0.04);
d += 0.35 * wave( (uv.y * 1.2) - time * 0.03 );
d -= 0.40 * wave( ((uv.y + uv.x) * 2.8) - time * 0.1 );
d += 0.60 * wave( ((uv.y - uv.x) * 1.9) - time * 0.2 );
d += 0.50 * wave( (uv.y * 2.5) - time * 0.06);
return d;
}
void main()
{
float d = amplitude * displace( gl_TexCoord[0].xy );
float c = texture2D(tex, gl_TexCoord[0]).r;
gl_FragColor = vec4( c + d, 0.0, 0.0, 1.0 );
}