root/OpenSceneGraph/trunk/examples/osgimpostor/osgimpostor.cpp @ 13496

Revision 13496, 10.5 kB (checked in by robert, 20 hours ago)

Implemented callbacks for validate(), textChanged() and returnPressed()

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgimpostor.
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#define OSG_USE_DEPRECATED_GEOMETRY_METHODS 1
20
21#include <osg/Geometry>
22#include <osg/Material>
23#include <osg/MatrixTransform>
24#include <osg/Quat>
25#include <osg/Geode>
26
27#include <osgSim/Impostor>
28#include <osgSim/InsertImpostorsVisitor>
29
30#include <osgDB/ReadFile>
31
32#include <osgViewer/Viewer>
33
34#include "TestManipulator.h"
35
36
37#include <iostream>
38#include <list>
39
40// container storing all house nodes
41typedef osg::ref_ptr<osg::Node>    NodePtr;
42typedef std::list<NodePtr>        NodeContainer;
43typedef NodeContainer::iterator    NodeIterator;
44
45NodeContainer                    nodes;
46
47//
48osg::ref_ptr<osg::Group> Root = 0;
49
50const int HOUSES_SIZE = 25000;        // total number of houses
51double XDim = 5000.0f;                // area dimension +/- XDim
52double ZDim = 5000.0f;                // area dimension +/- YDim
53
54int GridX = 20;                        // number of grids in x direction
55int GridY = 20;                        // number of grids in y direction
56
57bool UseImpostor = true;            // use impostor (or do not use)
58
59float Threshold = 3000.0f;            // distance where impostor are shown
60
61// create houses and store nodes in container
62void CreateHouses()
63{
64    int i;
65
66    GLubyte indices[48] = {
67        0, 2, 1,
68        3, 2, 0,
69        0, 4, 7,
70        7, 3, 0,
71        0, 1, 5,
72        5, 4, 0,
73        1, 6, 5,
74        2, 6, 1,
75        2, 3, 7,
76        2, 7, 6,
77        4, 8, 7,
78        5, 6, 9,
79        4, 5, 8,
80        8, 5, 9,
81        6, 7, 8,
82        8, 9, 6
83    };
84
85    // use the same color, normal and indices for all houses.
86    osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array(1);
87    (*colors)[0] = osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f);
88
89    // normals
90    osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array(16);
91    (*normals)[0] = osg::Vec3( 0.0f,  -0.0f, -1.0f);
92    (*normals)[1] = osg::Vec3( 0.0f,  -0.0f, -1.0f);
93    (*normals)[2] = osg::Vec3( 0.0f,  -1.0f,  0.0f);
94    (*normals)[3] = osg::Vec3( 0.0f,  -1.0f,  0.0f);
95    (*normals)[4] = osg::Vec3( 1.0f,  -0.0f,  0.0f);
96    (*normals)[5] = osg::Vec3( 1.0f,  -0.0f,  0.0f);
97    (*normals)[6] = osg::Vec3( 0.0f, 1.0f,  0.0f);
98    (*normals)[7] = osg::Vec3( 0.0f, 1.0f,  0.0f);
99    (*normals)[8] = osg::Vec3(-1.0f,  -0.0f,  0.0f);
100    (*normals)[9] = osg::Vec3(-1.0f,  -0.0f,  0.0f);
101    (*normals)[10] = osg::Vec3( 0.0f,  -0.928477f, 0.371391f);
102    (*normals)[11] = osg::Vec3( 0.0f, 0.928477f, 0.371391f);
103    (*normals)[12] = osg::Vec3( 0.707107f,  0.0f, 0.707107f);
104    (*normals)[13] = osg::Vec3( 0.707107f,  0.0f, 0.707107f);
105    (*normals)[14] = osg::Vec3(-0.707107f,  0.0f, 0.707107f);
106    (*normals)[15] = osg::Vec3(-0.707107f,  0.0f, 0.707107f);
107
108    // coordIndices
109    osg::ref_ptr<osg::UByteArray> coordIndices = new osg::UByteArray(48,indices);
110
111    // share the primitive set.
112    osg::PrimitiveSet* primitives = new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES,0,48);
113
114    for (int q = 0; q < HOUSES_SIZE; q++)
115    {
116        float xPos = ((static_cast<double> (rand()) /
117                      static_cast<double> (RAND_MAX))
118                     * 2.0 * XDim) - XDim;
119
120        float yPos = ((static_cast<double> (rand()) /
121                      static_cast<double> (RAND_MAX))
122                     * 2 * ZDim) - ZDim;
123
124        float scale = 10.0f;
125               
126        osg::Vec3 offset(xPos,yPos,0.0f);
127
128        // coords
129        osg::ref_ptr<osg::Vec3Array> coords = new osg::Vec3Array(10);
130        (*coords)[0] = osg::Vec3( 0.5f, -0.7f, 0.0f);
131        (*coords)[1] = osg::Vec3( 0.5f,  0.7f, 0.0f);
132        (*coords)[2] = osg::Vec3(-0.5f, 0.7f, 0.0f);
133        (*coords)[3] = osg::Vec3(-0.5f, -0.7f, 0.0f);
134        (*coords)[4] = osg::Vec3( 0.5f, -0.7f, 1.0f);
135        (*coords)[5] = osg::Vec3( 0.5f,  0.7f, 1.0f);
136        (*coords)[6] = osg::Vec3(-0.5f,  0.7f, 1.0f);
137        (*coords)[7] = osg::Vec3(-0.5f, -0.7f, 1.0f);
138        (*coords)[8] = osg::Vec3( 0.0f, -0.5f, 1.5f);
139        (*coords)[9] = osg::Vec3( 0.0f,  0.5f, 1.5f);
140
141        for (i = 0; i < 10; i++)
142        {
143            (*coords)[i] = (*coords)[i] * scale + offset;
144        }
145
146
147        // create geometry
148        osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry();
149               
150        geometry->addPrimitiveSet(primitives);
151               
152        geometry->setVertexArray(coords.get());
153        geometry->setVertexIndices(coordIndices.get());
154               
155        geometry->setColorArray(colors.get());
156        geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
157               
158        geometry->setNormalArray(normals.get());
159        geometry->setNormalBinding(osg::Geometry::BIND_PER_PRIMITIVE);
160
161        osg::ref_ptr<osg::Geode> geode = new osg::Geode();
162        geode->addDrawable(geometry.get());
163       
164        nodes.push_back(geode.get());
165    }
166}
167
168void LayoutAsGrid()
169{
170    // calculate bounding box
171    osg::BoundingBox bbox;
172    for (NodeIterator node = nodes.begin(); node != nodes.end(); ++node)
173        bbox.expandBy((*node)->getBound());
174
175    // setup grid information
176    osg::Group ** groups = new osg::Group*[GridX * GridY];
177        int i;
178    for (i = 0; i < GridX * GridY; i++)
179        groups[i] = new osg::Group();
180
181    float xGridStart = bbox.xMin();
182    float xGridSize = (bbox.xMax() - bbox.xMin()) / GridX;
183
184    float yGridStart = bbox.yMin();
185    float yGridSize = (bbox.yMax() - bbox.yMin()) / GridY;
186
187    // arrange buildings into right grid
188    for (NodeIterator nodeIter = nodes.begin(); nodeIter != nodes.end(); ++nodeIter)
189    {
190        osg::Node * node = nodeIter->get();
191        osg::Vec3 center = node->getBound().center();
192       
193        int x = (int)floor((center.x() - xGridStart) / xGridSize);
194        int z = (int)floor((center.y() - yGridStart) / yGridSize);
195
196        groups[z * GridX + x]->addChild(node);
197    }
198 
199    // add nodes to building root   
200    for (i = 0; i < GridX * GridY; i++)
201    {
202        osg::StateSet * stateset = new osg::StateSet();
203
204        osg::Material * material = new osg::Material();
205        osg::Vec4 color = osg::Vec4(
206            0.5f + (static_cast<double> (rand()) / (2.0*static_cast<double> (RAND_MAX))),
207            0.5f + (static_cast<double> (rand()) / (2.0*static_cast<double> (RAND_MAX))),
208            0.5f + (static_cast<double> (rand()) / ( 2.0*static_cast<double>(RAND_MAX))),
209            1.0f);
210           
211        material->setAmbient(osg::Material::FRONT_AND_BACK, color);
212        material->setDiffuse(osg::Material::FRONT_AND_BACK, color);
213        stateset->setAttributeAndModes(material, osg::StateAttribute::ON);
214
215        groups[i]->setStateSet(stateset);
216
217        if (UseImpostor)
218        {
219            osgSim::Impostor * impostor = new osgSim::Impostor();
220            impostor->setImpostorThreshold(static_cast<float> (Threshold));
221            impostor->addChild(groups[i]);
222            impostor->setRange(0, 0.0f, 1e7f);
223            impostor->setCenter(groups[i]->getBound().center());
224            Root->addChild(impostor);
225        }
226        else
227        {
228            Root->addChild(groups[i]);
229        }
230    }
231
232    delete[] groups;
233}
234
235
236int main( int argc, char **argv )
237{
238    // use an ArgumentParser object to manage the program arguments.
239    osg::ArgumentParser arguments(&argc,argv);
240
241    // construct the viewer.
242    osgViewer::Viewer viewer;
243
244    // add local test manipulator more suitable for testing impostors.
245    viewer.setCameraManipulator(new TestManipulator);
246
247
248    // load the nodes from the commandline arguments.
249    osg::Node* model = osgDB::readNodeFiles(arguments);
250    if (model)
251    {
252        // the osgSim::InsertImpostorsVisitor used lower down to insert impostors
253        // only operators on subclass of Group's, if the model top node is not
254        // a group then it won't be able to insert an impostor.  We therefore
255        // manually insert an impostor above the model.
256        if (dynamic_cast<osg::Group*>(model)==0)
257        {
258            const osg::BoundingSphere& bs = model->getBound();
259            if (bs.valid())
260            {
261
262                osgSim::Impostor* impostor = new osgSim::Impostor;
263
264                // standard LOD settings
265                impostor->addChild(model);
266                impostor->setRange(0,0.0f,1e7f);
267                impostor->setCenter(bs.center());
268
269                // impostor specfic settings.
270                impostor->setImpostorThresholdToBound(5.0f);
271
272                model = impostor;
273
274            }
275        }
276
277        // we insert an impostor node above the model, so we keep a handle
278        // on the rootnode of the model, the is required since the
279        // InsertImpostorsVisitor can add a new root in automatically and
280        // we would know about it, other than by following the parent path
281        // up from model.  This is really what should be done, but I'll pass
282        // on it right now as it requires a getRoots() method to be added to
283        // osg::Node, and we're about to make a release so no new features!
284        osg::Group* rootnode = new osg::Group;
285        rootnode->addChild(model);
286
287
288        // now insert impostors in the model using the InsertImpostorsVisitor.
289        osgSim::InsertImpostorsVisitor ov;
290
291        // traverse the model and collect all osg::Group's and osg::LOD's.
292        // however, don't traverse the rootnode since we want to keep it as
293        // the start of traversal, otherwise the insertImpostor could insert
294        // and Impostor above the current root, making it nolonger a root!
295        model->accept(ov);
296
297        // insert the Impostors above groups and LOD's
298        ov.insertImpostors();
299    }
300    else
301    {
302        // no user model so we'll create our own world.
303        model = Root = new osg::Group();
304        CreateHouses();
305        LayoutAsGrid();
306    }
307
308    // add model to viewer.
309    viewer.setSceneData(model);
310
311    return viewer.run();
312}
Note: See TracBrowser for help on using the browser.