Incorrect Translation from World to Screen Coordinates for projected map.

classic Classic list List threaded Threaded
4 messages Options
Greg @ NHI Greg @ NHI
Reply | Threaded
Open this post in threaded view
|

Incorrect Translation from World to Screen Coordinates for projected map.

I have two earth files that are the same except for the type:

<map name="OpenStreetMap" type="geocentric" version="2">
 
    <image name="osm_mapnik" driver="xyz">
        <url>http://[abc].tile.openstreetmap.org/{z}/{x}/{y}.png</url>
        <profile>spherical-mercator</profile>
        <cache_policy usage="none"/>
    </image>
   
    <options>
        <lighting>false</lighting>
        <terrain>
            <min_tile_range_factor>8</min_tile_range_factor>
        </terrain>
    </options>
   
</map>

The second map is type 'projected'.

I have a ModelNode placed under the map node.  I need to get the screen coordinates of the node.  My simplified code is below.  If I do this with any map (that I have tested) with type=geocentric, the coordinates convert correctly.  However, with the 'type=projected' map they do not convert to the same screen coordinates.  The world coordinates are the same regardless of type, but the translation to screen coordinates is incorrect.  Note that the ModelNode altitude is 0 and the AltitudeMode is ALTMODE_RELATIVE.

Code:

osgEarth::Annotation::ModelNode* modelNode = dynamic_cast<osgEarth::Annotation::ModelNode*>(node);
if (modelNode != nullptr)
{
        // get node position and convert to screen coordinates
        osgEarth::GeoPoint geoPos = modelNode->getPosition();

        osg::Vec3d worldPos;

        geoPos.getSRS()->getGeographicSRS()->getEllipsoid()->convertLatLongHeightToXYZ(osg::DegreesToRadians(geoPos.y()),
  osg::DegreesToRadians(geoPos.x()), geoPos.z(), worldPos.x(), worldPos.y(), worldPos.z());

       osg::Matrixd transmat =
                camera->getViewMatrix() *
                camera->getProjectionMatrix() *
                viewport->computeWindowMatrix();

        osg::Vec3d screenPos = worldPos * transmat;
}

Sample data:

type=geocentric
worldPos x = -15833.895468, y = -4737028.432224, z = 4256621.983484
screenPos x = 582.840793, y = 280.894097, z = 0.974209

type=projected
worldPos x = -15833.895468, y = -4737028.432224, z = 4256621.983484
screenPos x = -1044.483314, y = 1164.507919, z = 1.000029

I recently updated from OSG Earth 2.7 to 2.10.  I can't be sure but I believe this worked before the update.

Any help would be appreciated.  
gwaldron gwaldron
Reply | Threaded
Open this post in threaded view
|

Re: Incorrect Translation from World to Screen Coordinates for projected map.

Hi Greg,

Call GeoPoint::toWorld() to get worldPos.

"World" coordinates correspond to the actual coordinates in the scene graph. In geocentric, they are ECEF. In projected, they are whatever map coordinates the projection uses (in this case Mercator). The function convertLatLongHeightToXYZ will only produce geocentric coordinates, and thus won't work in the projected case. That's why you shouldn't use it directly.

[edit]
Actually, I left out a step. You must also transform your GeoPoint to the Map's SRS first like so:

GeoPoint pos = modelNode->getPosition();
pos.transform(map->getSRS());
pos.toWorld(worldPos);
Glenn Waldron / Pelican Mapping
Greg @ NHI Greg @ NHI
Reply | Threaded
Open this post in threaded view
|

Re: Incorrect Translation from World to Screen Coordinates for projected map.

Thanks Glenn.

Unfortunately that gives me the same results.  The world coordinates are always the same.  It's the values after converting to screen coordinates that are different and incorrect for the projected map.

Greg
gwaldron gwaldron
Reply | Threaded
Open this post in threaded view
|

Re: Incorrect Translation from World to Screen Coordinates for projected map.

Well, then it figures that the camera/viewpoint matrices differ. Try using the ModelViewMatrix instead of the ViewMatrix.

Did you apply the transformation in my edit?
Glenn Waldron / Pelican Mapping