用于存储顶点属性的数据结构 [英] Data Structure for storing Vertex Attributes

查看:145
本文介绍了用于存储顶点属性的数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现一个存储顶点的属性的结构。例如,每个顶点都有Normals或PositionColors等属性,并且可以通过他们的名字访问它们。

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;

现在一个简单的方法是使用预定义的数据类型如何使用OpenGL。喜欢:

Now one easy way I can do it is to create an enum with pre-defined data types, like how OpenGL does it. 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.

另一种可能的方式是使用抽象类。但即使这并不保证类型安全(除非使用RTTI),并且涉及铸造,也很难抽象。此外,没有办法创建通用的push_back,因此我需要为每种类型创建专门的函数。

Another possible way is using Abstract classes. But even that doesn't guarantee type-safety (unless using RTTI) and involves casting and also very hard to abstract it. Also, there is no way to create a generic 'push_back', so I need to create specialized functions for every type.

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

最后,使用char数组struct 描述在这个问题
那么,什么是最好的方式来创建一个结构抽象出向量,可以容纳任何类型的属性流?

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?

推荐答案

好吧,创建一个结构来存储3D模型的数据。顶点坐标和法线。老实说,我想你只是想让它想象。一个带有 vec3< double> 的结构,它存储位置和法线(实际上,如果你想在openGL中使用它们,支持双精度浮点数),然后你的系统存储为它的模型的任何其他数据。你使用这个结构的向量,它保存任何给定顶点的所有信息在一起。

Okay, so this looks like you're trying to create a struct to store data for a 3D model. 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. You use a vector of this structure and it keeps all of the information for any given vertex together.

如果你真的需要保持所有的数据分开,没有看到一个有这样需要的系统,但我的经验仍然比我想要的有限,然后创建一个结构或类,只是有一些矢量,每个包含一个vec3或任何你需要的特定结构的数据类型。大多数系统知道他们将要为他们的模型查找什么样的数据,所以静态实现这些结构不应该是一个问题。对于最终的顶点数据,您只需创建一个结构,其中包含构成特定顶点的单独组件的多个索引。

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

无效?

这篇关于用于存储顶点属性的数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