我无法在OpenGL中使用VAO和三角形条渲染网格。
这是我的代码,用于生成顶点和索引并将它们放在各自的缓冲区中。
void HeightField::generateMesh(int tesselation) {
// generate a mesh in range -1 to 1 in x and z
int verticesPerRow = sqrt(tesselation / 2) + 1;
float* verts = NULL;
verts = new float[verticesPerRow*verticesPerRow*3];
//Generate Vertices
float z = 1;
int idx = 0;
for(int j = 0; j < verticesPerRow; j++) {
float x = -1;
for (int k = 0; k < verticesPerRow; k++) {
verts[idx++] = x;
verts[idx++] = 0;
verts[idx++] = z;
x += (2.0f/float(verticesPerRow-1));
}
z -= (2.0f / float(verticesPerRow-1));
}
printf("\n VERTICES \n");
for (int o = 0; o < verticesPerRow*verticesPerRow*3; o++) {
printf("%f, ", verts[o]);
if (o % 3 == 2) {
printf("\n");
}
}
//Generate indices
int rows = sqrt(tesselation / 2);
int columns = rows+1;
int* indices = NULL;
int indicesAmount = 2 * rows*columns + rows;
indices = new int[indicesAmount];
idx = 0;
for (int r = 0; r < rows; r++) {
for (int c = 0; c < columns; c++) {
indices[idx++] = (r + 1) * columns + c;
indices[idx++] = r * columns + c;
//printf("%i, %i, ", indices[idx - 2], indices[idx - 1]);
if (r == rows - 1) {
indices[idx] = (r + 1) * columns + c - 1;
}
}
if (r < rows - 1) {
indices[idx++] = 9999;
//printf(", %i, ", indices[-1]);
}
}
for (int o = 0; o < indicesAmount; o++) {
printf("%i, ", indices[o]);
if (o % 3 == 2) {
printf("\n");
}
}
m_numIndices = indicesAmount;
printf("Vertices Per Row: %i \n", verticesPerRow);
printf("Number of indices: %i \n", m_numIndices);
glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);
glGenBuffers(1, &m_positionBuffer); // Create a handle for the vertex position buffer
glBindBuffer(GL_ARRAY_BUFFER, m_positionBuffer); // Set the newly created buffer as the current one
glBufferData(GL_ARRAY_BUFFER, verticesPerRow*verticesPerRow * 3, verts, GL_STATIC_DRAW); // Send the vetex position data to the current buffer
glVertexAttribPointer(0, 3, GL_FLOAT, false/*normalized*/, 0/*stride*/, 0/*offset*/);
glEnableVertexAttribArray(0);
glGenBuffers(1, &m_uvBuffer); // Create a handle for the vertex position buffer
glBindBuffer(GL_ARRAY_BUFFER, m_uvBuffer); // Set the newly created buffer as the current one
glBufferData(GL_ARRAY_BUFFER, verticesPerRow*verticesPerRow * 2, texCoords, GL_STATIC_DRAW); // Send the vetex position data to the current buffer
//glVertexAttribPointer(2, 2, GL_FLOAT, false/*normalized*/, 0/*stride*/, 0/*offset*/);
//glEnableVertexAttribArray(2);
glGenBuffers(1, &m_indexBuffer); // Create a handle for the vertex position buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer); // Set the newly created buffer as the current one
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_numIndices, indices, GL_STATIC_DRAW); // Send the vetex position data to the current buffer}
这是我的代码来呈现它:
void HeightField::submitTriangles(void){
if (m_vao == UINT32_MAX) {
std::cout << "No vertex array is generated, cannot draw anything.\n";
return;
}
glEnable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex(9999);
glBindVertexArray(m_vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDrawElements(GL_TRIANGLE_STRIP, m_numIndices, GL_UNSIGNED_INT, 0);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
//glDisable(GL_PRIMITIVE_RESTART); }
以下是尝试渲染16x16网格的屏幕截图。
如果我增加我尝试渲染的行数和列数,我会得到更多的行和列,看起来像我想要的那样,但是&#34;轮廓&#34;整个图形看起来都是一样的,具有你在图片中看到的类似钻石的形状。
答案 0 :(得分:0)
问题在于
glBufferData(GL_ARRAY_BUFFER, verticesPerRow*verticesPerRow * 3, verts, GL_STATIC_DRAW);
显然它需要以字节为单位的大小,而不是数组的长度(愚蠢的我)。
将上述行更改为
glBufferData(GL_ARRAY_BUFFER, verticesPerRow*verticesPerRow * 3 * sizeof(float), verts, GL_STATIC_DRAW);
并且所有其他缓冲区同样解决了问题。