glGetActiveUniform报告统一存在,但glGetUniformLocation返回-1 [英] glGetActiveUniform reports uniform exists, but glGetUniformLocation returns -1
问题描述
我有一个顶点着色器,由于某种原因,我不能得到一个制服的位置。
I have a vertex shader that for some reason I can't get the location of one of the uniforms.
我使用 glGetActiveUniform
以获得所有制服,我的制服是有( bones [0]
)。
I use glGetActiveUniform
to get all the uniforms available, and my uniform is there (bones[0]
).
但是,当我调用 glGetUniformLocation(shaderProgram_,bones [0]);
,它返回-1。
However, when I call glGetUniformLocation(shaderProgram_, "bones[0]");
, it returns -1.
我也尝试过 glGetUniformLocation(shaderProgram_,bones);
,但它也返回-1。
I also tried glGetUniformLocation(shaderProgram_, "bones");
, but it also returns -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);
}
}
我也尝试使用 mat4的骨骼[100];
在我的着色器,它给我的统一位置(0),然而,所有的数据 mat4
I also tried using uniform mat4 bones[100];
in my shader, which did give me the uniform location (0), however, all of the data was mat4(0.0)
, even though I'm pushing data to the uniform.
任何人都有什么想法?
推荐答案
统一块中的制服不具有位置。您可以通过将缓冲区对象绑定到该块的相应绑定索引来设置其值。这是在界面块中穿制服的整个过程。
Uniforms in a uniform block do not have locations. You set their values by binding a buffer object to the appropriate binding index for that block. This is the entire point of putting uniforms in interface blocks.
这篇关于glGetActiveUniform报告统一存在,但glGetUniformLocation返回-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!