source: osgVisual/trunk/src/core/visual_core.cpp @ 184

Last change on this file since 184 was 184, checked in by Torben Dannhauer, 13 years ago
File size: 16.7 KB
Line 
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
18#include <visual_core.h>
19
20using namespace osgVisual;
21
22visual_core::visual_core(osg::ArgumentParser& arguments_) : arguments(arguments_)
23{
24        OSG_NOTIFY( osg::ALWAYS ) << "visual_core instantiated." << std::endl;
25}
26
27visual_core::~visual_core(void)
28{
29        OSG_NOTIFY( osg::ALWAYS ) << "visual_core destroyed." << std::endl;
30}
31
32void visual_core::initialize()
33{
34        OSG_NOTIFY( osg::ALWAYS ) << "Initialize visual_core..." << std::endl;
35
36        // Check for config file to provide it to all modules during initialization.
37        if( arguments.read("-c", configFilename) || arguments.read("--config", configFilename) )
38        {
39                if( !osgDB::fileExists(configFilename) )
40                        configFilename = "";
41                else
42                        OSG_ALWAYS << "Using configuration file: " << configFilename << std::endl;
43        }
44
45        // Configure osg to use KdTrees
46        osgDB::Registry::instance()->setBuildKdTreesHint(osgDB::ReaderWriter::Options::BUILD_KDTREES);
47
48        // Setup pathes
49        osgDB::Registry::instance()->getDataFilePathList().push_back( "D:\\DA\\osgVisual\\models" );
50       
51        // Setup viewer
52        viewer = new osgViewer::Viewer(arguments);
53
54        // Setup coordinate system node
55        rootNode = new osg::CoordinateSystemNode;       // todo memleakf
56        rootNode->setEllipsoidModel(new osg::EllipsoidModel());
57
58        // Test memory leak (todo)
59        double* test = new double[1000];
60
61        #ifdef USE_SPACENAVIGATOR
62                mouse = NULL;
63        #endif
64
65        //osg::DisplaySettings::instance()->setNumOfDatabaseThreadsHint( 8 );
66
67        // Show model
68        viewer->setSceneData( rootNode );
69
70        osg::Group* distortedSceneGraph = NULL;
71#ifdef USE_DISTORTION
72        // Initialize distortion
73        distortion = new visual_distortion( viewer, arguments, configFilename );
74        distortedSceneGraph = distortion->initialize( rootNode, viewer->getCamera()->getClearColor() );
75#endif
76
77#ifdef USE_SKY_SILVERLINING
78        // Initialize sky
79        bool disabled = false;  // to ask if the skyp is disabled or enabled
80        sky = new visual_skySilverLining( viewer, configFilename, disabled );
81        if(disabled)
82                sky = NULL;
83        if(sky.valid())
84                sky->init(distortedSceneGraph, rootNode);       // Without distortion: distortedSceneGraph=NULL
85#endif
86
87        // Initialize DataIO interface
88        visual_dataIO::getInstance()->init(viewer, arguments, configFilename);
89
90        // Add manipulators for user interaction - after dataIO to be able to skip it in slaves rendering machines.
91        addManipulators();
92
93        loadTerrain(arguments);
94
95        // create the windows and run the threads.
96        viewer->realize();
97
98        // parse Configuration file
99        parseConfigFile(arguments);
100
101        // All modules are initialized - now check arguments for any unused parameter.
102        checkCommandlineArgumentsForFinalErrors();
103
104        // Run visual main loop
105        mainLoop();
106}
107
108void visual_core::mainLoop()
109{
110        int framestoScenerySetup = 5;
111        // run main loop
112        while( !viewer->done() )
113    {
114                // setup scenery
115                if(framestoScenerySetup-- == 0)
116                        setupScenery();
117
118                // next frame please....
119        viewer->advance();
120
121                /*double hat, hot, lat, lon, height;
122                util::getWGS84ofCamera( viewer->getCamera(), rootNode, lat, lon, height);
123                if (util::queryHeightOfTerrain( hot, rootNode, lat, lon) && util::queryHeightAboveTerrainInWGS84( hat, rootNode, lat, lon, height ) )
124                        OSG_NOTIFY( osg::ALWAYS ) << "HOT is: " << hot << ", HAT is: " << hat << std::endl;*/
125       
126                // perform all queued events
127                viewer->eventTraversal();
128
129                // update the scene by traversing it with the the update visitor which will
130        // call all node update callbacks and animations.
131        viewer->updateTraversal();
132               
133        // Render the Frame.
134        viewer->renderingTraversals();
135
136    }   // END WHILE
137}
138
139void visual_core::shutdown()
140{
141        OSG_NOTIFY( osg::ALWAYS ) << "Shutdown visual_core..." << std::endl;
142
143        // Shutdown Dbug HUD
144        if(hud.valid())
145                hud->shutdown();
146        // Unset scene data
147        viewer->setSceneData( NULL );
148
149#ifdef USE_SKY_SILVERLINING
150        // Shutdown sky
151        if( sky.valid() )
152                sky->shutdown();
153#endif
154
155#ifdef USE_DISTORTION
156        // Shutdown distortion
157        if( distortion.valid() )
158                distortion->shutdown();
159#endif
160
161        // Shutdown data
162        rootNode = NULL;
163
164        // Shutdown dataIO
165        visual_dataIO::getInstance()->shutdown();
166
167       
168#ifdef USE_SPACENAVIGATOR
169        //Delete SpaceMouse driver
170        if(mouse)
171        {
172                mouse->shutdown();
173                delete mouse;
174        }
175#endif
176
177        // Destroy osgViewer
178        viewer = NULL;
179}
180
181bool visual_core::loadTerrain(osg::ArgumentParser& arguments_)
182{
183        osg::ref_ptr<osg::Node> model = osgDB::readNodeFiles(arguments_);
184        if( model.valid() )
185        {
186        rootNode->addChild( model.get() );
187                return true;
188        }
189        else
190        {
191        OSG_NOTIFY( osg::FATAL ) << "Load terrain: No data loaded" << std::endl;
192        return false;
193    }   
194
195        return false;
196}
197
198void visual_core::addManipulators()
199{
200        if(!visual_dataIO::getInstance()->isSlave()) // set up the camera manipulators if not slave.
201    {
202        osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
203
204        keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() );
205        keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() );
206        keyswitchManipulator->addMatrixManipulator( '3', "Terrain", new osgGA::TerrainManipulator() );
207                nt = new osgGA::NodeTrackerManipulator();
208                keyswitchManipulator->addMatrixManipulator( '4', "NodeTrackerManipulator", nt );
209               
210#ifdef USE_SPACENAVIGATOR
211                // SpaceNavigator manipulator
212                mouse = new SpaceMouse();
213                mouse->initialize();
214                mouseTrackerManip = new NodeTrackerSpaceMouse(mouse);
215                mouseTrackerManip->setTrackerMode(NodeTrackerSpaceMouse::NODE_CENTER);
216                mouseTrackerManip->setRotationMode(NodeTrackerSpaceMouse::ELEVATION_AZIM);
217                mouseTrackerManip->setAutoComputeHomePosition( true );
218                keyswitchManipulator->addMatrixManipulator( '5', "Spacemouse Node Tracker", mouseTrackerManip );
219                keyswitchManipulator->addMatrixManipulator( '6', "Spacemouse Free (Airplane)", new FreeManipulator(mouse) );
220#endif
221
222                // objectMounted Manipulator for Camera control by Nodes
223                objectMountedCameraManip = new objectMountedManipulator();
224                keyswitchManipulator->addMatrixManipulator( '7', "Object mounted Camera", objectMountedCameraManip );
225
226                // Animation path manipulator
227        std::string pathfile;
228        char keyForAnimationPath = '8';
229        while (arguments.read("-p",pathfile))
230        {
231            osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile);
232            if (apm || !apm->valid()) 
233            {
234                unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
235                keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm );
236                keyswitchManipulator->selectMatrixManipulator(num);
237                ++keyForAnimationPath;
238            }
239        }
240
241        viewer->setCameraManipulator( keyswitchManipulator.get() );
242    }   // If not Slave END
243
244    // add the state manipulator
245    viewer->addEventHandler( new osgGA::StateSetManipulator(rootNode->getOrCreateStateSet()) );
246   
247    // add the thread model handler
248    viewer->addEventHandler(new osgViewer::ThreadingHandler);
249
250    // add the window size toggle handler
251    viewer->addEventHandler(new osgViewer::WindowSizeHandler);
252       
253    // add the stats handler
254    viewer->addEventHandler(new osgViewer::StatsHandler);
255
256    // add the help handler
257    viewer->addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage()));
258
259    // add the record camera path handler
260    viewer->addEventHandler(new osgViewer::RecordCameraPathHandler);
261
262    // add the LOD Scale handler
263    viewer->addEventHandler(new osgViewer::LODScaleHandler);
264
265    // add the screen capture handler
266    viewer->addEventHandler(new osgViewer::ScreenCaptureHandler);
267}
268
269void visual_core::parseConfigFile(osg::ArgumentParser& arguments_)
270{
271        if( configFilename != "" )
272        {
273                xmlDoc *doc = NULL;
274                xmlNode *root_element = NULL;
275               
276                doc = xmlReadFile(configFilename.c_str(), NULL, 0);
277                if (doc == NULL)
278                {
279                        OSG_ALWAYS << "visual_core::parseConfigFile() - ERROR: could not parse osgVisual config file" << configFilename  << std::endl;
280                }
281                else
282                {
283                        //  Get the root element node
284                        root_element = xmlDocGetRootElement(doc);
285
286                        // Parse the XML document.
287                        checkXMLNode(root_element);
288
289                        // free the document
290                        xmlFreeDoc(doc);;
291                }
292                // Free the global variables that may have been allocated by the parser.
293                xmlCleanupParser();
294
295        }       // IF configfile exists
296}
297
298void visual_core::checkXMLNode(xmlNode * a_node)
299{
300  for (xmlNode *cur_node = a_node; cur_node; cur_node = cur_node->next)
301        {
302                std::string node_name=reinterpret_cast<const char*>(cur_node->name);
303                if(cur_node->type == XML_ELEMENT_NODE && node_name == "osgvisualconfiguration")
304                {
305                        OSG_DEBUG << "XML node osgvisualconfiguration found" << std::endl;
306
307                        // Iterate to the next nodes to configure modules and scenery.
308                        checkXMLNode(cur_node->children);               
309                }
310
311                if (cur_node->type == XML_ELEMENT_NODE && node_name == "scenery")
312                {
313                        OSG_DEBUG << "XML node scenery found" << std::endl;
314
315                        parseScenery(cur_node);
316       
317            //OSG_DEBUG << "node type=Element, name:" << cur_node->name << std::endl;
318                        //OSG_DEBUG << "Processing children at " << cur_node->children << std::endl;
319        }       // IF(scenery) END
320    }   // FOR END
321}
322
323void visual_core::parseScenery(xmlNode * a_node)
324{
325        OSG_ALWAYS << "parseScenery()" << std::endl;
326}
327
328void visual_core::config(xmlNode * a_node)
329{
330        // Currently no configuration options fpr the core module are available.
331}
332
333bool visual_core::checkCommandlineArgumentsForFinalErrors()
334{
335        // Setup Application Usage
336        arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
337        arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the new FSD visualization tool, written by Torben Dannhauer");
338    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] Terrain_filename");
339        arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
340
341    // if user request help write it out to cout.
342    if (arguments.read("-h") || arguments.read("--help"))
343    {
344        arguments.getApplicationUsage()->write(std::cout);
345                //cause the viewer to exit and shut down clean.
346        viewer->setDone(true);
347    }
348
349    // report any errors if they have occurred when parsing the program arguments.
350    if (arguments.errors())
351    {
352        arguments.writeErrorMessages(std::cout);
353                //cause the viewer to exit and shut down clean.
354        viewer->setDone(true);
355    }
356
357         // any option left unread are converted into errors to write out later.
358    arguments.reportRemainingOptionsAsUnrecognized();
359
360    // report any errors if they have occurred when parsing the program arguments.
361    if (arguments.errors())
362    {
363        arguments.writeErrorMessages(std::cout);
364        return false;
365    }
366        return true;
367}
368
369void visual_core::setupScenery()
370{
371
372        // Sky settings:
373        if(sky.valid())
374        {
375                sky->setTime(15,30,00);
376                sky->setVisibility(50000);
377                sky->addWindVolume( 0.0, 15000.0, 25.0, 90.0 );
378               
379                //sky->addCloudLayer( 0, 20000, 20000, 600.0, 1000.0, 0.5, CUMULONIMBUS_CAPPILATUS );
380                //sky->addCloudLayer( 1, 5000000, 5000000, 600.0, 7351.0, 0.2, CIRRUS_FIBRATUS );
381                //sky->addCloudLayer( 2, 50000, 50000, 600.0, 7351.0, 0.2, CIRROCUMULUS );
382                ///sky->addCloudLayer( 2, 100000, 100000, 600.0, 2351.0, 0.75, STRATUS );
383                sky->addCloudLayer( 3, 50000, 50000, 1300.0, 700.0, 0.07, CUMULUS_CONGESTUS );
384                //sky->addCloudLayer( 1, 100000, 100000, 3500.0, 2000.0, 0.30, STRATOCUMULUS );
385
386                //sky->setSlotPrecipitation( 1, 0.0, 0.0, 0.0, 25.0 );
387        }
388
389
390        //testObj = new visual_object( rootNode, "testStab", objectMountedCameraManip );
391        //testObj->setNewPosition( osg::DegreesToRadians(47.7123), osg::DegreesToRadians(12.84088), 600 );
392        ///* using a huge cylinder to test position & orientation */
393        //testObj->setGeometry( util::getDemoCylinder(5000.0, 20.0 ) );
394        //testObj->addUpdater( new object_updater(testObj) );
395
396        //osg::ref_ptr<visual_object> testObj2 = new visual_object( rootNode, "neuschwanstein" );       // todo memleak
397        ////testObj2->setNewPosition( osg::DegreesToRadians(47.8123), osg::DegreesToRadians(12.94088), 600 );
398        //testObj2->setNewPosition( osg::DegreesToRadians(47.557523564234), osg::DegreesToRadians(10.749646398595), 950 );
399        //testObj2->loadGeometry( "../models/neuschwanstein.osgb" );
400        ////testObj2->addUpdater( new object_updater(testObj2) );       // todo memleak
401
402        osg::ref_ptr<visual_object> testObj3 = new visual_object( rootNode, "SAENGER1" );       // todo memleak
403        testObj3->setNewPosition( osg::DegreesToRadians(47.8123), osg::DegreesToRadians(12.94088), 600 );
404        testObj3->loadGeometry( "../models/saenger1.flt" );
405        testObj3->addUpdater( new object_updater(testObj3) );   // todo memleak
406       
407
408        osg::ref_ptr<visual_object> testObj4 = new visual_object( rootNode, "SAENGER2" );       // todo memleak
409        testObj4->setNewPosition( osg::DegreesToRadians(47.8123), osg::DegreesToRadians(12.94088), 650 );
410        testObj4->loadGeometry( "../models/saenger2.flt" );
411        testObj4->addUpdater( new object_updater(testObj4) );   // todo memleak
412        testObj4->addLabel("testLabel", "LabelTest!!\nnächste Zeile :)",osg::Vec4(1.0f,0.25f,1.0f,1.0f));
413
414        osg::ref_ptr<visual_object> testObj5 = new visual_object( rootNode, "SAENGER" );        // todo memleak
415        testObj5->setNewPosition( osg::DegreesToRadians(47.8123), osg::DegreesToRadians(12.94088), 550 );
416        testObj5->loadGeometry( "../models/saengerCombine.flt" );
417        //testObj5->setScale( 2 );
418        testObj5->addUpdater( new object_updater(testObj5) );   // todo memleak
419
420#ifdef USE_SPACENAVIGATOR
421        // Manipulatoren auf dieses Objekt binden (Primärobjekt)
422        if (objectMountedCameraManip.valid())
423                objectMountedCameraManip->setAttachedObject( testObj4 );
424        if (mouseTrackerManip.valid())
425        {
426                mouseTrackerManip->setTrackNode( testObj4->getGeometry() );
427                mouseTrackerManip->setMinimumDistance( 100 );
428        }
429#endif
430
431        if(nt.valid())
432        {
433                osgGA::NodeTrackerManipulator::TrackerMode trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER;
434                osgGA::NodeTrackerManipulator::RotationMode rotationMode = osgGA::NodeTrackerManipulator::ELEVATION_AZIM;
435                nt->setTrackerMode(trackerMode);
436                nt->setRotationMode(rotationMode);
437                //nt->setAutoComputeHomePosition( true );
438                nt->setMinimumDistance( 100 );
439                nt->setTrackNode(testObj4->getGeometry());
440                //nt->computeHomePosition();
441                nt->setAutoComputeHomePosition( true );
442                nt->setDistance( 250 );
443        }
444
445
446        // Load EDDF
447        //std::string filename = "D:\\DA\\EDDF_test\\eddf.ive";
448        //if( !osgDB::fileExists(filename) )
449        //{
450        //      OSG_NOTIFY(osg::ALWAYS) << "Warning: EDDF Model not loaded. File '" << filename << "' does not exist. Skipping.";
451        //}
452        //// read model
453        //osg::ref_ptr<osg::Node> tmpModel = osgDB::readNodeFile( filename );
454        //if (tmpModel.valid())
455        //      rootNode->addChild( tmpModel );
456       
457 
458        visual_draw2D::getInstance()->init( rootNode, viewer );
459        //osg::ref_ptr<visual_hud> hud = new visual_hud();
460        hud = new visual_debug_hud();
461        hud->init( viewer, rootNode );
462       
463       
464
465        //osg::ref_ptr<visual_draw3D> test = new visual_draw3D();
466        //test->init( rootNode, viewer );
467
468        //// Creating Testclasses
469        //osg::ref_ptr<osgVisual::dataIO_transportContainer> test = new osgVisual::dataIO_transportContainer();
470        //osg::ref_ptr<osgVisual::dataIO_executer> testEx = new osgVisual::dataIO_executer();
471        //osg::ref_ptr<osgVisual::dataIO_slot> testSlot = new osgVisual::dataIO_slot();
472        //test->setFrameID( 22 );
473        //test->setName("ugamoep");
474        //testEx->setexecuterID( osgVisual::dataIO_executer::IS_COLLISION );
475        //testSlot->setVariableName(std::string("HalloName"));
476        //testSlot->setdataDirection( osgVisual::dataIO_slot::TO_OBJ );
477        //testSlot->setvarType( osgVisual::dataIO_slot::DOUBLE );
478        //testSlot->setValue( 0.12345 );
479        //test->addExecuter( testEx );
480        //test->addSlot( testSlot );
481
482        visual_dataIO::getInstance()->setSlotData("TestSlot1", osgVisual::dataIO_slot::TO_OBJ, 0.12345);
483
484}
Note: See TracBrowser for help on using the repository browser.