glGetActiveUniform报告统一存在,但glGetUniformLocation返回-1

时间:2016-02-29 02:27:50

标签: c++ opengl

我有一个顶点着色器,由于某种原因,我无法获得其中一件制服的位置。

我使用glGetActiveUniform来获取所有制服,我的制服就在那里(bones[0])。

但是,当我调用glGetUniformLocation(shaderProgram_, "bones[0]");时,它会返回-1。

我也尝试了glGetUniformLocation(shaderProgram_, "bones");,但它也返回-1。

以下是着色器:

#version 330 core

uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;
uniform mat4 pvmMatrix;
uniform mat3 normalMatrix;

layout (std140) uniform Bones
{
    mat4 bones[100];
};

layout (location = 0) in vec3 position;
layout (location = 1) in vec4 color;
layout (location = 2) in vec3 normal;
layout (location = 3) in vec2 textureCoordinate;
layout (location = 4) in ivec4 boneIds;
layout (location = 5) in vec4 boneWeights;

out vec4 ourColor;
out vec2 texCoord;
out vec3 normalDirection;

void main()
{
    mat4 bones2[100];
    for(int i=0;i<100;++i)
    {
        bones2[i] = mat4(1.0);
    }

    // Calculate the transformation on the vertex position based on the bone weightings
    mat4 boneTransform = bones2[ boneIds[0] ] * boneWeights[0];
    boneTransform     += bones2[ boneIds[1] ] * boneWeights[1];
    boneTransform     += bones2[ boneIds[2] ] * boneWeights[2];
    boneTransform     += bones2[ boneIds[3] ] * boneWeights[3];

    //mat4 tempM = mat4(1.0);
    //boneTransform = tempM;

    // This is for animating the model
    vec4 tempPosition = boneTransform * vec4(position, 1.0);
    gl_Position = pvmMatrix * tempPosition;

    float sum = boneWeights[0] + boneWeights[1] + boneWeights[2] + boneWeights[3];
    if (sum > 1.01f)
        gl_Position = pvmMatrix * vec4(position, 1.0);
    else if (sum < 0.99f)
        gl_Position = pvmMatrix * vec4(position, 1.0);

    /*
    if (boneIds[0] > 99 || boneIds[0] < 0)
        gl_Position = pvmMatrix * vec4(position, 1.0);
    else if (boneIds[1] > 99 || boneIds[1] < 0)
        gl_Position = pvmMatrix * vec4(position, 1.0);
    else if (boneIds[2] > 99 || boneIds[2] < 0)
        gl_Position = pvmMatrix * vec4(position, 1.0);
    else if (boneIds[3] > 99 || boneIds[3] < 0)
        gl_Position = pvmMatrix * vec4(position, 1.0);
    */
    // Calculate normal
    vec4 normalDirTemp = boneTransform * vec4(normal, 0.0);
    normalDirection = normalize(normalMatrix * normalDirTemp.xyz);

    //gl_Position = vec4(position, 1.0);
    //gl_Position = pvmMatrix * vec4(position, 1.0);
    ourColor = color;
    texCoord = textureCoordinate;

    for(int i=0;i<100;++i)
    {
        if (bones[1] == mat4(0.0))
            ourColor = vec4(0.0, 0.0, 1.0, 1.0);
    }
}

我也尝试在我的着色器中使用uniform mat4 bones[100];,其中 给我统一的位置(0),但是,所有数据都是mat4(0.0),即使我和#39; m将数据推送到制服。

有人有什么想法吗?

2 个答案:

答案 0 :(得分:3)

统一区块中的制服不具有位置。您可以将binding a buffer object的值设置为该块的相应绑定索引。这是将制服放入interface blocks的全部内容。

答案 1 :(得分:2)

在OpenGL中,您无法直接获取特定数组索引的位置。相反,你应该做glGetUniformLocation(shaderProgram_, "bones");。这将为您提供数组的第一个元素的位置。如果要访问其他元素,如骨骼[1]或骨骼[20],则必须将所需的索引号添加到glGetUniformLocation返回的值。

因此,使用glGetUniformLocation(shaderProgram_, "bones");检索骨骼[0]的位置,使用glGetUniformLocation(shaderProgram_, "bones") + 1;检索骨骼[1]的位置,依此类推。