Added osg::ShaderBinary::readShaderBinaryFile(..) static method

This commit is contained in:
Robert Osfield 2009-11-18 11:25:28 +00:00
parent f8ab593c4b
commit 4262d10217
2 changed files with 30 additions and 7 deletions

View File

@ -58,6 +58,10 @@ class OSG_EXPORT ShaderBinary : public osg::Object
/** Get a const ptr to the shader binary data.*/ /** Get a const ptr to the shader binary data.*/
const unsigned char* getData() const { return _data.empty() ? 0 : &(_data.front()); } const unsigned char* getData() const { return _data.empty() ? 0 : &(_data.front()); }
/** Read shader binary from file.
* Return the resulting Shader or 0 if no valid shader binary could be read.*/
static ShaderBinary* readShaderBinaryFile(const std::string& fileName);
protected: protected:
typedef std::vector<unsigned char> Data; typedef std::vector<unsigned char> Data;
@ -132,7 +136,7 @@ class OSG_EXPORT Shader : public osg::Object
/** Read shader source from file and then constructor shader of specified type. /** Read shader source from file and then constructor shader of specified type.
* Return the resulting Shader or 0 if no valid shader source code be read.*/ * Return the resulting Shader or 0 if no valid shader source could be read.*/
static Shader* readShaderFile( Type type, const std::string& fileName ); static Shader* readShaderFile( Type type, const std::string& fileName );
/** Load the Shader's source code text from a file. */ /** Load the Shader's source code text from a file. */

View File

@ -63,6 +63,25 @@ void ShaderBinary::assign(unsigned int size, const unsigned char* data)
} }
} }
ShaderBinary* ShaderBinary::readShaderBinaryFile(const std::string& fileName)
{
std::ifstream fin;
fin.open(fileName.c_str(), std::ios::binary);
if (!fin) return 0;
fin.seekg(0, std::ios::end);
int length = fin.tellg();
if (length==0) return 0;
osg::ref_ptr<ShaderBinary> shaderBinary = new osg::ShaderBinary;
shaderBinary->allocate(length);
fin.seekg(0, std::ios::beg);
fin.read(reinterpret_cast<char*>(shaderBinary->getData()), length);
fin.close();
return shaderBinary.release();
}
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// static cache of glShaders flagged for deletion, which will actually // static cache of glShaders flagged for deletion, which will actually
// be deleted in the correct GL context. // be deleted in the correct GL context.