I'm sending data to uniform block in OpenGL, following a book GLSL by David Wolff.
layout(std140) uniform BlobSettings {
vec4 InnerColor;
vec4 OuterColor;
float RadiusInner;
float RadiusOuter;
};
Client code:
GLuint blockIndex = glGetUniformBlockIndex(programHandle, "BlobSettings");
// Allocate space for the buffer
GLint blockSize;
glGetActiveUniformBlockiv(programHandle, blockIndex,
GL_UNIFORM_BLOCK_DATA_SIZE, &blockSize);
GLubyte* blockBuffer = (GLubyte *) malloc(blockSize);
// Query for the offsets of each block variable
const GLchar *names[] = { "BlobSettings.InnerColor", "BlobSettings.OuterColor",
"BlobSettings.RadiusInner", "BlobSettings.RadiusOuter" };
GLuint indices[4];
glGetUniformIndices(programHandle, 4, names, indices);
GLint offset[4];
glGetActiveUniformsiv(programHandle, 4, indices, GL_UNIFORM_OFFSET, offset);
// Store data within the buffer at the appropriate offsets
GLfloat outerColor[] = {0.0f, 0.0f, 0.0f, 0.0f};
GLfloat innerColor[] = {1.0f, 1.0f, 0.75f, 1.0f};
GLfloat innerRadius = 0.25f, outerRadius = 0.45f;
memcpy(blockBuffer + offset[0], innerColor, 4 * sizeof(GLfloat));
memcpy(blockBuffer + offset[1], outerColor, 4 * sizeof(GLfloat));
memcpy(blockBuffer + offset[2], &innerRadius, sizeof(GLfloat));
memcpy(blockBuffer + offset[3], &outerRadius, sizeof(GLfloat));
The program has segmentation fault when doing memcpy. When I set a breakpoint there, I found that glGetActiveUniformsiv return offset and indices wrong. For example:
offset[]
0: 530332809
1: 2686632
2: 1971058176
3:0
While I'm expecting them to be 0, 4,8, 12 etc or something close to that. How do I fix this error?