From Art Trevs, add support for saving external shader files.

From Robert Osfield, adding missing member variable initializes and Output::getShaderFileNameForOutput() implementation
This commit is contained in:
Robert Osfield
2008-03-04 14:04:48 +00:00
parent 02b52cb73a
commit c4d07194a2
22 changed files with 515 additions and 19 deletions

View File

@@ -83,6 +83,12 @@ class OSG_EXPORT Shader : public osg::Object
/** Get the Shader type as a descriptive string. */
const char* getTypename() const;
/** Set file name for the shader source code. */
inline void setFileName(const std::string& fileName) { _shaderFileName = fileName; }
/** Get filename to which the shader source code belongs. */
inline const std::string& getFileName() const { return _shaderFileName; }
/** Resize any per context GLObject buffers to specified size. */
virtual void resizeGLObjectBuffers(unsigned int maxSize);
@@ -178,6 +184,7 @@ class OSG_EXPORT Shader : public osg::Object
protected: /*data*/
Type _type;
std::string _shaderSource;
std::string _shaderFileName;
/** osg::Programs that this osg::Shader is attached to */
typedef std::set< osg::Program* > ProgramSet;
ProgramSet _programSet;

View File

@@ -15,6 +15,7 @@
#define OSGDB_INPUT 1
#include <osg/Image>
#include <osg/Shader>
#include <osg/Node>
#include <osg/Drawable>
#include <osg/StateAttribute>
@@ -50,10 +51,12 @@ class OSGDB_EXPORT Input : public FieldReaderIterator
virtual osg::StateAttribute* readStateAttribute();
virtual osg::Uniform* readUniform();
virtual osg::Node* readNode();
virtual osg::Shader* readShader();
virtual osg::Object* readObject(const std::string& fileName);
virtual osg::Image* readImage(const std::string& fileName);
virtual osg::Node* readNode(const std::string& fileName);
virtual osg::Shader* readShader(const std::string& fileName);
virtual osg::Object* getObjectForUniqueID(const std::string& uniqueID);
virtual void registerUniqueIDForObject(const std::string& uniqueID,osg::Object* obj);

View File

@@ -92,6 +92,11 @@ class OSGDB_EXPORT Output : public std::ofstream
bool getOutputTextureFiles() const { return _outputTextureFiles; }
virtual std::string getTextureFileNameForOutput();
void setOutputShaderFiles(bool flag) { _outputShaderFiles = flag; }
bool getOutputShaderFiles() const { return _outputShaderFiles; }
virtual std::string getShaderFileNameForOutput();
protected:
@@ -111,9 +116,13 @@ class OSGDB_EXPORT Output : public std::ofstream
std::string _filename;
PathNameHint _pathNameHint;
bool _outputTextureFiles;
unsigned int _textureFileNameNumber;
bool _outputShaderFiles;
unsigned int _shaderFileNameNumber;
bool _writeOutDefaultValues;
};

View File

@@ -132,6 +132,26 @@ inline osg::Node* readNodeFiles(osg::ArgumentParser& parser)
return readNodeFiles(parser,Registry::instance()->getOptions());
}
/** Read an osg::Shader from file.
* Return valid osg::Shader on success,
* return NULL on failure.
* Use the Options object to control cache operations and file search paths in osgDB::Registry.
* The osgDB::Registry is used to load the appropriate ReaderWriter plugin
* for the filename extension, and this plugin then handles the request
* to read the specified file.*/
extern OSGDB_EXPORT osg::Shader* readShaderFile(const std::string& filename,const ReaderWriter::Options* options);
/** Read an osg::Shader from file.
* Return valid osg::Shader on success,
* return NULL on failure.
* The osgDB::Registry is used to load the appropriate ReaderWriter plugin
* for the filename extension, and this plugin then handles the request
* to read the specified file.*/
inline osg::Shader* readShaderFile(const std::string& filename)
{
return readShaderFile(filename,Registry::instance()->getOptions());
}
/** Read an osg::Object from file.
* Return an assigned osg::ref_ptr on success,
@@ -213,6 +233,26 @@ inline osg::ref_ptr<osg::Node> readRefNodeFile(const std::string& filename)
return readRefNodeFile(filename,Registry::instance()->getOptions());
}
/** Read an osg::Shader from file.
* Return an assigned osg::ref_ptr on success,
* return an osg::ref_ptr with a NULL pointer assigned to it on failure.
* Use the Options object to control cache operations and file search paths in osgDB::Registry.
* The osgDB::Registry is used to load the appropriate ReaderWriter plugin
* for the filename extension, and this plugin then handles the request
* to read the specified file.*/
extern OSGDB_EXPORT osg::ref_ptr<osg::Shader> readRefShaderFile(const std::string& filename,const ReaderWriter::Options* options);
/** Read an osg::Shader from file.
* Return an assigned osg::ref_ptr on success,
* return an osg::ref_ptr with a NULL pointer assigned to it on failure.
* The osgDB::Registry is used to load the appropriate ReaderWriter plugin
* for the filename extension, and this plugin then handles the request
* to read the specified file.*/
inline osg::ref_ptr<osg::Shader> readRefShaderFile(const std::string& filename)
{
return readRefShaderFile(filename,Registry::instance()->getOptions());
}
}

View File

@@ -75,12 +75,16 @@ class OSGDB_EXPORT ReaderWriter : public osg::Object
/// cache objects loaded via readObject(filename)
CACHE_OBJECTS = 16,
/// cache shaders loaded via readShader(filename)
CACHE_SHADERS = 32,
/// cache on all read*(filename) calls
CACHE_ALL = CACHE_NODES |
CACHE_IMAGES |
CACHE_HEIGHTFIELDS |
CACHE_ARCHIVES |
CACHE_OBJECTS
CACHE_OBJECTS |
CACHE_SHADERS
};
@@ -175,18 +179,21 @@ class OSGDB_EXPORT ReaderWriter : public osg::Object
osg::HeightField* getHeightField();
osg::Node* getNode();
osgDB::Archive* getArchive();
osg::Shader* getShader();
bool validObject() { return _object.valid(); }
bool validImage() { return getImage()!=0; }
bool validHeightField() { return getHeightField()!=0; }
bool validNode() { return getNode()!=0; }
bool validArchive() { return getArchive()!=0; }
bool validShader() { return getShader()!=0; }
osg::Object* takeObject();
osg::Image* takeImage();
osg::HeightField* takeHeightField();
osg::Node* takeNode();
osgDB::Archive* takeArchive();
osg::Shader* takeShader();
std::string& message() { return _message; }
const std::string& message() const { return _message; }
@@ -253,21 +260,25 @@ class OSGDB_EXPORT ReaderWriter : public osg::Object
virtual ReadResult readImage(const std::string& /*fileName*/,const Options* =NULL) const { return ReadResult(ReadResult::FILE_NOT_HANDLED); }
virtual ReadResult readHeightField(const std::string& /*fileName*/,const Options* =NULL) const { return ReadResult(ReadResult::FILE_NOT_HANDLED); }
virtual ReadResult readNode(const std::string& /*fileName*/,const Options* =NULL) const { return ReadResult(ReadResult::FILE_NOT_HANDLED); }
virtual ReadResult readShader(const std::string& /*fileName*/,const Options* =NULL) const { return ReadResult(ReadResult::FILE_NOT_HANDLED); }
virtual WriteResult writeObject(const osg::Object& /*obj*/,const std::string& /*fileName*/,const Options* =NULL) const {return WriteResult(WriteResult::FILE_NOT_HANDLED); }
virtual WriteResult writeImage(const osg::Image& /*image*/,const std::string& /*fileName*/,const Options* =NULL) const {return WriteResult(WriteResult::FILE_NOT_HANDLED); }
virtual WriteResult writeHeightField(const osg::HeightField& /*heightField*/,const std::string& /*fileName*/,const Options* =NULL) const {return WriteResult(WriteResult::FILE_NOT_HANDLED); }
virtual WriteResult writeNode(const osg::Node& /*node*/,const std::string& /*fileName*/,const Options* =NULL) const { return WriteResult(WriteResult::FILE_NOT_HANDLED); }
virtual WriteResult writeShader(const osg::Shader& /*shader*/,const std::string& /*fileName*/,const Options* =NULL) const {return WriteResult(WriteResult::FILE_NOT_HANDLED); }
virtual ReadResult readObject(std::istream& /*fin*/,const Options* =NULL) const { return ReadResult(ReadResult::FILE_NOT_HANDLED); }
virtual ReadResult readImage(std::istream& /*fin*/,const Options* =NULL) const { return ReadResult(ReadResult::FILE_NOT_HANDLED); }
virtual ReadResult readHeightField(std::istream& /*fin*/,const Options* =NULL) const { return ReadResult(ReadResult::FILE_NOT_HANDLED); }
virtual ReadResult readNode(std::istream& /*fin*/,const Options* =NULL) const { return ReadResult(ReadResult::FILE_NOT_HANDLED); }
virtual ReadResult readShader(std::istream& /*fin*/,const Options* =NULL) const { return ReadResult(ReadResult::FILE_NOT_HANDLED); }
virtual WriteResult writeObject(const osg::Object& /*obj*/,std::ostream& /*fout*/,const Options* =NULL) const { return WriteResult(WriteResult::FILE_NOT_HANDLED); }
virtual WriteResult writeImage(const osg::Image& /*image*/,std::ostream& /*fout*/,const Options* =NULL) const { return WriteResult(WriteResult::FILE_NOT_HANDLED); }
virtual WriteResult writeHeightField(const osg::HeightField& /*heightField*/,std::ostream& /*fout*/,const Options* =NULL) const { return WriteResult(WriteResult::FILE_NOT_HANDLED); }
virtual WriteResult writeNode(const osg::Node& /*node*/,std::ostream& /*fout*/,const Options* =NULL) const { return WriteResult(WriteResult::FILE_NOT_HANDLED); }
virtual WriteResult writeShader(const osg::Shader& /*shader*/,std::ostream& /*fout*/,const Options* =NULL) const { return WriteResult(WriteResult::FILE_NOT_HANDLED); }
};

