Open3D (C++ API)  0.12.0
FilamentScene.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2020 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 // 4068: Filament has some clang-specific vectorizing pragma's that MSVC flags
30 // 4146: Filament's utils/algorithm.h utils::details::ctz() tries to negate
31 // an unsigned int.
32 // 4293: Filament's utils/algorithm.h utils::details::clz() does strange
33 // things with MSVC. Somehow sizeof(unsigned int) > 4, but its size is
34 // 32 so that x >> 32 gives a warning. (Or maybe the compiler can't
35 // determine the if statement does not run.)
36 // 4305: LightManager.h needs to specify some constants as floats
37 #ifdef _MSC_VER
38 #pragma warning(push)
39 #pragma warning(disable : 4068 4146 4293 4305)
40 #endif // _MSC_VER
41 
42 #include <filament/LightManager.h>
43 #include <filament/RenderableManager.h>
44 #include <utils/Entity.h>
45 
46 #ifdef _MSC_VER
47 #pragma warning(pop)
48 #endif // _MSC_VER
49 
50 #include <Eigen/Geometry>
51 #include <unordered_map>
52 #include <vector>
53 
60 
62 namespace filament {
63 class Box;
64 class Engine;
65 class IndirectLight;
66 class Renderer;
67 class Scene;
68 class Skybox;
69 class TransformManager;
70 class VertexBuffer;
71 } // namespace filament
73 
74 namespace open3d {
75 namespace visualization {
76 namespace rendering {
77 
78 class FilamentView;
79 class GeometryBuffersBuilder;
80 class Renderer;
81 class View;
82 
83 // Contains renderable objects like geometry and lights
84 // Can have multiple views
85 class FilamentScene : public Scene {
86 public:
87  using Transform = Eigen::Transform<float, 3, Eigen::Affine>;
88 
89  FilamentScene(filament::Engine& engine,
90  FilamentResourceManager& resource_mgr,
91  Renderer& renderer);
92  ~FilamentScene();
93 
94  // NOTE: Temporarily needed to support old View interface for ImGUI
95  ViewHandle AddView(std::int32_t x,
96  std::int32_t y,
97  std::uint32_t w,
98  std::uint32_t h) override;
99 
100  View* GetView(const ViewHandle& view_id) const override;
101  void SetViewActive(const ViewHandle& view_id, bool is_active) override;
102  void SetRenderOnce(const ViewHandle& view_id) override;
103  void RemoveView(const ViewHandle& view_id) override;
104 
105  // Camera
106  void AddCamera(const std::string& camera_name,
107  std::shared_ptr<Camera> cam) override;
108  void RemoveCamera(const std::string& camera_name) override;
109  void SetActiveCamera(const std::string& camera_name) override;
110 
111  // Scene geometry
112  bool AddGeometry(const std::string& object_name,
113  const geometry::Geometry3D& geometry,
114  const Material& material,
115  const std::string& downsampled_name = "",
116  size_t downsample_threshold = SIZE_MAX) override;
117  bool AddGeometry(const std::string& object_name,
118  const t::geometry::PointCloud& point_cloud,
119  const Material& material,
120  const std::string& downsampled_name = "",
121  size_t downsample_threshold = SIZE_MAX) override;
122  bool AddGeometry(const std::string& object_name,
123  const TriangleMeshModel& model) override;
124  bool HasGeometry(const std::string& object_name) const override;
125  void UpdateGeometry(const std::string& object_name,
126  const t::geometry::PointCloud& point_cloud,
127  uint32_t update_flags) override;
128  void RemoveGeometry(const std::string& object_name) override;
129  void ShowGeometry(const std::string& object_name, bool show) override;
130  bool GeometryIsVisible(const std::string& object_name) override;
131  void SetGeometryTransform(const std::string& object_name,
132  const Transform& transform) override;
133  Transform GetGeometryTransform(const std::string& object_name) override;
134  geometry::AxisAlignedBoundingBox GetGeometryBoundingBox(
135  const std::string& object_name) override;
136  void GeometryShadows(const std::string& object_name,
137  bool cast_shadows,
138  bool receive_shadows) override;
139  void SetGeometryCulling(const std::string& object_name,
140  bool enable) override;
141  void SetGeometryPriority(const std::string& object_name,
142  uint8_t priority) override;
143  void OverrideMaterial(const std::string& object_name,
144  const Material& material) override;
145  void QueryGeometry(std::vector<std::string>& geometry) override;
146  void OverrideMaterialAll(const Material& material,
147  bool shader_only = true) override;
148 
149  // Lighting Environment
150  bool AddPointLight(const std::string& light_name,
151  const Eigen::Vector3f& color,
152  const Eigen::Vector3f& position,
153  float intensity,
154  float falloff,
155  bool cast_shadows) override;
156  bool AddSpotLight(const std::string& light_name,
157  const Eigen::Vector3f& color,
158  const Eigen::Vector3f& position,
159  const Eigen::Vector3f& direction,
160  float intensity,
161  float falloff,
162  float inner_cone_angle,
163  float outer_cone_angle,
164  bool cast_shadows) override;
165  bool AddDirectionalLight(const std::string& light_name,
166  const Eigen::Vector3f& color,
167  const Eigen::Vector3f& direction,
168  float intensity,
169  bool cast_shadows) override;
170  Light& GetLight(const std::string& light_name) override;
171  void RemoveLight(const std::string& light_name) override;
172  void UpdateLight(const std::string& light_name,
173  const Light& light) override;
174  void UpdateLightColor(const std::string& light_name,
175  const Eigen::Vector3f& color) override;
176  void UpdateLightPosition(const std::string& light_name,
177  const Eigen::Vector3f& position) override;
178  void UpdateLightDirection(const std::string& light_name,
179  const Eigen::Vector3f& direction) override;
180  void UpdateLightIntensity(const std::string& light_name,
181  float intensity) override;
182  void UpdateLightFalloff(const std::string& light_name,
183  float falloff) override;
184  void UpdateLightConeAngles(const std::string& light_name,
185  float inner_cone_angle,
186  float outer_cone_angle) override;
187  void EnableLightShadow(const std::string& light_name,
188  bool cast_shadows) override;
189 
190  void SetSunLight(const Eigen::Vector3f& direction,
191  const Eigen::Vector3f& color,
192  float intensity) override;
193  void EnableSunLight(bool enable) override;
194  void EnableSunLightShadows(bool enable) override;
195  float GetSunLightIntensity() override;
196  void SetSunLightDirection(const Eigen::Vector3f& direction) override;
197  Eigen::Vector3f GetSunLightDirection() override;
198  void SetSunAngularRadius(float radius) override;
199  void SetSunHaloSize(float size) override;
200  void SetSunHaloFalloff(float falloff) override;
201 
202  bool SetIndirectLight(const std::string& ibl_name) override;
203  const std::string& GetIndirectLight() override;
204  void EnableIndirectLight(bool enable) override;
205  void SetIndirectLightIntensity(float intensity) override;
206  float GetIndirectLightIntensity() override;
207  void SetIndirectLightRotation(const Transform& rotation) override;
208  Transform GetIndirectLightRotation() override;
209  void ShowSkybox(bool show) override;
210  void SetBackground(
211  const Eigen::Vector4f& color,
212  const std::shared_ptr<geometry::Image> image = nullptr) override;
213 
214  void RenderToImage(std::function<void(std::shared_ptr<geometry::Image>)>
215  callback) override;
216 
217  void Draw(filament::Renderer& renderer);
218  // NOTE: Can GetNativeScene be removed?
219  filament::Scene* GetNativeScene() const { return scene_; }
220 
221 private:
222  MaterialInstanceHandle AssignMaterialToFilamentGeometry(
223  filament::RenderableManager::Builder& builder,
224  const Material& material);
225  enum BufferReuse { kNo, kYes };
226  bool CreateAndAddFilamentEntity(
227  const std::string& object_name,
228  GeometryBuffersBuilder& buffer_builder,
229  filament::Box& aabb,
232  const Material& material,
233  BufferReuse reusing_vertex_buffer = BufferReuse::kNo);
234 
235  filament::Engine& engine_;
236  FilamentResourceManager& resource_mgr_;
237  filament::Scene* scene_ = nullptr;
238 
239  struct TextureMaps {
240  rendering::TextureHandle albedo_map =
241  rendering::FilamentResourceManager::kDefaultTexture;
242  rendering::TextureHandle normal_map =
243  rendering::FilamentResourceManager::kDefaultNormalMap;
244  rendering::TextureHandle ao_rough_metal_map =
245  rendering::FilamentResourceManager::kDefaultTexture;
246  rendering::TextureHandle reflectance_map =
247  rendering::FilamentResourceManager::kDefaultTexture;
248  rendering::TextureHandle clear_coat_map =
249  rendering::FilamentResourceManager::kDefaultTexture;
250  rendering::TextureHandle clear_coat_roughness_map =
251  rendering::FilamentResourceManager::kDefaultTexture;
252  rendering::TextureHandle anisotropy_map =
253  rendering::FilamentResourceManager::kDefaultTexture;
254  rendering::TextureHandle gradient_texture =
255  rendering::FilamentResourceManager::kDefaultTexture;
256  };
257 
258  struct GeometryMaterialInstance {
259  TextureMaps maps;
260  Material properties;
261  MaterialInstanceHandle mat_instance;
262  };
263 
264  struct RenderableGeometry {
265  std::string name;
266  bool visible = true;
267  bool cast_shadows = true;
268  bool receive_shadow = true;
269 
270  GeometryMaterialInstance mat;
271 
272  // Filament resources
273  utils::Entity filament_entity;
276  void ReleaseResources(filament::Engine& engine,
277  FilamentResourceManager& manager);
278  };
279 
280  struct LightEntity {
281  bool enabled = true;
282  utils::Entity filament_entity;
283  };
284 
285  // NOTE: ViewContainer and views_ are temporary
286  struct ViewContainer {
287  std::unique_ptr<FilamentView> view;
288  bool is_active = true;
289  int render_count = -1;
290  };
291  std::unordered_map<REHandle_abstract, ViewContainer> views_;
292 
293  std::vector<RenderableGeometry*> GetGeometry(const std::string& object_name,
294  bool warn_if_not_found = true);
295  bool GeometryIsModel(const std::string& object_name) const;
296  LightEntity* GetLightInternal(const std::string& light_name,
297  bool warn_if_not_found = true);
298  void OverrideMaterialInternal(RenderableGeometry* geom,
299  const Material& material,
300  bool shader_only = false);
301  void UpdateMaterialProperties(RenderableGeometry& geom);
302  void UpdateDefaultLit(GeometryMaterialInstance& geom_mi);
303  void UpdateDefaultLitSSR(GeometryMaterialInstance& geom_mi);
304  void UpdateDefaultUnlit(GeometryMaterialInstance& geom_mi);
305  void UpdateNormalShader(GeometryMaterialInstance& geom_mi);
306  void UpdateDepthShader(GeometryMaterialInstance& geom_mi);
307  void UpdateGradientShader(GeometryMaterialInstance& geom_mi);
308  void UpdateSolidColorShader(GeometryMaterialInstance& geom_mi);
309  void UpdateBackgroundShader(GeometryMaterialInstance& geom_mi);
310  void UpdateLineShader(GeometryMaterialInstance& geom_mi);
311  void UpdateUnlitPolygonOffsetShader(GeometryMaterialInstance& geom_mi);
312  utils::EntityInstance<filament::TransformManager>
313  GetGeometryTransformInstance(RenderableGeometry* geom);
314  void CreateSunDirectionalLight();
315 
316  std::unordered_map<std::string, RenderableGeometry> geometries_;
317  std::unordered_map<std::string, LightEntity> lights_;
318  std::unordered_map<std::string, std::vector<std::string>> model_geometries_;
319 
320  Eigen::Vector4f background_color_;
321  std::shared_ptr<geometry::Image> background_image_;
322  std::string ibl_name_;
323  bool ibl_enabled_ = false;
324  bool skybox_enabled_ = false;
325  std::weak_ptr<filament::IndirectLight> indirect_light_;
326  std::weak_ptr<filament::Skybox> skybox_;
327  LightEntity sun_;
328 };
329 
330 } // namespace rendering
331 } // namespace visualization
332 } // namespace open3d
void Draw(const std::vector< std::shared_ptr< geometry::Geometry3D >> &geometries, const std::string &window_name, int width, int height, const std::vector< DrawAction > &actions)
Definition: Draw.cpp:54
Eigen::Transform< float, 3, Eigen::Affine > Transform
Definition: FilamentScene.h:87
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t timeout_in_ms capture_handle capture_handle capture_handle image_handle temperature_c k4a_image_t image_handle uint8_t image_handle image_handle image_handle image_handle uint32_t
Definition: K4aPlugin.cpp:557
A bounding box that is aligned along the coordinate axes.
Definition: BoundingVolume.h:149
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t int32_t
Definition: K4aPlugin.cpp:398
Definition: FilamentResourceManager.h:62
int size
Definition: FilePCD.cpp:59
math::float4 color
Definition: LineSetBuffers.cpp:64
The base geometry class for 3D geometries.
Definition: Geometry3D.h:46
math::float3 position
Definition: LineSetBuffers.cpp:62
A pointcloud contains a set of 3D points.
Definition: PointCloud.h:94
Definition: FilamentGeometryBuffersBuilder.h:75
Definition: PinholeCameraIntrinsic.cpp:35
std::string name
Definition: FilePCD.cpp:58
Definition: FilamentEngine.h:31
bool SetActiveCamera(const std::string &path, std::shared_ptr< ConnectionBase > connection)
Definition: RemoteFunctions.cpp:394
filament::Scene * GetNativeScene() const
Definition: FilamentScene.h:219