Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa和OpenGL,如何使用数组设置GLSL顶点属性?_Opengl_Attributes_Shader_Glsl_Vertex - Fatal编程技术网

Cocoa和OpenGL,如何使用数组设置GLSL顶点属性?

Cocoa和OpenGL,如何使用数组设置GLSL顶点属性?,opengl,attributes,shader,glsl,vertex,Opengl,Attributes,Shader,Glsl,Vertex,我是OpenGL的新手,似乎遇到了一些困难。我已经在GLSL中编写了一个简单的着色器,它应该通过给定的关节矩阵变换顶点,允许简单的骨骼动画。每个顶点最多有两个骨骼影响(存储为Vec2的x和y分量)、与变换矩阵数组关联的索引和相应权重,并在我的着色器中指定为“属性变量”,然后使用“GLVertexAttributePointer”函数进行设置 这就是问题所在。。。我已经成功地正确设置了矩阵的“统一变量”数组,当我在着色器中检查这些值时,所有这些值都正确导入,并且它们包含正确的数据。但是,当我尝试设

我是OpenGL的新手,似乎遇到了一些困难。我已经在GLSL中编写了一个简单的着色器,它应该通过给定的关节矩阵变换顶点,允许简单的骨骼动画。每个顶点最多有两个骨骼影响(存储为Vec2的x和y分量)、与变换矩阵数组关联的索引和相应权重,并在我的着色器中指定为“属性变量”,然后使用“GLVertexAttributePointer”函数进行设置

这就是问题所在。。。我已经成功地正确设置了矩阵的“统一变量”数组,当我在着色器中检查这些值时,所有这些值都正确导入,并且它们包含正确的数据。但是,当我尝试设置关节索引变量时,顶点会与任意变换矩阵相乘!它们会跳转到空间中看似随机的位置(每次都不同),因此我假设索引设置不正确,并且我的着色器正在读取关节矩阵数组的末尾到下面的内存中。我不太清楚为什么,因为在阅读了我能找到的关于这个主题的所有信息后,我惊讶地发现在他们的示例中有相同(如果不是非常相似的话)的代码,而且似乎对他们有效

我已经尝试解决这个问题很长一段时间了,它真的开始让我感到不安。。。我知道矩阵是正确的,当我手动将着色器中的索引值更改为任意整数时,它会读取正确的矩阵值并按其应该的方式工作,通过该矩阵变换所有顶点,但当我尝试使用我编写的代码设置属性变量时,它似乎不起作用

我用来设置变量的代码如下

// 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);
}
这真的开始让我感到沮丧,我们将感谢您的任何帮助


-安德鲁·戈托

好的,我已经弄明白了。OpenGL使用drawArrays函数绘制三角形,每9个值读取一个三角形(3个顶点,每个顶点有3个组件)。因此,顶点在三角形之间重复,因此如果两个相邻三角形共享一个顶点,则该顶点在数组中出现两次。所以我的立方体,我原本以为有8个顶点,实际上有36个

六条边,一条边两个三角形,每个三角形三个顶点,所有这些顶点都会乘以36个独立顶点,而不是8个共享顶点

整个问题在于指定的值太少。当我将测试数组扩展到包含36个值时,它就工作得非常好