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 | #include "extViewer.h" |
---|
21 | |
---|
22 | #include <osgViewer/Viewer> |
---|
23 | #include <osg/Point> |
---|
24 | #include <osg/PolygonMode> |
---|
25 | #include <osg/TextureRectangle> |
---|
26 | |
---|
27 | #include <osgDB/Registry> |
---|
28 | #include <osgDB/ReaderWriter> |
---|
29 | #include <osgDB/WriteFile> |
---|
30 | |
---|
31 | #include "DistortionSetupStrategy.h" |
---|
32 | |
---|
33 | using namespace osg; |
---|
34 | using namespace osgViewer; |
---|
35 | |
---|
36 | |
---|
37 | DistortionManipulator::DistortionManipulator(extViewer* viewer, DistortionSet* ds) |
---|
38 | : _highlightColor( osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f) ), _distortionSet( ds ), _viewer(viewer) |
---|
39 | { |
---|
40 | activeSetupMode = DISABLED; |
---|
41 | activeDistortionMode = MESH; |
---|
42 | activeManualSetupMode = DISTORTION; |
---|
43 | activeVisualizationMode = NONE; |
---|
44 | |
---|
45 | _delegatedDistortionSetupStrategy = NULL; |
---|
46 | |
---|
47 | _highlighter = NULL; |
---|
48 | |
---|
49 | createVertexHighlighter(); |
---|
50 | createHUD(); |
---|
51 | |
---|
52 | _camera = 0; |
---|
53 | //_distortionMesh(distortionMesh) |
---|
54 | |
---|
55 | // Create Shader to vizualize intensitymap during blending setup. |
---|
56 | osg::Shader* sh = osg::Shader::readShaderFile( osg::Shader::FRAGMENT, "shaderIntensityMapVis.frag" ); |
---|
57 | sh->setName("shaderIntensityMapVis"); |
---|
58 | ds->setShaderIntensityMapVis( sh ); |
---|
59 | |
---|
60 | //LF Get Screen Resolution for Cursor Position Scaling |
---|
61 | osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); |
---|
62 | if (!wsi) { osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl; } |
---|
63 | wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), _screenPixWidth, _screenPixHeight); |
---|
64 | |
---|
65 | |
---|
66 | } |
---|
67 | |
---|
68 | DistortionManipulator::~DistortionManipulator() |
---|
69 | { |
---|
70 | } |
---|
71 | |
---|
72 | void DistortionManipulator::getUsage(osg::ApplicationUsage& usage) const |
---|
73 | { |
---|
74 | usage.addKeyboardMouseBinding("Keypad 0","Show/Hide distortion HUD."); |
---|
75 | usage.addKeyboardMouseBinding("Keypad 1","Reset distortion."); |
---|
76 | usage.addKeyboardMouseBinding("Keypad 2","Reset intensity blending."); |
---|
77 | usage.addKeyboardMouseBinding("Keypad 4","Toggles Setup Mode between DISABLED, MANUAL & DELEGATED."); |
---|
78 | usage.addKeyboardMouseBinding("Keypad 5","MANUAL Mode: Toggle between blending & distortion setup."); |
---|
79 | usage.addKeyboardMouseBinding("Keypad 6","MANUAL Mode: Toggle if distortion drags affect mesh or rtt texture coordinates."); // Defaults to Mesh |
---|
80 | usage.addKeyboardMouseBinding("Keypad 7","Show distortion mesh / intensity map / none."); |
---|
81 | usage.addKeyboardMouseBinding("Keypad 8","Save distortion set."); // via plugin |
---|
82 | } |
---|
83 | |
---|
84 | bool DistortionManipulator::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa, osg::Object* obj, osg::NodeVisitor* nv) |
---|
85 | { |
---|
86 | |
---|
87 | |
---|
88 | switch(ea.getEventType()) |
---|
89 | { |
---|
90 | case(osgGA::GUIEventAdapter::MOVE): break; |
---|
91 | case(osgGA::GUIEventAdapter::DRAG): |
---|
92 | { |
---|
93 | if ( activeSetupMode == MANUAL) |
---|
94 | { |
---|
95 | OSG_ALWAYS<<std::endl<<"Drag:"<<std::endl<<"ea.getGraphicsContext()="<<ea.getGraphicsContext()<<std::endl; |
---|
96 | OSG_ALWAYS<<"ea.getXnormalized()="<<ea.getXnormalized()<<std::endl; |
---|
97 | OSG_ALWAYS<<"ea.getYnormalized()="<<ea.getYnormalized()<<std::endl; |
---|
98 | OSG_ALWAYS<<"ea.getX()="<<ea.getX()<<std::endl; |
---|
99 | OSG_ALWAYS<<"ea.getXin()="<<ea.getXmin()<<std::endl; |
---|
100 | OSG_ALWAYS<<"ea.getXmax()="<<ea.getXmax()<<std::endl; |
---|
101 | OSG_ALWAYS<<"ea.getY()="<<ea.getY()<<std::endl; |
---|
102 | OSG_ALWAYS<<"ea.getYin()="<<ea.getYmin()<<std::endl; |
---|
103 | OSG_ALWAYS<<"ea.getYmax()="<<ea.getYmax()<<std::endl; |
---|
104 | |
---|
105 | //LF get X,Y normalized to lowerleftcorner (0,0) / upperrightcorner (1,1) |
---|
106 | OSG_ALWAYS<<"getXnormalizedLF()="<<(ea.getXnormalized()+1.0)/2.0<<std::endl; |
---|
107 | OSG_ALWAYS<<"getYnormalizedLF()="<<(ea.getYnormalized()+1.0)/2.0<<std::endl; |
---|
108 | |
---|
109 | //LF get X,Y in pixels (lowerleftcorner (0,0) / upperrightcorner (screenwidth,screenheigth) |
---|
110 | OSG_ALWAYS<<std::endl<<"Screen Resolution: "<<_screenPixWidth<<" x "<<_screenPixHeight<<std::endl; |
---|
111 | OSG_ALWAYS<<"getXPixelspaceLF()="<<(int)((float)_screenPixWidth*(ea.getXnormalized()+1.0)/2.0)<<std::endl; |
---|
112 | OSG_ALWAYS<<"getYPixelspaceLF()="<<(int)((float)_screenPixHeight*(ea.getYnormalized()+1.0)/2.0)<<std::endl; |
---|
113 | |
---|
114 | if(activeDistortionMode == MESH) |
---|
115 | { |
---|
116 | OSG_ALWAYS<<"MESH!"<<std::endl; |
---|
117 | } |
---|
118 | else if (activeDistortionMode == TEXCOORDINATES) |
---|
119 | { |
---|
120 | OSG_ALWAYS<<"TEXCOORDINATES!"<<std::endl; |
---|
121 | } |
---|
122 | |
---|
123 | return true; // true means event handled: not forwarded to the camera manipulator; |
---|
124 | } |
---|
125 | break; |
---|
126 | |
---|
127 | } |
---|
128 | case(osgGA::GUIEventAdapter::PUSH): |
---|
129 | { |
---|
130 | OSG_ALWAYS<<"mouse click!"<<std::endl; |
---|
131 | if ( activeSetupMode == MANUAL /*&& ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_CTRL*/ && ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON ) |
---|
132 | { |
---|
133 | osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa); |
---|
134 | if ( viewer ) |
---|
135 | { |
---|
136 | osg::notify(osg::ALWAYS)<<std::endl<<"Intersection:"<<std::endl<<"ea.getX()="<<ea.getX()<<std::endl; |
---|
137 | osg::notify(osg::ALWAYS)<<"ea.getY()="<<ea.getY()<<std::endl; |
---|
138 | osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::PROJECTION, ea.getX(), ea.getY()); |
---|
139 | osgUtil::IntersectionVisitor iv( intersector.get() ); |
---|
140 | _distortionSet->getDistortionCamera()->accept( iv ); |
---|
141 | |
---|
142 | if ( intersector->containsIntersections() ) |
---|
143 | { |
---|
144 | //osgUtil::LineSegmentIntersector::Intersection& result = *(intersector->getIntersections().begin()); |
---|
145 | osgUtil::LineSegmentIntersector::Intersection *result; |
---|
146 | result=&intersector->getFirstIntersection(); |
---|
147 | computeSelectedVertex( *result ); |
---|
148 | //computeSelectedVertex( result ); |
---|
149 | } |
---|
150 | } |
---|
151 | } |
---|
152 | break; |
---|
153 | } |
---|
154 | case(osgGA::GUIEventAdapter::RELEASE): break; |
---|
155 | case(osgGA::GUIEventAdapter::KEYDOWN): |
---|
156 | { |
---|
157 | if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Insert) // KP 0: Show/Hide HUD |
---|
158 | { |
---|
159 | bool oldValue = _distortionSet->getDistortionInternals()->getValue(DistortionSet::HUD); |
---|
160 | _distortionSet->getDistortionInternals()->setValue(DistortionSet::HUD, !oldValue); |
---|
161 | return(true); |
---|
162 | } |
---|
163 | if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_End) // KP 1: reset distortion |
---|
164 | { |
---|
165 | resetDistortion(); |
---|
166 | return(true); |
---|
167 | } |
---|
168 | if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Down) // KP 2: reset intensity map |
---|
169 | { |
---|
170 | resetIntensityMap(); |
---|
171 | return(true); |
---|
172 | } |
---|
173 | if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Left) // KP 4: Toggles Setup Mode between DISABLED, MANUAL & DELEGATED. |
---|
174 | { |
---|
175 | switch(activeSetupMode) |
---|
176 | { |
---|
177 | case DISABLED : |
---|
178 | { |
---|
179 | activeSetupMode = MANUAL; |
---|
180 | OSG_NOTICE<<"SetupMode MANUAL activated"<<std::endl; |
---|
181 | _distortionSet->getDistortionInternals()->setValue(DistortionSet::HIGHLIGHTER, true); |
---|
182 | break; |
---|
183 | } |
---|
184 | case MANUAL : |
---|
185 | { |
---|
186 | activeSetupMode = DELEGATED; |
---|
187 | _distortionSet->getDistortionInternals()->setValue(DistortionSet::HIGHLIGHTER, false); |
---|
188 | OSG_NOTICE<<"SetupMode DELEGATED activated"<<std::endl; |
---|
189 | break; |
---|
190 | } |
---|
191 | case DELEGATED : |
---|
192 | { |
---|
193 | activeSetupMode = DISABLED; |
---|
194 | _distortionSet->getDistortionInternals()->setValue(DistortionSet::HIGHLIGHTER, false); |
---|
195 | OSG_NOTICE<<"SetupMode DISABLED activated"<<std::endl; |
---|
196 | break; |
---|
197 | } |
---|
198 | } |
---|
199 | updateHUD(); |
---|
200 | return(true); |
---|
201 | } |
---|
202 | if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Begin) // KP 5: MANUAL Mode: Toggle between blending & distortion setup. |
---|
203 | { |
---|
204 | |
---|
205 | activeManualSetupMode = (activeManualSetupMode==DISTORTION?BLENDING:DISTORTION); |
---|
206 | updateHUD(); |
---|
207 | return(true); |
---|
208 | } |
---|
209 | 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 |
---|
210 | { |
---|
211 | activeDistortionMode = (activeDistortionMode==MESH?TEXCOORDINATES:MESH); |
---|
212 | updateHUD(); |
---|
213 | return(true); |
---|
214 | } |
---|
215 | if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Home) // KP 7: Show distortion mesh / intensity map / none. |
---|
216 | { |
---|
217 | switch(activeVisualizationMode) |
---|
218 | { |
---|
219 | case DISTORTION_MESH : |
---|
220 | { |
---|
221 | activeVisualizationMode = INTENSITY_MAP; |
---|
222 | showDistortionMesh(false); |
---|
223 | showIntensityMap(true); |
---|
224 | break; |
---|
225 | } |
---|
226 | case INTENSITY_MAP : |
---|
227 | { |
---|
228 | activeVisualizationMode = NONE; |
---|
229 | showDistortionMesh(false); |
---|
230 | showIntensityMap(false); |
---|
231 | break; |
---|
232 | } |
---|
233 | case NONE : |
---|
234 | { |
---|
235 | activeVisualizationMode = DISTORTION_MESH; |
---|
236 | showDistortionMesh(true); |
---|
237 | showIntensityMap(false); |
---|
238 | break; |
---|
239 | } |
---|
240 | } |
---|
241 | |
---|
242 | updateHUD(); |
---|
243 | return(true); |
---|
244 | } |
---|
245 | if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Up) // KP 8: Save distortion set via plugin |
---|
246 | { |
---|
247 | OSG_ALWAYS<<"KEY_KP_8 : todo: Save DistortionContainer"<<std::endl; |
---|
248 | |
---|
249 | osgDB::writeObjectFile( *_distortionSet, "distortionset.dist" ); |
---|
250 | return(true); |
---|
251 | } |
---|
252 | |
---|
253 | return false; // Event ignored |
---|
254 | } |
---|
255 | case(osgGA::GUIEventAdapter::KEYUP): break; |
---|
256 | case(osgGA::GUIEventAdapter::FRAME): |
---|
257 | { |
---|
258 | if ( activeSetupMode == DELEGATED && _delegatedDistortionSetupStrategy.valid()) |
---|
259 | { |
---|
260 | //OSG_ALWAYS<<"Calling delegated class.."<<std::endl; |
---|
261 | _delegatedDistortionSetupStrategy->delegateDistortionSetup(_distortionSet); |
---|
262 | if( _distortionSet->isDirty() && _viewer.valid()) |
---|
263 | { |
---|
264 | updateDistortionSetup(); |
---|
265 | } |
---|
266 | //OSG_ALWAYS<<"..done"<<std::endl; |
---|
267 | } |
---|
268 | break; |
---|
269 | } |
---|
270 | case (osgGA::GUIEventAdapter::RESIZE): // todo: adapt distortion mesh to new screen size |
---|
271 | { |
---|
272 | OSG_ALWAYS<<"RESIZE!"<<std::endl; |
---|
273 | break; |
---|
274 | } |
---|
275 | |
---|
276 | default: |
---|
277 | return false; |
---|
278 | } |
---|
279 | return false; |
---|
280 | } |
---|
281 | |
---|
282 | void DistortionManipulator::resetIntensityMap() |
---|
283 | { |
---|
284 | if(!_distortionSet.valid()) |
---|
285 | return; |
---|
286 | |
---|
287 | osg::ref_ptr<osg::Image> image = _distortionSet->getIntensityMap(); |
---|
288 | |
---|
289 | OSG_ALWAYS<<"Reseting IntensityMap Blending."<<std::endl; |
---|
290 | |
---|
291 | if (!image->isDataContiguous()) |
---|
292 | { |
---|
293 | OSG_WARN<<"Warning: DistortionManipulator does not support working with non contiguous imagery as blendmaps!"<<std::endl; |
---|
294 | return; |
---|
295 | } |
---|
296 | |
---|
297 | // Fill intensity map with 255 |
---|
298 | unsigned char* dataPtr = image->data(); |
---|
299 | for(unsigned int i=0;i<image->getTotalSizeInBytes();i++) |
---|
300 | { |
---|
301 | *dataPtr++ = 255; |
---|
302 | } |
---|
303 | image->dirty(); |
---|
304 | } |
---|
305 | |
---|
306 | void DistortionManipulator::resetDistortion() |
---|
307 | { |
---|
308 | if(!_distortionSet.valid()) |
---|
309 | return; |
---|
310 | |
---|
311 | OSG_ALWAYS<<"ToDo: resetDistortion()"<<std::endl; |
---|
312 | } |
---|
313 | |
---|
314 | void DistortionManipulator::showDistortionMesh(bool show) |
---|
315 | { |
---|
316 | osg::StateSet* stateset = _distortionSet->getDistortionInternals()->getChild(DistortionSet::MESH)->getOrCreateStateSet(); |
---|
317 | |
---|
318 | // Append a PolygonMode stateset if required |
---|
319 | osg::PolygonMode* polyModeObj = dynamic_cast<osg::PolygonMode*>(stateset->getAttribute(osg::StateAttribute::POLYGONMODE)); |
---|
320 | if (!polyModeObj) |
---|
321 | { |
---|
322 | polyModeObj = new osg::PolygonMode; |
---|
323 | stateset->setAttribute(polyModeObj, osg::StateAttribute::PROTECTED|osg::StateAttribute::ON); |
---|
324 | } |
---|
325 | |
---|
326 | // Set Polygonmode |
---|
327 | if(show) |
---|
328 | polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE); |
---|
329 | else |
---|
330 | polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::FILL); |
---|
331 | } |
---|
332 | |
---|
333 | void DistortionManipulator::showIntensityMap(bool show) |
---|
334 | { |
---|
335 | if(_distortionSet.valid()) |
---|
336 | { |
---|
337 | osg::StateSet* stateset = _distortionSet->getDistortionInternals()->getChild(DistortionSet::MESH)->getOrCreateStateSet(); |
---|
338 | osg::Program* program = static_cast<osg::Program*>(stateset->getAttribute(::osg::StateAttribute::PROGRAM)); |
---|
339 | |
---|
340 | if(show) |
---|
341 | { |
---|
342 | program->removeShader( _distortionSet->getShaderIntensityMap() ); |
---|
343 | program->addShader( _distortionSet->getShaderIntensityMapVis() ); |
---|
344 | } |
---|
345 | else |
---|
346 | { |
---|
347 | program->removeShader( _distortionSet->getShaderIntensityMapVis() ); |
---|
348 | program->addShader( _distortionSet->getShaderIntensityMap() ); |
---|
349 | } |
---|
350 | } |
---|
351 | } |
---|
352 | |
---|
353 | void DistortionManipulator::createVertexHighlighter() |
---|
354 | { |
---|
355 | osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array(1); |
---|
356 | (*colors)[0] = _highlightColor; |
---|
357 | |
---|
358 | _highlighter = new osg::Geometry; |
---|
359 | _highlighter->setDataVariance( osg::Object::DYNAMIC ); |
---|
360 | _highlighter->setUseDisplayList( false ); |
---|
361 | _highlighter->setUseVertexBufferObjects( true ); |
---|
362 | _highlighter->setVertexArray( new osg::Vec3Array(1) ); // The highlighter vertex is updated by computeSelectedVertex(..) |
---|
363 | _highlighter->setColorArray( colors.get() ); |
---|
364 | _highlighter->setColorBinding( osg::Geometry::BIND_OVERALL ); |
---|
365 | _highlighter->addPrimitiveSet( new osg::DrawArrays(GL_POINTS, 0, 1) ); |
---|
366 | |
---|
367 | |
---|
368 | osg::ref_ptr<osg::Geode> geode = new osg::Geode; |
---|
369 | geode->addDrawable( _highlighter.get() ); |
---|
370 | geode->getOrCreateStateSet()->setAttributeAndModes( new osg::Point(8.0f) ); |
---|
371 | geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF ); |
---|
372 | geode->setCullingActive(false); // disable the culling for the selector, otherwise the selector is culled away on the edge. |
---|
373 | |
---|
374 | _distortionSet->getDistortionInternals()->addChild(geode, false); // = child #1 :definition: child #0 = mesh, #1 = highlighter, #2 HUD |
---|
375 | } |
---|
376 | |
---|
377 | void DistortionManipulator::createHUD() |
---|
378 | { |
---|
379 | osg::ref_ptr<osg::Geode> geode = new osg::Geode; |
---|
380 | |
---|
381 | osg::StateSet* stateSet = geode->getOrCreateStateSet(); |
---|
382 | stateSet->setRenderBinDetails( 99, "RenderBin"); |
---|
383 | // disable depth test to ensure that it is always drawn. |
---|
384 | stateSet->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF); |
---|
385 | stateSet->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
---|
386 | |
---|
387 | // Modify StateSet to protect it against state changes by the stateset Manipulator |
---|
388 | osg::PolygonMode* polyModeObj = new osg::PolygonMode; |
---|
389 | stateSet->setAttribute(polyModeObj, osg::StateAttribute::PROTECTED|osg::StateAttribute::ON); |
---|
390 | |
---|
391 | // Add Text: |
---|
392 | osg::Vec3 position(20.0f,120.0f,0.0f); |
---|
393 | osg::Vec3 delta(0.0f,-25.0f,0.0f); |
---|
394 | std::string timesFont("fonts/arial.ttf"); |
---|
395 | { |
---|
396 | osg::ref_ptr<osgText::Text> textHeader = new osgText::Text; |
---|
397 | textHeader->setText("Distortion Setup:"); |
---|
398 | textHeader->setFont(timesFont); |
---|
399 | textHeader->setPosition(position); |
---|
400 | textHeader->setCharacterSize(21); |
---|
401 | textHeader->setDataVariance(osg::Object::STATIC); |
---|
402 | geode->addDrawable( textHeader ); |
---|
403 | |
---|
404 | position += delta; |
---|
405 | |
---|
406 | hudSetupMode = new osgText::Text; |
---|
407 | hudSetupMode->setFont(timesFont); |
---|
408 | hudSetupMode->setPosition(position); |
---|
409 | hudSetupMode->setCharacterSize(21); |
---|
410 | hudSetupMode->setDataVariance(osg::Object::DYNAMIC); |
---|
411 | geode->addDrawable( hudSetupMode ); |
---|
412 | |
---|
413 | position += delta; |
---|
414 | |
---|
415 | hudDistortionMode = new osgText::Text; |
---|
416 | hudDistortionMode->setFont(timesFont); |
---|
417 | hudDistortionMode->setPosition(position); |
---|
418 | hudDistortionMode->setCharacterSize(21); |
---|
419 | hudDistortionMode->setDataVariance(osg::Object::DYNAMIC); |
---|
420 | geode->addDrawable( hudDistortionMode ); |
---|
421 | |
---|
422 | position += delta; |
---|
423 | |
---|
424 | hudManualSetupMode = new osgText::Text; |
---|
425 | hudManualSetupMode->setFont(timesFont); |
---|
426 | hudManualSetupMode->setPosition(position); |
---|
427 | hudManualSetupMode->setCharacterSize(21); |
---|
428 | hudManualSetupMode->setDataVariance(osg::Object::DYNAMIC); |
---|
429 | geode->addDrawable( hudManualSetupMode ); |
---|
430 | |
---|
431 | position += delta; |
---|
432 | |
---|
433 | hudVisualizationMode = new osgText::Text; |
---|
434 | hudVisualizationMode->setFont(timesFont); |
---|
435 | hudVisualizationMode->setPosition(position); |
---|
436 | hudVisualizationMode->setCharacterSize(21); |
---|
437 | hudVisualizationMode->setDataVariance(osg::Object::DYNAMIC); |
---|
438 | geode->addDrawable( hudVisualizationMode ); |
---|
439 | } |
---|
440 | |
---|
441 | updateHUD(); |
---|
442 | _distortionSet->getDistortionInternals()->addChild(geode, false); // = child #2. The definition is: child #0 = mesh, #1 = highlighter, #2 HUD |
---|
443 | } |
---|
444 | |
---|
445 | void DistortionManipulator::updateHUD() |
---|
446 | { |
---|
447 | switch(activeSetupMode) |
---|
448 | { |
---|
449 | case DISABLED : hudSetupMode->setText("Setup Mode : DISABLED"); break; |
---|
450 | case MANUAL : hudSetupMode->setText("Setup Mode : MANUAL"); break; |
---|
451 | case DELEGATED : hudSetupMode->setText("Setup Mode : DELEGATED"); break; |
---|
452 | default: hudSetupMode->setText(""); |
---|
453 | }; |
---|
454 | switch(activeDistortionMode) |
---|
455 | { |
---|
456 | case MESH : hudDistortionMode->setText("Distortion Mode : MESH"); break; |
---|
457 | case TEXCOORDINATES : hudDistortionMode->setText("Distortion Mode : TEXCOORDINATES"); break; |
---|
458 | default: hudDistortionMode->setText(""); |
---|
459 | }; |
---|
460 | switch(activeManualSetupMode) |
---|
461 | { |
---|
462 | case DISTORTION : hudManualSetupMode->setText("Manual Setup Mode : DISTORTION"); break; |
---|
463 | case BLENDING : hudManualSetupMode->setText("Manual Setup Mode : BLENDING"); break; |
---|
464 | default: hudManualSetupMode->setText(""); |
---|
465 | }; |
---|
466 | switch(activeVisualizationMode) |
---|
467 | { |
---|
468 | case DISTORTION_MESH : hudVisualizationMode->setText("Visualization Mode : DISTORTION_MESH"); break; |
---|
469 | case INTENSITY_MAP : hudVisualizationMode->setText("Visualization Mode : INTENSITY_MAP"); break; |
---|
470 | case NONE : hudVisualizationMode->setText("Visualization Mode : NONE"); break; |
---|
471 | default: hudVisualizationMode->setText(""); |
---|
472 | }; |
---|
473 | } |
---|
474 | |
---|
475 | void DistortionManipulator::computeSelectedVertex( osgUtil::LineSegmentIntersector::Intersection& result ) |
---|
476 | { |
---|
477 | osg::Geometry* geom = dynamic_cast<osg::Geometry*>( result.drawable.get() ); |
---|
478 | if ( !geom || geom==_highlighter ) |
---|
479 | return; |
---|
480 | |
---|
481 | osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>( geom->getVertexArray() ); |
---|
482 | osg::Vec3Array* selVertices = dynamic_cast<osg::Vec3Array*>( _highlighter->getVertexArray() ); |
---|
483 | if ( !vertices || !selVertices ) |
---|
484 | return; |
---|
485 | |
---|
486 | OSG_NOTIFY(osg::ALWAYS)<<"size of vertices="<<vertices->size()<<std::endl; |
---|
487 | OSG_NOTIFY(osg::ALWAYS)<<"size of selVertices="<<selVertices->size()<<std::endl; |
---|
488 | |
---|
489 | osg::Vec3 point = result.getWorldIntersectPoint(); |
---|
490 | osg::Matrix matrix = osg::computeLocalToWorld( result.nodePath ); // To compute the intersection vertices in world coordinates not in model coordinates |
---|
491 | OSG_NOTIFY(osg::ALWAYS) << "Intersection-indices: Size=" << result.indexList.size() << std::endl; |
---|
492 | const std::vector<unsigned int>& selIndices = result.indexList; |
---|
493 | { |
---|
494 | double maxRatio = 0.0; |
---|
495 | int closestVertexIndex = 0; |
---|
496 | for ( unsigned int i=0; i<3 && i<result.ratioList.size(); i++ ) //iterate through rations and search for maxRation=nearestVertex |
---|
497 | { |
---|
498 | if(result.ratioList[i] > maxRatio) |
---|
499 | { |
---|
500 | maxRatio = result.ratioList[i]; |
---|
501 | closestVertexIndex = result.indexList[i]; |
---|
502 | } |
---|
503 | OSG_NOTIFY(osg::ALWAYS)<<"maxRatio="<<maxRatio<<std::endl; |
---|
504 | OSG_NOTIFY(osg::ALWAYS)<<"closestVertexIndex="<<closestVertexIndex<<std::endl; |
---|
505 | } |
---|
506 | OSG_NOTIFY(osg::ALWAYS)<<"nearest vertex: X="<<(*vertices)[closestVertexIndex].x()<<" Y="<<(*vertices)[closestVertexIndex].y()<<" Z="<<(*vertices)[closestVertexIndex].z()<<std::endl; |
---|
507 | osg::Vec3 vertex = (*vertices)[closestVertexIndex] * matrix; |
---|
508 | |
---|
509 | selVertices->front() = vertex; |
---|
510 | OSG_NOTIFY(osg::ALWAYS)<<"selected vertice: X="<<vertex.x()<<" Y="<<vertex.y()<<" Z="<<vertex.z()<<std::endl; |
---|
511 | } |
---|
512 | selVertices->dirty(); |
---|
513 | _highlighter->dirtyBound(); |
---|
514 | } |
---|
515 | |
---|
516 | void DistortionManipulator::updateDistortionSetup() |
---|
517 | { |
---|
518 | updateDistortionViewProjectionMatrix(); |
---|
519 | updateDistortionMesh(); |
---|
520 | _distortionSet->clearDirtyStatus(); |
---|
521 | } |
---|
522 | |
---|
523 | void DistortionManipulator::updateDistortionViewProjectionMatrix() |
---|
524 | { |
---|
525 | if(!_distortionSet.valid()) |
---|
526 | { |
---|
527 | osg::notify(osg::WARN) << "You cannot update the distortion without distortion set up." << std::endl; |
---|
528 | return; |
---|
529 | } |
---|
530 | |
---|
531 | if(_viewer.valid()) |
---|
532 | { |
---|
533 | _viewer->getCamera()->setViewMatrix(_distortionSet->getViewOffset()); |
---|
534 | _viewer->getCamera()->setProjectionMatrix(_distortionSet->getProjectionOffset()); |
---|
535 | } |
---|
536 | else |
---|
537 | OSG_NOTIFY(osg::ALWAYS)<<"Invalid viewer!"<<std::endl; |
---|
538 | } |
---|
539 | |
---|
540 | void DistortionManipulator::updateDistortionMesh() |
---|
541 | { |
---|
542 | // Create mesh vertex array & textCoord array |
---|
543 | osg::Vec3Array* vertices = new osg::Vec3Array; |
---|
544 | osg::Vec2Array* texCoords = new osg::Vec2Array; |
---|
545 | |
---|
546 | for(unsigned int i=0;i<_distortionSet->getDistortionMesh()->size();i++) |
---|
547 | { |
---|
548 | // Scale vector with the screen resolution |
---|
549 | osg::Vec3 tmpVec = osg::Vec3( _distortionSet->getDistortionMesh()->at(i).x()*_screenPixWidth, |
---|
550 | _distortionSet->getDistortionMesh()->at(i).y()*_screenPixHeight, |
---|
551 | 0.0 ); |
---|
552 | vertices->push_back( tmpVec ); |
---|
553 | OSG_ALWAYS<<"X:"<<tmpVec.x()<<" Y:"<<tmpVec.y()<<" Z:"<<tmpVec.z()<<std::endl; |
---|
554 | |
---|
555 | // Note texCoords |
---|
556 | osg::Vec2 tmpTexCoords = osg::Vec2( _distortionSet->getDistortionMesh()->at(i).z()*_screenPixWidth, |
---|
557 | _distortionSet->getDistortionMesh()->at(i).w()*_screenPixHeight ); |
---|
558 | texCoords->push_back(tmpTexCoords); |
---|
559 | } |
---|
560 | |
---|
561 | // Apply arrays |
---|
562 | _distortionSet->getDistortionInternals()->getChild(osgViewer::DistortionSet::MESH)->asGeode()->getDrawable(0)->asGeometry()->setVertexArray(vertices); |
---|
563 | _distortionSet->getDistortionInternals()->getChild(osgViewer::DistortionSet::MESH)->asGeode()->getDrawable(0)->asGeometry()->setTexCoordArray(0, texCoords); // todo: 0 ist hardcoded und müsste durch getTexUnitScene() ersetzt werden |
---|
564 | _distortionSet->getDistortionInternals()->getChild(osgViewer::DistortionSet::MESH)->asGeode()->getDrawable(0)->asGeometry()->dirtyDisplayList(); |
---|
565 | } |
---|