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/public-api/geometry/mesh.h>
22 #include <dali/internal/event/modeling/mesh-impl.h>
23 #include <dali/internal/event/modeling/material-impl.h>
24 #include <dali/public-api/common/dali-common.h>
25 #include <dali/public-api/math/vector3.h>
43 Mesh Mesh::New( const MeshData& meshData )
45 // Convert material handle to intrusive pointer
46 Dali::Material material( meshData.GetMaterial() );
47 Internal::MaterialIPtr materialPtr( &GetImplementation( material ) );
49 Internal::MeshIPtr meshPtr = Internal::Mesh::New( meshData, true /* discardable */, false /* scaling not required */ );
50 return Mesh( meshPtr.Get() );
53 Mesh Mesh::NewPlane(const float width, const float height, const int xSteps, const int ySteps, const Rect<float>& textureCoordinates)
55 DALI_ASSERT_DEBUG( xSteps > 1 && ySteps > 1 );
57 const int vertexCount = xSteps * ySteps;
60 MeshData::VertexContainer vertices(vertexCount);
61 const float xSpacing = width / ( xSteps - 1 );
62 const float ySpacing = height / (ySteps - 1 );
63 const float xOffset = -xSpacing * (0.5f * xSteps) + (0.5f * xSpacing); // origin at (width / 2, height / 2)
64 const float yOffset = -ySpacing * (0.5f * ySteps) + (0.5f * ySpacing);
65 const float xSpacingUV = textureCoordinates.width / (xSteps - 1);
66 const float ySpacingUV = textureCoordinates.height / (ySteps - 1);
68 for( int y = 0; y < ySteps; ++y )
70 for( int x = 0; x < xSteps; ++x )
72 MeshData::Vertex& vertex = vertices[vertexIndex];
73 vertex.x = xOffset + (xSpacing * x);
74 vertex.y = yOffset + (ySpacing * y);
81 vertex.u = textureCoordinates.x + (xSpacingUV * x);
82 vertex.v = textureCoordinates.y + (ySpacingUV * y);
88 const int faceCount = 2 * ((ySteps - 1) * (xSteps - 1));
89 MeshData::FaceIndices faces( faceCount * 3 );
90 unsigned short* pIndex = &(faces)[0];
91 unsigned short index0 = 0;
92 unsigned short index1 = 0;
93 unsigned short index2 = 0;
95 for( int y = 0; y < ySteps - 1; ++y )
97 for( int x = 0; x < xSteps - 1; ++x )
99 index0 = (y * xSteps) + x;
100 index1 = ((y + 1) * xSteps) + x;
101 index2 = ((y + 1) * xSteps) + (x + 1);
106 index0 = ((y + 1) * xSteps) + (x + 1);
107 index1 = (y * xSteps) + (x + 1);
108 index2 = (y * xSteps) + x;
118 meshData.SetHasNormals(true);
119 meshData.SetHasTextureCoords(true);
120 meshData.SetData( vertices, faces, bones, Material::New("PlaneMat"));
124 meshData.AddToBoundingVolume(vMin, vMax, Matrix::IDENTITY);
126 meshData.SetBoundingBoxMin(vMin);
127 meshData.SetBoundingBoxMax(vMax);
129 return New(meshData);
132 Mesh Mesh::DownCast( BaseHandle handle )
134 return Mesh( dynamic_cast<Dali::Internal::Mesh*>(handle.GetObjectPtr()) );
137 void Mesh::UpdateMeshData( const MeshData& meshData )
139 GetImplementation(*this).UpdateMeshData( meshData );
142 Mesh::Mesh(Internal::Mesh* internal)
143 : BaseHandle(internal)