source: experimental/distortionNG/DistortionManipulator.cpp @ 357

Last change on this file since 357 was 357, checked in by Torben Dannhauer, 12 years ago
File size: 16.2 KB
Line 
1/* osgVisual test. distortionNG, experimental.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include "DistortionManipulator.h"
20
21#include<osgViewer/Viewer>
22#include<osg/Point>
23
24using namespace osg;
25using namespace osgViewer;
26
27
28DistortionManipulator::DistortionManipulator(DistortionSet* ds)
29 : _highlightColor( osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f) ), _distortionSet( ds )
30{
31        activeSetupMode = DISABLED;
32        activeDistortionMode = MESH;
33        activeManualSetupMode = DISTORTION;
34        activeVisualizationMode = NONE;
35
36        _highlighter = NULL;
37       
38        createVertexHighlighter();
39        createHUD();
40
41        _camera = 0;
42        //_distortionMesh(distortionMesh)
43}
44
45DistortionManipulator::~DistortionManipulator()
46{
47}
48
49void DistortionManipulator::getUsage(osg::ApplicationUsage& usage) const
50{
51        usage.addKeyboardMouseBinding("Keypad 0","Show/Hide distortion HUD.");
52        usage.addKeyboardMouseBinding("Keypad 1","Reset distortion.");
53        usage.addKeyboardMouseBinding("Keypad 2","Reset intensity blending.");
54        usage.addKeyboardMouseBinding("Keypad 4","Toggles Setup Mode between DISABLED, MANUAL & DELEGATED.");
55        usage.addKeyboardMouseBinding("Keypad 5","MANUAL Mode: Toggle between blending & distortion setup.");
56    usage.addKeyboardMouseBinding("Keypad 6","MANUAL Mode: Toggle if distortion drags affect mesh or rtt texture coordinates.");        // Defaults to Mesh
57        usage.addKeyboardMouseBinding("Keypad 7","Show distortion mesh / intensity map / none.");
58        usage.addKeyboardMouseBinding("Keypad 8","Save distortion set.");       // via plugin
59}
60
61bool DistortionManipulator::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa, osg::Object* obj, osg::NodeVisitor* nv)
62{
63        switch(ea.getEventType())
64    {
65        case(osgGA::GUIEventAdapter::MOVE): break;
66        case(osgGA::GUIEventAdapter::DRAG):
67                {
68                        if ( activeSetupMode == MANUAL)
69                        {
70                                OSG_ALWAYS<<std::endl<<"Drag:"<<std::endl<<"ea.getGraphicsContext()="<<ea.getGraphicsContext()<<std::endl;
71                                OSG_ALWAYS<<"ea.getXnormalized()="<<ea.getXnormalized()<<std::endl;
72                                OSG_ALWAYS<<"ea.getYnormalized()="<<ea.getYnormalized()<<std::endl;
73                                OSG_ALWAYS<<"ea.getX()="<<ea.getX()<<std::endl;
74                                OSG_ALWAYS<<"ea.getXin()="<<ea.getXmin()<<std::endl;
75                                OSG_ALWAYS<<"ea.getXmax()="<<ea.getXmax()<<std::endl;
76                                OSG_ALWAYS<<"ea.getY()="<<ea.getY()<<std::endl;
77                                OSG_ALWAYS<<"ea.getYin()="<<ea.getYmin()<<std::endl;
78                                OSG_ALWAYS<<"ea.getYmax()="<<ea.getYmax()<<std::endl;
79
80                                if(activeDistortionMode == MESH)
81                                {
82                                        OSG_ALWAYS<<"MESH!"<<std::endl;
83                                }
84                                else if (activeDistortionMode == TEXCOORDINATES)
85                                {
86                                        OSG_ALWAYS<<"TEXCOORDINATES!"<<std::endl;
87                                }
88
89                                return true;    // true means event handled: not forwarded to the camera manipulator;
90                        }
91                        break;
92
93                }
94        case(osgGA::GUIEventAdapter::PUSH):
95                {
96                        OSG_ALWAYS<<"mouse click!"<<std::endl;
97                        if ( activeSetupMode == MANUAL /*&& ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_CTRL*/ && ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON )
98                        {
99                                osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa);
100                                if ( viewer )
101                                {
102                                        osg::notify(osg::ALWAYS)<<std::endl<<"Intersection:"<<std::endl<<"ea.getX()="<<ea.getX()<<std::endl;
103                                        osg::notify(osg::ALWAYS)<<"ea.getY()="<<ea.getY()<<std::endl;
104                                        osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::PROJECTION, ea.getX(), ea.getY());
105                                        osgUtil::IntersectionVisitor iv( intersector.get() );
106                                        _distortionSet->getDistortionCamera()->accept( iv );
107                               
108                                        if ( intersector->containsIntersections() )
109                                        {
110                                                osgUtil::LineSegmentIntersector::Intersection& result = *(intersector->getIntersections().begin());
111                                                computeSelectedVertex( result );
112                                        }
113                                }
114                        }
115                        break;
116                }
117        case(osgGA::GUIEventAdapter::RELEASE): break;
118        case(osgGA::GUIEventAdapter::KEYDOWN):
119                {
120                        if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Insert) // KP 0: Show/Hide HUD
121                        {
122                                bool oldValue = _distortionSet->getDistortionInternals()->getValue(DistortionSet::HUD);
123                                _distortionSet->getDistortionInternals()->setValue(DistortionSet::HUD, !oldValue);
124                                return(true);
125                        }
126                        if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_End)    // KP 1: reset distortion
127                        {
128                                resetDistortion();
129                                return(true);
130                        }
131                        if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Down)   // KP 2: reset intensity map
132                        {
133                                resetIntensityMap();
134                                return(true);
135                        }
136                        if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Left)   // KP 4: Toggles Setup Mode between DISABLED, MANUAL & DELEGATED.
137                        {
138                                switch(activeSetupMode)
139                                {
140                                        case DISABLED : 
141                                        {
142                                                activeSetupMode = MANUAL;
143                                                OSG_NOTICE<<"SetupMode MANUAL activated"<<std::endl;
144                                                _distortionSet->getDistortionInternals()->setValue(DistortionSet::HIGHLIGHTER, true);
145                                                break;
146                                        }
147                                        case MANUAL :
148                                        {
149                                                activeSetupMode = DELEGATED;
150                                                _distortionSet->getDistortionInternals()->setValue(DistortionSet::HIGHLIGHTER, false);
151                                                OSG_NOTICE<<"SetupMode DELEGATED activated"<<std::endl;
152                                                break;
153                                        }
154                                        case DELEGATED :
155                                        {
156                                                activeSetupMode = DISABLED;
157                                                _distortionSet->getDistortionInternals()->setValue(DistortionSet::HIGHLIGHTER, false); 
158                                                OSG_NOTICE<<"SetupMode DISABLED activated"<<std::endl;
159                                                break;
160                                        }
161                                }
162                                updateHUD();
163                                return(true);
164                        }
165                        if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Begin)  // KP 5: MANUAL Mode: Toggle between blending & distortion setup.
166                        {
167
168                                activeManualSetupMode = (activeManualSetupMode==DISTORTION?BLENDING:DISTORTION);
169                                updateHUD();
170                                return(true);
171                        }
172                        if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Right)  // KP 6: MANUAL Mode: Toggle if distortion drags affect mesh or rtt texture coordinates -> defaults to Mesh
173                        {
174                                activeDistortionMode = (activeDistortionMode==MESH?TEXCOORDINATES:MESH);
175                                updateHUD();
176                                return(true);
177                        }
178                        if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Home)   // KP 7: Show distortion mesh / intensity map / none.
179                        {
180                                switch(activeVisualizationMode)
181                                {       
182                                        case DISTORTION_MESH : 
183                                        {
184                                                activeVisualizationMode = INTENSITY_MAP;
185                                                showDistortionMesh(true);
186                                                showIntensityMap(false);
187                                                break;
188                                        }
189                                        case INTENSITY_MAP :
190                                        {
191                                                activeVisualizationMode = NONE;
192                                                showDistortionMesh(false);
193                                                showIntensityMap(true);
194                                                break;
195                                        }
196                                        case NONE : 
197                                        {
198                                                activeVisualizationMode = DISTORTION_MESH;
199                                                showDistortionMesh(false);
200                                                showIntensityMap(false);
201                                                break;
202                                        }
203                                }
204
205                                updateHUD();
206                                return(true);
207                        }
208                        if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Up)     // KP 8: Save distortion set     via plugin
209                        {
210                                OSG_ALWAYS<<"KEY_KP_8 : todo: Save DistortionContainer"<<std::endl;
211                                return(true);
212                        }
213                       
214                        return false;   // Event ignored
215                }
216        case(osgGA::GUIEventAdapter::KEYUP): break;
217                case(osgGA::GUIEventAdapter::FRAME):
218                {
219                        if ( activeSetupMode == DELEGATED)
220                        {
221                                OSG_ALWAYS<<"Todo: Calling delegated class!"<<std::endl;
222                        }
223                        break;
224                }
225        case (osgGA::GUIEventAdapter::RESIZE):  // todo: adapt distortion mesh to new screen size
226                {
227                        OSG_ALWAYS<<"RESIZE!"<<std::endl;
228                        break;
229                }
230
231        default:
232            return false;
233    }
234    return false;
235}
236
237void DistortionManipulator::resetIntensityMap()
238{
239        if(!_distortionSet.valid())
240                return;
241
242        osg::ref_ptr<osg::Image> image = _distortionSet->getIntensityMap();
243
244        OSG_ALWAYS<<"Reseting IntensityMap Blending."<<std::endl;
245
246        if (!image->isDataContiguous())
247        {
248                OSG_WARN<<"Warning: DistortionManipulator does not support working with non contiguous imagery as blendmaps!"<<std::endl;
249                return;
250        }
251
252        // Fill intensity map with 255
253        unsigned char* dataPtr = image->data(); 
254        for(unsigned int i=0;i<image->getTotalSizeInBytes();i++)
255        {
256                *dataPtr++ = 255; 
257        }
258        image->dirty();
259}
260
261void DistortionManipulator::resetDistortion()
262{
263        if(!_distortionSet.valid())
264                return;
265
266        OSG_ALWAYS<<"ToDo: resetDistortion()"<<std::endl;
267}
268
269void DistortionManipulator::showDistortionMesh(bool show)
270{
271        OSG_ALWAYS<<"ToDo: showDistortionMesh(bool) is now "<<show<<std::endl;
272
273        // Todo: Stateset muss das von der MeshGeode sein.
274
275   /* osg::PolygonMode* polyModeObj = dynamic_cast<osg::PolygonMode*>(_stateset->getAttribute(osg::StateAttribute::POLYGONMODE));
276    if (!polyModeObj)
277    {
278        polyModeObj = new osg::PolygonMode;
279        _stateset->setAttribute(polyModeObj);
280    }
281 
282        if(show)
283                polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE);
284        else
285                polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::FILL);*/
286}
287
288void DistortionManipulator::showIntensityMap(bool show)
289{
290                OSG_ALWAYS<<"ToDo: showIntensityMap(bool) is now "<<show<<std::endl;
291
292                if(_distortionSet.valid())
293                        _distortionSet->setShowIntensityMapOnly(show);
294}
295
296void DistortionManipulator::createVertexHighlighter()
297{
298        osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array(1);
299        (*colors)[0] = _highlightColor;
300
301        _highlighter = new osg::Geometry;
302        _highlighter->setDataVariance( osg::Object::DYNAMIC );
303        _highlighter->setUseDisplayList( false );
304        _highlighter->setUseVertexBufferObjects( true );
305        _highlighter->setVertexArray( new osg::Vec3Array(1) );  // The highlighter vertex is updated by computeSelectedVertex(..)
306        _highlighter->setColorArray( colors.get() );
307        _highlighter->setColorBinding( osg::Geometry::BIND_OVERALL );
308        _highlighter->addPrimitiveSet( new osg::DrawArrays(GL_POINTS, 0, 1) );
309
310       
311        osg::ref_ptr<osg::Geode> geode = new osg::Geode;
312        geode->addDrawable( _highlighter.get() );
313        geode->getOrCreateStateSet()->setAttributeAndModes( new osg::Point(8.0f) );
314        geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
315        geode->setCullingActive(false); // disable the culling for the selector, otherwise the selector is culled away on the edge.
316
317        _distortionSet->getDistortionInternals()->addChild(geode, false);       //  = child #1  :definition: child #0 = mesh, #1 = highlighter, #2 HUD
318}
319
320void DistortionManipulator::createHUD()
321{
322        osg::ref_ptr<osg::Geode> geode = new osg::Geode;
323       
324        osg::StateSet* stateSet = geode->getOrCreateStateSet();
325        stateSet->setRenderBinDetails( 99, "RenderBin");
326        // disable depth test to ensure that it is always drawn.
327        stateSet->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
328        stateSet->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
329
330        // Add Text:
331        osg::Vec3 position(20.0f,120.0f,0.0f);
332        osg::Vec3 delta(0.0f,-25.0f,0.0f);
333        std::string timesFont("fonts/arial.ttf");
334        {
335                osg::ref_ptr<osgText::Text> textHeader = new osgText::Text;
336                textHeader->setText("Distortion Setup:");
337                textHeader->setFont(timesFont);
338                textHeader->setPosition(position);
339                textHeader->setCharacterSize(21);
340                textHeader->setDataVariance(osg::Object::STATIC); 
341                geode->addDrawable( textHeader );
342
343                position += delta;
344
345                hudSetupMode = new osgText::Text;
346                hudSetupMode->setFont(timesFont);
347                hudSetupMode->setPosition(position);
348                hudSetupMode->setCharacterSize(21);
349                hudSetupMode->setDataVariance(osg::Object::DYNAMIC); 
350                geode->addDrawable( hudSetupMode );
351
352                position += delta;
353
354                hudDistortionMode = new osgText::Text;
355                hudDistortionMode->setFont(timesFont);
356                hudDistortionMode->setPosition(position);
357                hudDistortionMode->setCharacterSize(21);
358                hudDistortionMode->setDataVariance(osg::Object::DYNAMIC); 
359                geode->addDrawable( hudDistortionMode );
360
361                position += delta;
362
363                hudManualSetupMode = new osgText::Text;
364                hudManualSetupMode->setFont(timesFont);
365                hudManualSetupMode->setPosition(position);
366                hudManualSetupMode->setCharacterSize(21);
367                hudManualSetupMode->setDataVariance(osg::Object::DYNAMIC); 
368                geode->addDrawable( hudManualSetupMode );
369
370                position += delta;
371
372                hudVisualizationMode = new osgText::Text;
373                hudVisualizationMode->setFont(timesFont);
374                hudVisualizationMode->setPosition(position);
375                hudVisualizationMode->setCharacterSize(21);
376                hudVisualizationMode->setDataVariance(osg::Object::DYNAMIC); 
377                geode->addDrawable( hudVisualizationMode );
378        }
379       
380        updateHUD();
381        _distortionSet->getDistortionInternals()->addChild(geode, false);       //  = child #2  :definition: child #0 = mesh, #1 = highlighter, #2 HUD
382}
383
384void DistortionManipulator::updateHUD()
385{
386        switch(activeSetupMode)
387        {
388                case DISABLED : hudSetupMode->setText("Setup Mode : DISABLED"); break;
389                case MANUAL : hudSetupMode->setText("Setup Mode : MANUAL"); break;
390                case DELEGATED : hudSetupMode->setText("Setup Mode : DELEGATED"); break;
391                default: hudSetupMode->setText("");
392        };
393        switch(activeDistortionMode)
394        {
395                case MESH : hudDistortionMode->setText("Distortion Mode : MESH"); break;
396                case TEXCOORDINATES : hudDistortionMode->setText("Distortion Mode : TEXCOORDINATES"); break;
397                default: hudDistortionMode->setText("");
398        };
399        switch(activeManualSetupMode)
400        {
401                case DISTORTION : hudManualSetupMode->setText("Manual Setup Mode : DISTORTION"); break;
402                case BLENDING : hudManualSetupMode->setText("Manual Setup Mode : BLENDING"); break;
403                default: hudManualSetupMode->setText("");
404        };
405        switch(activeVisualizationMode)
406        {
407                case DISTORTION_MESH : hudVisualizationMode->setText("Visualization Mode : DISTORTION_MESH"); break;
408                case INTENSITY_MAP : hudVisualizationMode->setText("Visualization Mode : INTENSITY_MAP"); break;
409                case NONE : hudVisualizationMode->setText("Visualization Mode : NONE"); break;
410                default: hudVisualizationMode->setText("");
411        };
412}
413
414void DistortionManipulator::computeSelectedVertex( osgUtil::LineSegmentIntersector::Intersection& result )
415{
416        osg::Geometry* geom = dynamic_cast<osg::Geometry*>( result.drawable.get() );
417        if ( !geom || geom==_highlighter )
418                return;
419
420        osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>( geom->getVertexArray() );
421        osg::Vec3Array* selVertices = dynamic_cast<osg::Vec3Array*>( _highlighter->getVertexArray() );
422        if ( !vertices || !selVertices )
423                return;
424       
425        OSG_NOTIFY(osg::ALWAYS)<<"size of vertices="<<vertices->size()<<std::endl;
426        OSG_NOTIFY(osg::ALWAYS)<<"size of selVertices="<<selVertices->size()<<std::endl;
427
428        osg::Vec3 point = result.getWorldIntersectPoint();
429        osg::Matrix matrix = osg::computeLocalToWorld( result.nodePath );       // To compute the intersection vertices in world coordinates not in model coordinates
430        OSG_NOTIFY(osg::ALWAYS) << "Intersection-indices: Size=" << result.indexList.size() << std::endl;
431        const std::vector<unsigned int>& selIndices = result.indexList;
432        {
433                double maxRatio = 0.0;
434                int closestVertexIndex = 0;
435                for ( unsigned int i=0; i<3 && i<result.ratioList.size(); i++ ) //iterate through rations and search for maxRation=nearestVertex
436                {
437                        if(result.ratioList[i] > maxRatio)
438                        {
439                                maxRatio = result.ratioList[i];
440                                closestVertexIndex = result.indexList[i];
441                        }
442                        OSG_NOTIFY(osg::ALWAYS)<<"maxRatio="<<maxRatio<<std::endl;
443                        OSG_NOTIFY(osg::ALWAYS)<<"closestVertexIndex="<<closestVertexIndex<<std::endl;
444                }
445                OSG_NOTIFY(osg::ALWAYS)<<"nearest vertex: X="<<(*vertices)[closestVertexIndex].x()<<" Y="<<(*vertices)[closestVertexIndex].y()<<" Z="<<(*vertices)[closestVertexIndex].z()<<std::endl;
446                osg::Vec3 vertex = (*vertices)[closestVertexIndex] * matrix;
447
448                selVertices->front() = vertex;         
449                OSG_NOTIFY(osg::ALWAYS)<<"selected vertice: X="<<vertex.x()<<" Y="<<vertex.y()<<" Z="<<vertex.z()<<std::endl;
450        }
451        selVertices->dirty();
452        _highlighter->dirtyBound(); 
453}
Note: See TracBrowser for help on using the repository browser.