[osg-users] RE : Re: RE : Re: Converting a Drawable object to a Geometry object

Fuesz, Matthew matthew.fuesz at lmco.com
Thu Jul 31 05:10:06 PDT 2008

The problem is that you are trying to cast a pointer to a base class to a pointer to a subclass. This would only work if the actual object being pointed to is an instance of that subclass (or a further subclass of that subclass). Geometry is derived from Drawable, not the other way around - which seems to be the mistake that you are making.


Please refer to the inheritance diagrams for osg::Drawable and osg::Geometry.


Matthew W. Fuesz

Software Engineer Asc.

Lockheed Martin STS

1210 Massillon Road

Akron, OH 44315

matthew.fuesz at lmco.com



From: osg-users-bounces at lists.openscenegraph.org [mailto:osg-users-bounces at lists.openscenegraph.org] On Behalf Of Franclin Foping
Sent: Thursday, July 31, 2008 7:17 AM
To: OpenSceneGraph Users
Subject: [osg-users] RE : Re: RE : Re: Converting a Drawable object to a Geometry object


Hi Ulrich,
 I don't think you get the trick, from what you said, the only thing wrong is the ShapeDrawable. 
 I will post a new issue in a short while which is not different from this post and you will see that even without using ShapeDrawable objects, the same issue still hang around.
 Just bear with me.
 Thanks for your help.

Ulrich Hertlein <u.hertlein at sandbox.de> a écrit :

Hi Franclin,

> Downcasting from a derived class to a parent class is possible with the 
> dynamic_cast operator. From what you have written in your mail, it looks like
> the snippet won't compile at all, I can bet something it will as I tried it a
> couple of hours ago. Try it yourself and let me know.

The code is correct and will compile, it's just that the casts won't work as you 

Okay, let's see:
>> osg::Geode myGeode = new osg::Geode;
>> myGeode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(),1,1,1)));
>> osg::Drawable* myDrawable = myGeode->getDrawable(0);

myDrawable is a Drawable* to a ShapeDrawable
This is fine since Drawable is a base class of ShapeDrawable.
(I assume there aren't any other Drawables in myGeode and the first one is 
actually the ShapeDrawable you added.)

> osg::Geometry* myGeom = myDrawable->asGeometry();

If you take a look at 'osg::Drawable' you'll see that this actually *always* 
returns 0 and ShapeDrawable doesn't override it (Geometry is the only class that 
overrides it).

However, even doing what the comment says (and what you're doing for 'myGeom2'):

osg::Geometry* geom = dynamic_cast(this)

This cannot succeed since the *object* is-a ShapeDrawable (even though the 
pointer is Drawable*).

> Of course, downcasting from shapedrawable to geometry would have not worked
> and even worse would have not compiled at all.

Agreed. But introducing a Drawable* doesn't help because it doesn't change the 
type of the object the pointer is pointing to.

ShapeDrawable* shape = new ShapeDrawable(...)
Drawable* drawable = dynamic_cast(shape); // OK, is a base class
Geometry* geom = dynamic_cast(drawable); // is always 0

I'm probably not explaining this as clearly as possible but try to think of it 
this way: you have a base class 'Vehicle' and two derived classes 'Car' and 
'Plane'. You can create a 'Plane' and have a 'Vehicle' pointer to it but you 
can't cast that pointer to a 'Car'.

Hope this helps.


osg-users mailing list
osg-users at lists.openscenegraph.org




Envoyé avec Yahoo! Mail <http://us.rd.yahoo.com/mailuk/taglines/isp/control/*http:/us.rd.yahoo.com/evt=52423/*http:/fr.docs.yahoo.com/mail/overview/index.html> .
Une boite mail plus intelligente. 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20080731/fb94d0b3/attachment-0003.htm>

More information about the osg-users mailing list