| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include "Exception.h" |
|---|
| 16 | #include "StateSet.h" |
|---|
| 17 | #include "Object.h" |
|---|
| 18 | |
|---|
| 19 | #include <osg/StateAttribute> |
|---|
| 20 | |
|---|
| 21 | using namespace ive; |
|---|
| 22 | |
|---|
| 23 | void StateSet::write(DataOutputStream* out){ |
|---|
| 24 | |
|---|
| 25 | out->writeInt(IVESTATESET); |
|---|
| 26 | |
|---|
| 27 | osg::Object* obj = dynamic_cast<osg::Object*>(this); |
|---|
| 28 | if(obj){ |
|---|
| 29 | ((ive::Object*)(obj))->write(out); |
|---|
| 30 | } |
|---|
| 31 | else |
|---|
| 32 | out_THROW_EXCEPTION("StateSet::write(): Could not cast this osg::StateSet to an osg::Object."); |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | switch(getRenderBinMode()){ |
|---|
| 36 | case osg::StateSet::INHERIT_RENDERBIN_DETAILS: out->writeChar((char)0);break; |
|---|
| 37 | case osg::StateSet::USE_RENDERBIN_DETAILS: out->writeChar((char)1);break; |
|---|
| 38 | case osg::StateSet::OVERRIDE_RENDERBIN_DETAILS: out->writeChar((char)2);break; |
|---|
| 39 | default: out_THROW_EXCEPTION("Unknown RenderBinMode in StateSet::write()"); |
|---|
| 40 | } |
|---|
| 41 | out->writeInt(getBinNumber()); |
|---|
| 42 | out->writeString(getBinName()); |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | StateSet::ModeList ml = getModeList(); |
|---|
| 46 | StateSet::AttributeList al = getAttributeList(); |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | out->writeInt(ml.size()); |
|---|
| 50 | for(StateSet::ModeList::const_iterator mitr=ml.begin(); mitr!=ml.end(); ++mitr){ |
|---|
| 51 | out->writeInt((unsigned int)mitr->first); |
|---|
| 52 | out->writeInt((unsigned int)mitr->second); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | out->writeInt(al.size()); |
|---|
| 57 | for(StateSet::AttributeList::iterator aitr=al.begin(); aitr!=al.end(); ++aitr){ |
|---|
| 58 | out->writeStateAttribute(aitr->second.first.get()); |
|---|
| 59 | out->writeInt(aitr->second.second); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | StateSet::TextureModeList tml = getTextureModeList(); |
|---|
| 63 | StateSet::TextureAttributeList tal = getTextureAttributeList(); |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | int nUnits = tml.size(); |
|---|
| 67 | out->writeInt(nUnits); |
|---|
| 68 | int unit; |
|---|
| 69 | for(unit=0;unit<nUnits;unit++){ |
|---|
| 70 | ml = tml[unit]; |
|---|
| 71 | out->writeInt(ml.size()); |
|---|
| 72 | for(StateSet::ModeList::const_iterator mitr=ml.begin(); mitr!=ml.end(); ++mitr){ |
|---|
| 73 | out->writeInt((unsigned int)mitr->first); |
|---|
| 74 | out->writeInt((unsigned int)mitr->second); |
|---|
| 75 | } |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | nUnits = tal.size(); |
|---|
| 80 | out->writeInt(nUnits); |
|---|
| 81 | for(unit=0;unit<nUnits;unit++){ |
|---|
| 82 | al = tal[unit]; |
|---|
| 83 | out->writeInt(al.size()); |
|---|
| 84 | for(StateSet::AttributeList::iterator aitr=al.begin(); aitr!=al.end(); ++aitr){ |
|---|
| 85 | out->writeStateAttribute(aitr->second.first.get()); |
|---|
| 86 | out->writeInt(aitr->second.second); |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | if ( out->getVersion() >= VERSION_0010 ) |
|---|
| 91 | { |
|---|
| 92 | |
|---|
| 93 | StateSet::UniformList ul = getUniformList(); |
|---|
| 94 | out->writeInt(ul.size()); |
|---|
| 95 | for(StateSet::UniformList::iterator uitr=ul.begin(); uitr!=ul.end(); ++uitr) |
|---|
| 96 | { |
|---|
| 97 | out->writeUniform(uitr->second.first.get()); |
|---|
| 98 | out->writeInt(uitr->second.second); |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | void StateSet::read(DataInputStream* in){ |
|---|
| 104 | |
|---|
| 105 | int id = in->peekInt(); |
|---|
| 106 | if(id == IVESTATESET){ |
|---|
| 107 | |
|---|
| 108 | id = in->readInt(); |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | osg::Object* obj = dynamic_cast<osg::Object*>(this); |
|---|
| 112 | if(obj){ |
|---|
| 113 | ((ive::Object*)(obj))->read(in);; |
|---|
| 114 | } |
|---|
| 115 | else |
|---|
| 116 | in_THROW_EXCEPTION("StateSet::read(): Could not cast this osg::StateSet to an osg::Object."); |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | char c = in->readChar(); |
|---|
| 120 | int num = in->readInt(); |
|---|
| 121 | std::string name = in->readString(); |
|---|
| 122 | switch((int)c){ |
|---|
| 123 | case 0: |
|---|
| 124 | setRenderBinDetails(num, name, osg::StateSet::INHERIT_RENDERBIN_DETAILS); |
|---|
| 125 | break; |
|---|
| 126 | case 1: |
|---|
| 127 | setRenderBinDetails(num, name, osg::StateSet::USE_RENDERBIN_DETAILS); |
|---|
| 128 | break; |
|---|
| 129 | case 2: |
|---|
| 130 | setRenderBinDetails(num, name, osg::StateSet::OVERRIDE_RENDERBIN_DETAILS); |
|---|
| 131 | break; |
|---|
| 132 | case 3: |
|---|
| 133 | setRenderBinDetails(num, name, osg::StateSet::USE_RENDERBIN_DETAILS); |
|---|
| 134 | break; |
|---|
| 135 | default: in_THROW_EXCEPTION("Unknown RenderBinMode in StateSet::read()"); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | int size = in->readInt(); |
|---|
| 140 | int i; |
|---|
| 141 | for(i=0;i<size;i++){ |
|---|
| 142 | int mode = in->readInt(); |
|---|
| 143 | int value = in->readInt(); |
|---|
| 144 | setMode((osg::StateAttribute::GLMode)mode, (osg::StateAttribute::GLModeValue)value); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | size = in->readInt(); |
|---|
| 149 | for(i=0;i<size;i++){ |
|---|
| 150 | osg::StateAttribute* attribute = in->readStateAttribute(); |
|---|
| 151 | setAttribute(attribute, (osg::StateAttribute::OverrideValue)in->readInt()); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | int nUnits = in->readInt(); |
|---|
| 156 | int unit; |
|---|
| 157 | for(unit=0;unit<nUnits;unit++){ |
|---|
| 158 | size = in->readInt(); |
|---|
| 159 | for(i=0;i<size;i++){ |
|---|
| 160 | int mode = in->readInt(); |
|---|
| 161 | int value = in->readInt(); |
|---|
| 162 | setTextureMode(unit,(osg::StateAttribute::GLMode)mode, (osg::StateAttribute::GLModeValue)value); |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | nUnits = in->readInt(); |
|---|
| 168 | for(unit=0;unit<nUnits;unit++) |
|---|
| 169 | { |
|---|
| 170 | size = in->readInt(); |
|---|
| 171 | for(i=0;i<size;i++) |
|---|
| 172 | { |
|---|
| 173 | osg::StateAttribute* attribute = in->readStateAttribute(); |
|---|
| 174 | setTextureAttribute(unit, attribute, (osg::StateAttribute::OverrideValue)in->readInt()); |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | if ( in->getVersion() >= VERSION_0010 ) |
|---|
| 179 | { |
|---|
| 180 | |
|---|
| 181 | size = in->readInt(); |
|---|
| 182 | for(i=0;i<size;i++) |
|---|
| 183 | { |
|---|
| 184 | osg::Uniform* uniform = in->readUniform(); |
|---|
| 185 | addUniform(uniform, (osg::StateAttribute::OverrideValue)in->readInt()); |
|---|
| 186 | } |
|---|
| 187 | } |
|---|
| 188 | } |
|---|
| 189 | else{ |
|---|
| 190 | in_THROW_EXCEPTION("StateSet::read(): Expected StateSet identification"); |
|---|
| 191 | } |
|---|
| 192 | } |
|---|