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