[osg-users] Converting a Drawable object to a Geometry object

Ulrich Hertlein u.hertlein at sandbox.de
Wed Jul 30 17:24:46 PDT 2008

Franclin Foping wrote:
>  I would like to turn a Drawable object into a Geometry counterpart in 
> order to retrieve its texture coordinates.
>  I have tried to use both the asGeometry method of the Drawable class 
> and the dynamic cast operator but both attempts ended up delivering a 
> NULL pointer. I am not sure if there is another way of doing it.
> Here is a code snippet.
> 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);
> osg::Geometry* myGeom = myDrawable->asGeometry(); //This returns NULL!
> osg::Geometry*  myGeom2 = dynamic_cast<osg::Geometry *>(myDrawable);
> Unfortunately both  myGeom and myGeom3 are both NULL at this point.

Sorry, but what you're trying to do is impossible, the class hierarchy isn't 
setup that way.

What you have is:
- ShapeDrawable is-a Drawable, and
- Geometry is-a Drawable

and what you're attempting is to cast a ShapeDrawable to a Geometry.  This can 
never work since they don't have a parent-child relationship.  The fact that 
they're both derived from Drawable is irrelevant for what you're attempting.


More information about the osg-users mailing list