OSG 绘制一个圆

时间:2024-03-20 16:26:07

OSG绘制一个圆,采用的是多个线段收尾连接在一起的办法。
OSG 绘制一个圆
查看源码:

void createClock() {
	//设置线宽
	osg::ref_ptr<osg::LineWidth> lineSize = new osg::LineWidth;
	lineSize->setWidth(4.0);

	//获取根节点
	osg::ref_ptr<osg::Group> root = new osg::Group;
	osg::ref_ptr<osg::StateSet> stateSet = root->getOrCreateStateSet();
	//打开线宽属性
	stateSet->setAttributeAndModes(lineSize, osg::StateAttribute::ON);

	//表盘的几何节点
	osg::ref_ptr<osg::Geode> clockGeode = new osg::Geode;
	//表盘圈
	osg::ref_ptr<osg::Geometry> clockGeometry = new osg::Geometry;
	//标牌数字
	osg::ref_ptr<osg::Geometry> numPoint = new osg::Geometry;

	clockGeode->addChild(clockGeometry);
	//clockGeode->addChild(numPoint);
	root->addChild(clockGeode);

	//数字节点顶点
	osg::ref_ptr<osg::Vec3Array> num = new osg::Vec3Array;
	//存放所有圆盘上的点,把这些点连接成直线画成圆盘
	osg::ref_ptr<osg::Vec3Array> allPoints = new osg::Vec3Array;
	//表盘颜色
	osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
	//表针颜色
	osg::ref_ptr<osg::Vec4Array> colors2 = new osg::Vec4Array;
	//加入颜色得到十二个数字的顶点位置
	for (double i = 0; i < 6.28; i += 0.52) {
		num->push_back(osg::Vec3(45 * sin(i), -0.0, 45 * cos(i)));
	}

	//得到半径为50的钟表的314个点,这些点形成线坐标表盘
	for (double i = 0.0; i < 6.28;i += 0.02) {
		colors->push_back(osg::Vec4f(sin(i), cos(i), 0.5, 1.0));
		allPoints->push_back(osg::Vec3(50 * sin(i), -0.0, 50 * cos(i)));
	}

	//设置顶点
	clockGeometry->setVertexArray(allPoints);

	//画线
	clockGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, 0, allPoints->size()));


	//clockGeometry->addPrimitiveSet(new osg::DrawElementsUInt(osg::PrimitiveSet::LINE_LOOP, 0));
	clockGeometry->setColorArray(colors);
	clockGeometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);

	osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
	viewer->addEventHandler(new osgViewer::WindowSizeHandler());
	root->addChild(osgDB::readNodeFile("cow.osg"));
	viewer->setSceneData(root.get());

	viewer->realize();
	viewer->run();
}

aaa