0

I have got a complex code that is fully loaded with references to ISceneNode objects. I would like to enable shadows for these. However, the function that let us enable shadows is addShadowVolumeSceneNode(), which is only available for the class IMeshSceneNode.

My question is, how do I convert a ISceneNode into a IMeshSceneNode in order to apply shadows to it?

ps: I know it is not possible to apply shadows to a ISceneNode: http://irrlicht.sourceforge.net/forum/viewtopic.php?t=42174

PintoDoido
  • 1,011
  • 16
  • 35

1 Answers1

2

You can cast an ISceneNode pointer to an IMeshSceneNode pointer, if it actually points to an IMeshSceneNode object:

void AddShadowToSceneNodeIfPossible(ISceneNode* node)
{
    IMeshSceneNode* meshNode = dynamic_cast<IMeshSceneNode*>(node);
    if (meshNode)
    {
        meshNode->addShadowVolumeSceneNode(...);
    }
}

But the better solution would be to store IMeshSceneNode pointers as IMeshSceneNode pointers from the start.

Max Vollmer
  • 8,412
  • 9
  • 28
  • 43
  • Just one more question: under which condition **dynamic_cast(node)** fails? In my code (i mentioned above in my question) it fails the casting process ( i know that because I added an else {cout<<"hi!"} to your code snippet) – PintoDoido Aug 31 '18 at 16:03
  • 1
    @PintoDoido It returns `nullptr` for any `ISceneNode*` that is not an `IMeshSceneNode*`. Not every `ISceneNode*` is an `IMeshSceneNode*`, there's `IParticleSystemSceneNode*` or `ILightSceneNode*` or also `IShadowVolumeSceneNode*` which coincidentally gets created in `addShadowVolumeSceneNode`. And many other types more. – Max Vollmer Aug 31 '18 at 16:09
  • 1
    As I wrote it would be best to store pointers to the `IMeshSceneNode`s you create as `IMeshSceneNode*`, not as `ISceneNode*`, then you don't need any casting later. – Max Vollmer Aug 31 '18 at 16:11
  • @PintoDoido By the way, not all ISceneNode are IMeshSceneNode. You could very well have a scene node that doesn't render anything, so that makes sense that you can't apply shadow to a scene node unless it's actually rendering something. – MartinVeronneau Sep 04 '18 at 12:29