Changeset 309


Ignore:
Timestamp:
Aug 2, 2011, 11:38:09 PM (13 years ago)
Author:
Torben Dannhauer
Message:

first try to select a vertex in a mesh.
Nextstep: catch mouse drags from the GA, transform into mesh CF and modify vertex.

Location:
experimental/distortionNG
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • experimental/distortionNG/distortionNG.cpp

    r307 r309  
    11#include "distortionNG.h"
    22
    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
     15distortionNG::distortionNG()
    416{
    517}
    618
    7 distortionNG::~distortionNG(void)
     19distortionNG::~distortionNG()
    820{
    921}
     22
     23osg::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
     73osg::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
     95bool 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
     118void 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  
    22
    33#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
     14class distortionHandler : public osgGA::GUIEventHandler
     15{
     16
     17public:
     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
     26protected:
     27        osg::ref_ptr<osg::Geometry> _highlighter;
     28    osg::observer_ptr<osg::Camera> _camera;
     29        const osg::Vec4 _highlightColor;
     30};
     31
     32
    433
    534class distortionNG : public osg::Referenced
    635{
     36
    737public:
    8         distortionNG(void);
    9         virtual ~distortionNG(void);
     38        distortionNG();
     39        virtual ~distortionNG();
     40
     41        static osg::Geometry* createMesh(unsigned int column, unsigned int row);
    1042};
  • experimental/distortionNG/distortionNG.vcproj

    r307 r309  
    4444                                AdditionalIncludeDirectories="&quot;$(OSG_ROOT)\include&quot;"
    4545                                PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
    46                                 MinimalRebuild="true"
     46                                MinimalRebuild="false"
    4747                                BasicRuntimeChecks="3"
    4848                                RuntimeLibrary="3"
  • experimental/distortionNG/main.cpp

    r308 r309  
    1717*/
    1818
     19#include "distortionNG.h"
     20
    1921#include <osg/ArgumentParser>
     22#include <osg/PolygonOffset>
     23
    2024#include <osgDB/ReadFile>
    2125
     
    9397    if (!rootnode)
    9498    {
    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                }
    97105    }
    98106
     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) );
    99111
    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() );
    102113
     114        osg::ref_ptr<osg::Group> root = new osg::Group;
     115        root->addChild( geode.get() );
     116        root->addChild( selector->createVertexHighlighter() );
    103117
    104     // run the viewers main loop
    105     return viewer.run();
     118        viewer.addEventHandler( selector.get() );
     119        viewer.setSceneData( root.get() );
    106120
     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();
    107127}
Note: See TracChangeset for help on using the changeset viewer.