I’m writing a graphics engine in C++ and DirectX 9, which I intend to use in a game in a later stage. Some time ago, I wrote a fully functional 2D engine and a basic 3D engine in OpenGL, but now I decided to start a new project in DirectX 9.
In my previous engines, I made a std::vector of type Entity which could contain subtypes like Tank, Weapon, Particle, etc. I would then, via polymorphism, do something like ‘for each Entity in std::vector do Entity->Draw()‘. The draw function would do glVertex3f(...).
Now my question is: how do you implement a similar object oriented framework in DirectX 9? I was thinking about keeping an array of vertices for each type (Tank, Weapon) which is loaded from a file, copying all vertices of all objects to the same vertex buffer en then calling DrawPrimitive for each subset of vertices. Is this the proper way to do this / the general way? Or should you create different vertex buffers for different objects in the scene for example? Some pseudocode would be nice to illustrate the correct implementation.
It depends what your aim is, if you want efficiency, you could load your meshes in a
std::unordered_mapwith an associated handle, same for textures, then this would allow you to instance the vectors of the same object type.If you want to keep it more simple, you could just create a container that stores all the entities and theer needed rendering info (similar to your ‘old’ engine), though will might want to use a culling data structure as well (like an Octree):
Here
std::shared_ptr<RenderInfo>would be unique to a VB/IB pair, to avoid unneeded duplication