| 1 | #include <osg/Shader> |
|---|
| 2 | #include <osg/Notify> |
|---|
| 3 | #include <osg/GL> |
|---|
| 4 | |
|---|
| 5 | #include <osgDB/Registry> |
|---|
| 6 | #include <osgDB/FileNameUtils> |
|---|
| 7 | #include <osgDB/FileUtils> |
|---|
| 8 | #include <osgDB/fstream> |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | class ReaderWriterGLSL : public osgDB::ReaderWriter |
|---|
| 12 | { |
|---|
| 13 | public: |
|---|
| 14 | |
|---|
| 15 | ReaderWriterGLSL() |
|---|
| 16 | { |
|---|
| 17 | supportsExtension("gl","OpenGL Shader Language format"); |
|---|
| 18 | supportsExtension("frag","OpenGL Shader Language format"); |
|---|
| 19 | supportsExtension("vert","OpenGL Shader Language format"); |
|---|
| 20 | supportsExtension("glsl","OpenGL Shader Language format"); |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | virtual const char* className() const { return "GLSL Shader Reader"; } |
|---|
| 24 | |
|---|
| 25 | virtual ReadResult readShader(std::istream& fin,const Options* options) const |
|---|
| 26 | { |
|---|
| 27 | |
|---|
| 28 | fin.seekg(0, std::ios::end); |
|---|
| 29 | int length = fin.tellg(); |
|---|
| 30 | char *text = new char[length + 1]; |
|---|
| 31 | fin.seekg(0, std::ios::beg); |
|---|
| 32 | fin.read(text, length); |
|---|
| 33 | text[length] = '\0'; |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | osg::Shader* shader = new osg::Shader(); |
|---|
| 37 | shader->setShaderSource( text ); |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | if (options) |
|---|
| 41 | { |
|---|
| 42 | if (options->getOptionString().find("fragment")!=std::string::npos) shader->setType(osg::Shader::FRAGMENT); |
|---|
| 43 | if (options->getOptionString().find("vertex")!=std::string::npos) shader->setType(osg::Shader::VERTEX); |
|---|
| 44 | if (options->getOptionString().find("geometry")!=std::string::npos) shader->setType(osg::Shader::GEOMETRY); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | delete [] text; |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | return shader; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | virtual ReadResult readShader(const std::string& file, const osgDB::ReaderWriter::Options* options) const |
|---|
| 55 | { |
|---|
| 56 | std::string ext = osgDB::getLowerCaseFileExtension(file); |
|---|
| 57 | if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; |
|---|
| 58 | |
|---|
| 59 | std::string fileName = osgDB::findDataFile( file, options ); |
|---|
| 60 | if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; |
|---|
| 61 | |
|---|
| 62 | osgDB::ifstream istream(fileName.c_str(), std::ios::in | std::ios::binary); |
|---|
| 63 | if(!istream) return ReadResult::FILE_NOT_HANDLED; |
|---|
| 64 | ReadResult rr = readShader(istream, options); |
|---|
| 65 | if(rr.validShader()) |
|---|
| 66 | { |
|---|
| 67 | osg::Shader* shader = rr.getShader(); |
|---|
| 68 | shader->setFileName(file); |
|---|
| 69 | if (shader->getType() == osg::Shader::UNDEFINED) |
|---|
| 70 | { |
|---|
| 71 | |
|---|
| 72 | if (ext == "frag") shader->setType(osg::Shader::FRAGMENT); |
|---|
| 73 | if (ext == "vert") shader->setType(osg::Shader::VERTEX); |
|---|
| 74 | } |
|---|
| 75 | } |
|---|
| 76 | return rr; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | virtual WriteResult writeShader(const osg::Shader& shader,std::ostream& fout,const Options* = NULL) const |
|---|
| 80 | { |
|---|
| 81 | |
|---|
| 82 | std::string source = shader.getShaderSource(); |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | fout << source; |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | return WriteResult::FILE_SAVED; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | virtual WriteResult writeShader(const osg::Shader &shader,const std::string& fileName, const osgDB::ReaderWriter::Options*) const |
|---|
| 92 | { |
|---|
| 93 | std::string ext = osgDB::getFileExtension(fileName); |
|---|
| 94 | if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED; |
|---|
| 95 | |
|---|
| 96 | osgDB::ofstream fout(fileName.c_str(), std::ios::out | std::ios::binary); |
|---|
| 97 | if(!fout) return WriteResult::ERROR_IN_WRITING_FILE; |
|---|
| 98 | |
|---|
| 99 | return writeShader(shader, fout); |
|---|
| 100 | } |
|---|
| 101 | }; |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | REGISTER_OSGPLUGIN(glsl, ReaderWriterGLSL) |
|---|