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

Last change on this file since 231 was 231, checked in by Torben Dannhauer, 13 years ago

Reloaced eventhandler and manipulator in a dedicated manipulator and tracking class (preparation for automatic tracking by extLink)

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