2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/update/modeling/scene-graph-animatable-mesh.h>
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>
35 AnimatableMesh::AnimatableMesh( ResourceManager& resourceManager,
37 const MeshData::VertexContainer& vertices )
38 : mResourceManager(resourceManager),
41 mNumVertices = vertices.size();
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) );
47 for(unsigned int i=0; i<mNumVertices; i++)
49 // Use placement new to instantiate the vertex properties
50 new ( &mVertices[i] ) VertexProperties( vertices[i] );
54 AnimatableMesh::~AnimatableMesh()
56 for(unsigned int i=0; i<mNumVertices; i++)
58 // Used placement new to instantiate these objects, so have to directly call destructors
59 mVertices[i].~VertexProperties();
61 operator delete [] (mVertices);
64 void AnimatableMesh::UpdateMesh( BufferIndex updateBufferIndex )
66 // Copy properties to associated scenegraph mesh
67 SceneGraph::Mesh* mesh(mResourceManager.GetMesh(mMeshId));
68 DALI_ASSERT_DEBUG( mesh );
70 // TODO: Should be double buffered - pass in buffer index
71 MeshData& meshData = mesh->GetMeshData(Mesh::UPDATE_THREAD);
73 bool meshUpdated( false );
74 MeshData::VertexContainer& oldVertices = meshData.GetVertices();
75 DALI_ASSERT_DEBUG( oldVertices.size() == mNumVertices );
76 DALI_ASSERT_DEBUG( oldVertices.size() > 0 );
78 MeshData::Vertex* vertices = &oldVertices.at(0);
79 for(std::size_t i=0; i<mNumVertices; i++)
81 if ( ! mVertices[i].position.IsClean() )
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;
91 if ( ! mVertices[i].color.IsClean() )
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;
101 if ( ! mVertices[i].textureCoords.IsClean() )
103 const Vector2& texcoords = GetTextureCoords(updateBufferIndex, i);
104 vertices[i].u = texcoords.x;
105 vertices[i].v = texcoords.y;
113 mesh->MeshDataUpdated(updateBufferIndex, Mesh::UPDATE_THREAD, NULL);
117 void AnimatableMesh::BakePosition( BufferIndex updateBufferIndex, unsigned int vertex, const Vector3& position )
119 mVertices[vertex].position.Bake( updateBufferIndex, position );
122 void AnimatableMesh::BakeColor( BufferIndex updateBufferIndex, unsigned int vertex, const Vector4& color )
124 mVertices[vertex].color.Bake( updateBufferIndex, color );
127 void AnimatableMesh::BakeTextureCoords( BufferIndex updateBufferIndex, unsigned int vertex, const Vector2& coords )
129 mVertices[vertex].textureCoords.Bake( updateBufferIndex, coords );
132 const Vector3& AnimatableMesh::GetPosition( BufferIndex bufferIndex, unsigned int vertexIndex )
134 return mVertices[vertexIndex].position.Get(bufferIndex);
137 const Vector4& AnimatableMesh::GetColor( BufferIndex bufferIndex, unsigned int vertexIndex )
139 return mVertices[vertexIndex].color.Get(bufferIndex);
142 const Vector2& AnimatableMesh::GetTextureCoords( BufferIndex bufferIndex, unsigned int vertexIndex )
144 return mVertices[vertexIndex].textureCoords.Get(bufferIndex);
147 void AnimatableMesh::ResetDefaultProperties( BufferIndex updateBufferIndex )
149 for( std::size_t i=0; i<mNumVertices; i++)
151 mVertices[i].position.ResetToBaseValue( updateBufferIndex );
152 mVertices[i].color.ResetToBaseValue( updateBufferIndex );
153 mVertices[i].textureCoords.ResetToBaseValue( updateBufferIndex );