gpt4 book ai didi

c++ - OpenGL实例数组奇怪的顶点位置

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:47:57 26 4
gpt4 key购买 nike

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

请以某种方式帮助我。在过去的三天里,我一直在为此苦苦挣扎。谢谢大家!

编辑: 我一直在想它是否与列/行的主要顺序有关,所以我尝试转置矩阵,但我得出的结论不是造成这种情况的原因问题。

最佳答案

我知道了!我已经尝试在着色器中使用纹理缓冲区对象和 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 应用程序所需要做的全部工作!这是现在的样子:

关于c++ - OpenGL实例数组奇怪的顶点位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19737460/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com