00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #pragma once
00013
00015 #if defined(_WIN32) || defined(_WIN64)
00016 # define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
00017 # include <windows.h>
00018 #endif
00019
00020 #include <fstream>
00021 #include <set>
00022 #include "nvsg/nvsg.h"
00023 #include "nvsg/PlugInterface.h"
00024 #include "VRMLTypes.h"
00025
00026
00027 namespace nvsg
00028 {
00029 class DirectedLight;
00030 class Drawable;
00031 class GeoNode;
00032 class GeoSet;
00033 class Group;
00034 class LOD;
00035 class Material;
00036 class PointLight;
00037 class SpotLight;
00038 class StateSet;
00039 class Switch;
00040 class TextureAttribute;
00041 class Transform;
00042 class VNVector;
00043 template<typename T> class LinearInterpolatedAnimation;
00044 }
00045
00046
00047 #if ! defined( DOXYGEN_IGNORE ) && defined(_WIN32)
00048 # ifdef WRLLOADER_EXPORTS
00049 # define WRLLOADER_API __declspec(dllexport)
00050 # else
00051 # define WRLLOADER_API __declspec(dllimport)
00052 # endif
00053 #else
00054 # define WRLLOADER_API
00055 #endif
00056
00057 #if defined(LINUX)
00058 void lib_init() __attribute__ ((constructor));
00059 #endif
00060
00061 #if defined(LINUX)
00062 extern "C"
00063 {
00064 #endif
00065
00066
00070 WRLLOADER_API bool getPlugInterface(const nvutil::UPIID& piid, nvutil::PlugIn *& pi);
00071
00073
00078 WRLLOADER_API bool queryPlugInterfaceType(const nvutil::UPITID& pitid, std::vector<nvutil::UPIID>& piids);
00079 #if defined(LINUX)
00080 }
00081 #endif
00082
00084 class WRLLoader : public nvsg::SceneLoader
00085 {
00086 public :
00087
00088 WRLLoader();
00089
00090 public :
00092
00093 void deleteThis( void );
00094
00096
00101 const nvsg::Scene * load( const std::string& filename
00102 , const std::vector<std::string> &searchPaths
00103 , const nvsg::ViewState *& viewState
00105 );
00106
00107 private :
00108 void calculateNormals( vrml::IndexedFaceSet * pIndexedFaceSet,
00109 const nvsg::IndexSet &triVerts, const nvsg::IndexSet &triFaces,
00110 const nvsg::IndexSet &quadVerts, const nvsg::IndexSet &quadFaces );
00111 template<typename T>
00112 void combine( vrml::MFFloat &combinedKey, vrml::MFFloat &key, std::vector<T> &value );
00113 void determineTexGen( vrml::ElevationGrid * pElevationGrid, const nvsg::StateSet *pStateSet );
00114 void determineTexGen( vrml::IndexedFaceSet * pIndexedFaceSet, const nvsg::StateSet *pStateSet );
00115 template<typename VType>
00116 void eraseIndex( size_t f, size_t i, size_t count, bool perVertex, vrml::MFInt32 &index,
00117 VType &vec );
00118 template<typename VType>
00119 void filterZeroIntervals( vrml::MFFloat &key, std::vector<VType> &value );
00120 vrml::SFNode findNode( const vrml::SFNode currentNode, std::string name );
00121 size_t getKeyCount( vrml::MFFloat &key );
00122 void getKeyRange( vrml::MFFloat &key );
00123 bool getNextLine( void );
00124 std::string getNextToken( void );
00125 vrml::SFNode getNode( const std::string &nodeName );
00126 void ignoreBlock( const std::string &open, const std::string &close );
00127 const nvsg::Scene * import( const std::string &filename );
00128 const nvsg::StateSet * interpretAppearance( vrml::Appearance *pAppearance );
00129 void interpretChildren( vrml::MFNode &children, nvsg::Group *pNVSGGroup );
00130 void interpretBackground( const vrml::Background *pBackground );
00131 void interpretBox( vrml::Box *pBox, std::vector<const nvsg::Drawable*> &drawables, bool textured = false );
00132 const nvsg::DirectedLight * interpretDirectionalLight( vrml::DirectionalLight *pDirectionalLight );
00133 void interpretElevationGrid( vrml::ElevationGrid * pElevationGrid,
00134 std::vector<const nvsg::Drawable*> &drawables );
00135 void interpretGeometry( vrml::Geometry *pGeometry,
00136 std::vector<const nvsg::Drawable*> &drawables, bool textured = false );
00137 const nvsg::Group * interpretGroup( vrml::Group *pGroup );
00138 const nvsg::TextureAttribute * interpretImageTexture( vrml::ImageTexture *pImageTexture );
00139 void interpretIndexedFaceSet( vrml::IndexedFaceSet *pIndexedFaceSet,
00140 std::vector<const nvsg::Drawable*> &drawables );
00141 void interpretIndexedFaceSetGeoSet( vrml::IndexedFaceSet *pIndexedFaceSet,
00142 const nvsg::GeoSet *pGeoSet,
00143 const nvsg::LinearInterpolatedAnimation<nvsg::VNVector> *pAnimation,
00144
00145 size_t vpf, size_t faceCount,
00146 nvsg::IndexSet startIndices, nvsg::IndexSet faceIndices );
00147 const nvsg::Node * interpretInline( vrml::Inline * pInline );
00148 const nvsg::LOD * interpretLOD( vrml::LOD *pVRMLLOD );
00149 const nvsg::Material * interpretMaterial( vrml::Material * pVRMLMaterial );
00150 void interpretOrientationInterpolator( vrml::OrientationInterpolator *pOI );
00151 nvsg::TextureAttribute * interpretPixelTexture( const vrml::PixelTexture *pPixelTexture );
00152 const nvsg::PointLight * interpretPointLight( vrml::PointLight *pVRMLPointLight );
00153 nvmath::Vec3f interpretSFColor( const vrml::SFColor &c );
00154 const nvsg::Object * interpretSFNode( const vrml::SFNode n );
00155 nvmath::Quatf interpretSFRotation( const vrml::SFRotation &r );
00156 const nvsg::GeoNode * interpretShape( vrml::Shape *pShape );
00157 const nvsg::SpotLight * interpretSpotLight( vrml::SpotLight *pVRMLSpotLight );
00158 const nvsg::Switch * interpretSwitch( vrml::Switch *pVRMLSwitch );
00159 const nvsg::TextureAttribute * interpretTexture( vrml::Texture *pTexture );
00160 void interpretTextureTransform( const vrml::TextureTransform *pTextureTransform,
00161 const nvsg::TextureAttribute *pTextureAttribute );
00162 const nvsg::Transform * interpretTransform( vrml::Transform *pVRMLTransform );
00163 bool interpretURL( const vrml::MFString &url, std::string &fileName );
00164 nvsg::Object * interpretViewpoint( const vrml::Viewpoint *pViewpoint );
00165 void interpretVRMLTree( void );
00166 bool isValidScaling( const vrml::PositionInterpolator *pPositionInterpolator ) const;
00167 bool isValidScaling( const vrml::SFVec3f &sfVec3f ) const;
00168 void makeCompatible( vrml::CoordinateInterpolator *points, size_t ps,
00169 vrml::NormalInterpolator *normals, size_t ns );
00170 vrml::MFFloat * makeCompatible( vrml::PositionInterpolator *center,
00171 vrml::OrientationInterpolator *rotation,
00172 vrml::PositionInterpolator *scale,
00173 vrml::PositionInterpolator *translation );
00174 bool onIncompatibleValues( int value0, int value1, const std::string &node,
00175 const std::string &field0, const std::string &field1 ) const;
00176 template<typename T>
00177 bool onInvalidValue( T value, const std::string &node, const std::string &field ) const;
00178 bool onEmptyToken( const std::string &tokenType, const std::string &token ) const;
00179 bool onFileNotFound( const vrml::SFString &url ) const;
00180 bool onFilesNotFound( bool found, const vrml::MFString &url ) const;
00181 void onUnexpectedEndOfFile( bool error ) const;
00182 void onUnexpectedToken( const std::string &expected, const std::string &token ) const;
00183 void onUnknownToken( const std::string &tokenType, const std::string &token ) const;
00184 bool onUndefinedToken( const std::string &tokenType, const std::string &token ) const;
00185 bool onUnsupportedToken( const std::string &tokenType, const std::string &token ) const;
00186 vrml::Anchor * readAnchor( const std::string &nodeName );
00187 vrml::Appearance * readAppearance( const std::string &nodeName );
00188 vrml::AudioClip * readAudioClip( const std::string &nodeName );
00189 vrml::Background * readBackground( const std::string &nodeName );
00190 vrml::Billboard * readBillboard( const std::string &nodeName );
00191 vrml::Box * readBox( const std::string &nodeName );
00192 vrml::Collision * readCollision( const std::string &nodeName );
00193 vrml::Color * readColor( const std::string &nodeName );
00194 vrml::ColorInterpolator * readColorInterpolator( const std::string &nodeName );
00195 vrml::Cone * readCone( const std::string &nodeName );
00196 vrml::Coordinate * readCoordinate( const std::string &nodeName );
00197 vrml::CoordinateInterpolator * readCoordinateInterpolator( const std::string &nodeName );
00198 vrml::Cylinder * readCylinder( const std::string &nodeName );
00199 vrml::CylinderSensor * readCylinderSensor( const std::string &nodeName );
00200 vrml::DirectionalLight * readDirectionalLight( const std::string &nodeName );
00201 vrml::ElevationGrid * readElevationGrid( const std::string &nodeName );
00202 void readEXTERNPROTO( void );
00203 vrml::Extrusion * readExtrusion( const std::string &nodeName );
00204 vrml::Fog * readFog( const std::string &nodeName );
00205 vrml::FontStyle * readFontStyle( const std::string &nodeName );
00206 vrml::Group * readGroup( const std::string &nodeName );
00207 vrml::ImageTexture * readImageTexture( const std::string &nodeName );
00208 void readIndex( std::vector<vrml::SFInt32> &mf );
00209 vrml::IndexedFaceSet * readIndexedFaceSet( const std::string &nodeName );
00210 vrml::IndexedLineSet * readIndexedLineSet( const std::string &nodeName );
00211 vrml::Inline * readInline( const std::string &nodeName );
00212 vrml::LOD * readLOD( const std::string &nodeName );
00213 vrml::Material * readMaterial( const std::string &nodeName );
00214 void readMFNode( vrml::Group * fatherNode );
00215 template<typename SFType>
00216 void readMFType( std::vector<SFType> &mf, void (WRLLoader::*readSFType)( SFType &sf ) );
00217 vrml::MovieTexture * readMovieTexture( const std::string &nodeName );
00218 vrml::NavigationInfo * readNavigationInfo( const std::string &nodeName );
00219 vrml::Normal * readNormal( const std::string &nodeName );
00220 vrml::NormalInterpolator * readNormalInterpolator( const std::string &nodeName );
00221 vrml::OrientationInterpolator * readOrientationInterpolator( const std::string &nodeName );
00222 vrml::PixelTexture * readPixelTexture( const std::string &nodeName );
00223 vrml::PlaneSensor * readPlaneSensor( const std::string &nodeName );
00224 vrml::PointLight * readPointLight( const std::string &nodeName );
00225 vrml::PointSet * readPointSet( const std::string &nodeName );
00226 vrml::PositionInterpolator * readPositionInterpolator( const std::string &nodeName );
00227 void readPROTO( void );
00228 vrml::ProximitySensor * readProximitySensor( const std::string &nodeName );
00229 void readROUTE( const vrml::SFNode currentNode );
00230 vrml::ScalarInterpolator * readScalarInterpolator( const std::string &nodeName );
00231 vrml::Script * readScript( const std::string &nodeName );
00232 void readSFBool( vrml::SFBool &b );
00233 void readSFColor( vrml::SFColor &c );
00234 void readSFFloat( vrml::SFFloat &f );
00235 void readSFImage( vrml::SFImage &i );
00236 void readSFInt8( vrml::SFInt8 &i );
00237 void readSFInt32( vrml::SFInt32 &i );
00238 void readSFNode( const vrml::SFNode fatherNode, vrml::SFNode &n );
00239 void readSFRotation( vrml::SFRotation &r );
00240 void readSFString( vrml::SFString &s );
00241 void readSFTime( vrml::SFTime &t );
00242 void readSFVec2f( vrml::SFVec2f &v );
00243 void readSFVec3f( vrml::SFVec3f &v );
00244 vrml::Shape * readShape( const std::string &nodeName );
00245 vrml::Sound * readSound( const std::string &nodeName );
00246 vrml::Sphere * readSphere( const std::string &nodeName );
00247 vrml::SphereSensor * readSphereSensor( const std::string &nodeName );
00248 vrml::SpotLight * readSpotLight( const std::string &nodeName );
00249 void readStatements( void );
00250 vrml::Switch * readSwitch( const std::string &nodeName );
00251 vrml::Text * readText( const std::string &nodeName );
00252 vrml::TextureCoordinate * readTextureCoordinate( const std::string &nodeName );
00253 vrml::TextureTransform * readTextureTransform( const std::string &nodeName );
00254 vrml::TimeSensor * readTimeSensor( const std::string &nodeName );
00255 vrml::TouchSensor * readTouchSensor( const std::string &nodeName );
00256 vrml::Transform * readTransform( const std::string &nodeName );
00257 vrml::Viewpoint * readViewpoint( const std::string &nodeName );
00258 vrml::VisibilitySensor * readVisibilitySensor( const std::string &nodeName );
00259 vrml::WorldInfo * readWorldInfo( const std::string &nodeName );
00260 void setAnimationData( const nvsg::LinearInterpolatedAnimation<nvsg::VNVector> *pAnimation,
00261 vrml::Coordinate *pCoordinate );
00262 void setAnimationData( const nvsg::LinearInterpolatedAnimation<nvsg::VNVector> *pAnimation,
00263 vrml::Coordinate *pCoordinate, vrml::MFInt32 coordIndex,
00264 size_t faceCount, size_t verticesPerFace, nvsg::IndexSet indices );
00265 void skipLine( void );
00266 bool testWRLVersion( void );
00267 void ungetToken( std::string token );
00268
00269 private :
00270 std::string m_currentString;
00271 char * m_currentLine;
00272 size_t m_currentLineLength;
00273 std::string m_currentToken;
00274 std::map<std::string,vrml::SFNode> m_defNodes;
00275 bool m_eof;
00276 std::ifstream m_ifs;
00277 std::vector<vrml::SFNode> m_inlines;
00278 vrml::SFFloat m_keyMin;
00279 vrml::SFFloat m_keyMax;
00280 vrml::SFFloat m_keyStep;
00281 size_t m_keyFrames;
00282 size_t m_line;
00283 char * m_nextToken;
00284 std::vector<vrml::SFNode> m_openNodes;
00285 std::set<std::string> m_PROTONames;
00286 const nvsg::Group * m_rootNode;
00287 const nvsg::Scene * m_scene;
00288 std::vector<std::string> m_searchPaths;
00289 vrml::Group * m_topLevelGroup;
00290 bool m_strict;
00291 std::string m_ungetToken;
00292 };
00293
00294 inline void WRLLoader::deleteThis( void )
00295 {
00296 delete this;
00297 }
00298
00299 inline nvmath::Vec3f WRLLoader::interpretSFColor( const vrml::SFColor &c )
00300 {
00301 return( * (nvmath::Vec3f *) &c );
00302 }