ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
VtkMultiTextureRenderer.h
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 
8 #pragma once
9 
10 #include <vtkSmartPointer.h>
11 
12 #include <memory>
13 #include <string>
14 #include <unordered_map>
15 #include <vector>
16 
17 class vtkActor;
18 class vtkPolyData;
19 class vtkTexture;
20 class vtkImageData;
21 class vtkPolyDataMapper;
22 class vtkRenderer;
23 class vtkProperty;
24 
25 namespace VtkUtils {
26 
38 public:
42  struct PBRMaterial {
43  std::string name; // Material name
44 
45  // PBR texture paths
46  std::string baseColorTexture; // Albedo/Diffuse
47  std::string normalTexture; // Normal map
48  std::string metallicTexture; // Metallic
49  std::string roughnessTexture; // Roughness
50  std::string aoTexture; // Ambient Occlusion
51  std::string emissiveTexture; // Emissive
52 
53  // Advanced PBR texture paths (VTK 9.2+)
54  std::string sheenTexture; // Sheen
55  std::string clearcoatTexture; // Clearcoat
56  std::string clearcoatRoughnessTexture; // Clearcoat roughness
57  std::string anisotropyTexture; // Anisotropy
58 
59  // Material parameters (use these values if no texture)
60  float baseColor[3] = {1.0f, 1.0f, 1.0f};
61  float metallic = 0.0f;
62  float roughness = 0.5f;
63  float ao = 1.0f;
64  float emissive[3] = {0.0f, 0.0f, 0.0f};
65  float opacity = 1.0f;
66 
67  // Advanced PBR parameters (VTK 9.2+)
68  float anisotropy = 0.0f; // Anisotropic reflection [0,1]
69  float anisotropyRotation = 0.0f; // Anisotropy rotation angle [0,1]
70  float clearcoat = 0.0f; // Clearcoat layer strength [0,1]
71  float clearcoatRoughness = 0.0f; // Clearcoat roughness [0,1]
72  float sheen = 0.0f; // Sheen for fabric-like materials [0,1]
73  float sheenTint = 0.0f; // Sheen color tint [0,1]
74 
75  // Phong lighting parameters (for non-PBR rendering)
76  // RGB colors for ambient, diffuse, specular (can be used with
77  // SetAmbientColor, etc.) Default to white (1.0, 1.0, 1.0) to match
78  // baseColor and VTK defaults Ambient RGB color (white)
79  float ambientColor[3] = {1.0f, 1.0f, 1.0f};
80  // Diffuse RGB color (white)
81  float diffuseColor[3] = {1.0f, 1.0f, 1.0f};
82  // Specular RGB color (white)
83  float specularColor[3] = {1.0f, 1.0f, 1.0f};
84  // Intensity coefficients (for backward compatibility, used if colors
85  // are uniform) Ambient intensity (legacy, use ambientColor for RGB)
86  float ambient = 1.0f;
87  // Diffuse intensity (legacy, use diffuseColor for RGB)
88  float diffuse = 1.0f;
89  // Specular intensity (legacy, use specularColor for RGB)
90  float specular = 1.0f;
91  // Shininess (legacy, use shininess for specular power)
92  float shininess = 4.0f;
93 
94  // Flag to indicate if there are multiple map_Kd textures
95  // VTK PBR doesn't support multiple map_Kd, so we need to use
96  // traditional multi-texture rendering instead
97  bool hasMultipleMapKd = false;
98 
99  // Check if has PBR textures
100  bool hasPBRTextures() const {
101  return !baseColorTexture.empty() || !normalTexture.empty() ||
102  !metallicTexture.empty() || !roughnessTexture.empty() ||
103  !aoTexture.empty();
104  }
105 
106  // Check if has any texture
107  bool hasAnyTexture() const {
108  return hasPBRTextures() || !emissiveTexture.empty();
109  }
110  };
111 
112 public:
115 
125  const PBRMaterial& material,
127  vtkRenderer* renderer = nullptr);
128 
129 private:
133  enum class RenderingMode {
134  PBR, // Full PBR rendering (with PBR textures)
135  TEXTURED, // Single texture Phong rendering (only baseColor texture)
136  MATERIAL_ONLY // Pure material Phong rendering (no texture)
137  };
138 
142  enum class TextureQuality {
143  LOW, // Low quality, fast loading
144  MEDIUM, // Medium quality
145  HIGH, // High quality
146  ORIGINAL // Original quality, no compression
147  };
148 
152  enum class FilterMode {
153  NEAREST, // Nearest neighbor
154  LINEAR, // Linear interpolation
155  MIPMAP // Mipmap (best quality)
156  };
157 
161  struct RenderOptions {
162  TextureQuality quality = TextureQuality::HIGH;
163  FilterMode filter_mode = FilterMode::MIPMAP;
164  bool enable_mipmaps = true;
165  bool enable_texture_cache = true;
166  bool enable_compression = false; // Texture compression (save VRAM)
167  int max_texture_size = 4096; // Maximum texture size
168  bool interpolate_scalars = true; // Scalar interpolation
169  bool use_phong_shading = true; // Use Phong shading
170  };
171 
175  struct MaterialInfo {
176  std::string name; // Material name
177  std::string texture_file; // Texture file path
178  float ambient[3] = {1.0f, 1.0f, 1.0f}; // Ambient light
179  float diffuse[3] = {1.0f, 1.0f, 1.0f}; // Diffuse
180  float specular[3] = {0.0f, 0.0f, 0.0f}; // Specular
181  float shininess = 0.0f; // Shininess
182  float opacity = 1.0f; // Opacity
183  int illum_model = 2; // Illumination model
184  };
185 
191  RenderingMode DetectRenderingMode(const PBRMaterial& material) const;
192 
205  bool ApplyPBRRendering(vtkSmartPointer<vtkActor> actor,
206  const PBRMaterial& material,
208  vtkRenderer* renderer,
209  float metallic,
210  float roughness,
211  float ao,
212  float opacity);
213 
221  bool ApplyTexturedRendering(vtkSmartPointer<vtkActor> actor,
222  const PBRMaterial& material,
223  float opacity);
224 
232  bool ApplyMaterialOnlyRendering(vtkSmartPointer<vtkActor> actor,
233  const PBRMaterial& material,
234  float opacity);
235 
242  void SetProperties(vtkProperty* property,
243  const PBRMaterial& material,
244  float opacity);
245 
252  vtkSmartPointer<vtkTexture> LoadTexture(const std::string& texture_path,
253  const RenderOptions& options);
254 
255 private:
256  struct Impl;
257  std::unique_ptr<Impl> impl_;
258  RenderOptions default_options_;
259 
260  // Texture cache: path -> texture
261  std::unordered_map<std::string, vtkSmartPointer<vtkTexture>> texture_cache_;
262 
263  // Texture memory usage statistics
264  std::unordered_map<std::string, size_t> texture_memory_usage_;
265 };
266 
267 } // namespace VtkUtils
std::string name
Multi-texture renderer - Efficient and robust multi-texture mesh rendering support.
bool ApplyPBRMaterial(vtkSmartPointer< vtkActor > actor, const PBRMaterial &material, vtkSmartPointer< vtkPolyData > polydata, vtkRenderer *renderer=nullptr)
Apply PBR material to actor (Filament style)
RenderingMode
Rendering mode enumeration.
colmap::RenderOptions RenderOptions
Generic PBR material structure (supports multi-texture)