我对OpenGL相当陌生,而且我似乎遇到了一些困难。我已经在GLSL中编写了一个简单的着色器,它应该通过给定的联合矩阵来转换顶点,从而允许简单的骨架动画。每个顶点最多有两个骨骼影响(存储为Vec2的x和y分量),与变换矩阵数组关联的索引和相应的权重,并在我的着色器中指定为“属性变量”,然后设置使用“glVertexAttribPointer”函数。Cocoa和OpenGL,如何使用数组设置GLSL顶点属性?
问题出现在这里......我设法正确地设置了“统一变量”矩阵数组,当我在着色器中检查这些值时,所有这些值都正确导入并且它们包含正确的数据。但是,当我尝试设置联合索引变量时,顶点将乘以任意变换矩阵!他们跳到看似随机的空间位置(每次都是不同的),我假设这些索引设置不正确,我的着色器正在将我的联合矩阵数组的末尾读入以下内存。我不确定为什么,因为在阅读了我能找到的关于这个主题的所有信息后,我很惊讶地发现他们的例子中有相同的(如果不是非常相似的)代码,并且它似乎对他们有用。
我试图解决这个问题很长一段时间了,它真的开始让我紧张......我知道矩阵是正确的,当我手动将着色器中的索引值更改为任意值整数,它读取正确的矩阵值并按照它应该的方式工作,通过矩阵转换所有顶点,但是当我尝试使用我编写的代码来设置属性变量时,它似乎不起作用。
我使用的设置变量是如下的代码...
// 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