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