Open3D (C++ API)
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) 2019 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 #include <filament/utils/Entity.h>
30 #include <filament/utils/EntityInstance.h>
31 
32 #include <memory>
33 #include <unordered_map>
34 
36 
37 namespace filament {
38 class Engine;
39 class IndirectLight;
40 class Renderer;
41 class Scene;
42 class Skybox;
43 class TransformManager;
44 class VertexBuffer;
45 } // namespace filament
46 
47 namespace open3d {
48 namespace visualization {
49 
50 class FilamentResourceManager;
51 class FilamentView;
52 
53 class FilamentScene : public Scene {
54 public:
55  FilamentScene(filament::Engine& engine,
56  FilamentResourceManager& resource_mgr);
57  ~FilamentScene() override;
58 
59  // All views above first will discard
60  // only depth and stencil buffers by default
61  ViewHandle AddView(std::int32_t x,
62  std::int32_t y,
63  std::uint32_t w,
64  std::uint32_t h) override;
65 
66  View* GetView(const ViewHandle& view_id) const override;
67  void SetViewActive(const ViewHandle& view_id, bool is_active) override;
68  void RemoveView(const ViewHandle& view_id) override;
69 
70  GeometryHandle AddGeometry(const geometry::Geometry3D& geometry) override;
71  GeometryHandle AddGeometry(
72  const geometry::Geometry3D& geometry,
73  const MaterialInstanceHandle& material_id) override;
74  GeometryHandle AddGeometry(const geometry::Geometry3D& geometry,
75  const MaterialInstanceHandle& material_id,
76  const std::string& name) override;
77  std::vector<GeometryHandle> FindGeometryByName(
78  const std::string& name) override;
79  void AssignMaterial(const GeometryHandle& geometry_id,
80  const MaterialInstanceHandle& material_id) override;
81  MaterialInstanceHandle GetMaterial(
82  const GeometryHandle& geometry_id) const override;
83  void SetGeometryShadows(const GeometryHandle& geometry_id,
84  bool casts_shadows,
85  bool receives_shadows) override;
86  void RemoveGeometry(const GeometryHandle& geometry_id) override;
87 
88  LightHandle AddLight(const LightDescription& descr) override;
89  void SetLightIntensity(const LightHandle& id, float intensity) override;
90  void SetLightColor(const LightHandle& id,
91  const Eigen::Vector3f& color) override;
92  Eigen::Vector3f GetLightDirection(const LightHandle& id) const override;
93  void SetLightDirection(const LightHandle& id,
94  const Eigen::Vector3f& dir) override;
95  void SetLightPosition(const LightHandle& id,
96  const Eigen::Vector3f& pos) override;
97  void SetLightFalloff(const LightHandle& id, float falloff) override;
98  void RemoveLight(const LightHandle& id) override;
99 
100  void SetIndirectLight(const IndirectLightHandle& id) override;
101  void SetIndirectLightIntensity(float intensity) override;
102  float GetIndirectLightIntensity() const override;
103  void SetIndirectLightRotation(const Transform& rotation) override;
104  Transform GetIndirectLightRotation() const override;
105 
106  void SetSkybox(const SkyboxHandle& id) override;
107 
108  void SetEntityEnabled(const REHandle_abstract& entity_id,
109  bool enabled) override;
110  bool GetEntityEnabled(const REHandle_abstract& entity_id) override;
111  void SetEntityTransform(const REHandle_abstract& entity_id,
112  const Transform& transform) override;
113  Transform GetEntityTransform(const REHandle_abstract& entity_id) override;
114 
115  geometry::AxisAlignedBoundingBox GetEntityBoundingBox(
116  const REHandle_abstract& entity_id) override;
117 
118  void Draw(filament::Renderer& renderer);
119 
120  filament::Scene* GetNativeScene() const { return scene_; }
121 
122 private:
123  friend class FilamentView;
124 
125  struct SceneEntity {
126  struct Details {
127  utils::Entity self;
131 
132  bool IsValid() const { return !self.isNull(); }
133  void ReleaseResources(filament::Engine& engine,
134  FilamentResourceManager& manager);
135  } info;
136 
137  // We can disable entities removing them from scene, but not
138  // deallocating
139  bool enabled = true;
140  MaterialInstanceHandle material;
141  TextureHandle texture; // if none, default is used
142  // Used for relocating transform to center of mass
143  utils::Entity parent;
144  std::string name;
145 
146  bool IsValid() const { return info.IsValid(); }
147  void ReleaseResources(filament::Engine& engine,
148  FilamentResourceManager& manager);
149  };
150 
151  struct ViewContainer {
152  std::unique_ptr<FilamentView> view;
153  bool is_active = true;
154  };
155 
156  utils::EntityInstance<filament::TransformManager>
157  GetEntityTransformInstance(const REHandle_abstract& id);
158  void RemoveEntity(REHandle_abstract id);
159 
160  filament::Scene* scene_ = nullptr;
161 
162  filament::Engine& engine_;
163  FilamentResourceManager& resource_mgr_;
164 
165  std::unordered_map<REHandle_abstract, ViewContainer> views_;
166  std::unordered_map<REHandle_abstract, SceneEntity> entities_;
167  std::weak_ptr<filament::IndirectLight> indirect_light_;
168  std::weak_ptr<filament::Skybox> skybox_;
169 };
170 
171 } // namespace visualization
172 } // namespace open3d
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:553
Definition: FilamentResourceManager.h:59
EntityType
Definition: RendererHandle.h:48
A bounding box that is aligned along the coordinate axes.
Definition: BoundingVolume.h:164
filament::Scene * GetNativeScene() const
Definition: FilamentScene.h:120
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:394
Definition: FilamentView.h:49
math::float4 color
Definition: LineSetBuffers.cpp:46
The base geometry class for 3D geometries.
Definition: Geometry3D.h:46
IndexBufferHandle ib
Definition: FilamentScene.h:130
EntityType type
Definition: FilamentScene.h:128
VertexBufferHandle vb
Definition: FilamentScene.h:129
Definition: RendererStructs.h:42
Definition: View.h:37
Eigen::Transform< float, 3, Eigen::Affine > Transform
Definition: Scene.h:49
Definition: Open3DViewer.h:29
std::string name
Definition: FilePCD.cpp:56
Definition: FilamentScene.h:53
Definition: FilamentCamera.h:31
Definition: RendererHandle.h:71
bool IsValid() const
Definition: FilamentScene.h:132
Definition: Scene.h:47