Uniform block binding

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?

It looks like you are relying on the std140 layout for your uniform block. Make sure you declare it in the shader as


layout(std140) uniform ColorsBlock
{
    vec4 uColor;
} ColorInput[2];

Unless of course, you want to use the packed or shared layouts, in which case you need to query their exact locations through the API. Check here for more info on that.

This MAY or MAY NOT be the cause of your problem, but try that first just to be sure :slight_smile:

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
{
    // ...