[dali_1.0.9] Merge branch 'tizen'
[platform/core/uifw/dali-core.git] / dali / internal / update / node-attachments / scene-graph-mesh-attachment.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_MESH_ATTACHMENT_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_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/internal/common/buffer-index.h>
23 #include <dali/internal/common/event-to-update.h>
24 #include <dali/internal/update/common/property-owner.h>
25 #include <dali/internal/update/node-attachments/scene-graph-renderable-attachment.h>
26 #include <dali/internal/update/modeling/bone-transforms.h>
27 #include <dali/internal/event/actor-attachments/mesh-attachment-impl.h>
28 #include <dali/internal/event/actors/mesh-actor-impl.h>
29 #include <dali/public-api/geometry/mesh.h>
30 #include <dali/public-api/math/matrix.h>
31
32 namespace Dali
33 {
34
35 namespace Internal
36 {
37
38 class ResourceManager;
39
40 namespace SceneGraph
41 {
42 class Mesh;
43 class MeshRenderer;
44 class Material;
45 class Shader;
46
47 /**
48  * An attachment for rendering a mesh with a material.
49  */
50 class MeshAttachment : public RenderableAttachment, public PropertyOwner::Observer
51 {
52 public:
53
54   typedef std::vector<Node*> BoneNodeContainer;
55
56   struct MeshMatBones
57   {
58     MeshMatBones()
59     : meshResourceId( 0 ),
60       mesh( NULL ),
61       material( NULL ),
62       boneCount( 0 )
63     {
64     }
65
66     ResourceId                  meshResourceId;
67     SceneGraph::Mesh*           mesh;
68     const SceneGraph::Material* material;
69     std::size_t                 boneCount;
70     BoneNodeContainer           boneNodes;
71   };
72
73   /**
74    * Construct a new MeshAttachment.
75    * @return a new MeshAttachment
76    */
77   static MeshAttachment* New();
78
79   /**
80    * Virtual destructor
81    */
82   virtual ~MeshAttachment();
83
84   /**
85    * TODO: remove geometry scaling and this method can be gone
86    * @copydoc RenderableAttachment::ResolveVisibility
87    */
88   virtual bool ResolveVisibility( BufferIndex updateBufferIndex )
89   {
90     mHasSizeAndColorFlag = false;
91     const Vector4& color = mParent->GetWorldColor( updateBufferIndex );
92     if( color.a > FULLY_TRANSPARENT )               // not fully transparent
93     {
94       mHasSizeAndColorFlag = true;
95     }
96     return mHasSizeAndColorFlag;
97   }
98
99   /**
100    * @copydoc RenderableAttachment::GetRenderer().
101    */
102   virtual Renderer& GetRenderer();
103
104   /**
105    * @copydoc RenderableAttachment::GetRenderer().
106    */
107   virtual const Renderer& GetRenderer() const;
108
109   /**
110    * @copydoc Dali::Internal::SceneGraph::RenderableAttachment::ShaderChanged()
111    */
112   virtual void ShaderChanged( BufferIndex updateBufferIndex );
113
114   /**
115    * @copydoc RenderableAttachment::SizeChanged()
116    */
117   virtual void SizeChanged( BufferIndex updateBufferIndex );
118
119   /**
120    * @copydoc Dali::Internal::SceneGraph::RenderableAttachment::DoPrepareRender()
121    */
122   virtual void DoPrepareRender( BufferIndex updateBufferIndex );
123
124   /**
125    * @copydoc RenderableAttachment::IsFullyOpaque()
126    */
127   virtual bool IsFullyOpaque( BufferIndex updateBufferIndex );
128
129   /**
130    * @copydoc Dali::Internal::SceneGraph::RenderableAttachment::DoGetScaleForSize()
131    */
132   virtual void DoGetScaleForSize( const Vector3& nodeSize, Vector3& scaling );
133
134   /**
135    * Set the mesh to be rendererd by this attachment
136    * @param[in] meshId   The resource id of the mesh
137    * @param[in] material The material
138    * @param[in] boneCount The bone count
139    */
140   void SetMesh( ResourceId meshId, const SceneGraph::Material* material, std::size_t boneCount );
141
142   /**
143    * @copydoc Dali::Internal::MeshActor::SetMaterial(Material material)
144    */
145   void SetMaterial( const SceneGraph::Material* material );
146
147   /**
148    * @copydoc Dali::Internal::MeshAttachment::SetAffectedByLighting(bool);
149    */
150   void SetAffectedByLighting( bool affectedByLighting );
151
152   /**
153    * Set the node for the given mesh index and bone index
154    */
155   void SetBoneNode( Node* boneNode, size_t boneIdx, size_t boneCount );
156
157   /**
158    * Calculate matrices for bones based on bone node transforms.
159    */
160   void CalculateBoneTransforms( BufferIndex               updateBufferIndex,
161                                 const Mesh*               mesh,
162                                 const BoneNodeContainer&  boneNodes,
163                                 BoneTransforms&           boneTransforms );
164
165 private:
166   /**
167    * @copydoc PropertyOwner::Observer::PropertyOwnerDestroyed()
168    */
169   virtual void PropertyOwnerDestroyed( PropertyOwner& owner );
170
171   /**
172    * @copydoc RenderableAttachment::ConnectToSceneGraph2()
173    */
174   virtual void ConnectToSceneGraph2( BufferIndex updateBufferIndex );
175
176   /**
177    * @copydoc RenderableAttachment::OnDestroy2()
178    */
179   virtual void OnDestroy2();
180
181   /**
182    * @copydoc Dali::Internal::SceneGraph::RenderableAttachment::DoPrepareResources()
183    */
184   virtual bool DoPrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager );
185
186   /**
187    * Protected constructor. See New()
188    */
189   MeshAttachment();
190
191   // Undefined
192   MeshAttachment(const MeshAttachment&);
193
194   // Undefined
195   MeshAttachment& operator=(const MeshAttachment& rhs);
196
197 private:
198
199   MeshRenderer* mRenderer; ///< Raw-pointer to renderer that is owned by RenderManager
200
201   MeshMatBones  mMesh; ///< The mesh used to prepare for rendering, with associated materials and bones
202
203   bool          mAffectedByLighting:1;    ///< lighting mode.
204   bool          mLastLightingState:1;     ///< lighting state used at the last update.
205   bool          mLightInitialized:1;      ///< whether the light state has been transmitted to the mesh renderer
206
207 };
208
209 // Messages for MeshAttachment
210
211 inline void SetMeshMessage( EventToUpdate& eventToUpdate, const MeshAttachment& attachment, ResourceId meshId, const Material* material, std::size_t boneCount )
212 {
213   typedef MessageValue3< MeshAttachment, ResourceId, const Material*, std::size_t > LocalType;
214
215   // Reserve some memory inside the message queue
216   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
217
218   // Construct message in the message queue memory; note that delete should not be called on the return value
219   new (slot) LocalType( &attachment, &MeshAttachment::SetMesh, meshId, material, boneCount );
220 }
221
222 inline void SetMaterialMessage( EventToUpdate& eventToUpdate, const MeshAttachment& attachment, const Material* material )
223 {
224   typedef MessageValue1< MeshAttachment, const Material* > LocalType;
225
226   // Reserve some memory inside the message queue
227   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
228
229   // Construct message in the message queue memory; note that delete should not be called on the return value
230   new (slot) LocalType( &attachment, &MeshAttachment::SetMaterial, material );
231 }
232
233 inline void SetAffectedByLightingMessage( EventToUpdate& eventToUpdate, const MeshAttachment& attachment, bool affectedByLighting )
234 {
235   typedef MessageValue1< MeshAttachment, bool > LocalType;
236
237   // Reserve some memory inside the message queue
238   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
239
240   // Construct message in the message queue memory; note that delete should not be called on the return value
241   new (slot) LocalType( &attachment, &MeshAttachment::SetAffectedByLighting, affectedByLighting );
242 }
243
244 inline void SetBoneNodeMessage( EventToUpdate& eventToUpdate, const MeshAttachment& attachment, Node* boneNode, size_t boneIdx, size_t boneCount )
245 {
246   typedef MessageValue3< MeshAttachment, Node*, size_t, size_t > LocalType;
247
248   // Reserve some memory inside the message queue
249   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
250
251   // Construct message in the message queue memory; note that delete should not be called on the return value
252   new (slot) LocalType( &attachment, &MeshAttachment::SetBoneNode, boneNode, boneIdx, boneCount );
253 }
254
255 } // namespace SceneGraph
256
257 } // namespace Internal
258
259 } // namespace Dali
260
261 #endif // __DALI_INTERNAL_SCENE_GRAPH_MESH_ATTACHMENT_H__