| 1 | #include <sstream> |
|---|
| 2 | |
|---|
| 3 | #include <osg/Image> |
|---|
| 4 | #include <osg/Group> |
|---|
| 5 | #include <osg/Notify> |
|---|
| 6 | #include <osg/Version> |
|---|
| 7 | |
|---|
| 8 | #include <osgDB/FileNameUtils> |
|---|
| 9 | #include <osgDB/FileUtils> |
|---|
| 10 | #include <osgDB/fstream> |
|---|
| 11 | #include <osgDB/Registry> |
|---|
| 12 | #include <osgDB/Input> |
|---|
| 13 | #include <osgDB/Output> |
|---|
| 14 | |
|---|
| 15 | using namespace osg; |
|---|
| 16 | using namespace osgDB; |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #if 0 |
|---|
| 20 | |
|---|
| 21 | USE_DOTOSGWRAPPER(AlphaFunc) |
|---|
| 22 | USE_DOTOSGWRAPPER(AnimationPath) |
|---|
| 23 | USE_DOTOSGWRAPPER(AutoTransform) |
|---|
| 24 | USE_DOTOSGWRAPPER(Billboard) |
|---|
| 25 | USE_DOTOSGWRAPPER(BlendColor) |
|---|
| 26 | USE_DOTOSGWRAPPER(BlendEquation) |
|---|
| 27 | USE_DOTOSGWRAPPER(BlendFunc) |
|---|
| 28 | USE_DOTOSGWRAPPER(Camera) |
|---|
| 29 | USE_DOTOSGWRAPPER(CameraView) |
|---|
| 30 | USE_DOTOSGWRAPPER(ClearNode) |
|---|
| 31 | USE_DOTOSGWRAPPER(ClipNode) |
|---|
| 32 | USE_DOTOSGWRAPPER(ClipPlane) |
|---|
| 33 | USE_DOTOSGWRAPPER(ClusterCullingCallback) |
|---|
| 34 | USE_DOTOSGWRAPPER(ColorMask) |
|---|
| 35 | USE_DOTOSGWRAPPER(ColorMatrix) |
|---|
| 36 | USE_DOTOSGWRAPPER(ConvexPlanarOccluder) |
|---|
| 37 | USE_DOTOSGWRAPPER(CoordinateSystemNode) |
|---|
| 38 | USE_DOTOSGWRAPPER(CullFace) |
|---|
| 39 | USE_DOTOSGWRAPPER(Depth) |
|---|
| 40 | USE_DOTOSGWRAPPER(Drawable) |
|---|
| 41 | USE_DOTOSGWRAPPER(EllipsoidModel) |
|---|
| 42 | USE_DOTOSGWRAPPER(Fog) |
|---|
| 43 | USE_DOTOSGWRAPPER(FragmentProgram) |
|---|
| 44 | USE_DOTOSGWRAPPER(FrontFace) |
|---|
| 45 | USE_DOTOSGWRAPPER(Geode) |
|---|
| 46 | USE_DOTOSGWRAPPER(Geometry) |
|---|
| 47 | USE_DOTOSGWRAPPER(Group) |
|---|
| 48 | USE_DOTOSGWRAPPER(Image) |
|---|
| 49 | USE_DOTOSGWRAPPER(ImageSequence) |
|---|
| 50 | USE_DOTOSGWRAPPER(Light) |
|---|
| 51 | USE_DOTOSGWRAPPER(LightModel) |
|---|
| 52 | USE_DOTOSGWRAPPER(LightSource) |
|---|
| 53 | USE_DOTOSGWRAPPER(LineStipple) |
|---|
| 54 | USE_DOTOSGWRAPPER(LineWidth) |
|---|
| 55 | USE_DOTOSGWRAPPER(LOD) |
|---|
| 56 | USE_DOTOSGWRAPPER(Material) |
|---|
| 57 | USE_DOTOSGWRAPPER(MatrixTransform) |
|---|
| 58 | USE_DOTOSGWRAPPER(NodeCallback) |
|---|
| 59 | USE_DOTOSGWRAPPER(Node) |
|---|
| 60 | USE_DOTOSGWRAPPER(Object) |
|---|
| 61 | USE_DOTOSGWRAPPER(OccluderNode) |
|---|
| 62 | USE_DOTOSGWRAPPER(OcclusionQueryNode) |
|---|
| 63 | USE_DOTOSGWRAPPER(PagedLOD) |
|---|
| 64 | USE_DOTOSGWRAPPER(Point) |
|---|
| 65 | USE_DOTOSGWRAPPER(PointSprite) |
|---|
| 66 | USE_DOTOSGWRAPPER(PolygonMode) |
|---|
| 67 | USE_DOTOSGWRAPPER(PolygonOffset) |
|---|
| 68 | USE_DOTOSGWRAPPER(PositionAttitudeTransform) |
|---|
| 69 | USE_DOTOSGWRAPPER(Program) |
|---|
| 70 | USE_DOTOSGWRAPPER(Projection) |
|---|
| 71 | USE_DOTOSGWRAPPER(ProxyNode) |
|---|
| 72 | USE_DOTOSGWRAPPER(Scissor) |
|---|
| 73 | USE_DOTOSGWRAPPER(Sequence) |
|---|
| 74 | USE_DOTOSGWRAPPER(ShadeModel) |
|---|
| 75 | USE_DOTOSGWRAPPER(Shader) |
|---|
| 76 | USE_DOTOSGWRAPPER(Sphere) |
|---|
| 77 | USE_DOTOSGWRAPPER(Cone) |
|---|
| 78 | USE_DOTOSGWRAPPER(Capsule) |
|---|
| 79 | USE_DOTOSGWRAPPER(Box) |
|---|
| 80 | USE_DOTOSGWRAPPER(HeightField) |
|---|
| 81 | USE_DOTOSGWRAPPER(CompositeShape) |
|---|
| 82 | USE_DOTOSGWRAPPER(Cylinder) |
|---|
| 83 | USE_DOTOSGWRAPPER(ShapeDrawable) |
|---|
| 84 | USE_DOTOSGWRAPPER(StateAttribute) |
|---|
| 85 | USE_DOTOSGWRAPPER(StateSet) |
|---|
| 86 | USE_DOTOSGWRAPPER(Stencil) |
|---|
| 87 | USE_DOTOSGWRAPPER(Switch) |
|---|
| 88 | USE_DOTOSGWRAPPER(TessellationHints) |
|---|
| 89 | USE_DOTOSGWRAPPER(TexEnvCombine) |
|---|
| 90 | USE_DOTOSGWRAPPER(TexEnv) |
|---|
| 91 | USE_DOTOSGWRAPPER(TexEnvFilter) |
|---|
| 92 | USE_DOTOSGWRAPPER(TexGen) |
|---|
| 93 | USE_DOTOSGWRAPPER(TexGenNode) |
|---|
| 94 | USE_DOTOSGWRAPPER(TexMat) |
|---|
| 95 | USE_DOTOSGWRAPPER(Texture1D) |
|---|
| 96 | USE_DOTOSGWRAPPER(Texture2D) |
|---|
| 97 | USE_DOTOSGWRAPPER(Texture3D) |
|---|
| 98 | USE_DOTOSGWRAPPER(Texture) |
|---|
| 99 | USE_DOTOSGWRAPPER(TextureCubeMap) |
|---|
| 100 | USE_DOTOSGWRAPPER(TextureRectangle) |
|---|
| 101 | USE_DOTOSGWRAPPER(Transform) |
|---|
| 102 | USE_DOTOSGWRAPPER(Uniform) |
|---|
| 103 | USE_DOTOSGWRAPPER(VertexProgram) |
|---|
| 104 | USE_DOTOSGWRAPPER(Viewport) |
|---|
| 105 | #endif |
|---|
| 106 | |
|---|
| 107 | class OSGReaderWriter : public ReaderWriter |
|---|
| 108 | { |
|---|
| 109 | public: |
|---|
| 110 | |
|---|
| 111 | mutable OpenThreads::Mutex _mutex; |
|---|
| 112 | mutable bool _wrappersLoaded; |
|---|
| 113 | |
|---|
| 114 | OSGReaderWriter(): |
|---|
| 115 | _wrappersLoaded(false) |
|---|
| 116 | { |
|---|
| 117 | |
|---|
| 118 | supportsExtension("osg","OpenSceneGraph Ascii file format"); |
|---|
| 119 | supportsExtension("osgs","Psuedo OpenSceneGraph file loaded, with file encoded in filename string"); |
|---|
| 120 | supportsOption("precision","Set the floating point precision when writing out files"); |
|---|
| 121 | supportsOption("OutputTextureFiles","Write out the texture images to file"); |
|---|
| 122 | supportsOption("includeExternalReferences","Export option"); |
|---|
| 123 | supportsOption("writeExternalReferenceFiles","Export option"); |
|---|
| 124 | |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | virtual const char* className() const { return "OSG Reader/Writer"; } |
|---|
| 128 | |
|---|
| 129 | bool loadWrappers() const |
|---|
| 130 | { |
|---|
| 131 | #ifndef OSG_LIBRARY_STATIC |
|---|
| 132 | if (_wrappersLoaded) return true; |
|---|
| 133 | |
|---|
| 134 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 135 | if (_wrappersLoaded) return true; |
|---|
| 136 | |
|---|
| 137 | std::string filename = osgDB::Registry::instance()->createLibraryNameForExtension("deprecated_osg"); |
|---|
| 138 | if (osgDB::Registry::instance()->loadLibrary(filename)==osgDB::Registry::LOADED) |
|---|
| 139 | { |
|---|
| 140 | OSG_INFO<<"OSGReaderWriter wrappers loaded OK"<<std::endl; |
|---|
| 141 | _wrappersLoaded = true; |
|---|
| 142 | return true; |
|---|
| 143 | } |
|---|
| 144 | else |
|---|
| 145 | { |
|---|
| 146 | OSG_NOTICE<<"OSGReaderWriter wrappers failed to load"<<std::endl; |
|---|
| 147 | _wrappersLoaded = true; |
|---|
| 148 | return false; |
|---|
| 149 | } |
|---|
| 150 | #else |
|---|
| 151 | return true; |
|---|
| 152 | #endif |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | virtual ReadResult readObject(const std::string& file, const Options* opt) const |
|---|
| 156 | { |
|---|
| 157 | std::string ext = osgDB::getLowerCaseFileExtension(file); |
|---|
| 158 | |
|---|
| 159 | if (equalCaseInsensitive(ext,"osgs")) |
|---|
| 160 | { |
|---|
| 161 | std::istringstream fin(osgDB::getNameLessExtension(file)); |
|---|
| 162 | if (fin) return readNode(fin,opt); |
|---|
| 163 | return ReadResult::ERROR_IN_READING_FILE; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; |
|---|
| 167 | |
|---|
| 168 | std::string fileName = osgDB::findDataFile( file, opt ); |
|---|
| 169 | if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | osg::ref_ptr<Options> local_opt = opt ? static_cast<Options*>(opt->clone(osg::CopyOp::SHALLOW_COPY)) : new Options; |
|---|
| 173 | local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName)); |
|---|
| 174 | |
|---|
| 175 | osgDB::ifstream fin(fileName.c_str()); |
|---|
| 176 | if (fin) |
|---|
| 177 | { |
|---|
| 178 | return readObject(fin, local_opt.get()); |
|---|
| 179 | } |
|---|
| 180 | return 0L; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | virtual ReadResult readObject(std::istream& fin, const Options* options) const |
|---|
| 184 | { |
|---|
| 185 | loadWrappers(); |
|---|
| 186 | |
|---|
| 187 | fin.imbue(std::locale::classic()); |
|---|
| 188 | |
|---|
| 189 | Input fr; |
|---|
| 190 | fr.attach(&fin); |
|---|
| 191 | fr.setOptions(options); |
|---|
| 192 | |
|---|
| 193 | typedef std::vector<osg::Object*> ObjectList; |
|---|
| 194 | ObjectList objectList; |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | while(!fr.eof()) |
|---|
| 198 | { |
|---|
| 199 | Object *object = fr.readObject(); |
|---|
| 200 | if (object) objectList.push_back(object); |
|---|
| 201 | else fr.advanceOverCurrentFieldOrBlock(); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | if (objectList.empty()) |
|---|
| 205 | { |
|---|
| 206 | return ReadResult("No data loaded"); |
|---|
| 207 | } |
|---|
| 208 | else if (objectList.size()==1) |
|---|
| 209 | { |
|---|
| 210 | return objectList.front(); |
|---|
| 211 | } |
|---|
| 212 | else |
|---|
| 213 | { |
|---|
| 214 | return objectList.front(); |
|---|
| 215 | } |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | virtual ReadResult readNode(const std::string& file, const Options* opt) const |
|---|
| 219 | { |
|---|
| 220 | std::string ext = osgDB::getLowerCaseFileExtension(file); |
|---|
| 221 | |
|---|
| 222 | if (equalCaseInsensitive(ext,"osgs")) |
|---|
| 223 | { |
|---|
| 224 | std::istringstream fin(osgDB::getNameLessExtension(file)); |
|---|
| 225 | if (fin) return readNode(fin,opt); |
|---|
| 226 | return ReadResult::ERROR_IN_READING_FILE; |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; |
|---|
| 230 | |
|---|
| 231 | std::string fileName = osgDB::findDataFile( file, opt ); |
|---|
| 232 | if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | osg::ref_ptr<Options> local_opt = opt ? static_cast<Options*>(opt->clone(osg::CopyOp::SHALLOW_COPY)) : new Options; |
|---|
| 236 | local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName)); |
|---|
| 237 | |
|---|
| 238 | osgDB::ifstream fin(fileName.c_str()); |
|---|
| 239 | if (fin) |
|---|
| 240 | { |
|---|
| 241 | return readNode(fin, local_opt.get()); |
|---|
| 242 | } |
|---|
| 243 | return 0L; |
|---|
| 244 | |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | virtual ReadResult readNode(std::istream& fin, const Options* options) const |
|---|
| 248 | { |
|---|
| 249 | loadWrappers(); |
|---|
| 250 | |
|---|
| 251 | fin.imbue(std::locale::classic()); |
|---|
| 252 | |
|---|
| 253 | Input fr; |
|---|
| 254 | fr.attach(&fin); |
|---|
| 255 | fr.setOptions(options); |
|---|
| 256 | |
|---|
| 257 | typedef std::vector<osg::Node*> NodeList; |
|---|
| 258 | NodeList nodeList; |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | while(!fr.eof()) |
|---|
| 262 | { |
|---|
| 263 | Node *node = fr.readNode(); |
|---|
| 264 | if (node) nodeList.push_back(node); |
|---|
| 265 | else fr.advanceOverCurrentFieldOrBlock(); |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | if (nodeList.empty()) |
|---|
| 269 | { |
|---|
| 270 | return ReadResult("No data loaded"); |
|---|
| 271 | } |
|---|
| 272 | else if (nodeList.size()==1) |
|---|
| 273 | { |
|---|
| 274 | return nodeList.front(); |
|---|
| 275 | } |
|---|
| 276 | else |
|---|
| 277 | { |
|---|
| 278 | Group* group = new Group; |
|---|
| 279 | group->setName("import group"); |
|---|
| 280 | for(NodeList::iterator itr=nodeList.begin(); |
|---|
| 281 | itr!=nodeList.end(); |
|---|
| 282 | ++itr) |
|---|
| 283 | { |
|---|
| 284 | group->addChild(*itr); |
|---|
| 285 | } |
|---|
| 286 | return group; |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | void setPrecision(Output& fout, const osgDB::ReaderWriter::Options* options) const |
|---|
| 292 | { |
|---|
| 293 | if (options) |
|---|
| 294 | { |
|---|
| 295 | std::istringstream iss(options->getOptionString()); |
|---|
| 296 | std::string opt; |
|---|
| 297 | while (iss >> opt) |
|---|
| 298 | { |
|---|
| 299 | if(opt=="PRECISION" || opt=="precision") |
|---|
| 300 | { |
|---|
| 301 | int prec; |
|---|
| 302 | iss >> prec; |
|---|
| 303 | fout.precision(prec); |
|---|
| 304 | } |
|---|
| 305 | if (opt=="OutputTextureFiles") |
|---|
| 306 | { |
|---|
| 307 | fout.setOutputTextureFiles(true); |
|---|
| 308 | } |
|---|
| 309 | if (opt=="OutputShaderFiles") |
|---|
| 310 | { |
|---|
| 311 | fout.setOutputShaderFiles(true); |
|---|
| 312 | } |
|---|
| 313 | } |
|---|
| 314 | } |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | virtual WriteResult writeObject(const Object& obj, const std::string& fileName, const osgDB::ReaderWriter::Options* options) const |
|---|
| 318 | { |
|---|
| 319 | std::string ext = osgDB::getLowerCaseFileExtension(fileName); |
|---|
| 320 | if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED; |
|---|
| 321 | |
|---|
| 322 | Output fout(fileName.c_str()); |
|---|
| 323 | if (fout) |
|---|
| 324 | { |
|---|
| 325 | loadWrappers(); |
|---|
| 326 | |
|---|
| 327 | fout.setOptions(options); |
|---|
| 328 | |
|---|
| 329 | setPrecision(fout,options); |
|---|
| 330 | |
|---|
| 331 | fout.imbue(std::locale::classic()); |
|---|
| 332 | |
|---|
| 333 | fout.writeObject(obj); |
|---|
| 334 | fout.close(); |
|---|
| 335 | return WriteResult::FILE_SAVED; |
|---|
| 336 | } |
|---|
| 337 | return WriteResult("Unable to open file for output"); |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | virtual WriteResult writeObject(const Object& obj,std::ostream& fout, const osgDB::ReaderWriter::Options* options) const |
|---|
| 341 | { |
|---|
| 342 | if (fout) |
|---|
| 343 | { |
|---|
| 344 | loadWrappers(); |
|---|
| 345 | |
|---|
| 346 | Output foutput; |
|---|
| 347 | foutput.setOptions(options); |
|---|
| 348 | |
|---|
| 349 | std::ios &fios = foutput; |
|---|
| 350 | fios.rdbuf(fout.rdbuf()); |
|---|
| 351 | |
|---|
| 352 | fout.imbue(std::locale::classic()); |
|---|
| 353 | |
|---|
| 354 | setPrecision(foutput,options); |
|---|
| 355 | |
|---|
| 356 | foutput.writeObject(obj); |
|---|
| 357 | return WriteResult::FILE_SAVED; |
|---|
| 358 | } |
|---|
| 359 | return WriteResult("Unable to write to output stream"); |
|---|
| 360 | } |
|---|
| 361 | |
|---|
| 362 | |
|---|
| 363 | virtual WriteResult writeNode(const Node& node, const std::string& fileName, const osgDB::ReaderWriter::Options* options) const |
|---|
| 364 | { |
|---|
| 365 | std::string ext = getFileExtension(fileName); |
|---|
| 366 | if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED; |
|---|
| 367 | |
|---|
| 368 | Output fout(fileName.c_str()); |
|---|
| 369 | if (fout) |
|---|
| 370 | { |
|---|
| 371 | loadWrappers(); |
|---|
| 372 | |
|---|
| 373 | fout.setOptions(options); |
|---|
| 374 | |
|---|
| 375 | fout.imbue(std::locale::classic()); |
|---|
| 376 | |
|---|
| 377 | setPrecision(fout,options); |
|---|
| 378 | |
|---|
| 379 | fout.writeObject(node); |
|---|
| 380 | fout.close(); |
|---|
| 381 | return WriteResult::FILE_SAVED; |
|---|
| 382 | } |
|---|
| 383 | return WriteResult("Unable to open file for output"); |
|---|
| 384 | } |
|---|
| 385 | |
|---|
| 386 | virtual WriteResult writeNode(const Node& node, std::ostream& fout, const osgDB::ReaderWriter::Options* options) const |
|---|
| 387 | { |
|---|
| 388 | if (fout) |
|---|
| 389 | { |
|---|
| 390 | loadWrappers(); |
|---|
| 391 | |
|---|
| 392 | Output foutput; |
|---|
| 393 | foutput.setOptions(options); |
|---|
| 394 | |
|---|
| 395 | std::ios &fios = foutput; |
|---|
| 396 | fios.rdbuf(fout.rdbuf()); |
|---|
| 397 | |
|---|
| 398 | foutput.imbue(std::locale::classic()); |
|---|
| 399 | |
|---|
| 400 | setPrecision(foutput,options); |
|---|
| 401 | |
|---|
| 402 | foutput.writeObject(node); |
|---|
| 403 | return WriteResult::FILE_SAVED; |
|---|
| 404 | } |
|---|
| 405 | return WriteResult("Unable to write to output stream"); |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | }; |
|---|
| 409 | |
|---|
| 410 | |
|---|
| 411 | |
|---|
| 412 | REGISTER_OSGPLUGIN(osg, OSGReaderWriter) |
|---|