gpt4 book ai didi

c++ - 如何在 3D 中绘制 2D 网格?

转载 作者:行者123 更新时间:2023-11-30 03:39:21 25 4
gpt4 key购买 nike

我正在尝试绘制一个带有三角形网格的简单“地板”,但只绘制了其中的四分之一。例如,这里是 5 x 5 网格尝试的屏幕截图:

Ignore the white thing in the corner, it's unrelated.

我有几个不同的 M x N 网格,它似乎每次都在绘制 ⌊(MxN)/4⌋ 正方形。

因此,对于 5 x 5 网格:

int QUAD_DEPTH = 5;
int QUAD_WIDTH = 5;
std::vector<GLfloat> vertices;

我正在加载一个 vector ;每个顶点六个 float (XYZRGB),每个三角形三个顶点,每个正方形两个三角形:

for (int row = 0; row < QUAD_WIDTH; row++) {
for (int dist = 0; dist < QUAD_DEPTH; dist++) {
vertices.push_back(0.0f + row); // X
vertices.push_back(0.0f); // Y
vertices.push_back(0.0f + dist); // Z
vertices.push_back(1.0f); // R
vertices.push_back(0.0f); // G
vertices.push_back(0.0f); // B

vertices.push_back(1.0f + row); // X
vertices.push_back(0.0f); // Y
vertices.push_back(1.0f + dist); // Z
vertices.push_back(0.0f); // R
vertices.push_back(1.0f); // G
vertices.push_back(0.0f); // B

vertices.push_back(1.0f + row); // X
vertices.push_back(0.0f); // Y
vertices.push_back(0.0f + dist); // Z
vertices.push_back(0.0f); // R
vertices.push_back(0.0f); // G
vertices.push_back(1.0f); // B

vertices.push_back(0.0f + row); // X
vertices.push_back(0.0f); // Y
vertices.push_back(0.0f + dist); // Z
vertices.push_back(1.0f); // R
vertices.push_back(0.0f); // G
vertices.push_back(0.0f); // B

vertices.push_back(0.0f + row); // X
vertices.push_back(0.0f); // Y
vertices.push_back(1.0f + dist); // Z
vertices.push_back(0.0f); // R
vertices.push_back(1.0f); // G
vertices.push_back(0.0f); // B

vertices.push_back(1.0f + row); // X
vertices.push_back(0.0f); // Y
vertices.push_back(1.0f + dist); // Z
vertices.push_back(0.0f); // R
vertices.push_back(0.0f); // G
vertices.push_back(1.0f); // B
}
}

然后我缓冲 float :

glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);

glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size(), &vertices[0], GL_STATIC_DRAW);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);

glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);

glBindVertexArray(0);

然后画出来:

glBindVertexArray(VAO);
glUniformMatrix4fv(glGetUniformLocation(this->shader->Program, "projection"), 1, GL_FALSE, &projection[0][0]);
glUniformMatrix4fv(glGetUniformLocation(this->shader->Program, "view"), 1, GL_FALSE, &view[0][0]);
glUniformMatrix4fv(glGetUniformLocation(this->shader->Program, "model"), 1, GL_FALSE, &this->model_matrix[0][0]);
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
glBindVertexArray(0);

我还尝试了 glDrawArrays(GL_TRIANGLES, 0, vertices.size()/6); 因为每个顶点有六个 float ,但结果相同。

我是缓冲不正确还是绘图不正确,还是我的问题出在其他地方?

最佳答案

问题出在您对 glBufferData() 的调用上:

glBufferData(GL_ARRAY_BUFFER, vertices.size(), &vertices[0], GL_STATIC_DRAW);

第二个参数是以字节为单位的大小,而您在 vector 中传递 float 。您需要将 vector 的大小乘以 float 的大小以获得以字节为单位的大小。例如:

glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(vertices[0]), &vertices[0], GL_STATIC_DRAW);

关于c++ - 如何在 3D 中绘制 2D 网格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38908474/

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