Re: [osg-users] Update node color on demand

2018-11-24 Thread Eran Cohen
Hi Diego,

You can pass user events to the viewer (and thus to its Event Handlers):

Code:

// This struct will be passed to the event handler with the relevant parameters 
(for example, the node you want to affect and the color to change it to)
struct ChangeColorEvent : public osg::Referenced
{
  ChangeColorEvent(float r, float g, float b, osg::Node* node)
  {
this->r = r;
this->g = g;
this->b = b;
this->node = node;
  }

  float r;
  float g;
  float b;
  osg::Node* node;
}

// When you want to call the event (on a button click in QT for example)
viewer->getEventQueue()->userEvent(new ChangeColorEvent{ 1.0, 0.3, 0.4, node }, 
0);




To handle said event in your EventHandler:

Code:

class ColorHandler : public osgGA::GUIEventHandler 
{ 
   virtual bool handle(const osgGA::GUIEventAdapter& ea, 
osgGA::GUIActionAdapter& aa) override 
   { 
  if (ea.getEventType() == ea.USER) 
  { 
 auto changeColorEvent = dynamic_cast(ea.getUserData());
 if (changeColorEvent != nullptr)
 {
   // do whatever you want here, for example run the visitor on the node
   return true;
 }
  } 
  return false;
   } 
}; 





Cheers,
Eran

--
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=75241#75241





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


Re: [osg-users] Update node color on demand

2018-11-24 Thread Diego Mancilla
Hello Eran,

 Thank you very much for your answer. I should have been more explicit, due to 
the fact than I'm a newbie on OSG (and 3D development).

 I have an OSG viewer embedded on a Qt5 application. So the idea is that the 
user can change the color of one node (some dxf lines) on demand though the GUI 
(some dialog, pushing buttons, etc). So, bottom line... at some point at 
runtime I have a fresh new color (rgb, for instance) and I need to pass it to 
the viewer towards the "_lines" node. As I previuosly stated I'm new to OSG and 
I'm just digesting the scene graph scheme.

 So, how can I pass this information from the Qt5 environment, to the to the 
node using your suggestions?


Thank you in advance!

Cheers,

--
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=75240#75240





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


Re: [osg-users] Update node color on demand

2018-11-24 Thread Eran Cohen
Hi,
To respond to user events you can either inherit from osg::Callback and install 
it on your node as an EventCallback:


Code:

class ColorCallback : public osg::Callback
{
public:
virtual bool run(osg::Object* object, osg::Object* data) override
{
auto nv = dynamic_cast(data);
if (nv != nullptr && nv->getVisitorType() == nv->EVENT_VISITOR)
{
auto events = nv->asEventVisitor()->getEvents();
for (auto event : events)
{
// handle events
}
}

return traverse(object, data);
}
};

_lines->addEventCallback(new ColorCallback);




or use a global EventHandler and install it on your Viewer:

Code:

class ColorHandler : public osgGA::GUIEventHandler
{
virtual bool handle(const osgGA::GUIEventAdapter& ea, 
osgGA::GUIActionAdapter& aa) override
{
if (ea.getEventType() == ea.KEYDOWN)
{
// handle event 
}
}
};


viewer->addEventHandler(new ColorHandler);




Cheers,
Eran

--
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=75239#75239





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


[osg-users] Update node color on demand

2018-11-24 Thread Diego Mancilla
Hello,

I'm trying to change a node color on demand from my application. The idea is 
that the user, once the initial rendering took place can change the color of a 
node by pressing a key (or something similar). I know already hoy to change the 
color using a NodeVisitor (previous to the rendering). 


Code:
ColorVisitor newColor;
newColor.setColor(r, g, b);
_lines->accept(newColor);



Where _lines is a Node reference pointer and ColorVisitor is a subclass of 
NodeVisitor (http://forum.openscenegraph.org/viewtopic.php?p=75209#75209).

I tried to create a callback and attach it to the node but the callback get 
called every time and I cant pass to it the selected color on runtime.

Can anyone give some pointer about this issue?

Thank you!

Cheers,
[/url]

--
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=75238#75238





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