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

时间:2016-08-12 01:27:19

标签: c++ opengl vector std

我试图用一个三角形网格绘制一个简单的“地板”,但只绘制了它们的第一个四分之一。例如,以下是尝试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;

我正在加载一个向量;每个顶点六个浮点数(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
  }
}

然后我缓冲浮动:

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);,因为每个顶点有六个浮点数,但结果相同。

我是否错误地缓冲或绘图不正确或是我的问题在其他地方?

1 个答案:

答案 0 :(得分:2)

问题在于您致电glBufferData()

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

第二个参数是以字节为单位的大小,同时传递向量中的浮点数。您需要将向量的大小乘以float的大小以获得以字节为单位的大小。例如:

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