root/OpenSceneGraph/trunk/examples/osgpagedlod/osgpagedlod.cpp @ 2251

Revision 2251, 4.5 kB (checked in by robert, 11 years ago)

Fixes for osgFX.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osg/Group>
2#include <osg/Notify>
3#include <osg/Geometry>
4#include <osg/ArgumentParser>
5#include <osg/ApplicationUsage>
6#include <osg/Texture2D>
7#include <osg/Geode>
8#include <osg/PagedLOD>
9
10#include <osgDB/Registry>
11#include <osgDB/ReadFile>
12#include <osgDB/WriteFile>
13
14#include <osgUtil/Optimizer>
15
16osg::Geode* createTile(const osg::Vec3& lb, const osg::Vec3& rb,
17                       const osg::Vec3& lt, const osg::Vec3& rt,
18                       const std::string& imageFile)
19{
20
21    // create the geometry.
22    osg::Geometry* geom = new osg::Geometry;
23
24    osg::Vec3Array* coords = new osg::Vec3Array(4);
25    (*coords)[0] = lt;
26    (*coords)[1] = lb;
27    (*coords)[2] = rb;
28    (*coords)[3] = rt;
29    geom->setVertexArray(coords);
30
31    osg::Vec2Array* tcoords = new osg::Vec2Array(4);
32    (*tcoords)[0].set(0.0f,1.0f);
33    (*tcoords)[1].set(0.0f,0.0f);
34    (*tcoords)[2].set(1.0f,0.0f);
35    (*tcoords)[3].set(1.0f,1.0f);
36    geom->setTexCoordArray(0,tcoords);
37
38    osg::Vec4Array* colours = new osg::Vec4Array(1);
39    (*colours)[0].set(1.0f,1.0f,1.0,1.0f);
40    geom->setColorArray(colours);
41    geom->setColorBinding(osg::Geometry::BIND_OVERALL);
42
43    osg::Vec3Array* normals = new osg::Vec3Array(1);
44    (*normals)[0] = (rb-lb)^(lt-lb);
45    (*normals)[0].normalize();
46    geom->setNormalArray(normals);
47    geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
48
49    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));
50   
51   
52    // add the texture to the geometry.
53    geom->getOrCreateStateSet()->setTextureAttributeAndModes(
54        0, // texture unit 0.
55        new osg::Texture2D(osgDB::readImageFile(imageFile)),
56        osg::StateAttribute::ON);
57   
58   
59    // create the geode.
60    osg::Geode* geode = new osg::Geode;
61    geode->addDrawable(geom);
62   
63    return geode;
64
65}
66
67
68osg::Node* createPagedModel()
69{
70    osg::PagedLOD* level_0 = new osg::PagedLOD;
71
72    float distance = 1000.0f;
73   
74    osg::Vec3 lb(0.0f,0.0f,0.0f);
75    osg::Vec3 rb(distance,0.0f,0.0f);
76    osg::Vec3 rt(distance,0.0f,distance);
77    osg::Vec3 lt(0.0f,0.0f,distance);
78   
79    level_0->addChild(createTile(lb,rb,lt,rt,"lz.rgb"),distance,1e5,"");
80    level_0->addChild(createTile(lb,rb,lt,rt,"land_shallow_topo_2048.jpg"),0,distance,"level_1.osg");
81
82    return level_0;   
83}
84
85class WriteOutPagedLODSubgraphsVistor : public osg::NodeVisitor
86{
87public:
88    WriteOutPagedLODSubgraphsVistor():
89        osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
90    {
91    }
92   
93    virtual void apply(osg::PagedLOD& plod)
94    {
95        // go through all the named children and write them out to disk.
96        for(unsigned int i=0;i<plod.getNumChildren();++i)
97        {
98            osg::Node* child = plod.getChild(i);
99            std::string filename = plod.getFileName(i);
100            if (!filename.empty()) osgDB::writeNodeFile(*child,filename);
101        }
102   
103        traverse(plod);
104    }   
105};
106
107
108
109int main( int argc, char **argv )
110{
111
112    // use an ArgumentParser object to manage the program arguments.
113    osg::ArgumentParser arguments(&argc,argv);
114   
115    // set up the usage document, in case we need to print out how to use this program.
116    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
117    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" creates a hierachy of files for paging which can be later loaded by viewers.");
118    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
119    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
120
121    // if user request help write it out to cout.
122    if (arguments.read("-h") || arguments.read("--help"))
123    {
124        arguments.getApplicationUsage()->write(std::cout);
125        return 1;
126    }
127
128    // any option left unread are converted into errors to write out later.
129    arguments.reportRemainingOptionsAsUnrecognized();
130
131    // report any errors if they have occured when parsing the program aguments.
132    if (arguments.errors())
133    {
134        arguments.writeErrorMessages(std::cout);
135        return 1;
136    }
137   
138//     if (arguments.argc()<=1)
139//     {
140//         arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
141//         return 1;
142//     }
143
144
145    osg::ref_ptr<osg::Node> model = createPagedModel();
146   
147    if (model.valid())
148    {
149        osgDB::writeNodeFile(*model,"level_0.osg");
150       
151        WriteOutPagedLODSubgraphsVistor woplsv;
152        model->accept(woplsv);
153    }
154
155    return 0;
156}
Note: See TracBrowser for help on using the browser.