osg--状态机相关

状态机

  • 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");
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值