Instanced geometry problem

CactusJack

02-10-2010 20:03:07

Hey guys,

I've got the following problem: in my puzzle game I wish to draw a lot of cubes. I found out that there are 3 methods to do this: instanced geometry, static geometry, or simply draw them as individual entities.

Drawing them as individuals is not a good solution if there are more than 1k cubes.

Static geometry is OK for now, but later probably i will wish to move some cubes.

Instanced geometry maybe would be the best choice but I could not find any working sample on the web (neither for classic Ogre). I thought there are not so much difference between static and instanced geometries, but my code does not work.

string ID = Guid.NewGuid().ToString();
Entity Mesh = SceneMgr.CreateEntity("LvlElem" + ID, "ElemSimple.mesh");
Mesh.SetMaterialName("ElemSimple");
Mesh.CastShadows = false;

SceneMgr.DestroyInstancedGeometry("geom");
SceneMgr.DestroyStaticGeometry("geom");

InstancedGeometry ig = SceneMgr.CreateInstancedGeometry("geom");
//StaticGeometry ig = SceneMgr.CreateStaticGeometry("geom");

ig.AddEntity(Mesh, new Vector3(0, 20, 0));
ig.AddEntity(Mesh, new Vector3(0, 30, 0));
ig.AddEntity(Mesh, new Vector3(0, 40, 0));
ig.AddEntity(Mesh, new Vector3(0, 50, 0));

ig.Build();


The result is either seeing nothing or getting some mysterious access violations and other no-ogre exceptions. If I declare ig as StaticGeometry, everything is OK...

Please, help me with sharing some working code-snippets with instanced geometries.
Thanks in advance.

Jack

ianhfar

06-10-2010 14:56:38

Jack,

I hope this helps a little

This almost gets me there, the code doesn't throw exceptions, but my geometry flashes on the screen at different camera angle, so I think this might help you move forward, or at least experiment further

InstancedGeometry igBuildingGroup = sceneMgr.CreateInstancedGeometry("Building_02");
igBuildingGroup.BatchInstanceDimensions = new Vector3(1000000, 1000000, 1000000);
igBuildingGroup.AddEntity(building, Vector3.ZERO);
igBuildingGroup.AddEntity(building, Vector3.ZERO);
igBuildingGroup.AddEntity(building, Vector3.ZERO);
igBuildingGroup.AddEntity(building, Vector3.ZERO);
igBuildingGroup.Origin = Vector3.ZERO;
igBuildingGroup.Build();

uint count = igBuildingGroup.ObjectCount;
Console.WriteLine("" + count);

int x = 2000;
InstancedGeometry.BatchInstanceIterator iter = igBuildingGroup.GetBatchInstanceIterator();

while (iter.MoveNext())
{
InstancedGeometry.BatchInstance item = iter.Current;
Console.WriteLine(item.Name);
InstancedGeometry.BatchInstance.InstancedObjectIterator objIter = item.GetObjectIterator();
while (objIter.MoveNext())
{
InstancedGeometry.InstancedObject io = objIter.Current;
io.SetPositionAndOrientation(new Vector3(x, 2, 2000), Quaternion.IDENTITY);
io.NeedUpdate();
x += 2000;
}

}

iter = igBuildingGroup.GetBatchInstanceIterator();
while (iter.MoveNext())
{
InstancedGeometry.BatchInstance item = iter.Current;
Console.WriteLine(item.Name);
InstancedGeometry.BatchInstance.InstancedObjectIterator objIter = item.GetObjectIterator();
while (objIter.MoveNext())
{
InstancedGeometry.InstancedObject io = objIter.Current;
Console.WriteLine(io.Position.ToString());
}

}
igBuildingGroup.SetVisible(true);