ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
PBRRenderer.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
9 
10 #include <CVLog.h>
11 #include <ecvMaterialSet.h>
12 
16 
17 // VTK
18 #include <vtkActor.h>
19 #include <vtkLODActor.h>
20 #include <vtkPolyData.h>
21 #include <vtkPolyDataMapper.h>
22 #include <vtkRenderer.h>
23 
24 namespace PclUtils {
25 namespace renders {
26 
28  : vtk_renderer_(std::make_unique<VtkUtils::VtkMultiTextureRenderer>()) {}
29 
30 bool PBRRenderer::CanHandle(size_t material_count,
31  bool has_pbr_textures,
32  bool has_multiple_map_kd) const {
33  // PBR renderer can handle:
34  // 1. Only ONE material (VTK PBR limitation: each actor can only have one
35  // PBR material)
36  // 2. Does NOT have multiple map_Kd (VTK PBR limitation)
37  // 3. Can handle both cases:
38  // - Has PBR textures (will use PBR rendering mode)
39  // - No textures, only material properties (will use MATERIAL_ONLY
40  // rendering mode)
41  // VtkMultiTextureRenderer::ApplyPBRMaterial will automatically detect and
42  // use the appropriate mode
43  return material_count == 1 && !has_multiple_map_kd;
44 }
45 
47 
48 bool PBRRenderer::Apply(vtkLODActor* actor,
49  const ccMaterialSet* materials,
50  vtkPolyData* polydata,
51  vtkRenderer* renderer) {
52  if (!ValidateActor(actor) || !ValidateMaterials(materials)) {
53  return false;
54  }
55 
57  "[PBRRenderer::Apply] Applying unified PBR rendering for %zu "
58  "materials "
59  "(VTK PBR only supports single material per actor, using first "
60  "material). VtkMultiTextureRenderer will automatically detect "
61  "rendering mode "
62  "(PBR/TEXTURED/MATERIAL_ONLY) based on material properties.",
63  materials->size());
64 
65  // Convert material
66  auto pbr_material = MaterialConverter::FromMaterialSet(materials);
67 
69  "[PBRRenderer::Apply] Converted material '%s': "
70  "baseColorTexture=%s, normalTexture=%s, metallicTexture=%s, "
71  "roughnessTexture=%s, aoTexture=%s, hasPBRTextures=%d, "
72  "hasAnyTexture=%d",
73  pbr_material.name.c_str(), pbr_material.baseColorTexture.c_str(),
74  pbr_material.normalTexture.c_str(),
75  pbr_material.metallicTexture.c_str(),
76  pbr_material.roughnessTexture.c_str(),
77  pbr_material.aoTexture.c_str(), pbr_material.hasPBRTextures(),
78  pbr_material.hasAnyTexture());
79 
80  // Apply using VtkMultiTextureRenderer
81  vtkSmartPointer<vtkActor> actor_ptr(actor);
82  vtkSmartPointer<vtkPolyData> poly_ptr(polydata);
83  bool result = vtk_renderer_->ApplyPBRMaterial(actor_ptr, pbr_material,
84  poly_ptr, renderer);
85 
86  if (result) {
88  "[PBRRenderer::Apply] PBR material applied successfully");
89  } else {
90  CVLog::Error("[PBRRenderer::Apply] Failed to apply PBR material");
91  }
92 
93  return result;
94 }
95 
96 bool PBRRenderer::Update(vtkActor* actor,
97  const ccMaterialSet* materials,
98  vtkPolyData* polydata,
99  vtkRenderer* renderer) {
100  if (!ValidateActor(actor) || !ValidateMaterials(materials)) {
101  return false;
102  }
103 
104  vtkLODActor* lod_actor = vtkLODActor::SafeDownCast(actor);
105  if (!lod_actor) {
107  "[PBRRenderer::Update] Actor is not a vtkLODActor, cannot "
108  "apply PBR");
109  return false;
110  }
111 
112  return Apply(lod_actor, materials, polydata, renderer);
113 }
114 
115 std::string PBRRenderer::GetName() const { return "PBRRenderer"; }
116 
117 } // namespace renders
118 } // namespace PclUtils
core::Tensor result
Definition: VtkUtils.cpp:76
static bool PrintDebug(const char *format,...)
Same as Print, but works only in Debug mode.
Definition: CVLog.cpp:153
static bool Warning(const char *format,...)
Prints out a formatted warning message in console.
Definition: CVLog.cpp:133
static bool Error(const char *format,...)
Display an error dialog with formatted message.
Definition: CVLog.cpp:143
static VtkUtils::VtkMultiTextureRenderer::PBRMaterial FromMaterialSet(const ccMaterialSet *materials)
Convert first material from ccMaterialSet to PBRMaterial.
bool Apply(vtkLODActor *actor, const class ccMaterialSet *materials, vtkPolyData *polydata, vtkRenderer *renderer) override
Apply rendering to actor.
Definition: PBRRenderer.cpp:48
std::string GetName() const override
Get renderer name for logging.
RenderingMode GetMode() const override
Get the rendering mode this renderer implements.
Definition: PBRRenderer.cpp:46
bool CanHandle(size_t material_count, bool has_pbr_textures, bool has_multiple_map_kd) const override
Check if this renderer can handle the given material.
Definition: PBRRenderer.cpp:30
bool Update(vtkActor *actor, const class ccMaterialSet *materials, vtkPolyData *polydata, vtkRenderer *renderer) override
Update existing actor with new materials.
Definition: PBRRenderer.cpp:96
bool ValidateActor(vtkActor *actor) const
Helper: Validate actor.
bool ValidateMaterials(const class ccMaterialSet *materials) const
Helper: Validate materials.
Mesh (triangle) material.
RenderingMode
Rendering mode enumeration.
Definition: Eigen.h:85