Hello everyone!
I write a program about osgAL.Now I hava a problem:In my program,it can't play 
two media at the same time. Who can tell me what I should do?The code :
/*
*Write by FlySky
*
[EMAIL PROTECTED]  http://ww.osgChina.org   
*
*/
 
#include <osgAL/SoundNode>
#include <osgAL/SoundRoot>
#include <osgAL/SoundManager>
#include <osgAL/SoundState>
#include <osgAL/Version>
#include <osg/DeleteHandler>
#include <osg/Notify>
#include <osg/MatrixTransform>
#include <osg/PositionAttitudeTransform>
#include <osg/Geometry>
#include <osg/Geode>
#include <osgUtil/Optimizer>
#include <osgDB/Registry>
#include <osgDB/ReadFile>
#include <osgViewer/ViewerEventHandlers>
#include <osgViewer/Viewer>

osg::ref_ptr<osgAL::SoundNode> createSound(const std::string& file);
 

osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float 
radius,double looptime)
{
 osg::AnimationPath* animationPath = new osg::AnimationPath;
 animationPath->setLoopMode(osg::AnimationPath::LOOP);
 int numSamples = 40;
 float yaw = 0.0f;
 float yaw_delta = 2.0f*osg::PI/((float)numSamples-1.0f);
 float roll = osg::inDegrees(30.0f);
 double time=0.0f;
 double time_delta = looptime/(double)numSamples;
 for(int i=0;i<numSamples;++i)
 {
  osg::Vec3 position(center+osg::Vec3(sinf(yaw)*radius,cosf(yaw)*radius,0.0f));
  osg::Quat 
rotation(osg::Quat(roll,osg::Vec3(0.0,1.0,0.0))*osg::Quat(-(yaw+osg::inDegrees(90.0f)),osg::Vec3(0.0,0.0,1.0)));
  
animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation));
  yaw += yaw_delta;
  time += time_delta;
 }
 return animationPath;    
}

osg::Node* createBase(const osg::Vec3& center,float radius)
{
 int numTilesX = 10;
 int numTilesY = 10;
 float width = 2*radius;
 float height = 2*radius;
 osg::Vec3 v000(center - osg::Vec3(width*0.5f,height*0.5f,0.0f));
 osg::Vec3 dx(osg::Vec3(width/((float)numTilesX),0.0,0.0f));
 osg::Vec3 dy(osg::Vec3(0.0f,height/((float)numTilesY),0.0f));
 osg::Vec3Array* coords = new osg::Vec3Array;
 int iy;
 for(iy=0;iy<=numTilesY;++iy)
 {
  for(int ix=0;ix<=numTilesX;++ix)
  {
   coords->push_back(v000+dx*(float)ix+dy*(float)iy);
  }
 }
 osg::Vec4Array* colors = new osg::Vec4Array;
 colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); // white
 colors->push_back(osg::Vec4(0.0f,0.0f,0.0f,1.0f)); // black
 int numColors=colors->size();

 int numIndicesPerRow=numTilesX+1;
 osg::UByteArray* coordIndices = new osg::UByteArray; 
 osg::UByteArray* colorIndices = new osg::UByteArray;
 for(iy=0;iy<numTilesY;++iy)
 {
  for(int ix=0;ix<numTilesX;++ix)
  {
   coordIndices->push_back(ix    +(iy+1)*numIndicesPerRow);
   coordIndices->push_back(ix    +iy*numIndicesPerRow);
   coordIndices->push_back((ix+1)+iy*numIndicesPerRow);
   coordIndices->push_back((ix+1)+(iy+1)*numIndicesPerRow);
   colorIndices->push_back((ix+iy)%numColors);
  }
 }

 osg::Vec3Array* normals = new osg::Vec3Array;
 normals->push_back(osg::Vec3(0.0f,0.0f,1.0f));

 osg::Geometry* geom = new osg::Geometry;
 geom->setVertexArray(coords);
 geom->setVertexIndices(coordIndices);
 geom->setColorArray(colors);
 geom->setColorIndices(colorIndices);
 geom->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE);
 geom->setNormalArray(normals);
 geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
 geom->addPrimitiveSet(new 
osg::DrawArrays(osg::PrimitiveSet::QUADS,0,coordIndices->size()));
 osg::Geode* geode = new osg::Geode;
 geode->addDrawable(geom);
 return geode;
}

