Add OutputTextureFiles Option

Add OutputTextureFiles Option support Write out the texture images to file
This commit is contained in:
Long Huan 2019-07-18 09:13:25 +08:00 committed by Robert Osfield
parent c6c49c3745
commit 7ec8b65ec4
3 changed files with 17 additions and 7 deletions

View File

@ -421,7 +421,7 @@ void ObjPrimitiveIndexWriter::drawArrays(GLenum mode,GLint first,GLsizei count)
} }
OBJWriterNodeVisitor::OBJMaterial::OBJMaterial(osg::Material* mat, osg::Texture* tex, const osgDB::Options* options) : OBJWriterNodeVisitor::OBJMaterial::OBJMaterial(osg::Material* mat, osg::Texture* tex, bool outputTextureFiles, const osgDB::Options* options) :
diffuse(1,1,1,1), diffuse(1,1,1,1),
ambient(0.2,0.2,0.2,1), ambient(0.2,0.2,0.2,1),
specular(0,0,0,1), specular(0,0,0,1),
@ -445,6 +445,7 @@ OBJWriterNodeVisitor::OBJMaterial::OBJMaterial(osg::Material* mat, osg::Texture*
osg::Image* img = tex->getImage(0); osg::Image* img = tex->getImage(0);
if ((img) && (!img->getFileName().empty())) { if ((img) && (!img->getFileName().empty())) {
image = img->getFileName(); image = img->getFileName();
if(outputTextureFiles)
osgDB::writeImageFile(*img, image, options); osgDB::writeImageFile(*img, image, options);
} }
} }
@ -526,7 +527,7 @@ void OBJWriterNodeVisitor::processStateSet(osg::StateSet* ss)
if (mat || tex) if (mat || tex)
{ {
_materialMap.insert(std::make_pair(osg::ref_ptr<osg::StateSet>(ss), OBJMaterial(mat, tex, _options))); _materialMap.insert(std::make_pair(osg::ref_ptr<osg::StateSet>(ss), OBJMaterial(mat, tex, _outputTextureFiles, _options)));
_fout << "usemtl " << _materialMap[ss].name << std::endl; _fout << "usemtl " << _materialMap[ss].name << std::endl;
} }

View File

@ -50,13 +50,14 @@
class OBJWriterNodeVisitor: public osg::NodeVisitor { class OBJWriterNodeVisitor: public osg::NodeVisitor {
public: public:
OBJWriterNodeVisitor(std::ostream& fout, const std::string materialFileName = "", const osgDB::Options* options = NULL) : OBJWriterNodeVisitor(std::ostream& fout, const std::string materialFileName = "", bool outputTextureFiles = false, const osgDB::Options* options = NULL) :
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
_fout(fout), _fout(fout),
_currentStateSet(new osg::StateSet()), _currentStateSet(new osg::StateSet()),
_lastVertexIndex(1), _lastVertexIndex(1),
_lastNormalIndex(1), _lastNormalIndex(1),
_lastTexIndex(1), _lastTexIndex(1),
_outputTextureFiles(outputTextureFiles),
_options(options) _options(options)
{ {
_fout << "# file written by OpenSceneGraph" << std::endl << std::endl; _fout << "# file written by OpenSceneGraph" << std::endl << std::endl;
@ -114,7 +115,7 @@ class OBJWriterNodeVisitor: public osg::NodeVisitor {
class OBJMaterial { class OBJMaterial {
public: public:
OBJMaterial() {} OBJMaterial() {}
OBJMaterial(osg::Material* mat, osg::Texture* tex, const osgDB::Options* options); OBJMaterial(osg::Material* mat, osg::Texture* tex, bool outputTextureFiles = false, const osgDB::Options* options = NULL);
osg::Vec4 diffuse, ambient, specular; osg::Vec4 diffuse, ambient, specular;
float shininess; float shininess;
@ -155,6 +156,7 @@ class OBJWriterNodeVisitor: public osg::NodeVisitor {
std::map<std::string, unsigned int> _nameMap; std::map<std::string, unsigned int> _nameMap;
unsigned int _lastVertexIndex, _lastNormalIndex, _lastTexIndex; unsigned int _lastVertexIndex, _lastNormalIndex, _lastTexIndex;
MaterialMap _materialMap; MaterialMap _materialMap;
bool _outputTextureFiles;
osg::ref_ptr<const osgDB::Options> _options; osg::ref_ptr<const osgDB::Options> _options;
}; };

View File

@ -75,6 +75,7 @@ public:
supportsOption("NsIfNotPresent=<value>", "set specular exponent if not present"); supportsOption("NsIfNotPresent=<value>", "set specular exponent if not present");
supportsOption("precision=<digits>","Set the floating point precision when writing out files"); supportsOption("precision=<digits>","Set the floating point precision when writing out files");
supportsOption("OutputTextureFiles", "Write out the texture images to file");
} }
virtual const char* className() const { return "Wavefront OBJ Reader"; } virtual const char* className() const { return "Wavefront OBJ Reader"; }
@ -103,7 +104,7 @@ public:
f.precision(localOptions.precision); f.precision(localOptions.precision);
std::string materialFile = osgDB::getNameLessExtension(fileName) + ".mtl"; std::string materialFile = osgDB::getNameLessExtension(fileName) + ".mtl";
OBJWriterNodeVisitor nv(f, osgDB::getSimpleFileName(materialFile), options); OBJWriterNodeVisitor nv(f, osgDB::getSimpleFileName(materialFile), localOptions.outputTextureFiles, options);
// we must cast away constness // we must cast away constness
(const_cast<osg::Node*>(&node))->accept(nv); (const_cast<osg::Node*>(&node))->accept(nv);
@ -131,7 +132,7 @@ public:
// writing to a stream does not support materials // writing to a stream does not support materials
OBJWriterNodeVisitor nv(fout, "", options); OBJWriterNodeVisitor nv(fout, "", localOptions.outputTextureFiles, options);
// we must cast away constness // we must cast away constness
(const_cast<osg::Node*>(&node))->accept(nv); (const_cast<osg::Node*>(&node))->accept(nv);
@ -157,6 +158,7 @@ protected:
TextureAllocationMap textureUnitAllocation; TextureAllocationMap textureUnitAllocation;
/// Coordinates precision. /// Coordinates precision.
int precision; int precision;
bool outputTextureFiles;
int specularExponent; int specularExponent;
ObjOptionsStruct() ObjOptionsStruct()
@ -168,6 +170,7 @@ protected:
fixBlackMaterials = true; fixBlackMaterials = true;
noReverseFaces = false; noReverseFaces = false;
precision = std::numeric_limits<double>::digits10 + 2; precision = std::numeric_limits<double>::digits10 + 2;
outputTextureFiles = false;
specularExponent = -1; specularExponent = -1;
} }
}; };
@ -881,6 +884,10 @@ ReaderWriterOBJ::ObjOptionsStruct ReaderWriterOBJ::parseOptions(const osgDB::Rea
{ {
localOptions.noReverseFaces = true; localOptions.noReverseFaces = true;
} }
else if (pre_equals == "OutputTextureFiles")
{
localOptions.outputTextureFiles = true;
}
else if (pre_equals == "precision") else if (pre_equals == "precision")
{ {
int val = std::atoi(post_equals.c_str()); int val = std::atoi(post_equals.c_str());