From Johan Nouvel, "Some times ago, I have coded an osg to vrml2 writer. Today, I have updated our writer to osg 2.9.9.

As it works (for our needs at least) I've done and attached a tar.gz file for the VRML2 plugin with a new part to write a VRML2 file from an osg one.

The read part is the same as in osg 2.9.9.

The write part code is in convertToVRML.cpp and .h  files. It works for some osg nodes (group, geode, matrixTransform, positionAttitudeTransform and geometry). Textures are converted to jpeg (if not translucent) or png (if translucent).
There are some options that could be given to the writer (with -O switch) :

convertTextures=0   to copy textures without converting them to jpeg or png
convertTextures=-1  do not copy textures, keep them in their original format and location
convertTextures=-2  do not use textures, parse only geometry
convertTextures=-3 (default) convert textures to jpeg or png ones.

textureUnit=X  in case of multiple textures, X= texture unit to use (default value=0)

directoryTexture=aPath  when texture will be copied, it will be in this directory, not in the current one."
This commit is contained in:
Robert Osfield
2010-11-08 15:49:30 +00:00
parent be583ef0d6
commit af22bd70db
5 changed files with 1446 additions and 2 deletions

View File

@@ -37,10 +37,12 @@ SET(TARGET_SRC
IndexedFaceSet.cpp
Primitives.cpp
ReaderWriterVRML2.cpp
ConvertToVRML.cpp
)
SET(TARGET_H
ReaderWriterVRML2.h
ConvertToVRML.h
)
#### end var setup ###

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,115 @@
/*
*
* OSG to VRML2 converter for OpenSceneGraph.
*
* authors :
* Johan Nouvel (johan_nouvel@yahoo.com)
*
*
*/
#ifndef __convert_to_vrml_to_osg_
#define __convert_to_vrml_to_osg_ 1
#include <fstream>
#include <sstream>
// OSG headers
#include <osg/MatrixTransform>
#include <osg/PositionAttitudeTransform>
#include <osg/Geode>
#include <osg/Billboard>
#include <osg/Notify>
#include <osg/LineWidth>
#include <osg/Point>
#include <osg/TexEnv>
#include <osg/TexGen>
#include <osg/Texture2D>
#include <osg/PolygonMode>
#include <osg/BlendFunc>
#include <osg/Material>
#include <osg/CullFace>
#include <osg/LightModel>
#include <osg/LOD>
#include <osgUtil/TransformCallback>
#include <osg/Object>
#include <osgDB/ReaderWriter>
#include <osg/NodeVisitor>
#include <string>
#include <stack>
#include <osg/Node>
#include <vector>
#include <map>
#include <stack>
using namespace std;
/**
* This function is called to write an osg graph into a VRML file
*
*/
osgDB::ReaderWriter::WriteResult convertToVRML(const osg::Node& root, const std::string& filename, const osgDB::ReaderWriter::Options* options=NULL) ;
class ToVRML: public osg::NodeVisitor {
public:
typedef enum {
NO_TEXTURE = 0, KEEP_ORIGINAL_TEXTURE = 1, COPY_TEXTURE = 2, CONVERT_TEXTURE = 3
} TextureMode;
ToVRML(const std::string& fileName, const osgDB::ReaderWriter::Options* options);
virtual ~ToVRML();
osgDB::ReaderWriter::WriteResult result() {
return (_res);
}
;
virtual void apply(osg::Geode& node);
virtual void apply(osg::Group& node);
virtual void apply(osg::Billboard& node);
virtual void apply(osg::MatrixTransform& node);
virtual void apply(osg::PositionAttitudeTransform& node);
virtual void apply(osg::Node& node);
virtual void apply(osg::LOD& node);
char *indent();
char *indentM();
char *indentL();
void pushStateSetStack(osg::StateSet* ss);
void popStateSetStack();
osg::StateSet* getCurrentStateSet();
//void findObjectName(osg::Object* obj,std::string& name, bool& alreadyLoaded);
void findMaterialName(osg::Material* mat, std::string& name, bool& alreadyLoaded);
void findTextureName(osg::Texture2D* tex, std::string& name, bool& alreadyLoaded);
void apply(osg::Geometry* geom);
void apply(osg::Drawable* drawable);
void writeAppearance(osg::StateSet* stateset);
void writeCoord(osg::Vec3Array* array);
template<typename T> void writeCoordIndex(GLenum mode, T* indices, unsigned int number, std::vector<int>& primitiveSetFaces, int& primitiveFaces);
void writeTexCoord(osg::Vec2Array* array, osg::Vec3Array* array2);
osg::Vec2Array* buildUVArray(osg::TexGen* tGen, osg::Vec3Array* array);
osg::Vec2Array* buildUVArray(osg::TexEnv* tEnv, osg::Vec3Array* array);
void writeUVArray(osg::Vec2Array* uvArray, osg::Texture::WrapMode wrap_s, osg::Texture::WrapMode wrap_t);
void writeNormal(osg::Geometry* geom, std::vector<int>& primitiveSetFaces, int nbFaces);
void writeColor(osg::Geometry* geom, std::vector<int>& primitiveSetFaces, int nbFaces);
osgDB::ReaderWriter::WriteResult _res;
ofstream _fout;
int _indent;
char* _strIndent;
map<osg::ref_ptr<osg::Texture2D>, std::string> _texMap;
map<osg::ref_ptr<osg::Material>, std::string> _matMap;
vector<osg::ref_ptr<osg::StateSet> > _stack;
osg::ref_ptr<osg::Image> _defaultImage;
std::string _pathToOutput;
std::string _pathRelativeToOutput;
TextureMode _textureMode;
int _txtUnit;
};
#endif

