title: How to send more per-vertex data to custom shaders in kivy
body: <p>I would like to be able to pass more per-vertex-data to my own custom shaders in kivy than the usual vertex coords + texture coords. Specifically, I would like to pass a value that says which animation frame should be used in selecting the texture coords.</p>
  
  <p>I found an example (<a href="http://shadowmint.blogspot.com/2013/10/kivy-textured-quad-easy-right-no.html" rel="nofollow">http://shadowmint.blogspot.com/2013/10/kivy-textured-quad-easy-right-no.html</a>), and succeeded in changing the format of the vertices passed to a mesh using an argument to the constructor of the Mesh, like this:</p>
  
  <pre><code>Mesh(mode = 'triangles', fmt=[('v_pos', 2, 'float'),  
                                ('v_tex0', 2, 'float'),
                                ('v_frame_i', 1, 'float')]
  </code></pre>
  
  <p>I can then set the vertices to be drawn to something like this:</p>
  
  <pre><code>vertices = [x-r,y-r, uvpos[0],uvpos[1],animationFrame,
              x-r,y+r, uvpos[0],uvpos[1]+uvsize[1],animationFrame,
              x+r,y-r, uvpos[0]+uvsize[0],uvpos[1],animationFrame,
              x+r,y+r, uvpos[0]+uvsize[0],uvpos[1]+uvsize[1],animationFrame,
              x+r,y-r, uvpos[0]+uvsize[0],uvpos[1],animationFrame,
              x-r,y+r, uvpos[0],uvpos[1]+uvsize[1],animationFrame,
              ]
  </code></pre>
  
  <p>..this works well when I run in Ubuntu, but when I run on my android device the drawn texture either doesn't draw, or it looks like the vertex or texture coordinate data is corrupt / not aligned or something.</p>
  
  <p>Here is my shader code in case that is relevant. Again, this all behaves as I want it to when I run in ubuntu, but not when I run on android device.</p>
  
  <pre><code>---VERTEX SHADER---
  #ifdef GL_ES
      precision highp float;
  #endif
  
  /* vertex attributes */
  attribute vec2     v_pos;
  attribute vec2     v_tex0;
  attribute float    v_frame_i; // for animation
  
  /* uniform variables */
  uniform mat4       modelview_mat;
  uniform mat4       projection_mat;
  uniform vec4       color;
  uniform float      opacity;
  
  uniform float      sqrtNumFrames; // the width/height of the sprite-sheet
  uniform float      frameWidth;
  
  /* Outputs to the fragment shader */
  varying vec4 frag_color;
  varying vec2 tc;
  
  void main() {
    frag_color = color * vec4(1.0, 1.0, 1.0, opacity);
    gl_Position = projection_mat * modelview_mat * vec4(v_pos.xy, 0.0, 1.0);
  
    float f = round(v_frame_i);
    tc = v_tex0;
    float w = (1.0/sqrtNumFrames);
    tc *= w;  
    tc.x += w*mod(f,sqrtNumFrames);        //////////// I think that the problem might
    tc.y += w*round(f / sqrtNumFrames);   ///////////// be related to this code, here?
  }
  
  ---FRAGMENT SHADER--- 
  #ifdef GL_ES
      precision highp float;
  #endif
  
  /* Outputs from the vertex shader */
  varying vec4 frag_color;
  varying vec2 tc;
  
  /* uniform texture samplers */
  uniform sampler2D texture0;
  
  uniform vec2 player_pos;
  uniform vec2 window_size; // in pixels
  void main (void){
    gl_FragColor = frag_color * texture2D(texture0, tc);
  }
  </code></pre>
  
  <p>I wonder if it may have to do with a version of GLSL and int / float math (in particular in identifying which image from the sprite sheet to draw, see the comments in the glsl code. One version is running on my desktop and another on the device?</p>
  
  <p>Any suggestions for things to experiment with would be much appreciated!</p>