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