osg::Node* createMovingModel(const osg::Vec3& center, float radius)
{
 float animationLength = 10.0f;
 osg::AnimationPath* animationPath = 
createAnimationPath(center,radius,animationLength);
 osg::Group* model = new osg::Group;
 osg::Node* glider = osgDB::readNodeFile("glider.osg");
 if (glider)
 {
  const osg::BoundingSphere& bs = glider->getBound();
  float size = radius/bs.radius()*0.3f;
  osg::MatrixTransform* positioned = new osg::MatrixTransform;
  positioned->setDataVariance(osg::Object::STATIC);
  positioned->setMatrix(osg::Matrix::translate(-bs.center())*
   osg::Matrix::scale(size,size,size)*
   osg::Matrix::rotate(osg::inDegrees(-90.0f),0.0f,0.0f,1.0f));
  positioned->addChild(glider);

  osg::ref_ptr<osgAL::SoundNode> sound_node = createSound("high-e.wav");

  positioned->addChild(sound_node.get());
  osg::PositionAttitudeTransform* xform = new osg::PositionAttitudeTransform;   
 
  xform->setUpdateCallback(new 
osg::AnimationPathCallback(animationPath,0.0,1.0));
  xform->addChild(positioned);
  model->addChild(xform);
 }
 osg::Node* cessna = osgDB::readNodeFile("cessna.osg");
 if (cessna)
 {
  const osg::BoundingSphere& bs = cessna->getBound();
  float size = radius/bs.radius()*0.3f;
  osg::MatrixTransform* positioned = new osg::MatrixTransform;
  positioned->setDataVariance(osg::Object::STATIC);
  positioned->setMatrix(osg::Matrix::translate(-bs.center())*
   osg::Matrix::scale(size,size,size)*
   osg::Matrix::rotate(osg::inDegrees(180.0f),0.0f,0.0f,1.0f));
 
  positioned->addChild(cessna);
 
  osg::ref_ptr<osgAL::SoundNode> sound = createSound("bee.wav") ;
  positioned->addChild(sound.get()) ;
 
  osg::MatrixTransform* xform = new osg::MatrixTransform;
  xform->setUpdateCallback(new 
osg::AnimationPathCallback(animationPath,0.0f,2.0));
  xform->addChild(positioned);
  model->addChild(xform);
 }
 return model;
}
 
osg::ref_ptr<osgAL::SoundNode> createSound(const std::string& file)
{

 openalpp::Sample *sample = new openalpp::Sample(file.c_str());
 
 osg::ref_ptr<osgAL::SoundState> sound_state = new osgAL::SoundState("glider");

 sound_state->setSample(sample);

 sound_state->setGain(0.9f);

 sound_state->setPitch(1);

 sound_state->setPlay(true);
 
 sound_state->setLooping(true);
 
 sound_state->allocateSource(10, false);

 sound_state->setReferenceDistance(70);

 sound_state->setRolloffFactor(4);

 sound_state->apply();

 osgAL::SoundManager::instance()->addSoundState(sound_state.get());
 
 osg::ref_ptr<osgAL::SoundNode> sound = new osgAL::SoundNode;
 sound->setSoundState(sound_state.get());

 return sound;
}

osg::Node* createModel()
{
 osg::Vec3 center(0.0f,0.0f,0.0f);
 float radius = 100.0f;
 osg::Group* root = new osg::Group;
 root->addChild(createMovingModel(center,radius*0.8f));
 root->addChild(createBase(center-osg::Vec3(0.0f,0.0f,radius*0.5),radius));
 return root;
}
int main( int argc, char **argv )
{
 osgViewer::Viewer* viewer = new osgViewer::Viewer();
 viewer->addEventHandler(new osgViewer::WindowSizeHandler);
 viewer->addEventHandler(new osgViewer::StatsHandler);
 
 osgAL::SoundManager::instance()->init(16);

 
osgAL::SoundManager::instance()->getEnvironment()->setDistanceModel(openalpp::InverseDistance);

 osgAL::SoundManager::instance()->getEnvironment()->setDopplerFactor(1);
 
 osg::Node* model = createModel();
 if (!model)
 {
  return 1;
 }
 osg::MatrixTransform* rootnode = new osg::MatrixTransform;
 rootnode->setMatrix(osg::Matrix::rotate(osg::inDegrees(30.0f),1.0f,0.0f,0.0f));
 rootnode->addChild(model);

 osg::ref_ptr<osgAL::SoundRoot> sound_root = new osgAL::SoundRoot;
 sound_root->setUpdateEnable(true) ;
 rootnode->addChild(sound_root.get());
 
 osgUtil::Optimizer optimzer;
 optimzer.optimize(rootnode);

 viewer->setSceneData(rootnode);

 viewer->realize();
 viewer->run();

 if (osg::Referenced::getDeleteHandler()) 
 {
  osg::Referenced::getDeleteHandler()->setNumFramesToRetainObjects(0);
  osg::Referenced::getDeleteHandler()->flushAll();
 }
 osgAL::SoundManager::instance()->shutdown();
 return 0 ;
}
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to