fix obj-Plugin export texture file

Fix obj-plugin to support export texture file
This commit is contained in:
Long Huan 2019-07-17 14:52:10 +08:00 committed by Robert Osfield
parent da6b4df00a
commit acac960227
3 changed files with 13 additions and 10 deletions

View File

@ -18,7 +18,7 @@
#include <osg/io_utils> #include <osg/io_utils>
#include "OBJWriterNodeVisitor.h" #include "OBJWriterNodeVisitor.h"
#include <osgDB/WriteFile>
/** writes all values of an array out to a stream, applies a matrix beforehand if necessary */ /** writes all values of an array out to a stream, applies a matrix beforehand if necessary */
@ -421,7 +421,7 @@ void ObjPrimitiveIndexWriter::drawArrays(GLenum mode,GLint first,GLsizei count)
} }
OBJWriterNodeVisitor::OBJMaterial::OBJMaterial(osg::Material* mat, osg::Texture* tex) : OBJWriterNodeVisitor::OBJMaterial::OBJMaterial(osg::Material* mat, osg::Texture* tex, 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),
@ -443,9 +443,10 @@ OBJWriterNodeVisitor::OBJMaterial::OBJMaterial(osg::Material* mat, osg::Texture*
if (tex) { if (tex) {
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();
osgDB::writeImageFile(*img, image, options);
}
} }
} }

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 = "") : OBJWriterNodeVisitor(std::ostream& fout, const std::string materialFileName = "", 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),
_options(options)
{ {
_fout << "# file written by OpenSceneGraph" << std::endl << std::endl; _fout << "# file written by OpenSceneGraph" << std::endl << std::endl;
@ -113,7 +114,7 @@ class OBJWriterNodeVisitor: public osg::NodeVisitor {
class OBJMaterial { class OBJMaterial {
public: public:
OBJMaterial() {} OBJMaterial() {}
OBJMaterial(osg::Material* mat, osg::Texture* tex); OBJMaterial(osg::Material* mat, osg::Texture* tex, const osgDB::Options* options);
osg::Vec4 diffuse, ambient, specular; osg::Vec4 diffuse, ambient, specular;
float shininess; float shininess;
@ -154,6 +155,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;
osg::ref_ptr<const osgDB::Options> _options;
}; };

View File

@ -103,7 +103,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)); OBJWriterNodeVisitor nv(f, osgDB::getSimpleFileName(materialFile), 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 +131,7 @@ public:
// writing to a stream does not support materials // writing to a stream does not support materials
OBJWriterNodeVisitor nv(fout); OBJWriterNodeVisitor nv(fout, "", 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);