我对OpenGL还比较陌生,遇到了一些困难。我在GLSL中编写了一个简单的着色器,可以通过给定的关节矩阵来转换顶点,从而实现简单的骨骼动画。每个顶点最多有两个骨骼影响(存储为Vec2的x和y分量),与变换矩阵数组相关联的索引和相应的权重,并在我的着色器中指定为“属性变量”,然后使用“glVertexAttribPointer”函数设置。
我的顶点着色器如下所示...
问题出在这里...我已经成功设置了“Uniform Variable”矩阵数组,在着色器中检查这些值时,它们都被正确导入并包含正确的数据。但是,当我尝试设置关节索引变量时,顶点会被任意变换矩阵乘以!它们跳到看似随机的空间位置(每次都不同),因此我认为索引设置不正确,我的着色器正在读取超出关节矩阵数组末尾的内存。我不太确定原因,因为在阅读了所有我能找到的关于此主题的信息后,我惊讶地看到他们的示例中使用了相同(如果不是非常相似)的代码,并且似乎对他们有效。
我已经尝试解决这个问题很长时间了,现在真的开始让我心烦意乱...我知道矩阵是正确的,当我手动更改着色器中的索引值为任意整数时,它会读取正确的矩阵值并按预期工作,通过该矩阵转换所有顶点,但是当我尝试使用我编写的代码来设置属性变量时,似乎不起作用。
我用来设置变量的代码如下...
// this works properly...
GLuint boneMatLoc = glGetUniformLocation([[[obj material] shader] programID], "boneMatrices");
glUniformMatrix4fv( boneMatLoc, matCount, GL_TRUE, currentBoneMatrices );
GLfloat testBoneIndices[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
// this however, does not...
GLuint boneIndexLoc = glGetAttribLocation([[[obj material] shader] programID], "boneIndices");
glEnableVertexAttribArray( boneIndexLoc );
glVertexAttribPointer( boneIndexLoc, 2, GL_FLOAT, GL_FALSE, 0, testBoneIndices );
我的顶点着色器如下所示...
// this shader is supposed to transform the bones by a skeleton, a maximum of two
// bones per vertex with varying weights...
uniform mat4 boneMatrices[32]; // matrices for the bones
attribute vec2 boneIndices; // x for the first bone, y for the second
//attribute vec2 boneWeight; // the blend weights between the two bones
void main(void)
{
gl_TexCoord[0] = gl_MultiTexCoord0; // just set up the texture coordinates...
vec4 vertexPos1 = 1.0 * boneMatrices[ int(boneIndex.x) ] * gl_Vertex;
//vec4 vertexPos2 = 0.5 * boneMatrices[ int(boneIndex.y) ] * gl_Vertex;
gl_Position = gl_ModelViewProjectionMatrix * (vertexPos1);
}
这真的开始让我感到沮丧,非常需要并且感激任何帮助。
-Andrew Gotow