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