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/images/resource-image-impl.h>
31 #include <dali/internal/event/modeling/entity-impl.h>
32 #include <dali/internal/event/modeling/material-impl.h>
33 #include <dali/internal/event/modeling/mesh-impl.h>
34 #include <dali/internal/event/modeling/model-archive.h>
35 #include <dali/internal/update/modeling/internal-mesh-data.h>
39 const unsigned int ModelDataFormatVersion = 0x10004;
49 using namespace Serialize;
51 ModelData::ModelData()
55 DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
58 ModelDataPtr ModelData::New(const std::string& name)
60 DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
62 ModelDataPtr modelData(new ModelData());
63 modelData->SetName(name);
67 ModelData::~ModelData()
69 DALI_LOG_TRACE_METHOD(Debug::Filter::gModel);
73 mAnimationMaps.clear();
77 void ModelData::SetName(const std::string& name)
82 const std::string& ModelData::GetName() const
87 void ModelData::SetRootEntity(Dali::Entity root)
92 Dali::Entity ModelData::GetRootEntity() const
97 void ModelData::AddMesh(const Dali::MeshData& mesh)
99 mMeshes.PushBack( new Dali::MeshData( mesh ) );
102 void ModelData::AddMeshTicket(ResourceTicketPtr ticket)
104 mMeshTickets.push_back(ticket);
107 const Dali::MeshData& ModelData::GetMesh(unsigned int index) const
109 DALI_ASSERT_ALWAYS( index < mMeshes.Size() && "Mesh index out of bounds" );
110 return *mMeshes[index];
113 Dali::MeshData& ModelData::GetMesh(unsigned int index)
115 DALI_ASSERT_ALWAYS( index < mMeshes.Size() && "Mesh index out of bounds" );
116 return *mMeshes[index];
119 const ResourceTicketPtr ModelData::GetMeshTicket(unsigned int index) const
121 DALI_ASSERT_ALWAYS( index < mMeshTickets.size() && "Mesh index out of bounds" );
122 return mMeshTickets[index];
125 unsigned int ModelData::NumberOfMeshes() const
127 return mMeshes.Size();
130 void ModelData::AddMaterial(Dali::Material material)
132 mMaterials.push_back(material);
135 Dali::Material ModelData::GetMaterial(unsigned int index) const
137 DALI_ASSERT_DEBUG(index < mMaterials.size());
139 return mMaterials[index];
142 unsigned int ModelData::NumberOfMaterials() const
144 return mMaterials.size();
147 ModelAnimationMapContainer& ModelData::GetAnimationMapContainer()
149 return mAnimationMaps;
152 const ModelAnimationMap* ModelData::GetAnimationMap (unsigned int index) const
154 const ModelAnimationMap* found(NULL);
155 if( index < mAnimationMaps.size() )
157 found = &mAnimationMaps[index];
162 const ModelAnimationMap* ModelData::GetAnimationMap (const std::string& name) const
164 const ModelAnimationMap* found(NULL);
166 for(ModelAnimationMapContainer::const_iterator iter=mAnimationMaps.begin();
167 iter != mAnimationMaps.end();
170 const ModelAnimationMap& animData = *iter;
171 if(animData.name == name)
181 bool ModelData::FindAnimation (const std::string& name, unsigned int& index) const
186 for(ModelAnimationMapContainer::const_iterator iter=mAnimationMaps.begin();
187 iter != mAnimationMaps.end();
190 const ModelAnimationMap& animData(*iter);
191 if(animData.name == name)
200 float ModelData::GetAnimationDuration(size_t animationIndex) const
202 DALI_ASSERT_DEBUG (animationIndex < mAnimationMaps.size ());
203 const ModelAnimationMap& animData( mAnimationMaps[animationIndex] );
204 return animData.duration;
207 unsigned int ModelData::NumberOfAnimationMaps() const
209 return mAnimationMaps.size();
212 void ModelData::AddLight(Dali::Light light)
214 mLights.push_back(light);
217 Dali::Light ModelData::GetLight(unsigned int index) const
220 if (index < mLights.size())
222 light = mLights[index];
227 unsigned int ModelData::NumberOfLights() const
229 return mLights.size();
232 void ModelData::Unpack( ResourceClient& resourceClient )
234 ResourcePolicy::DataRetention dataRetentionPolicy = resourceClient.GetResourceDataRetentionPolicy();
235 ResourcePolicy::Discardable discardable = (dataRetentionPolicy == ResourcePolicy::DALI_DISCARDS_ALL_DATA ) ? ResourcePolicy::DISCARD : ResourcePolicy::RETAIN;
237 bool scalable = false; /* scaling is transmitted through parent Node */
242 // Ensure that mesh tickets are in same order as the meshes they represent
243 for ( size_t meshIdx = 0; meshIdx < NumberOfMeshes(); meshIdx++ )
245 // Copy the mesh-data into an internal structure, and pass ownership to the resourceClient
246 OwnerPointer<MeshData> meshDataPtr( new MeshData( GetMesh( meshIdx ), discardable, scalable ));
247 ResourceTicketPtr meshTicket = resourceClient.AllocateMesh( meshDataPtr );
249 AddMeshTicket( meshTicket );
252 // TODO: Update to use image tickets directly. Would need access to ImageFactory.
253 for ( size_t materialIdx = 0; materialIdx < NumberOfMaterials(); materialIdx++ )
255 Dali::Material material = GetMaterial( materialIdx );
256 const std::string& diffuseFileName = material.GetDiffuseFileName();
257 if( ! diffuseFileName.empty() )
259 material.SetDiffuseTexture( Dali::ResourceImage::New( diffuseFileName ) );
262 const std::string& opacityFileName = material.GetOpacityTextureFileName();
263 if( ! opacityFileName.empty() )
265 material.SetOpacityTexture( Dali::ResourceImage::New( opacityFileName ) );
268 const std::string& normalMapFileName = material.GetNormalMapFileName();
269 if( ! normalMapFileName.empty() )
271 material.SetNormalMap( Dali::ResourceImage::New( normalMapFileName ) );
279 bool ModelData::Read(std::streambuf& buf)
281 InputArchive ar(buf, ModelDataFormatVersion);
283 return ar.GetResult();
286 bool ModelData::Write(std::streambuf& buf) const
288 OutputArchive ar(buf, ModelDataFormatVersion);
290 return ar.GetResult();
293 } // namespace Internal