Merge branch 'devel/master' into tizen
[platform/core/uifw/dali-core.git] / dali / internal / update / modeling / scene-graph-animatable-mesh.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/update/modeling/scene-graph-animatable-mesh.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/update/common/property-owner.h>
23 #include <dali/internal/update/resources/resource-manager.h>
24 #include <dali/internal/update/modeling/scene-graph-mesh.h>
25
26 namespace Dali
27 {
28
29 namespace Internal
30 {
31
32 namespace SceneGraph
33 {
34
35 AnimatableMesh::AnimatableMesh( ResourceManager&                  resourceManager,
36                                 ResourceId                        meshId,
37                                 const MeshData::VertexContainer& vertices )
38 : mResourceManager(resourceManager),
39   mMeshId(meshId)
40 {
41   mNumVertices = vertices.size();
42
43   // Allocate a single block of memory for all properties
44   // (Can't use std::vector, as AnimatableProperties is a non-copyable type).
45   mVertices = static_cast<VertexProperties*>( operator new [] (sizeof(VertexProperties) * mNumVertices) );
46
47   for(unsigned int i=0; i<mNumVertices; i++)
48   {
49     // Use placement new to instantiate the vertex properties
50     new ( &mVertices[i] ) VertexProperties( vertices[i] );
51   }
52 }
53
54 AnimatableMesh::~AnimatableMesh()
55 {
56   for(unsigned int i=0; i<mNumVertices; i++)
57   {
58     // Used placement new to instantiate these objects, so have to directly call destructors
59     mVertices[i].~VertexProperties();
60   }
61   operator delete [] (mVertices);
62 }
63
64 void AnimatableMesh::UpdateMesh( BufferIndex updateBufferIndex )
65 {
66   // Copy properties to associated scenegraph mesh
67   SceneGraph::Mesh* mesh(mResourceManager.GetMesh(mMeshId));
68   DALI_ASSERT_DEBUG( mesh );
69
70   // TODO: Should be double buffered - pass in buffer index
71   MeshData& meshData = mesh->GetMeshData(Mesh::UPDATE_THREAD);
72
73   bool meshUpdated( false );
74   MeshData::VertexContainer& oldVertices = meshData.GetVertices();
75   DALI_ASSERT_DEBUG( oldVertices.size() == mNumVertices );
76   DALI_ASSERT_DEBUG( oldVertices.size() > 0 );
77
78   MeshData::Vertex* vertices = &oldVertices.at(0);
79   for(std::size_t i=0; i<mNumVertices; i++)
80   {
81     if ( ! mVertices[i].position.IsClean() )
82     {
83       const Vector3& position = GetPosition(updateBufferIndex, i);
84       vertices[i].x = position.x;
85       vertices[i].y = position.y;
86       vertices[i].z = position.z;
87
88       meshUpdated = true;
89     }
90
91     if ( ! mVertices[i].color.IsClean() )
92     {
93       const Vector4& color = GetColor(updateBufferIndex, i);
94       vertices[i].vertexR = color.r;
95       vertices[i].vertexG = color.g;
96       vertices[i].vertexB = color.b;
97
98       meshUpdated = true;
99     }
100
101     if ( ! mVertices[i].textureCoords.IsClean() )
102     {
103       const Vector2& texcoords = GetTextureCoords(updateBufferIndex, i);
104       vertices[i].u = texcoords.x;
105       vertices[i].v = texcoords.y;
106
107       meshUpdated = true;
108     }
109   }
110
111   if ( meshUpdated )
112   {
113     mesh->MeshDataUpdated(updateBufferIndex, Mesh::UPDATE_THREAD, NULL);
114   }
115 }
116
117 void AnimatableMesh::BakePosition( BufferIndex updateBufferIndex, unsigned int vertex, const Vector3& position )
118 {
119   mVertices[vertex].position.Bake( updateBufferIndex, position );
120 }
121
122 void AnimatableMesh::BakeColor( BufferIndex updateBufferIndex, unsigned int vertex, const Vector4& color )
123 {
124   mVertices[vertex].color.Bake( updateBufferIndex, color );
125 }
126
127 void AnimatableMesh::BakeTextureCoords( BufferIndex updateBufferIndex, unsigned int vertex, const Vector2& coords )
128 {
129   mVertices[vertex].textureCoords.Bake( updateBufferIndex, coords );
130 }
131
132 const Vector3& AnimatableMesh::GetPosition( BufferIndex bufferIndex, unsigned int vertexIndex )
133 {
134   return mVertices[vertexIndex].position.Get(bufferIndex);
135 }
136
137 const Vector4& AnimatableMesh::GetColor( BufferIndex bufferIndex, unsigned int vertexIndex )
138 {
139   return mVertices[vertexIndex].color.Get(bufferIndex);
140 }
141
142 const Vector2& AnimatableMesh::GetTextureCoords( BufferIndex bufferIndex, unsigned int vertexIndex )
143 {
144   return mVertices[vertexIndex].textureCoords.Get(bufferIndex);
145 }
146
147 void AnimatableMesh::ResetDefaultProperties( BufferIndex updateBufferIndex )
148 {
149   for( std::size_t i=0; i<mNumVertices; i++)
150   {
151     mVertices[i].position.ResetToBaseValue( updateBufferIndex );
152     mVertices[i].color.ResetToBaseValue( updateBufferIndex );
153     mVertices[i].textureCoords.ResetToBaseValue( updateBufferIndex );
154   }
155 }
156
157 } // SceneGraph
158
159 } // Internal
160
161 } // Dali