状态机
- osg使用osg::StateSet实例变量表示状态机
- 每个Node和Drawable有一个智指针指向osg::StateSet实例变量
- 只有所有node和drawable删除后osg::StateSet实例变量才删除
- osg使用osg::StateAttribute表示状态属性
- 常用方法
- 非纹理属性
- setAttribute
- setMode
- setAttributeAndModes
- 纹理属性
- setTextureAttribute
- setTextureMode
- setTextureAttributeAndModes
- 非纹理属性
示例
#include <osg/PolygonMode>
#include <osg/MatrixTransform>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
int main(int argc, char** argv)
{
osg::ref_ptr<osg::Node> model = osgDB::readNodeFile("cessna.osg");
osg::ref_ptr<osg::MatrixTransform> transformation1 = new osg::MatrixTransform;
transformation1->setMatrix(osg::Matrix::translate(-25.0f, 0.0f, 0.0f));
transformation1->addChild(model.get());
osg::ref_ptr<osg::MatrixTransform> transformation2 = new osg::MatrixTransform;
transformation2->setMatrix(osg::Matrix::translate(25.0f, 0.0f, 0.0f));
transformation2->addChild(model.get());
osg::ref_ptr<osg::PolygonMode> pm = new osg::PolygonMode;
pm->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE);
transformation1->getOrCreateStateSet()->setAttribute(pm.get());
osg::ref_ptr<osg::Group> root = new osg::Group;
root->addChild(transformation1.get());
root->addChild(transformation2.get());
osgViewer::Viewer viewer;
viewer.setSceneData(root.get());
return viewer.run();
}
状态继承
- 节点的状态机影响自身和孩子
- 父节点改变孩子节点状态机
- stateset->setAttribute( attr,osg::StateAttribute::OVERRIDE );
- stateset->setAttributeAndModes( attr,osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE );
- 孩子节点状态机可以继承/免疫父节点
- stateset->setAttribute( attr,osg::StateAttribute::PROTECTED );
- stateset->setAttribute( attr,osg::StateAttribute::INHERIT );
示例
#include <osg/PolygonMode>
#include <osg/MatrixTransform>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
int main(int argc, char** argv)
{
osg::ref_ptr<osg::Node> model = osgDB::readNodeFile("glider.osg");
osg::ref_ptr<osg::MatrixTransform> transformation1 = new osg::MatrixTransform;
transformation1->setMatrix(osg::Matrix::translate(-0.5f, 0.0f, 0.0f));
transformation1->addChild(model.get());
osg::ref_ptr<osg::MatrixTransform> transformation2 = new osg::MatrixTransform;
transformation2->setMatrix(osg::Matrix::translate(0.5f, 0.0f, 0.0f));
transformation2->addChild(model.get());
osg::ref_ptr<osg::Group> root = new osg::Group;
root->addChild(transformation1.get());
root->addChild(transformation2.get());
transformation1->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
transformation2->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED);
root->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
osgViewer::Viewer viewer;
viewer.setSceneData(root.get());
return viewer.run();
}
固定功能渲染状态
说明:
-
TYPE ID列为osg状态机中属性名称,如果没有设置返回值为NULL
osg::PolygonMode* pm = dynamic_castosg::PolygonMode*(stateset->getAttribute(osg::StateAttribute::POLYGONMODE) );
-
Associated mode列为osg状态机中模式名称
osg::StateAttribute::GLModeValue value = stateset->getMode( GL_LIGHTING );
-
Related OpenGL functions列为封装的opengl函数
示例
#include <osg/Fog>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
int main(int argc, char** argv)
{
osg::ref_ptr<osg::Fog> fog = new osg::Fog;
fog->setMode(osg::Fog::LINEAR);
fog->setStart(500.0f);
fog->setEnd(2500.0f);
fog->setColor(osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f));
osg::ref_ptr<osg::Node> model = osgDB::readNodeFile("lz.osg");