#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(8.0f) ); geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF ); return geode.release(); } bool distortionHandler::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa ) { // Only pressing CTRL activates distortion editing if ( !(ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_CTRL) ) return false; // False means not handled. if ( ea.getEventType() == osgGA::GUIEventAdapter::PUSH && ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON ) { 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 ); } } } if ( ea.getEventType() == osgGA::GUIEventAdapter::DRAG ) { osg::notify(osg::ALWAYS)<