2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "mesh-builder.h"
22 void AddVertex( MeshData::VertexContainer& verts, Vector3 V, Vector2 UV )
24 MeshData::Vertex meshVertex;
30 verts.push_back(meshVertex);
33 void SetNormal( MeshData::VertexContainer& verts, size_t vertIdx, Vector3 normal )
35 verts[vertIdx].nX = normal.x;
36 verts[vertIdx].nY = normal.y;
37 verts[vertIdx].nZ = normal.z;
40 void SetBone( MeshData::VertexContainer& verts, size_t vertIdx, size_t index, size_t boneIndex, float weight)
42 verts[vertIdx].boneIndices[index] = boneIndex;
43 verts[vertIdx].boneWeights[index] = weight;
46 void SetBones(MeshData::VertexContainer& verts)
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);
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);
58 SetBone(verts, 3, 1, 2, 0.5f);
59 SetBone(verts, 4, 1, 2, 0.5f);
60 SetBone(verts, 5, 1, 2, 0.5f);
63 void ConstructBones(BoneContainer& bones)
65 bones.push_back(Bone("Bone1", Matrix::IDENTITY));
66 bones.push_back(Bone("Bone2", Matrix::IDENTITY));
67 bones.push_back(Bone("Bone3", Matrix::IDENTITY));
70 void CopyVertex( MeshData::Vertex& vert, Vector3& vector )
77 void AddTriangle( MeshData::VertexContainer& verts,
78 MeshData::FaceIndices& faces,
79 size_t v0, size_t v1, size_t v2 )
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);
94 SetNormal(verts, v0, normal);
95 SetNormal(verts, v1, normal);
96 SetNormal(verts, v2, normal);
99 void ConstructVertices( MeshData::VertexContainer& vertices, float sz )
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
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
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
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
122 void ConstructFaces(MeshData::VertexContainer& vertices, MeshData::FaceIndices& faces)
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)
130 Material ConstructMaterial()
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;
141 Mesh ConstructMesh( float sz )
143 MeshData::VertexContainer vertices;
144 MeshData::FaceIndices faces;
145 ConstructVertices( vertices, sz );
146 ConstructFaces(vertices, faces);
147 Material customMaterial = ConstructMaterial();
151 meshData.SetData(vertices, faces, bones, customMaterial);
152 meshData.SetHasNormals(true);
153 meshData.SetHasTextureCoords(true);
155 Mesh mesh = Mesh::New(meshData);
160 void AddBone(Dali::BoneContainer& bones, const std::string& name, const Dali::Matrix& offsetMatrix)
162 bones.push_back(Bone(name, offsetMatrix));
165 void CreateMeshData(MeshData& meshData)
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);
181 Dali::ModelData BuildTreeModel()
183 Dali::ModelData modelData = Dali::ModelData::New("TreeModel");
186 CreateMeshData(meshData);
187 modelData.AddMesh(meshData);
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");
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);
202 modelData.SetRootEntity(rootEntity);
208 EntityAnimatorMap twigAnimator("TwigAnim");
209 twigAnimator.SetEntityName("twig");
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);
217 EntityAnimatorMap branchAnimator("BranchAnim");
218 branchAnimator.SetEntityName("branch");
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);
228 ModelAnimationMap modelAnim;
229 modelAnim.name = "Anim1";
230 modelAnim.duration = 10.0f;
231 modelAnim.animators.push_back(branchAnimator);
232 modelAnim.animators.push_back(twigAnimator);
234 modelData.GetAnimationMapContainer().push_back(modelAnim);