From 960d4a96c3ad8d00ced4fcb2ec1ad3583ce1a134 Mon Sep 17 00:00:00 2001 From: scrawl Date: Wed, 1 Feb 2017 03:18:17 +0100 Subject: [PATCH] Reduce the number of osg::Material state by setting the default state on the graph root --- apps/opencs/view/render/scenewidget.cpp | 7 +++++++ apps/openmw/mwrender/renderingmanager.cpp | 7 +++++++ components/nifosg/nifloader.cpp | 9 +++++++++ 3 files changed, 23 insertions(+) diff --git a/apps/opencs/view/render/scenewidget.cpp b/apps/opencs/view/render/scenewidget.cpp index e5b61f5d3..a8618923f 100644 --- a/apps/opencs/view/render/scenewidget.cpp +++ b/apps/opencs/view/render/scenewidget.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -74,6 +75,12 @@ RenderWidget::RenderWidget(QWidget *parent, Qt::WindowFlags f) mView->getCamera()->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON); mView->getCamera()->getOrCreateStateSet()->setMode(GL_CULL_FACE, osg::StateAttribute::ON); + osg::ref_ptr defaultMat (new osg::Material); + defaultMat->setColorMode(osg::Material::OFF); + defaultMat->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4f(1,1,1,1)); + defaultMat->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4f(1,1,1,1)); + defaultMat->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4f(0.f, 0.f, 0.f, 0.f)); + mView->getCamera()->getOrCreateStateSet()->getOrCreateStateSet()->setAttribute(defaultMat); mView->setSceneData(mRootNode); diff --git a/apps/openmw/mwrender/renderingmanager.cpp b/apps/openmw/mwrender/renderingmanager.cpp index 2414e49ee..417f33567 100644 --- a/apps/openmw/mwrender/renderingmanager.cpp +++ b/apps/openmw/mwrender/renderingmanager.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -221,6 +222,12 @@ namespace MWRender sceneRoot->getOrCreateStateSet()->setMode(GL_CULL_FACE, osg::StateAttribute::ON); sceneRoot->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::ON); sceneRoot->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON); + osg::ref_ptr defaultMat (new osg::Material); + defaultMat->setColorMode(osg::Material::OFF); + defaultMat->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4f(1,1,1,1)); + defaultMat->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4f(1,1,1,1)); + defaultMat->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4f(0.f, 0.f, 0.f, 0.f)); + sceneRoot->getOrCreateStateSet()->setAttribute(defaultMat); sceneRoot->setNodeMask(Mask_Scene); sceneRoot->setName("Scene Root"); diff --git a/components/nifosg/nifloader.cpp b/components/nifosg/nifloader.cpp index c758e8736..baba022ea 100644 --- a/components/nifosg/nifloader.cpp +++ b/components/nifosg/nifloader.cpp @@ -1758,6 +1758,15 @@ namespace NifOsg mat->setColorMode(osg::Material::AMBIENT); } + if (mat->getColorMode() == osg::Material::OFF + && mat->getDiffuse(osg::Material::FRONT_AND_BACK) == osg::Vec4f(1,1,1,1) + && mat->getAmbient(osg::Material::FRONT_AND_BACK) == osg::Vec4f(1,1,1,1) + && mat->getSpecular(osg::Material::FRONT_AND_BACK) == osg::Vec4f(0.f, 0.f, 0.f, 0.f)) + { + // default state, skip + return; + } + // TODO: this could be replaced by a more generic mechanism of sharing any type of State Attribute // apply only for Materials for now mat = shareMaterial(mat);