Discussion:
Node to Drawable?
Jacob Huckaby
2008-04-11 17:10:30 UTC
Permalink
Hi,

Quick question (at least I hope it is.) I am trying to find the Drawable
inside a Node that I read from a file, to which I can apply a custom
TriangleIntersector. I have tried casting the Node as a Geode, and using
getDrawable(), but it crashes on the getDrawable(). Here is the code I am
trying:

osg::Geode* geode = dynamic_cast<osg::Geode*> (node);
osg::Drawable* drawable = geode->getDrawable(0);
drawable->accept(ti);

I have been able to make this work with Shapes that I have put into
ShapeDrawables:

osg::Box* box = new osg::Box(osg::Vec3(1.0f,1.0f,1.0f),1.0f,1.0f,1.0f);
osg::ShapeDrawable* drawable = new osg::ShapeDrawable(box);
drawable->accept(ti);

My question: how can I get access to the Drawable in my Node (so that I can
apply my TriangleIntersector)? It is only a single node. If you have any
advice, I would greatly appreciate it. Thank you,

Jake
Robert Osfield
2008-04-11 17:53:48 UTC
Permalink
Hi Jacob,

Also use a conditional after a dynamic_cast<> otherwise you may well be
operating on a NULL pointer as this is what C++ will pass back to you when
the object isn't of the type you want.

To find the node you want it easiest to just write a custom NodeVisitor that
searches through the scene graph and at each Goede it encounters do what you
need to do.

Do a search through the examples to find places where NodeVisitor is
subclassed. Look for ones that have an apply(osg::Geode& ) as this will be
very close to what you need.

Robert
Post by Jacob Huckaby
Hi,
Quick question (at least I hope it is.) I am trying to find the Drawable
inside a Node that I read from a file, to which I can apply a custom
TriangleIntersector. I have tried casting the Node as a Geode, and using
getDrawable(), but it crashes on the getDrawable(). Here is the code I am
osg::Geode* geode = dynamic_cast<osg::Geode*> (node);
osg::Drawable* drawable = geode->getDrawable(0);
drawable->accept(ti);
I have been able to make this work with Shapes that I have put into
osg::Box* box = new
osg::Box(osg::Vec3(1.0f,1.0f,1.0f),1.0f,1.0f,1.0f);
osg::ShapeDrawable* drawable = new osg::ShapeDrawable(box);
drawable->accept(ti);
My question: how can I get access to the Drawable in my Node (so that I
can apply my TriangleIntersector)? It is only a single node. If you have any
advice, I would greatly appreciate it. Thank you,
Jake
_______________________________________________
osg-users mailing list
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Paul Martz
2008-04-11 17:58:29 UTC
Permalink
If your Node really is a Geode, then the dynamic_cast should succeed and
you should get a non-NULL Geode pointer. You say the getDrawable call
crashes; to me that sounds like your Geode pointer is NULL but of course you
can easily verify that with a debugger. From your code, the only way this
could happen is if your Node is not actually a Geode.

You might consider using a NodeVisitor and overriding the apply(Geode&)
method. If you did this, you would at least be guaranteed that you are
dealing with Geodes; detecting the "right" Geode would be up to your code
(check the name or some other criteria).

Hope that helps,

Paul Martz
Skew Matrix Software LLC
http://www.skew-matrix.com <http://www.skew-matrix.com/>
+1 303 859 9466




_____

From: osg-users-bounces-***@public.gmane.org
[mailto:osg-users-bounces-***@public.gmane.org] On Behalf Of Jacob
Huckaby
Sent: Friday, April 11, 2008 11:11 AM
To: osg-users-***@public.gmane.org
Subject: [osg-users] Node to Drawable?


Hi,

Quick question (at least I hope it is.) I am trying to find the Drawable
inside a Node that I read from a file, to which I can apply a custom
TriangleIntersector. I have tried casting the Node as a Geode, and using
getDrawable(), but it crashes on the getDrawable(). Here is the code I am
trying:

osg::Geode* geode = dynamic_cast<osg::Geode*> (node);
osg::Drawable* drawable = geode->getDrawable(0);
drawable->accept(ti);

I have been able to make this work with Shapes that I have put into
ShapeDrawables:

osg::Box* box = new osg::Box(osg::Vec3(1.0f,1.0f,1.0f),1.0f,1.0f,1.0f);
osg::ShapeDrawable* drawable = new osg::ShapeDrawable(box);
drawable->accept(ti);

My question: how can I get access to the Drawable in my Node (so that I can
apply my TriangleIntersector)? It is only a single node. If you have any
advice, I would greatly appreciate it. Thank you,

Jake
Per Rosengren
2008-04-15 11:37:37 UTC
Permalink
If it's not a Geode, it's probably a Group. Look for the Geode among your Node's
children. This approach may be easier if you always load the same model.
Post by Jacob Huckaby
Hi,
Quick question (at least I hope it is.) I am trying to find the Drawable
inside a Node that I read from a file, to which I can apply a custom
TriangleIntersector. I have tried casting the Node as a Geode, and using
getDrawable(), but it crashes on the getDrawable(). Here is the code I
osg::Geode* geode = dynamic_cast<osg::Geode*> (node);
osg::Drawable* drawable = geode->getDrawable(0);
drawable->accept(ti);
I have been able to make this work with Shapes that I have put into
osg::Box* box = new osg::Box(osg::Vec3(1.0f,1.0f,1.0f),1.0f,1.0f,1.0f);
osg::ShapeDrawable* drawable = new osg::ShapeDrawable(box);
drawable->accept(ti);
My question: how can I get access to the Drawable in my Node (so that I
can apply my TriangleIntersector)? It is only a single node. If you have
any advice, I would greatly appreciate it. Thank you,
Jake
------------------------------------------------------------------------
_______________________________________________
osg-users mailing list
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Loading...