Merge "FocusFinder use CalculateCurrentScreenExtents" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / model-components / model-primitive-impl.h
1 #ifndef DALI_SCENE3D_MODEL_COMPONENTS_MODEL_PRIMITIVE_IMPL_H
2 #define DALI_SCENE3D_MODEL_COMPONENTS_MODEL_PRIMITIVE_IMPL_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/public-api/common/dali-common.h>
23 #include <dali/public-api/common/intrusive-ptr.h>
24 #include <dali/public-api/object/base-object.h>
25 #include <dali/public-api/object/property-value.h>
26 #include <dali/public-api/object/property.h>
27 #include <set>
28
29 // INTERNAL INCLUDES
30 #include <dali-scene3d/internal/model-components/material-modify-observer.h>
31 #include <dali-scene3d/internal/model-components/model-primitive-modify-observer.h>
32 #include <dali-scene3d/public-api/light/light.h>
33 #include <dali-scene3d/public-api/loader/blend-shape-details.h>
34 #include <dali-scene3d/public-api/loader/mesh-definition.h>
35 #include <dali-scene3d/public-api/model-components/material.h>
36 #include <dali-scene3d/public-api/model-components/model-primitive.h>
37
38 namespace Dali
39 {
40 namespace Scene3D
41 {
42 namespace Internal
43 {
44 using ModelPrimitivePtr = IntrusivePtr<ModelPrimitive>;
45
46 /**
47  * @brief TODO : Explain me.
48  * Use Vector4 Tangent data
49  * Same ModelPrimitive all shares IBL
50  */
51 class ModelPrimitive : public BaseObject, public MaterialModifyObserver
52 {
53 private:
54   using ModelPrimitiveModifyObserverContainer = std::set<ModelPrimitiveModifyObserver*>;
55
56 public:
57   // Creation & Destruction
58   /**
59    * @brief Create a new ModelPrimitive object.
60    * @return A smart-pointer to the newly allocated ModelPrimitive.
61    */
62   static ModelPrimitivePtr New();
63
64 protected:
65   /**
66    * @brief Construct a new ModelPrimitive.
67    */
68   ModelPrimitive();
69
70   /**
71    * @brief Second-phase constructor.
72    */
73   void Initialize();
74
75   /**
76    * @brief Virtual destructor.
77    */
78   virtual ~ModelPrimitive();
79
80 public:
81   /**
82    * @brief Set Renderer that is created by Scene3D::Loader internally.
83    */
84   void SetRenderer(Dali::Renderer renderer);
85
86   /**
87    * @copydoc Dali::Scene3D::ModelPrimitive::GetRenderer()
88    */
89   Dali::Renderer GetRenderer() const;
90
91   /**
92    * @copydoc Dali::Scene3D::ModelPrimitive::SetGeometry()
93    */
94   void SetGeometry(Dali::Geometry geometry);
95
96   /**
97    * @copydoc Dali::Scene3D::ModelPrimitive::GetGeometry()
98    */
99   Dali::Geometry GetGeometry() const;
100
101   /**
102    * @copydoc Dali::Scene3D::ModelPrimitive::SetMaterial()
103    */
104   void SetMaterial(Dali::Scene3D::Material material, bool updateRenderer = true);
105
106   /**
107    * @copydoc Dali::Scene3D::ModelPrimitive::GetMaterial()
108    */
109   Dali::Scene3D::Material GetMaterial() const;
110
111   /**
112    * @brief Adds a primitive observer to this model primitive.
113    *
114    * @param[in] observer The observer to add.
115    */
116   void AddPrimitiveObserver(ModelPrimitiveModifyObserver* observer);
117
118   /**
119    * @brief Removes a primitive observer from this model primitive.
120    *
121    * @param[in] observer The observer to remove.
122    */
123   void RemovePrimitiveObserver(ModelPrimitiveModifyObserver* observer);
124
125   /**
126    * @brief Sets the image-based lighting texture for this model primitive.
127    *
128    * @param[in] diffuseTexture The diffuse texture.
129    * @param[in] specularTexture The specular texture.
130    * @param[in] iblScaleFactor The scale factor to set for image-based lighting.
131    * @param[in] specularMipmapLevels The number of mipmap levels of specular texture.
132    */
133   void SetImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float iblScaleFactor, uint32_t specularMipmapLevels);
134
135   /**
136    * @brief Sets the scale factor for image-based lighting for this model primitive.
137    *
138    * @param[in] iblScaleFactor The scale factor to set for image-based lighting.
139    */
140   void SetImageBasedLightScaleFactor(float iblScaleFactor);
141
142   /**
143    * @brief Adds a Light on this medel primitive
144    *
145    * @param[in] light Added light.
146    * @param[in] lightIndex Index of added light.
147    */
148   void AddLight(Scene3D::Light light, uint32_t lightIndex);
149
150   /**
151    * @brief Removes a light at the lightIndex of this model primitive
152    *
153    * @param[in] lightIndex Index of added light.
154    */
155   void RemoveLight(uint32_t lightIndex);
156
157   /**
158    * @brief Sets the blend shape data for this model primitive.
159    *
160    * @param[in] data The blend shape data to set.
161    */
162   void SetBlendShapeData(Scene3D::Loader::BlendShapes::BlendShapeData& data);
163
164   /**
165    * @brief Sets the blend shape geometry for this model primitive.
166    *
167    * @param[in] blendShapeGeometry The blend shape geometry to set.
168    */
169   void SetBlendShapeGeometry(Dali::Texture blendShapeGeometry);
170
171   /**
172    * @brief Sets the blend shape options for this model primitive.
173    *
174    * @param[in] hasPositions Whether or not this model primitive has positions for blend shapes.
175    * @param[in] hasNormals Whether or not this model primitive has normals for blend shapes.
176    * @param[in] hasTangents Whether or not this model primitive has tangents for blend shapes.
177    */
178   void SetBlendShapeOptions(bool hasPositions, bool hasNormals, bool hasTangents);
179
180   /**
181    * @brief Sets whether or not this model primitive is skinned.
182    *
183    * @param[in] isSkinned Whether or not this model primitive is skinned.
184    */
185   void SetSkinned(bool isSkinned);
186
187 private: // From MaterialModifyObserver
188   /**
189    * @copydoc Dali::Scene3D::Internal::Material::MaterialModifyObserver::OnMaterialModified()
190    */
191   void OnMaterialModified(Dali::Scene3D::Material material, MaterialModifyObserver::ModifyFlag flag) override;
192
193 private:
194   /**
195    * @brief Apply materials data into renderer.
196    */
197   void ApplyMaterialToRenderer(MaterialModifyObserver::ModifyFlag flag = MaterialModifyObserver::ModifyFlag::NONE);
198
199   /**
200    * @brief Updates the uniform of renderer.
201    */
202   void UpdateRendererUniform();
203
204   /**
205    * @brief Creates a renderer.
206    */
207   void CreateRenderer();
208
209   /**
210    * @brief Updates the image-based lighting texture.
211    */
212   void UpdateImageBasedLightTexture();
213
214 private:
215   // Delete copy & move operator
216   ModelPrimitive(const ModelPrimitive&)                    = delete;
217   ModelPrimitive(ModelPrimitive&&)                         = delete;
218   ModelPrimitive& operator=(const ModelPrimitive& rhs)     = delete;
219   ModelPrimitive& operator=(ModelPrimitive&& rhs) noexcept = delete;
220
221 private:
222   ModelPrimitiveModifyObserverContainer mObservers{};
223
224   // For Renderer
225   Dali::Renderer          mRenderer;
226   Dali::Geometry          mGeometry;
227   Dali::Shader            mShader;
228   Dali::TextureSet        mTextureSet;
229   Dali::Scene3D::Material mMaterial;
230
231   // Light
232   std::vector<Scene3D::Light> mLights;
233   int32_t                     mLightCount{0};
234
235   // For IBL
236   Dali::Texture mSpecularTexture;
237   Dali::Texture mDiffuseTexture;
238   float         mIblScaleFactor{1.0f};
239   uint32_t      mSpecularMipmapLevels{1u};
240
241   // For blend shape
242   Scene3D::Loader::BlendShapes::BlendShapeData mBlendShapeData;
243   Dali::Texture                                mBlendShapeGeometry;
244   bool                                         mHasSkinning  = false;
245   bool                                         mHasPositions = false;
246   bool                                         mHasNormals   = false;
247   bool                                         mHasTangents  = false;
248
249   bool mIsMaterialChanged        = false;
250   bool mNeedToSetRendererUniform = false;
251 };
252
253 } // namespace Internal
254
255 // Helpers for public-api forwarding methods
256
257 inline Internal::ModelPrimitive& GetImplementation(Dali::Scene3D::ModelPrimitive& modelPrimitive)
258 {
259   DALI_ASSERT_ALWAYS(modelPrimitive && "ModelPrimitive handle is empty");
260
261   BaseObject& handle = modelPrimitive.GetBaseObject();
262
263   return static_cast<Internal::ModelPrimitive&>(handle);
264 }
265
266 inline const Internal::ModelPrimitive& GetImplementation(const Dali::Scene3D::ModelPrimitive& modelPrimitive)
267 {
268   DALI_ASSERT_ALWAYS(modelPrimitive && "ModelPrimitive handle is empty");
269
270   const BaseObject& handle = modelPrimitive.GetBaseObject();
271
272   return static_cast<const Internal::ModelPrimitive&>(handle);
273 }
274
275 } // namespace Scene3D
276
277 } // namespace Dali
278
279 #endif // DALI_SCENE3D_MODEL_COMPONENTS_MODEL_PRIMITIVE_IMPL_H