Open3D (C++ API)  0.12.0
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) 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 <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 SetPreserveBuffer(bool preserve) override;
73  void UpdateSwapChain() override;
74 
75  void EnableCaching(bool enable) override;
76  void BeginFrame() override;
77  void Draw() override;
78  void EndFrame() override;
79 
80  MaterialHandle AddMaterial(const ResourceLoadRequest& request) override;
81  MaterialInstanceHandle AddMaterialInstance(
82  const MaterialHandle& material) override;
83  MaterialModifier& ModifyMaterial(const MaterialHandle& id) override;
84  MaterialModifier& ModifyMaterial(const MaterialInstanceHandle& id) override;
85  void RemoveMaterialInstance(const MaterialInstanceHandle& id) override;
86 
87  TextureHandle AddTexture(const ResourceLoadRequest& request,
88  bool srgb = false) override;
89  TextureHandle AddTexture(const std::shared_ptr<geometry::Image>& image,
90  bool srgb = false) override;
91  void RemoveTexture(const TextureHandle& id) override;
92 
93  IndirectLightHandle AddIndirectLight(
94  const ResourceLoadRequest& request) override;
95  void RemoveIndirectLight(const IndirectLightHandle& id) override;
96 
97  SkyboxHandle AddSkybox(const ResourceLoadRequest& request) override;
98  void RemoveSkybox(const SkyboxHandle& id) override;
99 
100  std::shared_ptr<visualization::rendering::RenderToBuffer>
101  CreateBufferRenderer() override;
102 
103  // Removes scene from scenes list and draws it last
104  // WARNING: will destroy previous gui scene if there was any
105  void ConvertToGuiScene(const SceneHandle& id);
106  FilamentScene* GetGuiScene() const { return gui_scene_.get(); }
107 
108  filament::Renderer* GetNative() { return renderer_; }
109 
110 private:
112 
113  filament::Engine& engine_;
114  filament::Renderer* renderer_ = nullptr;
115  filament::SwapChain* swap_chain_ = nullptr;
116  filament::SwapChain* swap_chain_cached_ = nullptr;
117 
118  std::unordered_map<REHandle_abstract, std::unique_ptr<FilamentScene>>
119  scenes_;
120  std::unique_ptr<FilamentScene> gui_scene_;
121 
122  std::unique_ptr<FilamentMaterialModifier> materials_modifier_;
123  FilamentResourceManager& resource_mgr_;
124 
125  std::unordered_set<FilamentRenderToBuffer*> buffer_renderers_;
126 
127  bool frame_started_ = false;
128  bool render_caching_enabled_ = false;
129  int render_count_ = 0;
130  float clear_color_[4];
131  bool preserve_buffer_ = false;
132 
133  void OnBufferRenderDestroyed(FilamentRenderToBuffer* render);
134 };
135 
136 } // namespace rendering
137 } // namespace visualization
138 } // 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
Definition: MaterialModifier.h:126
Definition: FilamentRenderToBuffer.h:48
Definition: FilamentResourceManager.h:62
FilamentScene * GetGuiScene() const
Definition: FilamentRenderer.h:106
math::float4 color
Definition: LineSetBuffers.cpp:64
filament::Renderer * GetNative()
Definition: FilamentRenderer.h:108
Definition: PinholeCameraIntrinsic.cpp:35
int height
Definition: FilePCD.cpp:72
Definition: FilamentEngine.h:31
int width
Definition: FilePCD.cpp:71