| 1 | #include <osg/Shader> |
|---|
| 2 | #include <osgDB/ObjectWrapper> |
|---|
| 3 | #include <osgDB/InputStream> |
|---|
| 4 | #include <osgDB/OutputStream> |
|---|
| 5 | |
|---|
| 6 | static bool checkShaderSource( const osg::Shader& shader ) |
|---|
| 7 | { |
|---|
| 8 | return !shader.getShaderSource().empty(); |
|---|
| 9 | } |
|---|
| 10 | |
|---|
| 11 | static bool readShaderSource( osgDB::InputStream& is, osg::Shader& shader ) |
|---|
| 12 | { |
|---|
| 13 | std::string code; |
|---|
| 14 | unsigned int size = is.readSize(); is >> osgDB::BEGIN_BRACKET; |
|---|
| 15 | for ( unsigned int i=0; i<size; ++i ) |
|---|
| 16 | { |
|---|
| 17 | std::string line; |
|---|
| 18 | is.readWrappedString( line ); |
|---|
| 19 | code.append( line ); code.append( 1, '\n' ); |
|---|
| 20 | } |
|---|
| 21 | is >> osgDB::END_BRACKET; |
|---|
| 22 | shader.setShaderSource( code ); |
|---|
| 23 | return true; |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | static bool writeShaderSource( osgDB::OutputStream& os, const osg::Shader& shader ) |
|---|
| 27 | { |
|---|
| 28 | std::vector<std::string> lines; |
|---|
| 29 | std::istringstream iss( shader.getShaderSource() ); |
|---|
| 30 | std::string line; |
|---|
| 31 | while ( std::getline(iss, line) ) |
|---|
| 32 | { |
|---|
| 33 | lines.push_back( line ); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | os.writeSize(lines.size()); os << osgDB::BEGIN_BRACKET << std::endl; |
|---|
| 37 | for ( std::vector<std::string>::const_iterator itr=lines.begin(); |
|---|
| 38 | itr!=lines.end(); ++itr ) |
|---|
| 39 | { |
|---|
| 40 | os.writeWrappedString( *itr ); |
|---|
| 41 | os << std::endl; |
|---|
| 42 | } |
|---|
| 43 | os << osgDB::END_BRACKET << std::endl; |
|---|
| 44 | return true; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | REGISTER_OBJECT_WRAPPER( Shader, |
|---|
| 48 | new osg::Shader, |
|---|
| 49 | osg::Shader, |
|---|
| 50 | "osg::Object osg::Shader" ) |
|---|
| 51 | { |
|---|
| 52 | BEGIN_ENUM_SERIALIZER3( Type, UNDEFINED ); |
|---|
| 53 | ADD_ENUM_VALUE( VERTEX ); |
|---|
| 54 | ADD_ENUM_VALUE( TESSCONTROL ); |
|---|
| 55 | ADD_ENUM_VALUE( TESSEVALUATION ); |
|---|
| 56 | ADD_ENUM_VALUE( FRAGMENT ); |
|---|
| 57 | ADD_ENUM_VALUE( GEOMETRY ); |
|---|
| 58 | ADD_ENUM_VALUE( UNDEFINED ); |
|---|
| 59 | END_ENUM_SERIALIZER(); |
|---|
| 60 | |
|---|
| 61 | ADD_USER_SERIALIZER( ShaderSource ); |
|---|
| 62 | ADD_OBJECT_SERIALIZER( ShaderBinary, osg::ShaderBinary, NULL ); |
|---|
| 63 | } |
|---|