Instancing

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
}{ermanssoN
Posts: 37
Joined: Mon May 26, 2008 7:39 pm

Instancing

Post by }{ermanssoN »

Has anybody instanced a node??

I wish to render one node several times on differens places on the same frame.

i got a list of clients. each frame the clients send ther position and rotation.

basicli I wan't to be able to loop trough them and render the att the corect spot

Code: Select all

for( u8 i=0; i< numClients; i++)
{
   node->setPosition(  client[ i ]->Position );
   node->updateAbsolutPosition();
   node->render();
 
}
Unforunaly it doesen't work that way :(


when i cal node->render() nothing hapens.

It's realy anying to be dependent on the scenemanager to render a node.

So if anyone have done instancing or has an idea how to do it please post :)

Im not asking how to hardware instance just how to render the same node serveral times on the same frame

// }{ermanssoN
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

When rendering you have to do it between driver->beginScene and driver->endScene. Plus if you then call smgr->drawAll AFTER you've rendered your nodes individually then the scene will draw over the top and possibly obscure them.

I wonder if you can create the node but then remove it from the scene graph somehow so that smgr->drawAll doesn't render them but you can still render them yourself....
Image Image Image
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

There's not much more overhead when you generate scene nodes for each mesh...
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

If you're determined to do this, then you could:

Move your calls to render() after smgr->drawAll(); and it will work, after a fashion. However, if you're rendering an opaque object, it will crap all over any translucent nodes since it's rendered out of order.

Alternatively, create a custom scene node type that encapsulates whatever node you want to render, and render the node multiple times in the desired render pass.

Code: Select all

#include <irrlicht.h>
#include <assert.h>

#pragma comment(lib, "Irrlicht.lib")

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

struct NodeInstancer : public ISceneNode
{
	NodeInstancer(ISceneNode * parent, ISceneManager * smgr, 
				  IAnimatedMeshSceneNode * meshNode, E_SCENE_NODE_RENDER_PASS renderPass) 
		: ISceneNode(parent, smgr)
	{
		MyNode = meshNode; 
		MyNode->grab();
		RenderPass = renderPass;
	}

	~NodeInstancer()
	{
		MyNode->drop();
	}

	const core::aabbox3d<f32>& getBoundingBox() const { return MyNode->getBoundingBox(); }

	void OnRegisterSceneNode()
	{
		SceneManager->registerNodeForRendering(this, RenderPass);
		ISceneNode::OnRegisterSceneNode();
	}

	void render()
	{
		for(f32 x = -50.f; x <= 50.f; x += 50.f)
		{
			MyNode->setPosition(vector3df(x, 0, 0));
			MyNode->updateAbsolutePosition();
			MyNode->render();
		}
	}


	IAnimatedMeshSceneNode * MyNode;
	E_SCENE_NODE_RENDER_PASS RenderPass;
};

