2007-01-07 05:35:57 +08:00
|
|
|
#include <osgViewer/Viewer>
|
2006-09-19 04:54:48 +08:00
|
|
|
|
|
|
|
#include <osg/Group>
|
|
|
|
#include <osg/Geode>
|
|
|
|
#include <osg/ShapeDrawable>
|
|
|
|
#include <osg/Texture2D>
|
|
|
|
#include <osg/PositionAttitudeTransform>
|
|
|
|
#include <osg/MatrixTransform>
|
|
|
|
#include <osg/CoordinateSystemNode>
|
2006-09-20 21:40:58 +08:00
|
|
|
#include <osg/ClusterCullingCallback>
|
2006-09-19 04:54:48 +08:00
|
|
|
|
|
|
|
#include <osgDB/FileUtils>
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
|
|
|
|
#include <osgText/FadeText>
|
|
|
|
|
|
|
|
#include <osgTerrain/DataSet>
|
|
|
|
|
|
|
|
#include <osgSim/OverlayNode>
|
|
|
|
#include <osgSim/SphereSegment>
|
|
|
|
|
2007-01-07 05:35:57 +08:00
|
|
|
#include <osgGA/TerrainManipulator>
|
2006-09-19 04:54:48 +08:00
|
|
|
|
2007-01-07 05:35:57 +08:00
|
|
|
#include <iostream>
|
2006-09-19 04:54:48 +08:00
|
|
|
|
2007-01-07 05:35:57 +08:00
|
|
|
class MyGraphicsContext {
|
|
|
|
public:
|
|
|
|
MyGraphicsContext()
|
2006-09-19 04:54:48 +08:00
|
|
|
{
|
2007-01-07 05:35:57 +08:00
|
|
|
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
|
|
|
|
traits->x = 0;
|
|
|
|
traits->y = 0;
|
|
|
|
traits->width = 1;
|
|
|
|
traits->height = 1;
|
|
|
|
traits->windowDecoration = false;
|
|
|
|
traits->doubleBuffer = false;
|
|
|
|
traits->sharedContext = 0;
|
|
|
|
traits->pbuffer = true;
|
|
|
|
|
|
|
|
_gc = osg::GraphicsContext::createGraphicsContext(traits.get());
|
|
|
|
|
|
|
|
if (!_gc)
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"Failed to create pbuffer, failing back to normal graphics window."<<std::endl;
|
|
|
|
|
|
|
|
traits->pbuffer = false;
|
|
|
|
_gc = osg::GraphicsContext::createGraphicsContext(traits.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_gc.valid())
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
_gc->realize();
|
|
|
|
_gc->makeCurrent();
|
|
|
|
std::cout<<"Realized window"<<std::endl;
|
|
|
|
}
|
2006-09-19 04:54:48 +08:00
|
|
|
}
|
|
|
|
|
2007-01-07 05:35:57 +08:00
|
|
|
bool valid() const { return _gc.valid() && _gc->isRealized(); }
|
|
|
|
|
2006-09-19 04:54:48 +08:00
|
|
|
private:
|
2007-01-07 05:35:57 +08:00
|
|
|
osg::ref_ptr<osg::GraphicsContext> _gc;
|
2006-09-19 04:54:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
osg::Node* createEarth()
|
|
|
|
{
|
|
|
|
osg::ref_ptr<osg::Node> scene;
|
|
|
|
|
|
|
|
{
|
|
|
|
std::string filename = osgDB::findDataFile("Images/land_shallow_topo_2048.jpg");
|
|
|
|
|
|
|
|
// make osgTerrain::DataSet quieter..
|
|
|
|
osgTerrain::DataSet::setNotifyOffset(1);
|
|
|
|
|
|
|
|
osg::ref_ptr<osgTerrain::DataSet> dataSet = new osgTerrain::DataSet;
|
|
|
|
|
|
|
|
// register the source imagery
|
|
|
|
{
|
|
|
|
osgTerrain::DataSet::Source* source = new osgTerrain::DataSet::Source(osgTerrain::DataSet::Source::IMAGE, filename);
|
|
|
|
|
|
|
|
source->setCoordinateSystemPolicy(osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS);
|
|
|
|
source->setCoordinateSystem(osgTerrain::DataSet::coordinateSystemStringToWTK("WGS84"));
|
|
|
|
|
2007-01-07 05:35:57 +08:00
|
|
|
source->setGeoTransformPolicy(osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS_BUT_SCALE_BY_FILE_RESOLUTION);
|
|
|
|
source->setGeoTransformFromRange(-180.0, 180.0, -90.0, 90.0);
|
2006-09-19 04:54:48 +08:00
|
|
|
|
|
|
|
dataSet->addSource(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up destination database paramters.
|
|
|
|
dataSet->setDatabaseType(osgTerrain::DataSet::LOD_DATABASE);
|
|
|
|
dataSet->setConvertFromGeographicToGeocentric(true);
|
|
|
|
dataSet->setDestinationName("test.osg");
|
|
|
|
|
|
|
|
// load the source data and record sizes.
|
|
|
|
dataSet->loadSources();
|
|
|
|
|
2007-01-07 05:35:57 +08:00
|
|
|
MyGraphicsContext context;
|
2006-09-19 04:54:48 +08:00
|
|
|
dataSet->createDestination(30);
|
|
|
|
|
|
|
|
if (dataSet->getDatabaseType()==osgTerrain::DataSet::LOD_DATABASE) dataSet->buildDestination();
|
|
|
|
else dataSet->writeDestination();
|
|
|
|
|
|
|
|
scene = dataSet->getDestinationRootNode();
|
|
|
|
|
|
|
|
// now we must get rid of all the old OpenGL objects before we start using the scene graph again
|
|
|
|
// otherwise it'll end up in an inconsistent state.
|
|
|
|
scene->releaseGLObjects(dataSet->getState());
|
|
|
|
osg::Texture::flushAllDeletedTextureObjects(0);
|
|
|
|
osg::Drawable::flushAllDeletedDisplayLists(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return scene.release();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-09-20 20:58:34 +08:00
|
|
|
osgText::Text* createText(osg::EllipsoidModel* ellipsoid, double latitude, double longitude, double height, const std::string& str)
|
|
|
|
{
|
|
|
|
double X,Y,Z;
|
|
|
|
ellipsoid->convertLatLongHeightToXYZ( osg::DegreesToRadians(latitude), osg::DegreesToRadians(longitude), height, X, Y, Z);
|
|
|
|
|
2006-09-20 21:40:58 +08:00
|
|
|
|
|
|
|
osgText::Text* text = new osgText::FadeText;
|
|
|
|
|
|
|
|
osg::Vec3 normal = ellipsoid->computeLocalUpVector( X, Y, Z);
|
|
|
|
text->setCullCallback(new osg::ClusterCullingCallback(osg::Vec3(X,Y,Z), normal, 0.0));
|
2006-09-20 20:58:34 +08:00
|
|
|
|
|
|
|
text->setText(str);
|
|
|
|
text->setFont("fonts/arial.ttf");
|
|
|
|
text->setPosition(osg::Vec3(X,Y,Z));
|
|
|
|
text->setCharacterSize(300000.0f);
|
|
|
|
text->setCharacterSizeMode(osgText::Text::OBJECT_COORDS_WITH_MAXIMUM_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT);
|
|
|
|
text->setAutoRotateToScreen(true);
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2006-09-19 04:54:48 +08:00
|
|
|
osg::Node* createFadeText(osg::EllipsoidModel* ellipsoid)
|
|
|
|
{
|
|
|
|
osg::Group* group = new osg::Group;
|
|
|
|
|
2006-09-20 20:58:34 +08:00
|
|
|
group->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
|
|
|
|
|
|
|
|
osg::Geode* geode = new osg::Geode;
|
|
|
|
group->addChild(geode);
|
2006-09-19 04:54:48 +08:00
|
|
|
|
2006-09-20 20:58:34 +08:00
|
|
|
std::vector<std::string> textList;
|
|
|
|
textList.push_back("Town");
|
|
|
|
textList.push_back("City");
|
|
|
|
textList.push_back("Village");
|
|
|
|
textList.push_back("River");
|
|
|
|
textList.push_back("Mountain");
|
|
|
|
textList.push_back("Road");
|
|
|
|
textList.push_back("Lake");
|
2006-09-19 04:54:48 +08:00
|
|
|
|
2006-09-20 20:58:34 +08:00
|
|
|
unsigned int numLat = 15;
|
|
|
|
unsigned int numLong = 20;
|
2006-09-20 23:48:27 +08:00
|
|
|
double latitude = 0.0;
|
|
|
|
double longitude = -100.0;
|
2006-09-20 20:58:34 +08:00
|
|
|
double deltaLatitude = 1.0f;
|
|
|
|
double deltaLongitude = 1.0f;
|
|
|
|
|
|
|
|
unsigned int t = 0;
|
|
|
|
for(unsigned int i = 0; i < numLat; ++i, latitude += deltaLatitude)
|
2006-09-19 04:54:48 +08:00
|
|
|
{
|
2006-09-20 23:48:27 +08:00
|
|
|
double lgnt = longitude;
|
|
|
|
for(unsigned int j = 0; j < numLong; ++j, ++t, lgnt += deltaLongitude)
|
2006-09-20 20:58:34 +08:00
|
|
|
{
|
2006-09-20 23:48:27 +08:00
|
|
|
geode->addDrawable( createText( ellipsoid, latitude, lgnt, 0, textList[t % textList.size()]) );
|
2006-09-20 20:58:34 +08:00
|
|
|
}
|
2006-09-19 04:54:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-07 05:35:57 +08:00
|
|
|
int main(int, char**)
|
2006-09-19 04:54:48 +08:00
|
|
|
{
|
|
|
|
// construct the viewer.
|
2007-01-07 05:35:57 +08:00
|
|
|
osgViewer::Viewer viewer;
|
2006-09-19 04:54:48 +08:00
|
|
|
|
2007-01-07 05:35:57 +08:00
|
|
|
viewer.getCamera()->setComputeNearFarMode(osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES);
|
|
|
|
viewer.getCamera()->setNearFarRatio(0.00001f);
|
2006-09-19 04:54:48 +08:00
|
|
|
|
|
|
|
// read the scene from the list of file specified commandline args.
|
|
|
|
osg::ref_ptr<osg::Node> root = createEarth();
|
|
|
|
|
|
|
|
if (!root) return 0;
|
|
|
|
|
|
|
|
// add a viewport to the viewer and attach the scene graph.
|
|
|
|
viewer.setSceneData(root.get());
|
|
|
|
|
|
|
|
osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(root.get());
|
|
|
|
if (csn)
|
|
|
|
{
|
|
|
|
// add fade text around the globe
|
|
|
|
csn->addChild(createFadeText(csn->getEllipsoidModel()));
|
|
|
|
}
|
|
|
|
|
2007-01-07 05:35:57 +08:00
|
|
|
viewer.setCameraManipulator(new osgGA::TerrainManipulator);
|
2006-09-19 04:54:48 +08:00
|
|
|
|
2007-01-07 05:35:57 +08:00
|
|
|
return viewer.run();
|
2006-09-19 04:54:48 +08:00
|
|
|
}
|