[GLSL] Per-pixel warping of UV coords of one texture based on another
madpenguin

Hi,

In my program, I'm trying to modify the coordinates of the texture in texture unit 0 based on the sum of values from texture units 1 and 2 (it's to simulate waves in slime based on two greyscale/GL_LUMINANCE waviness maps that scroll at right angles to one another).

How is this done? I have been reading Lighthouse3D and other sources, but I can't seem to find anywhere that changing the texcoords is explained. Here's what I have so far (apologies for newbishness, I've only just begun with GLSL)

1const char *vertex_prog_src = "\
2
3varying vec2 uv_reflection; \
4varying vec2 uv_wavemap_1; \
5varying vec2 uv_wavemap_2; \
6 \
7void main(void) \
8{ \
9 // don't modify vertex \
10 gl_Position = ftransform(); \
11 \
12 // save position in texture for later \
13 uv_reflection = gl_MultiTexCoord0.xy; \
14 uv_wavemap_1 = gl_MultiTexCoord1.xy; \
15 uv_wavemap_2 = gl_MultiTexCoord2.xy; \
16} \
17";
18 
19const char *fragment_prog_src = "\
20uniform sampler2D wavemap_1; \
21uniform sampler2D wavemap_2; \
22uniform sampler2D reflection_tex; \
23varying vec2 uv_reflection; \
24varying vec2 uv_wavemap_1; \
25varying vec2 uv_wavemap_2; \
26 \
27void main() \
28{ \
29 // and this is where I'm stuck. \
30} \
31";

I know I need to sample wavemap_1 and wavemap_2, add them together (clamped or scaled as needed), and use those to tweak the 'u' of reflection_tex, then set 'gl_FragColor' to the result of that; I don't really know how to express that in GLSL or where to begin to look, though...

Edit:
I think this might be what I want:

1const char *frag_prog_src = "\
2uniform sampler2D wavemap_1; \
3uniform sampler2D wavemap_2; \
4uniform sampler2D reflection_tex; \
5varying vec2 uv_reflection; \
6varying vec2 uv_wavemap_1; \
7varying vec2 uv_wavemap_2; \
8 \
9 \
10void main() \
11{ \
12 float my_s; \
13 float my_t; \
14 vec2 warped_texcoord; \
15 vec4 ref_colour; \
16 \
17 my_s = texture2D(wavemap_1,uv_wavemap_1).r / 2.0; \
18 my_t = texture2D(wavemap_2,uv_wavemap_2).r / 2.0; \
19 \
20 warped_texcoord.x = uv_reflection.x + my_s; \
21 warped_texcoord.y = uv_reflection.y + my_t; \
22 \
23 ref_colour = texture2D(reflection_tex,warped_texcoord).rgba; \
24 \
25 gl_FragColor = ref_colour; \
26} \
27";

Trying it now, will report back with results...

Edit the 2nd:
Nope, not quite. The wavemaps seem to get super-imposed over the texture in black, but that's not what I've told it to do (that I can see)...anyone out there got any ideas?

Edit the 3rd:
Fixed the above problem but the fragment program appears to do nothing; I changed 'ref_colour = texture2D(reflection_tex,warped_texcoord).rgba' to end with .rrrr instead, and there's no visible change in the reflection... ???

Thread #597780. Printed from Allegro.cc