[osg-users] different materials for a geometry and highlight
Sebastian Messerschmidt
sebastian.messerschmidt at gmx.de
Mon Sep 26 07:03:12 PDT 2016
Hi Gianni,
A very simplistic solution using a outline triangle:
<code>
#include <osg/Geometry>
#include <osg/Geode>
#include <osg/MatrixTransform>
#include <osg/PolygonOffset>
#include <osgViewer/CompositeViewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/MultiTouchTrackballManipulator>
#include <osg/PolygonMode>
#include <osg/LineWidth>
#include <osgDB/ReadFile>
//#include <osgQt/GraphicsWindowQt>
#include <iostream>
const osg::Vec4 selectedColor(1.0f, 1.0f, 1.0f, 0.5f);
const osg::Vec4 color1(1.0f, 0.0f, 0.0f, 1.0f);
const osg::Vec4 color2(0.0f, 1.0f, 0.0f, 1.0f);
const osg::Vec4 color3(0.0f, 0.0f, 1.0f, 1.0f);
const osg::Vec4 color4(1.0f, 0.0f, 1.0f, 1.0f);
class SelectModelHandler : public osgGA::GUIEventHandler
{
public:
SelectModelHandler(osg::ref_ptr<osg::Group> group)
: _selector(0), currentPrimitiveSetIndex(0), _root(group)
{}
virtual bool 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)
{
osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa);
if (viewer)
{
osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new
osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, ea.getX(),
ea.getY());
osgUtil::IntersectionVisitor iv(intersector.get());
osg::Camera* camera = viewer->getCamera();
camera->accept(iv);
if (intersector->containsIntersections())
{
osgUtil::LineSegmentIntersector::Intersection
result = *(intersector->getIntersections().begin());
doUserOperations(result);
}
}
}
return false;
}
virtual void
doUserOperations(osgUtil::LineSegmentIntersector::Intersection& result)
{
osg::Geometry* geom =
dynamic_cast<osg::Geometry*>(result.drawable.get());
//first solution: add some outline
if (_root->getNumChildren())
{
_root->removeChildren(0, _root->getNumChildren());
}
else if (result.indexList.size() > 2)
{
osg::DrawElementsUInt* elements = new
osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES);
osg::Vec3Array* vertices = new osg::Vec3Array;
osg::Vec4Array* colors = new osg::Vec4Array;
colors->push_back(osg::Vec4(1, 0, 0, 1));
const osg::Vec3Array& org_vertices = dynamic_cast<const
osg::Vec3Array&>(*geom->getVertexArray());
vertices->push_back(org_vertices[result.indexList[0]]);
vertices->push_back(org_vertices[result.indexList[1]]);
vertices->push_back(org_vertices[result.indexList[2]]);
(*elements).push_back(0);
(*elements).push_back(1);
(*elements).push_back(2);
osg::Geode* geode = new osg::Geode;
osg::Geometry* geometry = new osg::Geometry;
geode->addDrawable(geometry);
geometry->setVertexArray(vertices);
geometry->setColorArray(colors, osg::Array::BIND_OVERALL);
geometry->addPrimitiveSet(elements);
geometry->getOrCreateStateSet()->setAttribute(new osg::PolygonMode(
osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::LINE));
geometry->getOrCreateStateSet()->setAttribute(new osg::LineWidth(3.0));
_root->addChild(geode);
}
}
protected:
osg::ref_ptr<osg::Geometry> _selector;
unsigned int currentPrimitiveSetIndex;
osg::ref_ptr<osg::Group> _root;
};
osg::Vec3Array* buildVertices() {
osg::Vec3Array* vertices = new osg::Vec3Array;
vertices->push_back(osg::Vec3(0, 0, 0));
vertices->push_back(osg::Vec3(10, 0, 0));
vertices->push_back(osg::Vec3(10, 10, 0));
vertices->push_back(osg::Vec3(0, 10, 0));
vertices->push_back(osg::Vec3(20, 0, 0));
vertices->push_back(osg::Vec3(20, 10, 0));
vertices->push_back(osg::Vec3(20, 20, 0));
vertices->push_back(osg::Vec3(10, 20, 0));
vertices->push_back(osg::Vec3(0, 20, 0));
return vertices;
}
osg::DrawElementsUInt* buildElements()
{
osg::DrawElementsUInt* element = new
osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES);
element->push_back(0);
element->push_back(1);
element->push_back(2);
element->push_back(0);
element->push_back(2);
element->push_back(3);
//////////////////////////////////////////////////////////////////////////
element->push_back(1);
element->push_back(4);
element->push_back(5);
element->push_back(1);
element->push_back(5);
element->push_back(2);
//////////////////////////////////////////////////////////////////////////
element->push_back(2);
element->push_back(5);
element->push_back(6);
element->push_back(2);
element->push_back(6);
element->push_back(7);
//////////////////////////////////////////////////////////////////////////
element->push_back(3);
element->push_back(2);
element->push_back(7);
element->push_back(3);
element->push_back(7);
element->push_back(8);
return element;
}
osg::Vec4Array* buildColors() {
osg::Vec4Array* colors = new osg::Vec4Array(24);
std::fill(std::begin(*colors), std::end(*colors),
osg::Vec4f(0.5,0.5,0.5,1.0));
return colors;
}
osg::Geometry* buildGeometry() {
osg::Geometry* geometry = new osg::Geometry;
geometry->setDataVariance(osg::Object::DYNAMIC);
geometry->setVertexArray(buildVertices());
geometry->setColorArray(buildColors(), osg::Array::BIND_PER_VERTEX);
geometry->addPrimitiveSet(buildElements());
return geometry;
}
osg::Node* createScene() {
osg::Geode* geode = new osg::Geode;
geode->addDrawable(buildGeometry());
return geode;
}
int main(int argc, char** argv)
{
osg::ArgumentParser arguments(&argc, argv);
osgViewer::Viewer viewer(arguments);
viewer.setUpViewInWindow(0, 0, 1000, 1000, 1);
osg::ref_ptr<osg::Group> root = new osg::Group;
osg::ref_ptr<osg::Group> selection_root = new osg::Group;
root->addChild(createScene());
root->addChild(selection_root);
osg::ref_ptr<SelectModelHandler> selector = new
SelectModelHandler(selection_root);
viewer.setSceneData(root);
viewer.addEventHandler(selector.get());
viewer.setCameraManipulator(new osgGA::TrackballManipulator);
// add the window size toggle handler
viewer.addEventHandler(new osgViewer::WindowSizeHandler);
viewer.run();
}
</code>
Am 9/26/2016 um 3:08 PM schrieb Gianni Ambrosio:
> One question,
> why should I use a "vertex" shader/attribute when I need to colour a triangle uniformly?
>
> Regards,
> Gianni
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=68753#68753
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
More information about the osg-users
mailing list