一、osg::Geode类
类说明:Geode结点,是个几何结点,可以说是一个几何Group结点,一般的可绘制几何体都是通过它来传向root进行渲染。是OSG几何绘制的最高管理结点。
1、Geode ()
Geode (const Geode &, const CopyOp ©op=CopyOp::SHALLOW_COPY)
说明:构造函数,第二个构造函数意思为从一个已知的Geode结点构造一个新的Geode结点。第二个参数copyop代表拷贝类型,默认是浅拷贝,也可以是深拷贝,深拷贝:DEEP_COPY_OBJECTS
二、osg::Drawable类
类说明:该类也是最重要的类之一,其下派生类无数,其中包含重要的粒子系统,文字显示等等。这些都可以归功于osg::Drawable。它还是OSG与OpenGL的接口,有谁知道呢,哪里还有渲染不是可绘制[Drawable]的呢。事实上也如上面描述的,它的子类比它更出名。
1、Drawable ()
Drawable (const Drawable &drawable, const CopyOp ©op=CopyOp::SHALLOW_COPY)
说明:构造函数。第二个构造函数为从一个已经存在的Drawable构造一个新的Drawable。参数二为拷贝类型,默认为浅拷贝,也可以是深拷贝:DEEP_COPY_OBJECTS
2、virtual bool isSameKindAs (const Object *obj) const
说明:判断一类与Drawable是否为同一类型。如果为同一类型返回true,返则返回false.
3、virtual const char * libraryName () const
说明:得到库文件名。返回库文件名的常量字符串指针。
4、virtual const char * className () const
说明:得到类类型名。返回类类型名的常量字符串指针。
5、virtual Geometry * asGeometry ()
virtual const Geometry * asGeometry () const
说明:返回一个指向Geometry的指针。被当作Geometry来使用。
6、virtual void computeDataVariance ()
说明:基于Callback等计算数据差异。
7、const ParentList & getParents () const
ParentList getParents ()
说明:得到其父结点列表,其实是一个std::vector<Node*>。返回值为父结点列表。
8、Node * getParent (unsigned int i)
const Node * getParent (unsigned int i) const
说明:得到第i个父结点。如果不存在该父结点,则返回空。
9、unsigned int getNumParents () const
说明:得到父结点的个数。.
10、void setStateSet (StateSet *stateset)
说明:设置状态设置。参数为状态设置指针,改变结点的状态设置。
11、StateSet * getStateSet ()
const StateSet * getStateSet () const
说明:得到结点的状态设置指针,如果没有设置过,则返回false.
三、osg::Shape类
类说明:Shape中存放了一些图形,它的子类包含了内置的比如盒子、球这类原始类型。从它的类名也可以看出来它主要的功能:绘制形状。
1、Shape ()
Shape (const Shape &sa, const CopyOp ©op=CopyOp::SHALLOW_COPY)
说明:构造函数,第二个构造函数用来从一个已知的Shape构建新Shape。拷贝类型默认为浅拷贝。可以为深拷贝:DEEP_COPY_OBJECTS.
2、virtual Object * cloneType () const =0
virtual Object * clone (const CopyOp &) const =0
说明:纯虚函数。第一个为返回克隆类型。第二个为克隆。
3、virtual bool isSameKindAs (const Object *obj) const
说明:判断与类obj是否为同一类型,判断obj是否为Shape类型。
4、virtual const char * libraryName () const
说明:得到库名。返回存有库名的常量指针。
5、virtual const char * className () const
说明:返回类类型名指针。
6、virtual void accept (ShapeVisitor &)=0
virtual void accept (ConstShapeVisitor &) const =0
说明:接受Shape遍历器。
四、osg::Geometry类
类说明:基本绘制几何体类,用于绘制基本的几何体。
1、Geometry ()
Geometry (const Geometry &geometry, const CopyOp ©op=CopyOp::SHALLOW_COPY)
说明:构造函数,第二个构造函数为从已知的Geometry来构造新的geometry。参数二为拷贝类型,默认为是浅拷贝,也可以设置为深拷贝DEEP_COPY_OBJECTS。
2、virtual Object * cloneType () const
说明:从一个已有geometry来克隆一个新的object,并返回该object指针。
3、virtual Object * clone (const CopyOp ©op) const
说明:从一个已有的geometry来克隆一个新的object,并返回该object指针,参数为克隆类型,可以是SHALLOW_COPY[浅拷贝],也可以是DEEP_COPY_OBJECTS[深拷贝]。
4、virtual bool isSameKindAs (const Object *obj) const
说明:与参数中的类型是否为同一类型,若为同一类型则返回真,否则返回假,判断此obj类型是否与调用函数的this类为同一类型。
5、virtual const char * libraryName () const
说明:得到类的库名。返回库名的指针。
6、virtual const char * className () const
说明:返回类名称,得到类名称指针。
7、virtual Geometry * asGeometry ()
virtual const Geometry * asGeometry () const
说明:返回一个指向geometry的指针,一般是对象是可以使用这个。第二个函数为常量版本。
8、bool empty () const
说明:判断此geometry是否有可绘制的元素,若空空如也则返回真,否则返回假。
9、void setVertexArray (Array *array)
说明:设置顶点列表,也就是在绘制过程中要使用到的顶点, 在一个Array当中,装有顶点的数组。
10、Array * getVertexArray ()
const Array * getVertexArray () const
说明:返回geometry中的顶点数组。如果没有顶点则返回空。
11、void setVertexIndices (IndexArray *array)
说明:设置索引顶点数组,要设置的顶点索引数组指针。
12、IndexArray * getVertexIndices ()
const IndexArray * getVertexIndices () const
说明:得到索引顶点数组,如果没有设置,则得到NULL。
13、void setVertexData (const ArrayData &arrayData)
说明:设置数组数具。这三种都是向geometry传递参数的方法,向geometry传递的数组数据。
14、ArrayData & getVertexData ()
const ArrayData & getVertexData () const
说明:得到数组数据,第二个为常量版本。
15、void setNormalBinding (AttributeBinding ab)
说明:设置法线绑定方式。一般有全不绑定,逐顶点,逐片元等等,设置法线绑定方式,可以为以下值:BIND_OFF,BIND_OVERALL,BIND_PER_PRIMITIVE_SET,BIND_PER_PRIMITIVE,BIND_PER_VERTEX
BIND_OFF 表示不绑定;
BIND_OVERALL 表示用一条法向绑定所有的顶点(顶点数组中);
BIND_PER_PRIMITIVE_SET表示 为每个图元集绑定一条法线;(法线的个数要与图元集的个数一致)
BIND_PER_PRIMITIVE 表示为每个图元绑定一条法线;(法线的个数要与图元的个数一致)
BIND_PER_VERTEX表示 为每个顶点绑定一条法线。 (此时法线的个数要与顶点的个数一致)
16、AttributeBinding getNormalBinding () const
说明:得到顶点绑定方式。返回顶点绑定方式属性。
17、void setNormalArray (Array *array)
说明:设置法线数组,法线数组,用来设置法线,其中关乎绑定方式。
18、Array * getNormalArray ()
const Array * getNormalArray () const
说明:得到法线数组,第二个为常量版本。
19、void setNormalIndices (IndexArray *array)
说明:使用索引数组设置法线,索引数组,用来设置法线。
20、IndexArray * getNormalIndices ()
const IndexArray * getNormalIndices () const
说明:得到法线的索引数组。
21、void setNormalData (const ArrayData &arrayData)
说明:设置法线数据数组,数据数组,为设置法线传入法线数据数组。
22、ArrayData & getNormalData ()
const ArrayData & getNormalData () const
说明:得到已经设置的法线数据数组,如果没有设置则返回空。
23、void setColorBinding (AttributeBinding ab)
说明:设置颜色绑定方式,有片元、顶点等等方式,设置颜色绑定方式,可以为以下值:BIND_OFF,BIND_OVERALL,BIND_PER_PRIMITIVE_SET,BIND_PER_PRIMITIVE,BIND_PER_VERTEX。
BIND_OFF 表示不绑定;
BIND_OVERALL 表示用一条法向绑定所有的顶点(顶点数组中);
BIND_PER_PRIMITIVE_SET表示 为每个图元集绑定一条法线;(法线的个数要与图元集的个数一致)
BIND_PER_PRIMITIVE 表示为每个图元绑定一条法线;(法线的个数要与图元的个数一致)
BIND_PER_VERTEX表示 为每个顶点绑定一条法线。 (此时法线的个数要与顶点的个数一致)
24、AttributeBinding getColorBinding () const
说明:得到颜色绑定方式。
25、void setColorArray (Array *array)
说明:设置颜色数组。四元数分别为RGBA。
26、Array * getColorArray ()
const Array * getColorArray () const
说明:得到颜色数组。第二个为常量版本。
27、void setColorIndices (IndexArray *array)
说明:以索引数组的方式来设置颜色数组,颜色索引数组,用来设置颜色,其中关乎绑定方式。
28、IndexArray * getColorIndices ()
const IndexArray * getColorIndices () const
说明:得到颜色索引数组。返回颜色数组指针,如果没有设置则返回空。
29、void setColorData (const ArrayData &arrayData)
说明:以数据数组的方式来设置颜色数组。
30、ArrayData & getColorData ()
const ArrayData & getColorData () const
说明:得到数据颜色数组。第二个为常量版本。
31、void setSecondaryColorBinding (AttributeBinding ab)
说明:设置次级颜色绑定方式,可以为以下值:BIND_OFF,BIND_OVERALL,BIND_PER_PRIMITIVE_SET,BIND_PER_PRIMITIVE,BIND_PER_VERTEX
BIND_OFF 表示不绑定;
BIND_OVERALL 表示用一条法向绑定所有的顶点(顶点数组中);
BIND_PER_PRIMITIVE_SET表示 为每个图元集绑定一条法线;(法线的个数要与图元集的个数一致)
BIND_PER_PRIMITIVE 表示为每个图元绑定一条法线;(法线的个数要与图元的个数一致)
BIND_PER_VERTEX表示 为每个顶点绑定一条法线。 (此时法线的个数要与顶点的个数一致)
32、AttributeBinding getSecondaryColorBinding () const
说明:返回次级颜色绑定方式。
33、void setSecondaryColorArray (Array *array)
说明:设置次级颜色数组。参数以数组方式传入。
34、Array * getSecondaryColorArray ()
const Array * getSecondaryColorArray () const
说明:得到次级颜色数组。
35、void setSecondaryColorIndices (IndexArray *array)
说明:以索引数组方式设置次级颜色数组。
36、IndexArray * getSecondaryColorIndices ()
const IndexArray * getSecondaryColorIndices () const
说明:得到次级颜色数组,如果次级颜色数组并未设置,则返回NULL。
37、void setSecondaryColorData (const ArrayData &arrayData)
说明:以数据数组的方式来设置颜色数组。
38、ArrayData & getSecondaryColorData ()
const ArrayData & getSecondaryColorData () const
说明:获取次颜色数组的数据数组形式。如果未设置则返回NULL。
五、实例运用
#include<Windows.h>
#include<iostream>
#include <osgViewer/Viewer>
#include <osg/Node>
#include <osg/Geode>
#include <osg/Group>
#include <osg/ShapeDrawable>
#include <osg/Material>
#include <osg/Image>
#include <osg/Texture2D>
#include <osg/LineWidth>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgUtil/Optimizer>
#include <osgViewer/ViewerEventHandlers>
// 创建纹理渲染的正方体
osg::ref_ptr<osg::Geode> CreateBox()
{
// 创建精细度
osg::ref_ptr<osg::TessellationHints> hints = new osg::TessellationHints;
hints->setDetailRatio(0.5);
// 创建形状绘制
osg::ref_ptr<osg::ShapeDrawable> shape = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0, 0.0, 0.0), 4.0, 1.0, 4.0), hints.get());
shape->setColor(osg::Vec4(1, 1, 1, 1)); // 设置颜色
// 创建材料属性
osg::ref_ptr<osg::Material> material = new osg::Material;
material->setAmbient(osg::Material::Face::FRONT_AND_BACK, osg::Vec4f(0.5, 1.0, 1.0, 1)); // 设置全景光
material->setDiffuse(osg::Material::Face::FRONT_AND_BACK, osg::Vec4f(1.0, 1.0, 1.0, 1)); // 设置混合光
material->setSpecular(osg::Material::Face::FRONT_AND_BACK, osg::Vec4f(1.0, 1.0, 1.0, 1)); // 设置反射
material->setShininess(osg::Material::Face::FRONT_AND_BACK, 60); // 设置反射类型
// 创建设置纹理
osg::ref_ptr<osg::Texture2D> texture2D = new osg::Texture2D;
osg::ref_ptr<osg::Image> image = osgDB::readImageFile("ExampleThumbnails/IMG_20190915_180256.jpg");
if (image && image->valid())
{
texture2D->setImage(image);
}
// 创建叶子结点并指定属性
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
geode->getOrCreateStateSet()->setAttributeAndModes(material.get(), osg::StateAttribute::ON);
geode->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture2D.get(), osg::StateAttribute::ON); // 开启设置图片纹理
geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON); // 开启透明度
geode->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON); // 开启深度测试(顺序渲染层级)
geode->addDrawable(shape.get());
return geode;
}
// 创建透明平面
osg::ref_ptr<osg::Node> ReadOsgModel()
{
/*******************************************************************/ // 顶点
// 创建矩形顶点数组,设置顶点(逆时针设置点位)
osg::ref_ptr<osg::Vec3Array> coords = new osg::Vec3Array;
coords->push_back(osg::Vec3(-3, 0, -3));
coords->push_back(osg::Vec3(3, 0, -3));
coords->push_back(osg::Vec3(3, 0, 3));
coords->push_back(osg::Vec3(-3, 0, 3));
// 创建顶点颜色数组,设置颜色
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
colors->push_back(osg::Vec4(1.0, 0.0, 0.0, 0.5));
colors->push_back(osg::Vec4(0.0, 1.0, 0.0, 0.5));
colors->push_back(osg::Vec4(0.0, 0.0, 1.0, 0.5));
colors->push_back(osg::Vec4(1.0, 1.0, 0.0, 0.5));
/*******************************************************************/
// 创建线宽
osg::ref_ptr<osg::LineWidth> lineWidth = new osg::LineWidth;
lineWidth->setWidth(6.0);
// 创建设置法向量(避免正面看是黑色)
osg::ref_ptr<osg::Vec3Array> norms = new osg::Vec3Array;
norms->push_back(osg::Vec3(0.0, -1.0, 0.0));
// 创建几何
osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry;
geometry->setVertexArray(coords); // 加入顶点
geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::Mode::LINE_LOOP, 0, 4)); // 设置顶点连接方式(LINE_LOOP循环连接、QUADS平面)
geometry->setColorArray(colors); // 设置顶点颜色
geometry->setColorBinding(osg::Geometry::AttributeBinding::BIND_PER_VERTEX); // 绑定顶点颜色
geometry->setNormalArray(norms); // 设置法向量
geometry->setNormalBinding(osg::Geometry::AttributeBinding::BIND_OVERALL); // 设置绑定方式
// 创建叶子结点
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
geode->addDrawable(geometry); // 将几何加入节点
geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON); // 开启透明度
geode->getOrCreateStateSet()->setAttributeAndModes(lineWidth, osg::StateAttribute::ON); // 开启线宽
return geode;
}
int main()
{
// 创建场景
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
// 将盒子与透明平面放入同组(注意顺序)
osg::ref_ptr<osg::Group> group = new osg::Group;
group->addChild(CreateBox());
group->addChild(ReadOsgModel());
viewer->setSceneData(group);
// 设置窗口大小
viewer->setUpViewInWindow(200, 200, 800, 800);
// 循环
return viewer->run();
}
可以敲一下代码,运行结果,函数用法都有说明,也可以自己尝试着写一个