Skip to content

Commit 7f8ff41

Browse files
mwapka
authored andcommittedJul 5, 2011
- node placement on terrain surface - placement test using generated models
1 parent a2eeab1 commit 7f8ff41

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed
 

‎src/plugins/globe/globe_plugin.cpp

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,16 @@
3535
#include <osgGA/StateSetManipulator>
3636
#include <osgGA/GUIEventHandler>
3737

38+
#include <osg/MatrixTransform>
39+
#include <osg/ShapeDrawable>
3840
#include <osgViewer/Viewer>
3941
#include <osgViewer/ViewerEventHandlers>
4042
#include <osgEarth/Notify>
4143
#include <osgEarth/Map>
4244
#include <osgEarth/MapNode>
4345
#include <osgEarthUtil/EarthManipulator>
46+
#include <osgEarthUtil/ElevationManager>
47+
#include <osgEarthUtil/ObjectPlacer>
4448
#include <osgEarth/TileSource>
4549
#include <osgEarthDrivers/gdal/GDALOptions>
4650
#include <osgEarthDrivers/tms/TMSOptions>
@@ -62,7 +66,9 @@ GlobePlugin::GlobePlugin( QgisInterface* theQgisInterface )
6266
mQActionPointer( NULL ),
6367
viewer(),
6468
mQDockWidget( tr( "Globe" ) ),
65-
mTileSource(0)
69+
mTileSource(0),
70+
mElevationManager( NULL ),
71+
mObjectPlacer( NULL )
6672
{
6773
}
6874

@@ -149,6 +155,52 @@ void GlobePlugin::run()
149155
ControlCanvas* cs = new ControlCanvas( &viewer );
150156
root->addChild( cs );
151157

158+
// model placement utils
159+
mElevationManager = new osgEarthUtil::ElevationManager( mMapNode->getMap() );
160+
mElevationManager->setTechnique( osgEarthUtil::ElevationManager::TECHNIQUE_GEOMETRIC );
161+
mElevationManager->setMaxTilesToCache( 50 );
162+
163+
mObjectPlacer = new osgEarthUtil::ObjectPlacer( mMapNode );
164+
165+
#if 0
166+
// model placement test
167+
168+
// create simple tree model from primitives
169+
osg::TessellationHints* hints = new osg::TessellationHints();
170+
hints->setDetailRatio(0.1);
171+
172+
osg::Cylinder* cylinder = new osg::Cylinder( osg::Vec3(0 ,0, 5), 0.5, 10 );
173+
osg::ShapeDrawable* cylinderDrawable = new osg::ShapeDrawable( cylinder, hints );
174+
cylinderDrawable->setColor( osg::Vec4( 0.5, 0.25, 0.125, 1.0 ) );
175+
osg::Geode* cylinderGeode = new osg::Geode();
176+
cylinderGeode->addDrawable( cylinderDrawable );
177+
178+
osg::Cone* cone = new osg::Cone( osg::Vec3(0 ,0, 10), 4, 10 );
179+
osg::ShapeDrawable* coneDrawable = new osg::ShapeDrawable( cone, hints );
180+
coneDrawable->setColor( osg::Vec4( 0.0, 0.5, 0.0, 1.0 ) );
181+
osg::Geode* coneGeode = new osg::Geode();
182+
coneGeode->addDrawable( coneDrawable );
183+
184+
osg::Group* model = new osg::Group();
185+
model->addChild( cylinderGeode );
186+
model->addChild( coneGeode );
187+
188+
// place models on jittered grid
189+
srand( 23 );
190+
double lat = 47.235;
191+
double lon = 9.36;
192+
double gridSize = 0.001;
193+
for( int i=0; i<10; i++ )
194+
{
195+
for( int j=0; j<10; j++ )
196+
{
197+
double dx = gridSize * ( rand()/( (double)RAND_MAX + 1.0 ) - 0.5 );
198+
double dy = gridSize * ( rand()/( (double)RAND_MAX + 1.0 ) - 0.5 );
199+
placeNode( root, model, lat + i * gridSize + dx, lon + j * gridSize + dy );
200+
}
201+
}
202+
#endif
203+
152204
viewer.setSceneData( root );
153205

154206
// Set a home viewpoint
@@ -265,6 +317,21 @@ void GlobePlugin::help()
265317
{
266318
}
267319

320+
void GlobePlugin::placeNode( osg::Group* root, osg::Node* node, double lat, double lon, double alt /*= 0.0*/ )
321+
{
322+
// get elevation
323+
double elevation = 0.0;
324+
double resolution = 0.0;
325+
mElevationManager->getElevation( lon, lat, 0, NULL, elevation, resolution );
326+
327+
// place model
328+
osg::Matrix mat;
329+
mObjectPlacer->createPlacerMatrix( lat, lon, elevation + alt, mat );
330+
331+
osg::MatrixTransform* mt = new osg::MatrixTransform( mat );
332+
mt->addChild( node );
333+
root->addChild( mt );
334+
}
268335

269336
bool FlyToExtentHandler::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa )
270337
{

‎src/plugins/globe/globe_plugin.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ class QAction;
3232
class QToolBar;
3333
class QgisInterface;
3434

35+
namespace osgEarthUtil
36+
{
37+
class ElevationManager;
38+
class ObjectPlacer;
39+
};
40+
3541
class GlobePlugin : public QObject, public QgisPlugin
3642
{
3743
Q_OBJECT
@@ -62,6 +68,8 @@ class GlobePlugin : public QObject, public QgisPlugin
6268
void extentsChanged();
6369

6470
private:
71+
void placeNode( osg::Group* root, osg::Node* node, double lat, double lon, double alt = 0.0 );
72+
6573
int mPluginType;
6674
//! Pointer to the QGIS interface object
6775
QgisInterface *mQGisIface;
@@ -77,6 +85,10 @@ class GlobePlugin : public QObject, public QgisPlugin
7785
osgEarth::MapLayer* mQgisMapLayer;
7886
//! Tile source
7987
osgEarth::Drivers::QgsOsgEarthTileSource* mTileSource;
88+
//! Elevation manager
89+
osgEarthUtil::ElevationManager* mElevationManager;
90+
//! Object placer
91+
osgEarthUtil::ObjectPlacer* mObjectPlacer;
8092
};
8193

8294

0 commit comments

Comments
 (0)
Please sign in to comment.