osg水面

来源:互联网 发布:网络商店分类 编辑:程序博客网 时间:2024/04/28 14:17
[cpp] view plaincopyprint?
  1. #include <osgViewer/Viewer>  
  2. #include <osgDB/ReadFile>  
  3. #include <osgGA/TrackballManipulator>  
  4. #include <osgOcean/OceanScene>  
  5. #include <osgOcean/FFTOceanSurface>  
  6. #include <osgViewer/ViewerEventHandlers>  
  7. #include <osg/TextureCubeMap>  
  8. #include <osg/MatrixTransform>  
  9.   
  10. #include "SkyDome.h"  
  11. #include "SphereSegment.h"  
  12.   
  13. osg::ref_ptr<osg::TextureCubeMap> loadMap(){  
  14.     osg::ref_ptr<osg::TextureCubeMap> cubeMap = new osg::TextureCubeMap;  
  15.     cubeMap->setInternalFormat(GL_RGB);  
  16.     cubeMap->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR_MIPMAP_LINEAR);  
  17.     cubeMap->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR);  
  18.     cubeMap->setWrap(osg::Texture::WRAP_S,osg::Texture::CLAMP_TO_EDGE);  
  19.     cubeMap->setWrap(osg::Texture::WRAP_T,osg::Texture::CLAMP_TO_EDGE);  
  20.     cubeMap->setImage(osg::TextureCubeMap::NEGATIVE_X,osgDB::readImageFile("resources/textures/west.png"));  
  21.     cubeMap->setImage(osg::TextureCubeMap::POSITIVE_X,osgDB::readImageFile("resources/textures/east.png"));  
  22.     cubeMap->setImage(osg::TextureCubeMap::NEGATIVE_Y,osgDB::readImageFile("resources/textures/up.png"));  
  23.     cubeMap->setImage(osg::TextureCubeMap::POSITIVE_Y,osgDB::readImageFile("resources/textures/down.png"));  
  24.     cubeMap->setImage(osg::TextureCubeMap::NEGATIVE_Z,osgDB::readImageFile("resources/textures/south.png"));  
  25.     cubeMap->setImage(osg::TextureCubeMap::POSITIVE_Z,osgDB::readImageFile("resources/textures/north.png"));  
  26.     return cubeMap;  
  27. }  
  28. class CameraTrackCallback: public osg::NodeCallback  
  29. {  
  30. public:  
  31.     virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)  
  32.     {  
  33.         if( nv->getVisitorType() == osg::NodeVisitor::CULL_VISITOR )  
  34.         {  
  35.             osgUtil::CullVisitor* cv = static_cast<osgUtil::CullVisitor*>(nv);  
  36.             osg::Vec3f centre,up,eye;  
  37.   
  38.             cv->getRenderStage()->getCamera()->getViewMatrixAsLookAt(eye,centre,up);  
  39.   
  40.             osg::MatrixTransform* mt = static_cast<osg::MatrixTransform*>(node);  
  41.             mt->setMatrix( osg::Matrix::translate( eye.x(), eye.y(), mt->getMatrix().getTrans().z() ) );  
  42.         }  
  43.   
  44.         traverse(node, nv);   
  45.     }  
  46. };  
  47.   
  48.   
  49. class BoatPositionCallback :public osg::NodeCallback{  
  50. public:  
  51.     BoatPositionCallback(osgOcean::OceanScene * scene){  
  52.         _scene = scene;  
  53.     }  
  54.     virtual void operator()(osg::Node* node, osg::NodeVisitor* nv){  
  55.         if(nv->getVisitorType() ==osg::NodeVisitor::UPDATE_VISITOR)  
  56.         {  
  57.             osg::MatrixTransform * mat =dynamic_cast<osg::MatrixTransform*>(node);  
  58.   
  59.             if(mat){  
  60.                 osg::Matrix  matrix = osg::computeLocalToWorld(nv->getNodePath());  
  61.                 osg::Vec3d pos = matrix.getTrans();  
  62.   
  63.                 osg::Vec3f normal;  
  64.                 float height = _scene->getOceanSurfaceHeightAt(pos[0],pos[1],&normal);  
  65.   
  66.                 static float a = 0;  
  67.                 matrix.makeTranslate(osg::Vec3(pos[0]+cosf(a),pos[1]+sinf(a),height));  
  68.   
  69.                 osg::Matrixf rot;  
  70.                 rot.makeIdentity();  
  71.                 rot.makeRotate(normal.x(),osg::X_AXIS,  
  72.                     normal.y(),osg::Y_AXIS,  
  73.                     a,osg::Z_AXIS);  
  74.   
  75.                 a+=0.005;  
  76.                 matrix = rot * matrix;  
  77.                 mat->setMatrix(matrix);  
  78.   
  79.             }  
  80.         }  
  81.     }  
  82.   
  83. private:  
  84.   
  85.     osgOcean::OceanScene * _scene;  
  86.   
  87. };  
  88.   
  89.   
  90. int main()  
  91. {  
  92.     osg::ref_ptr<osgViewer::Viewer>viewer = new osgViewer::Viewer ;  
  93.     viewer->setUpViewInWindow(150,150,1024,768,0);  
  94.   
  95.   
  96.     osg::ref_ptr<osgOcean::FFTOceanSurface>surface = new osgOcean::FFTOceanSurface(64,256,17,osg::Vec2(1.1f,1.1f),12,10,0.8,1e-8,true,2.5,20.0,256);  
  97.     osg::ref_ptr<osgOcean::OceanScene> scene = new osgOcean::OceanScene(surface.get());  
  98.   
  99.   
  100.   
  101.     //天空盒  
  102.     osg::ref_ptr<osg::TextureCubeMap> cubeMAP = loadMap();  
  103.     osg::ref_ptr<SkyDome> sky = new SkyDome(1900,16,16,cubeMAP.get());  
  104.     sky->setNodeMask(scene->getReflectedSceneMask() | scene->getNormalSceneMask());  
  105.   
  106.     osg::MatrixTransform * mat =new osg::MatrixTransform;  
  107.     mat->setDataVariance(osg::Object::DYNAMIC);  
  108.     mat->setMatrix(osg::Matrixf::translate(osg::Vec3d(0,0,0)));  
  109.     mat->addChild(sky.get());  
  110.     mat->setCullCallback(new CameraTrackCallback);  
  111.   
  112.     scene->addChild(mat);  
  113.   
  114.   
  115.   
  116.     //雾效和反射  
  117.     scene->setAboveWaterFog(0.0012,osg::Vec4(0.67,0.87,0.97,1.0));  
  118.     scene->enableReflections(true);  
  119.     surface->setEnvironmentMap(cubeMAP);  
  120.   
  121.     //水花  
  122.     surface->setFoamBottomHeight(2.2);  
  123.     surface->setFoamTopHeight(3.0);  
  124.     surface->enableCrestFoam(true);   
  125.   
  126.   
  127.     viewer->addEventHandler(surface->getEventHandler());  
  128.     viewer->addEventHandler(scene->getEventHandler());  
  129.   
  130.   
  131.     //添加小船  
  132.     osg::ref_ptr<osg::Node> galley = osgDB::readNodeFile("resources/boat/boat.3ds");  
  133.     galley->setNodeMask(scene->getNormalSceneMask() | scene->getReflectedSceneMask() | scene->getRefractedSceneMask());  
  134.   
  135.     osg::MatrixTransform * trans = new osg::MatrixTransform;  
  136.     trans->setMatrix(osg::Matrix::translate(osg::Vec3(0,0,0)));  
  137.     trans->setUpdateCallback(new BoatPositionCallback(scene));  
  138.     trans->addChild(galley);  
  139.   
  140.     scene->addChild(trans);  
  141.   
  142.   
  143.   
  144.   
  145.     osgGA::TrackballManipulator *tb = new osgGA::TrackballManipulator;  
  146.     tb->setHomePosition(osg::Vec3d(0,0,20),osg::Vec3d(0,20,20),osg::Z_AXIS);  
  147.     viewer->setCameraManipulator(tb);  
  148.     viewer->addEventHandler(new osgViewer::StatsHandler);  
  149.   
  150.     viewer->setSceneData(scene.get());  
  151.     viewer->run();  
  152.   
  153.   
  154.   
  155.     return 0;  
  156. }  


其中天空盒用的是osgocean中的例子

效果如下图

\


http://blog.csdn.net/zhuyingqingfen/article/details/8655428