代码之家  ›  专栏  ›  技术社区  ›  birgersp

无法在绘制元素后绘制数组

  •  0
  • birgersp  · 技术社区  · 10 年前

    我画了一个巨大的彩色表面(超过130万个指数)。该曲面使用“彩色顶点”进行索引。每当我尝试在缓冲三角形(有色顶点)之后绘制非缓冲三角形时,非缓冲三角形不会出现。

    如果我跳过绘制曲面(缓冲三角形),则非缓冲三角形将按应有方式绘制。

    绘制缓冲顶点:

    // Bind array buffer
    arrayBuffer->bind();
    
    // Set position of vertices
    int posLocation = program->attributeLocation("a_position");
    program->enableAttributeArray(posLocation);
    program->setAttributeBuffer(posLocation, GL_FLOAT, 0, 3, sizeof(ColoredVertex));
    
    // Set color of vertices
    int colorLocation = program->attributeLocation("a_color");
    program->enableAttributeArray(colorLocation);
    program->setAttributeBuffer(colorLocation, GL_FLOAT, sizeof(QVector3D), 3, sizeof(ColoredVertex));
    (GLfloat) 0.0);
    
    // Bind index buffer and draw triangles
    indexBuffer->bind();
    glDrawElements(GL_TRIANGLES, indicesCount(), GL_UNSIGNED_INT, 0);
    

    绘制非缓冲顶点(面):

    if (faces.size() == 0) {
        qDebug("Tried to draw a textured block with no faces.");
        return;
    }
    
    int posLocation = program->attributeLocation("a_position");
    int texcoordLocation = program->attributeLocation("a_texcoord");
    
    // Draw faces
    QHashIterator<Block::Side, Face> iterator(faces);
    while (iterator.hasNext()) {
        Face face = iterator.next().value();
    
        program->setAttributeArray(posLocation, face.coordinates.constData());
        program->enableAttributeArray(posLocation);
    
        program->setAttributeArray(texcoordLocation, face.textureCoordinates.constData());
        program->enableAttributeArray(texcoordLocation);
    
        glDrawArrays(GL_LINES, 0, 6);
    }
    
    1 回复  |  直到 10 年前
        1
  •  0
  •   birgersp    10 年前

    该错误似乎是由于未禁用属性数组并正确释放缓冲区对象所致。以下代码正常工作。

    绘制缓冲顶点:

    // Bind array buffer
    arrayBuffer->bind();
    
    int posLocation = program->attributeLocation("a_position");
    int colorLocation = program->attributeLocation("a_color");
    
    program->enableAttributeArray("a_position");
    program->enableAttributeArray("a_color");
    
    program->setAttributeBuffer(posLocation, GL_FLOAT, 0, 3, sizeof(ColoredVertex));
    program->setAttributeBuffer(colorLocation, GL_FLOAT, sizeof(QVector3D), 3, sizeof(ColoredVertex));
    
    // Bind index buffer and draw triangles
    indexBuffer->bind();
    glDrawElements(GL_TRIANGLES, indicesCount(), GL_UNSIGNED_INT, 0);
    indexBuffer->release();
    
    program->disableAttributeArray(posLocation);
    program->disableAttributeArray(colorLocation);
    
    arrayBuffer->release();
    

    绘制非缓冲顶点(面):

    if (faces.size() == 0) {
        qDebug("Tried to draw a textured block with no faces.");
        return;
    }
    
    int posLocation = program->attributeLocation("a_position");
    int texcoordLocation = program->attributeLocation("a_texcoord");
    
    program->enableAttributeArray(posLocation);
    program->enableAttributeArray(posLocation);
    
    program->enableAttributeArray(texcoordLocation);
    program->enableAttributeArray(texcoordLocation);
    
    // Draw faces
    QHashIterator<Block::Side, Face> iterator(faces);
    while (iterator.hasNext()) {
        Face face = iterator.next().value();
    
        program->setAttributeArray(posLocation, face.coordinates.constData());
        program->setAttributeArray(texcoordLocation, face.textureCoordinates.constData());
    
        glDrawArrays(GL_TRIANGLES, 0, face.coordinates.size());
    }
    
    program->disableAttributeArray(posLocation);
    program->disableAttributeArray(texcoordLocation);