查看: 1214|回复: 6

全局变量问题

[复制链接]

该用户从未签到

发表于 2011-7-6 11:40:50 | 显示全部楼层 |阅读模式
class FaSheDanCallback:public osg::NodeCallback
{
public:
FaSheDanCallback():_angle(osg:I),rot(osg::PI/6),zizhu(-osg::PI_2), /*hudu(0.0),*/ /*z(-100.0), */_time(0),x(0.0)/*,y(0.0)/*,zizhu(0.0)*/ {}
virtual void operator()(osg::Node *node, osg::NodeVisitor *nv)
{
  osg::ref_ptr<osg::PositionAttitudeTransform>matrixfashe = dynamic_cast<osg::PositionAttitudeTransform*>(node);
  if(matrixfashe)
  {     
   //直线
   if (z < 1000)
   {   
    matrixfashe->setPosition(osg::Vec3(0, 0, z ));
    //matrixfashe->setAttitude(osg:uat(zizhu,osg::Z_AXIS));
    z += 1;
    zizhu+=osg::PI/30;
    if(zizhu>=2*osg::PI)
    {     
                    zizhu=0.0;
    }   
   }
   
   if((y<200)&&(z>=1000))
   {
   
                x = 200+200*cos(_angle);
                y = 200+200*cos(_angle);
    z =1000+200*sin(_angle);
    matrixfashe->setAttitude(osg::Quat(hudu,osg::Vec3(1,-1,0)));   
   
    matrixfashe->setPosition(osg::Vec3(x,y,z));
    _angle -= osg::PI/200;
    //y+=0.1;
    hudu -= osg::PI/200;
       if(hudu<-osg::PI_2)
     hudu=-osg::PI_2;
    if(_angle<=osg::PI)
    {
    ZhuTuiQi2->removeChild(ZhuTuiQi1.get());
    ZhuYuiTuoLi_lable=true;//对全局变量赋值,但是在主函数中读取不到该变量的值   
    }
     
   }
   
  }
     traverse(node, nv);
}
protected:
  float x;
    float _angle;
  float rot;
    float z1;
  int _time;
  float zizhu;
  
};

我在头文件中定义了这个类,其中    if(_angle<=osg::PI)
    {
    ZhuTuiQi2->removeChild(ZhuTuiQi1.get());
    ZhuYuiTuoLi_lable=true;//对全局变量赋值,但是在主函数中读取不到该变量的值   
    }
中对ZhuYuiTuoLi_lable赋值,而且赋值成功,但是在主函数中无法读取该变量的值,读取的值仍然是处置 false,请问这种问题怎么处理啊,谢谢!

该用户从未签到

发表于 2011-7-6 14:30:02 | 显示全部楼层
您程序本身结构有问题~~~~~

该用户从未签到

 楼主| 发表于 2011-7-6 14:59:19 | 显示全部楼层
本帖最后由 zhoujiajun2010 于 2011-7-6 15:03 编辑

您指的是那个地方啊,能否指点一下啊!我把上述类放到main()函数的前面,同样不行!

该用户从未签到

发表于 2011-7-6 15:37:32 | 显示全部楼层
在您的程序里根本看不到ZhuYuiTuoLi_lable的定义,我如何判断您说的话是正确的

该用户从未签到

 楼主| 发表于 2011-7-7 09:51:30 | 显示全部楼层
本帖最后由 zhoujiajun2010 于 2011-7-7 09:54 编辑

