1762f46efbefdfe4f7e7f375ad039d76c950df26
[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::DoInitialize
108    */
109   virtual void DoInitialize( Actor& actor, const Property::Map& propertyMap );
110
111   /**
112    * @copydoc Visual::Base::DoSetOnStage
113    */
114   virtual void DoSetOnStage( Actor& actor );
115
116 public:
117
118   /**
119    * Declare whether a texture map should be used for the object, if it's present. Defaults to true.
120    * @param[in] useTexture boolean declaration.
121    */
122   void SetUseTexture( bool useTexture );
123
124   /**
125    * Declare whether a normal map should be used for the object, if it's present. Defaults to true.
126    * @param[in] useNormalMap boolean declaration.
127    */
128   void SetUseNormalMap( bool useNormalMap );
129
130 private:
131
132   /**
133    * @brief Provide an empty geometry for the visual to use.
134    * @details For use in error cases where the initialisation has failed for varying reasons.
135    */
136   void SupplyEmptyGeometry();
137
138   /**
139    * @brief Initialize the visual with the geometry and shader from the cache, if not available, create and save to the cache for sharing.
140    */
141   void InitializeRenderer();
142
143   /**
144    * @brief Create a shader for the object to use.
145    */
146   void CreateShader();
147
148   /**
149    * @brief Update shader related info, uniforms, etc. for the new shader.
150    */
151   void UpdateShaderUniforms();
152
153   /**
154    * @brief Use the object URL stored in the mesh visual to load and create the geometry of the object.
155    * @return Boolean of success of operation.
156    */
157   bool CreateGeometry();
158
159   /**
160    * @brief Use the object URL stored in the visual to load the geometry of the object.
161    * @return Boolean of success of operation.
162    */
163   bool LoadGeometry();
164
165   /**
166    * @brief Use the material URL stored in the mesh visual to load the material of the object.
167    * @return Boolean of success of operation.
168    */
169   bool LoadMaterial();
170
171   /**
172    * @brief Use the image and texture URL components to load the different types of texture.
173    * @return Boolean of success of operation. Returns false if any texture fails to load from a url.
174    */
175   bool LoadTextures();
176
177 private:
178
179   // Undefined
180   MeshVisual( const MeshVisual& meshVisual );
181
182   // Undefined
183   MeshVisual& operator=( const MeshVisual& meshVisual );
184
185 private:
186
187   std::string mObjectUrl;
188   std::string mMaterialUrl;
189
190   std::string mDiffuseTextureUrl;
191   std::string mNormalTextureUrl;
192   std::string mGlossTextureUrl;
193   std::string mTexturesPath;
194
195   Shader mShader;
196   Geometry mGeometry;
197   TextureSet mTextureSet;
198
199   ObjLoader mObjLoader;
200   Vector3 mSceneCenter;
201   Vector3 mSceneSize;
202
203   Vector3 mLightPosition;
204   Toolkit::MeshVisual::ShadingMode::Value mShadingMode;
205
206   bool mUseTexture;
207   bool mUseMipmapping;
208   bool mUseSoftNormals;
209 };
210
211 } // namespace Internal
212
213 } // namespace Toolkit
214
215 } // namespace Dali
216
217 #endif /* DALI_TOOLKIT_INTERNAL_MESH_VISUAL_H */