Merge "Combine Internal::ProxyObject & Internal::Object" into tizen
[platform/core/uifw/dali-core.git] / dali / internal / event / modeling / model-archive.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-archive.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/common/stage.h>
23 #include <dali/internal/event/animation/key-frames-impl.h>
24 #include <dali/internal/event/modeling/entity-impl.h>
25 #include <dali/internal/event/render-tasks/render-task-impl.h>
26 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
27 #include <dali/integration-api/debug.h>
28
29 using std::string;
30
31 #define FOURCC(a,b,c,d)   \
32   ((((a) & 0xff)      ) | \
33    (((b) & 0xff) <<  8) | \
34    (((c) & 0xff) << 16) | \
35    (((d) & 0xff) << 24))
36
37 namespace Dali
38 {
39
40 namespace Internal
41 {
42
43 namespace Serialize
44 {
45 /*------ serialization overrides of operator<< () and operator>> () ------*/
46
47 //---------- MaterialProperties ----------//
48 Archive& operator<< (Archive& ar, const MaterialProperties& t)
49 {
50   ar << t.mOpacity;
51   ar << t.mShininess;
52   ar << t.mAmbientColor;
53   ar << t.mDiffuseColor;
54   ar << t.mSpecularColor;
55   ar << t.mEmissiveColor;
56   ar << t.mMapU;
57   ar << t.mMapV;
58   ar << t.mDiffuseUVIndex;
59   ar << t.mOpacityUVIndex;
60   ar << t.mNormalUVIndex;
61   ar << t.mHasHeightMap;
62
63   return ar;
64 }
65
66 Archive& operator>> (Archive& ar, MaterialProperties& t)
67 {
68   ar >> t.mOpacity;
69   ar >> t.mShininess;
70   ar >> t.mAmbientColor;
71   ar >> t.mDiffuseColor;
72   ar >> t.mSpecularColor;
73   ar >> t.mEmissiveColor;
74   ar >> t.mMapU;
75   ar >> t.mMapV;
76   ar >> t.mDiffuseUVIndex;
77   ar >> t.mOpacityUVIndex;
78   ar >> t.mNormalUVIndex;
79   ar >> t.mHasHeightMap;
80
81   return ar;
82 }
83
84 //---------- Material ----------//
85 Archive& operator<< (Archive& ar, const Material& t)
86 {
87   ar.OpenChunk(FOURCC('M', 'A', 'T', '_'));
88
89   // Material's name
90   ar << t.GetName();
91
92   // Diffuse texture file name
93   ar << t.GetDiffuseTextureFileName();
94
95   // Opacity texture file name
96   ar << t.GetOpacityTextureFileName();
97
98   // Normal map file name
99   ar << t.GetNormalMapFileName();
100
101   // Material properties
102   ar << t.GetProperties();
103
104   ar.CloseChunk(); // MAT_
105
106   return ar;
107 }
108
109 Archive& operator>> (Archive& ar, Material& t)
110 {
111   string name;
112
113   if( ar.GetResult() &&
114       ar.OpenChunk(FOURCC('M', 'A', 'T', '_')) )
115   {
116     // Material's name
117     ar >> name;
118     t.SetName(name);
119
120     // Diffuse texture file name
121     ar >> name;
122     t.SetDiffuseTextureFileName(name);
123
124     // Opacity texture file name
125     ar >> name;
126     t.SetOpacityTextureFileName(name);
127
128     // Normal map file name
129     ar >> name;
130     t.SetNormalMapFileName(name);
131
132     // Material properties
133     MaterialProperties properties;
134     ar >> properties;
135     t.SetProperties(properties);
136
137     ar.CloseChunk(); // MAT_
138   }
139
140   return ar;
141 }
142
143 /*---- Vertex ----*/
144
145 Archive& operator<< (Archive& ar, const Dali::MeshData::Vertex& t)
146 {
147   // Position
148   ar << t.x << t.y << t.z;
149
150   // Texture coordinates
151   ar << t.u << t.v;
152
153   // Normals
154   ar << t.nX << t.nY << t.nZ;
155
156   // Bones
157   for( unsigned int i = 0; i < Dali::MeshData::Vertex::MAX_BONE_INFLUENCE; ++i)
158   {
159     ar << t.boneIndices[i];
160   }
161
162   for( unsigned int i = 0; i < Dali::MeshData::Vertex::MAX_BONE_INFLUENCE; ++i)
163   {
164     ar << t.boneWeights[i];
165   }
166
167   return ar;
168 }
169
170 Archive& operator>> (Archive& ar, MeshData::Vertex& t)
171 {
172   // Position
173   ar >> t.x >> t.y >> t.z;
174
175   // Texture coordinates
176   ar >> t.u >> t.v;
177
178   // Normals
179   ar >> t.nX >> t.nY >> t.nZ;
180
181   // Bones
182   for( unsigned int i = 0; i < Dali::MeshData::Vertex::MAX_BONE_INFLUENCE; ++i)
183   {
184     ar >> t.boneIndices[i];
185   }
186
187   for( unsigned int i = 0; i < Dali::MeshData::Vertex::MAX_BONE_INFLUENCE; ++i)
188   {
189     ar >> t.boneWeights[i];
190   }
191
192   return ar;
193 }
194
195 /*---- Light ----*/
196
197 Archive& operator<< (Archive& ar, const Dali::Light& t)
198 {
199   ar.OpenChunk(FOURCC('L', 'I', 'T', '_'));
200
201   ar << t.GetName();
202   ar << static_cast<unsigned int>(t.GetType());
203   ar << t.GetFallOff();
204   ar << t.GetSpotAngle();
205   ar << t.GetAmbientColor();
206   ar << t.GetDiffuseColor();
207   ar << t.GetSpecularColor();
208   ar << t.GetDirection();
209
210   ar.CloseChunk(); // LIT_
211   return ar;
212 }
213
214 Archive& operator>> (Archive& ar, Dali::Light& t)
215 {
216   std::string name;
217   Vector2 v2Value;
218   Vector3 v3Value;
219
220   if( ar.GetResult() &&
221       ar.OpenChunk(FOURCC('L', 'I', 'T', '_')) )
222   {
223
224     ar >> name;
225     t.SetName(name);
226
227     ar >> v2Value;
228     t.SetFallOff(v2Value);
229     ar >> v2Value;
230     t.SetSpotAngle(v2Value);
231     ar >> v3Value;
232     t.SetAmbientColor(v3Value);
233     ar >> v3Value;
234     t.SetDiffuseColor(v3Value);
235     ar >> v3Value;
236     t.SetSpecularColor(v3Value);
237     ar >> v3Value;
238     t.SetDirection(v3Value);
239
240     ar.CloseChunk(); // LIT_
241   }
242
243   return ar;
244 }
245
246 /*---- Bone ----*/
247
248 Archive& operator<< (Archive& ar, const Dali::Bone& t)
249 {
250   ar.OpenChunk(FOURCC('B', 'O', 'N', 'E'));
251
252   ar << t.GetName();
253   ar << t.GetOffsetMatrix();
254
255   ar.CloseChunk(); // BONE
256   return ar;
257 }
258
259 Archive& operator>> (Archive& ar, Bone& t)
260 {
261   if( ar.GetResult() &&
262       ar.OpenChunk(FOURCC('B', 'O', 'N', 'E')) )
263   {
264     std::string name;
265     Matrix offsetMatrix;
266     ar >> name;
267     ar >> offsetMatrix;
268     t = Bone( name, offsetMatrix );
269     ar.CloseChunk(); // BONE
270   }
271   return ar;
272 }
273
274 /*---- MeshData ----*/
275
276 Archive& operator<< (Archive& ar, const Dali::MeshData& meshData)
277 {
278   ar.OpenChunk(FOURCC('M', 'E', 'S', 'H'));
279
280   unsigned int count;
281
282   // write vertices
283   ar.OpenChunk(FOURCC('V', 'R', 'T', 'S'));
284   // definition of a vertex
285   ar.OpenChunk(FOURCC('D', 'E', 'F', '_'));
286   ar << static_cast<unsigned char>(VET_Position);
287   ar << static_cast<unsigned char>(VET_UV);
288   ar << static_cast<unsigned char>(VET_Normal);
289   ar << static_cast<unsigned char>(VET_BoneIndices);
290   ar << static_cast<unsigned char>(VET_BoneWeights);
291   ar.CloseChunk(); // DEF_
292   count = meshData.GetVertexCount();
293   ar << count;
294   if( count )
295   {
296     const Dali::MeshData::VertexContainer& vertices(meshData.GetVertices());
297     for( unsigned int i = 0; i < count; ++i)
298     {
299       ar << vertices[i];
300     }
301   }
302   ar.CloseChunk(); // VRTS
303
304   // write faces
305   ar.OpenChunk(FOURCC('F', 'C', 'E', 'S'));
306   count = meshData.GetFaceCount();
307   count *= 3;  // 3 elements per triangular face
308   ar << count;
309   if( count )
310   {
311     const Dali::MeshData::FaceIndices& faces(meshData.GetFaces());
312     for( unsigned int i = 0; i < count; ++i)
313     {
314       ar << faces[i];
315     }
316   }
317   ar.CloseChunk(); // FCES
318
319   // write bones
320   ar.OpenChunk(FOURCC('B', 'N', 'E', 'S'));
321   count = meshData.GetBoneCount();
322   ar << count;
323   if( count )
324   {
325     const Dali::BoneContainer& bones(meshData.GetBones());
326     for( unsigned int i = 0; i < count; ++i)
327     {
328       ar << bones.at(i);
329     }
330   }
331   ar.CloseChunk(); // BNES
332
333   // write material (just write the material's name)
334   ar << meshData.GetMaterial().GetName();
335
336   // write texturing options
337   ar << meshData.HasTextureCoords();
338   ar << meshData.HasNormals();
339
340   // write AABB
341   ar << meshData.GetBoundingBoxMin();
342   ar << meshData.GetBoundingBoxMax();
343
344   ar.CloseChunk(); // MESH
345   return ar;
346 }
347
348 Archive& operator>> (Archive& ar, MeshData& meshData)
349 {
350   Dali::MeshData::VertexContainer vertices;
351   Dali::MeshData::FaceIndices faces;
352   Dali::BoneContainer bones;
353   Dali::Material material;
354   Dali::MeshData::Vertex vertex;
355   Dali::MeshData::FaceIndex faceIndex;
356   Dali::Bone bone;
357   std::string name;
358   unsigned int count = 0;
359
360   if( ar.GetResult() &&
361       ar.OpenChunk(FOURCC('M', 'E', 'S', 'H')) )
362   {
363     // read vertices
364     if( ar.OpenChunk(FOURCC('V', 'R', 'T', 'S')) )
365     {
366       // Version 1 has fixed vertex elements, so skip definition
367       ar.SkipChunk(FOURCC('D', 'E', 'F', '_'));
368       ar >> count;
369       if( count )
370       {
371         vertices.reserve(count);
372
373         for( unsigned int i = 0; i < count; ++i )
374         {
375           ar >> vertex;
376           vertices.push_back(vertex);
377         }
378       }
379       ar.CloseChunk(); // VRTS
380     }
381
382     // read faces
383     if( ar.GetResult() &&
384         ar.OpenChunk(FOURCC('F', 'C', 'E', 'S')) )
385     {
386       ar >> count;
387       if( count )
388       {
389         faces.reserve(count);
390
391         for( unsigned int i = 0; i < count; ++i )
392         {
393           ar >> faceIndex;
394           faces.push_back(faceIndex);
395         }
396       }
397       ar.CloseChunk(); // FCES
398     }
399
400     // read bones
401     if( ar.GetResult() &&
402         ar.OpenChunk(FOURCC('B', 'N', 'E', 'S')) )
403     {
404       ar >> count;
405       if( count )
406       {
407         bones.reserve(count);
408
409         for( unsigned int i = 0; i < count; ++i )
410         {
411           ar >> bone;
412           bones.push_back(bone);
413         }
414       }
415       ar.CloseChunk(); // BNES
416     }
417
418     // read material
419     ar >> name;
420     material = Dali::Material::New(name);
421
422     meshData.SetData(vertices, faces, bones, material);
423
424     // write texturing options
425     bool option = false;
426     ar >> option;
427     meshData.SetHasTextureCoords(option);
428     ar >> option;
429     meshData.SetHasNormals(option);
430
431     // write AABB
432     Vector4 bounds;
433     ar >> bounds;
434     bounds.w = 0.0f;
435     meshData.SetBoundingBoxMin(bounds);
436     ar >> bounds;
437     bounds.w = 0.0f;
438     meshData.SetBoundingBoxMax(bounds);
439
440     ar.CloseChunk(); // MESH
441   }
442   return ar;
443 }
444
445 /*---- EntityAnimatorMap ----*/
446 Archive& operator<< (Archive& ar, const Dali::EntityAnimatorMap& t)
447 {
448   ar.OpenChunk(FOURCC('E', 'A', 'N', 'I'));
449
450   ar << t.GetEntityName();
451   ar << t.GetDuration();
452
453   unsigned int count = 0;
454
455   const KeyFrameVector3* positionKeyFrames = NULL;
456   GetSpecialization(GetImplementation(t.GetPositionKeyFrames()), positionKeyFrames);
457   if( positionKeyFrames )
458   {
459     count = positionKeyFrames->GetNumberOfKeyFrames();
460     ar << count;
461     if( count )
462     {
463       for( unsigned int i = 0; i < count; ++i)
464       {
465         float progress;
466         Vector3 position;
467         positionKeyFrames->GetKeyFrame(i, progress, position);
468
469         ar << progress << position;
470       }
471     }
472   }
473   else
474   {
475     ar << 0;
476   }
477
478   const KeyFrameVector3* scaleKeyFrames = NULL;
479   GetSpecialization(GetImplementation(t.GetScaleKeyFrames()), scaleKeyFrames);
480   if( scaleKeyFrames )
481   {
482     count = scaleKeyFrames->GetNumberOfKeyFrames();
483     ar << count;
484     if( count )
485     {
486       for( unsigned int i = 0; i < count; ++i)
487       {
488         float progress;
489         Vector3 scale;
490         scaleKeyFrames->GetKeyFrame(i, progress, scale);
491
492         ar << progress << scale;
493       }
494     }
495   }
496   else
497   {
498     ar << 0;
499   }
500
501   const KeyFrameQuaternion* rotationKeyFrames = NULL;
502   GetSpecialization(GetImplementation(t.GetRotationKeyFrames()), rotationKeyFrames);
503   if( rotationKeyFrames )
504   {
505     count = rotationKeyFrames->GetNumberOfKeyFrames();
506     ar << count;
507     if( count )
508     {
509       for( unsigned int i = 0; i < count; ++i)
510       {
511         float progress;
512         Quaternion rotation;
513         rotationKeyFrames->GetKeyFrame(i, progress, rotation);
514
515         ar << progress << rotation;
516       }
517     }
518   }
519   else
520   {
521     ar << 0;
522   }
523
524   ar.CloseChunk(); // EANI
525   return ar;
526 }
527
528 Archive& operator>> (Archive& ar, Dali::EntityAnimatorMap& t)
529 {
530   std::string name;
531   unsigned int count = 0;
532   float floatValue = 0.0f;
533   Vector3 vec3Value;
534   Quaternion quatValue;
535
536   if( ar.GetResult() &&
537       ar.OpenChunk(FOURCC('E', 'A', 'N', 'I')) )
538   {
539     ar >> name;
540     t.SetEntityName(name);
541     ar >> floatValue;
542     t.SetDuration(floatValue);
543
544
545     Dali::KeyFrames positionKeyFrames = Dali::KeyFrames::New();
546     ar >> count;
547     if( count )
548     {
549       for( unsigned int i = 0; i < count; ++i )
550       {
551         ar >> floatValue;
552         ar >> vec3Value;
553         Property::Value value(vec3Value);
554         positionKeyFrames.Add(floatValue, value);
555       }
556       t.SetPositionKeyFrames(positionKeyFrames);
557     }
558
559     Dali::KeyFrames scaleKeyFrames = Dali::KeyFrames::New();
560     ar >> count;
561     if( count )
562     {
563       for( unsigned int i = 0; i < count; ++i )
564       {
565         ar >> floatValue;
566         ar >> vec3Value;
567         Property::Value value(vec3Value);
568         scaleKeyFrames.Add(floatValue, value);
569       }
570       t.SetScaleKeyFrames(scaleKeyFrames);
571     }
572
573     Dali::KeyFrames rotationKeyFrames = Dali::KeyFrames::New();
574     ar >> count;
575     if( count )
576     {
577       for( unsigned int i = 0; i < count; ++i )
578       {
579         ar >> floatValue;
580         ar >> quatValue;
581         Property::Value value(quatValue);
582         rotationKeyFrames.Add(floatValue, value);
583       }
584       t.SetRotationKeyFrames(rotationKeyFrames);
585     }
586
587     ar.CloseChunk(); // EANI
588   }
589
590   return ar;
591 }
592
593 /*---- ModelAnimationMap ----*/
594 Archive& operator<< (Archive& ar, const Dali::ModelAnimationMap& t)
595 {
596   ar.OpenChunk(FOURCC('A', 'N', 'I', 'M'));
597
598   ar << t.name;
599
600   // animators
601   unsigned int count = t.animators.size();
602   ar << count;
603   if( count )
604   {
605     for( unsigned int i = 0; i < count; ++i)
606     {
607       const EntityAnimatorMap& entityAnimatorMap(t.animators[i]);
608
609       ar << entityAnimatorMap;
610     }
611   }
612
613   ar << t.duration;
614   ar << t.repeats;
615
616   ar.CloseChunk(); // ANIM
617   return ar;
618 }
619
620 Archive& operator>> (Archive& ar, ModelAnimationMap& t)
621 {
622   std::string name;
623   unsigned int count = 0;
624
625   if( ar.GetResult() &&
626       ar.OpenChunk(FOURCC('A', 'N', 'I', 'M')) )
627   {
628     ar >> t.name;
629
630     // animators
631     ar >> count;
632     if( count )
633     {
634       t.animators.reserve(count);
635       for( unsigned int i = 0; i < count; ++i)
636       {
637         EntityAnimatorMap entityAnimator("");
638         ar >> entityAnimator;
639         t.animators.push_back(entityAnimator);
640       }
641     }
642
643     ar >> t.duration;
644     ar >> t.repeats;
645
646     ar.CloseChunk(); // ANIM
647   }
648
649   return ar;
650 }
651
652 /*---- Entity ----*/
653 Archive& operator<< (Archive& ar, const Dali::Entity& t)
654 {
655   ar.OpenChunk(FOURCC('E', 'N', 'T', 'Y'));
656
657   unsigned int count;
658
659   ar << t.GetName();
660
661   // meshes
662   count = t.NumberOfMeshes();
663   ar << count;
664   if( count )
665   {
666     const EntityMeshIndices& meshes = t.GetMeshes();
667     for( unsigned int i = 0; i < count; ++i)
668     {
669       ar << meshes[i];
670     }
671   }
672
673   // matrix
674   ar << t.GetTransformMatrix();
675
676   // bounds
677   ar << t.GetLowerBounds();
678   ar << t.GetUpperBounds();
679
680   // type
681   ar << static_cast<unsigned int>(t.GetType());
682
683   // children
684   count = t.NumberOfChildren();
685   ar << count;
686   if( count )
687   {
688     const EntityContainer& entities = t.GetChildren();
689     for( unsigned int i = 0; i < count; ++i )
690     {
691       ar << entities[i];
692     }
693   }
694
695   ar.CloseChunk(); // ENTY
696   return ar;
697 }
698
699 Archive& operator>> (Archive& ar, Dali::Entity& t)
700 {
701   unsigned int count = 0;
702   unsigned int uintValue = 0;
703   Matrix mat4Value;
704   Vector3 vec3Value;
705   std::string name;
706
707   if( ar.GetResult() &&
708       ar.OpenChunk(FOURCC('E', 'N', 'T', 'Y')) )
709   {
710     ar >> name;
711     t.SetName(name);
712
713     // meshes
714     ar >> count;
715     if( count )
716     {
717       t.SetMeshCapacity(count);
718       for( unsigned int i = 0; i < count; ++i)
719       {
720         ar >> uintValue;
721         t.AddMeshIndex(uintValue);
722       }
723     }
724
725     // matrix
726     ar >> mat4Value;
727     t.SetTransformMatrix(mat4Value);
728
729     // bounds
730     ar >> vec3Value;
731     GetImplementation(t).SetLowerBounds(vec3Value);
732     ar >> vec3Value;
733     GetImplementation(t).SetUpperBounds(vec3Value);
734
735     // type
736     ar >> uintValue;
737     t.SetType(static_cast<Dali::Entity::EntityType>(uintValue));
738
739     // children
740     ar >> count;
741     if( count )
742     {
743       for( unsigned int i = 0; i < count; ++i )
744       {
745         Dali::Entity child(Dali::Entity::New(""));
746         ar >> child;
747
748         t.Add(child);
749         t.AddToBounds(child);
750       }
751     }
752
753     ar.CloseChunk(); // ENTY
754   }
755
756   return ar;
757 }
758
759 /*---- ModelData ----*/
760
761 Archive& operator<< (Archive& ar, const ModelData& t)
762 {
763   unsigned int count;
764
765   ar.OpenChunk(FOURCC('D', 'A', 'L', 'I'));
766
767   ar.OpenChunk(FOURCC('T', 'Y', 'P', 'E'));
768   std::string info("Dali Binary Model");
769   ar << info;
770   ar.CloseChunk(); // TYPE
771
772   ar.OpenChunk(FOURCC('V', 'E', 'R', 'S'));
773   ar << ar.GetVersion();
774   ar.CloseChunk(); // VERS
775
776   ar.OpenChunk(FOURCC('N', 'A', 'M', 'E'));
777   ar << t.GetName();
778   ar.CloseChunk(); // NAME
779
780   // Materials
781   ar.OpenChunk(FOURCC('M', 'A', 'T', 'S'));
782   count = t.NumberOfMaterials();
783   ar << count;
784   for( unsigned int i = 0; i < count; ++i)
785   {
786     Dali::Material material = t.GetMaterial(i);
787     ar << static_cast<const Material&>(material.GetBaseObject());
788   }
789   ar.CloseChunk(); // MATS
790
791   // Meshes
792   ar.OpenChunk(FOURCC('M', 'S', 'H', 'S'));
793   count = t.NumberOfMeshes();
794   ar << count;
795   for( unsigned int i = 0; i < count; ++i)
796   {
797     const Dali::MeshData& mesh(t.GetMesh(i));
798
799     ar << mesh;
800   }
801   ar.CloseChunk(); // MSHS
802
803   // Lights
804   ar.OpenChunk(FOURCC('L', 'I', 'T', 'S'));
805   count = t.NumberOfLights();
806   ar << count;
807   for( unsigned int i = 0; i < count; ++i)
808   {
809     ar << t.GetLight(i);
810   }
811   ar.CloseChunk(); // LITS
812
813   // Entities
814   ar.OpenChunk(FOURCC('E', 'N', 'T', 'S'));
815   ar << t.GetRootEntity();
816   ar.CloseChunk(); // ENTS
817
818   // Animations
819   ar.OpenChunk(FOURCC('A', 'N', 'I', 'S'));
820   count = t.NumberOfAnimationMaps();
821   ar << count;
822   if( count )
823   {
824     for( unsigned int i = 0; i < count; ++i)
825     {
826       ar << *t.GetAnimationMap(i);
827     }
828   }
829   ar.CloseChunk(); // ANIS
830
831   ar.CloseChunk(); // DALI
832   return ar;
833 }
834
835 Archive& operator>> (Archive& ar, ModelData& t)
836 {
837   unsigned int count = 0;
838   unsigned int uintValue = 0;
839
840   if( ar.GetResult() &&
841       ar.OpenChunk(FOURCC('D', 'A', 'L', 'I')) )
842   {
843     DALI_ASSERT_DEBUG( ar.PeekChunk() == FOURCC('T', 'Y', 'P', 'E') );
844     //  ar.OpenChunk(FOURCC('T', 'Y', 'P', 'E'));
845     ar.SkipChunk(FOURCC('T', 'Y', 'P', 'E'));
846
847     if( ar.GetResult() &&
848         ar.OpenChunk(FOURCC('V', 'E', 'R', 'S')) )
849     {
850       ar >> uintValue;
851       static_cast<InputArchive&>(ar).SetFileVersion(uintValue);
852       ar.CloseChunk();
853       // Set archive failure if there is a version mismatch
854       if( uintValue != ar.GetVersion() )
855       {
856         ar.SetResultFailed();
857       }
858     }
859
860     //  ar.OpenChunk(FOURCC('N', 'A', 'M', 'E'));
861     ar.SkipChunk(FOURCC('N', 'A', 'M', 'E'));
862
863     // Materials
864     if( ar.GetResult() &&
865         ar.OpenChunk(FOURCC('M', 'A', 'T', 'S')) )
866     {
867       ar >> count;
868       for( unsigned int i = 0; i < count; ++i)
869       {
870         Dali::Material material = Dali::Material::New("");
871         ar >> static_cast<Material&>(material.GetBaseObject());
872         t.AddMaterial(material);
873       }
874       ar.CloseChunk();
875     }
876
877     // Meshes
878     if( ar.GetResult() &&
879         ar.OpenChunk(FOURCC('M', 'S', 'H', 'S')) )
880     {
881       ar >> count;
882       for( unsigned int i = 0; i < count; ++i)
883       {
884         Dali::MeshData meshData;
885         ar >> meshData;
886
887         for( unsigned j = 0; j < t.NumberOfMaterials(); ++j)
888         {
889           Dali::Material material = t.GetMaterial(j);
890           if( material.GetName() == meshData.GetMaterial().GetName() )
891           {
892             meshData.SetMaterial(material);
893           }
894         }
895         t.AddMesh(meshData);
896       }
897       ar.CloseChunk(); // MSHS
898     }
899
900     if( ar.GetResult() &&
901         ar.OpenChunk(FOURCC('L', 'I', 'T', 'S')) )
902     {
903       ar >> count;
904       if( count )
905       {
906         for( unsigned int i = 0; i < count; ++i )
907         {
908           Dali::Light light(Dali::Light::New(""));
909           ar >> light;
910           t.AddLight(light);
911         }
912       }
913       ar.CloseChunk(); // LITS
914     }
915
916     // Entities
917     if( ar.GetResult() &&
918         ar.OpenChunk(FOURCC('E', 'N', 'T', 'S')) )
919     {
920       Dali::Entity root(Dali::Entity::New(""));
921       ar >> root;
922       t.SetRootEntity(root);
923       ar.CloseChunk(); // ENTS
924     }
925
926     // Animations
927     if( ar.GetResult() &&
928         ar.OpenChunk(FOURCC('A', 'N', 'I', 'S')) )
929     {
930       ar >> count;
931       if( count )
932       {
933         ModelAnimationMapContainer& mapContainer( t.GetAnimationMapContainer() );
934         mapContainer.reserve(count);
935         for( unsigned int i = 0; i < count; ++i)
936         {
937           ModelAnimationMap animation;
938           ar >> animation;
939           mapContainer.push_back(animation);
940         }
941       }
942       ar.CloseChunk(); // ANIS
943     }
944
945     ar.CloseChunk(); // DALI
946   }
947   return ar;
948 }
949
950 } // namespace Serialize
951
952 } // Internal
953
954 } // Dali