OSG RTT 渲染到纹理

时间:2021-11-03 16:13:36
[cpp]  view plain  copy  print ?
  1. #include <osg/Camera>  
  2. #include <osg/Group>  
  3. #include <osg/Geometry>  
  4. #include <osgDB/ReadFile>  
  5. #include <osgViewer/Viewer>  
  6. #include <osg/Texture2D>  
  7. #include <osgViewer/ViewerEventHandlers>  
  8. #include <osg/GraphicsContext>  
  9.   
  10. #include <iostream>  
  11.   
  12.   
  13. using namespace std;  
  14.   
  15.   
  16. void CreateTexture(osgViewer::Viewer *viewer){  
  17.   if(!viewer)  
  18.     return;  
  19.   
  20.   osg::GraphicsContext::WindowingSystemInterface *wsi= osg::GraphicsContext::getWindowingSystemInterface();  
  21.   if(!wsi){  
  22.     cout<<"wsi is NULL"<<endl;  
  23.     return;  
  24.   }  
  25.   unsigned int width,height;  
  26.   wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);  
  27.   
  28.   osg::ref_ptr<osg::GraphicsContext::Traits>traits=new osg::GraphicsContext::Traits;  
  29.   traits->x=0;  
  30.   traits->y=0;  
  31.   traits->width=width;  
  32.   traits->height=height;  
  33.   traits->windowDecoration=false;  
  34.   traits->doubleBuffer=true;  
  35.   traits->sharedContext=0;  
  36.   
  37.   osg::ref_ptr<osg::GraphicsContext>gc=osg::GraphicsContext::createGraphicsContext(traits);  
  38.   if(!gc.valid()){  
  39.     cout<<"gc is NULL" <<endl;  
  40.     return;  
  41.   }  
  42.   osg::ref_ptr<osg::Camera>master=new osg::Camera;  
  43.   master->setGraphicsContext(gc);  
  44.   master->setViewport(0, 0, width, height);  
  45.   viewer->addSlave(master.get());  
  46.   cout<<height<<"  ,  "<<width<<endl;  
  47.   
  48.   osg::ref_ptr<osg::Camera>rttCamera=new osg::Camera;  
  49.   rttCamera->setRenderOrder(osg::Camera::PRE_RENDER);//最先渲染  
  50.   rttCamera->setGraphicsContext(gc);  
  51.   rttCamera->setViewport(0, 0, width, height);  
  52.   rttCamera->addChild(osgDB::readNodeFile("ceep.ive"));  
  53.   viewer->addSlave(rttCamera,false);  
  54.   
  55.   
  56.   
  57.   
  58.   osg::ref_ptr<osg::Group>root=new osg::Group;  
  59.   osg::ref_ptr<osg::Geode>geode=new osg::Geode;  
  60.   root->addChild(geode);  
  61.   
  62.   osg::ref_ptr<osg::Geometry>gm=new osg::Geometry;  
  63.   geode->addDrawable(gm);  
  64.   
  65.   osg::ref_ptr<osg::Vec3Array>vertex=new osg::Vec3Array;  
  66.   vertex->push_back(osg::Vec3(-400,200,-300));  
  67.   vertex->push_back(osg::Vec3(400,200,-300));  
  68.   vertex->push_back(osg::Vec3(400,200,300));  
  69.   vertex->push_back(osg::Vec3(-400,200,300));  
  70.   gm->setVertexArray(vertex);  
  71.   
  72.   osg::ref_ptr<osg::Vec3Array>norm=new osg::Vec3Array;  
  73.   norm->push_back(osg::Vec3(0,-1,0));  
  74.   gm->setNormalArray(norm);  
  75.   gm->setNormalBinding(osg::Geometry::BIND_OVERALL);  
  76.   
  77.   osg::ref_ptr<osg::Vec2Array>coord=new osg::Vec2Array;  
  78.   coord->push_back(osg::Vec2(0,0));  
  79.   coord->push_back(osg::Vec2(1,0));  
  80.   coord->push_back(osg::Vec2(1,1));  
  81.   coord->push_back(osg::Vec2(0,1));  
  82.   gm->setTexCoordArray(0, coord);  
  83.   gm->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));  
  84.   gm->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);  
  85.   
  86.   osg::ref_ptr<osg::Texture2D>t2d=new osg::Texture2D;  
  87. /*  osg::ref_ptr<osg::Image>image=osgDB::readImageFile("Images/blueFlowers.png"); 
  88.   if(!image){ 
  89.     cout<<"image is NULL"<<endl; 
  90.     return; 
  91.   } 
  92.   t2d->setImage(0,image); 
  93. */  
  94.   
  95.   t2d->setInternalFormat(GL_RGBA);  
  96. //  viewer->getCamera()->attach(osg::Camera::COLOR_BUFFER, t2d);  
  97.   
  98.   rttCamera->attach(osg::Camera::COLOR_BUFFER,t2d);  
  99.   
  100.   gm->getOrCreateStateSet()->setTextureAttributeAndModes(0, t2d,osg::StateAttribute::ON);  
  101.   
  102.   root->addChild(osgDB::readNodeFile("ceep.ive"));  
  103.   
  104.   viewer->setSceneData(root);  
  105.   return;  
  106.   
  107. }  
  108. int main(int argc, char *argv[])  
  109. {  
  110.   osg::ref_ptr<osgViewer::Viewer>viewer=new osgViewer::Viewer;  
  111.   CreateTexture(viewer);  
  112.   
  113.   viewer->addEventHandler(new osgViewer::WindowSizeHandler);  
  114.   viewer->addEventHandler(new osgViewer::ScreenCaptureHandler);  
  115.   return viewer->run();  
  116.   return 0;  
  117. }  


OSG RTT 渲染到纹理


转载地址:http://blog.csdn.net/zhuyingqingfen/article/details/8241028