|
本帖最后由 sy2178668 于 2013-1-8 23:18 编辑
我在osg自带的osgviewerMFC程序里加了一个粒子效果,运行没有问题,当一卸载后,就有可能会崩溃,百思不得其解,望高手解答,下附代码和崩溃截图
这是创建粒子的函数:- osg::ref_ptr<osg::Group> CreateParticle::createInitPar()
- {
- osg::ref_ptr<osg::Group> parRoot = new osg::Group;
- osgParticle::Particle ptemplate;
- ptemplate.setLifeTime(2.0);
- ptemplate.setSizeRange(osgParticle::rangef(0.1,0.1));
- ptemplate.setAlphaRange(osgParticle::rangef(1.0,0.0));
- ptemplate.setColorRange(osgParticle::rangev4(osg::Vec4(1.0,1.0,1.0,1.0),osg::Vec4(1.0,1.0,1.0,0.0)));
- ptemplate.setRadius(0.05);
- ptemplate.setMass(0.05);
- osg::ref_ptr<osgParticle::ParticleSystem> ps = new osgParticle::ParticleSystem;
- ps->setDefaultAttributes("Images/smoke.rgb",false,false);
- ps->setDefaultParticleTemplate(ptemplate);
- osg::ref_ptr<osgParticle::ModularEmitter> emitter = new osgParticle::ModularEmitter;
- emitter->setLifeTime(2.0);
- emitter->setStartTime(0.0);
- emitter->setEndless(true);
- emitter->setParticleSystem(ps.get());
- osg::ref_ptr<osgParticle::RandomRateCounter> counter = new osgParticle::RandomRateCounter;
- counter->setRateRange(10000.0,10000.0);
- emitter->setCounter(counter.get());
- osg::ref_ptr<osgParticle::SectorPlacer> placer = new osgParticle::SectorPlacer;
- placer->setCenter(osg::Vec3(0.0,0.0,0.0));
- placer->setRadiusRange(0.0,0.05);
- emitter->setPlacer(placer.get());
- osg::ref_ptr<osgParticle::RadialShooter> shooter = new osgParticle::RadialShooter;
- shooter->setInitialSpeedRange(40.0,70.0);
- emitter->setShooter(shooter.get());
- osg::ref_ptr<osgParticle::ModularProgram> program = new osgParticle::ModularProgram;
- program->setParticleSystem(ps);
- osg::ref_ptr<osgParticle::AccelOperator> ao = new osgParticle::AccelOperator;
- ao->setToGravity(1);
- program->addOperator(ao);
- osg::ref_ptr<osgParticle::FluidFrictionOperator> ffo = new osgParticle::FluidFrictionOperator;
- ffo->setFluidViscosity(1.8e-5);
- ffo->setFluidDensity(1.2929);
- ffo->setWind(osg::Vec3(0.f,0.f,0.f));
- program->addOperator(ffo);
- osg::ref_ptr<osgParticle::ParticleSystemUpdater> psu = new osgParticle::ParticleSystemUpdater;
- psu->addParticleSystem(ps.get());
-
- osg::Geode* geode = new osg::Geode;
- geode->addDrawable(ps.get());
- parRoot->addChild(emitter.get());
- parRoot->addChild(program.get());
- parRoot->addChild(geode);
- parRoot->addChild(psu.get());
- return parRoot;
- }
复制代码 这是移除和添加粒子代码,其中parGroup是专门存放粒子的组节点,其父节点是场景总节点:- parGroup->removeChildren(0, parGroup->getNumChildren());
- parGroup->addChild(cp.createInitPar().get());
复制代码 这是运行截图:
这是崩溃的位置。。。:
崩溃位置1:
崩溃位置2:
崩溃位置3:
我怀疑出现这个问题有可能是以下几个原因:
1:粒子释放的时候释放不干净;
2:由于每帧更新的粒子数目太多,所以释放会出问题;
3:osg渲染是在- m_ThreadHandle = (HANDLE)_beginthread(&CCoreOSG::Render, 0, m_OSG) ;
复制代码 线程里渲染的,是不是释放和这个线程有冲突,因为经常死在这里。。。
很纳闷,求解答 |
|