OSG Clip例程剖析
首先是创建剪切节点的函数代码:
osg::ref_ptr<osg::Node> decorate_with_clip_node(const osg::ref_ptr<osg::Node>& subgraph)
{
osg::ref_ptr<osg::Group> rootnode = new osg::Group; // create wireframe view of the model so the user can see
// what parts are being culled away.
osg::StateSet* stateset = new osg::StateSet;
//osg::Material* material = new osg::Material;
osg::PolygonMode* polymode = new osg::PolygonMode;
polymode->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE);
stateset->setAttributeAndModes(polymode,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); osg::Group* wireframe_subgraph = new osg::Group;
wireframe_subgraph->setStateSet(stateset);
wireframe_subgraph->addChild(subgraph);
rootnode->addChild(wireframe_subgraph); // more complex approach to managing ClipNode, allowing
// ClipNode node to be transformed independently from the subgraph
// that it is clipping. osg::MatrixTransform* transform= new osg::MatrixTransform; osg::NodeCallback* nc = new osg::AnimationPathCallback(subgraph->getBound().center(),osg::Vec3(0.0f,1.0f,1.0f),osg::inDegrees(45.0f));
transform->setUpdateCallback(nc); osg::ClipNode* clipnode = new osg::ClipNode;
osg::BoundingSphere bs = subgraph->getBound();
bs.radius()*= 0.4f; osg::BoundingBox bb;
bb.expandBy(bs); clipnode->createClipBox(bb);
clipnode->setCullingActive(false); transform->addChild(clipnode);
rootnode->addChild(transform); // create clipped part.
osg::Group* clipped_subgraph = new osg::Group; clipped_subgraph->setStateSet(clipnode->getStateSet());
clipped_subgraph->addChild(subgraph);
rootnode->addChild(clipped_subgraph); return rootnode;
}
经过梳理发现这段代码主要有两大部分组成:根节点下包含线框子图wireframe_subgraph和裁剪子图clipped_subgraph两部分。他们分别负责绘制线框模型和裁剪模型。
通过将线框子图的状态集设置为osg::PolygonMode::LINE,并将函数的参数osg::Node& subgraph加入到其下作为子节点。
声明转换矩阵transform,并设置其模拟路径回调函数。声明裁剪节点clipnode,并创建其边界盒(边界正方体),设置边界盒的边长为函数的参数osg::Node& subgraph的边界球半径的0.4倍。将裁剪节点clipnode作为转换矩阵transform的字节点,以使其按照模拟回调函数不断地进行旋转裁剪,显示动态裁剪效果。
最后将已裁剪的节点clipped_subgraph的状态集设置为裁剪节点clipnode的状态集。并将其加入到根节点。
利用绘图工具对以上函数的对象关系进行梳理分析如下:
注意在上图中,裁剪节点并没有包含函数参数subgraph作为其子节点。它仅仅是一个旋转的裁剪盒子。
下一个函数是简单裁剪函数,与以上函数不同的是,它没有显示模型的线框模式,而是对模型直接进行裁剪。其函数代码如下:
osg::ref_ptr<osg::Node> simple_decorate_with_clip_node(const osg::ref_ptr<osg::Node>& subgraph)
{
osg::ref_ptr<osg::Group> rootnode = new osg::Group; // more complex approach to managing ClipNode, allowing
// ClipNode node to be transformed independently from the subgraph
// that it is clipping. osg::MatrixTransform* transform= new osg::MatrixTransform; osg::NodeCallback* nc = new osg::AnimationPathCallback(subgraph->getBound().center(),osg::Vec3(0.0f,0.0f,1.0f),osg::inDegrees(45.0f));
transform->setUpdateCallback(nc); osg::ClipNode* clipnode = new osg::ClipNode;
osg::BoundingSphere bs = subgraph->getBound();
bs.radius()*= 0.4f; osg::BoundingBox bb;
bb.expandBy(bs); clipnode->createClipBox(bb);
clipnode->setCullingActive(false); transform->addChild(clipnode);
rootnode->addChild(transform); // create clipped part.
osg::Group* clipped_subgraph = new osg::Group; clipped_subgraph->setStateSet(clipnode->getStateSet());
clipped_subgraph->addChild(subgraph);
rootnode->addChild(clipped_subgraph); return rootnode;
}
可以看到,简单裁剪函数去掉了第一个函数中5~16行创建线框模型的代码。分别用正常和简单模式运行程序,结果如下:
普通模式
简单模式
总结:
程序通过函数参数subgraph获得其边界球,并取其边界球半径的0.4倍建立边界盒。将上述边界盒设为裁剪节点clipnode的裁剪盒。建立转换矩阵transform,设置其模拟路径(动态旋转效果),并将clipnode作为其子节点。将裁剪节点clipnode的状态集设置为裁剪后的子图clipped_subgraph的状态集,以完成对子图参数subgrpah的动态裁剪。最后将转换矩阵transform,裁剪后的子图clipped_subgraph,线框模型添加到根节点中,实现最终的裁剪效果。通过设置转换矩阵transform的模拟回调路径,或修改裁剪节点clipnode的大小和形状,可修改动态裁剪的效果。