|
我想实现一个自定义背景, 当前面的模型被拖拽变化时背景不会变化。但现在的问题是模型被旋转时背景会遮挡部分模型。
我的做法是先添加了一个Box,然后添加了一个HUD背景图片。(但HUD的camera只能设置为POST_RENDER,然后必须setClearMask()不是默认的才可以显示图片。)最后设置了setRenderBinDetails顺序为先渲染背景后渲染Box。
下面是实现的代码:
-
- osg::Group* groupBox = new osg::Group;
- osg::Group* root = new osg::Group;
- osg::Box* sp0 = new osg::Box(osg::Vec3(0.0001,0,0),2,3,0.2);
- osg::ShapeDrawable* drawable0 = new osg::ShapeDrawable(sp0);
- osg::Geode* geode0 = new osg::Geode();
- geode0->addDrawable(drawable0);
- groupBox->addChild(geode0);
-
- osg::Camera* ca = new osg::Camera;
- ca->setProjectionMatrix(osg::Matrix::ortho2D(0,650,0,700));
- ca->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
- ca->setRenderOrder(osg::Camera::POST_RENDER);
- ca->setClearMask(GL_POLYGON_BIT);
- osg::Geode* geode = new osg::Geode;
- osg::Geometry* geometry = new osg::Geometry;
- geode->addDrawable(geometry);
- ca->addChild(geode);
- osg::Vec3Array* vertices = new osg::Vec3Array;
- vertices->push_back( osg::Vec3( 0, 0, -0) ); // 左下
- vertices->push_back( osg::Vec3(0, 700, -0) ); // 左上
- vertices->push_back( osg::Vec3(650,700,-0) ); // 右上
- vertices->push_back( osg::Vec3( 650,0, -0) ); // 右下
- geometry->setVertexArray(vertices);
- osg::DrawElementsUInt* pyramidfront =
- new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, 0);
- pyramidfront->push_back(3);
- pyramidfront->push_back(2);
- pyramidfront->push_back(1);
- pyramidfront->push_back(0);
- geometry->addPrimitiveSet(pyramidfront);
- osg::Vec4Array* colors = new osg::Vec4Array;
- colors->push_back(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) ); //索引0 白色
- osg::TemplateIndexArray
- <unsigned int, osg::Array::UIntArrayType,4,4> *colorIndexArray;
- colorIndexArray = new osg::TemplateIndexArray<unsigned int, osg::Array::UIntArrayType,4,4>;
- colorIndexArray->push_back(0);
- colorIndexArray->push_back(0);
- colorIndexArray->push_back(0);
- colorIndexArray->push_back(0);
- geometry->setColorArray(colors);
- geometry->setColorIndices(colorIndexArray);
- geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
- osg::Vec2Array* texcoords = new osg::Vec2Array(4);
- (*texcoords)[0].set(0.00f,0.0f);
- (*texcoords)[1].set(0.00f,1.0f);
- (*texcoords)[2].set(1.00f,1.0f);
- (*texcoords)[3].set(1.00f,0.0f);
- geometry->setTexCoordArray(0,texcoords);
- osg::Texture2D* te5 = new osg::Texture2D(osgDB::readImageFile("E:\\data\\Images\\test.png"));
- osg::StateSet* st5 = new osg::StateSet;
- st5->setTextureAttributeAndModes(0,te5);
- st5->setRenderBinDetails(-1,"RenderBin");
- ca->setStateSet(st5);
-
- groupBox->getOrCreateStateSet()->setRenderBinDetails(1,"RenderBin");
- root->addChild(ca);
- root->addChild(groupBox);
- root->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
-
- osgViewer::Viewer viewer;
- viewer.setSceneData(root);
- viewer.run();
复制代码 |
|