PbrtExporter.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2020, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #ifndef ASSIMP_BUILD_NO_EXPORT
  34. #ifndef ASSIMP_BUILD_NO_PBRT_EXPORTER
  35. #include "PbrtExporter.h"
  36. #include <assimp/version.h> // aiGetVersion
  37. #include <assimp/DefaultIOSystem.h>
  38. #include <assimp/IOSystem.hpp>
  39. #include <assimp/Exporter.hpp>
  40. #include <assimp/DefaultLogger.hpp>
  41. #include <assimp/StreamWriter.h> // StreamWriterLE
  42. #include <assimp/Exceptional.h> // DeadlyExportError
  43. #include <assimp/material.h> // aiTextureType
  44. #include <assimp/scene.h>
  45. #include <assimp/mesh.h>
  46. // Header files, standard library.
  47. #include <memory> // shared_ptr
  48. #include <string>
  49. #include <sstream> // stringstream
  50. #include <ctime> // localtime, tm_*
  51. #include <map>
  52. #include <set>
  53. #include <vector>
  54. #include <array>
  55. #include <unordered_set>
  56. #include <numeric>
  57. using namespace Assimp;
  58. // some constants that we'll use for writing metadata
  59. namespace Assimp {
  60. // ---------------------------------------------------------------------
  61. // Worker function for exporting a scene to ascii pbrt.
  62. // Prototyped and registered in Exporter.cpp
  63. void ExportScenePbrt (
  64. const char* pFile,
  65. IOSystem* pIOSystem,
  66. const aiScene* pScene,
  67. const ExportProperties* pProperties
  68. ){
  69. std::string path = DefaultIOSystem::absolutePath(std::string(pFile));
  70. std::string file = DefaultIOSystem::completeBaseName(std::string(pFile));
  71. // initialize the exporter
  72. PbrtExporter exporter(pScene, pIOSystem, path, file);
  73. }
  74. } // end of namespace Assimp
  75. // Constructor
  76. PbrtExporter::PbrtExporter (
  77. const aiScene* pScene, IOSystem* pIOSystem,
  78. const std::string path, const std::string file)
  79. : mScene(pScene),
  80. mIOSystem(pIOSystem),
  81. mPath(path),
  82. mFile(file)
  83. {
  84. std::unique_ptr<IOStream> outfile;
  85. // Open the indicated file for writing
  86. outfile.reset(mIOSystem->Open(mPath,"wt"));
  87. if (!outfile) {
  88. throw DeadlyExportError(
  89. "could not open output .pbrt file: " + std::string(mFile)
  90. );
  91. }
  92. // Write Header
  93. WriteHeader();
  94. // Write metadata to file
  95. WriteMetaData();
  96. // Write scene-wide rendering options
  97. WriteSceneWide();
  98. // Write geometry
  99. WriteGeometry();
  100. // Write World Description
  101. WriteWorldDefinition();
  102. // Write out to file
  103. outfile->Write(mOutput.str().c_str(),
  104. mOutput.str().length(), 1);
  105. // explicitly release file pointer,
  106. // so we don't have to rely on class destruction.
  107. outfile.reset();
  108. // TODO Prettify the output
  109. // TODO Do Animation
  110. }
  111. // Destructor
  112. PbrtExporter::~PbrtExporter() {
  113. // Empty
  114. }
  115. void PbrtExporter::WriteHeader() {
  116. // TODO
  117. }
  118. void PbrtExporter::WriteMetaData() {
  119. mOutput << "# Writing out scene metadata:" << std::endl;
  120. aiMetadata* pMetaData = mScene->mMetaData;
  121. for (int i = 0; i < pMetaData->mNumProperties; i++) {
  122. mOutput << "# - ";
  123. mOutput << pMetaData->mKeys[i].C_Str() << " :";
  124. switch(pMetaData->mValues[i].mType) {
  125. case AI_BOOL : {
  126. mOutput << " ";
  127. if (*static_cast<bool*>(pMetaData->mValues[i].mData))
  128. mOutput << "TRUE" << std::endl;
  129. else
  130. mOutput << "FALSE" << std::endl;
  131. break;
  132. }
  133. case AI_INT32 : {
  134. mOutput << " " <<
  135. *static_cast<int32_t*>(pMetaData->mValues[i].mData) <<
  136. std::endl;
  137. break;
  138. }
  139. case AI_UINT64 :
  140. mOutput << " " <<
  141. *static_cast<uint64_t*>(pMetaData->mValues[i].mData) <<
  142. std::endl;
  143. break;
  144. case AI_FLOAT :
  145. mOutput << " " <<
  146. *static_cast<float*>(pMetaData->mValues[i].mData) <<
  147. std::endl;
  148. break;
  149. case AI_DOUBLE :
  150. mOutput << " " <<
  151. *static_cast<double*>(pMetaData->mValues[i].mData) <<
  152. std::endl;
  153. break;
  154. case AI_AISTRING : {
  155. aiString* value =
  156. static_cast<aiString*>(pMetaData->mValues[i].mData);
  157. std::string svalue = value->C_Str();
  158. std::size_t found = svalue.find_first_of("\n");
  159. mOutput << std::endl;
  160. while (found != std::string::npos) {
  161. mOutput << "# " << svalue.substr(0, found) << std::endl;
  162. svalue = svalue.substr(found + 1);
  163. found = svalue.find_first_of("\n");
  164. }
  165. mOutput << "# " << svalue << std::endl;
  166. break;
  167. }
  168. case AI_AIVECTOR3D :
  169. // TODO
  170. mOutput << " Vector3D (unable to print)" << std::endl;
  171. break;
  172. default:
  173. // AI_META_MAX and FORCE_32BIT
  174. mOutput << " META_MAX or FORCE_32Bit (unable to print)" << std::endl;
  175. break;
  176. }
  177. }
  178. }
  179. void PbrtExporter::WriteSceneWide() {
  180. // Cameras & Film
  181. WriteCameras();
  182. // Samplers
  183. // Filters
  184. // Integrators
  185. // Accelerators
  186. // Participating Media
  187. }
  188. void PbrtExporter::WriteWorldDefinition() {
  189. }
  190. void PbrtExporter::WriteCameras() {
  191. mOutput << std::endl;
  192. mOutput << "# Writing Camera data:" << std::endl;
  193. mOutput << "# - Number of Cameras found in scene: ";
  194. mOutput << mScene->mNumCameras << std::endl;
  195. if (mScene->mNumCameras == 0){
  196. mOutput << "# - No Cameras found in the scene" << std::endl;
  197. return;
  198. }
  199. if (mScene->mNumCameras > 1) {
  200. mOutput << "# - Multiple Cameras found in scene" << std::endl;
  201. mOutput << "# - Defaulting to first Camera specified" << std::endl;
  202. }
  203. for(int i = 0; i < mScene->mNumCameras; i++){
  204. WriteCamera(i);
  205. }
  206. }
  207. void PbrtExporter::WriteCamera(int i) {
  208. auto camera = mScene->mCameras[i];
  209. bool cameraActive = i == 0;
  210. mOutput << "# - Camera " << i+1 << ": "
  211. << camera->mName.C_Str() << std::endl;
  212. // Get camera aspect ratio
  213. float aspect = camera->mAspect;
  214. if(aspect == 0){
  215. mOutput << "# No aspect ratio set, defaulting to 4/3" << std::endl;
  216. aspect = 4.0/3.0;
  217. }
  218. if(!cameraActive)
  219. mOutput << "# ";
  220. mOutput << "\"float aspect_" << camera->mName.C_Str() << "\" ["
  221. << aspect << "]" << std::endl;
  222. // Get camera fov
  223. if (!cameraActive)
  224. mOutput << "# ";
  225. if (aspect >= 1.0) {
  226. mOutput << "\"float fov_" << camera->mName.C_Str() << "\" ["
  227. << AI_RAD_TO_DEG(camera->mHorizontalFOV)
  228. << "]" << std::endl;
  229. } else {
  230. mOutput << "\"float fov_" << camera->mName.C_Str() << "\" ["
  231. << AI_RAD_TO_DEG(camera->mHorizontalFOV * aspect)
  232. << "]" << std::endl;
  233. }
  234. // Get Film xres and yres
  235. if(!cameraActive)
  236. mOutput << "# ";
  237. mOutput << "\"integer xres_" << camera->mName.C_Str() << "\" ["
  238. << (int)640 << "]" << std::endl;
  239. if(!cameraActive)
  240. mOutput << "# ";
  241. mOutput << "\"integer yres_" << camera->mName.C_Str() << "\" ["
  242. << (int)round(640/aspect) << "]" << std::endl;
  243. // Print Film for this camera
  244. if (!cameraActive)
  245. mOutput << "# ";
  246. mOutput << "Film \"image\" " << std::endl;
  247. if (!cameraActive)
  248. mOutput << "# ";
  249. mOutput << " \"integer xresolution\" \"xres_"
  250. << camera->mName.C_Str() << "\"" << std::endl;
  251. if (!cameraActive)
  252. mOutput << "# ";
  253. mOutput << " \"integer yresolution\" \"yres_"
  254. << camera->mName.C_Str() << "\"" << std::endl;
  255. // Get Camera clipping planes?
  256. // TODO
  257. // Get camera transform
  258. // Isn't optimally efficient, but is the simplest implementation
  259. // Get camera node
  260. auto cameraNode = mScene->mRootNode->FindNode(camera->mName);
  261. if (!cameraNode) {
  262. mOutput << "# ERROR: Camera declared but not found in scene tree" << std::endl;
  263. }
  264. else {
  265. std::vector<aiMatrix4x4> matrixChain;
  266. auto tempNode = cameraNode;
  267. while(tempNode) {
  268. matrixChain.insert(matrixChain.begin(), tempNode->mTransformation);
  269. tempNode = tempNode->mParent;
  270. }
  271. aiMatrix4x4 w2c = matrixChain[0];
  272. for(int i = 1; i < matrixChain.size(); i++){
  273. w2c *= matrixChain[i];
  274. }
  275. if (!cameraActive)
  276. mOutput << "# ";
  277. mOutput << "Transform "
  278. << w2c.a1 << " " << w2c.a2 << " " << w2c.a3 << " " << w2c.a4 << " "
  279. << w2c.b1 << " " << w2c.b2 << " " << w2c.b3 << " " << w2c.b4 << " "
  280. << w2c.c1 << " " << w2c.c2 << " " << w2c.c3 << " " << w2c.c4 << " "
  281. << w2c.d1 << " " << w2c.d2 << " " << w2c.d3 << " " << w2c.d4
  282. << std::endl;
  283. }
  284. // Print camera descriptor
  285. if(!cameraActive)
  286. mOutput << "# ";
  287. mOutput << "Camera \"perspective\" \"float fov\" "
  288. << "\"fov_" << camera->mName.C_Str() << "\"" << std::endl;
  289. }
  290. void PbrtExporter::WriteGeometry() {
  291. }
  292. #endif // ASSIMP_BUILD_NO_PBRT_EXPORTER
  293. #endif // ASSIMP_BUILD_NO_EXPORT