|
我在程序里用PositionAttitudeTransform节点addchild(),然后回调,回调的时候再addchild(),是不是回调的时候PositionAttitudeTransform的坐标原点会变化的?具体程序代码如下:
osg::ref_ptr<osg::Group> root = new osg::Group;
osg::ref_ptr<osg:ositionAttitudeTransform> pat2 = new osg::PositionAttitudeTransform();
osg::ref_ptr<osg::Node> tanknode = new osg::Node;
tanknode = osgDB::readNodeFile("T72-tank/t72-tank_des.flt");
if(tanknode.get())
{
osg::StateSet *set = tanknode->getOrCreateStateSet();
osg::Texture2D* tex = new osg::Texture2D;
tex->setImage(osgDB::readImageFile("T72-tank/textures/t72-tank_des02.rgb"));
set->setTextureAttributeAndModes( 0, tex, osg::StateAttribute::ON );
pat2->setPosition(osg::Vec3(2985.f,0.f,230.f));
pat2->addChild(tanknode.get());
pat2->setScale(osg::Vec3(5,5,5));
pat2->setUpdateCallback(new tubcallback());
osgUtil::SmoothingVisitor smooth;
tanknode->accept(smooth);
}
root->addChild(pat2.get());
class tubcallback : public osg::NodeCallback
{
public:
tubcallback():_weizhi(false),_chushi(362972.6,1114926.26,2000.f)
{
_oldposition = osg::Vec3(367772.6f,1108746.26f,2236.0f);
}
void updateparament()
{
///读 入txt赋值给newposition
std::vector<double> ch;
std::vector<double>::iterator iter;
double ival;
std::ifstream infile("../virtual/debug/out.txt");
if(!infile.is_open())
{
std::cerr <<"Error! can not open file!" <<std::endl;
// exit(EXIT_FAILURE);
}
else
{
while(infile>>ival)
{
ch.push_back(ival);
}
// int i=0,j=0;
std::cout<<"ch.size():"<<ch.size()<<std::endl;
for(iter=ch.begin();iter!=ch.end();)
{
// std::cout<<"*iter:"<<*iter<<std::endl;
_newposition.x() = *iter;
_newposition.y() = *(++iter);
// std::cout<<"*iter:"<<*iter<<std::endl;
_newposition.z() = *(++iter);
_speed = *(++iter);
(iter++)++;
}
}
if(_newposition != _oldposition)
{
_weizhi = true;
}
infile.close();
}
virtual void operator()(osg::Node* node,osg::NodeVisitor* nv)
{
updateparament();
if(_weizhi)
{
osg::PositionAttitudeTransform* pat =dynamic_cast<osg::PositionAttitudeTransform*>(node);
if (pat)
{
osg::Vec3d scale(_newposition.x()-_chushi.x(),_newposition.y()-_chushi.y(),_chushi.z());
osg::Vec3d oldscale(_oldposition.x()-_chushi.x(),_oldposition.y()-_chushi.y(),_chushi.z());
_speed = _speed/5;
for(int i = 0;i<10;i++)
{
osgUtil::IntersectVisitor ivXY;
osg::ref_ptr<osg:ineSegment> lineXY = new osg::LineSegment( scale,oldscale);
osg::ref_ptr<osg::LineSegment> lineZ = new osg::LineSegment( scale+osg::Vec3(0.0f,0.0f,1.0f),scale+osg::Vec3(0.0f,0.0f,-1.0f));
ivXY.addLineSegment(lineZ.get());
ivXY.addLineSegment(lineXY.get());
node->accept(ivXY);
if(!ivXY.hits())
{
coords = new osg::Vec3Array;
geom = new osg::Geometry;
geode = new osg::Geode;
colors = new osg::Vec4Array;
normals = new osg::Vec3Array;
coords->push_back(osg::Vec3(fabs(oldscale.x()),fabs(oldscale.y()),oldscale.z()));
coords->push_back(osg::Vec3(fabs(scale.x()),fabs(scale.y()),scale.z()));
geom->setVertexArray(coords.get());
geom->addPrimitiveSet(new osg:rawArrays(osg::PrimitiveSet::LINES,0,2));
colors->push_back(osg::Vec4(1.0,0.0,0.0,1.0));
geom->setColorArray(colors.get());
geom->setColorBinding(osg::Geometry::BIND_OVERALL);
normals->push_back(osg::Vec3(0.0f,1.0f,1.0f));
geom->setNormalArray(normals.get());
geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
geode->addDrawable(geom.get());
pat->addChild(geode.get());
pat->setPosition(osg::Vec3d(fabs(scale.x()),fabs(scale.y()),scale.z()));
}
oldscale = scale;
if(_newposition.x()!=_oldposition.x()&&_newposition.y()==_oldposition.y())
scale = osg::Vec3(scale.x(),scale.y()+_speed,scale.z());
else if(_newposition.x()==_oldposition.x()&&_newposition.y() != _oldposition.y())
scale = osg::Vec3(scale.x()+_speed,scale.y(),scale.z());
else
scale = osg::Vec3(scale.x()+_speed,scale.y()+_speed,scale.z());
}
}
_oldposition = _newposition;
}
traverse(node,nv);
}
private:
osg::ref_ptr<osg::Geometry> geom ;
osg::ref_ptr<osg::Geode> geode;
osg::ref_ptr<osg::Vec3Array> coords;
osg::ref_ptr<osg::Vec4Array> colors;
osg::ref_ptr<osg::Vec3Array> normals;
osg::Vec3d _newposition;
double _speed;
osg::Vec3d _oldposition;
osg::Vec3d _chushi;
bool _weizhi;
}; |
|