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