简体   繁体   English

OpenGL VBO 索引(如何计算索引数组)

[英]OpenGL VBO Indexing ( How to compute Index Array)

We are migrating OpenGL to newer version ( ES 2.0 ).我们正在将 OpenGL 迁移到更新版本(ES 2.0)。 Application actually renders Vector images ( ie CGM files).应用程序实际上呈现矢量图像(即 CGM 文件)。 I have successfully rendered the graphics using vertices using VBO.我已经使用 VBO 使用顶点成功渲染了图形。 But the problem is performance.但问题是性能。 DisplayList performance is way better than VBO. DisplayList 性能比 VBO 好得多。 So I am thinking using VBO indexing.所以我正在考虑使用 VBO 索引。 How to come up the indices array?如何提出索引数组? Will Indexing improve performance?索引会提高性能吗? Please find my code below请在下面找到我的代码

//This is my data structure

struct DisplayIndexID {
        int idx;
        DrawStateT drawState;

        //Every display Index ID has its own draw models.
        std::vector<std::unique_ptr<vertexModel>> readytoDrawModels;
    };

//Initializing the VBO 
void initVbo(std::vector<DisplayIndexID> & v)

{
    glBindVertexArray(geomVAO);
    glBindBuffer(GL_ARRAY_BUFFER, geomVBO);
    std::vector<QVector3D> vecToDraw;
    std::vector<QVector3D> finalVecToDraw;
    for (int j = 0; j < v.size(); j++)
        for (auto& vModel : v[j].readytoDrawModels)
        {
            if (vModel) {
                vecToDraw = vModel->getVertices();
                finalVecToDraw.insert(finalVecToDraw.end(), vecToDraw.begin(), vecToDraw.end());
    
            }
        }
    
    glBufferData(GL_ARRAY_BUFFER, sizeof(QVector3D) * finalVecToDraw.size(), &finalVecToDraw[0],GL_STATIC_DRAW );
glBindBuffer(GL_ARRAY_BUFFER, 0);
}

//Rendering function 
void drawDisplayLists(std::vector<DisplayIndexID> & v)
{
    GLintptr offset = 0;
    
    initVbo(v);
    
    for (int i = 0; i < v.size(); i++)
    {

        //***********PRINT AREA***********************/
        for (auto& vModel : v[i].readytoDrawModels)
        {
           glBindBuffer(GL_ARRAY_BUFFER, geomVBO);
           glEnableVertexAttribArray(0);
           glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(QVector3D), (GLvoid*)offset); 
           switch (vModel->getDrawMode())
            {
                case 0: //GL_POINTS
                    glDrawArrays(GL_POINTS, 0, vModel->getVertices().size());
                    break;

                case 1: //GL_LINES
                    glDrawArrays(GL_LINES, 0, vModel->getVertices().size());
                    break;
                case 3: // GL_TRIANGLE
                     ...
        }
                  offset += sizeof(QVector3D) * vModel->getVertices().size();
    }  



}

I never worked with vector graphics but this is just on how to create indices from some vertices.我从未使用过矢量图形,但这只是关于如何从某些顶点创建索引。 So if I read your code right, your vertices are stored in the finalVecToDraw vector.因此,如果我正确阅读了您的代码,您的顶点将存储在finalVecToDraw向量中。 First create a vector for the indices, the supported types are unsigned char up to unsigned int .首先为索引创建一个向量,支持的类型是unsigned charunsigned int

Then resize that vector to the size of your vertex array (this will save you much time resizing the vector).然后将该向量调整为顶点数组的大小(这将为您节省大量调整向量大小的时间)。

Fill the vector, so that the element at index n has the value n.填充向量,使索引 n 处的元素具有值 n。 So for example with this code:因此,例如使用以下代码:

for(int i = 0; i < indices.size(); i++) {
    indices[i] = i;
}

This will do what you already do, but using indices.这将做你已经做的,但使用索引。

Now we need to remove the duplicate entries in the vertices vector and adjust the indices.现在我们需要删除顶点向量中的重复条目并调整索引。 I don't know what algorithm would be the best to find duplicates for your case, but you need to do this.我不知道哪种算法最适合为您的案例找到重复项,但您需要这样做。 The duplicate is at the index d and the first time this value showed up was at o .副本位于索引d处,并且该值第一次出现在o处。 d must be bigger than o : d必须大于o

  1. Delete the vertex at d.删除 d 处的顶点。
  2. Iterate over your indices and if an element has a value bigger than d, subtract one from it.遍历您的索引,如果一个元素的值大于 d,则从中减去一个。
  3. Set the value of the index at d to o.将 d 处的索引值设置为 o。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM