[osg-users] adding object models in osgEarth best practices

909 views
Skip to first unread message

David Bobavid

unread,
May 2, 2019, 2:21:16 AM5/2/19
to osg-...@lists.openscenegraph.org
Hi,

I'm new to OSG and OSGEarth, and I did some googling, but still have some trouble finding out the proper way to do this.

So I have an osgEarth program, and I want to add some entity models, such as planes, moving around my globe. While I'm good with loading a .earth file for the terrain, or adding the textures/elevation data through code, I'm having some trouble adding models.

From the examples (such as the osgearth_annotation), it seems like models are added by way of Styles:

Code:

Style style;
style.getOrCreate<ModelSymbol>()->autoScale() = true;
style.getOrCreate<ModelSymbol>()->url()->setLiteral("../data/red_flag.osg.50.scale");
ModelNode* modelNode = new ModelNode(mapNode, style);
modelNode->setPosition(GeoPoint(geoSRS, -100, 52));
annoGroup->addChild(modelNode);




Which seems to work all right. I have tried adding another model, such as the cow.osg that comes with the osg data, and it doesn't look so good. For starters, it's very small, even with the scaling. I tried setting a new scale, but it didn't seem to help:

Code:

Style style;
style.getOrCreate<ModelSymbol>()->autoScale() = true;
style.getOrCreate<ModelSymbol>()->url()->setLiteral("../data/cow.osg");
style.getOrCreate<ModelSymbol>()->scale() = 4000;
ModelNode* modelNode = new ModelNode(mapNode, style);
modelNode->setPosition(GeoPoint(geoSRS, -100, 52));
annoGroup->addChild(modelNode);




The cow seems to the same size (tiny!) and even when I zooom in, it's still super small. Also, I tried setting the heading:

Code:

Style style;
style.getOrCreate<ModelSymbol>()->autoScale() = true;
style.getOrCreate<ModelSymbol>()->url()->setLiteral("../data/red_flag.osg.50.scale");
style.getOrCreate<ModelSymbol>()->heading() = 180;
ModelNode* modelNode = new ModelNode(mapNode, style);
modelNode->setPosition(GeoPoint(geoSRS, -100, 52));
annoGroup->addChild(modelNode);




And in this case, it seems like the model is 180degrees from where it was facing, not 180 from North.

So - what is the best way to go about adding a model and orienting it properly? Is this styles approach the correct way? I have seen some reference in another post about an osgEarthUtil::ObjectPlacer, but that class doesn't seem to exist any more.


Thank you!

Cheers,
David

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





_______________________________________________
osg-users mailing list
osg-...@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Eran Cohen

unread,
May 2, 2019, 11:02:03 AM5/2/19
to osg-...@lists.openscenegraph.org
Hi David,

If you're adding a model from code, you can simple load it normally using osgDB::readNodeFile and place it under an osgEarth::GeoTransform to transform to the correct location:


Code:

auto model = osgDB::readNodeFile("path-to-model");

auto geoTransform = new osgEarth::GeoTransform;
geoTransform->addChild(model);

...





GeoTransform can place the model using a GeoPoint and orients correctly so that the Z-axis down is toward the earth.

To scale and rotate the model, simply put it under an additional transform:

Code:

auto model = osgDB::readNodeFile("path-to-model");

auto transform = new osg::MatrixTransform;
transform->setMatrix(osg::Matrix::scale(10, 10, 10));
transform->addChild(model);

auto geoTransform = new osgEarth::GeoTransform;
geoTransform->addChild(transform);

...




Cheers,
Eran

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

David Bobavid

unread,
May 2, 2019, 11:05:41 AM5/2/19
to osg-...@lists.openscenegraph.org
Hi Eran,

Great, thanks so much. I'll give that a shot!

Cheers,
David

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

David Bobavid

unread,
May 2, 2019, 12:11:30 PM5/2/19
to osg-...@lists.openscenegraph.org
Hi Eran,

Ok, dumb follow up - I'm trying to add the geoTransform to my mapnode, and nothing is showing up. Is there something else I should be doing to add this transformed model to my map node?

(Sorry, total newb with osg/osgearth, here)

Thanks again,
David

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

Eran Cohen

unread,
May 2, 2019, 2:12:46 PM5/2/19
to osg-...@lists.openscenegraph.org
Hi David,

