Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

NBFLoader.h

00001 // Copyright NVIDIA Corporation 2002-2004
00002 // TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
00003 // *AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
00004 // OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
00005 // AND FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS
00006 // BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES
00007 // WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
00008 // BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY LOSS)
00009 // ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF NVIDIA HAS
00010 // BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES 
00011 
00012 #pragma once
00013 
00014 #include "nvsg/PlugInterface.h"
00015 #include "nvsg/NBF.h" // nbf structs 
00016 
00017 #include <map>
00018 #include <vector>
00019 #include <string>
00020 
00021 // storage-class defines 
00022 #if defined(_WIN32)
00023 # ifdef NBFLOADER_EXPORTS
00024 #  define NBFLOADER_API __declspec(dllexport)
00025 # else
00026 #  define NBFLOADER_API __declspec(dllimport)
00027 # endif
00028 #else
00029 #  define NBFLOADER_API
00030 #endif
00031 
00032 #if defined(LINUX)
00033 void lib_init() __attribute__ ((constructor));   // will be called before dlopen() returns
00034 #endif
00035 
00036 // exports required for a scene loader plug-in
00037 #if defined(LINUX)
00038 extern "C"
00039 {
00040 #endif
00041 NBFLOADER_API bool getPlugInterface(const nvutil::UPIID& piid, nvutil::PlugIn *& pi);
00042 NBFLOADER_API bool queryPlugInterfaceType(const nvutil::UPITID& pitid, std::vector<nvutil::UPIID>& piids);
00043 #if defined(LINUX)
00044 }
00045 #endif
00046 
00047 // memory leak detection
00048 #ifdef _DEBUG
00049 # ifndef new
00050 #  define new new(__FILE__, __LINE__)
00051 #  define _DEFINED_DBGNEW // restrict the 'new' macro to this header file only
00052 # endif
00053 #endif
00054 
00055 // forward declaration
00056 namespace nvsg
00057 {
00058   class AnimatedQuads;
00059   class AnimatedTransform;
00060   class AnimatedTriangles;
00061   class Camera;
00062   class CgFx;
00063   class DirectedLight;
00064   class Drawable;
00065   class FaceAttribute;
00066   class GeoNode;
00067   class GeoSet;
00068   class Group;
00069   class LightSource;
00070   class LOD;
00071   class Material;
00072   class Node;
00073   class ParallelCamera;
00074   class PerspectiveCamera;
00075   class PointLight;
00076   class Quads;
00077   class QuadStrips;
00078   class SimpleCamera;
00079   class SkinnedTriangles;
00080   class SpotLight;
00081   class StateAttribute;
00082   class StateSet;
00083   class TextureAttribute;
00084   class TextureAttributeItem;
00085   class Transform;
00086   class Triangles;
00087   class TriStrips;
00088   class Switch;
00089   class Scene;
00090   class ViewState;
00091   template<typename T> class FramedAnimation;
00092   template<typename T> class InterpolatedAnimation;
00093 }
00094 
00096 class NBFLoader : public nvsg::SceneLoader
00097 {
00098 public:
00099   NBFLoader();
00100 
00102 
00103   void deleteThis( void );
00104 
00106 
00110   const nvsg::Scene * load( 
00111     const std::string& filename            
00112   , const std::vector<std::string> &searchPaths 
00113   , const nvsg::ViewState *& viewState     
00115   );
00116 
00117 protected:
00119   virtual  ~NBFLoader(void);
00120 
00122 
00133   virtual const nvsg::Object * loadCustomObject(
00134     uint_t objectCode 
00135   , uint_t offset     
00136   );
00137 
00139 
00141   ubyte_t * mapOffset(
00142     uint_t offset     
00143   , size_t numBytes   
00144   );
00145 
00146 private:
00147 
00148   bool mapViewOfFile(const std::string& file);
00149   void unmapViewOfFile();
00150 
00151   // assign an object to an offset 
00152   void mapObject(uint_t offset, const nvsg::Object * object);
00153 
00154   // animations
00155   const nvsg::Animation<nvmath::Trafo> * loadTrafoAnimation(uint_t offset);
00156   const nvsg::Animation<nvsg::VNVector> * loadVNVectorAnimation(uint_t offset);
00157   // ... framed
00158   const nvsg::FramedAnimation<nvmath::Trafo> * loadFramedTrafoAnimation(uint_t offset);
00159   const nvsg::FramedAnimation<nvsg::VNVector> * loadFramedVNVectorAnimation(uint_t offset);
00160   // ... interpolated
00161   const nvsg::InterpolatedAnimation<nvmath::Trafo> * loadInterpolatedTrafoAnimation(uint_t offset);
00162   const nvsg::InterpolatedAnimation<nvsg::VNVector> * loadInterpolatedVNVectorAnimation(uint_t offset);
00163   // cameras
00164   const nvsg::Camera * loadCamera(uint_t offset);
00165   const nvsg::ParallelCamera * loadParallelCamera(uint_t offset);
00166   const nvsg::PerspectiveCamera * loadPerspectiveCamera(uint_t offset);
00167   // drawables
00168   const nvsg::Drawable * loadDrawable(uint_t offset);
00169   const nvsg::Triangles * loadTriangles(uint_t offset);
00170   const nvsg::AnimatedTriangles * loadAnimatedTriangles(uint_t offset);
00171   const nvsg::SkinnedTriangles * loadSkinnedTriangles(uint_t offset);
00172   const nvsg::Quads * loadQuads(uint_t offset);
00173   const nvsg::AnimatedQuads * loadAnimatedQuads(uint_t offset);
00174   template<typename StripType> const StripType * loadStrips(uint_t offset);
00175   // scene
00176   const nvsg::Scene * loadScene(uint_t offset);
00177   // nodes
00178   const nvsg::Node * loadNode(uint_t offset);
00179   const nvsg::GeoNode * loadGeoNode(uint_t offset);
00180   const nvsg::Group * loadGroup(uint_t offset);
00181   const nvsg::Transform * loadTransform(uint_t offset);
00182   const nvsg::AnimatedTransform * loadAnimatedTransform(uint_t offset);
00183   const nvsg::LOD * loadLOD(uint_t offset);
00184   const nvsg::Switch * loadSwitch(uint_t offset);
00185   const nvsg::LightSource * loadLightSource(uint_t offset);
00186   const nvsg::DirectedLight * loadDirectedLight(uint_t offset);
00187   const nvsg::PointLight * loadPointLight(uint_t offset);
00188   const nvsg::SpotLight * loadSpotLight(uint_t offset);
00189   // state attribs
00190   const nvsg::StateAttribute * loadStateAttribute(uint_t offset);
00191   const nvsg::CgFx * loadCgFx(uint_t offset);
00192   const nvsg::FaceAttribute * loadFaceAttribute(uint_t offset);
00193   const nvsg::Material * loadMaterial(uint_t offset);
00194   const nvsg::TextureAttribute * loadTextureAttribute(uint_t offset);
00195   const nvsg::TextureAttributeItem * loadTextureAttributeItem(uint_t offset);
00196   // state set
00197   const nvsg::StateSet * loadStateSet(uint_t offset);
00198   // view sate
00199   const nvsg::ViewState * loadViewState(uint_t offset);
00200   // shared object handling
00201   template <typename ObjectType, typename NBFObjectType>
00202   bool loadSharedObject(nvutil::RCObject_AutoPtr<ObjectType>& obj, NBFObjectType *& objPtr);
00203 
00204   // read in non-concrete objects
00205   void readObject(nvsg::Object * dst, const NBFObject * src);
00206   void readGroup(nvsg::Group * dst, const NBFGroup * src);
00207   void readGeoSet(nvsg::GeoSet * dst, const NBFGeoSet * src);
00208   void readLightSource(nvsg::LightSource * dst, NBFLightSource * src);
00209   void readNode(nvsg::Node * dst, NBFNode * src);
00210   void readSimpleCamera(nvsg::SimpleCamera * dst, const NBFSimpleCamera * src);
00211 
00212 private:
00213 
00214   enum
00215   {
00216     VIEWSIZE = 0x04000000
00217   };
00218 
00219   struct ViewHeader
00220   {
00221     ubyte_t * basePtr;   // points to the beginning of the view
00222     size_t    startOffs; // the view's start offset inside the mapped file 
00223     size_t    endOffs;   // the view's 'past tht end' offset inside the mapped file
00224   };
00225   std::vector<ViewHeader>    m_mappedViews;  // collection of currently mapped views
00226 #if defined(_WIN32)
00227   HANDLE                m_file;         // file handle returned by CreateFile
00228   HANDLE                m_fileMapping;  // handle of file mapping object returned by CreateFileMapping
00229 #elif defined(LINUX)
00230   int                   m_file;         // file descriptor returned by open()
00231 #endif
00232   size_t                m_mappingSize;  // actual size of the file mapping
00233   size_t                m_allocationGranularity; // systems allocation granularity
00234   std::vector<std::string>        m_searchPaths;  // a private copy of the search pathes given to us via the load API
00235   std::map<uint_t, const nvsg::Object *> m_offsetObjectMap; // mapping offsets to NVSG objects
00236   std::map<nvsg::DataID, const nvsg::Object *> m_sharedObjectsMap; // lookup shared objects given the corresponding objectID
00237   // private copy of the nbf version used to save the file
00238   ubyte_t m_nbfMajor;   // major version
00239   ubyte_t m_nbfMinor;   // minor version
00240   ubyte_t m_nbfBugfix;  // bugfix level
00241 };
00242 
00243 inline void NBFLoader::deleteThis()
00244 {
00245   delete this;
00246 }
00247 
00248 #ifdef _DEFINED_DBGNEW
00249 # undef new
00250 #endif

Generated on Tue Mar 1 13:19:20 2005 for NVSGSDK by NVIDIA