一、背景
如果使用camera->setViewMatrixAsLookAt(eye,center,up)看向某个点,这个时候就需要知道当前位置头顶正上方的向量是什么,这个向量可不是osg::Vec3(0,0,1),Z轴正方向从地心指向正北,此时要计算XYZ点的up向量,其实就是地心指向XYZ点的向量,也就是up=osg::Vec3(X,Y,Z)。
二、代码
inline osg::Vec3d EllipsoidModel::computeLocalUpVector(double X, double Y, double Z) const
{
// Note latitude is angle between normal to ellipsoid surface and XY-plane
double latitude;
double longitude;
double altitude;
convertXYZToLatLongHeight(X,Y,Z,latitude,longitude,altitude);
// Compute up vector
return osg::Vec3d( cos(longitude) * cos(latitude),
sin(longitude) * cos(latitude),
sin(latitude));
}
//通过XYZ点计算该点UP方向向量
osg::Vec3d cameraAndTargetEvent::UpInWorldFromXYZ(osg::Vec3d pos)
{
osg::EllipsoidModel* em = new osg::EllipsoidModel();
return osg::Vec3d(em->computeLocalUpVector(pos.x(), pos.y(), pos.z()));
}
其实通过上面代码求地球表面物体向上的方向向量,完全没有必要,直接用该点的XYZ坐标即为向上的方向向量。