【问题标题】:Get object indices获取对象索引
【发布时间】:2014-03-27 14:42:03
【问题描述】:

我从 .obj 文件中加载了网格

o Plane_Plane.002
v 1.000000 0.000000 1.000000
v -1.000000 0.000000 1.000000
v 1.000000 0.000000 -1.000000
v -1.000000 0.000000 -1.000000
vt 0.000100 0.000100
vt 0.999900 0.000100
vt 0.999900 0.999900
vt 0.000100 0.999900
vn 0.000000 1.000000 0.000000
usemtl None
s off
f 2/1/1 1/2/1 3/3/1
f 4/4/1 2/1/1 3/3/1

我用数据顺序创建顶点缓冲区:

PosX,PosY,PosZ,NormX,NormY,NormZ,TexX,TexY

现在我必须生成索引来绘制这个平面,比如 0,1,2,0,2,3 或 0,1,2,3,4,5,因为我已经在顶点缓冲区中创建了 6 个顶点。我在这里真的很困惑:(

【问题讨论】:

    标签: opengl-es-2.0 mesh vertex-buffer indices


    【解决方案1】:

    您可以使用以顶点为键、索引为值的地图

    从 int counter = 0 开始,遍历所有顶点,如果地图已经包含这个顶点然后
    索引到这个顶点 = counter 然后设置 map[vertex] = counter++

    否则索引 = 地图[顶点]

    当然,您将为用于顶点的任何类型重载
    这是统一顶点的地图使用示例代码

    #include <iostream>
    #include <map>
    
    using namespace std;
    
    struct Point
    {
        float x;
        float y;
        float z;
    
        Point()
        {
    
        }
    
        Point(float _x, float _y, float _z)
        {
            x = _x;
            y = _y;
            z = _z;
        }
    
        bool operator<( const Point& p ) const
        {
            if(x < p.x)
                return true;
            if(y < p.y)
                return true;
            if(z < p.z)
                return true;
            return false;
        }
    };
    
    void main()
    {
        Point p[4];
        p[0] = Point(0,0,0);
        p[1] = Point(1,1,1);
        p[2] = Point(0,0,0);
        p[3] = Point(1,1,1);
    
        std::map<Point, int> indicesMap;
        int counter = 0;
    
        for(int i=0;i<4;i++)
        {
            if(indicesMap.find(p[i]) == indicesMap.cend()) // new vertex
            {
                indicesMap[p[i]] = counter++;
            }
        }
    
        for(int i=0;i<4;i++)
        {
            std::cout << indicesMap[p[i]] << std::endl;
        }
    }
    

    输出将是
    0
    1
    0
    1

    因为 p[2] 是 p[0] 而 p[3] 是 p[1]

    【讨论】:

    • 谢谢,很有帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 2020-05-04
    • 2023-03-03
    • 2012-02-21
    相关资源
    最近更新 更多