| 1 | #include "osg/Program" |
|---|
| 2 | #include "osg/Shader" |
|---|
| 3 | |
|---|
| 4 | #include "osgDB/Registry" |
|---|
| 5 | #include "osgDB/Input" |
|---|
| 6 | #include "osgDB/Output" |
|---|
| 7 | |
|---|
| 8 | using namespace osg; |
|---|
| 9 | using namespace osgDB; |
|---|
| 10 | using namespace std; |
|---|
| 11 | |
|---|
| 12 | extern bool Geometry_matchPrimitiveModeStr(const char* str, GLenum& mode); |
|---|
| 13 | extern const char* Geometry_getPrimitiveModeStr(GLenum mode); |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | bool Program_readLocalData(Object& obj, Input& fr); |
|---|
| 17 | bool Program_writeLocalData(const Object& obj, Output& fw); |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | REGISTER_DOTOSGWRAPPER(Program) |
|---|
| 21 | ( |
|---|
| 22 | new osg::Program, |
|---|
| 23 | "Program", |
|---|
| 24 | "Object StateAttribute Program", |
|---|
| 25 | &Program_readLocalData, |
|---|
| 26 | &Program_writeLocalData |
|---|
| 27 | ); |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | bool Program_readLocalData(Object& obj, Input& fr) |
|---|
| 31 | { |
|---|
| 32 | bool iteratorAdvanced = false; |
|---|
| 33 | |
|---|
| 34 | Program& program = static_cast<Program&>(obj); |
|---|
| 35 | |
|---|
| 36 | if(fr.matchSequence("GeometryVerticesOut %i")) |
|---|
| 37 | { |
|---|
| 38 | unsigned int verticesOut; |
|---|
| 39 | fr[1].getUInt(verticesOut); |
|---|
| 40 | program.setParameter(GL_GEOMETRY_VERTICES_OUT_EXT, verticesOut); |
|---|
| 41 | fr += 2; |
|---|
| 42 | iteratorAdvanced = true; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | if(fr.matchSequence("GeometryInputType %w")) |
|---|
| 46 | { |
|---|
| 47 | std::string primitiveMode = fr[1].getStr(); |
|---|
| 48 | GLenum mode; |
|---|
| 49 | if(Geometry_matchPrimitiveModeStr(primitiveMode.c_str(), mode)) |
|---|
| 50 | program.setParameter(GL_GEOMETRY_INPUT_TYPE_EXT, mode); |
|---|
| 51 | fr += 2; |
|---|
| 52 | iteratorAdvanced = true; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | if(fr.matchSequence("GeometryOutputType %w")) |
|---|
| 56 | { |
|---|
| 57 | std::string primitiveMode = fr[1].getStr(); |
|---|
| 58 | GLenum mode; |
|---|
| 59 | if(Geometry_matchPrimitiveModeStr(primitiveMode.c_str(), mode)) |
|---|
| 60 | program.setParameter(GL_GEOMETRY_OUTPUT_TYPE_EXT, mode); |
|---|
| 61 | fr += 2; |
|---|
| 62 | iteratorAdvanced = true; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | while(fr.matchSequence("AttribBindingLocation %i %w")) |
|---|
| 66 | { |
|---|
| 67 | unsigned int index; |
|---|
| 68 | fr[1].getUInt(index); |
|---|
| 69 | program.addBindAttribLocation(fr[2].getStr(), index); |
|---|
| 70 | fr += 3; |
|---|
| 71 | iteratorAdvanced = true; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | while(fr.matchSequence("AttribBindingLocation %w %i")) |
|---|
| 76 | { |
|---|
| 77 | unsigned int index; |
|---|
| 78 | fr[2].getUInt(index); |
|---|
| 79 | program.addBindAttribLocation(fr[1].getStr(), index); |
|---|
| 80 | fr += 3; |
|---|
| 81 | iteratorAdvanced = true; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | int num_shaders; |
|---|
| 85 | if (fr[0].matchWord("num_shaders") && |
|---|
| 86 | fr[1].getInt(num_shaders)) |
|---|
| 87 | { |
|---|
| 88 | |
|---|
| 89 | fr+=2; |
|---|
| 90 | iteratorAdvanced = true; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | Object* object = NULL; |
|---|
| 94 | while((object=fr.readObject())!=NULL) |
|---|
| 95 | { |
|---|
| 96 | program.addShader(dynamic_cast<Shader*>(object)); |
|---|
| 97 | iteratorAdvanced = true; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | return iteratorAdvanced; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | bool Program_writeLocalData(const Object& obj,Output& fw) |
|---|
| 105 | { |
|---|
| 106 | const Program& program = static_cast<const Program&>(obj); |
|---|
| 107 | |
|---|
| 108 | fw.indent() << "GeometryVerticesOut " << program.getParameter(GL_GEOMETRY_VERTICES_OUT_EXT) << std::endl; |
|---|
| 109 | fw.indent() << "GeometryInputType " << Geometry_getPrimitiveModeStr(program.getParameter(GL_GEOMETRY_INPUT_TYPE_EXT)) << std::endl; |
|---|
| 110 | fw.indent() << "GeometryOutputType " << Geometry_getPrimitiveModeStr(program.getParameter(GL_GEOMETRY_OUTPUT_TYPE_EXT)) << std::endl; |
|---|
| 111 | |
|---|
| 112 | const Program::AttribBindingList& abl = program.getAttribBindingList(); |
|---|
| 113 | Program::AttribBindingList::const_iterator i; |
|---|
| 114 | for(i=abl.begin(); i!=abl.end(); i++) |
|---|
| 115 | { |
|---|
| 116 | fw.indent() << "AttribBindingLocation " << (*i).first << " " << (*i).second << std::endl; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | fw.indent() << "num_shaders " << program.getNumShaders() << std::endl; |
|---|
| 120 | for(unsigned int ic=0;ic<program.getNumShaders();++ic) |
|---|
| 121 | { |
|---|
| 122 | fw.writeObject(*program.getShader(ic)); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | return true; |
|---|
| 126 | } |
|---|