View File

@@ -4,7 +4,10 @@
*
* VRML2 file converter for OpenSceneGraph.
*
* authors : Jan Ciger (jan.ciger@gmail.com),
* authors :
* Johan Nouvel (johan_nouvel@yahoo.com) for the writeNode function.
*
* Jan Ciger (jan.ciger@gmail.com),
* Tolga Abaci (tolga.abaci@gmail.com),
* Bruno Herbelin (bruno.herbelin@gmail.com)
*
@@ -16,6 +19,7 @@
*/
#include "ReaderWriterVRML2.h"
#include "ConvertToVRML.h"
#include <iostream>
#include <fstream>
@@ -223,6 +227,21 @@ osgDB::ReaderWriter::ReadResult ReaderWriterVRML2::readNode(const std::string& f
catch (std::invalid_argument) { return ReadResult::FILE_NOT_HANDLED; }
}
osgDB::ReaderWriter::WriteResult ReaderWriterVRML2::writeNode(const osg::Node& root,const std::string& filename, const osgDB::ReaderWriter::Options *options) const
{
std::string ext = osgDB::getLowerCaseFileExtension(filename);
if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED;
osg::notify(osg::INFO) << "osgDB::ReaderWriterVRML::writeNode() Writing file "
<< filename << std::endl;
return(convertToVRML(root,filename,options));
}
osg::ref_ptr<osg::Node> ReaderWriterVRML2::convertFromVRML(openvrml::node *obj) const
{
//static int osgLightNum = 0; //light

View File

@@ -4,7 +4,10 @@
*
* VRML2 file converter for OpenSceneGraph.
*
* authors : Jan Ciger (jan.ciger@gmail.com),
* authors :
* Johan Nouvel (johan_nouvel@yahoo.com) for the writeNode function.
*
* Jan Ciger (jan.ciger@gmail.com),
* Tolga Abaci (tolga.abaci@gmail.com),
* Bruno Herbelin (bruno.herbelin@gmail.com)
*
@@ -75,6 +78,8 @@ public:
virtual ReadResult readNode(const std::string&, const osgDB::Options *options) const;
// virtual ReadResult readNode(std::istream& fin, const osgDB::Options* options) const;
virtual WriteResult writeNode(const osg::Node&,const std::string& filename,const osgDB::ReaderWriter::Options *options) const;
private:
typedef std::map<float, osg::ref_ptr<osg::Geometry> > SphereLibrary;
typedef std::map<osg::Vec3, osg::ref_ptr<osg::Geometry> > BoxLibrary;