I just need pass a huge array (count 1521) of Int
to shader for my animation.
Firstly, I treat it as a uniform, but it seems too huge as uniform.
Then, I found that Passing a list of values to fragment shader I don't enough reputation for comment, so I have to open another question. I am not familiar with those APIs, I tried all of them
- 1D Textures: I can't find any API which works.
- Uniform Buffer Objects: Most examples are clear and understandable, but it's not available on iOS with ES 2.0
- Buffer Textures: I think it's very close, but glTexBuffer and GL_TEXTURE_BUFFER seems missing on iOS with ES 2.0
- Shader Storage Buffer Objects: That's something for GL 4.3, it's too new for ES 2.0
Please help me, I just need to access that array in shader.
That's what I am doing:
// i var
GLint *randomArray;
GLuint _randomArrayTexture;
GLuint _randomArrayUnifrom;
// init texture
randomArray = (GLint*)malloc(1521 * sizeof(GLint));
for (int i=0; i<1521; i++) {
randomArray[i] = array[i].intValue;
}
glGenTextures(0, &_randomArrayTexture);
glBindTexture(GL_TEXTURE_2D, _randomArrayTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 1521, 1, 0, GL_ALPHA, GL_INT, randomArray);
glBindTexture(GL_TEXTURE_2D, 0);
_randomArrayUnifrom = glGetUniformLocation(_program, "randomArray"); // after shader compiled
// before draw
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, _randomArrayTexture);
glUniform1i(_randomArrayUnifrom, 1);
glBindTexture(GL_TEXTURE_2D, 0);
// the thing in shader
uniform sampler2D randomArray;
texture2D(randomArray, vec2(0.1, 0.0));
// Could I access the texture with real index (not uniformed) such as vec2(1109, 0)