绘制四边形
模式
- 点显示 osg::PolygonMode::POINT
- 线显示 osg::PolygonMode::LINE
- 面显示 osg::PolygonMode::FILL
关于 setRenderBinDetails 与setRenderingHint 的关系
为了简化操作,用户程序还可以使用StateSet::setRenderingHint 来设置渲染的顺序,这个函数的传入参数可以为枚举量OPAQUE_BIN 或TRANSPARENT_BIN。前者可以指定该渲染状态用于不透明物体的渲染,后者则指定该渲染状态用于透明物体的渲染,此时OSG自动将其渲染顺序置后,并设置它所管理的“状态节点”和“渲染叶”数据按照深度值降序进行排序。
关于 setRenderBinDetails 与setRenderingHint 的关系,也可以这样解释:
stateSet->setRenderingHint ( OPAQUE_BIN );
stateSet->setRenderingHint ( TRANSPARENT_BIN );
分别等价于:
stateSet->setRenderBinDetails( 0, “RenderBin” );
stateSet->setRenderBinDetails( 10, “DepthSortedBin” );
图元装配方式
#include <windows.h>
#include <osg\node>
#include <osg\group>
#include <osg\geometry>
#include <osg\matrixtransform>
#include <osg\Point>
#include <osg\LineWidth>
#include <osg\LineStipple>
#include <osgViewer\Viewer>
int main()
{
osgViewer::Viewer viewer;
osg::ref_ptr<osg::group> group = new osg::Group();
viewer.setSceneData(group);
osg::ref_ptr<osg::geode> geode = new osg::Geode();
geode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE);
geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
geode->getOrCreateStateSet()->setRenderingHint(TRANSPARENT);
geode->getOrCreateStateSet()->setAttributeAndModes(new osg::Point(5.0f));
geode->getOrCreateStateSet()->setAttributeAndModes(new osg::LineWidth(2.0f));
geode->getOrCreateStateSet()->setAttributeAndModes(new osg::LineStipple(1, 0x0F0F));
geode->getOrCreateStateSet()->setAttributeAndModes(new osg::PolygonMode(osg::PolygonMode::FRONT, osg::PolygonMode::FILL));
osg::ref_ptr<osg::geometry> geometry = new osg::Geometry();
//顶点
osg::ref_ptr<osg::vec3array> vertex = new osg::Vec3Array();
vertex->push_back(osg::Vec3(-1.0, -1.0, 0.5));
vertex->push_back(osg::Vec3(1.0, -1.0, 0.5));
vertex->push_back(osg::Vec3(1.0, 1.0, 0.5));
vertex->push_back(osg::Vec3(-1.0, 1.0, 0.5));
geometry->setVertexArray(vertex);
//颜色
osg::ref_ptr<osg::vec4array> colors = new osg::Vec4Array();
colors->push_back(osg::Vec4(1.0, 1.0, 1.0, 0.5));
geometry->setColorArray(colors, osg::Array::BIND_OVERALL);
//图元装配
geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::Mode::QUADS, 0, vertex->size()));
geode->addDrawable(geometry);
group->addChild(geode);
viewer.setUpViewInWindow(100, 100, 500, 400);
return viewer.run();
}