00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef VRS_X3D_X3DREADER_H
00019 #define VRS_X3D_X3DREADER_H
00020
00021 #include <vrs/container/dictionary.h>
00022 #include <vrs/container/stack.h>
00023 #include <vrs/io/objectreader.h>
00024 #include <vrs/io/ioerror.h>
00025 #include <vrs/sg/lodnode.h>
00026 #include <vrs/sg/scenething.h>
00027 #include <vrs/polygonset.h>
00028 #include <vrs/linestyle.h>
00029 #include <vrs/facestyle.h>
00030 #include <vrs/opengl/shapematerialgl.h>
00031 #include <vrs/image/image.h>
00032 #include <vrs/opengl/imagetexture2dgl.h>
00033 #include <vrs/opengl/texenvcombinegl.h>
00034 #include <vrs/opengl/texgengl.h>
00035 #include <vrs/matrix.h>
00036 #include <vrs/extrusion.h>
00037 #include <vrs/x3d/x3dhelper.h>
00038
00039 #include <xercesc/util/XercesDefs.hpp>
00040 #include <xercesc/sax2/SAX2XMLReader.hpp>
00041 #include <xercesc/sax2/XMLReaderFactory.hpp>
00042 #include <xercesc/sax2/DefaultHandler.hpp>
00043 #include <xercesc/sax2/Attributes.hpp>
00044 #include <xercesc/sax/SAXParseException.hpp>
00045 #include <xercesc/util/PlatformUtils.hpp>
00046 #include <xercesc/util/XMLUniDefs.hpp>
00047 #include <xercesc/util/XMLStringTokenizer.hpp>
00048 #include <xercesc/framework/LocalFileInputSource.hpp>
00049
00050 #ifdef WIN32
00051 #define XMLCH(CHAR) L ## CHAR
00052 #else
00053 #include <memory>
00054 #include <vrs/x3d/x3dxercesstring.h>
00055 #define XMLCH(CHAR) fromNative(CHAR).c_str()
00056 #endif
00057
00058 namespace VRS {
00059 namespace X3D {
00060
00120 class VRS_X3D_API X3DReader : public ObjectReader, public XERCES_CPP_NAMESPACE::DefaultHandler {
00121 private:
00123 static const XMLCh* delim_;
00124 #ifdef WIN32
00125 static const wchar_t* x3d_delimiters_;
00126 #else
00127 static const char* x3d_delimiters_;
00128 #endif
00129 static unsigned short restriction_;
00130 static bool warn_;
00131
00132 XERCES_CPP_NAMESPACE::SAX2XMLReader* parser_;
00133
00134 mutable std::string m_currentDir;
00135 mutable std::set<std::string> m_associatedFiles;
00136
00137 mutable SO<Stack<SO<SceneNode> > > nodeStack_;
00138 #ifdef WIN32
00139 mutable SO<Dictionary<std::wstring, SO<SharedObj> > > defUseDictionary_;
00140 #else
00141 mutable std::map<XercesString, SO<SharedObj> > defUseDictionary_;
00142 #endif
00143 mutable SO<Stack<unsigned int> > lodIndexStack_;
00144
00145 mutable SO<X3DAppearance> x3dAppearance_;
00146 mutable SO<X3DGeometry> x3dGeometry_;
00147
00148 void append(SO<SceneNode> node);
00149 static Color stringToColor(const XMLCh* const string, int components);
00150 void defAttribute(const XMLCh* const name, SO<SharedObj> object);
00151 SO<SharedObj> useAttribute(const XMLCh* const name);
00152
00153 void inlineTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00154
00155
00156 void groupTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00157 void transformTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00158
00159
00160 void attachRendering(void);
00161 void rangeCountAttribute(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00162 void indexAttribute(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00163 void additionalIndexAttribute(const XERCES_CPP_NAMESPACE::Attributes& attributes, const std::string& type);
00164 void commonAttributes(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00165
00166 void indexedLineSetTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00167 void indexedTriangleFanSetTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00168 void indexedTriangleSetTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00169 void indexedTriangleStripSetTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00170 void pointSetTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00171 void lineSetTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00172 void triangleFanSetTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00173 void triangleSetTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00174 void triangleStripSetTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00175
00176 void coordinateTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00177 void colorTag(const XERCES_CPP_NAMESPACE::Attributes& attributes, bool alpha);
00178 void normalTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00179
00180
00181 void attachShape(void);
00182 void shapeTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00183 void appearanceTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00184 void linePropertiesTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00185 void materialTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00186
00187
00188 void boxTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00189 void coneTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00190 void cylinderTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00191 void sphereTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00192 void extrusionTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00193
00194
00195 void directionalLightTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00196 void pointLightTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00197 void spotLightTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00198
00199
00200 void attachTexture(void);
00201 void imageTextureTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00202 void multiTextureTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00203 void multiTextureCoordinateTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00204 void multiTextureTransformTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00205 void pixelTextureTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00206 void textureCoordinateTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00207 void textureCoordinateGeneratorTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00208 void textureTransformTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00209
00210
00211 void lodTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00212 void billboardTag(const XERCES_CPP_NAMESPACE::Attributes& attributes);
00213
00214 static double toDouble(const XMLCh* const str);
00215 static int toInt(const XMLCh* const str);
00216
00217 static std::vector<double> toDoubles(const XMLCh* const str);
00218 static std::vector<int> toInts(const XMLCh* const str);
00219
00220 public:
00221 X3DReader(void);
00222 ~X3DReader(void);
00223
00224
00226 virtual SO<SharedObj> read(DataResource* data, const ID& targetType) const;
00227
00228 virtual const char* objectFormatName() const { return "Extensible 3D"; }
00229 virtual const char* objectFileSuffix() const { return "x3d"; }
00230
00231
00232 virtual void startElement(const XMLCh* const uri, const XMLCh* const localname, const XMLCh* const qname, const XERCES_CPP_NAMESPACE::Attributes& attributes);
00233 virtual void endElement(const XMLCh* const uri, const XMLCh* const localname, const XMLCh* const qname);
00234
00235 virtual XERCES_CPP_NAMESPACE::InputSource* resolveEntity(const XMLCh* const publicId, const XMLCh* const systemId);
00236
00237 virtual void warning(const XERCES_CPP_NAMESPACE::SAXParseException& e);
00238 virtual void error(const XERCES_CPP_NAMESPACE::SAXParseException& e);
00239 virtual void fatalError(const XERCES_CPP_NAMESPACE::SAXParseException& e);
00240
00241 enum Restriction { Viewpoint = 1 };
00242 static void setRestriction(const unsigned short restriction) { restriction_ = restriction; }
00243 static void setWarning(const bool warn) { warn_ = warn; }
00244
00245 VRS_TYPEINFO(X3DReader, ObjectReader);
00246 };
00247
00248 }
00249 }
00250
00251 #endif // VRS_X3D_X3DREADER_H