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