我对如何填充我的顶点数组以正确绘制它感到困惑。我正在使用的 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”中的右手图。