Hello,
I’m trying to use Uniform Buffer Objects to pass arbitrary data as a float array to a shader, and I’m having some trouble with that. I can get it to run, but I don’t get the results I expect.
Like I said, I’m only trying to pass an array of floats which has a hard-coded size of 50, for now. Here is my opengl code (in Java with jogl):
(Assume myShader is a valid pointer to a working shader.)
int blockIndex = gl.glGetUniformBlockIndex(myShader, "MyBlock");
FloatBuffer data = FloatBuffer.allocate(50);
// Fill array with floats ranging from 1.0 to 0.0
for (int i = 50; i > 0; i--) {
data.put(((float)i) / 50f);
}
data.rewind();
IntBuffer buf = Buffers.newDirectIntBuffer(1);
gl.glGenBuffers(1, buf);
int bo = buf.get();
gl.glBindBuffer( GL2.GL_UNIFORM_BUFFER, bo );
gl.glBufferData(GL2.GL_UNIFORM_BUFFER, 50 * Float.SIZE, data, GL.GL_DYNAMIC_DRAW);
gl.glBindBufferBase( GL2.GL_UNIFORM_BUFFER, blockIndex, bo );
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
and here is my fragment shader code:
layout(std140) uniform MyBlock
{
float myDataArray[50];
};
void main (void)
{
int pos = int (gl_FragCoord.x) % 50;
gl_FragColor = vec4(myDataArray[pos], myDataArray[pos], myDataArray[pos], 1);
}
this is the output:
[ATTACH=CONFIG]877[/ATTACH]
I would expect a smooth white-to-black gradient over 50 pixels and then repeat, but instead, the gradient goes over 13 pixels, and the remaining 37 pixels are all black. It’s as if my 50-sized array gets crushed into the fisrt 13 elements of the output array.
This is what I would expect (done in photoshop):
[ATTACH=CONFIG]878[/ATTACH]
Any ideas?