I'm trying to apply a DuDv Map effect (distortions) on my water.

My texture is 512x512 in size.

I'm using the following code:

(Vertex Shader)

out vec2 texCoord0;

void main()...

texCoord0 = vec2(position.x / 2.0 + 0.5, position.y / 2.0 + 0.5) * 6.0;

(Fragment Shader)

in vec2 texCoord0;

void main()

        vec3 ndc = (clipSpace.xyz / clipSpace.w)/ 2.0 + 0.5;

    vec2 reflectTexCoords = vec2(ndc.x, -ndc.y);
    vec2 refractTexCoords = vec2(ndc.x, ndc.y);

    vec2 distorsion1 = (texture(dudvMap, vec2(texCoord0.x, texCoord0.y)).rg * 2.0 - 1.0) * waveStrength; // waveStrength = 0.02;

    refractTexCoords += distorsion1;

    reflectTexCoords += distorsion1;

    vec4 reflectColor = texture(reflectionTexture, reflectTexCoords);
    vec4 refractionColor = texture(refractionTexture, refractTexCoords);

      outColor = mix(reflectColor, refractionColor, 0.5);

As you can see when I add my distortion to my reflection and refraction texture coordinates, it seems that something is wrong and can't figure what, because I get this effect:


Do any of you have any idea how can I fix this? Thanks for your time!

  • \$\begingroup\$ Looks like distortion is too strong, too frequent, or both. Try to multiply "texCoord0" and "distorsion1" by some small number. \$\endgroup\$ – snake5 Oct 16 '15 at 13:45
  • \$\begingroup\$ It looks like texCoord0 might be measured in pixels in the vertex shader, but in the fragment shader it will expect texCoord0 to range from 0 to 1. In your picture there appears to be high frequency aliasing which supports this theory. (If this is the case, then dividing texCoord0 by the size of your DuDv map in pixels should be a step forward) \$\endgroup\$ – MickLH Oct 16 '15 at 14:09
  • \$\begingroup\$ @MickLH dividing in fragment shader? (if I understand correctly) \$\endgroup\$ – Shmwel Oct 16 '15 at 15:01
  • 1
    \$\begingroup\$ Yes. (As a side note, I like to divide 1 / Size on the CPU once, and then multiply in the shader instead) \$\endgroup\$ – MickLH Oct 16 '15 at 15:16
  • \$\begingroup\$ @MickLH Indeed, that fixed my problem. Thanks:) (you can post it as an answer so I can validate it:) \$\endgroup\$ – Shmwel Oct 16 '15 at 20:16

The texture sampling function expects texture coordinates to be normalized into the [0 - 1] range.

You can divide by the size of the DuDv map in pixels, to normalize your texture coordinate into the [0 - 1] scale. For the sake of efficiency, I prefer to evaluate 1 / Size on the CPU, and use multiplication instead of division in your shader.

I arrived at my assumptions because your offsetting code in the vertex shader seems to indicate that your variable texCoord0 is measured in pixels, not normalized:

texCoord0 = vec2(position.x / 2.0 + 0.5, position.y / 2.0 + 0.5) * 6.0;

Your screen capture appears to contain high frequency aliasing which supports this theory. Here I have zoomed in on the characteristic artifacts which often arise from severe undersampling:

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.