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