Copies of Entities


10-04-2009 18:22:10

The Problem:
I must load between 0 and 256 copies of a mesh. The only differences between these meshes are the textures which I will apply, and their location.

What I've Done:
Originally I was using sceneManager.CreateEntity() to create each of these, but I noticed that this causes substantial load time and memory usage. (Approx. 1mb of PF per mesh, and 1-2 seconds to load each)

I then attempted to use Entity.clone(), hoping that the mesh itself would not be duplicated, simply multiple references allowing for different positioning and names. Unfortunately clone() seems to do the same as CreateEntity(), only duplicating the current mesh instead of loading a specified one.

Is there a way to quickly and with relatively low memory usage, load many instances of the same mesh, that allows for unique positioning and texturing?

Other Thoughts:
I think that this must be a common problem, and can be achieved rather simply. Unfortunately I haven't been able to find anything in the documentation or through web searches so far. If someone could point me in the right direction, I would greatly appreciate the assistance.

Thanks in advance.

| Ogre 1.6.1 | Python-Ogre 1.1 R845 Stable | Windows XP SP3 | Python 2.5.4 |


11-04-2009 09:14:46

Yes there is, and its called 'Instancing' :)

There are lots of ways to do this though, and a lot depends on whether you want your meshes to be animated or not, and how you want to handle your materials.

for a start check out the Grass demo which demonstrates how to use staticGeometry, that allows you to place many copies of a static mesh in the scene, but ogre essentially treats them all as one mesh, which makes things a bit faster.

Next have a look at the instanced geometry demo ( i think this is only in the c++ ogre demos at the moment, i will have a go at converting it to python soon, it should work just as well ). This one demonstrates lots of animated meshes.

You can also look at level of detail to perhaps replace meshes that are far away with low poly versions or billboards ( this is what the forests demo does, using pagedgeometry ). The main problem you have is the need to have a different material on every mesh, you will have to be clever here because its the number of materials that really slow things down, rather than the number of meshes. A good start would be use a texture atlas, and try to cram as many of the textures you need into that, then use a simple vertex shader to move the uv coordinates for different meshes.

heres a useful link to get started with the theory:


14-04-2009 18:57:44

Thanks for the help, I'll look it over and see what I can use.

Also, rather than making a new post for what seems like a simple problem:
I'm creating a material programmatically, and running into an issue where instead of displaying the texture on the mesh, I just get a blank white copy of the mesh.
# strkey is a unique identifier (string) used to refer to the entity, it's texture, and it's material
mat = ogre.MaterialManager.getSingleton().create('mat' + strkey, 'General')
mat.createTechnique().createPass().createTextureUnitState().setTextureName('tex' + strkey)

I've checked to make sure that the texture is loaded properly (if using a pre-defined material, everything displays properly). I'm guessing there must be a setting or something I'm missing, but all the examples I found online stayed pretty similar to what I have, and any changes I attempted to copy into my code made no difference on the outcome.
mat = ogre.MaterialManager.getSingleton().getByName('Scene/Generic')
mat.getTechnique(0).getPass(0).getTextureUnitState(0).setTextureName('tex' + strkey)
The working code, loading an existing material.
Update: Figured out my problem. I was setting the entity to the wrong material. :oops: