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.
19 #include <dali/internal/event/modeling/model-data-impl.h>
26 #include <dali/public-api/common/light.h>
27 #include <dali/integration-api/debug.h>
28 #include <dali/integration-api/platform-abstraction.h>
29 #include <dali/internal/event/common/thread-local-storage.h>
30 #include <dali/internal/event/modeling/entity-impl.h>
31 #include <dali/internal/event/modeling/material-impl.h>
32 #include <dali/internal/event/modeling/mesh-impl.h>
33 #include <dali/internal/event/modeling/model-archive.h>
34 #include <dali/internal/update/modeling/internal-mesh-data.h>
38 const unsigned int ModelDataFormatVersion = 0x10004;
48 using namespace Serialize;
50 ModelData::ModelData()
54 DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
57 ModelDataPtr ModelData::New(const std::string& name)
59 DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
61 ModelDataPtr modelData(new ModelData());
62 modelData->SetName(name);
66 ModelData::~ModelData()
68 DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
72 mAnimationMaps.clear();
76 void ModelData::SetName(const std::string& name)
81 const std::string& ModelData::GetName() const
86 void ModelData::SetRootEntity(Dali::Entity root)
91 Dali::Entity ModelData::GetRootEntity() const
96 void ModelData::AddMesh(const Dali::MeshData& mesh)
98 mMeshes.PushBack( new Dali::MeshData( mesh ) );
101 void ModelData::AddMeshTicket(ResourceTicketPtr ticket)
103 mMeshTickets.push_back(ticket);
106 const Dali::MeshData& ModelData::GetMesh(unsigned int index) const
108 DALI_ASSERT_ALWAYS( index < mMeshes.Size() && "Mesh index out of bounds" );
109 return *mMeshes[index];
112 Dali::MeshData& ModelData::GetMesh(unsigned int index)
114 DALI_ASSERT_ALWAYS( index < mMeshes.Size() && "Mesh index out of bounds" );
115 return *mMeshes[index];
118 const ResourceTicketPtr ModelData::GetMeshTicket(unsigned int index) const
120 DALI_ASSERT_ALWAYS( index < mMeshTickets.size() && "Mesh index out of bounds" );
121 return mMeshTickets[index];
124 unsigned int ModelData::NumberOfMeshes() const
126 return mMeshes.Size();
129 void ModelData::AddMaterial(Dali::Material material)
131 mMaterials.push_back(material);
134 Dali::Material ModelData::GetMaterial(unsigned int index) const
136 DALI_ASSERT_DEBUG(index < mMaterials.size());
138 return mMaterials[index];
141 unsigned int ModelData::NumberOfMaterials() const
143 return mMaterials.size();
146 ModelAnimationMapContainer& ModelData::GetAnimationMapContainer()
148 return mAnimationMaps;
151 const ModelAnimationMap* ModelData::GetAnimationMap (unsigned int index) const
153 const ModelAnimationMap* found(NULL);
154 if( index < mAnimationMaps.size() )
156 found = &mAnimationMaps[index];
161 const ModelAnimationMap* ModelData::GetAnimationMap (const std::string& name) const
163 const ModelAnimationMap* found(NULL);
165 for(ModelAnimationMapContainer::const_iterator iter=mAnimationMaps.begin();
166 iter != mAnimationMaps.end();
169 const ModelAnimationMap& animData = *iter;
170 if(animData.name == name)
180 bool ModelData::FindAnimation (const std::string& name, unsigned int& index) const
185 for(ModelAnimationMapContainer::const_iterator iter=mAnimationMaps.begin();
186 iter != mAnimationMaps.end();
189 const ModelAnimationMap& animData(*iter);
190 if(animData.name == name)
199 float ModelData::GetAnimationDuration(size_t animationIndex) const
201 DALI_ASSERT_DEBUG (animationIndex < mAnimationMaps.size ());
202 const ModelAnimationMap& animData( mAnimationMaps[animationIndex] );
203 return animData.duration;
206 unsigned int ModelData::NumberOfAnimationMaps() const
208 return mAnimationMaps.size();
211 void ModelData::AddLight(Dali::Light light)
213 mLights.push_back(light);
216 Dali::Light ModelData::GetLight(unsigned int index) const
219 if (index < mLights.size())
221 light = mLights[index];
226 unsigned int ModelData::NumberOfLights() const
228 return mLights.size();
231 void ModelData::Unpack( ResourceClient& resourceClient )
233 ResourcePolicy::DataRetention dataRetentionPolicy = resourceClient.GetResourceDataRetentionPolicy();
234 ResourcePolicy::Discardable discardable = (dataRetentionPolicy == ResourcePolicy::DALI_DISCARDS_ALL_DATA ) ? ResourcePolicy::DISCARD : ResourcePolicy::RETAIN;
236 bool scalable = false; /* scaling is transmitted through parent Node */
241 // Ensure that mesh tickets are in same order as the meshes they represent
242 for ( size_t meshIdx = 0; meshIdx < NumberOfMeshes(); meshIdx++ )
244 // Copy the mesh-data into an internal structure, and pass ownership to the resourceClient
245 OwnerPointer<MeshData> meshDataPtr( new MeshData( GetMesh( meshIdx ), discardable, scalable ));
246 ResourceTicketPtr meshTicket = resourceClient.AllocateMesh( meshDataPtr );
248 AddMeshTicket( meshTicket );
251 // TODO: Update to use image tickets directly. Would need access to ImageFactory.
252 for ( size_t materialIdx = 0; materialIdx < NumberOfMaterials(); materialIdx++ )
254 Dali::Material material = GetMaterial( materialIdx );
255 const std::string& diffuseFileName = material.GetDiffuseFileName();
256 if( ! diffuseFileName.empty() )
258 material.SetDiffuseTexture( Dali::Image::New( diffuseFileName ) );
261 const std::string& opacityFileName = material.GetOpacityTextureFileName();
262 if( ! opacityFileName.empty() )
264 material.SetOpacityTexture( Dali::Image::New( opacityFileName ) );
267 const std::string& normalMapFileName = material.GetNormalMapFileName();
268 if( ! normalMapFileName.empty() )
270 material.SetNormalMap( Dali::Image::New( normalMapFileName ) );
278 bool ModelData::Read(std::streambuf& buf)
280 InputArchive ar(buf, ModelDataFormatVersion);
282 return ar.GetResult();
285 bool ModelData::Write(std::streambuf& buf) const
287 OutputArchive ar(buf, ModelDataFormatVersion);
289 return ar.GetResult();
292 } // namespace Internal