b0ccb972c81c39a4d709f39e5a681fc5a9c06ae5
[platform/core/uifw/dali-core.git] / dali / internal / event / actor-attachments / mesh-attachment-impl.h
1 #ifndef __DALI_INTERNAL_MESH_ATTACHMENT_H__
2 #define __DALI_INTERNAL_MESH_ATTACHMENT_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/geometry/mesh.h>
23 #include <dali/public-api/modeling/material.h>
24 #include <dali/internal/common/owner-container.h>
25 #include <dali/internal/event/actors/actor-declarations.h>
26 #include <dali/internal/event/actor-attachments/actor-attachment-declarations.h>
27 #include <dali/internal/event/actor-attachments/renderable-attachment-impl.h>
28 #include <dali/internal/event/modeling/mesh-impl.h>
29 #include <dali/internal/event/modeling/material-impl.h>
30 #include <dali/internal/event/resources/resource-ticket.h>
31 #include <dali/internal/event/common/object-impl.h>
32
33 namespace Dali
34 {
35
36 namespace Internal
37 {
38
39 namespace SceneGraph
40 {
41 class MeshAttachment;
42 class Node;
43 }
44
45 typedef std::vector<std::string>               BoneNames;
46 typedef BoneNames::const_iterator              BoneNamesIter;
47
48 /**
49  * An attachment for rendering a 3D mesh with a material.
50  */
51 class MeshAttachment : public RenderableAttachment
52 {
53 public:
54
55   struct MeshAndCustomMaterial
56   {
57     Internal::MeshIPtr          mMesh;
58     Internal::MaterialIPtr      mMaterial;
59     Internal::MaterialIPtr      mCustomMaterial;
60     BoneNames                   mBoneNames;
61   };
62
63   /**
64    * Create an initialised MeshAttachment.
65    * @param[in] stage The stage to use for messaging
66    * @param[in] parentNode The node to attach a scene-object to.
67    * This will not be displayed until a mesh is added with SetMesh().
68    * @return A handle to a newly allocated Dali resource.
69    */
70   static MeshAttachmentPtr New( Stage& stage, const SceneGraph::Node& parentNode );
71
72   /**
73    * Set the mesh to be rendererd by this attachment
74    * @param[in] meshPtr  A mesh impl (may be null)
75    * @param[in] meshId   The resource id of the mesh
76    * @param[in] bones    Container of bones for this mesh
77    * @param[in] material The material
78    */
79   void SetMesh( const MeshIPtr        meshPtr,
80                 ResourceId            meshId,
81                 const BoneContainer&  bones,
82                 const MaterialIPtr    material );
83
84   /**
85    * Add a mesh to the collection of meshes being rendered by this attachment
86    * @param [in] meshResource A mesh resource
87    * @param[in] bones    Container of bones for this mesh
88    * @param [in] material Material
89    */
90   void SetMesh( const ResourceTicketPtr     meshResource,
91                 const BoneContainer&  bones,
92                 const MaterialIPtr    material );
93
94   /**
95    * Set the material of the mesh
96    * @param[in] materialPtr - a custom material to apply to this mesh
97    */
98   void SetMaterial( Internal::MaterialIPtr materialPtr );
99
100   /**
101    * Get the material of the mesh
102    * @return The internal material pointer
103    */
104   Internal::MaterialIPtr GetMaterial( ) const;
105
106   /**
107    * Disconnect any connected material from the mesh
108    */
109   void DisconnectMaterial( );
110
111   /**
112    * Set whether this mesh actor should be affected by lights in the
113    * scene.  If it set to false, then the mesh will be unaffected by
114    * lighting, and will be evenly lit without any shading.
115    * This property is not inherited.
116    * @param[in] affectedByLighting Whether the actor should be lit by the scene lighting.
117    */
118   void SetAffectedByLighting( bool affectedByLighting );
119
120   /**
121    * @return true if the actor is lit by the scene, or false if evenly lit.
122    */
123   bool IsAffectedByLighting();
124
125   /**
126    * Search the actor tree for all named bones in the mesh  and connect them.
127    * @param[in] rootActor - the root actor of the actor tree.
128    */
129   void BindBonesToMesh( Internal::ActorPtr rootActor );
130
131   /**
132    * Connects a boneActor to the mesh (allows the mesh renderer to do
133    * skeletal animation by calculating the actor transforms)
134    * @param[in] boneActor - the actor containing the bone's current position
135    * @param[in] boneIdx   - the index of the bone in this mesh
136    * @param[in] boneCount - the bone count
137    */
138   void ConnectBoneActor( Internal::ActorPtr boneActor, size_t boneIdx, size_t boneCount );
139
140   const MeshAndCustomMaterial& GetMesh() const
141   {
142     return mMesh;
143   }
144
145 protected:
146
147   /**
148    * A reference counted object may only be deleted by calling Unreference()
149    */
150   virtual ~MeshAttachment();
151
152 private:
153
154   /**
155    * First stage construction of a MeshAttachment.
156    * @param[in] stage Used to send messages to scene-graph.
157    */
158   MeshAttachment( Stage& stage );
159
160   /**
161    * @copydoc Dali::Internal::RenderableAttachment::OnStageConnection2()
162    */
163   virtual void OnStageConnection2();
164
165   /**
166    * @copydoc Dali::Internal::RenderableAttachment::OnStageDisconnection2()
167    */
168   virtual void OnStageDisconnection2();
169
170   /**
171    * @copydoc Dali::Internal::RenderableAttachment::GetSceneObject()
172    */
173   virtual const SceneGraph::RenderableAttachment& GetSceneObject() const;
174
175 protected:
176   /**
177    * Set the node for given bone index in the scene graph object
178    * @param[in] node The node to set
179    * @param[in] boneIdx The bone index
180    */
181   void SetBoneNode( SceneGraph::Node* node, size_t boneIdx );
182
183   /**
184    * Helper class for connecting Nodes to the scene-graph MeshAttachment
185    */
186   class Connector : public Object::Observer
187   {
188   public:
189     /**
190      * Create the helper class
191      */
192     Connector( Internal::ActorPtr boneActor, size_t boneIdx, MeshAttachment& meshAttachment);
193
194     /**
195      * Copy constructor required for vector storage
196      */
197     Connector(const Connector&);
198
199     /**
200      * Assignment constructor required for vector storage
201      */
202     Connector& operator=(const Connector& rhs);
203
204     /**
205      * Destructor
206      */
207     ~Connector();
208
209     /**
210      * Update the scene-graph mesh attachment with this node
211      */
212     void ConnectNode();
213
214   public: // From Object::Observer
215
216     /**
217      * @copydoc Dali::Internal::Object::Observer::SceneObjectAdded
218      */
219     virtual void SceneObjectAdded( Object& object );
220
221     /**
222      * @copydoc Dali::Internal::Object::Observer::SceneObjectAdded
223      */
224     virtual void SceneObjectRemoved( Object& object );
225
226     /**
227      * @copydoc Dali::Internal::Object::Observer::ObjectDestroyed
228      */
229     virtual void ObjectDestroyed( Object& object );
230
231   public:
232     MeshAttachment& mMeshAttachment; ///< Parent
233     Actor* mActor;   ///< Raw-pointer to the actor; not owned.
234     size_t mBoneIdx; ///< Index of the bone with which this connecter is associated
235   };
236   typedef OwnerContainer< Connector* > ConnectorList;
237
238 private:
239
240   const SceneGraph::MeshAttachment* mSceneObject; ///< Not owned
241
242   MeshAndCustomMaterial mMesh;
243   bool                  mAffectedByLighting;
244   ConnectorList         mConnectors;
245 };
246
247 } // namespace Internal
248
249 } // namespace Dali
250
251 #endif // __DALI_INTERNAL_MESH_ATTACHMENT_H__