绘制五角星

#include <Windows.h>
#include <osg\Node>
#include <osg\Group>
#include <osg\Geometry>
#include <osgViewer/Viewer>

osg::Vec3Array* createVertexArray()
{
	osg::ref_ptr<osg::Vec3Array> vertex = new osg::Vec3Array();
	int n = 5;
	double step = 360 / n;
	for (int i = 0; i <= n; ++i)
	{
		double x = sin(osg::DegreesToRadians(i*step));
		double y = cos(osg::DegreesToRadians(i*step));
		double z = 0.0;
		vertex->push_back(osg::Vec3(x, y, z));
	}
	return vertex.release();
}

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()->setAttributeAndModes(new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::LINE));
	osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry();

	//顶点
	osg::ref_ptr<osg::Vec3Array> vertex = createVertexArray();
	geometry->setVertexArray(vertex);

	//颜色
	osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array();
	colors->push_back(osg::Vec4(1.0, 1.0, 1.0, 1.0));
	geometry->setColorArray(colors, osg::Array::BIND_OVERALL);

	//图元装配
	osg::ref_ptr<osg::DrawElementsUInt> draw_elements = new osg::DrawElementsUInt(osg::PrimitiveSet::LINE_LOOP, 0);	
	draw_elements->addElement(0);
	draw_elements->addElement(2);
	draw_elements->addElement(4);
	draw_elements->addElement(1);
	draw_elements->addElement(3);
	geometry->addPrimitiveSet(draw_elements);

	geode->addDrawable(geometry);
	group->addChild(geode);

	viewer.setUpViewInWindow(100, 100, 500, 400);
	return viewer.run();
}

绘制五角星

绘制五角星

上一篇:Envoy基于文件系统的EDS动态配置


下一篇:信息安全-第六章