diff --git a/src/osgPlugins/ive/DataInputStream.cpp b/src/osgPlugins/ive/DataInputStream.cpp index 49546d463..b682d9267 100644 --- a/src/osgPlugins/ive/DataInputStream.cpp +++ b/src/osgPlugins/ive/DataInputStream.cpp @@ -60,6 +60,7 @@ #include "Impostor.h" #include "CoordinateSystemNode.h" #include "Uniform.h" +#include "Shader.h" #include "LightPointNode.h" #include "MultiSwitch.h" @@ -818,6 +819,31 @@ osg::Uniform* DataInputStream::readUniform() return uniform; } + +osg::Shader* DataInputStream::readShader() +{ + // Read shaders unique ID. + int id = readInt(); + // See if shader is already in the list. + ShaderMap::iterator itr= _shaderMap.find(id); + if (itr!=_shaderMap.end()) return itr->second.get(); + + // Shader is not in list. + // Create a new shader, + osg::Shader* shader = new osg::Shader(); + + // read its properties from stream + ((ive::Shader*)(shader))->read(this); + + // and add it to the shader map, + _shaderMap[id] = shader; + + + if (_verboseOutput) std::cout<<"read/writeShader() ["< > StateSetMap; typedef std::map > StateAttributeMap; typedef std::map > UniformMap; + typedef std::map > ShaderMap; typedef std::map > DrawableMap; typedef std::map > ShapeMap; typedef std::map > NodeMap; @@ -109,13 +111,14 @@ private: StateSetMap _statesetMap; StateAttributeMap _stateAttributeMap; UniformMap _uniformMap; + ShaderMap _shaderMap; DrawableMap _drawableMap; ShapeMap _shapeMap; NodeMap _nodeMap; bool _loadExternalReferenceFiles; - osg::ref_ptr _options; + osg::ref_ptr _options; }; diff --git a/src/osgPlugins/ive/DataOutputStream.cpp b/src/osgPlugins/ive/DataOutputStream.cpp index 7bcf8dc1d..dcc77db24 100644 --- a/src/osgPlugins/ive/DataOutputStream.cpp +++ b/src/osgPlugins/ive/DataOutputStream.cpp @@ -43,6 +43,7 @@ #include "FrontFace.h" #include "Program.h" #include "Uniform.h" +#include "Shader.h" #include "Group.h" #include "MatrixTransform.h" @@ -599,7 +600,7 @@ void DataOutputStream::writeUniform(const osg::Uniform* uniform) // Id already exists so just write ID. writeInt(itr->second); - if (_verboseOutput) std::cout<<"read/writeStateSet() ["<second<<"]"<second<<"]"<second); + + if (_verboseOutput) std::cout<<"read/writeShader() ["<second<<"]"<write(this); + + if (_verboseOutput) std::cout<<"read/writeShader() ["< StateSetMap; typedef std::map StateAttributeMap; typedef std::map UniformMap; + typedef std::map ShaderMap; typedef std::map DrawableMap; typedef std::map ShapeMap; typedef std::map NodeMap; @@ -109,6 +111,7 @@ private: StateSetMap _stateSetMap; StateAttributeMap _stateAttributeMap; UniformMap _uniformMap; + ShaderMap _shaderMap; DrawableMap _drawableMap; ShapeMap _shapeMap; NodeMap _nodeMap; diff --git a/src/osgPlugins/ive/Program.cpp b/src/osgPlugins/ive/Program.cpp index 0321daaef..bb60e81de 100644 --- a/src/osgPlugins/ive/Program.cpp +++ b/src/osgPlugins/ive/Program.cpp @@ -30,6 +30,22 @@ void Program::write(DataOutputStream* out){ else throw Exception("Program::write(): Could not cast this osg::Program to an osg::Object."); + const AttribBindingList& abl = getAttribBindingList(); + out->writeUInt(abl.size()); + for(AttribBindingList::const_iterator itr = abl.begin(); + itr != abl.end(); + ++itr) + { + out->writeString(itr->first); + out->writeUInt(itr->second); + } + + // Write + out->writeUInt(getNumShaders()); + for(unsigned int si=0; siwriteShader(getShader(si)); + } } void Program::read(DataInputStream* in) @@ -54,4 +70,22 @@ void Program::read(DataInputStream* in) { throw Exception("Program::read(): Expected Program identification."); } + + // reading in shaders. + unsigned int size = in->readUInt(); + for(unsigned int ai=0; aireadString(); + unsigned int index = in->readUInt(); + addBindAttribLocation(name, index); + } + + // reading in shaders. + size = in->readUInt(); + for(unsigned int si=0; sireadShader(); + addShader(shader); + } + } diff --git a/src/osgPlugins/ive/Shader.cpp b/src/osgPlugins/ive/Shader.cpp index 79df6478a..4b8b3c450 100644 --- a/src/osgPlugins/ive/Shader.cpp +++ b/src/osgPlugins/ive/Shader.cpp @@ -30,6 +30,10 @@ void Shader::write(DataOutputStream* out){ else throw Exception("Shader::write(): Could not cast this osg::Shader to an osg::Object."); + out->writeString(getName()); + out->writeInt(getType()); + out->writeString(getShaderSource()); + } void Shader::read(DataInputStream* in) @@ -54,4 +58,9 @@ void Shader::read(DataInputStream* in) { throw Exception("Shader::read(): Expected Shader identification."); } + + setName(in->readString()); + setType(static_cast(in->readInt())); + setShaderSource(in->readString()); + } diff --git a/src/osgPlugins/ive/StateSet.cpp b/src/osgPlugins/ive/StateSet.cpp index ac8a15b1a..e8a4cd5ad 100644 --- a/src/osgPlugins/ive/StateSet.cpp +++ b/src/osgPlugins/ive/StateSet.cpp @@ -90,10 +90,11 @@ void StateSet::write(DataOutputStream* out){ if ( out->getVersion() >= VERSION_0010 ) { - // Write stateset attributes, this could for instance be alphafunctions, materials, etc. + // Write stateset uniforms StateSet::UniformList ul = getUniformList(); out->writeInt(ul.size()); - for(StateSet::UniformList::iterator uitr=ul.begin(); uitr!=ul.end(); ++uitr){ + for(StateSet::UniformList::iterator uitr=ul.begin(); uitr!=ul.end(); ++uitr) + { out->writeUniform(uitr->second.first.get()); out->writeInt(uitr->second.second); } @@ -165,9 +166,11 @@ void StateSet::read(DataInputStream* in){ // Read texture attributes. nUnits = in->readInt(); - for(unit=0;unitreadInt(); - for(i=0;ireadStateAttribute(); setTextureAttribute(unit, attribute, (osg::StateAttribute::OverrideValue)in->readInt()); } @@ -177,7 +180,8 @@ void StateSet::read(DataInputStream* in){ { // Read uniforms size = in->readInt(); - for(i=0;ireadUniform(); addUniform(uniform, (osg::StateAttribute::OverrideValue)in->readInt()); } diff --git a/src/osgPlugins/ive/Uniform.cpp b/src/osgPlugins/ive/Uniform.cpp index 23e17aa46..832cde200 100644 --- a/src/osgPlugins/ive/Uniform.cpp +++ b/src/osgPlugins/ive/Uniform.cpp @@ -30,6 +30,8 @@ void Uniform::write(DataOutputStream* out){ else throw Exception("Uniform::write(): Could not cast this osg::Uniform to an osg::Object."); + out->writeInt(getType()); + out->writeString(getName()); } void Uniform::read(DataInputStream* in) @@ -54,4 +56,7 @@ void Uniform::read(DataInputStream* in) { throw Exception("Uniform::read(): Expected Uniform identification."); } + + setType(static_cast(in->readInt())); + setName(in->readString()); }