Property enum name changes in dali-core: Core changes
[platform/core/uifw/dali-core.git] / dali / public-api / geometry / mesh-data.h
1 #ifndef __DALI_MESH_DATA_H__
2 #define __DALI_MESH_DATA_H__
3
4 /*
5  * Copyright (c) 2014 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
22 // INTERNAL INCLUDES
23 #include <dali/public-api/common/dali-common.h>
24 #include <dali/public-api/math/vector4.h>
25 #include <dali/public-api/modeling/bone.h>
26 #include <dali/public-api/modeling/material.h>
27
28
29 // DECLARATION FILES
30
31
32 namespace Dali
33 {
34 class Matrix;
35
36 /**
37  * @brief The MeshData class encompasses all the data required to describe and
38  * render a 3D mesh.
39  *
40  * The mesh can have one of three geometry types: Points, Lines or Triangles.
41  * The Point type draws each vertex. The shader can control the point size.
42  * The Line type draws single pixel width lines between each specified vertex.
43  * The Triangles type draws solid color or texture between each specified
44  * vertex.
45  *
46  * When using the Points or Lines geometry type, normals aren't used.
47  *
48  * If the HasColor property is set, the vertex color is used to draw the points,
49  * lines or triangles, otherwise the material's diffuse color or texture is
50  * used. The material's diffuse alpha value is always used.
51  *
52  * When using the Triangles geometry type, the normals are mutually exclusive
53  * with vertex color. This means that shading cannot be performed if vertex
54  * color is being used.
55  *
56  * It is possible to deform the mesh using skeletal deformation. To acheive
57  * this, a number of bones can be supplied during creation. These are named
58  * actors that can be anywhere in the actor tree. Each bone has an offset
59  * matrix. A vertex in the mesh can reference up to 4 bones in the bone
60  * container with an associated weighting.
61  *
62  * When the mesh is added to the scene graph, the node for each named actor is
63  * added to an internal container. Each Update frame after the node update pass,
64  * the node's world matrix is multiplied by the associated bone's offset matrix.
65  * When rendering the mesh, these transform matrices are multiplied by the view
66  * matrix, their inverse transforms calculated and passed onto the shaders.
67  *
68  * The mesh vertex shader then applies up to 4 weighted transforms to each
69  * vertex and vertex normal.
70  */
71 class DALI_IMPORT_API MeshData
72 {
73 public:
74   static const unsigned int MAX_NUMBER_OF_BONES = 12; ///< Maximum number of bones that can affect this mesh.
75
76   struct Vertex;
77   typedef std::vector<Vertex>                 VertexContainer;    ///< Collection of vertices
78   typedef VertexContainer::iterator           VertexIter;         ///< Iterator for @ref Dali::MeshData::VertexContainer
79   typedef VertexContainer::const_iterator     VertexConstIter;    ///< Const iterator for @ref Dali::MeshData::VertexContainer
80
81   typedef unsigned short                      FaceIndex;          ///< index of one point of a polygonal face
82   typedef std::vector<FaceIndex>              FaceIndices;        ///< Collection of FaceIndex items ( The span should match the geometry type: Lines - span is 2; Triangles - span is 3 ).
83   typedef FaceIndices::iterator               FaceIndexIter;      ///< Iterator for @ref Dali::MeshData::FaceIndices
84   typedef FaceIndices::const_iterator         FaceIndexConstIter; ///< Const iterator for @ref Dali::MeshData::FaceIndices
85
86   /**
87    * @brief The type of geometry to draw
88    */
89   enum VertexGeometryType
90   {
91     POINTS,    ///< Draw only points at each vertex
92     LINES,     ///< Draw lines between specified vertices
93     TRIANGLES  ///< Draw filled triangles between specified vertices
94   };
95
96 public: // construction, destruction and initialisation
97
98   /**
99    * @brief Create a new mesh.
100    */
101   MeshData( );
102
103   /**
104    * @brief Copy constructor.
105    *
106    * @param[in] meshData object to copy
107    */
108   MeshData( const MeshData& meshData );
109
110   /**
111    * @brief Assignment operator.
112    *
113    * @param[in] rhs MeshData object to copy data from
114    * @return A reference to this
115    */
116   MeshData& operator=(const MeshData& rhs);
117
118   /**
119    * @brief Sets the vertex coords, the face indices, the bones affecting this mesh and a default
120    * material.
121    *
122    * @param[in] vertices The Vertex data (coordinates of each vertex)
123    * @param[in] faceIndices The index into the vertices buffer for each corner of each triangular face.
124    * @param[in] bones A container of Bones affecting this mesh.
125    * @param[in] material A handle to a material object.
126    */
127   void SetData( const VertexContainer& vertices,
128                 const FaceIndices&     faceIndices,
129                 const BoneContainer&   bones,
130                 const Material         material );
131
132   /**
133    * @brief Set the vertex coords and end points of each line.
134    *
135    * @param[in] vertices The vertex data (coords & color of each vertex)
136    * @param[in] lineIndices A list of vertex indices for the start & end of each line.
137    * @param[in] material A handle to a material object.
138    */
139   void SetLineData( const VertexContainer& vertices,
140                     const FaceIndices&    lineIndices,
141                     const Material        material );
142
143   /**
144    * @brief Set the vertex coords for each point.
145    *
146    * @param[in] vertices The vertex data (coords & color of each vertex)
147    * @param[in] material A handle to a material object.
148    */
149   void SetPointData( const VertexContainer& vertices,
150                      const Material         material );
151
152   /**
153    * @brief Set the vertex coords for each point.
154    *
155    * @param[in] vertices The vertex data (coords & color of each vertex)
156    */
157   void SetVertices( const VertexContainer& vertices );
158
159   /**
160    * @brief Sets the face indices.
161    *
162    * @param[in] faceIndices The index into the vertices buffer for each corner of each triangular face.or for the start and end of each line.
163    */
164   void SetFaceIndices( const FaceIndices& faceIndices );
165
166   /**
167    * @brief Add the mesh to the bounding volume.
168    *
169    * Expands a bounding volume to include the mesh
170    * @param[in,out] min   Lower bounds
171    * @param[in,out] max   Upper bounds
172    * @param[in] transform transform the mesh vertices
173    */
174   void AddToBoundingVolume(Vector4& min, Vector4& max, const Matrix& transform);
175
176   /**
177    * @brief Get the geometry type.
178    *
179    * A mesh defaults to triangles if no data has been set.
180    * @return the geometry type;
181    */
182   VertexGeometryType GetVertexGeometryType() const;
183
184   /**
185    * @brief Get the number of vertices.
186    *
187    * @return The number of vertices
188    */
189   size_t GetVertexCount() const;
190
191   /**
192    * @brief Get the vertex array.
193    *
194    * @return The vertex array
195    */
196   const VertexContainer& GetVertices() const;
197
198   /**
199    * @brief Get the number of points, lines or faces (note this is not the same as the number of face indices!).
200    *
201    * depending on the geometry type;
202    * @return Number of points, lines or faces
203    */
204   size_t GetFaceCount() const;
205
206   /**
207    * @brief Get the face index array.
208    *
209    * @return The face index array
210    */
211   const FaceIndices& GetFaces() const;
212
213   /**
214    * @brief Sets if the mesh has texture coordinates.
215    *
216    * @param hasTexCoords - True if the mesh has texture coordinates.
217    */
218   void SetHasTextureCoords(bool hasTexCoords);
219
220   /**
221    * @brief Checks if the mesh is textured.
222    *
223    * @return true if the mesh is texture mapped.
224    */
225   bool HasTextureCoords() const;
226
227   /**
228    * @brief Sets if the mesh has normals.
229    *
230    * Mutually exclusive with HasColor. Setting this to true will force the
231    * HasColor property to be set to false.
232    * @param hasNormals - True if the mesh has normals
233    */
234   void SetHasNormals(bool hasNormals);
235
236   /**
237    * @brief Checks if the mesh has normals.
238    *
239    * @return true if the mesh contains normals.
240    */
241   bool HasNormals() const;
242
243   /**
244    * @brief Sets if the mesh vertices have color.
245    *
246    * Mutually exclusive with HasNormals. Setting this to true will force the
247    * HasNormals property to be set to false.
248    * @param hasColor - True if the mesh vertices have color.
249    */
250   void SetHasColor(bool hasColor);
251
252   /**
253    * @brief Checks if the mesh vertices have color.
254    *
255    * @return true if the mesh contains colored vertices.
256    */
257   bool HasColor() const;
258
259   /**
260    * @brief Get the original material associated with this mesh.
261    *
262    * @return Handle to the material
263    */
264   Material GetMaterial() const;
265
266   /**
267    * @brief Set the default material associated with this mesh.
268    */
269   void SetMaterial( Material material );
270
271   /**
272    * @brief Get the number of bones affecting this mesh.
273    *
274    * @return The number of bones affecting this mesh
275    */
276   size_t GetBoneCount() const;
277
278   /**
279    * @brief Does this mesh have bones?.
280    *
281    * @return true if this mesh has bones.
282    */
283   bool HasBones() const;
284
285   /**
286    * @brief Get the bone container.
287    *
288    * @return the bones
289    */
290   const BoneContainer& GetBones() const;
291
292   /**
293    * @brief Get the lower bounds of the bounding box containing the vertices.
294    *
295    * @return the lower bounds
296    */
297   const Vector4& GetBoundingBoxMin() const;
298
299   /**
300    * @brief Set the lower bounds of the bounding box containing the vertices.
301    *
302    * @param bounds The lower bounds
303    */
304   void SetBoundingBoxMin(const Vector4& bounds);
305
306   /**
307    * @brief Get the upper bounds of the bounding box containing the vertices.
308    *
309    * @return the upper bounds
310    */
311   const Vector4& GetBoundingBoxMax() const;
312
313   /**
314    * @brief Set the upper bounds of the bounding box containing the vertices.
315    *
316    * @param bounds The upper bounds
317    */
318   void SetBoundingBoxMax(const Vector4& bounds);
319
320   /**
321    * @brief Destructor.
322    */
323   ~MeshData();
324
325 private:
326   VertexContainer         mVertices;          ///< The vertex data
327   FaceIndices             mFaces;             ///< Indices of triangle faces or line terminators
328   VertexGeometryType      mGeometryType;      ///< The type of geometry to draw
329   bool                    mUseTextureCoords;  ///< Whether the vertex data contains texture coords
330   bool                    mUseNormals;        ///< Whether the vertex data contains normals
331   bool                    mUseColor;          ///< Whether the vertex data contains vertex color
332   BoneContainer           mBones;             ///< Bones for skeletal deformation
333
334   Material                mMaterial;          ///< Material of the mesh
335
336   Vector4                 mMin;               ///< The lower bounds of the bounding box
337   Vector4                 mMax;               ///< The upper bounds of the bounding box
338
339 }; // class MeshData
340
341 /**
342  * @brief A vertex within a mesh, with the corresponding texture coordinate, normal and up to 4 bone influences.
343  */
344 struct MeshData::Vertex
345 {
346   static const unsigned int MAX_BONE_INFLUENCE = 4; ///< Maximum number of bones that can influence this particular vertex.
347
348   Vertex()
349   : x(0.0f),  y(0.0f),  z(0.0f),
350     u(0.0f),  v(0.0f),
351     nX(0.0f), nY(0.0f), nZ(0.0f)
352   {
353   }
354
355   /**
356    * @brief Constructor.
357    *
358    * @param[in] position The vertex position
359    * @param[in] textureCoordinates The texture coordinates
360    * @param[in] normal The surface normal
361    */
362   Vertex( const Vector3& position, const Vector2& textureCoordinates, const Vector3& normal )
363   : x(position.x),  y(position.y),  z(position.z),
364     u(textureCoordinates.x),  v(textureCoordinates.y),
365     nX(normal.x), nY(normal.y), nZ(normal.z)
366   {
367   }
368
369   // Vertices
370   float x;
371   float y;
372   float z;
373
374   // Texture coordinates
375   float u;
376   float v;
377
378   // Normals / vertex colours
379   union
380   {
381     float nX;
382     float vertexR;
383   };
384   union
385   {
386     float nY;
387     float vertexG;
388   };
389   union
390   {
391     float nZ;
392     float vertexB;
393   };
394
395   /**
396    * @brief Bone indices specify which bones have an influence over the vertex (if any).
397    */
398   unsigned char boneIndices[MAX_BONE_INFLUENCE];
399
400   /**
401    * @brief Bone weights determine the strength of the influence of each bone.
402    */
403   float boneWeights[MAX_BONE_INFLUENCE];
404 };
405
406 } // namespace Dali
407
408 #endif // __DALI_MESH_DATA_H__