Options 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSGDB_OPTIONS
  14. #define OSGDB_OPTIONS 1
  15. #include <osgDB/Callbacks>
  16. #include <osgDB/ObjectCache>
  17. #include <osg/ObserverNodePath>
  18. #include <deque>
  19. #include <list>
  20. #include <iosfwd>
  21. namespace osgDB {
  22. /** Options base class used for passing options into plugins to control their operation.*/
  23. class OSGDB_EXPORT Options : public osg::Object
  24. {
  25. public:
  26. /// bit mask for setting up which object types get cached by readObject/Image/HeightField/Node(filename) calls
  27. enum CacheHintOptions
  28. { /// do not cache objects of any type
  29. CACHE_NONE = 0,
  30. /// cache nodes loaded via readNode(filename)
  31. CACHE_NODES = 1<<0,
  32. /// cache images loaded via readImage(filename)
  33. CACHE_IMAGES = 1<<1,
  34. /// cache heightfield loaded via readHeightField(filename)
  35. CACHE_HEIGHTFIELDS = 1<<2,
  36. /// cache heightfield loaded via readHeightField(filename)
  37. CACHE_ARCHIVES = 1<<3,
  38. /// cache objects loaded via readObject(filename)
  39. CACHE_OBJECTS = 1<<4,
  40. /// cache shaders loaded via readShader(filename)
  41. CACHE_SHADERS = 1<<5,
  42. /// cache on all read*(filename) calls
  43. CACHE_ALL = CACHE_NODES |
  44. CACHE_IMAGES |
  45. CACHE_HEIGHTFIELDS |
  46. CACHE_ARCHIVES |
  47. CACHE_OBJECTS |
  48. CACHE_SHADERS
  49. };
  50. /// Bit mask for which geometry attributes should be imported with double precision where source data is held in double precision
  51. /// This is useful for data that will be pre-processed before rendering.
  52. /// In general the geometry should be converted to floating point before rendering to ensure good performance.
  53. enum PrecisionHint
  54. {
  55. FLOAT_PRECISION_ALL = 0,
  56. DOUBLE_PRECISION_VERTEX = 1<<0,
  57. DOUBLE_PRECISION_NORMAL = 1<<1,
  58. DOUBLE_PRECISION_COLOR = 1<<2,
  59. DOUBLE_PRECISION_SECONDARY_COLOR = 1<<3,
  60. DOUBLE_PRECISION_FOG_COORD = 1<<4,
  61. DOUBLE_PRECISION_TEX_COORD = 1<<5,
  62. DOUBLE_PRECISION_VERTEX_ATTRIB = 1<<6,
  63. DOUBLE_PRECISION_ALL = DOUBLE_PRECISION_VERTEX |
  64. DOUBLE_PRECISION_NORMAL |
  65. DOUBLE_PRECISION_COLOR |
  66. DOUBLE_PRECISION_SECONDARY_COLOR |
  67. DOUBLE_PRECISION_FOG_COORD |
  68. DOUBLE_PRECISION_TEX_COORD |
  69. DOUBLE_PRECISION_VERTEX_ATTRIB
  70. };
  71. /// range of options of whether to build kdtrees automatically on loading
  72. enum BuildKdTreesHint
  73. {
  74. NO_PREFERENCE,
  75. DO_NOT_BUILD_KDTREES,
  76. BUILD_KDTREES
  77. };
  78. Options();
  79. Options(const std::string& str);
  80. Options(const Options& options,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  81. META_Object(osgDB,Options);
  82. Options* cloneOptions(const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY) const { return static_cast<Options*>(clone(copyop)); }
  83. /** Set the general Options string.*/
  84. void setOptionString(const std::string& str) { _str = str; parsePluginStringData(str); }
  85. /** Get the general Options string.*/
  86. const std::string& getOptionString() const { return _str; }
  87. /** Set the database path to use a hint of where to look when loading models.*/
  88. void setDatabasePath(const std::string& str) { _databasePaths.clear(); _databasePaths.push_back(str); }
  89. /** Get the database path which is used a hint of where to look when loading models.*/
  90. FilePathList& getDatabasePathList() { return _databasePaths; }
  91. /** Get the const database path which is used a hint of where to look when loading models.*/
  92. const FilePathList& getDatabasePathList() const { return _databasePaths; }
  93. /** Set whether the Registry::ObjectCache should be used by default.*/
  94. void setObjectCacheHint(CacheHintOptions useObjectCache) { _objectCacheHint = useObjectCache; }
  95. /** Get whether the Registry::ObjectCache should be used by default.*/
  96. CacheHintOptions getObjectCacheHint() const { return _objectCacheHint; }
  97. /** Set the ObjectCache that is used to cache previously loaded data.*/
  98. void setObjectCache(ObjectCache* objectCache) { _objectCache = objectCache; }
  99. /** Get the ObjectCache that is used to cache previously loaded data.*/
  100. ObjectCache* getObjectCache() const { return _objectCache.get(); }
  101. /** Set which geometry attributes plugins should import at double precision. */
  102. void setPrecisionHint(PrecisionHint hint) { _precisionHint = hint; }
  103. /** Get which geometry attributes plugins should import at double precision. */
  104. PrecisionHint getPrecisionHint() const { return _precisionHint; }
  105. /** Set whether the KdTrees should be built for geometry in the loader model. */
  106. void setBuildKdTreesHint(BuildKdTreesHint hint) { _buildKdTreesHint = hint; }
  107. /** Get whether the KdTrees should be built for geometry in the loader model. */
  108. BuildKdTreesHint getBuildKdTreesHint() const { return _buildKdTreesHint; }
  109. /** Set the password map to be used by plugins when access files from secure locations.*/
  110. void setAuthenticationMap(AuthenticationMap* authenticationMap) { _authenticationMap = authenticationMap; }
  111. /** Get the password map to be used by plugins when access files from secure locations.*/
  112. const AuthenticationMap* getAuthenticationMap() const { return _authenticationMap.get(); }
  113. /** Sets a plugindata value PluginData with a string */
  114. void setPluginData(const std::string& s, void* v) const { _pluginData[s] = v; }
  115. /** Get a value from the PluginData */
  116. void* getPluginData(const std::string& s) { return _pluginData[s]; }
  117. /** Get a value from the PluginData */
  118. const void* getPluginData(const std::string& s) const
  119. {
  120. PluginDataMap::const_iterator itr = _pluginData.find(s);
  121. return (itr == _pluginData.end()) ? 0 : itr->second;
  122. }
  123. /** Remove a value from the PluginData */
  124. void removePluginData(const std::string& s) const { _pluginData.erase(s); }
  125. /** Get number of PluginData values */
  126. unsigned int getNumPluginData() const { return static_cast<unsigned int>(_pluginData.size()); }
  127. /** Sets a plugindata value PluginData with a string */
  128. void setPluginStringData(const std::string& s, const std::string& v) const { _pluginStringData[s] = v; }
  129. /** Get a string from the PluginStrData */
  130. std::string& getPluginStringData(const std::string& s) { return _pluginStringData[s]; }
  131. /** Get a value from the PluginData */
  132. const std::string getPluginStringData(const std::string& s) const
  133. {
  134. PluginStringDataMap::const_iterator itr = _pluginStringData.find(s);
  135. return (itr == _pluginStringData.end()) ? std::string("") : itr->second;
  136. }
  137. /** Remove a value from the PluginData */
  138. void removePluginStringData(const std::string& s) const { _pluginStringData.erase(s); }
  139. /** Get number of PluginStrData values */
  140. unsigned int getNumPluginStringData() const { return static_cast<unsigned int>(_pluginStringData.size()); }
  141. /** Parse string into plugin string data. This will be automatically done in Options(const std::string&) */
  142. void parsePluginStringData(const std::string& str, char separator1=' ', char separator2='=');
  143. /** Set the find callback to use in place of the default findFile calls.*/
  144. void setFindFileCallback( FindFileCallback* cb) { _findFileCallback = cb; }
  145. /** Get the const findFile callback.*/
  146. FindFileCallback* getFindFileCallback() const { return _findFileCallback.get(); }
  147. /** Set the read callback to use in place of the default readFile calls.*/
  148. void setReadFileCallback( ReadFileCallback* cb) { _readFileCallback = cb; }
  149. /** Get the const readFile callback.*/
  150. ReadFileCallback* getReadFileCallback() const { return _readFileCallback.get(); }
  151. /** Set the callback to use in place of the default writeFile calls.*/
  152. void setWriteFileCallback( WriteFileCallback* cb) { _writeFileCallback = cb; }
  153. /** Get the const writeFile callback.*/
  154. WriteFileCallback* getWriteFileCallback() const { return _writeFileCallback.get(); }
  155. /** Set the callback to use inform the DatabasePager whether a file is located on local or remote file system.*/
  156. void setFileLocationCallback( FileLocationCallback* cb) { _fileLocationCallback = cb; }
  157. /** Get the callback to use inform the DatabasePager whether a file is located on local or remote file system.*/
  158. FileLocationCallback* getFileLocationCallback() const { return _fileLocationCallback.get(); }
  159. /** Set the FileCache that is used to manage local storage of files downloaded from the internet.*/
  160. void setFileCache(FileCache* fileCache) { _fileCache = fileCache; }
  161. /** Get the FileCache that is used to manage local storage of files downloaded from the internet.*/
  162. FileCache* getFileCache() const { return _fileCache.get(); }
  163. /** Set the terrain observer_ptr, use to decorate any osgTerrain subgraphs.*/
  164. void setTerrain(osg::observer_ptr<osg::Node>& terrain) { _terrain = terrain; }
  165. /** Get the terrain observer_ptr, use to decorate any osgTerrain subgraphs.*/
  166. const osg::observer_ptr<osg::Node>& getTerrain() const { return _terrain; }
  167. /** Set the parentGroup observer_ptr, where the loaded model is intended to be added */
  168. void setParentGroup(osg::observer_ptr<osg::Group>& parentGroup) { _parentGroup= parentGroup; }
  169. /** Get the parentGroup observer_ptr, where the loaded model is intended to be added */
  170. const osg::observer_ptr<osg::Group>& getParentGroup() const { return _parentGroup; }
  171. bool operator < (const Options &rhs) const;
  172. bool operator == (const Options &rhs) const;
  173. protected:
  174. virtual ~Options();
  175. std::string _str;
  176. FilePathList _databasePaths;
  177. CacheHintOptions _objectCacheHint;
  178. osg::ref_ptr<ObjectCache> _objectCache;
  179. PrecisionHint _precisionHint;
  180. BuildKdTreesHint _buildKdTreesHint;
  181. osg::ref_ptr<AuthenticationMap> _authenticationMap;
  182. typedef std::map<std::string,void*> PluginDataMap;
  183. mutable PluginDataMap _pluginData;
  184. typedef std::map<std::string,std::string> PluginStringDataMap;
  185. mutable PluginStringDataMap _pluginStringData;
  186. osg::ref_ptr<FindFileCallback> _findFileCallback;
  187. osg::ref_ptr<ReadFileCallback> _readFileCallback;
  188. osg::ref_ptr<WriteFileCallback> _writeFileCallback;
  189. osg::ref_ptr<FileLocationCallback> _fileLocationCallback;
  190. osg::ref_ptr<FileCache> _fileCache;
  191. osg::observer_ptr<osg::Node> _terrain;
  192. osg::observer_ptr<osg::Group> _parentGroup; // Set by the DatabasePager to the node where the requested file will be inserted. NOTE: observer since prent can be dettached whilst DB thread is loading the object
  193. };
  194. }
  195. #endif // OSGDB_OPTIONS