简体   繁体   English

将2D数组传递到GLES opengl着色器

[英]Passing 2D array into GLES opengl shader

I have a 2D array of floats (in this case it represents a 256 color palette with RGB values) 我有一个2D浮点数组(在这种情况下,它代表RGB值的256个调色板)

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

I fill the array and now want to pass it to GLES. 我填充数组,现在想将其传递给GLES。 How do I pass a 2D array in android? 如何在Android中传递2D数组? I try 我尝试

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

but it complains about expecting a 1D array (found float[][] expecting float[]). 但是它抱怨期望使用一维数组(发现float [] []期望使用float [])。

The shader is expecting a vec3 uniform, ie 着色器期望vec3统一,即

uniform vec3 palette[256];

so I need to keep the 2D array so the RGB components are separate floats. 所以我需要保留2D数组,以便RGB分量是独立的浮点数。

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 将2D数组正确传递到GLES着色器的秘诀是什么,这样我就可以使用来访问着色器中的RGB值

int red = palette[100].r;

The short answer is: You cannot pass a 2D array to a shader. 简短的答案是:您不能将2D数组传递给着色器。

In order to pass a 2D array to OpenGL, one has to flatten the array. 为了将2D数组传递给OpenGL,必须将其展平。 This is relatively easy and can, eg, 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. 但是我建议,如果可能的话,请直接用一维定义数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM