3

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

1
  • Hi @MikeShiny have u found the solution, I tried the second solution and it looks like it works with arrays with small sizes, but when it comes to stuff like 512*512 arrays it will give "Too many uniforms" is that correct. Commented Jan 8, 2023 at 17:52

2 Answers 2

8

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

http://www.opengl.org/wiki/GLAPI/glUniform

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");
GLES20.glUniform1iv(location,
              3,
              array,
              0);
Sign up to request clarification or add additional context in comments.

2 Comments

How do I find out what the maximum size I can use for the array is? Thanks
@Trax I have tried to use the overload function of it that works with a float buffer but it does not work, this one works, is it possible to use the one with float(maybe I am using it wrong) GLES20.glUniform1fv(maskLoc, fb.remaining(), fb) nice ans anyway :)
3

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.

1 Comment

Thanks for the examples. It is actually a very large array (about 516*516) but this will help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.