License conversion from Flora to Apache 2.0
[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
69   // TODO: Should be double buffered - pass in buffer index
70   MeshData& meshData = mesh->GetMeshData(Mesh::UPDATE_THREAD);
71
72   bool meshUpdated( false );
73   MeshData::VertexContainer& oldVertices = meshData.GetVertices();
74   DALI_ASSERT_DEBUG( oldVertices.size() == mNumVertices );
75   DALI_ASSERT_DEBUG( oldVertices.size() > 0 );
76
77   MeshData::Vertex* vertices = &oldVertices.at(0);
78   for(std::size_t i=0; i<mNumVertices; i++)
79   {
80     if ( ! mVertices[i].position.IsClean() )
81     {
82       const Vector3& position = GetPosition(updateBufferIndex, i);
83       vertices[i].x = position.x;
84       vertices[i].y = position.y;
85       vertices[i].z = position.z;
86
87       meshUpdated = true;
88     }
89
90     if ( ! mVertices[i].color.IsClean() )
91     {
92       const Vector4& color = GetColor(updateBufferIndex, i);
93       vertices[i].vertexR = color.r;
94       vertices[i].vertexG = color.g;
95       vertices[i].vertexB = color.b;
96
97       meshUpdated = true;
98     }
99
100     if ( ! mVertices[i].textureCoords.IsClean() )
101     {
102       const Vector2& texcoords = GetTextureCoords(updateBufferIndex, i);
103       vertices[i].u = texcoords.x;
104       vertices[i].v = texcoords.y;
105
106       meshUpdated = true;
107     }
108   }
109
110   if ( meshUpdated )
111   {
112     mesh->MeshDataUpdated(updateBufferIndex, Mesh::UPDATE_THREAD, NULL);
113   }
114 }
115
116 void AnimatableMesh::BakePosition( BufferIndex updateBufferIndex, unsigned int vertex, const Vector3& position )
117 {
118   mVertices[vertex].position.Bake( updateBufferIndex, position );
119 }
120
121 void AnimatableMesh::BakeColor( BufferIndex updateBufferIndex, unsigned int vertex, const Vector4& color )
122 {
123   mVertices[vertex].color.Bake( updateBufferIndex, color );
124 }
125
126 void AnimatableMesh::BakeTextureCoords( BufferIndex updateBufferIndex, unsigned int vertex, const Vector2& coords )
127 {
128   mVertices[vertex].textureCoords.Bake( updateBufferIndex, coords );
129 }
130
131 const Vector3& AnimatableMesh::GetPosition( BufferIndex bufferIndex, unsigned int vertexIndex )
132 {
133   return mVertices[vertexIndex].position.Get(bufferIndex);
134 }
135
136 const Vector4& AnimatableMesh::GetColor( BufferIndex bufferIndex, unsigned int vertexIndex )
137 {
138   return mVertices[vertexIndex].color.Get(bufferIndex);
139 }
140
141 const Vector2& AnimatableMesh::GetTextureCoords( BufferIndex bufferIndex, unsigned int vertexIndex )
142 {
143   return mVertices[vertexIndex].textureCoords.Get(bufferIndex);
144 }
145
146 void AnimatableMesh::ResetDefaultProperties( BufferIndex updateBufferIndex )
147 {
148   for( std::size_t i=0; i<mNumVertices; i++)
149   {
150     mVertices[i].position.ResetToBaseValue( updateBufferIndex );
151     mVertices[i].color.ResetToBaseValue( updateBufferIndex );
152     mVertices[i].textureCoords.ResetToBaseValue( updateBufferIndex );
153   }
154 }
155
156 } // SceneGraph
157
158 } // Internal
159
160 } // Dali