How to do it...

In the OpenGL program, after linking the shader program, use the following steps to assign data to the uniform block in the fragment shader:

  1. Get the index of the uniform block using glGetUniformBlockIndex:
GLuint blockIndex = glGetUniformBlockIndex(programHandle,    "BlobSettings");
  1. Allocate space for the buffer to contain the data for the uniform block. We get the size using glGetActiveUniformBlockiv:
GLint blockSize; 
glGetActiveUniformBlockiv(programHandle, blockIndex,        GL_UNIFORM_BLOCK_DATA_SIZE, &blockSize); 
 
GLubyte * blockBuffer; 
blockBuffer = (GLubyte *) malloc(blockSize); 
  1. Query for the offset of each variable within the block. To do so, we first find the index of each variable within the block:
const GLchar *names[] ...

Get OpenGL 4 Shading Language Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.