root/OpenSceneGraph/trunk/examples/osgterrain/osgterrain.cpp @ 11257

Revision 11257, 5.5 kB (checked in by robert, 5 years ago)

Added new osgterrain example that is simplified to just basic setup and control osgTerrain nodes, leaving the osgthreadedterrain as the more complex example.

Line 
1/* OpenSceneGraph example, osgterrain.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osg/ArgumentParser>
20#include <osgDB/ReadFile>
21
22#include <osgViewer/Viewer>
23#include <osgViewer/ViewerEventHandlers>
24
25#include <osgGA/TrackballManipulator>
26#include <osgGA/FlightManipulator>
27#include <osgGA/DriveManipulator>
28#include <osgGA/KeySwitchMatrixManipulator>
29#include <osgGA/StateSetManipulator>
30#include <osgGA/AnimationPathManipulator>
31#include <osgGA/TerrainManipulator>
32
33#include <osgTerrain/Terrain>
34#include <osgTerrain/TerrainTile>
35#include <osgTerrain/GeometryTechnique>
36#include <osgTerrain/Layer>
37
38#include <iostream>
39
40template<class T>
41class FindTopMostNodeOfTypeVisitor : public osg::NodeVisitor
42{
43public:
44    FindTopMostNodeOfTypeVisitor():
45        osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
46        _foundNode(0)
47    {}
48
49    void apply(osg::Node& node)
50    {
51        T* result = dynamic_cast<T*>(&node);
52        if (result)
53        {
54            _foundNode = result;
55        }
56        else
57        {
58            traverse(node);
59        }
60    }
61
62    T* _foundNode;
63};
64
65template<class T>
66T* findTopMostNodeOfType(osg::Node* node)
67{
68    if (!node) return 0;
69
70    FindTopMostNodeOfTypeVisitor<T> fnotv;
71    node->accept(fnotv);
72
73    return fnotv._foundNode;
74}
75
76int main(int argc, char** argv)
77{
78    osg::ArgumentParser arguments(&argc, argv);
79
80    // construct the viewer.
81    osgViewer::Viewer viewer(arguments);
82
83    // set up the camera manipulators.
84    {
85        osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
86
87        keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() );
88        keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() );
89        keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() );
90        keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() );
91
92        std::string pathfile;
93        char keyForAnimationPath = '5';
94        while (arguments.read("-p",pathfile))
95        {
96            osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile);
97            if (apm || !apm->valid())
98            {
99                unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
100                keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm );
101                keyswitchManipulator->selectMatrixManipulator(num);
102                ++keyForAnimationPath;
103            }
104        }
105
106        viewer.setCameraManipulator( keyswitchManipulator.get() );
107    }
108
109
110    // add the state manipulator
111    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
112
113    // add the stats handler
114    viewer.addEventHandler(new osgViewer::StatsHandler);
115
116    // add the record camera path handler
117    viewer.addEventHandler(new osgViewer::RecordCameraPathHandler);
118
119    // obtain the vertical scale
120    float verticalScale = 1.0f;
121    while(arguments.read("-v",verticalScale)) {}
122
123    // obtain the sample ratio
124    float sampleRatio = 1.0f;
125    while(arguments.read("-r",sampleRatio)) {}
126
127    osgTerrain::TerrainTile::BlendingPolicy blendingPolicy = osgTerrain::TerrainTile::INHERIT;
128    std::string strBlendingPolicy;
129    while(arguments.read("--blending-policy", strBlendingPolicy))
130    {
131        if (strBlendingPolicy == "INHERIT") blendingPolicy = osgTerrain::TerrainTile::INHERIT;
132        else if (strBlendingPolicy == "DO_NOT_SET_BLENDING") blendingPolicy = osgTerrain::TerrainTile::DO_NOT_SET_BLENDING;
133        else if (strBlendingPolicy == "ENABLE_BLENDING") blendingPolicy = osgTerrain::TerrainTile::ENABLE_BLENDING;
134        else if (strBlendingPolicy == "ENABLE_BLENDING_WHEN_ALPHA_PRESENT") blendingPolicy = osgTerrain::TerrainTile::ENABLE_BLENDING_WHEN_ALPHA_PRESENT;
135    }
136
137    // load the nodes from the commandline arguments.
138    osg::Node* rootnode = osgDB::readNodeFiles(arguments);
139
140    if (!rootnode)
141    {
142        osg::notify(osg::NOTICE)<<"Warning: no valid data loaded, please specify a database on the command line."<<std::endl;
143        return 1;
144    }
145
146    osgTerrain::Terrain* terrain = findTopMostNodeOfType<osgTerrain::Terrain>(rootnode);
147    if (!terrain)
148    {
149        terrain = new osgTerrain::Terrain;
150        terrain->addChild(rootnode);
151
152        rootnode = terrain;
153    }
154
155    terrain->setSampleRatio(sampleRatio);
156    terrain->setVerticalScale(verticalScale);
157    terrain->setBlendingPolicy(blendingPolicy);
158
159    // add a viewport to the viewer and attach the scene graph.
160    viewer.setSceneData( rootnode );
161
162
163    // run the viewers main loop
164    return viewer.run();
165
166}
Note: See TracBrowser for help on using the browser.