ReadFile 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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_READFILE
  14. #define OSGDB_READFILE 1
  15. #include <string>
  16. #include <osg/Node>
  17. #include <osg/Image>
  18. #include <osg/ArgumentParser>
  19. #include <osgDB/Export>
  20. #include <osgDB/Registry>
  21. namespace osgDB {
  22. /** Read an osg::Object from file.
  23. * Return an assigned osg::ref_ptr on success,
  24. * return an osg::ref_ptr with a NULL pointer assigned to it on failure.
  25. * Use the Options object to control cache operations and file search paths in osgDB::Registry.
  26. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  27. * for the filename extension, and this plugin then handles the request
  28. * to read the specified file.*/
  29. extern OSGDB_EXPORT osg::ref_ptr<osg::Object> readRefObjectFile(const std::string& filename,const Options* options);
  30. /** Read an osg::Object from file.
  31. * Return an assigned osg::ref_ptr on success,
  32. * return an osg::ref_ptr with a NULL pointer assigned to it on failure.
  33. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  34. * for the filename extension, and this plugin then handles the request
  35. * to read the specified file.*/
  36. inline osg::ref_ptr<osg::Object> readRefObjectFile(const std::string& filename)
  37. {
  38. return readRefObjectFile(filename, Registry::instance()->getOptions());
  39. }
  40. template<class T>
  41. inline osg::ref_ptr<T> readRefFile(const std::string& filename, const Options* options)
  42. {
  43. osg::ref_ptr<osg::Object> object = readRefObjectFile(filename, options);
  44. osg::ref_ptr<T> t = dynamic_cast<T*>(object.get());
  45. return t;
  46. }
  47. template<class T>
  48. inline osg::ref_ptr<T> readRefFile(const std::string& filename)
  49. {
  50. return readRefFile<T>(filename, Registry::instance()->getOptions());
  51. }
  52. /** Read an osg::Image from file.
  53. * Return an assigned osg::ref_ptr on success,
  54. * return an osg::ref_ptr with a NULL pointer assigned to it on failure.
  55. * Use the Options object to control cache operations and file search paths in osgDB::Registry.
  56. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  57. * for the filename extension, and this plugin then handles the request
  58. * to read the specified file.*/
  59. extern OSGDB_EXPORT osg::ref_ptr<osg::Image> readRefImageFile(const std::string& filename,const Options* options);
  60. /** Read an osg::Image from file.
  61. * Return an assigned osg::ref_ptr on success,
  62. * return an osg::ref_ptr with a NULL pointer assigned to it on failure.
  63. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  64. * for the filename extension, and this plugin then handles the request
  65. * to read the specified file.*/
  66. inline osg::ref_ptr<osg::Image> readRefImageFile(const std::string& filename)
  67. {
  68. return readRefImageFile(filename,Registry::instance()->getOptions());
  69. }
  70. /** Read an osg::HeightField from file.
  71. * Return an assigned osg::ref_ptr on success,
  72. * return an osg::ref_ptr with a NULL pointer assigned to it on failure.
  73. * Use the Options object to control cache operations and file search paths in osgDB::Registry.
  74. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  75. * for the filename extension, and this plugin then handles the request
  76. * to read the specified file.*/
  77. extern OSGDB_EXPORT osg::ref_ptr<osg::HeightField> readRefHeightFieldFile(const std::string& filename,const Options* options);
  78. /** Read an osg::HeightField from file.
  79. * Return an assigned osg::ref_ptr on success,
  80. * return an osg::ref_ptr with a NULL pointer assigned to it on failure.
  81. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  82. * for the filename extension, and this plugin then handles the request
  83. * to read the specified file.*/
  84. inline osg::ref_ptr<osg::HeightField> readRefHeightFieldFile(const std::string& filename)
  85. {
  86. return readRefHeightFieldFile(filename,Registry::instance()->getOptions());
  87. }
  88. /** Read an osg::Node from file.
  89. * Return an assigned osg::ref_ptr on success,
  90. * return an osg::ref_ptr with a NULL pointer assigned to it on failure.
  91. * Use the Options object to control cache operations and file search paths in osgDB::Registry.
  92. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  93. * for the filename extension, and this plugin then handles the request
  94. * to read the specified file.*/
  95. extern OSGDB_EXPORT osg::ref_ptr<osg::Node> readRefNodeFile(const std::string& filename,const Options* options);
  96. /** Read an osg::Node from file.
  97. * Return an assigned osg::ref_ptr on success,
  98. * return an osg::ref_ptr with a NULL pointer assigned to it on failure.
  99. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  100. * for the filename extension, and this plugin then handles the request
  101. * to read the specified file.*/
  102. inline osg::ref_ptr<osg::Node> readRefNodeFile(const std::string& filename)
  103. {
  104. return readRefNodeFile(filename,Registry::instance()->getOptions());
  105. }
  106. /** Read an osg::Node subgraph from files, creating a osg::Group to contain the nodes if more
  107. * than one subgraph has been loaded.
  108. * Use the Options object to control cache operations and file search paths in osgDB::Registry.
  109. * Does NOT ignore strings beginning with a dash '-' character. */
  110. extern OSGDB_EXPORT osg::ref_ptr<osg::Node> readRefNodeFiles(std::vector<std::string>& fileList,const Options* options);
  111. /** Read an osg::Node subgraph from files, creating a osg::Group to contain the nodes if more
  112. * than one subgraph has been loaded.*/
  113. inline osg::ref_ptr<osg::Node> readRefNodeFiles(std::vector<std::string>& fileList)
  114. {
  115. return readRefNodeFiles(fileList, Registry::instance()->getOptions());
  116. }
  117. /** Read an osg::Node subgraph from files, creating a osg::Group to contain the nodes if more
  118. * than one subgraph has been loaded.
  119. * Use the Options object to control cache operations and file search paths in osgDB::Registry.*/
  120. extern OSGDB_EXPORT osg::ref_ptr<osg::Node> readRefNodeFiles(osg::ArgumentParser& parser,const Options* options);
  121. /** Read an osg::Node subgraph from files, creating a osg::Group to contain the nodes if more
  122. * than one subgraph has been loaded.*/
  123. inline osg::ref_ptr<osg::Node> readRefNodeFiles(osg::ArgumentParser& parser)
  124. {
  125. return readRefNodeFiles(parser,Registry::instance()->getOptions());
  126. }
  127. /** Read an osg::Shader from file.
  128. * Return an assigned osg::ref_ptr on success,
  129. * return an osg::ref_ptr with a NULL pointer assigned to it on failure.
  130. * Use the Options object to control cache operations and file search paths in osgDB::Registry.
  131. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  132. * for the filename extension, and this plugin then handles the request
  133. * to read the specified file.*/
  134. extern OSGDB_EXPORT osg::ref_ptr<osg::Shader> readRefShaderFile(const std::string& filename,const Options* options);
  135. /** Read an osg::Shader from file.
  136. * Return an assigned osg::ref_ptr on success,
  137. * return an osg::ref_ptr with a NULL pointer assigned to it on failure.
  138. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  139. * for the filename extension, and this plugin then handles the request
  140. * to read the specified file.*/
  141. inline osg::ref_ptr<osg::Shader> readRefShaderFile(const std::string& filename)
  142. {
  143. return readRefShaderFile(filename, Registry::instance()->getOptions());
  144. }
  145. /** Read an osg::Shader from file and set to specified shader type.
  146. * Return valid osg::Shader on success,
  147. * return NULL on failure.
  148. * Use the Options object to control cache operations and file search paths in osgDB::Registry.
  149. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  150. * for the filename extension, and this plugin then handles the request
  151. * to read the specified file.*/
  152. inline osg::ref_ptr<osg::Shader> readRefShaderFile(osg::Shader::Type type, const std::string& filename, const Options* options)
  153. {
  154. osg::ref_ptr<osg::Shader> shader = readRefShaderFile(filename, options);
  155. if (shader.valid() && type != osg::Shader::UNDEFINED) shader->setType(type);
  156. return shader;
  157. }
  158. /** Read an osg::Shader from file and set to specified shader type
  159. * Return valid osg::Shader on success,
  160. * return NULL on failure.
  161. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  162. * for the filename extension, and this plugin then handles the request
  163. * to read the specified file.*/
  164. inline osg::ref_ptr<osg::Shader> readRefShaderFile(osg::Shader::Type type, const std::string& filename)
  165. {
  166. return readRefShaderFile(type, filename, Registry::instance()->getOptions());
  167. }
  168. /** Read an osg::Shader from file and set to specified shader type, if a shader isn't loaded fallback to specific shader source.
  169. * Use the Options object to control cache operations and file search paths in osgDB::Registry.
  170. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  171. * for the filename extension, and this plugin then handles the request
  172. * to read the specified file.*/
  173. extern OSGDB_EXPORT osg::ref_ptr<osg::Shader> readRefShaderFileWithFallback(osg::Shader::Type type, const std::string& filename, const Options* options, const char* fallback);
  174. /** Read an osg::Shader from file and set to specified shader type, if a shader isn't loaded fallback to specific shader source.
  175. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  176. * for the filename extension, and this plugin then handles the request
  177. * to read the specified file.*/
  178. inline osg::ref_ptr<osg::Shader> readRefShaderFileWithFallback(osg::Shader::Type type, const std::string& filename, const char* fallback)
  179. {
  180. return osgDB::readRefShaderFileWithFallback(type, filename, Registry::instance()->getOptions(), fallback);
  181. }
  182. /** Read an osg::Script from file.
  183. * Return an assigned osg::ref_ptr on success,
  184. * return an osg::ref_ptr with a NULL pointer assigned to it on failure.
  185. * Use the Options object to control cache operations and file search paths in osgDB::Registry.
  186. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  187. * for the filename extension, and this plugin then handles the request
  188. * to read the specified file.*/
  189. extern OSGDB_EXPORT osg::ref_ptr<osg::Script> readRefScriptFile(const std::string& filename,const Options* options);
  190. /** Read an osg::Script from file.
  191. * Return an assigned osg::ref_ptr on success,
  192. * return an osg::ref_ptr with a NULL pointer assigned to it on failure.
  193. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  194. * for the filename extension, and this plugin then handles the request
  195. * to read the specified file.*/
  196. inline osg::ref_ptr<osg::Script> readRefScriptFile(const std::string& filename)
  197. {
  198. return readRefScriptFile(filename,Registry::instance()->getOptions());
  199. }
  200. #ifdef OSG_PROVIDE_READFILE
  201. /** Read an osg::Object from file.
  202. * Return valid osg::Object on success,
  203. * return NULL on failure.
  204. * Use the Options object to control cache operations and file search paths in osgDB::Registry.
  205. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  206. * for the filename extension, and this plugin then handles the request
  207. * to read the specified file.*/
  208. extern OSGDB_EXPORT osg::Object* readObjectFile(const std::string& filename,const Options* options);
  209. /** Read an osg::Object from file.
  210. * Return valid osg::Object on success,
  211. * return NULL on failure.
  212. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  213. * for the filename extension, and this plugin then handles the request
  214. * to read the specified file.*/
  215. inline osg::Object* readObjectFile(const std::string& filename)
  216. {
  217. return readObjectFile(filename, Registry::instance()->getOptions());
  218. }
  219. template<typename T>
  220. inline T* readFile(const std::string& filename, const Options* options)
  221. {
  222. osg::ref_ptr<osg::Object> object = readRefObjectFile(filename, options);
  223. osg::ref_ptr<T> t = dynamic_cast<T*>(object.get());
  224. object = 0;
  225. return t.release();
  226. }
  227. template<typename T>
  228. inline T* readFile(const std::string& filename)
  229. {
  230. return readFile<T>(filename, Registry::instance()->getOptions());
  231. }
  232. /** Read an osg::Image from file.
  233. * Return valid osg::Image on success,
  234. * return NULL on failure.
  235. * Use the Options object to control cache operations and file search paths in osgDB::Registry.
  236. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  237. * for the filename extension, and this plugin then handles the request
  238. * to read the specified file.*/
  239. extern OSGDB_EXPORT osg::Image* readImageFile(const std::string& filename,const Options* options);
  240. /** Read an osg::Image from file.
  241. * Return valid osg::Image on success,
  242. * return NULL on failure.
  243. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  244. * for the filename extension, and this plugin then handles the request
  245. * to read the specified file.*/
  246. inline osg::Image* readImageFile(const std::string& filename)
  247. {
  248. return readImageFile(filename,Registry::instance()->getOptions());
  249. }
  250. /** Read an osg::HeightField from file.
  251. * Return valid osg::HeightField on success,
  252. * return NULL on failure.
  253. * Use the Options object to control cache operations and file search paths in osgDB::Registry.
  254. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  255. * for the filename extension, and this plugin then handles the request
  256. * to read the specified file.*/
  257. extern OSGDB_EXPORT osg::HeightField* readHeightFieldFile(const std::string& filename,const Options* options);
  258. /** Read an osg::HeightField from file.
  259. * Return valid osg::HeightField on success,
  260. * return NULL on failure.
  261. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  262. * for the filename extension, and this plugin then handles the request
  263. * to read the specified file.*/
  264. inline osg::HeightField* readHeightFieldFile(const std::string& filename)
  265. {
  266. return readHeightFieldFile(filename,Registry::instance()->getOptions());
  267. }
  268. /** Read an osg::Node from file.
  269. * Return valid osg::Node on success,
  270. * return NULL on failure.
  271. * Use the Options object to control cache operations and file search paths in osgDB::Registry.
  272. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  273. * for the filename extension, and this plugin then handles the request
  274. * to read the specified file.*/
  275. extern OSGDB_EXPORT osg::Node* readNodeFile(const std::string& filename,const Options* options);
  276. /** Read an osg::Node from file.
  277. * Return valid osg::Node on success,
  278. * return NULL on failure.
  279. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  280. * for the filename extension, and this plugin then handles the request
  281. * to read the specified file.*/
  282. inline osg::Node* readNodeFile(const std::string& filename)
  283. {
  284. return readNodeFile(filename,Registry::instance()->getOptions());
  285. }
  286. /** Read an osg::Node subgraph from files, creating a osg::Group to contain the nodes if more
  287. * than one subgraph has been loaded.
  288. * Use the Options object to control cache operations and file search paths in osgDB::Registry.
  289. * Does NOT ignore strings beginning with a dash '-' character. */
  290. extern OSGDB_EXPORT osg::Node* readNodeFiles(std::vector<std::string>& fileList,const Options* options);
  291. /** Read an osg::Node subgraph from files, creating a osg::Group to contain the nodes if more
  292. * than one subgraph has been loaded.*/
  293. inline osg::Node* readNodeFiles(std::vector<std::string>& fileList)
  294. {
  295. return readNodeFiles(fileList,Registry::instance()->getOptions());
  296. }
  297. /** Read an osg::Node subgraph from files, creating a osg::Group to contain the nodes if more
  298. * than one subgraph has been loaded.
  299. * Use the Options object to control cache operations and file search paths in osgDB::Registry.*/
  300. extern OSGDB_EXPORT osg::Node* readNodeFiles(osg::ArgumentParser& parser,const Options* options);
  301. /** Read an osg::Node subgraph from files, creating a osg::Group to contain the nodes if more
  302. * than one subgraph has been loaded.*/
  303. inline osg::Node* readNodeFiles(osg::ArgumentParser& parser)
  304. {
  305. return readNodeFiles(parser,Registry::instance()->getOptions());
  306. }
  307. /** Read an osg::Shader from file.
  308. * Return valid osg::Shader on success,
  309. * return NULL on failure.
  310. * Use the Options object to control cache operations and file search paths in osgDB::Registry.
  311. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  312. * for the filename extension, and this plugin then handles the request
  313. * to read the specified file.*/
  314. extern OSGDB_EXPORT osg::Shader* readShaderFile(const std::string& filename,const Options* options);
  315. /** Read an osg::Shader from file.
  316. * Return valid osg::Shader on success,
  317. * return NULL on failure.
  318. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  319. * for the filename extension, and this plugin then handles the request
  320. * to read the specified file.*/
  321. inline osg::Shader* readShaderFile(const std::string& filename)
  322. {
  323. return readShaderFile(filename,Registry::instance()->getOptions());
  324. }
  325. /** Read an osg::Shader from file and set to specified shader type.
  326. * Return valid osg::Shader on success,
  327. * return NULL on failure.
  328. * Use the Options object to control cache operations and file search paths in osgDB::Registry.
  329. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  330. * for the filename extension, and this plugin then handles the request
  331. * to read the specified file.*/
  332. inline osg::Shader* readShaderFile(osg::Shader::Type type, const std::string& filename,const Options* options)
  333. {
  334. osg::Shader* shader = readShaderFile(filename, options);
  335. if (shader && type != osg::Shader::UNDEFINED) shader->setType(type);
  336. return shader;
  337. }
  338. /** Read an osg::Shader from file and set to specified shader type
  339. * Return valid osg::Shader on success,
  340. * return NULL on failure.
  341. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  342. * for the filename extension, and this plugin then handles the request
  343. * to read the specified file.*/
  344. inline osg::Shader* readShaderFile(osg::Shader::Type type, const std::string& filename)
  345. {
  346. return readShaderFile(type, filename,Registry::instance()->getOptions());
  347. }
  348. /** Read an osg::Shader from file and set to specified shader type, if a shader isn't loaded fallback to specific shader source.
  349. * Use the Options object to control cache operations and file search paths in osgDB::Registry.
  350. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  351. * for the filename extension, and this plugin then handles the request
  352. * to read the specified file.*/
  353. inline osg::Shader* readShaderFileWithFallback(osg::Shader::Type type, const std::string& filename, const Options* options, const char* fallback)
  354. {
  355. osg::Shader* shader = readShaderFile(filename, options);
  356. if (shader && type != osg::Shader::UNDEFINED) shader->setType(type);
  357. if (!shader) shader = new osg::Shader(type, fallback);
  358. return shader;
  359. }
  360. /** Read an osg::Shader from file and set to specified shader type, if a shader isn't loaded fallback to specific shader source.
  361. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  362. * for the filename extension, and this plugin then handles the request
  363. * to read the specified file.*/
  364. inline osg::Shader* readShaderFileWithFallback(osg::Shader::Type type, const std::string& filename, const char* fallback)
  365. {
  366. return osgDB::readShaderFileWithFallback(type, filename, Registry::instance()->getOptions(), fallback);
  367. }
  368. /** Read an osg::Script from file.
  369. * Return valid osg::Script on success,
  370. * return NULL on failure.
  371. * Use the Options object to control cache operations and file search paths in osgDB::Registry.
  372. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  373. * for the filename extension, and this plugin then handles the request
  374. * to read the specified file.*/
  375. extern OSGDB_EXPORT osg::Script* readScriptFile(const std::string& filename,const Options* options);
  376. /** Read an osg::Script from file.
  377. * Return valid osg::Script on success,
  378. * return NULL on failure.
  379. * The osgDB::Registry is used to load the appropriate ReaderWriter plugin
  380. * for the filename extension, and this plugin then handles the request
  381. * to read the specified file.*/
  382. inline osg::Script* readScriptFile(const std::string& filename)
  383. {
  384. return readScriptFile(filename,Registry::instance()->getOptions());
  385. }
  386. #endif
  387. }
  388. #endif