查看: 4859|回复: 1

将俯视图渲染到纹理 Render to texture

[复制链接]

该用户从未签到

发表于 2009-6-6 01:56:57 | 显示全部楼层 |阅读模式
//创建一个小型场景..并将俯视图渲染到纹理 保存为bmp图像文件。并使用该纹理渲染一个矩形.
//以前没学过opengl 没怎么用过.不知道texturesize需要是2^n*2^m.在设置texturesize时候 没设置正确 导致错误..
//看来要学好osg得先掌握好opengl
  1. #include<osgViewer/Viewer>
  2. #include <osgViewer/CompositeViewer>
  3. #include<osg/Node>
  4. #include<osg/Geode>
  5. #include<osg/Group>
  6. #include<osg/Geometry>
  7. #include<osg/Matrixd>
  8. #include<osg/Shape>
  9. #include<osg/ShapeDrawable>
  10. #include<osg/PositionAttitudeTransform>
  11. #include<osgDB/WriteFile>
  12. #include<osg/Texture2D>
  13. #include<iostream>
  14. struct MyCameraPostDrawCallback : public osg::Camera::DrawCallback
  15. {
  16.     MyCameraPostDrawCallback(osg::Image* image):
  17.         _image(image)
  18.     {
  19.     }

  20.     virtual void operator () (const osg::Camera& /*camera*/) const
  21.     {
  22.                 osgDB::writeImageFile(*_image,"skyup.bmp");
  23.     }
  24.     osg::Image* _image;
  25. };


  26. osg::ref_ptr<osg::Group> create()
  27. {
  28.         osg::ref_ptr<osg::Group>root=new osg::Group;
  29.         osg::ref_ptr<osg::Geometry> geom=new osg::Geometry;

  30.         osg::ref_ptr<osg::Vec3Array> v=new osg::Vec3Array;

  31.         geom->setVertexArray(v.get());
  32.         GLint count=0;
  33.         for(float i=-100;i<=100;i++)
  34.         {
  35.                 count+=4;
  36.                 v->push_back(osg::Vec3(-100.f,i,0.f));
  37.                 v->push_back(osg::Vec3(100.f,i,0.f));
  38.                
  39.                 v->push_back(osg::Vec3(i,-100.f,0.f));
  40.                 v->push_back(osg::Vec3(i,100.f,0.f));
  41.         }
  42.         osg::ref_ptr<osg::Vec4Array> c=new osg::Vec4Array;

  43.         geom->setColorArray(c.get());
  44.         geom->setColorBinding(osg::Geometry::BIND_OVERALL);

  45.         c->push_back(osg::Vec4(0.0f,10.0f,10.0f,1.f));

  46.         osg::ref_ptr<osg::Vec3Array> n= new osg::Vec3Array;

  47.         geom->setNormalArray(n.get());

  48.         geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
  49.         n->push_back(osg::Vec3(0.f,1.f,0.f));

  50.         geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,count));
  51.        
  52.         osg::Geode *geode =new osg::Geode;
  53.         geode->addDrawable(geom.get());
  54.         root->addChild(geode);

  55.         osg::Box* unitCube = new osg::Box( osg::Vec3(1,1,2), 4);

  56.         osg::ShapeDrawable* unitCubeDrawable = new osg::ShapeDrawable(unitCube);
  57. // 声明Geode类的实例
  58.         osg::Geode* basicShapesGeode = new osg::Geode();

  59. // 将单位立方体添加到Geode中
  60.         basicShapesGeode->addDrawable(unitCubeDrawable);

  61. // 将Geode添加到场景中
  62.         root->addChild(basicShapesGeode);
  63.         osg::Sphere* unitSphere = new osg::Sphere( osg::Vec3(1,1,2), 3);
  64.         osg::ShapeDrawable* unitSphereDrawable = new osg::ShapeDrawable(unitSphere);
  65.         osg::PositionAttitudeTransform* sphereXForm = new osg::PositionAttitudeTransform();
  66.         sphereXForm->setPosition(osg::Vec3(20,-20,1.5));

  67.         osg::Geode* unitSphereGeode = new osg::Geode();
  68.         root->addChild(sphereXForm);

  69.         sphereXForm->addChild(unitSphereGeode);
  70.         unitSphereGeode->addDrawable(unitSphereDrawable);

  71.         return root.get();
  72. }
  73. osg::Group* createHUD(osg::ref_ptr<osg::Group> root)
  74. {

  75.         osg::Group* group=new osg::Group();
  76.         osg::Camera* camera = new osg::Camera;
  77.         osg::Geode *geode =new osg::Geode;
  78.         osg::Texture2D* texture2D = new osg::Texture2D;
  79.         texture2D->setTextureSize(256,256);
  80.         texture2D->setInternalFormat(GL_RGBA);
  81.         texture2D->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
  82.         texture2D->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
  83.         osg::ref_ptr<osg::Geometry> geom=new osg::Geometry;
  84.         osg::ref_ptr<osg::Vec3Array> v=new osg::Vec3Array;
  85.         geom->setVertexArray(v.get());
  86.         v->push_back(osg::Vec3(-100,-100,0));
  87.         v->push_back(osg::Vec3(100,-100,0));
  88.         v->push_back(osg::Vec3(100,100,0));
  89.         v->push_back(osg::Vec3(-100,100,0));

  90.         osg::ref_ptr<osg::Vec4Array> c=new osg::Vec4Array;
  91.         geom->setColorArray(c.get());
  92.         geom->setColorBinding(osg::Geometry::BIND_OVERALL);

  93.         c->push_back(osg::Vec4(1.0f,1.0f,1.0f,0.5f));

  94.         osg::ref_ptr<osg::Vec3Array> n= new osg::Vec3Array;

  95.         geom->setNormalArray(n.get());

  96.         geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
  97.         n->push_back(osg::Vec3(0.f,0.f,1.f));

  98.         geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));

  99.         osg::Vec2Array* texcoords = new osg::Vec2Array(4);
  100.         (*texcoords)[0].set(0.0f,0.0f);
  101.         (*texcoords)[1].set(1.0f,0.0f);
  102.         (*texcoords)[2].set(1.0f,1.0f);
  103.         (*texcoords)[3].set(0.0f,1.0f);
  104.         geom->setTexCoordArray(0,texcoords);
  105.         geom->setDataVariance(osg::Object::DYNAMIC);

  106.         osg::StateSet* state = new osg::StateSet();
  107.         state->setTextureAttributeAndModes(0,texture2D,osg::StateAttribute::ON);

  108.         geom->setStateSet(state);
  109.         geode->addDrawable(geom.get());
  110.         group->addChild(geode);

  111.     camera->setClearColor(osg::Vec4(0.1f,0.1f,0.3f,1.0f));
  112.     camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  113.         camera->setProjectionMatrix(osg::Matrix::ortho2D(-100,100,-100,100));
  114.         camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);

  115.         osg::Matrixd mat;
  116.         osg::Vec3f _eye=osg::Vec3f(0.0,0.0,9.0);
  117.         osg::Vec3f _center=osg::Vec3f(0.0,0.0,0.0);
  118.         osg::Vec3f _up=osg::Vec3f(0.0,1.0,0.0);
  119.         mat.makeLookAt(_eye,_center,_up);
  120.         camera->setViewMatrix(mat);

  121.         camera->setViewport(0,0,256,256);
  122.         camera->setRenderOrder(osg::Camera::POST_RENDER);
  123.         camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
  124.         //camera->attach(osg::Camera::COLOR_BUFFER,texture, 0, 0, false,0,0);
  125.         osg::Image* image=new osg::Image();
  126.         image->allocateImage(256, 256, 1, GL_RGBA, GL_UNSIGNED_BYTE);
  127.         camera->attach(osg::Camera::COLOR_BUFFER,image,0,0);
  128.         texture2D->setImage(0,image);
  129.         camera->setPostDrawCallback(new MyCameraPostDrawCallback(image));
  130.         camera->addChild(root.get());
  131.         group->addChild(camera);
  132.     return group;
  133. }
  134. int main()
  135. {
  136.         osg::ref_ptr<osg::Group> root=create();
  137.         osgViewer::Viewer viewer;
  138.         osg::Group* ans=new osg::Group();
  139.         ans->addChild(createHUD(root));
  140.         viewer.setSceneData(ans);
  141.         viewer.run();
  142. }
复制代码

[ 本帖最后由 acmiyou 于 2009-6-6 01:58 编辑 ]

osg_exe.rar

1008.67 KB, 下载次数: 730, 下载积分: 威望 1

该用户从未签到

发表于 2009-6-6 08:27:38 | 显示全部楼层
支持一下~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表