Search code examples
c++openglopengl-3

glGetUniformIndices not returning correct index


I am trying to learn how to use uniform buffer objects, reading the OpenGL Superbible 5. I have a uniform block in the my shader:

layout(std140) uniform SkeletonBlock  
{  
    vec3 position[64];  
    vec4 orientation[64];  
} Skeleton;

Now my code to grab the index is:

const GLchar* uniformNames[2] =
{
    "SkeletonBlock.position",
    "SkeletonBlock.orientation"
};

GLuint uniformIndex[2];
glGetUniformIndices(shaderProgram, 2, uniformNames, uniformIndex);

For some reason this call is giving me a really high index (4294967295, consistently) and I am not sure why. I feel like I am missing something obvious. OpenGL is reporting one active uniform block, which is correct, out a max allowed of 15. No error flags are active before or after this section of code either. Any suggestions where it could be going wrong?


Solution

  • I believe you want

    const GLchar* uniformNames[2] =
    {
        "Skeleton.position",
        "Skeleton.orientation"
    };
    

    As the semantics of a C-style language (such as GLSL) have it, you are declaring a variable of type uniform SkeletonBlock named Skeleton. Hence, "SkeletonBlock.position" is of the form <typename>.<member>, where you want <variable>.<member>.

    The OpenGL docs say that you will get a GL_INVALID_INDEX from glGetUniformIndices() when you give it a bad uniform name. It might be smart to check each of the returned indices against this. I would bet that GL_INVALID_INDEX == -1.

    Also, this number 4294967295 is the unsigned interpretation of a 32-bit -1 (twos complement).