2012-01-29 89 views
0

所以我有一种Z字形图案,如下所示。 ZigZag Pattern,其由下面的片段着色器创建的:为使用数组着色的着色器设置动画

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); 
} 

,我想通过向上具有Z字形移动的动画。

我的问题是,考虑到我使用数组来创建中位数的偏移量,我该如何做到这一点?我不完全确定如何调整数组,以便在下一个动画步骤中,数组看起来像(.1,.2,.1,0, - 。1, - 。2, - 。1,0, 0.1)?

回答

1

有两种方法可以做到这一点。您可以将偏移量设置为数组(可能是最简单的),也可以为数组本身设置动画。你已经传递了一个时间参数,所以你可以使用它,就像这样:

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

或者你可以传入数组。要传入一个数组,只需获取数组第一个元素的统一位置,然后将其增加以用于以后的值。因此,在你的源代码,你可以这样做:

片段着色器:

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

源代码:

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 ]); 
}