[307] | 1 | #include "distortionNG.h" |
---|
| 2 | |
---|
[309] | 3 | #include<osg/Point> |
---|
| 4 | |
---|
[313] | 5 | |
---|
[309] | 6 | #include<osgViewer/Viewer> |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | distortionNG::distortionNG() |
---|
[307] | 11 | { |
---|
| 12 | } |
---|
| 13 | |
---|
[309] | 14 | distortionNG::~distortionNG() |
---|
[307] | 15 | { |
---|
| 16 | } |
---|
[309] | 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 ); |
---|
[329] | 29 | _highlighter->setVertexArray( new osg::Vec3Array(1) ); // The highlighter vertex is updated by computeSelectedVertex(..) |
---|
[309] | 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() ); |
---|
[310] | 36 | geode->getOrCreateStateSet()->setAttributeAndModes( new osg::Point(8.0f) ); |
---|
[309] | 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 | { |
---|
[310] | 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() ); |
---|
[329] | 56 | //viewer->getCamera()->accept( iv ); |
---|
| 57 | _camera->accept( iv ); |
---|
[310] | 58 | |
---|
| 59 | if ( intersector->containsIntersections() ) |
---|
| 60 | { |
---|
| 61 | osgUtil::LineSegmentIntersector::Intersection& result = *(intersector->getIntersections().begin()); |
---|
| 62 | computeSelectedVertex( result ); |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | if ( ea.getEventType() == osgGA::GUIEventAdapter::DRAG ) |
---|
| 68 | { |
---|
| 69 | osg::notify(osg::ALWAYS)<<std::endl<<"ea.getGraphicsContext()="<<ea.getGraphicsContext()<<std::endl; |
---|
| 70 | osg::notify(osg::ALWAYS)<<"ea.getXnormalized()="<<ea.getXnormalized()<<std::endl; |
---|
| 71 | osg::notify(osg::ALWAYS)<<"ea.getYnormalized()="<<ea.getYnormalized()<<std::endl; |
---|
| 72 | osg::notify(osg::ALWAYS)<<"ea.getX()="<<ea.getX()<<std::endl; |
---|
| 73 | osg::notify(osg::ALWAYS)<<"ea.getXin()="<<ea.getXmin()<<std::endl; |
---|
| 74 | osg::notify(osg::ALWAYS)<<"ea.getXmax()="<<ea.getXmax()<<std::endl; |
---|
| 75 | osg::notify(osg::ALWAYS)<<"ea.getY()="<<ea.getY()<<std::endl; |
---|
| 76 | osg::notify(osg::ALWAYS)<<"ea.getYin()="<<ea.getYmin()<<std::endl; |
---|
| 77 | osg::notify(osg::ALWAYS)<<"ea.getYmax()="<<ea.getYmax()<<std::endl; |
---|
| 78 | |
---|
| 79 | return true; // true means event handeld: not forwarded to the camera manipulator; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | return false; // False means not handled. |
---|
[309] | 84 | } |
---|
| 85 | |
---|
| 86 | void distortionHandler::computeSelectedVertex( osgUtil::LineSegmentIntersector::Intersection& result ) |
---|
| 87 | { |
---|
| 88 | osg::Geometry* geom = dynamic_cast<osg::Geometry*>( result.drawable.get() ); |
---|
| 89 | if ( !geom || !_highlighter || geom==_highlighter ) |
---|
| 90 | return; |
---|
| 91 | |
---|
| 92 | osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>( geom->getVertexArray() ); |
---|
| 93 | osg::Vec3Array* selVertices = dynamic_cast<osg::Vec3Array*>( _highlighter->getVertexArray() ); |
---|
| 94 | if ( !vertices || !selVertices ) |
---|
| 95 | return; |
---|
[329] | 96 | |
---|
| 97 | OSG_NOTIFY(osg::ALWAYS)<<"size of vertices="<<vertices->size()<<std::endl; |
---|
| 98 | OSG_NOTIFY(osg::ALWAYS)<<"size of selVertices="<<selVertices->size()<<std::endl; |
---|
[309] | 99 | |
---|
| 100 | osg::Vec3 point = result.getWorldIntersectPoint(); |
---|
| 101 | osg::Matrix matrix = osg::computeLocalToWorld( result.nodePath ); // To compute the intersection vertices in world coordinates not in model coordinates |
---|
[329] | 102 | OSG_NOTIFY(osg::ALWAYS) << "Intersection-indices: Size=" << result.indexList.size() << std::endl; |
---|
[309] | 103 | const std::vector<unsigned int>& selIndices = result.indexList; |
---|
| 104 | { |
---|
| 105 | double maxRatio = 0.0; |
---|
| 106 | int closestVertexIndex = 0; |
---|
[329] | 107 | for ( unsigned int i=0; i<3 && i<result.ratioList.size(); i++ ) //iterate through rations and search for maxRation=nearestVertex |
---|
[309] | 108 | { |
---|
| 109 | if(result.ratioList[i] > maxRatio) |
---|
| 110 | { |
---|
| 111 | maxRatio = result.ratioList[i]; |
---|
| 112 | closestVertexIndex = result.indexList[i]; |
---|
| 113 | } |
---|
[329] | 114 | OSG_NOTIFY(osg::ALWAYS)<<"maxRatio="<<maxRatio<<std::endl; |
---|
| 115 | OSG_NOTIFY(osg::ALWAYS)<<"closestVertexIndex="<<closestVertexIndex<<std::endl; |
---|
[309] | 116 | } |
---|
[329] | 117 | OSG_NOTIFY(osg::ALWAYS)<<"nearest vertex: X="<<(*vertices)[closestVertexIndex].x()<<" Y="<<(*vertices)[closestVertexIndex].y()<<" Z="<<(*vertices)[closestVertexIndex].z()<<std::endl; |
---|
[309] | 118 | osg::Vec3 vertex = (*vertices)[closestVertexIndex] * matrix; |
---|
[329] | 119 | |
---|
| 120 | selVertices->front() = vertex; |
---|
| 121 | OSG_NOTIFY(osg::ALWAYS)<<"selected vertice: X="<<vertex.x()<<" Y="<<vertex.y()<<" Z="<<vertex.z()<<std::endl; |
---|
[309] | 122 | } |
---|
| 123 | selVertices->dirty(); |
---|
| 124 | _highlighter->dirtyBound(); |
---|
[331] | 125 | } |
---|
| 126 | |
---|
| 127 | /* |
---|
| 128 | Grundsätzliche Gedanken Schritte: |
---|
| 129 | Die Verzerrung kann auf zwei Arten realisiert werden |
---|
| 130 | - Verzerrung der Textur-Koordinaten aus der RTT-Szene |
---|
| 131 | - Verzerrung des Meshes im Verzerrungs-Rendern |
---|
[332] | 132 | * |
---|
| 133 | Die IntensityMap kann folgendermaßen angewendet werden: |
---|
| 134 | * Extrahierung der Coloran den Mesh-Nodes und anwendung als Color im Mesh node. Vorteil : Einfach. Nachteil: Helligkeiten werden zwischen Mesh-Nodes interpoliert: nur grobe Intensity Steuerung möglich |
---|
| 135 | * Anwendung der Intensity-Textur pro Pixel: Verwenden eines Sharders um die Frament-Color mit der Farbe der Intensity-color zu multiplizieren |
---|
| 136 | * |
---|
| 137 | -> Evtl. sollte die Funktion der Intensitysteuerung aus den OSG-Distortion-Funktionen ausgeklammert und als eigene OSG-Schnittstelle/Funktion angestrebt werden. |
---|
[331] | 138 | |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | */ |
---|