绘制四边形
- 点显示 osg::PolygonMode::POINT
- 线显示 osg::PolygonMode::LINE
- 面显示 osg::PolygonMode::FILL
#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()->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();
}