1 #ifndef __DALI_MESH_DATA_H__
2 #define __DALI_MESH_DATA_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
32 namespace Dali DALI_IMPORT_API
37 * @brief The MeshData class encompasses all the data required to describe and
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
46 * When using the Points or Lines geometry type, normals aren't used.
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.
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.
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.
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.
68 * The mesh vertex shader then applies up to 4 weighted transforms to each
69 * vertex and vertex normal.
74 static const unsigned int MAX_NUMBER_OF_BONES = 12; ///< Maximum number of bones that can affect this mesh.
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
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
87 * @brief The type of geometry to draw
89 enum VertexGeometryType
91 POINTS, ///< Draw only points at each vertex
92 LINES, ///< Draw lines between specified vertices
93 TRIANGLES ///< Draw filled triangles between specified vertices
96 public: // construction, destruction and initialisation
99 * @brief Create a new mesh.
104 * @brief Copy constructor.
106 * @param[in] meshData object to copy
108 explicit MeshData( const MeshData& meshData );
111 * @brief Assignment operator.
113 * @param[in] rhs MeshData object to copy data from
114 * @return A reference to this
116 MeshData& operator=(const MeshData& rhs);
119 * @brief Sets the vertex coords, the face indices, the bones affecting this mesh and a default
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.
127 void SetData( const VertexContainer& vertices,
128 const FaceIndices& faceIndices,
129 const BoneContainer& bones,
130 const Material material );
133 * @brief Set the vertex coords and end points of each line.
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.
139 void SetLineData( const VertexContainer& vertices,
140 const FaceIndices& lineIndices,
141 const Material material );
144 * @brief Set the vertex coords for each point.
146 * @param[in] vertices The vertex data (coords & color of each vertex)
147 * @param[in] material A handle to a material object.
149 void SetPointData( const VertexContainer& vertices,
150 const Material material );
153 * @brief Set the vertex coords for each point.
155 * @param[in] vertices The vertex data (coords & color of each vertex)
157 void SetVertices( const VertexContainer& vertices );
160 * @brief Sets the face indices.
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.
164 void SetFaceIndices( const FaceIndices& faceIndices );
167 * @brief Add the mesh to the bounding volume.
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
174 void AddToBoundingVolume(Vector4& min, Vector4& max, const Matrix& transform);
177 * @brief Get the geometry type.
179 * A mesh defaults to triangles if no data has been set.
180 * @return the geometry type;
182 VertexGeometryType GetVertexGeometryType() const;
185 * @brief Get the number of vertices.
187 * @return The number of vertices
189 size_t GetVertexCount() const;
192 * @brief Get the vertex array.
194 * @return The vertex array
196 const VertexContainer& GetVertices() const;
199 * @brief Get the number of points, lines or faces (note this is not the same as the number of face indices!).
201 * depending on the geometry type;
202 * @return Number of points, lines or faces
204 size_t GetFaceCount() const;
207 * @brief Get the face index array.
209 * @return The face index array
211 const FaceIndices& GetFaces() const;
214 * @brief Sets if the mesh has texture coordinates.
216 * @param hasTexCoords - True if the mesh has texture coordinates.
218 void SetHasTextureCoords(bool hasTexCoords);
221 * @brief Checks if the mesh is textured.
223 * @return true if the mesh is texture mapped.
225 bool HasTextureCoords() const;
228 * @brief Sets if the mesh has normals.
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
234 void SetHasNormals(bool hasNormals);
237 * @brief Checks if the mesh has normals.
239 * @return true if the mesh contains normals.
241 bool HasNormals() const;
244 * @brief Sets if the mesh vertices have color.
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.
250 void SetHasColor(bool hasColor);
253 * @brief Checks if the mesh vertices have color.
255 * @return true if the mesh contains colored vertices.
257 bool HasColor() const;
260 * @brief Get the original material associated with this mesh.
262 * @return Handle to the material
264 Material GetMaterial() const;
267 * @brief Set the default material associated with this mesh.
269 void SetMaterial( Material material );
272 * @brief Get the number of bones affecting this mesh.
274 * @return The number of bones affecting this mesh
276 size_t GetBoneCount() const;
279 * @brief Does this mesh have bones?.
281 * @return true if this mesh has bones.
283 bool HasBones() const;
286 * @brief Get the bone container.
290 const BoneContainer& GetBones() const;
293 * @brief Get the lower bounds of the bounding box containing the vertices.
295 * @return the lower bounds
297 const Vector4& GetBoundingBoxMin() const;
300 * @brief Set the lower bounds of the bounding box containing the vertices.
302 * @param bounds The lower bounds
304 void SetBoundingBoxMin(const Vector4& bounds);
307 * @brief Get the upper bounds of the bounding box containing the vertices.
309 * @return the upper bounds
311 const Vector4& GetBoundingBoxMax() const;
314 * @brief Set the upper bounds of the bounding box containing the vertices.
316 * @param bounds The upper bounds
318 void SetBoundingBoxMax(const Vector4& bounds);
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
334 Material mMaterial; ///< Material of the mesh
336 Vector4 mMin; ///< The lower bounds of the bounding box
337 Vector4 mMax; ///< The upper bounds of the bounding box
342 * @brief A vertex within a mesh, with the corresponding texture coordinate, normal and up to 4 bone influences.
344 struct MeshData::Vertex
346 static const unsigned int MAX_BONE_INFLUENCE = 4; ///< Maximum number of bones that can influence this particular vertex.
349 : x(0.0f), y(0.0f), z(0.0f),
351 nX(0.0f), nY(0.0f), nZ(0.0f)
356 * @brief Constructor.
358 * @param[in] position The vertex position
359 * @param[in] textureCoordinates The texture coordinates
360 * @param[in] normal The surface normal
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)
374 // Texture coordinates
378 // Normals / vertex colours
396 * @brief Bone indices specify which bones have an influence over the vertex (if any).
398 unsigned char boneIndices[MAX_BONE_INFLUENCE];
401 * @brief Bone weights determine the strength of the influence of each bone.
403 float boneWeights[MAX_BONE_INFLUENCE];
408 #endif // __DALI_MESH_DATA_H__