# Problem making water, dudv image map keeps translating to the left

I’m attempting to create water using a dudv map, but having trouble with the waving effect. I managed to create the effect but as time goes by, the wave effect moves to the left of the quad until disappearing. I’ll show you an example and if you look a carefully, you’ll see what I’m talking about, here’s a screen cap:

http://recordit.co/F0dt7H63px

I’m learning how to make water so please excuse me. I’m suspecting this has to do with the refractionTexCoords += totalDistortion.xy line, but I’m not sure how to avoid this visual bug. I’ll share the portion of the frag shader that is in charge with movig the water. Thanks in advance!

``````
// Oh and this is how I compute the clipSpace in the vertex shader: clipSpace = mvp * a_position;

vec2 ndc = (clipSpace.xy/clipSpace.w) / 2.0 + 0.5;

vec2 reflectionTexCoords = vec2(ndc.x, -ndc.y);
vec2 refractionTexCoords = vec2(ndc.x, ndc.y);
vec4 distortion1        = (texture2D(dudv, vec2(newTexCoords.x  + moveFactor, newTexCoords.y).rg * 2.0 - 1.0)) *  waveStr;
vec4 distortion2        = (texture2D(dudv, vec2(-newTexCoords.x + moveFactor, newTexCoords.y + moveFactor).rg * 2.0 - 1.0)) *  waveStr;
vec4 totalDistortion    = distortion1 + distortion2;

refractionTexCoords += totalDistortion.xy;
refractionTexCoords = clamp(refractionTexCoords, 0.001, 0.999);

reflectionTexCoords += totalDistortion.xy;
reflectionTexCoords.x = clamp(reflectionTexCoords.x, 0.001, 0.999);
reflectionTexCoords.y = clamp(reflectionTexCoords.y, -0.999, -0.001);

vec4 reflectionTex  = texture2D(reflection, reflectionTexCoords);
vec4 refractionTex  = texture2D(refraction, refractionTexCoords);

vec4 finalColor = mix(refractionTex, reflectionTex, 0.5);
finalColor = mix(finalColor, vec4(0.0, 0.3, 0.2, 1.0), 0.5);

gl_FragColor = finalColor;

``````

Update:

I recorded another video but without the waving effect, if you notice the borders of the water texture tile moves, but initially matched. I’m starting to think I have two problems here

http://recordit.co/BqftsO0FCu

Is applying the unsigned->signed mapping to the texture coordinates intentional? Or did you intend to apply it to the values retrieved from the texture instead?

``````
vec2 distortion1        = (texture2D(dudv, vec2(newTexCoords.x  + moveFactor, newTexCoords.y)).rg * 2.0 - 1.0) *  waveStr;

``````

[QUOTE=GClements;1287584]Is applying the unsigned->signed mapping to the texture coordinates intentional? Or did you intend to apply it to the values retrieved from the texture instead?

``````
vec2 distortion1        = (texture2D(dudv, vec2(newTexCoords.x  + moveFactor, newTexCoords.y)).rg * 2.0 - 1.0) *  waveStr;

``````

[/QUOTE]

Hi GClements, if I understand correctly, I was planning on retrieving the values from the dudv texture for the distortion.

Oh I left out what newTexCoords is, my apologies: vec2(a_texCoords.x / 2.0 + 0.5, a_texCoords.y/2.0 + 0.5) * 1.0;

But does the dudv texture use a signed format? Or is it intentional that the distortion vector is always in the first quadrant (X/Y both positive)?

Sorry GC, it’s intentional that the distortion vector is the first quadrant. The dudv texture i got provides unsigned values. My apologies if I don’t seem very clear, I’ve been learning slowly with a tutorial.

I forgot to mention, I’m starting to think that the problem with the dudv texture moving has to do with not tiling the texture properly. I tried to render only the dudv texture and as soon it moves out of it’s original position, it looks stretched:

For water, I’d say it should be tiled so it waves repeatedly. But for some reason when I try to tile dudv image from the vertex shader, anything over 1 would look streched.

``````
// vertex shader
newTexCoords 	= vec2(a_texCoords.x / 2.0 + 0.5, a_texCoords.y/2.0 + 0.5) * tile;

// Frag Shader
// Then I use it here
vec4 distortion1   = texture2D(dudv, vec2(newTexCoords.x  + 0.0, newTexCoords.y).rg * 2.0 - 1.0) *  waveStr;

``````

I figured it out. The dudv map didn’t have the right texture parameters. I found this out by looking at the 3 textures I was passing and the only one that seemed off was the dudv map, so clearly I had to fix something I was doing with OpenGL, not glsl.

Thanks!

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.