| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include "Exception.h" |
|---|
| 16 | #include "Geometry.h" |
|---|
| 17 | #include "Drawable.h" |
|---|
| 18 | #include "DrawArrays.h" |
|---|
| 19 | #include "DrawArrayLengths.h" |
|---|
| 20 | #include "DrawElementsUByte.h" |
|---|
| 21 | #include "DrawElementsUShort.h" |
|---|
| 22 | #include "DrawElementsUInt.h" |
|---|
| 23 | |
|---|
| 24 | using namespace ive; |
|---|
| 25 | |
|---|
| 26 | void Geometry::write(DataOutputStream* out){ |
|---|
| 27 | |
|---|
| 28 | out->writeInt(IVEGEOMETRY); |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | osg::Drawable* drawable = dynamic_cast<osg::Drawable*>(this); |
|---|
| 32 | if(drawable){ |
|---|
| 33 | ((ive::Drawable*)(drawable))->write(out); |
|---|
| 34 | } |
|---|
| 35 | else |
|---|
| 36 | out_THROW_EXCEPTION("Geometry::write(): Could not cast this osg::Geometry to an osg::Drawable."); |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | int size = getNumPrimitiveSets(); |
|---|
| 43 | out->writeInt(size); |
|---|
| 44 | for(int i=0;i<size;i++){ |
|---|
| 45 | if(dynamic_cast<osg::DrawArrays*>(getPrimitiveSet(i))) |
|---|
| 46 | ((ive::DrawArrays*)(getPrimitiveSet(i)))->write(out); |
|---|
| 47 | else if(dynamic_cast<osg::DrawArrayLengths*>(getPrimitiveSet(i))) |
|---|
| 48 | ((ive::DrawArrayLengths*)(getPrimitiveSet(i)))->write(out); |
|---|
| 49 | else if(dynamic_cast<osg::DrawElementsUByte*>(getPrimitiveSet(i))) |
|---|
| 50 | ((ive::DrawElementsUByte*)(getPrimitiveSet(i)))->write(out); |
|---|
| 51 | else if(dynamic_cast<osg::DrawElementsUShort*>(getPrimitiveSet(i))) |
|---|
| 52 | ((ive::DrawElementsUShort*)(getPrimitiveSet(i)))->write(out); |
|---|
| 53 | else if(dynamic_cast<osg::DrawElementsUInt*>(getPrimitiveSet(i))) |
|---|
| 54 | ((ive::DrawElementsUInt*)(getPrimitiveSet(i)))->write(out); |
|---|
| 55 | else |
|---|
| 56 | out_THROW_EXCEPTION("Unknown PrimitivSet in Geometry::write()"); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | out->writeBool(getVertexArray()!=0); |
|---|
| 61 | if (getVertexArray()) |
|---|
| 62 | { |
|---|
| 63 | out->writeArray(getVertexArray()); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | out->writeBool(getVertexIndices()!=0); |
|---|
| 67 | if (getVertexIndices()) |
|---|
| 68 | { |
|---|
| 69 | out->writeArray(getVertexIndices()); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | if ( out->getVersion() < VERSION_0013 ) |
|---|
| 74 | { |
|---|
| 75 | osg::Vec3Array* normals = dynamic_cast<osg::Vec3Array*>(getNormalArray()); |
|---|
| 76 | out->writeBool(normals!=0); |
|---|
| 77 | if (normals) |
|---|
| 78 | { |
|---|
| 79 | out->writeBinding(getNormalBinding()); |
|---|
| 80 | out->writeVec3Array(normals); |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | else |
|---|
| 84 | { |
|---|
| 85 | out->writeBool(getNormalArray()!=0); |
|---|
| 86 | if (getNormalArray()!=0) |
|---|
| 87 | { |
|---|
| 88 | out->writeBinding(getNormalBinding()); |
|---|
| 89 | out->writeArray(getNormalArray()); |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | out->writeBool(getNormalIndices()!=0); |
|---|
| 95 | if (getNormalIndices()){ |
|---|
| 96 | out->writeArray(getNormalIndices()); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | out->writeBool(getColorArray()!=0); |
|---|
| 100 | if (getColorArray()){ |
|---|
| 101 | out->writeBinding(getColorBinding()); |
|---|
| 102 | out->writeArray(getColorArray()); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | out->writeBool(getColorIndices()!=0); |
|---|
| 106 | if (getColorIndices()){ |
|---|
| 107 | out->writeArray(getColorIndices()); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | out->writeBool(getSecondaryColorArray()!=0); |
|---|
| 111 | if (getSecondaryColorArray()){ |
|---|
| 112 | out->writeBinding(getSecondaryColorBinding()); |
|---|
| 113 | out->writeArray(getSecondaryColorArray()); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | out->writeBool(getSecondaryColorIndices()!=0); |
|---|
| 117 | if (getSecondaryColorIndices()){ |
|---|
| 118 | out->writeArray(getSecondaryColorIndices()); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | out->writeBool(getFogCoordArray()!=0); |
|---|
| 122 | if (getFogCoordArray()){ |
|---|
| 123 | out->writeBinding(getFogCoordBinding()); |
|---|
| 124 | out->writeArray(getFogCoordArray()); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | out->writeBool(getFogCoordIndices()!=0); |
|---|
| 128 | if (getFogCoordIndices()){ |
|---|
| 129 | out->writeArray(getFogCoordIndices()); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | Geometry::ArrayDataList& tcal = getTexCoordArrayList(); |
|---|
| 133 | out->writeInt(tcal.size()); |
|---|
| 134 | unsigned int j; |
|---|
| 135 | for(j=0;j<tcal.size();j++) |
|---|
| 136 | { |
|---|
| 137 | |
|---|
| 138 | out->writeBool(tcal[j].array.valid()); |
|---|
| 139 | if (tcal[j].array.valid()){ |
|---|
| 140 | out->writeArray(tcal[j].array.get()); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | out->writeBool(tcal[j].indices.valid()); |
|---|
| 144 | if (tcal[j].indices.valid()){ |
|---|
| 145 | out->writeArray(tcal[j].indices.get()); |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | Geometry::ArrayDataList& vaal = getVertexAttribArrayList(); |
|---|
| 151 | out->writeInt(vaal.size()); |
|---|
| 152 | for(j=0;j<vaal.size();j++) |
|---|
| 153 | { |
|---|
| 154 | |
|---|
| 155 | const osg::Geometry::ArrayData& arrayData = vaal[j]; |
|---|
| 156 | out->writeBinding(arrayData.binding); |
|---|
| 157 | out->writeBool(arrayData.normalize==GL_TRUE); |
|---|
| 158 | out->writeBool(arrayData.array.valid()); |
|---|
| 159 | if (arrayData.array.valid()){ |
|---|
| 160 | out->writeArray(arrayData.array.get()); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | out->writeBool(arrayData.indices.valid()); |
|---|
| 164 | if (arrayData.indices.valid()){ |
|---|
| 165 | out->writeArray(arrayData.indices.get()); |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | void Geometry::read(DataInputStream* in){ |
|---|
| 171 | |
|---|
| 172 | int id = in->peekInt(); |
|---|
| 173 | if(id == IVEGEOMETRY){ |
|---|
| 174 | |
|---|
| 175 | id = in->readInt(); |
|---|
| 176 | |
|---|
| 177 | osg::Drawable* drawable = dynamic_cast<osg::Drawable*>(this); |
|---|
| 178 | if(drawable){ |
|---|
| 179 | ((ive::Drawable*)(drawable))->read(in); |
|---|
| 180 | } |
|---|
| 181 | else |
|---|
| 182 | in_THROW_EXCEPTION("Geometry::read(): Could not cast this osg::Geometry to an osg::Drawable."); |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | int size = in->readInt(); |
|---|
| 189 | int i; |
|---|
| 190 | for(i=0;i<size;i++){ |
|---|
| 191 | osg::PrimitiveSet* prim; |
|---|
| 192 | int primID = in->peekInt(); |
|---|
| 193 | if(primID==IVEDRAWARRAYS){ |
|---|
| 194 | prim = new osg::DrawArrays(); |
|---|
| 195 | ((ive::DrawArrays*)(prim))->read(in); |
|---|
| 196 | addPrimitiveSet(prim); |
|---|
| 197 | } |
|---|
| 198 | else if(primID==IVEDRAWARRAYLENGTHS){ |
|---|
| 199 | prim = new osg::DrawArrayLengths(); |
|---|
| 200 | ((ive::DrawArrayLengths*)(prim))->read(in); |
|---|
| 201 | addPrimitiveSet(prim); |
|---|
| 202 | } |
|---|
| 203 | else if(primID==IVEDRAWELEMENTSUBYTE){ |
|---|
| 204 | prim = new osg::DrawElementsUByte(); |
|---|
| 205 | ((ive::DrawElementsUByte*)(prim))->read(in); |
|---|
| 206 | addPrimitiveSet(prim); |
|---|
| 207 | } |
|---|
| 208 | else if(primID==IVEDRAWELEMENTSUSHORT){ |
|---|
| 209 | prim = new osg::DrawElementsUShort(); |
|---|
| 210 | ((ive::DrawElementsUShort*)(prim))->read(in); |
|---|
| 211 | addPrimitiveSet(prim); |
|---|
| 212 | } |
|---|
| 213 | else if(primID==IVEDRAWELEMENTSUINT){ |
|---|
| 214 | prim = new osg::DrawElementsUInt(); |
|---|
| 215 | ((ive::DrawElementsUInt*)(prim))->read(in); |
|---|
| 216 | addPrimitiveSet(prim); |
|---|
| 217 | } |
|---|
| 218 | else{ |
|---|
| 219 | in_THROW_EXCEPTION("Unknown PrimitiveSet in Geometry::read()"); |
|---|
| 220 | } |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | bool va=in->readBool(); |
|---|
| 225 | if (va){ |
|---|
| 226 | setVertexArray(in->readArray()); |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | bool vi = in->readBool(); |
|---|
| 230 | if (vi){ |
|---|
| 231 | setVertexIndices(static_cast<osg::IndexArray*>(in->readArray())); |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | if ( in->getVersion() < VERSION_0013 ) |
|---|
| 236 | { |
|---|
| 237 | bool na =in->readBool(); |
|---|
| 238 | if(na){ |
|---|
| 239 | setNormalBinding(in->readBinding()); |
|---|
| 240 | setNormalArray(in->readVec3Array()); |
|---|
| 241 | } |
|---|
| 242 | } |
|---|
| 243 | else |
|---|
| 244 | { |
|---|
| 245 | bool na =in->readBool(); |
|---|
| 246 | if(na){ |
|---|
| 247 | setNormalBinding(in->readBinding()); |
|---|
| 248 | setNormalArray(in->readArray()); |
|---|
| 249 | } |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | bool ni = in->readBool(); |
|---|
| 254 | if(ni){ |
|---|
| 255 | setNormalIndices(static_cast<osg::IndexArray*>(in->readArray())); |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | if(in->readBool()){ |
|---|
| 259 | setColorBinding(in->readBinding()); |
|---|
| 260 | setColorArray(in->readArray()); |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | if(in->readBool()){ |
|---|
| 264 | setColorIndices(static_cast<osg::IndexArray*>(in->readArray())); |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | if(in->readBool()){ |
|---|
| 268 | setSecondaryColorBinding(in->readBinding()); |
|---|
| 269 | setSecondaryColorArray(in->readArray()); |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | if(in->readBool()){ |
|---|
| 273 | setSecondaryColorIndices(static_cast<osg::IndexArray*>(in->readArray())); |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | if(in->readBool()){ |
|---|
| 277 | setFogCoordBinding(in->readBinding()); |
|---|
| 278 | setFogCoordArray(in->readArray()); |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | if(in->readBool()){ |
|---|
| 282 | setFogCoordIndices(static_cast<osg::IndexArray*>(in->readArray())); |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | size = in->readInt(); |
|---|
| 286 | for(i =0;i<size;i++) |
|---|
| 287 | { |
|---|
| 288 | |
|---|
| 289 | bool coords_valid = in->readBool(); |
|---|
| 290 | if(coords_valid) |
|---|
| 291 | setTexCoordArray(i, in->readArray()); |
|---|
| 292 | |
|---|
| 293 | bool indices_valid = in->readBool(); |
|---|
| 294 | if(indices_valid) |
|---|
| 295 | setTexCoordIndices(i, static_cast<osg::IndexArray*>(in->readArray())); |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | |
|---|
| 299 | size = in->readInt(); |
|---|
| 300 | for(i =0;i<size;i++) |
|---|
| 301 | { |
|---|
| 302 | setVertexAttribBinding(i,in->readBinding()); |
|---|
| 303 | setVertexAttribNormalize(i,in->readBool()?GL_TRUE:GL_FALSE); |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | bool coords_valid = in->readBool(); |
|---|
| 307 | if(coords_valid) |
|---|
| 308 | setVertexAttribArray(i, in->readArray()); |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | bool indices_valid = in->readBool(); |
|---|
| 312 | if(indices_valid) |
|---|
| 313 | setVertexAttribIndices(i, static_cast<osg::IndexArray*>(in->readArray())); |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | } |
|---|
| 317 | else{ |
|---|
| 318 | in_THROW_EXCEPTION("Geometry::read(): Expected Geometry identification."); |
|---|
| 319 | } |
|---|
| 320 | } |
|---|