OSG RTT 渲染到纹理

[cpp]  view plain  copy
 print ?
  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5. #include   
  6. #include   
  7. #include   
  8. #include   
  9.   
  10. #include   
  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"<
  23.     return;  
  24.   }  
  25.   unsigned int width,height;  
  26.   wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);  
  27.   
  28.   osg::ref_ptrtraits=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_ptrgc=osg::GraphicsContext::createGraphicsContext(traits);  
  38.   if(!gc.valid()){  
  39.     cout<<"gc is NULL" <
  40.     return;  
  41.   }  
  42.   osg::ref_ptrmaster=new osg::Camera;  
  43.   master->setGraphicsContext(gc);  
  44.   master->setViewport(0, 0, width, height);  
  45.   viewer->addSlave(master.get());  
  46.   cout<"  ,  "<
  47.   
  48.   osg::ref_ptrrttCamera=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_ptrroot=new osg::Group;  
  59.   osg::ref_ptrgeode=new osg::Geode;  
  60.   root->addChild(geode);  
  61.   
  62.   osg::ref_ptrgm=new osg::Geometry;  
  63.   geode->addDrawable(gm);  
  64.   
  65.   osg::ref_ptrvertex=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_ptrnorm=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_ptrcoord=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_ptrt2d=new osg::Texture2D;  
  87. /*  osg::ref_ptrimage=osgDB::readImageFile("Images/blueFlowers.png"); 
  88.   if(!image){ 
  89.     cout<<"image is NULL"< 
  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_ptrviewer=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. }  




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

你可能感兴趣的:(OSG)