Open3D (C++ API)  0.11.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 OverrideMaterial(const std::string& object_name,
140  const Material& material) override;
141  void QueryGeometry(std::vector<std::string>& geometry) override;
142  void OverrideMaterialAll(const Material& material,
143  bool shader_only = true) override;
144 
145  // Lighting Environment
146  bool AddPointLight(const std::string& light_name,
147  const Eigen::Vector3f& color,
148  const Eigen::Vector3f& position,
149  float intensity,
150  float falloff,
151  bool cast_shadows) override;
152  bool AddSpotLight(const std::string& light_name,
153  const Eigen::Vector3f& color,
154  const Eigen::Vector3f& position,
155  const Eigen::Vector3f& direction,
156  float intensity,
157  float falloff,
158  float inner_cone_angle,
159  float outer_cone_angle,
160  bool cast_shadows) override;
161  Light& GetLight(const std::string& light_name) override;
162  void RemoveLight(const std::string& light_name) override;
163  void UpdateLight(const std::string& light_name,
164  const Light& light) override;
165  void UpdateLightColor(const std::string& light_name,
166  const Eigen::Vector3f& color) override;
167  void UpdateLightPosition(const std::string& light_name,
168  const Eigen::Vector3f& position) override;
169  void UpdateLightDirection(const std::string& light_name,
170  const Eigen::Vector3f& direction) override;
171  void UpdateLightIntensity(const std::string& light_name,
172  float intensity) override;
173  void UpdateLightFalloff(const std::string& light_name,
174  float falloff) override;
175  void UpdateLightConeAngles(const std::string& light_name,
176  float inner_cone_angle,
177  float outer_cone_angle) override;
178  void EnableLightShadow(const std::string& light_name,
179  bool cast_shadows) override;
180 
181  void SetDirectionalLight(const Eigen::Vector3f& direction,
182  const Eigen::Vector3f& color,
183  float intensity) override;
184  void EnableDirectionalLight(bool enable) override;
185  void EnableDirectionalLightShadows(bool enable) override;
186  void SetDirectionalLightDirection(
187  const Eigen::Vector3f& direction) override;
188  Eigen::Vector3f GetDirectionalLightDirection() override;
189 
190  bool SetIndirectLight(const std::string& ibl_name) override;
191  const std::string& GetIndirectLight() override;
192  void EnableIndirectLight(bool enable) override;
193  void SetIndirectLightIntensity(float intensity) override;
194  float GetIndirectLightIntensity() override;
195  void SetIndirectLightRotation(const Transform& rotation) override;
196  Transform GetIndirectLightRotation() override;
197  void ShowSkybox(bool show) override;
198  void SetBackgroundColor(const Eigen::Vector4f& color) override;
199 
200  void RenderToImage(std::function<void(std::shared_ptr<geometry::Image>)>
201  callback) override;
202 
203  void Draw(filament::Renderer& renderer);
204  // NOTE: Can GetNativeScene be removed?
205  filament::Scene* GetNativeScene() const { return scene_; }
206 
207 private:
208  MaterialInstanceHandle AssignMaterialToFilamentGeometry(
209  filament::RenderableManager::Builder& builder,
210  const Material& material);
211  enum BufferReuse { kNo, kYes };
212  bool CreateAndAddFilamentEntity(
213  const std::string& object_name,
214  GeometryBuffersBuilder& buffer_builder,
215  filament::Box& aabb,
218  const Material& material,
219  BufferReuse reusing_vertex_buffer = BufferReuse::kNo);
220 
221  filament::Engine& engine_;
222  FilamentResourceManager& resource_mgr_;
223  filament::Scene* scene_ = nullptr;
224 
225  struct TextureMaps {
226  rendering::TextureHandle albedo_map =
227  rendering::FilamentResourceManager::kDefaultTexture;
228  rendering::TextureHandle normal_map =
229  rendering::FilamentResourceManager::kDefaultNormalMap;
230  rendering::TextureHandle ao_rough_metal_map =
231  rendering::FilamentResourceManager::kDefaultTexture;
232  rendering::TextureHandle reflectance_map =
233  rendering::FilamentResourceManager::kDefaultTexture;
234  rendering::TextureHandle clear_coat_map =
235  rendering::FilamentResourceManager::kDefaultTexture;
236  rendering::TextureHandle clear_coat_roughness_map =
237  rendering::FilamentResourceManager::kDefaultTexture;
238  rendering::TextureHandle anisotropy_map =
239  rendering::FilamentResourceManager::kDefaultTexture;
240  rendering::TextureHandle gradient_texture =
241  rendering::FilamentResourceManager::kDefaultTexture;
242  };
243 
244  struct GeometryMaterialInstance {
245  TextureMaps maps;
246  Material properties;
247  MaterialInstanceHandle mat_instance;
248  };
249 
250  struct RenderableGeometry {
251  std::string name;
252  bool visible = true;
253  bool cast_shadows = true;
254  bool receive_shadow = true;
255 
256  GeometryMaterialInstance mat;
257 
258  // Filament resources
259  utils::Entity filament_entity;
262  void ReleaseResources(filament::Engine& engine,
263  FilamentResourceManager& manager);
264  };
265 
266  struct LightEntity {
267  bool enabled = true;
268  utils::Entity filament_entity;
269  };
270 
271  // NOTE: ViewContainer and views_ are temporary
272  struct ViewContainer {
273  std::unique_ptr<FilamentView> view;
274  bool is_active = true;
275  int render_count = -1;
276  };
277  std::unordered_map<REHandle_abstract, ViewContainer> views_;
278 
279  std::vector<RenderableGeometry*> GetGeometry(const std::string& object_name,
280  bool warn_if_not_found = true);
281  bool GeometryIsModel(const std::string& object_name) const;
282  LightEntity* GetLightInternal(const std::string& light_name,
283  bool warn_if_not_found = true);
284  void OverrideMaterialInternal(RenderableGeometry* geom,
285  const Material& material,
286  bool shader_only = false);
287  void UpdateMaterialProperties(RenderableGeometry& geom);
288  void UpdateDefaultLit(GeometryMaterialInstance& geom_mi);
289  void UpdateDefaultUnlit(GeometryMaterialInstance& geom_mi);
290  void UpdateNormalShader(GeometryMaterialInstance& geom_mi);
291  void UpdateDepthShader(GeometryMaterialInstance& geom_mi);
292  void UpdateGradientShader(GeometryMaterialInstance& geom_mi);
293  void UpdateSolidColorShader(GeometryMaterialInstance& geom_mi);
294  utils::EntityInstance<filament::TransformManager>
295  GetGeometryTransformInstance(RenderableGeometry* geom);
296  void CreateSunDirectionalLight();
297 
298  std::unordered_map<std::string, RenderableGeometry> geometries_;
299  std::unordered_map<std::string, LightEntity> lights_;
300  std::unordered_map<std::string, std::vector<std::string>> model_geometries_;
301 
302  std::string ibl_name_;
303  bool ibl_enabled_ = false;
304  bool skybox_enabled_ = false;
305  std::weak_ptr<filament::IndirectLight> indirect_light_;
306  std::weak_ptr<filament::Skybox> skybox_;
307  std::weak_ptr<filament::Skybox> color_skybox_;
308  LightEntity sun_;
309 };
310 
311 } // namespace rendering
312 } // namespace visualization
313 } // namespace open3d
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:554
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:395
Definition: FilamentResourceManager.h:62
math::float4 color
Definition: LineSetBuffers.cpp:63
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:93
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:205