From dd3db860a2a36d4d926f80538d8b4f590c8863ec Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 8 Apr 2005 13:06:14 +0000 Subject: [PATCH] From Carlo Camporesi, add support for text into .ive plugin, tweak by Robert Osfield to allow font names to be exported correctly with extension --- VisualStudio/osgPlugins/ive/ive.dsp | 8 + src/osgPlugins/ive/DataInputStream.cpp | 19 ++- src/osgPlugins/ive/DataInputStream.h | 132 ++++++++--------- src/osgPlugins/ive/DataOutputStream.cpp | 17 ++- src/osgPlugins/ive/GNUmakefile | 1 + src/osgPlugins/ive/ReadWrite.h | 5 +- src/osgPlugins/ive/Text.cpp | 187 ++++++++++++++++++++++++ src/osgPlugins/ive/Text.h | 15 ++ src/osgPlugins/osgText/IO_Text.cpp | 2 +- 9 files changed, 310 insertions(+), 76 deletions(-) create mode 100644 src/osgPlugins/ive/Text.cpp create mode 100644 src/osgPlugins/ive/Text.h diff --git a/VisualStudio/osgPlugins/ive/ive.dsp b/VisualStudio/osgPlugins/ive/ive.dsp index 61925c18e..33d8ea0d8 100755 --- a/VisualStudio/osgPlugins/ive/ive.dsp +++ b/VisualStudio/osgPlugins/ive/ive.dsp @@ -368,6 +368,10 @@ SOURCE=..\..\..\src\osgPlugins\ive\TexMat.cpp # End Source File # Begin Source File +SOURCE=..\..\..\src\osgPlugins\ive\Text.cpp +# End Source File +# Begin Source File + SOURCE=..\..\..\src\osgPlugins\ive\Texture.cpp # End Source File # Begin Source File @@ -680,6 +684,10 @@ SOURCE=..\..\..\src\osgPlugins\ive\TexMat.h # End Source File # Begin Source File +SOURCE=..\..\..\src\osgPlugins\ive\Text.h +# End Source File +# Begin Source File + SOURCE=..\..\..\src\osgPlugins\ive\Texture.h # End Source File # Begin Source File diff --git a/src/osgPlugins/ive/DataInputStream.cpp b/src/osgPlugins/ive/DataInputStream.cpp index 940b7147b..4a1f4bcfa 100644 --- a/src/osgPlugins/ive/DataInputStream.cpp +++ b/src/osgPlugins/ive/DataInputStream.cpp @@ -9,7 +9,7 @@ * * HISTORY: Created 11.03.2003 * Updated for texture1D by Don Burns, 27.1.2004 - * Updated for light model - Stan Blinov at 25 august 7512 from World Creation (7.09.2004) + * Updated for light model - Stan Blinov at 25 august 7512 from World Creation (7.09.2004) * * * Copyright 2003 VR-C @@ -23,6 +23,7 @@ #include "CullFace.h" #include "ClipPlane.h" #include "PolygonOffset.h" +//#include "PolygonMode.h" #include "ShadeModel.h" #include "Point.h" #include "LineWidth.h" @@ -68,6 +69,8 @@ #include "ShapeDrawable.h" #include "Shape.h" +#include "Text.h" + #include #include #include @@ -92,7 +95,7 @@ DataInputStream::DataInputStream(std::istream* istream) { unsigned int endianType ; - _loadExternalReferenceFiles = false; + _loadExternalReferenceFiles = false; _verboseOutput = false; @@ -630,7 +633,7 @@ osg::Image* DataInputStream::readImage(std::string filename) { // If image is already read and in list // then just return pointer to this. - ImageMap::iterator mitr=_imageMap.find(filename); + ImageMap::iterator mitr=_imageMap.find(filename); if (mitr!=_imageMap.end()) return mitr->second.get(); // Image is not in list. @@ -708,6 +711,10 @@ osg::StateAttribute* DataInputStream::readStateAttribute() attribute = new osg::PolygonOffset(); ((ive::PolygonOffset*)(attribute))->read(this); } +/* else if(attributeID == IVEPOLYGONMODE){ + attribute = new osg::PolygonMode(); + ((ive::PolygonMode*)(attribute))->read(this); + }*/ else if(attributeID == IVESHADEMODEL){ attribute = new osg::ShadeModel(); ((ive::ShadeModel*)(attribute))->read(this); @@ -764,7 +771,7 @@ osg::StateAttribute* DataInputStream::readStateAttribute() attribute = new osg::LightModel(); ((ive::LightModel*)(attribute))->read(this); } - else if(attributeID == IVEFRONTFACE){ + else if(attributeID == IVEFRONTFACE){ attribute = new osg::FrontFace(); ((ive::FrontFace*)(attribute))->read(this); } @@ -804,6 +811,10 @@ osg::Drawable* DataInputStream::readDrawable() drawable = new osg::ShapeDrawable(); ((ShapeDrawable*)(drawable))->read(this); } + else if(drawableTypeID == IVETEXT){ + drawable = new osgText::Text(); + ((Text*)(drawable))->read(this); + } else throw Exception("Unknown drawable drawableTypeIDentification in Geode::read()"); diff --git a/src/osgPlugins/ive/DataInputStream.h b/src/osgPlugins/ive/DataInputStream.h index f2aafd429..82b5c4e46 100644 --- a/src/osgPlugins/ive/DataInputStream.h +++ b/src/osgPlugins/ive/DataInputStream.h @@ -2,7 +2,7 @@ #define IVE_DATAINPUTSTREAM 1 -#include // for ifstream +#include // for ifstream #include #include #include @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include #include @@ -23,7 +23,7 @@ #include #include "IveVersion.h" -#include "DataTypeSize.h" +#include "DataTypeSize.h" #include "Exception.h" @@ -33,83 +33,83 @@ namespace ive{ class DataInputStream{ public: - DataInputStream(std::istream* istream); - ~DataInputStream(); + DataInputStream(std::istream* istream); + ~DataInputStream(); void setOptions(const osgDB::ReaderWriter::Options* options); const osgDB::ReaderWriter::Options* getOptions() const { return _options.get(); } - unsigned int getVersion(); - bool readBool(); - char readChar(); - unsigned char readUChar(); - unsigned short readUShort(); - unsigned int readUInt(); - int readInt(); - int peekInt(); - float readFloat(); - long readLong(); - unsigned long readULong(); - double readDouble(); - std::string readString(); - void readCharArray(char* data, int size); + unsigned int getVersion(); + bool readBool(); + char readChar(); + unsigned char readUChar(); + unsigned short readUShort(); + unsigned int readUInt(); + int readInt(); + int peekInt(); + float readFloat(); + long readLong(); + unsigned long readULong(); + double readDouble(); + std::string readString(); + void readCharArray(char* data, int size); - osg::Vec2 readVec2(); - osg::Vec3 readVec3(); - osg::Vec4 readVec4(); - osg::Vec2d readVec2d(); - osg::Vec3d readVec3d(); - osg::Vec4d readVec4d(); - osg::Plane readPlane(); - osg::UByte4 readUByte4(); - osg::Quat readQuat(); - osg::Matrix readMatrix(); - osg::Geometry::AttributeBinding readBinding(); - osg::Array* readArray(); - osg::IntArray* readIntArray(); - osg::UByteArray* readUByteArray(); - osg::UShortArray* readUShortArray(); - osg::UIntArray* readUIntArray(); - osg::UByte4Array* readUByte4Array(); - osg::FloatArray* readFloatArray(); - osg::Vec2Array* readVec2Array(); - osg::Vec3Array* readVec3Array(); - osg::Vec4Array* readVec4Array(); + osg::Vec2 readVec2(); + osg::Vec3 readVec3(); + osg::Vec4 readVec4(); + osg::Vec2d readVec2d(); + osg::Vec3d readVec3d(); + osg::Vec4d readVec4d(); + osg::Plane readPlane(); + osg::UByte4 readUByte4(); + osg::Quat readQuat(); + osg::Matrix readMatrix(); + osg::Geometry::AttributeBinding readBinding(); + osg::Array* readArray(); + osg::IntArray* readIntArray(); + osg::UByteArray* readUByteArray(); + osg::UShortArray* readUShortArray(); + osg::UIntArray* readUIntArray(); + osg::UByte4Array* readUByte4Array(); + osg::FloatArray* readFloatArray(); + osg::Vec2Array* readVec2Array(); + osg::Vec3Array* readVec3Array(); + osg::Vec4Array* readVec4Array(); - osg::Image* readImage(std::string s); - osg::StateSet* readStateSet(); - osg::StateAttribute* readStateAttribute(); - osg::Drawable* readDrawable(); - osg::Shape* readShape(); - osg::Node* readNode(); + osg::Image* readImage(std::string s); + osg::StateSet* readStateSet(); + osg::StateAttribute* readStateAttribute(); + osg::Drawable* readDrawable(); + osg::Shape* readShape(); + osg::Node* readNode(); - // Set and get if must be generated external reference ive files - void setLoadExternalReferenceFiles(bool b) {_loadExternalReferenceFiles=b;}; - bool getLoadExternalReferenceFiles() {return _loadExternalReferenceFiles;}; + // Set and get if must be generated external reference ive files + void setLoadExternalReferenceFiles(bool b) {_loadExternalReferenceFiles=b;}; + bool getLoadExternalReferenceFiles() {return _loadExternalReferenceFiles;}; - typedef std::map > ImageMap; - typedef std::map > StateSetMap; - typedef std::map > StateAttributeMap; - typedef std::map > DrawableMap; - typedef std::map > ShapeMap; - typedef std::map > NodeMap; + typedef std::map > ImageMap; + typedef std::map > StateSetMap; + typedef std::map > StateAttributeMap; + typedef std::map > DrawableMap; + typedef std::map > ShapeMap; + typedef std::map > NodeMap; bool _verboseOutput; - std::istream* _istream; + std::istream* _istream; int _byteswap; private: - int _version; - bool _peeking; - int _peekValue; - ImageMap _imageMap; - StateSetMap _statesetMap; - StateAttributeMap _stateAttributeMap; - DrawableMap _drawableMap; - ShapeMap _shapeMap; - NodeMap _nodeMap; + int _version; + bool _peeking; + int _peekValue; + ImageMap _imageMap; + StateSetMap _statesetMap; + StateAttributeMap _stateAttributeMap; + DrawableMap _drawableMap; + ShapeMap _shapeMap; + NodeMap _nodeMap; - bool _loadExternalReferenceFiles; + bool _loadExternalReferenceFiles; osg::ref_ptr _options; diff --git a/src/osgPlugins/ive/DataOutputStream.cpp b/src/osgPlugins/ive/DataOutputStream.cpp index 372c81599..d687a8d0f 100644 --- a/src/osgPlugins/ive/DataOutputStream.cpp +++ b/src/osgPlugins/ive/DataOutputStream.cpp @@ -24,6 +24,7 @@ #include "CullFace.h" #include "ClipPlane.h" #include "PolygonOffset.h" +//#include "PolygonMode.h" #include "ShadeModel.h" #include "Point.h" #include "LineWidth.h" @@ -70,6 +71,8 @@ #include "Shape.h" +#include "Text.h" + #include #include @@ -103,8 +106,8 @@ DataOutputStream::DataOutputStream(std::ostream * ostream) _includeImageData= true; _includeExternalReferences = false; - _writeExternalReferenceFiles = true; - _useOriginalExternalReferences = false; + _writeExternalReferenceFiles = false; + _useOriginalExternalReferences = true; _ostream = ostream; @@ -510,6 +513,10 @@ void DataOutputStream::writeStateAttribute(const osg::StateAttribute* attribute) else if(dynamic_cast(attribute)){ ((ive::PolygonOffset*)(attribute))->write(this); } +/* // This is a PolygonMode + else if(dynamic_cast(attribute)){ + ((ive::PolygonMode*)(attribute))->write(this); + }*/ else if(dynamic_cast(attribute)){ ((ive::ShadeModel*)(attribute))->write(this); } @@ -566,8 +573,8 @@ void DataOutputStream::writeStateAttribute(const osg::StateAttribute* attribute) else if(dynamic_cast(attribute)){ ((ive::LightModel*)(attribute))->write(this); } - // This is a FrontFace - else if(dynamic_cast(attribute)){ + // This is a FrontFace + else if(dynamic_cast(attribute)){ ((ive::FrontFace*)(attribute))->write(this); } @@ -604,6 +611,8 @@ void DataOutputStream::writeDrawable(const osg::Drawable* drawable) ((ive::Geometry*)(drawable))->write(this); else if(dynamic_cast(drawable)) ((ive::ShapeDrawable*)(drawable))->write(this); + else if(dynamic_cast(drawable)) + ((ive::Text*)(drawable))->write(this); else { throw Exception("Unknown drawable in DataOutputStream::writeDrawable()"); diff --git a/src/osgPlugins/ive/GNUmakefile b/src/osgPlugins/ive/GNUmakefile index 75fb5bd5f..9d2b2f57f 100644 --- a/src/osgPlugins/ive/GNUmakefile +++ b/src/osgPlugins/ive/GNUmakefile @@ -74,6 +74,7 @@ CXXFILES =\ Texture2D.cpp\ Texture3D.cpp\ Texture.cpp\ + Text.cpp\ TextureCubeMap.cpp\ Transform.cpp\ VertexProgram.cpp\ diff --git a/src/osgPlugins/ive/ReadWrite.h b/src/osgPlugins/ive/ReadWrite.h index 9acce3f3b..961eeaad2 100644 --- a/src/osgPlugins/ive/ReadWrite.h +++ b/src/osgPlugins/ive/ReadWrite.h @@ -102,7 +102,10 @@ namespace ive { #define IVEDIRECTIONALSECTOR 0x0010000A // osgFX classes -#define IVEMULTITEXTURECONTROL 0x01000001 +#define IVEMULTITEXTURECONTROL 0x01000001 + +//osgText classes +#define IVETEXT 0x10000001 class ReadWrite{ diff --git a/src/osgPlugins/ive/Text.cpp b/src/osgPlugins/ive/Text.cpp new file mode 100644 index 000000000..02589768c --- /dev/null +++ b/src/osgPlugins/ive/Text.cpp @@ -0,0 +1,187 @@ +/********************************************************************** + * + * FILE: Text.cpp + * + * DESCRIPTION: Read/Write osgText::Text in binary format to disk. + * + * CREATED BY: Auto generated by iveGenerator + * and later modified by Rune Schmidt Jensen. + * + * HISTORY: Created 27.3.2003 + * + * Copyright 2003 VR-C + **********************************************************************/ + +#include "Exception.h" +#include "Text.h" +#include "Drawable.h" +#include "Object.h" + +#include +#include +#include + +using namespace ive; + +void Text::write(DataOutputStream* out){ + // Write Text's identification. + out->writeInt(IVETEXT); + // If the osg class is inherited by any other class we should also write this to file. + osg::Object* obj = dynamic_cast(this); + if(obj){ + ((ive::Drawable*)(obj))->write(out); + } + else + throw Exception("Text::write(): Could not cast this osgText::Text to an osg::Drawable."); + // Write Text's properties. + if( getFont() ) + { + std::string fname = getFont()->getFileName(); + + if(!fname.empty()) + { + if(out->getUseOriginalExternalReferences()) + { + out->writeString(fname); //Saving file name with local directory + } + else + { + out->writeString(osgDB::getSimpleFileName(fname)); //Saving original file name + } + } + else + out->writeString(""); //Blank string + } + else + out->writeString(""); //Blank string + + out->writeUInt(getFontWidth()); + out->writeUInt(getFontHeight()); + out->writeFloat(getCharacterHeight()); + out->writeFloat(getCharacterAspectRatio()); + out->writeUInt(getCharacterSizeMode()); + out->writeFloat(getMaximumWidth()); + out->writeFloat(getMaximumHeight()); + out->writeUInt(getAlignment()); + + out->writeQuat(getRotation()); //FIXME: controllare che ci sia + + out->writeBool(getAutoRotateToScreen()); + out->writeUInt(getLayout()); + out->writeVec3(getPosition()); + out->writeVec4(getColor()); + out->writeUInt(getDrawMode()); + + // text :: Modified from osgPlugins::osg + const osgText::String& textstring = getText(); + bool isACString = true; + osgText::String::const_iterator itr; + for(itr=textstring.begin(); + itr!=textstring.end() && isACString; + ++itr) + { + if (*itr==0 || *itr>256) isACString=false; + } + + if (isACString) + { + std::string str; + + for(itr=textstring.begin(); + itr!=textstring.end(); + ++itr) + { + str += (char)(*itr); + } + + //std::copy(textstring.begin(),textstring.end(),std::back_inserter(str)); + + out->writeBool(true); + out->writeString(str); + } + else + { + // do it the hardway...output each character as an int + osg::ref_ptr strarr = new osg::UByteArray(textstring.size()); + + for(itr=textstring.begin(); + itr!=textstring.end(); + ++itr) + { + strarr->push_back((char)(*itr)); + } + + out->writeBool(false); + out->writeUByteArray(strarr.get()); + } +} + +void Text::read(DataInputStream* in){ + // Peek on Text's identification. + int id = in->peekInt(); + if(id == IVETEXT){ + // Read Text's identification. + id = in->readInt(); + // If the osg class is inherited by any other class we should also read this from file. + osg::Object* obj = dynamic_cast(this); + if(obj){ + ((ive::Drawable*)(obj))->read(in); + } + else + throw Exception("Text::read(): Could not cast this osgText::Text to an osg::Drawable."); + // Read Text's properties + + unsigned int width, height; + float c_height, aspectRatio; + + setFont(in->readString()); + + width = in->readUInt(); + height = in->readUInt(); + + setFontResolution(width,height); + + c_height = in->readFloat(); + aspectRatio = in->readFloat(); + + setCharacterSize(height,aspectRatio); + + setCharacterSizeMode((osgText::Text::CharacterSizeMode) in->readUInt()); + + setMaximumWidth(in->readFloat()); + setMaximumHeight(in->readFloat()); + + setAlignment((osgText::Text::AlignmentType) in->readUInt()); + + //Nothing to do... + //setAxisAlignment((osgText::Text::AxisAlignment) in->readUint()); + + setRotation(in->readQuat()); + setAutoRotateToScreen(in->readBool()); + setLayout((osgText::Text::Layout) in->readUInt()); + + setPosition(in->readVec3()); + setColor(in->readVec4()); + setDrawMode(in->readUInt()); + + if(in->readBool()) + setText(in->readString()); + else + { + std::string textstr; + + osg::ref_ptr arr = in->readUByteArray(); + + for(unsigned int i = 0; i < arr->getNumElements(); i++) + { + textstr += (char) arr->at(i); + } + + setText(textstr); + } + + } + else{ + throw Exception("ShadeModel::read(): Expected ShadeModel identification."); + } +} diff --git a/src/osgPlugins/ive/Text.h b/src/osgPlugins/ive/Text.h new file mode 100644 index 000000000..93c9f1bbb --- /dev/null +++ b/src/osgPlugins/ive/Text.h @@ -0,0 +1,15 @@ +#ifndef IVE_TEXT +#define IVE_TEXT 1 + +#include +#include "ReadWrite.h" + +namespace ive{ +class Text : public osgText::Text, public ReadWrite { +public: + void write(DataOutputStream* out); + void read(DataInputStream* in); +}; +} + +#endif diff --git a/src/osgPlugins/osgText/IO_Text.cpp b/src/osgPlugins/osgText/IO_Text.cpp index f3fc3bc38..02e7f6b7a 100644 --- a/src/osgPlugins/osgText/IO_Text.cpp +++ b/src/osgPlugins/osgText/IO_Text.cpp @@ -87,7 +87,7 @@ bool Text_readLocalData(osg::Object &obj, osgDB::Input &fr) float height; if (fr[1].getFloat(height)) { - text.setMaximumWidth(height); + text.setMaximumHeight(height); fr += 2; itAdvanced = true; }