mtl.patch.txt 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. diff --git a/src/core/api.cpp b/src/core/api.cpp
  2. index e0c4b4a..b275b85 100644
  3. --- a/src/core/api.cpp
  4. +++ b/src/core/api.cpp
  5. @@ -234,6 +234,9 @@ static std::vector<uint32_t> pushedActiveTransformBits;
  6. static TransformCache transformCache;
  7. int catIndentCount = 0;
  8. +static bool renderEachMaterial = getenv("PBRT_MTL_HACK");
  9. +static std::map<std::string, std::shared_ptr<Material>>::iterator namedMaterialsIter;
  10. +
  11. // API Forward Declarations
  12. std::vector<std::shared_ptr<Shape>> MakeShapes(const std::string &name,
  13. const Transform *ObjectToWorld,
  14. @@ -1346,6 +1349,14 @@ void pbrtWorldEnd() {
  15. // Create scene and render
  16. if (PbrtOptions.cat || PbrtOptions.toPly) {
  17. printf("%*sWorldEnd\n", catIndentCount, "");
  18. + } else if (renderEachMaterial) {
  19. + for (namedMaterialsIter = graphicsState.namedMaterials.begin();
  20. + namedMaterialsIter != graphicsState.namedMaterials.end(); ++namedMaterialsIter) {
  21. + std::unique_ptr<Integrator> integrator(renderOptions->MakeIntegrator());
  22. + std::unique_ptr<Scene> scene(renderOptions->MakeScene());
  23. + if (scene && integrator) integrator->Render(*scene);
  24. + }
  25. + // TODO? render stuff with no material bound?
  26. } else {
  27. std::unique_ptr<Integrator> integrator(renderOptions->MakeIntegrator());
  28. std::unique_ptr<Scene> scene(renderOptions->MakeScene());
  29. @@ -1371,13 +1382,24 @@ void pbrtWorldEnd() {
  30. }
  31. Scene *RenderOptions::MakeScene() {
  32. - std::shared_ptr<Primitive> accelerator =
  33. - MakeAccelerator(AcceleratorName, primitives, AcceleratorParams);
  34. + std::shared_ptr<Primitive> accelerator;
  35. + if (renderEachMaterial) {
  36. + std::vector<std::shared_ptr<Primitive>> filteredPrims;
  37. + std::copy_if(primitives.begin(), primitives.end(), std::back_inserter(filteredPrims),
  38. + [](const std::shared_ptr<Primitive> &p) -> bool {
  39. + return p->GetMaterial() == namedMaterialsIter->second.get();
  40. + });
  41. + accelerator = MakeAccelerator(AcceleratorName, filteredPrims, AcceleratorParams);
  42. + } else {
  43. + accelerator = MakeAccelerator(AcceleratorName, primitives, AcceleratorParams);
  44. + }
  45. if (!accelerator) accelerator = std::make_shared<BVHAccel>(primitives);
  46. Scene *scene = new Scene(accelerator, lights);
  47. - // Erase primitives and lights from _RenderOptions_
  48. - primitives.erase(primitives.begin(), primitives.end());
  49. - lights.erase(lights.begin(), lights.end());
  50. + if (!renderEachMaterial) {
  51. + // Erase primitives and lights from _RenderOptions_
  52. + primitives.erase(primitives.begin(), primitives.end());
  53. + lights.erase(lights.begin(), lights.end());
  54. + }
  55. return scene;
  56. }
  57. @@ -1427,7 +1449,17 @@ Integrator *RenderOptions::MakeIntegrator() const {
  58. Camera *RenderOptions::MakeCamera() const {
  59. std::unique_ptr<Filter> filter = MakeFilter(FilterName, FilterParams);
  60. - Film *film = MakeFilm(FilmName, FilmParams, std::move(filter));
  61. + Film *film = nullptr;
  62. + if (renderEachMaterial) {
  63. + ParamSet fp = FilmParams;
  64. + std::unique_ptr<std::string[]> fns(new std::string[1]);
  65. + fns[0] = namedMaterialsIter->first + ".exr";
  66. + fp.AddString("filename", std::move(fns), 1);
  67. + std::unique_ptr<Filter> box(new BoxFilter({0.5f, 0.5f}));
  68. + film = MakeFilm(FilmName, fp, std::move(box));
  69. + } else
  70. + film = MakeFilm(FilmName, FilmParams, std::move(filter));
  71. +
  72. if (!film) {
  73. Error("Unable to create film.");
  74. return nullptr;