diff --git a/src/osgPlugins/obj/OBJWriterNodeVisitor.cpp b/src/osgPlugins/obj/OBJWriterNodeVisitor.cpp index eaaec1187..81ce5fee6 100644 --- a/src/osgPlugins/obj/OBJWriterNodeVisitor.cpp +++ b/src/osgPlugins/obj/OBJWriterNodeVisitor.cpp @@ -384,7 +384,7 @@ void ObjPrimitiveIndexWriter::drawArrays(GLenum mode,GLint first,GLsizei count) for(GLsizei i=0;igetDiffuse(osg::Material::FRONT); ambient = mat->getAmbient(osg::Material::FRONT); specular = mat->getSpecular(osg::Material::FRONT); + shininess = mat->getShininess(osg::Material::FRONT)*1000.0f/128.0f; } if (tex) { @@ -454,6 +456,8 @@ std::ostream& operator<<(std::ostream& fout, const OBJWriterNodeVisitor::OBJMate fout << " " << "Ka " << mat.ambient << std::endl; fout << " " << "Kd " << mat.diffuse << std::endl; fout << " " << "Ks " << mat.specular << std::endl; + if (mat.shininess != -1) + fout << " " << "Ns " << mat.shininess<< std::endl; if(!mat.image.empty()) fout << " " << "map_Kd " << mat.image << std::endl; diff --git a/src/osgPlugins/obj/OBJWriterNodeVisitor.h b/src/osgPlugins/obj/OBJWriterNodeVisitor.h index 8dc16a0d5..8a9cf8f6f 100644 --- a/src/osgPlugins/obj/OBJWriterNodeVisitor.h +++ b/src/osgPlugins/obj/OBJWriterNodeVisitor.h @@ -116,6 +116,7 @@ class OBJWriterNodeVisitor: public osg::NodeVisitor { OBJMaterial(osg::Material* mat, osg::Texture* tex); osg::Vec4 diffuse, ambient, specular; + float shininess; std::string image; std::string name; }; diff --git a/src/osgPlugins/obj/ReaderWriterOBJ.cpp b/src/osgPlugins/obj/ReaderWriterOBJ.cpp index 00cc880dc..fc4cb993c 100644 --- a/src/osgPlugins/obj/ReaderWriterOBJ.cpp +++ b/src/osgPlugins/obj/ReaderWriterOBJ.cpp @@ -72,6 +72,7 @@ public: supportsOption("BUMP=", "Set texture unit for bumpmap texture"); supportsOption("DISPLACEMENT=", "Set texture unit for displacement texture"); supportsOption("REFLECTION=", "Set texture unit for reflection texture"); + supportsOption("NsIfNotPresent=", "set specular exponent if not present"); supportsOption("precision=","Set the floating point precision when writing out files"); } @@ -156,6 +157,7 @@ protected: TextureAllocationMap textureUnitAllocation; /// Coordinates precision. int precision; + int specularExponent; ObjOptionsStruct() { @@ -166,6 +168,7 @@ protected: fixBlackMaterials = true; noReverseFaces = false; precision = std::numeric_limits::digits10 + 2; + specularExponent = -1; } }; @@ -356,12 +359,13 @@ void ReaderWriterOBJ::buildMaterialToStateSetMap(obj::Model& model, MaterialToSt osg_material->setDiffuse(osg::Material::FRONT_AND_BACK,material.diffuse); osg_material->setEmission(osg::Material::FRONT_AND_BACK,material.emissive); - if (material.illum == 2) { + if (material.illum >= 2) { osg_material->setSpecular(osg::Material::FRONT_AND_BACK,material.specular); } else { osg_material->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0,0,0,1)); } - osg_material->setShininess(osg::Material::FRONT_AND_BACK,(material.Ns/1000.0f)*128.0f ); // note OBJ shiniess is 0..1000. + int ns = material.Ns != -1 ? material.Ns : localOptions.specularExponent != -1 ? localOptions.specularExponent : 0; + osg_material->setShininess(osg::Material::FRONT_AND_BACK,(ns/1000.0f)*128.0f ); // note OBJ shiniess is 0..1000. if (material.ambient[3]!=1.0 || material.diffuse[3]!=1.0 || @@ -887,6 +891,11 @@ ReaderWriterOBJ::ObjOptionsStruct ReaderWriterOBJ::parseOptions(const osgDB::Rea localOptions.precision = val; } } + else if (pre_equals == "NsIfNotPresent") + { + int value = atoi(post_equals.c_str()); + localOptions.specularExponent = value ; + } else if (post_equals.length()>0) { obj::Material::Map::TextureMapType type = obj::Material::Map::UNKNOWN; diff --git a/src/osgPlugins/obj/obj.h b/src/osgPlugins/obj/obj.h index b100ac88d..9d79c533a 100644 --- a/src/osgPlugins/obj/obj.h +++ b/src/osgPlugins/obj/obj.h @@ -43,7 +43,7 @@ public: illum(2), Tf(0.0f,0.0f,0.0f,1.0f), Ni(0), - Ns(0), + Ns(-1), // textureReflection(false), alpha(1.0f) {}