/********************************************************************** * * FILE: DataInputStream.cpp * * DESCRIPTION: Implements methods to read simpel datatypes from an * input stream. * * CREATED BY: Rune Schmidt Jensen * * HISTORY: Created 11.03.2003 * * Copyright 2003 VR-C **********************************************************************/ #include "DataInputStream.h" #include "StateSet.h" #include "BlendFunc.h" #include "Material.h" #include "CullFace.h" #include "PolygonOffset.h" #include "ShadeModel.h" #include "Point.h" #include "Texture2D.h" #include "TextureCubeMap.h" #include "TexEnv.h" #include "TexEnvCombine.h" #include "TexGen.h" #include "TexMat.h" #include "Group.h" #include "MatrixTransform.h" #include "Geode.h" #include "LightSource.h" #include "Billboard.h" #include "Sequence.h" #include "LOD.h" #include "PagedLOD.h" #include "PositionAttitudeTransform.h" #include "DOFTransform.h" #include "Transform.h" #include "Switch.h" #include "OccluderNode.h" #include "Impostor.h" #include "LightPointNode.h" #include "MultiSwitch.h" #include "Geometry.h" #include using namespace ive; using namespace std; DataInputStream::DataInputStream(std::istream* istream) { _verboseOutput = false; _istream = istream; _peeking = false; _peekValue = 0; if(!istream){ throw Exception("DataInputStream::DataInputStream(): null pointer exception in argument."); } _version = readInt(); // Are we trying to open a binary .ive file which version are newer than this library. if(_version>VERSION){ throw Exception("DataInputStream::DataInputStream(): The version found in the file is newer than this library can handle."); } } DataInputStream::~DataInputStream(){} bool DataInputStream::readBool(){ char c; _istream->read(&c, CHARSIZE); if (_istream->rdstate() & _istream->failbit) throw Exception("DataInputStream::readBool(): Failed to read boolean value."); if (_verboseOutput) std::cout<<"read/writeBool() ["<<(int)c<<"]"<read(&c, CHARSIZE); if (_istream->rdstate() & _istream->failbit) throw Exception("DataInputStream::readChar(): Failed to read char value."); if (_verboseOutput) std::cout<<"read/writeChar() ["<<(int)c<<"]"<read((char*)&c, CHARSIZE); if (_istream->rdstate() & _istream->failbit) throw Exception("DataInputStream::readUChar(): Failed to read unsigned char value."); if (_verboseOutput) std::cout<<"read/writeUChar() ["<<(int)c<<"]"<read((char*)&s, SHORTSIZE); if (_istream->rdstate() & _istream->failbit) throw Exception("DataInputStream::readUShort(): Failed to read unsigned short value."); if (_verboseOutput) std::cout<<"read/writeUShort() ["<read((char*)&s, INTSIZE); if (_istream->rdstate() & _istream->failbit) throw Exception("DataInputStream::readUInt(): Failed to read unsigned int value."); if (_verboseOutput) std::cout<<"read/writeUInt() ["<read((char*)&i, INTSIZE); // comment out for time being as this check seems to eroneously cause a // premature exit when reading .ive files under OSX!#?:! // Robet Osfield, September 12th 2003. // if (_istream->rdstate() & _istream->failbit) // throw Exception("DataInputStream::readInt(): Failed to read int value."); if (_verboseOutput) std::cout<<"read/writeInt() ["<read((char*)&f, FLOATSIZE); if (_istream->rdstate() & _istream->failbit) throw Exception("DataInputStream::readFloat(): Failed to read float value."); if (_verboseOutput) std::cout<<"read/writeFloat() ["<read((char*)&l, LONGSIZE); if (_istream->rdstate() & _istream->failbit) throw Exception("DataInputStream::readLong(): Failed to read long value."); if (_verboseOutput) std::cout<<"read/writeLong() ["<read((char*)&l, LONGSIZE); if (_istream->rdstate() & _istream->failbit) throw Exception("DataInputStream::readULong(): Failed to read unsigned long value."); if (_verboseOutput) std::cout<<"read/writeULong() ["<read((char*)&d, DOUBLESIZE); if (_istream->rdstate() & _istream->failbit) throw Exception("DataInputStream::readDouble(): Failed to read double value."); if (_verboseOutput) std::cout<<"read/writeDouble() ["<read((char*)s.c_str(), size); if (_istream->rdstate() & _istream->failbit) throw Exception("DataInputStream::readString(): Failed to read string value."); if (_verboseOutput) std::cout<<"read/writeString() ["<read(data, size); if (_istream->rdstate() & _istream->failbit) throw Exception("DataInputStream::readCharArray(): Failed to read char value."); if (_verboseOutput) std::cout<<"read/writeCharArray() ["<read((char*)&((*a)[0]), INTSIZE*size); if (_istream->rdstate() & _istream->failbit) throw Exception("DataInputStream::readIntArray(): Failed to read Int array."); if (_verboseOutput) std::cout<<"read/writeIntArray() ["<read((char*)&((*a)[0]), CHARSIZE*size); if (_istream->rdstate() & _istream->failbit) throw Exception("DataInputStream::readUByteArray(): Failed to read UByte array."); if (_verboseOutput) std::cout<<"read/writeUByteArray() ["<read((char*)&((*a)[0]), SHORTSIZE*size); if (_istream->rdstate() & _istream->failbit) throw Exception("DataInputStream::readUShortArray(): Failed to read UShort array."); if (_verboseOutput) std::cout<<"read/writeUShortArray() ["<read((char*)&((*a)[0]), INTSIZE*size); if (_istream->rdstate() & _istream->failbit) throw Exception("DataInputStream::readUIntArray(): Failed to read UInt array."); if (_verboseOutput) std::cout<<"read/writeUIntArray() ["<read((char*)&((*a)[0]), INTSIZE*size); if (_istream->rdstate() & _istream->failbit) throw Exception("DataInputStream::readUbyte4Array(): Failed to read UByte4 array."); if (_verboseOutput) std::cout<<"read/writeUByte4Array() ["<read((char*)&((*a)[0]), FLOATSIZE*size); if (_istream->rdstate() & _istream->failbit) throw Exception("DataInputStream::readFloatArray(): Failed to read float array."); if (_verboseOutput) std::cout<<"read/writeFloatArray() ["<read((char*)&((*a)[0]), FLOATSIZE*2*size); if (_istream->rdstate() & _istream->failbit) throw Exception("DataInputStream::readVec2Array(): Failed to read Vec2 array."); if (_verboseOutput) std::cout<<"read/writeVec2Array() ["<read((char*)&((*a)[0]), FLOATSIZE*3*size); if (_istream->rdstate() & _istream->failbit) throw Exception("DataInputStream::readVec3Array(): Failed to read Vec3 array."); if (_verboseOutput) std::cout<<"read/writeVec3Array() ["<read((char*)&((*a)[0]), FLOATSIZE*4*size); if (_istream->rdstate() & _istream->failbit) throw Exception("DataInputStream::readVec4Array(): Failed to read Vec4 array."); if (_verboseOutput) std::cout<<"read/writeVec4Array() ["<rdstate() & _istream->failbit) throw Exception("DataInputStream::readMatrix(): Failed to read Matrix array."); if (_verboseOutput) std::cout<<"read/writeMatrix() ["<second.get(); // Image is not in list. // Read it from disk, osg::Image* image = osgDB::readImageFile(filename.c_str()); // add it to the imageList, _imageMap[filename] = image; // and return image pointer. if (_verboseOutput) std::cout<<"read/writeImage() ["<second.get(); // StateSet is not in list. // Create a new stateset, osg::StateSet* stateset = new osg::StateSet(); // read its properties from stream ((ive::StateSet*)(stateset))->read(this); // and add it to the stateset map, _statesetMap[id] = stateset; if (_verboseOutput) std::cout<<"read/writeStateSet() ["<second.get(); // stateattribute is not in list. // Create a new stateattribute, osg::StateAttribute* attribute; int attributeID = peekInt(); if(attributeID == IVEBLENDFUNC){ attribute = new osg::BlendFunc(); ((ive::BlendFunc*)(attribute))->read(this); } else if(attributeID == IVEMATERIAL){ attribute = new osg::Material(); ((ive::Material*)(attribute))->read(this); } else if(attributeID == IVECULLFACE){ attribute = new osg::CullFace(); ((ive::CullFace*)(attribute))->read(this); } else if(attributeID == IVEPOLYGONOFFSET){ attribute = new osg::PolygonOffset(); ((ive::PolygonOffset*)(attribute))->read(this); } else if(attributeID == IVESHADEMODEL){ attribute = new osg::ShadeModel(); ((ive::ShadeModel*)(attribute))->read(this); } else if(attributeID == IVEPOINT){ attribute = new osg::Point(); ((ive::Point*)(attribute))->read(this); } else if(attributeID == IVETEXTURE2D){ attribute = new osg::Texture2D(); ((ive::Texture2D*)(attribute))->read(this); } else if(attributeID == IVETEXTURECUBEMAP){ attribute = new osg::TextureCubeMap(); ((ive::TextureCubeMap*)(attribute))->read(this); } else if(attributeID == IVETEXENV){ attribute = new osg::TexEnv(); ((ive::TexEnv*)(attribute))->read(this); } else if(attributeID == IVETEXENVCOMBINE){ attribute = new osg::TexEnvCombine(); ((ive::TexEnvCombine*)(attribute))->read(this); } else if(attributeID == IVETEXGEN){ attribute = new osg::TexGen(); ((ive::TexGen*)(attribute))->read(this); } else if(attributeID == IVETEXMAT){ attribute = new osg::TexMat(); ((ive::TexMat*)(attribute))->read(this); } else{ throw Exception("Unkown StateAttribute in StateSet::read()"); } // and add it to the stateattribute map, _stateAttributeMap[id] = attribute; if (_verboseOutput) std::cout<<"read/writeStateAttribute() ["<second.get(); // stateattribute is not in list. // Create a new stateattribute, int drawableTypeID = peekInt(); osg::Drawable* drawable; if(drawableTypeID == IVEGEOMETRY){ drawable = new osg::Geometry(); ((Geometry*)(drawable))->read(this); } else throw Exception("Unknown drawable drawableTypeIDentification in Geode::read()"); // and add it to the stateattribute map, _drawableMap[id] = drawable; if (_verboseOutput) std::cout<<"read/writeDrawable() ["<second.get(); // stateattribute is not in list. // Create a new node, osg::Node* node; int nodeTypeID= peekInt(); if(nodeTypeID== IVEMATRIXTRANSFORM){ node = new osg::MatrixTransform(); ((ive::MatrixTransform*)(node))->read(this); } else if(nodeTypeID== IVEPOSITIONATTITUDETRANSFORM){ node = new osg::PositionAttitudeTransform(); ((ive::PositionAttitudeTransform*)(node))->read(this); } else if(nodeTypeID== IVEDOFTRANSFORM){ node = new osgSim::DOFTransform(); ((ive::DOFTransform*)(node))->read(this); } else if(nodeTypeID== IVETRANSFORM){ node = new osg::Transform(); ((ive::Transform*)(node))->read(this); } else if(nodeTypeID== IVELIGHTSOURCE){ node = new osg::LightSource(); ((ive::LightSource*)(node))->read(this); } else if(nodeTypeID== IVESEQUENCE){ node = new osg::Sequence(); ((ive::Sequence*)(node))->read(this); } else if(nodeTypeID== IVELOD){ node = new osg::LOD(); ((ive::LOD*)(node))->read(this); } else if(nodeTypeID== IVEPAGEDLOD){ node = new osg::PagedLOD(); ((ive::PagedLOD*)(node))->read(this); } else if(nodeTypeID== IVESWITCH){ node = new osg::Switch(); ((ive::Switch*)(node))->read(this); } else if(nodeTypeID== IVEMULTISWITCH){ node = new osgSim::MultiSwitch(); ((ive::MultiSwitch*)(node))->read(this); } else if(nodeTypeID== IVEIMPOSTOR){ node = new osg::Impostor(); ((ive::Impostor*)(node))->read(this); } else if(nodeTypeID== IVEOCCLUDERNODE){ node = new osg::OccluderNode(); ((ive::OccluderNode*)(node))->read(this); } else if(nodeTypeID== IVEGROUP){ node = new osg::Group(); ((ive::Group*)(node))->read(this); } else if(nodeTypeID== IVEBILLBOARD){ node = new osg::Billboard(); ((ive::Billboard*)(node))->read(this); } else if(nodeTypeID== IVEGEODE){ node = new osg::Geode(); ((ive::Geode*)(node))->read(this); } else if(nodeTypeID== IVELIGHTPOINTNODE){ node = new osgSim::LightPointNode(); ((ive::LightPointNode*)(node))->read(this); } else{ throw Exception("Unknown node identification in DataInputStream::readNode()"); } // and add it to the node map, _nodeMap[id] = node; if (_verboseOutput) std::cout<<"read/writeNode() ["<