#include "distortionNG.h" #include #include #include #include #include #include distortionNG::distortionNG() { } distortionNG::~distortionNG() { } osg::Geometry* distortionNG::createMesh( unsigned int column, unsigned int row ) { osg::ref_ptr vertices = new osg::Vec3Array(column * row); osg::ref_ptr texcoords = new osg::Vec2Array(column * row); for ( unsigned int i=0; i geom = new osg::Geometry; geom->setUseDisplayList( false ); geom->setUseVertexBufferObjects( true ); geom->setVertexArray( vertices.get() ); geom->setTexCoordArray( 0, texcoords.get() ); for ( unsigned int i=0; i de = new osg::DrawElementsUInt(GL_QUAD_STRIP, column*2); for ( unsigned int j=0; jaddPrimitiveSet( de.get() ); } osg::ComputeBoundsVisitor cbv; cbv.applyDrawable(geom); osg::BoundingBox box = cbv.getBoundingBox(); if (!box.valid()) std::cout << "Invalid bounding box!"; geom->setInitialBound(box); osg::ref_ptr texture = new osg::Texture2D; texture->setImage( osgDB::readImageFile("Images/osg256.png") ); texture->setFilter( osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR_MIPMAP_LINEAR ); texture->setFilter( osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR_MIPMAP_LINEAR ); geom->getOrCreateStateSet()->setTextureAttributeAndModes( 0, texture.get() ); // Create normals osgUtil::SmoothingVisitor::smooth( *geom ); return geom.release(); } osg::Geode* distortionHandler::createVertexHighlighter() { osg::ref_ptr colors = new osg::Vec4Array(1); (*colors)[0] = _highlightColor; _highlighter = new osg::Geometry; _highlighter->setDataVariance( osg::Object::DYNAMIC ); _highlighter->setUseDisplayList( false ); _highlighter->setUseVertexBufferObjects( true ); _highlighter->setVertexArray( new osg::Vec3Array(1) ); _highlighter->setColorArray( colors.get() ); _highlighter->setColorBinding( osg::Geometry::BIND_OVERALL ); _highlighter->addPrimitiveSet( new osg::DrawArrays(GL_POINTS, 0, 1) ); osg::ref_ptr geode = new osg::Geode; geode->addDrawable( _highlighter.get() ); geode->getOrCreateStateSet()->setAttributeAndModes( new osg::Point(10.0f) ); geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF ); return geode.release(); } bool distortionHandler::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) ) return false; osgViewer::View* viewer = dynamic_cast(&aa); if ( viewer ) { osg::ref_ptr intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, ea.getX(), ea.getY()); osgUtil::IntersectionVisitor iv( intersector.get() ); viewer->getCamera()->accept( iv ); if ( intersector->containsIntersections() ) { osgUtil::LineSegmentIntersector::Intersection& result = *(intersector->getIntersections().begin()); computeSelectedVertex( result ); } } return false; } void distortionHandler::computeSelectedVertex( osgUtil::LineSegmentIntersector::Intersection& result ) { osg::Geometry* geom = dynamic_cast( result.drawable.get() ); if ( !geom || !_highlighter || geom==_highlighter ) return; osg::Vec3Array* vertices = dynamic_cast( geom->getVertexArray() ); osg::Vec3Array* selVertices = dynamic_cast( _highlighter->getVertexArray() ); if ( !vertices || !selVertices ) return; osg::Vec3 point = result.getWorldIntersectPoint(); osg::Matrix matrix = osg::computeLocalToWorld( result.nodePath ); // To compute the intersection vertices in world coordinates not in model coordinates //std::cout << "Intersection-indices: Size=" << result.indexList.size() << std::endl; const std::vector& selIndices = result.indexList; { double maxRatio = 0.0; int closestVertexIndex = 0; for ( unsigned int i=0; i<3 && i maxRatio) { maxRatio = result.ratioList[i]; closestVertexIndex = result.indexList[i]; } } osg::Vec3 vertex = (*vertices)[closestVertexIndex] * matrix; selVertices->front() = vertex; } selVertices->dirty(); _highlighter->dirtyBound(); }