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.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

yes, yes, we all know you're a genius blindside ;)
Image Image Image
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

Ok I'm not gonna post anymore. :P
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
}{ermanssoN
Posts: 37
Joined: Mon May 26, 2008 7:39 pm

Post by }{ermanssoN »

Well went with haveing an arry of players. each player has a scene node wich i added thesame mesh to, did samething with projectiles. and it worked fine.

Very nice examples and answers, thats the best thing with opensource, there are lots of users that share knowledge, It's nice :)
sio2
Competition winner
Posts: 1003
Joined: Thu Sep 21, 2006 5:33 pm
Location: UK

Post by sio2 »

dlangdev wrote:"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
Pah! They're not even animated! 8)

100 skinned meshes:
Image
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

oh yeah!

don't expect me to believe ya right away...

gotta see the code as there's a dx demo of it in the dx sdk.
Image
Saturn
Posts: 418
Joined: Mon Sep 25, 2006 5:58 pm

Post by Saturn »

sio2 wrote:
dlangdev wrote:"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
Pah! They're not even animated! 8)
The actual crowd demo is animated.
}{ermanssoN
Posts: 37
Joined: Mon May 26, 2008 7:39 pm

Post by }{ermanssoN »

Well, having a lot of nodes seems to be heavy. I used boxnodes as bullets. I started to make an array of 25 boxesfor just one player.


Code: Select all

for( u8 i = 0; i< MAX_NUM_BOXPROJECTILES; i++ )
	{
		m_ppBoxProjectiles[ i ] = new SProjectile();
		m_ppBoxProjectiles[ i ]->Direction = vector3df( 0.0f, 0.0f, 0.0f );
		m_ppBoxProjectiles[ i ]->pMesh = NULL;
		m_ppBoxProjectiles[ i ]->pNode = pScene->addCubeSceneNode( 5.0f );
		m_ppBoxProjectiles[ i ]->Position = vector3df( 0.0f, 0.0f, 0.0f );
		m_ppBoxProjectiles[ i ]->Roration = Tmp;
		m_ppBoxProjectiles[ i ]->Speed = 300.0f;
		m_ppBoxProjectiles[ i ]->TimeToImpact = 0.0f;

		m_ppBoxProjectiles[ i ]->pNode->setVisible( false );
		
	}
This works perfectly, If you only have one player, or even two.
But i figured that the maximum amount of players should be 16, and every player shouls have 25 projectiles, so theoreticly we could hav as much as 400 projectiles in the air.
But initializing 400 boxes was not an good idea. The program won't even start. I managed to create up to 250. but more just makes the program crash. And a CubeSceneNode must be much "simpler" than an IAnimatedMeshSceneNode.

One thiing is strange, 25 nodes or 250 nodes make no difference on the fps. Is there a maximum number of nodes that a scenemanager can handle??
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:But initializing 400 boxes was not an good idea. The program won't even start. I managed to create up to 250. but more just makes the program crash.
There is no hard limit, and a crash generally indicates user error. If you post a test case that reproduces the behaviour, I'm sure that we can find the problem.
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 »

there is no error message, windows just complains that the application don't respond and i end it with the taskmanager.

MAX_NUM_BOXPROJECTILES is set to 25, it starts and exits with out problem.

if BOXPROJECTILES is sett to high, around 300 it's forces a crash.

but i'l post the whole class:

Code: Select all

#ifndef _PROJECTILEMANAGER_CPP
#define _PROJECTILEMANAGER_CPP

#include "globals.h"

ProjectileManager::ProjectileManager( void )
{
	m_ppBoxProjectiles		= NULL;
	m_NumFired				= 0;
}
ProjectileManager::~ProjectileManager( void )
{
	
}

bool
ProjectileManager::InitializeManager( void )
{
	ISceneManager* pScene = g_pApp->GetSceneManager();
	
	m_ppBoxProjectiles = new SProjectile*[ MAX_NUM_BOXPROJECTILES ];
	
	memset( m_ppBoxProjectiles, 0, sizeof(SProjectile**) * MAX_NUM_BOXPROJECTILES );
		
		
	SRotation Tmp;
	Tmp.X = 0.0f;
	Tmp.Y = 0.0f;
	Tmp.Y = 0.0f;

	for( u8 i = 0; i< MAX_NUM_BOXPROJECTILES; i++ )
	{
		m_ppBoxProjectiles[ i ] = new SProjectile();
		m_ppBoxProjectiles[ i ]->Direction = vector3df( 0.0f, 0.0f, 0.0f );
		m_ppBoxProjectiles[ i ]->pMesh = NULL;
		m_ppBoxProjectiles[ i ]->pNode = pScene->addCubeSceneNode( 5.0f );
		m_ppBoxProjectiles[ i ]->Position = vector3df( 0.0f, 0.0f, 0.0f );
		m_ppBoxProjectiles[ i ]->Roration = Tmp;
		m_ppBoxProjectiles[ i ]->Speed = 300.0f;
		m_ppBoxProjectiles[ i ]->TimeToImpact = 0.0f;

		m_ppBoxProjectiles[ i ]->pNode->setVisible( false );
		
	}
	
	SWorld* pWorld = g_pApp->GetWorldManager()->GetActiveWorld();
	m_pTringleSelector = pScene->createOctTreeTriangleSelector( pWorld->pWorldMesh->getMesh( 0 ), pWorld->pWorldNode, 128 );
	return true;
}


bool
ProjectileManager::DestroyManager( void )
{
	if( m_ppBoxProjectiles )
	{
		for( u8 i = 0; i< MAX_NUM_BOXPROJECTILES; i++ )
		{
			if( m_ppBoxProjectiles [ i ] && m_ppBoxProjectiles [ i ]->pNode )
				m_ppBoxProjectiles[ i ]->pNode->remove();
		}
		delete [] m_ppBoxProjectiles;
	}

		return true;
}
void
ProjectileManager::FireProjectile(irr::core::vector3df Direction, irr::core::vector3df StartPosition)
{
		for( u8 i = 0; i< MAX_NUM_BOXPROJECTILES; i++ )
		{
			SProjectile* pProjectile = m_ppBoxProjectiles[ i ];

			if( pProjectile && !pProjectile->pNode->isVisible() )
			{
				MathManager* pMath = g_pApp->GetMathManager();
				ISceneCollisionManager* pCollision = g_pApp->GetSceneManager()->getSceneCollisionManager();
				vector3df TravelVec,Intersection;
				triangle3df Trinagle;

				if( pCollision->getCollisionPoint( pMath->GetLineOfFire( StartPosition, Direction, 2000.0f ), m_pTringleSelector, Intersection, Trinagle ))
					TravelVec = Intersection - StartPosition;
				pProjectile->Direction = Direction;
				pProjectile->Position = StartPosition;
				pProjectile->pNode->setVisible( true );
				pProjectile->TimeToImpact = pMath->CalculateTimeToIntersection( TravelVec.getLength(), pProjectile->Speed );
				pProjectile->pNode->setPosition( StartPosition );
				pProjectile->pNode->updateAbsolutePosition();
				m_NumFired ++;
				g_pApp->GetIrrDevice()->getLogger()->log( "Projectile fired\n" );
				return;
			}
				
		}
}
void
ProjectileManager::Update( void )
{
	f32 DeltaTime = g_pApp->GetTimeManager()->GetDeltaTime();
	for( u8 i = 0; i< MAX_NUM_BOXPROJECTILES; i++ )
	{
		SProjectile* pProjectile = m_ppBoxProjectiles[ i ];

		if( pProjectile && pProjectile->pNode->isVisible() )
		{
			if(	pProjectile->TimeToImpact > 0.0f )
			{
				pProjectile->Position += pProjectile->Direction * ( DeltaTime * pProjectile->Speed );
				pProjectile->pNode->setPosition( pProjectile->Position );
				pProjectile->pNode->updateAbsolutePosition();
				pProjectile->TimeToImpact -= DeltaTime;
			}
			else
			{
				g_pApp->GetIrrDevice()->getLogger()->log( "Prijectile hit\n" );
				pProjectile->pNode->setVisible( false );
				pProjectile->TimeToImpact = 0.0f;
			}
		}

	}
}
SProjectile*
ProjectileManager::GetProjectile( u8 Index )
{
	if( m_ppBoxProjectiles[ Index ] && Index <= MAX_NUM_BOXPROJECTILES )
		return m_ppBoxProjectiles[ Index ];	
	else 
		return NULL;
}
vector3df
ProjectileManager::GetProjectilePosition( u8 Index )
{
	if( m_ppBoxProjectiles[ Index ] && Index <= MAX_NUM_BOXPROJECTILES )
		return m_ppBoxProjectiles[ Index ]->Position;	
	else 
		return vector3df( 0.0f, 0.0f, 0.0f );
}
#endif //_PROJECTILEMANAGER_CPP
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Code: Select all

for( u8 i = 0; i< MAX_NUM_BOXPROJECTILES; i++ )
Image

Sew-crates has a method he'd like me to try out.

What's the maximum value that a u8 can hold?

What happens when you increment a u8 that's already at its maximum value?

Sew-creates thinks that it's important to identify the problem before coming up with a solution.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

You should know that on a 32-bit system using a u8 is infact a tiny bit slower than a u32. :D

(So basically, I have no idea why the hell you did that.)
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
}{ermanssoN
Posts: 37
Joined: Mon May 26, 2008 7:39 pm

Post by }{ermanssoN »

ok foregting to switch the u8 to u32 is kind of embaressing :S

But no I did not know that a u32 is faster than u8.

Why is it?? 32 is bigger than 8??
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

A 32 bit CPU has 32 bit registers.

Moving a 32 bit value to or from a 32 bit register is generally a faster operation than moving an 8 bit value to or from a 32 bit register, as doing so requires padding or truncating it.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Post Reply