| 1 | #include "osg/Shader" |
|---|
| 2 | #include "osg/Notify" |
|---|
| 3 | |
|---|
| 4 | #include <iostream> |
|---|
| 5 | #include <sstream> |
|---|
| 6 | |
|---|
| 7 | #include "osgDB/Registry" |
|---|
| 8 | #include "osgDB/Input" |
|---|
| 9 | #include "osgDB/Output" |
|---|
| 10 | #include "osgDB/FileUtils" |
|---|
| 11 | #include "osgDB/ReadFile" |
|---|
| 12 | #include "osgDB/WriteFile" |
|---|
| 13 | |
|---|
| 14 | using namespace osg; |
|---|
| 15 | using namespace osgDB; |
|---|
| 16 | using namespace std; |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | bool Shader_readLocalData(Object& obj, Input& fr); |
|---|
| 20 | bool Shader_writeLocalData(const Object& obj, Output& fw); |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | REGISTER_DOTOSGWRAPPER(Shader) |
|---|
| 24 | ( |
|---|
| 25 | new osg::Shader, |
|---|
| 26 | "Shader", |
|---|
| 27 | "Object Shader", |
|---|
| 28 | &Shader_readLocalData, |
|---|
| 29 | &Shader_writeLocalData |
|---|
| 30 | ); |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | bool Shader_readLocalData(Object& obj, Input& fr) |
|---|
| 34 | { |
|---|
| 35 | bool iteratorAdvanced = false; |
|---|
| 36 | |
|---|
| 37 | Shader& shader = static_cast<Shader&>(obj); |
|---|
| 38 | |
|---|
| 39 | if (fr.matchSequence("type %w")) |
|---|
| 40 | { |
|---|
| 41 | shader.setType( Shader::getTypeId(fr[1].getStr()) ); |
|---|
| 42 | fr+=2; |
|---|
| 43 | iteratorAdvanced = true; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | if (fr.matchSequence("file %w") || fr.matchSequence("file %s") ) |
|---|
| 47 | { |
|---|
| 48 | |
|---|
| 49 | osg::ref_ptr<Shader> s = osgDB::readShaderFile(fr[1].getStr(), fr.getOptions()); |
|---|
| 50 | if(s.get()) |
|---|
| 51 | shader.setShaderSource(s->getShaderSource()); |
|---|
| 52 | else |
|---|
| 53 | shader.loadShaderSourceFromFile( osgDB::findDataFile(fr[1].getStr()) ); |
|---|
| 54 | |
|---|
| 55 | fr += 2; |
|---|
| 56 | iteratorAdvanced = true; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | if (fr.matchSequence("code {")) |
|---|
| 60 | { |
|---|
| 61 | std::string code; |
|---|
| 62 | fr += 2; |
|---|
| 63 | iteratorAdvanced = true; |
|---|
| 64 | int entry = fr[0].getNoNestedBrackets(); |
|---|
| 65 | while (!fr.eof() && fr[0].getNoNestedBrackets() >= entry) |
|---|
| 66 | { |
|---|
| 67 | if (fr[0].getStr()) { |
|---|
| 68 | code.append(std::string(fr[0].getStr())); |
|---|
| 69 | code += '\n' ; |
|---|
| 70 | } |
|---|
| 71 | ++fr; |
|---|
| 72 | } |
|---|
| 73 | shader.setShaderSource(code.c_str()); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | return iteratorAdvanced; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | bool Shader_writeLocalData(const Object& obj,Output& fw) |
|---|
| 81 | { |
|---|
| 82 | const Shader& shader = static_cast<const Shader&>(obj); |
|---|
| 83 | |
|---|
| 84 | fw.indent() << "type " << shader.getTypename() << std::endl; |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | if (fw.getOutputShaderFiles()) |
|---|
| 90 | { |
|---|
| 91 | std::string fileName = shader.getFileName(); |
|---|
| 92 | |
|---|
| 93 | if (fileName.empty()) |
|---|
| 94 | { |
|---|
| 95 | fileName = fw.getShaderFileNameForOutput(); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | osgDB::writeShaderFile(shader, fileName); |
|---|
| 99 | |
|---|
| 100 | if (!fileName.empty()) |
|---|
| 101 | { |
|---|
| 102 | fw.indent() << "file "<<fw.wrapString(fw.getFileNameForOutput(fileName))<< std::endl; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | } |
|---|
| 106 | else |
|---|
| 107 | { |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | std::vector<std::string> lines; |
|---|
| 111 | std::istringstream iss(shader.getShaderSource()); |
|---|
| 112 | std::string line; |
|---|
| 113 | while (std::getline(iss, line)) { |
|---|
| 114 | lines.push_back(line); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | fw.indent() << "code {\n"; |
|---|
| 118 | fw.moveIn(); |
|---|
| 119 | |
|---|
| 120 | std::vector<std::string>::const_iterator j; |
|---|
| 121 | for (j=lines.begin(); j!=lines.end(); ++j) { |
|---|
| 122 | fw.indent() << fw.wrapString(*j) << "\n"; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | fw.moveOut(); |
|---|
| 126 | fw.indent() << "}\n"; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | return true; |
|---|
| 130 | } |
|---|