文章

2.osg之为球增加颜色

1.源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <osgViewer/Viewer>
#include <osg/Geode>
#include <osg/ShapeDrawable>

int main()
{
	// 创建一个Viewer对象
	osgViewer::Viewer viewer;

	// 创建一个用于绘制形状的Geode节点
	osg::ref_ptr<osg::Geode> geode = new osg::Geode();

	// 创建一个球体,设置其半径为1.0
	osg::ref_ptr<osg::Sphere> sphere = new osg::Sphere(osg::Vec3(), 1.0);

	// 创建一个ShapeDrawable对象,将球体添加到其中
	osg::ref_ptr<osg::ShapeDrawable> shapeDrawable = new osg::ShapeDrawable(sphere.get());

	// 设置ShapeDrawable的颜色
	shapeDrawable->setColor(osg::Vec4(1.0, 0.0, 0.0, 1.0)); // 红色

	// 将ShapeDrawable添加到Geode节点
	geode->addDrawable(shapeDrawable.get());

	// 将Geode节点添加到场景图中
	viewer.setSceneData(geode.get());

	// 开始渲染循环
	return viewer.run();
}

2.说明

在这个示例中,我们添加了一行新的代码:shapeDrawable->setColor(osg::Vec4(1.0, 0.0, 0.0, 1.0));。这行代码设置了ShapeDrawable的颜色。osg::Vec4是一个四维向量,它的四个分量分别代表红色、绿色、蓝色和透明度(alpha)。在这里,我们设置了红色分量为1.0,绿色和蓝色分量为0.0,透明度为1.0,所以最终的颜色是红色。

本文由作者按照 CC BY 4.0 进行授权