Loading [MathJax]/extensions/TeX/AMSsymbols.js
Open3D (C++ API)  0.14.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
FilamentRenderer.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018-2021 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 <memory>
30 #include <unordered_map>
31 #include <unordered_set>
32 
34 
36 namespace filament {
37 class Engine;
38 class Renderer;
39 class Scene;
40 class SwapChain;
41 class VertexBuffer;
42 } // namespace filament
44 
45 namespace open3d {
46 namespace visualization {
47 namespace rendering {
48 
49 class FilamentMaterialModifier;
50 class FilamentRenderToBuffer;
51 class FilamentResourceManager;
52 class FilamentScene;
53 class FilamentView;
54 
55 class FilamentRenderer : public Renderer {
56 public:
57  FilamentRenderer(filament::Engine& engine,
58  void* native_drawable,
59  FilamentResourceManager& resource_mgr);
60  // This will create an offscreen renderer
61  explicit FilamentRenderer(filament::Engine& engine,
62  int width,
63  int height,
64  FilamentResourceManager& resource_mgr);
65  ~FilamentRenderer() override;
66 
67  SceneHandle CreateScene() override;
68  Scene* GetScene(const SceneHandle& id) const override;
69  void DestroyScene(const SceneHandle& id) override;
70 
71  virtual void SetClearColor(const Eigen::Vector4f& color) override;
72  void UpdateSwapChain() override;
73  void UpdateBitmapSwapChain(int width, int height) override;
74 
75  void BeginFrame() override;
76  void Draw() override;
77  void RequestReadPixels(int width,
78  int height,
79  std::function<void(std::shared_ptr<core::Tensor>)>
80  callback) override;
81  void EndFrame() override;
82 
83  void SetOnAfterDraw(std::function<void()> callback) override;
84 
85  MaterialHandle AddMaterial(const ResourceLoadRequest& request) override;
86  MaterialInstanceHandle AddMaterialInstance(
87  const MaterialHandle& material) override;
88  MaterialModifier& ModifyMaterial(const MaterialHandle& id) override;
89  MaterialModifier& ModifyMaterial(const MaterialInstanceHandle& id) override;
90  void RemoveMaterialInstance(const MaterialInstanceHandle& id) override;
91 
92  TextureHandle AddTexture(const ResourceLoadRequest& request,
93  bool srgb = false) override;
94  TextureHandle AddTexture(const std::shared_ptr<geometry::Image> image,
95  bool srgb = false) override;
96  TextureHandle AddTexture(const t::geometry::Image& image,
97  bool srgb = false) override;
98  bool UpdateTexture(TextureHandle texture,
99  const std::shared_ptr<geometry::Image> image,
100  bool srgb) override;
101  bool UpdateTexture(TextureHandle texture,
102  const t::geometry::Image& image,
103  bool srgb) override;
104  void RemoveTexture(const TextureHandle& id) override;
105 
106  IndirectLightHandle AddIndirectLight(
107  const ResourceLoadRequest& request) override;
108  void RemoveIndirectLight(const IndirectLightHandle& id) override;
109 
110  SkyboxHandle AddSkybox(const ResourceLoadRequest& request) override;
111  void RemoveSkybox(const SkyboxHandle& id) override;
112 
113  std::shared_ptr<visualization::rendering::RenderToBuffer>
114  CreateBufferRenderer() override;
115 
116  // Removes scene from scenes list and draws it last
117  // WARNING: will destroy previous gui scene if there was any
118  void ConvertToGuiScene(const SceneHandle& id);
119  FilamentScene* GetGuiScene() const { return gui_scene_.get(); }
120 
121  filament::Renderer* GetNative() { return renderer_; }
122 
123 private:
125 
126  filament::Engine& engine_;
127  filament::Renderer* renderer_ = nullptr;
128  filament::SwapChain* swap_chain_ = nullptr;
129  filament::SwapChain* swap_chain_cached_ = nullptr;
130 
131  std::unordered_map<REHandle_abstract, std::unique_ptr<FilamentScene>>
132  scenes_;
133  std::unique_ptr<FilamentScene> gui_scene_;
134 
135  std::unique_ptr<FilamentMaterialModifier> materials_modifier_;
136  FilamentResourceManager& resource_mgr_;
137 
138  std::unordered_set<std::shared_ptr<FilamentRenderToBuffer>>
139  buffer_renderers_;
140 
141  bool frame_started_ = false;
142  std::function<void()> on_after_draw_;
143  bool needs_wait_after_draw_ = false;
144 };
145 
146 } // namespace rendering
147 } // namespace visualization
148 } // 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:63
int height
Definition: FilePCD.cpp:72
Definition: MaterialModifier.h:126
Definition: FilamentRenderToBuffer.h:48
Definition: FilamentResourceManager.h:69
FilamentScene * GetGuiScene() const
Definition: FilamentRenderer.h:119
The Image class stores image with customizable rows, cols, channels, dtype and device.
Definition: Image.h:48
math::float4 color
Definition: LineSetBuffers.cpp:64
filament::Renderer * GetNative()
Definition: FilamentRenderer.h:121
Definition: PinholeCameraIntrinsic.cpp:35
Definition: FilamentEngine.h:31
int width
Definition: FilePCD.cpp:71
std::shared_ptr< core::Tensor > image
Definition: FilamentRenderer.cpp:228
std::function< void(std::shared_ptr< core::Tensor >)> callback
Definition: FilamentRenderer.cpp:227