Improved pan gesture prediction
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / dali-test-suite-utils / mesh-builder.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 #include "mesh-builder.h"
18
19 namespace Dali
20 {
21
22 void AddVertex( MeshData::VertexContainer& verts, Vector3 V, Vector2 UV )
23 {
24   MeshData::Vertex meshVertex;
25   meshVertex.x = V.x;
26   meshVertex.y = V.y;
27   meshVertex.z = V.z;
28   meshVertex.u = UV.x;
29   meshVertex.v = UV.y;
30   verts.push_back(meshVertex);
31 }
32
33 void SetNormal( MeshData::VertexContainer& verts, size_t vertIdx, Vector3 normal )
34 {
35   verts[vertIdx].nX = normal.x;
36   verts[vertIdx].nY = normal.y;
37   verts[vertIdx].nZ = normal.z;
38 }
39
40 void SetBone( MeshData::VertexContainer& verts, size_t vertIdx, size_t index, size_t boneIndex, float weight)
41 {
42   verts[vertIdx].boneIndices[index] = boneIndex;
43   verts[vertIdx].boneWeights[index] = weight;
44 }
45
46 void SetBones(MeshData::VertexContainer& verts)
47 {
48   // Set all verts in one corner to be affected fully by bone 0
49   SetBone(verts, 0, 0, 0, 1.0f);
50   SetBone(verts, 1, 0, 0, 1.0f);
51   SetBone(verts, 2, 0, 0, 1.0f);
52
53   // Set all verts in next corner to be affected by bone 1 and bone 2 equally
54   SetBone(verts, 3, 0, 1, 0.5f);
55   SetBone(verts, 4, 0, 1, 0.5f);
56   SetBone(verts, 5, 0, 1, 0.5f);
57
58   SetBone(verts, 3, 1, 2, 0.5f);
59   SetBone(verts, 4, 1, 2, 0.5f);
60   SetBone(verts, 5, 1, 2, 0.5f);
61 }
62
63 void ConstructBones(BoneContainer& bones)
64 {
65   bones.push_back(Bone("Bone1", Matrix::IDENTITY));
66   bones.push_back(Bone("Bone2", Matrix::IDENTITY));
67   bones.push_back(Bone("Bone3", Matrix::IDENTITY));
68 }
69
70 void CopyVertex( MeshData::Vertex& vert, Vector3& vector )
71 {
72   vector.x = vert.x;
73   vector.y = vert.y;
74   vector.z = vert.z;
75 }
76
77 void AddTriangle( MeshData::VertexContainer& verts,
78                   MeshData::FaceIndices& faces,
79                   size_t v0, size_t v1, size_t v2 )
80 {
81   faces.push_back(v0);
82   faces.push_back(v1);
83   faces.push_back(v2);
84
85   // Calculate normal...
86   Vector3 vert0, vert1, vert2;
87   CopyVertex(verts[v0], vert0);
88   CopyVertex(verts[v1], vert1);
89   CopyVertex(verts[v2], vert2);
90   Vector3 e0 = vert1 - vert0;
91   Vector3 e1 = vert2 - vert1;
92   Vector3 normal = e0.Cross(e1);
93   normal.Normalize();
94   SetNormal(verts, v0, normal);
95   SetNormal(verts, v1, normal);
96   SetNormal(verts, v2, normal);
97 }
98
99 void ConstructVertices( MeshData::VertexContainer& vertices, float sz )
100 {
101   // back
102   AddVertex(vertices, Vector3( 0.0f,  -sz, 0.0f), Vector2(0.50f, 0.50f));        // 0a  0
103   AddVertex(vertices, Vector3( 0.0f,  -sz, 0.0f), Vector2(0.50f, 0.50f));        // 0b  1
104   AddVertex(vertices, Vector3( 0.0f,  -sz, 0.0f), Vector2(0.50f, 0.50f));        // 0c  2
105
106   // left
107   AddVertex(vertices, Vector3(-sz*0.5f, sz*0.3f, sz*0.5f), Vector2(0.25f, 0.50f));  // 1a  3
108   AddVertex(vertices, Vector3(-sz*0.5f, sz*0.3f, sz*0.5f), Vector2(0.25f, 0.50f));  // 1b  4
109   AddVertex(vertices, Vector3(-sz*0.5f, sz*0.3f, sz*0.5f), Vector2(0.25f, 0.50f));  // 1c  5
110
111   // right
112   AddVertex(vertices, Vector3( sz*0.5f, sz*0.3f, sz*0.5f), Vector2(0.50f, 0.25f));  // 2a  6
113   AddVertex(vertices, Vector3( sz*0.5f, sz*0.3f, sz*0.5f), Vector2(0.50f, 0.25f));  // 2b  7
114   AddVertex(vertices, Vector3( sz*0.5f, sz*0.3f, sz*0.5f), Vector2(0.50f, 0.25f));  // 2c  8
115
116   // top
117   AddVertex(vertices, Vector3( 0.0f,   sz*0.3f, -sz*0.7f), Vector2(0.25f, 0.25f)); // 3a  9
118   AddVertex(vertices, Vector3( 0.0f,   sz*0.3f, -sz*0.7f), Vector2(0.25f, 0.25f)); // 3b 10
119   AddVertex(vertices, Vector3( 0.0f,   sz*0.3f, -sz*0.7f), Vector2(0.25f, 0.25f)); // 3c 11
120 }
121
122 void ConstructFaces(MeshData::VertexContainer& vertices, MeshData::FaceIndices& faces)
123 {
124   AddTriangle(vertices, faces,  0, 6,  3); // 0, 2, 1  back, right, left (ac)
125   AddTriangle(vertices, faces,  1, 9,  7); // 0, 3, 2  back, top , right (ac)
126   AddTriangle(vertices, faces,  2, 4, 10); // 0, 1, 3  back, left, top   (ac)
127   AddTriangle(vertices, faces, 11, 5,  8); // 3, 1, 2  top, left, right  (ac)
128 }
129
130 Material ConstructMaterial()
131 {
132   Material customMaterial = Material::New("CustomMaterial");
133   customMaterial.SetOpacity(.76f);
134   customMaterial.SetDiffuseColor(Vector4(0.8f, 0.0f, 0.4f, 1.0f));
135   customMaterial.SetAmbientColor(Vector4(0.2f, 1.0f, 0.6f, 1.0f));
136   customMaterial.SetSpecularColor(Vector4(0.5f, 0.6f, 0.7f, 1.0f));
137   return customMaterial;
138 }
139
140
141 Mesh ConstructMesh( float sz )
142 {
143   MeshData::VertexContainer vertices;
144   MeshData::FaceIndices     faces;
145   ConstructVertices( vertices, sz );
146   ConstructFaces(vertices, faces);
147   Material customMaterial = ConstructMaterial();
148
149   MeshData meshData;
150   BoneContainer bones;
151   meshData.SetData(vertices, faces, bones, customMaterial);
152   meshData.SetHasNormals(true);
153   meshData.SetHasTextureCoords(true);
154
155   Mesh mesh = Mesh::New(meshData);
156   return mesh;
157 }
158
159
160 void AddBone(Dali::BoneContainer& bones, const std::string& name, const Dali::Matrix& offsetMatrix)
161 {
162   bones.push_back(Bone(name, offsetMatrix));
163 }
164
165 void CreateMeshData(MeshData& meshData)
166 {
167   MeshData::VertexContainer vertices;
168   MeshData::FaceIndices faces;
169   Dali::BoneContainer bones;
170   AddBone(bones, "trunk",  Matrix::IDENTITY);
171   AddBone(bones, "branch", Matrix::IDENTITY);
172   AddBone(bones, "twig",   Matrix::IDENTITY);
173   ConstructVertices( vertices, 50 );
174   ConstructFaces(vertices, faces);
175   Material customMaterial = ConstructMaterial();
176   meshData.SetData(vertices, faces, bones, customMaterial);
177   meshData.SetHasNormals(true);
178   meshData.SetHasTextureCoords(true);
179 }
180
181 Dali::ModelData BuildTreeModel()
182 {
183   Dali::ModelData modelData = Dali::ModelData::New("TreeModel");
184
185   MeshData meshData;
186   CreateMeshData(meshData);
187   modelData.AddMesh(meshData);
188
189   Dali::Entity rootEntity = Dali::Entity::New("root");
190   Dali::Entity e1 = Dali::Entity::New("trunk");
191   Dali::Entity e2 = Dali::Entity::New("branch");
192   Dali::Entity e3 = Dali::Entity::New("twig");
193   Dali::Entity e4 = Dali::Entity::New("leaf");
194
195   rootEntity.SetType(Dali::Entity::OBJECT);
196   e1.SetType(Dali::Entity::OBJECT);
197   e2.SetType(Dali::Entity::OBJECT);
198   e3.SetType(Dali::Entity::OBJECT);
199   e4.SetType(Dali::Entity::OBJECT);
200   e4.AddMeshIndex(0);
201
202   modelData.SetRootEntity(rootEntity);
203   rootEntity.Add(e1);
204   e1.Add(e2);
205   e2.Add(e3);
206   e2.Add(e4);
207
208   EntityAnimatorMap twigAnimator("TwigAnim");
209   twigAnimator.SetEntityName("twig");
210
211   KeyFrames pKF1 = KeyFrames::New();
212   pKF1.Add(0.0f, Vector3(2.0f, 1.0f,  0.0f));
213   pKF1.Add(0.5f, Vector3(3.0f, 1.0f,  5.0f));
214   pKF1.Add(1.0f, Vector3(4.0f, 1.0f, 10.0f));
215   twigAnimator.SetPositionKeyFrames(pKF1);
216
217   EntityAnimatorMap branchAnimator("BranchAnim");
218   branchAnimator.SetEntityName("branch");
219
220   KeyFrames pKF2 = KeyFrames::New();
221   pKF2.Add(0.0f, Vector3(0.0f,  1.0f,  0.0f));
222   pKF2.Add(0.25f, Vector3(-1.0f,1.0f,  5.0f));
223   pKF2.Add(0.5f, Vector3(0.0f,  1.0f, 10.0f));
224   pKF2.Add(0.75f, Vector3(1.0f, 1.0f,  5.0f));
225   pKF2.Add(1.0f, Vector3(0.0f,  1.0f, 10.0f));
226   branchAnimator.SetPositionKeyFrames(pKF2);
227
228   ModelAnimationMap modelAnim;
229   modelAnim.name = "Anim1";
230   modelAnim.duration = 10.0f;
231   modelAnim.animators.push_back(branchAnimator);
232   modelAnim.animators.push_back(twigAnimator);
233
234   modelData.GetAnimationMapContainer().push_back(modelAnim);
235
236   return modelData;
237 }
238
239 } // namespace Dali