int main()
{
    IrrlichtDevice *device =
       createDevice( video::EDT_OPENGL, dimension2d<s32>(640, 480), 16,
          false, false, false, 0);

    IVideoDriver* driver = device->getVideoDriver();
    ISceneManager* smgr = device->getSceneManager();

    IAnimatedMeshSceneNode* dwarf = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/dwarf.x"));

	NodeInstancer myNode(smgr->getRootSceneNode(), smgr, dwarf, ESNRP_SOLID);
	dwarf->remove();

	smgr->addCameraSceneNode(0, vector3df(0, 40, -80));

    while(device->run())
    {
        driver->beginScene(true, true, SColor(255,100,101,140));
        smgr->drawAll();
        driver->endScene();
    }

    device->drop();

    return 0;
} 
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
}{ermanssoN
Posts: 37
Joined: Mon May 26, 2008 7:39 pm

Post by }{ermanssoN »

I mean, what if you have an multiplayer game. 5 v s players and each player has a rocket launcher. First of all, if you can't instance a model you would have loaded 10 highpoly charcters wich has their own nodes. having 10 highpoly models in the same frame would draw much fps. and if those players have a rocket launcher witch fires rockets, each rocket would need it's own mesh and a node. I could easily be like 5 players and 20 rockets on the same place. With out instancing their will be imposible to maintain even average fps. I know Ogre has an instacing demo and Ogre like Irrlicht uses scene nodes and a scenemanager. I don't know how ogre does with rendering tough.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

}{ermanssoN wrote:First of all, if you can't instance a model ...

Code: Select all

#include <irrlicht.h>

#pragma comment(lib, "Irrlicht.lib")

using namespace irr;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;


int main()
{
    IrrlichtDevice *device =
       createDevice( video::EDT_OPENGL, dimension2d<s32>(640, 480), 16,
          false, false, false, 0);

    IVideoDriver* driver = device->getVideoDriver();
    ISceneManager* smgr = device->getSceneManager();

	IAnimatedMesh * dwarf1 = smgr->getMesh("../../media/dwarf.x");
	IAnimatedMesh * dwarf2 = smgr->getMesh("../../media/dwarf.x");

	if(dwarf1 != dwarf2)
	{
		(void)printf("I'll bet my hard drive that these are the same object.");
		(void)system("format c:\");
	}

    device->drop();

    return 0;
} 
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Yeah irrlicht has a useful thing called a mesh cache. When you load a mesh its filename is first checked to see if it was loaded before, if so it just returns a pointer to the previously loaded one, otherwise it goes ahead and loads from the disc.

Same for the texture cache.

So you just need to have a Scene Node for each object that uses the same mesh, which basically is instancing i suppose.... You can think of the scene node merely as an object which says i'm using 'this' mesh and i'm at 'this' position/rotation/scale etc. then when it's rendered it uses the details from the mesh to draw itself.
Image Image Image
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

}{ermanssoN wrote:having 10 highpoly models in the same frame would draw much fps.
Out of interest, what makes you think that rendering the same mesh 10 times would be faster than rendering 10 unique instances of that mesh? You're throwing the same number of polygons at the GPU either way.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Yeah well pointed out rogerborg... having the mesh loaded 10 times would just mean 10x more memory usage rather than anything else.

Plus you'd be unlikely to have a super high poly rocket launcher model anyway, especially if there's gonna be lots of people using it in the game!
Image Image Image
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

}{ermanssoN was probably referring to gpu instancing, which offloads processing from the cpu to the gpu. that's why i wrote a thread asking for information about texture arrays.

i read this one on gpugems as well.

played around with the demo as an added bonus.
Image
Saturn
Posts: 418
Joined: Mon Sep 25, 2006 5:58 pm

Post by Saturn »

rogerborg wrote:
}{ermanssoN wrote:having 10 highpoly models in the same frame would draw much fps.
Out of interest, what makes you think that rendering the same mesh 10 times would be faster than rendering 10 unique instances of that mesh? You're throwing the same number of polygons at the GPU either way.
Mostly because of less CPU overhead. Using instancing requires a single draw call instead of 10, and games more often than not are still CPU limited.

This kind of instancing more or less works like this: You have one set of vertex/index buffers with the model and a second buffer with object matrices. First the first matrix is set and the model is drawn, when done, the next matrix is set and the model is drawn again. Lather, rinse, repeat.
It is actually a bit more complex and flexible than this, but that's the general idea. And the nice thing is, is that the management is done in the GPU, the CPU is free to do other things than managing draw calls.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Sounds similar to hardware skinning then i think, if that's what i'm thinking of. Where you can effectively animate models on the GPU and hence keep the animated model in a vertex buffer. sio2 did a little demo of that with irrlicht.
Image Image Image
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

"I know Ogre has an instacing demo and Ogre like Irrlicht uses scene nodes and a scenemanager."

This is what he was talking about. Definitely GPU assisted instancing.

http://www.ogre3d.org/phpBB2/viewtopic.php?t=20193

Image
Image
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Instancing

Post by hybrid »

}{ermanssoN wrote: Im not asking how to hardware instance just how to render the same node serveral times on the same frame
No, he just wants to know how to render several times. Try the VBOs from SVN trunk in order to get a better render performance.
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

Hardware skinning and instancing are both in the works for Irrlicht, they're pretty easy to implement except there are some gotchas concerning ATI and GLSL, they don't conform well to uniform arrays.

Remember this?
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
Post Reply