99ed559b7e007928d7ad45539c6008e5d1579cf6
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / mesh / mesh-visual.h
1 #ifndef DALI_TOOLKIT_INTERNAL_MESH_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_MESH_VISUAL_H
3
4 /*
5  * Copyright (c) 2019 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 <fstream>
23 #include <string.h>
24 #include <dali/public-api/common/intrusive-ptr.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/public-api/visuals/mesh-visual-properties.h>
28 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
29 #include <dali-toolkit/internal/controls/model3d-view/obj-loader.h>
30
31 namespace Dali
32 {
33
34 namespace Toolkit
35 {
36
37 namespace Internal
38 {
39
40 class MeshVisual;
41 typedef IntrusivePtr< MeshVisual > MeshVisualPtr;
42
43 /**
44  * The visual which renders a 3D object to the control's quad
45  *
46  * The following Property::Map keys are required to create a MeshVisual
47  *
48  * | %Property Name  | Type        | Representing                                                          |
49  * |-----------------|-------------|-----------------------------------------------------------------------|
50  * | objectUrl       | STRING      | A URL to the .obj file                                                |
51  * | materialUrl     | STRING      | A URL to the .mtl file                                                |
52  * | texturesPath    | STRING      | A URL of the path to the texture images                               |
53  * | shadingMode     | STRING      | An enum of shading modes                                              |
54  * | useMipmapping   | BOOLEAN     | If true, use mipmaps for textures. Default true.                      |
55  * | useSoftNormals  | BOOLEAN     | If true, average normals at points for smooth textures. Default true. |
56  * | lightPosition   | VECTOR3     | The position (on stage) of the light                                  |
57  */
58 class MeshVisual: public Visual::Base
59 {
60 public:
61
62   /**
63    * @brief Create a new mesh visual.
64    *
65    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
66    * @param[in] properties A Property::Map containing settings for this visual
67    * @return A smart-pointer to the newly allocated visual.
68    */
69   static MeshVisualPtr New( VisualFactoryCache& factoryCache, const Property::Map& properties );
70
71 public:  // from Visual
72
73   /**
74    * @copydoc Visual::Base::CreatePropertyMap
75    */
76   void DoCreatePropertyMap( Property::Map& map ) const override;
77
78   /**
79    * @copydoc Visual::Base::CreateInstancePropertyMap
80    */
81   void DoCreateInstancePropertyMap( Property::Map& map ) const override;
82
83 protected:
84
85   /**
86    * @brief Constructor.
87    *
88    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
89    */
90   MeshVisual( VisualFactoryCache& factoryCache );
91
92   /**
93    * @brief A reference counted object may only be deleted by calling Unreference().
94    */
95   virtual ~MeshVisual();
96
97   /**
98    * @copydoc Visual::Base::DoSetProperties
99    */
100   void DoSetProperties( const Property::Map& propertyMap ) override;
101
102   /**
103    * @copydoc Visual::Base::OnSetTransform
104    */
105   void OnSetTransform() override;
106
107   /**
108    * @copydoc Visual::Base::DoSetOnStage
109    */
110   void DoSetOnStage( Actor& actor ) override;
111
112 public:
113
114   /**
115    * Declare whether a texture map should be used for the object, if it's present. Defaults to true.
116    * @param[in] useTexture boolean declaration.
117    */
118   void SetUseTexture( bool useTexture );
119
120   /**
121    * Declare whether a normal map should be used for the object, if it's present. Defaults to true.
122    * @param[in] useNormalMap boolean declaration.
123    */
124   void SetUseNormalMap( bool useNormalMap );
125
126 private:
127
128   /**
129    * @brief Provide an empty geometry for the visual to use.
130    * @details For use in error cases where the initialisation has failed for varying reasons.
131    */
132   void SupplyEmptyGeometry();
133
134   /**
135    * @brief Initialize the visual with the geometry and shader from the cache, if not available, create and save to the cache for sharing.
136    */
137   void InitializeRenderer();
138
139   /**
140    * @brief Create a shader for the object to use.
141    */
142   void CreateShader();
143
144   /**
145    * @brief Update shader related info, uniforms, etc. for the new shader.
146    */
147   void UpdateShaderUniforms();
148
149   /**
150    * @brief Use the object URL stored in the mesh visual to load and create the geometry of the object.
151    * @return Boolean of success of operation.
152    */
153   bool CreateGeometry();
154
155   /**
156    * @brief Use the object URL stored in the visual to load the geometry of the object.
157    * @return Boolean of success of operation.
158    */
159   bool LoadGeometry();
160
161   /**
162    * @brief Use the material URL stored in the mesh visual to load the material of the object.
163    * @return Boolean of success of operation.
164    */
165   bool LoadMaterial();
166
167   /**
168    * @brief Use the image and texture URL components to load the different types of texture.
169    * @return Boolean of success of operation. Returns false if any texture fails to load from a url.
170    */
171   bool LoadTextures();
172
173   /**
174    * Helper method to set individual values by index key.
175    * @param[in] index The index key of the value
176    * @param[in] value The value
177    */
178   void DoSetProperty( Property::Index index, const Property::Value& value );
179
180 private:
181
182   // Undefined
183   MeshVisual( const MeshVisual& meshVisual );
184
185   // Undefined
186   MeshVisual& operator=( const MeshVisual& meshVisual );
187
188 private:
189
190   std::string mObjectUrl;
191   std::string mMaterialUrl;
192
193   std::string mDiffuseTextureUrl;
194   std::string mNormalTextureUrl;
195   std::string mGlossTextureUrl;
196   std::string mTexturesPath;
197
198   Shader mShader;
199   Geometry mGeometry;
200   TextureSet mTextureSet;
201
202   ObjLoader mObjLoader;
203   Vector3 mSceneCenter;
204   Vector3 mSceneSize;
205
206   Vector3 mLightPosition;
207   Toolkit::MeshVisual::ShadingMode::Value mShadingMode;
208
209   bool mUseTexture;
210   bool mUseMipmapping;
211   bool mUseSoftNormals;
212 };
213
214 } // namespace Internal
215
216 } // namespace Toolkit
217
218 } // namespace Dali
219
220 #endif /* DALI_TOOLKIT_INTERNAL_MESH_VISUAL_H */