00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #pragma once
00013
00015 #include <fstream>
00016 #include "CgFx/ICgFXEffect.h"
00017 #include "nvsg/nvsgapi.h"
00018 #include "nvsg/nvsg.h"
00019 #include "nvsg/PlugInterface.h"
00020 #include "nvsg/SkinnedTriangles.h"
00021 #include "nvsg/TextureAttribute.h"
00022
00023
00024 namespace nvsg
00025 {
00026 class AnimatedQuads;
00027 class AnimatedTransform;
00028 class AnimatedTriangles;
00029 class CgFx;
00030 class DirectedLight;
00031 class FaceAttribute;
00032 class GeoNode;
00033 class GeoSet;
00034 class LOD;
00035 class Material;
00036 class ParallelCamera;
00037 class PerspectiveCamera;
00038 class PointLight;
00039 class Quads;
00040 class QuadStrips;
00041 class SimpleCamera;
00042 class SpotLight;
00043 class StateAttribute;
00044 class Switch;
00045 class Transform;
00046 class TriStrips;
00047 class VNVector;
00048 template<typename T> class FramedAnimation;
00049 template<typename T> class LinearInterpolatedAnimation;
00050 }
00051
00052
00053
00054 #if ! defined( DOXYGEN_IGNORE )
00055 #ifdef NVSGLOADER_EXPORTS
00056 #define NVSGLOADER_API __declspec(dllexport)
00057 #else
00058 #define NVSGLOADER_API __declspec(dllimport)
00059 #endif
00060 #endif // DOXYGEN_IGNORE
00061
00063
00067 NVSGLOADER_API bool getPlugInterface(const nvutil::UPIID& piid, nvutil::PlugIn *& pi);
00068
00070
00075 NVSGLOADER_API bool queryPlugInterfaceType(const nvutil::UPITID& pitid, std::vector<nvutil::UPIID>& piids);
00076
00078
00080 class NVSGLoader : public nvsg::SceneLoader
00081 {
00082 public :
00083
00084 NVSGLoader();
00085 ~NVSGLoader();
00086
00087 public :
00089
00090 void deleteThis( void );
00091
00093
00098 const nvsg::Scene * load( const std::string& filename
00099 , const std::vector<std::string> &searchPaths
00100 , const nvsg::ViewState *& viewState
00102 );
00103
00104 private :
00105 void cleanup( void );
00106 bool getNextLine( void );
00107 char * getNextToken( void );
00108 const nvsg::Scene * import( const std::string &filename, const nvsg::ViewState *& viewState );
00109 bool onIncompatibleValues( int value0, int value1, const std::string &node, const std::string &field0, const std::string &field1 ) const;
00110 template<typename T> bool onInvalidValue( T value, const std::string &node, const std::string &field ) const;
00111 bool onEmptyToken( const std::string &tokenType, const std::string &token ) const;
00112 bool onFileNotFound( const std::string &file ) const;
00113 bool onFilesNotFound( bool found, const std::vector<std::string> &files ) const;
00114 void onUnexpectedEndOfFile( bool error ) const;
00115 void onUnexpectedToken( const std::string &expected, const std::string &token ) const;
00116 void onUnknownToken( const std::string &context, const std::string &token ) const;
00117 bool onUndefinedToken( const std::string &context, const std::string &token ) const;
00118 bool onUnsupportedToken( const std::string &context, const std::string &token ) const;
00119 const nvsg::AnimatedQuads * readAnimatedQuads( const char *name );
00120 const nvsg::AnimatedTransform * readAnimatedTransform( const char *name );
00121 const nvsg::AnimatedTriangles * readAnimatedTriangles( const char *name );
00122 void readAttributes( std::vector<const nvsg::StateAttribute*> &attribs );
00123 bool readBool( void );
00124 const nvsg::CgFx * readCgFx( const char *name );
00125 const nvsg::Node * readChild( const char *token = NULL );
00126 void readChildren( nvsg::Group *g );
00127 size_t readCount( const char *token = NULL );
00128 const nvsg::DirectedLight * readDirectedLight( const char *name );
00129 DWORD readDWORD( void );
00130 CgFXPARAMETERTYPE readCgFXParameterType( void );
00131 const nvsg::FaceAttribute * readFaceAttribute( const char *name );
00132 float readFloat( const char *token = NULL );
00133 void readFloats( std::vector<float> &v );
00134 const nvsg::FramedAnimation<nvmath::Trafo> * readFramedTrafoAnimation( const char *name );
00135 const nvsg::FramedAnimation<nvsg::VNVector> * readFramedVNVectorAnimation( const char *name );
00136 const nvsg::GeoNode * readGeoNode( const char *name );
00137 bool readGeoSetToken( nvsg::GeoSet *gs, const char *token );
00138 const nvsg::Group * readGroup( const char *name );
00139 bool readGroupToken( nvsg::Group *g, const char *token );
00140 void readIndexSet( nvsg::IndexSet &v, const char *token );
00141 int readInt( void );
00142 const nvsg::LinearInterpolatedAnimation<nvmath::Trafo> * readLinearInterpolatedTrafoAnimation( const char *name );
00143 const nvsg::LinearInterpolatedAnimation<nvsg::VNVector> * readLinearInterpolatedVNVectorAnimation( const char *name );
00144 bool readLightSourceToken( nvsg::LightSource *ls, const char *token );
00145 const nvsg::LOD * readLOD( const char *name );
00146 nvmath::Mat33f readMat33f( void );
00147 nvmath::Mat44f readMat44f( void );
00148 const nvsg::Material * readMaterial( const char *name );
00149 std::string readName( const char *t = NULL );
00150 bool readNodeToken( nvsg::Node *n, const char *token );
00151 const nvsg::ParallelCamera * readParallelCamera( const char *name );
00152 const nvsg::PerspectiveCamera * readPerspectiveCamera( const char *name );
00153 const nvsg::PointLight * readPointLight( const char *name );
00154 const nvsg::Quads * readQuads( const char *name );
00155 const nvsg::QuadStrips * readQuadStrips( const char *name );
00156 bool readQuadsToken( nvsg::Quads *q, const char *token );
00157 nvmath::Quatf readQuatf( const char *token = NULL );
00158 const nvsg::Scene * readScene( void );
00159 bool readSimpleCameraToken( nvsg::SimpleCamera * pc, const char *token );
00160 nvsg::Skin readSkin( char *token );
00161 const nvsg::SkinnedTriangles * readSkinnedTriangles( const char *name );
00162 void readSkins( std::vector<nvsg::Skin> &skins );
00163 const nvsg::SpotLight * readSpotLight( const char *name );
00164 const nvsg::StateSet * readStateSet( const char *name );
00165 void readStrips( std::vector<nvsg::IndexSet> &strips );
00166 const nvsg::Switch * readSwitch( const char *name );
00167 void readTexGen( nvsg::TextureAttributeItem * texItem );
00168 const nvsg::TextureAttribute * readTextureAttribute( const char *name );
00169 const nvsg::TextureAttributeItem * readTextureAttributeItem( const char *name );
00170 void readTextureAttributeItems(std::vector<const nvsg::TextureAttributeItem*>& texItems);
00171 nvsg::TextureEnvMode readTextureEnvMode( void );
00172 nvsg::TextureMagFilterMode readTextureMagFilterMode( void );
00173 nvsg::TextureMinFilterMode readTextureMinFilterMode( void );
00174 nvsg::TextureType readTextureType( void );
00175 nvsg::TextureWrapMode readTextureWrapMode( void );
00176 const nvsg::Transform * readTransform( const char *name );
00177 bool readTransformToken( nvsg::Transform *t, const char *token );
00178 const nvsg::Triangles * readTriangles( const char *name );
00179 bool readTrianglesToken( nvsg::Triangles *t, const char *token );
00180 const nvsg::TriStrips * readTriStrips( const char *name );
00181 nvmath::Vec2f readVec2f( const char *token = NULL );
00182 void readVec2fs( std::vector<nvmath::Vec2f> &vecs );
00183 nvmath::Vec3f readVec3f( const char *token = NULL );
00184 void readVec3fs( std::vector<nvmath::Vec3f> &vecs );
00185 nvmath::Vec4f readVec4f( const char *token = NULL);
00186 void readVec4fs( std::vector<nvmath::Vec4f> &vecs );
00187 const nvsg::ViewState * readViewState( void );
00188 bool testNVSGVersion( void );
00189
00190 template<size_t n> nvsg::Face<n> readFace( const char *token );
00191 template<size_t n> void readFaces( std::vector<nvsg::Face<n> > &faces );
00192 template<typename ObjectType> void storeNamedObject(const std::string& name, std::map<std::string, const ObjectType*>& container, const ObjectType * obj);
00193
00194 private :
00195 std::map<std::string,const nvsg::AnimatedQuads*> m_animatedQuads;
00196 std::map<std::string,const nvsg::AnimatedTransform*> m_animatedTransforms;
00197 std::map<std::string,const nvsg::AnimatedTriangles*> m_animatedTriangles;
00198 std::map<std::string,const nvsg::CgFx*> m_cgfxs;
00199 nvmath::Vec3f m_cameraDirection;
00200 nvmath::Vec3f m_cameraUpVector;
00201 char * m_currentLine;
00202 size_t m_currentLineLength;
00203 std::string m_currentString;
00204 std::map<std::string,const nvsg::DirectedLight*> m_directedLights;
00205 bool m_eof;
00206 std::map<std::string,const nvsg::FaceAttribute*> m_faceAttributes;
00207 std::map<std::string,const nvsg::FramedAnimation<nvmath::Trafo>*> m_framedTrafoAnimations;
00208 std::map<std::string,const nvsg::FramedAnimation<nvsg::VNVector>*> m_framedVNVectorAnimations;
00209 std::map<std::string,const nvsg::GeoNode*> m_geoNodes;
00210 std::map<std::string,const nvsg::Group*> m_groups;
00211 std::ifstream m_ifs;
00212 size_t m_line;
00213 std::map<std::string,const nvsg::LinearInterpolatedAnimation<nvmath::Trafo>*> m_linearInterpolatedTrafoAnimations;
00214 std::map<std::string,const nvsg::LinearInterpolatedAnimation<nvsg::VNVector>*> m_linearInterpolatedVNVectorAnimations;
00215 std::map<std::string,const nvsg::LOD*> m_LODs;
00216 std::map<std::string,const nvsg::Material*> m_materials;
00217 char * m_nextToken;
00218 std::map<std::string,const nvsg::ParallelCamera*> m_parallelCameras;
00219 std::map<std::string,const nvsg::PerspectiveCamera*> m_perspectiveCameras;
00220 std::map<std::string,const nvsg::PointLight*> m_pointLights;
00221 std::map<std::string,const nvsg::Quads*> m_quads;
00222 std::map<std::string,const nvsg::QuadStrips*> m_quadStrips;
00223 const nvsg::Scene * m_scene;
00224 std::map<std::string,const nvsg::SkinnedTriangles*> m_skinnedTriangles;
00225 std::map<std::string,const nvsg::SpotLight*> m_spotLights;
00226 std::map<std::string,const nvsg::StateSet*> m_stateSets;
00227 std::vector<std::string> m_searchPaths;
00228 std::map<std::string,const nvsg::Switch*> m_switches;
00229 std::map<std::string,const nvsg::TextureAttribute*> m_textureAttributes;
00230 std::map<std::string,const nvsg::TextureAttributeItem*> m_textureAttributeItems;
00231 std::map<std::string,const nvsg::Transform*> m_transforms;
00232 std::map<std::string,const nvsg::Triangles*> m_triangles;
00233 std::map<std::string,const nvsg::TriStrips*> m_triStrips;
00234 };
00235
00236 inline void NVSGLoader::deleteThis( void )
00237 {
00238 delete this;
00239 }
00240
00241 template<size_t n>
00242 inline nvsg::Face<n> NVSGLoader::readFace( const char *token )
00243 {
00244 Face<n> f;
00245 f[0] = (UINT) readCount( token );
00246 for ( size_t i=1 ; i<n ; i++ )
00247 {
00248 f[i] = (UINT) readCount();
00249 }
00250 return( f );
00251 }
00252
00253 template<size_t n>
00254 inline void NVSGLoader::readFaces( std::vector<nvsg::Face<n> > &faces )
00255 {
00256 faces.clear();
00257 char * token = getNextToken();
00258 onUnexpectedToken( "[", token );
00259 token = getNextToken();
00260 while ( strcmp( token, "]" ) )
00261 {
00262 faces.push_back( readFace<n>( token ) );
00263 token = getNextToken();
00264 }
00265 }
00266
00267 template <typename ObjectType>
00268 inline void NVSGLoader::storeNamedObject(const std::string& name, std::map<std::string, const ObjectType*>& container, const ObjectType * obj)
00269 {
00270 if ( obj )
00271 {
00272 if ( container.find( name ) != container.end() )
00273 {
00274 container[name]->removeRef();
00275 }
00276 obj->addRef();
00277 container[name] = obj;
00278 }
00279 }