Changeset 353 for experimental
- Timestamp:
- Apr 21, 2012, 9:49:26 PM (13 years ago)
- Location:
- experimental/distortionNG
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
experimental/distortionNG/DistortionManipulator.cpp
r350 r353 20 20 21 21 #include<osgViewer/Viewer> 22 #include<osg/Point> 22 23 23 24 using namespace osg; … … 25 26 26 27 27 DistortionManipulator::DistortionManipulator() 28 DistortionManipulator::DistortionManipulator(DistortionSet* ds) 29 : _highlightColor( osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f) ), _distortionSet( ds ) 28 30 { 29 31 activeSetupMode = DISABLED; … … 32 34 activeVisualizationMode = NONE; 33 35 34 _distortionSet = NULL; 36 _highlighter = NULL; 37 38 createVertexHighlighter(); 39 40 _camera = 0; 41 //_distortionMesh(distortionMesh) 35 42 } 36 43 … … 52 59 bool DistortionManipulator::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa, osg::Object* obj, osg::NodeVisitor* nv) 53 60 { 54 //osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa);55 //if ( viewer && !_distortionSet.isValid())56 //{57 // OSG_ALWAYS<<"Fetching valid DistortionSet!"<<std::endl;58 // _distortionSet = viewer->getDistortionSet();59 //}60 61 61 switch(ea.getEventType()) 62 62 { … … 64 64 case(osgGA::GUIEventAdapter::DRAG): 65 65 { 66 OSG_ALWAYS<<"DRAG!"<<std::endl; 67 break; 66 osg::notify(osg::ALWAYS)<<std::endl<<"Drag:"<<std::endl<<"ea.getGraphicsContext()="<<ea.getGraphicsContext()<<std::endl; 67 osg::notify(osg::ALWAYS)<<"ea.getXnormalized()="<<ea.getXnormalized()<<std::endl; 68 osg::notify(osg::ALWAYS)<<"ea.getYnormalized()="<<ea.getYnormalized()<<std::endl; 69 osg::notify(osg::ALWAYS)<<"ea.getX()="<<ea.getX()<<std::endl; 70 osg::notify(osg::ALWAYS)<<"ea.getXin()="<<ea.getXmin()<<std::endl; 71 osg::notify(osg::ALWAYS)<<"ea.getXmax()="<<ea.getXmax()<<std::endl; 72 osg::notify(osg::ALWAYS)<<"ea.getY()="<<ea.getY()<<std::endl; 73 osg::notify(osg::ALWAYS)<<"ea.getYin()="<<ea.getYmin()<<std::endl; 74 osg::notify(osg::ALWAYS)<<"ea.getYmax()="<<ea.getYmax()<<std::endl; 75 76 return true; // true means event handled: not forwarded to the camera manipulator; 77 68 78 } 69 79 case(osgGA::GUIEventAdapter::PUSH): 70 80 { 71 if(ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) 72 { 73 OSG_ALWAYS<<"mouse click left!"<<std::endl; 81 OSG_ALWAYS<<"mouse click!"<<std::endl; 82 if ( activeSetupMode == MANUAL && ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_CTRL && ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON ) 83 { 84 osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa); 85 if ( viewer ) 86 { 87 osg::notify(osg::ALWAYS)<<std::endl<<"Intersection:"<<std::endl<<"ea.getX()="<<ea.getX()<<std::endl; 88 osg::notify(osg::ALWAYS)<<"ea.getY()="<<ea.getY()<<std::endl; 89 osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::PROJECTION, ea.getX(), ea.getY()); 90 osgUtil::IntersectionVisitor iv( intersector.get() ); 91 _distortionSet->getDistortionCamera()->accept( iv ); 92 93 if ( intersector->containsIntersections() ) 94 { 95 osgUtil::LineSegmentIntersector::Intersection& result = *(intersector->getIntersections().begin()); 96 computeSelectedVertex( result ); 97 } 98 } 74 99 } 75 100 break; … … 92 117 switch(activeSetupMode) 93 118 { 94 case DISABLED : activeSetupMode = MANUAL; break; 95 case MANUAL : activeSetupMode = DELEGATED; break; 96 case DELEGATED : activeSetupMode = DISABLED; break; 119 case DISABLED : 120 { 121 activeSetupMode = MANUAL; 122 OSG_ALWAYS<<"SetupMode MANUAL activated"<<std::endl; 123 _distortionSet->getDistortionInternals()->setValue(1, true); // per definition #0 = mesh, #1 = highlighter 124 break; 125 } 126 case MANUAL : 127 { 128 activeSetupMode = DELEGATED; 129 _distortionSet->getDistortionInternals()->setValue(1, false); // per definition #0 = mesh, #1 = highlighter 130 OSG_ALWAYS<<"SetupMode DELEGATED activated"<<std::endl; 131 break; 132 } 133 case DELEGATED : 134 { 135 activeSetupMode = DISABLED; 136 _distortionSet->getDistortionInternals()->setValue(1, false); // per definition #0 = mesh, #1 = highlighter 137 OSG_ALWAYS<<"SetupMode DISABLED activated"<<std::endl; 138 break; 139 } 97 140 } 98 141 99 OSG_ALWAYS<<"KEY_KP_4 : Setup Mode now "<<activeSetupMode<<std::endl;100 142 return(true); 101 143 } … … 229 271 _distortionSet->setShowIntensityMapOnly(show); 230 272 } 273 274 void DistortionManipulator::createVertexHighlighter() 275 { 276 osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array(1); 277 (*colors)[0] = _highlightColor; 278 279 _highlighter = new osg::Geometry; 280 _highlighter->setDataVariance( osg::Object::DYNAMIC ); 281 _highlighter->setUseDisplayList( false ); 282 _highlighter->setUseVertexBufferObjects( true ); 283 _highlighter->setVertexArray( new osg::Vec3Array(1) ); // The highlighter vertex is updated by computeSelectedVertex(..) 284 _highlighter->setColorArray( colors.get() ); 285 _highlighter->setColorBinding( osg::Geometry::BIND_OVERALL ); 286 _highlighter->addPrimitiveSet( new osg::DrawArrays(GL_POINTS, 0, 1) ); 287 288 289 osg::ref_ptr<osg::Geode> geode = new osg::Geode; 290 geode->addDrawable( _highlighter.get() ); 291 geode->getOrCreateStateSet()->setAttributeAndModes( new osg::Point(8.0f) ); 292 geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF ); 293 geode->setCullingActive(false); // disable the culling for the selector, otherwise the selector is culled away on the edge. 294 295 _distortionSet->getDistortionInternals()->addChild(geode, false); 296 } 297 298 void DistortionManipulator::computeSelectedVertex( osgUtil::LineSegmentIntersector::Intersection& result ) 299 { 300 osg::Geometry* geom = dynamic_cast<osg::Geometry*>( result.drawable.get() ); 301 if ( !geom || geom==_highlighter ) 302 return; 303 304 osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>( geom->getVertexArray() ); 305 osg::Vec3Array* selVertices = dynamic_cast<osg::Vec3Array*>( _highlighter->getVertexArray() ); 306 if ( !vertices || !selVertices ) 307 return; 308 309 OSG_NOTIFY(osg::ALWAYS)<<"size of vertices="<<vertices->size()<<std::endl; 310 OSG_NOTIFY(osg::ALWAYS)<<"size of selVertices="<<selVertices->size()<<std::endl; 311 312 osg::Vec3 point = result.getWorldIntersectPoint(); 313 osg::Matrix matrix = osg::computeLocalToWorld( result.nodePath ); // To compute the intersection vertices in world coordinates not in model coordinates 314 OSG_NOTIFY(osg::ALWAYS) << "Intersection-indices: Size=" << result.indexList.size() << std::endl; 315 const std::vector<unsigned int>& selIndices = result.indexList; 316 { 317 double maxRatio = 0.0; 318 int closestVertexIndex = 0; 319 for ( unsigned int i=0; i<3 && i<result.ratioList.size(); i++ ) //iterate through rations and search for maxRation=nearestVertex 320 { 321 if(result.ratioList[i] > maxRatio) 322 { 323 maxRatio = result.ratioList[i]; 324 closestVertexIndex = result.indexList[i]; 325 } 326 OSG_NOTIFY(osg::ALWAYS)<<"maxRatio="<<maxRatio<<std::endl; 327 OSG_NOTIFY(osg::ALWAYS)<<"closestVertexIndex="<<closestVertexIndex<<std::endl; 328 } 329 OSG_NOTIFY(osg::ALWAYS)<<"nearest vertex: X="<<(*vertices)[closestVertexIndex].x()<<" Y="<<(*vertices)[closestVertexIndex].y()<<" Z="<<(*vertices)[closestVertexIndex].z()<<std::endl; 330 osg::Vec3 vertex = (*vertices)[closestVertexIndex] * matrix; 331 332 selVertices->front() = vertex; 333 OSG_NOTIFY(osg::ALWAYS)<<"selected vertice: X="<<vertex.x()<<" Y="<<vertex.y()<<" Z="<<vertex.z()<<std::endl; 334 } 335 selVertices->dirty(); 336 _highlighter->dirtyBound(); 337 } -
experimental/distortionNG/DistortionManipulator.h
r349 r353 21 21 22 22 #include <osgGA/GUIEventHandler> 23 #include<osg/Referenced> 23 #include <osg/Referenced> 24 #include <osg/Switch> 25 #include <osg/Geometry> 26 #include <osgUtil/IntersectionVisitor> 27 #include <osgUtil/LineSegmentIntersector> 28 24 29 #include "DistortionSet.h" 25 30 26 31 27 32 namespace osgViewer { 33 28 34 29 35 class DistortionManipulator : public osgGA::GUIEventHandler … … 50 56 51 57 52 DistortionManipulator( );58 DistortionManipulator(DistortionSet* ds); 53 59 virtual ~DistortionManipulator(); 54 60 … … 58 64 void setDistortionSet(DistortionSet* ds) {_distortionSet = ds;} 59 65 66 67 60 68 private: 61 69 void resetIntensityMap(); … … 63 71 void showDistortionMesh(bool show); 64 72 void showIntensityMap(bool show); 73 74 void computeSelectedVertex( osgUtil::LineSegmentIntersector::Intersection& result ); 75 void createVertexHighlighter(); 65 76 66 77 SetupMode activeSetupMode; … … 70 81 71 82 osg::ref_ptr<DistortionSet> _distortionSet; 83 84 85 osg::ref_ptr<osg::Geometry> _highlighter; 86 osg::observer_ptr<osg::Camera> _camera; 87 osg::Geometry* _distortionMesh; 88 const osg::Vec4 _highlightColor; 72 89 }; 73 90 -
experimental/distortionNG/DistortionSet.cpp
r350 r353 36 36 37 37 _showIntesityMapMapOnly = false; 38 39 _camera = NULL; 40 _distortionInternals = NULL; 41 42 _distortionInternals = new osg::Switch(); 38 43 } 39 44 … … 41 46 { 42 47 } 43 44 48 45 49 REGISTER_OBJECT_WRAPPER( osgViewer_DistortionSet, // The unique wrapper name -
experimental/distortionNG/DistortionSet.h
r350 r353 21 21 #include <osg/Image> 22 22 #include <osg/Matrixd> 23 #include <osg/Camera> 24 #include <osg/Switch> 23 25 24 26 namespace osgViewer { … … 46 48 osg::Image* getIntensityMap() { return _intensityMap; } // Also for manipulating the intensity map 47 49 bool& getShowIntensityMapOnlyAsRef() { return _showIntesityMapMapOnly;} 50 51 void setDistortionCamera(osg::Camera* camera) { _camera = camera;} 52 osg::Camera* getDistortionCamera() { return _camera.get(); } 53 54 osg::Switch* getDistortionInternals() { return _distortionInternals; } 48 55 49 56 … … 92 99 93 100 bool _showIntesityMapMapOnly; // Intensity Blending is done by a frag shader. To control it on runtime, the uniform must be bound to a central variable. 101 102 // Interconnect-Pointer to allow acces between distortion elements (osgViewer) and the distortion manipulator. 103 // These variable are therefor not saved/restored by the serializer but populated on runtime. 104 osg::observer_ptr<osg::Camera> _camera; // is used to pass the distortion cam from osgViewers setupDistortion() method to the manipulator. 105 osg::ref_ptr<osg::Switch> _distortionInternals; 94 106 }; 95 107 -
experimental/distortionNG/distortionNG.cpp
r347 r353 5 5 6 6 #include<osgViewer/Viewer> 7 8 9 10 distortionNG::distortionNG()11 {12 }13 14 distortionNG::~distortionNG()15 {16 }17 7 18 8 … … 79 69 osg::notify(osg::ALWAYS)<<"ea.getYmax()="<<ea.getYmax()<<std::endl; 80 70 81 return true; // true means event hand eld: not forwarded to the camera manipulator;71 return true; // true means event handled: not forwarded to the camera manipulator; 82 72 } 83 73 -
experimental/distortionNG/distortionNG.h
r329 r353 30 30 const osg::Vec4 _highlightColor; 31 31 }; 32 33 34 35 class distortionNG : public osg::Referenced36 {37 38 public:39 distortionNG();40 virtual ~distortionNG();41 }; -
experimental/distortionNG/extViewer.cpp
r352 r353 20 20 #include "distortionNG.h" 21 21 22 #include <osg/Switch> 22 23 #include <osg/PolygonOffset> 23 24 #include <osg/Texture2D> … … 273 274 camera->setViewMatrix(osg::Matrix::identity()); 274 275 275 osg::ref_ptr<osg:: Group> root = new osg::Group;276 root->addChild(geode ); // add mesh to rootso camera renders mesh (and thus render the scene)276 osg::ref_ptr<osg::Switch> root = _distortionSet->getDistortionInternals(); 277 root->addChild(geode, true); // adds mesh,shader,.. so camera renders mesh (and thus render the scene) 277 278 camera->addChild(root); 278 279 279 // selector 280 //geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF ); 281 //geode->getOrCreateStateSet()->setAttributeAndModes( new osg::PolygonOffset(1.0f, 1.0f) ); 282 //osg::ref_ptr<distortionHandler> selectorHandler = new distortionHandler( camera, distortionMesh ); 283 //osg::Geode* selectorHighlighter = selectorHandler->createVertexHighlighter(); 284 //selectorHighlighter->setCullingActive(false); // disable the culling for the selector, otherwise the selector is culled away on the edge. 285 //root->addChild(selectorHighlighter); 286 //addEventHandler( selectorHandler.get() );// should be in main.cpp 280 _distortionSet->setDistortionCamera( camera ); 281 282 // Ensure selector is visible: 283 geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF ); 284 geode->getOrCreateStateSet()->setAttributeAndModes( new osg::PolygonOffset(1.0f, 1.0f) ); 287 285 288 //// Avoid that the highlighter is culled away289 //osg::CullSettings::CullingMode mode = camera->getCullingMode();290 //camera->setCullingMode( mode & (~osg::CullSettings::SMALL_FEATURE_CULLING) );291 292 286 camera->setName("Dist Cam"); 293 287 -
experimental/distortionNG/main.cpp
r352 r353 47 47 osg::Image* intMap = osgDB::readImageFile("intensitymap.png"); 48 48 if (!intMap) 49 { 50 osg::notify(osg::WARN) << "Couldn't find intensity map, quiting." << std::endl; 51 return -1; 52 } 49 osg::notify(osg::WARN) << "Couldn't find intensity map, skip intensityMap setup." << std::endl; 50 53 51 // Create DistortionSet 54 52 osg::ref_ptr<osgViewer::DistortionSet> _distortionSet = new osgViewer::DistortionSet(); … … 62 60 63 61 // set up the camera manipulators. 62 osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator; 63 64 keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() ); 65 keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() ); 66 keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() ); 67 keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() ); 68 69 std::string pathfile; 70 char keyForAnimationPath = '5'; 71 while (arguments.read("-p",pathfile)) 64 72 { 65 osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator; 66 67 keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() ); 68 keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() ); 69 keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() ); 70 keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() ); 71 72 std::string pathfile; 73 char keyForAnimationPath = '5'; 74 while (arguments.read("-p",pathfile)) 73 osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile); 74 if (apm || !apm->valid()) 75 75 { 76 osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile); 77 if (apm || !apm->valid()) 78 { 79 unsigned int num = keyswitchManipulator->getNumMatrixManipulators(); 80 keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm ); 81 keyswitchManipulator->selectMatrixManipulator(num); 82 ++keyForAnimationPath; 83 } 76 unsigned int num = keyswitchManipulator->getNumMatrixManipulators(); 77 keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm ); 78 keyswitchManipulator->selectMatrixManipulator(num); 79 ++keyForAnimationPath; 84 80 } 85 86 viewer.setCameraManipulator( keyswitchManipulator.get() );87 81 } 88 82 83 // Add keyswitch manipulator for multiple scene controls- 84 viewer.setCameraManipulator( keyswitchManipulator.get() ); 85 89 86 // Add the distortion manipulator 90 osgViewer::DistortionManipulator* distManip = new osgViewer::DistortionManipulator(); 91 distManip->setDistortionSet(_distortionSet); 92 viewer.addEventHandler(distManip); 87 viewer.addEventHandler(new osgViewer::DistortionManipulator(_distortionSet)); 93 88 94 89 // add the state manipulator
Note: See TracChangeset
for help on using the changeset viewer.