Use Mipmap level to sample specular texture
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / controls / scene-view / scene-view-impl.h
1 #ifndef DALI_SCENE3D_INTERNAL_SCENE_VIEW_H
2 #define DALI_SCENE3D_INTERNAL_SCENE_VIEW_H
3
4 /*
5  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali-toolkit/internal/visuals/image/image-visual.h>
23 #include <dali-toolkit/public-api/controls/control-impl.h>
24 #include <dali/public-api/actors/camera-actor.h>
25 #include <dali/public-api/actors/layer.h>
26 #include <dali/public-api/adaptor-framework/window.h>
27 #include <dali/public-api/animation/animation.h>
28 #include <dali/public-api/object/weak-handle.h>
29 #include <dali/public-api/render-tasks/render-task.h>
30 #include <dali/public-api/rendering/frame-buffer.h>
31 #include <dali/public-api/rendering/texture.h>
32
33 // INTERNAL INCLUDES
34 #include <dali-scene3d/internal/common/environment-map-load-task.h>
35 #include <dali-scene3d/internal/common/image-based-light-observer.h>
36 #include <dali-scene3d/public-api/controls/scene-view/scene-view.h>
37
38 namespace Dali
39 {
40 namespace Scene3D
41 {
42 class SceneView;
43
44 namespace Internal
45 {
46 /**
47  * @brief Impl class for SceneView.
48  */
49 class SceneView : public Dali::Toolkit::Internal::Control
50 {
51 public:
52   /**
53    * @brief Creates a new SceneView.
54    *
55    * @return A public handle to the newly allocated SceneView.
56    */
57   static Dali::Scene3D::SceneView New();
58
59   /**
60    * @copydoc SceneView::AddCamera()
61    */
62   void AddCamera(Dali::CameraActor camera);
63
64   /**
65    * @copydoc SceneView::RemoveCamera()
66    */
67   void RemoveCamera(CameraActor camera);
68
69   /**
70    * @copydoc SceneView::GetCameraCount()
71    */
72   uint32_t GetCameraCount() const;
73
74   /**
75    * @copydoc SceneView::GetSelectedCamera()
76    */
77   CameraActor GetSelectedCamera() const;
78
79   /**
80    * @copydoc SceneView::GetCamera()
81    */
82   CameraActor GetCamera(uint32_t index) const;
83
84   /**
85    * @copydoc SceneView::GetCamera()
86    */
87   CameraActor GetCamera(const std::string& name) const;
88
89   /**
90    * @copydoc SceneView::SelectCamera()
91    */
92   void SelectCamera(uint32_t index);
93
94   /**
95    * @copydoc SceneView::SelectCamera()
96    */
97   void SelectCamera(const std::string& name);
98
99   /**
100    * @brief Register an item.
101    *
102    * Some works(e.g, lighting) of SceneView should be propagated to the child 3D items.
103    * SceneView can avoid unnecessary tree traversal to find 3D items by storing only 3D items as a list.
104    *
105    * @param[in] item scene observer to be registered.
106    */
107   void RegisterSceneItem(Scene3D::Internal::ImageBasedLightObserver* item);
108
109   /**
110    * @brief Unregister an item
111    *
112    * @param[in] item scene observer to be unregistered.
113    */
114   void UnregisterSceneItem(Scene3D::Internal::ImageBasedLightObserver* item);
115
116   /**
117    * @copydoc SceneView::SetImageBasedLightSource()
118    */
119   void SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor);
120
121   /**
122    * @copydoc SceneView::SetImageBasedLightScaleFactor()
123    */
124   void SetImageBasedLightScaleFactor(float scaleFactor);
125
126   /**
127    * @copydoc SceneView::GetImageBasedLightScaleFactor()
128    */
129   float GetImageBasedLightScaleFactor() const;
130
131   /**
132    * @copydoc SceneView::UseFramebuffer()
133    */
134   void UseFramebuffer(bool useFramebuffer);
135
136   /**
137    * @copydoc SceneView::IsUsingFramebuffer()
138    */
139   bool IsUsingFramebuffer() const;
140
141   /**
142    * @copydoc SceneView::SetFramebufferMultiSamplingLevel()
143    */
144   void SetFramebufferMultiSamplingLevel(uint8_t multiSamplingLevel);
145
146   /**
147    * @copydoc SceneView::GetFramebufferMultiSamplingLevel()
148    */
149   uint8_t GetFramebufferMultiSamplingLevel() const;
150
151   /**
152    * @copydoc SceneView::SetSkybox()
153    */
154   void SetSkybox(const std::string& skyboxUrl);
155
156   /**
157    * @copydoc SceneView::SetSkyboxEnvironmentMapType()
158    */
159   void SetSkyboxEnvironmentMapType(Scene3D::EnvironmentMapType skyboxEnvironmentMapType);
160
161   /**
162    * @copydoc SceneView::SetSkyboxIntensity()
163    */
164   void SetSkyboxIntensity(float intensity);
165
166   /**
167    * @copydoc SceneView::GetSkyboxIntensity()
168    */
169   float GetSkyboxIntensity() const;
170
171   /**
172    * @copydoc SceneView::SetSkyboxOrientation()
173    */
174   void SetSkyboxOrientation(const Quaternion& orientation);
175
176   /**
177    * @copydoc SceneView::GetSkyboxOrientation()
178    */
179   Quaternion GetSkyboxOrientation() const;
180
181 protected:
182   /**
183    * @brief Constructs a new SceneView.
184    */
185   SceneView();
186
187   /**
188    * A reference counted object may only be deleted by calling Unreference()
189    */
190   virtual ~SceneView();
191
192 private:
193   /**
194    * @copydoc CustomActorImpl::OnSceneConnection()
195    */
196   void OnSceneConnection(int depth) override;
197
198   /**
199    * @copydoc CustomActorImpl::OnSceneDisconnection()
200    */
201   void OnSceneDisconnection() override;
202
203   /**
204    * @copydoc Toolkit::Control::OnInitialize()
205    */
206   void OnInitialize() override;
207
208   /**
209    * @copydoc Toolkit::Control::OnChildAdd()
210    */
211   void OnChildAdd(Actor& child) override;
212
213   /**
214    * @copydoc Toolkit::Control::OnChildRemove()
215    */
216   void OnChildRemove(Actor& child) override;
217
218   /**
219    * @copydoc Toolkit::Control::GetHeightForWidth()
220    */
221   float GetHeightForWidth(float width) override;
222
223   /**
224    * @copydoc Toolkit::Control::GetWidthForHeight()
225    */
226   float GetWidthForHeight(float height) override;
227
228   /**
229    * @copydoc Toolkit::Control::OnRelayout()
230    */
231   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
232
233   /**
234    * @copydoc Toolkit::Control::IsResourceReady()
235    */
236   bool IsResourceReady() const override;
237
238   /**
239    * @brief Changes main camera as a input camera
240    *
241    * @param camera CameraActor that will be a main camera of the SceneView
242    */
243   void UpdateCamera(CameraActor camera);
244
245   /**
246    * @brief Updates RenderTask to use selected camera and to make framebuffer
247    */
248   void UpdateRenderTask();
249
250   /**
251    * @brief Callback that will be called when window is resized.
252    */
253   void OnWindowResized(Window window, Window::WindowSize size);
254
255   /**
256    * @brief Updates camera's projection orientation according to the screen orientation.
257    */
258   void RotateCamera();
259
260   /**
261    * @brief UpdateSkybox with skybox url and skybox environment map type.
262    *
263    * @param[in] skyboxUrl image url for skybox.
264    * @param[in] skyboxEnvironmentMapType The environment type of skybox.
265    */
266   void UpdateSkybox(const std::string& skyboxUrl, Scene3D::EnvironmentMapType skyboxEnvironmentMapType);
267
268   /**
269    * @brief Asynchronously skybox loading finished.
270    */
271   void OnSkyboxLoadComplete();
272
273   /**
274    * @brief Asynchronously ibl diffusel image loading finished.
275    */
276   void OnIblDiffuseLoadComplete();
277
278   /**
279    * @brief Asynchronously ibl specular image loading finished.
280    */
281   void OnIblSpecularLoadComplete();
282
283   /**
284    * @brief Asynchronously ibl loading finished.
285    */
286   void OnIblLoadComplete();
287
288   /**
289    * @brief Notify the changes of Ibl textures to the child items.
290    */
291   void NotifyImageBasedLightTextureChange();
292
293 private:
294   Toolkit::Visual::Base mVisual;
295
296   /////////////////////////////////////////////////////////////
297   // FrameBuffer and Rendertask to render child objects as a 3D Scene
298   Dali::WeakHandle<Dali::Window>                           mWindow;
299   CameraActor                                              mDefaultCamera;
300   CameraActor                                              mSelectedCamera;
301   std::vector<CameraActor>                                 mCameras;
302   std::vector<Scene3D::Internal::ImageBasedLightObserver*> mItems;
303   Dali::FrameBuffer                                        mFrameBuffer;
304   Dali::Texture                                            mTexture;
305   Dali::RenderTask                                         mRenderTask;
306   Layer                                                    mRootLayer;
307   int32_t                                                  mWindowOrientation;
308   Dali::Actor                                              mSkybox;
309   Quaternion                                               mSkyboxOrientation;
310   float                                                    mSkyboxIntensity{1.0f};
311   uint8_t                                                  mFrameBufferMultiSamplingLevel{0u};
312
313   // Asynchronous Loading.
314   EnvironmentMapLoadTaskPtr mSkyboxLoadTask;
315   EnvironmentMapLoadTaskPtr mIblDiffuseLoadTask;
316   EnvironmentMapLoadTaskPtr mIblSpecularLoadTask;
317   std::string               mSkyboxUrl;
318   std::string               mDiffuseIblUrl;
319   std::string               mSpecularIblUrl;
320
321   Scene3D::EnvironmentMapType mSkyboxEnvironmentMapType{Scene3D::EnvironmentMapType::AUTO};
322   Dali::Texture               mSkyboxTexture;
323   Dali::Texture               mDiffuseTexture;
324   Dali::Texture               mSpecularTexture;
325   float                       mIblScaleFactor{1.0f};
326   uint32_t                    mSpecularMipmapLevels{1u};
327   bool                        mUseFrameBuffer{false};
328   bool                        mSkyboxResourceReady{true};
329   bool                        mIblDiffuseResourceReady{true};
330   bool                        mIblSpecularResourceReady{true};
331   bool                        mSkyboxDirty{false};
332   bool                        mIblDiffuseDirty{false};
333   bool                        mIblSpecularDirty{false};
334
335   // TODO : Light Source
336 };
337
338 } // namespace Internal
339
340 // Helpers for public-api forwarding methods
341 inline Dali::Scene3D::Internal::SceneView& GetImpl(Dali::Scene3D::SceneView& obj)
342 {
343   DALI_ASSERT_ALWAYS(obj);
344   Dali::RefObject& handle = obj.GetImplementation();
345   return static_cast<Dali::Scene3D::Internal::SceneView&>(handle);
346 }
347
348 inline const Dali::Scene3D::Internal::SceneView& GetImpl(const Dali::Scene3D::SceneView& obj)
349 {
350   DALI_ASSERT_ALWAYS(obj);
351   const Dali::RefObject& handle = obj.GetImplementation();
352   return static_cast<const Dali::Scene3D::Internal::SceneView&>(handle);
353 }
354
355 } // namespace Scene3D
356
357 } // namespace Dali
358
359 #endif // DALI_SCENE3D_INTERNAL_SCENE_VIEW_H