查看: 1325|回复: 1

求助想请大家帮我分析一段代码

[复制链接]

该用户从未签到

发表于 2010-3-11 18:33:12 | 显示全部楼层 |阅读模式
我写了一段创建移动目标,并修改替换的代码,不知可否优化,请高人指点:
//创建单个航迹
osg::Node* createSingleFly(CString HJName,CString AirModelName,
                double lon,double lat, double height,
                double anglex,double angley,double anglez)
{
osg::ref_ptr<osg::Node> flyNode = osgDB::readNodeFile(AirModelName.GetBuffer());
if (!flyNode.valid())
{
  return 0;
}
osg::StateSet* stateset = new osg::StateSet;
stateset->setMode(GL_LIGHTING, osg::StateAttribute::ON);
stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
flyNode->setStateSet(stateset);
osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform;
osg::ref_ptr<osg::MatrixTransform> scaler = new osg::MatrixTransform;
// 模型缩放比例
double size = 100.;
int ppp;
ppp=AirModelName.Find(".");
CString m_TempStr,m_TempStr2;
m_TempStr=AirModelName.Mid(ppp-4,4);
m_TempStr2=AirModelName.Mid(ppp-8,8);
if((m_TempStr==_T("Ah11"))||
  (m_TempStr==_T("c130"))||
  (m_TempStr==_T("dh89"))||
  (m_TempStr==_T("Mv22"))||
  (m_TempStr==_T("Mi24")))
{
  scaler->setMatrix( osg::Matrix::scale( size, size, size ) *
   osg::Matrix::rotate( osg:egreesToRadians( 90.0f ), 0.0f, 0.0f, 1.0f ) *
   osg::Matrix::rotate( osg::DegreesToRadians( 180.0f ), 0.0f, 1.0f, .0f ) *
   osg::Matrix::rotate( osg::DegreesToRadians(180.0f ), 1.0f, 0.0f, .0f ) *
    osg::Matrix::translate(-flyNode.get()->getBound().center()));
}
else if(m_TempStr2==_T("APACHE_L"))
{
  scaler->setMatrix( osg::Matrix::scale( 10, 10, 10 ) *
   osg::Matrix::rotate( osg::DegreesToRadians( 90.0f ), 0.0f, 0.0f, 1.0f ) *
   osg::Matrix::rotate( osg::DegreesToRadians( 180.0f ), 0.0f, 1.0f, .0f ) *
   osg::Matrix::rotate( osg::DegreesToRadians(180.0f ), 1.0f, 0.0f, .0f ) *
   osg::Matrix::translate(-flyNode.get()->getBound().center()));
}
else
{
  scaler->setMatrix( osg::Matrix::scale( size, size, size ) *
   osg::Matrix::rotate( osg::DegreesToRadians( 0.0f ), 0.0f, 0.0f, 1.0f ) *
   osg::Matrix::rotate( osg::DegreesToRadians( 180.0f ), 0.0f, 1.0f, .0f ) *
   osg::Matrix::rotate( osg::DegreesToRadians(-270.0f ), 1.0f, 0.0f, .0f ) *
    osg::Matrix::translate(-flyNode.get()->getBound().center()));
}
scaler->addChild(flyNode.get());
scaler->getOrCreateStateSet()->setMode(GL_RESCALE_NORMAL,osg::StateAttribute::ON);
mt->addChild(scaler.get());
mt->setMatrix(osg::Matrix::rotate(-(anglez+osg::inDegrees(90.0f)),0.0,0.0,1.0)
  *osg::Matrix::rotate(angley,0.0,1.0,0.0)
  *osg::Matrix::rotate((anglex-osg::inDegrees(90.0f)),1.0,0.0,0.0));
osg::Matrixd matrix;
_ellipsoidModel->computeLocalToWorldTransformFromLatLongHeight(osg::inDegrees(lat),
  osg::inDegrees(lon),height,matrix);
mt->setMatrix(matrix);
mt->setName(HJName.GetBuffer());
return mt.release();
}
void add_ES_SingleFly(CString HJName,CString AirModelName,
        double lon,double lat, double height,
        double anglex,double angley,double anglez)
{
_HangjiGroup2->addChild(createSingleFly(HJName,AirModelName,lon,lat, height,anglex,angley,anglez));
}
void update_ES_SingleFly(CString HJName,CString AirModelName,
           double lon,double lat, double height
           ,double anglex,double angley,double anglez)
{
std::string m_SearchName = HJName.GetBuffer();
//创建节点查找访问器
FindNodeVisitor SearchGZ(m_SearchName);
//启动访问器,开始执行遍历
// 是否开机状态
_HangjiGroup2->accept(SearchGZ);
//如果找不到则新画
if(!SearchGZ.getFirst())
  add_ES_SingleFly(HJName,AirModelName,lon,lat, height,anglex,angley,anglez);
else
{
//  _HangjiGroup2->replaceChild(SearchGZ.getFirst(),createSingleFly(HJName,AirModelName,lon,lat,height,anglex,angley,anglez));
  setFlyMatrix((osg::MatrixTransform*)(SearchGZ.getFirst()),AirModelName,lon,lat,height,anglex,angley,anglez);
}
}
//替换模型
void replace_ES_SingleFly(CString HJName,CString AirModelName,
         double lon,double lat, double height,
         double anglex,double angley,double anglez)
{
std::string m_SearchName = HJName.GetBuffer();
//创建节点查找访问器
FindNodeVisitor SearchGZ(m_SearchName);
//启动访问器,开始执行遍历
// 是否开机状态
_HangjiGroup2->accept(SearchGZ);
//如果找到则新画
if(SearchGZ.getFirst())
  _HangjiGroup2->replaceChild(SearchGZ.getFirst(),createSingleFly(HJName,AirModelName,lon,lat,height,anglex,angley,anglez));
}
//修改矩阵
void setFlyMatrix(osg::MatrixTransform* replaceNode,CString AirModelName,
       double lon,double lat, double height,
       double anglex,double angley,double anglez)
{
// 模型缩放比例
double size = 100.;
int ppp;
ppp=AirModelName.Find(".");
CString m_TempStr,m_TempStr2;
m_TempStr=AirModelName.Mid(ppp-4,4);
m_TempStr2=AirModelName.Mid(ppp-8,8);
osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform;
osg::ref_ptr<osg::MatrixTransform> scaler = new osg::MatrixTransform;
if((m_TempStr==_T("Ah11"))||
  (m_TempStr==_T("c130"))||
  (m_TempStr==_T("dh89"))||
  (m_TempStr==_T("Mv22"))||
  (m_TempStr==_T("Mi24")))
{
  scaler->setMatrix( osg::Matrix::scale( size, size, size ) *
   osg::Matrix::rotate( osg::DegreesToRadians( 90.0f ), 0.0f, 0.0f, 1.0f ) *
   osg::Matrix::rotate( osg::DegreesToRadians( 180.0f ), 0.0f, 1.0f, .0f ) *
   osg::Matrix::rotate( osg::DegreesToRadians(180.0f ), 1.0f, 0.0f, .0f ) *
   osg::Matrix::translate(-replaceNode->getBound().center()));
}
else if(m_TempStr2==_T("APACHE_L"))
{
  scaler->setMatrix( osg::Matrix::scale( 10, 10, 10 ) *
   osg::Matrix::rotate( osg::DegreesToRadians( 90.0f ), 0.0f, 0.0f, 1.0f ) *
   osg::Matrix::rotate( osg::DegreesToRadians( 180.0f ), 0.0f, 1.0f, .0f ) *
   osg::Matrix::rotate( osg::DegreesToRadians(180.0f ), 1.0f, 0.0f, .0f ) *
   osg::Matrix::translate(-replaceNode->getBound().center()));
}
else
{
  scaler->setMatrix( osg::Matrix::scale( size, size, size ) *
   osg::Matrix::rotate( osg::DegreesToRadians( 0.0f ), 0.0f, 0.0f, 1.0f ) *
   osg::Matrix::rotate( osg::DegreesToRadians( 180.0f ), 0.0f, 1.0f, .0f ) *
   osg::Matrix::rotate( osg::DegreesToRadians(-270.0f ), 1.0f, 0.0f, .0f ) *
   osg::Matrix::translate(-replaceNode->getBound().center()));
}
scaler->getOrCreateStateSet()->setMode(GL_RESCALE_NORMAL,osg::StateAttribute::ON);
replaceNode->addChild(scaler.get());
mt->setMatrix(osg::Matrix::rotate(-(anglez+osg::inDegrees(90.0f)),0.0,0.0,1.0)
  *osg::Matrix::rotate(angley,0.0,1.0,0.0)
  *osg::Matrix::rotate((anglex-osg::inDegrees(90.0f)),1.0,0.0,0.0));
mt->getOrCreateStateSet()->setMode(GL_RESCALE_NORMAL,osg::StateAttribute::ON);
replaceNode->addChild(mt.get());
osg::Matrixd matrix;
_ellipsoidModel->computeLocalToWorldTransformFromLatLongHeight(osg::inDegrees(lat),
  osg::inDegrees(lon),height,matrix);
replaceNode->setMatrix(matrix);
}
voi delete_ES_SingleFly()
{
// 清除组节点
if (_HangjiGroup2.valid())
{
  if (_HangjiGroup2->getNumChildren())
  {
   if (!_HangjiGroup2 ->removeChildren( 0,
    _HangjiGroup2->getNumChildren() ))
   {
    return;
   }
  }
}
}

该用户从未签到

发表于 2010-3-21 22:06:04 | 显示全部楼层
实在有点费劲   你能不能用代码模式插入这些东西

还有你的注释  能不能分一下段  清楚的表明你的意图   我耐着性子都看不下去了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表