Chris 'Xenon' Hanson wrote:
> On 9/14/2011 7:55 AM, Ethan Fahy wrote:
> 
> > To answer my own question, yes, the code snippet that I posted does work 
> > and allows me to attach an object to the terrain using lat lon elevation.  
> > The next step is to be able to place an object on the terrain by only 
> > giving it a lat lot and having it figure out the elevation automatically...
> > 
> 
> http://eckhart.stderr.org/doc/openscenegraph-doc/openscenegraph/classosgSim_1_1HeightAboveTerrain.html
> 
> Use that to query the terrain elevation at a given location.
> 


Thanks!  Just to document my method for anyone else that stumbles upon this:
I read in a terrain.ive file that was converted using osgdem with the 
--geocentric flag (mandatory step).  In order to gain access to the ellipsoid 
model in the terrain.ive file, I did this:

Code:
        osg::CoordinateSystemNode*  terrainTemp = 
dynamic_cast<osg::CoordinateSystemNode*> (terrain);
        osg::ref_ptr<osg::EllipsoidModel> ellipsoid = 
terrainTemp->getEllipsoidModel();




Then I wrote this function that accepts a lat lon in degrees plus the terrain:

Code:
double GetHeightOnTerrain(double lat, double lon, osg::CoordinateSystemNode* 
terrain)
{
double X,Y,Z;
double maxElevation = 100000.0;//arbitrarily large number way above terrain 
[meters]
terrain->getEllipsoidModel()->convertLatLongHeightToXYZ(osg::DegreesToRadians(lat),
                                                                                
                                osg::DegreesToRadians(lon),
                                                                                
                                maxElevation, X,Y,Z);
double hat = 
osgSim::HeightAboveTerrain::computeHeightAboveTerrain(terrain,osg::Vec3(X,Y,Z), 
-1);
return (maxElevation - hat);
} 



When I want to get a terrain height from a lat lon, I do this:

Code:
//Find Terrain Elevation Based on Lat Lon
        float treeLat = 50.0;
        float treeLon = 50.0;
        
        // Place Tree on Ellipsoid Model Based on Lat Lon
        double treeAltitude = GetHeightOnTerrain(treeLat, treeLon, terrainTemp);
        osg::ref_ptr<osg::MatrixTransform> treePlacer = new 
osg::MatrixTransform();
        osg::Matrixd treePosition;
        
ellipsoid->computeLocalToWorldTransformFromLatLongHeight(osg::DegreesToRadians(treeLat),osg::DegreesToRadians(treeLon),
 treeAltitude, treePosition);
        treePlacer->setMatrix(treePosition);  



The treePlacer can be attached to the root alongside the terrain itself, then 
the tree object can be attached to the treePotision as a child in order to 
position the tree on the terrain.

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=42797#42797





_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to