Question

So I have a sort of zigzag pattern, as shown below.ZigZag Pattern, which is created by the following fragment shader:

uniform float time;
varying vec2 texture_coord;
void main()
{
    float wav[10] = float[10](0,.1,.2,.1,0,-.1,-.2,-.1,0,.1);
    //gl_FragColor = gl_Color;
    float mod_time = mod(time, 1);
    float x_pos = mod(texture_coord.x, 1.1);
    float x_pos2 = x_pos * 10;
    int index = int(x_pos2);
    if(texture_coord.y < .5 + wav[index])
        gl_FragColor = vec4(.7,.3,.3,1.0);
    else
        gl_FragColor = vec4(.3,.3,.3,1.0);
}

which I would like to animate by having the zigzag move upwards.

My question is, how would I do this, considering that I'm using an array to create the offset from the median? I'm not exactly sure how I would adjust the array so that in the next animation step, the array looks like (.1,.2,.1,0,-.1,-.2,-.1,0,.1)?

Was it helpful?

Solution

There are 2 ways you could do it. You can either animate the offset into the array (probably the easiest) or you can animate the array itself. You're already passing in a time parameter, so you could use that, like so:

if (texture_coord.y < 0.5 + wav [ (index + (mod_time * 10)) % 10 ]) // Note you may have to calculate the "%" operator yourself
... etc. ...

Or you could pass in the array. To pass in an array, you simply get the uniform location of the first element of the array, and increment it for later values. So in your source code, you could do this:

Fragment Shader:

uniform float wav [ 10 ];
... rest of fragment shader ...

Source Code:

wavLoc = glGetUniformLocation (program, "wav");
offset++; // This starts at 0 and is incremented on each frame you want to advance the pattern
for (int i = 0; i < 10; i++)
{
    glUniform1f (wavLoc + i, wavePattern [ (i + offset) % 10 ]);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top