Is it possible to store different 3D primitives in the same container?

Hello oF users,

I’m currently working on a simple 3D scene editor with oF, and with that kind of software, one of the first questions that I cannot seem to answer properly is : “what’s the best way to store all the objects of the scene ?” .

What I’d really like to do is a kind of wrapper object that has all the properties / methods of a normal 3D primitive, with some stuff along like a list of the materials or shaders to apply to this primitive. It would feel much more intuitive to me to have all of these gathered in the same object than to have separate lists of each.

I tried some things like coding a sort of wrapper that inherits of3dPrimitive, but since of3dPrimitive is the base class, it makes the objects impossible to use “as is” (you can’t call setRadius on an of3dPrimitive…). My C++ is a bit rusty with things like inheritance and I know it can be tricky sometimes, I also tried something with templates but no more luck.

For the moment I just have a vector of of3dPrimitives * that lets me use the common functions to all primitives. I’d like not to have to cast every object every time I want to interact with it… In the future I’d also like to be able to import external 3D meshes ; is there a way to put 3D meshes (e.g. ofMesh obtained with Assimp) and 3D primitives in the same container ? I was thinking of storing just the ofMesh of the primitives, but since I don’t know how to re-create a primitive from an ofMesh, I would be at a loss… :frowning:

So, what is the best way to store 3D objects in a scene ? Is it a good idea to wrap them in order to store more data about them (materials/shaders to use) ? Is there a convenient way to deal with both ofMesh (external 3d objects) and of3dPrimitives ?

Thank you for your help :slight_smile:

hi @Pando, maybe this addon is what are you looking for ofxScene i think you should be able to do your idea, since it has a scene graph structure similar to OpenSceneGraph (OSG) or you could use OSG inside Openframeworks with ofxOpenSceneGraph addon.

that’s how inheritance/polymorphism works, once you put something on a container you can’t know of which specific type it is but calling on it’s base class methods will actually call the correct implementation of each instance.

Usually you would store a reference to all your of3DPrimitives in a vector<of3DPrimitive*> and keep the specific instances around as variables or in vectors of the specific type if you have lots of them.

You would use the generic container to for example draw everything and the specific instances to update things.

Also you can create an of3dPrimitive from an existing mesh like:

of3DPrimitive primitive;
primitive.getMesh() = assimp.getMesh(..);

then you can put a reference to that 3d primitive in your scene vector

Okay, thanks for the insight : )

I didn’t think of use of3dPrimitive::getMesh like that ! :smiley:

Well for the moment, I think I’ll just have a vector of ofMesh, independently of where they come from (OF primitive or external mesh), and a vector of object informations about each mesh (what kind of primitive, materials and shaders to apply on it, etc. ).

This way, I should be able to re-create the correct primitive “on the fly” each time I want to make a change.

Maybe I’ll give a try with ofxScene if I need something more complex. But it is intended to stay simple at the moment (essentially to test and visualize effects of materials / shaders / kinds of lights on the geometry).

Thank you for the answer.