回复 4# array


   
  1. #include<osg/MatrixTransform>
  2. #include<osgGA/MatrixManipulator>
  3. #include <osgAnimation/UpdateCallback>
  4. #include<osg/Geode>
  5. #include<osg/ShapeDrawable>
  6. #include<osgDB/ReadFile>
  7. #include<osgViewer/Viewer>
  8. #include<osg/Image>
  9. #include<osg/Texture2D>
  10. #include<osg/Material>
  11. #include<osg/PositionAttitudeTransform>
  12. #include<osgGA/NodeTrackerManipulator>
  13. #include<osg/Matrix>
  14. #include<osgViewer/ViewerEventHandlers>
  15. #include <osgParticle/ExplosionDebrisEffect>
  16. #include <osgParticle/ExplosionEffect>
  17. #include <osgParticle/SmokeEffect>
  18. #include <osgParticle/FireEffect>
  19. #include<windows.h>
  20. #include<osg/BoundingSphere>

  21. #include <osgUtil/Optimizer>

  22. #include <osg/BlendFunc>
  23. #include <osg/Depth>
  24. #include <osg/Projection>
  25. #include <osg/Camera>
  26. #include <osg/io_utils>
  27. #include <osgText/Text>
  28. #include <sstream>
  29. #include<osg/Node>

  30. #include <osgGA/TrackballManipulator>

  31. #include <osg/Notify>
  32. #include <osgDB/Registry>
  33. #include <osgGA/KeySwitchMatrixManipulator>
  34. #include <osg/Transform>

  35. #include <osgText/Font>
  36. #include "KeyboardHandler.h"

  37. #include <osgParticle/Particle>
  38. #include <osgParticle/ParticleSystemUpdater>
  39. #include <osgParticle/ModularEmitter>
  40. #include <osgParticle/RandomRateCounter>
  41. #include <osgParticle/MultiSegmentPlacer>
  42. #include <osgParticle/PointPlacer>
  43. #include <osgParticle/SectorPlacer>
  44. #include <osgParticle/RadialShooter>
  45. #include <osgParticle/FluidProgram>
  46. #include <osgParticle/ModularProgram>
  47. #include <osgParticle/AccelOperator>
  48. #include <osgParticle/FluidFrictionOperator>
  49. #include <osgParticle/LinearInterpolator>
  50. #include <osgParticle/ParticleSystem>
  51. #include <string>
  52. #include <iostream>
  53. #include <osg/Sequence>

  54. float z=-50.0;
  55. bool ZhuYuiTuoLi_lable;

  56. osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;

  57. osg::ref_ptr<osg::PositionAttitudeTransform> matrixfashe = new osg::PositionAttitudeTransform;
  58. osg::ref_ptr<osg::PositionAttitudeTransform>ZhuTuiQi2=new osg::PositionAttitudeTransform(); // 声明并初始化位置变换节点
  59. osg::ref_ptr<osg::PositionAttitudeTransform>ZhuTuiQi1=new osg::PositionAttitudeTransform(); // 声明并初始化位置变换节点

  60. osg::ref_ptr<osg::PositionAttitudeTransform>ZhuTuiQi1CALLback=new osg::PositionAttitudeTransform();
  61. osg::ref_ptr<osg::PositionAttitudeTransform>ZhuTuiQi2CALLback=new osg::PositionAttitudeTransform();

  62. osg::ref_ptr<osg::Node> dantou=osgDB::readNodeFile("MuBiaoDan/Mdantou1.obj");
  63. osg::ref_ptr<osg::Node> zhuituiqi1= osgDB::readNodeFile("MuBiaoDan/Mzhutuiqi11.obj");
  64. osg::ref_ptr<osg::Node> zhuituiqi2= osgDB::readNodeFile("MuBiaoDan/Mzhutuiqi21.obj");


  65. unsigned int width,height;
  66. unsigned int count = 0;
  67. int ex = 0;
  68. float pos = 0.0;
  69. int cnt= 0;
  70. float dis = 0.0;
  71. int bait = 0;
  72. float hudu=0.0;
  73. float zizhu=0.0;

  74. float y=0.0;
  75. //float x=0.0;
  76. int IM1=0;
  77. int IM2=0;

  78. int IN1=0;
  79. int IN2=0;




  80. //---------------------------------发射烟雾-----------------------------//

  81. void createDarkSmoke(osg::ref_ptr<osg::MatrixTransform> smokeNode)
  82. {

  83. osg::ref_ptr<osg::Geode> geode = new osg::Geode;

  84. //创建粒子系统模板
  85. osgParticle::Particle ptemplate;
  86. //设置粒子形状
  87. ptemplate.setShape(osgParticle::Particle::QUAD);
  88. //设置生命周期
  89. ptemplate.setLifeTime(50.0);//单位:秒
  90. //设置粒子大小变化范围
  91. ptemplate.setSizeRange(osgParticle::rangef(10.0, 260.0));//单位:米
  92. //设置粒子Alpha变化范围
  93. // ptemplate.setAlphaRange(osgParticle::rangef(0.0f, 1.0f));
  94. /* //设置粒子颜色变化范围
  95. ptemplate.setColorRange(osgParticle::rangev4(
  96. osg::Vec4(1.0f, 0.5f, 0.3f, 1.0f),
  97. osg::Vec4(0.0f, 0.7f, 1.0f, 0.0f)));*/
  98. //设置半径
  99. ptemplate.setRadius(0.2f);
  100. //设置重量
  101. ptemplate.setMass(10.0f);//单位:千克
  102. //设置位置
  103. ptemplate.setPosition(osg::Vec3(0.0f, 0.0f, 0.0f));
  104. //初速度
  105. ptemplate.setVelocity(osg::Vec3(1.0f, 1.0f, 1.0f));
  106. ptemplate.setSizeInterpolator(new osgParticle::LinearInterpolator);
  107. ptemplate.setAlphaInterpolator(new osgParticle::LinearInterpolator);
  108. //创建粒子系统
  109. osg::ref_ptr<osgParticle::ParticleSystem> ps = new osgParticle::ParticleSystem();
  110. ps->setDataVariance(osg::Node::STATIC);
  111. //设置材质,是否放射粒子,是否添加光照
  112. ps->setDefaultAttributes("11.bmp", true, false);
  113. //加入模板
  114. ps->setDefaultParticleTemplate(ptemplate);

  115. //创建发射器和计数器,调整每一帧增加的粒子的数目
  116. osg::ref_ptr<osgParticle::RandomRateCounter> counter = new osgParticle::RandomRateCounter();
  117. //设置每秒添加的粒子的个数
  118. counter->setRateRange(0, 10);
  119. //每秒新生成的粒子范围
  120. counter->setDataVariance(osg::Node::DYNAMIC);

  121. //设置一个点放置器
  122. osg::ref_ptr<osgParticle::PointPlacer> placer = new osgParticle::PointPlacer();
  123. //设置位置
  124. placer->setCenter(osg::Vec3(0.0f, 0.0f, 0.05f));
  125. placer->setDataVariance(osg::Node::DYNAMIC);

  126. //创建弧度发射器
  127. osg::ref_ptr<osgParticle::RadialShooter> shooter = new osgParticle::RadialShooter();
  128. //设置发射器速度变化范围
  129. shooter->setInitialSpeedRange(10, 15);//单位:米/秒
  130. //设置发射器属性
  131. shooter->setDataVariance(osg::Node::DYNAMIC);
  132. shooter->setThetaRange(0.1f,-0.1f);//弧度值,与Z轴夹角0.392699f
  133. shooter->setPhiRange(0.1f,-0.1f);

  134. //创建粒子放射器(包括计数器、放置器和发射器)
  135. osg::ref_ptr<osgParticle::ModularEmitter> emitter = new osgParticle::ModularEmitter();
  136. emitter->setDataVariance(osg::Node::DYNAMIC);
  137. emitter->setCullingActive(false);

  138. //关联粒子系统
  139. emitter->setParticleSystem(ps.get());
  140. //关联计数器
  141. emitter->setCounter(counter.get());
  142. //关联点放置器
  143. emitter->setPlacer(placer.get());
  144. //关联发射器
  145. emitter->setShooter(shooter.get());
  146. smokeNode->addChild(emitter.get());
  147. osg::ref_ptr<osgParticle::ModularProgram> program = new osgParticle::ModularProgram();
  148. program->setParticleSystem(ps.get());

  149. osg::ref_ptr<osgParticle::ParticleSystemUpdater> psu = new osgParticle::ParticleSystemUpdater();
  150. psu->addParticleSystem(ps.get());
  151. smokeNode->addChild(psu.get());
  152. smokeNode->addChild(program.get());
  153. geode->addDrawable(ps);
  154. smokeNode->addChild(geode);

  155. }



  156. //更新回调1
  157. class ZhuTuiQi1Callback:public osg::NodeCallback
  158. {
  159. public:

  160. ZhuTuiQi1Callback():z1(1000.0),x(0),y(0),_angle(osg::PI),s(0),zs(0),s1(0){}
  161. virtual void operator()(osg::Node *node, osg::NodeVisitor *nv)
  162. {
  163. osg::PositionAttitudeTransform *zhutuiqi = dynamic_cast<osg::PositionAttitudeTransform*>(node);
  164. if(zhutuiqi)
  165. {
  166. if(z>=1000)
  167. {
  168. if(z1<=1150)
  169. {
  170. x = 150+150*cos(_angle);
  171. y = 150+150*cos(_angle);
  172. z1 =1000+150*sin(_angle);
  173. zhutuiqi->setPosition(osg::Vec3(x, y, z1 ));

  174. _angle -= osg::PI/500;
  175. if(_angle<=osg::PI_2)
  176. _angle=osg::PI_2;
  177. }

  178. if(z1>=1150)
  179. {
  180. x = 150+s;
  181. y = 150+s;
  182. z1 =1150-zs*s1/2;
  183. zhutuiqi->setPosition(osg::Vec3(x, y, z1 ));
  184. s+=1;
  185. s1+=0.5;
  186. zs+=0.05;
  187. }
  188. }
  189. }
  190. traverse(node, nv);
  191. }

  192. private:
  193. float x;
  194. float y;
  195. float _angle;
  196. float rot;
  197. float z1;
  198. int _time;
  199. float s;
  200. float s1;
  201. float zs;

  202. };


  203. //更新回调2
  204. class ZhuTuiQi2Callback:public osg::NodeCallback
  205. {
  206. public:
  207. ZhuTuiQi2Callback():z1(1200.0),x(200.0),/*y(200.0),*/_angle(osg::PI),s(0),zs(0),s1(0){}
  208. virtual void operator()(osg::Node *node, osg::NodeVisitor *nv)
  209. {
  210. osg::PositionAttitudeTransform *zhutuiqi = dynamic_cast<osg::PositionAttitudeTransform*>(node);
  211. if(zhutuiqi)
  212. {
  213. if(y>=500)
  214. {
  215. y+= 1;
  216. z1 =1200-zs*s1/2;
  217. zhutuiqi->setPosition(osg::Vec3(y, y, z1 ));
  218. s+=1;
  219. s1+=0.5;
  220. zs+=0.05;
  221. }

  222. }
  223. traverse(node, nv);
  224. }

  225. private:
  226. float x;
  227. float _angle;
  228. float rot;
  229. float z1;
  230. int _time;
  231. float s;
  232. float s1;
  233. float zs;

  234. };


  235. /**主更新回调*/

  236. class FaSheDanCallback:public osg::NodeCallback
  237. {
  238. public:
  239. FaSheDanCallback():_angle(osg::PI),rot(osg::PI/6),zizhu(-osg::PI_2), /*hudu(0.0),*/ /*z(-100.0), */_time(0),x(0.0)/*,y(0.0)/*,zizhu(0.0)*/ {}
  240. virtual void operator()(osg::Node *node, osg::NodeVisitor *nv)
  241. {
  242. osg::ref_ptr<osg::PositionAttitudeTransform>matrixfashe = dynamic_cast<osg::PositionAttitudeTransform*>(node);
  243. if(matrixfashe)
  244. {

  245. if (z < 1000)
  246. {
  247. matrixfashe->setPosition(osg::Vec3(0, 0, z ));
  248. z += 1;
  249. zizhu+=osg::PI/30;
  250. if(zizhu>=2*osg::PI)
  251. {
  252. zizhu=0.0;
  253. }
  254. }

  255. if((y<200)&&(z>=1000))
  256. {

  257. x = 200+200*cos(_angle);
  258. y = 200+200*cos(_angle);
  259. z =1000+200*sin(_angle);
  260. matrixfashe->setAttitude(osg::Quat(hudu,osg::Vec3(1,-1,0))); //旋转模型

  261. matrixfashe->setPosition(osg::Vec3(x,y,z));
  262. _angle -= osg::PI/200;
  263. hudu -= osg::PI/200;
  264. if(hudu<-osg::PI_2)
  265. hudu=-osg::PI_2;
  266. if(_angle<=osg::PI)
  267. {
  268. ZhuTuiQi2->removeChild(ZhuTuiQi1.get());
  269. ZhuYuiTuoLi_lable=true;

  270. }
  271. }
  272. else if(y>=200)
  273. {
  274. matrixfashe->setPosition(osg::Vec3(y,y,1200));
  275. matrixfashe->setAttitude(osg::Quat(-osg::PI_2,osg::Vec3(1,-1,0)));
  276. y+=1;
  277. if(y>=500)
  278. matrixfashe->removeChild(ZhuTuiQi2.get());//删除第一节助推器
  279. if(y>=1000)
  280. {
  281. matrixfashe->setAttitude(osg::Quat(zizhu,osg::Vec3(1,-1,0)));
  282. zizhu+=osg::PI/800;
  283. if(zizhu<osg::PI_2)
  284. zizhu=osg::PI_2;
  285. }
  286. if(y>=1200)
  287. {
  288. matrixfashe->setAttitude(osg::Quat(zizhu,osg::Vec3(1,-1,0)));
  289. zizhu+=osg::PI/400;
  290. if(zizhu>=osg::PI_2*3)
  291. zizhu=osg::PI_2*3;
  292. }
  293. }

  294. }
  295. traverse(node, nv);
  296. }

  297. private:
  298. float x;
  299. float _angle;
  300. float rot;
  301. float z1;
  302. int _time;
  303. float zizhu;

  304. };





  305. class BaitstCallback:public osg::NodeCallback
  306. {
  307. public:
  308. BaitstCallback():_angle(1.78),s(1.0),z1(1200){}
  309. virtual void operator()(osg::Node *node, osg::NodeVisitor *nv)
  310. {
  311. osg::PositionAttitudeTransform *matrixbait1 = dynamic_cast<osg::PositionAttitudeTransform*>(node);
  312. if(matrixbait1)
  313. {

  314. if(y>=500)
  315. {
  316. matrixbait1->setAttitude(osg::Quat(-osg::PI_2,osg::Vec3(1,-1,0)));
  317. matrixbait1->setPosition(osg::Vec3(y, y, z1 ));
  318. y+=1.568;
  319. z1-=0.1;
  320. if(z1<=1180)
  321. z1=1180;

  322. matrixbait1->setScale(osg::Vec3(s,s,s));
  323. s+=0.01;
  324. if(s>=3)
  325. s=3;

  326. }
  327. }
  328. traverse(node, nv);
  329. }
  330. private:
  331. float s;
  332. float _angle;
  333. float z1;
  334. };
复制代码

这是头文件里的内容,在头文件的开始定义了bool ZhuYuiTuoLi_lable;然后再主更新回调(class FaSheDanCallback:public osg::NodeCallback
)里进行赋值,在其他函数中无法读取赋值后的ZhuYuiTuoLi_lable值,在主函数中也无法读取赋值后的ZhuYuiTuoLi_lable值,情况就是这样的!不知道这样表达是否清楚!

该用户从未签到

发表于 2011-7-7 10:26:06 | 显示全部楼层
我没有看到您在什么地方尝试读取过这个变量,我也不知道您说的“无法读取”是怎么一种情况。可以说这只能是一个您自己的纰漏造成的小错误而已,请自己仔细排查

该用户从未签到

 楼主| 发表于 2011-7-7 15:17:05 | 显示全部楼层
就是用std::cout<<ZhuYuiTuoLi_lable<<std::endl打印,把这条语句只有放在主更新回调(class FaSheDanCallback:public osg::NodeCallback),打印的值是1,也就是true,发在其他任何地方打印的值都是0,也就是初始值false,还是谢谢锐哥了,我自己在查一下吧,谢谢
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

联系我们

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