Jump to content
Site Stability Read more... ×
  • Advertisement
Sign in to follow this  

Design issues with templates and interfaces

This topic is 2956 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've got a bit of a design problem.

I used to have a class VertexArray which one could populate with instances of the class Vertex. This vertex would contain position, texture coordinates, normal, tangent, bitangent and skinning data. Now very often I don't need shading or skinning data (when rendering sprites or lines, e.g.), so I created several vertex classes: SimpleVertex, ShadedVertex, etc. These classes must, however, not be inherited from one another, as I need to be sure of the order of their elements and their exact size when uploading them to the graphics card. The same holds for the corresponding VertexArray class. So I chose to convert it to a template instead:

template<class VertexType>
class VertexArrayTemplate

Geometry::Topology m_topology;
vector<VertexType> m_vertices;



typedef VertexArrayTemplate<SimpleVertex> SimpleVertexArray;

typedef VertexArrayTemplate<SkinnedVertex> SkinnedVertexArray;
typedef VertexArrayTemplate<ShadedVertex> ShadedVertexArray;

typedef VertexArrayTemplate<ShadedSkinnedVertex> ShadedSkinnedVertexArray;

To upload data to a vertex buffer on the graphics card, I've used the following function of my GraphicsEngine class:

virtual VertexBuffer *GraphicsEngine::addVertexBuffer( shared_ptr<const VertexArray> vertexArray ) = 0;

But now that VertexArray is a template class, I would have to declare this function a template in order to be able to add vertex arrays using different vertex definitions. That, however, is not possible, as GraphicsEngine itself is an interface for classes such as OGL3GraphicsEngine, D3D10GraphicsEngine, etc. and interfaces are not allowed to have template member functions.

So my question is - is there a way to avoid having to add a separate function overload in the interface and all implementations of GraphicsEngine for each template spezialization of a new vertex definition?

Share this post

Link to post
Share on other sites
You could derive VertexArray from a base class with virtual methods for all of the critical operations... not sure how that fits into your overall engine data flow, though.

Share this post

Link to post
Share on other sites
ApochPiQ has answered the best, IMO, and popular used solution.
Inherit your template class from a non-template base class which expose virtual functions to be implemented by the template class.

However, I think maybe another idea is to change the vertex design.
Vertex should be able to inherit from other class. If you want strict binary layout, each vertex holds a POD data structure with the desired binary layout so vertex is free to in any form.
Then each vertex may expose a virtual function such like,

virtual void * getData(int * outDataSize);

To return the data pointer the size.

BTW, I think it's not a good idea to depend on binary layout of non-POD class. Though most compilers have the same layout, it's not standard.

Share this post

Link to post
Share on other sites
thanks for the input you two!

I've derived VertexArrayTemplate from a base class which declares everything the graphics engine needs via virtual member functions, working fine.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!