Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: Problem making water, dudv image map keeps translating to the left.

  1. #1
    Intern Contributor
    Join Date
    Sep 2016
    Posts
    71

    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!

    Code :
        // 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
    Last edited by hashbrown; 06-18-2017 at 03:44 PM.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,349
    Quote Originally Posted by hashbrown View Post
    Code :
        vec4 distortion1        = (texture2D(dudv, vec2(newTexCoords.x  + moveFactor, newTexCoords.y).rg * 2.0 - 1.0)) *  waveStr;
    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?
    Code :
        vec2 distortion1        = (texture2D(dudv, vec2(newTexCoords.x  + moveFactor, newTexCoords.y)).rg * 2.0 - 1.0) *  waveStr;

  3. #3
    Intern Contributor
    Join Date
    Sep 2016
    Posts
    71
    Quote Originally Posted by GClements View Post
    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?
    Code :
        vec2 distortion1        = (texture2D(dudv, vec2(newTexCoords.x  + moveFactor, newTexCoords.y)).rg * 2.0 - 1.0) *  waveStr;
    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;
    Last edited by hashbrown; 06-18-2017 at 04:32 PM.

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,349
    Quote Originally Posted by hashbrown View Post
    Hi GClements, if I understand correctly, I was planning on retrieving the values from the dudv texture for the distortion.
    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)?

  5. #5
    Intern Contributor
    Join Date
    Sep 2016
    Posts
    71
    Quote Originally Posted by GClements View Post
    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.

  6. #6
    Intern Contributor
    Join Date
    Sep 2016
    Posts
    71
    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.

    Code :
    // 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;

  7. #7
    Intern Contributor
    Join Date
    Sep 2016
    Posts
    71
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •