Page 1 of 1

Mesh from an ISceneNode

Posted: Sat Oct 04, 2008 5:50 pm
by Kapitan
Hi all!!!

I've created my scene on IrrEdit and
loaded with loadscene(), but now i have
to take the mesh of an ISceneNode
(grabbed with getSceneNodeFromId).

Is that possible?

Here:
http://www.irrlicht3d.org/wiki/index.ph ... gCollision

He serialize the node and then
take the name of the mesh.

Is the only way?

Thanks and sorry but i really an
Irrlicht newbie.

Posted: Sun Oct 05, 2008 3:19 am
by Acki
if you have the node, can't you get the mesh with node->getMesh() ?!?!?
(maybe a type cast to IMeshSceneNode needed)

Code: Select all

IMesh* m = ((IMeshSceneNode*)node)->getMesh();

Posted: Sun Oct 05, 2008 6:18 am
by lovloss
Acki wrote:if you have the node, can't you get the mesh with node->getMesh() ?!?!?
(maybe a type cast to IMeshSceneNode needed)

Code: Select all

IMesh* m = ((IMeshSceneNode*)node)->getMesh();
Sometimes :) Sometimes you go node->getMesh()->getMesh(0)... i think?

Posted: Sun Oct 05, 2008 7:02 am
by Halifax
lovloss wrote:
Acki wrote:if you have the node, can't you get the mesh with node->getMesh() ?!?!?
(maybe a type cast to IMeshSceneNode needed)

Code: Select all

IMesh* m = ((IMeshSceneNode*)node)->getMesh();
Sometimes :) Sometimes you go node->getMesh()->getMesh(0)... i think?
I think you are thinking of an animated scene node:

Code: Select all

IMesh* m = ((IAnimatedMeshSceneNode*)node)->getMesh(0);

Posted: Sun Oct 05, 2008 12:21 pm
by rogerborg
First, call ISceneNode::getType(), then cast to the appropriate type based on the result.

Posted: Sun Oct 05, 2008 3:41 pm
by Kapitan
I've tried both (Mesh and AnimatedMesh cast),
but the result is a null pointer...maybe a bug
or my fault....

I'll try again and i'll post some code.

Posted: Mon Oct 06, 2008 3:59 pm
by kornwaretm
read rogerborg's post above
something like :

Code: Select all

IMesh* mesh = 0;
switch(node->getType()){
case(ESNT_ANIMATED_MESH):{
     mesh = ((IAnimatedMeshSceneNode*)node)->getMesh();
}
case(ESNT_MESH):{
     mesh = ((IMeshSceneNode*)node)->getMesh();
}
//etc.............................. other cast
}
if mesh still 0. check console messages. any model missing?

Posted: Wed Oct 08, 2008 5:34 am
by Kapitan
Yes it works, i was using the dynamic_cast
keyword and it told me that there were not
the RTTI.

I've switched using the static_cast keyword
and it works.

Now is:

IMeshNode *msn = static_cast<IMeshNode*>(var)
and perfectly found my mesh.

Posted: Thu Oct 09, 2008 6:32 am
by wuallen
Kapitan wrote:Yes it works, i was using the dynamic_cast
keyword and it told me that there were not
the RTTI.

I've switched using the static_cast keyword
and it works.
I encounted the same problem. I don't know why there is no RTTI.

I use VC2009, is it due to the config of VC2009 or something?

Posted: Thu Oct 09, 2008 7:13 am
by hybrid
Well, you shouldn't simply use a dynamic_cast when all you want to do is going down the inheritance hierarchy. That's what static cast is meant for, in the worst case you'd use a reinterpret_cast. dynamic_cast is for navigating across different inheritance branches. RTTI needs to be activated in the project settings, but remember that there's a major runtime penalty related to dynamic_casts.

Posted: Thu Oct 09, 2008 3:33 pm
by vitek
hybrid wrote:Well, you shouldn't simply use a dynamic_cast when all you want to do is going down the inheritance hierarchy.
Not true. The dynamic_cast<> operator is there to ensure that object is a complete object of the requested type. Given an ISceneNode* you don't know if you can safely cast down the inheritance heirarchy to IMeshSceneNode*.

As mentioned above, you can use getType() to find the actual type before doing a static_cast<>. That would be safe. This is essentially the same as a dynamic_cast<> because of the virtual dispatch that happens.
hybrid wrote:That's what static cast is meant for, in the worst case you'd use a reinterpret_cast.
A reinterpret_cast<> would be dangerous, as it might give wrong results in the face of multiple inheritance.
hybrid wrote:dynamic_cast is for navigating across different inheritance branches. RTTI needs to be activated in the project settings, but remember that there's a major runtime penalty related to dynamic_casts.
Have you ever profiled the use of RTTI? The runtime cost of RTTI depends on the implementation and the inheritance heirarchy. For a simple heirarchy like that used by ISceneNode*, the runtime cost should be minimal.

Travis

Posted: Thu Oct 09, 2008 3:39 pm
by hybrid
I simply rely on the number given in the C++ optimization guides from Agner Fog and the C++ performance report. I've never profiled any apps for these comparisons.

Posted: Thu Oct 09, 2008 5:09 pm
by Kapitan
vitek wrote: As mentioned above, you can use getType() to find the actual type before doing a static_cast<>. That would be safe. This is essentially the same as a dynamic_cast<> because of the virtual dispatch that happens.

A reinterpret_cast<> would be dangerous, as it might give wrong results in the face of multiple inheritance.
I quote this because there's a big difference.
The getType() + static_cast is the same theorically
of the dynamic_cast...i don't know the profiling,
i will test it.

Posted: Thu Oct 09, 2008 5:58 pm
by vitek
Don't bother. I wrote up some code and the results were in-line with what hybrid had posted...

Code: Select all

static_cast<IMeshSceneNode>: 2.37s
static_cast<IAnimatedMeshSceneNode>: 2.29s
dynamic_cast<IMeshSceneNode>: 21.35s
dynamic_cast<IAnimatedMeshSceneNode>: 34.97s
The static_cast<> tests used a call to getType() followed by a static_cast<>, while the dynamic_cast<> tests just did a dynamic_cast<>. Each test was 500,000,000 iterations. I'm open to posting the code if anyone wants to see it.

Travis

Posted: Sat Oct 11, 2008 3:14 am
by wuallen
For choosing static_cast, dynamic_cast or reinterpret_cast, I agree with vitek, and for the performance penalty, I agree hybrid. thanks to every one.