1 | #include "distortionNG.h" |
---|
2 | |
---|
3 | #include<osg/Point> |
---|
4 | |
---|
5 | |
---|
6 | #include<osgViewer/Viewer> |
---|
7 | |
---|
8 | |
---|
9 | |
---|
10 | distortionNG::distortionNG() |
---|
11 | { |
---|
12 | } |
---|
13 | |
---|
14 | distortionNG::~distortionNG() |
---|
15 | { |
---|
16 | } |
---|
17 | |
---|
18 | |
---|
19 | |
---|
20 | osg::Geode* distortionHandler::createVertexHighlighter() |
---|
21 | { |
---|
22 | osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array(1); |
---|
23 | (*colors)[0] = _highlightColor; |
---|
24 | |
---|
25 | _highlighter = new osg::Geometry; |
---|
26 | _highlighter->setDataVariance( osg::Object::DYNAMIC ); |
---|
27 | _highlighter->setUseDisplayList( false ); |
---|
28 | _highlighter->setUseVertexBufferObjects( true ); |
---|
29 | _highlighter->setVertexArray( new osg::Vec3Array(1) ); |
---|
30 | _highlighter->setColorArray( colors.get() ); |
---|
31 | _highlighter->setColorBinding( osg::Geometry::BIND_OVERALL ); |
---|
32 | _highlighter->addPrimitiveSet( new osg::DrawArrays(GL_POINTS, 0, 1) ); |
---|
33 | |
---|
34 | osg::ref_ptr<osg::Geode> geode = new osg::Geode; |
---|
35 | geode->addDrawable( _highlighter.get() ); |
---|
36 | geode->getOrCreateStateSet()->setAttributeAndModes( new osg::Point(8.0f) ); |
---|
37 | geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF ); |
---|
38 | |
---|
39 | return geode.release(); |
---|
40 | } |
---|
41 | |
---|
42 | bool distortionHandler::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa ) |
---|
43 | { |
---|
44 | // Only pressing CTRL activates distortion editing |
---|
45 | if ( !(ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_CTRL) ) |
---|
46 | return false; // False means not handled. |
---|
47 | |
---|
48 | |
---|
49 | if ( ea.getEventType() == osgGA::GUIEventAdapter::PUSH && ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON ) |
---|
50 | { |
---|
51 | osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa); |
---|
52 | if ( viewer ) |
---|
53 | { |
---|
54 | osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, ea.getX(), ea.getY()); |
---|
55 | osgUtil::IntersectionVisitor iv( intersector.get() ); |
---|
56 | viewer->getCamera()->accept( iv ); |
---|
57 | |
---|
58 | if ( intersector->containsIntersections() ) |
---|
59 | { |
---|
60 | osgUtil::LineSegmentIntersector::Intersection& result = *(intersector->getIntersections().begin()); |
---|
61 | computeSelectedVertex( result ); |
---|
62 | } |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | if ( ea.getEventType() == osgGA::GUIEventAdapter::DRAG ) |
---|
67 | { |
---|
68 | osg::notify(osg::ALWAYS)<<std::endl<<"ea.getGraphicsContext()="<<ea.getGraphicsContext()<<std::endl; |
---|
69 | osg::notify(osg::ALWAYS)<<"ea.getXnormalized()="<<ea.getXnormalized()<<std::endl; |
---|
70 | osg::notify(osg::ALWAYS)<<"ea.getYnormalized()="<<ea.getYnormalized()<<std::endl; |
---|
71 | osg::notify(osg::ALWAYS)<<"ea.getX()="<<ea.getX()<<std::endl; |
---|
72 | osg::notify(osg::ALWAYS)<<"ea.getXin()="<<ea.getXmin()<<std::endl; |
---|
73 | osg::notify(osg::ALWAYS)<<"ea.getXmax()="<<ea.getXmax()<<std::endl; |
---|
74 | osg::notify(osg::ALWAYS)<<"ea.getY()="<<ea.getY()<<std::endl; |
---|
75 | osg::notify(osg::ALWAYS)<<"ea.getYin()="<<ea.getYmin()<<std::endl; |
---|
76 | osg::notify(osg::ALWAYS)<<"ea.getYmax()="<<ea.getYmax()<<std::endl; |
---|
77 | |
---|
78 | return true; // true means event handeld: not forwarded to the camera manipulator; |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | return false; // False means not handled. |
---|
83 | } |
---|
84 | |
---|
85 | void distortionHandler::computeSelectedVertex( osgUtil::LineSegmentIntersector::Intersection& result ) |
---|
86 | { |
---|
87 | osg::Geometry* geom = dynamic_cast<osg::Geometry*>( result.drawable.get() ); |
---|
88 | if ( !geom || !_highlighter || geom==_highlighter ) |
---|
89 | return; |
---|
90 | |
---|
91 | osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>( geom->getVertexArray() ); |
---|
92 | osg::Vec3Array* selVertices = dynamic_cast<osg::Vec3Array*>( _highlighter->getVertexArray() ); |
---|
93 | if ( !vertices || !selVertices ) |
---|
94 | return; |
---|
95 | |
---|
96 | osg::Vec3 point = result.getWorldIntersectPoint(); |
---|
97 | osg::Matrix matrix = osg::computeLocalToWorld( result.nodePath ); // To compute the intersection vertices in world coordinates not in model coordinates |
---|
98 | //std::cout << "Intersection-indices: Size=" << result.indexList.size() << std::endl; |
---|
99 | const std::vector<unsigned int>& selIndices = result.indexList; |
---|
100 | { |
---|
101 | double maxRatio = 0.0; |
---|
102 | int closestVertexIndex = 0; |
---|
103 | for ( unsigned int i=0; i<3 && i<result.ratioList.size(); i++ ) //iterate through rations and search for max |
---|
104 | { |
---|
105 | if(result.ratioList[i] > maxRatio) |
---|
106 | { |
---|
107 | maxRatio = result.ratioList[i]; |
---|
108 | closestVertexIndex = result.indexList[i]; |
---|
109 | } |
---|
110 | } |
---|
111 | osg::Vec3 vertex = (*vertices)[closestVertexIndex] * matrix; |
---|
112 | selVertices->front() = vertex; |
---|
113 | } |
---|
114 | |
---|
115 | selVertices->dirty(); |
---|
116 | _highlighter->dirtyBound(); |
---|
117 | } |
---|