source: experimental/osgVisualNG/visual_core.cpp @ 417

Last change on this file since 417 was 417, checked in by Torben Dannhauer, 12 years ago
  • Property svn:eol-style set to native
File size: 5.5 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#include <visual_util.h>
20#include <osgTerrain/Terrain>
21#include <osgDB/FileNameUtils>
22
23using namespace osgVisual;
24
25visual_core::visual_core(osg::ArgumentParser& arguments_) : arguments(arguments_)
26{
27        OSG_NOTIFY( osg::ALWAYS ) << "visual_core instantiated." << std::endl;
28}
29
30visual_core::~visual_core(void)
31{
32        OSG_NOTIFY( osg::ALWAYS ) << "visual_core destroyed." << std::endl;
33}
34
35void visual_core::initialize()
36{
37        OSG_NOTIFY( osg::ALWAYS ) << "Initialize visual_core..." << std::endl;
38
39
40        // Configure osg to use KdTrees
41        osgDB::Registry::instance()->setBuildKdTreesHint(osgDB::ReaderWriter::Options::BUILD_KDTREES);
42
43        // Configure Multisampling
44        osg::DisplaySettings::instance()->setNumMultiSamples(4);
45
46        // Setup pathes
47        osgDB::Registry::instance()->getDataFilePathList().push_back( "D:/DA/osgVisual/models" );
48       
49        // Setup viewer
50        viewer = new osgViewer::Viewer(arguments);
51
52        // Setup coordinate system node
53        rootNode = new osg::CoordinateSystemNode;       
54        rootNode->setEllipsoidModel(new osg::EllipsoidModel());
55
56
57        //osg::DisplaySettings::instance()->setNumOfDatabaseThreadsHint( 8 );
58
59        // Show model
60        viewer->setSceneData( rootNode );
61
62        // create the windows and run the threads.
63        viewer->realize();
64
65        loadTerrain(arguments);
66
67        // All modules are initialized - now check arguments for any unused parameter.
68        checkCommandlineArgumentsForFinalErrors();
69
70        // Run visual main loop
71        mainLoop();
72}
73
74void visual_core::mainLoop()
75{
76        int framestoScenerySetup = 5;
77        // run main loop
78        while( !viewer->done() )
79    {
80                // setup scenery
81                if(framestoScenerySetup-- == 0)
82                        setupScenery();
83
84                // next frame please....
85        viewer->advance();
86
87                /*double hat, hot, lat, lon, height;
88                util::getWGS84ofCamera( viewer->getCamera(), rootNode, lat, lon, height);
89                if (util::queryHeightOfTerrain( hot, rootNode, lat, lon) && util::queryHeightAboveTerrainInWGS84( hat, rootNode, lat, lon, height ) )
90                        OSG_NOTIFY( osg::ALWAYS ) << "HOT is: " << hot << ", HAT is: " << hat << std::endl;*/
91       
92                // perform all queued events
93                viewer->eventTraversal();
94
95                // update the scene by traversing it with the the update visitor which will
96        // call all node update callbacks and animations.
97        viewer->updateTraversal();
98               
99        // Render the Frame.
100        viewer->renderingTraversals();
101
102    }   // END WHILE
103}
104
105void visual_core::shutdown()
106{
107        OSG_NOTIFY( osg::ALWAYS ) << "Shutdown visual_core..." << std::endl;
108
109        // Unset scene data
110        viewer->setSceneData( NULL );
111
112        // Shutdown data
113        rootNode = NULL;
114
115        // Destroy osgViewer
116        viewer = NULL;
117}
118
119bool visual_core::loadTerrain(osg::ArgumentParser& arguments_)
120{
121        //std::vector<std::string> terrainFile = util::getTerrainFromXMLConfig(configFilename);
122
123        // Add each terrain path to the FilePath list to help OSG to find the subtiles.
124        for(unsigned int i=0;i<terrainFile.size();i++)
125                osgDB::Registry::instance()->getDataFilePathList().push_back(osgDB::getFilePath(terrainFile[i]));
126
127        osg::ref_ptr<osg::Node> model = osgDB::readNodeFiles(terrainFile);
128        if( model.valid() )
129        {
130        osgTerrain::Terrain* terrain = util::findTopMostNodeOfType<osgTerrain::Terrain>(model.get());
131                if (!terrain)
132                {
133                        terrain = new osgTerrain::Terrain;
134                        terrain->addChild(model.get());
135
136                        model = terrain;                       
137                }
138                rootNode->addChild( terrain );
139                return true;
140        }
141        else
142        {
143        OSG_NOTIFY( osg::FATAL ) << "Load terrain: No data loaded" << std::endl;
144        return false;
145    }   
146
147        return false;
148}
149
150bool visual_core::checkCommandlineArgumentsForFinalErrors()
151{
152        // Setup Application Usage
153        arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
154        arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the a new visualization tool, written by Torben Dannhauer");
155        arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
156
157
158    // if user request help write it out to cout.
159    if (arguments.read("-h") || arguments.read("--help"))
160    {
161        arguments.getApplicationUsage()->write(std::cout);
162                //cause the viewer to exit and shut down clean.
163        viewer->setDone(true);
164    }
165
166    // report any errors if they have occurred when parsing the program arguments.
167    if (arguments.errors())
168    {
169        arguments.writeErrorMessages(std::cout);
170                //cause the viewer to exit and shut down clean.
171        viewer->setDone(true);
172    }
173
174         // any option left unread are converted into errors to write out later.
175    arguments.reportRemainingOptionsAsUnrecognized();
176
177    // report any errors if they have occurred when parsing the program arguments.
178    if (arguments.errors())
179    {
180        arguments.writeErrorMessages(std::cout);
181        return false;
182    }
183        return true;
184}
185
186void visual_core::setupScenery()
187{
188        // Nothing
189}
Note: See TracBrowser for help on using the repository browser.