PbrtExporter.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 preamble
  97. WritePreamble();
  98. // Write World Description
  99. WriteWorldDefinition();
  100. // Write out to file
  101. outfile->Write(mOutput.str().c_str(),
  102. mOutput.str().length(), 1);
  103. // explicitly release file pointer,
  104. // so we don't have to rely on class destruction.
  105. outfile.reset();
  106. }
  107. // Destructor
  108. PbrtExporter::~PbrtExporter() {
  109. // Empty
  110. }
  111. void PbrtExporter::WriteHeader() {
  112. }
  113. void PbrtExporter::WriteMetaData() {
  114. mOutput << "# Writing out scene metadata:" << std::endl;
  115. aiMetadata* pMetaData = mScene->mMetaData;
  116. for (int i = 0; i < pMetaData->mNumProperties; i++) {
  117. mOutput << "# - ";
  118. mOutput << pMetaData->mKeys[i].C_Str() << " :";
  119. switch(pMetaData->mValues[i].mType) {
  120. case AI_BOOL : {
  121. mOutput << " ";
  122. if (*static_cast<bool*>(pMetaData->mValues[i].mData))
  123. mOutput << "TRUE" << std::endl;
  124. else
  125. mOutput << "FALSE" << std::endl;
  126. break;
  127. }
  128. case AI_INT32 : {
  129. mOutput << " " <<
  130. *static_cast<int32_t*>(pMetaData->mValues[i].mData) <<
  131. std::endl;
  132. break;
  133. }
  134. case AI_UINT64 :
  135. mOutput << " " <<
  136. *static_cast<uint64_t*>(pMetaData->mValues[i].mData) <<
  137. std::endl;
  138. break;
  139. case AI_FLOAT :
  140. mOutput << " " <<
  141. *static_cast<float*>(pMetaData->mValues[i].mData) <<
  142. std::endl;
  143. break;
  144. case AI_DOUBLE :
  145. mOutput << " " <<
  146. *static_cast<double*>(pMetaData->mValues[i].mData) <<
  147. std::endl;
  148. break;
  149. case AI_AISTRING : {
  150. aiString* value =
  151. static_cast<aiString*>(pMetaData->mValues[i].mData);
  152. std::string svalue = value->C_Str();
  153. std::size_t found = svalue.find_first_of("\n");
  154. mOutput << std::endl;
  155. while (found != std::string::npos) {
  156. mOutput << "# " << svalue.substr(0, found) << std::endl;
  157. svalue = svalue.substr(found + 1);
  158. found = svalue.find_first_of("\n");
  159. }
  160. mOutput << "# " << svalue << std::endl;
  161. break;
  162. }
  163. case AI_AIVECTOR3D :
  164. break;
  165. }
  166. mOutput << std::endl;
  167. }
  168. }
  169. void PbrtExporter::WritePreamble() {
  170. }
  171. void PbrtExporter::WriteWorldDefinition() {
  172. }
  173. #endif // ASSIMP_BUILD_NO_PBRT_EXPORTER
  174. #endif // ASSIMP_BUILD_NO_EXPORT