Learning OSG programing---osgScribe

Learning OSG programing---osgScribe

  Scribe可以翻译为素描,抄写等。本例通过在模型表面添加一层素描,来显示模型的骨架。

  关键代码:

 1     osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet;
 2     osg::ref_ptr<osg::PolygonOffset> polyoffset = new osg::PolygonOffset;
 3     polyoffset->setFactor(-1.0f);
 4     polyoffset->setUnits(5.0f);
 5     osg::ref_ptr<osg::PolygonMode> polymode = new osg::PolygonMode;
 6     polymode->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE);
 7     stateset->setAttributeAndModes(polyoffset,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
 8     stateset->setAttributeAndModes(polymode,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
 9 
10     osg::ref_ptr<osg::Material> material = new osg::Material;
11     material->setAmbient(osg::Material::FRONT_AND_BACK,osg::Vec4f(1.0,0.0,1.0,1.0));
12     stateset->setAttributeAndModes(material,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
13   stateset->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF);
14   decorator->setStateSet(stateset);

  这段代码主要设置用于绘制素描效果的属性集stateset。属性集的设置主要是三个方面:

  PolygonOffest polyoffset用于设置素描模型和加载模型之间的偏移,避免深度检测过小,出现的闪烁现象。这个类主要是封装了OpenGL中的glPolygonOffset函数,其成员函数和说明可见于这里。关于OpenGL中的glPolygonOffset函数的使用说明可见这里。分析这个例程的对象关系图,如下所示:

  这个例程的对象关系还是很清晰的,通过对Scribe装饰对象decorator设置其状态集,设置PolygonOffset避免闪烁,设置PolygonMode将装饰器设置为线模型,通过设置materal可更改线框的颜色,材质,纹理,各种光照反射效果等。最后将装饰器节点和加载模型共同添加到根节点,显示线框效果:

原文地址:https://www.cnblogs.com/SupremeGIS-Developer/p/10617284.html