|
本帖最后由 whychina 于 2011-6-3 21:30 编辑
我添加包围盒后编译没问题 ,可运行时自动退出,不知道我包围盒的添加出什么问题了-
- osg::ref_ptr<osg::MatrixTransform> cOSG::DrawModule(int ModuleIndex)
- {
- //每个杆件开始节点,均为绿色
- osg::ref_ptr<osg::MatrixTransform> module=new osg::MatrixTransform;
- //***************设置包围盒参数********************//由我添加的第一部分
- osg::ref_ptr<osg::Geode> box = new osg::Geode;
- osg::ref_ptr<osg::ShapeDrawable> sd = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0f,0.0f,0.0f),1));
- sd->setColor(osg::Vec4(1.0,1.0,0.0,1.0));//设置颜色绿色
- osg::ref_ptr<osg::StateSet> state = sd->getOrCreateStateSet();
- osg::ref_ptr<osg::PolygonMode> pm = new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::LINE);
- state->setAttributeAndModes(pm.get());//也设置线框模式
- osg::ref_ptr<osg::LineWidth> lw =new osg::LineWidth(2.0);
- state->setAttribute(lw.get());//设置线条粗细
- box->addDrawable(sd.get());
- //***************设置包围盒参数********************//由我添加的第一部分(完)
- switch(ModuleIndex)
- {
- case 1:
- {
- osg::ref_ptr<osg::MatrixTransform> mtPole1=new osg::MatrixTransform;
- mtPole1->addChild(DrawBox(osg::Vec3(0.0,0.0,0.0),0.115,0.115,0.115,osg::Vec4(1.0,1.0,0.0,1.0)).get());////////
- module=mtPole1;
- //*************** 计算包围盒********************//由我添加的第二部分
- osg::ref_ptr<osg::MatrixTransform> boxBounding1=new osg::MatrixTransform;//mtPole1 的包围盒节点
- osg::ComputeBoundsVisitor boundvisitor1;
- mtPole1->accept(boundvisitor1);//用headTrans节点去接受这个访问器
- osg::BoundingBox bb1;
- osg::BoundingBox localBB = boundvisitor1.getBoundingBox();
- osg::Matrix localToWorld =
- osg::computeLocalToWorld( mtPole1->getParent(0)->getParentalNodePaths()[0] );
- for (unsigned int i = 0; i < 8; i++)
- {
- bb1.expandBy(localBB.corner(i) * localToWorld);
- }
- boxBounding1->setMatrix(osg::Matrix::scale(bb1.xMax()-bb1.xMin(),bb1.yMax()-bb1.yMin(),bb1.zMax()-bb1.zMin())
- *osg::Matrix::translate(bb1.center()));
- boxBounding1->addChild(box.get());
- mtPole1->addChild(boxBounding1);
- //*************** 计算包围盒********************//由我添加的第二部分(完)
- break;
- }
- default:
- break;
- }
- return module;
- }
-
复制代码 |
|