2016-02-29 17 views
3

我有一個頂點着色器,出於某種原因,我無法獲得其中一個制服的位置。glGetActive統一報告統一存在,但glGetUniformLocation返回-1

我用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),即使我」將數據推送給制服。

任何人有任何想法?

回答

3

制服在統一區塊不要位置。您可以通過binding a buffer object將它們的值設置爲該塊的適當綁定索引。這是穿制服interface blocks的全部要點。

+0

啊,這很有道理!謝謝@NicolBolas。我改變了我的着色器,使用佈局(std140,binding = 0)uniform Bones',它現在一切正常(可以看到我的動畫)。奇怪的是,'統一的mat4骨骼;'如果我使用'glGetUniformLocation'和'glBindBufferBase'函數仍然不起作用。好吧。 – Jarrett

+0

您不能將緩衝區綁定到非塊統一。 –

2

在OpenGL中,您無法直接獲取特定數組索引的位置。相反,你應該做glGetUniformLocation(shaderProgram_, "bones");。這會給你數組第一個元素的位置。如果你想訪問另一個元素,比如bones [1]或者bones [20],你必須把所需的索引號加到glGetUniformLocation返回的值上。

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

+1

感謝info @Ivelate,不知道。不幸的是,這也沒有奏效(我編輯了我的問題以反映這一點)。 – Jarrett