osg解析系列-[osg::StateSet] 常用设置,可存成代码段 直接调用

	// osg::StateSet 常用设置,可存成代码段 直接调用
	// set root=内容节点,camera=相机节点
	osg::Group* root;
	osg::Group* camera;

	// 光栅化方式(FILL/LINE/POINT)
	osg::ref_ptr<osg::PolygonMode> polygon = new osg::PolygonMode;
	polygon->setMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::LINE);
	root->getOrCreateStateSet()->setAttributeAndModes(polygon.get(),osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
	// 材质(Shininess=镜面指数(1~100))
	osg::ref_ptr<osg::Material> material = new osg::Material;
	material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
	material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0.2,0.2,0.2,1));
	material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0, 1.0, 1.0, 1));
	material->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(1,1,1,1));
	material->setShininess(osg::Material::FRONT, 65.0f);
	root->getOrCreateStateSet()->setAttributeAndModes(material.get(),osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
	// 深度偏移(为漂亮地渲染边缘着重显示的实心物体;拉近:factor=-1 unit=-1;拉远:factor=1 unit=1;)
	osg::ref_ptr<osg::PolygonOffset> polyoffset = new osg::PolygonOffset;
	polyoffset->setUnits(-1);
	polyoffset->setFactor(-1);
	root->getOrCreateStateSet()->setAttributeAndModes(polyoffset.get(),osg::StateAttribute::ON);
	// shader(着色器)
	static const char* computeSrc = {
		"uniform float osg_FrameTime;\n"
		"void main() {\n"
		"..."
		"}\n"
	};
	osg::ref_ptr<osg::Program> computeProg = new osg::Program;
	computeProg->addShader(new osg::Shader(osg::Shader::COMPUTE, computeSrc));
	root->getOrCreateStateSet()->setAttributeAndModes(computeProg.get());
	// 裁剪
	osg::ref_ptr<osg::CullFace> cullface = new osg::CullFace(osg::CullFace::BACK);
	// 线宽
	osg::LineWidth* linewidth = new osg::LineWidth();
	linewidth->setWidth(2.0f);
	root->getOrCreateStateSet()->setAttributeAndModes(linewidth, osg::StateAttribute::ON);
	// 片段测试中的模板测试(仅用于相机节点)
	osg::ref_ptr<osg::Stencil> stencil = new osg::Stencil;
	stencil->setFunction(osg::Stencil::EQUAL, 0, ~0u);
	stencil->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::KEEP);
	camera->getOrCreateStateSet()->setAttributeAndModes(stencil.get(), osg::StateAttribute::ON);
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值