From cfa9c3682f37452183dadba36f418d74d30bd861 Mon Sep 17 00:00:00 2001 From: Paul MARTZ Date: Wed, 17 Mar 2010 16:59:50 +0000 Subject: [PATCH] 2.8 branch: This is part I of an attempt to add the new ply plugin and also bring the 3ds plugin up to date (writer capability, etc). Revisions merged from trunk in this commit: 10012, 10013, 10014, 10040, 10041, 10079, 10080, 9906, 10057, and 10076. --- include/osgDB/ReaderWriter | 26 +- src/osgPlugins/3ds/ReaderWriter3DS.cpp | 61 +- src/osgPlugins/3ds/atmosphere.cpp | 130 +- src/osgPlugins/3ds/atmosphere.h | 7 +- src/osgPlugins/3ds/background.cpp | 78 +- src/osgPlugins/3ds/background.h | 7 +- src/osgPlugins/3ds/camera.cpp | 46 +- src/osgPlugins/3ds/camera.h | 7 +- src/osgPlugins/3ds/chunk.cpp | 68 +- src/osgPlugins/3ds/chunk.h | 22 +- src/osgPlugins/3ds/file.cpp | 336 +-- src/osgPlugins/3ds/file.h | 12 +- src/osgPlugins/3ds/light.cpp | 128 +- src/osgPlugins/3ds/light.h | 8 +- src/osgPlugins/3ds/material.cpp | 406 ++-- src/osgPlugins/3ds/material.h | 7 +- src/osgPlugins/3ds/mesh.cpp | 192 +- src/osgPlugins/3ds/mesh.h | 9 +- src/osgPlugins/3ds/node.cpp | 200 +- src/osgPlugins/3ds/node.h | 8 +- src/osgPlugins/3ds/readwrite.cpp | 181 +- src/osgPlugins/3ds/readwrite.h | 44 +- src/osgPlugins/3ds/shadow.cpp | 48 +- src/osgPlugins/3ds/shadow.h | 7 +- src/osgPlugins/3ds/tcb.cpp | 37 +- src/osgPlugins/3ds/tcb.h | 9 +- src/osgPlugins/3ds/tracks.cpp | 116 +- src/osgPlugins/3ds/tracks.h | 20 +- src/osgPlugins/3ds/viewport.cpp | 212 +- src/osgPlugins/3ds/viewport.h | 7 +- src/osgPlugins/CMakeLists.txt | 1 + src/osgPlugins/ply/CMakeLists.txt | 15 + src/osgPlugins/ply/ReaderWriterPLY.cpp | 81 + src/osgPlugins/ply/ply.h | 172 ++ src/osgPlugins/ply/plyfile.cpp | 2686 ++++++++++++++++++++++++ src/osgPlugins/ply/typedefs.h | 112 + src/osgPlugins/ply/vertexData.cpp | 373 ++++ src/osgPlugins/ply/vertexData.h | 74 + 38 files changed, 4798 insertions(+), 1155 deletions(-) create mode 100644 src/osgPlugins/ply/CMakeLists.txt create mode 100644 src/osgPlugins/ply/ReaderWriterPLY.cpp create mode 100644 src/osgPlugins/ply/ply.h create mode 100644 src/osgPlugins/ply/plyfile.cpp create mode 100644 src/osgPlugins/ply/typedefs.h create mode 100644 src/osgPlugins/ply/vertexData.cpp create mode 100644 src/osgPlugins/ply/vertexData.h diff --git a/include/osgDB/ReaderWriter b/include/osgDB/ReaderWriter index 237d74298..cc7470039 100644 --- a/include/osgDB/ReaderWriter +++ b/include/osgDB/ReaderWriter @@ -125,7 +125,8 @@ class OSGDB_EXPORT ReaderWriter : public osg::Object _databasePaths(options._databasePaths), _objectCacheHint(options._objectCacheHint), _buildKdTreesHint(options._buildKdTreesHint), - _pluginData(options._pluginData){} + _pluginData(options._pluginData), + _pluginStringData(options._pluginStringData){} META_Object(osgDB,Options); @@ -182,6 +183,26 @@ class OSGDB_EXPORT ReaderWriter : public osg::Object /** Remove a value from the PluginData */ void removePluginData(const std::string& s) const { _pluginData.erase(s); } + + /** Sets a plugindata value PluginData with a string */ + void setPluginStringData(const std::string& s, const std::string& v) const { _pluginStringData[s] = v; } + + /** Get a string from the PluginStrData */ + std::string getPluginStringData(const std::string& s) { return _pluginStringData[s]; } + + /** Get a value from the PluginData */ + const std::string getPluginStringData(const std::string& s) const + { + PluginStringDataMap::const_iterator itr = _pluginStringData.find(s); + return (itr == _pluginStringData.end()) ? std::string("") : itr->second; + } + + /** Remove a value from the PluginData */ + void removePluginStringData(const std::string& s) const { _pluginStringData.erase(s); } + + + + protected: virtual ~Options() {} @@ -194,6 +215,9 @@ class OSGDB_EXPORT ReaderWriter : public osg::Object typedef std::map PluginDataMap; mutable PluginDataMap _pluginData; + typedef std::map PluginStringDataMap; + mutable PluginStringDataMap _pluginStringData; + }; diff --git a/src/osgPlugins/3ds/ReaderWriter3DS.cpp b/src/osgPlugins/3ds/ReaderWriter3DS.cpp index fd3f25a2a..178924558 100644 --- a/src/osgPlugins/3ds/ReaderWriter3DS.cpp +++ b/src/osgPlugins/3ds/ReaderWriter3DS.cpp @@ -34,6 +34,7 @@ #include #include #include +#include using namespace std; using namespace osg; @@ -95,8 +96,10 @@ class ReaderWriter3DS : public osgDB::ReaderWriter virtual const char* className() const { return "3DS Auto Studio Reader"; } virtual ReadResult readNode(const std::string& file, const osgDB::ReaderWriter::Options* options) const; + virtual ReadResult readNode(std::istream& fin, const Options* options) const; protected: + ReadResult constructFrom3dsFile(Lib3dsFile *f,const std::string& filename, const Options* options) const; class ReaderObject @@ -440,9 +443,29 @@ osg::Node* ReaderWriter3DS::ReaderObject::processNode(StateSetMap drawStateMap,L } } +osgDB::ReaderWriter::ReadResult ReaderWriter3DS::readNode(std::istream& fin, const osgDB::ReaderWriter::Options* options) const +{ + osgDB::ReaderWriter::ReadResult result = ReadResult::FILE_NOT_HANDLED; + + std::string optFileName = ""; + if (options) + { + optFileName = options->getPluginStringData("STREAM_FILENAME"); + } + + Lib3dsFile *f = lib3ds_stream_load((iostream *) &fin); + if (f) + { + result = constructFrom3dsFile(f,optFileName,options); + lib3ds_file_free(f); + } + + return(result); +} osgDB::ReaderWriter::ReadResult ReaderWriter3DS::readNode(const std::string& file, const osgDB::ReaderWriter::Options* options) const { + osgDB::ReaderWriter::ReadResult result = ReadResult::FILE_NOT_HANDLED; std::string ext = osgDB::getLowerCaseFileExtension(file); if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; @@ -450,7 +473,22 @@ osgDB::ReaderWriter::ReadResult ReaderWriter3DS::readNode(const std::string& fil std::string fileName = osgDB::findDataFile( file, options ); if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; - Lib3dsFile *f = lib3ds_file_load(fileName.c_str()); + Lib3dsFile *f = lib3ds_file_load(fileName.c_str(),options); + + if (f) + { + osg::ref_ptr local_opt = options ? static_cast(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options; + local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName)); + + result = constructFrom3dsFile(f,file,local_opt.get()); + lib3ds_file_free(f); + } + + return result; +} + +osgDB::ReaderWriter::ReadResult ReaderWriter3DS::constructFrom3dsFile(Lib3dsFile *f,const std::string& fileName, const osgDB::ReaderWriter::Options* options) const +{ if (f==NULL) return ReadResult::FILE_NOT_HANDLED; // MIKEC @@ -461,7 +499,7 @@ osgDB::ReaderWriter::ReadResult ReaderWriter3DS::readNode(const std::string& fil ReaderObject reader; - reader._directory = osgDB::getFilePath(fileName); + reader._directory = options->getDatabasePathList().empty() ? osgDB::getFilePath(fileName) : options->getDatabasePathList().front(); osg::Group* group = new osg::Group; group->setName(fileName); @@ -515,8 +553,6 @@ osgDB::ReaderWriter::ReadResult ReaderWriter3DS::readNode(const std::string& fil group->accept(pv); } - lib3ds_file_free(f); - return group; } @@ -690,17 +726,26 @@ osg::Texture2D* ReaderWriter3DS::ReaderObject::createTexture(Lib3dsTextureMap * { if (texture && *(texture->name)) { + osg::notify(osg::NOTICE)<<"texture->name="<name<<", _directory="<<_directory<name,_directory,osgDB::CASE_INSENSITIVE); if (fileName.empty()) { // file not found in .3ds file's directory, so we'll look in the datafile path list. fileName = osgDB::findDataFile(texture->name,options, osgDB::CASE_INSENSITIVE); } - + if (fileName.empty()) { - osg::notify(osg::WARN) << "texture '"<name<<"' not found"<< std::endl; - return NULL; + if (osgDB::containsServerAddress(_directory)) + { + // if 3DS file is loaded from http, just attempt to load texture from same location. + fileName = _directory + "/" + texture->name; + } else { + osg::notify(osg::WARN) << "texture '"<name<<"' not found"<< std::endl; + return NULL; + } } if (label) osg::notify(osg::DEBUG_INFO) << label; @@ -821,7 +866,7 @@ osg::StateSet* ReaderWriter3DS::ReaderObject::createStateSet(Lib3dsMaterial *mat // stateset->setTextureAttribute(0,texenv); } - if (transparency>0.0f || textureTransparancy || mat->opacity_map.flags!=0) + if (transparency>0.0f || textureTransparancy) { stateset->setMode(GL_BLEND,osg::StateAttribute::ON); stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); diff --git a/src/osgPlugins/3ds/atmosphere.cpp b/src/osgPlugins/3ds/atmosphere.cpp index c24301a00..54b3ef122 100644 --- a/src/osgPlugins/3ds/atmosphere.cpp +++ b/src/osgPlugins/3ds/atmosphere.cpp @@ -33,27 +33,27 @@ static Lib3dsBool -fog_read(Lib3dsFog *fog, FILE *f) +fog_read(Lib3dsFog *fog, iostream *strm) { Lib3dsChunk c; Lib3dsWord chunk; - if (!lib3ds_chunk_read_start(&c, LIB3DS_FOG, f)) { + if (!lib3ds_chunk_read_start(&c, LIB3DS_FOG, strm)) { return(LIB3DS_FALSE); } - fog->near_plane=lib3ds_float_read(f); - fog->near_density=lib3ds_float_read(f); - fog->far_plane=lib3ds_float_read(f); - fog->far_density=lib3ds_float_read(f); - lib3ds_chunk_read_tell(&c, f); + fog->near_plane=lib3ds_float_read(strm); + fog->near_density=lib3ds_float_read(strm); + fog->far_plane=lib3ds_float_read(strm); + fog->far_density=lib3ds_float_read(strm); + lib3ds_chunk_read_tell(&c, strm); - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { switch (chunk) { case LIB3DS_LIN_COLOR_F: { int i; for (i=0; i<3; ++i) { - fog->col[i]=lib3ds_float_read(f); + fog->col[i]=lib3ds_float_read(strm); } } break; @@ -69,60 +69,60 @@ fog_read(Lib3dsFog *fog, FILE *f) } } - lib3ds_chunk_read_end(&c, f); + lib3ds_chunk_read_end(&c, strm); return(LIB3DS_TRUE); } static Lib3dsBool -layer_fog_read(Lib3dsLayerFog *fog, FILE *f) +layer_fog_read(Lib3dsLayerFog *fog, iostream *strm) { Lib3dsChunk c; Lib3dsWord chunk; - if (!lib3ds_chunk_read_start(&c, LIB3DS_LAYER_FOG, f)) { + if (!lib3ds_chunk_read_start(&c, LIB3DS_LAYER_FOG, strm)) { return(LIB3DS_FALSE); } - fog->near_y=lib3ds_float_read(f); - fog->far_y=lib3ds_float_read(f); - fog->density=lib3ds_float_read(f); - fog->flags=lib3ds_dword_read(f); - lib3ds_chunk_read_tell(&c, f); + fog->near_y=lib3ds_float_read(strm); + fog->far_y=lib3ds_float_read(strm); + fog->density=lib3ds_float_read(strm); + fog->flags=lib3ds_dword_read(strm); + lib3ds_chunk_read_tell(&c, strm); - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { switch (chunk) { case LIB3DS_LIN_COLOR_F: - lib3ds_rgb_read(fog->col,f); + lib3ds_rgb_read(fog->col,strm); break; case LIB3DS_COLOR_F: - lib3ds_rgb_read(fog->col,f); + lib3ds_rgb_read(fog->col,strm); break; default: lib3ds_chunk_unknown(chunk); } } - lib3ds_chunk_read_end(&c, f); + lib3ds_chunk_read_end(&c, strm); return(LIB3DS_TRUE); } static Lib3dsBool -distance_cue_read(Lib3dsDistanceCue *cue, FILE *f) +distance_cue_read(Lib3dsDistanceCue *cue, iostream *strm) { Lib3dsChunk c; Lib3dsWord chunk; - if (!lib3ds_chunk_read_start(&c, LIB3DS_DISTANCE_CUE, f)) { + if (!lib3ds_chunk_read_start(&c, LIB3DS_DISTANCE_CUE, strm)) { return(LIB3DS_FALSE); } - cue->near_plane=lib3ds_float_read(f); - cue->near_dimming=lib3ds_float_read(f); - cue->far_plane=lib3ds_float_read(f); - cue->far_dimming=lib3ds_float_read(f); - lib3ds_chunk_read_tell(&c, f); + cue->near_plane=lib3ds_float_read(strm); + cue->near_dimming=lib3ds_float_read(strm); + cue->far_plane=lib3ds_float_read(strm); + cue->far_dimming=lib3ds_float_read(strm); + lib3ds_chunk_read_tell(&c, strm); - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { switch (chunk) { case LIB3DS_DCUE_BGND: { @@ -134,7 +134,7 @@ distance_cue_read(Lib3dsDistanceCue *cue, FILE *f) } } - lib3ds_chunk_read_end(&c, f); + lib3ds_chunk_read_end(&c, strm); return(LIB3DS_TRUE); } @@ -143,35 +143,35 @@ distance_cue_read(Lib3dsDistanceCue *cue, FILE *f) * \ingroup atmosphere */ Lib3dsBool -lib3ds_atmosphere_read(Lib3dsAtmosphere *atmosphere, FILE *f) +lib3ds_atmosphere_read(Lib3dsAtmosphere *atmosphere, iostream *strm) { Lib3dsChunk c; - if (!lib3ds_chunk_read(&c, f)) { + if (!lib3ds_chunk_read(&c, strm)) { return(LIB3DS_FALSE); } switch (c.chunk) { case LIB3DS_FOG: { - lib3ds_chunk_read_reset(&c, f); - if (!fog_read(&atmosphere->fog, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!fog_read(&atmosphere->fog, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_LAYER_FOG: { - lib3ds_chunk_read_reset(&c, f); - if (!layer_fog_read(&atmosphere->layer_fog, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!layer_fog_read(&atmosphere->layer_fog, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_DISTANCE_CUE: { - lib3ds_chunk_read_reset(&c, f); - if (!distance_cue_read(&atmosphere->dist_cue, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!distance_cue_read(&atmosphere->dist_cue, strm)) { return(LIB3DS_FALSE); } } @@ -201,32 +201,32 @@ lib3ds_atmosphere_read(Lib3dsAtmosphere *atmosphere, FILE *f) * \ingroup atmosphere */ Lib3dsBool -lib3ds_atmosphere_write(Lib3dsAtmosphere *atmosphere, FILE *f) +lib3ds_atmosphere_write(Lib3dsAtmosphere *atmosphere, iostream *strm) { if (atmosphere->fog.use) { /*---- LIB3DS_FOG ----*/ Lib3dsChunk c; c.chunk=LIB3DS_FOG; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - lib3ds_float_write(atmosphere->fog.near_plane,f); - lib3ds_float_write(atmosphere->fog.near_density,f); - lib3ds_float_write(atmosphere->fog.far_plane,f); - lib3ds_float_write(atmosphere->fog.far_density,f); + lib3ds_float_write(atmosphere->fog.near_plane,strm); + lib3ds_float_write(atmosphere->fog.near_density,strm); + lib3ds_float_write(atmosphere->fog.far_plane,strm); + lib3ds_float_write(atmosphere->fog.far_density,strm); { Lib3dsChunk c; c.chunk=LIB3DS_COLOR_F; c.size=18; - lib3ds_chunk_write(&c,f); - lib3ds_rgb_write(atmosphere->fog.col,f); + lib3ds_chunk_write(&c,strm); + lib3ds_rgb_write(atmosphere->fog.col,strm); } if (atmosphere->fog.fog_background) { Lib3dsChunk c; c.chunk=LIB3DS_FOG_BGND; c.size=6; - lib3ds_chunk_write(&c,f); + lib3ds_chunk_write(&c,strm); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } } @@ -235,37 +235,37 @@ lib3ds_atmosphere_write(Lib3dsAtmosphere *atmosphere, FILE *f) Lib3dsChunk c; c.chunk=LIB3DS_LAYER_FOG; c.size=40; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(atmosphere->layer_fog.near_y,f); - lib3ds_float_write(atmosphere->layer_fog.far_y,f); - lib3ds_float_write(atmosphere->layer_fog.near_y,f); - lib3ds_dword_write(atmosphere->layer_fog.flags,f); + lib3ds_chunk_write(&c,strm); + lib3ds_float_write(atmosphere->layer_fog.near_y,strm); + lib3ds_float_write(atmosphere->layer_fog.far_y,strm); + lib3ds_float_write(atmosphere->layer_fog.near_y,strm); + lib3ds_dword_write(atmosphere->layer_fog.flags,strm); { Lib3dsChunk c; c.chunk=LIB3DS_COLOR_F; c.size=18; - lib3ds_chunk_write(&c,f); - lib3ds_rgb_write(atmosphere->fog.col,f); + lib3ds_chunk_write(&c,strm); + lib3ds_rgb_write(atmosphere->fog.col,strm); } } if (atmosphere->dist_cue.use) { /*---- LIB3DS_DISTANCE_CUE ----*/ Lib3dsChunk c; c.chunk=LIB3DS_DISTANCE_CUE; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - lib3ds_float_write(atmosphere->dist_cue.near_plane,f); - lib3ds_float_write(atmosphere->dist_cue.near_dimming,f); - lib3ds_float_write(atmosphere->dist_cue.far_plane,f); - lib3ds_float_write(atmosphere->dist_cue.far_dimming,f); + lib3ds_float_write(atmosphere->dist_cue.near_plane,strm); + lib3ds_float_write(atmosphere->dist_cue.near_dimming,strm); + lib3ds_float_write(atmosphere->dist_cue.far_plane,strm); + lib3ds_float_write(atmosphere->dist_cue.far_dimming,strm); if (atmosphere->dist_cue.cue_background) { Lib3dsChunk c; c.chunk=LIB3DS_DCUE_BGND; c.size=6; - lib3ds_chunk_write(&c,f); + lib3ds_chunk_write(&c,strm); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } } @@ -274,21 +274,21 @@ lib3ds_atmosphere_write(Lib3dsAtmosphere *atmosphere, FILE *f) Lib3dsChunk c; c.chunk=LIB3DS_USE_FOG; c.size=6; - lib3ds_chunk_write(&c,f); + lib3ds_chunk_write(&c,strm); } if (atmosphere->layer_fog.use) { /*---- LIB3DS_USE_LAYER_FOG ----*/ Lib3dsChunk c; c.chunk=LIB3DS_USE_LAYER_FOG; c.size=6; - lib3ds_chunk_write(&c,f); + lib3ds_chunk_write(&c,strm); } if (atmosphere->dist_cue.use) { /*---- LIB3DS_USE_DISTANCE_CUE ----*/ Lib3dsChunk c; c.chunk=LIB3DS_USE_V_GRADIENT; c.size=6; - lib3ds_chunk_write(&c,f); + lib3ds_chunk_write(&c,strm); } return(LIB3DS_TRUE); diff --git a/src/osgPlugins/3ds/atmosphere.h b/src/osgPlugins/3ds/atmosphere.h index 58c620eb6..df5a4bbaf 100644 --- a/src/osgPlugins/3ds/atmosphere.h +++ b/src/osgPlugins/3ds/atmosphere.h @@ -27,6 +27,9 @@ #include "types.h" #endif +#include +using namespace std; + #ifdef __cplusplus extern "C" { #endif @@ -91,8 +94,8 @@ struct _Lib3dsAtmosphere { Lib3dsDistanceCue dist_cue; }; -extern LIB3DSAPI Lib3dsBool lib3ds_atmosphere_read(Lib3dsAtmosphere *atmosphere, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_atmosphere_write(Lib3dsAtmosphere *atmosphere, FILE *f); +extern LIB3DSAPI Lib3dsBool lib3ds_atmosphere_read(Lib3dsAtmosphere *atmosphere, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_atmosphere_write(Lib3dsAtmosphere *atmosphere, iostream *strm); #ifdef __cplusplus } diff --git a/src/osgPlugins/3ds/background.cpp b/src/osgPlugins/3ds/background.cpp index 410563d80..f4eabfe53 100644 --- a/src/osgPlugins/3ds/background.cpp +++ b/src/osgPlugins/3ds/background.cpp @@ -35,35 +35,35 @@ static Lib3dsBool -solid_bgnd_read(Lib3dsBackground *background, FILE *f) +solid_bgnd_read(Lib3dsBackground *background, iostream *strm) { Lib3dsChunk c; Lib3dsWord chunk; - if (!lib3ds_chunk_read_start(&c, LIB3DS_SOLID_BGND, f)) { + if (!lib3ds_chunk_read_start(&c, LIB3DS_SOLID_BGND, strm)) { return(LIB3DS_FALSE); } - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { switch (chunk) { case LIB3DS_LIN_COLOR_F: - lib3ds_rgb_read(background->solid.col, f); + lib3ds_rgb_read(background->solid.col, strm); break; case LIB3DS_COLOR_F: - lib3ds_rgb_read(background->solid.col, f); + lib3ds_rgb_read(background->solid.col, strm); break; default: lib3ds_chunk_unknown(chunk); } } - lib3ds_chunk_read_end(&c, f); + lib3ds_chunk_read_end(&c, strm); return(LIB3DS_TRUE); } static Lib3dsBool -v_gradient_read(Lib3dsBackground *background, FILE *f) +v_gradient_read(Lib3dsBackground *background, iostream *strm) { Lib3dsChunk c; Lib3dsWord chunk; @@ -72,21 +72,21 @@ v_gradient_read(Lib3dsBackground *background, FILE *f) int have_lin=0; - if (!lib3ds_chunk_read_start(&c, LIB3DS_V_GRADIENT, f)) { + if (!lib3ds_chunk_read_start(&c, LIB3DS_V_GRADIENT, strm)) { return(LIB3DS_FALSE); } - background->gradient.percent=lib3ds_float_read(f); - lib3ds_chunk_read_tell(&c, f); + background->gradient.percent=lib3ds_float_read(strm); + lib3ds_chunk_read_tell(&c, strm); index[0]=index[1]=0; - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { switch (chunk) { case LIB3DS_COLOR_F: - lib3ds_rgb_read(col[0][index[0]],f); + lib3ds_rgb_read(col[0][index[0]],strm); index[0]++; break; case LIB3DS_LIN_COLOR_F: - lib3ds_rgb_read(col[1][index[1]],f); + lib3ds_rgb_read(col[1][index[1]],strm); index[1]++; have_lin=1; break; @@ -102,7 +102,7 @@ v_gradient_read(Lib3dsBackground *background, FILE *f) background->gradient.bottom[i]=col[have_lin][2][i]; } } - lib3ds_chunk_read_end(&c, f); + lib3ds_chunk_read_end(&c, strm); return(LIB3DS_TRUE); } @@ -111,34 +111,34 @@ v_gradient_read(Lib3dsBackground *background, FILE *f) * \ingroup background */ Lib3dsBool -lib3ds_background_read(Lib3dsBackground *background, FILE *f) +lib3ds_background_read(Lib3dsBackground *background, iostream *strm) { Lib3dsChunk c; - if (!lib3ds_chunk_read(&c, f)) { + if (!lib3ds_chunk_read(&c, strm)) { return(LIB3DS_FALSE); } switch (c.chunk) { case LIB3DS_BIT_MAP: { - if (!lib3ds_string_read(background->bitmap.name, 64, f)) { + if (!lib3ds_string_read(background->bitmap.name, 64, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_SOLID_BGND: { - lib3ds_chunk_read_reset(&c, f); - if (!solid_bgnd_read(background, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!solid_bgnd_read(background, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_V_GRADIENT: { - lib3ds_chunk_read_reset(&c, f); - if (!v_gradient_read(background, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!v_gradient_read(background, strm)) { return(LIB3DS_FALSE); } } @@ -165,19 +165,19 @@ lib3ds_background_read(Lib3dsBackground *background, FILE *f) static Lib3dsBool -colorf_write(Lib3dsRgba rgb, FILE *f) +colorf_write(Lib3dsRgba rgb, iostream *strm) { Lib3dsChunk c; c.chunk=LIB3DS_COLOR_F; c.size=18; - lib3ds_chunk_write(&c,f); - lib3ds_rgb_write(rgb,f); + lib3ds_chunk_write(&c,strm); + lib3ds_rgb_write(rgb,strm); c.chunk=LIB3DS_LIN_COLOR_F; c.size=18; - lib3ds_chunk_write(&c,f); - lib3ds_rgb_write(rgb,f); + lib3ds_chunk_write(&c,strm); + lib3ds_rgb_write(rgb,strm); return(LIB3DS_TRUE); } @@ -199,22 +199,22 @@ colorf_defined(Lib3dsRgba rgb) * \ingroup background */ Lib3dsBool -lib3ds_background_write(Lib3dsBackground *background, FILE *f) +lib3ds_background_write(Lib3dsBackground *background, iostream *strm) { if (strlen(background->bitmap.name)) { /*---- LIB3DS_BIT_MAP ----*/ Lib3dsChunk c; c.chunk=LIB3DS_BIT_MAP; c.size=6+1+strlen(background->bitmap.name); - lib3ds_chunk_write(&c,f); - lib3ds_string_write(background->bitmap.name, f); + lib3ds_chunk_write(&c,strm); + lib3ds_string_write(background->bitmap.name, strm); } if (colorf_defined(background->solid.col)) { /*---- LIB3DS_SOLID_BGND ----*/ Lib3dsChunk c; c.chunk=LIB3DS_SOLID_BGND; c.size=42; - lib3ds_chunk_write(&c,f); - colorf_write(background->solid.col,f); + lib3ds_chunk_write(&c,strm); + colorf_write(background->solid.col,strm); } if (colorf_defined(background->gradient.top) || @@ -223,32 +223,32 @@ lib3ds_background_write(Lib3dsBackground *background, FILE *f) Lib3dsChunk c; c.chunk=LIB3DS_V_GRADIENT; c.size=118; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(background->gradient.percent,f); - colorf_write(background->gradient.top,f); - colorf_write(background->gradient.middle,f); - colorf_write(background->gradient.bottom,f); + lib3ds_chunk_write(&c,strm); + lib3ds_float_write(background->gradient.percent,strm); + colorf_write(background->gradient.top,strm); + colorf_write(background->gradient.middle,strm); + colorf_write(background->gradient.bottom,strm); } if (background->bitmap.use) { /*---- LIB3DS_USE_BIT_MAP ----*/ Lib3dsChunk c; c.chunk=LIB3DS_USE_BIT_MAP; c.size=6; - lib3ds_chunk_write(&c,f); + lib3ds_chunk_write(&c,strm); } if (background->solid.use) { /*---- LIB3DS_USE_SOLID_BGND ----*/ Lib3dsChunk c; c.chunk=LIB3DS_USE_SOLID_BGND; c.size=6; - lib3ds_chunk_write(&c,f); + lib3ds_chunk_write(&c,strm); } if (background->gradient.use) { /*---- LIB3DS_USE_V_GRADIENT ----*/ Lib3dsChunk c; c.chunk=LIB3DS_USE_V_GRADIENT; c.size=6; - lib3ds_chunk_write(&c,f); + lib3ds_chunk_write(&c,strm); } return(LIB3DS_TRUE); diff --git a/src/osgPlugins/3ds/background.h b/src/osgPlugins/3ds/background.h index 82a054483..1297140c4 100644 --- a/src/osgPlugins/3ds/background.h +++ b/src/osgPlugins/3ds/background.h @@ -27,6 +27,9 @@ #include "types.h" #endif +#include +using namespace std; + #ifdef __cplusplus extern "C" { #endif @@ -71,8 +74,8 @@ struct _Lib3dsBackground { Lib3dsGradient gradient; }; -extern LIB3DSAPI Lib3dsBool lib3ds_background_read(Lib3dsBackground *background, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_background_write(Lib3dsBackground *background, FILE *f); +extern LIB3DSAPI Lib3dsBool lib3ds_background_read(Lib3dsBackground *background, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_background_write(Lib3dsBackground *background, iostream *strm); #ifdef __cplusplus } diff --git a/src/osgPlugins/3ds/camera.cpp b/src/osgPlugins/3ds/camera.cpp index af789d070..26dc9cb08 100644 --- a/src/osgPlugins/3ds/camera.cpp +++ b/src/osgPlugins/3ds/camera.cpp @@ -96,27 +96,27 @@ lib3ds_camera_dump(Lib3dsCamera *camera) * \ingroup camera */ Lib3dsBool -lib3ds_camera_read(Lib3dsCamera *camera, FILE *f) +lib3ds_camera_read(Lib3dsCamera *camera, iostream *strm) { Lib3dsChunk c; Lib3dsWord chunk; - if (!lib3ds_chunk_read_start(&c, LIB3DS_N_CAMERA, f)) { + if (!lib3ds_chunk_read_start(&c, LIB3DS_N_CAMERA, strm)) { return(LIB3DS_FALSE); } { int i; for (i=0; i<3; ++i) { - camera->position[i]=lib3ds_float_read(f); + camera->position[i]=lib3ds_float_read(strm); } for (i=0; i<3; ++i) { - camera->target[i]=lib3ds_float_read(f); + camera->target[i]=lib3ds_float_read(strm); } } - camera->roll=lib3ds_float_read(f); + camera->roll=lib3ds_float_read(strm); { float s; - s=lib3ds_float_read(f); + s=lib3ds_float_read(strm); if (fabs(s)fov=45.0; } @@ -124,9 +124,9 @@ lib3ds_camera_read(Lib3dsCamera *camera, FILE *f) camera->fov=2400.0f/s; } } - lib3ds_chunk_read_tell(&c, f); + lib3ds_chunk_read_tell(&c, strm); - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { switch (chunk) { case LIB3DS_CAM_SEE_CONE: { @@ -135,8 +135,8 @@ lib3ds_camera_read(Lib3dsCamera *camera, FILE *f) break; case LIB3DS_CAM_RANGES: { - camera->near_range=lib3ds_float_read(f); - camera->far_range=lib3ds_float_read(f); + camera->near_range=lib3ds_float_read(strm); + camera->far_range=lib3ds_float_read(strm); } break; default: @@ -144,7 +144,7 @@ lib3ds_camera_read(Lib3dsCamera *camera, FILE *f) } } - lib3ds_chunk_read_end(&c, f); + lib3ds_chunk_read_end(&c, strm); return(LIB3DS_TRUE); } @@ -153,41 +153,41 @@ lib3ds_camera_read(Lib3dsCamera *camera, FILE *f) * \ingroup camera */ Lib3dsBool -lib3ds_camera_write(Lib3dsCamera *camera, FILE *f) +lib3ds_camera_write(Lib3dsCamera *camera, iostream *strm) { Lib3dsChunk c; c.chunk=LIB3DS_N_CAMERA; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - lib3ds_vector_write(camera->position, f); - lib3ds_vector_write(camera->target, f); - lib3ds_float_write(camera->roll, f); + lib3ds_vector_write(camera->position, strm); + lib3ds_vector_write(camera->target, strm); + lib3ds_float_write(camera->roll, strm); if (fabs(camera->fov)fov, f); + lib3ds_float_write(2400.0f/camera->fov, strm); } if (camera->see_cone) { Lib3dsChunk c; c.chunk=LIB3DS_CAM_SEE_CONE; c.size=6; - lib3ds_chunk_write(&c, f); + lib3ds_chunk_write(&c, strm); } { Lib3dsChunk c; c.chunk=LIB3DS_CAM_RANGES; c.size=14; - lib3ds_chunk_write(&c, f); - lib3ds_float_write(camera->near_range, f); - lib3ds_float_write(camera->far_range, f); + lib3ds_chunk_write(&c, strm); + lib3ds_float_write(camera->near_range, strm); + lib3ds_float_write(camera->far_range, strm); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } return(LIB3DS_TRUE); diff --git a/src/osgPlugins/3ds/camera.h b/src/osgPlugins/3ds/camera.h index 06467ca81..3a16084fa 100644 --- a/src/osgPlugins/3ds/camera.h +++ b/src/osgPlugins/3ds/camera.h @@ -27,6 +27,9 @@ #include "types.h" #endif +#include +using namespace std; + #ifdef __cplusplus extern "C" { #endif @@ -50,8 +53,8 @@ struct _Lib3dsCamera { extern LIB3DSAPI Lib3dsCamera* lib3ds_camera_new(const char *name); extern LIB3DSAPI void lib3ds_camera_free(Lib3dsCamera *mesh); extern LIB3DSAPI void lib3ds_camera_dump(Lib3dsCamera *camera); -extern LIB3DSAPI Lib3dsBool lib3ds_camera_read(Lib3dsCamera *camera, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_camera_write(Lib3dsCamera *camera, FILE *f); +extern LIB3DSAPI Lib3dsBool lib3ds_camera_read(Lib3dsCamera *camera, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_camera_write(Lib3dsCamera *camera, iostream *strm); #ifdef __cplusplus } diff --git a/src/osgPlugins/3ds/chunk.cpp b/src/osgPlugins/3ds/chunk.cpp index 457eb4fc9..5184403ad 100644 --- a/src/osgPlugins/3ds/chunk.cpp +++ b/src/osgPlugins/3ds/chunk.cpp @@ -93,20 +93,20 @@ lib3ds_chunk_enable_dump(Lib3dsBool enable, Lib3dsBool unknown) * \return True on success, False otherwise. */ Lib3dsBool -lib3ds_chunk_read(Lib3dsChunk *c, FILE *f) +lib3ds_chunk_read(Lib3dsChunk *c, iostream *strm) { ASSERT(c); - ASSERT(f); - c->cur=ftell(f); - c->chunk=lib3ds_word_read(f); - c->size=lib3ds_dword_read(f); + ASSERT(strm); + c->cur=strm->tellg(); + c->chunk=lib3ds_word_read(strm); + c->size=lib3ds_dword_read(strm); c->end=c->cur+c->size; c->cur+=6; - if (ferror(f) || (c->size<6)) { + + if (strm->fail()||(c->size<6)) { return(LIB3DS_FALSE); } return(LIB3DS_TRUE); - } @@ -114,11 +114,11 @@ lib3ds_chunk_read(Lib3dsChunk *c, FILE *f) * \ingroup chunk */ Lib3dsBool -lib3ds_chunk_read_start(Lib3dsChunk *c, Lib3dsWord chunk, FILE *f) +lib3ds_chunk_read_start(Lib3dsChunk *c, Lib3dsWord chunk, iostream *strm) { ASSERT(c); - ASSERT(f); - if (!lib3ds_chunk_read(c, f)) { + ASSERT(strm); + if (!lib3ds_chunk_read(c, strm)) { return(LIB3DS_FALSE); } lib3ds_chunk_debug_enter(c); @@ -130,9 +130,9 @@ lib3ds_chunk_read_start(Lib3dsChunk *c, Lib3dsWord chunk, FILE *f) * \ingroup chunk */ void -lib3ds_chunk_read_tell(Lib3dsChunk *c, FILE *f) +lib3ds_chunk_read_tell(Lib3dsChunk *c, iostream *strm) { - c->cur=ftell(f); + c->cur=strm->tellg(); } @@ -140,7 +140,7 @@ lib3ds_chunk_read_tell(Lib3dsChunk *c, FILE *f) * \ingroup chunk */ Lib3dsWord -lib3ds_chunk_read_next(Lib3dsChunk *c, FILE *f) +lib3ds_chunk_read_next(Lib3dsChunk *c, iostream *strm) { Lib3dsChunk d; @@ -149,9 +149,9 @@ lib3ds_chunk_read_next(Lib3dsChunk *c, FILE *f) return(0); } - fseek(f, (long)c->cur, SEEK_SET); - d.chunk=lib3ds_word_read(f); - d.size=lib3ds_dword_read(f); + strm->seekg((long)c->cur,ios_base::beg); + d.chunk=lib3ds_word_read(strm); + d.size=lib3ds_dword_read(strm); lib3ds_chunk_debug_dump(&d); c->cur+=d.size; return(d.chunk); @@ -162,9 +162,9 @@ lib3ds_chunk_read_next(Lib3dsChunk *c, FILE *f) * \ingroup chunk */ void -lib3ds_chunk_read_reset(Lib3dsChunk *, FILE *f) -{ - fseek(f, -6, SEEK_CUR); +lib3ds_chunk_read_reset(Lib3dsChunk *, iostream *strm) +{ + strm->seekg(-6,ios_base::cur); } @@ -172,10 +172,10 @@ lib3ds_chunk_read_reset(Lib3dsChunk *, FILE *f) * \ingroup chunk */ void -lib3ds_chunk_read_end(Lib3dsChunk *c, FILE *f) +lib3ds_chunk_read_end(Lib3dsChunk *c, iostream *strm) { lib3ds_chunk_debug_leave(c); - fseek(f, c->end, SEEK_SET); + strm->seekg(c->end,ios_base::beg); } @@ -190,14 +190,14 @@ lib3ds_chunk_read_end(Lib3dsChunk *c, FILE *f) * \return True on success, False otherwise. */ Lib3dsBool -lib3ds_chunk_write(Lib3dsChunk *c, FILE *f) +lib3ds_chunk_write(Lib3dsChunk *c, iostream *strm) { ASSERT(c); - if (!lib3ds_word_write(c->chunk, f)) { + if (!lib3ds_word_write(c->chunk, strm)) { LIB3DS_ERROR_LOG; return(LIB3DS_FALSE); } - if (!lib3ds_dword_write(c->size, f)) { + if (!lib3ds_dword_write(c->size, strm)) { LIB3DS_ERROR_LOG; return(LIB3DS_FALSE); } @@ -209,15 +209,15 @@ lib3ds_chunk_write(Lib3dsChunk *c, FILE *f) * \ingroup chunk */ Lib3dsBool -lib3ds_chunk_write_start(Lib3dsChunk *c, FILE *f) +lib3ds_chunk_write_start(Lib3dsChunk *c, iostream *strm) { ASSERT(c); c->size=0; - c->cur=ftell(f); - if (!lib3ds_word_write(c->chunk, f)) { + c->cur=strm->tellp(); + if (!lib3ds_word_write(c->chunk, strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_dword_write(c->size, f)) { + if (!lib3ds_dword_write(c->size, strm)) { return(LIB3DS_FALSE); } return(LIB3DS_TRUE); @@ -228,19 +228,19 @@ lib3ds_chunk_write_start(Lib3dsChunk *c, FILE *f) * \ingroup chunk */ Lib3dsBool -lib3ds_chunk_write_end(Lib3dsChunk *c, FILE *f) +lib3ds_chunk_write_end(Lib3dsChunk *c, iostream *strm) { ASSERT(c); - c->size=ftell(f) - c->cur; - fseek(f, c->cur+2, SEEK_SET); - if (!lib3ds_dword_write(c->size, f)) { + c->size=(Lib3dsDword)(strm->tellp()) - c->cur; + strm->seekp(c->cur+2,ios_base::beg); + if (!lib3ds_dword_write(c->size, strm)) { LIB3DS_ERROR_LOG; return(LIB3DS_FALSE); } c->cur+=c->size; - fseek(f, c->cur, SEEK_SET); - if (ferror(f)) { + strm->seekp(c->cur, ios_base::beg); + if (strm->fail()) { LIB3DS_ERROR_LOG; return(LIB3DS_FALSE); } diff --git a/src/osgPlugins/3ds/chunk.h b/src/osgPlugins/3ds/chunk.h index 5d061d760..2b317b930 100644 --- a/src/osgPlugins/3ds/chunk.h +++ b/src/osgPlugins/3ds/chunk.h @@ -27,10 +27,14 @@ #include "types.h" #endif +#include +using namespace std; + #ifdef __cplusplus extern "C" { #endif + typedef enum _Lib3dsChunks { LIB3DS_NULL_CHUNK =0x0000, LIB3DS_M3DMAGIC =0x4D4D, /*3DS file*/ @@ -267,15 +271,15 @@ typedef struct _Lib3dsChunk { } Lib3dsChunk; extern LIB3DSAPI void lib3ds_chunk_enable_dump(Lib3dsBool enable, Lib3dsBool unknown); -extern LIB3DSAPI Lib3dsBool lib3ds_chunk_read(Lib3dsChunk *c, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_chunk_read_start(Lib3dsChunk *c, Lib3dsWord chunk, FILE *f); -extern LIB3DSAPI void lib3ds_chunk_read_tell(Lib3dsChunk *c, FILE *f); -extern LIB3DSAPI Lib3dsWord lib3ds_chunk_read_next(Lib3dsChunk *c, FILE *f); -extern LIB3DSAPI void lib3ds_chunk_read_reset(Lib3dsChunk *c, FILE *f); -extern LIB3DSAPI void lib3ds_chunk_read_end(Lib3dsChunk *c, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_chunk_write(Lib3dsChunk *c, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_chunk_write_start(Lib3dsChunk *c, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_chunk_write_end(Lib3dsChunk *c, FILE *f); +extern LIB3DSAPI Lib3dsBool lib3ds_chunk_read(Lib3dsChunk *c, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_chunk_read_start(Lib3dsChunk *c, Lib3dsWord chunk, iostream *strm); +extern LIB3DSAPI void lib3ds_chunk_read_tell(Lib3dsChunk *c, iostream *strm); +extern LIB3DSAPI Lib3dsWord lib3ds_chunk_read_next(Lib3dsChunk *c, iostream *strm); +extern LIB3DSAPI void lib3ds_chunk_read_reset(Lib3dsChunk *c, iostream *strm); +extern LIB3DSAPI void lib3ds_chunk_read_end(Lib3dsChunk *c, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_chunk_write(Lib3dsChunk *c, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_chunk_write_start(Lib3dsChunk *c, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_chunk_write_end(Lib3dsChunk *c, iostream *strm); extern LIB3DSAPI const char* lib3ds_chunk_name(Lib3dsWord chunk); extern LIB3DSAPI void lib3ds_chunk_unknown(Lib3dsWord chunk); extern LIB3DSAPI void lib3ds_chunk_dump_info(const char *format, ...); diff --git a/src/osgPlugins/3ds/file.cpp b/src/osgPlugins/3ds/file.cpp index bd64f5af3..34d82d435 100644 --- a/src/osgPlugins/3ds/file.cpp +++ b/src/osgPlugins/3ds/file.cpp @@ -37,8 +37,11 @@ #include #endif -#include - +#include +#include +//#include +#include +#include /*! * \defgroup file Files @@ -65,30 +68,43 @@ * \ingroup file */ Lib3dsFile* -lib3ds_file_load(const char *filename) +lib3ds_file_load(const char *filename, const osgDB::ReaderWriter::Options* options) { - FILE *f; - Lib3dsFile *file; - - f=osgDB::fopen(filename, "rb"); - if (!f) { - return(0); + Lib3dsFile *file = NULL; + std::stringstream bufferedStream; + ifstream inputStream(filename,ios::in|ios::binary); + if (!inputStream.fail()) + { + bufferedStream.operator<<(inputStream.rdbuf()); + bufferedStream.seekp(ios_base::beg); + file = lib3ds_stream_load(&bufferedStream); + inputStream.close(); } - file=lib3ds_file_new(); - if (!file) { - fclose(f); - return(0); - } - - if (!lib3ds_file_read(file, f)) { - free(file); - fclose(f); - return(0); - } - fclose(f); return(file); } +Lib3dsFile* +lib3ds_stream_load(iostream * strm) +{ + Lib3dsFile *file = NULL; + if (strm) + { + file=lib3ds_file_new(); + if (file) + { + if (!lib3ds_file_read(file,strm)) + { + free(file); + file = NULL; + } + } + else + file = NULL; + } + return(file); +} + + /*! * Saves a .3DS file from memory to disk. @@ -106,18 +122,16 @@ lib3ds_file_load(const char *filename) Lib3dsBool lib3ds_file_save(Lib3dsFile *file, const char *filename) { - FILE *f; + fstream strm; - f=osgDB::fopen(filename, "wb"); - if (!f) { - return(LIB3DS_FALSE); - } + strm.open(filename,ios_base::out | ios_base::binary); + if (strm.fail()) return (LIB3DS_FALSE); - if (!lib3ds_file_write(file, f)) { - fclose(f); + if (!lib3ds_file_write(file, &strm)) { + strm.close(); return(LIB3DS_FALSE); } - fclose(f); + strm.close(); return(LIB3DS_TRUE); } @@ -213,22 +227,22 @@ lib3ds_file_eval(Lib3dsFile *file, Lib3dsFloat t) static Lib3dsBool -named_object_read(Lib3dsFile *file, FILE *f) +named_object_read(Lib3dsFile *file, iostream *strm) { Lib3dsChunk c; char name[64]; Lib3dsWord chunk; - if (!lib3ds_chunk_read_start(&c, LIB3DS_NAMED_OBJECT, f)) { + if (!lib3ds_chunk_read_start(&c, LIB3DS_NAMED_OBJECT, strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_string_read(name, 64, f)) { + if (!lib3ds_string_read(name, 64, strm)) { return(LIB3DS_FALSE); } lib3ds_chunk_dump_info(" NAME=%s", name); - lib3ds_chunk_read_tell(&c, f); + lib3ds_chunk_read_tell(&c, strm); - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { switch (chunk) { case LIB3DS_N_TRI_OBJECT: { @@ -238,8 +252,8 @@ named_object_read(Lib3dsFile *file, FILE *f) if (!mesh) { return(LIB3DS_FALSE); } - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_mesh_read(mesh, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!lib3ds_mesh_read(mesh, strm)) { return(LIB3DS_FALSE); } lib3ds_file_insert_mesh(file, mesh); @@ -253,8 +267,8 @@ named_object_read(Lib3dsFile *file, FILE *f) if (!camera) { return(LIB3DS_FALSE); } - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_camera_read(camera, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!lib3ds_camera_read(camera, strm)) { return(LIB3DS_FALSE); } lib3ds_file_insert_camera(file, camera); @@ -268,8 +282,8 @@ named_object_read(Lib3dsFile *file, FILE *f) if (!light) { return(LIB3DS_FALSE); } - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_light_read(light, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!lib3ds_light_read(light, strm)) { return(LIB3DS_FALSE); } lib3ds_file_insert_light(file, light); @@ -280,29 +294,29 @@ named_object_read(Lib3dsFile *file, FILE *f) } } - lib3ds_chunk_read_end(&c, f); + lib3ds_chunk_read_end(&c, strm); return(LIB3DS_TRUE); } static Lib3dsBool -ambient_read(Lib3dsFile *file, FILE *f) +ambient_read(Lib3dsFile *file, iostream *strm) { Lib3dsChunk c; Lib3dsWord chunk; Lib3dsBool have_lin=LIB3DS_FALSE; - if (!lib3ds_chunk_read_start(&c, LIB3DS_AMBIENT_LIGHT, f)) { + if (!lib3ds_chunk_read_start(&c, LIB3DS_AMBIENT_LIGHT, strm)) { return(LIB3DS_FALSE); } - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { switch (chunk) { case LIB3DS_LIN_COLOR_F: { int i; for (i=0; i<3; ++i) { - file->ambient[i]=lib3ds_float_read(f); + file->ambient[i]=lib3ds_float_read(strm); } } have_lin=LIB3DS_TRUE; @@ -314,7 +328,7 @@ ambient_read(Lib3dsFile *file, FILE *f) if (!have_lin) { int i; for (i=0; i<3; ++i) { - file->ambient[i]=lib3ds_float_read(f); + file->ambient[i]=lib3ds_float_read(strm); } } } @@ -324,31 +338,31 @@ ambient_read(Lib3dsFile *file, FILE *f) } } - lib3ds_chunk_read_end(&c, f); + lib3ds_chunk_read_end(&c, strm); return(LIB3DS_TRUE); } static Lib3dsBool -mdata_read(Lib3dsFile *file, FILE *f) +mdata_read(Lib3dsFile *file, iostream *strm) { Lib3dsChunk c; Lib3dsWord chunk; - if (!lib3ds_chunk_read_start(&c, LIB3DS_MDATA, f)) { + if (!lib3ds_chunk_read_start(&c, LIB3DS_MDATA, strm)) { return(LIB3DS_FALSE); } - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { switch (chunk) { case LIB3DS_MESH_VERSION: { - file->mesh_version=lib3ds_intd_read(f); + file->mesh_version=lib3ds_intd_read(strm); } break; case LIB3DS_MASTER_SCALE: { - file->master_scale=lib3ds_float_read(f); + file->master_scale=lib3ds_float_read(strm); } break; case LIB3DS_SHADOW_MAP_SIZE: @@ -359,8 +373,8 @@ mdata_read(Lib3dsFile *file, FILE *f) case LIB3DS_SHADOW_FILTER: case LIB3DS_RAY_BIAS: { - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_shadow_read(&file->shadow, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!lib3ds_shadow_read(&file->shadow, strm)) { return(LIB3DS_FALSE); } } @@ -368,8 +382,8 @@ mdata_read(Lib3dsFile *file, FILE *f) case LIB3DS_VIEWPORT_LAYOUT: case LIB3DS_DEFAULT_VIEW: { - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_viewport_read(&file->viewport, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!lib3ds_viewport_read(&file->viewport, strm)) { return(LIB3DS_FALSE); } } @@ -378,14 +392,14 @@ mdata_read(Lib3dsFile *file, FILE *f) { int i; for (i=0; i<3; ++i) { - file->construction_plane[i]=lib3ds_float_read(f); + file->construction_plane[i]=lib3ds_float_read(strm); } } break; case LIB3DS_AMBIENT_LIGHT: { - lib3ds_chunk_read_reset(&c, f); - if (!ambient_read(file, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!ambient_read(file, strm)) { return(LIB3DS_FALSE); } } @@ -397,8 +411,8 @@ mdata_read(Lib3dsFile *file, FILE *f) case LIB3DS_USE_SOLID_BGND: case LIB3DS_USE_V_GRADIENT: { - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_background_read(&file->background, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!lib3ds_background_read(&file->background, strm)) { return(LIB3DS_FALSE); } } @@ -410,8 +424,8 @@ mdata_read(Lib3dsFile *file, FILE *f) case LIB3DS_USE_LAYER_FOG: case LIB3DS_USE_DISTANCE_CUE: { - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_atmosphere_read(&file->atmosphere, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!lib3ds_atmosphere_read(&file->atmosphere, strm)) { return(LIB3DS_FALSE); } } @@ -424,8 +438,8 @@ mdata_read(Lib3dsFile *file, FILE *f) if (!material) { return(LIB3DS_FALSE); } - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_material_read(material, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!lib3ds_material_read(material, strm)) { return(LIB3DS_FALSE); } lib3ds_file_insert_material(file, material); @@ -433,8 +447,8 @@ mdata_read(Lib3dsFile *file, FILE *f) break; case LIB3DS_NAMED_OBJECT: { - lib3ds_chunk_read_reset(&c, f); - if (!named_object_read(file, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!named_object_read(file, strm)) { return(LIB3DS_FALSE); } } @@ -444,48 +458,48 @@ mdata_read(Lib3dsFile *file, FILE *f) } } - lib3ds_chunk_read_end(&c, f); + lib3ds_chunk_read_end(&c, strm); return(LIB3DS_TRUE); } static Lib3dsBool -kfdata_read(Lib3dsFile *file, FILE *f) +kfdata_read(Lib3dsFile *file, iostream *strm) { Lib3dsChunk c; Lib3dsWord chunk; - if (!lib3ds_chunk_read_start(&c, LIB3DS_KFDATA, f)) { + if (!lib3ds_chunk_read_start(&c, LIB3DS_KFDATA, strm)) { return(LIB3DS_FALSE); } - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { switch (chunk) { case LIB3DS_KFHDR: { - file->keyf_revision=lib3ds_word_read(f); - if (!lib3ds_string_read(file->name, 12+1, f)) { + file->keyf_revision=lib3ds_word_read(strm); + if (!lib3ds_string_read(file->name, 12+1, strm)) { return(LIB3DS_FALSE); } - file->frames=lib3ds_intd_read(f); + file->frames=lib3ds_intd_read(strm); } break; case LIB3DS_KFSEG: { - file->segment_from=lib3ds_intd_read(f); - file->segment_to=lib3ds_intd_read(f); + file->segment_from=lib3ds_intd_read(strm); + file->segment_to=lib3ds_intd_read(strm); } break; case LIB3DS_KFCURTIME: { - file->current_frame=lib3ds_intd_read(f); + file->current_frame=lib3ds_intd_read(strm); } break; case LIB3DS_VIEWPORT_LAYOUT: case LIB3DS_DEFAULT_VIEW: { - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_viewport_read(&file->viewport_keyf, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!lib3ds_viewport_read(&file->viewport_keyf, strm)) { return(LIB3DS_FALSE); } } @@ -498,8 +512,8 @@ kfdata_read(Lib3dsFile *file, FILE *f) if (!node) { return(LIB3DS_FALSE); } - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_node_read(node, file, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!lib3ds_node_read(node, file, strm)) { return(LIB3DS_FALSE); } lib3ds_file_insert_node(file, node); @@ -513,8 +527,8 @@ kfdata_read(Lib3dsFile *file, FILE *f) if (!node) { return(LIB3DS_FALSE); } - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_node_read(node, file, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!lib3ds_node_read(node, file, strm)) { return(LIB3DS_FALSE); } lib3ds_file_insert_node(file, node); @@ -528,8 +542,8 @@ kfdata_read(Lib3dsFile *file, FILE *f) if (!node) { return(LIB3DS_FALSE); } - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_node_read(node, file, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!lib3ds_node_read(node, file, strm)) { return(LIB3DS_FALSE); } lib3ds_file_insert_node(file, node); @@ -543,8 +557,8 @@ kfdata_read(Lib3dsFile *file, FILE *f) if (!node) { return(LIB3DS_FALSE); } - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_node_read(node, file, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!lib3ds_node_read(node, file, strm)) { return(LIB3DS_FALSE); } lib3ds_file_insert_node(file, node); @@ -559,8 +573,8 @@ kfdata_read(Lib3dsFile *file, FILE *f) if (!node) { return(LIB3DS_FALSE); } - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_node_read(node, file, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!lib3ds_node_read(node, file, strm)) { return(LIB3DS_FALSE); } lib3ds_file_insert_node(file, node); @@ -574,8 +588,8 @@ kfdata_read(Lib3dsFile *file, FILE *f) if (!node) { return(LIB3DS_FALSE); } - lib3ds_chunk_read_reset(&c, f); - if (!lib3ds_node_read(node, file, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!lib3ds_node_read(node, file, strm)) { return(LIB3DS_FALSE); } lib3ds_file_insert_node(file, node); @@ -586,7 +600,7 @@ kfdata_read(Lib3dsFile *file, FILE *f) } } - lib3ds_chunk_read_end(&c, f); + lib3ds_chunk_read_end(&c, strm); return(LIB3DS_TRUE); } @@ -595,19 +609,19 @@ kfdata_read(Lib3dsFile *file, FILE *f) * \ingroup file */ Lib3dsBool -lib3ds_file_read(Lib3dsFile *file, FILE *f) +lib3ds_file_read(Lib3dsFile *file, iostream *strm) { Lib3dsChunk c; Lib3dsWord chunk; - if (!lib3ds_chunk_read_start(&c, 0, f)) { + if (!lib3ds_chunk_read_start(&c, 0, strm)) { return(LIB3DS_FALSE); } switch (c.chunk) { case LIB3DS_MDATA: { - lib3ds_chunk_read_reset(&c, f); - if (!mdata_read(file, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!mdata_read(file, strm)) { return(LIB3DS_FALSE); } } @@ -616,25 +630,25 @@ lib3ds_file_read(Lib3dsFile *file, FILE *f) case LIB3DS_MLIBMAGIC: case LIB3DS_CMAGIC: { - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { switch (chunk) { case LIB3DS_M3D_VERSION: { - file->mesh_version=lib3ds_dword_read(f); + file->mesh_version=lib3ds_dword_read(strm); } break; case LIB3DS_MDATA: { - lib3ds_chunk_read_reset(&c, f); - if (!mdata_read(file, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!mdata_read(file, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_KFDATA: { - lib3ds_chunk_read_reset(&c, f); - if (!kfdata_read(file, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!kfdata_read(file, strm)) { return(LIB3DS_FALSE); } } @@ -650,36 +664,36 @@ lib3ds_file_read(Lib3dsFile *file, FILE *f) return(LIB3DS_FALSE); } - lib3ds_chunk_read_end(&c, f); + lib3ds_chunk_read_end(&c, strm); return(LIB3DS_TRUE); } static Lib3dsBool -colorf_write(Lib3dsRgba rgb, FILE *f) +colorf_write(Lib3dsRgba rgb, iostream *strm) { Lib3dsChunk c; c.chunk=LIB3DS_COLOR_F; c.size=18; - lib3ds_chunk_write(&c,f); - lib3ds_rgb_write(rgb,f); + lib3ds_chunk_write(&c,strm); + lib3ds_rgb_write(rgb,strm); c.chunk=LIB3DS_LIN_COLOR_F; c.size=18; - lib3ds_chunk_write(&c,f); - lib3ds_rgb_write(rgb,f); + lib3ds_chunk_write(&c,strm); + lib3ds_rgb_write(rgb,strm); return(LIB3DS_TRUE); } static Lib3dsBool -mdata_write(Lib3dsFile *file, FILE *f) +mdata_write(Lib3dsFile *file, iostream *strm) { Lib3dsChunk c; c.chunk=LIB3DS_MDATA; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } @@ -687,15 +701,15 @@ mdata_write(Lib3dsFile *file, FILE *f) Lib3dsChunk c; c.chunk=LIB3DS_MESH_VERSION; c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_intd_write(file->mesh_version,f); + lib3ds_chunk_write(&c,strm); + lib3ds_intd_write(file->mesh_version,strm); } { /*---- LIB3DS_MASTER_SCALE ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MASTER_SCALE; c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(file->master_scale,f); + lib3ds_chunk_write(&c,strm); + lib3ds_float_write(file->master_scale,strm); } { /*---- LIB3DS_O_CONSTS ----*/ int i; @@ -708,8 +722,8 @@ mdata_write(Lib3dsFile *file, FILE *f) Lib3dsChunk c; c.chunk=LIB3DS_O_CONSTS; c.size=18; - lib3ds_chunk_write(&c,f); - lib3ds_vector_write(file->construction_plane,f); + lib3ds_chunk_write(&c,strm); + lib3ds_vector_write(file->construction_plane,strm); } } @@ -724,18 +738,18 @@ mdata_write(Lib3dsFile *file, FILE *f) Lib3dsChunk c; c.chunk=LIB3DS_AMBIENT_LIGHT; c.size=42; - lib3ds_chunk_write(&c,f); - colorf_write(file->ambient,f); + lib3ds_chunk_write(&c,strm); + colorf_write(file->ambient,strm); } } - lib3ds_background_write(&file->background, f); - lib3ds_atmosphere_write(&file->atmosphere, f); - lib3ds_shadow_write(&file->shadow, f); - lib3ds_viewport_write(&file->viewport, f); + lib3ds_background_write(&file->background, strm); + lib3ds_atmosphere_write(&file->atmosphere, strm); + lib3ds_shadow_write(&file->shadow, strm); + lib3ds_viewport_write(&file->viewport, strm); { Lib3dsMaterial *p; for (p=file->materials; p!=0; p=p->next) { - if (!lib3ds_material_write(p,f)) { + if (!lib3ds_material_write(p,strm)) { return(LIB3DS_FALSE); } } @@ -746,12 +760,12 @@ mdata_write(Lib3dsFile *file, FILE *f) for (p=file->cameras; p!=0; p=p->next) { c.chunk=LIB3DS_NAMED_OBJECT; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - lib3ds_string_write(p->name,f); - lib3ds_camera_write(p,f); - if (!lib3ds_chunk_write_end(&c,f)) { + lib3ds_string_write(p->name,strm); + lib3ds_camera_write(p,strm); + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } } @@ -762,12 +776,12 @@ mdata_write(Lib3dsFile *file, FILE *f) for (p=file->lights; p!=0; p=p->next) { c.chunk=LIB3DS_NAMED_OBJECT; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - lib3ds_string_write(p->name,f); - lib3ds_light_write(p,f); - if (!lib3ds_chunk_write_end(&c,f)) { + lib3ds_string_write(p->name,strm); + lib3ds_light_write(p,strm); + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } } @@ -778,18 +792,18 @@ mdata_write(Lib3dsFile *file, FILE *f) for (p=file->meshes; p!=0; p=p->next) { c.chunk=LIB3DS_NAMED_OBJECT; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - lib3ds_string_write(p->name,f); - lib3ds_mesh_write(p,f); - if (!lib3ds_chunk_write_end(&c,f)) { + lib3ds_string_write(p->name,strm); + lib3ds_mesh_write(p,strm); + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } } } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } return(LIB3DS_TRUE); @@ -798,15 +812,15 @@ mdata_write(Lib3dsFile *file, FILE *f) static Lib3dsBool -nodes_write(Lib3dsNode *node, Lib3dsFile *file, FILE *f) +nodes_write(Lib3dsNode *node, Lib3dsFile *file, iostream *strm) { { Lib3dsNode *p; for (p=node->childs; p!=0; p=p->next) { - if (!lib3ds_node_write(p, file, f)) { + if (!lib3ds_node_write(p, file, strm)) { return(LIB3DS_FALSE); } - nodes_write(p, file, f); + nodes_write(p, file, strm); } } return(LIB3DS_TRUE); @@ -814,12 +828,12 @@ nodes_write(Lib3dsNode *node, Lib3dsFile *file, FILE *f) static Lib3dsBool -kfdata_write(Lib3dsFile *file, FILE *f) +kfdata_write(Lib3dsFile *file, iostream *strm) { Lib3dsChunk c; c.chunk=LIB3DS_KFDATA; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } @@ -827,41 +841,41 @@ kfdata_write(Lib3dsFile *file, FILE *f) Lib3dsChunk c; c.chunk=LIB3DS_KFHDR; c.size=6 + 2 + strlen(file->name)+1 +4; - lib3ds_chunk_write(&c,f); - lib3ds_intw_write(file->keyf_revision,f); - lib3ds_string_write(file->name, f); - lib3ds_intd_write(file->frames, f); + lib3ds_chunk_write(&c,strm); + lib3ds_intw_write(file->keyf_revision,strm); + lib3ds_string_write(file->name, strm); + lib3ds_intd_write(file->frames, strm); } { /*---- LIB3DS_KFSEG ----*/ Lib3dsChunk c; c.chunk=LIB3DS_KFSEG; c.size=14; - lib3ds_chunk_write(&c,f); - lib3ds_intd_write(file->segment_from,f); - lib3ds_intd_write(file->segment_to,f); + lib3ds_chunk_write(&c,strm); + lib3ds_intd_write(file->segment_from,strm); + lib3ds_intd_write(file->segment_to,strm); } { /*---- LIB3DS_KFCURTIME ----*/ Lib3dsChunk c; c.chunk=LIB3DS_KFCURTIME; c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_intd_write(file->current_frame,f); + lib3ds_chunk_write(&c,strm); + lib3ds_intd_write(file->current_frame,strm); } - lib3ds_viewport_write(&file->viewport_keyf, f); + lib3ds_viewport_write(&file->viewport_keyf, strm); { Lib3dsNode *p; for (p=file->nodes; p!=0; p=p->next) { - if (!lib3ds_node_write(p, file, f)) { + if (!lib3ds_node_write(p, file, strm)) { return(LIB3DS_FALSE); } - if (!nodes_write(p, file, f)) { + if (!nodes_write(p, file, strm)) { return(LIB3DS_FALSE); } } } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } return(LIB3DS_TRUE); @@ -872,12 +886,12 @@ kfdata_write(Lib3dsFile *file, FILE *f) * \ingroup file */ Lib3dsBool -lib3ds_file_write(Lib3dsFile *file, FILE *f) +lib3ds_file_write(Lib3dsFile *file, iostream *strm) { Lib3dsChunk c; c.chunk=LIB3DS_M3DMAGIC; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { LIB3DS_ERROR_LOG; return(LIB3DS_FALSE); } @@ -887,18 +901,18 @@ lib3ds_file_write(Lib3dsFile *file, FILE *f) c.chunk=LIB3DS_M3D_VERSION; c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_dword_write(file->mesh_version, f); + lib3ds_chunk_write(&c,strm); + lib3ds_dword_write(file->mesh_version, strm); } - if (!mdata_write(file, f)) { + if (!mdata_write(file, strm)) { return(LIB3DS_FALSE); } - if (!kfdata_write(file, f)) { + if (!kfdata_write(file, strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } return(LIB3DS_TRUE); diff --git a/src/osgPlugins/3ds/file.h b/src/osgPlugins/3ds/file.h index d6535a347..67f8bc1fb 100644 --- a/src/osgPlugins/3ds/file.h +++ b/src/osgPlugins/3ds/file.h @@ -35,11 +35,16 @@ #ifndef INCLUDED_LIB3DS_VIEWPORT_H #include "viewport.h" #endif +#include +#include +using namespace std; #ifdef __cplusplus extern "C" { #endif + + /*! * 3ds file structure * \ingroup file @@ -67,13 +72,14 @@ struct _Lib3dsFile { Lib3dsNode *nodes; }; -extern LIB3DSAPI Lib3dsFile* lib3ds_file_load(const char *filename); +extern LIB3DSAPI Lib3dsFile* lib3ds_file_load(const char *filename, const osgDB::ReaderWriter::Options* options); +extern LIB3DSAPI Lib3dsFile* lib3ds_stream_load(iostream * strm); extern LIB3DSAPI Lib3dsBool lib3ds_file_save(Lib3dsFile *file, const char *filename); extern LIB3DSAPI Lib3dsFile* lib3ds_file_new(); extern LIB3DSAPI void lib3ds_file_free(Lib3dsFile *file); extern LIB3DSAPI void lib3ds_file_eval(Lib3dsFile *file, Lib3dsFloat t); -extern LIB3DSAPI Lib3dsBool lib3ds_file_read(Lib3dsFile *file, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_file_write(Lib3dsFile *file, FILE *f); +extern LIB3DSAPI Lib3dsBool lib3ds_file_read(Lib3dsFile *file, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_file_write(Lib3dsFile *file, iostream *strm); extern LIB3DSAPI void lib3ds_file_insert_material(Lib3dsFile *file, Lib3dsMaterial *material); extern LIB3DSAPI void lib3ds_file_remove_material(Lib3dsFile *file, Lib3dsMaterial *material); extern LIB3DSAPI Lib3dsMaterial* lib3ds_file_material_by_name(Lib3dsFile *file, const char *name); diff --git a/src/osgPlugins/3ds/light.cpp b/src/osgPlugins/3ds/light.cpp index d82bcf556..8e4a47eb0 100644 --- a/src/osgPlugins/3ds/light.cpp +++ b/src/osgPlugins/3ds/light.cpp @@ -121,28 +121,28 @@ lib3ds_light_dump(Lib3dsLight *light) * \ingroup light */ static Lib3dsBool -spotlight_read(Lib3dsLight *light, FILE *f) +spotlight_read(Lib3dsLight *light, iostream *strm) { Lib3dsChunk c; Lib3dsWord chunk; int i; - if (!lib3ds_chunk_read_start(&c, LIB3DS_DL_SPOTLIGHT, f)) { + if (!lib3ds_chunk_read_start(&c, LIB3DS_DL_SPOTLIGHT, strm)) { return(LIB3DS_FALSE); } light->spot_light=LIB3DS_TRUE; for (i=0; i<3; ++i) { - light->spot[i]=lib3ds_float_read(f); + light->spot[i]=lib3ds_float_read(strm); } - light->hot_spot = lib3ds_float_read(f); - light->fall_off = lib3ds_float_read(f); - lib3ds_chunk_read_tell(&c, f); + light->hot_spot = lib3ds_float_read(strm); + light->fall_off = lib3ds_float_read(strm); + lib3ds_chunk_read_tell(&c, strm); - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { switch (chunk) { case LIB3DS_DL_SPOT_ROLL: { - light->roll=lib3ds_float_read(f); + light->roll=lib3ds_float_read(strm); } break; case LIB3DS_DL_SHADOWED: @@ -152,9 +152,9 @@ spotlight_read(Lib3dsLight *light, FILE *f) break; case LIB3DS_DL_LOCAL_SHADOW2: { - light->shadow_bias=lib3ds_float_read(f); - light->shadow_filter=lib3ds_float_read(f); - light->shadow_size=lib3ds_intw_read(f); + light->shadow_bias=lib3ds_float_read(strm); + light->shadow_filter=lib3ds_float_read(strm); + light->shadow_size=lib3ds_intw_read(strm); } break; case LIB3DS_DL_SEE_CONE: @@ -169,13 +169,13 @@ spotlight_read(Lib3dsLight *light, FILE *f) break; case LIB3DS_DL_SPOT_ASPECT: { - light->spot_aspect=lib3ds_float_read(f); + light->spot_aspect=lib3ds_float_read(strm); } break; case LIB3DS_DL_SPOT_PROJECTOR: { light->use_projector=LIB3DS_TRUE; - if (!lib3ds_string_read(light->projector, 64, f)) { + if (!lib3ds_string_read(light->projector, 64, strm)) { return(LIB3DS_FALSE); } } @@ -186,7 +186,7 @@ spotlight_read(Lib3dsLight *light, FILE *f) break; case LIB3DS_DL_RAY_BIAS: { - light->ray_bias=lib3ds_float_read(f); + light->ray_bias=lib3ds_float_read(strm); } break; case LIB3DS_DL_RAYSHAD: @@ -199,7 +199,7 @@ spotlight_read(Lib3dsLight *light, FILE *f) } } - lib3ds_chunk_read_end(&c, f); + lib3ds_chunk_read_end(&c, strm); return(LIB3DS_TRUE); } @@ -208,29 +208,29 @@ spotlight_read(Lib3dsLight *light, FILE *f) * \ingroup light */ Lib3dsBool -lib3ds_light_read(Lib3dsLight *light, FILE *f) +lib3ds_light_read(Lib3dsLight *light, iostream * strm) { Lib3dsChunk c; Lib3dsWord chunk; - if (!lib3ds_chunk_read_start(&c, LIB3DS_N_DIRECT_LIGHT, f)) { + if (!lib3ds_chunk_read_start(&c, LIB3DS_N_DIRECT_LIGHT, strm)) { return(LIB3DS_FALSE); } { int i; for (i=0; i<3; ++i) { - light->position[i]=lib3ds_float_read(f); + light->position[i]=lib3ds_float_read(strm); } } - lib3ds_chunk_read_tell(&c, f); + lib3ds_chunk_read_tell(&c, strm); - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { switch (chunk) { case LIB3DS_COLOR_F: { int i; for (i=0; i<3; ++i) { - light->color[i]=lib3ds_float_read(f); + light->color[i]=lib3ds_float_read(strm); } } break; @@ -241,17 +241,17 @@ lib3ds_light_read(Lib3dsLight *light, FILE *f) break; case LIB3DS_DL_OUTER_RANGE: { - light->outer_range=lib3ds_float_read(f); + light->outer_range=lib3ds_float_read(strm); } break; case LIB3DS_DL_INNER_RANGE: { - light->inner_range=lib3ds_float_read(f); + light->inner_range=lib3ds_float_read(strm); } break; case LIB3DS_DL_MULTIPLIER: { - light->multiplier=lib3ds_float_read(f); + light->multiplier=lib3ds_float_read(strm); } break; case LIB3DS_DL_EXCLUDE: @@ -261,13 +261,13 @@ lib3ds_light_read(Lib3dsLight *light, FILE *f) } case LIB3DS_DL_ATTENUATE: { - light->attenuation=lib3ds_float_read(f); + light->attenuation=lib3ds_float_read(strm); } break; case LIB3DS_DL_SPOTLIGHT: { - lib3ds_chunk_read_reset(&c, f); - if (!spotlight_read(light, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!spotlight_read(light, strm)) { return(LIB3DS_FALSE); } } @@ -277,7 +277,7 @@ lib3ds_light_read(Lib3dsLight *light, FILE *f) } } - lib3ds_chunk_read_end(&c, f); + lib3ds_chunk_read_end(&c, strm); return(LIB3DS_TRUE); } @@ -286,79 +286,79 @@ lib3ds_light_read(Lib3dsLight *light, FILE *f) * \ingroup light */ Lib3dsBool -lib3ds_light_write(Lib3dsLight *light, FILE *f) +lib3ds_light_write(Lib3dsLight *light, iostream *strm) { Lib3dsChunk c; c.chunk=LIB3DS_N_DIRECT_LIGHT; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - lib3ds_vector_write(light->position, f); + lib3ds_vector_write(light->position, strm); { /*---- LIB3DS_COLOR_F ----*/ Lib3dsChunk c; c.chunk=LIB3DS_COLOR_F; c.size=18; - lib3ds_chunk_write(&c, f); - lib3ds_rgb_write(light->color,f); + lib3ds_chunk_write(&c, strm); + lib3ds_rgb_write(light->color,strm); } if (light->off) { /*---- LIB3DS_DL_OFF ----*/ Lib3dsChunk c; c.chunk=LIB3DS_DL_OFF; c.size=6; - lib3ds_chunk_write(&c, f); + lib3ds_chunk_write(&c, strm); } { /*---- LIB3DS_DL_OUTER_RANGE ----*/ Lib3dsChunk c; c.chunk=LIB3DS_DL_OUTER_RANGE; c.size=10; - lib3ds_chunk_write(&c, f); - lib3ds_float_write(light->outer_range,f); + lib3ds_chunk_write(&c, strm); + lib3ds_float_write(light->outer_range,strm); } { /*---- LIB3DS_DL_INNER_RANGE ----*/ Lib3dsChunk c; c.chunk=LIB3DS_DL_INNER_RANGE; c.size=10; - lib3ds_chunk_write(&c, f); - lib3ds_float_write(light->inner_range,f); + lib3ds_chunk_write(&c, strm); + lib3ds_float_write(light->inner_range,strm); } { /*---- LIB3DS_DL_MULTIPLIER ----*/ Lib3dsChunk c; c.chunk=LIB3DS_DL_MULTIPLIER; c.size=10; - lib3ds_chunk_write(&c, f); - lib3ds_float_write(light->multiplier, f); + lib3ds_chunk_write(&c, strm); + lib3ds_float_write(light->multiplier, strm); } if (light->attenuation) { /*---- LIB3DS_DL_ATTENUATE ----*/ Lib3dsChunk c; c.chunk=LIB3DS_DL_ATTENUATE; c.size=6; - lib3ds_chunk_write(&c, f); + lib3ds_chunk_write(&c, strm); } if (light->spot_light) { Lib3dsChunk c; c.chunk=LIB3DS_DL_SPOTLIGHT; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - lib3ds_vector_write(light->spot, f); - lib3ds_float_write(light->hot_spot, f); - lib3ds_float_write(light->fall_off, f); + lib3ds_vector_write(light->spot, strm); + lib3ds_float_write(light->hot_spot, strm); + lib3ds_float_write(light->fall_off, strm); { /*---- LIB3DS_DL_SPOT_ROLL ----*/ Lib3dsChunk c; c.chunk=LIB3DS_DL_SPOT_ROLL; c.size=10; - lib3ds_chunk_write(&c, f); - lib3ds_float_write(light->roll,f); + lib3ds_chunk_write(&c, strm); + lib3ds_float_write(light->roll,strm); } if (light->shadowed) { /*---- LIB3DS_DL_SHADOWED ----*/ Lib3dsChunk c; c.chunk=LIB3DS_DL_SHADOWED; c.size=6; - lib3ds_chunk_write(&c, f); + lib3ds_chunk_write(&c, strm); } if ((fabs(light->shadow_bias)>LIB3DS_EPSILON) || (fabs(light->shadow_filter)>LIB3DS_EPSILON) || @@ -366,61 +366,61 @@ lib3ds_light_write(Lib3dsLight *light, FILE *f) Lib3dsChunk c; c.chunk=LIB3DS_DL_LOCAL_SHADOW2; c.size=16; - lib3ds_chunk_write(&c, f); - lib3ds_float_write(light->shadow_bias,f); - lib3ds_float_write(light->shadow_filter,f); - lib3ds_intw_write(light->shadow_size,f); + lib3ds_chunk_write(&c, strm); + lib3ds_float_write(light->shadow_bias,strm); + lib3ds_float_write(light->shadow_filter,strm); + lib3ds_intw_write(light->shadow_size,strm); } if (light->see_cone) { /*---- LIB3DS_DL_SEE_CONE ----*/ Lib3dsChunk c; c.chunk=LIB3DS_DL_SEE_CONE; c.size=6; - lib3ds_chunk_write(&c, f); + lib3ds_chunk_write(&c, strm); } if (light->rectangular_spot) { /*---- LIB3DS_DL_SPOT_RECTANGULAR ----*/ Lib3dsChunk c; c.chunk=LIB3DS_DL_SPOT_RECTANGULAR; c.size=6; - lib3ds_chunk_write(&c, f); + lib3ds_chunk_write(&c, strm); } if (fabs(light->spot_aspect)>LIB3DS_EPSILON) { /*---- LIB3DS_DL_SPOT_ASPECT ----*/ Lib3dsChunk c; c.chunk=LIB3DS_DL_SPOT_ASPECT; c.size=10; - lib3ds_chunk_write(&c, f); - lib3ds_float_write(light->spot_aspect,f); + lib3ds_chunk_write(&c, strm); + lib3ds_float_write(light->spot_aspect,strm); } if (light->use_projector) { /*---- LIB3DS_DL_SPOT_PROJECTOR ----*/ Lib3dsChunk c; c.chunk=LIB3DS_DL_SPOT_PROJECTOR; c.size=10; - lib3ds_chunk_write(&c, f); - lib3ds_string_write(light->projector,f); + lib3ds_chunk_write(&c, strm); + lib3ds_string_write(light->projector,strm); } if (light->spot_overshoot) { /*---- LIB3DS_DL_SPOT_OVERSHOOT ----*/ Lib3dsChunk c; c.chunk=LIB3DS_DL_SPOT_OVERSHOOT; c.size=6; - lib3ds_chunk_write(&c, f); + lib3ds_chunk_write(&c, strm); } if (fabs(light->ray_bias)>LIB3DS_EPSILON) { /*---- LIB3DS_DL_RAY_BIAS ----*/ Lib3dsChunk c; c.chunk=LIB3DS_DL_RAY_BIAS; c.size=10; - lib3ds_chunk_write(&c, f); - lib3ds_float_write(light->ray_bias,f); + lib3ds_chunk_write(&c, strm); + lib3ds_float_write(light->ray_bias,strm); } if (light->ray_shadows) { /*---- LIB3DS_DL_RAYSHAD ----*/ Lib3dsChunk c; c.chunk=LIB3DS_DL_RAYSHAD; c.size=6; - lib3ds_chunk_write(&c, f); + lib3ds_chunk_write(&c, strm); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } return(LIB3DS_TRUE); diff --git a/src/osgPlugins/3ds/light.h b/src/osgPlugins/3ds/light.h index bec3fe496..2a1ceb8f8 100644 --- a/src/osgPlugins/3ds/light.h +++ b/src/osgPlugins/3ds/light.h @@ -27,10 +27,14 @@ #include "types.h" #endif +#include +using namespace std; + #ifdef __cplusplus extern "C" { #endif + /*! * Light * \ingroup light @@ -68,8 +72,8 @@ struct _Lib3dsLight { extern LIB3DSAPI Lib3dsLight* lib3ds_light_new(const char *name); extern LIB3DSAPI void lib3ds_light_free(Lib3dsLight *mesh); extern LIB3DSAPI void lib3ds_light_dump(Lib3dsLight *light); -extern LIB3DSAPI Lib3dsBool lib3ds_light_read(Lib3dsLight *light, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_light_write(Lib3dsLight *light, FILE *f); +extern LIB3DSAPI Lib3dsBool lib3ds_light_read(Lib3dsLight *light, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_light_write(Lib3dsLight *light, iostream *strm); #ifdef __cplusplus } diff --git a/src/osgPlugins/3ds/material.cpp b/src/osgPlugins/3ds/material.cpp index 35371d045..4e2043c26 100644 --- a/src/osgPlugins/3ds/material.cpp +++ b/src/osgPlugins/3ds/material.cpp @@ -67,23 +67,23 @@ lib3ds_material_free(Lib3dsMaterial *material) static Lib3dsBool -color_read(Lib3dsRgba rgb, FILE *f) +color_read(Lib3dsRgba rgb, iostream *strm) { Lib3dsChunk c; Lib3dsWord chunk; Lib3dsBool have_lin=LIB3DS_FALSE; - if (!lib3ds_chunk_read_start(&c, 0, f)) { + if (!lib3ds_chunk_read_start(&c, 0, strm)) { return(LIB3DS_FALSE); } - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { switch (chunk) { case LIB3DS_LIN_COLOR_24: { int i; for (i=0; i<3; ++i) { - rgb[i]=1.0f*lib3ds_byte_read(f)/255.0f; + rgb[i]=1.0f*lib3ds_byte_read(strm)/255.0f; } rgb[3]=1.0f; } @@ -95,40 +95,40 @@ color_read(Lib3dsRgba rgb, FILE *f) if (!have_lin) { int i; for (i=0; i<3; ++i) { - rgb[i]=1.0f*lib3ds_byte_read(f)/255.0f; + rgb[i]=1.0f*lib3ds_byte_read(strm)/255.0f; } rgb[3]=1.0f; } break; case LIB3DS_COLOR_F: // sth: this will fix 3ds-files exported from cinema 4d - lib3ds_rgb_read(rgb, f); + lib3ds_rgb_read(rgb, strm); break; default: lib3ds_chunk_unknown(chunk); } } - lib3ds_chunk_read_end(&c, f); + lib3ds_chunk_read_end(&c, strm); return(LIB3DS_TRUE); } static Lib3dsBool -int_percentage_read(Lib3dsFloat *p, FILE *f) +int_percentage_read(Lib3dsFloat *p, iostream *strm) { Lib3dsChunk c; Lib3dsWord chunk; - if (!lib3ds_chunk_read_start(&c, 0, f)) { + if (!lib3ds_chunk_read_start(&c, 0, strm)) { return(LIB3DS_FALSE); } - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { switch (chunk) { case LIB3DS_INT_PERCENTAGE: { - Lib3dsIntw i=lib3ds_intw_read(f); + Lib3dsIntw i=lib3ds_intw_read(strm); *p=(Lib3dsFloat)(1.0*i/100.0); } break; @@ -137,31 +137,31 @@ int_percentage_read(Lib3dsFloat *p, FILE *f) } } - lib3ds_chunk_read_end(&c, f); + lib3ds_chunk_read_end(&c, strm); return(LIB3DS_TRUE); } static Lib3dsBool -texture_map_read(Lib3dsTextureMap *map, FILE *f) +texture_map_read(Lib3dsTextureMap *map, iostream *strm) { Lib3dsChunk c; Lib3dsWord chunk; - if (!lib3ds_chunk_read_start(&c, 0, f)) { + if (!lib3ds_chunk_read_start(&c, 0, strm)) { return(LIB3DS_FALSE); } - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { switch (chunk) { case LIB3DS_INT_PERCENTAGE: { - map->percent=1.0f*lib3ds_intw_read(f)/100.0f; + map->percent=1.0f*lib3ds_intw_read(strm)/100.0f; } break; case LIB3DS_MAT_MAPNAME: { - if (!lib3ds_string_read(map->name, 64, f)) { + if (!lib3ds_string_read(map->name, 64, strm)) { return(LIB3DS_FALSE); } lib3ds_chunk_dump_info(" NAME=%s", map->name); @@ -169,72 +169,72 @@ texture_map_read(Lib3dsTextureMap *map, FILE *f) break; case LIB3DS_MAT_MAP_TILING: { - map->flags=lib3ds_word_read(f); + map->flags=lib3ds_word_read(strm); } break; case LIB3DS_MAT_MAP_TEXBLUR: { - map->blur=lib3ds_float_read(f); + map->blur=lib3ds_float_read(strm); } break; case LIB3DS_MAT_MAP_USCALE: { - map->scale[0]=lib3ds_float_read(f); + map->scale[0]=lib3ds_float_read(strm); } break; case LIB3DS_MAT_MAP_VSCALE: { - map->scale[1]=lib3ds_float_read(f); + map->scale[1]=lib3ds_float_read(strm); } break; case LIB3DS_MAT_MAP_UOFFSET: { - map->offset[0]=lib3ds_float_read(f); + map->offset[0]=lib3ds_float_read(strm); } break; case LIB3DS_MAT_MAP_VOFFSET: { - map->offset[1]=lib3ds_float_read(f); + map->offset[1]=lib3ds_float_read(strm); } break; case LIB3DS_MAT_MAP_ANG: { - map->rotation=lib3ds_float_read(f); + map->rotation=lib3ds_float_read(strm); } break; case LIB3DS_MAT_MAP_COL1: { - map->tint_1[0]=1.0f*lib3ds_byte_read(f)/255.0f; - map->tint_1[1]=1.0f*lib3ds_byte_read(f)/255.0f; - map->tint_1[2]=1.0f*lib3ds_byte_read(f)/255.0f; + map->tint_1[0]=1.0f*lib3ds_byte_read(strm)/255.0f; + map->tint_1[1]=1.0f*lib3ds_byte_read(strm)/255.0f; + map->tint_1[2]=1.0f*lib3ds_byte_read(strm)/255.0f; } break; case LIB3DS_MAT_MAP_COL2: { - map->tint_2[0]=1.0f*lib3ds_byte_read(f)/255.0f; - map->tint_2[1]=1.0f*lib3ds_byte_read(f)/255.0f; - map->tint_2[2]=1.0f*lib3ds_byte_read(f)/255.0f; + map->tint_2[0]=1.0f*lib3ds_byte_read(strm)/255.0f; + map->tint_2[1]=1.0f*lib3ds_byte_read(strm)/255.0f; + map->tint_2[2]=1.0f*lib3ds_byte_read(strm)/255.0f; } break; case LIB3DS_MAT_MAP_RCOL: { - map->tint_r[0]=1.0f*lib3ds_byte_read(f)/255.0f; - map->tint_r[1]=1.0f*lib3ds_byte_read(f)/255.0f; - map->tint_r[2]=1.0f*lib3ds_byte_read(f)/255.0f; + map->tint_r[0]=1.0f*lib3ds_byte_read(strm)/255.0f; + map->tint_r[1]=1.0f*lib3ds_byte_read(strm)/255.0f; + map->tint_r[2]=1.0f*lib3ds_byte_read(strm)/255.0f; } break; case LIB3DS_MAT_MAP_GCOL: { - map->tint_g[0]=1.0f*lib3ds_byte_read(f)/255.0f; - map->tint_g[1]=1.0f*lib3ds_byte_read(f)/255.0f; - map->tint_g[2]=1.0f*lib3ds_byte_read(f)/255.0f; + map->tint_g[0]=1.0f*lib3ds_byte_read(strm)/255.0f; + map->tint_g[1]=1.0f*lib3ds_byte_read(strm)/255.0f; + map->tint_g[2]=1.0f*lib3ds_byte_read(strm)/255.0f; } break; case LIB3DS_MAT_MAP_BCOL: { - map->tint_b[0]=1.0f*lib3ds_byte_read(f)/255.0f; - map->tint_b[1]=1.0f*lib3ds_byte_read(f)/255.0f; - map->tint_b[2]=1.0f*lib3ds_byte_read(f)/255.0f; + map->tint_b[0]=1.0f*lib3ds_byte_read(strm)/255.0f; + map->tint_b[1]=1.0f*lib3ds_byte_read(strm)/255.0f; + map->tint_b[2]=1.0f*lib3ds_byte_read(strm)/255.0f; } break; default: @@ -242,7 +242,7 @@ texture_map_read(Lib3dsTextureMap *map, FILE *f) } } - lib3ds_chunk_read_end(&c, f); + lib3ds_chunk_read_end(&c, strm); return(LIB3DS_TRUE); } @@ -337,21 +337,21 @@ lib3ds_material_dump(Lib3dsMaterial *material) * \ingroup material */ Lib3dsBool -lib3ds_material_read(Lib3dsMaterial *material, FILE *f) +lib3ds_material_read(Lib3dsMaterial *material, iostream *strm) { Lib3dsChunk c; Lib3dsWord chunk; ASSERT(material); - if (!lib3ds_chunk_read_start(&c, LIB3DS_MAT_ENTRY, f)) { + if (!lib3ds_chunk_read_start(&c, LIB3DS_MAT_ENTRY, strm)) { return(LIB3DS_FALSE); } - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { switch (chunk) { case LIB3DS_MAT_NAME: { - if (!lib3ds_string_read(material->name, 64, f)) { + if (!lib3ds_string_read(material->name, 64, strm)) { return(LIB3DS_FALSE); } lib3ds_chunk_dump_info(" NAME=%s", material->name); @@ -359,56 +359,56 @@ lib3ds_material_read(Lib3dsMaterial *material, FILE *f) break; case LIB3DS_MAT_AMBIENT: { - lib3ds_chunk_read_reset(&c, f); - if (!color_read(material->ambient, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!color_read(material->ambient, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_DIFFUSE: { - lib3ds_chunk_read_reset(&c, f); - if (!color_read(material->diffuse, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!color_read(material->diffuse, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_SPECULAR: { - lib3ds_chunk_read_reset(&c, f); - if (!color_read(material->specular, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!color_read(material->specular, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_SHININESS: { - lib3ds_chunk_read_reset(&c, f); - if (!int_percentage_read(&material->shininess, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!int_percentage_read(&material->shininess, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_SHIN2PCT: { - lib3ds_chunk_read_reset(&c, f); - if (!int_percentage_read(&material->shin_strength, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!int_percentage_read(&material->shin_strength, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_TRANSPARENCY: { - lib3ds_chunk_read_reset(&c, f); - if (!int_percentage_read(&material->transparency, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!int_percentage_read(&material->transparency, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_XPFALL: { - lib3ds_chunk_read_reset(&c, f); - if (!int_percentage_read(&material->falloff, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!int_percentage_read(&material->falloff, strm)) { return(LIB3DS_FALSE); } } @@ -420,8 +420,8 @@ lib3ds_material_read(Lib3dsMaterial *material, FILE *f) break; case LIB3DS_MAT_REFBLUR: { - lib3ds_chunk_read_reset(&c, f); - if (!int_percentage_read(&material->blur, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!int_percentage_read(&material->blur, strm)) { return(LIB3DS_FALSE); } } @@ -433,7 +433,7 @@ lib3ds_material_read(Lib3dsMaterial *material, FILE *f) break; case LIB3DS_MAT_SHADING: { - material->shading=lib3ds_intw_read(f); + material->shading=lib3ds_intw_read(strm); } break; case LIB3DS_MAT_SELF_ILLUM: @@ -478,144 +478,144 @@ lib3ds_material_read(Lib3dsMaterial *material, FILE *f) break; case LIB3DS_MAT_WIRE_SIZE: { - material->wire_size=lib3ds_float_read(f); + material->wire_size=lib3ds_float_read(strm); } break; case LIB3DS_MAT_TEXMAP: { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->texture1_map, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!texture_map_read(&material->texture1_map, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_TEXMASK: { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->texture1_mask, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!texture_map_read(&material->texture1_mask, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_TEX2MAP: { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->texture2_map, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!texture_map_read(&material->texture2_map, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_TEX2MASK: { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->texture2_mask, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!texture_map_read(&material->texture2_mask, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_OPACMAP: { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->opacity_map, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!texture_map_read(&material->opacity_map, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_OPACMASK: { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->opacity_mask, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!texture_map_read(&material->opacity_mask, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_BUMPMAP: { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->bump_map, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!texture_map_read(&material->bump_map, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_BUMPMASK: { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->bump_mask, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!texture_map_read(&material->bump_mask, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_SPECMAP: { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->specular_map, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!texture_map_read(&material->specular_map, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_SPECMASK: { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->specular_mask, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!texture_map_read(&material->specular_mask, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_SHINMAP: { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->shininess_map, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!texture_map_read(&material->shininess_map, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_SHINMASK: { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->shininess_mask, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!texture_map_read(&material->shininess_mask, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_SELFIMAP: { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->self_illum_map, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!texture_map_read(&material->self_illum_map, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_SELFIMASK: { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->self_illum_mask, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!texture_map_read(&material->self_illum_mask, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_REFLMAP: { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->reflection_map, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!texture_map_read(&material->reflection_map, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_REFLMASK: { - lib3ds_chunk_read_reset(&c, f); - if (!texture_map_read(&material->reflection_mask, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!texture_map_read(&material->reflection_mask, strm)) { return(LIB3DS_FALSE); } } break; case LIB3DS_MAT_ACUBIC: { - lib3ds_intb_read(f); - material->autorefl_map.level=lib3ds_intb_read(f); - material->autorefl_map.flags=lib3ds_intw_read(f); - material->autorefl_map.size=lib3ds_intd_read(f); - material->autorefl_map.frame_step=lib3ds_intd_read(f); + lib3ds_intb_read(strm); + material->autorefl_map.level=lib3ds_intb_read(strm); + material->autorefl_map.flags=lib3ds_intw_read(strm); + material->autorefl_map.size=lib3ds_intd_read(strm); + material->autorefl_map.frame_step=lib3ds_intd_read(strm); } break; default: @@ -623,50 +623,50 @@ lib3ds_material_read(Lib3dsMaterial *material, FILE *f) } } - lib3ds_chunk_read_end(&c, f); + lib3ds_chunk_read_end(&c, strm); return(LIB3DS_TRUE); } static Lib3dsBool -color_write(Lib3dsRgba rgb, FILE *f) +color_write(Lib3dsRgba rgb, iostream *strm) { Lib3dsChunk c; c.chunk=LIB3DS_COLOR_24; c.size=9; - lib3ds_chunk_write(&c,f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*rgb[0]+0.5),f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*rgb[1]+0.5),f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*rgb[2]+0.5),f); + lib3ds_chunk_write(&c,strm); + lib3ds_byte_write((Lib3dsByte)floor(255.0*rgb[0]+0.5),strm); + lib3ds_byte_write((Lib3dsByte)floor(255.0*rgb[1]+0.5),strm); + lib3ds_byte_write((Lib3dsByte)floor(255.0*rgb[2]+0.5),strm); c.chunk=LIB3DS_LIN_COLOR_24; c.size=9; - lib3ds_chunk_write(&c,f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*rgb[0]+0.5),f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*rgb[1]+0.5),f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*rgb[2]+0.5),f); + lib3ds_chunk_write(&c,strm); + lib3ds_byte_write((Lib3dsByte)floor(255.0*rgb[0]+0.5),strm); + lib3ds_byte_write((Lib3dsByte)floor(255.0*rgb[1]+0.5),strm); + lib3ds_byte_write((Lib3dsByte)floor(255.0*rgb[2]+0.5),strm); return(LIB3DS_TRUE); } static Lib3dsBool -int_percentage_write(Lib3dsFloat p, FILE *f) +int_percentage_write(Lib3dsFloat p, iostream *strm) { Lib3dsChunk c; c.chunk=LIB3DS_INT_PERCENTAGE; c.size=8; - lib3ds_chunk_write(&c,f); - lib3ds_intw_write((Lib3dsByte)floor(100.0*p+0.5),f); + lib3ds_chunk_write(&c,strm); + lib3ds_intw_write((Lib3dsByte)floor(100.0*p+0.5),strm); return(LIB3DS_TRUE); } static Lib3dsBool -texture_map_write(Lib3dsWord chunk, Lib3dsTextureMap *map, FILE *f) +texture_map_write(Lib3dsWord chunk, Lib3dsTextureMap *map, iostream *strm) { Lib3dsChunk c; @@ -674,127 +674,127 @@ texture_map_write(Lib3dsWord chunk, Lib3dsTextureMap *map, FILE *f) return(LIB3DS_TRUE); } c.chunk=chunk; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - int_percentage_write(map->percent,f); + int_percentage_write(map->percent,strm); { /*---- LIB3DS_MAT_MAPNAME ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_MAPNAME; c.size=6+strlen(map->name)+1; - lib3ds_chunk_write(&c,f); - lib3ds_string_write(map->name,f); + lib3ds_chunk_write(&c,strm); + lib3ds_string_write(map->name,strm); } { /*---- LIB3DS_MAT_MAP_TILING ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_MAP_TILING; c.size=8; - lib3ds_chunk_write(&c,f); - lib3ds_word_write((Lib3dsWord)map->flags,f); + lib3ds_chunk_write(&c,strm); + lib3ds_word_write((Lib3dsWord)map->flags,strm); } { /*---- LIB3DS_MAT_MAP_TEXBLUR ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_MAP_TEXBLUR; c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(map->blur,f); + lib3ds_chunk_write(&c,strm); + lib3ds_float_write(map->blur,strm); } { /*---- LIB3DS_MAT_MAP_USCALE ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_MAP_USCALE; c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(map->scale[0],f); + lib3ds_chunk_write(&c,strm); + lib3ds_float_write(map->scale[0],strm); } { /*---- LIB3DS_MAT_MAP_VSCALE ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_MAP_VSCALE; c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(map->scale[1],f); + lib3ds_chunk_write(&c,strm); + lib3ds_float_write(map->scale[1],strm); } { /*---- LIB3DS_MAT_MAP_UOFFSET ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_MAP_UOFFSET; c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(map->offset[0],f); + lib3ds_chunk_write(&c,strm); + lib3ds_float_write(map->offset[0],strm); } { /*---- LIB3DS_MAT_MAP_VOFFSET ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_MAP_VOFFSET; c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(map->offset[1],f); + lib3ds_chunk_write(&c,strm); + lib3ds_float_write(map->offset[1],strm); } { /*---- LIB3DS_MAT_MAP_ANG ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_MAP_ANG; c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(map->rotation,f); + lib3ds_chunk_write(&c,strm); + lib3ds_float_write(map->rotation,strm); } { /*---- LIB3DS_MAT_MAP_COL1 ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_MAP_COL1; c.size=9; - lib3ds_chunk_write(&c,f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_1[0]+0.5), f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_1[1]+0.5), f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_1[2]+0.5), f); + lib3ds_chunk_write(&c,strm); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_1[0]+0.5), strm); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_1[1]+0.5), strm); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_1[2]+0.5), strm); } { /*---- LIB3DS_MAT_MAP_COL2 ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_MAP_COL2; c.size=9; - lib3ds_chunk_write(&c,f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_2[0]+0.5), f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_2[1]+0.5), f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_2[2]+0.5), f); + lib3ds_chunk_write(&c,strm); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_2[0]+0.5), strm); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_2[1]+0.5), strm); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_2[2]+0.5), strm); } { /*---- LIB3DS_MAT_MAP_RCOL ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_MAP_RCOL; c.size=9; - lib3ds_chunk_write(&c,f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_r[0]+0.5), f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_r[1]+0.5), f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_r[2]+0.5), f); + lib3ds_chunk_write(&c,strm); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_r[0]+0.5), strm); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_r[1]+0.5), strm); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_r[2]+0.5), strm); } { /*---- LIB3DS_MAT_MAP_GCOL ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_MAP_GCOL; c.size=9; - lib3ds_chunk_write(&c,f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_g[0]+0.5), f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_g[1]+0.5), f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_g[2]+0.5), f); + lib3ds_chunk_write(&c,strm); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_g[0]+0.5), strm); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_g[1]+0.5), strm); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_g[2]+0.5), strm); } { /*---- LIB3DS_MAT_MAP_BCOL ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_MAP_BCOL; c.size=9; - lib3ds_chunk_write(&c,f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_b[0]+0.5), f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_b[1]+0.5), f); - lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_b[2]+0.5), f); + lib3ds_chunk_write(&c,strm); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_b[0]+0.5), strm); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_b[1]+0.5), strm); + lib3ds_byte_write((Lib3dsByte)floor(255.0*map->tint_b[2]+0.5), strm); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } return(LIB3DS_TRUE); @@ -805,12 +805,12 @@ texture_map_write(Lib3dsWord chunk, Lib3dsTextureMap *map, FILE *f) * \ingroup material */ Lib3dsBool -lib3ds_material_write(Lib3dsMaterial *material, FILE *f) +lib3ds_material_write(Lib3dsMaterial *material, iostream *strm) { Lib3dsChunk c; c.chunk=LIB3DS_MAT_ENTRY; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } @@ -818,210 +818,210 @@ lib3ds_material_write(Lib3dsMaterial *material, FILE *f) Lib3dsChunk c; c.chunk=LIB3DS_MAT_NAME; c.size=6+strlen(material->name)+1; - lib3ds_chunk_write(&c,f); - lib3ds_string_write(material->name,f); + lib3ds_chunk_write(&c,strm); + lib3ds_string_write(material->name,strm); } { /*---- LIB3DS_MAT_AMBIENT ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_AMBIENT; c.size=24; - lib3ds_chunk_write(&c,f); - color_write(material->ambient,f); + lib3ds_chunk_write(&c,strm); + color_write(material->ambient,strm); } { /*---- LIB3DS_MAT_DIFFUSE ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_DIFFUSE; c.size=24; - lib3ds_chunk_write(&c,f); - color_write(material->diffuse,f); + lib3ds_chunk_write(&c,strm); + color_write(material->diffuse,strm); } { /*---- LIB3DS_MAT_SPECULAR ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_SPECULAR; c.size=24; - lib3ds_chunk_write(&c,f); - color_write(material->specular,f); + lib3ds_chunk_write(&c,strm); + color_write(material->specular,strm); } { /*---- LIB3DS_MAT_SHININESS ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_SHININESS; c.size=14; - lib3ds_chunk_write(&c,f); - int_percentage_write(material->shininess,f); + lib3ds_chunk_write(&c,strm); + int_percentage_write(material->shininess,strm); } { /*---- LIB3DS_MAT_SHIN2PCT ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_SHIN2PCT; c.size=14; - lib3ds_chunk_write(&c,f); - int_percentage_write(material->shin_strength,f); + lib3ds_chunk_write(&c,strm); + int_percentage_write(material->shin_strength,strm); } { /*---- LIB3DS_MAT_TRANSPARENCY ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_TRANSPARENCY; c.size=14; - lib3ds_chunk_write(&c,f); - int_percentage_write(material->transparency,f); + lib3ds_chunk_write(&c,strm); + int_percentage_write(material->transparency,strm); } { /*---- LIB3DS_MAT_XPFALL ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_XPFALL; c.size=14; - lib3ds_chunk_write(&c,f); - int_percentage_write(material->falloff,f); + lib3ds_chunk_write(&c,strm); + int_percentage_write(material->falloff,strm); } if (material->use_falloff) { /*---- LIB3DS_MAT_USE_XPFALL ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_USE_XPFALL; c.size=6; - lib3ds_chunk_write(&c,f); + lib3ds_chunk_write(&c,strm); } { /*---- LIB3DS_MAT_SHADING ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_SHADING; c.size=8; - lib3ds_chunk_write(&c,f); - lib3ds_intw_write(material->shading,f); + lib3ds_chunk_write(&c,strm); + lib3ds_intw_write(material->shading,strm); } { /*---- LIB3DS_MAT_REFBLUR ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_REFBLUR; c.size=14; - lib3ds_chunk_write(&c,f); - int_percentage_write(material->blur,f); + lib3ds_chunk_write(&c,strm); + int_percentage_write(material->blur,strm); } if (material->use_blur) { /*---- LIB3DS_MAT_USE_REFBLUR ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_USE_REFBLUR; c.size=6; - lib3ds_chunk_write(&c,f); + lib3ds_chunk_write(&c,strm); } if (material->self_illum) { /*---- LIB3DS_MAT_SELF_ILLUM ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_SELF_ILLUM; c.size=6; - lib3ds_chunk_write(&c,f); + lib3ds_chunk_write(&c,strm); } if (material->two_sided) { /*---- LIB3DS_MAT_TWO_SIDE ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_TWO_SIDE; c.size=6; - lib3ds_chunk_write(&c,f); + lib3ds_chunk_write(&c,strm); } if (material->map_decal) { /*---- LIB3DS_MAT_DECAL ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_DECAL; c.size=6; - lib3ds_chunk_write(&c,f); + lib3ds_chunk_write(&c,strm); } if (material->additive) { /*---- LIB3DS_MAT_ADDITIVE ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_ADDITIVE; c.size=6; - lib3ds_chunk_write(&c,f); + lib3ds_chunk_write(&c,strm); } if (material->use_wire) { /*---- LIB3DS_MAT_WIRE ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_WIRE; c.size=6; - lib3ds_chunk_write(&c,f); + lib3ds_chunk_write(&c,strm); } if (material->use_wire_abs) { /*---- LIB3DS_MAT_WIREABS ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_WIREABS; c.size=6; - lib3ds_chunk_write(&c,f); + lib3ds_chunk_write(&c,strm); } { /*---- LIB3DS_MAT_WIRE_SIZE ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_WIRE_SIZE; c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(material->wire_size,f); + lib3ds_chunk_write(&c,strm); + lib3ds_float_write(material->wire_size,strm); } if (material->face_map) { /*---- LIB3DS_MAT_FACEMAP ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_FACEMAP; c.size=6; - lib3ds_chunk_write(&c,f); + lib3ds_chunk_write(&c,strm); } if (material->soften) { /*---- LIB3DS_MAT_PHONGSOFT ----*/ Lib3dsChunk c; c.chunk=LIB3DS_MAT_PHONGSOFT; c.size=6; - lib3ds_chunk_write(&c,f); + lib3ds_chunk_write(&c,strm); } - if (!texture_map_write(LIB3DS_MAT_TEXMAP, &material->texture1_map, f)) { + if (!texture_map_write(LIB3DS_MAT_TEXMAP, &material->texture1_map, strm)) { return(LIB3DS_FALSE); } - if (!texture_map_write(LIB3DS_MAT_TEXMASK, &material->texture1_mask, f)) { + if (!texture_map_write(LIB3DS_MAT_TEXMASK, &material->texture1_mask, strm)) { return(LIB3DS_FALSE); } - if (!texture_map_write(LIB3DS_MAT_TEX2MAP, &material->texture2_map, f)) { + if (!texture_map_write(LIB3DS_MAT_TEX2MAP, &material->texture2_map, strm)) { return(LIB3DS_FALSE); } - if (!texture_map_write(LIB3DS_MAT_TEX2MASK, &material->texture2_mask, f)) { + if (!texture_map_write(LIB3DS_MAT_TEX2MASK, &material->texture2_mask, strm)) { return(LIB3DS_FALSE); } - if (!texture_map_write(LIB3DS_MAT_OPACMAP, &material->opacity_map, f)) { + if (!texture_map_write(LIB3DS_MAT_OPACMAP, &material->opacity_map, strm)) { return(LIB3DS_FALSE); } - if (!texture_map_write(LIB3DS_MAT_OPACMASK, &material->opacity_mask, f)) { + if (!texture_map_write(LIB3DS_MAT_OPACMASK, &material->opacity_mask, strm)) { return(LIB3DS_FALSE); } - if (!texture_map_write(LIB3DS_MAT_BUMPMAP, &material->bump_map, f)) { + if (!texture_map_write(LIB3DS_MAT_BUMPMAP, &material->bump_map, strm)) { return(LIB3DS_FALSE); } - if (!texture_map_write(LIB3DS_MAT_BUMPMASK, &material->bump_mask, f)) { + if (!texture_map_write(LIB3DS_MAT_BUMPMASK, &material->bump_mask, strm)) { return(LIB3DS_FALSE); } - if (!texture_map_write(LIB3DS_MAT_SPECMAP, &material->specular_map, f)) { + if (!texture_map_write(LIB3DS_MAT_SPECMAP, &material->specular_map, strm)) { return(LIB3DS_FALSE); } - if (!texture_map_write(LIB3DS_MAT_SPECMASK, &material->specular_mask, f)) { + if (!texture_map_write(LIB3DS_MAT_SPECMASK, &material->specular_mask, strm)) { return(LIB3DS_FALSE); } - if (!texture_map_write(LIB3DS_MAT_SHINMAP, &material->shininess_map, f)) { + if (!texture_map_write(LIB3DS_MAT_SHINMAP, &material->shininess_map, strm)) { return(LIB3DS_FALSE); } - if (!texture_map_write(LIB3DS_MAT_SHINMASK, &material->shininess_mask, f)) { + if (!texture_map_write(LIB3DS_MAT_SHINMASK, &material->shininess_mask, strm)) { return(LIB3DS_FALSE); } - if (!texture_map_write(LIB3DS_MAT_SELFIMAP, &material->self_illum_map, f)) { + if (!texture_map_write(LIB3DS_MAT_SELFIMAP, &material->self_illum_map, strm)) { return(LIB3DS_FALSE); } - if (!texture_map_write(LIB3DS_MAT_SELFIMASK, &material->self_illum_mask, f)) { + if (!texture_map_write(LIB3DS_MAT_SELFIMASK, &material->self_illum_mask, strm)) { return(LIB3DS_FALSE); } - if (!texture_map_write(LIB3DS_MAT_REFLMAP, &material->reflection_map, f)) { + if (!texture_map_write(LIB3DS_MAT_REFLMAP, &material->reflection_map, strm)) { return(LIB3DS_FALSE); } - if (!texture_map_write(LIB3DS_MAT_REFLMASK, &material->reflection_mask, f)) { + if (!texture_map_write(LIB3DS_MAT_REFLMASK, &material->reflection_mask, strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } return(LIB3DS_TRUE); diff --git a/src/osgPlugins/3ds/material.h b/src/osgPlugins/3ds/material.h index 25b12da81..d7848d1dd 100644 --- a/src/osgPlugins/3ds/material.h +++ b/src/osgPlugins/3ds/material.h @@ -27,6 +27,9 @@ #include "types.h" #endif +#include +using namespace std; + #ifdef __cplusplus extern "C" { #endif @@ -156,8 +159,8 @@ struct _Lib3dsMaterial { extern LIB3DSAPI Lib3dsMaterial* lib3ds_material_new(); extern LIB3DSAPI void lib3ds_material_free(Lib3dsMaterial *material); extern LIB3DSAPI void lib3ds_material_dump(Lib3dsMaterial *material); -extern LIB3DSAPI Lib3dsBool lib3ds_material_read(Lib3dsMaterial *material, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_material_write(Lib3dsMaterial *material, FILE *f); +extern LIB3DSAPI Lib3dsBool lib3ds_material_read(Lib3dsMaterial *material, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_material_write(Lib3dsMaterial *material, iostream *strm); #ifdef __cplusplus } diff --git a/src/osgPlugins/3ds/mesh.cpp b/src/osgPlugins/3ds/mesh.cpp index cd13aed00..6f1847484 100644 --- a/src/osgPlugins/3ds/mesh.cpp +++ b/src/osgPlugins/3ds/mesh.cpp @@ -42,19 +42,19 @@ static Lib3dsBool -face_array_read(Lib3dsMesh *mesh, FILE *f) +face_array_read(Lib3dsMesh *mesh, iostream *strm) { Lib3dsChunk c; Lib3dsWord chunk; int i; int faces; - if (!lib3ds_chunk_read_start(&c, LIB3DS_FACE_ARRAY, f)) { + if (!lib3ds_chunk_read_start(&c, LIB3DS_FACE_ARRAY, strm)) { return(LIB3DS_FALSE); } lib3ds_mesh_free_face_list(mesh); - faces=lib3ds_word_read(f); + faces=lib3ds_word_read(strm); if (faces) { if (!lib3ds_mesh_new_face_list(mesh, faces)) { LIB3DS_ERROR_LOG; @@ -62,21 +62,21 @@ face_array_read(Lib3dsMesh *mesh, FILE *f) } for (i=0; ifaceL[i].material, ""); - mesh->faceL[i].points[0]=lib3ds_word_read(f); - mesh->faceL[i].points[1]=lib3ds_word_read(f); - mesh->faceL[i].points[2]=lib3ds_word_read(f); - mesh->faceL[i].flags=lib3ds_word_read(f); + mesh->faceL[i].points[0]=lib3ds_word_read(strm); + mesh->faceL[i].points[1]=lib3ds_word_read(strm); + mesh->faceL[i].points[2]=lib3ds_word_read(strm); + mesh->faceL[i].flags=lib3ds_word_read(strm); } - lib3ds_chunk_read_tell(&c, f); + lib3ds_chunk_read_tell(&c, strm); - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { switch (chunk) { case LIB3DS_SMOOTH_GROUP: { unsigned i; for (i=0; ifaces; ++i) { - mesh->faceL[i].smoothing=lib3ds_dword_read(f); + mesh->faceL[i].smoothing=lib3ds_dword_read(strm); } } break; @@ -87,12 +87,12 @@ face_array_read(Lib3dsMesh *mesh, FILE *f) unsigned i; unsigned index; - if (!lib3ds_string_read(name, 64, f)) { + if (!lib3ds_string_read(name, 64, strm)) { return(LIB3DS_FALSE); } - faces=lib3ds_word_read(f); + faces=lib3ds_word_read(strm); for (i=0; ifaces); strcpy(mesh->faceL[index].material, name); } @@ -102,27 +102,27 @@ face_array_read(Lib3dsMesh *mesh, FILE *f) { char name[64]; - if (!lib3ds_string_read(name, 64, f)) { + if (!lib3ds_string_read(name, 64, strm)) { return(LIB3DS_FALSE); } strcpy(mesh->box_map.front, name); - if (!lib3ds_string_read(name, 64, f)) { + if (!lib3ds_string_read(name, 64, strm)) { return(LIB3DS_FALSE); } strcpy(mesh->box_map.back, name); - if (!lib3ds_string_read(name, 64, f)) { + if (!lib3ds_string_read(name, 64, strm)) { return(LIB3DS_FALSE); } strcpy(mesh->box_map.left, name); - if (!lib3ds_string_read(name, 64, f)) { + if (!lib3ds_string_read(name, 64, strm)) { return(LIB3DS_FALSE); } strcpy(mesh->box_map.right, name); - if (!lib3ds_string_read(name, 64, f)) { + if (!lib3ds_string_read(name, 64, strm)) { return(LIB3DS_FALSE); } strcpy(mesh->box_map.top, name); - if (!lib3ds_string_read(name, 64, f)) { + if (!lib3ds_string_read(name, 64, strm)) { return(LIB3DS_FALSE); } strcpy(mesh->box_map.bottom, name); @@ -134,7 +134,7 @@ face_array_read(Lib3dsMesh *mesh, FILE *f) } } - lib3ds_chunk_read_end(&c, f); + lib3ds_chunk_read_end(&c, strm); return(LIB3DS_TRUE); } @@ -523,16 +523,16 @@ lib3ds_mesh_dump(Lib3dsMesh *mesh) * \ingroup mesh */ Lib3dsBool -lib3ds_mesh_read(Lib3dsMesh *mesh, FILE *f) +lib3ds_mesh_read(Lib3dsMesh *mesh, iostream *strm) { Lib3dsChunk c; Lib3dsWord chunk; - if (!lib3ds_chunk_read_start(&c, LIB3DS_N_TRI_OBJECT, f)) { + if (!lib3ds_chunk_read_start(&c, LIB3DS_N_TRI_OBJECT, strm)) { return(LIB3DS_FALSE); } - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { switch (chunk) { case LIB3DS_MESH_MATRIX: { @@ -541,14 +541,14 @@ lib3ds_mesh_read(Lib3dsMesh *mesh, FILE *f) lib3ds_matrix_identity(mesh->matrix); for (i=0; i<4; i++) { for (j=0; j<3; j++) { - mesh->matrix[i][j]=lib3ds_float_read(f); + mesh->matrix[i][j]=lib3ds_float_read(strm); } } } break; case LIB3DS_MESH_COLOR: { - mesh->color=lib3ds_byte_read(f); + mesh->color=lib3ds_byte_read(strm); } break; case LIB3DS_POINT_ARRAY: @@ -557,7 +557,7 @@ lib3ds_mesh_read(Lib3dsMesh *mesh, FILE *f) unsigned points; lib3ds_mesh_free_point_list(mesh); - points=lib3ds_word_read(f); + points=lib3ds_word_read(strm); if (points) { if (!lib3ds_mesh_new_point_list(mesh, points)) { LIB3DS_ERROR_LOG; @@ -565,7 +565,7 @@ lib3ds_mesh_read(Lib3dsMesh *mesh, FILE *f) } for (i=0; ipoints; ++i) { for (j=0; j<3; ++j) { - mesh->pointL[i].pos[j]=lib3ds_float_read(f); + mesh->pointL[i].pos[j]=lib3ds_float_read(strm); } } ASSERT((!mesh->flags) || (mesh->points==mesh->flags)); @@ -579,14 +579,14 @@ lib3ds_mesh_read(Lib3dsMesh *mesh, FILE *f) unsigned flags; lib3ds_mesh_free_flag_list(mesh); - flags=lib3ds_word_read(f); + flags=lib3ds_word_read(strm); if (flags) { if (!lib3ds_mesh_new_flag_list(mesh, flags)) { LIB3DS_ERROR_LOG; return(LIB3DS_FALSE); } for (i=0; iflags; ++i) { - mesh->flagL[i]=lib3ds_word_read(f); + mesh->flagL[i]=lib3ds_word_read(strm); } ASSERT((!mesh->points) || (mesh->flags==mesh->points)); ASSERT((!mesh->texels) || (mesh->flags==mesh->texels)); @@ -595,8 +595,8 @@ lib3ds_mesh_read(Lib3dsMesh *mesh, FILE *f) break; case LIB3DS_FACE_ARRAY: { - lib3ds_chunk_read_reset(&c, f); - if (!face_array_read(mesh, f)) { + lib3ds_chunk_read_reset(&c, strm); + if (!face_array_read(mesh, strm)) { return(LIB3DS_FALSE); } } @@ -606,23 +606,23 @@ lib3ds_mesh_read(Lib3dsMesh *mesh, FILE *f) int i,j; for (i=0; i<2; ++i) { - mesh->map_data.tile[i]=lib3ds_float_read(f); + mesh->map_data.tile[i]=lib3ds_float_read(strm); } for (i=0; i<3; ++i) { - mesh->map_data.pos[i]=lib3ds_float_read(f); + mesh->map_data.pos[i]=lib3ds_float_read(strm); } - mesh->map_data.scale=lib3ds_float_read(f); + mesh->map_data.scale=lib3ds_float_read(strm); lib3ds_matrix_identity(mesh->map_data.matrix); for (i=0; i<4; i++) { for (j=0; j<3; j++) { - mesh->map_data.matrix[i][j]=lib3ds_float_read(f); + mesh->map_data.matrix[i][j]=lib3ds_float_read(strm); } } for (i=0; i<2; ++i) { - mesh->map_data.planar_size[i]=lib3ds_float_read(f); + mesh->map_data.planar_size[i]=lib3ds_float_read(strm); } - mesh->map_data.cylinder_height=lib3ds_float_read(f); + mesh->map_data.cylinder_height=lib3ds_float_read(strm); } break; case LIB3DS_TEX_VERTS: @@ -631,15 +631,15 @@ lib3ds_mesh_read(Lib3dsMesh *mesh, FILE *f) unsigned texels; lib3ds_mesh_free_texel_list(mesh); - texels=lib3ds_word_read(f); + texels=lib3ds_word_read(strm); if (texels) { if (!lib3ds_mesh_new_texel_list(mesh, texels)) { LIB3DS_ERROR_LOG; return(LIB3DS_FALSE); } for (i=0; itexels; ++i) { - mesh->texelL[i][0]=lib3ds_float_read(f); - mesh->texelL[i][1]=lib3ds_float_read(f); + mesh->texelL[i][0]=lib3ds_float_read(strm); + mesh->texelL[i][1]=lib3ds_float_read(strm); } ASSERT((!mesh->points) || (mesh->texels==mesh->points)); ASSERT((!mesh->flags) || (mesh->texels==mesh->flags)); @@ -666,13 +666,13 @@ lib3ds_mesh_read(Lib3dsMesh *mesh, FILE *f) } } - lib3ds_chunk_read_end(&c, f); + lib3ds_chunk_read_end(&c, strm); return(LIB3DS_TRUE); } static Lib3dsBool -point_array_write(Lib3dsMesh *mesh, FILE *f) +point_array_write(Lib3dsMesh *mesh, iostream *strm) { Lib3dsChunk c; unsigned i; @@ -683,18 +683,18 @@ point_array_write(Lib3dsMesh *mesh, FILE *f) ASSERT(mesh->points<0x10000); c.chunk=LIB3DS_POINT_ARRAY; c.size=8+12*mesh->points; - lib3ds_chunk_write(&c, f); + lib3ds_chunk_write(&c, strm); - lib3ds_word_write((Lib3dsWord)mesh->points, f); + lib3ds_word_write((Lib3dsWord)mesh->points, strm); for (i=0; ipoints; ++i) { - lib3ds_vector_write(mesh->pointL[i].pos, f); + lib3ds_vector_write(mesh->pointL[i].pos, strm); } return(LIB3DS_TRUE); } static Lib3dsBool -flag_array_write(Lib3dsMesh *mesh, FILE *f) +flag_array_write(Lib3dsMesh *mesh, iostream *strm) { Lib3dsChunk c; unsigned i; @@ -705,18 +705,18 @@ flag_array_write(Lib3dsMesh *mesh, FILE *f) ASSERT(mesh->flags<0x10000); c.chunk=LIB3DS_POINT_FLAG_ARRAY; c.size=8+2*mesh->flags; - lib3ds_chunk_write(&c, f); + lib3ds_chunk_write(&c, strm); - lib3ds_word_write((Lib3dsWord)mesh->flags, f); + lib3ds_word_write((Lib3dsWord)mesh->flags, strm); for (i=0; iflags; ++i) { - lib3ds_word_write(mesh->flagL[i], f); + lib3ds_word_write(mesh->flagL[i], strm); } return(LIB3DS_TRUE); } static Lib3dsBool -face_array_write(Lib3dsMesh *mesh, FILE *f) +face_array_write(Lib3dsMesh *mesh, iostream *strm) { Lib3dsChunk c; @@ -725,18 +725,18 @@ face_array_write(Lib3dsMesh *mesh, FILE *f) } ASSERT(mesh->faces<0x10000); c.chunk=LIB3DS_FACE_ARRAY; - if (!lib3ds_chunk_write_start(&c, f)) { + if (!lib3ds_chunk_write_start(&c, strm)) { return(LIB3DS_FALSE); } { unsigned i; - lib3ds_word_write((Lib3dsWord)mesh->faces, f); + lib3ds_word_write((Lib3dsWord)mesh->faces, strm); for (i=0; ifaces; ++i) { - lib3ds_word_write(mesh->faceL[i].points[0], f); - lib3ds_word_write(mesh->faceL[i].points[1], f); - lib3ds_word_write(mesh->faceL[i].points[2], f); - lib3ds_word_write(mesh->faceL[i].flags, f); + lib3ds_word_write(mesh->faceL[i].points[0], strm); + lib3ds_word_write(mesh->faceL[i].points[1], strm); + lib3ds_word_write(mesh->faceL[i].points[2], strm); + lib3ds_word_write(mesh->faceL[i].flags, strm); } } @@ -760,14 +760,14 @@ face_array_write(Lib3dsMesh *mesh, FILE *f) c.chunk=LIB3DS_MSH_MAT_GROUP; c.size=6+ strlen(mesh->faceL[i].material)+1 +2+2*num; - lib3ds_chunk_write(&c, f); - lib3ds_string_write(mesh->faceL[i].material, f); - lib3ds_word_write(num, f); - lib3ds_word_write((Lib3dsWord)i, f); + lib3ds_chunk_write(&c, strm); + lib3ds_string_write(mesh->faceL[i].material, strm); + lib3ds_word_write(num, strm); + lib3ds_word_write((Lib3dsWord)i, strm); for (j=i+1; jfaces; ++j) { if (strcmp(mesh->faceL[i].material, mesh->faceL[j].material)==0) { - lib3ds_word_write((Lib3dsWord)j, f); + lib3ds_word_write((Lib3dsWord)j, strm); matf[j]=1; } } @@ -782,10 +782,10 @@ face_array_write(Lib3dsMesh *mesh, FILE *f) c.chunk=LIB3DS_SMOOTH_GROUP; c.size=6+4*mesh->faces; - lib3ds_chunk_write(&c, f); + lib3ds_chunk_write(&c, strm); for (i=0; ifaces; ++i) { - lib3ds_dword_write(mesh->faceL[i].smoothing, f); + lib3ds_dword_write(mesh->faceL[i].smoothing, strm); } } @@ -800,24 +800,24 @@ face_array_write(Lib3dsMesh *mesh, FILE *f) strlen(mesh->box_map.bottom)) { c.chunk=LIB3DS_MSH_BOXMAP; - if (!lib3ds_chunk_write_start(&c, f)) { + if (!lib3ds_chunk_write_start(&c, strm)) { return(LIB3DS_FALSE); } - lib3ds_string_write(mesh->box_map.front, f); - lib3ds_string_write(mesh->box_map.back, f); - lib3ds_string_write(mesh->box_map.left, f); - lib3ds_string_write(mesh->box_map.right, f); - lib3ds_string_write(mesh->box_map.top, f); - lib3ds_string_write(mesh->box_map.bottom, f); + lib3ds_string_write(mesh->box_map.front, strm); + lib3ds_string_write(mesh->box_map.back, strm); + lib3ds_string_write(mesh->box_map.left, strm); + lib3ds_string_write(mesh->box_map.right, strm); + lib3ds_string_write(mesh->box_map.top, strm); + lib3ds_string_write(mesh->box_map.bottom, strm); - if (!lib3ds_chunk_write_end(&c, f)) { + if (!lib3ds_chunk_write_end(&c, strm)) { return(LIB3DS_FALSE); } } } - if (!lib3ds_chunk_write_end(&c, f)) { + if (!lib3ds_chunk_write_end(&c, strm)) { return(LIB3DS_FALSE); } return(LIB3DS_TRUE); @@ -825,7 +825,7 @@ face_array_write(Lib3dsMesh *mesh, FILE *f) static Lib3dsBool -texel_array_write(Lib3dsMesh *mesh, FILE *f) +texel_array_write(Lib3dsMesh *mesh, iostream *strm) { Lib3dsChunk c; unsigned i; @@ -836,12 +836,12 @@ texel_array_write(Lib3dsMesh *mesh, FILE *f) ASSERT(mesh->texels<0x10000); c.chunk=LIB3DS_TEX_VERTS; c.size=8+8*mesh->texels; - lib3ds_chunk_write(&c, f); + lib3ds_chunk_write(&c, strm); - lib3ds_word_write((Lib3dsWord)mesh->texels, f); + lib3ds_word_write((Lib3dsWord)mesh->texels, strm); for (i=0; itexels; ++i) { - lib3ds_float_write(mesh->texelL[i][0], f); - lib3ds_float_write(mesh->texelL[i][1], f); + lib3ds_float_write(mesh->texelL[i][0], strm); + lib3ds_float_write(mesh->texelL[i][1], strm); } return(LIB3DS_TRUE); } @@ -851,18 +851,18 @@ texel_array_write(Lib3dsMesh *mesh, FILE *f) * \ingroup mesh */ Lib3dsBool -lib3ds_mesh_write(Lib3dsMesh *mesh, FILE *f) +lib3ds_mesh_write(Lib3dsMesh *mesh, iostream *strm) { Lib3dsChunk c; c.chunk=LIB3DS_N_TRI_OBJECT; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - if (!point_array_write(mesh, f)) { + if (!point_array_write(mesh, strm)) { return(LIB3DS_FALSE); } - if (!texel_array_write(mesh, f)) { + if (!texel_array_write(mesh, strm)) { return(LIB3DS_FALSE); } @@ -872,32 +872,32 @@ lib3ds_mesh_write(Lib3dsMesh *mesh, FILE *f) c.chunk=LIB3DS_MESH_TEXTURE_INFO; c.size=92; - if (!lib3ds_chunk_write(&c,f)) { + if (!lib3ds_chunk_write(&c,strm)) { return(LIB3DS_FALSE); } - lib3ds_word_write(mesh->map_data.maptype, f); + lib3ds_word_write(mesh->map_data.maptype, strm); for (i=0; i<2; ++i) { - lib3ds_float_write(mesh->map_data.tile[i], f); + lib3ds_float_write(mesh->map_data.tile[i], strm); } for (i=0; i<3; ++i) { - lib3ds_float_write(mesh->map_data.pos[i], f); + lib3ds_float_write(mesh->map_data.pos[i], strm); } - lib3ds_float_write(mesh->map_data.scale, f); + lib3ds_float_write(mesh->map_data.scale, strm); for (i=0; i<4; i++) { for (j=0; j<3; j++) { - lib3ds_float_write(mesh->map_data.matrix[i][j], f); + lib3ds_float_write(mesh->map_data.matrix[i][j], strm); } } for (i=0; i<2; ++i) { - lib3ds_float_write(mesh->map_data.planar_size[i], f); + lib3ds_float_write(mesh->map_data.planar_size[i], strm); } - lib3ds_float_write(mesh->map_data.cylinder_height, f); + lib3ds_float_write(mesh->map_data.cylinder_height, strm); } - if (!flag_array_write(mesh, f)) { + if (!flag_array_write(mesh, strm)) { return(LIB3DS_FALSE); } { /*---- LIB3DS_MESH_MATRIX ----*/ @@ -906,12 +906,12 @@ lib3ds_mesh_write(Lib3dsMesh *mesh, FILE *f) c.chunk=LIB3DS_MESH_MATRIX; c.size=54; - if (!lib3ds_chunk_write(&c,f)) { + if (!lib3ds_chunk_write(&c,strm)) { return(LIB3DS_FALSE); } for (i=0; i<4; i++) { for (j=0; j<3; j++) { - lib3ds_float_write(mesh->matrix[i][j], f); + lib3ds_float_write(mesh->matrix[i][j], strm); } } } @@ -921,16 +921,16 @@ lib3ds_mesh_write(Lib3dsMesh *mesh, FILE *f) c.chunk=LIB3DS_MESH_COLOR; c.size=7; - if (!lib3ds_chunk_write(&c,f)) { + if (!lib3ds_chunk_write(&c,strm)) { return(LIB3DS_FALSE); } - lib3ds_byte_write(mesh->color, f); + lib3ds_byte_write(mesh->color, strm); } - if (!face_array_write(mesh, f)) { + if (!face_array_write(mesh, strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } return(LIB3DS_TRUE); diff --git a/src/osgPlugins/3ds/mesh.h b/src/osgPlugins/3ds/mesh.h index cd481e52f..d3ad73a17 100644 --- a/src/osgPlugins/3ds/mesh.h +++ b/src/osgPlugins/3ds/mesh.h @@ -27,10 +27,15 @@ #include "types.h" #endif +#include +using namespace std; + #ifdef __cplusplus extern "C" { #endif + + /*! * Triangular mesh point * \ingroup mesh @@ -125,8 +130,8 @@ extern LIB3DSAPI void lib3ds_mesh_free_face_list(Lib3dsMesh *mesh); extern LIB3DSAPI void lib3ds_mesh_bounding_box(Lib3dsMesh *mesh, Lib3dsVector min, Lib3dsVector max); extern LIB3DSAPI void lib3ds_mesh_calculate_normals(Lib3dsMesh *mesh, Lib3dsVector *normalL); extern LIB3DSAPI void lib3ds_mesh_dump(Lib3dsMesh *mesh); -extern LIB3DSAPI Lib3dsBool lib3ds_mesh_read(Lib3dsMesh *mesh, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_mesh_write(Lib3dsMesh *mesh, FILE *f); +extern LIB3DSAPI Lib3dsBool lib3ds_mesh_read(Lib3dsMesh *mesh, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_mesh_write(Lib3dsMesh *mesh, iostream *strm); #ifdef __cplusplus } diff --git a/src/osgPlugins/3ds/node.cpp b/src/osgPlugins/3ds/node.cpp index b957a48f8..288a8731f 100644 --- a/src/osgPlugins/3ds/node.cpp +++ b/src/osgPlugins/3ds/node.cpp @@ -407,13 +407,13 @@ lib3ds_node_dump(Lib3dsNode *node, Lib3dsIntd level) * \ingroup node */ Lib3dsBool -lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *, FILE *f) +lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *, iostream *strm) { Lib3dsChunk c; Lib3dsWord chunk; ASSERT(node); - if (!lib3ds_chunk_read_start(&c, 0, f)) { + if (!lib3ds_chunk_read_start(&c, 0, strm)) { return(LIB3DS_FALSE); } switch (c.chunk) { @@ -429,22 +429,22 @@ lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *, FILE *f) return(LIB3DS_FALSE); } - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { switch (chunk) { case LIB3DS_NODE_ID: { - node->node_id=lib3ds_word_read(f); + node->node_id=lib3ds_word_read(strm); lib3ds_chunk_dump_info(" ID = %d", (short)node->node_id); } break; case LIB3DS_NODE_HDR: { - if (!lib3ds_string_read(node->name, 64, f)) { + if (!lib3ds_string_read(node->name, 64, strm)) { return(LIB3DS_FALSE); } - node->flags1=lib3ds_word_read(f); - node->flags2=lib3ds_word_read(f); - node->parent_id=lib3ds_word_read(f); + node->flags1=lib3ds_word_read(strm); + node->flags2=lib3ds_word_read(strm); + node->parent_id=lib3ds_word_read(strm); lib3ds_chunk_dump_info(" NAME =%s", node->name); lib3ds_chunk_dump_info(" PARENT=%d", (short)node->parent_id); } @@ -454,7 +454,7 @@ lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *, FILE *f) if (node->type==LIB3DS_OBJECT_NODE) { int i; for (i=0; i<3; ++i) { - node->data.object.pivot[i]=lib3ds_float_read(f); + node->data.object.pivot[i]=lib3ds_float_read(strm); } } else { @@ -465,7 +465,7 @@ lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *, FILE *f) case LIB3DS_INSTANCE_NAME: { if (node->type==LIB3DS_OBJECT_NODE) { - if (!lib3ds_string_read(node->data.object.instance, 64, f)) { + if (!lib3ds_string_read(node->data.object.instance, 64, strm)) { return(LIB3DS_FALSE); } } @@ -479,10 +479,10 @@ lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *, FILE *f) if (node->type==LIB3DS_OBJECT_NODE) { int i; for (i=0; i<3; ++i) { - node->data.object.bbox_min[i]=lib3ds_float_read(f); + node->data.object.bbox_min[i]=lib3ds_float_read(strm); } for (i=0; i<3; ++i) { - node->data.object.bbox_max[i]=lib3ds_float_read(f); + node->data.object.bbox_max[i]=lib3ds_float_read(strm); } } else { @@ -496,10 +496,10 @@ lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *, FILE *f) switch (node->type) { case LIB3DS_AMBIENT_NODE: - result=lib3ds_lin3_track_read(&node->data.ambient.col_track, f); + result=lib3ds_lin3_track_read(&node->data.ambient.col_track, strm); break; case LIB3DS_LIGHT_NODE: - result=lib3ds_lin3_track_read(&node->data.light.col_track, f); + result=lib3ds_lin3_track_read(&node->data.light.col_track, strm); break; default: lib3ds_chunk_unknown(chunk); @@ -515,19 +515,19 @@ lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *, FILE *f) switch (node->type) { case LIB3DS_OBJECT_NODE: - result=lib3ds_lin3_track_read(&node->data.object.pos_track, f); + result=lib3ds_lin3_track_read(&node->data.object.pos_track, strm); break; case LIB3DS_CAMERA_NODE: - result=lib3ds_lin3_track_read(&node->data.camera.pos_track, f); + result=lib3ds_lin3_track_read(&node->data.camera.pos_track, strm); break; case LIB3DS_TARGET_NODE: - result=lib3ds_lin3_track_read(&node->data.target.pos_track, f); + result=lib3ds_lin3_track_read(&node->data.target.pos_track, strm); break; case LIB3DS_LIGHT_NODE: - result=lib3ds_lin3_track_read(&node->data.light.pos_track, f); + result=lib3ds_lin3_track_read(&node->data.light.pos_track, strm); break; case LIB3DS_SPOT_NODE: - result=lib3ds_lin3_track_read(&node->data.spot.pos_track, f); + result=lib3ds_lin3_track_read(&node->data.spot.pos_track, strm); break; default: lib3ds_chunk_unknown(chunk); @@ -540,7 +540,7 @@ lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *, FILE *f) case LIB3DS_ROT_TRACK_TAG: { if (node->type==LIB3DS_OBJECT_NODE) { - if (!lib3ds_quat_track_read(&node->data.object.rot_track, f)) { + if (!lib3ds_quat_track_read(&node->data.object.rot_track, strm)) { return(LIB3DS_FALSE); } } @@ -552,7 +552,7 @@ lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *, FILE *f) case LIB3DS_SCL_TRACK_TAG: { if (node->type==LIB3DS_OBJECT_NODE) { - if (!lib3ds_lin3_track_read(&node->data.object.scl_track, f)) { + if (!lib3ds_lin3_track_read(&node->data.object.scl_track, strm)) { return(LIB3DS_FALSE); } } @@ -564,7 +564,7 @@ lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *, FILE *f) case LIB3DS_FOV_TRACK_TAG: { if (node->type==LIB3DS_CAMERA_NODE) { - if (!lib3ds_lin1_track_read(&node->data.camera.fov_track, f)) { + if (!lib3ds_lin1_track_read(&node->data.camera.fov_track, strm)) { return(LIB3DS_FALSE); } } @@ -576,7 +576,7 @@ lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *, FILE *f) case LIB3DS_HOT_TRACK_TAG: { if (node->type==LIB3DS_LIGHT_NODE) { - if (!lib3ds_lin1_track_read(&node->data.light.hotspot_track, f)) { + if (!lib3ds_lin1_track_read(&node->data.light.hotspot_track, strm)) { return(LIB3DS_FALSE); } } @@ -588,7 +588,7 @@ lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *, FILE *f) case LIB3DS_FALL_TRACK_TAG: { if (node->type==LIB3DS_LIGHT_NODE) { - if (!lib3ds_lin1_track_read(&node->data.light.falloff_track, f)) { + if (!lib3ds_lin1_track_read(&node->data.light.falloff_track, strm)) { return(LIB3DS_FALSE); } } @@ -603,10 +603,10 @@ lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *, FILE *f) switch (node->type) { case LIB3DS_CAMERA_NODE: - result=lib3ds_lin1_track_read(&node->data.camera.roll_track, f); + result=lib3ds_lin1_track_read(&node->data.camera.roll_track, strm); break; case LIB3DS_LIGHT_NODE: - result=lib3ds_lin1_track_read(&node->data.light.roll_track, f); + result=lib3ds_lin1_track_read(&node->data.light.roll_track, strm); break; default: lib3ds_chunk_unknown(chunk); @@ -619,7 +619,7 @@ lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *, FILE *f) case LIB3DS_HIDE_TRACK_TAG: { if (node->type==LIB3DS_OBJECT_NODE) { - if (!lib3ds_bool_track_read(&node->data.object.hide_track, f)) { + if (!lib3ds_bool_track_read(&node->data.object.hide_track, strm)) { return(LIB3DS_FALSE); } } @@ -631,7 +631,7 @@ lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *, FILE *f) case LIB3DS_MORPH_SMOOTH: { if (node->type==LIB3DS_OBJECT_NODE) { - node->data.object.morph_smooth=lib3ds_float_read(f); + node->data.object.morph_smooth=lib3ds_float_read(strm); } else { lib3ds_chunk_unknown(chunk); @@ -641,7 +641,7 @@ lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *, FILE *f) case LIB3DS_MORPH_TRACK_TAG: { if (node->type==LIB3DS_OBJECT_NODE) { - if (!lib3ds_morph_track_read(&node->data.object.morph_track, f)) { + if (!lib3ds_morph_track_read(&node->data.object.morph_track, strm)) { return(LIB3DS_FALSE); } } @@ -655,7 +655,7 @@ lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *, FILE *f) } } - lib3ds_chunk_read_end(&c, f); + lib3ds_chunk_read_end(&c, strm); return(LIB3DS_TRUE); } @@ -664,7 +664,7 @@ lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *, FILE *f) * \ingroup node */ Lib3dsBool -lib3ds_node_write(Lib3dsNode *node, Lib3dsFile *file, FILE *f) +lib3ds_node_write(Lib3dsNode *node, Lib3dsFile *file, iostream *strm) { Lib3dsChunk c; @@ -695,7 +695,7 @@ lib3ds_node_write(Lib3dsNode *node, Lib3dsFile *file, FILE *f) default: return(LIB3DS_FALSE); } - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } @@ -703,19 +703,19 @@ lib3ds_node_write(Lib3dsNode *node, Lib3dsFile *file, FILE *f) Lib3dsChunk c; c.chunk=LIB3DS_NODE_ID; c.size=8; - lib3ds_chunk_write(&c,f); - lib3ds_intw_write(node->node_id,f); + lib3ds_chunk_write(&c,strm); + lib3ds_intw_write(node->node_id,strm); } { /*---- LIB3DS_NODE_HDR ----*/ Lib3dsChunk c; c.chunk=LIB3DS_NODE_HDR; c.size=6+ 1+strlen(node->name) +2+2+2; - lib3ds_chunk_write(&c,f); - lib3ds_string_write(node->name,f); - lib3ds_word_write(node->flags1,f); - lib3ds_word_write(node->flags2,f); - lib3ds_word_write(node->parent_id,f); + lib3ds_chunk_write(&c,strm); + lib3ds_string_write(node->name,strm); + lib3ds_word_write(node->flags1,strm); + lib3ds_word_write(node->flags2,strm); + lib3ds_word_write(node->parent_id,strm); } switch (c.chunk) { @@ -723,13 +723,13 @@ lib3ds_node_write(Lib3dsNode *node, Lib3dsFile *file, FILE *f) { /*---- LIB3DS_COL_TRACK_TAG ----*/ Lib3dsChunk c; c.chunk=LIB3DS_COL_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_lin3_track_write(&node->data.ambient.col_track,f)) { + if (!lib3ds_lin3_track_write(&node->data.ambient.col_track,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } } @@ -739,8 +739,8 @@ lib3ds_node_write(Lib3dsNode *node, Lib3dsFile *file, FILE *f) Lib3dsChunk c; c.chunk=LIB3DS_PIVOT; c.size=18; - lib3ds_chunk_write(&c,f); - lib3ds_vector_write(node->data.object.pivot,f); + lib3ds_chunk_write(&c,strm); + lib3ds_vector_write(node->data.object.pivot,strm); } { /*---- LIB3DS_INSTANCE_NAME ----*/ Lib3dsChunk c; @@ -750,8 +750,8 @@ lib3ds_node_write(Lib3dsNode *node, Lib3dsFile *file, FILE *f) c.chunk=LIB3DS_INSTANCE_NAME; c.size=6+1+strlen(name); - lib3ds_chunk_write(&c,f); - lib3ds_string_write(name,f); + lib3ds_chunk_write(&c,strm); + lib3ds_string_write(name,strm); } } { @@ -767,60 +767,60 @@ lib3ds_node_write(Lib3dsNode *node, Lib3dsFile *file, FILE *f) Lib3dsChunk c; c.chunk=LIB3DS_BOUNDBOX; c.size=30; - lib3ds_chunk_write(&c,f); - lib3ds_vector_write(node->data.object.bbox_min, f); - lib3ds_vector_write(node->data.object.bbox_max, f); + lib3ds_chunk_write(&c,strm); + lib3ds_vector_write(node->data.object.bbox_min, strm); + lib3ds_vector_write(node->data.object.bbox_max, strm); } } { /*---- LIB3DS_POS_TRACK_TAG ----*/ Lib3dsChunk c; c.chunk=LIB3DS_POS_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_lin3_track_write(&node->data.object.pos_track,f)) { + if (!lib3ds_lin3_track_write(&node->data.object.pos_track,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } } { /*---- LIB3DS_ROT_TRACK_TAG ----*/ Lib3dsChunk c; c.chunk=LIB3DS_ROT_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_quat_track_write(&node->data.object.rot_track,f)) { + if (!lib3ds_quat_track_write(&node->data.object.rot_track,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } } { /*---- LIB3DS_SCL_TRACK_TAG ----*/ Lib3dsChunk c; c.chunk=LIB3DS_SCL_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_lin3_track_write(&node->data.object.scl_track,f)) { + if (!lib3ds_lin3_track_write(&node->data.object.scl_track,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } } if (node->data.object.hide_track.keyL) { /*---- LIB3DS_HIDE_TRACK_TAG ----*/ Lib3dsChunk c; c.chunk=LIB3DS_HIDE_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_bool_track_write(&node->data.object.hide_track,f)) { + if (!lib3ds_bool_track_write(&node->data.object.hide_track,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } } @@ -828,47 +828,47 @@ lib3ds_node_write(Lib3dsNode *node, Lib3dsFile *file, FILE *f) Lib3dsChunk c; c.chunk=LIB3DS_MORPH_SMOOTH; c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(node->data.object.morph_smooth,f); + lib3ds_chunk_write(&c,strm); + lib3ds_float_write(node->data.object.morph_smooth,strm); } break; case LIB3DS_CAMERA_NODE_TAG: { /*---- LIB3DS_POS_TRACK_TAG ----*/ Lib3dsChunk c; c.chunk=LIB3DS_POS_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_lin3_track_write(&node->data.camera.pos_track,f)) { + if (!lib3ds_lin3_track_write(&node->data.camera.pos_track,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } } { /*---- LIB3DS_FOV_TRACK_TAG ----*/ Lib3dsChunk c; c.chunk=LIB3DS_FOV_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_lin1_track_write(&node->data.camera.fov_track,f)) { + if (!lib3ds_lin1_track_write(&node->data.camera.fov_track,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } } { /*---- LIB3DS_ROLL_TRACK_TAG ----*/ Lib3dsChunk c; c.chunk=LIB3DS_ROLL_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_lin1_track_write(&node->data.camera.roll_track,f)) { + if (!lib3ds_lin1_track_write(&node->data.camera.roll_track,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } } @@ -877,13 +877,13 @@ lib3ds_node_write(Lib3dsNode *node, Lib3dsFile *file, FILE *f) { /*---- LIB3DS_POS_TRACK_TAG ----*/ Lib3dsChunk c; c.chunk=LIB3DS_POS_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_lin3_track_write(&node->data.target.pos_track,f)) { + if (!lib3ds_lin3_track_write(&node->data.target.pos_track,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } } @@ -892,26 +892,26 @@ lib3ds_node_write(Lib3dsNode *node, Lib3dsFile *file, FILE *f) { /*---- LIB3DS_POS_TRACK_TAG ----*/ Lib3dsChunk c; c.chunk=LIB3DS_POS_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_lin3_track_write(&node->data.light.pos_track,f)) { + if (!lib3ds_lin3_track_write(&node->data.light.pos_track,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } } { /*---- LIB3DS_COL_TRACK_TAG ----*/ Lib3dsChunk c; c.chunk=LIB3DS_COL_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_lin3_track_write(&node->data.light.col_track,f)) { + if (!lib3ds_lin3_track_write(&node->data.light.col_track,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } } @@ -920,65 +920,65 @@ lib3ds_node_write(Lib3dsNode *node, Lib3dsFile *file, FILE *f) { /*---- LIB3DS_POS_TRACK_TAG ----*/ Lib3dsChunk c; c.chunk=LIB3DS_POS_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_lin3_track_write(&node->data.light.pos_track,f)) { + if (!lib3ds_lin3_track_write(&node->data.light.pos_track,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } } { /*---- LIB3DS_COL_TRACK_TAG ----*/ Lib3dsChunk c; c.chunk=LIB3DS_COL_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_lin3_track_write(&node->data.light.col_track,f)) { + if (!lib3ds_lin3_track_write(&node->data.light.col_track,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } } { /*---- LIB3DS_HOT_TRACK_TAG ----*/ Lib3dsChunk c; c.chunk=LIB3DS_HOT_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_lin1_track_write(&node->data.light.hotspot_track,f)) { + if (!lib3ds_lin1_track_write(&node->data.light.hotspot_track,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } } { /*---- LIB3DS_FALL_TRACK_TAG ----*/ Lib3dsChunk c; c.chunk=LIB3DS_FALL_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_lin1_track_write(&node->data.light.falloff_track,f)) { + if (!lib3ds_lin1_track_write(&node->data.light.falloff_track,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } } { /*---- LIB3DS_ROLL_TRACK_TAG ----*/ Lib3dsChunk c; c.chunk=LIB3DS_ROLL_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_lin1_track_write(&node->data.light.roll_track,f)) { + if (!lib3ds_lin1_track_write(&node->data.light.roll_track,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } } @@ -987,13 +987,13 @@ lib3ds_node_write(Lib3dsNode *node, Lib3dsFile *file, FILE *f) { /*---- LIB3DS_POS_TRACK_TAG ----*/ Lib3dsChunk c; c.chunk=LIB3DS_POS_TRACK_TAG; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_lin3_track_write(&node->data.spot.pos_track,f)) { + if (!lib3ds_lin3_track_write(&node->data.spot.pos_track,strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } } @@ -1002,7 +1002,7 @@ lib3ds_node_write(Lib3dsNode *node, Lib3dsFile *file, FILE *f) return(LIB3DS_FALSE); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } return(LIB3DS_TRUE); diff --git a/src/osgPlugins/3ds/node.h b/src/osgPlugins/3ds/node.h index 88666ee14..dd751c387 100644 --- a/src/osgPlugins/3ds/node.h +++ b/src/osgPlugins/3ds/node.h @@ -27,10 +27,14 @@ #include "tracks.h" #endif +#include +using namespace std; + #ifdef __cplusplus extern "C" { #endif + /*! * Scene graph ambient color node data * \ingroup node @@ -159,8 +163,8 @@ extern LIB3DSAPI Lib3dsNode* lib3ds_node_by_name(Lib3dsNode *node, const char* n Lib3dsNodeTypes type); extern LIB3DSAPI Lib3dsNode* lib3ds_node_by_id(Lib3dsNode *node, Lib3dsWord node_id); extern LIB3DSAPI void lib3ds_node_dump(Lib3dsNode *node, Lib3dsIntd level); -extern LIB3DSAPI Lib3dsBool lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *file, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_node_write(Lib3dsNode *node, Lib3dsFile *file, FILE *f); +extern LIB3DSAPI Lib3dsBool lib3ds_node_read(Lib3dsNode *node, Lib3dsFile *file, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_node_write(Lib3dsNode *node, Lib3dsFile *file, iostream *strm); #ifdef __cplusplus } diff --git a/src/osgPlugins/3ds/readwrite.cpp b/src/osgPlugins/3ds/readwrite.cpp index 139d3af66..f23b939b8 100644 --- a/src/osgPlugins/3ds/readwrite.cpp +++ b/src/osgPlugins/3ds/readwrite.cpp @@ -23,13 +23,13 @@ #include "readwrite.h" #include + /*! * \defgroup readwrite Portable Binary Input/Ouput * * \author J.E. Hoffmann */ - static bool s_requiresByteSwap = false; extern LIB3DSAPI void setByteOrder() @@ -49,13 +49,12 @@ extern LIB3DSAPI void setByteOrder() * \return The byte read. */ Lib3dsByte -lib3ds_byte_read(FILE *f) +lib3ds_byte_read(iostream *strm) { Lib3dsByte b; - ASSERT(f); - int result = fread(&b,1,1,f); - if (result==0) return 0; + ASSERT(strm); + strm->read((char*)&b,1); return(b); } @@ -68,15 +67,13 @@ lib3ds_byte_read(FILE *f) * \return The word read. */ Lib3dsWord -lib3ds_word_read(FILE *f) +lib3ds_word_read(iostream *strm) { Lib3dsByte b[2]; Lib3dsWord w; - ASSERT(f); - int result = fread(b,2,1,f); - if (result==0) return 0; - + ASSERT(strm); + strm->read((char*)&b,2); w=((Lib3dsWord)b[1] << 8) | ((Lib3dsWord)b[0]); return(w); @@ -93,15 +90,13 @@ lib3ds_word_read(FILE *f) * \return The dword read. */ Lib3dsDword -lib3ds_dword_read(FILE *f) +lib3ds_dword_read(iostream *strm) { Lib3dsByte b[4]; Lib3dsDword d; - ASSERT(f); - int result = fread(b,4,1,f); - if (result==0) return 0; - + ASSERT(strm); + strm->read((char*)&b,4); d=((Lib3dsDword)b[3] << 24) | ((Lib3dsDword)b[2] << 16) | ((Lib3dsDword)b[1] << 8) | @@ -120,14 +115,12 @@ lib3ds_dword_read(FILE *f) * \return The signed byte read. */ Lib3dsIntb -lib3ds_intb_read(FILE *f) +lib3ds_intb_read(iostream *strm) { Lib3dsIntb b; - ASSERT(f); - int result = fread(&b,1,1,f); - if (result==0) return 0; - + ASSERT(strm); + strm->read((char*)&b,1); return(b); } @@ -142,13 +135,12 @@ lib3ds_intb_read(FILE *f) * \return The signed word read. */ Lib3dsIntw -lib3ds_intw_read(FILE *f) +lib3ds_intw_read(iostream *strm) { Lib3dsByte b[2]; - ASSERT(f); - int result = fread(b,2,1,f); - if (result==0) return 0; + ASSERT(strm); + strm->read((char*)&b,2); if (s_requiresByteSwap) { @@ -169,13 +161,12 @@ lib3ds_intw_read(FILE *f) * \return The signed dword read. */ Lib3dsIntd -lib3ds_intd_read(FILE *f) +lib3ds_intd_read(iostream *strm) { - Lib3dsByte b[4]; + Lib3dsByte b[4]; - ASSERT(f); - int result = fread(b,4,1,f); - if (result==0) return 0; + ASSERT(strm); + strm->read((char*)&b,4); if (s_requiresByteSwap) { @@ -183,6 +174,7 @@ lib3ds_intd_read(FILE *f) } return (*((Lib3dsIntd*)b)); + } @@ -196,13 +188,13 @@ lib3ds_intd_read(FILE *f) * \return The float read. */ Lib3dsFloat -lib3ds_float_read(FILE *f) +lib3ds_float_read(iostream *strm) { Lib3dsByte b[4]; - ASSERT(f); - int result = fread(b,4,1,f); - if (result==0) return 0; + ASSERT(strm); + b[0]=b[1]=b[2]=b[3]=0; + strm->read((char*)&b,4); if (s_requiresByteSwap) { @@ -225,18 +217,18 @@ lib3ds_float_read(FILE *f) * \return The float read. */ Lib3dsBool -lib3ds_vector_read(Lib3dsVector v, FILE *f) +lib3ds_vector_read(Lib3dsVector v, iostream *strm) { - v[0]=lib3ds_float_read(f); - v[1]=lib3ds_float_read(f); - v[2]=lib3ds_float_read(f); + v[0]=lib3ds_float_read(strm); + v[1]=lib3ds_float_read(strm); + v[2]=lib3ds_float_read(strm); - if (ferror(f)) { + if (strm->fail()) { return(LIB3DS_FALSE); } - + /*printf("lib3ds_vector_read %f %f %f\n",v[0],v[1],v[2]);*/ - + return(LIB3DS_TRUE); } @@ -245,13 +237,13 @@ lib3ds_vector_read(Lib3dsVector v, FILE *f) * \ingroup readwrite */ Lib3dsBool -lib3ds_rgb_read(Lib3dsRgb rgb, FILE *f) +lib3ds_rgb_read(Lib3dsRgb rgb, iostream *strm) { - rgb[0]=lib3ds_float_read(f); - rgb[1]=lib3ds_float_read(f); - rgb[2]=lib3ds_float_read(f); + rgb[0]=lib3ds_float_read(strm); + rgb[1]=lib3ds_float_read(strm); + rgb[2]=lib3ds_float_read(strm); - if (ferror(f)) { + if (strm->fail()) { return(LIB3DS_FALSE); } /*printf("lib3ds_rgb_read %f %f %f\n",rgb[0],rgb[1],rgb[2]);*/ @@ -272,22 +264,24 @@ lib3ds_rgb_read(Lib3dsRgb rgb, FILE *f) * \return True on success, False otherwise. */ Lib3dsBool -lib3ds_string_read(char *s, int buflen, FILE *f) +lib3ds_string_read(char *s, int buflen, iostream *strm) { int k=0; - ASSERT(f); - while ((*s++=char(fgetc(f)))!=0) { - if (++k>=buflen) { - return(LIB3DS_FALSE); - } - } - if (ferror(f)) { + ASSERT(s); + s--; + do + { + s++; + k++; + strm->read(s,1); + } while ((*s!=0) && (kfail()) { return(LIB3DS_FALSE); } return(LIB3DS_TRUE); } - /*! * \ingroup readwrite * @@ -299,10 +293,11 @@ lib3ds_string_read(char *s, int buflen, FILE *f) * \return True on success, False otherwise. */ Lib3dsBool -lib3ds_byte_write(Lib3dsByte b, FILE *f) +lib3ds_byte_write(Lib3dsByte b, iostream *strm) { - ASSERT(f); - if (fwrite(&b,1,1,f)!=1) { + ASSERT(strm); + strm->write((char*)&b,1); + if (strm->fail()) { return(LIB3DS_FALSE); } return(LIB3DS_TRUE); @@ -320,14 +315,15 @@ lib3ds_byte_write(Lib3dsByte b, FILE *f) * \return True on success, False otherwise. */ Lib3dsBool -lib3ds_word_write(Lib3dsWord w, FILE *f) +lib3ds_word_write(Lib3dsWord w, iostream *strm) { Lib3dsByte b[2]; - ASSERT(f); + ASSERT(strm); b[1]=(Lib3dsByte)(((Lib3dsWord)w & 0xFF00) >> 8); b[0]=(Lib3dsByte)((Lib3dsWord)w & 0x00FF); - if (fwrite(b,2,1,f)!=1) { + strm->write((char*)b,2); + if (strm->fail()) { return(LIB3DS_FALSE); } return(LIB3DS_TRUE); @@ -345,16 +341,18 @@ lib3ds_word_write(Lib3dsWord w, FILE *f) * \return True on success, False otherwise. */ Lib3dsBool -lib3ds_dword_write(Lib3dsDword d, FILE *f) +lib3ds_dword_write(Lib3dsDword d, iostream *strm) { Lib3dsByte b[4]; - ASSERT(f); + ASSERT(strm); b[3]=(Lib3dsByte)(((Lib3dsDword)d & 0xFF000000) >> 24); b[2]=(Lib3dsByte)(((Lib3dsDword)d & 0x00FF0000) >> 16); b[1]=(Lib3dsByte)(((Lib3dsDword)d & 0x0000FF00) >> 8); b[0]=(Lib3dsByte)(((Lib3dsDword)d & 0x000000FF)); - if (fwrite(b,4,1,f)!=1) { + + strm->write((char*)b,4); + if (strm->fail()) { return(LIB3DS_FALSE); } return(LIB3DS_TRUE); @@ -372,10 +370,11 @@ lib3ds_dword_write(Lib3dsDword d, FILE *f) * \return True on success, False otherwise. */ Lib3dsBool -lib3ds_intb_write(Lib3dsIntb b, FILE *f) +lib3ds_intb_write(Lib3dsIntb b, iostream *strm) { - ASSERT(f); - if (fwrite(&b,1,1,f)!=1) { + ASSERT(strm); + strm->write((char*)b,1); + if (strm->fail()) { return(LIB3DS_FALSE); } return(LIB3DS_TRUE); @@ -393,14 +392,16 @@ lib3ds_intb_write(Lib3dsIntb b, FILE *f) * \return True on success, False otherwise. */ Lib3dsBool -lib3ds_intw_write(Lib3dsIntw w, FILE *f) +lib3ds_intw_write(Lib3dsIntw w, iostream *strm) { Lib3dsByte b[2]; - ASSERT(f); + ASSERT(strm); b[1]=(Lib3dsByte)(((Lib3dsWord)w & 0xFF00) >> 8); b[0]=(Lib3dsByte)((Lib3dsWord)w & 0x00FF); - if (fwrite(b,2,1,f)!=1) { + + strm->write((char*)b,2); + if (strm->fail()) { return(LIB3DS_FALSE); } return(LIB3DS_TRUE); @@ -418,16 +419,18 @@ lib3ds_intw_write(Lib3dsIntw w, FILE *f) * \return True on success, False otherwise. */ Lib3dsBool -lib3ds_intd_write(Lib3dsIntd d, FILE *f) +lib3ds_intd_write(Lib3dsIntd d, iostream *strm) { Lib3dsByte b[4]; - ASSERT(f); + ASSERT(strm); b[3]=(Lib3dsByte)(((Lib3dsDword)d & 0xFF000000) >> 24); b[2]=(Lib3dsByte)(((Lib3dsDword)d & 0x00FF0000) >> 16); b[1]=(Lib3dsByte)(((Lib3dsDword)d & 0x0000FF00) >> 8); b[0]=(Lib3dsByte)(((Lib3dsDword)d & 0x000000FF)); - if (fwrite(b,4,1,f)!=1) { + + strm->write((char*)b,4); + if (strm->fail()) { return(LIB3DS_FALSE); } return(LIB3DS_TRUE); @@ -444,11 +447,10 @@ lib3ds_intd_write(Lib3dsIntd d, FILE *f) * * \return True on success, False otherwise. */ - - Lib3dsBool -lib3ds_float_write(Lib3dsFloat l, FILE *f) +lib3ds_float_write(Lib3dsFloat l, iostream *strm) { + ASSERT(strm); Lib3dsByte b[4]; Lib3dsByte* ptr = (Lib3dsByte*) (&l); @@ -467,8 +469,9 @@ lib3ds_float_write(Lib3dsFloat l, FILE *f) b[2] = *ptr++; b[3] = *ptr++; } - - if (fwrite(b,4,1,f)!=1) { + + strm->write((char*)b,4); + if (strm->fail()) { return(LIB3DS_FALSE); } return(LIB3DS_TRUE); @@ -485,15 +488,15 @@ lib3ds_float_write(Lib3dsFloat l, FILE *f) * \param f Input file stream. */ Lib3dsBool -lib3ds_vector_write(Lib3dsVector v, FILE *f) +lib3ds_vector_write(Lib3dsVector v, iostream *strm) { - if (!lib3ds_float_write(v[0], f)) { + if (!lib3ds_float_write(v[0], strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_float_write(v[1], f)) { + if (!lib3ds_float_write(v[1], strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_float_write(v[2], f)) { + if (!lib3ds_float_write(v[2], strm)) { return(LIB3DS_FALSE); } return(LIB3DS_TRUE); @@ -504,15 +507,15 @@ lib3ds_vector_write(Lib3dsVector v, FILE *f) * \ingroup readwrite */ Lib3dsBool -lib3ds_rgb_write(Lib3dsRgb rgb, FILE *f) +lib3ds_rgb_write(Lib3dsRgb rgb, iostream *strm) { - if (!lib3ds_float_write(rgb[0], f)) { + if (!lib3ds_float_write(rgb[0], strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_float_write(rgb[1], f)) { + if (!lib3ds_float_write(rgb[1], strm)) { return(LIB3DS_FALSE); } - if (!lib3ds_float_write(rgb[2], f)) { + if (!lib3ds_float_write(rgb[2], strm)) { return(LIB3DS_FALSE); } return(LIB3DS_TRUE); @@ -530,12 +533,12 @@ lib3ds_rgb_write(Lib3dsRgb rgb, FILE *f) * \return True on success, False otherwise. */ Lib3dsBool -lib3ds_string_write(const char *s, FILE *f) +lib3ds_string_write(const char *s, iostream *strm) { ASSERT(s); - ASSERT(f); - do fputc(*s,f); while (*s++); - if (ferror(f)) { + ASSERT(strm); + do strm->write(s,1); while (*s++); + if (strm->fail()) { return(LIB3DS_FALSE); } return(LIB3DS_TRUE); diff --git a/src/osgPlugins/3ds/readwrite.h b/src/osgPlugins/3ds/readwrite.h index 4d1e6a597..650ec2ad7 100644 --- a/src/osgPlugins/3ds/readwrite.h +++ b/src/osgPlugins/3ds/readwrite.h @@ -27,32 +27,34 @@ #include "types.h" #endif +#include +using namespace std; + #ifdef __cplusplus extern "C" { #endif extern LIB3DSAPI void setByteOrder(); - -extern LIB3DSAPI Lib3dsByte lib3ds_byte_read(FILE *f); -extern LIB3DSAPI Lib3dsWord lib3ds_word_read(FILE *f); -extern LIB3DSAPI Lib3dsDword lib3ds_dword_read(FILE *f); -extern LIB3DSAPI Lib3dsIntb lib3ds_intb_read(FILE *f); -extern LIB3DSAPI Lib3dsIntw lib3ds_intw_read(FILE *f); -extern LIB3DSAPI Lib3dsIntd lib3ds_intd_read(FILE *f); -extern LIB3DSAPI Lib3dsFloat lib3ds_float_read(FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_vector_read(Lib3dsVector v, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_rgb_read(Lib3dsRgb rgb, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_string_read(char *s, int buflen, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_byte_write(Lib3dsByte b, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_word_write(Lib3dsWord w, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_dword_write(Lib3dsDword d, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_intb_write(Lib3dsIntb b, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_intw_write(Lib3dsIntw w, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_intd_write(Lib3dsIntd d, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_float_write(Lib3dsFloat l, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_vector_write(Lib3dsVector v, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_rgb_write(Lib3dsRgb rgb, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_string_write(const char *s, FILE *f); +extern LIB3DSAPI Lib3dsByte lib3ds_byte_read(iostream *strm); +extern LIB3DSAPI Lib3dsWord lib3ds_word_read(iostream *strm); +extern LIB3DSAPI Lib3dsDword lib3ds_dword_read(iostream *strm); +extern LIB3DSAPI Lib3dsIntb lib3ds_intb_read(iostream *strm); +extern LIB3DSAPI Lib3dsIntw lib3ds_intw_read(iostream *strm); +extern LIB3DSAPI Lib3dsIntd lib3ds_intd_read(iostream *strm); +extern LIB3DSAPI Lib3dsFloat lib3ds_float_read(iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_vector_read(Lib3dsVector v, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_rgb_read(Lib3dsRgb rgb, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_string_read(char *s, int buflen, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_byte_write(Lib3dsByte b, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_word_write(Lib3dsWord w, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_dword_write(Lib3dsDword d, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_intb_write(Lib3dsIntb b, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_intw_write(Lib3dsIntw w, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_intd_write(Lib3dsIntd d, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_float_write(Lib3dsFloat l, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_vector_write(Lib3dsVector v, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_rgb_write(Lib3dsRgb rgb, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_string_write(const char *s, iostream *strm); #ifdef __cplusplus } diff --git a/src/osgPlugins/3ds/shadow.cpp b/src/osgPlugins/3ds/shadow.cpp index d8951a39d..98f5803f9 100644 --- a/src/osgPlugins/3ds/shadow.cpp +++ b/src/osgPlugins/3ds/shadow.cpp @@ -37,48 +37,48 @@ * \ingroup shadow */ Lib3dsBool -lib3ds_shadow_read(Lib3dsShadow *shadow, FILE *f) +lib3ds_shadow_read(Lib3dsShadow *shadow, iostream *strm) { Lib3dsChunk c; - if (!lib3ds_chunk_read(&c, f)) { + if (!lib3ds_chunk_read(&c, strm)) { return(LIB3DS_FALSE); } switch (c.chunk) { case LIB3DS_SHADOW_MAP_SIZE: { - shadow->map_size=lib3ds_intw_read(f); + shadow->map_size=lib3ds_intw_read(strm); } break; case LIB3DS_LO_SHADOW_BIAS: { - shadow->lo_bias=lib3ds_float_read(f); + shadow->lo_bias=lib3ds_float_read(strm); } break; case LIB3DS_HI_SHADOW_BIAS: { - shadow->hi_bias=lib3ds_float_read(f); + shadow->hi_bias=lib3ds_float_read(strm); } break; case LIB3DS_SHADOW_SAMPLES: { - shadow->samples=lib3ds_intw_read(f); + shadow->samples=lib3ds_intw_read(strm); } break; case LIB3DS_SHADOW_RANGE: { - shadow->range=lib3ds_intd_read(f); + shadow->range=lib3ds_intd_read(strm); } break; case LIB3DS_SHADOW_FILTER: { - shadow->filter=lib3ds_float_read(f); + shadow->filter=lib3ds_float_read(strm); } break; case LIB3DS_RAY_BIAS: { - shadow->ray_bias=lib3ds_float_read(f); + shadow->ray_bias=lib3ds_float_read(strm); } break; } @@ -91,61 +91,61 @@ lib3ds_shadow_read(Lib3dsShadow *shadow, FILE *f) * \ingroup shadow */ Lib3dsBool -lib3ds_shadow_write(Lib3dsShadow *shadow, FILE *f) +lib3ds_shadow_write(Lib3dsShadow *shadow, iostream *strm) { if (fabs(shadow->lo_bias)>LIB3DS_EPSILON) { /*---- LIB3DS_LO_SHADOW_BIAS ----*/ Lib3dsChunk c; c.chunk=LIB3DS_LO_SHADOW_BIAS; c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(shadow->lo_bias,f); + lib3ds_chunk_write(&c,strm); + lib3ds_float_write(shadow->lo_bias,strm); } if (fabs(shadow->hi_bias)>LIB3DS_EPSILON) { /*---- LIB3DS_HI_SHADOW_BIAS ----*/ Lib3dsChunk c; c.chunk=LIB3DS_HI_SHADOW_BIAS; c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(shadow->hi_bias,f); + lib3ds_chunk_write(&c,strm); + lib3ds_float_write(shadow->hi_bias,strm); } if (shadow->map_size) { /*---- LIB3DS_SHADOW_MAP_SIZE ----*/ Lib3dsChunk c; c.chunk=LIB3DS_SHADOW_MAP_SIZE; c.size=8; - lib3ds_chunk_write(&c,f); - lib3ds_intw_write(shadow->map_size,f); + lib3ds_chunk_write(&c,strm); + lib3ds_intw_write(shadow->map_size,strm); } if (shadow->samples) { /*---- LIB3DS_SHADOW_SAMPLES ----*/ Lib3dsChunk c; c.chunk=LIB3DS_SHADOW_SAMPLES; c.size=8; - lib3ds_chunk_write(&c,f); - lib3ds_intw_write(shadow->samples,f); + lib3ds_chunk_write(&c,strm); + lib3ds_intw_write(shadow->samples,strm); } if (shadow->range) { /*---- LIB3DS_SHADOW_RANGE ----*/ Lib3dsChunk c; c.chunk=LIB3DS_SHADOW_RANGE; c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_intd_write(shadow->range,f); + lib3ds_chunk_write(&c,strm); + lib3ds_intd_write(shadow->range,strm); } if (fabs(shadow->filter)>LIB3DS_EPSILON) { /*---- LIB3DS_SHADOW_FILTER ----*/ Lib3dsChunk c; c.chunk=LIB3DS_SHADOW_FILTER; c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(shadow->filter,f); + lib3ds_chunk_write(&c,strm); + lib3ds_float_write(shadow->filter,strm); } if (fabs(shadow->ray_bias)>LIB3DS_EPSILON) { /*---- LIB3DS_RAY_BIAS ----*/ Lib3dsChunk c; c.chunk=LIB3DS_RAY_BIAS; c.size=10; - lib3ds_chunk_write(&c,f); - lib3ds_float_write(shadow->ray_bias,f); + lib3ds_chunk_write(&c,strm); + lib3ds_float_write(shadow->ray_bias,strm); } return(LIB3DS_TRUE); } diff --git a/src/osgPlugins/3ds/shadow.h b/src/osgPlugins/3ds/shadow.h index 606c46bb3..ab2f4f284 100644 --- a/src/osgPlugins/3ds/shadow.h +++ b/src/osgPlugins/3ds/shadow.h @@ -27,6 +27,9 @@ #include "types.h" #endif +#include +using namespace std; + #ifdef __cplusplus extern "C" { #endif @@ -45,8 +48,8 @@ struct _Lib3dsShadow { Lib3dsFloat ray_bias; }; -extern LIB3DSAPI Lib3dsBool lib3ds_shadow_read(Lib3dsShadow *shadow, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_shadow_write(Lib3dsShadow *shadow, FILE *f); +extern LIB3DSAPI Lib3dsBool lib3ds_shadow_read(Lib3dsShadow *shadow, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_shadow_write(Lib3dsShadow *shadow, iostream *strm); #ifdef __cplusplus } diff --git a/src/osgPlugins/3ds/tcb.cpp b/src/osgPlugins/3ds/tcb.cpp index 33d6bc609..2af3d9cec 100644 --- a/src/osgPlugins/3ds/tcb.cpp +++ b/src/osgPlugins/3ds/tcb.cpp @@ -77,28 +77,29 @@ lib3ds_tcb(Lib3dsTcb *p, Lib3dsTcb *pc, Lib3dsTcb *c, Lib3dsTcb *nc, Lib3dsTcb * * \ingroup tcb */ Lib3dsBool -lib3ds_tcb_read(Lib3dsTcb *tcb, FILE *f) +lib3ds_tcb_read(Lib3dsTcb *tcb, iostream *strm) { Lib3dsWord flags; - tcb->frame=lib3ds_intd_read(f); - tcb->flags=flags=lib3ds_word_read(f); + tcb->frame=lib3ds_intd_read(strm); + tcb->flags=flags=lib3ds_word_read(strm); if (flags&LIB3DS_USE_TENSION) { - tcb->tens=lib3ds_float_read(f); + tcb->tens=lib3ds_float_read(strm); } if (flags&LIB3DS_USE_CONTINUITY) { - tcb->cont=lib3ds_float_read(f); + tcb->cont=lib3ds_float_read(strm); } if (flags&LIB3DS_USE_BIAS) { - tcb->bias=lib3ds_float_read(f); + tcb->bias=lib3ds_float_read(strm); } if (flags&LIB3DS_USE_EASE_TO) { - tcb->ease_to=lib3ds_float_read(f); + tcb->ease_to=lib3ds_float_read(strm); } if (flags&LIB3DS_USE_EASE_FROM) { - tcb->ease_from=lib3ds_float_read(f); + tcb->ease_from=lib3ds_float_read(strm); } - if (ferror(f)) { + + if (strm->fail()){ return(LIB3DS_FALSE); } return(LIB3DS_TRUE); @@ -109,26 +110,26 @@ lib3ds_tcb_read(Lib3dsTcb *tcb, FILE *f) * \ingroup tcb */ Lib3dsBool -lib3ds_tcb_write(Lib3dsTcb *tcb, FILE *f) +lib3ds_tcb_write(Lib3dsTcb *tcb, iostream *strm) { - lib3ds_intd_write(tcb->frame,f); - lib3ds_word_write(tcb->flags,f); + lib3ds_intd_write(tcb->frame,strm); + lib3ds_word_write(tcb->flags,strm); if (tcb->flags&LIB3DS_USE_TENSION) { - lib3ds_float_write(tcb->tens,f); + lib3ds_float_write(tcb->tens,strm); } if (tcb->flags&LIB3DS_USE_CONTINUITY) { - lib3ds_float_write(tcb->cont,f); + lib3ds_float_write(tcb->cont,strm); } if (tcb->flags&LIB3DS_USE_BIAS) { - lib3ds_float_write(tcb->bias,f); + lib3ds_float_write(tcb->bias,strm); } if (tcb->flags&LIB3DS_USE_EASE_TO) { - lib3ds_float_write(tcb->ease_to,f); + lib3ds_float_write(tcb->ease_to,strm); } if (tcb->flags&LIB3DS_USE_EASE_FROM) { - lib3ds_float_write(tcb->ease_from,f); + lib3ds_float_write(tcb->ease_from,strm); } - if (ferror(f)) { + if (strm->fail()){ return(LIB3DS_FALSE); } return(LIB3DS_TRUE); diff --git a/src/osgPlugins/3ds/tcb.h b/src/osgPlugins/3ds/tcb.h index fe630e985..7b5a9aa23 100644 --- a/src/osgPlugins/3ds/tcb.h +++ b/src/osgPlugins/3ds/tcb.h @@ -27,10 +27,15 @@ #include "types.h" #endif +#include +using namespace std; + #ifdef __cplusplus extern "C" { #endif + + typedef enum _Lib3dsTcbFlags{ LIB3DS_USE_TENSION =0x0001, LIB3DS_USE_CONTINUITY =0x0002, @@ -52,8 +57,8 @@ typedef struct _Lib3dsTcb { extern LIB3DSAPI void lib3ds_tcb(Lib3dsTcb *p, Lib3dsTcb *pc, Lib3dsTcb *c, Lib3dsTcb *nc, Lib3dsTcb *n, Lib3dsFloat *ksm, Lib3dsFloat *ksp, Lib3dsFloat *kdm, Lib3dsFloat *kdp); -extern LIB3DSAPI Lib3dsBool lib3ds_tcb_read(Lib3dsTcb *tcb, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_tcb_write(Lib3dsTcb *tcb, FILE *f); +extern LIB3DSAPI Lib3dsBool lib3ds_tcb_read(Lib3dsTcb *tcb, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_tcb_write(Lib3dsTcb *tcb, iostream *strm); #ifdef __cplusplus } diff --git a/src/osgPlugins/3ds/tracks.cpp b/src/osgPlugins/3ds/tracks.cpp index 43b167d15..59f395391 100644 --- a/src/osgPlugins/3ds/tracks.cpp +++ b/src/osgPlugins/3ds/tracks.cpp @@ -196,20 +196,20 @@ lib3ds_bool_track_eval(Lib3dsBoolTrack *track, Lib3dsBool *p, Lib3dsFloat t) * \ingroup tracks */ Lib3dsBool -lib3ds_bool_track_read(Lib3dsBoolTrack *track, FILE *f) +lib3ds_bool_track_read(Lib3dsBoolTrack *track, iostream *strm) { int keys; int i; Lib3dsBoolKey *k; - track->flags=lib3ds_word_read(f); - lib3ds_dword_read(f); - lib3ds_dword_read(f); - keys=lib3ds_intd_read(f); + track->flags=lib3ds_word_read(strm); + lib3ds_dword_read(strm); + lib3ds_dword_read(strm); + keys=lib3ds_intd_read(strm); for (i=0; itcb, f)) { + if (!lib3ds_tcb_read(&k->tcb, strm)) { return(LIB3DS_FALSE); } lib3ds_bool_track_insert(track, k); @@ -223,20 +223,20 @@ lib3ds_bool_track_read(Lib3dsBoolTrack *track, FILE *f) * \ingroup tracks */ Lib3dsBool -lib3ds_bool_track_write(Lib3dsBoolTrack *track, FILE *f) +lib3ds_bool_track_write(Lib3dsBoolTrack *track, iostream *strm) { Lib3dsBoolKey *k; Lib3dsDword num=0; for (k=track->keyL; k; k=k->next) { ++num; } - lib3ds_word_write((Lib3dsWord)track->flags,f); - lib3ds_dword_write(0,f); - lib3ds_dword_write(0,f); - lib3ds_dword_write(num,f); + lib3ds_word_write((Lib3dsWord)track->flags,strm); + lib3ds_dword_write(0,strm); + lib3ds_dword_write(0,strm); + lib3ds_dword_write(num,strm); for (k=track->keyL; k; k=k->next) { - if (!lib3ds_tcb_write(&k->tcb,f)) { + if (!lib3ds_tcb_write(&k->tcb,strm)) { return(LIB3DS_FALSE); } } @@ -500,23 +500,23 @@ lib3ds_lin1_track_eval(Lib3dsLin1Track *track, Lib3dsFloat *p, Lib3dsFloat t) * \ingroup tracks */ Lib3dsBool -lib3ds_lin1_track_read(Lib3dsLin1Track *track, FILE *f) +lib3ds_lin1_track_read(Lib3dsLin1Track *track, iostream *strm) { int keys; int i; Lib3dsLin1Key *k; - track->flags=lib3ds_word_read(f); - lib3ds_dword_read(f); - lib3ds_dword_read(f); - keys=lib3ds_intd_read(f); + track->flags=lib3ds_word_read(strm); + lib3ds_dword_read(strm); + lib3ds_dword_read(strm); + keys=lib3ds_intd_read(strm); for (i=0; itcb, f)) { + if (!lib3ds_tcb_read(&k->tcb, strm)) { return(LIB3DS_FALSE); } - k->value=lib3ds_float_read(f); + k->value=lib3ds_float_read(strm); lib3ds_lin1_track_insert(track, k); } lib3ds_lin1_track_setup(track); @@ -528,23 +528,23 @@ lib3ds_lin1_track_read(Lib3dsLin1Track *track, FILE *f) * \ingroup tracks */ Lib3dsBool -lib3ds_lin1_track_write(Lib3dsLin1Track *track, FILE *f) +lib3ds_lin1_track_write(Lib3dsLin1Track *track, iostream *strm) { Lib3dsLin1Key *k; Lib3dsDword num=0; for (k=track->keyL; k; k=k->next) { ++num; } - lib3ds_word_write((Lib3dsWord)track->flags,f); - lib3ds_dword_write(0,f); - lib3ds_dword_write(0,f); - lib3ds_dword_write(num,f); + lib3ds_word_write((Lib3dsWord)track->flags,strm); + lib3ds_dword_write(0,strm); + lib3ds_dword_write(0,strm); + lib3ds_dword_write(num,strm); for (k=track->keyL; k; k=k->next) { - if (!lib3ds_tcb_write(&k->tcb,f)) { + if (!lib3ds_tcb_write(&k->tcb,strm)) { return(LIB3DS_FALSE); } - lib3ds_float_write(k->value,f); + lib3ds_float_write(k->value,strm); } return(LIB3DS_TRUE); } @@ -809,24 +809,24 @@ lib3ds_lin3_track_eval(Lib3dsLin3Track *track, Lib3dsVector p, Lib3dsFloat t) * \ingroup tracks */ Lib3dsBool -lib3ds_lin3_track_read(Lib3dsLin3Track *track, FILE *f) +lib3ds_lin3_track_read(Lib3dsLin3Track *track, iostream *strm) { int keys; int i,j; Lib3dsLin3Key *k; - track->flags=lib3ds_word_read(f); - lib3ds_dword_read(f); - lib3ds_dword_read(f); - keys=lib3ds_intd_read(f); + track->flags=lib3ds_word_read(strm); + lib3ds_dword_read(strm); + lib3ds_dword_read(strm); + keys=lib3ds_intd_read(strm); for (i=0; itcb, f)) { + if (!lib3ds_tcb_read(&k->tcb, strm)) { return(LIB3DS_FALSE); } for (j=0; j<3; ++j) { - k->value[j]=lib3ds_float_read(f); + k->value[j]=lib3ds_float_read(strm); } lib3ds_lin3_track_insert(track, k); } @@ -839,23 +839,23 @@ lib3ds_lin3_track_read(Lib3dsLin3Track *track, FILE *f) * \ingroup tracks */ Lib3dsBool -lib3ds_lin3_track_write(Lib3dsLin3Track *track, FILE *f) +lib3ds_lin3_track_write(Lib3dsLin3Track *track, iostream *strm) { Lib3dsLin3Key *k; Lib3dsDword num=0; for (k=track->keyL; k; k=k->next) { ++num; } - lib3ds_word_write((Lib3dsWord)track->flags,f); - lib3ds_dword_write(0,f); - lib3ds_dword_write(0,f); - lib3ds_dword_write(num,f); + lib3ds_word_write((Lib3dsWord)track->flags,strm); + lib3ds_dword_write(0,strm); + lib3ds_dword_write(0,strm); + lib3ds_dword_write(num,strm); for (k=track->keyL; k; k=k->next) { - if (!lib3ds_tcb_write(&k->tcb,f)) { + if (!lib3ds_tcb_write(&k->tcb,strm)) { return(LIB3DS_FALSE); } - lib3ds_vector_write(k->value,f); + lib3ds_vector_write(k->value,strm); } return(LIB3DS_TRUE); } @@ -1156,25 +1156,25 @@ lib3ds_quat_track_eval(Lib3dsQuatTrack *track, Lib3dsQuat q, Lib3dsFloat t) * \ingroup tracks */ Lib3dsBool -lib3ds_quat_track_read(Lib3dsQuatTrack *track, FILE *f) +lib3ds_quat_track_read(Lib3dsQuatTrack *track, iostream *strm) { int keys; int i,j; Lib3dsQuatKey *k; - track->flags=lib3ds_word_read(f); - lib3ds_dword_read(f); - lib3ds_dword_read(f); - keys=lib3ds_intd_read(f); + track->flags=lib3ds_word_read(strm); + lib3ds_dword_read(strm); + lib3ds_dword_read(strm); + keys=lib3ds_intd_read(strm); for (i=0; itcb, f)) { + if (!lib3ds_tcb_read(&k->tcb, strm)) { return(LIB3DS_FALSE); } - k->angle=lib3ds_float_read(f); + k->angle=lib3ds_float_read(strm); for (j=0; j<3; ++j) { - k->axis[j]=lib3ds_float_read(f); + k->axis[j]=lib3ds_float_read(strm); } lib3ds_quat_track_insert(track, k); } @@ -1187,24 +1187,24 @@ lib3ds_quat_track_read(Lib3dsQuatTrack *track, FILE *f) * \ingroup tracks */ Lib3dsBool -lib3ds_quat_track_write(Lib3dsQuatTrack *track, FILE *f) +lib3ds_quat_track_write(Lib3dsQuatTrack *track, iostream *strm) { Lib3dsQuatKey *k; Lib3dsDword num=0; for (k=track->keyL; k; k=k->next) { ++num; } - lib3ds_word_write((Lib3dsWord)track->flags,f); - lib3ds_dword_write(0,f); - lib3ds_dword_write(0,f); - lib3ds_dword_write(num,f); + lib3ds_word_write((Lib3dsWord)track->flags,strm); + lib3ds_dword_write(0,strm); + lib3ds_dword_write(0,strm); + lib3ds_dword_write(num,strm); for (k=track->keyL; k; k=k->next) { - if (!lib3ds_tcb_write(&k->tcb,f)) { + if (!lib3ds_tcb_write(&k->tcb,strm)) { return(LIB3DS_FALSE); } - lib3ds_float_write(k->angle,f); - lib3ds_vector_write(k->axis,f); + lib3ds_float_write(k->angle,strm); + lib3ds_vector_write(k->axis,strm); } return(LIB3DS_TRUE); } @@ -1364,7 +1364,7 @@ lib3ds_morph_track_eval(Lib3dsMorphTrack *track, char *p, Lib3dsFloat t) * \ingroup tracks */ Lib3dsBool -lib3ds_morph_track_read(Lib3dsMorphTrack *, FILE *) +lib3ds_morph_track_read(Lib3dsMorphTrack *, iostream *strm) { /* FIXME: */ return(LIB3DS_TRUE); @@ -1375,7 +1375,7 @@ lib3ds_morph_track_read(Lib3dsMorphTrack *, FILE *) * \ingroup tracks */ Lib3dsBool -lib3ds_morph_track_write(Lib3dsMorphTrack *, FILE *) +lib3ds_morph_track_write(Lib3dsMorphTrack *, iostream *strm) { /* FIXME: */ ASSERT(0); diff --git a/src/osgPlugins/3ds/tracks.h b/src/osgPlugins/3ds/tracks.h index 0449c86ab..57783203d 100644 --- a/src/osgPlugins/3ds/tracks.h +++ b/src/osgPlugins/3ds/tracks.h @@ -154,8 +154,8 @@ extern LIB3DSAPI void lib3ds_bool_track_free_keys(Lib3dsBoolTrack *track); extern LIB3DSAPI void lib3ds_bool_track_insert(Lib3dsBoolTrack *track, Lib3dsBoolKey* key); extern LIB3DSAPI void lib3ds_bool_track_remove(Lib3dsBoolTrack *track, Lib3dsIntd frame); extern LIB3DSAPI void lib3ds_bool_track_eval(Lib3dsBoolTrack *track, Lib3dsBool *p, Lib3dsFloat t); -extern LIB3DSAPI Lib3dsBool lib3ds_bool_track_read(Lib3dsBoolTrack *track, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_bool_track_write(Lib3dsBoolTrack *track, FILE *f); +extern LIB3DSAPI Lib3dsBool lib3ds_bool_track_read(Lib3dsBoolTrack *track, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_bool_track_write(Lib3dsBoolTrack *track, iostream *strm); extern LIB3DSAPI Lib3dsLin1Key* lib3ds_lin1_key_new(); extern LIB3DSAPI void lib3ds_lin1_key_free(Lib3dsLin1Key* key); @@ -166,8 +166,8 @@ extern LIB3DSAPI void lib3ds_lin1_track_setup(Lib3dsLin1Track *track); extern LIB3DSAPI void lib3ds_lin1_track_insert(Lib3dsLin1Track *track, Lib3dsLin1Key *key); extern LIB3DSAPI void lib3ds_lin1_track_remove(Lib3dsLin1Track *track, Lib3dsIntd frame); extern LIB3DSAPI void lib3ds_lin1_track_eval(Lib3dsLin1Track *track, Lib3dsFloat *p, Lib3dsFloat t); -extern LIB3DSAPI Lib3dsBool lib3ds_lin1_track_read(Lib3dsLin1Track *track, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_lin1_track_write(Lib3dsLin1Track *track, FILE *f); +extern LIB3DSAPI Lib3dsBool lib3ds_lin1_track_read(Lib3dsLin1Track *track, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_lin1_track_write(Lib3dsLin1Track *track, iostream *strm); extern LIB3DSAPI Lib3dsLin3Key* lib3ds_lin3_key_new(); extern LIB3DSAPI void lib3ds_lin3_key_free(Lib3dsLin3Key* key); @@ -178,8 +178,8 @@ extern LIB3DSAPI void lib3ds_lin3_track_setup(Lib3dsLin3Track *track); extern LIB3DSAPI void lib3ds_lin3_track_insert(Lib3dsLin3Track *track, Lib3dsLin3Key *key); extern LIB3DSAPI void lib3ds_lin3_track_remove(Lib3dsLin3Track *track, Lib3dsIntd frame); extern LIB3DSAPI void lib3ds_lin3_track_eval(Lib3dsLin3Track *track, Lib3dsVector p, Lib3dsFloat t); -extern LIB3DSAPI Lib3dsBool lib3ds_lin3_track_read(Lib3dsLin3Track *track, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_lin3_track_write(Lib3dsLin3Track *track, FILE *f); +extern LIB3DSAPI Lib3dsBool lib3ds_lin3_track_read(Lib3dsLin3Track *track, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_lin3_track_write(Lib3dsLin3Track *track, iostream *strm); extern LIB3DSAPI Lib3dsQuatKey* lib3ds_quat_key_new(); extern LIB3DSAPI void lib3ds_quat_key_free(Lib3dsQuatKey* key); @@ -190,8 +190,8 @@ extern LIB3DSAPI void lib3ds_quat_track_setup(Lib3dsQuatTrack *track); extern LIB3DSAPI void lib3ds_quat_track_insert(Lib3dsQuatTrack *track, Lib3dsQuatKey *key); extern LIB3DSAPI void lib3ds_quat_track_remove(Lib3dsQuatTrack *track, Lib3dsIntd frame); extern LIB3DSAPI void lib3ds_quat_track_eval(Lib3dsQuatTrack *track, Lib3dsQuat p, Lib3dsFloat t); -extern LIB3DSAPI Lib3dsBool lib3ds_quat_track_read(Lib3dsQuatTrack *track, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_quat_track_write(Lib3dsQuatTrack *track, FILE *f); +extern LIB3DSAPI Lib3dsBool lib3ds_quat_track_read(Lib3dsQuatTrack *track, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_quat_track_write(Lib3dsQuatTrack *track, iostream *strm); extern LIB3DSAPI Lib3dsMorphKey* lib3ds_morph_key_new(); extern LIB3DSAPI void lib3ds_morph_key_free(Lib3dsMorphKey* key); @@ -199,8 +199,8 @@ extern LIB3DSAPI void lib3ds_morph_track_free_keys(Lib3dsMorphTrack *track); extern LIB3DSAPI void lib3ds_morph_track_insert(Lib3dsMorphTrack *track, Lib3dsMorphKey *key); extern LIB3DSAPI void lib3ds_morph_track_remove(Lib3dsMorphTrack *track, Lib3dsIntd frame); extern LIB3DSAPI void lib3ds_morph_track_eval(Lib3dsMorphTrack *track, char *p, Lib3dsFloat t); -extern LIB3DSAPI Lib3dsBool lib3ds_morph_track_read(Lib3dsMorphTrack *track, FILE *f); -extern LIB3DSAPI Lib3dsBool lib3ds_morph_track_write(Lib3dsMorphTrack *track, FILE *f); +extern LIB3DSAPI Lib3dsBool lib3ds_morph_track_read(Lib3dsMorphTrack *track, iostream *strm); +extern LIB3DSAPI Lib3dsBool lib3ds_morph_track_write(Lib3dsMorphTrack *track, iostream *strm); #ifdef __cplusplus } diff --git a/src/osgPlugins/3ds/viewport.cpp b/src/osgPlugins/3ds/viewport.cpp index 4dfd87d9d..2d49c3e9d 100644 --- a/src/osgPlugins/3ds/viewport.cpp +++ b/src/osgPlugins/3ds/viewport.cpp @@ -38,12 +38,12 @@ * \ingroup viewport */ Lib3dsBool -lib3ds_viewport_read(Lib3dsViewport *viewport, FILE *f) +lib3ds_viewport_read(Lib3dsViewport *viewport, iostream *strm) { Lib3dsChunk c; Lib3dsWord chunk; - if (!lib3ds_chunk_read_start(&c, 0, f)) { + if (!lib3ds_chunk_read_start(&c, 0, strm)) { return(LIB3DS_FALSE); } @@ -51,42 +51,39 @@ lib3ds_viewport_read(Lib3dsViewport *viewport, FILE *f) case LIB3DS_VIEWPORT_LAYOUT: { int cur=0; - viewport->layout.style=lib3ds_word_read(f); - viewport->layout.active=lib3ds_intw_read(f); - lib3ds_intw_read(f); - viewport->layout.swap=lib3ds_intw_read(f); - lib3ds_intw_read(f); - viewport->layout.swap_prior=lib3ds_intw_read(f); - viewport->layout.swap_view=lib3ds_intw_read(f); - lib3ds_chunk_read_tell(&c, f); - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + viewport->layout.style=lib3ds_word_read(strm); + viewport->layout.active=lib3ds_intw_read(strm); + lib3ds_intw_read(strm); + viewport->layout.swap=lib3ds_intw_read(strm); + lib3ds_intw_read(strm); + viewport->layout.swap_prior=lib3ds_intw_read(strm); + viewport->layout.swap_view=lib3ds_intw_read(strm); + lib3ds_chunk_read_tell(&c, strm); + while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { switch (chunk) { case LIB3DS_VIEWPORT_SIZE: { - viewport->layout.position[0]=lib3ds_word_read(f); - viewport->layout.position[1]=lib3ds_word_read(f); - viewport->layout.size[0]=lib3ds_word_read(f); - viewport->layout.size[1]=lib3ds_word_read(f); + viewport->layout.position[0]=lib3ds_word_read(strm); + viewport->layout.position[1]=lib3ds_word_read(strm); + viewport->layout.size[0]=lib3ds_word_read(strm); + viewport->layout.size[1]=lib3ds_word_read(strm); } break; case LIB3DS_VIEWPORT_DATA_3: { lib3ds_viewport_set_views(viewport,cur+1); - lib3ds_intw_read(f); - viewport->layout.viewL[cur].axis_lock=lib3ds_word_read(f); - viewport->layout.viewL[cur].position[0]=lib3ds_intw_read(f); - viewport->layout.viewL[cur].position[1]=lib3ds_intw_read(f); - viewport->layout.viewL[cur].size[0]=lib3ds_intw_read(f); - viewport->layout.viewL[cur].size[1]=lib3ds_intw_read(f); - viewport->layout.viewL[cur].type=lib3ds_word_read(f); - viewport->layout.viewL[cur].zoom=lib3ds_float_read(f); - lib3ds_vector_read(viewport->layout.viewL[cur].center,f); - viewport->layout.viewL[cur].horiz_angle=lib3ds_float_read(f); - viewport->layout.viewL[cur].vert_angle=lib3ds_float_read(f); - - int result = fread(viewport->layout.viewL[cur].camera,11,1,f); - if (result==0) return (LIB3DS_FALSE); - + lib3ds_intw_read(strm); + viewport->layout.viewL[cur].axis_lock=lib3ds_word_read(strm); + viewport->layout.viewL[cur].position[0]=lib3ds_intw_read(strm); + viewport->layout.viewL[cur].position[1]=lib3ds_intw_read(strm); + viewport->layout.viewL[cur].size[0]=lib3ds_intw_read(strm); + viewport->layout.viewL[cur].size[1]=lib3ds_intw_read(strm); + viewport->layout.viewL[cur].type=lib3ds_word_read(strm); + viewport->layout.viewL[cur].zoom=lib3ds_float_read(strm); + lib3ds_vector_read(viewport->layout.viewL[cur].center,strm); + viewport->layout.viewL[cur].horiz_angle=lib3ds_float_read(strm); + viewport->layout.viewL[cur].vert_angle=lib3ds_float_read(strm); + strm->read(viewport->layout.viewL[cur].camera,11); ++cur; } break; @@ -103,65 +100,64 @@ lib3ds_viewport_read(Lib3dsViewport *viewport, FILE *f) case LIB3DS_DEFAULT_VIEW: { memset(&viewport->default_view,0,sizeof(Lib3dsDefaultView)); - while ((chunk=lib3ds_chunk_read_next(&c, f))!=0) { + while ((chunk=lib3ds_chunk_read_next(&c, strm))!=0) { switch (chunk) { case LIB3DS_VIEW_TOP: { viewport->default_view.type=LIB3DS_VIEW_TYPE_TOP; - lib3ds_vector_read(viewport->default_view.position,f); - viewport->default_view.width=lib3ds_float_read(f); + lib3ds_vector_read(viewport->default_view.position,strm); + viewport->default_view.width=lib3ds_float_read(strm); } break; case LIB3DS_VIEW_BOTTOM: { viewport->default_view.type=LIB3DS_VIEW_TYPE_BOTTOM; - lib3ds_vector_read(viewport->default_view.position,f); - viewport->default_view.width=lib3ds_float_read(f); + lib3ds_vector_read(viewport->default_view.position,strm); + viewport->default_view.width=lib3ds_float_read(strm); } break; case LIB3DS_VIEW_LEFT: { viewport->default_view.type=LIB3DS_VIEW_TYPE_LEFT; - lib3ds_vector_read(viewport->default_view.position,f); - viewport->default_view.width=lib3ds_float_read(f); + lib3ds_vector_read(viewport->default_view.position,strm); + viewport->default_view.width=lib3ds_float_read(strm); } break; case LIB3DS_VIEW_RIGHT: { viewport->default_view.type=LIB3DS_VIEW_TYPE_RIGHT; - lib3ds_vector_read(viewport->default_view.position,f); - viewport->default_view.width=lib3ds_float_read(f); + lib3ds_vector_read(viewport->default_view.position,strm); + viewport->default_view.width=lib3ds_float_read(strm); } break; case LIB3DS_VIEW_FRONT: { viewport->default_view.type=LIB3DS_VIEW_TYPE_FRONT; - lib3ds_vector_read(viewport->default_view.position,f); - viewport->default_view.width=lib3ds_float_read(f); + lib3ds_vector_read(viewport->default_view.position,strm); + viewport->default_view.width=lib3ds_float_read(strm); } break; case LIB3DS_VIEW_BACK: { viewport->default_view.type=LIB3DS_VIEW_TYPE_BACK; - lib3ds_vector_read(viewport->default_view.position,f); - viewport->default_view.width=lib3ds_float_read(f); + lib3ds_vector_read(viewport->default_view.position,strm); + viewport->default_view.width=lib3ds_float_read(strm); } break; case LIB3DS_VIEW_USER: { viewport->default_view.type=LIB3DS_VIEW_TYPE_USER; - lib3ds_vector_read(viewport->default_view.position,f); - viewport->default_view.width=lib3ds_float_read(f); - viewport->default_view.horiz_angle=lib3ds_float_read(f); - viewport->default_view.vert_angle=lib3ds_float_read(f); - viewport->default_view.roll_angle=lib3ds_float_read(f); + lib3ds_vector_read(viewport->default_view.position,strm); + viewport->default_view.width=lib3ds_float_read(strm); + viewport->default_view.horiz_angle=lib3ds_float_read(strm); + viewport->default_view.vert_angle=lib3ds_float_read(strm); + viewport->default_view.roll_angle=lib3ds_float_read(strm); } break; case LIB3DS_VIEW_CAMERA: { viewport->default_view.type=LIB3DS_VIEW_TYPE_CAMERA; - int result = fread(viewport->default_view.camera,11,1,f); - if (result==0) return (LIB3DS_FALSE); + strm->read(viewport->default_view.camera,11); } break; default: @@ -172,7 +168,7 @@ lib3ds_viewport_read(Lib3dsViewport *viewport, FILE *f) break; } - lib3ds_chunk_read_end(&c, f); + lib3ds_chunk_read_end(&c, strm); return(LIB3DS_TRUE); } @@ -206,58 +202,57 @@ lib3ds_viewport_set_views(Lib3dsViewport *viewport, Lib3dsDword views) * \ingroup viewport */ Lib3dsBool -lib3ds_viewport_write(Lib3dsViewport *viewport, FILE *f) +lib3ds_viewport_write(Lib3dsViewport *viewport, iostream *strm) { if (viewport->layout.views) { Lib3dsChunk c; unsigned i; c.chunk=LIB3DS_VIEWPORT_LAYOUT; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } - lib3ds_word_write(viewport->layout.style,f); - lib3ds_intw_write(viewport->layout.active,f); - lib3ds_intw_write(0,f); - lib3ds_intw_write(viewport->layout.swap,f); - lib3ds_intw_write(0,f); - lib3ds_intw_write(viewport->layout.swap_prior,f); - lib3ds_intw_write(viewport->layout.swap_view,f); + lib3ds_word_write(viewport->layout.style,strm); + lib3ds_intw_write(viewport->layout.active,strm); + lib3ds_intw_write(0,strm); + lib3ds_intw_write(viewport->layout.swap,strm); + lib3ds_intw_write(0,strm); + lib3ds_intw_write(viewport->layout.swap_prior,strm); + lib3ds_intw_write(viewport->layout.swap_view,strm); { Lib3dsChunk c; c.chunk=LIB3DS_VIEWPORT_SIZE; c.size=14; - lib3ds_chunk_write(&c,f); - lib3ds_intw_write(viewport->layout.position[0],f); - lib3ds_intw_write(viewport->layout.position[1],f); - lib3ds_intw_write(viewport->layout.size[0],f); - lib3ds_intw_write(viewport->layout.size[1],f); + lib3ds_chunk_write(&c,strm); + lib3ds_intw_write(viewport->layout.position[0],strm); + lib3ds_intw_write(viewport->layout.position[1],strm); + lib3ds_intw_write(viewport->layout.size[0],strm); + lib3ds_intw_write(viewport->layout.size[1],strm); } for (i=0; ilayout.views; ++i) { Lib3dsChunk c; c.chunk=LIB3DS_VIEWPORT_DATA_3; c.size=55; - lib3ds_chunk_write(&c,f); + lib3ds_chunk_write(&c,strm); - lib3ds_intw_write(0,f); - lib3ds_word_write(viewport->layout.viewL[i].axis_lock,f); - lib3ds_intw_write(viewport->layout.viewL[i].position[0],f); - lib3ds_intw_write(viewport->layout.viewL[i].position[1],f); - lib3ds_intw_write(viewport->layout.viewL[i].size[0],f); - lib3ds_intw_write(viewport->layout.viewL[i].size[1],f); - lib3ds_word_write(viewport->layout.viewL[i].type,f); - lib3ds_float_write(viewport->layout.viewL[i].zoom,f); - lib3ds_vector_write(viewport->layout.viewL[i].center,f); - lib3ds_float_write(viewport->layout.viewL[i].horiz_angle,f); - lib3ds_float_write(viewport->layout.viewL[i].vert_angle,f); - int result = fwrite(viewport->layout.viewL[i].camera,11,1,f); - if (result==0) return (LIB3DS_FALSE); + lib3ds_intw_write(0,strm); + lib3ds_word_write(viewport->layout.viewL[i].axis_lock,strm); + lib3ds_intw_write(viewport->layout.viewL[i].position[0],strm); + lib3ds_intw_write(viewport->layout.viewL[i].position[1],strm); + lib3ds_intw_write(viewport->layout.viewL[i].size[0],strm); + lib3ds_intw_write(viewport->layout.viewL[i].size[1],strm); + lib3ds_word_write(viewport->layout.viewL[i].type,strm); + lib3ds_float_write(viewport->layout.viewL[i].zoom,strm); + lib3ds_vector_write(viewport->layout.viewL[i].center,strm); + lib3ds_float_write(viewport->layout.viewL[i].horiz_angle,strm); + lib3ds_float_write(viewport->layout.viewL[i].vert_angle,strm); + strm->write(viewport->layout.viewL[i].camera,11); } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } } @@ -266,7 +261,7 @@ lib3ds_viewport_write(Lib3dsViewport *viewport, FILE *f) Lib3dsChunk c; c.chunk=LIB3DS_DEFAULT_VIEW; - if (!lib3ds_chunk_write_start(&c,f)) { + if (!lib3ds_chunk_write_start(&c,strm)) { return(LIB3DS_FALSE); } @@ -276,9 +271,9 @@ lib3ds_viewport_write(Lib3dsViewport *viewport, FILE *f) Lib3dsChunk c; c.chunk=LIB3DS_VIEW_TOP; c.size=22; - lib3ds_chunk_write(&c,f); - lib3ds_vector_write(viewport->default_view.position,f); - lib3ds_float_write(viewport->default_view.width,f); + lib3ds_chunk_write(&c,strm); + lib3ds_vector_write(viewport->default_view.position,strm); + lib3ds_float_write(viewport->default_view.width,strm); } break; case LIB3DS_VIEW_TYPE_BOTTOM: @@ -286,9 +281,9 @@ lib3ds_viewport_write(Lib3dsViewport *viewport, FILE *f) Lib3dsChunk c; c.chunk=LIB3DS_VIEW_BOTTOM; c.size=22; - lib3ds_chunk_write(&c,f); - lib3ds_vector_write(viewport->default_view.position,f); - lib3ds_float_write(viewport->default_view.width,f); + lib3ds_chunk_write(&c,strm); + lib3ds_vector_write(viewport->default_view.position,strm); + lib3ds_float_write(viewport->default_view.width,strm); } break; case LIB3DS_VIEW_TYPE_LEFT: @@ -296,9 +291,9 @@ lib3ds_viewport_write(Lib3dsViewport *viewport, FILE *f) Lib3dsChunk c; c.chunk=LIB3DS_VIEW_LEFT; c.size=22; - lib3ds_chunk_write(&c,f); - lib3ds_vector_write(viewport->default_view.position,f); - lib3ds_float_write(viewport->default_view.width,f); + lib3ds_chunk_write(&c,strm); + lib3ds_vector_write(viewport->default_view.position,strm); + lib3ds_float_write(viewport->default_view.width,strm); } break; case LIB3DS_VIEW_TYPE_RIGHT: @@ -306,9 +301,9 @@ lib3ds_viewport_write(Lib3dsViewport *viewport, FILE *f) Lib3dsChunk c; c.chunk=LIB3DS_VIEW_RIGHT; c.size=22; - lib3ds_chunk_write(&c,f); - lib3ds_vector_write(viewport->default_view.position,f); - lib3ds_float_write(viewport->default_view.width,f); + lib3ds_chunk_write(&c,strm); + lib3ds_vector_write(viewport->default_view.position,strm); + lib3ds_float_write(viewport->default_view.width,strm); } break; case LIB3DS_VIEW_TYPE_FRONT: @@ -316,9 +311,9 @@ lib3ds_viewport_write(Lib3dsViewport *viewport, FILE *f) Lib3dsChunk c; c.chunk=LIB3DS_VIEW_FRONT; c.size=22; - lib3ds_chunk_write(&c,f); - lib3ds_vector_write(viewport->default_view.position,f); - lib3ds_float_write(viewport->default_view.width,f); + lib3ds_chunk_write(&c,strm); + lib3ds_vector_write(viewport->default_view.position,strm); + lib3ds_float_write(viewport->default_view.width,strm); } break; case LIB3DS_VIEW_TYPE_BACK: @@ -326,9 +321,9 @@ lib3ds_viewport_write(Lib3dsViewport *viewport, FILE *f) Lib3dsChunk c; c.chunk=LIB3DS_VIEW_BACK; c.size=22; - lib3ds_chunk_write(&c,f); - lib3ds_vector_write(viewport->default_view.position,f); - lib3ds_float_write(viewport->default_view.width,f); + lib3ds_chunk_write(&c,strm); + lib3ds_vector_write(viewport->default_view.position,strm); + lib3ds_float_write(viewport->default_view.width,strm); } break; case LIB3DS_VIEW_TYPE_USER: @@ -336,12 +331,12 @@ lib3ds_viewport_write(Lib3dsViewport *viewport, FILE *f) Lib3dsChunk c; c.chunk=LIB3DS_VIEW_USER; c.size=34; - lib3ds_chunk_write(&c,f); - lib3ds_vector_write(viewport->default_view.position,f); - lib3ds_float_write(viewport->default_view.width,f); - lib3ds_float_write(viewport->default_view.horiz_angle,f); - lib3ds_float_write(viewport->default_view.vert_angle,f); - lib3ds_float_write(viewport->default_view.roll_angle,f); + lib3ds_chunk_write(&c,strm); + lib3ds_vector_write(viewport->default_view.position,strm); + lib3ds_float_write(viewport->default_view.width,strm); + lib3ds_float_write(viewport->default_view.horiz_angle,strm); + lib3ds_float_write(viewport->default_view.vert_angle,strm); + lib3ds_float_write(viewport->default_view.roll_angle,strm); } break; case LIB3DS_VIEW_TYPE_CAMERA: @@ -349,14 +344,13 @@ lib3ds_viewport_write(Lib3dsViewport *viewport, FILE *f) Lib3dsChunk c; c.chunk=LIB3DS_VIEW_CAMERA; c.size=17; - lib3ds_chunk_write(&c,f); - int result = fwrite(viewport->default_view.camera,1,11,f); - if (result==0) return (LIB3DS_FALSE); + lib3ds_chunk_write(&c,strm); + strm->write(viewport->default_view.camera,11); // NAH Potential issue here } break; } - if (!lib3ds_chunk_write_end(&c,f)) { + if (!lib3ds_chunk_write_end(&c,strm)) { return(LIB3DS_FALSE); } } diff --git a/src/osgPlugins/3ds/viewport.h b/src/osgPlugins/3ds/viewport.h index 38857ee3a..ba794e4dc 100644 --- a/src/osgPlugins/3ds/viewport.h +++ b/src/osgPlugins/3ds/viewport.h @@ -27,6 +27,9 @@ #include "types.h" #endif +#include +using namespace std; + #ifdef __cplusplus extern "C" { #endif @@ -122,9 +125,9 @@ struct _Lib3dsViewport { Lib3dsDefaultView default_view; }; -extern LIB3DSAPI Lib3dsBool lib3ds_viewport_read(Lib3dsViewport *viewport, FILE *f); +extern LIB3DSAPI Lib3dsBool lib3ds_viewport_read(Lib3dsViewport *viewport, iostream *strm); extern LIB3DSAPI void lib3ds_viewport_set_views(Lib3dsViewport *viewport, Lib3dsDword views); -extern LIB3DSAPI Lib3dsBool lib3ds_viewport_write(Lib3dsViewport *viewport, FILE *f); +extern LIB3DSAPI Lib3dsBool lib3ds_viewport_write(Lib3dsViewport *viewport, iostream *strm); #ifdef __cplusplus } diff --git a/src/osgPlugins/CMakeLists.txt b/src/osgPlugins/CMakeLists.txt index e182f175b..a1f012f6f 100644 --- a/src/osgPlugins/CMakeLists.txt +++ b/src/osgPlugins/CMakeLists.txt @@ -168,6 +168,7 @@ ADD_SUBDIRECTORY(lwo) ADD_SUBDIRECTORY(bvh) ADD_SUBDIRECTORY(x) ADD_SUBDIRECTORY(dw) +ADD_SUBDIRECTORY(ply) ADD_SUBDIRECTORY(dxf) ADD_SUBDIRECTORY(OpenFlight) # ADD_SUBDIRECTORY(flt) diff --git a/src/osgPlugins/ply/CMakeLists.txt b/src/osgPlugins/ply/CMakeLists.txt new file mode 100644 index 000000000..c83bab123 --- /dev/null +++ b/src/osgPlugins/ply/CMakeLists.txt @@ -0,0 +1,15 @@ +#this file is automatically generated + + +SET(TARGET_SRC ReaderWriterPLY.cpp + vertexData.cpp + plyfile.cpp +) + +SET(TARGET_H + typedefs.h + ply.h + vertexData.h +) +#### end var setup ### +SETUP_PLUGIN(ply) diff --git a/src/osgPlugins/ply/ReaderWriterPLY.cpp b/src/osgPlugins/ply/ReaderWriterPLY.cpp new file mode 100644 index 000000000..b0873056a --- /dev/null +++ b/src/osgPlugins/ply/ReaderWriterPLY.cpp @@ -0,0 +1,81 @@ +/* + * PLY ( Stanford Triangle Format ) File Loader for OSG + * Copyright (C) 2009 by VizExperts Limited + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include +#include +#include + + +#include "vertexData.h" + +using namespace osg; +using namespace osgDB; +using namespace std; + +/////////////////////////////////////////////////////////////////////////////// +//! +//! \class ReaderWriterPLY +//! \brief This is the Reader for the ply file format +//! +////////////////////////////////////////////////////////////////////////////// +class ReaderWriterPLY : public osgDB::ReaderWriter +{ +public: + ReaderWriterPLY() + { + supportsExtension("ply","Stanford Triangle Format"); + } + + virtual const char* className() { return "ReaderWriterPLY"; } + virtual ReadResult readNode(const std::string& fileName, const osgDB::ReaderWriter::Options*) const; +protected: +}; + +// register with Registry to instantiate the above reader/writer. +REGISTER_OSGPLUGIN(ply, ReaderWriterPLY) + +/////////////////////////////////////////////////////////////////////////////// +//! +//! \brief Function which is called when any ply file is requested to load in +//! \osgDB. Load read ply file and if it successes return the osg::Node +//! +/////////////////////////////////////////////////////////////////////////////// +osgDB::ReaderWriter::ReadResult ReaderWriterPLY::readNode(const std::string& filename, const osgDB::ReaderWriter::Options* options) const +{ + // Get the file extension + std::string ext = osgDB::getFileExtension(filename); + + // If the file extension does not support then return that file is not handled + if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; + + // Check whether or not file exist or not + std::string fileName = osgDB::findDataFile(filename, options); + if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; + + //Instance of vertex data which will read the ply file and convert in to osg::Node + ply::VertexData vertexData; + osg::Node* node = vertexData.readPlyFile(filename.c_str()); + + if (node) + return node; + + return ReadResult::FILE_NOT_HANDLED; +} diff --git a/src/osgPlugins/ply/ply.h b/src/osgPlugins/ply/ply.h new file mode 100644 index 000000000..1ae2c6afc --- /dev/null +++ b/src/osgPlugins/ply/ply.h @@ -0,0 +1,172 @@ +/* + +Header for PLY polygon files. + +- Greg Turk, March 1994 + +A PLY file contains a single polygonal _object_. + +An object is composed of lists of _elements_. Typical elements are +vertices, faces, edges and materials. + +Each type of element for a given object has one or more _properties_ +associated with the element type. For instance, a vertex element may +have as properties three floating-point values x,y,z and three unsigned +chars for red, green and blue. + +--------------------------------------------------------------- + +Copyright (c) 1994 The Board of Trustees of The Leland Stanford +Junior University. All rights reserved. + +Permission to use, copy, modify and distribute this software and its +documentation for any purpose is hereby granted without fee, provided +that the above copyright notice and this permission notice appear in +all copies of this software and that you do not sell the software. + +THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND, +EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY +WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + +*/ + +#ifndef __PLY_H__ +#define __PLY_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#define PLY_ASCII 1 /* ascii PLY file */ +#define PLY_BINARY_BE 2 /* binary PLY file, big endian */ +#define PLY_BINARY_LE 3 /* binary PLY file, little endian */ + +#define PLY_OKAY 0 /* ply routine worked okay */ +#define PLY_ERROR -1 /* error in ply routine */ + +/* scalar data types supported by PLY format */ + +#define PLY_START_TYPE 0 +#define PLY_CHAR 1 +#define PLY_SHORT 2 +#define PLY_INT 3 +#define PLY_UCHAR 4 +#define PLY_USHORT 5 +#define PLY_UINT 6 +#define PLY_FLOAT 7 +#define PLY_DOUBLE 8 +#define PLY_FLOAT32 9 +#define PLY_UINT8 10 +#define PLY_INT32 11 +#define PLY_END_TYPE 12 + +#define PLY_SCALAR 0 +#define PLY_LIST 1 + + +typedef struct PlyProperty { /* description of a property */ + + char *name; /* property name */ + int external_type; /* file's data type */ + int internal_type; /* program's data type */ + int offset; /* offset bytes of prop in a struct */ + + int is_list; /* 1 = list, 0 = scalar */ + int count_external; /* file's count type */ + int count_internal; /* program's count type */ + int count_offset; /* offset byte for list count */ + +} PlyProperty; + +typedef struct PlyElement { /* description of an element */ + char *name; /* element name */ + int num; /* number of elements in this object */ + int size; /* size of element (bytes) or -1 if variable */ + int nprops; /* number of properties for this element */ + PlyProperty **props; /* list of properties in the file */ + char *store_prop; /* flags: property wanted by user? */ + int other_offset; /* offset to un-asked-for props, or -1 if none*/ + int other_size; /* size of other_props structure */ +} PlyElement; + +typedef struct PlyOtherProp { /* describes other properties in an element */ + char *name; /* element name */ + int size; /* size of other_props */ + int nprops; /* number of properties in other_props */ + PlyProperty **props; /* list of properties in other_props */ +} PlyOtherProp; + +typedef struct OtherData { /* for storing other_props for an other element */ + void *other_props; +} OtherData; + +typedef struct OtherElem { /* data for one "other" element */ + char *elem_name; /* names of other elements */ + int elem_count; /* count of instances of each element */ + OtherData **other_data; /* actual property data for the elements */ + PlyOtherProp *other_props; /* description of the property data */ +} OtherElem; + +typedef struct PlyOtherElems { /* "other" elements, not interpreted by user */ + int num_elems; /* number of other elements */ + OtherElem *other_list; /* list of data for other elements */ +} PlyOtherElems; + +typedef struct PlyFile { /* description of PLY file */ + FILE *fp; /* file pointer */ + int file_type; /* ascii or binary */ + float version; /* version number of file */ + int nelems; /* number of elements of object */ + PlyElement **elems; /* list of elements */ + int num_comments; /* number of comments */ + char **comments; /* list of comments */ + int num_obj_info; /* number of items of object information */ + char **obj_info; /* list of object info items */ + PlyElement *which_elem; /* which element we're currently writing */ + PlyOtherElems *other_elems; /* "other" elements from a PLY file */ +} PlyFile; + +/* memory allocation */ +extern char *my_alloc(); +#define myalloc(mem_size) my_alloc((mem_size), __LINE__, __FILE__) + + +/*** delcaration of routines ***/ + +extern PlyFile *ply_write(FILE *, int, char **, int); +extern PlyFile *ply_open_for_writing(char *, int, char **, int, float *); +extern void ply_describe_element(PlyFile *, char *, int, int, PlyProperty *); +extern void ply_describe_property(PlyFile *, char *, PlyProperty *); +extern void ply_element_count(PlyFile *, const char *, int); +extern void ply_header_complete(PlyFile *); +extern void ply_put_element_setup(PlyFile *, const char *); +extern void ply_put_element(PlyFile *, void *); +extern void ply_put_comment(PlyFile *, const char *); +extern void ply_put_obj_info(PlyFile *, const char *); +extern PlyFile *ply_read(FILE *, int *, char ***); +extern PlyFile *ply_open_for_reading( char *, int *, char ***, int *, float *); +extern PlyProperty **ply_get_element_description(PlyFile *, char *, int*, int*); +extern void ply_get_element_setup( PlyFile *, char *, int, PlyProperty *); +extern void ply_get_property(PlyFile *, char *, PlyProperty *); +extern PlyOtherProp *ply_get_other_properties(PlyFile *, char *, int); +extern void ply_get_element(PlyFile *, void *); +extern char **ply_get_comments(PlyFile *, int *); +extern char **ply_get_obj_info(PlyFile *, int *); +extern void ply_close(PlyFile *); +extern void ply_get_info(PlyFile *, float *, int *); +extern PlyOtherElems *ply_get_other_element (PlyFile *, char *, int); +extern void ply_describe_other_elements ( PlyFile *, PlyOtherElems *); +extern void ply_put_other_elements (PlyFile *); +extern void ply_free_other_elements (PlyOtherElems *); + +extern int equal_strings(const char *, const char *); + + +#ifdef __cplusplus +} +#endif +#endif /* !__PLY_H__ */ + diff --git a/src/osgPlugins/ply/plyfile.cpp b/src/osgPlugins/ply/plyfile.cpp new file mode 100644 index 000000000..d1705622d --- /dev/null +++ b/src/osgPlugins/ply/plyfile.cpp @@ -0,0 +1,2686 @@ +/* Copyright (c) 2005-2007, Stefan Eilemann + All rights reserved. + - Cleaned up code for 64 bit, little and big endian support + - Added new ply data types (uint8, float32, int32) + */ + +/* + +The interface routines for reading and writing PLY polygon files. + +Greg Turk, February 1994 + +--------------------------------------------------------------- + +A PLY file contains a single polygonal _object_. + +An object is composed of lists of _elements_. Typical elements are +vertices, faces, edges and materials. + +Each type of element for a given object has one or more _properties_ +associated with the element type. For instance, a vertex element may +have as properties the floating-point values x,y,z and the three unsigned +chars representing red, green and blue. + +--------------------------------------------------------------- + +Copyright (c) 1994 The Board of Trustees of The Leland Stanford +Junior University. All rights reserved. + +Permission to use, copy, modify and distribute this software and its +documentation for any purpose is hereby granted without fee, provided +that the above copyright notice and this permission notice appear in +all copies of this software and that you do not sell the software. + +THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND, +EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY +WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + +*/ + +#include "ply.h" +#include +#include +#include +#include + +#ifdef WIN32 +# ifndef LITTLE_ENDIAN +# define LITTLE_ENDIAN +# endif +#endif + +const char *type_names[] = { + "invalid", + "char", "short", "int", + "uchar", "ushort", "uint", + "float", "double", "float32", "uint8", "int32" +}; + +int ply_type_size[] = { + 0, 1, 2, 4, 1, 2, 4, 4, 8, 4, 1, 4 +}; + +#define NO_OTHER_PROPS -1 + +#define DONT_STORE_PROP 0 +#define STORE_PROP 1 + +#define OTHER_PROP 0 +#define NAMED_PROP 1 + + +/* returns 1 if strings are equal, 0 if not */ +int equal_strings(const char *, const char *); + +/* find an element in a plyfile's list */ +PlyElement *find_element(PlyFile *, const char *); + +/* find a property in an element's list */ +PlyProperty *find_property(PlyElement *, const char *, int *); + +/* write to a file the word describing a PLY file data type */ +void write_scalar_type (FILE *, int); + +/* read a line from a file and break it up into separate words */ +char **get_words(FILE *, int *, char **); + +/* write an item to a file */ +void write_binary_item(PlyFile *, int, unsigned int, double, int); +void write_ascii_item(FILE *, int, unsigned int, double, int); + +/* add information to a PLY file descriptor */ +void add_element(PlyFile *, char **, int); +void add_property(PlyFile *, char **, int); +void add_comment(PlyFile *, char *); +void add_obj_info(PlyFile *, char *); + +/* copy a property */ +void copy_property(PlyProperty *, PlyProperty *); + +/* store a value into where a pointer and a type specify */ +void store_item(char *, int, int, unsigned int, double); + +/* return the value of a stored item */ +void get_stored_item( void *, int, int *, unsigned int *, double *); + +/* return the value stored in an item, given ptr to it and its type */ +double get_item_value(char *, int); + +/* get binary or ascii item and store it according to ptr and type */ +void get_ascii_item(char *, int, int *, unsigned int *, double *); +void get_binary_item(PlyFile *, int, int *, unsigned int *, double *); + +/* get a bunch of elements from a file */ +void ascii_get_element(PlyFile *, char *); +void binary_get_element(PlyFile *, char *); + +/* memory allocation */ +char *my_alloc(int, int, const char *); + +/************************/ +/* Byte-swapping macros */ +/************************/ + +void swap2Bytes( void* ptr ) +{ + unsigned char* bytes = (unsigned char*)ptr; + unsigned short* result = (unsigned short*)ptr; + + *result = (bytes[0]<<8) | bytes[1]; +} + +void swap4Bytes( void* ptr ) +{ + unsigned char* bytes = (unsigned char*)ptr; + unsigned int* result = (unsigned int*)ptr; + + *result = (bytes[0]<<24) | (bytes[1]<<16) | (bytes[2]<<8) | bytes[3]; +} + +void swap8Bytes( void* ptr ) +{ + unsigned char* bytes = (unsigned char*)ptr; + unsigned long long* result = (unsigned long long*)ptr; + + *result = ((unsigned long long)(bytes[0])) << 56 | + ((unsigned long long)(bytes[1])) << 48 | + ((unsigned long long)(bytes[2])) << 40 | + ((unsigned long long)(bytes[3])) << 32 | + ((unsigned long long)(bytes[4])) << 24 | + ((unsigned long long)(bytes[5])) << 16 | + ((unsigned long long)(bytes[6])) << 8 | + bytes[7]; + + +} + +#ifdef LITTLE_ENDIAN +void swap2LE( void* ) {} +void swap2LE( short* ) {} +void swap2LE( unsigned short* ) {} +void swap4LE( void* ) {} +void swap4LE( int* ) {} +void swap4LE( unsigned int* ) {} +void swap4LE( float* ) {} +void swap8LE( void* ) {} +void swap8LE( long long* ) {} +void swap8LE( unsigned long long* ) {} +void swap8LE( double* ) {} + +void swap2BE( void* ptr ) { swap2Bytes(ptr); } +void swap2BE( short* ptr ) { swap2Bytes(ptr); } +void swap2BE( unsigned short* ptr ) { swap2Bytes(ptr); } +void swap4BE( void* ptr ) { swap4Bytes(ptr); } +void swap4BE( int* ptr ) { swap4Bytes(ptr); } +void swap4BE( unsigned int* ptr ) { swap4Bytes(ptr); } +void swap4BE( float* ptr ) { swap4Bytes(ptr); } +void swap8BE( long long* ptr ) { swap8Bytes(ptr); } +void swap8BE( void* ptr ) { swap8Bytes(ptr); } +void swap8BE( unsigned long long* ptr ) { swap8Bytes(ptr); } +void swap8BE( double* ptr ) { swap8Bytes(ptr); } + +#else // LITTLE_ENDIAN + +void swap2LE( void* ptr ) { swap2Bytes(ptr); } +void swap2LE( short* ptr ) { swap2Bytes(ptr); } +void swap2LE( unsigned short* ptr ) { swap2Bytes(ptr); } +void swap4LE( void* ptr ) { swap4Bytes(ptr); } +void swap4LE( int* ptr ) { swap4Bytes(ptr); } +void swap4LE( unsigned int* ptr ) { swap4Bytes(ptr); } +void swap4LE( float* ptr ) { swap4Bytes(ptr); } +void swap8LE( long long* ptr ) { swap8Bytes(ptr); } +void swap8LE( void* ptr ) { swap8Bytes(ptr); } +void swap8LE( unsigned long long* ptr ) { swap8Bytes(ptr); } +void swap8LE( double* ptr ) { swap8Bytes(ptr); } + +void swap2BE( void* ) {} +void swap2BE( short* ) {} +void swap2BE( unsigned short* ) {} +void swap4BE( void* ) {} +void swap4BE( int* ) {} +void swap4BE( unsigned int* ) {} +void swap4BE( float* ) {} +void swap8BE( void* ) {} +void swap8BE( long long* ) {} +void swap8BE( unsigned long long* ) {} +void swap8BE( double* ) {} + +#endif // LITTLE_ENDIAN + + +/*************/ +/* Writing */ +/*************/ + + +/****************************************************************************** +Given a file pointer, get ready to write PLY data to the file. + +Entry: + fp - the given file pointer + nelems - number of elements in object + elem_names - list of element names + file_type - file type, either ascii or binary + +Exit: + returns a pointer to a PlyFile, used to refer to this file, or NULL if error +******************************************************************************/ + +PlyFile *ply_write( + FILE *fp, + int nelems, + const char **elem_names, + int file_type +) +{ + int i; + PlyFile *plyfile; + PlyElement *elem; + + /* check for NULL file pointer */ + if (fp == NULL) + return (NULL); + + /* create a record for this object */ + + plyfile = (PlyFile *) myalloc (sizeof (PlyFile)); + plyfile->file_type = file_type; + plyfile->num_comments = 0; + plyfile->num_obj_info = 0; + plyfile->nelems = nelems; + plyfile->version = 1.0; + plyfile->fp = fp; + plyfile->other_elems = NULL; + + /* tuck aside the names of the elements */ + + plyfile->elems = (PlyElement **) myalloc (sizeof (PlyElement *) * nelems); + for (i = 0; i < nelems; i++) { + elem = (PlyElement *) myalloc (sizeof (PlyElement)); + plyfile->elems[i] = elem; + elem->name = strdup (elem_names[i]); + elem->num = 0; + elem->nprops = 0; + } + + /* return pointer to the file descriptor */ + return (plyfile); +} + + +/****************************************************************************** +Open a polygon file for writing. + +Entry: + filename - name of file to read from + nelems - number of elements in object + elem_names - list of element names + file_type - file type, either ascii or binary + +Exit: + version - version number of PLY file + returns a file identifier, used to refer to this file, or NULL if error +******************************************************************************/ + +PlyFile *ply_open_for_writing( + char *filename, + int nelems, + const char **elem_names, + int file_type, + float *version +) +{ + PlyFile *plyfile; + char *name; + FILE *fp; + + + /* tack on the extension .ply, if necessary */ + name = (char *) myalloc (sizeof (char) * + (static_cast(strlen (filename)) + 5)); + strcpy (name, filename); + if (strlen (name) < 4 || + strcmp (name + strlen (name) - 4, ".ply") != 0) + strcat (name, ".ply"); + + /* open the file for writing */ + + fp = fopen (name, "wb"); + free (name); //wjs remove memory leak// + if (fp == NULL) { + return (NULL); + } + + /* create the actual PlyFile structure */ + + plyfile = ply_write (fp, nelems, elem_names, file_type); + if (plyfile == NULL) + return (NULL); + + /* say what PLY file version number we're writing */ + *version = plyfile->version; + + /* return pointer to the file descriptor */ + return (plyfile); +} + + +/****************************************************************************** +Describe an element, including its properties and how many will be written +to the file. + +Entry: + plyfile - file identifier + elem_name - name of element that information is being specified about + nelems - number of elements of this type to be written + nprops - number of properties contained in the element + prop_list - list of properties +******************************************************************************/ + +void ply_describe_element( + PlyFile *plyfile, + const char *elem_name, + int nelems, + int nprops, + PlyProperty *prop_list +) +{ + int i; + PlyElement *elem; + PlyProperty *prop; + + /* look for appropriate element */ + elem = find_element (plyfile, elem_name); + if (elem == NULL) { + fprintf(stderr,"ply_describe_element: can't find element '%s'\n",elem_name); + exit (-1); + } + + elem->num = nelems; + + /* copy the list of properties */ + + elem->nprops = nprops; + elem->props = (PlyProperty **) myalloc (sizeof (PlyProperty *) * nprops); + elem->store_prop = (char *) myalloc (sizeof (char) * nprops); + + for (i = 0; i < nprops; i++) { + prop = (PlyProperty *) myalloc (sizeof (PlyProperty)); + elem->props[i] = prop; + elem->store_prop[i] = NAMED_PROP; + copy_property (prop, &prop_list[i]); + } +} + + +/****************************************************************************** +Describe a property of an element. + +Entry: + plyfile - file identifier + elem_name - name of element that information is being specified about + prop - the new property +******************************************************************************/ + +void ply_describe_property( + PlyFile *plyfile, + const char *elem_name, + PlyProperty *prop +) +{ + PlyElement *elem; + PlyProperty *elem_prop; + + /* look for appropriate element */ + elem = find_element (plyfile, elem_name); + if (elem == NULL) { + fprintf(stderr, "ply_describe_property: can't find element '%s'\n", + elem_name); + return; + } + + /* create room for new property */ + + if (elem->nprops == 0) { + elem->props = (PlyProperty **) myalloc (sizeof (PlyProperty *)); + elem->store_prop = (char *) myalloc (sizeof (char)); + elem->nprops = 1; + } + else { + elem->nprops++; + elem->props = (PlyProperty **) + realloc (elem->props, sizeof (PlyProperty *) * elem->nprops); + elem->store_prop = (char *) + realloc (elem->store_prop, sizeof (char) * elem->nprops); + } + + /* copy the new property */ + elem->other_offset = 0; //added by wjs Purify UMR + elem_prop = (PlyProperty *) myalloc (sizeof (PlyProperty)); + elem->props[elem->nprops - 1] = elem_prop; + elem->store_prop[elem->nprops - 1] = NAMED_PROP; + copy_property (elem_prop, prop); +} + + +/****************************************************************************** +Describe what the "other" properties are that are to be stored, and where +they are in an element. +******************************************************************************/ + +void ply_describe_other_properties( + PlyFile *plyfile, + PlyOtherProp *other, + int offset +) +{ + int i; + PlyElement *elem; + PlyProperty *prop; + + /* look for appropriate element */ + elem = find_element (plyfile, other->name); + if (elem == NULL) { + fprintf(stderr, "ply_describe_other_properties: can't find element '%s'\n", + other->name); + return; + } + + /* create room for other properties */ + + if (elem->nprops == 0) { + elem->props = (PlyProperty **) + myalloc (sizeof (PlyProperty *) * other->nprops); + elem->store_prop = (char *) myalloc (sizeof (char) * other->nprops); + elem->nprops = 0; + } + else { + int newsize; + newsize = elem->nprops + other->nprops; + elem->props = (PlyProperty **) + realloc (elem->props, sizeof (PlyProperty *) * newsize); + elem->store_prop = (char *) + realloc (elem->store_prop, sizeof (char) * newsize); + } + + /* copy the other properties */ + + for (i = 0; i < other->nprops; i++) { + prop = (PlyProperty *) myalloc (sizeof (PlyProperty)); + copy_property (prop, other->props[i]); + elem->props[elem->nprops] = prop; + elem->store_prop[elem->nprops] = OTHER_PROP; + elem->nprops++; + } + + /* save other info about other properties */ + elem->other_size = other->size; + elem->other_offset = offset; +} + + +/****************************************************************************** +State how many of a given element will be written. + +Entry: + plyfile - file identifier + elem_name - name of element that information is being specified about + nelems - number of elements of this type to be written +******************************************************************************/ + +void ply_element_count( + PlyFile *plyfile, + const char *elem_name, + int nelems +) +{ + PlyElement *elem; + + /* look for appropriate element */ + elem = find_element (plyfile, elem_name); + if (elem == NULL) { + fprintf(stderr,"ply_element_count: can't find element '%s'\n",elem_name); + exit (-1); + } + + elem->num = nelems; +} + + +/****************************************************************************** +Signal that we've described everything a PLY file's header and that the +header should be written to the file. + +Entry: + plyfile - file identifier +******************************************************************************/ + +void ply_header_complete(PlyFile *plyfile) +{ + int i,j; + FILE *fp = plyfile->fp; + PlyElement *elem; + PlyProperty *prop; + + fprintf (fp, "ply\n"); + + switch (plyfile->file_type) { + case PLY_ASCII: + fprintf (fp, "format ascii 1.0\n"); + break; + case PLY_BINARY_BE: + fprintf (fp, "format binary_big_endian 1.0\n"); + break; + case PLY_BINARY_LE: + fprintf (fp, "format binary_little_endian 1.0\n"); + break; + default: + fprintf (stderr, "ply_header_complete: bad file type = %d\n", + plyfile->file_type); + exit (-1); + } + + /* write out the comments */ + + for (i = 0; i < plyfile->num_comments; i++) + fprintf (fp, "comment %s\n", plyfile->comments[i]); + + /* write out object information */ + + for (i = 0; i < plyfile->num_obj_info; i++) + fprintf (fp, "obj_info %s\n", plyfile->obj_info[i]); + + /* write out information about each element */ + + for (i = 0; i < plyfile->nelems; i++) { + + elem = plyfile->elems[i]; + fprintf (fp, "element %s %d\n", elem->name, elem->num); + + /* write out each property */ + for (j = 0; j < elem->nprops; j++) { + prop = elem->props[j]; + if (prop->is_list) { + fprintf (fp, "property list "); + write_scalar_type (fp, prop->count_external); + fprintf (fp, " "); + write_scalar_type (fp, prop->external_type); + fprintf (fp, " %s\n", prop->name); + } + else { + fprintf (fp, "property "); + write_scalar_type (fp, prop->external_type); + fprintf (fp, " %s\n", prop->name); + } + } + } + + fprintf (fp, "end_header\n"); +} + + +/****************************************************************************** +Specify which elements are going to be written. This should be called +before a call to the routine ply_put_element(). + +Entry: + plyfile - file identifier + elem_name - name of element we're talking about +******************************************************************************/ + +void ply_put_element_setup(PlyFile *plyfile, const char *elem_name) +{ + PlyElement *elem; + + elem = find_element (plyfile, elem_name); + if (elem == NULL) { + fprintf(stderr, "ply_elements_setup: can't find element '%s'\n", elem_name); + exit (-1); + } + + plyfile->which_elem = elem; +} + + +/****************************************************************************** +Write an element to the file. This routine assumes that we're +writing the type of element specified in the last call to the routine +ply_put_element_setup(). + +Entry: + plyfile - file identifier + elem_ptr - pointer to the element +******************************************************************************/ + +void ply_put_element(PlyFile *plyfile, void *elem_ptr) +{ + int j, k; + FILE *fp = plyfile->fp; + PlyElement *elem; + PlyProperty *prop; + char *elem_data,*item; + char **item_ptr; + int list_count; + int item_size; + int int_val; + unsigned int uint_val; + double double_val; + char **other_ptr; + + elem = plyfile->which_elem; + elem_data = (char *)elem_ptr; + other_ptr = (char **) (((char *) elem_ptr) + elem->other_offset); + + /* write out either to an ascii or binary file */ + + if (plyfile->file_type == PLY_ASCII) { + + /* write an ascii file */ + + /* write out each property of the element */ + for (j = 0; j < elem->nprops; j++) { + prop = elem->props[j]; + if (elem->store_prop[j] == OTHER_PROP) + elem_data = *other_ptr; + else + elem_data = (char *)elem_ptr; + if (prop->is_list) { + item = elem_data + prop->count_offset; + get_stored_item ((void *) item, prop->count_internal, + &int_val, &uint_val, &double_val); + write_ascii_item (fp, int_val, uint_val, double_val, + prop->count_external); + list_count = uint_val; + item_ptr = (char **) (elem_data + prop->offset); + item = item_ptr[0]; + item_size = ply_type_size[prop->internal_type]; + for (k = 0; k < list_count; k++) { + get_stored_item ((void *) item, prop->internal_type, + &int_val, &uint_val, &double_val); + write_ascii_item (fp, int_val, uint_val, double_val, + prop->external_type); + item += item_size; + } + } + else { + item = elem_data + prop->offset; + get_stored_item ((void *) item, prop->internal_type, + &int_val, &uint_val, &double_val); + write_ascii_item (fp, int_val, uint_val, double_val, + prop->external_type); + } + } + + fprintf (fp, "\n"); + } + else { + + /* write a binary file */ + + /* write out each property of the element */ + for (j = 0; j < elem->nprops; j++) { + prop = elem->props[j]; + if (elem->store_prop[j] == OTHER_PROP) + elem_data = *other_ptr; + else + elem_data = (char *)elem_ptr; + if (prop->is_list) { + item = elem_data + prop->count_offset; + item_size = ply_type_size[prop->count_internal]; + get_stored_item ((void *) item, prop->count_internal, + &int_val, &uint_val, &double_val); + write_binary_item (plyfile, int_val, uint_val, double_val, + prop->count_external); + list_count = uint_val; + item_ptr = (char **) (elem_data + prop->offset); + item = item_ptr[0]; + item_size = ply_type_size[prop->internal_type]; + for (k = 0; k < list_count; k++) { + get_stored_item ((void *) item, prop->internal_type, + &int_val, &uint_val, &double_val); + write_binary_item (plyfile, int_val, uint_val, double_val, + prop->external_type); + item += item_size; + } + } + else { + item = elem_data + prop->offset; + item_size = ply_type_size[prop->internal_type]; + get_stored_item ((void *) item, prop->internal_type, + &int_val, &uint_val, &double_val); + write_binary_item (plyfile, int_val, uint_val, double_val, + prop->external_type); + } + } + + } +} + + +/****************************************************************************** +Specify a comment that will be written in the header. + +Entry: + plyfile - file identifier + comment - the comment to be written +******************************************************************************/ + +void ply_put_comment(PlyFile *plyfile, const char *comment) +{ + /* (re)allocate space for new comment */ + if (plyfile->num_comments == 0) + { + plyfile->comments = (char **) myalloc (sizeof (char *)); + } + else + { + plyfile->comments = (char **) realloc (plyfile->comments, + sizeof (char *) * (plyfile->num_comments + 1)); + } + + /* add comment to list */ + plyfile->comments[plyfile->num_comments] = strdup (comment); + plyfile->num_comments++; +} + + +/****************************************************************************** +Specify a piece of object information (arbitrary text) that will be written +in the header. + +Entry: + plyfile - file identifier + obj_info - the text information to be written +******************************************************************************/ + +void ply_put_obj_info(PlyFile *plyfile, const char *obj_info) +{ + /* (re)allocate space for new info */ + if (plyfile->num_obj_info == 0) + { + plyfile->obj_info = (char **) myalloc (sizeof (char *)); + } + else + { + plyfile->obj_info = (char **) realloc (plyfile->obj_info, + sizeof (char *) * (plyfile->num_obj_info + 1)); + } + + /* add info to list */ + plyfile->obj_info[plyfile->num_obj_info] = strdup (obj_info); + plyfile->num_obj_info++; +} + + + + + + + +/*************/ +/* Reading */ +/*************/ + + + +/****************************************************************************** +Given a file pointer, get ready to read PLY data from the file. + +Entry: + fp - the given file pointer + +Exit: + nelems - number of elements in object + elem_names - list of element names + returns a pointer to a PlyFile, used to refer to this file, or NULL if error +******************************************************************************/ + +PlyFile *ply_read(FILE *fp, int *nelems, char ***elem_names) +{ + int i,j; + PlyFile *plyfile; + int nwords; + char **words; + char **elist; + PlyElement *elem; + char *orig_line; + + /* check for NULL file pointer */ + if (fp == NULL) + return (NULL); + + /* create record for this object */ + + plyfile = (PlyFile *) myalloc (sizeof (PlyFile)); + plyfile->nelems = 0; + plyfile->comments = NULL; + plyfile->num_comments = 0; + plyfile->obj_info = NULL; + plyfile->num_obj_info = 0; + plyfile->fp = fp; + plyfile->other_elems = NULL; + + /* read and parse the file's header */ + + words = get_words (plyfile->fp, &nwords, &orig_line); + if (!words || !equal_strings (words[0], "ply")) + return (NULL); + + while (words) { + + /* parse words */ + + if (equal_strings (words[0], "format")) { + if (nwords != 3) + return (NULL); + if (equal_strings (words[1], "ascii")) + plyfile->file_type = PLY_ASCII; + else if (equal_strings (words[1], "binary_big_endian")) + plyfile->file_type = PLY_BINARY_BE; + else if (equal_strings (words[1], "binary_little_endian")) + plyfile->file_type = PLY_BINARY_LE; + else + { + free (words); + return (NULL); + } + plyfile->version = atof (words[2]); + } + else if (equal_strings (words[0], "element")) + add_element (plyfile, words, nwords); + else if (equal_strings (words[0], "property")) + add_property (plyfile, words, nwords); + else if (equal_strings (words[0], "comment")) + add_comment (plyfile, orig_line); + else if (equal_strings (words[0], "obj_info")) + add_obj_info (plyfile, orig_line); + else if (equal_strings (words[0], "end_header")) + { + free (words); + break; + } + + /* free up words space */ + free (words); + + words = get_words (plyfile->fp, &nwords, &orig_line); + } + + + /* create tags for each property of each element, to be used */ + /* later to say whether or not to store each property for the user */ + + for (i = 0; i < plyfile->nelems; i++) { + elem = plyfile->elems[i]; + elem->store_prop = (char *) myalloc (sizeof (char) * elem->nprops); + for (j = 0; j < elem->nprops; j++) + elem->store_prop[j] = DONT_STORE_PROP; + elem->other_offset = NO_OTHER_PROPS; /* no "other" props by default */ + } + + /* set return values about the elements */ + + elist = (char **) myalloc (sizeof (char *) * plyfile->nelems); + for (i = 0; i < plyfile->nelems; i++) + elist[i] = strdup (plyfile->elems[i]->name); + + *elem_names = elist; + *nelems = plyfile->nelems; + + /* return a pointer to the file's information */ + + return (plyfile); +} + + +/****************************************************************************** +Open a polygon file for reading. + +Entry: + filename - name of file to read from + +Exit: + nelems - number of elements in object + elem_names - list of element names + file_type - file type, either ascii or binary + version - version number of PLY file + returns a file identifier, used to refer to this file, or NULL if error +******************************************************************************/ + +PlyFile *ply_open_for_reading( + char *filename, + int *nelems, + char ***elem_names, + int *file_type, + float *version +) +{ + FILE *fp; + PlyFile *plyfile; + char *name; + + /* tack on the extension .ply, if necessary */ + + name = (char *) myalloc (sizeof (char) * + (static_cast(strlen (filename) + 5))); + strcpy (name, filename); + if (strlen (name) < 4 || + strcmp (name + strlen (name) - 4, ".ply") != 0) + strcat (name, ".ply"); + + /* open the file for reading */ + + fp = fopen (name, "rb"); + free(name); + if (fp == NULL) + return (NULL); + + /* create the PlyFile data structure */ + + plyfile = ply_read (fp, nelems, elem_names); + + /* determine the file type and version */ + + *file_type = plyfile->file_type; + *version = plyfile->version; + + /* return a pointer to the file's information */ + + return (plyfile); +} + + +/****************************************************************************** +Get information about a particular element. + +Entry: + plyfile - file identifier + elem_name - name of element to get information about + +Exit: + nelems - number of elements of this type in the file + nprops - number of properties + returns a list of properties, or NULL if the file doesn't contain that elem +******************************************************************************/ + +PlyProperty **ply_get_element_description( + PlyFile *plyfile, + char *elem_name, + int *nelems, + int *nprops +) +{ + int i; + PlyElement *elem; + PlyProperty *prop; + PlyProperty **prop_list; + + /* find information about the element */ + elem = find_element (plyfile, elem_name); + if (elem == NULL) + return (NULL); + + *nelems = elem->num; + *nprops = elem->nprops; + + /* make a copy of the element's property list */ + prop_list = (PlyProperty **) myalloc (sizeof (PlyProperty *) * elem->nprops); + for (i = 0; i < elem->nprops; i++) { + prop = (PlyProperty *) myalloc (sizeof (PlyProperty)); + copy_property (prop, elem->props[i]); + prop_list[i] = prop; + } + + /* return this duplicate property list */ + return (prop_list); +} + + +/****************************************************************************** +Specify which properties of an element are to be returned. This should be +called before a call to the routine ply_get_element(). + +Entry: + plyfile - file identifier + elem_name - which element we're talking about + nprops - number of properties + prop_list - list of properties +******************************************************************************/ + +void ply_get_element_setup( PlyFile *plyfile, char *elem_name, int nprops, + PlyProperty *prop_list ) +{ + int i; + PlyElement *elem; + PlyProperty *prop; + int index; + + /* find information about the element */ + elem = find_element (plyfile, elem_name); + plyfile->which_elem = elem; + + /* deposit the property information into the element's description */ + for (i = 0; i < nprops; i++) + { + /* look for actual property */ + prop = find_property (elem, prop_list[i].name, &index); + if (prop == NULL) + { + fprintf ( stderr, + "Warning: Can't find property '%s' in element '%s'\n", + prop_list[i].name, elem_name ); + continue; + } + + /* store its description */ + prop->internal_type = prop_list[i].internal_type; + prop->offset = prop_list[i].offset; + prop->count_internal = prop_list[i].count_internal; + prop->count_offset = prop_list[i].count_offset; + + /* specify that the user wants this property */ + elem->store_prop[index] = STORE_PROP; + } +} + + +/****************************************************************************** +Specify a property of an element that is to be returned. This should be +called (usually multiple times) before a call to the routine ply_get_element(). +This routine should be used in preference to the less flexible old routine +called ply_get_element_setup(). + +Entry: + plyfile - file identifier + elem_name - which element we're talking about + prop - property to add to those that will be returned +******************************************************************************/ + +void ply_get_property( + PlyFile *plyfile, + char *elem_name, + PlyProperty *prop +) +{ + PlyElement *elem; + PlyProperty *prop_ptr; + int index; + + /* find information about the element */ + elem = find_element (plyfile, elem_name); + plyfile->which_elem = elem; + + /* deposit the property information into the element's description */ + + prop_ptr = find_property (elem, prop->name, &index); + if (prop_ptr == NULL) { + fprintf (stderr, "Warning: Can't find property '%s' in element '%s'\n", + prop->name, elem_name); + return; + } + prop_ptr->internal_type = prop->internal_type; + prop_ptr->offset = prop->offset; + prop_ptr->count_internal = prop->count_internal; + prop_ptr->count_offset = prop->count_offset; + + /* specify that the user wants this property */ + elem->store_prop[index] = STORE_PROP; +} + + +/****************************************************************************** +Read one element from the file. This routine assumes that we're reading +the type of element specified in the last call to the routine +ply_get_element_setup(). + +Entry: + plyfile - file identifier + elem_ptr - pointer to location where the element information should be put +******************************************************************************/ + +void ply_get_element(PlyFile *plyfile, void *elem_ptr) +{ + if (plyfile->file_type == PLY_ASCII) + ascii_get_element (plyfile, (char *) elem_ptr); + else + binary_get_element (plyfile, (char *) elem_ptr); +} + + +/****************************************************************************** +Extract the comments from the header information of a PLY file. + +Entry: + plyfile - file identifier + +Exit: + num_comments - number of comments returned + returns a pointer to a list of comments +******************************************************************************/ + +char **ply_get_comments(PlyFile *plyfile, int *num_comments) +{ + *num_comments = plyfile->num_comments; + return (plyfile->comments); +} + + +/****************************************************************************** +Extract the object information (arbitrary text) from the header information +of a PLY file. + +Entry: + plyfile - file identifier + +Exit: + num_obj_info - number of lines of text information returned + returns a pointer to a list of object info lines +******************************************************************************/ + +char **ply_get_obj_info(PlyFile *plyfile, int *num_obj_info) +{ + *num_obj_info = plyfile->num_obj_info; + return (plyfile->obj_info); +} + + +/****************************************************************************** +Make ready for "other" properties of an element-- those properties that +the user has not explicitly asked for, but that are to be stashed away +in a special structure to be carried along with the element's other +information. + +Entry: + plyfile - file identifier + elem - element for which we want to save away other properties +******************************************************************************/ + +void setup_other_props(PlyFile *, PlyElement *elem) +{ + int i; + PlyProperty *prop; + int size = 0; + int type_size; + + /* Examine each property in decreasing order of size. */ + /* We do this so that all data types will be aligned by */ + /* word, half-word, or whatever within the structure. */ + + for (type_size = 8; type_size > 0; type_size /= 2) { + + /* add up the space taken by each property, and save this information */ + /* away in the property descriptor */ + + for (i = 0; i < elem->nprops; i++) { + + /* don't bother with properties we've been asked to store explicitly */ + if (elem->store_prop[i]) + continue; + + prop = elem->props[i]; + + /* internal types will be same as external */ + prop->internal_type = prop->external_type; + prop->count_internal = prop->count_external; + + /* check list case */ + if (prop->is_list) { + + /* pointer to list */ + if (type_size == sizeof (void *)) { + prop->offset = size; + size += sizeof (void *); /* always use size of a pointer here */ + } + + /* count of number of list elements */ + if (type_size == ply_type_size[prop->count_external]) { + prop->count_offset = size; + size += ply_type_size[prop->count_external]; + } + } + /* not list */ + else if (type_size == ply_type_size[prop->external_type]) { + prop->offset = size; + size += ply_type_size[prop->external_type]; + } + } + + } + + /* save the size for the other_props structure */ + elem->other_size = size; +} + + +/****************************************************************************** +Specify that we want the "other" properties of an element to be tucked +away within the user's structure. The user needn't be concerned for how +these properties are stored. + +Entry: + plyfile - file identifier + elem_name - name of element that we want to store other_props in + offset - offset to where other_props will be stored inside user's structure + +Exit: + returns pointer to structure containing description of other_props +******************************************************************************/ + +PlyOtherProp *ply_get_other_properties( + PlyFile *plyfile, + char *elem_name, + int offset +) +{ + int i; + PlyElement *elem; + PlyOtherProp *other; + PlyProperty *prop; + int nprops; + + /* find information about the element */ + elem = find_element (plyfile, elem_name); + if (elem == NULL) { + fprintf (stderr, "ply_get_other_properties: Can't find element '%s'\n", + elem_name); + return (NULL); + } + + /* remember that this is the "current" element */ + plyfile->which_elem = elem; + + /* save the offset to where to store the other_props */ + elem->other_offset = offset; + + /* place the appropriate pointers, etc. in the element's property list */ + setup_other_props (plyfile, elem); + + /* create structure for describing other_props */ + other = (PlyOtherProp *) myalloc (sizeof (PlyOtherProp)); + other->name = strdup (elem_name); +#if 0 + if (elem->other_offset == NO_OTHER_PROPS) { + other->size = 0; + other->props = NULL; + other->nprops = 0; + return (other); + } +#endif + other->size = elem->other_size; + other->props = (PlyProperty **) myalloc (sizeof(PlyProperty) * elem->nprops); + + /* save descriptions of each "other" property */ + nprops = 0; + for (i = 0; i < elem->nprops; i++) { + if (elem->store_prop[i]) + continue; + prop = (PlyProperty *) myalloc (sizeof (PlyProperty)); + copy_property (prop, elem->props[i]); + other->props[nprops] = prop; + nprops++; + } + other->nprops = nprops; + +#if 1 + /* set other_offset pointer appropriately if there are NO other properties */ + if (other->nprops == 0) { + elem->other_offset = NO_OTHER_PROPS; + } +#endif + + /* return structure */ + return (other); +} + + + + +/*************************/ +/* Other Element Stuff */ +/*************************/ + + + + +/****************************************************************************** +Grab all the data for an element that a user does not want to explicitly +read in. + +Entry: + plyfile - pointer to file + elem_name - name of element whose data is to be read in + elem_count - number of instances of this element stored in the file + +Exit: + returns pointer to ALL the "other" element data for this PLY file +******************************************************************************/ + +PlyOtherElems *ply_get_other_element ( + PlyFile *plyfile, + char *elem_name, + int elem_count +) +{ + int i; + PlyElement *elem; + PlyOtherElems *other_elems; + OtherElem *other; + + /* look for appropriate element */ + elem = find_element (plyfile, elem_name); + if (elem == NULL) { + fprintf (stderr, + "ply_get_other_element: can't find element '%s'\n", elem_name); + exit (-1); + } + + /* create room for the new "other" element, initializing the */ + /* other data structure if necessary */ + + if (plyfile->other_elems == NULL) { + plyfile->other_elems = (PlyOtherElems *) myalloc (sizeof (PlyOtherElems)); + other_elems = plyfile->other_elems; + other_elems->other_list = (OtherElem *) myalloc (sizeof (OtherElem)); + other = &(other_elems->other_list[0]); + other_elems->num_elems = 1; + } + else { + other_elems = plyfile->other_elems; + other_elems->other_list = (OtherElem *) realloc (other_elems->other_list, + sizeof (OtherElem) * other_elems->num_elems + 1); + other = &(other_elems->other_list[other_elems->num_elems]); + other_elems->num_elems++; + } + + /* count of element instances in file */ + other->elem_count = elem_count; + + /* save name of element */ + other->elem_name = strdup (elem_name); + + /* create a list to hold all the current elements */ + other->other_data = (OtherData **) + malloc (sizeof (OtherData *) * other->elem_count); + + /* set up for getting elements */ + other->other_props = ply_get_other_properties (plyfile, elem_name, + offsetof(OtherData,other_props)); + + /* grab all these elements */ + for (i = 0; i < other->elem_count; i++) { + /* grab and element from the file */ + other->other_data[i] = (OtherData *) malloc (sizeof (OtherData)); + ply_get_element (plyfile, (void *) other->other_data[i]); + } + + /* return pointer to the other elements data */ + return (other_elems); +} + + +/****************************************************************************** +Pass along a pointer to "other" elements that we want to save in a given +PLY file. These other elements were presumably read from another PLY file. + +Entry: + plyfile - file pointer in which to store this other element info + other_elems - info about other elements that we want to store +******************************************************************************/ + +void ply_describe_other_elements ( + PlyFile *plyfile, + PlyOtherElems *other_elems +) +{ + int i; + OtherElem *other; + + /* ignore this call if there is no other element */ + if (other_elems == NULL) + return; + + /* save pointer to this information */ + plyfile->other_elems = other_elems; + + /* describe the other properties of this element */ + + for (i = 0; i < other_elems->num_elems; i++) { + other = &(other_elems->other_list[i]); + ply_element_count (plyfile, other->elem_name, other->elem_count); + ply_describe_other_properties (plyfile, other->other_props, + offsetof(OtherData,other_props)); + } +} + + +/****************************************************************************** +Write out the "other" elements specified for this PLY file. + +Entry: + plyfile - pointer to PLY file to write out other elements for +******************************************************************************/ + +void ply_put_other_elements (PlyFile *plyfile) +{ + int i,j; + OtherElem *other; + + /* make sure we have other elements to write */ + if (plyfile->other_elems == NULL) + return; + + /* write out the data for each "other" element */ + + for (i = 0; i < plyfile->other_elems->num_elems; i++) { + + other = &(plyfile->other_elems->other_list[i]); + ply_put_element_setup (plyfile, other->elem_name); + + /* write out each instance of the current element */ + for (j = 0; j < other->elem_count; j++) + ply_put_element (plyfile, (void *) other->other_data[j]); + } +} + + +/****************************************************************************** +Free up storage used by an "other" elements data structure. + +Entry: + other_elems - data structure to free up +******************************************************************************/ + +void ply_free_other_elements (PlyOtherElems *) +{ + +} + + + +/*******************/ +/* Miscellaneous */ +/*******************/ + + + +/****************************************************************************** +Close a PLY file. + +Entry: + plyfile - identifier of file to close +******************************************************************************/ + +void ply_close(PlyFile *plyfile) +{ + // Changed by Will Schroeder. Old stuff leaked like a sieve. + + /* free up memory associated with the PLY file */ + fclose (plyfile->fp); + + int i, j; + PlyElement *elem; + for (i=0; inelems; i++) + { + elem = plyfile->elems[i]; + if ( elem->name ) {free(elem->name);} + for (j=0; jnprops; j++) + { + if ( elem->props[j]->name ) {free(const_cast(elem->props[j]->name));} + free (elem->props[j]); + } + free (elem->props); + free (elem->store_prop); + free (elem); + } + free(plyfile->elems); + + for (i=0; inum_comments; i++) + { + free (plyfile->comments[i]); + } + free (plyfile->comments); + + for (i=0; inum_obj_info; i++) + { + free (plyfile->obj_info[i]); + } + free (plyfile->obj_info); + + free (plyfile); +} + + +/****************************************************************************** +Get version number and file type of a PlyFile. + +Entry: + ply - pointer to PLY file + +Exit: + version - version of the file + file_type - PLY_ASCII, PLY_BINARY_BE, or PLY_BINARY_LE +******************************************************************************/ + +void ply_get_info(PlyFile *ply, float *version, int *file_type) +{ + if (ply == NULL) + return; + + *version = ply->version; + *file_type = ply->file_type; +} + + +/****************************************************************************** +Compare two strings. Returns 1 if they are the same, 0 if not. +******************************************************************************/ + +int equal_strings(const char *s1, const char *s2) +{ + while (*s1 && *s2) + if (*s1++ != *s2++) + return (0); + + if (*s1 != *s2) + return (0); + else + return (1); +} + + +/****************************************************************************** +Find an element from the element list of a given PLY object. + +Entry: + plyfile - file id for PLY file + element - name of element we're looking for + +Exit: + returns the element, or NULL if not found +******************************************************************************/ + +PlyElement *find_element(PlyFile *plyfile, const char *element) +{ + int i; + + for (i = 0; i < plyfile->nelems; i++) + if (equal_strings (element, plyfile->elems[i]->name)) + return (plyfile->elems[i]); + + return (NULL); +} + + +/****************************************************************************** +Find a property in the list of properties of a given element. + +Entry: + elem - pointer to element in which we want to find the property + prop_name - name of property to find + +Exit: + index - index to position in list + returns a pointer to the property, or NULL if not found +******************************************************************************/ + +PlyProperty *find_property(PlyElement *elem, const char *prop_name, int *index) +{ + int i; + + for( i = 0; i < elem->nprops; i++) + if (equal_strings (prop_name, elem->props[i]->name)) + { + *index = i; + return (elem->props[i]); + } + + *index = -1; + return (NULL); +} + + +/****************************************************************************** +Read an element from an ascii file. + +Entry: + plyfile - file identifier + elem_ptr - pointer to element +******************************************************************************/ + +void ascii_get_element(PlyFile *plyfile, char *elem_ptr) +{ + int j,k; + PlyElement *elem; + PlyProperty *prop; + char **words; + int nwords; + int which_word; + char *elem_data,*item=0; + char *item_ptr; + int item_size=0; + int int_val; + unsigned int uint_val; + double double_val; + int list_count; + int store_it; + char **store_array; + char *orig_line; + char *other_data=0; + int other_flag; + + /* the kind of element we're reading currently */ + elem = plyfile->which_elem; + + /* do we need to setup for other_props? */ + + if (elem->other_offset != NO_OTHER_PROPS) { + char **ptr; + other_flag = 1; + /* make room for other_props */ + other_data = (char *) myalloc (elem->other_size); + /* store pointer in user's structure to the other_props */ + ptr = (char **) (elem_ptr + elem->other_offset); + *ptr = other_data; + } + else + other_flag = 0; + + /* read in the element */ + + words = get_words (plyfile->fp, &nwords, &orig_line); + if (words == NULL) { + fprintf (stderr, "ply_get_element: unexpected end of file\n"); + exit (-1); + } + + which_word = 0; + + for (j = 0; j < elem->nprops; j++) { + + prop = elem->props[j]; + store_it = (elem->store_prop[j] | other_flag); + + /* store either in the user's structure or in other_props */ + if (elem->store_prop[j]) + elem_data = elem_ptr; + else + elem_data = other_data; + + if (prop->is_list) { /* a list */ + + /* get and store the number of items in the list */ + get_ascii_item (words[which_word++], prop->count_external, + &int_val, &uint_val, &double_val); + if (store_it) { + item = elem_data + prop->count_offset; + store_item(item, prop->count_internal, int_val, uint_val, double_val); + } + + /* allocate space for an array of items and store a ptr to the array */ + list_count = int_val; + item_size = ply_type_size[prop->internal_type]; + store_array = (char **) (elem_data + prop->offset); + + if (list_count == 0) { + if (store_it) + *store_array = NULL; + } + else { + if (store_it) { + item_ptr = (char *) myalloc (sizeof (char) * item_size * list_count); + item = item_ptr; + *store_array = item_ptr; + } + + /* read items and store them into the array */ + for (k = 0; k < list_count; k++) { + get_ascii_item (words[which_word++], prop->external_type, + &int_val, &uint_val, &double_val); + if (store_it) { + store_item (item, prop->internal_type, + int_val, uint_val, double_val); + item += item_size; + } + } + } + + } + else { /* not a list */ + get_ascii_item (words[which_word++], prop->external_type, + &int_val, &uint_val, &double_val); + if (store_it) { + item = elem_data + prop->offset; + store_item (item, prop->internal_type, int_val, uint_val, double_val); + } + } + + } + + free (words); +} + + +/****************************************************************************** +Read an element from a binary file. + +Entry: + plyfile - file identifier + elem_ptr - pointer to an element +******************************************************************************/ + +void binary_get_element(PlyFile *plyfile, char *elem_ptr) +{ + int j,k; + PlyElement *elem; + PlyProperty *prop; + //FILE *fp = plyfile->fp; + char *elem_data,*item=0; + char *item_ptr; + int item_size=0; + int int_val; + unsigned int uint_val; + double double_val; + int list_count; + int store_it; + char **store_array; + char *other_data=0; + int other_flag; + + /* the kind of element we're reading currently */ + elem = plyfile->which_elem; + + /* do we need to setup for other_props? */ + + if (elem->other_offset != NO_OTHER_PROPS) { + char **ptr; + other_flag = 1; + /* make room for other_props */ + other_data = (char *) myalloc (elem->other_size); + /* store pointer in user's structure to the other_props */ + ptr = (char **) (elem_ptr + elem->other_offset); + *ptr = other_data; + } + else + other_flag = 0; + + /* read in a number of elements */ + + for (j = 0; j < elem->nprops; j++) { + + prop = elem->props[j]; + store_it = (elem->store_prop[j] | other_flag); + + /* store either in the user's structure or in other_props */ + if (elem->store_prop[j]) + elem_data = elem_ptr; + else + elem_data = other_data; + + if (prop->is_list) { /* a list */ + + /* get and store the number of items in the list */ + get_binary_item (plyfile, prop->count_external, + &int_val, &uint_val, &double_val); + if (store_it) { + item = elem_data + prop->count_offset; + store_item(item, prop->count_internal, int_val, uint_val, double_val); + } + + /* allocate space for an array of items and store a ptr to the array */ + list_count = int_val; + /* The "if" was added by Afra Zomorodian 8/22/95 + * so that zipper won't crash reading plies that have additional + * properties. + */ + if (store_it) { + item_size = ply_type_size[prop->internal_type]; + } + store_array = (char **) (elem_data + prop->offset); + if (list_count == 0) { + if (store_it) + *store_array = NULL; + } + else { + if (store_it) { + item_ptr = (char *) myalloc (sizeof (char) * item_size * list_count); + item = item_ptr; + *store_array = item_ptr; + } + + /* read items and store them into the array */ + for (k = 0; k < list_count; k++) { + get_binary_item (plyfile, prop->external_type, + &int_val, &uint_val, &double_val); + if (store_it) { + store_item (item, prop->internal_type, + int_val, uint_val, double_val); + item += item_size; + } + } + } + + } + else { /* not a list */ + get_binary_item (plyfile, prop->external_type, + &int_val, &uint_val, &double_val); + if (store_it) { + item = elem_data + prop->offset; + store_item (item, prop->internal_type, int_val, uint_val, double_val); + } + } + + } +} + + +/****************************************************************************** +Write to a file the word that represents a PLY data type. + +Entry: + fp - file pointer + code - code for type +******************************************************************************/ + +void write_scalar_type (FILE *fp, int code) +{ + /* make sure this is a valid code */ + + if (code <= PLY_START_TYPE || code >= PLY_END_TYPE) { + fprintf (stderr, "write_scalar_type: bad data code = %d\n", code); + exit (-1); + } + + /* write the code to a file */ + + fprintf (fp, "%s", type_names[code]); +} + + +/****************************************************************************** +Get a text line from a file and break it up into words. + +IMPORTANT: The calling routine call "free" on the returned pointer once +finished with it. + +Entry: + fp - file to read from + +Exit: + nwords - number of words returned + orig_line - the original line of characters + returns a list of words from the line, or NULL if end-of-file +******************************************************************************/ + +char **get_words(FILE *fp, int *nwords, char **orig_line) +{ +#define BIG_STRING 4096 + static char str[BIG_STRING]; + static char str_copy[BIG_STRING]; + char **words; + int max_words = 10; + int num_words = 0; + char *ptr,*ptr2; + char *result; + + /* read in a line */ + result = fgets (str, BIG_STRING, fp); + if (result == NULL) { + *nwords = 0; + *orig_line = NULL; + return (NULL); + } + + words = (char **) myalloc (sizeof (char *) * max_words); + + /* convert line-feed and tabs into spaces */ + /* (this guarentees that there will be a space before the */ + /* null character at the end of the string) */ + + str[BIG_STRING-2] = ' '; + str[BIG_STRING-1] = '\0'; + + for (ptr = str, ptr2 = str_copy; *ptr != '\0'; ptr++, ptr2++) { + *ptr2 = *ptr; + if (*ptr == '\t') { + *ptr = ' '; + *ptr2 = ' '; + } + else if (*ptr == '\n') { + *ptr = ' '; + *ptr2 = '\0'; + break; + } + } + + /* find the words in the line */ + + ptr = str; + while (*ptr != '\0') { + + /* jump over leading spaces */ + while (*ptr == ' ') + ptr++; + + /* break if we reach the end */ + if (*ptr == '\0') + break; + + /* save pointer to beginning of word */ + if (num_words >= max_words) { + max_words += 10; + words = (char **) realloc (words, sizeof (char *) * max_words); + } + words[num_words++] = ptr; + + /* jump over non-spaces */ + while (*ptr != ' ') + ptr++; + + /* place a null character here to mark the end of the word */ + *ptr++ = '\0'; + } + + /* return the list of words */ + *nwords = num_words; + *orig_line = str_copy; + return (words); +} + + +/****************************************************************************** +Return the value of an item, given a pointer to it and its type. + +Entry: + item - pointer to item + type - data type that "item" points to + +Exit: + returns a double-precision float that contains the value of the item +******************************************************************************/ + +double get_item_value(char *item, int type) +{ + unsigned char *puchar; + char *pchar; + short int *pshort; + unsigned short int *pushort; + int *pint; + unsigned int *puint; + float *pfloat; + double *pdouble; + int int_value; + unsigned int uint_value; + double double_value; + + switch (type) { + case PLY_CHAR: + pchar = (char *) item; + int_value = *pchar; + return ((double) int_value); + case PLY_UCHAR: + case PLY_UINT8: + puchar = (unsigned char *) item; + int_value = *puchar; + return ((double) int_value); + case PLY_SHORT: + pshort = (short int *) item; + int_value = *pshort; + return ((double) int_value); + case PLY_USHORT: + pushort = (unsigned short int *) item; + int_value = *pushort; + return ((double) int_value); + case PLY_INT: + case PLY_INT32: + pint = (int *) item; + int_value = *pint; + return ((double) int_value); + case PLY_UINT: + puint = (unsigned int *) item; + uint_value = *puint; + return ((double) uint_value); + case PLY_FLOAT: + case PLY_FLOAT32: + pfloat = (float *) item; + double_value = *pfloat; + return (double_value); + case PLY_DOUBLE: + pdouble = (double *) item; + double_value = *pdouble; + return (double_value); + } + fprintf (stderr, "get_item_value: bad type = %d\n", type); + return 0; +} + + +/****************************************************************************** +Write out an item to a file as raw binary bytes. + +Entry: + fp - file to write to + int_val - integer version of item + uint_val - unsigned integer version of item + double_val - double-precision float version of item + type - data type to write out +******************************************************************************/ + +void write_binary_item(PlyFile *plyfile, + int int_val, + unsigned int uint_val, + double double_val, + int type +) +{ + FILE *fp = plyfile->fp; + unsigned char uchar_val; + char char_val; + unsigned short ushort_val; + short short_val; + float float_val; + + switch (type) { + case PLY_CHAR: + char_val = int_val; + fwrite (&char_val, 1, 1, fp); + break; + case PLY_SHORT: + short_val = int_val; + if( plyfile->file_type == PLY_BINARY_BE ) + swap2BE(&short_val); + else + swap2LE(&short_val); + fwrite (&short_val, 2, 1, fp); + break; + case PLY_INT: + case PLY_INT32: + if( plyfile->file_type == PLY_BINARY_BE ) + { + swap4BE(&int_val); + } + else + { + swap4LE(&int_val); + } + fwrite (&int_val, 4, 1, fp); + break; + case PLY_UCHAR: + case PLY_UINT8: + uchar_val = uint_val; + fwrite (&uchar_val, 1, 1, fp); + break; + case PLY_USHORT: + if( plyfile->file_type == PLY_BINARY_BE ) + { + swap2BE(&ushort_val); + } + else + { + swap2LE(&ushort_val); + } + ushort_val = uint_val; + fwrite (&ushort_val, 2, 1, fp); + break; + case PLY_UINT: + if( plyfile->file_type == PLY_BINARY_BE ) + { + swap4BE(&uint_val); + } + else + { + swap4LE(&uint_val); + } + fwrite (&uint_val, 4, 1, fp); + break; + case PLY_FLOAT: + case PLY_FLOAT32: + float_val = double_val; + if( plyfile->file_type == PLY_BINARY_BE ) + { + swap4BE(&float_val); + } + else + { + swap4LE(&float_val); + } + fwrite (&float_val, 4, 1, fp); + break; + case PLY_DOUBLE: + if( plyfile->file_type == PLY_BINARY_BE ) + { + swap8BE(&double_val); + } + else + { + swap8LE(&double_val); + } + fwrite (&double_val, 8, 1, fp); + break; + default: + fprintf (stderr, "write_binary_item: bad type = %d\n", type); + exit (-1); + } +} + + +/****************************************************************************** +Write out an item to a file as ascii characters. + +Entry: + fp - file to write to + int_val - integer version of item + uint_val - unsigned integer version of item + double_val - double-precision float version of item + type - data type to write out +******************************************************************************/ + +void write_ascii_item( + FILE *fp, + int int_val, + unsigned int uint_val, + double double_val, + int type +) +{ + switch (type) { + case PLY_CHAR: + case PLY_SHORT: + case PLY_INT: + case PLY_INT32: + fprintf (fp, "%d ", int_val); + break; + case PLY_UCHAR: + case PLY_UINT8: + case PLY_USHORT: + case PLY_UINT: + fprintf (fp, "%u ", uint_val); + break; + case PLY_FLOAT: + case PLY_FLOAT32: + case PLY_DOUBLE: + fprintf (fp, "%g ", double_val); + break; + default: + fprintf (stderr, "write_ascii_item: bad type = %d\n", type); + exit (-1); + } +} + +/****************************************************************************** +Get the value of an item that is in memory, and place the result +into an integer, an unsigned integer and a double. + +Entry: + ptr - pointer to the item + type - data type supposedly in the item + +Exit: + int_val - integer value + uint_val - unsigned integer value + double_val - double-precision floating point value +******************************************************************************/ + +void get_stored_item( + void *ptr, + int type, + int *int_val, + unsigned int *uint_val, + double *double_val +) +{ + switch (type) { + case PLY_CHAR: + *int_val = *((char *) ptr); + *uint_val = *int_val; + *double_val = *int_val; + break; + case PLY_UCHAR: + case PLY_UINT8: + *uint_val = *((unsigned char *) ptr); + *int_val = *uint_val; + *double_val = *uint_val; + break; + case PLY_SHORT: + *int_val = *((short int *) ptr); + *uint_val = *int_val; + *double_val = *int_val; + break; + case PLY_USHORT: + *uint_val = *((unsigned short int *) ptr); + *int_val = *uint_val; + *double_val = *uint_val; + break; + case PLY_INT: + case PLY_INT32: + *int_val = *((int *) ptr); + *uint_val = *int_val; + *double_val = *int_val; + break; + case PLY_UINT: + *uint_val = *((unsigned int *) ptr); + *int_val = *uint_val; + *double_val = *uint_val; + break; + case PLY_FLOAT: + case PLY_FLOAT32: + *double_val = *((float *) ptr); + *int_val = (int) *double_val; + *uint_val = (unsigned int) *double_val; + break; + case PLY_DOUBLE: + *double_val = *((double *) ptr); + *int_val = (int) *double_val; + *uint_val = (unsigned int) *double_val; + break; + default: + fprintf (stderr, "get_stored_item: bad type = %d\n", type); + exit (-1); + } +} + + +/****************************************************************************** +Get the value of an item from a binary file, and place the result +into an integer, an unsigned integer and a double. + +Entry: + fp - file to get item from + type - data type supposedly in the word + +Exit: + int_val - integer value + uint_val - unsigned integer value + double_val - double-precision floating point value +******************************************************************************/ + +void get_binary_item( + PlyFile *plyfile, + int type, + int *int_val, + unsigned int *uint_val, + double *double_val +) +{ + char c[8]; + void *ptr; + + ptr = (void *) c; + + switch (type) { + case PLY_CHAR: + fread (ptr, 1, 1, plyfile->fp); + *int_val = *((char *) ptr); + *uint_val = *int_val; + *double_val = *int_val; + break; + case PLY_UCHAR: + case PLY_UINT8: + fread (ptr, 1, 1, plyfile->fp); + *uint_val = *((unsigned char *) ptr); + *int_val = *uint_val; + *double_val = *uint_val; + break; + case PLY_SHORT: + fread (ptr, 2, 1, plyfile->fp); + if( plyfile->file_type == PLY_BINARY_BE ) + { + swap2BE(ptr); + } + else + { + swap2LE(ptr); + } + *int_val = *((short int *) ptr); + *uint_val = *int_val; + *double_val = *int_val; + break; + case PLY_USHORT: + fread (ptr, 2, 1, plyfile->fp); + if( plyfile->file_type == PLY_BINARY_BE ) + { + swap2BE(ptr); + } + else + { + swap2LE(ptr); + } + *uint_val = *((unsigned short int *) ptr); + *int_val = *uint_val; + *double_val = *uint_val; + break; + case PLY_INT: + case PLY_INT32: + fread (ptr, 4, 1, plyfile->fp); + if( plyfile->file_type == PLY_BINARY_BE ) + { + swap4BE(ptr); + } + else + { + swap4LE(ptr); + } + *int_val = *((int *) ptr); + *uint_val = *int_val; + *double_val = *int_val; + break; + case PLY_UINT: + fread (ptr, 4, 1, plyfile->fp); + if( plyfile->file_type == PLY_BINARY_BE ) + { + swap4BE(ptr); + } + else + { + swap4LE(ptr); + } + *uint_val = *((unsigned int *) ptr); + *int_val = *uint_val; + *double_val = *uint_val; + break; + case PLY_FLOAT: + case PLY_FLOAT32: + fread (ptr, 4, 1, plyfile->fp); + if( plyfile->file_type == PLY_BINARY_BE ) + { + swap4BE(ptr); + } + else + { + swap4LE(ptr); + } + *double_val = *((float *) ptr); + *int_val = (int) *double_val; + *uint_val = (unsigned int) *double_val; + break; + case PLY_DOUBLE: + fread (ptr, 8, 1, plyfile->fp); + if( plyfile->file_type == PLY_BINARY_BE ) + { + swap8BE(ptr); + } + else + { + swap8LE(ptr); + } + *double_val = *((double *) ptr); + *int_val = (int) *double_val; + *uint_val = (unsigned int) *double_val; + break; + default: + fprintf (stderr, "get_binary_item: bad type = %d\n", type); + exit (-1); + } +} + + +/****************************************************************************** +Extract the value of an item from an ascii word, and place the result +into an integer, an unsigned integer and a double. + +Entry: + word - word to extract value from + type - data type supposedly in the word + +Exit: + int_val - integer value + uint_val - unsigned integer value + double_val - double-precision floating point value +******************************************************************************/ + +void get_ascii_item( + char *word, + int type, + int *int_val, + unsigned int *uint_val, + double *double_val +) +{ + switch (type) { + case PLY_CHAR: + case PLY_UCHAR: + case PLY_UINT8: + case PLY_SHORT: + case PLY_USHORT: + case PLY_INT: + case PLY_INT32: + *int_val = atoi (word); + *uint_val = *int_val; + *double_val = *int_val; + break; + + case PLY_UINT: + *uint_val = strtoul (word, (char **) NULL, 10); + *int_val = *uint_val; + *double_val = *uint_val; + break; + + case PLY_FLOAT: + case PLY_FLOAT32: + case PLY_DOUBLE: + *double_val = atof (word); + *int_val = (int) *double_val; + *uint_val = (unsigned int) *double_val; + break; + + default: + fprintf (stderr, "get_ascii_item: bad type = %d\n", type); + exit (-1); + } +} + + +/****************************************************************************** +Store a value into a place being pointed to, guided by a data type. + +Entry: + item - place to store value + type - data type + int_val - integer version of value + uint_val - unsigned integer version of value + double_val - double version of value + +Exit: + item - pointer to stored value +******************************************************************************/ + +void store_item ( + char *item, + int type, + int int_val, + unsigned int uint_val, + double double_val +) +{ + unsigned char *puchar; + short int *pshort; + unsigned short int *pushort; + int *pint; + unsigned int *puint; + float *pfloat; + double *pdouble; + + switch (type) { + case PLY_CHAR: + *item = int_val; + break; + case PLY_UCHAR: + case PLY_UINT8: + puchar = (unsigned char *) item; + *puchar = uint_val; + break; + case PLY_SHORT: + pshort = (short *) item; + *pshort = int_val; + break; + case PLY_USHORT: + pushort = (unsigned short *) item; + *pushort = uint_val; + break; + case PLY_INT: + case PLY_INT32: + pint = (int *) item; + *pint = int_val; + break; + case PLY_UINT: + puint = (unsigned int *) item; + *puint = uint_val; + break; + case PLY_FLOAT: + case PLY_FLOAT32: + pfloat = (float *) item; + *pfloat = double_val; + break; + case PLY_DOUBLE: + pdouble = (double *) item; + *pdouble = double_val; + break; + default: + fprintf (stderr, "store_item: bad type = %d\n", type); + exit (-1); + } +} + + +/****************************************************************************** +Add an element to a PLY file descriptor. + +Entry: + plyfile - PLY file descriptor + words - list of words describing the element + nwords - number of words in the list +******************************************************************************/ + +void add_element (PlyFile *plyfile, char **words, int) +{ + PlyElement *elem; + + /* create the new element */ + elem = (PlyElement *) myalloc (sizeof (PlyElement)); + elem->name = strdup (words[1]); + elem->num = atoi (words[2]); + elem->nprops = 0; + + /* make room for new element in the object's list of elements */ + if (plyfile->nelems == 0) + plyfile->elems = (PlyElement **) myalloc (sizeof (PlyElement *)); + else + plyfile->elems = (PlyElement **) realloc (plyfile->elems, + sizeof (PlyElement *) * (plyfile->nelems + 1)); + + /* add the new element to the object's list */ + plyfile->elems[plyfile->nelems] = elem; + plyfile->nelems++; +} + + +/****************************************************************************** +Return the type of a property, given the name of the property. + +Entry: + name - name of property type + +Exit: + returns integer code for property, or 0 if not found +******************************************************************************/ + +int get_prop_type(char *type_name) +{ + int i; + + for (i = PLY_START_TYPE + 1; i < PLY_END_TYPE; i++) + if (equal_strings (type_name, type_names[i])) + return (i); + + /* if we get here, we didn't find the type */ + return (0); +} + + +/****************************************************************************** +Add a property to a PLY file descriptor. + +Entry: + plyfile - PLY file descriptor + words - list of words describing the property + nwords - number of words in the list +******************************************************************************/ + +void add_property (PlyFile *plyfile, char **words, int ) +{ + PlyProperty *prop; + PlyElement *elem; + + /* create the new property */ + + prop = (PlyProperty *) myalloc (sizeof (PlyProperty)); + + if (equal_strings (words[1], "list")) { /* is a list */ + prop->count_external = get_prop_type (words[2]); + prop->external_type = get_prop_type (words[3]); + prop->name = strdup (words[4]); + prop->is_list = 1; + } + else { /* not a list */ + prop->external_type = get_prop_type (words[1]); + prop->name = strdup (words[2]); + prop->is_list = 0; + } + + /* add this property to the list of properties of the current element */ + + elem = plyfile->elems[plyfile->nelems - 1]; + + if (elem->nprops == 0) + elem->props = (PlyProperty **) myalloc (sizeof (PlyProperty *)); + else + elem->props = (PlyProperty **) realloc (elem->props, + sizeof (PlyProperty *) * (elem->nprops + 1)); + + elem->props[elem->nprops] = prop; + elem->nprops++; +} + + +/****************************************************************************** +Add a comment to a PLY file descriptor. + +Entry: + plyfile - PLY file descriptor + line - line containing comment +******************************************************************************/ + +void add_comment (PlyFile *plyfile, char *line) +{ + int i; + + /* skip over "comment" and leading spaces and tabs */ + i = 7; + while (line[i] == ' ' || line[i] == '\t') + i++; + + ply_put_comment (plyfile, &line[i]); +} + + +/****************************************************************************** +Add a some object information to a PLY file descriptor. + +Entry: + plyfile - PLY file descriptor + line - line containing text info +******************************************************************************/ + +void add_obj_info (PlyFile *plyfile, char *line) +{ + int i; + + /* skip over "obj_info" and leading spaces and tabs */ + i = 8; + while (line[i] == ' ' || line[i] == '\t') + i++; + + ply_put_obj_info (plyfile, &line[i]); +} + + +/****************************************************************************** +Copy a property. +******************************************************************************/ + +void copy_property(PlyProperty *dest, PlyProperty *src) +{ + dest->name = strdup (src->name); + dest->external_type = src->external_type; + dest->internal_type = src->internal_type; + dest->offset = src->offset; + + dest->is_list = src->is_list; + dest->count_external = src->count_external; + dest->count_internal = src->count_internal; + dest->count_offset = src->count_offset; +} + + +/****************************************************************************** +Allocate some memory. + +Entry: + size - amount of memory requested (in bytes) + lnum - line number from which memory was requested + fname - file name from which memory was requested +******************************************************************************/ + +char *my_alloc(int size, int lnum, const char *fname) +{ + char *ptr; + + ptr = (char *) malloc (size); + + if (ptr == 0) + fprintf( stderr, "Memory allocation bombed on line %d in %s\n", + lnum, fname); + + return (ptr); +} + diff --git a/src/osgPlugins/ply/typedefs.h b/src/osgPlugins/ply/typedefs.h new file mode 100644 index 000000000..b5c01b236 --- /dev/null +++ b/src/osgPlugins/ply/typedefs.h @@ -0,0 +1,112 @@ +/* + typedefs.h + Copyright (c) 2007, Tobias Wolf + All rights reserved. + + Type definitions for the mesh classes. +*/ + +/** note, derived from Equalizer LGPL source.*/ + + +#ifndef MESH_TYPEDEFS_H +#define MESH_TYPEDEFS_H + +# ifdef WIN32 +# include +# include +# endif + +# include +# include +# define MESHASSERT assert +# define MESHERROR osg::notify(osg::WARN) +# define MESHWARN osg::notify(osg::WARN) +# define MESHINFO osg::notify(osg::INFO) + +#ifdef WIN32 +typedef int socklen_t; + +typedef UINT64 uint64_t; +typedef INT64 int64_t; +typedef UINT32 uint32_t; +typedef INT32 int32_t; +typedef UINT16 uint16_t; +typedef UINT8 uint8_t; +# ifndef HAVE_SSIZE_T +typedef SSIZE_T ssize_t; +# endif + +#endif // Win32 + +#include +#include +#include + +namespace ply +{ + + + + typedef size_t Index; +// typedef unsigned short ShortIndex; + + + // mesh exception + struct MeshException : public std::exception + { + explicit MeshException( const std::string& msg ) : _message( msg ) {} + virtual ~MeshException() throw() {} + virtual const char* what() const throw() { return _message.c_str(); } + private: + std::string _message; + }; + + // null output stream that discards everything written to it + struct NullOStream : std::ostream + { + struct NullStreamBuf : std::streambuf + { + int overflow( int c ) { return traits_type::not_eof( c ); } + } _nullBuf; + + NullOStream() : std::ios( &_nullBuf ), std::ostream( &_nullBuf ) {} + }; + + // wrapper to enable array use where arrays would not be allowed otherwise + template< class T, size_t d > + struct ArrayWrapper + { + T& operator[]( const size_t i ) + { + MESHASSERT( i < d ); + return data[i]; + } + + const T& operator[]( const size_t i ) const + { + MESHASSERT( i < d ); + return data[i]; + } + + private: + T data[d]; + }; + + + // binary mesh file version, increment if changing the file format + const unsigned short FILE_VERSION ( 0x0114 ); + + + // enumeration for the sort axis + enum Axis + { + AXIS_X, + AXIS_Y, + AXIS_Z + }; + +} + + +#endif // MESH_TYPEDEFS_H diff --git a/src/osgPlugins/ply/vertexData.cpp b/src/osgPlugins/ply/vertexData.cpp new file mode 100644 index 000000000..66ae3d302 --- /dev/null +++ b/src/osgPlugins/ply/vertexData.cpp @@ -0,0 +1,373 @@ +/* + vertexData.cpp + Copyright (c) 2007, Tobias Wolf + All rights reserved. + + Implementation of the VertexData class. +*/ + +/** note, derived from Equalizer LGPL source.*/ + +#include "typedefs.h" + +#include "vertexData.h" +#include "ply.h" +#include +#include +#include +#include +#include + +using namespace std; +using namespace ply; + + +struct Normal{ + osg::Vec3 triNormal; + void normal(osg::Vec3 v1, osg::Vec3 v2, osg::Vec3 v3) + { + osg::Vec3 u,v; + + // right hand system, CCW triangle + u = v2 - v1; + v = v3 - v1; + triNormal = u^v; + triNormal.normalize(); + } +}; + +/* Contructor. */ +VertexData::VertexData() + : _invertFaces( false ) +{ + // Initialize the members + _vertices = NULL; + _colors = NULL; + _normals = NULL; + _triangles = NULL; + +} + + +/* Read the vertex and (if available/wanted) color data from the open file. */ +void VertexData::readVertices( PlyFile* file, const int nVertices, + const bool readColors ) +{ + // temporary vertex structure for ply loading + struct _Vertex + { + float x; + float y; + float z; + unsigned char r; + unsigned char g; + unsigned char b; + } vertex; + + PlyProperty vertexProps[] = + { + { "x", PLY_FLOAT, PLY_FLOAT, offsetof( _Vertex, x ), 0, 0, 0, 0 }, + { "y", PLY_FLOAT, PLY_FLOAT, offsetof( _Vertex, y ), 0, 0, 0, 0 }, + { "z", PLY_FLOAT, PLY_FLOAT, offsetof( _Vertex, z ), 0, 0, 0, 0 }, + { "red", PLY_UCHAR, PLY_UCHAR, offsetof( _Vertex, r ), 0, 0, 0, 0 }, + { "green", PLY_UCHAR, PLY_UCHAR, offsetof( _Vertex, g ), 0, 0, 0, 0 }, + { "blue", PLY_UCHAR, PLY_UCHAR, offsetof( _Vertex, b ), 0, 0, 0, 0 } + }; + + // use all 6 properties when reading colors, only the first 3 otherwise + int limit = readColors ? 6 : 3; + for( int i = 0; i < limit; ++i ) + ply_get_property( file, "vertex", &vertexProps[i] ); + + // check whether array is valid otherwise allocate the space + if(!_vertices.valid()) + _vertices = new osg::Vec3Array; + + // If read colors allocate space for color array + if( readColors ) + { + if(!_colors.valid()) + _colors = new osg::Vec4Array; + } + + // read in the vertices + for( int i = 0; i < nVertices; ++i ) + { + ply_get_element( file, static_cast< void* >( &vertex ) ); + _vertices->push_back( osg::Vec3( vertex.x, vertex.y, vertex.z ) ); + if( readColors ) + _colors->push_back( osg::Vec4( (unsigned int) vertex.r / 256.0, (unsigned int) vertex.g / 256.0 , (unsigned int) vertex.b/ 256.0, 0.0 ) ); + } +} + + +/* Read the index data from the open file. */ +void VertexData::readTriangles( PlyFile* file, const int nFaces ) +{ + // temporary face structure for ply loading + struct _Face + { + unsigned char nVertices; + int* vertices; + } face; + + PlyProperty faceProps[] = + { + { "vertex_indices", PLY_INT, PLY_INT, offsetof( _Face, vertices ), + 1, PLY_UCHAR, PLY_UCHAR, offsetof( _Face, nVertices ) } + }; + + ply_get_property( file, "face", &faceProps[0] ); + + //triangles.clear(); + //triangles.reserve( nFaces ); + if(!_triangles.valid()) + _triangles = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0); + + + // read in the faces, asserting that they are only triangles + uint8_t ind1 = _invertFaces ? 2 : 0; + uint8_t ind3 = _invertFaces ? 0 : 2; + for( int i = 0; i < nFaces; ++i ) + { + ply_get_element( file, static_cast< void* >( &face ) ); + MESHASSERT( face.vertices != 0 ); + if( (unsigned int)(face.nVertices) != 3 ) + { + free( face.vertices ); + throw MeshException( "Error reading PLY file. Encountered a " + "face which does not have three vertices." ); + } + // Add the face indices in the premitive set + _triangles->push_back( face.vertices[ind1]); + _triangles->push_back( face.vertices[1]); + _triangles->push_back( face.vertices[ind3] ); + + // free the memory that was allocated by ply_get_element + free( face.vertices ); + } +} + + +/* Open a PLY file and read vertex, color and index data. and returns the node */ +osg::Node* VertexData::readPlyFile( const char* filename, const bool ignoreColors ) +{ + int nPlyElems; + char** elemNames; + int fileType; + float version; + bool result = false; + int nComments; + char** comments; + + PlyFile* file = ply_open_for_reading( const_cast< char* >( filename ), + &nPlyElems, &elemNames, + &fileType, &version ); + + if( !file ) + { + MESHERROR << "Unable to open PLY file " << filename + << " for reading." << endl; + return NULL; + } + + MESHASSERT( elemNames != 0 ); + + nComments = file->num_comments; + comments = file->comments; + + + #ifndef NDEBUG + MESHINFO << filename << ": " << nPlyElems << " elements, file type = " + << fileType << ", version = " << version << endl; + #endif + + for( int i = 0; i < nComments; i++ ) + { + if( equal_strings( comments[i], "modified by flipply" ) ) + { + _invertFaces = true; + } + + } + for( int i = 0; i < nPlyElems; ++i ) + { + int nElems; + int nProps; + + PlyProperty** props = ply_get_element_description( file, elemNames[i], + &nElems, &nProps ); + MESHASSERT( props != 0 ); + + #ifndef NDEBUG + MESHINFO << "element " << i << ": name = " << elemNames[i] << ", " + << nProps << " properties, " << nElems << " elements" << endl; + for( int j = 0; j < nProps; ++j ) + { + MESHINFO << "element " << i << ", property " << j << ": " + << "name = " << props[j]->name << endl; + } + #endif + + // if the string is vertex means vertex data is started + if( equal_strings( elemNames[i], "vertex" ) ) + { + bool hasColors = false; + // determine if the file stores vertex colors + for( int j = 0; j < nProps; ++j ) + // if the string have the red means color info is there + if( equal_strings( props[j]->name, "red" ) ) + hasColors = true; + + if( ignoreColors ) + MESHINFO << "Colors in PLY file ignored per request." << endl; + + // Read vertices and store in a std::vector array + readVertices( file, nElems, hasColors && !ignoreColors ); + // Check whether all vertices are loaded or not + MESHASSERT( _vertices->size() == static_cast< size_t >( nElems ) ); + // Check all color elements read or not + if( hasColors && !ignoreColors ) + MESHASSERT( _colors->size() == static_cast< size_t >( nElems ) ); + } + // If the string is face means triangle info started + else if( equal_strings( elemNames[i], "face" ) ) + try + { + // Read Triangles + readTriangles( file, nElems ); + // Check whether all face elements read or not + MESHASSERT( _triangles->size()/3 == static_cast< size_t >( nElems ) ); + result = true; + } + catch( exception& e ) + { + MESHERROR << "Unable to read PLY file, an exception occured: " + << e.what() << endl; + // stop for loop by setting the loop variable to break condition + // this way resources still get released even on error cases + i = nPlyElems; + } + + // free the memory that was allocated by ply_get_element_description + for( int j = 0; j < nProps; ++j ) + free( props[j] ); + free( props ); + } + + ply_close( file ); + + // free the memory that was allocated by ply_open_for_reading + for( int i = 0; i < nPlyElems; ++i ) + free( elemNames[i] ); + free( elemNames ); + + // If the result is true means the ply file is successfully read + if(result) + { + // Create geometry node + osg::Geometry* geom = new osg::Geometry; + + // set the vertex array + geom->setVertexArray(_vertices.get()); + + // If the normals are not calculated calculate the normals for faces + if(!_normals.valid()) + _calculateNormals(); + + + // set the normals + geom->setNormalArray(_normals.get()); + geom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX); + + // Add the premetive set + geom->addPrimitiveSet(_triangles.get()); + + // if color info is given set the color array + if(_colors.valid()) + { + geom->setColorArray(_colors.get()); + geom->setColorBinding( osg::Geometry::BIND_PER_VERTEX ); + + } + + + // set flage true to activate the vertex buffer object of drawable + geom->setUseVertexBufferObjects(true); + + + osg::Geode* geode = new osg::Geode; + geode->addDrawable(geom); + return geode; + } + + return NULL; +} + + +/* Calculate the face or vertex normals of the current vertex data. */ +void VertexData::_calculateNormals( const bool vertexNormals ) +{ + + if(_normals.valid()) + return; + + #ifndef NDEBUG + int wrongNormals = 0; + #endif + + if(!_normals.valid()) + { + _normals = new osg::Vec3Array; + } + + //normals.clear(); + if( vertexNormals ) + { + // initialize all normals to zero + for( size_t i = 0; i < _vertices->size(); ++i ) + { + _normals->push_back( osg::Vec3( 0, 0, 0 ) ); + } + } + + + for( size_t i = 0; i < ((_triangles->size())); i += 3 ) + { + // iterate over all triangles and add their normals to adjacent vertices + Normal triangleNormal; + unsigned int i0, i1, i2; + i0 = (*_triangles)[i+0]; + i1 = (*_triangles)[i+1]; + i2 = (*_triangles)[i+2]; + triangleNormal.normal((*_vertices)[i0], + (*_vertices)[i1], + (*_vertices)[i2] ); + + // count emtpy normals in debug mode + #ifndef NDEBUG + if( triangleNormal.triNormal.length() == 0.0f ) + ++wrongNormals; + #endif + + if( vertexNormals ) + { + (*_normals)[i0] += triangleNormal.triNormal; + (*_normals)[i1] += triangleNormal.triNormal; + (*_normals)[i2] += triangleNormal.triNormal; + } + else + _normals->push_back( triangleNormal.triNormal ); + } + + // normalize all the normals + if( vertexNormals ) + for( size_t i = 0; i < _normals->size(); ++i ) + (*_normals)[i].normalize(); + + #ifndef NDEBUG + if( wrongNormals > 0 ) + MESHINFO << wrongNormals << " faces had no valid normal." << endl; + #endif +} + diff --git a/src/osgPlugins/ply/vertexData.h b/src/osgPlugins/ply/vertexData.h new file mode 100644 index 000000000..173ecefd6 --- /dev/null +++ b/src/osgPlugins/ply/vertexData.h @@ -0,0 +1,74 @@ +/* + vertexData.h + Copyright (c) 2007, Tobias Wolf + All rights reserved. + + Header file of the VertexData class. +*/ + +/** note, derived from Equalizer LGPL source.*/ + + +#ifndef MESH_VERTEXDATA_H +#define MESH_VERTEXDATA_H + + +#include +#include + +#include + +/////////////////////////////////////////////////////////////////////////////// +//! +//! \class VertexData +//! \brief helps to read ply file and converts in to osg::Node format +//! +/////////////////////////////////////////////////////////////////////////////// + +// defined elsewhere +class PlyFile; + +namespace ply +{ + /* Holds the flat data and offers routines to read, scale and sort it. */ + class VertexData + { + public: + // Default constructor + VertexData(); + + // Reads ply file and convert in to osg::Node and returns the same + osg::Node* readPlyFile( const char* file, const bool ignoreColors = false ); + + // to set the flag for using inverted face + void useInvertedFaces() { _invertFaces = true; } + + private: + // Function which reads all the vertices and colors if color info is + // given and also if the user wants that information + void readVertices( PlyFile* file, const int nVertices, + const bool readColors ); + + // Reads the triangle indices from the ply file + void readTriangles( PlyFile* file, const int nFaces ); + + // Calculates the normals according to passed flag + // if vertexNormals is true then computes normal per vertices + // otherwise per triangle means per face + void _calculateNormals( const bool vertexNormals = true ); + + bool _invertFaces; + + // Vertex array in osg format + osg::ref_ptr _vertices; + // Color array in osg format + osg::ref_ptr _colors; + // Normals in osg format + osg::ref_ptr _normals; + // The indices of the faces in premitive set + osg::ref_ptr _triangles; + }; +} + + +#endif // MESH_VERTEXDATA_H