Take the 2-minute tour ×

I am very new to OpenGl and shaders in general. I want to use a static int array from my java code in the fragment shader to manipulate the color of the camera. Is there a way to pass in my int array to the shader or just have the shader be able to access my array somehow?

Thanks, Mike

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

The correct way to use a so called static array within your shader code is to pass it as an Uniform:


Within your shader code you specify you need an external array:

uniform int colors[3]; // Specify an array of 3 integers

within your java code use this:

int array[3] = {0, 1, 2};
int location = GLES20.glGetUniformLocation(program_id, "colors");
share|improve this answer
add comment

Since you are trying to manipulate the color of the camera, I assume you are trying to pass a small array into the fragment shader (probably 3 or 4 integers).

Because there's probably only one camera with the same color for all the fragments, the easiest way to pass the data is using an uniform. In your fragment declare an uniform:

uniform vec4 cameraColor;

and then in your Java code, get the location of the uniform and pass the data to it (this is C++ code, Java code might be a bit different):

GLint uniColorLocation = glGetUniformLocation( shaderProgram, "cameraColor" );
glUniform4i( uniColorLocation, array[0], array[1], array[2], array[3] );

If you'd like to pass many colors into a fragment shader, you cold use the fragment attributes (ins) to pass the data. In Java code you would use calls such as glVertexAttribPointer and glBufferData to achieve this. Another option is to use texture data to pass information to the shaders. Here, a single texel could correspond to one object's camera color.

share|improve this answer
Thanks for the examples. It is actually a very large array (about 516*516) but this will help! –  MikeShiny Jan 7 '13 at 15:32
add comment

Your Answer


