2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
18 #include <dali/public-api/geometry/mesh.h>
21 #include <dali/internal/event/modeling/mesh-impl.h>
22 #include <dali/internal/event/modeling/material-impl.h>
23 #include <dali/public-api/common/dali-common.h>
24 #include <dali/public-api/math/vector3.h>
42 Mesh Mesh::New( const MeshData& meshData )
44 // Convert material handle to intrusive pointer
45 Dali::Material material( meshData.GetMaterial() );
46 Internal::MaterialIPtr materialPtr( &GetImplementation( material ) );
48 Internal::MeshIPtr meshPtr = Internal::Mesh::New( meshData, true /* discardable */, false /* scaling not required */ );
49 return Mesh( meshPtr.Get() );
52 Mesh Mesh::NewPlane(const float width, const float height, const int xSteps, const int ySteps, const Rect<float>& textureCoordinates)
54 DALI_ASSERT_DEBUG( xSteps > 1 && ySteps > 1 );
56 const int vertexCount = xSteps * ySteps;
59 MeshData::VertexContainer vertices(vertexCount);
60 const float xSpacing = width / ( xSteps - 1 );
61 const float ySpacing = height / (ySteps - 1 );
62 const float xOffset = -xSpacing * (0.5f * xSteps) + (0.5f * xSpacing); // origin at (width / 2, height / 2)
63 const float yOffset = -ySpacing * (0.5f * ySteps) + (0.5f * ySpacing);
64 const float xSpacingUV = textureCoordinates.width / (xSteps - 1);
65 const float ySpacingUV = textureCoordinates.height / (ySteps - 1);
67 for( int y = 0; y < ySteps; ++y )
69 for( int x = 0; x < xSteps; ++x )
71 MeshData::Vertex& vertex = vertices[vertexIndex];
72 vertex.x = xOffset + (xSpacing * x);
73 vertex.y = yOffset + (ySpacing * y);
80 vertex.u = textureCoordinates.x + (xSpacingUV * x);
81 vertex.v = textureCoordinates.y + (ySpacingUV * y);
87 const int faceCount = 2 * ((ySteps - 1) * (xSteps - 1));
88 MeshData::FaceIndices faces( faceCount * 3 );
89 unsigned short* pIndex = &(faces)[0];
90 unsigned short index0 = 0;
91 unsigned short index1 = 0;
92 unsigned short index2 = 0;
94 for( int y = 0; y < ySteps - 1; ++y )
96 for( int x = 0; x < xSteps - 1; ++x )
98 index0 = (y * xSteps) + x;
99 index1 = ((y + 1) * xSteps) + x;
100 index2 = ((y + 1) * xSteps) + (x + 1);
105 index0 = ((y + 1) * xSteps) + (x + 1);
106 index1 = (y * xSteps) + (x + 1);
107 index2 = (y * xSteps) + x;
117 meshData.SetHasNormals(true);
118 meshData.SetHasTextureCoords(true);
119 meshData.SetData( vertices, faces, bones, Material::New("PlaneMat"));
123 meshData.AddToBoundingVolume(vMin, vMax, Matrix::IDENTITY);
125 meshData.SetBoundingBoxMin(vMin);
126 meshData.SetBoundingBoxMax(vMax);
128 return New(meshData);
131 Mesh Mesh::DownCast( BaseHandle handle )
133 return Mesh( dynamic_cast<Dali::Internal::Mesh*>(handle.GetObjectPtr()) );
136 void Mesh::UpdateMeshData( const MeshData& meshData )
138 GetImplementation(*this).UpdateMeshData( meshData );
141 Mesh::Mesh(Internal::Mesh* internal)
142 : BaseHandle(internal)