Ive been spending the last couple of days making drafts of a directx+opengl abstraction layer and Ive run across some points that make me want to get input.

So basically, all the drawing data is stored in an "Entity" class as an abstract VertexDecleration class and some of the data like position and orientation are stored in an EntityInstance class. Drawing data is API specific so i have a glVertexDecleration + dxDecleration etc.....
I have a "mesh loader" class that reads a file, creates buffers (on system memory) along with some semantics and stuff. Then the mesh goes through an API specific class, lets call it "mesh\texture binder" that handles VBO, indexing and details like that. Then the entity is stored in a "cache" class. something like this:

Now, scenes are managed with a scenemanager that handles instances of Entities and is oblivious of the rest of the system, especially to the underlying VertexDecleration type and API. So FINALLY my question is: how do i elegantly collect entities to be drawn and propegate them to the API specific renderer? I know i can just cast upwards and stuff but that feels flakey to me...

So, id just like input on how you handle stuff like this in your own code... THANKS!

I would suggest making a graphics module and let that be the only set of code (with VERY few exceptions) that changes per API type.Inside the graphics module you have a CVertexBuffer, CIndexBuffer, etc., classes, and it is these that change with each API.

The outside world’s code is the same on every platform. Entities simply contain a CVertexBuffer and CIndexBuffer, they call an agnostic API to activate those buffers, and to render them. There is no reason an entity should ever know what DirectX or OpenGL is.

Additionally, while I simplified my example classes down to CVertexBuffer and CIndexBuffer, you should actually organize it how I described here: http://lspiroengine.com/?p=49

I read your blog post, i think thats sort of what im doing now. Entities, nodes in the scenemanager and the scenemanager themselves dont actually have any API specific code other than "Entity" storing the API agnostic data needed for rendering (position, bounding volume stuff like that) but also the api specific stuff like buffers and textures in the form of say a list of "vertex elements" and "Texture" classes like youv'e talked about in your post that i extend with the information that each API, the handles, sizes and all that.

Then these Entities are attached to nodes in the scenemanager which collects them every frame to dispatch to the API specific renderer in the form of a queue of vertexelements and materials, possibly arranged, sorted and grouped for instancing.

Creating an entity and rendering it might go something like this (at the lowest level):[source lang="cpp"]ElementList *elemList = ElementList::create();elemList.push_back(VertexElement::create(Position, someBufferOfData));elemList.push_back(VertexElement::create(Normal, someOtherBufferOfData));elemList.push_back(VertexElement::create(TextureCoords, yetAnotherBufferOfData));Texture *texture = Texture::create("./texture.tga");Entity someEntity(elemList, texture);SceneManager manager(...);manager.addNode(someEntity);Renderer renderer = Renderer::create();RenderingQueue queue = manager.getQueue();renderer.render(queue);[/source]I guess that what i was more specifically asking is that once the renderer does recieve the entities to be rendered, how do i avoid casting the VertexElements and Textures to the glTexture and glVertexElement. But looking at your code i guess i can subclass this.

As for the first section of your post, you have the classes backwards.Based on your submitted code, glVertexElement is the top-level class, which means some kind of factory or something needs to generate it based on which API is active.You use glVertexElement to add OpenGL features to VertexElement, which means to use those features you will constantly be casting to glVertexElement, since your entities are storing them as VertexElement types.

The hierarchy I suggested was like this:VertexElementBaseglVertexElement : public VertexElementBaseVertexElement : public glVertexElement

The class never needs to be cast. By having a VertexElement instance, its OpenGL/Direct3D features are already exposed.

You are violating the single-responsibility principal. The features of the class that are specific to OpenGL or DirectX don’t need to be exposed. No other classes should be handling that information; it is not their responsibility. The glVertexElement class itself should know how to handle its own GLuint handle etc.

I will use an index buffer for my example because it is the smallest and most concise piece of code. Here is the DirectX 9 version for rendering with an index buffer:

The OpenGL IBO handle m_uiVboId is never accessed outside of COpenGlIndexBuffer.The Direct3D 9 pointer m_pibIndexBuffer is never accessed outside of CDirectX9IndexBuffer.Those things are the responsibilities of those classes and no others. The index buffer itself knows how to perform a render operation.Don’t pass this type of responsibility off to anything else.Also note that no virtual functions are used. RenderApi is guaranteed to exist on from whatever class CIndexBuffer inherits.

For the second part of your question it is not clear if you understand not to pass entities to the render module.

in the form of a queue of vertexelements and materials

I guess that what i was more specifically asking is that once the renderer does recieve the entities to be rendered

Does it pass vertex elements and materials, or does it pass entities?

Either way is incorrect.Firstly, if you are passing vertex elements and materials, there is too much micro-managing and/or memory copying taking place, as well as another violation of the single responsibility principal.Instead of passing off materials, textures, vertex buffers, etc., to be handled by the render, why don’t the entities themselves just draw themselves (using abstracted API-agnostic functions exposed by the graphics module)? Entities themselves know best how to do that. If you try to make the renderer handle all the different things that can be passed off to it, you will soon find yourself lost in spaghetti. It will be unmanageable to try to handle all the different ways things can be drawn in one isolated spot.

Secondly, if you are passing entities, this is wrong in of itself. A renderer should never know what an entity is. An entity knows what a renderer is because an entity needs to be rendered and thus needs to know about certain aspects of the renderer. If the renderer then also knows what an entity is, you have a circular dependency or a hackish work-around to avoid it.

I suggest the following:The Entity class inherits from RenderQueueClient, which provides virtual functions the Entity class can overload for rendering itself.

The scene manager, after culling objects, then asks the remaining objects to put themselves into a render queue. There could be multiple entries for any given entity, since they can have many meshes each.A render item is this:

(A CRenderQueueSet is a pair of render queues, one for opaque and one for translucent. Entities themselves know where to put each render-queue item they have.)Notice that it passes a this pointer to the render queue.When it is time to render, the render queue uses that pointer to call the FinalRender() virtual function mentioned previously.

prqcSender here is the this pointer that was passed to the render queue’s AddItem().

Notice how the render queue was able to sort the items by shader and textures for reduced state switching, but then delagate the actual rendering of each object to the objects themselves.The render queue is part of the graphics module but it has no idea what entities are nor how to draw them.It simply exposes the building blocks necessary for rendering and lets renderable objects render themselves.

This is important for several reasons.Firstly, even if all you did was render models, that would already be a hairy mess to handle all within the renderer.But you also have water objects with a totally different way to render themselves.Terrain renders in a totally different way.Skies and clouds will have yet another entirely new set of ways to render.

Great! I better understand what you're saying now. The main reason i went down the path that i did was the idea that a renderable shouldn't be allowed to render itself in the context of a whole scene. But really optimizations like avoiding unnecessairy state changes, grouping for instancing and all that stuff don't really require me to centeralize my rendering code.