2
0

PbrtExporter.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, 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. /* TODO:
  34. Material improvements:
  35. - don't export embedded textures that we're not going to use
  36. - diffuse roughness
  37. - what is with the uv mapping, uv transform not coming through??
  38. - metal? glass? mirror? detect these better?
  39. - eta/k from RGB?
  40. - emissive textures: warn at least
  41. Other:
  42. - use aiProcess_GenUVCoords if needed to handle spherical/planar uv mapping?
  43. - don't build up a big string in memory but write directly to a file
  44. - aiProcess_Triangulate meshes to get triangles only?
  45. - animation (allow specifying a time)
  46. */
  47. #ifndef ASSIMP_BUILD_NO_EXPORT
  48. #ifndef ASSIMP_BUILD_NO_PBRT_EXPORTER
  49. #include "PbrtExporter.h"
  50. #include <assimp/version.h>
  51. #include <assimp/DefaultIOSystem.h>
  52. #include <assimp/IOSystem.hpp>
  53. #include <assimp/Exporter.hpp>
  54. #include <assimp/DefaultLogger.hpp>
  55. #include <assimp/StreamWriter.h>
  56. #include <assimp/Exceptional.h>
  57. #include <assimp/material.h>
  58. #include <assimp/scene.h>
  59. #include <assimp/mesh.h>
  60. #include <algorithm>
  61. #include <cctype>
  62. #include <cmath>
  63. #include <fstream>
  64. #include <functional>
  65. #include <iostream>
  66. #include <memory>
  67. #include <sstream>
  68. #include <string>
  69. #include "Common/StbCommon.h"
  70. using namespace Assimp;
  71. namespace Assimp {
  72. void ExportScenePbrt(const char *pFile, IOSystem *pIOSystem, const aiScene *pScene,
  73. const ExportProperties *) {
  74. std::string path = DefaultIOSystem::absolutePath(std::string(pFile));
  75. std::string file = DefaultIOSystem::completeBaseName(std::string(pFile));
  76. // initialize the exporter
  77. PbrtExporter exporter(pScene, pIOSystem, path, file);
  78. }
  79. } // end of namespace Assimp
  80. static void create_embedded_textures_folder(const aiScene *scene, IOSystem *pIOSystem) {
  81. if (scene->mNumTextures > 0) {
  82. if (!pIOSystem->Exists("textures")) {
  83. if (!pIOSystem->CreateDirectory("textures")) {
  84. throw DeadlyExportError("Could not create textures/ directory.");
  85. }
  86. }
  87. }
  88. }
  89. PbrtExporter::PbrtExporter(
  90. const aiScene *pScene, IOSystem *pIOSystem,
  91. const std::string &path, const std::string &file) :
  92. mScene(pScene),
  93. mIOSystem(pIOSystem),
  94. mPath(path),
  95. mFile(file),
  96. mRootTransform(
  97. // rotates the (already left-handed) CRS -90 degrees around the x axis in order to
  98. // make +Z 'up' and +Y 'towards viewer', as in default in pbrt
  99. 1.f, 0.f, 0.f, 0.f, //
  100. 0.f, 0.f, -1.f, 0.f, //
  101. 0.f, 1.f, 0.f, 0.f, //
  102. 0.f, 0.f, 0.f, 1.f //
  103. ) {
  104. mRootTransform = aiMatrix4x4(
  105. -1.f, 0, 0.f, 0.f, //
  106. 0.0f, -1.f, 0.f, 0.f, //
  107. 0.f, 0.f, 1.f, 0.f, //
  108. 0.f, 0.f, 0.f, 1.f //
  109. ) * mRootTransform;
  110. // Export embedded textures.
  111. create_embedded_textures_folder(mScene, mIOSystem);
  112. for (unsigned int i = 0; i < mScene->mNumTextures; ++i) {
  113. aiTexture* tex = mScene->mTextures[i];
  114. std::string fn = CleanTextureFilename(tex->mFilename, false);
  115. std::cerr << "Writing embedded texture: " << tex->mFilename.C_Str() << " -> "
  116. << fn << "\n";
  117. std::unique_ptr<IOStream> outfile(mIOSystem->Open(fn, "wb"));
  118. if (!outfile) {
  119. throw DeadlyExportError("could not open output texture file: " + fn);
  120. }
  121. if (tex->mHeight == 0) {
  122. // It's binary data
  123. outfile->Write(tex->pcData, tex->mWidth, 1);
  124. } else {
  125. std::cerr << fn << ": TODO handle uncompressed embedded textures.\n";
  126. }
  127. }
  128. #if 0
  129. // Debugging: print the full node hierarchy
  130. std::function<void(aiNode*, int)> visitNode;
  131. visitNode = [&](aiNode* node, int depth) {
  132. for (int i = 0; i < depth; ++i) std::cerr << " ";
  133. std::cerr << node->mName.C_Str() << "\n";
  134. for (int i = 0; i < node->mNumChildren; ++i)
  135. visitNode(node->mChildren[i], depth + 1);
  136. };
  137. visitNode(mScene->mRootNode, 0);
  138. #endif
  139. mOutput.precision(ASSIMP_AI_REAL_TEXT_PRECISION);
  140. // Write everything out
  141. WriteMetaData();
  142. WriteCameras();
  143. WriteWorldDefinition();
  144. // And write the file to disk...
  145. std::string outputFilePath = mPath;
  146. if (!outputFilePath.empty()) {
  147. outputFilePath = outputFilePath + mIOSystem->getOsSeparator();
  148. }
  149. outputFilePath = outputFilePath + mFile +".pbrt";
  150. std::unique_ptr<IOStream> outfile(mIOSystem->Open(outputFilePath,"wt"));
  151. if (!outfile) {
  152. throw DeadlyExportError("could not open output .pbrt file: " + std::string(mFile));
  153. }
  154. outfile->Write(mOutput.str().c_str(), mOutput.str().length(), 1);
  155. }
  156. void PbrtExporter::WriteMetaData() {
  157. mOutput << "#############################\n";
  158. mOutput << "# Scene metadata:\n";
  159. aiMetadata* pMetaData = mScene->mMetaData;
  160. for (unsigned int i = 0; i < pMetaData->mNumProperties; i++) {
  161. mOutput << "# - ";
  162. mOutput << pMetaData->mKeys[i].C_Str() << " :";
  163. switch(pMetaData->mValues[i].mType) {
  164. case AI_BOOL : {
  165. mOutput << " ";
  166. if (*static_cast<bool*>(pMetaData->mValues[i].mData))
  167. mOutput << "TRUE\n";
  168. else
  169. mOutput << "FALSE\n";
  170. break;
  171. }
  172. case AI_INT32 : {
  173. mOutput << " " <<
  174. *static_cast<int32_t*>(pMetaData->mValues[i].mData) <<
  175. std::endl;
  176. break;
  177. }
  178. case AI_UINT64 :
  179. mOutput << " " <<
  180. *static_cast<uint64_t*>(pMetaData->mValues[i].mData) <<
  181. std::endl;
  182. break;
  183. case AI_FLOAT :
  184. mOutput << " " <<
  185. *static_cast<float*>(pMetaData->mValues[i].mData) <<
  186. std::endl;
  187. break;
  188. case AI_DOUBLE :
  189. mOutput << " " <<
  190. *static_cast<double*>(pMetaData->mValues[i].mData) <<
  191. std::endl;
  192. break;
  193. case AI_AISTRING : {
  194. aiString* value =
  195. static_cast<aiString*>(pMetaData->mValues[i].mData);
  196. std::string svalue = value->C_Str();
  197. std::size_t found = svalue.find_first_of('\n');
  198. mOutput << "\n";
  199. while (found != std::string::npos) {
  200. mOutput << "# " << svalue.substr(0, found) << "\n";
  201. svalue = svalue.substr(found + 1);
  202. found = svalue.find_first_of('\n');
  203. }
  204. mOutput << "# " << svalue << "\n";
  205. break;
  206. }
  207. case AI_AIVECTOR3D :
  208. // TODO
  209. mOutput << " Vector3D (unable to print)\n";
  210. break;
  211. default:
  212. // AI_META_MAX and FORCE_32BIT
  213. mOutput << " META_MAX or FORCE_32Bit (unable to print)\n";
  214. break;
  215. }
  216. }
  217. }
  218. void PbrtExporter::WriteCameras() {
  219. mOutput << "\n";
  220. mOutput << "###############################\n";
  221. mOutput << "# Cameras (" << mScene->mNumCameras << ") total\n\n";
  222. if (mScene->mNumCameras == 0) {
  223. std::cerr << "Warning: No cameras found in scene file.\n";
  224. return;
  225. }
  226. if (mScene->mNumCameras > 1) {
  227. std::cerr << "Multiple cameras found in scene file; defaulting to first one specified.\n";
  228. }
  229. for (unsigned int i = 0; i < mScene->mNumCameras; i++) {
  230. WriteCamera(i);
  231. }
  232. }
  233. aiMatrix4x4 PbrtExporter::GetNodeTransform(const aiString &name) const {
  234. aiMatrix4x4 m;
  235. auto node = mScene->mRootNode->FindNode(name);
  236. if (!node) {
  237. std::cerr << '"' << name.C_Str() << "\": node not found in scene tree.\n";
  238. throw DeadlyExportError("Could not find node");
  239. }
  240. else {
  241. while (node) {
  242. m = node->mTransformation * m;
  243. node = node->mParent;
  244. }
  245. }
  246. return mRootTransform * m;
  247. }
  248. std::string PbrtExporter::TransformAsString(const aiMatrix4x4 &m) {
  249. // Transpose on the way out to match pbrt's expected layout (sanity
  250. // check: the translation component should be the last 3 entries
  251. // before a '1' as the final entry in the matrix, assuming it's
  252. // non-projective.)
  253. std::stringstream s;
  254. s << m.a1 << " " << m.b1 << " " << m.c1 << " " << m.d1 << " "
  255. << m.a2 << " " << m.b2 << " " << m.c2 << " " << m.d2 << " "
  256. << m.a3 << " " << m.b3 << " " << m.c3 << " " << m.d3 << " "
  257. << m.a4 << " " << m.b4 << " " << m.c4 << " " << m.d4;
  258. return s.str();
  259. }
  260. void PbrtExporter::WriteCamera(int i) {
  261. auto camera = mScene->mCameras[i];
  262. bool cameraActive = i == 0;
  263. mOutput << "# - Camera " << i+1 << ": "
  264. << camera->mName.C_Str() << "\n";
  265. // Get camera aspect ratio
  266. float aspect = camera->mAspect;
  267. if (aspect == 0) {
  268. aspect = 4.f/3.f;
  269. mOutput << "# - Aspect ratio : 1.33333 (no aspect found, defaulting to 4/3)\n";
  270. } else {
  271. mOutput << "# - Aspect ratio : " << aspect << "\n";
  272. }
  273. // Get Film xres and yres
  274. int xres = 1920;
  275. int yres = (int)round(xres/aspect);
  276. // Print Film for this camera
  277. if (!cameraActive)
  278. mOutput << "# ";
  279. mOutput << "Film \"rgb\" \"string filename\" \"" << mFile << ".exr\"\n";
  280. if (!cameraActive)
  281. mOutput << "# ";
  282. mOutput << " \"integer xresolution\" [" << xres << "]\n";
  283. if (!cameraActive)
  284. mOutput << "# ";
  285. mOutput << " \"integer yresolution\" [" << yres << "]\n";
  286. // Get camera fov
  287. float hfov = AI_RAD_TO_DEG(camera->mHorizontalFOV);
  288. float fov = (aspect >= 1.0) ? hfov : (hfov / aspect);
  289. if (fov < 5) {
  290. std::cerr << fov << ": suspiciously low field of view specified by camera. Setting to 45 degrees.\n";
  291. fov = 45;
  292. }
  293. // Get camera transform
  294. aiMatrix4x4 worldFromCamera = GetNodeTransform(camera->mName);
  295. // Print Camera LookAt
  296. auto position = worldFromCamera * camera->mPosition;
  297. auto lookAt = worldFromCamera * (camera->mPosition + camera->mLookAt);
  298. aiMatrix3x3 worldFromCamera3(worldFromCamera);
  299. auto up = worldFromCamera3 * camera->mUp;
  300. up.Normalize();
  301. if (!cameraActive)
  302. mOutput << "# ";
  303. mOutput << "Scale 1 1 1\n";
  304. if (!cameraActive)
  305. mOutput << "# ";
  306. mOutput << "LookAt "
  307. << position.x << " " << position.y << " " << position.z << "\n";
  308. if (!cameraActive)
  309. mOutput << "# ";
  310. mOutput << " "
  311. << lookAt.x << " " << lookAt.y << " " << lookAt.z << "\n";
  312. if (!cameraActive)
  313. mOutput << "# ";
  314. mOutput << " "
  315. << up.x << " " << up.y << " " << up.z << "\n";
  316. // Print camera descriptor
  317. if (!cameraActive)
  318. mOutput << "# ";
  319. mOutput << "Camera \"perspective\" \"float fov\" " << "[" << fov << "]\n\n";
  320. }
  321. void PbrtExporter::WriteWorldDefinition() {
  322. // Figure out which meshes are referenced multiple times; those will be
  323. // emitted as object instances and the rest will be emitted directly.
  324. std::map<int, int> meshUses;
  325. std::function<void(aiNode*)> visitNode;
  326. visitNode = [&](aiNode* node) {
  327. for (unsigned int i = 0; i < node->mNumMeshes; ++i)
  328. ++meshUses[node->mMeshes[i]];
  329. for (unsigned int i = 0; i < node->mNumChildren; ++i)
  330. visitNode(node->mChildren[i]);
  331. };
  332. visitNode(mScene->mRootNode);
  333. int nInstanced = 0, nUnused = 0;
  334. for (const auto &u : meshUses) {
  335. if (u.second == 0) ++nUnused;
  336. else if (u.second > 1) ++nInstanced;
  337. }
  338. std::cerr << nInstanced << " / " << mScene->mNumMeshes << " meshes instanced.\n";
  339. if (nUnused)
  340. std::cerr << nUnused << " meshes defined but not used in scene.\n";
  341. mOutput << "WorldBegin\n";
  342. WriteLights();
  343. WriteTextures();
  344. WriteMaterials();
  345. // Object instance definitions
  346. mOutput << "# Object instance definitions\n\n";
  347. for (const auto &mu : meshUses) {
  348. if (mu.second > 1) {
  349. WriteInstanceDefinition(mu.first);
  350. }
  351. }
  352. mOutput << "# Geometry\n\n";
  353. WriteGeometricObjects(mScene->mRootNode, mRootTransform, meshUses);
  354. }
  355. void PbrtExporter::WriteTextures() {
  356. mOutput << "###################\n";
  357. mOutput << "# Textures\n\n";
  358. C_STRUCT aiString path;
  359. aiTextureMapping mapping;
  360. unsigned int uvIndex;
  361. ai_real blend;
  362. aiTextureOp op;
  363. aiTextureMapMode mapMode[3];
  364. // For every material in the scene,
  365. for (unsigned int m = 0 ; m < mScene->mNumMaterials; m++) {
  366. auto material = mScene->mMaterials[m];
  367. // Parse through all texture types,
  368. for (int tt = 1; tt <= aiTextureType_UNKNOWN; tt++) {
  369. int ttCount = material->GetTextureCount(aiTextureType(tt));
  370. // ... and get every texture
  371. for (int t = 0; t < ttCount; t++) {
  372. // TODO write out texture specifics
  373. // TODO UV transforms may be material specific
  374. // so those may need to be baked into unique tex name
  375. if (material->GetTexture(aiTextureType(tt), t, &path, &mapping,
  376. &uvIndex, &blend, &op, mapMode) != AI_SUCCESS) {
  377. std::cerr << "Error getting texture! " << m << " " << tt << " " << t << "\n";
  378. continue;
  379. }
  380. std::string filename = CleanTextureFilename(path);
  381. if (uvIndex != 0)
  382. std::cerr << "Warning: texture \"" << filename << "\" uses uv set #" <<
  383. uvIndex << " but the pbrt converter only exports uv set 0.\n";
  384. #if 0
  385. if (op != aiTextureOp_Multiply)
  386. std::cerr << "Warning: unexpected texture op " << (int)op <<
  387. " encountered for texture \"" <<
  388. filename << "\". The resulting scene may have issues...\n";
  389. if (blend != 1)
  390. std::cerr << "Blend value of " << blend << " found for texture \"" << filename
  391. << "\" but not handled in converter.\n";
  392. #endif
  393. std::string mappingString;
  394. #if 0
  395. if (mapMode[0] != mapMode[1])
  396. std::cerr << "Different texture boundary mode for u and v for texture \"" <<
  397. filename << "\". Using u for both.\n";
  398. switch (mapMode[0]) {
  399. case aiTextureMapMode_Wrap:
  400. // pbrt's default
  401. break;
  402. case aiTextureMapMode_Clamp:
  403. mappingString = "\"string wrap\" \"clamp\"";
  404. break;
  405. case aiTextureMapMode_Decal:
  406. std::cerr << "Decal texture boundary mode not supported by pbrt for texture \"" <<
  407. filename << "\"\n";
  408. break;
  409. case aiTextureMapMode_Mirror:
  410. std::cerr << "Mirror texture boundary mode not supported by pbrt for texture \"" <<
  411. filename << "\"\n";
  412. break;
  413. default:
  414. std::cerr << "Unexpected map mode " << (int)mapMode[0] << " for texture \"" <<
  415. filename << "\"\n";
  416. //throw DeadlyExportError("Unexpected aiTextureMapMode");
  417. }
  418. #endif
  419. #if 0
  420. aiUVTransform uvTransform;
  421. if (material->Get(AI_MATKEY_TEXTURE(tt, t), uvTransform) == AI_SUCCESS) {
  422. mOutput << "# UV transform " << uvTransform.mTranslation.x << " "
  423. << uvTransform.mTranslation.y << " " << uvTransform.mScaling.x << " "
  424. << uvTransform.mScaling.y << " " << uvTransform.mRotation << "\n";
  425. }
  426. #endif
  427. std::string texName, texType, texOptions;
  428. if (aiTextureType(tt) == aiTextureType_SHININESS ||
  429. aiTextureType(tt) == aiTextureType_OPACITY ||
  430. aiTextureType(tt) == aiTextureType_HEIGHT ||
  431. aiTextureType(tt) == aiTextureType_DISPLACEMENT ||
  432. aiTextureType(tt) == aiTextureType_METALNESS ||
  433. aiTextureType(tt) == aiTextureType_DIFFUSE_ROUGHNESS) {
  434. texType = "float";
  435. texName = std::string("float:") + RemoveSuffix(filename);
  436. if (aiTextureType(tt) == aiTextureType_SHININESS) {
  437. texOptions = " \"bool invert\" true\n";
  438. texName += "_Roughness";
  439. }
  440. } else if (aiTextureType(tt) == aiTextureType_DIFFUSE ||
  441. aiTextureType(tt) == aiTextureType_BASE_COLOR) {
  442. texType = "spectrum";
  443. texName = std::string("rgb:") + RemoveSuffix(filename);
  444. }
  445. // Don't export textures we're not actually going to use...
  446. if (texName.empty())
  447. continue;
  448. if (mTextureSet.find(texName) == mTextureSet.end()) {
  449. mOutput << "Texture \"" << texName << "\" \"" << texType << "\" \"imagemap\"\n"
  450. << texOptions
  451. << " \"string filename\" \"" << filename << "\" " << mappingString << '\n';
  452. mTextureSet.insert(texName);
  453. }
  454. // Also emit a float version for use with alpha testing...
  455. if ((aiTextureType(tt) == aiTextureType_DIFFUSE ||
  456. aiTextureType(tt) == aiTextureType_BASE_COLOR) &&
  457. TextureHasAlphaMask(filename)) {
  458. texType = "float";
  459. texName = std::string("alpha:") + filename;
  460. if (mTextureSet.find(texName) == mTextureSet.end()) {
  461. mOutput << "Texture \"" << texName << "\" \"" << texType << "\" \"imagemap\"\n"
  462. << " \"string filename\" \"" << filename << "\" " << mappingString << '\n';
  463. mTextureSet.insert(texName);
  464. }
  465. }
  466. }
  467. }
  468. }
  469. }
  470. bool PbrtExporter::TextureHasAlphaMask(const std::string &filename) {
  471. // TODO: STBIDEF int stbi_info (char const *filename, int *x, int *y, int *comp);
  472. // quick return if it's 3
  473. int xSize, ySize, nComponents;
  474. unsigned char *data = stbi_load(filename.c_str(), &xSize, &ySize, &nComponents, 0);
  475. if (!data) {
  476. std::cerr << filename << ": unable to load texture and check for alpha mask in texture. "
  477. "Geometry will not be alpha masked with this texture.\n";
  478. return false;
  479. }
  480. bool hasMask = false;
  481. switch (nComponents) {
  482. case 1:
  483. for (int i = 0; i < xSize * ySize; ++i)
  484. if (data[i] != 255) {
  485. hasMask = true;
  486. break;
  487. }
  488. break;
  489. case 2:
  490. for (int y = 0; y < ySize; ++y)
  491. for (int x = 0; x < xSize; ++x)
  492. if (data[2 * (x + y * xSize) + 1] != 255) {
  493. hasMask = true;
  494. break;
  495. }
  496. break;
  497. case 3:
  498. break;
  499. case 4:
  500. for (int y = 0; y < ySize; ++y)
  501. for (int x = 0; x < xSize; ++x)
  502. if (data[4 * (x + y * xSize) + 3] != 255) {
  503. hasMask = true;
  504. break;
  505. }
  506. break;
  507. default:
  508. std::cerr << filename << ": unexpected number of image channels, " <<
  509. nComponents << ".\n";
  510. }
  511. stbi_image_free(data);
  512. return hasMask;
  513. }
  514. void PbrtExporter::WriteMaterials() {
  515. mOutput << "\n";
  516. mOutput << "####################\n";
  517. mOutput << "# Materials (" << mScene->mNumMaterials << ") total\n\n";
  518. for (unsigned int i = 0; i < mScene->mNumMaterials; i++) {
  519. WriteMaterial(i);
  520. }
  521. mOutput << "\n\n";
  522. }
  523. void PbrtExporter::WriteMaterial(int m) {
  524. aiMaterial* material = mScene->mMaterials[m];
  525. // get material name
  526. auto materialName = material->GetName();
  527. mOutput << std::endl << "# - Material " << m+1 << ": " << materialName.C_Str() << "\n";
  528. // Print out number of properties
  529. mOutput << "# - Number of Material Properties: " << material->mNumProperties << "\n";
  530. // Print out texture type counts
  531. mOutput << "# - Non-Zero Texture Type Counts: ";
  532. for (int i = 1; i <= aiTextureType_UNKNOWN; i++) {
  533. int count = material->GetTextureCount(aiTextureType(i));
  534. if (count > 0)
  535. mOutput << aiTextureTypeToString(aiTextureType(i)) << ": " << count << " ";
  536. }
  537. mOutput << "\n";
  538. auto White = [](const aiColor3D &c) { return c.r == 1 && c.g == 1 && c.b == 1; };
  539. auto Black = [](const aiColor3D &c) { return c.r == 0 && c.g == 0 && c.b == 0; };
  540. aiColor3D diffuse, specular, transparency;
  541. bool constantDiffuse = (material->Get(AI_MATKEY_COLOR_DIFFUSE, diffuse) == AI_SUCCESS &&
  542. !White(diffuse));
  543. bool constantSpecular = (material->Get(AI_MATKEY_COLOR_SPECULAR, specular) == AI_SUCCESS &&
  544. !White(specular));
  545. bool constantTransparency = (material->Get(AI_MATKEY_COLOR_TRANSPARENT, transparency) == AI_SUCCESS &&
  546. !Black(transparency));
  547. float opacity, shininess, shininessStrength, eta;
  548. bool constantOpacity = (material->Get(AI_MATKEY_OPACITY, opacity) == AI_SUCCESS &&
  549. opacity != 0);
  550. bool constantShininess = material->Get(AI_MATKEY_SHININESS, shininess) == AI_SUCCESS;
  551. bool constantShininessStrength = material->Get(AI_MATKEY_SHININESS_STRENGTH, shininessStrength) == AI_SUCCESS;
  552. bool constantEta = (material->Get(AI_MATKEY_REFRACTI, eta) == AI_SUCCESS &&
  553. eta != 1);
  554. mOutput << "# - Constants: diffuse " << constantDiffuse << " specular " << constantSpecular <<
  555. " transparency " << constantTransparency << " opacity " << constantOpacity <<
  556. " shininess " << constantShininess << " shininess strength " << constantShininessStrength <<
  557. " eta " << constantEta << "\n";
  558. aiString roughnessMap;
  559. if (material->Get(AI_MATKEY_TEXTURE_SHININESS(0), roughnessMap) == AI_SUCCESS) {
  560. std::string roughnessTexture = std::string("float:") +
  561. RemoveSuffix(CleanTextureFilename(roughnessMap)) + "_Roughness";
  562. mOutput << "MakeNamedMaterial \"" << materialName.C_Str() << "\""
  563. << " \"string type\" \"coateddiffuse\"\n"
  564. << " \"texture roughness\" \"" << roughnessTexture << "\"\n";
  565. } else if (constantShininess) {
  566. // Assume plastic for now at least
  567. float roughness = std::max(0.f, 1.f - shininess);
  568. mOutput << "MakeNamedMaterial \"" << materialName.C_Str() << "\""
  569. << " \"string type\" \"coateddiffuse\"\n"
  570. << " \"float roughness\" " << roughness << "\n";
  571. } else
  572. // Diffuse
  573. mOutput << "MakeNamedMaterial \"" << materialName.C_Str() << "\""
  574. << " \"string type\" \"diffuse\"\n";
  575. aiString diffuseTexture;
  576. if (material->Get(AI_MATKEY_TEXTURE_DIFFUSE(0), diffuseTexture) == AI_SUCCESS)
  577. mOutput << " \"texture reflectance\" \"rgb:" << RemoveSuffix(CleanTextureFilename(diffuseTexture)) << "\"\n";
  578. else
  579. mOutput << " \"rgb reflectance\" [ " << diffuse.r << " " << diffuse.g <<
  580. " " << diffuse.b << " ]\n";
  581. aiString displacementTexture, normalMap;
  582. if (material->Get(AI_MATKEY_TEXTURE_NORMALS(0), displacementTexture) == AI_SUCCESS)
  583. mOutput << " \"string normalmap\" \"" << CleanTextureFilename(displacementTexture) << "\"\n";
  584. else if (material->Get(AI_MATKEY_TEXTURE_HEIGHT(0), displacementTexture) == AI_SUCCESS)
  585. mOutput << " \"texture displacement\" \"float:" <<
  586. RemoveSuffix(CleanTextureFilename(displacementTexture)) << "\"\n";
  587. else if (material->Get(AI_MATKEY_TEXTURE_DISPLACEMENT(0), displacementTexture) == AI_SUCCESS)
  588. mOutput << " \"texture displacement\" \"float:" <<
  589. RemoveSuffix(CleanTextureFilename(displacementTexture)) << "\"\n";
  590. }
  591. std::string PbrtExporter::CleanTextureFilename(const aiString &f, bool rewriteExtension) const {
  592. std::string fn = f.C_Str();
  593. // Remove directory name
  594. size_t offset = fn.find_last_of("/\\");
  595. if (offset != std::string::npos) {
  596. fn.erase(0, offset + 1);
  597. }
  598. // Expect all textures in textures
  599. fn = std::string("textures") + mIOSystem->getOsSeparator() + fn;
  600. // Rewrite extension for unsupported file formats.
  601. if (rewriteExtension) {
  602. offset = fn.rfind('.');
  603. if (offset != std::string::npos) {
  604. std::string extension = fn;
  605. extension.erase(0, offset + 1);
  606. std::transform(extension.begin(), extension.end(), extension.begin(),
  607. [](unsigned char c) { return (char)std::tolower(c); });
  608. if (extension != "tga" && extension != "exr" && extension != "png" &&
  609. extension != "pfm" && extension != "hdr") {
  610. std::string orig = fn;
  611. fn.erase(offset + 1);
  612. fn += "png";
  613. // Does it already exist? Warn if not.
  614. std::ifstream filestream(fn);
  615. if (!filestream.good())
  616. std::cerr << orig << ": must convert this texture to PNG.\n";
  617. }
  618. }
  619. }
  620. return fn;
  621. }
  622. std::string PbrtExporter::RemoveSuffix(std::string filename) {
  623. size_t offset = filename.rfind('.');
  624. if (offset != std::string::npos)
  625. filename.erase(offset);
  626. return filename;
  627. }
  628. void PbrtExporter::WriteLights() {
  629. mOutput << "\n";
  630. mOutput << "#################\n";
  631. mOutput << "# Lights\n\n";
  632. if (mScene->mNumLights == 0) {
  633. // Skip the default light if no cameras and this is flat up geometry
  634. if (mScene->mNumCameras > 0) {
  635. std::cerr << "No lights specified. Using default infinite light.\n";
  636. mOutput << "AttributeBegin\n";
  637. mOutput << " # default light\n";
  638. mOutput << " LightSource \"infinite\" \"blackbody L\" [6000 1]\n";
  639. mOutput << "AttributeEnd\n\n";
  640. }
  641. } else {
  642. for (unsigned int i = 0; i < mScene->mNumLights; ++i) {
  643. const aiLight *light = mScene->mLights[i];
  644. mOutput << "# Light " << light->mName.C_Str() << "\n";
  645. mOutput << "AttributeBegin\n";
  646. aiMatrix4x4 worldFromLight = GetNodeTransform(light->mName);
  647. mOutput << " Transform [ " << TransformAsString(worldFromLight) << " ]\n";
  648. aiColor3D color = light->mColorDiffuse + light->mColorSpecular;
  649. if (light->mAttenuationConstant != 0)
  650. color = color * (ai_real)(1. / light->mAttenuationConstant);
  651. switch (light->mType) {
  652. case aiLightSource_DIRECTIONAL: {
  653. mOutput << " LightSource \"distant\"\n";
  654. mOutput << " \"point3 from\" [ " << light->mPosition.x << " " <<
  655. light->mPosition.y << " " << light->mPosition.z << " ]\n";
  656. aiVector3D to = light->mPosition + light->mDirection;
  657. mOutput << " \"point3 to\" [ " << to.x << " " << to.y << " " << to.z << " ]\n";
  658. mOutput << " \"rgb L\" [ " << color.r << " " << color.g << " " << color.b << " ]\n";
  659. break;
  660. } case aiLightSource_POINT:
  661. mOutput << " LightSource \"distant\"\n";
  662. mOutput << " \"point3 from\" [ " << light->mPosition.x << " " <<
  663. light->mPosition.y << " " << light->mPosition.z << " ]\n";
  664. mOutput << " \"rgb L\" [ " << color.r << " " << color.g << " " << color.b << " ]\n";
  665. break;
  666. case aiLightSource_SPOT: {
  667. mOutput << " LightSource \"spot\"\n";
  668. mOutput << " \"point3 from\" [ " << light->mPosition.x << " " <<
  669. light->mPosition.y << " " << light->mPosition.z << " ]\n";
  670. aiVector3D to = light->mPosition + light->mDirection;
  671. mOutput << " \"point3 to\" [ " << to.x << " " << to.y << " " << to.z << " ]\n";
  672. mOutput << " \"rgb L\" [ " << color.r << " " << color.g << " " << color.b << " ]\n";
  673. mOutput << " \"float coneangle\" [ " << AI_RAD_TO_DEG(light->mAngleOuterCone) << " ]\n";
  674. mOutput << " \"float conedeltaangle\" [ " << AI_RAD_TO_DEG(light->mAngleOuterCone -
  675. light->mAngleInnerCone) << " ]\n";
  676. break;
  677. } case aiLightSource_AMBIENT:
  678. mOutput << "# ignored ambient light source\n";
  679. break;
  680. case aiLightSource_AREA: {
  681. aiVector3D left = light->mDirection ^ light->mUp;
  682. // rectangle. center at position, direction is normal vector
  683. ai_real dLeft = light->mSize.x / 2, dUp = light->mSize.y / 2;
  684. aiVector3D vertices[4] = {
  685. light->mPosition - dLeft * left - dUp * light->mUp,
  686. light->mPosition + dLeft * left - dUp * light->mUp,
  687. light->mPosition - dLeft * left + dUp * light->mUp,
  688. light->mPosition + dLeft * left + dUp * light->mUp };
  689. mOutput << " AreaLightSource \"diffuse\"\n";
  690. mOutput << " \"rgb L\" [ " << color.r << " " << color.g << " " << color.b << " ]\n";
  691. mOutput << " Shape \"bilinearmesh\"\n";
  692. mOutput << " \"point3 p\" [ ";
  693. for (int j = 0; j < 4; ++j)
  694. mOutput << vertices[j].x << " " << vertices[j].y << " " << vertices[j].z;
  695. mOutput << " ]\n";
  696. mOutput << " \"integer indices\" [ 0 1 2 3 ]\n";
  697. break;
  698. } default:
  699. mOutput << "# ignored undefined light source type\n";
  700. break;
  701. }
  702. mOutput << "AttributeEnd\n\n";
  703. }
  704. }
  705. }
  706. void PbrtExporter::WriteMesh(aiMesh* mesh) {
  707. mOutput << "# - Mesh: ";
  708. const char* mName;
  709. if (mesh->mName == aiString(""))
  710. mName = "<No Name>";
  711. else
  712. mName = mesh->mName.C_Str();
  713. mOutput << mName << "\n";
  714. // Check if any types other than tri
  715. if ( (mesh->mPrimitiveTypes & aiPrimitiveType_POINT)
  716. || (mesh->mPrimitiveTypes & aiPrimitiveType_LINE)
  717. || (mesh->mPrimitiveTypes & aiPrimitiveType_POLYGON)) {
  718. std::cerr << "Error: ignoring point / line / polygon mesh " << mName << ".\n";
  719. return;
  720. }
  721. mOutput << "AttributeBegin\n";
  722. aiMaterial* material = mScene->mMaterials[mesh->mMaterialIndex];
  723. mOutput << " NamedMaterial \"" << material->GetName().C_Str() << "\"\n";
  724. // Handle area lights
  725. aiColor3D emission;
  726. if (material->Get(AI_MATKEY_COLOR_EMISSIVE, emission) == AI_SUCCESS &&
  727. (emission.r > 0 || emission.g > 0 || emission.b > 0))
  728. mOutput << " AreaLightSource \"diffuse\" \"rgb L\" [ " << emission.r <<
  729. " " << emission.g << " " << emission.b << " ]\n";
  730. // Alpha mask
  731. std::string alpha;
  732. aiString opacityTexture;
  733. if (material->Get(AI_MATKEY_TEXTURE_OPACITY(0), opacityTexture) == AI_SUCCESS ||
  734. material->Get(AI_MATKEY_TEXTURE_DIFFUSE(0), opacityTexture) == AI_SUCCESS) {
  735. // material->Get(AI_MATKEY_TEXTURE_BASE_COLOR(0), opacityTexture) == AI_SUCCESS)
  736. std::string texName = std::string("alpha:") + CleanTextureFilename(opacityTexture);
  737. if (mTextureSet.find(texName) != mTextureSet.end())
  738. alpha = std::string(" \"texture alpha\" \"") + texName + "\"\n";
  739. } else {
  740. float opacity = 1;
  741. if (material->Get(AI_MATKEY_OPACITY, opacity) == AI_SUCCESS && opacity < 1)
  742. alpha = std::string(" \"float alpha\" [ ") + std::to_string(opacity) + " ]\n";
  743. }
  744. // Output the shape specification
  745. mOutput << "Shape \"trianglemesh\"\n" <<
  746. alpha <<
  747. " \"integer indices\" [";
  748. // Start with faces (which hold indices)
  749. for (unsigned int i = 0; i < mesh->mNumFaces; i++) {
  750. auto face = mesh->mFaces[i];
  751. if (face.mNumIndices != 3) throw DeadlyExportError("oh no not a tri!");
  752. for (unsigned int j = 0; j < face.mNumIndices; j++) {
  753. mOutput << face.mIndices[j] << " ";
  754. }
  755. if ((i % 7) == 6) mOutput << "\n ";
  756. }
  757. mOutput << "]\n";
  758. // Then go to vertices
  759. mOutput << " \"point3 P\" [";
  760. for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
  761. auto vector = mesh->mVertices[i];
  762. mOutput << vector.x << " " << vector.y << " " << vector.z << " ";
  763. if ((i % 4) == 3) mOutput << "\n ";
  764. }
  765. mOutput << "]\n";
  766. // Normals (if present)
  767. if (mesh->mNormals) {
  768. mOutput << " \"normal N\" [";
  769. for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
  770. auto normal = mesh->mNormals[i];
  771. mOutput << normal.x << " " << normal.y << " " << normal.z << " ";
  772. if ((i % 4) == 3) mOutput << "\n ";
  773. }
  774. mOutput << "]\n";
  775. }
  776. // Tangents (if present)
  777. if (mesh->mTangents) {
  778. mOutput << " \"vector3 S\" [";
  779. for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
  780. auto tangent = mesh->mTangents[i];
  781. mOutput << tangent.x << " " << tangent.y << " " << tangent.z << " ";
  782. if ((i % 4) == 3) mOutput << "\n ";
  783. }
  784. mOutput << "]\n";
  785. }
  786. // Texture Coords (if present)
  787. // Find the first set of 2D texture coordinates..
  788. for (int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
  789. if (mesh->mNumUVComponents[i] == 2) {
  790. // assert(mesh->mTextureCoords[i] != nullptr);
  791. aiVector3D* uv = mesh->mTextureCoords[i];
  792. mOutput << " \"point2 uv\" [";
  793. for (unsigned int j = 0; j < mesh->mNumVertices; ++j) {
  794. mOutput << uv[j].x << " " << uv[j].y << " ";
  795. if ((j % 6) == 5) mOutput << "\n ";
  796. }
  797. mOutput << "]\n";
  798. break;
  799. }
  800. }
  801. // TODO: issue warning if there are additional UV sets?
  802. mOutput << "AttributeEnd\n";
  803. }
  804. void PbrtExporter::WriteInstanceDefinition(int i) {
  805. aiMesh* mesh = mScene->mMeshes[i];
  806. mOutput << "ObjectBegin \"";
  807. if (mesh->mName == aiString(""))
  808. mOutput << "mesh_" << i+1 << "\"\n";
  809. else
  810. mOutput << mesh->mName.C_Str() << "_" << i+1 << "\"\n";
  811. WriteMesh(mesh);
  812. mOutput << "ObjectEnd\n";
  813. }
  814. void PbrtExporter::WriteGeometricObjects(aiNode* node, aiMatrix4x4 worldFromObject,
  815. std::map<int, int> &meshUses) {
  816. // Sometimes interior nodes have degenerate matrices??
  817. if (node->mTransformation.Determinant() != 0)
  818. worldFromObject = worldFromObject * node->mTransformation;
  819. if (node->mNumMeshes > 0) {
  820. mOutput << "AttributeBegin\n";
  821. mOutput << " Transform [ " << TransformAsString(worldFromObject) << "]\n";
  822. for (unsigned int i = 0; i < node->mNumMeshes; i++) {
  823. aiMesh* mesh = mScene->mMeshes[node->mMeshes[i]];
  824. if (meshUses[node->mMeshes[i]] == 1) {
  825. // If it's only used once in the scene, emit it directly as
  826. // a triangle mesh.
  827. mOutput << " # " << mesh->mName.C_Str();
  828. WriteMesh(mesh);
  829. } else {
  830. // If it's used multiple times, there will be an object
  831. // instance for it, so emit a reference to that.
  832. mOutput << " ObjectInstance \"";
  833. if (mesh->mName == aiString(""))
  834. mOutput << "mesh_" << node->mMeshes[i] + 1 << "\"\n";
  835. else
  836. mOutput << mesh->mName.C_Str() << "_" << node->mMeshes[i] + 1 << "\"\n";
  837. }
  838. }
  839. mOutput << "AttributeEnd\n\n";
  840. }
  841. // Recurse through children
  842. for (unsigned int i = 0; i < node->mNumChildren; i++) {
  843. WriteGeometricObjects(node->mChildren[i], worldFromObject, meshUses);
  844. }
  845. }
  846. #endif // ASSIMP_BUILD_NO_PBRT_EXPORTER
  847. #endif // ASSIMP_BUILD_NO_EXPORT