source: experimental/distortionNG/DistortionManipulator.cpp @ 350

Last change on this file since 350 was 350, checked in by Torben Dannhauer, 12 years ago
File size: 7.3 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
23using namespace osg;
24using namespace osgViewer;
25
26
27DistortionManipulator::DistortionManipulator()
28{
29        activeSetupMode = DISABLED;
30        activeDistortionMode = MESH;
31        activeManualSetupMode = DISTORTION;
32        activeVisualizationMode = NONE;
33
34        _distortionSet = NULL;
35}
36
37DistortionManipulator::~DistortionManipulator()
38{
39}
40
41void DistortionManipulator::getUsage(osg::ApplicationUsage& usage) const
42{
43    usage.addKeyboardMouseBinding("Keypad 7","Show distortion mesh / intensity map / none.");
44        usage.addKeyboardMouseBinding("Keypad 8","Save distortion set.");       // via plugin
45        usage.addKeyboardMouseBinding("Keypad 4","Toggles Setup Mode between DISABLED, MANUAL & DELEGATED.");
46        usage.addKeyboardMouseBinding("Keypad 5","MANUAL Mode: Toggle between blending & distortion setup.");
47        usage.addKeyboardMouseBinding("Keypad 6","MANUAL Mode: Toggle if distortion drags affect mesh or rtt texture coordinates.");    // Defaults to Mesh
48        usage.addKeyboardMouseBinding("Keypad 1","Reset distortion.");
49        usage.addKeyboardMouseBinding("Keypad 2","Reset intensity blending.");
50}
51
52bool DistortionManipulator::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa, osg::Object* obj, osg::NodeVisitor* nv)
53{
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
61        switch(ea.getEventType())
62    {
63        case(osgGA::GUIEventAdapter::MOVE): break;
64        case(osgGA::GUIEventAdapter::DRAG):
65                {
66                        OSG_ALWAYS<<"DRAG!"<<std::endl;
67                        break;
68                }
69        case(osgGA::GUIEventAdapter::PUSH):
70                {
71                        if(ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)
72                        {
73                                OSG_ALWAYS<<"mouse click left!"<<std::endl;
74                        }
75                        break;
76                }
77        case(osgGA::GUIEventAdapter::RELEASE): break;
78        case(osgGA::GUIEventAdapter::KEYDOWN):
79                {
80                        if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_End)    // KP 1: reset distortion
81                        {
82                                resetDistortion();
83                                return(true);
84                        }
85                        if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Down)   // KP 2: reset intensity map
86                        {
87                                resetIntensityMap();
88                                return(true);
89                        }
90                        if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Left)   // KP 4: Toggles Setup Mode between DISABLED, MANUAL & DELEGATED.
91                        {
92                                switch(activeSetupMode)
93                                {
94                                        case DISABLED : activeSetupMode = MANUAL; break;
95                                        case MANUAL : activeSetupMode = DELEGATED; break;
96                                        case DELEGATED : activeSetupMode = DISABLED; break;
97                                }
98
99                                OSG_ALWAYS<<"KEY_KP_4 : Setup Mode now "<<activeSetupMode<<std::endl;
100                                return(true);
101                        }
102                        if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Begin)  // KP 5: MANUAL Mode: Toggle between blending & distortion setup.
103                        {
104
105                                activeManualSetupMode = (activeManualSetupMode==DISTORTION?BLENDING:DISTORTION);
106                                OSG_ALWAYS<<"KEY_KP_5 : activeManualSetupMode is now "<<activeManualSetupMode<<std::endl;
107                                return(true);
108                        }
109                        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
110                        {
111                                activeDistortionMode = (activeDistortionMode==MESH?TEXCOORDINATES:MESH);
112                                OSG_ALWAYS<<"KEY_KP_6 : activeDistortionMode is now "<<activeDistortionMode<<std::endl;
113                                return(true);
114                        }
115                        if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Home)   // KP 7: Show distortion mesh / intensity map / none.
116                        {
117                                switch(activeVisualizationMode)
118                                {       
119                                        case DISTORTION_MESH : 
120                                        {
121                                                activeVisualizationMode = INTENSITY_MAP;
122                                                showDistortionMesh(true);
123                                                showIntensityMap(false);
124                                                break;
125                                        }
126                                        case INTENSITY_MAP :
127                                        {
128                                                activeVisualizationMode = NONE;
129                                                showDistortionMesh(false);
130                                                showIntensityMap(true);
131                                                break;
132                                        }
133                                        case NONE : 
134                                        {
135                                                activeVisualizationMode = DISTORTION_MESH;
136                                                showDistortionMesh(false);
137                                                showIntensityMap(false);
138                                                break;
139                                        }
140                                }
141
142
143                                OSG_ALWAYS<<"KEY_KP_7 : activeVisualizationMode is now "<<activeVisualizationMode<<std::endl<<std::endl;
144                                return(true);
145                        }
146                        if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Up)     // KP 8: Save distortion set     via plugin
147                        {
148                                OSG_ALWAYS<<"KEY_KP_8 : todo: Save DistortionContainer"<<std::endl;
149                                return(true);
150                        }
151                       
152
153                        return false;   // Event ignored
154                }
155        case(osgGA::GUIEventAdapter::KEYUP): break;
156                case(osgGA::GUIEventAdapter::FRAME):
157                {
158                        //OSG_ALWAYS<<"FRAME!"<<std::endl;
159                        break;
160                }
161        case (osgGA::GUIEventAdapter::RESIZE):  // todo: adapt distortion mesh to new screen size
162                {
163                        OSG_ALWAYS<<"RESIZE!"<<std::endl;
164                        break;
165                }
166
167        default:
168            return false;
169    }
170    return false;
171}
172
173void DistortionManipulator::resetIntensityMap()
174{
175        if(!_distortionSet.valid())
176                return;
177
178        osg::ref_ptr<osg::Image> image = _distortionSet->getIntensityMap();
179
180        OSG_ALWAYS<<"Reseting IntensityMap Blending."<<std::endl;
181
182        if (!image->isDataContiguous())
183        {
184                OSG_WARN<<"Warning: DistortionManipulator does not support working with non contiguous imagery as blendmaps!"<<std::endl;
185                return;
186        }
187
188        // Fill intensity map with 255
189        unsigned char* dataPtr = image->data(); 
190        for(unsigned int i=0;i<image->getTotalSizeInBytes();i++)
191        {
192                *dataPtr++ = 255; 
193        }
194        image->dirty();
195}
196
197void DistortionManipulator::resetDistortion()
198{
199        if(!_distortionSet.valid())
200                return;
201
202        OSG_ALWAYS<<"ToDo: resetDistortion()"<<std::endl;
203}
204
205void DistortionManipulator::showDistortionMesh(bool show)
206{
207        OSG_ALWAYS<<"ToDo: showDistortionMesh(bool) is now "<<show<<std::endl;
208
209        // Todo: Stateset muss das von der MeshGeode sein.
210
211   /* osg::PolygonMode* polyModeObj = dynamic_cast<osg::PolygonMode*>(_stateset->getAttribute(osg::StateAttribute::POLYGONMODE));
212    if (!polyModeObj)
213    {
214        polyModeObj = new osg::PolygonMode;
215        _stateset->setAttribute(polyModeObj);
216    }
217 
218        if(show)
219                polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE);
220        else
221                polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::FILL);*/
222}
223
224void DistortionManipulator::showIntensityMap(bool show)
225{
226                OSG_ALWAYS<<"ToDo: showIntensityMap(bool) is now "<<show<<std::endl;
227
228                if(_distortionSet.valid())
229                        _distortionSet->setShowIntensityMapOnly(show);
230}
Note: See TracBrowser for help on using the repository browser.