[346] | 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 | |
---|
[347] | 21 | #include<osgViewer/Viewer> |
---|
| 22 | |
---|
[346] | 23 | using namespace osg; |
---|
| 24 | using namespace osgViewer; |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | DistortionManipulator::DistortionManipulator() |
---|
| 28 | { |
---|
[348] | 29 | activeSetupMode = DISABLED; |
---|
| 30 | activeDistortionMode = MESH; |
---|
| 31 | activeManualSetupMode = DISTORTION; |
---|
| 32 | activeVisualizationMode = NONE; |
---|
[349] | 33 | |
---|
| 34 | _distortionSet = NULL; |
---|
[346] | 35 | } |
---|
| 36 | |
---|
| 37 | DistortionManipulator::~DistortionManipulator() |
---|
| 38 | { |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | void 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 | |
---|
[348] | 52 | bool DistortionManipulator::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa, osg::Object* obj, osg::NodeVisitor* nv) |
---|
[346] | 53 | { |
---|
[349] | 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 | //} |
---|
[347] | 60 | |
---|
| 61 | switch(ea.getEventType()) |
---|
[346] | 62 | { |
---|
[348] | 63 | case(osgGA::GUIEventAdapter::MOVE): break; |
---|
[346] | 64 | case(osgGA::GUIEventAdapter::DRAG): |
---|
| 65 | { |
---|
[349] | 66 | OSG_ALWAYS<<"DRAG!"<<std::endl; |
---|
[346] | 67 | break; |
---|
| 68 | } |
---|
| 69 | case(osgGA::GUIEventAdapter::PUSH): |
---|
[348] | 70 | { |
---|
| 71 | if(ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) |
---|
| 72 | { |
---|
[349] | 73 | OSG_ALWAYS<<"mouse click left!"<<std::endl; |
---|
[348] | 74 | } |
---|
| 75 | break; |
---|
| 76 | } |
---|
| 77 | case(osgGA::GUIEventAdapter::RELEASE): break; |
---|
[346] | 78 | case(osgGA::GUIEventAdapter::KEYDOWN): |
---|
[347] | 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 | { |
---|
[348] | 92 | switch(activeSetupMode) |
---|
| 93 | { |
---|
| 94 | case DISABLED : activeSetupMode = MANUAL; break; |
---|
| 95 | case MANUAL : activeSetupMode = DELEGATED; break; |
---|
| 96 | case DELEGATED : activeSetupMode = DISABLED; break; |
---|
| 97 | } |
---|
| 98 | |
---|
[349] | 99 | OSG_ALWAYS<<"KEY_KP_4 : Setup Mode now "<<activeSetupMode<<std::endl; |
---|
[347] | 100 | return(true); |
---|
| 101 | } |
---|
| 102 | if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Begin) // KP 5: MANUAL Mode: Toggle between blending & distortion setup. |
---|
| 103 | { |
---|
[348] | 104 | |
---|
| 105 | activeManualSetupMode = (activeManualSetupMode==DISTORTION?BLENDING:DISTORTION); |
---|
[349] | 106 | OSG_ALWAYS<<"KEY_KP_5 : activeManualSetupMode is now "<<activeManualSetupMode<<std::endl; |
---|
[347] | 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 | { |
---|
[348] | 111 | activeDistortionMode = (activeDistortionMode==MESH?TEXCOORDINATES:MESH); |
---|
[349] | 112 | OSG_ALWAYS<<"KEY_KP_6 : activeDistortionMode is now "<<activeDistortionMode<<std::endl; |
---|
[347] | 113 | return(true); |
---|
| 114 | } |
---|
| 115 | if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Home) // KP 7: Show distortion mesh / intensity map / none. |
---|
| 116 | { |
---|
[348] | 117 | switch(activeVisualizationMode) |
---|
| 118 | { |
---|
[349] | 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 | } |
---|
[348] | 140 | } |
---|
[349] | 141 | |
---|
| 142 | |
---|
| 143 | OSG_ALWAYS<<"KEY_KP_7 : activeVisualizationMode is now "<<activeVisualizationMode<<std::endl; |
---|
[347] | 144 | return(true); |
---|
| 145 | } |
---|
| 146 | if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Up) // KP 8: Save distortion set via plugin |
---|
| 147 | { |
---|
[349] | 148 | OSG_ALWAYS<<"KEY_KP_8 : todo: Save DistortionContainer"<<std::endl; |
---|
[347] | 149 | return(true); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | return false; // Event ignored |
---|
| 154 | } |
---|
[348] | 155 | case(osgGA::GUIEventAdapter::KEYUP): break; |
---|
[346] | 156 | case(osgGA::GUIEventAdapter::FRAME): |
---|
| 157 | { |
---|
[349] | 158 | //OSG_ALWAYS<<"FRAME!"<<std::endl; |
---|
[346] | 159 | break; |
---|
| 160 | } |
---|
| 161 | case (osgGA::GUIEventAdapter::RESIZE): // todo: adapt distortion mesh to new screen size |
---|
| 162 | { |
---|
[349] | 163 | OSG_ALWAYS<<"RESIZE!"<<std::endl; |
---|
[346] | 164 | break; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | default: |
---|
| 168 | return false; |
---|
| 169 | } |
---|
| 170 | return false; |
---|
[347] | 171 | } |
---|
| 172 | |
---|
| 173 | void DistortionManipulator::resetIntensityMap() |
---|
| 174 | { |
---|
[349] | 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(); |
---|
[347] | 195 | } |
---|
| 196 | |
---|
| 197 | void DistortionManipulator::resetDistortion() |
---|
| 198 | { |
---|
[349] | 199 | if(!_distortionSet.valid()) |
---|
| 200 | return; |
---|
| 201 | |
---|
| 202 | OSG_ALWAYS<<"ToDo: resetDistortion()"<<std::endl; |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | void 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 | |
---|
| 224 | void DistortionManipulator::showIntensityMap(bool show) |
---|
| 225 | { |
---|
| 226 | OSG_ALWAYS<<"ToDo: showIntensityMap(bool) is now "<<show<<std::endl; |
---|
| 227 | } |
---|