有什么好的算法可以用来从VBO获取EBO?有通用的解决办法吗?

问题描述 投票:0回答:1

我正在编写一个程序,从图像文件中读取顶点,但我在找出从所述图像文件中生成的顶点 VBO 构造 EBO 的最佳方法时遇到了困难。 (画画的时候用的是三角形)

我知道像 Blender 这样的程序可以在具有非常不同拓扑的模型中输出顶点和索引分开的文件格式。这对我来说意味着有一个从顶点列表创建 EBO 的通用解决方案,但我什至很难为 2D 顶点网格创建算法。我尝试环顾四周,但似乎找不到有关如何正确执行此操作的信息(可能使用了错误的搜索词)。所以我尽我最大努力自己做。

这就是我想到的:

for (int x = 0; x < (GridWidth - 1); ++x)
    {
    for (int y = 0; y < GridHeight; ++y)
        {
        if (y == 0)
            {
            Indices[(x * 3)] = Vertices[x * VBOSIZE];
            Indices[(x * 3) + 1] = Vertices[(x + 1) * VBOSIZE];
            Indices[(x * 3) + 2] = Vertices[(x + GridWidth) * VBOSIZE]; 
            }
        else if (y == (GridHeight - 1))
            {
            Indices[(x * 3)] = Vertices[(x + 1 - GridWidth) * VBOSIZE];
            Indices[(x * 3) + 1] = Vertices[(x + 1) * VBOSIZE];
            Indices[(x * 3) + 2] = Vertices[x * VBOSIZE]; 
            }
        else
            {
            //Left, Right, Left Down
            Indices[(x * 3)] = Vertices[x * VBOSIZE];
            Indices[(x * 3) + 1] = Vertices[(x + 1) * VBOSIZE];
            Indices[(x * 3) + 2] = Vertices[(x + GridWidth) * VBOSIZE]; 
            
            //Up Right, Right, Left
            Indices[(x * 3)] = Vertices[(x + 1 - GridWidth) * VBOSIZE];
            Indices[(x * 3) + 1] = Vertices[(x + 1) * VBOSIZE];
            Indices[(x * 3) + 2] = Vertices[x * VBOSIZE]; 
            }
        }
    }

我从左到右使用左、右、左下顶点和右上、右、左顶点制作三角形。 (见图)

令我困扰的是,它只适用于这个用例,并且需要检查你是否处于 ypos 0 和 height - 1。老实说,对我来说,这感觉就像是针对设计糟糕的算法的创可贴解决方案。如果有人有任何关于如何制定更通用的解决方案的线索或信息,我们将不胜感激。

algorithm opengl vertex
1个回答
0
投票

对于有 m 行和 n 列顶点的特殊情况,网格的索引可以这样生成:

    int *indices = pointer_to_a_block_of_ints;

    for (int row=0; row < rows - 1; ++row) {     //rows: grid height
        for (int col=0; col < cols - 1; ++col) { //cols: grid width
    
            const int left_top     = row * cols + col;
            const int left_bottom  = (row + 1) * cols + col;
            const int right_top    = row * cols + (col + 1);
            const int right_bottom = (row + 1) * cols + (col + 1);
    
            //- the idea is to render quads
            //- a quad can be decomposed into two triangles
            //- we start at the lower left corner, CCW
    
            //triangle 1
            *indices++ = left_bottom;
            *indices++ = right_top;
            *indices++ = left_top;
    
            //triangle 2
            *indices++ = left_bottom;
            *indices++ = right_bottom;
            *indices++ = right_top;
        }
    }

如上所述,此代码仅适用于网格的特殊情况。没有适用于所有类型网格的通用解决方案。想象一下球体的顶点集,其中两极只有一个顶点,并且朝向赤道的顶点数不断增加。

创作工具(如搅拌机)确实知道网格是如何构造的,因此能够根据该知识导出顶点和索引数据。如果没有这些信息,一组顶点只不过是一组顶点(可以是从点到线到线带到三角形到三角形带到三角形扇形或任何非原始的任何东西)。

© www.soinside.com 2019 - 2024. All rights reserved.