|
楼主 |
发表于 2011-12-12 16:58:21
|
显示全部楼层
fenma3422 发表于 2011-12-9 22:08
应该是你关联的问题,就是说的path动画也得作用到包围盒上,简单的你把包围盒节点作为路径动画所关联的MT节 ...
我添加了一个MT节点了,只是多出了一个运动的物体,包围盒还是不动啊~~我把代码贴上去,谢谢帮我调试一下吧,谢谢啊~!
代码如下:
#include <osg/Group>
#include <osg/Node>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osg/PositionAttitudeTransform>
#include <osg/NodeCallback>
#include <osg/MatrixTransform>
#include <osgGA/TrackballManipulator>
#include <osgViewer/Viewer>
#include <iostream>
#include <osg/io_utils>
#include <osg/Geometry>
#include <osg/Shape>
#include <osg/Geode>
#include <osg/ShapeDrawable>
#include <osg/Object>
#include <Windows.h>
#include <vector>
#include <osg/Timer>
#include <osg/Switch>
#include <osg/NodeVisitor>
#include <osg/BoundingBox>
#include <osg/BoundingSphere>
#include <osgGA/GUIEventHandler>
#include <osg/ComputeBoundsVisitor>
#include <osg/StateSet>
#include <osg/LineWidth>
#include <osg/PolygonMode>
osg::ref_ptr<osg:ositionAttitudeTransform> ganPAT = new osg::PositionAttitudeTransform; //返回boxPAT智能指针
osg::ref_ptr<osg::PositionAttitudeTransform> testPAT = new osg::PositionAttitudeTransform;
osg::ref_ptr<osg::Group> root = new osg::Group; //返回root智能指针,指向osg::Group,所有加入场景中的数据都会加入到Group当中
osg::ref_ptr<osg::Group> testGroup = new osg::Group;
osg::ref_ptr<osg::MatrixTransform> gantrans=new osg::MatrixTransform;
osg::ComputeBoundsVisitor cbv; //调用计算包围盒访问器类
osg::BoundingBox bb; //调用获得包围盒的类
osg::AnimationPath* createpath1()
{
osg::Vec3 yoz(1.0,0.0,0.0), xoz(0.0,1.0,0.0), xoy(0.0,0.0,1.0);
osg::ref_ptr<osg::AnimationPath> path1 = new osg::AnimationPath;
path1->insert(0.0,
osg::AnimationPath::ControlPoint(osg::Vec3(0.0,0.0,0.0),osg:uat(0.0,yoz,0.0,xoz,0.0,xoy)));
//path1->insert(4.0,
// osg::AnimationPath::ControlPoint( osg::Vec3(0.0,0.0,0.0),osg::Quat(0.0,yoz,osg::PI_2/3,xoz,0.0,xoy)));
//path1->insert(6.0,
// osg::AnimationPath::ControlPoint( osg::Vec3(0.0,0.0,0.0),osg::Quat(0.0,yoz,osg::PI_2/6,xoz,0.0,xoy)));
path1->insert(18.0,
osg::AnimationPath::ControlPoint( osg::Vec3(0.0,0.0,0.0),osg::Quat(0.0,yoz,osg::PI_2,xoz,0.0,xoy)));
return path1.release();
}
osg::ref_ptr<osg::Geode> boundingBoxGeode = new osg::Geode;
osg::ref_ptr<osg::Geode> createBoundingBox(osg::BoundingBox &box) //创建包围盒的函数
{
osg::ref_ptr<osg::Geode> boundingBoxGeode = new osg::Geode;
osg::ref_ptr<osg::MatrixTransform> boxBounding=new osg::MatrixTransform;
osg::ref_ptr<osg::Geode> geode = new osg::Geode; //返回几何节点Geode
//根据求得的节点长宽高,画出包围盒
float lengthX = box.xMax() - box.xMin(); //包围盒x方向的最大值减去x方向的最小值
float lengthY = box.yMax() - box.yMin();
float lengthZ = box.zMax() - box.zMin();
osg::ref_ptr<osg::ShapeDrawable> sd = new osg::ShapeDrawable(new osg::Box(box.center(), lengthX, lengthY, lengthZ)); //几何参数指针
osg::ref_ptr<osg::Vec3Array> colors = new osg::Vec3Array; //表示颜色的指针
sd->setColor(osg::Vec4(1,0,0,1)); //将颜色参数传给几何模型
osg::ref_ptr<osg::StateSet> state = sd->getOrCreateStateSet(); //状态设置指针
osg::ref_ptr<osg::PolygonMode> pm = new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode:INE); //多边形模型指针
state->setAttributeAndModes(pm.get());
//设置线宽
osg::ref_ptr<osg::LineWidth> lw = new osg::LineWidth(1.0f);
state->setAttribute(lw.get());
geode->addDrawable(sd.get());
boxBounding->addChild(geode.get());
return geode.get();
}
int main()
{
osg::ref_ptr<osg::MatrixTransform> gan = new osg::MatrixTransform;
gan->addChild(osgDB::readNodeFile("gan.IVE"));
ganPAT->addChild(gan.get()); //获取boxNode节点的参数传到boxPAT节点
//ganPAT->setPosition(osg::Vec3(10.0, 0.0, 0.0)); //将坐标位置参数传到boxPAT节点
ganPAT->setScale(osg::Vec3(1.0, 1.0, 1.0)); //将表示大小的参数传到boxPAT节点
ganPAT->accept(cbv); //计算Group中的包围盒的函数accept传到testGroup中
bb = cbv.getBoundingBox(); //得到包围盒
boundingBoxGeode = createBoundingBox(bb); //Group的几何节点指针boundingBoxGeode
gantrans->addChild(ganPAT.get());
root->addChild(boundingBoxGeode.get()); //得到boundingBoxGeode的参数传到根节点中
root->addChild(gantrans.get()); //将testGroup节点的参数传到根节点root下
osg::ref_ptr<osg::MatrixTransform> zaw1 = new osg::MatrixTransform;
zaw1->setMatrix(osg::Matrix::translate(50.0f,0.0f,100.0f));
zaw1->addChild(osgDB::readNodeFile("zaw1.IVE"));
osg::ref_ptr<osg::MatrixTransform> zaw2 = new osg::MatrixTransform;
zaw2->setMatrix(osg::Matrix::translate(150.0f,0.0f,40.0f));
zaw2->addChild(osgDB::readNodeFile("zaw2.IVE"));
osg::ref_ptr<osg::MatrixTransform> zaw3 = new osg::MatrixTransform;
zaw3->setMatrix(osg::Matrix::translate(-50.0f,0.0f,100.0f));
zaw3->addChild(osgDB::readNodeFile("zaw3.IVE"));
osg::ref_ptr<osg::MatrixTransform> zaw4 = new osg::MatrixTransform;
zaw4->setMatrix(osg::Matrix::translate(200.0f,0.0f,200.0f));
zaw4->addChild(osgDB::readNodeFile("zaw3.IVE"));
osg::AnimationPath* path1 = createpath1();
path1->setLoopMode(osg::AnimationPath::NO_LOOPING);
//将包围盒的MT节点与运动关联起来
gantrans->setUpdateCallback(new osg::AnimationPathCallback(path1));
gantrans->dirtyBound(); //更新testGroup
osg::ref_ptr<osg::Node> axes = new osg::Node;
axes = osgDB::readNodeFile("axes.osg");
//osg::ref_ptr<osg::Group> root =new osg::Group;
root->addChild(gan.get());
root->addChild(axes.get());
root->addChild(zaw1.get());
root->addChild(zaw2.get());
root->addChild(zaw3.get());
root->addChild(zaw4.get());
//申请一个viewer,将root放入。
osgViewer::Viewer viewer;
viewer.setSceneData(root.get());
return viewer.run();
}
|
-
-
zaw3.IVE
1.66 KB, 下载次数: 175, 下载积分: 威望 1
-
-
zaw2.IVE
1.66 KB, 下载次数: 152, 下载积分: 威望 1
-
-
zaw1.IVE
1.66 KB, 下载次数: 161, 下载积分: 威望 1
-
-
gan.IVE
16.01 KB, 下载次数: 156, 下载积分: 威望 1
|