Changeset 353 for experimental


Ignore:
Timestamp:
Apr 21, 2012, 9:49:26 PM (12 years ago)
Author:
Torben Dannhauer
Message:

DistortionManipulator? is working (alpha stadium)

Location:
experimental/distortionNG
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • experimental/distortionNG/DistortionManipulator.cpp

    r350 r353  
    2020
    2121#include<osgViewer/Viewer>
     22#include<osg/Point>
    2223
    2324using namespace osg;
     
    2526
    2627
    27 DistortionManipulator::DistortionManipulator()
     28DistortionManipulator::DistortionManipulator(DistortionSet* ds)
     29 : _highlightColor( osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f) ), _distortionSet( ds )
    2830{
    2931        activeSetupMode = DISABLED;
     
    3234        activeVisualizationMode = NONE;
    3335
    34         _distortionSet = NULL;
     36        _highlighter = NULL;
     37       
     38        createVertexHighlighter();
     39
     40        _camera = 0;
     41        //_distortionMesh(distortionMesh)
    3542}
    3643
     
    5259bool DistortionManipulator::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa, osg::Object* obj, osg::NodeVisitor* nv)
    5360{
    54         //osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa);
    55         //if ( viewer && !_distortionSet.isValid())
    56         //{
    57         //      OSG_ALWAYS<<"Fetching valid DistortionSet!"<<std::endl;
    58         //      _distortionSet = viewer->getDistortionSet();
    59         //}
    60 
    6161        switch(ea.getEventType())
    6262    {
     
    6464        case(osgGA::GUIEventAdapter::DRAG):
    6565                {
    66                         OSG_ALWAYS<<"DRAG!"<<std::endl;
    67                         break;
     66                        osg::notify(osg::ALWAYS)<<std::endl<<"Drag:"<<std::endl<<"ea.getGraphicsContext()="<<ea.getGraphicsContext()<<std::endl;
     67                        osg::notify(osg::ALWAYS)<<"ea.getXnormalized()="<<ea.getXnormalized()<<std::endl;
     68                        osg::notify(osg::ALWAYS)<<"ea.getYnormalized()="<<ea.getYnormalized()<<std::endl;
     69                        osg::notify(osg::ALWAYS)<<"ea.getX()="<<ea.getX()<<std::endl;
     70                        osg::notify(osg::ALWAYS)<<"ea.getXin()="<<ea.getXmin()<<std::endl;
     71                        osg::notify(osg::ALWAYS)<<"ea.getXmax()="<<ea.getXmax()<<std::endl;
     72                        osg::notify(osg::ALWAYS)<<"ea.getY()="<<ea.getY()<<std::endl;
     73                        osg::notify(osg::ALWAYS)<<"ea.getYin()="<<ea.getYmin()<<std::endl;
     74                        osg::notify(osg::ALWAYS)<<"ea.getYmax()="<<ea.getYmax()<<std::endl;
     75
     76                        return true;    // true means event handled: not forwarded to the camera manipulator;
     77
    6878                }
    6979        case(osgGA::GUIEventAdapter::PUSH):
    7080                {
    71                         if(ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)
    72                         {
    73                                 OSG_ALWAYS<<"mouse click left!"<<std::endl;
     81                        OSG_ALWAYS<<"mouse click!"<<std::endl;
     82                        if ( activeSetupMode == MANUAL && ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_CTRL && ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON )
     83                        {
     84                                osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa);
     85                                if ( viewer )
     86                                {
     87                                        osg::notify(osg::ALWAYS)<<std::endl<<"Intersection:"<<std::endl<<"ea.getX()="<<ea.getX()<<std::endl;
     88                                        osg::notify(osg::ALWAYS)<<"ea.getY()="<<ea.getY()<<std::endl;
     89                                        osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::PROJECTION, ea.getX(), ea.getY());
     90                                        osgUtil::IntersectionVisitor iv( intersector.get() );
     91                                        _distortionSet->getDistortionCamera()->accept( iv );
     92                               
     93                                        if ( intersector->containsIntersections() )
     94                                        {
     95                                                osgUtil::LineSegmentIntersector::Intersection& result = *(intersector->getIntersections().begin());
     96                                                computeSelectedVertex( result );
     97                                        }
     98                                }
    7499                        }
    75100                        break;
     
    92117                                switch(activeSetupMode)
    93118                                {
    94                                         case DISABLED : activeSetupMode = MANUAL; break;
    95                                         case MANUAL : activeSetupMode = DELEGATED; break;
    96                                         case DELEGATED : activeSetupMode = DISABLED; break;
     119                                        case DISABLED :
     120                                        {
     121                                                activeSetupMode = MANUAL;
     122                                                OSG_ALWAYS<<"SetupMode MANUAL activated"<<std::endl;
     123                                                _distortionSet->getDistortionInternals()->setValue(1, true); // per definition #0 = mesh, #1 = highlighter
     124                                                break;
     125                                        }
     126                                        case MANUAL :
     127                                        {
     128                                                activeSetupMode = DELEGATED;
     129                                                _distortionSet->getDistortionInternals()->setValue(1, false); // per definition #0 = mesh, #1 = highlighter
     130                                                OSG_ALWAYS<<"SetupMode DELEGATED activated"<<std::endl;
     131                                                break;
     132                                        }
     133                                        case DELEGATED :
     134                                        {
     135                                                activeSetupMode = DISABLED;
     136                                                _distortionSet->getDistortionInternals()->setValue(1, false); // per definition #0 = mesh, #1 = highlighter
     137                                                OSG_ALWAYS<<"SetupMode DISABLED activated"<<std::endl;
     138                                                break;
     139                                        }
    97140                                }
    98141
    99                                 OSG_ALWAYS<<"KEY_KP_4 : Setup Mode now "<<activeSetupMode<<std::endl;
    100142                                return(true);
    101143                        }
     
    229271                        _distortionSet->setShowIntensityMapOnly(show);
    230272}
     273
     274void DistortionManipulator::createVertexHighlighter()
     275{
     276        osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array(1);
     277        (*colors)[0] = _highlightColor;
     278
     279        _highlighter = new osg::Geometry;
     280        _highlighter->setDataVariance( osg::Object::DYNAMIC );
     281        _highlighter->setUseDisplayList( false );
     282        _highlighter->setUseVertexBufferObjects( true );
     283        _highlighter->setVertexArray( new osg::Vec3Array(1) );  // The highlighter vertex is updated by computeSelectedVertex(..)
     284        _highlighter->setColorArray( colors.get() );
     285        _highlighter->setColorBinding( osg::Geometry::BIND_OVERALL );
     286        _highlighter->addPrimitiveSet( new osg::DrawArrays(GL_POINTS, 0, 1) );
     287
     288       
     289        osg::ref_ptr<osg::Geode> geode = new osg::Geode;
     290        geode->addDrawable( _highlighter.get() );
     291        geode->getOrCreateStateSet()->setAttributeAndModes( new osg::Point(8.0f) );
     292        geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
     293        geode->setCullingActive(false); // disable the culling for the selector, otherwise the selector is culled away on the edge.
     294
     295        _distortionSet->getDistortionInternals()->addChild(geode, false);
     296}
     297
     298void DistortionManipulator::computeSelectedVertex( osgUtil::LineSegmentIntersector::Intersection& result )
     299{
     300        osg::Geometry* geom = dynamic_cast<osg::Geometry*>( result.drawable.get() );
     301        if ( !geom || geom==_highlighter )
     302                return;
     303
     304        osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>( geom->getVertexArray() );
     305        osg::Vec3Array* selVertices = dynamic_cast<osg::Vec3Array*>( _highlighter->getVertexArray() );
     306        if ( !vertices || !selVertices )
     307                return;
     308       
     309        OSG_NOTIFY(osg::ALWAYS)<<"size of vertices="<<vertices->size()<<std::endl;
     310        OSG_NOTIFY(osg::ALWAYS)<<"size of selVertices="<<selVertices->size()<<std::endl;
     311
     312        osg::Vec3 point = result.getWorldIntersectPoint();
     313        osg::Matrix matrix = osg::computeLocalToWorld( result.nodePath );       // To compute the intersection vertices in world coordinates not in model coordinates
     314        OSG_NOTIFY(osg::ALWAYS) << "Intersection-indices: Size=" << result.indexList.size() << std::endl;
     315        const std::vector<unsigned int>& selIndices = result.indexList;
     316        {
     317                double maxRatio = 0.0;
     318                int closestVertexIndex = 0;
     319                for ( unsigned int i=0; i<3 && i<result.ratioList.size(); i++ ) //iterate through rations and search for maxRation=nearestVertex
     320                {
     321                        if(result.ratioList[i] > maxRatio)
     322                        {
     323                                maxRatio = result.ratioList[i];
     324                                closestVertexIndex = result.indexList[i];
     325                        }
     326                        OSG_NOTIFY(osg::ALWAYS)<<"maxRatio="<<maxRatio<<std::endl;
     327                        OSG_NOTIFY(osg::ALWAYS)<<"closestVertexIndex="<<closestVertexIndex<<std::endl;
     328                }
     329                OSG_NOTIFY(osg::ALWAYS)<<"nearest vertex: X="<<(*vertices)[closestVertexIndex].x()<<" Y="<<(*vertices)[closestVertexIndex].y()<<" Z="<<(*vertices)[closestVertexIndex].z()<<std::endl;
     330                osg::Vec3 vertex = (*vertices)[closestVertexIndex] * matrix;
     331
     332                selVertices->front() = vertex;         
     333                OSG_NOTIFY(osg::ALWAYS)<<"selected vertice: X="<<vertex.x()<<" Y="<<vertex.y()<<" Z="<<vertex.z()<<std::endl;
     334        }
     335        selVertices->dirty();
     336        _highlighter->dirtyBound();
     337}
  • experimental/distortionNG/DistortionManipulator.h

    r349 r353  
    2121
    2222#include <osgGA/GUIEventHandler>
    23 #include<osg/Referenced>
     23#include <osg/Referenced>
     24#include <osg/Switch>
     25#include <osg/Geometry>
     26#include <osgUtil/IntersectionVisitor>
     27#include <osgUtil/LineSegmentIntersector>
     28
    2429#include "DistortionSet.h"
    2530
    2631
    2732namespace osgViewer {
     33
    2834
    2935class DistortionManipulator : public osgGA::GUIEventHandler
     
    5056
    5157
    52         DistortionManipulator();
     58        DistortionManipulator(DistortionSet* ds);
    5359        virtual ~DistortionManipulator();
    5460
     
    5864        void setDistortionSet(DistortionSet* ds) {_distortionSet = ds;}
    5965
     66       
     67
    6068private:
    6169        void resetIntensityMap();
     
    6371        void showDistortionMesh(bool show);
    6472        void showIntensityMap(bool show);
     73
     74        void computeSelectedVertex( osgUtil::LineSegmentIntersector::Intersection& result );
     75        void createVertexHighlighter();
    6576
    6677        SetupMode activeSetupMode;
     
    7081
    7182        osg::ref_ptr<DistortionSet> _distortionSet;
     83
     84
     85        osg::ref_ptr<osg::Geometry> _highlighter;
     86    osg::observer_ptr<osg::Camera> _camera;
     87        osg::Geometry* _distortionMesh;
     88        const osg::Vec4 _highlightColor;
    7289};
    7390
  • experimental/distortionNG/DistortionSet.cpp

    r350 r353  
    3636
    3737        _showIntesityMapMapOnly = false;
     38
     39        _camera = NULL;
     40        _distortionInternals = NULL;
     41
     42        _distortionInternals = new osg::Switch();
    3843}
    3944
     
    4146{
    4247}
    43 
    4448
    4549REGISTER_OBJECT_WRAPPER( osgViewer_DistortionSet,                  // The unique wrapper name
  • experimental/distortionNG/DistortionSet.h

    r350 r353  
    2121#include <osg/Image>
    2222#include <osg/Matrixd>
     23#include <osg/Camera>
     24#include <osg/Switch>
    2325
    2426namespace osgViewer {
     
    4648        osg::Image* getIntensityMap()  { return _intensityMap; }        // Also for manipulating the intensity map
    4749        bool& getShowIntensityMapOnlyAsRef()    { return _showIntesityMapMapOnly;}
     50
     51        void setDistortionCamera(osg::Camera* camera)   { _camera = camera;}
     52        osg::Camera* getDistortionCamera()      { return _camera.get(); }
     53
     54        osg::Switch* getDistortionInternals()   { return _distortionInternals; }
    4855
    4956
     
    9299
    93100        bool _showIntesityMapMapOnly;   // Intensity Blending is done by a frag shader. To control it on runtime, the uniform must be bound to a central variable.
     101
     102        // Interconnect-Pointer to allow acces between distortion elements (osgViewer) and the distortion manipulator.
     103        // These variable are therefor not saved/restored by the serializer but populated on runtime.
     104        osg::observer_ptr<osg::Camera> _camera; // is used to pass the distortion cam from osgViewers setupDistortion() method to the manipulator.
     105        osg::ref_ptr<osg::Switch> _distortionInternals;
    94106};
    95107
  • experimental/distortionNG/distortionNG.cpp

    r347 r353  
    55
    66#include<osgViewer/Viewer>
    7 
    8 
    9 
    10 distortionNG::distortionNG()
    11 {
    12 }
    13 
    14 distortionNG::~distortionNG()
    15 {
    16 }
    177
    188
     
    7969        osg::notify(osg::ALWAYS)<<"ea.getYmax()="<<ea.getYmax()<<std::endl;
    8070
    81                 return true;    // true means event handeld: not forwarded to the camera manipulator;
     71                return true;    // true means event handled: not forwarded to the camera manipulator;
    8272        }
    8373
  • experimental/distortionNG/distortionNG.h

    r329 r353  
    3030        const osg::Vec4 _highlightColor;
    3131};
    32 
    33 
    34 
    35 class distortionNG : public osg::Referenced
    36 {
    37 
    38 public:
    39         distortionNG();
    40         virtual ~distortionNG();
    41 };
  • experimental/distortionNG/extViewer.cpp

    r352 r353  
    2020#include "distortionNG.h"
    2121
     22#include <osg/Switch>
    2223#include <osg/PolygonOffset>
    2324#include <osg/Texture2D>
     
    273274        camera->setViewMatrix(osg::Matrix::identity());
    274275
    275                 osg::ref_ptr<osg::Group> root = new osg::Group;
    276                 root->addChild(geode);  // add mesh to root so camera renders mesh (and  thus render the scene)
     276                osg::ref_ptr<osg::Switch> root = _distortionSet->getDistortionInternals();
     277                root->addChild(geode, true);    // adds mesh,shader,.. so camera renders mesh (and  thus render the scene)
    277278                camera->addChild(root);
    278279
    279                 // selector
    280                 //geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
    281                 //geode->getOrCreateStateSet()->setAttributeAndModes( new osg::PolygonOffset(1.0f, 1.0f) );
    282                 //osg::ref_ptr<distortionHandler> selectorHandler = new distortionHandler( camera, distortionMesh );
    283                 //osg::Geode* selectorHighlighter = selectorHandler->createVertexHighlighter();
    284                 //selectorHighlighter->setCullingActive(false); // disable the culling for the selector, otherwise the selector is culled away on the edge.
    285                 //root->addChild(selectorHighlighter);
    286                 //addEventHandler( selectorHandler.get() );// should be in main.cpp
     280                _distortionSet->setDistortionCamera( camera );
     281
     282                // Ensure selector is visible:
     283                geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
     284                geode->getOrCreateStateSet()->setAttributeAndModes( new osg::PolygonOffset(1.0f, 1.0f) );
    287285               
    288                 //// Avoid that the highlighter is culled away
    289                 //osg::CullSettings::CullingMode mode = camera->getCullingMode();
    290                 //camera->setCullingMode( mode & (~osg::CullSettings::SMALL_FEATURE_CULLING) );
    291 
    292286        camera->setName("Dist Cam");
    293287
  • experimental/distortionNG/main.cpp

    r352 r353  
    4747        osg::Image* intMap = osgDB::readImageFile("intensitymap.png");
    4848        if (!intMap)
    49         {
    50                 osg::notify(osg::WARN) << "Couldn't find intensity map, quiting." << std::endl;
    51                 return -1;
    52         }
     49                osg::notify(osg::WARN) << "Couldn't find intensity map, skip intensityMap setup." << std::endl;
     50
    5351        // Create DistortionSet
    5452        osg::ref_ptr<osgViewer::DistortionSet> _distortionSet = new osgViewer::DistortionSet();
     
    6260
    6361        // set up the camera manipulators.
     62    osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
     63
     64    keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() );
     65    keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() );
     66    keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() );
     67    keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() );
     68
     69    std::string pathfile;
     70    char keyForAnimationPath = '5';
     71    while (arguments.read("-p",pathfile))
    6472    {
    65         osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
    66 
    67         keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() );
    68         keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() );
    69         keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() );
    70         keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() );
    71 
    72         std::string pathfile;
    73         char keyForAnimationPath = '5';
    74         while (arguments.read("-p",pathfile))
     73        osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile);
     74        if (apm || !apm->valid())
    7575        {
    76             osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile);
    77             if (apm || !apm->valid())
    78             {
    79                 unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
    80                 keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm );
    81                 keyswitchManipulator->selectMatrixManipulator(num);
    82                 ++keyForAnimationPath;
    83             }
     76            unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
     77            keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm );
     78            keyswitchManipulator->selectMatrixManipulator(num);
     79            ++keyForAnimationPath;
    8480        }
    85 
    86         viewer.setCameraManipulator( keyswitchManipulator.get() );
    8781    }
    8882
     83        // Add keyswitch manipulator for multiple scene controls-
     84    viewer.setCameraManipulator( keyswitchManipulator.get() );
     85
    8986        // Add the distortion manipulator
    90         osgViewer::DistortionManipulator* distManip = new osgViewer::DistortionManipulator();
    91         distManip->setDistortionSet(_distortionSet);
    92         viewer.addEventHandler(distManip);
     87        viewer.addEventHandler(new osgViewer::DistortionManipulator(_distortionSet));
    9388
    9489    // add the state manipulator
Note: See TracChangeset for help on using the changeset viewer.