1

I have a 2D array of floats (in this case it represents a 256 color palette with R G B values)

static float[][] glColorPaletteArray = new float[256][3];

I fill the array and now want to pass it to GLES. How do I pass a 2D array in android? I try

GLES20.glUniform3fv(paletteLoc,256,glColorPaletteArray,0);

but it complains about expecting a 1D array (found float[][] expecting float[]).

The shader is expecting a vec3 uniform, ie

uniform vec3 palette[256];

so I need to keep the 2D array so the RGB components are separate floats.

What is the secret to getting the 2D array passed correctly to the GLES shader so I can then access the RGB values in the shader by using

int red = palette[100].r;

1 Answer 1

1

The short answer is: You cannot pass a 2D array to a shader.

In order to pass a 2D array to OpenGL, one has to flatten the array. This is relatively easy and can, e.g., be done as follows:

static float[][] glColorPaletteArray = new float[256][3];
static float[] openglArray = new float[256*3];

for (int i = 0; i < 256; i++)
    for (int j = 0; j < 3; j++)
        openglArray[i * 3 + j] = glColorPalletArray[i][j];

But I would suggest that, if possible, you define the array in 1D at first hand.

Sign up to request clarification or add additional context in comments.

2 Comments

OK, but once converted to 1D will the shader (expecting vec3 entries) know how to extract the RGB components of each of the 256 colors?
Yes it will. Always 3 consecutive floats will be used as one vec3.

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.