View Full Version : Uniform block binding

01-07-2014, 11:03 AM
Hello, I'm trying to set some data to a shader through uniform blocks. But I don't get what I wanted. Here's the code:

const GLfloat pColor[] =
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f
GLuint nUniformBuffer;
glGenBuffers(1, &nUniformBuffer);
glBindBuffer(GL_UNIFORM_BUFFER, nUniformBuffer);
glBufferData(GL_UNIFORM_BUFFER, sizeof(pColor), pColor, GL_STATIC_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);

GLuint nBlockIndex0 = Shader.GetUniformBlockLocation("ColorsBlock[0]"); // glGetUniformBlockIndex(...)
GLuint nBlockIndex1 = Shader.GetUniformBlockLocation("ColorsBlock[1]");

glBindBufferRange(GL_UNIFORM_BUFFER, 0, nUniformBuffer, 0, 4 * sizeof(GLfloat));
glBindBufferRange(GL_UNIFORM_BUFFER, 1, nUniformBuffer, 4 * sizeof(GLfloat), 4 * sizeof(GLfloat));

glUniformBlockBinding(Shader, nBlockIndex0, 0);
glUniformBlockBinding(Shader, nBlockIndex1, 1);

uniform ColorsBlock
vec4 uColor;
} ColorInput[2];

// ...
teColor = (ColorInput[0].uColor.rgb + ColorInput[1].uColor.rgb) / 2.0f;
// ...


But I instead of getting white I get gray. So I think it's because I don't set ColorInput[1]. What am I doing wrong?

01-07-2014, 08:41 PM
Although I don't believe this is the cause of your problem, it looks like you are relying on the std140 block layout in your program, and so you should declare your block in your shader like this instead:

layout(std140) uniform ColorsBlock
// ...