View File

@@ -126,6 +126,7 @@ class OSGDB_EXPORT Registry : public osg::Referenced
osg::Uniform* readUniform(Input& fr);
osg::StateAttribute* readStateAttribute(Input& fr);
osg::Node* readNode(Input& fr);
osg::Shader* readShader(Input& fr);
bool writeObject(const osg::Object& obj,Output& fw);
@@ -158,6 +159,11 @@ class OSGDB_EXPORT Registry : public osg::Referenced
{
return osgDB::Registry::instance()->readNodeImplementation(filename,options);
}
virtual ReaderWriter::ReadResult readShader(const std::string& filename, const ReaderWriter::Options* options)
{
return osgDB::Registry::instance()->readShaderImplementation(filename,options);
}
protected:
virtual ~ReadFileCallback() {}
@@ -208,6 +214,12 @@ class OSGDB_EXPORT Registry : public osg::Referenced
}
ReaderWriter::ReadResult readNodeImplementation(const std::string& fileName,const ReaderWriter::Options* options);
ReaderWriter::ReadResult readShader(const std::string& fileName,const ReaderWriter::Options* options)
{
if (_readFileCallback.valid()) return _readFileCallback->readShader(fileName,options);
else return readShaderImplementation(fileName,options);
}
ReaderWriter::ReadResult readShaderImplementation(const std::string& fileName,const ReaderWriter::Options* options);
@@ -234,6 +246,11 @@ class OSGDB_EXPORT Registry : public osg::Referenced
{
return osgDB::Registry::instance()->writeNodeImplementation(obj,fileName,options);
}
virtual ReaderWriter::WriteResult writeShader(const osg::Shader& obj, const std::string& fileName,const ReaderWriter::Options* options)
{
return osgDB::Registry::instance()->writeShaderImplementation(obj,fileName,options);
}
protected:
virtual ~WriteFileCallback() {}
@@ -277,6 +294,12 @@ class OSGDB_EXPORT Registry : public osg::Referenced
}
ReaderWriter::WriteResult writeNodeImplementation(const osg::Node& node, const std::string& fileName,const ReaderWriter::Options* options);
ReaderWriter::WriteResult writeShader(const osg::Shader& obj, const std::string& fileName,const ReaderWriter::Options* options)
{
if (_writeFileCallback.valid()) return _writeFileCallback->writeShader(obj,fileName,options);
else return writeShaderImplementation(obj,fileName,options);
}
ReaderWriter::WriteResult writeShaderImplementation(const osg::Shader& obj, const std::string& fileName,const ReaderWriter::Options* options);
void setCreateNodeFromImage(bool flag) { _createNodeFromImage = flag; }
@@ -447,6 +470,7 @@ class OSGDB_EXPORT Registry : public osg::Referenced
struct ReadHeightFieldFunctor;
struct ReadNodeFunctor;
struct ReadArchiveFunctor;
struct ReadShaderFunctor;
// make helper classes friends to get round VS6.0 "issues"
friend struct ReadFunctor;
@@ -455,6 +479,7 @@ class OSGDB_EXPORT Registry : public osg::Referenced
friend struct ReadHeightFieldFunctor;
friend struct ReadNodeFunctor;
friend struct ReadArchiveFunctor;
friend struct ReadShaderFunctor;
ReaderWriter::ReadResult read(const ReadFunctor& readFunctor);
ReaderWriter::ReadResult readImplementation(const ReadFunctor& readFunctor,bool useObjectCache);
@@ -474,6 +499,7 @@ class OSGDB_EXPORT Registry : public osg::Referenced
DotOsgWrapperMap _stateAttrWrapperMap;
DotOsgWrapperMap _uniformWrapperMap;
DotOsgWrapperMap _nodeWrapperMap;
DotOsgWrapperMap _shaderWrapperMap;
DotOsgWrapperMap _classNameWrapperMap;

View File

@@ -105,6 +105,26 @@ inline bool writeNodeFile(const osg::Node& node, const std::string& filename)
return writeNodeFile( node, filename, Registry::instance()->getOptions() );
}
/** Write an osg::Shader to file.
* Return true on success,
* return false on failure.
* Use the Options object to control cache operations and file search paths in osgDB::Registry.
* The osgDB::Registry is used to load the appropriate ReaderWriter plugin
* for the filename extension, and this plugin then handles the request
* to write the specified file.*/
extern OSGDB_EXPORT bool writeShaderFile(const osg::Shader& shader, const std::string& filename, const ReaderWriter::Options* options );
/** Write an osg::Shader to file.
* Return true on success,
* return false on failure.
* The osgDB::Registry is used to load the appropriate ReaderWriter plugin
* for the filename extension, and this plugin then handles the request
* to write the specified file.*/
inline bool writeShaderFile(const osg::Shader& shader, const std::string& filename)
{
return writeShaderFile( shader, filename, Registry::instance()->getOptions() );
}
}
#endif