1 | /* -*-c++-*- osgVisual - Copyright (C) 2009-2010 Torben Dannhauer |
---|
2 | * |
---|
3 | * This library is based on OpenSceneGraph, open source and may be redistributed and/or modified under |
---|
4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
---|
5 | * (at your option) any later version. The full license is in LICENSE file |
---|
6 | * included with this distribution, and on the openscenegraph.org website. |
---|
7 | * |
---|
8 | * osgVisual requires for some proprietary modules a license from the correspondig manufacturer. |
---|
9 | * You have to aquire licenses for all used proprietary modules. |
---|
10 | * |
---|
11 | * This library is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * OpenSceneGraph Public License for more details. |
---|
15 | */ |
---|
16 | |
---|
17 | #include <visual_core.h> |
---|
18 | |
---|
19 | using namespace osgVisual; |
---|
20 | |
---|
21 | visual_core::visual_core(osg::ArgumentParser& arguments_) : arguments(arguments_) |
---|
22 | { |
---|
23 | OSG_NOTIFY( osg::ALWAYS ) << "visual_core instantiated." << std::endl; |
---|
24 | |
---|
25 | // Setup pathes |
---|
26 | osgDB::Registry::instance()->getDataFilePathList().push_back( "D:\\DA\\osgVisual\\models" ); |
---|
27 | |
---|
28 | // Setup viewer |
---|
29 | viewer = new osgViewer::Viewer(arguments); |
---|
30 | |
---|
31 | // Setup coordinate system node |
---|
32 | rootNode = new osg::CoordinateSystemNode; |
---|
33 | rootNode->setEllipsoidModel(new osg::EllipsoidModel()); |
---|
34 | |
---|
35 | // Test memory leak (todo) |
---|
36 | //double* test = new double[1000]; |
---|
37 | |
---|
38 | mouse = NULL; |
---|
39 | } |
---|
40 | |
---|
41 | visual_core::~visual_core(void) |
---|
42 | { |
---|
43 | // shut osgVisual down |
---|
44 | shutdown(); |
---|
45 | |
---|
46 | OSG_NOTIFY( osg::ALWAYS ) << "visual_core destroyed." << std::endl; |
---|
47 | |
---|
48 | } |
---|
49 | |
---|
50 | void visual_core::initialize() |
---|
51 | { |
---|
52 | OSG_NOTIFY( osg::ALWAYS ) << "Initialize visual_core..." << std::endl; |
---|
53 | |
---|
54 | // Process commandline arguments for configuration parameter |
---|
55 | processCommandlineArguments(); |
---|
56 | |
---|
57 | // Add manipulators for user interaction |
---|
58 | addManipulators(); |
---|
59 | |
---|
60 | // Load terrain |
---|
61 | loadTerrain(arguments); |
---|
62 | |
---|
63 | // Show model |
---|
64 | viewer->setSceneData( rootNode ); |
---|
65 | |
---|
66 | #ifdef USE_DISTORTION |
---|
67 | // Initialize distortion |
---|
68 | distortion = new visual_distortion( viewer, arguments ); |
---|
69 | distortion->initialize( rootNode, viewer->getCamera()->getClearColor() ); |
---|
70 | #endif |
---|
71 | |
---|
72 | #ifdef USE_SKY_SILVERLINING |
---|
73 | // Initialize sky |
---|
74 | sky = new visual_skySilverLining( viewer ); |
---|
75 | #ifdef USE_DISTORTION |
---|
76 | if ( distortion.valid() ) |
---|
77 | { |
---|
78 | OSG_NOTIFY( osg::ALWAYS ) << "Using sky with distortion." << std::endl; |
---|
79 | sky->init( distortion->getDistortedSceneGraph(), rootNode); |
---|
80 | } |
---|
81 | #else |
---|
82 | OSG_NOTIFY( osg::ALWAYS ) << "Using Sky without distortion." << std::endl; |
---|
83 | sky->init(rootNode); |
---|
84 | #endif |
---|
85 | #endif |
---|
86 | |
---|
87 | // Initialize DataIO interface |
---|
88 | visual_dataIO::getInstance()->init(viewer, arguments); |
---|
89 | |
---|
90 | // All modules are initialized - now check arguments for any unused parameter. |
---|
91 | checkCommandlineArgumentsForFinalErrors(); |
---|
92 | |
---|
93 | // create the windows and run the threads. |
---|
94 | viewer->realize(); |
---|
95 | |
---|
96 | // setup scenery |
---|
97 | setupScenery(); |
---|
98 | |
---|
99 | // Run visual main loop |
---|
100 | mainLoop(); |
---|
101 | } |
---|
102 | |
---|
103 | void visual_core::mainLoop() |
---|
104 | { |
---|
105 | // run main loop |
---|
106 | while( !viewer->done() ) |
---|
107 | { |
---|
108 | // next frame please.... |
---|
109 | viewer->advance(); |
---|
110 | |
---|
111 | /*double hat, hot, lat, lon, height; |
---|
112 | util::getWGS84ofCamera( viewer->getCamera(), rootNode, lat, lon, height); |
---|
113 | if (util::queryHeightOfTerrain( hot, rootNode, lat, lon) && util::queryHeightAboveTerrainInWGS84( hat, rootNode, lat, lon, height ) ) |
---|
114 | OSG_NOTIFY( osg::ALWAYS ) << "HOT is: " << hot << ", HAT is: " << hat << std::endl;*/ |
---|
115 | |
---|
116 | // update the scene by traversing it with the the update visitor which will |
---|
117 | // call all node update callbacks and animations. |
---|
118 | viewer->eventTraversal(); |
---|
119 | viewer->updateTraversal(); |
---|
120 | |
---|
121 | // Render the Frame. |
---|
122 | viewer->renderingTraversals(); |
---|
123 | |
---|
124 | } // END WHILE |
---|
125 | } |
---|
126 | |
---|
127 | void visual_core::shutdown() |
---|
128 | { |
---|
129 | OSG_NOTIFY( osg::ALWAYS ) << "Shutdown visual_core..." << std::endl; |
---|
130 | |
---|
131 | // Unset scene data |
---|
132 | viewer->setSceneData( NULL ); |
---|
133 | |
---|
134 | #ifdef USE_SKY_SILVERLINING |
---|
135 | // Shutdown sky |
---|
136 | if( sky.valid() ) |
---|
137 | sky->shutdown(); |
---|
138 | #endif |
---|
139 | |
---|
140 | #ifdef USE_DISTORTION |
---|
141 | // Shutdown distortion |
---|
142 | if( distortion.valid() ) |
---|
143 | distortion->shutdown(); |
---|
144 | #endif |
---|
145 | |
---|
146 | // Shutdown dataIO |
---|
147 | visual_dataIO::getInstance()->shutdown(); |
---|
148 | |
---|
149 | |
---|
150 | #ifdef USE_SPACENAVIGATOR |
---|
151 | //Delete SpaceMouse driver |
---|
152 | if(mouse) |
---|
153 | { |
---|
154 | mouse->shutdown(); |
---|
155 | delete mouse; |
---|
156 | } |
---|
157 | #endif |
---|
158 | } |
---|
159 | |
---|
160 | bool visual_core::loadTerrain(osg::ArgumentParser& arguments_) |
---|
161 | { |
---|
162 | osg::ref_ptr<osg::Node> model = osgDB::readNodeFiles(arguments_); |
---|
163 | if( model.valid() ) |
---|
164 | { |
---|
165 | rootNode->addChild( model.get() ); |
---|
166 | return true; |
---|
167 | } |
---|
168 | else |
---|
169 | { |
---|
170 | OSG_NOTIFY( osg::FATAL ) << arguments_.getApplicationName() <<"Load terrain: No data loaded" << std::endl; |
---|
171 | return false; |
---|
172 | } |
---|
173 | |
---|
174 | return false; |
---|
175 | } |
---|
176 | |
---|
177 | void visual_core::addManipulators() |
---|
178 | { |
---|
179 | // set up the camera manipulators. |
---|
180 | { |
---|
181 | osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator; |
---|
182 | |
---|
183 | keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() ); |
---|
184 | keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() ); |
---|
185 | keyswitchManipulator->addMatrixManipulator( '3', "Terrain", new osgGA::TerrainManipulator() ); |
---|
186 | nt = new osgGA::NodeTrackerManipulator(); |
---|
187 | keyswitchManipulator->addMatrixManipulator( '4', "NodeTrackerManipulator", nt ); |
---|
188 | |
---|
189 | #ifdef USE_SPACENAVIGATOR |
---|
190 | // SpaceNavigator manipulator |
---|
191 | mouse = new SpaceMouse(); |
---|
192 | mouse->initialize(); |
---|
193 | mouseTrackerManip = new NodeTrackerSpaceMouse(mouse); |
---|
194 | mouseTrackerManip->setTrackerMode(NodeTrackerSpaceMouse::NODE_CENTER); |
---|
195 | mouseTrackerManip->setRotationMode(NodeTrackerSpaceMouse::ELEVATION_AZIM); |
---|
196 | mouseTrackerManip->setAutoComputeHomePosition( true ); |
---|
197 | keyswitchManipulator->addMatrixManipulator( '5', "Spacemouse Node Tracker", mouseTrackerManip ); |
---|
198 | keyswitchManipulator->addMatrixManipulator( '6', "Spacemouse Free (Airplane)", new FreeManipulator(mouse) ); |
---|
199 | #endif |
---|
200 | |
---|
201 | // objectMounted Manipulator for Camera control by Nodes |
---|
202 | objectMountedCameraManip = new objectMountedManipulator(); |
---|
203 | keyswitchManipulator->addMatrixManipulator( '7', "Object mounted Camera", objectMountedCameraManip ); |
---|
204 | |
---|
205 | // Animation path manipulator |
---|
206 | std::string pathfile; |
---|
207 | char keyForAnimationPath = '8'; |
---|
208 | while (arguments.read("-p",pathfile)) |
---|
209 | { |
---|
210 | osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile); |
---|
211 | if (apm || !apm->valid()) |
---|
212 | { |
---|
213 | unsigned int num = keyswitchManipulator->getNumMatrixManipulators(); |
---|
214 | keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm ); |
---|
215 | keyswitchManipulator->selectMatrixManipulator(num); |
---|
216 | ++keyForAnimationPath; |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | viewer->setCameraManipulator( keyswitchManipulator.get() ); |
---|
221 | } |
---|
222 | |
---|
223 | // add the state manipulator |
---|
224 | viewer->addEventHandler( new osgGA::StateSetManipulator(rootNode->getOrCreateStateSet()) ); |
---|
225 | |
---|
226 | // add the thread model handler |
---|
227 | viewer->addEventHandler(new osgViewer::ThreadingHandler); |
---|
228 | |
---|
229 | // add the window size toggle handler |
---|
230 | viewer->addEventHandler(new osgViewer::WindowSizeHandler); |
---|
231 | |
---|
232 | // add the stats handler |
---|
233 | viewer->addEventHandler(new osgViewer::StatsHandler); |
---|
234 | |
---|
235 | // add the help handler |
---|
236 | viewer->addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage())); |
---|
237 | |
---|
238 | // add the record camera path handler |
---|
239 | viewer->addEventHandler(new osgViewer::RecordCameraPathHandler); |
---|
240 | |
---|
241 | // add the LOD Scale handler |
---|
242 | viewer->addEventHandler(new osgViewer::LODScaleHandler); |
---|
243 | |
---|
244 | // add the screen capture handler |
---|
245 | viewer->addEventHandler(new osgViewer::ScreenCaptureHandler); |
---|
246 | } |
---|
247 | |
---|
248 | void visual_core::processCommandlineArguments() |
---|
249 | { |
---|
250 | std::cout << "Processing general commandline arguments..." << std::endl; |
---|
251 | |
---|
252 | // Setup Application Usage |
---|
253 | arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName()); |
---|
254 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the new FSD visualization tool, written by Torben Dannhauer"); |
---|
255 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] Terrain_filename"); |
---|
256 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
---|
257 | |
---|
258 | // if user request help write it out to cout. |
---|
259 | if (arguments.read("-h") || arguments.read("--help")) |
---|
260 | { |
---|
261 | arguments.getApplicationUsage()->write(std::cout); |
---|
262 | //cause the viewer to exit and shut down clean. |
---|
263 | viewer->setDone(true); |
---|
264 | } |
---|
265 | |
---|
266 | // report any errors if they have occurred when parsing the program arguments. |
---|
267 | if (arguments.errors()) |
---|
268 | { |
---|
269 | arguments.writeErrorMessages(std::cout); |
---|
270 | //cause the viewer to exit and shut down clean. |
---|
271 | viewer->setDone(true); |
---|
272 | } |
---|
273 | } |
---|
274 | |
---|
275 | bool visual_core::checkCommandlineArgumentsForFinalErrors() |
---|
276 | { |
---|
277 | // any option left unread are converted into errors to write out later. |
---|
278 | arguments.reportRemainingOptionsAsUnrecognized(); |
---|
279 | |
---|
280 | // report any errors if they have occurred when parsing the program arguments. |
---|
281 | if (arguments.errors()) |
---|
282 | { |
---|
283 | arguments.writeErrorMessages(std::cout); |
---|
284 | return false; |
---|
285 | } |
---|
286 | return true; |
---|
287 | } |
---|
288 | |
---|
289 | void visual_core::setupScenery() |
---|
290 | { |
---|
291 | //testObj = new visual_object( rootNode, "testStab", objectMountedCameraManip ); |
---|
292 | //testObj->setNewPosition( osg::DegreesToRadians(47.7123), osg::DegreesToRadians(12.84088), 600 ); |
---|
293 | ///* using a huge cylinder to test position & orientation */ |
---|
294 | //testObj->setGeometry( util::getDemoCylinder(5000.0, 20.0 ) ); |
---|
295 | //testObj->addUpdater( new object_updater(testObj) ); |
---|
296 | |
---|
297 | osg::ref_ptr<visual_object> testObj2 = new visual_object( rootNode, "cessna" ); |
---|
298 | //testObj2->setNewPosition( osg::DegreesToRadians(47.8123), osg::DegreesToRadians(12.94088), 600 ); |
---|
299 | testObj2->setNewPosition( osg::DegreesToRadians(50.8123), osg::DegreesToRadians(8.94088), 600 ); |
---|
300 | testObj2->loadGeometry( "../models/cessna.osg" ); |
---|
301 | testObj2->addUpdater( new object_updater(testObj2) ); |
---|
302 | |
---|
303 | osg::ref_ptr<visual_object> testObj3 = new visual_object( rootNode, "SAENGER1" ); |
---|
304 | testObj3->setNewPosition( osg::DegreesToRadians(47.8123), osg::DegreesToRadians(12.94088), 600 ); |
---|
305 | testObj3->loadGeometry( "../models/saenger1.flt" ); |
---|
306 | testObj3->addUpdater( new object_updater(testObj3) ); |
---|
307 | |
---|
308 | |
---|
309 | osg::ref_ptr<visual_object> testObj4 = new visual_object( rootNode, "SAENGER2" ); |
---|
310 | testObj4->setNewPosition( osg::DegreesToRadians(47.8123), osg::DegreesToRadians(12.94088), 650 ); |
---|
311 | testObj4->loadGeometry( "../models/saenger2.flt" ); |
---|
312 | testObj4->addUpdater( new object_updater(testObj4) ); |
---|
313 | testObj4->addLabel("testLabel", "LabelTest!!\nnächste Zeile :)",osg::Vec4(1.0f,0.25f,1.0f,1.0f)); |
---|
314 | |
---|
315 | osg::ref_ptr<visual_object> testObj5 = new visual_object( rootNode, "SAENGER" ); |
---|
316 | testObj5->setNewPosition( osg::DegreesToRadians(47.8123), osg::DegreesToRadians(12.94088), 550 ); |
---|
317 | testObj5->loadGeometry( "../models/saengerCombine.flt" ); |
---|
318 | //testObj5->setScale( 2 ); |
---|
319 | testObj5->addUpdater( new object_updater(testObj5) ); |
---|
320 | |
---|
321 | #ifdef USE_SPACENAVIGATOR |
---|
322 | // Manipulatoren auf dieses Objekt binden (Primärobjekt) |
---|
323 | if (objectMountedCameraManip.valid()) |
---|
324 | objectMountedCameraManip->setAttachedObject( testObj4 ); |
---|
325 | if (mouseTrackerManip.valid()) |
---|
326 | { |
---|
327 | mouseTrackerManip->setTrackNode( testObj4->getGeometry() ); |
---|
328 | mouseTrackerManip->setMinimumDistance( 100 ); |
---|
329 | } |
---|
330 | #endif |
---|
331 | |
---|
332 | osgGA::NodeTrackerManipulator::TrackerMode trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER; |
---|
333 | osgGA::NodeTrackerManipulator::RotationMode rotationMode = osgGA::NodeTrackerManipulator::ELEVATION_AZIM; |
---|
334 | nt->setTrackerMode(trackerMode); |
---|
335 | nt->setRotationMode(rotationMode); |
---|
336 | //nt->setAutoComputeHomePosition( true ); |
---|
337 | nt->setMinimumDistance( 100 ); |
---|
338 | nt->setTrackNode(testObj4->getGeometry()); |
---|
339 | //nt->computeHomePosition(); |
---|
340 | nt->setAutoComputeHomePosition( true ); |
---|
341 | nt->setDistance( 250 ); |
---|
342 | |
---|
343 | |
---|
344 | // Load EDDF |
---|
345 | //std::string filename = "D:\\DA\\EDDF_test\\eddf.ive"; |
---|
346 | //if( !osgDB::fileExists(filename) ) |
---|
347 | //{ |
---|
348 | // OSG_NOTIFY(osg::ALWAYS) << "Warning: EDDF Model not loaded. File '" << filename << "' does not exist. Skipping."; |
---|
349 | //} |
---|
350 | //// read model |
---|
351 | //osg::ref_ptr<osg::Node> tmpModel = osgDB::readNodeFile( filename ); |
---|
352 | //if (tmpModel.valid()) |
---|
353 | // rootNode->addChild( tmpModel ); |
---|
354 | |
---|
355 | |
---|
356 | visual_draw2D::getInstance()->init( rootNode, viewer ); |
---|
357 | //osg::ref_ptr<visual_hud> hud = new visual_hud(); |
---|
358 | osg::ref_ptr<visual_debug_hud> hud = new visual_debug_hud(); |
---|
359 | hud->init( viewer, rootNode ); |
---|
360 | |
---|
361 | |
---|
362 | //osg::ref_ptr<visual_draw3D> test = new visual_draw3D(); |
---|
363 | //test->init( rootNode, viewer ); |
---|
364 | |
---|
365 | // Creating Testclasses |
---|
366 | osg::ref_ptr<osgVisual::dataIO_transportContainer> test = new osgVisual::dataIO_transportContainer(); |
---|
367 | osg::ref_ptr<osgVisual::dataIO_executer> testEx = new osgVisual::dataIO_executer(); |
---|
368 | osg::ref_ptr<osgVisual::dataIO_slot> testSlot = new osgVisual::dataIO_slot(); |
---|
369 | test->setFrameID( 22 ); |
---|
370 | test->setName("ugamoep"); |
---|
371 | testEx->setexecuterID( osgVisual::dataIO_executer::IS_COLLISION ); |
---|
372 | testSlot->setVariableName(std::string("HalloName")); |
---|
373 | testSlot->setdataDirection( osgVisual::dataIO_slot::TO_OBJ ); |
---|
374 | testSlot->setvarType( osgVisual::dataIO_slot::DOUBLE ); |
---|
375 | testSlot->setValue( 0.12345 ); |
---|
376 | test->addExecuter( testEx ); |
---|
377 | test->addSlot( testSlot ); |
---|
378 | |
---|
379 | // Writing object to stream |
---|
380 | //std::ofstream myOstream("test.txt" ); |
---|
381 | std::stringstream myOstream; |
---|
382 | std::string extension = "osgb"; |
---|
383 | bool compressed = false; |
---|
384 | osgDB::ReaderWriter* rw = osgDB::Registry::instance()->getReaderWriterForExtension(extension.c_str()); |
---|
385 | if ( rw ) |
---|
386 | { |
---|
387 | osgDB::ReaderWriter::WriteResult wr; |
---|
388 | |
---|
389 | if (extension == "osgb") |
---|
390 | { |
---|
391 | if (compressed) |
---|
392 | wr = rw->writeObject( *test, myOstream, new osgDB::Options("Compressor=zlib") ); |
---|
393 | else |
---|
394 | wr = rw->writeObject( *test, myOstream ); |
---|
395 | } |
---|
396 | |
---|
397 | if (extension == "osgt") |
---|
398 | { |
---|
399 | if (compressed) |
---|
400 | wr = rw->writeObject( *test, myOstream, new osgDB::Options("Ascii Compressor=zlib") ); |
---|
401 | else |
---|
402 | wr = rw->writeObject( *test, myOstream, new osgDB::Options("Ascii") ); |
---|
403 | } |
---|
404 | |
---|
405 | if (extension == "osgx") |
---|
406 | { |
---|
407 | if (compressed) |
---|
408 | wr = rw->writeObject( *test, myOstream, new osgDB::Options("XML Compressor=zlib") ); |
---|
409 | else |
---|
410 | wr = rw->writeObject( *test, myOstream, new osgDB::Options("XML") ); |
---|
411 | } |
---|
412 | |
---|
413 | |
---|
414 | if (!wr.success() ) OSG_NOTIFY( osg::WARN ) << "ERROR: Save failed: " << wr.message() << std::endl; |
---|
415 | } |
---|
416 | else |
---|
417 | OSG_NOTIFY( osg::WARN ) << "error getting readerWriter for osgt" << std::endl; |
---|
418 | |
---|
419 | // Size ermitteln. |
---|
420 | stringbuf *pbuf; |
---|
421 | pbuf = myOstream.rdbuf(); |
---|
422 | OSG_NOTIFY( osg::WARN ) << "PBUF Bytes available: " << pbuf->in_avail() << std::endl; |
---|
423 | OSG_NOTIFY( osg::ALWAYS ) << "STRING Bytes available: " << myOstream.str().length() << std::endl; |
---|
424 | OSG_NOTIFY( osg::ALWAYS ) << "STRING content: " << myOstream.str() << std::endl; |
---|
425 | |
---|
426 | //Reading Stream to node |
---|
427 | if ( rw ) |
---|
428 | { |
---|
429 | osgDB::ReaderWriter::ReadResult rr = rw->readObject( myOstream ); |
---|
430 | osg::ref_ptr<osgVisual::dataIO_transportContainer> test2 = dynamic_cast<osgVisual::dataIO_transportContainer*>(rr.takeObject()); |
---|
431 | if (test2) |
---|
432 | { |
---|
433 | OSG_NOTIFY( osg::WARN ) << "TEST::FrameID is: " << test->getFrameID() << std::endl; |
---|
434 | } |
---|
435 | else |
---|
436 | OSG_NOTIFY( osg::WARN ) << "Error converting stream to Node" << std::endl; |
---|
437 | } |
---|
438 | } |
---|