2007-06-12 22:20:16 +08:00
|
|
|
/* OpenSceneGraph example, osgimpostor.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
#include <osg/Geometry>
|
|
|
|
#include <osg/Material>
|
|
|
|
#include <osg/MatrixTransform>
|
|
|
|
#include <osg/Quat>
|
|
|
|
#include <osg/Geode>
|
|
|
|
|
2005-05-02 03:48:49 +08:00
|
|
|
#include <osgSim/Impostor>
|
|
|
|
#include <osgSim/InsertImpostorsVisitor>
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
|
2007-01-08 18:00:16 +08:00
|
|
|
#include <osgViewer/Viewer>
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
#include "TestManipulator.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
// container storing all house nodes
|
2005-11-18 04:22:55 +08:00
|
|
|
typedef osg::ref_ptr<osg::Node> NodePtr;
|
|
|
|
typedef std::list<NodePtr> NodeContainer;
|
|
|
|
typedef NodeContainer::iterator NodeIterator;
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-11-18 04:22:55 +08:00
|
|
|
NodeContainer nodes;
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
//
|
2013-06-18 19:18:28 +08:00
|
|
|
osg::ref_ptr<osg::Group> Root = 0;
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-11-18 04:22:55 +08:00
|
|
|
const int HOUSES_SIZE = 25000; // total number of houses
|
|
|
|
double XDim = 5000.0f; // area dimension +/- XDim
|
|
|
|
double ZDim = 5000.0f; // area dimension +/- YDim
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-11-18 04:22:55 +08:00
|
|
|
int GridX = 20; // number of grids in x direction
|
|
|
|
int GridY = 20; // number of grids in y direction
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-11-18 04:22:55 +08:00
|
|
|
bool UseImpostor = true; // use impostor (or do not use)
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-11-18 04:22:55 +08:00
|
|
|
float Threshold = 3000.0f; // distance where impostor are shown
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
// create houses and store nodes in container
|
|
|
|
void CreateHouses()
|
|
|
|
{
|
2005-11-18 04:22:55 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
GLubyte indices[48] = {
|
|
|
|
0, 2, 1,
|
|
|
|
3, 2, 0,
|
|
|
|
0, 4, 7,
|
|
|
|
7, 3, 0,
|
|
|
|
0, 1, 5,
|
|
|
|
5, 4, 0,
|
|
|
|
1, 6, 5,
|
|
|
|
2, 6, 1,
|
|
|
|
2, 3, 7,
|
|
|
|
2, 7, 6,
|
|
|
|
4, 8, 7,
|
|
|
|
5, 6, 9,
|
|
|
|
4, 5, 8,
|
|
|
|
8, 5, 9,
|
|
|
|
6, 7, 8,
|
|
|
|
8, 9, 6
|
|
|
|
};
|
|
|
|
|
|
|
|
// use the same color, normal and indices for all houses.
|
2013-06-18 19:18:28 +08:00
|
|
|
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array(1);
|
2005-11-18 04:22:55 +08:00
|
|
|
(*colors)[0] = osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f);
|
|
|
|
|
|
|
|
// normals
|
2013-06-18 19:18:28 +08:00
|
|
|
osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array(16);
|
2005-11-18 04:22:55 +08:00
|
|
|
(*normals)[0] = osg::Vec3( 0.0f, -0.0f, -1.0f);
|
|
|
|
(*normals)[1] = osg::Vec3( 0.0f, -0.0f, -1.0f);
|
|
|
|
(*normals)[2] = osg::Vec3( 0.0f, -1.0f, 0.0f);
|
|
|
|
(*normals)[3] = osg::Vec3( 0.0f, -1.0f, 0.0f);
|
|
|
|
(*normals)[4] = osg::Vec3( 1.0f, -0.0f, 0.0f);
|
|
|
|
(*normals)[5] = osg::Vec3( 1.0f, -0.0f, 0.0f);
|
|
|
|
(*normals)[6] = osg::Vec3( 0.0f, 1.0f, 0.0f);
|
|
|
|
(*normals)[7] = osg::Vec3( 0.0f, 1.0f, 0.0f);
|
|
|
|
(*normals)[8] = osg::Vec3(-1.0f, -0.0f, 0.0f);
|
|
|
|
(*normals)[9] = osg::Vec3(-1.0f, -0.0f, 0.0f);
|
|
|
|
(*normals)[10] = osg::Vec3( 0.0f, -0.928477f, 0.371391f);
|
|
|
|
(*normals)[11] = osg::Vec3( 0.0f, 0.928477f, 0.371391f);
|
|
|
|
(*normals)[12] = osg::Vec3( 0.707107f, 0.0f, 0.707107f);
|
|
|
|
(*normals)[13] = osg::Vec3( 0.707107f, 0.0f, 0.707107f);
|
|
|
|
(*normals)[14] = osg::Vec3(-0.707107f, 0.0f, 0.707107f);
|
|
|
|
(*normals)[15] = osg::Vec3(-0.707107f, 0.0f, 0.707107f);
|
|
|
|
|
|
|
|
// coordIndices
|
2013-06-18 19:18:28 +08:00
|
|
|
osg::ref_ptr<osg::UByteArray> coordIndices = new osg::UByteArray(48,indices);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2013-06-18 19:18:28 +08:00
|
|
|
// share the primitive set.
|
|
|
|
osg::PrimitiveSet* primitives = new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES,0,48);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-11-18 04:22:55 +08:00
|
|
|
for (int q = 0; q < HOUSES_SIZE; q++)
|
|
|
|
{
|
|
|
|
float xPos = ((static_cast<double> (rand()) /
|
|
|
|
static_cast<double> (RAND_MAX))
|
|
|
|
* 2.0 * XDim) - XDim;
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-11-18 04:22:55 +08:00
|
|
|
float yPos = ((static_cast<double> (rand()) /
|
|
|
|
static_cast<double> (RAND_MAX))
|
|
|
|
* 2 * ZDim) - ZDim;
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-11-18 04:22:55 +08:00
|
|
|
float scale = 10.0f;
|
2013-06-26 00:10:24 +08:00
|
|
|
|
2013-06-18 19:18:28 +08:00
|
|
|
osg::Vec3 offset(xPos,yPos,0.0f);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-11-18 04:22:55 +08:00
|
|
|
// coords
|
2013-06-18 19:18:28 +08:00
|
|
|
osg::ref_ptr<osg::Vec3Array> coords = new osg::Vec3Array(10);
|
2005-11-18 04:22:55 +08:00
|
|
|
(*coords)[0] = osg::Vec3( 0.5f, -0.7f, 0.0f);
|
|
|
|
(*coords)[1] = osg::Vec3( 0.5f, 0.7f, 0.0f);
|
|
|
|
(*coords)[2] = osg::Vec3(-0.5f, 0.7f, 0.0f);
|
|
|
|
(*coords)[3] = osg::Vec3(-0.5f, -0.7f, 0.0f);
|
|
|
|
(*coords)[4] = osg::Vec3( 0.5f, -0.7f, 1.0f);
|
|
|
|
(*coords)[5] = osg::Vec3( 0.5f, 0.7f, 1.0f);
|
|
|
|
(*coords)[6] = osg::Vec3(-0.5f, 0.7f, 1.0f);
|
|
|
|
(*coords)[7] = osg::Vec3(-0.5f, -0.7f, 1.0f);
|
|
|
|
(*coords)[8] = osg::Vec3( 0.0f, -0.5f, 1.5f);
|
|
|
|
(*coords)[9] = osg::Vec3( 0.0f, 0.5f, 1.5f);
|
|
|
|
|
|
|
|
for (i = 0; i < 10; i++)
|
|
|
|
{
|
|
|
|
(*coords)[i] = (*coords)[i] * scale + offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// create geometry
|
2013-06-26 00:10:24 +08:00
|
|
|
osg::ref_ptr<deprecated_osg::Geometry> geometry = new deprecated_osg::Geometry();
|
|
|
|
|
2005-11-18 04:22:55 +08:00
|
|
|
geometry->addPrimitiveSet(primitives);
|
2013-06-26 00:10:24 +08:00
|
|
|
|
2013-06-18 19:18:28 +08:00
|
|
|
geometry->setVertexArray(coords.get());
|
|
|
|
geometry->setVertexIndices(coordIndices.get());
|
2013-06-26 00:10:24 +08:00
|
|
|
|
2013-06-18 19:18:28 +08:00
|
|
|
geometry->setColorArray(colors.get());
|
2013-06-26 00:10:24 +08:00
|
|
|
geometry->setColorBinding(deprecated_osg::Geometry::BIND_OVERALL);
|
|
|
|
|
2013-06-18 19:18:28 +08:00
|
|
|
geometry->setNormalArray(normals.get());
|
2013-06-26 00:10:24 +08:00
|
|
|
geometry->setNormalBinding(deprecated_osg::Geometry::BIND_PER_PRIMITIVE);
|
2005-11-18 04:22:55 +08:00
|
|
|
|
2013-06-18 19:18:28 +08:00
|
|
|
osg::ref_ptr<osg::Geode> geode = new osg::Geode();
|
|
|
|
geode->addDrawable(geometry.get());
|
2013-06-26 00:10:24 +08:00
|
|
|
|
2013-06-18 19:18:28 +08:00
|
|
|
nodes.push_back(geode.get());
|
2005-11-18 04:22:55 +08:00
|
|
|
}
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LayoutAsGrid()
|
|
|
|
{
|
2005-11-18 04:22:55 +08:00
|
|
|
// calculate bounding box
|
|
|
|
osg::BoundingBox bbox;
|
|
|
|
for (NodeIterator node = nodes.begin(); node != nodes.end(); ++node)
|
|
|
|
bbox.expandBy((*node)->getBound());
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2005-11-18 04:22:55 +08:00
|
|
|
// setup grid information
|
|
|
|
osg::Group ** groups = new osg::Group*[GridX * GridY];
|
2003-03-15 04:35:45 +08:00
|
|
|
int i;
|
2005-11-18 04:22:55 +08:00
|
|
|
for (i = 0; i < GridX * GridY; i++)
|
|
|
|
groups[i] = new osg::Group();
|
|
|
|
|
|
|
|
float xGridStart = bbox.xMin();
|
|
|
|
float xGridSize = (bbox.xMax() - bbox.xMin()) / GridX;
|
|
|
|
|
|
|
|
float yGridStart = bbox.yMin();
|
|
|
|
float yGridSize = (bbox.yMax() - bbox.yMin()) / GridY;
|
|
|
|
|
|
|
|
// arrange buildings into right grid
|
|
|
|
for (NodeIterator nodeIter = nodes.begin(); nodeIter != nodes.end(); ++nodeIter)
|
|
|
|
{
|
|
|
|
osg::Node * node = nodeIter->get();
|
|
|
|
osg::Vec3 center = node->getBound().center();
|
2013-06-26 00:10:24 +08:00
|
|
|
|
2005-11-18 04:22:55 +08:00
|
|
|
int x = (int)floor((center.x() - xGridStart) / xGridSize);
|
|
|
|
int z = (int)floor((center.y() - yGridStart) / yGridSize);
|
|
|
|
|
|
|
|
groups[z * GridX + x]->addChild(node);
|
|
|
|
}
|
2013-06-26 00:10:24 +08:00
|
|
|
|
|
|
|
// add nodes to building root
|
2005-11-18 04:22:55 +08:00
|
|
|
for (i = 0; i < GridX * GridY; i++)
|
|
|
|
{
|
|
|
|
osg::StateSet * stateset = new osg::StateSet();
|
|
|
|
|
|
|
|
osg::Material * material = new osg::Material();
|
|
|
|
osg::Vec4 color = osg::Vec4(
|
|
|
|
0.5f + (static_cast<double> (rand()) / (2.0*static_cast<double> (RAND_MAX))),
|
|
|
|
0.5f + (static_cast<double> (rand()) / (2.0*static_cast<double> (RAND_MAX))),
|
|
|
|
0.5f + (static_cast<double> (rand()) / ( 2.0*static_cast<double>(RAND_MAX))),
|
|
|
|
1.0f);
|
2013-06-26 00:10:24 +08:00
|
|
|
|
2005-11-18 04:22:55 +08:00
|
|
|
material->setAmbient(osg::Material::FRONT_AND_BACK, color);
|
|
|
|
material->setDiffuse(osg::Material::FRONT_AND_BACK, color);
|
|
|
|
stateset->setAttributeAndModes(material, osg::StateAttribute::ON);
|
|
|
|
|
|
|
|
groups[i]->setStateSet(stateset);
|
|
|
|
|
|
|
|
if (UseImpostor)
|
|
|
|
{
|
|
|
|
osgSim::Impostor * impostor = new osgSim::Impostor();
|
|
|
|
impostor->setImpostorThreshold(static_cast<float> (Threshold));
|
|
|
|
impostor->addChild(groups[i]);
|
|
|
|
impostor->setRange(0, 0.0f, 1e7f);
|
|
|
|
impostor->setCenter(groups[i]->getBound().center());
|
|
|
|
Root->addChild(impostor);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Root->addChild(groups[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete[] groups;
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main( int argc, char **argv )
|
|
|
|
{
|
|
|
|
// use an ArgumentParser object to manage the program arguments.
|
|
|
|
osg::ArgumentParser arguments(&argc,argv);
|
|
|
|
|
|
|
|
// construct the viewer.
|
2007-01-08 18:00:16 +08:00
|
|
|
osgViewer::Viewer viewer;
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
// add local test manipulator more suitable for testing impostors.
|
2007-01-08 18:00:16 +08:00
|
|
|
viewer.setCameraManipulator(new TestManipulator);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
// load the nodes from the commandline arguments.
|
2015-10-22 21:42:19 +08:00
|
|
|
osg::ref_ptr<osg::Node> model = osgDB::readRefNodeFiles(arguments);
|
2003-03-15 04:35:45 +08:00
|
|
|
if (model)
|
|
|
|
{
|
2005-05-02 03:48:49 +08:00
|
|
|
// the osgSim::InsertImpostorsVisitor used lower down to insert impostors
|
2003-03-15 04:35:45 +08:00
|
|
|
// only operators on subclass of Group's, if the model top node is not
|
|
|
|
// a group then it won't be able to insert an impostor. We therefore
|
|
|
|
// manually insert an impostor above the model.
|
2013-06-19 18:56:31 +08:00
|
|
|
if (dynamic_cast<osg::Group*>(model.get())==0)
|
2003-03-15 04:35:45 +08:00
|
|
|
{
|
|
|
|
const osg::BoundingSphere& bs = model->getBound();
|
|
|
|
if (bs.valid())
|
|
|
|
{
|
|
|
|
|
2005-05-02 03:48:49 +08:00
|
|
|
osgSim::Impostor* impostor = new osgSim::Impostor;
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
// standard LOD settings
|
2013-06-19 18:56:31 +08:00
|
|
|
impostor->addChild(model.get());
|
2003-03-15 04:35:45 +08:00
|
|
|
impostor->setRange(0,0.0f,1e7f);
|
|
|
|
impostor->setCenter(bs.center());
|
|
|
|
|
2016-05-30 19:30:05 +08:00
|
|
|
// impostor specific settings.
|
2003-03-15 04:35:45 +08:00
|
|
|
impostor->setImpostorThresholdToBound(5.0f);
|
|
|
|
|
|
|
|
model = impostor;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we insert an impostor node above the model, so we keep a handle
|
|
|
|
// on the rootnode of the model, the is required since the
|
|
|
|
// InsertImpostorsVisitor can add a new root in automatically and
|
|
|
|
// we would know about it, other than by following the parent path
|
|
|
|
// up from model. This is really what should be done, but I'll pass
|
|
|
|
// on it right now as it requires a getRoots() method to be added to
|
2013-06-26 00:10:24 +08:00
|
|
|
// osg::Node, and we're about to make a release so no new features!
|
2013-06-19 18:56:31 +08:00
|
|
|
osg::ref_ptr<osg::Group> rootnode = new osg::Group;
|
2015-10-22 21:42:19 +08:00
|
|
|
rootnode->addChild(model);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
// now insert impostors in the model using the InsertImpostorsVisitor.
|
2005-05-02 03:48:49 +08:00
|
|
|
osgSim::InsertImpostorsVisitor ov;
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
// traverse the model and collect all osg::Group's and osg::LOD's.
|
|
|
|
// however, don't traverse the rootnode since we want to keep it as
|
|
|
|
// the start of traversal, otherwise the insertImpostor could insert
|
|
|
|
// and Impostor above the current root, making it nolonger a root!
|
|
|
|
model->accept(ov);
|
|
|
|
|
|
|
|
// insert the Impostors above groups and LOD's
|
|
|
|
ov.insertImpostors();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// no user model so we'll create our own world.
|
2005-11-18 04:22:55 +08:00
|
|
|
model = Root = new osg::Group();
|
|
|
|
CreateHouses();
|
|
|
|
LayoutAsGrid();
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// add model to viewer.
|
2015-10-22 21:42:19 +08:00
|
|
|
viewer.setSceneData(model);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2007-01-08 18:00:16 +08:00
|
|
|
return viewer.run();
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|