[osg-users] Hiding and Showing a Node with Key Press

637 views
Skip to first unread message

Ross Lance

unread,
Feb 18, 2011, 1:45:36 PM2/18/11
to osg-...@lists.openscenegraph.org
All,
 
I am trying to setup a keyboard event to toggle a particular node on and off. I am not sure if I should use osg::Switch node or node masks? Any suggestions or examples you could point me to?
 
Thanks,
Ross

Ralf Stokholm

unread,
Feb 18, 2011, 1:51:29 PM2/18/11
to OpenSceneGraph Users
node()->setNodeMask(visible ? ~0 : 0);

should do it

_______________________________________________
osg-users mailing list
osg-...@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org




--
Ralf Stokholm
Director R&D
Email: a...@arenalogic.com
Phone: +45 28 30 83 52
Web: www.arenalogic.com

This transmission and any accompanying documents are intended only for confidential use of the designated recipient(s) identified above.  This message contains proprietary information belonging to Arenalogic Aps.  If you have received this message by error, please notify the sender.

Judson Weissert

unread,
Feb 18, 2011, 2:01:59 PM2/18/11
to osg-...@lists.openscenegraph.org
If you associate a node mask to each type of node that may need to be
hidden using setNodeMask(), you can then modify the cull mask of the
camera to show/hide the nodes accordingly.

osg::Camera::setCullMask (mask) - would be called when the keyboard
button is pressed.
osg::Node::setNodeMask (mask) - would be called when creating the
original scene graph.

If you use osg::SwitchNode, it would be a slightly different process,
but similar.

- Judson

Ross Lance

unread,
Feb 19, 2011, 2:47:40 PM2/19/11
to OpenSceneGraph Users
Thanks that did the trick. Just for other's reference this is what worked well for me using setNodeMask():
 
// Keyboard Event Handler to Show/Hide Node
class HideQuadKeyboardEventHandler : public osgGA::GUIEventHandler
{
    public:
        HideQuadKeyboardEventHandler(osg::Node* node) { _node = node; visible = true; }
        virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&);
        virtual void accept(osgGA::GUIEventHandlerVisitor& v)   { v.visit(*this); };
    private:
        bool visible;
        osg::Node* _node;
};
 
bool HideQuadKeyboardEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
{
    switch(ea.getEventType())
    {
        case(osgGA::GUIEventAdapter::KEYDOWN):
        {
            switch(ea.getKey())
            {
                // Toggle Node on and Off
                case 'x':
                {
                    if(_node)
                    {
                        visible = !visible;
                        _node->setNodeMask(visible ? 0xffffffff : 0x0);
                    }
                    return false;
                }
                break;
                default:
                    return false;
            }
        }
        default:
        return false;
   }
}
Reply all
Reply to author
Forward
0 new messages