【发布时间】:2013-11-02 00:13:32
【问题描述】:
从使用 VBO/VAO 的普通绘图切换到使用 VBO/VAO 的实例化数组后,我无法从 OpenGL 应用程序获取正确的输出。输出应该是一堆球体,但它是疯狂定位的顶点,形成奇怪的三角形。我检查了调试器控制台中的顶点位置,但似乎没有任何可疑之处。它们都在 -10 到 10 的范围内,在场景中它们可能会达到 +/-30。
这是我的代码的重要部分。如果我没有看到任何东西,请告诉我。我将不胜感激!
char const *attribInSphereModelMatrix = "inSphereModelMatrix";
std::vector<glm::mat4> dNodesModelMatrices;
...
attributes[ATTRIB_SPHERE_MODEL_COLUMN0] = shader->getAttribLocation(attribInSphereModelMatrix);
attributes[ATTRIB_SPHERE_MODEL_COLUMN1] = attributes[ATTRIB_SPHERE_MODEL_COLUMN0] + 1;
attributes[ATTRIB_SPHERE_MODEL_COLUMN2] = attributes[ATTRIB_SPHERE_MODEL_COLUMN0] + 2;
attributes[ATTRIB_SPHERE_MODEL_COLUMN3] = attributes[ATTRIB_SPHERE_MODEL_COLUMN0] + 3;
...
// Create the vertex array object
glGenVertexArrays(1, &sphereVertexArray);
glBindVertexArray(sphereVertexArray);
// Create the buffer object to hold the sphere view matrix
glGenBuffers(1, &sphereModelMatrixBuffer);
glBindBuffer(GL_ARRAY_BUFFER, sphereModelMatrixBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::mat4) * dNodes.size(), &dNodesModelMatrices[0], GL_STATIC_DRAW);
// All matrix data is in one VBO, set the appropriate offsets
glEnableVertexAttribArray(attributes[ATTRIB_SPHERE_MODEL_COLUMN0]);
glVertexAttribPointer(attributes[ATTRIB_SPHERE_MODEL_COLUMN0], 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), NULL);
glVertexAttribDivisorARB(attributes[ATTRIB_SPHERE_MODEL_COLUMN0], 1);
glEnableVertexAttribArray(attributes[ATTRIB_SPHERE_MODEL_COLUMN1]);
glVertexAttribPointer(attributes[ATTRIB_SPHERE_MODEL_COLUMN1], 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (const GLvoid *)(sizeof(glm::vec4)));
glVertexAttribDivisorARB(attributes[ATTRIB_SPHERE_MODEL_COLUMN1], 1);
glEnableVertexAttribArray(attributes[ATTRIB_SPHERE_MODEL_COLUMN2]);
glVertexAttribPointer(attributes[ATTRIB_SPHERE_MODEL_COLUMN2], 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (const GLvoid *)(2 * sizeof(glm::vec4)));
glVertexAttribDivisorARB(attributes[ATTRIB_SPHERE_MODEL_COLUMN2], 1);
glEnableVertexAttribArray(attributes[ATTRIB_SPHERE_MODEL_COLUMN3]);
glVertexAttribPointer(attributes[ATTRIB_SPHERE_MODEL_COLUMN3], 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (const GLvoid *)(3 * sizeof(glm::vec4)));
glVertexAttribDivisorARB(attributes[ATTRIB_SPHERE_MODEL_COLUMN3], 1);
一旦我准备好设置,我就会去绘图。
glEnableVertexAttribArray(attributes[ATTRIB_SPHERE_MODEL_COLUMN0]);
glEnableVertexAttribArray(attributes[ATTRIB_SPHERE_MODEL_COLUMN1]);
glEnableVertexAttribArray(attributes[ATTRIB_SPHERE_MODEL_COLUMN2]);
glEnableVertexAttribArray(attributes[ATTRIB_SPHERE_MODEL_COLUMN3]);
...
glDrawElementsInstanced(GL_TRIANGLES, numSphereTris, GL_UNSIGNED_INT, NULL, (int)dNodes.size());
这里是顶点着色器的重要部分:
in mat4 inSphereModelMatrix;
...
void main()
{
vec4 v = vec4(inPosition, 1.0);
mat4 modelViewMatrix = viewMatrix * inSphereModelMatrix;
...
gl_Position = projectionMatrix * modelViewMatrix * v;
}
结果如下:
请以某种方式帮助我。在过去的 3 天里,我一直在为此苦苦挣扎。谢谢大家!
编辑:我一直想知道它是否与列/行主要顺序有关,所以我尝试转置矩阵,但我得出的结论不是导致的问题。
【问题讨论】: