Merge "Follow the include-order coding conventions" into tizen
[platform/core/uifw/dali-core.git] / dali / internal / event / actors / mesh-actor-impl.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/actors/mesh-actor-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/internal/event/modeling/model-impl.h>
24 #include <dali/internal/event/modeling/animatable-mesh-impl.h>
25 #include <dali/internal/event/modeling/mesh-impl.h>
26
27 namespace Dali
28 {
29
30 using Internal::MeshIter;
31 using Internal::MeshIPtr;
32 using Internal::MaterialIPtr;
33
34 namespace Internal
35 {
36
37
38 namespace
39 {
40
41 BaseHandle Create()
42 {
43   return Dali::MeshActor::New();
44 }
45
46 TypeRegistration mType( typeid(Dali::MeshActor), typeid(Dali::RenderableActor), Create );
47
48 }
49
50 MeshActorPtr MeshActor::New()
51 {
52   MeshActorPtr actor(new MeshActor());
53
54   // Second-phase construction
55   actor->Initialize();
56
57   // Create the attachment
58   actor->mMeshAttachment = MeshAttachment::New( *actor->mStage, *actor->mNode );
59   actor->Attach(*actor->mMeshAttachment);
60   actor->SetCullFace( Dali::CullBack );
61
62   return actor;
63 }
64
65 MeshActorPtr MeshActor::New(Dali::Mesh mesh)
66 {
67   MeshActorPtr actor = MeshActor::New();
68
69   if(actor)
70   {
71     actor->SetMesh(mesh);
72   }
73
74   return actor;
75 }
76
77 MeshActorPtr MeshActor::New(Dali::AnimatableMesh mesh)
78 {
79   MeshActorPtr actor = MeshActor::New();
80
81   if(actor)
82   {
83     actor->SetMesh(mesh);
84   }
85
86   return actor;
87 }
88
89
90 MeshActorPtr MeshActor::New(ModelDataPtr modelData, Dali::Entity entity)
91 {
92   MeshActorPtr actor = MeshActor::New();
93
94   actor->SetName(entity.GetName());
95
96   DALI_ASSERT_ALWAYS(entity.NumberOfMeshes() == 1 && "Dali does not support multiple meshes per node in the model");
97
98   actor->SetMesh(modelData, entity.GetMeshByIndex(0)); // only use the first mesh
99
100   Matrix transform(entity.GetTransformMatrix());
101   Vector3 position;
102   Quaternion rotation;
103   Vector3 scale;
104   transform.GetTransformComponents(position, rotation, scale);
105   actor->SetPosition(position);
106   actor->SetRotation(rotation);
107   actor->SetScale(scale);
108
109   return actor;
110 }
111
112 MeshActor::MeshActor()
113 : RenderableActor()
114 {
115 }
116
117 MeshActor::~MeshActor()
118 {
119 }
120
121 void MeshActor::SetMesh(Dali::Mesh mesh)
122 {
123   SetMesh( &GetImplementation(mesh) );
124 }
125
126 void MeshActor::SetMesh(Dali::AnimatableMesh mesh)
127 {
128   mAnimatableMeshHandle = mesh;
129   SetMesh( GetImplementation(mesh).GetMesh() );
130
131   SetInitialVolume(Vector3(1.0f, 1.0f, 1.0f));
132 }
133
134 void MeshActor::SetMesh( MeshIPtr meshPtr )
135 {
136   mMeshAttachment->SetMesh( meshPtr, meshPtr->GetResourceId(), meshPtr->GetBones(),  meshPtr->GetMaterial() );
137 }
138
139 // Used by Internal::ModelActorFactory
140 void MeshActor::SetMesh(ModelDataPtr modelData, unsigned int meshIndex)
141 {
142   ResourceTicketPtr meshTicket = modelData->GetMeshTicket(meshIndex);
143   const Dali::MeshData& meshData = modelData->GetMesh(meshIndex);
144   Dali::Material material = meshData.GetMaterial();
145   DALI_ASSERT_ALWAYS( material && "No material found" );
146   MaterialIPtr matPtr = &GetImplementation(material);
147
148   mMeshAttachment->SetMesh(meshTicket, meshData.GetBones(), matPtr);
149 }
150
151 void MeshActor::SetMaterial(const Dali::Material material)
152 {
153   MaterialIPtr materialPtr = const_cast<Internal::Material*>(&GetImplementation(material));
154   mMeshAttachment->SetMaterial(materialPtr);
155 }
156
157 Dali::Material MeshActor::GetMaterial() const
158 {
159   Dali::Material material;
160
161   Internal::MaterialIPtr internalPtr = mMeshAttachment->GetMaterial( );
162
163   if(internalPtr)
164   {
165     material = Dali::Material(internalPtr.Get());
166   }
167
168   return material;
169 }
170
171 void MeshActor::SetAffectedByLighting(bool affectedByLighting)
172 {
173   mMeshAttachment->SetAffectedByLighting( affectedByLighting );
174 }
175
176 bool MeshActor::IsAffectedByLighting()
177 {
178   return mMeshAttachment->IsAffectedByLighting();
179 }
180
181 void MeshActor::BindBonesToMesh(Internal::ActorPtr rootActor)
182 {
183   mMeshAttachment->BindBonesToMesh(rootActor);
184 }
185
186 } // namespace Internal
187
188 } // namespace Dali