Visuals call SetProperties internally for 2-stage initialization
[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) 2016 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   virtual void DoCreatePropertyMap( Property::Map& map ) const;
77
78 protected:
79
80   /**
81    * @brief Constructor.
82    *
83    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
84    */
85   MeshVisual( VisualFactoryCache& factoryCache );
86
87   /**
88    * @brief A reference counted object may only be deleted by calling Unreference().
89    */
90   virtual ~MeshVisual();
91
92   /**
93    * @copydoc Visual::Base::DoSetProperties
94    */
95   virtual void DoSetProperties( const Property::Map& propertyMap );
96
97   /**
98    * @copydoc Visual::Base::OnSetTransform
99    */
100   virtual void OnSetTransform();
101
102   /**
103    * @copydoc Visual::Base::DoSetOnStage
104    */
105   virtual void DoSetOnStage( Actor& actor );
106
107 public:
108
109   /**
110    * Declare whether a texture map should be used for the object, if it's present. Defaults to true.
111    * @param[in] useTexture boolean declaration.
112    */
113   void SetUseTexture( bool useTexture );
114
115   /**
116    * Declare whether a normal map should be used for the object, if it's present. Defaults to true.
117    * @param[in] useNormalMap boolean declaration.
118    */
119   void SetUseNormalMap( bool useNormalMap );
120
121 private:
122
123   /**
124    * @brief Provide an empty geometry for the visual to use.
125    * @details For use in error cases where the initialisation has failed for varying reasons.
126    */
127   void SupplyEmptyGeometry();
128
129   /**
130    * @brief Initialize the visual with the geometry and shader from the cache, if not available, create and save to the cache for sharing.
131    */
132   void InitializeRenderer();
133
134   /**
135    * @brief Create a shader for the object to use.
136    */
137   void CreateShader();
138
139   /**
140    * @brief Update shader related info, uniforms, etc. for the new shader.
141    */
142   void UpdateShaderUniforms();
143
144   /**
145    * @brief Use the object URL stored in the mesh visual to load and create the geometry of the object.
146    * @return Boolean of success of operation.
147    */
148   bool CreateGeometry();
149
150   /**
151    * @brief Use the object URL stored in the visual to load the geometry of the object.
152    * @return Boolean of success of operation.
153    */
154   bool LoadGeometry();
155
156   /**
157    * @brief Use the material URL stored in the mesh visual to load the material of the object.
158    * @return Boolean of success of operation.
159    */
160   bool LoadMaterial();
161
162   /**
163    * @brief Use the image and texture URL components to load the different types of texture.
164    * @return Boolean of success of operation. Returns false if any texture fails to load from a url.
165    */
166   bool LoadTextures();
167
168 private:
169
170   // Undefined
171   MeshVisual( const MeshVisual& meshVisual );
172
173   // Undefined
174   MeshVisual& operator=( const MeshVisual& meshVisual );
175
176 private:
177
178   std::string mObjectUrl;
179   std::string mMaterialUrl;
180
181   std::string mDiffuseTextureUrl;
182   std::string mNormalTextureUrl;
183   std::string mGlossTextureUrl;
184   std::string mTexturesPath;
185
186   Shader mShader;
187   Geometry mGeometry;
188   TextureSet mTextureSet;
189
190   ObjLoader mObjLoader;
191   Vector3 mSceneCenter;
192   Vector3 mSceneSize;
193
194   Vector3 mLightPosition;
195   Toolkit::MeshVisual::ShadingMode::Value mShadingMode;
196
197   bool mUseTexture;
198   bool mUseMipmapping;
199   bool mUseSoftNormals;
200 };
201
202 } // namespace Internal
203
204 } // namespace Toolkit
205
206 } // namespace Dali
207
208 #endif /* DALI_TOOLKIT_INTERNAL_MESH_VISUAL_H */