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.

File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.