From 74dfb23e7bb4063324b7ea643a136b1cf3421dda Mon Sep 17 00:00:00 2001 From: scrawl Date: Mon, 23 Feb 2015 21:16:37 +0100 Subject: [PATCH] Handle material properties in trishapeToGeometry --- components/nifosg/nifloader.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/components/nifosg/nifloader.cpp b/components/nifosg/nifloader.cpp index 2eb57a4f8..b895f8e13 100644 --- a/components/nifosg/nifloader.cpp +++ b/components/nifosg/nifloader.cpp @@ -113,12 +113,11 @@ namespace collectMaterialProperties(nifNode->parent, out); } - void updateMaterialProperties(osg::StateSet* stateset, const std::vector& properties) + void updateMaterialProperties(osg::StateSet* stateset, const std::vector& properties, bool hasVertexColors) { int specFlags = 0; // Specular is disabled by default, even if there's a specular color in the NiMaterialProperty osg::Material* mat = new osg::Material; - // FIXME: color mode should be disabled if the TriShape has no vertex colors - mat->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE); + mat->setColorMode(hasVertexColors ? osg::Material::AMBIENT_AND_DIFFUSE : osg::Material::OFF); for (std::vector::const_reverse_iterator it = properties.rbegin(); it != properties.rend(); ++it) { const Nif::Property* property = *it; @@ -145,6 +144,8 @@ namespace case Nif::RC_NiVertexColorProperty: { const Nif::NiVertexColorProperty* vertprop = static_cast(property); + if (!hasVertexColors) + break; switch (vertprop->flags) { case 0: @@ -504,13 +505,20 @@ namespace NifOsg geometry->setTexCoordArray(textureStage, new osg::Vec2Array(data->uvlist[uvSet].size(), &data->uvlist[uvSet][0]), osg::Array::BIND_PER_VERTEX); } - // FIXME: material ColorMode should be disabled if the TriShape has no vertex colors if (!data->colors.empty()) geometry->setColorArray(new osg::Vec4Array(data->colors.size(), &data->colors[0]), osg::Array::BIND_PER_VERTEX); geometry->addPrimitiveSet(new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLES, data->triangles.size(), (unsigned short*)&data->triangles[0])); + + // osg::Material properties are handled here for two reasons: + // - if there are no vertex colors, we need to disable colorMode. + // - there are 3 "overlapping" nif properties that all affect the osg::Material, handling them + // above the actual renderable would be tedious. + std::vector materialProps; + collectMaterialProperties(triShape, materialProps); + updateMaterialProperties(geometry->getOrCreateStateSet(), materialProps, !data->colors.empty()); } void Loader::handleTriShape(const Nif::NiTriShape* triShape, osg::Group* parentNode, const std::map& boundTextures) @@ -653,9 +661,6 @@ namespace NifOsg case Nif::RC_NiSpecularProperty: { // TODO: handle these in handleTriShape so we know whether vertex colors are available - std::vector materialProps; - collectMaterialProperties(nifNode, materialProps); - updateMaterialProperties(stateset, materialProps); break; } case Nif::RC_NiAlphaProperty: