OpenGL实例化数组奇怪的顶点位置

时间:2013-11-02 00:13:32

标签: c++ arrays opengl vbo vao

从使用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;
}

结果如下:

end result

请以某种方式帮助我。在过去的3天里,我一直在努力解决这个问题。谢谢大家!

编辑:我一直想知道它是否与列/行主要顺序有关,所以我试过转置矩阵,但我得出的结论并不是造成的这个问题。

1 个答案:

答案 0 :(得分:2)

我知道了!我已经尝试在着色器中使用纹理缓冲区对象和gl_InstanceID来实际获取矩阵列并且它有效!我无法相信我之前所做的事情是否应该不起作用并使用纹理缓冲对象(TBO)工作......但是,我将发布代码的重要部分以供将来参考。

/// A texture buffer object to pass to the shaders
GLuint sphereModelMatrixTBO;

/// A texture holding the matrices data
GLuint sphereModelMatrixTex;
.
.
.
/// Vector containing the model matrices of the spheres
std::vector<glm::mat4> dNodesModelMatrices;
.
.
.
glGenBuffers(1, &sphereModelMatrixTBO);
glBindBuffer(GL_TEXTURE_BUFFER, sphereModelMatrixTBO);
glBufferData(GL_TEXTURE_BUFFER, sizeof(glm::mat4) * dNodes.size(), &dNodesModelMatrices[0], GL_STATIC_DRAW);

在绘制程序中,我执行以下操作:

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_BUFFER, sphereModelMatrixTex);
glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA32F, sphereModelMatrixTBO);

glUniform1i(uniforms[UNIFORM_MODEL_MATRIX_BUFFER], 0);
.
.
.
glDrawElementsInstanced(GL_TRIANGLES, numSphereTris, GL_UNSIGNED_INT, NULL, (int)dNodes.size());

顶点着色器现在看起来如下:

uniform samplerBuffer sphereModelMatrixBuffer;
.
.
.
vec4 col1 = texelFetch(sphereModelMatrixBuffer, gl_InstanceID * 4);
vec4 col2 = texelFetch(sphereModelMatrixBuffer, gl_InstanceID * 4 + 1);
vec4 col3 = texelFetch(sphereModelMatrixBuffer, gl_InstanceID * 4 + 2);
vec4 col4 = texelFetch(sphereModelMatrixBuffer, gl_InstanceID * 4 + 3);
mat4 modelMatrix = mat4(col1, col2, col3, col4);

这就是你需要做的就是拥有一个功能齐全的快速OpenGL应用程序!这就是现在的样子: