简体   繁体   English

用于存储顶点属性的数据结构

[英]Data Structure for storing Vertex Attributes

I need to implement a structure which stores vertices by the attributes. 我需要实现一种通过属性存储顶点的结构。 For example every vertex has an attribute like "Normals" or "Position" "Colors" etc., and they can be accessed by their names. 例如,每个顶点都有一个属性,例如“法线”或“位置”,“颜色”等,可以通过名称来访问它们。

VertexStructure vstruct;
AttribStream posStream = vstruct.CreateNewAttributeStream("Position",Vec3<double>);
posStream.push_back(Vec3<double>(23.5,12.5,11.3));

AttribStream normalStream = vstruct.CreateNewAttributeStream("Normals",Vec3<double>);
normalStream.push_back(Vec3<double>(0.03,0.02,0.18));

AttribStream indexStream = vstruct.CreateNewAttribStream("Indices",int);
indexStream.push_back(23);

std::cout << indexStream.size() << "," << normalStream.size() << posStream.size() << std::endl;

Now one easy way I can do it is to create an enum with pre-defined data types, like how OpenGL does it. 现在,我可以执行的一种简单方法是使用预定义的数据类型创建一个枚举,例如OpenGL的方式。 Like: 喜欢:

enum {
 DOUBLE = 0,
 FLOAT,
 INT,
};

 AttribStream posStream = vstruct.CreateNewAttributeStream("Position",DOUBLE,3);
 posStream.push_back_vec3d(23.5,12.5,11.3);

But, it is very verbose and I have to create an enum for every possible data type and have separate functions for each data type to push back new elements. 但是,这非常冗长,我必须为每种可能的数据类型创建一个枚举,并为每种数据类型具有单独的功能以推回新元素。

Another possible way is using Abstract classes. 另一种可能的方法是使用Abstract类。 But even that doesn't guarantee type-safety (unless using RTTI) and involves casting and also very hard to abstract it. 但是,即使那样也不能保证类型安全(除非使用RTTI),并且涉及强制转换,并且也很难对其进行抽象。 Also, there is no way to create a generic 'push_back' , so I need to create specialized functions for every type. 另外, 也没有办法创建通用的'push_back' ,因此我需要为每种类型创建专门的函数。

class PX_STREAMBase
{
public:

    template <typename T>
    PX_STREAMBase( T val) : value_type(typeid(T)),
    value_size(sizeof(val))
    {};

    virtual size_t bytes() const = 0;
    virtual size_t size() const = 0;
    virtual const void* data() const = 0;

    const std::type_info& value_type;
    const std::size_t value_size;

private:


};

template<typename T, template <typename, typename> class Container =  std::vector> class PX_STREAM;
template<typename T, template <typename, typename> class Container>
class PX_STREAM : public PX_STREAMBase {
public:
    PX_STREAM() :
    PX_STREAMBase( T())
    {}

    size_t bytes() const {
        return vec.size() * value_size;
    }
    size_t size() const {
        return vec.size();
    }
    const void* data() const {
        return vec.data();
    }
private:


    Container<T, std::allocator<T> > vec;
};

Lastly, there is using a char array struct as described in this question So, what is the best way to create a structure that abstracts the vector and can hold an attribute stream of any type? 最后,使用此问题中描述的char数组结构。那么,创建抽象该向量并可以保存任何类型的属性流的结构的最佳方法是什么?

Okay, so this looks like you're trying to create a struct to store data for a 3D model. 好的,看起来您正在尝试创建一个结构来存储3D模型的数据。 Vertex coordinates and normals. 顶点坐标和法线。 Honestly, I think you're just trying to make it to fancy. 老实说,我认为您只是想使其变得幻想起来。 A struct with a pair vec3<double> s that store the positions and normals (actually, these should be floats if you want to use them in openGL as most graphics cards have terrible support for double precision floats), and then whatever other data your system is storing for it's models. 具有一对vec3<double>的结构,用于存储位置和法线(实际上,如果要在openGL中使用它们,则它们应该是浮点数,因为大多数图形卡都对双精度浮点数提供了可怕的支持),然后再输入其他任何数据系统正在为其模型存储。 You use a vector of this structure and it keeps all of the information for any given vertex together. 您使用这种结构的向量,它将任何给定顶点的所有信息保持在一起。

If you really need to keep all of the data separate, I've not seen a system that has such a need yet but my experience is still more limited than I would like, then create a struct or class that just has a number of vectors that each contains a vec3 or whatever datatype you need for that particular structure. 如果您确实需要将所有数据分开,那么我还没有看到有这样的需求的系统,但是我的经验仍然比我想要的要局限得多,因此创建一个仅包含多个向量的结构或类每个都包含vec3或特定结构所需的任何数据类型。 Most systems know what kind of data they're going to be looking at for their models, so implementing these structures statically shouldn't be a problem. 大多数系统都知道要为模型寻找什么样的数据,因此静态实现这些结构不成问题。 For the final vertex data, you just create a structure that contains a number of indices for the separate components that make up a particular vertex. 对于最终的顶点数据,您只需创建一个结构,该结构包含构成特定顶点的各个组件的多个索引。

Is there a reason a much simpler system like: 是否有一个更简单的系统,例如:

class Model
{
private:
    //These vectors serve just to hold all of the data for all of your vertices
    vector<vec3<float> > positions;
    vector<vec3<float> > normals;
    vector<vec3<float> > colors;

    //This struct holds indices to the vectors above
    struct Vertex
    {
        int position;
        int normal;
        int color;
    };

    //Holds a list of all of your vertices, but allows you to pair different positions with different normals, etc...
    vector<Vertex> vertices;

    //Holds a list of indices from vertices that are your points to draw.
    vector<int> drawPoints;
public:
    //constructor, destructor, and accessors to add elements to your various elements.
};

wouldn't work? 不行吗

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

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