source: experimental/distortionNG/distortionNG.cpp @ 339

Last change on this file since 339 was 339, checked in by Torben Dannhauer, 12 years ago

Mesh can now be triangle_strip or quad_strip

-> important to fix vertex selector working :)

File size: 5.7 KB
Line 
1#include "distortionNG.h"
2
3#include<osg/Point>
4
5
6#include<osgViewer/Viewer>
7
8
9
10distortionNG::distortionNG()
11{
12}
13
14distortionNG::~distortionNG()
15{
16}
17
18
19
20osg::Geode* distortionHandler::createVertexHighlighter()
21{
22        osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array(1);
23        (*colors)[0] = _highlightColor;
24
25        _highlighter = new osg::Geometry;
26        _highlighter->setDataVariance( osg::Object::DYNAMIC );
27        _highlighter->setUseDisplayList( false );
28        _highlighter->setUseVertexBufferObjects( true );
29        _highlighter->setVertexArray( new osg::Vec3Array(1) );  // The highlighter vertex is updated by computeSelectedVertex(..)
30        _highlighter->setColorArray( colors.get() );
31        _highlighter->setColorBinding( osg::Geometry::BIND_OVERALL );
32        _highlighter->addPrimitiveSet( new osg::DrawArrays(GL_POINTS, 0, 1) );
33
34        osg::ref_ptr<osg::Geode> geode = new osg::Geode;
35        geode->addDrawable( _highlighter.get() );
36        geode->getOrCreateStateSet()->setAttributeAndModes( new osg::Point(8.0f) );
37        geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
38
39        return geode.release();
40}
41
42bool distortionHandler::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa )
43{
44        // Only pressing CTRL activates distortion editing
45        if ( !(ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_CTRL) )
46                return false;   // False means not handled.
47
48
49    if ( ea.getEventType() == osgGA::GUIEventAdapter::PUSH && ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON )
50        {
51                osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa);
52                if ( viewer )
53                {
54                        osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, ea.getX(), ea.getY());
55                        osgUtil::IntersectionVisitor iv( intersector.get() );
56                        //viewer->getCamera()->accept( iv );
57                        _camera->accept( iv );
58               
59                        if ( intersector->containsIntersections() )
60                        {
61                                osgUtil::LineSegmentIntersector::Intersection& result = *(intersector->getIntersections().begin());
62                                computeSelectedVertex( result );
63                        }
64                }
65        }
66
67        if ( ea.getEventType() == osgGA::GUIEventAdapter::DRAG  )
68        {
69        osg::notify(osg::ALWAYS)<<std::endl<<"ea.getGraphicsContext()="<<ea.getGraphicsContext()<<std::endl;
70        osg::notify(osg::ALWAYS)<<"ea.getXnormalized()="<<ea.getXnormalized()<<std::endl;
71        osg::notify(osg::ALWAYS)<<"ea.getYnormalized()="<<ea.getYnormalized()<<std::endl;
72        osg::notify(osg::ALWAYS)<<"ea.getX()="<<ea.getX()<<std::endl;
73        osg::notify(osg::ALWAYS)<<"ea.getXin()="<<ea.getXmin()<<std::endl;
74        osg::notify(osg::ALWAYS)<<"ea.getXmax()="<<ea.getXmax()<<std::endl;
75        osg::notify(osg::ALWAYS)<<"ea.getY()="<<ea.getY()<<std::endl;
76        osg::notify(osg::ALWAYS)<<"ea.getYin()="<<ea.getYmin()<<std::endl;
77        osg::notify(osg::ALWAYS)<<"ea.getYmax()="<<ea.getYmax()<<std::endl;
78
79                return true;    // true means event handeld: not forwarded to the camera manipulator;
80        }
81
82
83    return false;       // False means not handled.
84}
85
86void distortionHandler::computeSelectedVertex( osgUtil::LineSegmentIntersector::Intersection& result )
87{
88        osg::Geometry* geom = dynamic_cast<osg::Geometry*>( result.drawable.get() );
89        if ( !geom || !_highlighter || geom==_highlighter )
90                return;
91
92        osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>( geom->getVertexArray() );
93        osg::Vec3Array* selVertices = dynamic_cast<osg::Vec3Array*>( _highlighter->getVertexArray() );
94        if ( !vertices || !selVertices )
95                return;
96       
97        OSG_NOTIFY(osg::ALWAYS)<<"size of vertices="<<vertices->size()<<std::endl;
98        OSG_NOTIFY(osg::ALWAYS)<<"size of selVertices="<<selVertices->size()<<std::endl;
99
100        osg::Vec3 point = result.getWorldIntersectPoint();
101        osg::Matrix matrix = osg::computeLocalToWorld( result.nodePath );       // To compute the intersection vertices in world coordinates not in model coordinates
102        OSG_NOTIFY(osg::ALWAYS) << "Intersection-indices: Size=" << result.indexList.size() << std::endl;
103        const std::vector<unsigned int>& selIndices = result.indexList;
104        {
105                double maxRatio = 0.0;
106                int closestVertexIndex = 0;
107                for ( unsigned int i=0; i<3 && i<result.ratioList.size(); i++ ) //iterate through rations and search for maxRation=nearestVertex
108                {
109                        if(result.ratioList[i] > maxRatio)
110                        {
111                                maxRatio = result.ratioList[i];
112                                closestVertexIndex = result.indexList[i];
113                        }
114                //      OSG_NOTIFY(osg::ALWAYS)<<"maxRatio="<<maxRatio<<std::endl;
115                //      OSG_NOTIFY(osg::ALWAYS)<<"closestVertexIndex="<<closestVertexIndex<<std::endl;
116                }
117                OSG_NOTIFY(osg::ALWAYS)<<"nearest vertex: X="<<(*vertices)[closestVertexIndex].x()<<" Y="<<(*vertices)[closestVertexIndex].y()<<" Z="<<(*vertices)[closestVertexIndex].z()<<std::endl;
118                osg::Vec3 vertex = (*vertices)[closestVertexIndex] * matrix;
119
120                selVertices->front() = vertex;         
121                OSG_NOTIFY(osg::ALWAYS)<<"selected vertice: X="<<vertex.x()<<" Y="<<vertex.y()<<" Z="<<vertex.z()<<std::endl;
122        }
123        selVertices->dirty();
124        _highlighter->dirtyBound(); 
125}
126
127/*
128        Grundsätzliche Gedanken Schritte:
129 Die Verzerrung kann auf zwei Arten realisiert werden
130        - Verzerrung der Textur-Koordinaten aus der RTT-Szene
131    - Verzerrung des Meshes im Verzerrungs-Rendern
132 *
133 Die IntensityMap kann folgendermaßen angewendet werden:
134 * Extrahierung der Coloran den Mesh-Nodes und anwendung als Color im Mesh node. Vorteil : Einfach. Nachteil: Helligkeiten werden zwischen Mesh-Nodes interpoliert: nur grobe Intensity Steuerung möglich
135 * Anwendung der Intensity-Textur pro Pixel: Verwenden eines Sharders um die Frament-Color mit der Farbe der Intensity-color zu multiplizieren
136 *
137 -> Evtl. sollte die Funktion der Intensitysteuerung aus den OSG-Distortion-Funktionen ausgeklammert und als eigene OSG-Schnittstelle/Funktion angestrebt werden.
138
139
140
141*/
Note: See TracBrowser for help on using the repository browser.