Hi all!
I’m new in OpenSceneGraph. I have big models exported from 3dmax and materials
from VRay (about 50 000 objects).
I want highlight object on scene when clicking on it. Example with showing
bounding box work for me ok but I need
to highlight object with, for example, green color and transparency.
I modify handle function as:
bool PickHandler::handle( const osgGA::GUIEventAdapter& ea,
osgGA::GUIActionAdapter& aa )
{
if ( ea.getEventType()!=osgGA::GUIEventAdapter::RELEASE ||
ea.getButton()!=osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON ||
!(ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_CTRL) )
return false;
osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa);
if (viewer)
{
osg::ref_ptr<osgUtil::LineSegmentIntersector> ray =
new osgUtil::LineSegmentIntersector(
osgUtil::Intersector::WINDOW, ea.getX(), ea.getY()
);
osgUtil::IntersectionVisitor iv(ray.get());
iv.setTraversalMask( ~0x1 );
viewer->getCamera()->accept(iv);
if (ray->containsIntersections())
{
osgUtil::LineSegmentIntersector::Intersection resInt =
*(ray->getIntersections().begin());
osg::BoundingBox bb = resInt.drawable->getBound();
osg::Vec3 worldCenter = bb.center() *
osg::computeLocalToWorld(resInt.nodePath);
m_selectionBox->setMatrix(
osg::Matrix::scale(bb.xMax()-bb.xMin(),
bb.yMax()-bb.yMin(),
bb.zMax()-bb.zMin()) *
osg::Matrix::translate(worldCenter) );
///////////////////////////////////////
if (m_SelectedNode){
osg::Material* rev_material = new osg::Material;
osg::StateSet* rev_state =
m_SelectedNode->getOrCreateStateSet();
rev_state->setMode(GL_BLEND,osg::StateAttribute::ON |
osg::StateAttribute::OVERRIDE);
rev_material->setAlpha(osg::Material::FRONT_AND_BACK, 1);
rev_state->setAttributeAndModes(rev_material,osg::StateAttribute::ON |
osg::StateAttribute::OVERRIDE);
osg::BlendFunc* bf = new osg::BlendFunc(
osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA );
rev_state->setAttributeAndModes(bf);
m_SelectedNode->setStateSet(rev_state);
m_SelectedNode=NULL;
}
osg::NodePath& nodePath = resInt.nodePath;
osg::Node* node =
(nodePath.size()>=1)?nodePath[nodePath.size()-1]:0;
osg::Group* parent =
(nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0;
if (node) {
m_SelectedNode = node;
osg::Material* material = new osg::Material;
osg::StateSet* state = node->getOrCreateStateSet();
state->setMode(GL_BLEND,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
material->setAlpha(osg::Material::FRONT_AND_BACK, 0.6);
state->setAttribute(material,osg::StateAttribute::ON |
osg::StateAttribute::OVERRIDE);
osg::BlendFunc* bf = new osg::BlendFunc(
osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA );
state->setAttribute(bf);
state->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
state->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
node->setStateSet(state);
}
///////////////////////////////////////////
}
}
return false;
}
When select object I have transparency on it. But when I select another object
I see that previous object lost it color and become grey.
How I can restore previous state of object material?
Regards,
Alexey.
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org