135333a64b7fddda659ca18f1a39f52cec7b855e
[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    * Search the actor tree for all named bones in the mesh  and connect them.
113    * @param[in] rootActor - the root actor of the actor tree.
114    */
115   void BindBonesToMesh( Internal::ActorPtr rootActor );
116
117   /**
118    * Connects a boneActor to the mesh (allows the mesh renderer to do
119    * skeletal animation by calculating the actor transforms)
120    * @param[in] boneActor - the actor containing the bone's current position
121    * @param[in] boneIdx   - the index of the bone in this mesh
122    * @param[in] boneCount - the bone count
123    */
124   void ConnectBoneActor( Internal::ActorPtr boneActor, size_t boneIdx, size_t boneCount );
125
126   const MeshAndCustomMaterial& GetMesh() const
127   {
128     return mMesh;
129   }
130
131 protected:
132
133   /**
134    * A reference counted object may only be deleted by calling Unreference()
135    */
136   virtual ~MeshAttachment();
137
138 private:
139
140   /**
141    * First stage construction of a MeshAttachment.
142    * @param[in] stage Used to send messages to scene-graph.
143    */
144   MeshAttachment( Stage& stage );
145
146   /**
147    * @copydoc Dali::Internal::RenderableAttachment::OnStageConnection2()
148    */
149   virtual void OnStageConnection2();
150
151   /**
152    * @copydoc Dali::Internal::RenderableAttachment::OnStageDisconnection2()
153    */
154   virtual void OnStageDisconnection2();
155
156   /**
157    * @copydoc Dali::Internal::RenderableAttachment::GetSceneObject()
158    */
159   virtual const SceneGraph::RenderableAttachment& GetSceneObject() const;
160
161 protected:
162   /**
163    * Set the node for given bone index in the scene graph object
164    * @param[in] node The node to set
165    * @param[in] boneIdx The bone index
166    */
167   void SetBoneNode( SceneGraph::Node* node, size_t boneIdx );
168
169   /**
170    * Helper class for connecting Nodes to the scene-graph MeshAttachment
171    */
172   class Connector : public Object::Observer
173   {
174   public:
175     /**
176      * Create the helper class
177      */
178     Connector( Internal::ActorPtr boneActor, size_t boneIdx, MeshAttachment& meshAttachment);
179
180     /**
181      * Copy constructor required for vector storage
182      */
183     Connector(const Connector&);
184
185     /**
186      * Assignment constructor required for vector storage
187      */
188     Connector& operator=(const Connector& rhs);
189
190     /**
191      * Destructor
192      */
193     ~Connector();
194
195     /**
196      * Update the scene-graph mesh attachment with this node
197      */
198     void ConnectNode();
199
200   public: // From Object::Observer
201
202     /**
203      * @copydoc Dali::Internal::Object::Observer::SceneObjectAdded
204      */
205     virtual void SceneObjectAdded( Object& object );
206
207     /**
208      * @copydoc Dali::Internal::Object::Observer::SceneObjectAdded
209      */
210     virtual void SceneObjectRemoved( Object& object );
211
212     /**
213      * @copydoc Dali::Internal::Object::Observer::ObjectDestroyed
214      */
215     virtual void ObjectDestroyed( Object& object );
216
217   public:
218     MeshAttachment& mMeshAttachment; ///< Parent
219     Actor* mActor;   ///< Raw-pointer to the actor; not owned.
220     size_t mBoneIdx; ///< Index of the bone with which this connecter is associated
221   };
222   typedef OwnerContainer< Connector* > ConnectorList;
223
224 private:
225
226   const SceneGraph::MeshAttachment* mSceneObject; ///< Not owned
227
228   MeshAndCustomMaterial mMesh;
229   bool                  mAffectedByLighting;
230   ConnectorList         mConnectors;
231 };
232
233 } // namespace Internal
234
235 } // namespace Dali
236
237 #endif // __DALI_INTERNAL_MESH_ATTACHMENT_H__