[Tizen] Scene, Scene Loader, and glTF Loader
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / scene / scene-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_SCENE_H\r
2 #define DALI_TOOLKIT_INTERNAL_SCENE_H\r
3 \r
4 /*\r
5  * Copyright (c) 2018 Samsung Electronics Co., Ltd.\r
6  *\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  * http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  *\r
19  */\r
20 \r
21 // EXTERNAL INCLUDES\r
22 #include <cstring>\r
23 #include <dali/public-api/object/base-object.h>\r
24 #include <dali/devel-api/adaptor-framework/image-loading.h>\r
25 #include <dali/devel-api/adaptor-framework/file-loader.h>\r
26 \r
27 // INTERNAL INCLUDES\r
28 #include <dali-toolkit/devel-api/controls/scene/scene.h>\r
29 #include <dali-toolkit/internal/controls/scene/gltf-loader.h>\r
30 #include <dali-toolkit/public-api/controls/control-impl.h>\r
31 \r
32 namespace Dali\r
33 {\r
34 \r
35 namespace Toolkit\r
36 {\r
37 \r
38 class Scene;\r
39 \r
40 namespace Internal\r
41 {\r
42 \r
43 class GltfLoader;\r
44 \r
45 namespace\r
46 {\r
47 \r
48 // glTF file extension\r
49 const std::string GLTF_EXT( ".gltf" );\r
50 \r
51 /**\r
52  * cube map face index\r
53  */\r
54 const int cubeMap_index_x[2][6] = { { 2, 0, 1, 1, 1, 3 },{ 0, 1, 2, 3, 4, 5 } };\r
55 const int cubeMap_index_y[2][6] = { { 1, 1, 0, 2, 1, 1 },{ 0, 0, 0, 0, 0, 0 } };\r
56 \r
57 } // namespace\r
58 \r
59 /**\r
60  * Scene implementation class\r
61  */\r
62 class Scene : public Control\r
63 {\r
64 public:\r
65 \r
66   enum CubeType\r
67   {\r
68     CROSS_HORIZENTAL = 0,                 // Cross horizental style cube map\r
69     ARRAY_HORIZENTAL,                     // array horizental style cube map\r
70     NONE\r
71   };\r
72 \r
73 \r
74   /**\r
75    * @copydoc Dali::Toolkit::Scene::Scene\r
76    */\r
77   Scene();\r
78 \r
79   /**\r
80    * @copydoc Dali::Toolkit::Scene::~Scene\r
81    */\r
82   virtual ~Scene();\r
83 \r
84   /**\r
85    * @copydoc Dali::Toolkit::Scene::New( const std::string& filePath )\r
86    */\r
87   static Dali::Toolkit::Scene New( const std::string& filePath );\r
88 \r
89   /**\r
90    * @copydoc Dali::Toolkit::Scene::New( const std::string& filePath, const std::string& diffuseTexturePath, const std::string& specularTexturePath, Vector4 ScaleFactor = Vector4( 1.0, 1.0, 1.0, 1.0 ) )\r
91    */\r
92   static Dali::Toolkit::Scene New( const std::string& filePath, const std::string& diffuseTexturePath, const std::string& specularTexturePath, Vector4 ScaleFactor = Vector4( 1.0, 1.0, 1.0, 1.0 ) );\r
93 \r
94   /**\r
95    * @copydoc Dali::Toolkit::Scene::CreateScene( const std::string& filePath )\r
96    */\r
97   bool CreateScene( const std::string& filePath );\r
98 \r
99   /**\r
100    * @copydoc Dali::Toolkit::Scene::CreateScene( const std::string& filePath )\r
101    */\r
102   uint32_t GetAnimationCount();\r
103 \r
104   /**\r
105    * @copydoc Dali::Toolkit::Scene::PlayAnimation()\r
106    */\r
107   bool PlayAnimation( uint32_t index );\r
108 \r
109   /**\r
110    * @copydoc Dali::Toolkit::Scene::PlayAnimations()\r
111    */\r
112   bool PlayAnimations();\r
113 \r
114   /**\r
115    * @copydoc Dali::Toolkit::Scene::SetLight( Toolkit::Scene::LightType type, Vector3 lightVector, Vector3 lightColor = Vector3( 1.0, 1.0, 1.0 ) )\r
116    */\r
117   bool SetLight( Toolkit::Scene::LightType type, Vector3 lightVector, Vector3 lightColor = Vector3( 1.0, 1.0, 1.0 ) );\r
118 \r
119   /**\r
120    * @brief Set default CameraActor.\r
121    * with Dali::Camera::Type = Dali::Camera::LOOK_AT_TARGET,\r
122    * near clipping plane = 0.1,\r
123    * and camera position = Vector3( 0.0, 0.0, 0.0 ).\r
124    */\r
125   bool SetDefaultCamera( const Dali::Camera::Type type = Dali::Camera::LOOK_AT_TARGET, const float nearPlane = 0.1, const Vector3 cameraPosition = Vector3( 0.0, 0.0, 0.0 ) );\r
126 \r
127   /**\r
128    * @brief Add CameraActor loaded from scene format file.\r
129    */\r
130   void AddCamera( CameraActor& cameraActor );\r
131 \r
132   /**\r
133    * @brief Add Animation loaded from scene format file.\r
134    */\r
135   void AddAnimation( Animation& animation );\r
136 \r
137   /**\r
138    * @brief Add new Shader.\r
139    * Actors can share same Shader if they use same properties.\r
140    * If a property changes in a shader, then the property of all actors that use the shader change.\r
141    */\r
142   void AddShader( Shader shader );\r
143 \r
144   /**\r
145    * @brief Get Root Actor.\r
146    */\r
147   Actor GetRoot();\r
148 \r
149   /**\r
150    * @copydoc Dali::Toolkit::Scene::GetDefaultCamera()\r
151    */\r
152   CameraActor GetDefaultCamera();\r
153 \r
154   /**\r
155    * @copydoc Dali::Toolkit::Scene::GetCamera( int cameraIndex = -1 )\r
156    */\r
157   CameraActor GetCamera( int cameraIndex = -1 );\r
158 \r
159   /**\r
160    * @brief Get light type.\r
161    */\r
162   Toolkit::Scene::LightType GetLightType();\r
163 \r
164   /**\r
165    * @brief Get light vector.\r
166    * Return light position when light type is LightType::POINT_LIGHT\r
167    * Return light direction when light type is LightType::DIRECTIONAL_LIGHT\r
168    */\r
169   Vector3 GetLightVector();\r
170 \r
171   /**\r
172    * @brief Get light color.\r
173    */\r
174   Vector3 GetLightColor();\r
175 \r
176   /**\r
177    * @brief Get Scaling factor of IBL.\r
178    */\r
179   Vector4 GetIBLScaleFactor();\r
180 \r
181   /**\r
182    * @brief Get BRDF Texture.\r
183    */\r
184   Texture GetBRDFTexture();\r
185 \r
186   /**\r
187    * @brief Get diffuse cube map texture.\r
188    */\r
189   Texture GetDiffuseTexture();\r
190 \r
191   /**\r
192    * @brief Get specular cube map texture.\r
193    */\r
194   Texture GetSpecularTexture();\r
195 \r
196 private:\r
197   /**\r
198    * @brief Crop image buffer.\r
199    * For each direction, Offset + faceSize must be width or height or less then them.\r
200    */\r
201   unsigned char* CropBuffer( unsigned char* sourceBuffer, unsigned int bytesPerPixel, unsigned int width, unsigned int height, unsigned int xOffset, unsigned int yOffset, unsigned int xFaceSize, unsigned int yFaceSize );\r
202 \r
203   /**\r
204    * @brief Upload cube map texture.\r
205    */\r
206   void UploadTextureFace( Texture& texture, Devel::PixelBuffer pixelBuffer, int faceIndex );\r
207 \r
208   /**\r
209    * @brief Set diffuse and specular cube map textures.\r
210    */\r
211   void SetCubeMap( const std::string& diffuseTexturePath, const std::string& specularTexturePath, Vector4 ScaleFactor = Vector4( 1.0, 1.0, 1.0, 1.0 ) );\r
212 \r
213   virtual void OnInitialize();\r
214 \r
215 \r
216   /**\r
217    * @brief Load 2D texture.\r
218    * @param[in] imageUrl Image URL of the texture.\r
219    * @param[in] generateMipmaps If generateMipmaps is true, then generate mipmap of this texture.\r
220    * @return Texture loaded from imageUrl.\r
221    */\r
222   Texture LoadTexture( const char *imageUrl, bool generateMipmaps );\r
223 \r
224 private:\r
225   Actor mRoot; // Root actor that contains scene graph\r
226 \r
227   std::vector<Shader> mShaderArray; // Shader Array to change properties of scene such as lighting.\r
228 \r
229   std::vector<CameraActor> mCameraActorArray; // CameraActer array loaded from scene format file.\r
230   CameraActor mDefaultCamera; // Default CameraActor for the empty mCameraActorArray.\r
231 \r
232   std::vector<Animation> mAnimationArray; // Animation array loaded from scene format file.\r
233 \r
234   Toolkit::Scene::LightType mLightType; // Light type\r
235   Vector3 mLightVector; // Light position when mLightType is LightType::POINT_LIGHT\r
236                         // Light direction when mLightType is LightType::DIRECTIONAL_LIGHT\r
237   Vector3 mLightColor; // Light color\r
238 \r
239   Vector4 mIBLScaleFactor; // IBL scaling factor for the IBL rendering\r
240   Texture mBRDFTexture; // BRDF texture for the PBR rendering\r
241   Texture mSpecularTexture; // Specular cube map texture\r
242   Texture mDiffuseTexture; // Diffuse cube map texture\r
243 \r
244 private:\r
245 \r
246   // Undefined copy constructor.\r
247   Scene( const Scene& );\r
248 \r
249   // Undefined assignment operator.\r
250   Scene& operator=( const Scene& );\r
251 };\r
252 \r
253 } // namespace Internal\r
254 \r
255   // Helpers for public-api forwarding methods\r
256 inline const Internal::Scene& GetImpl( const Toolkit::Scene& scene )\r
257 {\r
258   DALI_ASSERT_ALWAYS( scene && "Scene handle is empty" );\r
259   const Dali::RefObject& handle = scene.GetImplementation();\r
260 \r
261   return static_cast<const Toolkit::Internal::Scene&>( handle );\r
262 }\r
263 \r
264 inline Internal::Scene& GetImpl( Toolkit::Scene& scene )\r
265 {\r
266   DALI_ASSERT_ALWAYS( scene && "Scene handle is empty" );\r
267 \r
268   Dali::RefObject& handle = scene.GetImplementation();\r
269 \r
270   return static_cast<Toolkit::Internal::Scene&>( handle );\r
271 }\r
272 \r
273 }//namespace Toolkit\r
274 \r
275 }//namespace Dali\r
276 \r
277 #endif // DALI_TOOLKIT_INTERNAL_SCENE_H\r