Conversion to Apache 2.0 license
[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/proxy-object.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] parentNode The node to attach a scene-object to.
66    * This will not be displayed until a mesh is added with SetMesh().
67    * @return A handle to a newly allocated Dali resource.
68    */
69   static MeshAttachmentPtr New( const SceneGraph::Node& parentNode );
70
71   /**
72    * Set the mesh to be rendererd by this attachment
73    * @param[in] meshPtr  A mesh impl (may be null)
74    * @param[in] meshId   The resource id of the mesh
75    * @param[in] bones    Container of bones for this mesh
76    * @param[in] material The material
77    */
78   void SetMesh( const MeshIPtr        meshPtr,
79                 ResourceId            meshId,
80                 const BoneContainer&  bones,
81                 const MaterialIPtr    material );
82
83   /**
84    * Add a mesh to the collection of meshes being rendered by this attachment
85    * @param [in] meshResource A mesh resource
86    * @param[in] bones    Container of bones for this mesh
87    * @param [in] material Material
88    */
89   void SetMesh( const ResourceTicketPtr     meshResource,
90                 const BoneContainer&  bones,
91                 const MaterialIPtr    material );
92
93   /**
94    * Set the material of the mesh
95    * @param[in] materialPtr - a custom material to apply to this mesh
96    */
97   void SetMaterial( Internal::MaterialIPtr materialPtr );
98
99   /**
100    * Get the material of the mesh
101    * @return The internal material pointer
102    */
103   Internal::MaterialIPtr GetMaterial( ) const;
104
105   /**
106    * Disconnect any connected material from the mesh
107    */
108   void DisconnectMaterial( );
109
110   /**
111    * Set whether this mesh actor should be affected by lights in the
112    * scene.  If it set to false, then the mesh will be unaffected by
113    * lighting, and will be evenly lit without any shading.
114    * This property is not inherited.
115    * @param[in] affectedByLighting Whether the actor should be lit by the scene lighting.
116    */
117   void SetAffectedByLighting( bool affectedByLighting );
118
119   /**
120    * @return true if the actor is lit by the scene, or false if evenly lit.
121    */
122   bool IsAffectedByLighting();
123
124   /**
125    * Search the actor tree for all named bones in the mesh  and connect them.
126    * @param[in] rootActor - the root actor of the actor tree.
127    */
128   void BindBonesToMesh( Internal::ActorPtr rootActor );
129
130   /**
131    * Connects a boneActor to the mesh (allows the mesh renderer to do
132    * skeletal animation by calculating the actor transforms)
133    * @param[in] boneActor - the actor containing the bone's current position
134    * @param[in] boneIdx   - the index of the bone in this mesh
135    * @param[in] boneCount - the bone count
136    */
137   void ConnectBoneActor( Internal::ActorPtr boneActor, size_t boneIdx, size_t boneCount );
138
139   const MeshAndCustomMaterial& GetMesh() const
140   {
141     return mMesh;
142   }
143
144 protected:
145
146   /**
147    * A reference counted object may only be deleted by calling Unreference()
148    */
149   virtual ~MeshAttachment();
150
151 private:
152
153   /**
154    * First stage construction of a MeshAttachment.
155    * @param[in] stage Used to send messages to scene-graph.
156    */
157   MeshAttachment( Stage& stage );
158
159   /**
160    * @copydoc Dali::Internal::RenderableAttachment::OnStageConnection2()
161    */
162   virtual void OnStageConnection2();
163
164   /**
165    * @copydoc Dali::Internal::RenderableAttachment::OnStageDisconnection2()
166    */
167   virtual void OnStageDisconnection2();
168
169   /**
170    * @copydoc Dali::Internal::RenderableAttachment::GetSceneObject()
171    */
172   virtual const SceneGraph::RenderableAttachment& GetSceneObject() const;
173
174 protected:
175   /**
176    * Set the node for given bone index in the scene graph object
177    * @param[in] node The node to set
178    * @param[in] boneIdx The bone index
179    */
180   void SetBoneNode( SceneGraph::Node* node, size_t boneIdx );
181
182   /**
183    * Helper class for connecting Nodes to the scene-graph MeshAttachment
184    */
185   class Connector : public ProxyObject::Observer
186   {
187   public:
188     /**
189      * Create the helper class
190      */
191     Connector( Internal::ActorPtr boneActor, size_t boneIdx, MeshAttachment& meshAttachment);
192
193     /**
194      * Copy constructor required for vector storage
195      */
196     Connector(const Connector&);
197
198     /**
199      * Assignment constructor required for vector storage
200      */
201     Connector& operator=(const Connector& rhs);
202
203     /**
204      * Destructor
205      */
206     ~Connector();
207
208     /**
209      * Update the scene-graph mesh attachment with this node
210      */
211     void ConnectNode();
212
213   public: // From ProxyObject::Observer
214
215     /**
216      * @copydoc Dali::Internal::ProxyObject::Observer::SceneObjectAdded
217      */
218     virtual void SceneObjectAdded( ProxyObject& proxy );
219
220     /**
221      * @copydoc Dali::Internal::ProxyObject::Observer::SceneObjectAdded
222      */
223     virtual void SceneObjectRemoved( ProxyObject& proxy );
224
225     /**
226      * @copydoc Dali::Internal::ProxyObject::Observer::ProxyDestroyed
227      */
228     virtual void ProxyDestroyed( ProxyObject& proxy );
229
230   public:
231     MeshAttachment& mMeshAttachment; ///< Parent
232     Actor* mActor;   ///< Raw-pointer to the actor; not owned.
233     size_t mBoneIdx; ///< Index of the bone with which this connecter is associated
234   };
235   typedef OwnerContainer< Connector* > ConnectorList;
236
237 private:
238
239   const SceneGraph::MeshAttachment* mSceneObject; ///< Not owned
240
241   MeshAndCustomMaterial mMesh;
242   bool                  mAffectedByLighting;
243   ConnectorList         mConnectors;
244 };
245
246 } // namespace Internal
247
248 } // namespace Dali
249
250 #endif // __DALI_INTERNAL_MESH_ATTACHMENT_H__