【问题标题】:Mesh grid using triangle strips rendering incorrectly?使用三角形条的网格网格渲染不正确?
【发布时间】:2018-05-19 14:35:31
【问题描述】:

我在使用 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 网格的屏幕截图。

如果我增加我尝试渲染的行数和列数,我会得到更多的行数和列数,看起来像我想要的那样,但是整个图形的“轮廓”看起来是一样的,你的形状像菱形见图片。

【问题讨论】:

    标签: c++ opengl rendering


    【解决方案1】:

    问题出在

    glBufferData(GL_ARRAY_BUFFER, verticesPerRow*verticesPerRow * 3, verts, GL_STATIC_DRAW);
    

    显然它需要以字节为单位的大小,而不是数组的长度(我很傻)。

    将上面的行改为

    glBufferData(GL_ARRAY_BUFFER, verticesPerRow*verticesPerRow * 3 * sizeof(float), verts, GL_STATIC_DRAW);
    

    同样对于所有其他缓冲区都解决了问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-06
      • 1970-01-01
      • 2017-12-09
      • 2013-10-23
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      相关资源
      最近更新 更多