2 * Copyright (c) 2015 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.
17 #include "scene-graph-geometry.h"
27 : mIndexBuffer( NULL ),
31 mGeometryType(Dali::Geometry::TRIANGLES),
32 mRequiresDepthTest(false)
35 // Observe our own PropertyOwner's uniform map
36 AddUniformMapObserver( *this );
41 // @todo Inform renderers of deletion of buffers?
43 // could remove self from own uniform map observer, but it's about to be destroyed.
46 void Geometry::AddVertexBuffer( PropertyBuffer* vertexBuffer )
48 mVertexBuffers.PushBack( vertexBuffer );
49 CalculateExtents( vertexBuffer );
50 vertexBuffer->AddUniformMapObserver(*this);
51 mConnectionObservers.ConnectionsChanged(*this);
54 void Geometry::RemoveVertexBuffer( PropertyBuffer* vertexBuffer )
56 DALI_ASSERT_DEBUG( NULL != vertexBuffer );
58 // Find the object and destroy it
59 Vector<PropertyBuffer*>::Iterator match = std::find(
60 mVertexBuffers.Begin(),
64 DALI_ASSERT_DEBUG( mVertexBuffers.End() != match );
65 if( mVertexBuffers.End() != match )
67 vertexBuffer->RemoveUniformMapObserver(*this);
68 mVertexBuffers.Erase( match );
69 mConnectionObservers.ConnectionsChanged(*this);
73 void Geometry::SetIndexBuffer( PropertyBuffer* indexBuffer )
75 if( mIndexBuffer != indexBuffer )
77 mIndexBuffer = indexBuffer;
78 indexBuffer->AddUniformMapObserver(*this);
79 mConnectionObservers.ConnectionsChanged(*this);
83 void Geometry::ClearIndexBuffer()
85 // @todo Actually delete, or put on Discard Queue and tell Renderer in render thread?
88 mIndexBuffer->RemoveUniformMapObserver(*this);
91 mConnectionObservers.ConnectionsChanged(*this);
94 void Geometry::SetGeometryType( BufferIndex bufferIndex, Geometry::GeometryType geometryType )
96 mGeometryType.Set( bufferIndex, geometryType);
99 Vector<PropertyBuffer*>& Geometry::GetVertexBuffers()
101 return mVertexBuffers;
104 PropertyBuffer* Geometry::GetIndexBuffer()
109 Geometry::GeometryType Geometry::GetGeometryType( BufferIndex bufferIndex) const
111 int geometryType = mGeometryType[ bufferIndex ];
112 return static_cast< GeometryDataProvider::GeometryType > ( geometryType );
115 bool Geometry::GetRequiresDepthTesting( BufferIndex bufferIndex ) const
117 return mRequiresDepthTest.GetBoolean( bufferIndex );
120 void Geometry::ResetDefaultProperties( BufferIndex updateBufferIndex )
122 // Reset the animated properties
123 mCenter.ResetToBaseValue( updateBufferIndex );
124 mHalfExtents.ResetToBaseValue( updateBufferIndex );
125 mRadius.ResetToBaseValue( updateBufferIndex );
127 // Age the double buffered properties
128 mGeometryType.CopyPrevious(updateBufferIndex);
129 mRequiresDepthTest.CopyPrevious(updateBufferIndex);
132 void Geometry::CalculateExtents( PropertyBuffer* vertexBuffer )
134 // TODO calculate extents for all vertex buffers attached to geometry
135 unsigned int elementIndex = 0;
136 unsigned int elementCount = vertexBuffer->GetElementCount( 0 );
137 unsigned int elementCount1 = vertexBuffer->GetElementCount( 1 );
139 // Select the double buffered element list that is the largest...
140 if ( elementCount < elementCount1 )
142 elementCount = elementCount1;
146 unsigned int attributeCount = vertexBuffer->GetAttributeCount( elementIndex );
147 unsigned int elementSize = vertexBuffer->GetElementSize( elementIndex );
149 std::string posName( "aPos" );
157 // Find the position attribute index
158 for ( unsigned int i = 0; i < attributeCount; ++i )
160 found = vertexBuffer->GetAttributeName( 0, i ).find( posName );
161 if ( found != std::string::npos )
163 unsigned int offset = vertexBuffer->GetAttributeOffset( elementIndex, i );
164 const PropertyBufferDataProvider::BufferType& data = vertexBuffer->GetData( elementIndex );
166 // Check attribute type to determine correct position type
167 Property::Type positionType = vertexBuffer->GetAttributeType( elementIndex, i );
170 switch ( positionType )
172 case Property::VECTOR2:
174 for ( unsigned int j = 0; j < elementCount; ++j )
176 const Vector2* position = reinterpret_cast< const Vector2* >( &data[ offset ] );
177 offset += elementSize;
179 if ( position->x < left )
183 if ( position->x > right )
187 if ( position->y < top )
191 if ( position->y > bottom )
193 bottom = position->y;
197 halfExtents = Vector3( ( right - left ) * 0.5f, ( bottom - top ) * 0.5f, 0.0f );
198 center = Vector3( halfExtents.x + left , halfExtents.y + top, 0.0f );
201 case Property::VECTOR3:
205 for ( unsigned int j = 0; j < elementCount; ++j )
207 const Vector3* position = reinterpret_cast< const Vector3* >( &data[ offset ] );
208 offset += elementSize;
210 if ( position->x < left )
214 if ( position->x > right )
218 if ( position->y < top )
222 if ( position->y > bottom )
224 bottom = position->y;
226 if ( position->z > far )
230 if ( position->z < near )
235 halfExtents = Vector3( ( right - left ) * 0.5f, ( bottom - top ) * 0.5f, ( far - near ) * 0.5f );
236 center = Vector3( halfExtents.x + left , halfExtents.y + top, halfExtents.z + near );
244 mCenter.Bake( 0, center );
245 mCenter.Bake( 1, center );
246 mHalfExtents.Bake( 0, halfExtents );
247 mHalfExtents.Bake( 1, halfExtents );
249 float radius = halfExtents.x;
250 if ( radius < halfExtents.y )
252 radius = halfExtents.y;
254 mRadius.SetInitial( radius );
259 void Geometry::ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex )
263 void Geometry::DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex )
267 void Geometry::AddConnectionObserver( ConnectionChangePropagator::Observer& observer )
269 mConnectionObservers.Add(observer);
272 void Geometry::RemoveConnectionObserver( ConnectionChangePropagator::Observer& observer )
274 mConnectionObservers.Remove(observer);
277 void Geometry::UniformMappingsChanged( const UniformMap& mappings )
279 // Our uniform map, or that of one of the watched children has changed.
280 // Inform connected observers.
281 mConnectionObservers.ConnectedUniformMapChanged();
284 } // namespace SceneGraph
285 } // namespace Internal