Patterns vs. Switch

I am trying to find my way in C ++ land and am getting more and more confused. The toy application is a very simple OpenGL engine. So here is a (I think simple) problem: I would like to process grids of different vertex data, so I would have, for example,

struct Vertex { Vector3f position; } struct VertexN : public Vertex { Vector3f normal; } 

Now I need a Mesh class that stores data and draws it. I tried something like this:

 template<class T> class Mesh { public: Mesh(); ~Mesh(); void load(const T * vertices, int num); void draw(); protected: T * vertices_; 

};

Different vertices need to be loaded and drawn in different ways, and this can be done using specialized specialization.

My problem is that I like to have another class that contains instances of Mesh objects, but the template members of the class are obviously not allowed.

Another solution I can come up with is to hold pointers to the Vertex base structure in Mesh, pass the identifier for the Vertex type used, and then use the switch statements in load () and draw () to allow various implementations.

What is the best way to do this?

Any help is greatly appreciated.

+4
source share
3 answers

You can use inheritance and virtual functions. For instance:

 class MeshBase { public: virtual ~MeshBase() { } virtual void draw() = 0; }; template <typename T> class Mesh : public MeshBase { public: virtual void draw() { } // ... }; 

With this approach, you can store pointers in the MeshBase base class in a container.

Ideally, you can use a pointer container that will manipulate pointers for you, or you can use a container of smart pointers (e.g. std::vector<std::shared_ptr<MeshBase> > if your implementation includes shared_ptr , if not, it can be found in several places ).

I would also recommend storing vertices in a container of the Mesh class rather than using manual dynamic allocation and memory management.

+7
source

I recommend you not templatize your mesh class, but make it capable of handling various vertex data types. This is a common graphics problem and is discussed in DirectX with the concept of linking different data streams together to draw geometry. In other words, your normals will be a different data stream than your location data. Then your mesh object becomes a container with multiple data streams and will not be bound to a specific vertex format at compile time. I do not know OpenGL either, but I assume that there is a similar concept.

See: http://msdn.microsoft.com/en-us/library/bb147299(VS.85).aspx

0
source

@bshields has a point, you need to provide data tops the most effective way, that in case of OpenGL - this vertex buffer objects (VBOs): http://www.opengl.org/wiki/Vertex_Buffer_Object

Given the recommendations given in the link above, which are consistent with what @James McNellis says you don’t use inheritance for your vertex types, and the fact that loading and drawing are likely to depend on the type of vertices and input type (or output) I would suggest you apply a strategy template using static polymorphism as indicated in the answer to this other question: template strategy template

0
source

Source: https://habr.com/ru/post/1313405/