// 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);
osg解析系列-[osg::StateSet] 常用设置,可存成代码段 直接调用
于 2023-12-05 11:17:47 首次发布