osg不要让视角穿到地底代码
时间: 2024-02-11 13:05:22 浏览: 142
OSG3.6.0和osgEarth2.10源代码
以下是osg中避免视角穿过地底的示例代码:
```cpp
#include <osgViewer/Viewer>
#include <osg/Node>
#include <osg/Geode>
#include <osg/ShapeDrawable>
#include <osg/PositionAttitudeTransform>
#include <osgDB/ReadFile>
#include <osg/MatrixTransform>
#include <osgGA/TrackballManipulator>
#include <osgUtil/CullVisitor>
int main(int argc, char** argv)
{
osg::ref_ptr<osg::Node> scene = osgDB::readNodeFile("cow.osg");
// 设置相机位置和朝向
osg::Vec3d eye(-100.0, 0.0, 100.0);
osg::Vec3d center(0.0, 0.0, 0.0);
osg::Vec3d up(0.0, 0.0, 1.0);
osg::ref_ptr<osgGA::TrackballManipulator> manipulator = new osgGA::TrackballManipulator();
manipulator->setHomePosition(eye, center, up);
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
viewer->setCameraManipulator(manipulator);
// 设置透视投影
double aspectRatio = static_cast<double>(viewer->getCamera()->getViewport()->width()) / static_cast<double>(viewer->getCamera()->getViewport()->height());
double nearDist = 0.1;
double farDist = 10000.0;
double fovY = 30.0;
viewer->getCamera()->setProjectionMatrixAsPerspective(fovY, aspectRatio, nearDist, farDist);
// 设置遮挡剔除
osg::ref_ptr<osgUtil::CullVisitor> cullVisitor = new osgUtil::CullVisitor();
cullVisitor->setCullingMode(osgUtil::CullVisitor::VIEW_FRUSTUM_SIDES_ONLY);
viewer->getCamera()->setCullingMode(osgUtil::CullVisitor::VIEW_FRUSTUM_SIDES_ONLY);
// 加载地形模型
osg::ref_ptr<osg::Node> terrain = osgDB::readNodeFile("terrain.osg");
osg::ref_ptr<osg::MatrixTransform> terrainTransform = new osg::MatrixTransform();
terrainTransform->addChild(terrain);
// 创建根节点
osg::ref_ptr<osg::Group> root = new osg::Group();
root->addChild(terrainTransform);
root->addChild(scene);
viewer->setSceneData(root);
return viewer->run();
}
```
在这个示例代码中,我们设置相机的位置和朝向,同时设置透视投影和遮挡剔除,以避免视角穿过地底。同时,我们加载了地形模型,并将地形模型作为根节点的子节点,以实现地形的显示。
阅读全文