osg 瞄准镜 车窗镜 透视镜片 做法

/*HUD 和 RTT 的结合*/
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

osg::Node *createCircle(osg::Texture2D*t2d){
  osg::ref_ptrgeode=new osg::Geode;
  osg::StateSet *ss=geode->getOrCreateStateSet();
/*
  osg::ref_ptrimage=osgDB::readImageFile("Images/clockface.JPG");

  if(!image.valid()){
    cout<<"image is NULL"<setImage(0, image);
*/
  ss->setTextureAttributeAndModes(0,t2d, osg::StateAttribute::ON);
  ss->setMode(GL_LIGHTING, osg::StateAttribute::OFF);

  osg::TessellationHints *hints=new osg::TessellationHints;
  hints->setDetailRatio(1.5f);
  geode->addDrawable(new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(400,300,0),150,0.1),hints));
  return geode.release();
}
//创建固定圆盘
osg::Camera*createHUD(osg::Texture2D*t2d){
  osg::ref_ptrcamera=new osg::Camera;
  camera->setViewMatrix(osg::Matrix::identity());
  camera->setAllowEventFocus(false);
  camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
  camera->setViewport(0, 0, 800, 600);
  camera->setProjectionMatrixAsOrtho2D(0, 800, 0, 600);
  camera->setClearMask(GL_DEPTH_BUFFER_BIT);
  camera->setRenderOrder(osg::Camera::POST_RENDER);

  camera->addChild(createCircle(t2d));//设置HUD的内容
  return camera.release();
}
//烘培纹理
void createRtt(osgViewer::Viewer*viewer){
  osg::ref_ptrgp = new osg::Group;
  osg::ref_ptrceep=osgDB::readNodeFile("ceep.ive");
  gp->addChild(ceep);

  if(!viewer){
    return ;
  }
  osg::GraphicsContext::WindowingSystemInterface* wsi=osg::GraphicsContext::getWindowingSystemInterface();
  if(!wsi){
    return;
  }
  unsigned int width,height;
  wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);

  osg::ref_ptrtraits=new osg::GraphicsContext::Traits;
  traits->x=0;
  traits->y=0;
  traits->width=width;
  traits->height=height;
  traits->windowDecoration=false;
  traits->doubleBuffer=true;
  traits->sharedContext=0;

  osg::ref_ptrgc=osg::GraphicsContext::createGraphicsContext(traits);
  if(!gc.valid()){
    return;
  }
//创建主相机
  osg::ref_ptrmaster=new osg::Camera;
  master->setGraphicsContext(gc);
  master->setViewport(0, 0, width,height);
  viewer->addSlave(master);

//创建RTT相机
  osg::ref_ptrrttCamera=new osg::Camera;
  rttCamera->setRenderOrder(osg::Camera::PRE_RENDER);
  rttCamera->setGraphicsContext(gc);
  rttCamera->setViewport(0,0,width,height);
  rttCamera->addChild(ceep);
//做其他效果,基本上都是对addSlave 的参数作手脚
  viewer->addSlave(rttCamera,osg::Matrix::scale(8,8,8),osg::Matrix::identity(),false);

  osg::Texture2D*t2d=new osg::Texture2D;
  t2d->setInternalFormat(GL_RGBA);
  rttCamera->attach(osg::Camera::COLOR_BUFFER, t2d);

  gp->addChild(createHUD(t2d));

  viewer->setSceneData(gp);
  return;
}

int main(int argc, char *argv[])
{
  osg::ref_ptrviewer=new osgViewer::Viewer;
  createRtt(viewer);
  viewer->addEventHandler(new osgViewer::ScreenCaptureHandler);
  return viewer->run();
}



你可能感兴趣的:(OSG)