From ba3488046465c443f35398afbc8acab446914583 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 8 Jan 2003 14:32:13 +0000 Subject: [PATCH] Added support for reading and writing Sphere, Box, Cone, Cylinder and Grid shapes. --- AUTHORS | 1 - VisualStudio/osgDB/osgDB.dsp | 4 + VisualStudio/osgPlugins/osg/dot_osg.dsp | 4 + include/osg/Shape | 4 +- include/osgDB/FieldReaderIterator | 20 +++ include/osgDB/Output | 30 +--- include/osgDB/ParameterOutput | 173 ++++++++++++++++++++++++ src/osg/ShapeDrawable.cpp | 8 ++ src/osgDB/FieldReaderIterator.cpp | 161 ++++++++++++++++++++++ src/osgPlugins/osg/Drawable.cpp | 18 +++ src/osgPlugins/osg/GeoSet.cpp | 8 +- src/osgPlugins/osg/Geometry.cpp | 100 +++----------- src/osgPlugins/osg/Makefile | 1 + 13 files changed, 413 insertions(+), 119 deletions(-) create mode 100644 include/osgDB/ParameterOutput diff --git a/AUTHORS b/AUTHORS index c8946d9a1..7f6ffb2f8 100644 --- a/AUTHORS +++ b/AUTHORS @@ -123,7 +123,6 @@ Ruben Lopez Pavel Moloshtan - Support for LWO2 format in the LightWave loader. - Daniel Sjölie - Support for multitextured flt files. diff --git a/VisualStudio/osgDB/osgDB.dsp b/VisualStudio/osgDB/osgDB.dsp index 50b6b0d1d..ec0bd47e6 100755 --- a/VisualStudio/osgDB/osgDB.dsp +++ b/VisualStudio/osgDB/osgDB.dsp @@ -189,6 +189,10 @@ SOURCE=..\..\Include\osgDB\Output # End Source File # Begin Source File +SOURCE=..\..\Include\osgDB\ParameterOutput +# End Source File +# Begin Source File + SOURCE=..\..\include\osgDB\ReaderWriter # End Source File # Begin Source File diff --git a/VisualStudio/osgPlugins/osg/dot_osg.dsp b/VisualStudio/osgPlugins/osg/dot_osg.dsp index fdcb5e6c1..582d51cc5 100755 --- a/VisualStudio/osgPlugins/osg/dot_osg.dsp +++ b/VisualStudio/osgPlugins/osg/dot_osg.dsp @@ -246,6 +246,10 @@ SOURCE=..\..\..\src\osgPlugins\osg\ShadeModel.cpp # End Source File # Begin Source File +SOURCE=..\..\..\src\osgPlugins\osg\Shape.cpp +# End Source File +# Begin Source File + SOURCE=..\..\..\src\osgPlugins\osg\ShapeDrawable.cpp # End Source File # Begin Source File diff --git a/include/osg/Shape b/include/osg/Shape index d951bd486..6d19fa5ae 100644 --- a/include/osg/Shape +++ b/include/osg/Shape @@ -287,8 +287,8 @@ class Cylinder : public Shape Cylinder(): _center(0.0f,0.0f,0.0f), - _radius(-1.0f), - _height(0.0f) {} + _radius(1.0f), + _height(1.0f) {} Cylinder(const osg::Vec3& center,float radius,float height): _center(center), diff --git a/include/osgDB/FieldReaderIterator b/include/osgDB/FieldReaderIterator index 668227685..aecf5fd7d 100644 --- a/include/osgDB/FieldReaderIterator +++ b/include/osgDB/FieldReaderIterator @@ -5,6 +5,10 @@ #ifndef OSGDB_FIELDREADERITERATOR #define OSGDB_FIELDREADERITERATOR 1 +#include +#include +#include + #include #include @@ -48,6 +52,22 @@ class OSGDB_EXPORT FieldReaderIterator void advanceToEndOfBlock(int noNestBrackets); bool matchSequence(const char* str); + + bool readSequence(const char* keyword,std::string& value); + bool readSequence(const char* keyword,unsigned int& value); + bool readSequence(const char* keyword,int& value); + bool readSequence(const char* keyword,float& value); + bool readSequence(const char* keyword,osg::Vec2& value); + bool readSequence(const char* keyword,osg::Vec3& value); + bool readSequence(const char* keyword,osg::Vec4& value); + + bool readSequence(std::string& value); + bool readSequence(unsigned int& value); + bool readSequence(int& value); + bool readSequence(float& value); + bool readSequence(osg::Vec2& value); + bool readSequence(osg::Vec3& value); + bool readSequence(osg::Vec4& value); private: diff --git a/include/osgDB/Output b/include/osgDB/Output index 0c43d22bc..1d025f3d7 100644 --- a/include/osgDB/Output +++ b/include/osgDB/Output @@ -50,6 +50,7 @@ class OSGDB_EXPORT Output : public std::ofstream void moveOut(); virtual bool writeObject(const osg::Object& obj); + bool getUniqueIDForObject(const osg::Object* obj,std::string& uniqueID); bool createUniqueIDForObject(const osg::Object* obj,std::string& uniqueID); @@ -68,6 +69,7 @@ class OSGDB_EXPORT Output : public std::ofstream virtual const std::string getFileNameForOutput(const std::string& filename) const; + protected: // prevent copy construction and assignment. @@ -90,34 +92,6 @@ class OSGDB_EXPORT Output : public std::ofstream }; -template -bool writeArrayBlock(Output& fw,T* start,T* finish) -{ - fw.indent() << "{" << std::endl; - fw.moveIn(); - int numIndicesThisLine = 0; - for(T* itr=start;itr!=finish;++itr) - { - if (numIndicesThisLine>=fw.getNumIndicesPerLine()) - { - fw << std::endl; - numIndicesThisLine = 0; - } - - if (numIndicesThisLine==0) fw.indent(); - else fw << " "; - - fw << *itr; - - ++numIndicesThisLine; - - } - fw << std::endl; - fw.moveOut(); - fw.indent() << "}" << std::endl; - return true; -} - } #endif // __SG_OUTPUT_H diff --git a/include/osgDB/ParameterOutput b/include/osgDB/ParameterOutput new file mode 100644 index 000000000..243463ff4 --- /dev/null +++ b/include/osgDB/ParameterOutput @@ -0,0 +1,173 @@ +//C++ header - Open Scene Graph - Copyright (C) 1998-2002 Robert Osfield +//Distributed under the terms of the GNU Library General Public License (LGPL) +//as published by the Free Software Foundation. + +#ifndef OSGDB_PARAMETEROUTPUT +#define OSGDB_PARAMETEROUTPUT 1 + +#include + +namespace osgDB { + +class OSGDB_EXPORT ParameterOutput +{ + public: + + ParameterOutput(Output& fw): + _fw(fw), + _numItemsPerLine(fw.getNumIndicesPerLine()), + _column(0) {} + + ParameterOutput(Output& fw,int numItemsPerLine): + _fw(fw), + _numItemsPerLine(numItemsPerLine), + _column(0) {} + + void begin() + { + _fw.indent() << "{"< + void write(const T& t) + { + if (_column==0) _fw.indent(); + + _fw << t; + + ++_column; + if (_column==_numItemsPerLine) + { + _fw << std::endl; + _column = 0; + } + else + { + _fw << " "; + } + + } + + template + void write(Iterator first, Iterator last) + { + for(Iterator itr=first; + itr!=last; + ++itr) + { + write(*itr); + } + + } + + template + void writeAsInts(Iterator first, Iterator last) + { + for(Iterator itr=first; + itr!=last; + ++itr) + { + write((int)*itr); + } + + } + + void newLine() + { + if (_column!=0) _fw << std::endl; + _column = 0; + } + + void end() + { + if (_column!=0) _fw << std::endl; + _fw.moveOut(); + _fw.indent() << "}"< +void writeArray(Output& fw, Iterator first, Iterator last,int noItemsPerLine=0) +{ + if (noItemsPerLine==0) noItemsPerLine=fw.getNumIndicesPerLine(); + + fw.indent() << "{"< +void writeArrayAsInts(Output& fw, Iterator first, Iterator last,int noItemsPerLine=0) +{ + if (noItemsPerLine==0) noItemsPerLine=fw.getNumIndicesPerLine(); + + fw.indent() << "{"<zMax) + { + // no valid entries so don't reset the bounding box + return; + } + if (field.zeroRotation()) { diff --git a/src/osgDB/FieldReaderIterator.cpp b/src/osgDB/FieldReaderIterator.cpp index a20782899..62aff97e5 100644 --- a/src/osgDB/FieldReaderIterator.cpp +++ b/src/osgDB/FieldReaderIterator.cpp @@ -377,3 +377,164 @@ bool FieldReaderIterator::matchSequence(const char* str) } return true; } + + +bool FieldReaderIterator::readSequence(const char* keyword,std::string& value) +{ + if ((*this)[0].matchWord(keyword) && (*this)[1].isString()) + { + value = (*this)[1].getStr(); + (*this)+=2; + return true; + } + return false; +} + +bool FieldReaderIterator::readSequence(const char* keyword,unsigned int& value) +{ + if ((*this)[0].matchWord(keyword) && (*this)[1].getUInt(value)) + { + (*this)+=2; + return true; + } + return false; +} + +bool FieldReaderIterator::readSequence(const char* keyword,int& value) +{ + if ((*this)[0].matchWord(keyword) && (*this)[1].getInt(value)) + { + (*this)+=2; + return true; + } + return false; +} + +bool FieldReaderIterator::readSequence(const char* keyword,float& value) +{ + if ((*this)[0].matchWord(keyword) && + (*this)[1].getFloat(value)) + { + (*this)+=2; + return true; + } + return false; +} + +bool FieldReaderIterator::readSequence(const char* keyword,osg::Vec2& value) +{ + if ((*this)[0].matchWord(keyword) && + (*this)[1].getFloat(value[0]) && + (*this)[2].getFloat(value[1])) + { + (*this)+=3; + return true; + } + return false; +} + +bool FieldReaderIterator::readSequence(const char* keyword,osg::Vec3& value) +{ + if ((*this)[0].matchWord(keyword) && + (*this)[1].getFloat(value[0]) && + (*this)[2].getFloat(value[1]) && + (*this)[3].getFloat(value[2])) + { + (*this)+=4; + return true; + } + return false; +} + +bool FieldReaderIterator::readSequence(const char* keyword,osg::Vec4& value) +{ + if ((*this)[0].matchWord(keyword) && + (*this)[1].getFloat(value[0]) && + (*this)[2].getFloat(value[1]) && + (*this)[3].getFloat(value[2]) && + (*this)[4].getFloat(value[3])) + { + (*this)+=5; + return true; + } + return false; +} + +bool FieldReaderIterator::readSequence(std::string& value) +{ + if ((*this)[0].isString()) + { + value = (*this)[0].getStr(); + (*this)+=1; + return true; + } + return false; +} + +bool FieldReaderIterator::readSequence(unsigned int& value) +{ + if ((*this)[0].getUInt(value)) + { + (*this)+=1; + return true; + } + return false; +} + +bool FieldReaderIterator::readSequence(int& value) +{ + if ((*this)[0].getInt(value)) + { + (*this)+=1; + return true; + } + return false; +} + +bool FieldReaderIterator::readSequence(float& value) +{ + if ((*this)[0].getFloat(value)) + { + (*this)+=1; + return true; + } + return false; +} + +bool FieldReaderIterator::readSequence(osg::Vec2& value) +{ + if ((*this)[0].getFloat(value[0]) && + (*this)[1].getFloat(value[1])) + { + (*this)+=2; + return true; + } + return false; +} + +bool FieldReaderIterator::readSequence(osg::Vec3& value) +{ + if ((*this)[0].getFloat(value[0]) && + (*this)[1].getFloat(value[1]) && + (*this)[2].getFloat(value[2])) + { + (*this)+=3; + return true; + } + return false; +} + +bool FieldReaderIterator::readSequence(osg::Vec4& value) +{ + if ((*this)[0].getFloat(value[0]) && + (*this)[1].getFloat(value[1]) && + (*this)[2].getFloat(value[2]) && + (*this)[3].getFloat(value[3])) + { + (*this)+=4; + return true; + } + return false; +} + + diff --git a/src/osgPlugins/osg/Drawable.cpp b/src/osgPlugins/osg/Drawable.cpp index 947967d98..4a621d6f8 100644 --- a/src/osgPlugins/osg/Drawable.cpp +++ b/src/osgPlugins/osg/Drawable.cpp @@ -1,4 +1,5 @@ #include "osg/Drawable" +#include "osg/Notify" #include "osgDB/Registry" #include "osgDB/Input" @@ -34,6 +35,16 @@ bool Drawable_readLocalData(Object& obj, Input& fr) iteratorAdvanced = true; } + ref_ptr readObject = fr.readObject(); + if (readObject.valid()) + { + osg::Shape* shape = dynamic_cast(readObject.get()); + if (shape) drawable.setShape(shape); + else notify(WARN)<<"Warning:: "<className()<<" loaded but cannot not be attached to Drawable."<(geoset); @@ -1010,13 +1010,13 @@ bool GeoSet_writeIndexData(Output& fw, const char* IndexName,const GeoSet::Index { // write our CoordIndex fw.indent() << IndexName << " ushort " << ip._size<< std::endl; - writeArrayBlock(fw,ip._ptr._ushort,ip._ptr._ushort+ip._size); + writeArray(fw,ip._ptr._ushort,ip._ptr._ushort+ip._size); } else { // write our CoordIndex fw.indent() << IndexName << " uint " << ip._size<< std::endl; - writeArrayBlock(fw,ip._ptr._uint,ip._ptr._uint+ip._size); + writeArray(fw,ip._ptr._uint,ip._ptr._uint+ip._size); } return true; } diff --git a/src/osgPlugins/osg/Geometry.cpp b/src/osgPlugins/osg/Geometry.cpp index f9e4c1769..f19b523c1 100644 --- a/src/osgPlugins/osg/Geometry.cpp +++ b/src/osgPlugins/osg/Geometry.cpp @@ -3,7 +3,7 @@ #include #include -#include +#include using namespace osg; using namespace osgDB; @@ -557,74 +557,6 @@ Array* Array_readLocalData(Input& fr) } -template -void Array_writeLocalData(Output& fw, Iterator first, Iterator last,int noItemsPerLine=8) -{ - fw.indent() << "{"< -void Array_writeLocalDataAsInts(Output& fw, Iterator first, Iterator last,int noItemsPerLine=8) -{ - fw.indent() << "{"<1) @@ -651,7 +583,7 @@ bool Array_writeLocalData(const Array& array,Output& fw) { const ByteArray& carray = static_cast(array); fw<(array); fw<(array); fw<(array); fw<(array); fw<(array); fw<(array); fw<(array); fw<(array); fw<(array); fw<(array); fw<(prim); fw<(prim); fw<(prim); fw<(prim); fw<