【问题标题】:Cocoa and OpenGL, How do I set a GLSL vertex attribute using an array?Cocoa 和 OpenGL,如何使用数组设置 GLSL 顶点属性?
【发布时间】:2009-12-31 10:20:12
【问题描述】:

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

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

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

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

// 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 attributes shader glsl vertex


    【解决方案1】:

    好的,我想通了。 OpenGL 使用 drawArrays 函数通过将每 9 个值读取为一个三角形(3 个顶点,每个顶点有 3 个分量)来绘制三角形。正因为如此,顶点在三角形之间重复,所以如果两个相邻的三角形共享一个顶点,它会在数组中出现两次。所以我原本以为有 8 个顶点的立方体,实际上有 36 个!

    六个边,一个边两个三角形,每个三角形三个顶点,全部相乘成总共 36 个独立顶点,而不是 8 个共享顶点。

    整个问题在于指定的值太少。一旦我扩展了我的测试数组以包含 36 个值,它就可以完美地工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-19
      • 1970-01-01
      • 1970-01-01
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-19
      相关资源
      最近更新 更多