It would help if you post the code that doesn't work for you so that we can see whats wrong with it.

Have you set its location using a GeoPoint?


Code:


auto model = osgDB::readNodeFile("path-to-model");

auto geoTransform = new osgEarth::GeoTransform;
geoTransform->addChild(model);

// This sets the location to New ork at an altitude of 1000 meters
auto srs = osgEarth::SpatialReference::get("wgs84);
GeoPoint newYork(srs, -73.935242, 40.730610, 1000);
geoTransform->setLocation(newYork);


...




If this isn't the problem then reply with a snippet of the code that isn't working.

Cheers,
Eran

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

David Bobavid

unread,
May 2, 2019, 2:26:07 PM5/2/19
to osg-...@lists.openscenegraph.org
Hi Eran,

Yes, sure... this is what I'm doing:

First to load my map:

Code:

_map = new osgEarth::Map();

osgEarth::Drivers::GDALOptions basemap;
basemap.url() = "I:/Dev/QtOsgTest1/data/world.tif";
_map->addLayer(new ImageLayer(ImageLayerOptions("basemap", basemap)));

_mapNode = new osgEarth::MapNode(_map);





Then later to load the model:

Code:


auto model = osgDB::readNodeFile("I:/Dev/QtOsgTest1/data/cow.osg");
if (!model) {
qDebug() << "Oh shit, model didn't load!";
}

auto transform = new osg::MatrixTransform;
transform->setMatrix(osg::Matrix::scale(100, 100, 100));
if (!transform->addChild(model)) {
qDebug() << "Failed adding model to matrix transform";
}

auto geoTransform = new osgEarth::GeoTransform;
geoTransform->setTerrain(_mapNode->getTerrain());
if (!geoTransform->setPosition(osgEarth::GeoPoint(_mapNode->getMapSRS()->getGeographicSRS(), -100, 52, 1000))) {
qDebug() << "Setting position failed";
}

if (!geoTransform->addChild(transform)) {
qDebug() << "Adding matrix transform to geotransform failed";
}

_mapNode->addChild(geoTransform);




I don't see the model show up, but I also don't see any of my error statements, either.

Thanks again,
david

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

David Bobavid

unread,
May 2, 2019, 2:36:19 PM5/2/19
to osg-...@lists.openscenegraph.org
Hi Eran,

So, it looks like if I zoom in *a lot* I do see my cow... I was playing around with it again and tried getting in real close, and setting the scale much larger again, and it's showing up. I guess it's a very small model, and I wasn't really looking close enough for it, either.

Thank you again for your help!

Cheers,
David

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

Eran Cohen

unread,
May 2, 2019, 2:46:35 PM5/2/19
to osg-...@lists.openscenegraph.org
Good to hear!
As an aside, if you find yourself dealing with clipping issues when zooming in closely to models, take a look at osgEarth::Util::LogarithmicDepthBuffer.

Cheers,
Eran

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

David Bobavid

unread,
May 2, 2019, 3:46:10 PM5/2/19
to osg-...@lists.openscenegraph.org
Hi Eran,

Ok, thanks! I'll look into that.

Last question (I think) for now... I'm trying to set my cow's orientation. From what you have suggested previously, would I add another MatrixTransform node with the rotations set into it?

Or should I add a PositionAttitudeTransform instead? If so, are there some helper classes that I can use that will create the Quat properly based on my heading, pitch and roll? I've been looking through the headers and cannot seem to find something that will help.

Thank you again!

Cheers,
David

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

Trajce Nikolov NICK

unread,
May 3, 2019, 2:34:51 AM5/3/19
to OpenSceneGraph Users
Hi David,

to set up a Quat with your hpr you can do something like this:
osg::Quat q;
q.makeRotate(h,osg::Vec3(0,0,1),p,osg::Vec3(1,0,0),r,osg::Vec3(0,1,0));

of course you can tune the order

Cheers,
Nick
--
trajce nikolov nick

David Bobavid

unread,
May 3, 2019, 8:44:57 AM5/3/19
to osg-...@lists.openscenegraph.org
Hi Nick,

Thanks - I'll give it a try!

Cheers,
David

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=75946#75946
Reply all
Reply to author
Forward
0 new messages