具有非硬编码元素数组的 EBO
我正在尝试创建一个元素/索引数组,并使用一定量的内存动态构建该数组。但是,我的代码仅在硬编码该数组时才有效。特别是,以下是所有相关代码:
GLfloat elements[15 * 15 * 2 * 3 * 2];
GLuint second_elements[] = {
0, 16, 17,
0, 1, 17,
1, 17, 18,
1, 2, 18,
...
};
GLuint VBO;
glGenBuffers(1, &VBO);
GLuint EBO;
glGenBuffers(1, &EBO);
GLuint VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (void*)0);
glEnableVertexAttribArray(0);
...
glDrawElements(GL_TRIANGLES, sizeof(elements) / sizeof(elements[0]), GL_UNSIGNED_INT, 0);
当我使用 elements
时,我的代码不会呈现任何内容。当我使用“second_elements”时,我的代码完美地呈现了一切。我已经确认它们保存相同的数据,并尝试仅渲染每个三角形作为测试,但我似乎无法将 EBO 正确加载到 VAO 中。有人可以帮忙吗?
I'm trying to create an element/index array that I dynamically build with a set amount of memory. However, my code only works when I hard-code this array. In particular, the following is all the relevant code:
GLfloat elements[15 * 15 * 2 * 3 * 2];
GLuint second_elements[] = {
0, 16, 17,
0, 1, 17,
1, 17, 18,
1, 2, 18,
...
};
GLuint VBO;
glGenBuffers(1, &VBO);
GLuint EBO;
glGenBuffers(1, &EBO);
GLuint VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (void*)0);
glEnableVertexAttribArray(0);
...
glDrawElements(GL_TRIANGLES, sizeof(elements) / sizeof(elements[0]), GL_UNSIGNED_INT, 0);
When I use elements
, my code doesn't render anything. When I use 'second_elements', my code renders everything perfectly. I've confirmed that they hold the same data, and have tried to just render one triangle from each as a test, but I just can't seem to get the EBO to load into VAO properly. Can anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,(出于某种原因)如果我使用 std::vector 而不是普通的堆内存,我可以通过传入 elements.size() 和
&元素[0]
。我不知道为什么,但我会拿走我能得到的。Turns out, (for some reason) if I use a
std::vector
instead of normal heap memory, I can make this work by passing inelements.size()
and&elements[0]
. I don't know why, but I'll take what I can get.