查看: 1625|回复: 2

求助——自定义背景遮挡前景模型的问题

[复制链接]

该用户从未签到

发表于 2010-7-6 00:25:22 | 显示全部楼层 |阅读模式
我想实现一个自定义背景, 当前面的模型被拖拽变化时背景不会变化。但现在的问题是模型被旋转时背景会遮挡部分模型。
      我的做法是先添加了一个Box,然后添加了一个HUD背景图片。(但HUD的camera只能设置为POST_RENDER,然后必须setClearMask()不是默认的才可以显示图片。)最后设置了setRenderBinDetails顺序为先渲染背景后渲染Box。

下面是实现的代码:

  1. osg::Group* groupBox = new osg::Group;
  2. osg::Group* root = new osg::Group;
  3. osg::Box* sp0 = new osg::Box(osg::Vec3(0.0001,0,0),2,3,0.2);
  4. osg::ShapeDrawable* drawable0 = new osg::ShapeDrawable(sp0);
  5. osg::Geode* geode0 = new osg::Geode();
  6. geode0->addDrawable(drawable0);
  7. groupBox->addChild(geode0);

  8. osg::Camera* ca = new osg::Camera;
  9. ca->setProjectionMatrix(osg::Matrix::ortho2D(0,650,0,700));
  10. ca->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
  11. ca->setRenderOrder(osg::Camera::POST_RENDER);
  12. ca->setClearMask(GL_POLYGON_BIT);

  13. osg::Geode* geode = new osg::Geode;
  14. osg::Geometry* geometry = new osg::Geometry;
  15. geode->addDrawable(geometry);
  16. ca->addChild(geode);
  17. osg::Vec3Array* vertices = new osg::Vec3Array;
  18. vertices->push_back( osg::Vec3( 0, 0, -0) ); // 左下
  19. vertices->push_back( osg::Vec3(0, 700, -0) ); // 左上
  20. vertices->push_back( osg::Vec3(650,700,-0) ); // 右上
  21. vertices->push_back( osg::Vec3( 650,0, -0) ); // 右下
  22. geometry->setVertexArray(vertices);
  23. osg::DrawElementsUInt* pyramidfront =
  24.   new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, 0);
  25. pyramidfront->push_back(3);
  26. pyramidfront->push_back(2);
  27. pyramidfront->push_back(1);
  28. pyramidfront->push_back(0);
  29. geometry->addPrimitiveSet(pyramidfront);
  30. osg::Vec4Array* colors = new osg::Vec4Array;
  31. colors->push_back(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) ); //索引0 白色
  32. osg::TemplateIndexArray
  33.   <unsigned int, osg::Array::UIntArrayType,4,4> *colorIndexArray;
  34. colorIndexArray = new osg::TemplateIndexArray<unsigned int, osg::Array::UIntArrayType,4,4>;
  35. colorIndexArray->push_back(0);
  36. colorIndexArray->push_back(0);
  37. colorIndexArray->push_back(0);
  38. colorIndexArray->push_back(0);
  39. geometry->setColorArray(colors);
  40. geometry->setColorIndices(colorIndexArray);
  41. geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
  42. osg::Vec2Array* texcoords = new osg::Vec2Array(4);
  43. (*texcoords)[0].set(0.00f,0.0f);
  44. (*texcoords)[1].set(0.00f,1.0f);
  45. (*texcoords)[2].set(1.00f,1.0f);
  46. (*texcoords)[3].set(1.00f,0.0f);
  47. geometry->setTexCoordArray(0,texcoords);
  48. osg::Texture2D* te5 = new osg::Texture2D(osgDB::readImageFile("E:\\data\\Images\\test.png"));
  49. osg::StateSet* st5 = new osg::StateSet;
  50. st5->setTextureAttributeAndModes(0,te5);
  51. st5->setRenderBinDetails(-1,"RenderBin");
  52. ca->setStateSet(st5);

  53. groupBox->getOrCreateStateSet()->setRenderBinDetails(1,"RenderBin");
  54. root->addChild(ca);
  55. root->addChild(groupBox);
  56. root->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);

  57. osgViewer::Viewer viewer;
  58. viewer.setSceneData(root);
  59. viewer.run();
复制代码

该用户从未签到

发表于 2010-7-6 22:12:37 | 显示全部楼层
这是一个绘制天空盒的例子,不过我想它同样适用于您的情况,您可以考虑将其改写:
http://lists.openscenegraph.org/ ... January/006201.html

该用户从未签到

 楼主| 发表于 2010-7-9 20:12:10 | 显示全部楼层
2# array  用shader解决了,是深度值错误引起的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

联系我们

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