GLSL - Set element of array

Started by TheBoneJarmer, September 27, 2015, 09:47:09

Previous topic - Next topic

TheBoneJarmer

Hey guys

Just a very basic question. How to set an element of an array in GLSL in Java using the glUniform methods? To clarify this a bit. Here is what I got:

Vertex Shader
#version 120

uniform float myFloats[10];

void main()
{
    gl_Position = gl_Vertex;
}


And this is kinda what I'm searching for in Java:
glUniformf(location, elementIndex, elementValue);


Thanks in advance!
TBJ

Kai

Just do glUniform1f(location+elementIndex, elementValue)

This is because arrays in GLSL are like individually declared uniforms, with the location of the uniform variable being the location of its first element and its other elements get successive locations.

That is, if you had a shader like the following:
uniform float myArray[10];
uniform float anotherFloat;

then 'myArray' would have a possible location of 1, and the location of 'anotherFloat' could then not be within 2..10 but will likely be 11.

There is also a funky syntax to query the location of individual array elements with glGetUniformLocation(program, "myArray[5]"). This will give you the location of the sixth element, which is just the location of the first element (i.e. the array itself) plus 5.

TheBoneJarmer