【发布时间】:2013-09-04 23:06:56
【问题描述】:
我想在 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";
【问题讨论】:
-
你能再解释一下想要的动画吗?你在哪个平台上运行?
-
哦,你说的“没有运气”是什么意思?你实际得到了什么结果?
-
所需的动画将是从 0 到 9 的数字,并随着计时器创建的正弦波移动而再次下降。有了这个设置,什么都没有发生,我得到一个空白屏幕,但没有错误消息。我知道计时器、纹理坐标和 glFragColor 正在工作,因为我已经单独测试了它们。这让我相信它与使用的 if 语句有关。
-
您在检查 GLSL 编译错误吗?您的这段代码看起来很可疑:` "else" "vec2 flipped_texcoord = vec2(textureCoordinate.x+0.9, 1.0 -textureCoordinate.y);\n"` - 您可能需要在 "else" 之后添加一些空格。跨度>
-
哦,您可能只想定义一次 flipped_texcoord。我相当肯定您编写的着色器代码不会编译。我也不明白为什么要在着色器中将计时器转换为纹理坐标 - 在绘图代码中执行此操作会简单得多。你没有说你在为什么平台编码。
标签: text opengl-es-2.0 fragment-shader