Open3D (C++ API)  0.18.0+3975044
FilamentRenderer.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <memory>
11 #include <unordered_map>
12 #include <unordered_set>
13 
15 
17 namespace filament {
18 class Engine;
19 class Renderer;
20 class Scene;
21 class SwapChain;
22 class VertexBuffer;
23 } // namespace filament
25 
26 namespace open3d {
27 namespace visualization {
28 namespace rendering {
29 
30 class FilamentMaterialModifier;
31 class FilamentRenderToBuffer;
32 class FilamentResourceManager;
33 class FilamentScene;
34 class FilamentView;
35 
36 class FilamentRenderer : public Renderer {
37 public:
38  FilamentRenderer(filament::Engine& engine,
39  void* native_drawable,
40  FilamentResourceManager& resource_mgr);
41  // This will create an offscreen renderer
42  explicit FilamentRenderer(filament::Engine& engine,
43  int width,
44  int height,
45  FilamentResourceManager& resource_mgr);
46  ~FilamentRenderer() override;
47 
48  SceneHandle CreateScene() override;
49  Scene* GetScene(const SceneHandle& id) const override;
50  void DestroyScene(const SceneHandle& id) override;
51 
52  virtual void SetClearColor(const Eigen::Vector4f& color) override;
53  void UpdateSwapChain() override;
54  void UpdateBitmapSwapChain(int width, int height) override;
55 
56  void BeginFrame() override;
57  void Draw() override;
58  void RequestReadPixels(int width,
59  int height,
60  std::function<void(std::shared_ptr<core::Tensor>)>
61  callback) override;
62  void EndFrame() override;
63 
64  void SetOnAfterDraw(std::function<void()> callback) override;
65 
66  MaterialHandle AddMaterial(const ResourceLoadRequest& request) override;
68  const MaterialHandle& material) override;
69  MaterialModifier& ModifyMaterial(const MaterialHandle& id) override;
71  void RemoveMaterialInstance(const MaterialInstanceHandle& id) override;
72 
74  bool srgb = false) override;
75  TextureHandle AddTexture(const std::shared_ptr<geometry::Image> image,
76  bool srgb = false) override;
78  bool srgb = false) override;
79  bool UpdateTexture(TextureHandle texture,
80  const std::shared_ptr<geometry::Image> image,
81  bool srgb) override;
82  bool UpdateTexture(TextureHandle texture,
84  bool srgb) override;
85  void RemoveTexture(const TextureHandle& id) override;
86 
88  const ResourceLoadRequest& request) override;
89  void RemoveIndirectLight(const IndirectLightHandle& id) override;
90 
91  SkyboxHandle AddSkybox(const ResourceLoadRequest& request) override;
92  void RemoveSkybox(const SkyboxHandle& id) override;
93 
94  std::shared_ptr<visualization::rendering::RenderToBuffer>
95  CreateBufferRenderer() override;
96 
97  // Removes scene from scenes list and draws it last
98  // WARNING: will destroy previous gui scene if there was any
99  void ConvertToGuiScene(const SceneHandle& id);
100  FilamentScene* GetGuiScene() const { return gui_scene_.get(); }
101 
102  filament::Renderer* GetNative() { return renderer_; }
103 
104 private:
106 
107  filament::Engine& engine_;
108  filament::Renderer* renderer_ = nullptr;
109  filament::SwapChain* swap_chain_ = nullptr;
110  filament::SwapChain* swap_chain_cached_ = nullptr;
111 
112  std::unordered_map<REHandle_abstract, std::unique_ptr<FilamentScene>>
113  scenes_;
114  std::unique_ptr<FilamentScene> gui_scene_;
115 
116  std::unique_ptr<FilamentMaterialModifier> materials_modifier_;
117  FilamentResourceManager& resource_mgr_;
118 
119  std::unordered_set<std::shared_ptr<FilamentRenderToBuffer>>
120  buffer_renderers_;
121 
122  bool frame_started_ = false;
123  std::function<void()> on_after_draw_;
124  bool needs_wait_after_draw_ = false;
125 };
126 
127 } // namespace rendering
128 } // namespace visualization
129 } // namespace open3d
std::shared_ptr< core::Tensor > image
Definition: FilamentRenderer.cpp:183
std::function< void(std::shared_ptr< core::Tensor >)> callback
Definition: FilamentRenderer.cpp:182
math::float4 color
Definition: LineSetBuffers.cpp:45
The Image class stores image with customizable rows, cols, channels, dtype and device.
Definition: Image.h:29
Definition: FilamentRenderToBuffer.h:29
virtual void SetClearColor(const Eigen::Vector4f &color) override
Definition: FilamentRenderer.cpp:101
SkyboxHandle AddSkybox(const ResourceLoadRequest &request) override
Definition: FilamentRenderer.cpp:312
void SetOnAfterDraw(std::function< void()> callback) override
Definition: FilamentRenderer.cpp:112
SceneHandle CreateScene() override
Definition: FilamentRenderer.cpp:80
void RemoveSkybox(const SkyboxHandle &id) override
Definition: FilamentRenderer.cpp:322
TextureHandle AddTexture(const ResourceLoadRequest &request, bool srgb=false) override
Definition: FilamentRenderer.cpp:269
bool UpdateTexture(TextureHandle texture, const std::shared_ptr< geometry::Image > image, bool srgb) override
Definition: FilamentRenderer.cpp:280
void BeginFrame() override
Definition: FilamentRenderer.cpp:128
MaterialModifier & ModifyMaterial(const MaterialHandle &id) override
Definition: FilamentRenderer.cpp:229
void UpdateSwapChain() override
Definition: FilamentRenderer.cpp:116
void RequestReadPixels(int width, int height, std::function< void(std::shared_ptr< core::Tensor >)> callback) override
Definition: FilamentRenderer.cpp:198
void UpdateBitmapSwapChain(int width, int height) override
Definition: FilamentRenderer.cpp:122
filament::Renderer * GetNative()
Definition: FilamentRenderer.h:102
FilamentScene * GetGuiScene() const
Definition: FilamentRenderer.h:100
void ConvertToGuiScene(const SceneHandle &id)
Definition: FilamentRenderer.cpp:332
MaterialInstanceHandle AddMaterialInstance(const MaterialHandle &material) override
Definition: FilamentRenderer.cpp:224
std::shared_ptr< visualization::rendering::RenderToBuffer > CreateBufferRenderer() override
Definition: FilamentRenderer.cpp:326
FilamentRenderer(filament::Engine &engine, void *native_drawable, FilamentResourceManager &resource_mgr)
Definition: FilamentRenderer.cpp:50
void RemoveTexture(const TextureHandle &id) override
Definition: FilamentRenderer.cpp:293
void Draw() override
Definition: FilamentRenderer.cpp:149
void EndFrame() override
Definition: FilamentRenderer.cpp:169
IndirectLightHandle AddIndirectLight(const ResourceLoadRequest &request) override
Definition: FilamentRenderer.cpp:297
void RemoveIndirectLight(const IndirectLightHandle &id) override
Definition: FilamentRenderer.cpp:308
Scene * GetScene(const SceneHandle &id) const override
Definition: FilamentRenderer.cpp:88
~FilamentRenderer() override
Definition: FilamentRenderer.cpp:73
void RemoveMaterialInstance(const MaterialInstanceHandle &id) override
Definition: FilamentRenderer.cpp:264
MaterialHandle AddMaterial(const ResourceLoadRequest &request) override
Definition: FilamentRenderer.cpp:219
void DestroyScene(const SceneHandle &id) override
Definition: FilamentRenderer.cpp:97
Definition: FilamentResourceManager.h:50
Definition: MaterialModifier.h:107
int width
Definition: FilePCD.cpp:52
int height
Definition: FilePCD.cpp:53
Definition: FilamentEngine.h:12
const char const char value recording_handle imu_sample void
Definition: K4aPlugin.cpp:250
Definition: PinholeCameraIntrinsic.cpp:16