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