Open3D (C++ API)  0.16.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) 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
36namespace filament {
37class Engine;
38class Renderer;
39class Scene;
40class SwapChain;
41class VertexBuffer;
42} // namespace filament
44
45namespace open3d {
46namespace visualization {
47namespace rendering {
48
49class FilamentMaterialModifier;
50class FilamentRenderToBuffer;
51class FilamentResourceManager;
52class FilamentScene;
53class FilamentView;
54
55class FilamentRenderer : public Renderer {
56public:
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;
87 const MaterialHandle& material) override;
90 void RemoveMaterialInstance(const MaterialInstanceHandle& id) override;
91
93 bool srgb = false) override;
94 TextureHandle AddTexture(const std::shared_ptr<geometry::Image> image,
95 bool srgb = false) override;
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,
103 bool srgb) override;
104 void RemoveTexture(const TextureHandle& id) override;
105
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
123private:
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
std::shared_ptr< core::Tensor > image
Definition: FilamentRenderer.cpp:202
std::function< void(std::shared_ptr< core::Tensor >)> callback
Definition: FilamentRenderer.cpp:201
math::float4 color
Definition: LineSetBuffers.cpp:64
The Image class stores image with customizable rows, cols, channels, dtype and device.
Definition: Image.h:48
Definition: FilamentRenderToBuffer.h:48
virtual void SetClearColor(const Eigen::Vector4f &color) override
Definition: FilamentRenderer.cpp:120
SkyboxHandle AddSkybox(const ResourceLoadRequest &request) override
Definition: FilamentRenderer.cpp:331
filament::Renderer * GetNative()
Definition: FilamentRenderer.h:121
void SetOnAfterDraw(std::function< void()> callback) override
Definition: FilamentRenderer.cpp:131
SceneHandle CreateScene() override
Definition: FilamentRenderer.cpp:99
void RemoveSkybox(const SkyboxHandle &id) override
Definition: FilamentRenderer.cpp:341
TextureHandle AddTexture(const ResourceLoadRequest &request, bool srgb=false) override
Definition: FilamentRenderer.cpp:288
bool UpdateTexture(TextureHandle texture, const std::shared_ptr< geometry::Image > image, bool srgb) override
Definition: FilamentRenderer.cpp:299
void BeginFrame() override
Definition: FilamentRenderer.cpp:147
MaterialModifier & ModifyMaterial(const MaterialHandle &id) override
Definition: FilamentRenderer.cpp:248
void UpdateSwapChain() override
Definition: FilamentRenderer.cpp:135
void RequestReadPixels(int width, int height, std::function< void(std::shared_ptr< core::Tensor >)> callback) override
Definition: FilamentRenderer.cpp:217
void UpdateBitmapSwapChain(int width, int height) override
Definition: FilamentRenderer.cpp:141
void ConvertToGuiScene(const SceneHandle &id)
Definition: FilamentRenderer.cpp:351
MaterialInstanceHandle AddMaterialInstance(const MaterialHandle &material) override
Definition: FilamentRenderer.cpp:243
std::shared_ptr< visualization::rendering::RenderToBuffer > CreateBufferRenderer() override
Definition: FilamentRenderer.cpp:345
FilamentRenderer(filament::Engine &engine, void *native_drawable, FilamentResourceManager &resource_mgr)
Definition: FilamentRenderer.cpp:69
void RemoveTexture(const TextureHandle &id) override
Definition: FilamentRenderer.cpp:312
void Draw() override
Definition: FilamentRenderer.cpp:168
void EndFrame() override
Definition: FilamentRenderer.cpp:188
FilamentScene * GetGuiScene() const
Definition: FilamentRenderer.h:119
IndirectLightHandle AddIndirectLight(const ResourceLoadRequest &request) override
Definition: FilamentRenderer.cpp:316
void RemoveIndirectLight(const IndirectLightHandle &id) override
Definition: FilamentRenderer.cpp:327
Scene * GetScene(const SceneHandle &id) const override
Definition: FilamentRenderer.cpp:107
~FilamentRenderer() override
Definition: FilamentRenderer.cpp:92
void RemoveMaterialInstance(const MaterialInstanceHandle &id) override
Definition: FilamentRenderer.cpp:283
MaterialHandle AddMaterial(const ResourceLoadRequest &request) override
Definition: FilamentRenderer.cpp:238
void DestroyScene(const SceneHandle &id) override
Definition: FilamentRenderer.cpp:116
Definition: FilamentResourceManager.h:69
Definition: MaterialModifier.h:126
int width
Definition: FilePCD.cpp:71
int height
Definition: FilePCD.cpp:72
Definition: FilamentEngine.h:31
const char const char value recording_handle imu_sample void
Definition: K4aPlugin.cpp:269
Definition: PinholeCameraIntrinsic.cpp:35