Difference between revisions of "Example/Program Interface Non Block Uniforms"

From OpenGL Wiki
Jump to: navigation, search
m (Corrected mistake within Program Interface Non Block Uniforms example.)
 
Line 11: Line 11:
 
   glGetProgramResourceiv(prog, GL_UNIFORM, unif, 4, properties, 4, NULL, values);
 
   glGetProgramResourceiv(prog, GL_UNIFORM, unif, 4, properties, 4, NULL, values);
  
   //Skip any uniforms that are in a block.
+
   // Skip any uniforms that are in a block.
 
   if(values[0] != -1)
 
   if(values[0] != -1)
 
     continue;
 
     continue;
  
   //Get the name. Must use a std::vector rather than a std::string for C++03 standards issues.
+
   // Get the name. Must use a std::vector rather than a std::string for C++03 standards issues.
   //C++11 would let you use a std::string directly.
+
   // C++11 would let you use a std::string directly.
 
   std::vector<char> nameData(values[2]);
 
   std::vector<char> nameData(values[2]);
 
   glGetProgramResourceName(prog, GL_UNIFORM, unif, nameData.size(), NULL, &nameData[0]);
 
   glGetProgramResourceName(prog, GL_UNIFORM, unif, nameData.size(), NULL, &nameData[0]);

Latest revision as of 11:18, 3 January 2018

Iteration over all non-block uniform variables, fetching their names, types, and locations:

GLint numUniforms = 0;
glGetProgramInterfaceiv(prog, GL_UNIFORM, GL_ACTIVE_RESOURCES, &numUniforms);
const GLenum properties[4] = {GL_BLOCK_INDEX​, GL_TYPE​, GL_NAME_LENGTH​, GL_LOCATION};

for(int unif = 0; unif < numUniforms; ++unif)
{
  GLint values[4];
  glGetProgramResourceiv(prog, GL_UNIFORM, unif, 4, properties, 4, NULL, values);

  // Skip any uniforms that are in a block.
  if(values[0] != -1)
    continue;

  // Get the name. Must use a std::vector rather than a std::string for C++03 standards issues.
  // C++11 would let you use a std::string directly.
  std::vector<char> nameData(values[2]);
  glGetProgramResourceName(prog, GL_UNIFORM, unif, nameData.size(), NULL, &nameData[0]);
  std::string name(nameData.begin(), nameData.end() - 1);
}