我很困惑如何填充我的顶点数组,以便它被正确绘制。我正在使用的OpenGL代码是:如何正确填充顶点数组
float vertices[size];
//Here I have a method to populate the array with my values from a 2D matrix
glGenVertexArrays(1, &vaoID[0]); // Create our Vertex Array Object
glBindVertexArray(vaoID[0]); // Bind our Vertex Array Object so we can use it
glGenBuffers(1, vboID); // Generate our Vertex Buffer Object
glBindBuffer(GL_ARRAY_BUFFER, vboID[0]); // Bind our Vertex Buffer Object
glBufferData(GL_ARRAY_BUFFER, (size) * sizeof(GLfloat), vertices, GL_STATIC_DRAW); // Set the size and data of our VBO and set it to STATIC_DRAW
glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0); // Set up our vertex attributes pointer
glEnableVertexAttribArray(0); // Disable our Vertex Array Object
glBindVertexArray(0); // Disable our Vertex Buffer Object
glBindVertexArray(vaoID[0]); // Bind our Vertex Array Object
glDrawArrays(GL_TRIANGLES, 0, size/3); // Draw
glBindVertexArray(0); // Unbind our Vertex Array Object
希望图像使这个更容易理解。在图像'A'中,左边的3x3矩阵显示了当我绘制三角形时我所得到的结果,右边的矩阵就是我想要实现的。
当前,当我创建要绘制的顶点数组时,我按照图像'B'中显示的顺序填充它。当我沿着一行工作时,我将值添加到数组中,然后逐步减少一列并重复。因此,例如:
vertexArray[0] = matrixElement[0].x
vertexArray[1] = matrixElement[0].y
vertexArray[2] = matrixElement[0].z
vertexArray[3] = matrixElement[1].x
vertexArray[4] = matrixElement[1].y
vertexArray[5] = matrixElement[1].z
输入到vertexArray此顺序给出了可以在图像“C”,在那里创建了错误的三角形可以看到结果。
我的问题是:以一种看起来像图像'A'中的右手图的方式,按照将在顶点的大矩阵上创建三角形的顺序,填充顶点数组的明智方式是什么。
如此大规模的数据重复?有没有一种方法可以存储顶点一次,但多次引用它们? – Calco
@Calco:当然,使用'GL_TRIANGLE_STRIP'&索引顶点提交通过'glDrawElements()'。 – genpfault
@Calco是的,有这样一种方式,那就是'glDrawElements'和元素数组缓冲区(也称为索引缓冲区或索引缓冲区对象)。 – immibis