目录
一、使用裁剪节点对模型进行裁剪
1.静态剖切:创建一个ClipPlane即可
- 单面剖切
osg::Plane plane;
osg::Vec3f plane_normal;
plane_normal = osg::Vec3f(1.0, 0., 0.);//切割法线反方向
plane.set(plane_normal, osg::Vec3f(0.,0.,0.));
osg::ref_ptr<osg::ClipPlane> clip_plane = new osg::ClipPlane;
clip_plane->setClipPlane(plane);
//要剖切的节点设置状态
node->getOrCreateStateSet()->setAttributeAndModes(clip_plane, osg::StateAttribute::ON);
- 多面组合静态剖切
osg::ref_ptr<osg::ClipPlane> _1_fixed_plane = new osg::ClipPlane;
osg::ref_ptr<osg::ClipPlane> _2_fixed_plane = new osg::ClipPlane;
osg::Plane plane1;
osg::Vec3f plane1_normal;
plane1_normal = osg::Vec3f(1.0, 0., 0.);
plane1.set(plane1_normal, osg::Vec3f(0.,0.,0.));
osg::Plane plane2;
osg::Vec3f plane2_normal;
plane2_normal = osg::Vec3f(0., 0., -1.0);
plane2.set(plane2_normal, osg::Vec3f(0.,0.,0.));
_1_fixed_plane->setClipPlaneNum(1);
_1_fixed_plane->setClipPlane(plane1);
_2_fixed_plane->setClipPlaneNum(2);
_2_fixed_plane->setClipPlane(plane2);
node->getOrCreateStateSet()->setAttributeAndModes(_1_fixed_plane, osg::StateAttribute::ON);
node->getOrCreateStateSet()->setAttributeAndModes(_2_fixed_plane, osg::StateAttribute::ON);
2.动态剖切
//按上面创建osg::ref_ptr<osg::ClipPlane> clip_plane
osg::ref_ptr<osg::ClipNode> clip_node = new osg::ClipNode;//创建ClipNode
clip_node->addClipPlane(clip_plane);//添加剖切面
node->getOrCreateStateSet()->setAttributeAndModes(clip_plane, osg::StateAttribute::ON);//设置模型剖切
osg::ref_ptr<osgManipulator::Selection> selection = new osgManipulator::Selection();//section 相当于MatrixTransform
selection->addChild(clip_node );
root->addChild(selection);
//然后添加操作器控制selection即可
osg::ref_ptr<osgManipulator::Dragger> dragger = new osgManipulator::TranslateAxisDragger();
dragger->setupDefaultGeometry();
dragger->addTransformUpdating(selection);
dragger->setHandleEvents(true);
root->addChild(dragger);