Changeset 309 for experimental
- Timestamp:
- Aug 2, 2011, 11:38:09 PM (13 years ago)
- Location:
- experimental/distortionNG
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
experimental/distortionNG/distortionNG.cpp
r307 r309 1 1 #include "distortionNG.h" 2 2 3 distortionNG::distortionNG(void) 3 #include<osg/Texture2D> 4 #include<osg/Point> 5 #include<osg/ComputeBoundsVisitor> 6 7 #include<osgViewer/Viewer> 8 9 #include<osgDB/ReadFile> 10 11 #include <osgUtil/SmoothingVisitor> 12 13 14 15 distortionNG::distortionNG() 4 16 { 5 17 } 6 18 7 distortionNG::~distortionNG( void)19 distortionNG::~distortionNG() 8 20 { 9 21 } 22 23 osg::Geometry* distortionNG::createMesh( unsigned int column, unsigned int row ) 24 { 25 osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array(column * row); 26 osg::ref_ptr<osg::Vec2Array> texcoords = new osg::Vec2Array(column * row); 27 for ( unsigned int i=0; i<row; ++i ) 28 { 29 for ( unsigned int j=0; j<column; ++j ) 30 { 31 (*vertices)[i*column + j].set( (float)i, (float)j, 0.0f ); 32 (*texcoords)[i*column + j].set( (float)i/(float)row, (float)j/(float)column ); 33 } 34 } 35 36 osg::ref_ptr<osg::Geometry> geom = new osg::Geometry; 37 geom->setUseDisplayList( false ); 38 geom->setUseVertexBufferObjects( true ); 39 geom->setVertexArray( vertices.get() ); 40 geom->setTexCoordArray( 0, texcoords.get() ); 41 for ( unsigned int i=0; i<row-1; ++i ) 42 { 43 osg::ref_ptr<osg::DrawElementsUInt> de = new osg::DrawElementsUInt(GL_QUAD_STRIP, column*2); 44 for ( unsigned int j=0; j<column; ++j ) 45 { 46 (*de)[j*2 + 0] = i*column + j; 47 (*de)[j*2 + 1] = (i+1)*column + j; 48 } 49 geom->addPrimitiveSet( de.get() ); 50 } 51 52 53 54 osg::ComputeBoundsVisitor cbv; 55 cbv.applyDrawable(geom); 56 osg::BoundingBox box = cbv.getBoundingBox(); 57 if (!box.valid()) 58 std::cout << "Invalid bounding box!"; 59 geom->setInitialBound(box); 60 61 osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D; 62 texture->setImage( osgDB::readImageFile("Images/osg256.png") ); 63 texture->setFilter( osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR_MIPMAP_LINEAR ); 64 texture->setFilter( osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR_MIPMAP_LINEAR ); 65 geom->getOrCreateStateSet()->setTextureAttributeAndModes( 0, texture.get() ); 66 67 // Create normals 68 osgUtil::SmoothingVisitor::smooth( *geom ); 69 70 return geom.release(); 71 } 72 73 osg::Geode* distortionHandler::createVertexHighlighter() 74 { 75 osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array(1); 76 (*colors)[0] = _highlightColor; 77 78 _highlighter = new osg::Geometry; 79 _highlighter->setDataVariance( osg::Object::DYNAMIC ); 80 _highlighter->setUseDisplayList( false ); 81 _highlighter->setUseVertexBufferObjects( true ); 82 _highlighter->setVertexArray( new osg::Vec3Array(1) ); 83 _highlighter->setColorArray( colors.get() ); 84 _highlighter->setColorBinding( osg::Geometry::BIND_OVERALL ); 85 _highlighter->addPrimitiveSet( new osg::DrawArrays(GL_POINTS, 0, 1) ); 86 87 osg::ref_ptr<osg::Geode> geode = new osg::Geode; 88 geode->addDrawable( _highlighter.get() ); 89 geode->getOrCreateStateSet()->setAttributeAndModes( new osg::Point(10.0f) ); 90 geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF ); 91 92 return geode.release(); 93 } 94 95 bool distortionHandler::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa ) 96 { 97 if ( ea.getEventType()!=osgGA::GUIEventAdapter::RELEASE || 98 ea.getButton()!=osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON || 99 !(ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_CTRL) ) 100 return false; 101 102 osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa); 103 if ( viewer ) 104 { 105 osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, ea.getX(), ea.getY()); 106 osgUtil::IntersectionVisitor iv( intersector.get() ); 107 viewer->getCamera()->accept( iv ); 108 109 if ( intersector->containsIntersections() ) 110 { 111 osgUtil::LineSegmentIntersector::Intersection& result = *(intersector->getIntersections().begin()); 112 computeSelectedVertex( result ); 113 } 114 } 115 return false; 116 } 117 118 void distortionHandler::computeSelectedVertex( osgUtil::LineSegmentIntersector::Intersection& result ) 119 { 120 osg::Geometry* geom = dynamic_cast<osg::Geometry*>( result.drawable.get() ); 121 if ( !geom || !_highlighter || geom==_highlighter ) 122 return; 123 124 osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>( geom->getVertexArray() ); 125 osg::Vec3Array* selVertices = dynamic_cast<osg::Vec3Array*>( _highlighter->getVertexArray() ); 126 if ( !vertices || !selVertices ) 127 return; 128 129 osg::Vec3 point = result.getWorldIntersectPoint(); 130 osg::Matrix matrix = osg::computeLocalToWorld( result.nodePath ); // To compute the intersection vertices in world coordinates not in model coordinates 131 //std::cout << "Intersection-indices: Size=" << result.indexList.size() << std::endl; 132 const std::vector<unsigned int>& selIndices = result.indexList; 133 { 134 double maxRatio = 0.0; 135 int closestVertexIndex = 0; 136 for ( unsigned int i=0; i<3 && i<result.ratioList.size(); i++ ) //iterate through rations and search for max 137 { 138 if(result.ratioList[i] > maxRatio) 139 { 140 maxRatio = result.ratioList[i]; 141 closestVertexIndex = result.indexList[i]; 142 } 143 } 144 osg::Vec3 vertex = (*vertices)[closestVertexIndex] * matrix; 145 selVertices->front() = vertex; 146 } 147 148 selVertices->dirty(); 149 _highlighter->dirtyBound(); 150 } -
experimental/distortionNG/distortionNG.h
r308 r309 2 2 3 3 #include<osg/Referenced> 4 #include<osg/Geometry> 5 #include<osg/MatrixTransform> 6 7 #include<osgGA/GUIEventHandler> 8 9 #include<osgViewer/Viewer> 10 11 12 13 14 class distortionHandler : public osgGA::GUIEventHandler 15 { 16 17 public: 18 distortionHandler( osg::Camera* camera ) 19 : _highlighter(0), _camera(camera), _highlightColor( osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f) ) {} 20 21 void computeSelectedVertex( osgUtil::LineSegmentIntersector::Intersection& result ); 22 bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa ); 23 24 osg::Geode* createVertexHighlighter(); 25 26 protected: 27 osg::ref_ptr<osg::Geometry> _highlighter; 28 osg::observer_ptr<osg::Camera> _camera; 29 const osg::Vec4 _highlightColor; 30 }; 31 32 4 33 5 34 class distortionNG : public osg::Referenced 6 35 { 36 7 37 public: 8 distortionNG(void); 9 virtual ~distortionNG(void); 38 distortionNG(); 39 virtual ~distortionNG(); 40 41 static osg::Geometry* createMesh(unsigned int column, unsigned int row); 10 42 }; -
experimental/distortionNG/distortionNG.vcproj
r307 r309 44 44 AdditionalIncludeDirectories=""$(OSG_ROOT)\include"" 45 45 PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE" 46 MinimalRebuild=" true"46 MinimalRebuild="false" 47 47 BasicRuntimeChecks="3" 48 48 RuntimeLibrary="3" -
experimental/distortionNG/main.cpp
r308 r309 17 17 */ 18 18 19 #include "distortionNG.h" 20 19 21 #include <osg/ArgumentParser> 22 #include <osg/PolygonOffset> 23 20 24 #include <osgDB/ReadFile> 21 25 … … 93 97 if (!rootnode) 94 98 { 95 osg::notify(osg::WARN)<<"Warning: no valid data loaded, please specify a database on the command line."<<std::endl; 96 return 1; 99 rootnode = osgDB::readNodeFile("cow.osgt"); 100 if(!rootnode) 101 { 102 osg::notify(osg::WARN)<<"Warning: no valid data loaded, please specify a database on the command line."<<std::endl; 103 return 1; 104 } 97 105 } 98 106 107 osg::ref_ptr<osg::Geode> geode = new osg::Geode; 108 geode->addDrawable( distortionNG::createMesh(16, 9) ); 109 geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF ); 110 geode->getOrCreateStateSet()->setAttributeAndModes( new osg::PolygonOffset(1.0f, 1.0f) ); 99 111 100 // add a viewport to the viewer and attach the scene graph. 101 viewer.setSceneData( rootnode ); 112 osg::ref_ptr<distortionHandler> selector = new distortionHandler( viewer.getCamera() ); 102 113 114 osg::ref_ptr<osg::Group> root = new osg::Group; 115 root->addChild( geode.get() ); 116 root->addChild( selector->createVertexHighlighter() ); 103 117 104 // run the viewers main loop 105 return viewer.run();118 viewer.addEventHandler( selector.get() ); 119 viewer.setSceneData( root.get() ); 106 120 121 // Avoid that the highlighter is culled away 122 osg::CullSettings::CullingMode mode = viewer.getCamera()->getCullingMode(); 123 viewer.getCamera()->setCullingMode( mode & (~osg::CullSettings::SMALL_FEATURE_CULLING) ); 124 125 // run the viewers main loop 126 return viewer.run(); 107 127 }
Note: See TracChangeset
for help on using the changeset viewer.