Question

I got mesh loaded from .obj file

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

and I create vertex buffer with data order:

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

now do I have to generate indices to draw this plane like 0,1,2,0,2,3 or 0,1,2,3,4,5 because I already created 6 vertices in my vertex buffer. I'm really confused here :(

Était-ce utile?

La solution

You can use a map with the vertex as the key and the index as value

Starting from int counter = 0, traverse all vertices, if the map already contain this vertex then
index to this vertex = counter then set map[vertex] = counter++

Otherwise the index = map[vertex]

Of course you will to overload < operator for any type you use for vertex as the map expects its key to be comparable

Here is a sample code of map usage in unifying vertices

#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;
    }
}

The output will be
0
1
0
1

as p[2] is p[0] and p[3] is p[1]

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top