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),
ambient(0.2,0.2,0.2,1),
specular(0,0,0,1),
@ -445,6 +445,7 @@ OBJWriterNodeVisitor::OBJMaterial::OBJMaterial(osg::Material* mat, osg::Texture*
osg::Image* img = tex->getImage(0);
if ((img) && (!img->getFileName().empty())) {
image = img->getFileName();
if(outputTextureFiles)
osgDB::writeImageFile(*img, image, options);
}
}
@ -526,7 +527,7 @@ void OBJWriterNodeVisitor::processStateSet(osg::StateSet* ss)
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;
}

View File

@ -50,13 +50,14 @@
class OBJWriterNodeVisitor: public osg::NodeVisitor {
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),
_fout(fout),
_currentStateSet(new osg::StateSet()),
_lastVertexIndex(1),
_lastNormalIndex(1),
_lastTexIndex(1),
_outputTextureFiles(outputTextureFiles),
_options(options)
{
_fout << "# file written by OpenSceneGraph" << std::endl << std::endl;
@ -114,7 +115,7 @@ class OBJWriterNodeVisitor: public osg::NodeVisitor {
class OBJMaterial {
public:
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;
float shininess;
@ -155,6 +156,7 @@ class OBJWriterNodeVisitor: public osg::NodeVisitor {
std::map<std::string, unsigned int> _nameMap;
unsigned int _lastVertexIndex, _lastNormalIndex, _lastTexIndex;
MaterialMap _materialMap;
bool _outputTextureFiles;
osg::ref_ptr<const osgDB::Options> _options;
};

View File

@ -75,6 +75,7 @@ public:
supportsOption("NsIfNotPresent=<value>", "set specular exponent if not present");
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"; }
@ -103,7 +104,7 @@ public:
f.precision(localOptions.precision);
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
(const_cast<osg::Node*>(&node))->accept(nv);
@ -131,7 +132,7 @@ public:
// writing to a stream does not support materials
OBJWriterNodeVisitor nv(fout, "", options);
OBJWriterNodeVisitor nv(fout, "", localOptions.outputTextureFiles, options);
// we must cast away constness
(const_cast<osg::Node*>(&node))->accept(nv);
@ -157,6 +158,7 @@ protected:
TextureAllocationMap textureUnitAllocation;
/// Coordinates precision.
int precision;
bool outputTextureFiles;
int specularExponent;
ObjOptionsStruct()
@ -168,6 +170,7 @@ protected:
fixBlackMaterials = true;
noReverseFaces = false;
precision = std::numeric_limits<double>::digits10 + 2;
outputTextureFiles = false;
specularExponent = -1;
}
};
@ -881,6 +884,10 @@ ReaderWriterOBJ::ObjOptionsStruct ReaderWriterOBJ::parseOptions(const osgDB::Rea
{
localOptions.noReverseFaces = true;
}
else if (pre_equals == "OutputTextureFiles")
{
localOptions.outputTextureFiles = true;
}
else if (pre_equals == "precision")
{
int val = std::atoi(post_equals.c_str());