0

I want to animate a some numbers in openGl ES 2.0. To try and make it work to start with I have created a timer and am trying to get the numbers to change dependant on the timer. The timer is set up on a sin() wave so the numbers should increase then decrease again. The numbers are on the top line of the texture atlas so by increasing the texture coordinates in the x direction moves it up to the next number. I have tried using the fragment shader with if statements to move the texture coordinates but have had no luck. Im sure there must be a better way of doing this.

Fragment Shader

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";
D.Cole
  • 27
  • 10
  • Can you explain the desired animation a little more? What platform are you running on? – Clyde Sep 03 '13 at 22:19
  • Oh, and what do you mean by "have had no luck"? What results do you actually get? – Clyde Sep 03 '13 at 22:19
  • The desired animation will be numbers changing from 0 up to 9 and back down again as the sin wave created by the timer moves. With this set up nothing happens I get a blank screen but no error messages. I know the timer, texture coordinates and glFragColor is working as I have tested them individually. This leads me to believe its something to do with the if statements used. – D.Cole Sep 04 '13 at 08:36
  • Are you checking for GLSL compile errors? This bit of your code looks suspect: ` "else" "vec2 flipped_texcoord = vec2(textureCoordinate.x+0.9, 1.0 -textureCoordinate.y);\n"` - you probably want some white-space after "else". – Clyde Sep 04 '13 at 09:45
  • Oh, and you probably want to define flipped_texcoord only once. I am fairly certain that the shader code you wrote will not compile. I also don't understand why you doing the conversion from the timer to the texture coord in the shader - it would be much simpler to do it in your drawing code. You didn't say what platform you are coding for. – Clyde Sep 04 '13 at 09:49
  • Im coding cross platform on MoSync. I would ideally do all this in the draw function however the timer only seems to activate once in the fragment shader. – D.Cole Sep 04 '13 at 12:34

1 Answers1

0

Ok, this was such a basic fix, my 'elseif' should have been 'else if'. Just a problem spanning from my lack of knowledge of C++ coding.

D.Cole
  • 27
  • 10