0

我想在 openGl ES 2.0 中为一些数字制作动画。为了尝试让它开始工作,我创建了一个计时器,并试图让数字根据计时器而改变。计时器设置在 sin() 波上,因此数字应该增加然后再次减少。数字位于纹理图集的顶行,因此通过增加 x 方向的纹理坐标可将其向上移动到下一个数字。我尝试使用带有 if 语句的片段着色器来移动纹理坐标,但没有运气。我确信必须有更好的方法来做到这一点。

片段着色器

char FragShader[] =
        "precision highp float;\n"

        "varying highp vec2 textureCoordinate;\n"
        "uniform sampler2D Texture;\n"
        "uniform float time;\n"

        "void main()\n"
        "{\n"
        "float c1 = 0.5*sin(time)+0.5;\n"

        "if (c1 >= 0.0 && c1 < 0.1)"
        "vec2 flipped_texcoord = vec2(textureCoordinate.x, 1.0 -textureCoordinate.y);\n"

        "elseif (c1 >= 0.1 && c1 < 0.2)"
        "vec2 flipped_texcoord = vec2(textureCoordinate.x+0.1, 1.0 -textureCoordinate.y);\n"

        "elseif (c1 >= 0.2 && c1 < 0.3)"
        "vec2 flipped_texcoord = vec2(textureCoordinate.x+0.2, 1.0 -textureCoordinate.y);\n"

        "elseif (c1 >= 0.3 && c1 < 0.4)"
        "vec2 flipped_texcoord = vec2(textureCoordinate.x+0.3, 1.0 -textureCoordinate.y);\n"

        "elseif (c1 >= 0.4 && c1 < 0.5)"
        "vec2 flipped_texcoord = vec2(textureCoordinate.x+0.4, 1.0 -textureCoordinate.y);\n"

        "elseif (c1 >= 0.5 && c1 < 0.6)"
        "vec2 flipped_texcoord = vec2(textureCoordinate.x+0.5, 1.0 -textureCoordinate.y);\n"

        "elseif (c1 >= 0.6 && c1 < 0.7)"
        "vec2 flipped_texcoord = vec2(textureCoordinate.x+0.6, 1.0 -textureCoordinate.y);\n"

        "elseif (c1 >= 0.7 && c1 < 0.8)"
        "vec2 flipped_texcoord = vec2(textureCoordinate.x+0.7, 1.0 -textureCoordinate.y);\n"

        "elseif (c1 >= 0.8 && c1 < 0.9)"
        "vec2 flipped_texcoord = vec2(textureCoordinate.x+0.8, 1.0 -textureCoordinate.y);\n"

        "else"
        "vec2 flipped_texcoord = vec2(textureCoordinate.x+0.9, 1.0 -textureCoordinate.y);\n"

        "vec4 colour = texture2D(Texture, flipped_texcoord);\n"
        "gl_FragColor = colour;\n"

        "}\n";
4

1 回答 1

0

好的,这是一个基本的修复,我的“elseif”应该是“else if”。只是我缺乏 C++ 编码知识造成的问题。

于 2013-09-04T15:06:56.683 回答