Text 在OpenGL ES 2.0中设置纹理动画的最佳方法

Text 在OpenGL ES 2.0中设置纹理动画的最佳方法,text,opengl-es-2.0,fragment-shader,Text,Opengl Es 2.0,Fragment Shader,我想在openGl ES 2.0中制作一些数字的动画。为了让它工作起来,我创建了一个计时器,并试图让数字根据计时器的不同而变化。定时器设置在sin()波上,因此数字应该先增加,然后再减少。这些数字位于纹理图集的顶行,因此通过在x方向上增加纹理坐标,将其上移到下一个数字。我曾尝试使用带有if语句的片段着色器来移动纹理坐标,但没有成功。我相信一定有更好的办法 片段着色器 char FragShader[] = "precision highp float;\n" "

我想在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";
char FragShader[]=
“精度高浮点;\n”
“变化的highp vec2纹理坐标;\n”
“二维纹理的均匀采样;\n”
“统一浮动时间;\n”
“void main()\n”
“{\n”
“浮点c1=0.5*sin(时间)+0.5;\n”
“如果(c1>=0.0&&c1<0.1)”
vec2翻转的_texcoord=vec2(TextureCoord.x,1.0-TextureCoord.y);\n
“elseif(c1>=0.1&&c1<0.2)”
vec2翻转的_texcoord=vec2(TextureCoord.x+0.1,1.0-TextureCoord.y);\n
“elseif(c1>=0.2&&c1<0.3)”
vec2翻转的_texcoord=vec2(TextureCoord.x+0.2,1.0-TextureCoord.y);\n
“elseif(c1>=0.3&&c1<0.4)”
vec2翻转的_texcoord=vec2(TextureCoord.x+0.3,1.0-TextureCoord.y);\n
“elseif(c1>=0.4&&c1<0.5)”
vec2翻转的_texcoord=vec2(TextureCoord.x+0.4,1.0-TextureCoord.y);\n
“elseif(c1>=0.5&&c1<0.6)”
vec2翻转的_texcoord=vec2(TextureCoord.x+0.5,1.0-TextureCoord.y);\n
“elseif(c1>=0.6&&c1<0.7)”
vec2翻转的_texcoord=vec2(TextureCoord.x+0.6,1.0-TextureCoord.y);\n
“elseif(c1>=0.7&&c1<0.8)”
vec2翻转的_texcoord=vec2(TextureCoord.x+0.7,1.0-TextureCoord.y);\n
“elseif(c1>=0.8&&c1<0.9)”
vec2翻转的_texcoord=vec2(TextureCoord.x+0.8,1.0-TextureCoord.y);\n
“其他”
vec2翻转的_texcoord=vec2(TextureCoord.x+0.9,1.0-TextureCoord.y);\n
vec4 color=texture2D(纹理,翻转的纹理坐标);\n
“gl\u FragColor=颜色;\n”
“}\n”;

好的,这是一个基本的修复,我的“elseif”应该是“elseif”。这只是一个问题,因为我对C++代码的知识缺乏。p> 你能再解释一下想要的动画吗?你在哪个站台上跑步?哦,你说的“没有运气”是什么意思?您实际得到的结果是什么?所需的动画将是数字从0变为9,然后随着计时器创建的正弦波移动而再次变回。通过此设置,什么也没有发生,我得到一个空白屏幕,但没有错误消息。我知道计时器、纹理坐标和glFragColor正在工作,因为我已经分别测试了它们。这让我相信这和使用的if语句有关。您是否正在检查GLSL编译错误?这段代码看起来很可疑:`“else”“vec2 flipped_texcoord=vec2(texturecoordination.x+0.9,1.0-texturecoordination.y);\n`-您可能希望在“else”之后有一些空白。哦,您可能只想定义一次flipped_texcoord。我相当肯定您编写的着色器代码不会编译。我也不明白为什么要在着色器中进行从计时器到纹理坐标的转换-在绘图代码中进行转换要简单得多。你没有说你是为哪个平台编码的。