License conversion from Flora to Apache 2.0
[platform/core/uifw/dali-core.git] / dali / internal / event / modeling / model-logger.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
18 // CLASS HEADER
19 #include <dali/internal/event/modeling/model-logger.h>
20
21 // EXTERNAL INCLUDES
22 #include <iostream>
23 #include <sstream>
24 #include <iomanip>
25
26 // INTERNAL INCLUDES
27 #include <dali/public-api/modeling/bone.h>
28 #include <dali/public-api/modeling/entity.h>
29 #include <dali/public-api/modeling/material.h>
30 #include <dali/public-api/modeling/model-animation-map.h>
31 #include <dali/public-api/modeling/entity-animator-map.h>
32
33 #include <dali/integration-api/debug.h>
34
35 #include <dali/internal/event/modeling/light-impl.h>
36 #include <dali/internal/event/modeling/mesh-impl.h>
37 #include <dali/internal/event/animation/key-frames-impl.h>
38 #include <dali/internal/event/modeling/model-impl.h>
39
40 using namespace std;
41
42 namespace Dali
43 {
44 using Internal::MaterialIPtr;
45 using Internal::MeshIPtr;
46 using Dali::MeshData;
47
48 namespace Internal
49 {
50
51 void ModelLogger::WriteMeshes()
52 {
53   cout << "Meshes: " << endl;
54
55   for(unsigned int meshIdx = 0; meshIdx < mModel.NumberOfMeshes(); ++meshIdx)
56   {
57     const Dali::MeshData& meshData = mModel.GetMesh(meshIdx);
58
59     cout << endl;
60     cout << "Mesh[" << meshIdx << "]  NumVertices: " << meshData.GetVertexCount()
61          << "  NumFaces: " << meshData.GetFaceCount()
62          << "  HasTexCoords: " << (meshData.HasTextureCoords()?"T":"F")
63          << "  HasNormals: " << (meshData.HasNormals()?"T":"F")
64          << "  Material: " << meshData.GetMaterial().GetName() << endl
65          << "  Bounding box: " << meshData.GetBoundingBoxMin() << " - " << meshData.GetBoundingBoxMax() << endl;
66
67     if (meshData.GetBoneCount())
68     {
69       const BoneContainer& bones = meshData.GetBones();
70       for(size_t boneIdx = 0; boneIdx < bones.size(); ++boneIdx)
71       {
72         cout << "   Bones[" << boneIdx << "] " << bones.at(boneIdx).GetName() << endl;
73       }
74     }
75
76
77     if(mWriteVertices)
78     {
79       // create temporary pointer to the vertices
80       const Dali::MeshData::VertexContainer& vertices = meshData.GetVertices();
81       for(unsigned int vertexIdx = 0; vertexIdx < vertices.size(); ++vertexIdx)
82       {
83         const MeshData::Vertex& vertex = vertices[vertexIdx];
84         cout << "Vertex[" << vertexIdx << "] Pos<"<<vertex.x<<","<<vertex.y<<","<<vertex.z<<">";
85         if(meshData.HasTextureCoords())
86         {
87           cout << " TexC<"<<vertex.u<<","<<vertex.v<<">";
88         }
89         if(meshData.HasNormals())
90         {
91           cout << " Normal<"<<vertex.nX<<","<<vertex.nY<<","<<vertex.nZ<<">";
92         }
93         cout << endl;
94         if(meshData.HasBones())
95         {
96           cout << "    BoneIndices[] = ";
97           for(size_t i=0; i<MeshData::Vertex::MAX_BONE_INFLUENCE; i++)
98           {
99             cout << (int)vertex.boneIndices[i] << ", ";
100           }
101           cout << endl << "    BoneWeights[] = ";
102           for (size_t i=0; i< MeshData::Vertex::MAX_BONE_INFLUENCE; i++)
103           {
104             cout << vertex.boneWeights[i] << ", ";
105           }
106           cout << endl;
107         }
108       }
109       cout << endl;
110
111       const Dali::MeshData::FaceIndices& faces = meshData.GetFaces();
112       for(unsigned int faceIdx = 0; faceIdx < meshData.GetFaceCount(); ++faceIdx)
113       {
114         const FaceIndex* face = &(faces)[faceIdx * 3];
115         cout << "(" << face[0] <<"," << face[1] << "," << face[2] << ") ";
116         cout << endl;
117       }
118
119       cout << endl;
120     }
121
122   }
123   cout << endl;
124 }
125
126 void ModelLogger::WriteMaterials()
127 {
128   cout << "Materials:" << endl;
129
130   for(unsigned int matIdx = 0; matIdx < mModel.NumberOfMaterials(); ++matIdx)
131   {
132     Dali::Material mat = mModel.GetMaterial(matIdx);
133     cout << "Material["   << matIdx << "] " << mat.GetName();
134     cout << "  Opacity: "   << mat.GetOpacity();
135     cout << "  Shininess: " << mat.GetShininess();
136     cout << "  Ambient: "   << Debug::ColorToString(mat.GetAmbientColor());
137     cout << "  Diffuse: "   << Debug::ColorToString(mat.GetDiffuseColor());
138     cout << "  Specular: "  << Debug::ColorToString(mat.GetSpecularColor());
139     cout << "  Emissive: "  << Debug::ColorToString(mat.GetEmissiveColor()) << endl;
140
141     // TODO: Add
142   }
143   cout << endl;
144 }
145
146 void ModelLogger::WriteLights()
147 {
148   cout << "Lights:" << endl;
149   for(unsigned int lightIdx = 0; lightIdx < mModel.NumberOfLights(); ++lightIdx)
150   {
151     Dali::Light light = mModel.GetLight(lightIdx);
152     cout << "Light[" << lightIdx << "] " << light.GetName()
153          << " type(" << light.GetType()
154          << ") falloff(" << light.GetFallOff().x << ", " << light.GetFallOff().y
155          << ") spotangle(" << light.GetSpotAngle().x << ", " << light.GetSpotAngle().y
156          << ") ambient(" << light.GetAmbientColor().r << ", " << light.GetAmbientColor().g << ", " << light.GetAmbientColor().b
157          << ") diffuse(" << light.GetDiffuseColor().r << ", " << light.GetDiffuseColor().g << ", " << light.GetDiffuseColor().b
158          << ") specular(" << light.GetSpecularColor().r << ", " << light.GetSpecularColor().g << ", " << light.GetSpecularColor().b
159          << ")" << endl;
160   }
161   cout << endl;
162 }
163
164 void ModelLogger::WriteEntity(Dali::Entity entity, int level)
165 {
166   if (entity)
167   {
168     string indent = string(level*2, ' ');
169     cout << indent << "Entity " << "Type:<";
170     switch(entity.GetType())
171     {
172       case Dali::Entity::OBJECT: cout<<"OBJECT"; break;
173       case Dali::Entity::CAMERA: cout<<"CAMERA"; break;
174       case Dali::Entity::LIGHT:  cout<<"LIGHT";  break;
175     }
176     cout << "> Name: " << entity.GetName() << endl;
177     cout << indent << "  Meshes(" << entity.NumberOfMeshes() << ") = [";
178     for (int i=0; i<entity.NumberOfMeshes(); i++)
179     {
180       cout << entity.GetMeshByIndex(i) << ", ";
181     }
182     cout << "]" << endl;
183
184     cout << indent << "  LowerBounds: " << Debug::Vector3ToString(entity.GetLowerBounds()) << endl;
185     cout << indent << "  UpperBounds: " << Debug::Vector3ToString(entity.GetUpperBounds()) << endl;
186     cout << indent << "  TransformMatrix: " << endl;
187     cout << Debug::MatrixToString(entity.GetTransformMatrix(), 3, level*2+4) << endl;
188
189     if(entity.HasChildren())
190     {
191       ++level;
192       const EntityContainer& children = entity.GetChildren();
193       for(EntityConstIter iter = children.begin(); iter != children.end(); ++iter)
194       {
195         WriteEntity(*iter, level);
196       }
197     }
198   }
199   cout << endl;
200 }
201
202 void ModelLogger::WriteAnimations()
203 {
204   cout << "Animations (:" << mModel.NumberOfAnimationMaps() << ")" << endl;
205   for(unsigned int idx = 0; idx < mModel.NumberOfAnimationMaps(); ++idx)
206   {
207     const ModelAnimationMap* animation( mModel.GetAnimationMap(idx) );
208     cout << "Animation ["<< idx << "] " << endl;
209     if( animation != NULL )
210     {
211       cout << "  Duration:  " << animation->duration << endl;
212       cout << "  Repeats:   " << animation->repeats << endl;
213
214       size_t animatorIdx=0;
215
216       for(EntityAnimatorMapIter iter = animation->animators.begin(); iter != animation->animators.end(); ++iter)
217       {
218         const EntityAnimatorMap& animator(*iter);
219         std::string entityName = animator.GetEntityName();
220
221         cout << "  Animator ["<<animatorIdx<<"] " << entityName << "  "
222              << "Duration: " << animator.GetDuration() << endl;
223
224         const KeyFrameVector3* positionKeyFrames;
225         GetSpecialization(GetImplementation(animator.GetPositionKeyFrames()), positionKeyFrames);
226         const KeyFrameVector3* scaleKeyFrames;
227         GetSpecialization(GetImplementation(animator.GetScaleKeyFrames()), scaleKeyFrames);
228         const KeyFrameQuaternion* rotationKeyFrames;
229         GetSpecialization(GetImplementation(animator.GetRotationKeyFrames()), rotationKeyFrames);
230
231         if(positionKeyFrames->GetNumberOfKeyFrames() > 0)
232         {
233           cout << "    Channel: Position" << endl;
234
235           for(size_t kfIndex = 0; kfIndex < positionKeyFrames->GetNumberOfKeyFrames(); kfIndex++)
236           {
237             float progress;
238             Vector3 position;
239
240             positionKeyFrames->GetKeyFrame(kfIndex, progress, position);
241             cout << "  [" << kfIndex << "] " << progress << position << endl;
242           }
243         }
244
245         if(scaleKeyFrames->GetNumberOfKeyFrames() > 0)
246         {
247           cout << "    Channel: Scale" << endl;
248           for(size_t kfIndex = 0; kfIndex < scaleKeyFrames->GetNumberOfKeyFrames(); kfIndex++)
249           {
250             float progress;
251             Vector3 scale;
252             scaleKeyFrames->GetKeyFrame(kfIndex, progress, scale);
253             cout << "  [" << kfIndex << "] " << progress << scale << endl;
254           }
255         }
256
257         if(rotationKeyFrames->GetNumberOfKeyFrames() > 0)
258         {
259           cout << "    Channel: Rotation" << endl;
260           for(size_t kfIndex = 0; kfIndex < rotationKeyFrames->GetNumberOfKeyFrames(); kfIndex++)
261           {
262             float progress;
263             Quaternion rotation;
264             rotationKeyFrames->GetKeyFrame(kfIndex, progress, rotation);
265             cout << "  [" << kfIndex << "] " << progress << rotation << endl;
266           }
267         }
268       }
269     }
270     else
271     {
272       cout << "Not found";
273     }
274   }
275   cout << endl;
276 }
277
278
279 void ModelLogger::Write()
280 {
281   cout << "Model name : " << mModel.GetName() << endl;
282   WriteMeshes();
283   WriteMaterials();
284   WriteLights();
285   cout << "Entities:" << endl;
286   WriteEntity(mModel.GetRootEntity(), 0);
287   WriteAnimations();
288 }
289
290
291 } // Internal
292 } // Dali