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

Last change on this file since 273 was 273, checked in by Torben Dannhauer, 13 years ago
File size: 15.0 KB
RevLine 
[221]1/* -*-c++-*- osgVisual - Copyright (C) 2009-2011 Torben Dannhauer
[31]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
[86]17
[31]18#include <visual_core.h>
[273]19#include <visual_util.h>
20#include <osgTerrain/Terrain>
[31]21
22using namespace osgVisual;
23
24visual_core::visual_core(osg::ArgumentParser& arguments_) : arguments(arguments_)
25{
26        OSG_NOTIFY( osg::ALWAYS ) << "visual_core instantiated." << std::endl;
[86]27}
[31]28
[86]29visual_core::~visual_core(void)
30{
31        OSG_NOTIFY( osg::ALWAYS ) << "visual_core destroyed." << std::endl;
32}
33
34void visual_core::initialize()
35{
36        OSG_NOTIFY( osg::ALWAYS ) << "Initialize visual_core..." << std::endl;
37
[144]38        // Check for config file to provide it to all modules during initialization.
39        if( arguments.read("-c", configFilename) || arguments.read("--config", configFilename) )
40        {
41                if( !osgDB::fileExists(configFilename) )
42                        configFilename = "";
43                else
44                        OSG_ALWAYS << "Using configuration file: " << configFilename << std::endl;
45        }
46
[86]47        // Configure osg to use KdTrees
48        osgDB::Registry::instance()->setBuildKdTreesHint(osgDB::ReaderWriter::Options::BUILD_KDTREES);
49
[31]50        // Setup pathes
51        osgDB::Registry::instance()->getDataFilePathList().push_back( "D:\\DA\\osgVisual\\models" );
52       
53        // Setup viewer
54        viewer = new osgViewer::Viewer(arguments);
55
56        // Setup coordinate system node
[87]57        rootNode = new osg::CoordinateSystemNode;       // todo memleakf
[31]58        rootNode->setEllipsoidModel(new osg::EllipsoidModel());
59
60        // Test memory leak (todo)
[116]61        double* test = new double[1000];
[31]62
[55]63        //osg::DisplaySettings::instance()->setNumOfDatabaseThreadsHint( 8 );
[31]64
65        // Show model
66        viewer->setSceneData( rootNode );
67
[71]68        osg::Group* distortedSceneGraph = NULL;
[31]69#ifdef USE_DISTORTION
70        // Initialize distortion
[144]71        distortion = new visual_distortion( viewer, arguments, configFilename );
[151]72        distortedSceneGraph = distortion->initialize( rootNode, viewer->getCamera()->getClearColor() );
[31]73#endif
74
75#ifdef USE_SKY_SILVERLINING
76        // Initialize sky
[182]77        bool disabled = false;  // to ask if the skyp is disabled or enabled
78        sky = new visual_skySilverLining( viewer, configFilename, disabled );
79        if(disabled)
80                sky = NULL;
81        if(sky.valid())
82                sky->init(distortedSceneGraph, rootNode);       // Without distortion: distortedSceneGraph=NULL
[31]83#endif
84
85        // Initialize DataIO interface
[185]86        visual_dataIO::getInstance()->init(viewer, configFilename);
[31]87
[144]88        // Add manipulators for user interaction - after dataIO to be able to skip it in slaves rendering machines.
[231]89        manipulators = new core_manipulator();
90        manipulators->init( viewer, arguments, configFilename, rootNode);
[73]91
[31]92        // create the windows and run the threads.
93        viewer->realize();
94
[189]95        loadTerrain(arguments);
96
[127]97        // All modules are initialized - now check arguments for any unused parameter.
98        checkCommandlineArgumentsForFinalErrors();
99
[31]100        // Run visual main loop
101        mainLoop();
102}
103
104void visual_core::mainLoop()
105{
[134]106        int framestoScenerySetup = 5;
[31]107        // run main loop
108        while( !viewer->done() )
109    {
[134]110                // setup scenery
111                if(framestoScenerySetup-- == 0)
112                        setupScenery();
113
[31]114                // next frame please....
115        viewer->advance();
116
117                /*double hat, hot, lat, lon, height;
118                util::getWGS84ofCamera( viewer->getCamera(), rootNode, lat, lon, height);
119                if (util::queryHeightOfTerrain( hot, rootNode, lat, lon) && util::queryHeightAboveTerrainInWGS84( hat, rootNode, lat, lon, height ) )
120                        OSG_NOTIFY( osg::ALWAYS ) << "HOT is: " << hot << ", HAT is: " << hat << std::endl;*/
121       
[70]122                // perform all queued events
123                viewer->eventTraversal();
124
[31]125                // update the scene by traversing it with the the update visitor which will
126        // call all node update callbacks and animations.
127        viewer->updateTraversal();
128               
129        // Render the Frame.
130        viewer->renderingTraversals();
131
132    }   // END WHILE
133}
134
135void visual_core::shutdown()
136{
137        OSG_NOTIFY( osg::ALWAYS ) << "Shutdown visual_core..." << std::endl;
138
[87]139        // Shutdown Dbug HUD
140        if(hud.valid())
141                hud->shutdown();
[31]142        // Unset scene data
143        viewer->setSceneData( NULL );
144
145#ifdef USE_SKY_SILVERLINING
146        // Shutdown sky
147        if( sky.valid() )
148                sky->shutdown();
149#endif
150
151#ifdef USE_DISTORTION
152        // Shutdown distortion
153        if( distortion.valid() )
154                distortion->shutdown();
155#endif
156
[87]157        // Shutdown data
158        rootNode = NULL;
159
[31]160        // Shutdown dataIO
161        visual_dataIO::getInstance()->shutdown();
162
[231]163        // Shutdown manipulators
164        if(manipulators.valid())
165                manipulators->shutdown();
[86]166
167        // Destroy osgViewer
168        viewer = NULL;
[31]169}
170
171bool visual_core::loadTerrain(osg::ArgumentParser& arguments_)
172{
[192]173        osg::ref_ptr<osg::Node> model = osgDB::readNodeFiles(util::getTerrainFromXMLConfig(configFilename));
[31]174        if( model.valid() )
175        {
[273]176        osgTerrain::Terrain* terrain = util::findTopMostNodeOfType<osgTerrain::Terrain>(model.get());
177                if (!terrain)
178                {
179                        terrain = new osgTerrain::Terrain;
180                        terrain->addChild(model.get());
181
182                        model = terrain;                       
183                }
184                rootNode->addChild( terrain );
[31]185                return true;
186        }
187        else
188        {
[58]189        OSG_NOTIFY( osg::FATAL ) << "Load terrain: No data loaded" << std::endl;
[31]190        return false;
191    }   
192
193        return false;
194}
195
[188]196void visual_core::parseScenery(xmlNode* a_node)
[125]197{
[128]198        OSG_ALWAYS << "parseScenery()" << std::endl;
[125]199
[188]200        a_node = a_node->children;
201
202        for (xmlNode *cur_node = a_node; cur_node; cur_node = cur_node->next)
203        {
204                std::string node_name=reinterpret_cast<const char*>(cur_node->name);
205
[194]206                // terrain is parsend seperately
207                // animationpath is parsend seperately
[188]208
209                if(cur_node->type == XML_ELEMENT_NODE && node_name == "models")
210                {
[202]211                        for (xmlNode *modelNode = cur_node->children; modelNode; modelNode = modelNode->next)
212                        {
[203]213                                std::string name=reinterpret_cast<const char*>(modelNode->name);
[227]214                                if(modelNode->type == XML_ELEMENT_NODE && name == "model")
[203]215                                {
216                                        visual_object::createNodeFromXMLConfig(rootNode, modelNode);
217                                }
[227]218                                if(modelNode->type == XML_ELEMENT_NODE && name == "trackmodel")
[225]219                                {
[226]220                                        // Extract track-ID and track the model
[227]221                                        xmlAttr  *attr = modelNode->properties;
[226]222                                        while ( attr ) 
223                                        { 
224                                                std::string attr_name=reinterpret_cast<const char*>(attr->name);
225                                                std::string attr_value=reinterpret_cast<const char*>(attr->children->content);
[231]226                                                if( attr_name == "id" ) manipulators->trackNode( util::strToInt(attr_value) );
[234]227                                                if( attr_name == "updater_slot" ) manipulators->setTrackingIdUpdaterSlot(attr_value);
[226]228                                                attr = attr->next; 
229                                        }
230                                       
[225]231                                }
[202]232                        }
[188]233                }
234
[211]235#ifdef USE_SKY_SILVERLINING
[188]236                if(cur_node->type == XML_ELEMENT_NODE && node_name == "datetime")
237                {
[248]238                        int day=-1, month=-1, year=-1, hour=-1, minute=-1;
[189]239
240                        xmlAttr  *attr = cur_node->properties;
241                        while ( attr ) 
242                        { 
243                                std::string attr_name=reinterpret_cast<const char*>(attr->name);
244                                std::string attr_value=reinterpret_cast<const char*>(attr->children->content);
[219]245                                if( attr_name == "day" ) day = util::strToInt(attr_value);
246                                if( attr_name == "month" ) month = util::strToInt(attr_value);
247                                if( attr_name == "year" ) year = util::strToInt(attr_value);
248                                if( attr_name == "hour" ) hour = util::strToInt(attr_value);
249                                if( attr_name == "minute" ) minute = util::strToInt(attr_value);
250
[189]251                                attr = attr->next; 
252                        }
253                        if(sky.valid())
254                        {
255                                if(day!=0 && month!=0 && year!=0)
256                                        sky->setDate(year, month, day);
257                                sky->setTime(hour,minute,00);
258                        }
[188]259                }
260
261                if(cur_node->type == XML_ELEMENT_NODE && node_name == "visibility")
262                {
[195]263                        float range = 50000, turbidity=2.2;
264                        xmlAttr  *attr = cur_node->properties;
265                        while ( attr ) 
266                        { 
267                                std::string attr_name=reinterpret_cast<const char*>(attr->name);
268                                std::string attr_value=reinterpret_cast<const char*>(attr->children->content);
[219]269                                if( attr_name == "range" ) range = util::strToDouble(attr_value);
270                                if( attr_name == "turbidity" ) turbidity = util::strToDouble(attr_value);
271
[195]272                                attr = attr->next; 
273                        }
[211]274
[195]275                        if(sky.valid())
276                        {
277                                sky->setVisibility( range );
278                                sky->setTurbidity( turbidity );
279                        }
[188]280                }
281
[200]282                if(cur_node->type == XML_ELEMENT_NODE && node_name == "clouds")
[188]283                {
[198]284                        if(sky.valid())
285                                sky->configureCloudlayerbyXML( cur_node );
[188]286                }
287
288                if(cur_node->type == XML_ELEMENT_NODE && node_name == "windlayer")
289                {
[195]290                        float bottom = 0.0, top=5000.0, speed=25.0, direction=0.0;
291                        xmlAttr  *attr = cur_node->properties;
292                        while ( attr ) 
293                        { 
294                                std::string attr_name=reinterpret_cast<const char*>(attr->name);
295                                std::string attr_value=reinterpret_cast<const char*>(attr->children->content);
[219]296                                if( attr_name == "bottom" ) bottom = util::strToDouble(attr_value);
297                                if( attr_name == "top" ) top = util::strToDouble(attr_value);
298                                if( attr_name == "speed" ) speed = util::strToDouble(attr_value);
299                                if( attr_name == "direction" ) direction = util::strToDouble(attr_value);
300
[195]301                                attr = attr->next; 
302                        }
303                        if(sky.valid())
304                        {
305                                sky->addWindVolume( bottom, top, speed, direction );
306                        }
[188]307                }
[212]308
309                // Track Node
310
[211]311#endif
[188]312        }// FOR all nodes END
313
[129]314}
315
[67]316bool visual_core::checkCommandlineArgumentsForFinalErrors()
[31]317{
318        // Setup Application Usage
319        arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
320        arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the new FSD visualization tool, written by Torben Dannhauer");
[212]321    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [OSG options] -c XML-Configurationfile");
[31]322        arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
[212]323        arguments.getApplicationUsage()->addCommandLineOption("-c or --config","XML configuration filename");
[31]324
[212]325
[31]326    // if user request help write it out to cout.
327    if (arguments.read("-h") || arguments.read("--help"))
328    {
329        arguments.getApplicationUsage()->write(std::cout);
330                //cause the viewer to exit and shut down clean.
331        viewer->setDone(true);
332    }
333
334    // report any errors if they have occurred when parsing the program arguments.
335    if (arguments.errors())
336    {
337        arguments.writeErrorMessages(std::cout);
338                //cause the viewer to exit and shut down clean.
339        viewer->setDone(true);
340    }
341
342         // any option left unread are converted into errors to write out later.
343    arguments.reportRemainingOptionsAsUnrecognized();
344
345    // report any errors if they have occurred when parsing the program arguments.
346    if (arguments.errors())
347    {
348        arguments.writeErrorMessages(std::cout);
349        return false;
350    }
351        return true;
352}
353
354void visual_core::setupScenery()
355{
[194]356        // Parse Scenery from Configuration file
357        xmlDoc* tmpDoc;
358        xmlNode* sceneryNode = util::getSceneryXMLConfig(configFilename, tmpDoc);
359        parseScenery(sceneryNode);
360        if(sceneryNode)
361        {
362                xmlFreeDoc(tmpDoc); xmlCleanupParser();
363        }
[122]364
[273]365        osgTerrain::Terrain* terrain = util::findTopMostNodeOfType<osgTerrain::Terrain>(rootNode);
366    if (!terrain)
367    {
368        OSG_ALWAYS << "No TerrainNode found!" << std::endl;
369    }
370        else
371        {
372                OSG_ALWAYS << "BorderEqual activated" << std::endl;
373                terrain->setEqualizeBoundaries(true);
374        }
[194]375
[31]376        //testObj = new visual_object( rootNode, "testStab", objectMountedCameraManip );
377        //testObj->setNewPosition( osg::DegreesToRadians(47.7123), osg::DegreesToRadians(12.84088), 600 );
378        ///* using a huge cylinder to test position & orientation */
379        //testObj->setGeometry( util::getDemoCylinder(5000.0, 20.0 ) );
380        //testObj->addUpdater( new object_updater(testObj) );
[235]381        //testObj->setTrackingId(2);
[31]382
[115]383        //osg::ref_ptr<visual_object> testObj2 = new visual_object( rootNode, "neuschwanstein" );       // todo memleak
384        ////testObj2->setNewPosition( osg::DegreesToRadians(47.8123), osg::DegreesToRadians(12.94088), 600 );
385        //testObj2->setNewPosition( osg::DegreesToRadians(47.557523564234), osg::DegreesToRadians(10.749646398595), 950 );
386        //testObj2->loadGeometry( "../models/neuschwanstein.osgb" );
[216]387        ////testObj2->addUpdater( new object_updater(testObj2) );
[235]388        //testObj2->setTrackingId(3);
[31]389
[212]390        //osg::ref_ptr<visual_object> testObj3 = new visual_object( rootNode, "SAENGER1" );     // todo memleak
391        //testObj3->setNewPosition( osg::DegreesToRadians(47.8123), osg::DegreesToRadians(12.94088), 600 );
392        //testObj3->loadGeometry( "../models/saenger1.flt" );
[216]393        //testObj3->addUpdater( new object_updater(testObj3) );
[235]394        //testObj3->setTrackingId(4);
[31]395
[224]396        osg::ref_ptr<visual_object> testObj4 = new visual_object( rootNode, "SAENGER2" );       // todo memleak
[31]397        testObj4->setNewPosition( osg::DegreesToRadians(47.8123), osg::DegreesToRadians(12.94088), 650 );
398        testObj4->loadGeometry( "../models/saenger2.flt" );
[216]399        testObj4->addUpdater( new object_updater(testObj4) );
[215]400        testObj4->addLabel("testLabel", "Object4 :)",osg::Vec4(1.0f,0.25f,1.0f,1.0f));
[240]401        testObj4->setTrackingId(2);
[31]402
[212]403        //osg::ref_ptr<visual_object> testObj5 = new visual_object( rootNode, "SAENGER" );      // todo memleak
404        //testObj5->setNewPosition( osg::DegreesToRadians(47.8123), osg::DegreesToRadians(12.94088), 550 );
405        //testObj5->loadGeometry( "../models/saengerCombine.flt" );
406        ////testObj5->setScale( 2 );
[216]407        //testObj5->addUpdater( new object_updater(testObj5) );
[235]408        //testObj5->setTrackingId(6);
[31]409
[235]410        manipulators->trackNode( testObj4 );
[31]411
412        // Load EDDF
413        //std::string filename = "D:\\DA\\EDDF_test\\eddf.ive";
414        //if( !osgDB::fileExists(filename) )
415        //{
416        //      OSG_NOTIFY(osg::ALWAYS) << "Warning: EDDF Model not loaded. File '" << filename << "' does not exist. Skipping.";
417        //}
418        //// read model
419        //osg::ref_ptr<osg::Node> tmpModel = osgDB::readNodeFile( filename );
420        //if (tmpModel.valid())
421        //      rootNode->addChild( tmpModel );
422       
423 
424        visual_draw2D::getInstance()->init( rootNode, viewer );
425        //osg::ref_ptr<visual_hud> hud = new visual_hud();
[87]426        hud = new visual_debug_hud();
[31]427        hud->init( viewer, rootNode );
428       
[122]429       
[31]430
431        //osg::ref_ptr<visual_draw3D> test = new visual_draw3D();
432        //test->init( rootNode, viewer );
433
[69]434        //// Creating Testclasses
435        //osg::ref_ptr<osgVisual::dataIO_transportContainer> test = new osgVisual::dataIO_transportContainer();
436        //osg::ref_ptr<osgVisual::dataIO_executer> testEx = new osgVisual::dataIO_executer();
437        //osg::ref_ptr<osgVisual::dataIO_slot> testSlot = new osgVisual::dataIO_slot();
438        //test->setFrameID( 22 );
439        //test->setName("ugamoep");
440        //testEx->setexecuterID( osgVisual::dataIO_executer::IS_COLLISION );
441        //testSlot->setVariableName(std::string("HalloName"));
442        //testSlot->setdataDirection( osgVisual::dataIO_slot::TO_OBJ );
443        //testSlot->setvarType( osgVisual::dataIO_slot::DOUBLE );
444        //testSlot->setValue( 0.12345 );
445        //test->addExecuter( testEx );
446        //test->addSlot( testSlot );
[31]447
[69]448        visual_dataIO::getInstance()->setSlotData("TestSlot1", osgVisual::dataIO_slot::TO_OBJ, 0.12345);
[155]449
[31]450}
Note: See TracBrowser for help on using the repository browser.