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.
18 #include "mesh-factory.h"
20 #include <dali/public-api/geometry/mesh-data.h>
21 #include <dali/public-api/modeling/material.h>
28 Dali::MeshData NewPlane(const float width, const float height, const int xSteps, const int ySteps, const Rect<float>& textureCoordinates)
30 DALI_ASSERT_DEBUG( xSteps > 1 && ySteps > 1 );
32 const int vertexCount = xSteps * ySteps;
35 MeshData::VertexContainer vertices(vertexCount);
36 const float xSpacing = width / ( xSteps - 1 );
37 const float ySpacing = height / (ySteps - 1 );
38 const float xOffset = -xSpacing * (0.5f * xSteps) + (0.5f * xSpacing); // origin at (width / 2, height / 2)
39 const float yOffset = -ySpacing * (0.5f * ySteps) + (0.5f * ySpacing);
40 const float xSpacingUV = textureCoordinates.width / (xSteps - 1);
41 const float ySpacingUV = textureCoordinates.height / (ySteps - 1);
43 for( int y = 0; y < ySteps; ++y )
45 for( int x = 0; x < xSteps; ++x )
47 MeshData::Vertex& vertex = vertices[vertexIndex];
48 vertex.x = xOffset + (xSpacing * x);
49 vertex.y = yOffset + (ySpacing * y);
56 vertex.u = textureCoordinates.x + (xSpacingUV * x);
57 vertex.v = textureCoordinates.y + (ySpacingUV * y);
63 const int faceCount = 2 * ((ySteps - 1) * (xSteps - 1));
64 MeshData::FaceIndices faces( faceCount * 3 );
65 unsigned short* pIndex = &(faces)[0];
66 unsigned short index0 = 0;
67 unsigned short index1 = 0;
68 unsigned short index2 = 0;
70 for( int y = 0; y < ySteps - 1; ++y )
72 for( int x = 0; x < xSteps - 1; ++x )
74 index0 = (y * xSteps) + x;
75 index1 = ((y + 1) * xSteps) + x;
76 index2 = ((y + 1) * xSteps) + (x + 1);
81 index0 = ((y + 1) * xSteps) + (x + 1);
82 index1 = (y * xSteps) + (x + 1);
83 index2 = (y * xSteps) + x;
92 Dali::MeshData meshData;
94 meshData.SetHasNormals(true);
95 meshData.SetHasTextureCoords(true);
96 meshData.SetData( vertices, faces, bones, Dali::Material::New("PlaneMat"));
100 meshData.AddToBoundingVolume(vMin, vMax, Matrix::IDENTITY);
102 meshData.SetBoundingBoxMin(vMin);
103 meshData.SetBoundingBoxMax(vMax);
108 Dali::MeshData NewPath( Dali::Path path, const unsigned int resolution )
111 meshData.SetHasNormals(false);
112 meshData.SetHasTextureCoords(false);
114 if( resolution != 0 )
116 size_t vertexCount(resolution+1);
117 MeshData::VertexContainer vertex(vertexCount);
119 float sampleDelta = 1.0f/(float)resolution;
121 for( size_t i(0); i!=vertexCount; ++i )
123 //Sample path to get the vertex position
124 Vector3* vertexPosition = reinterpret_cast<Vector3*>(&vertex[i].x);
125 path.Sample( i*sampleDelta, *vertexPosition, tangent );
128 //Generate indices. Each vertex is connected to the next
129 size_t indexCount(resolution*2);
130 MeshData::FaceIndices index(indexCount);
131 unsigned int nIndex = 0;
132 for( size_t i(0); i!=indexCount; i+=2 )
135 index[i+1] = ++nIndex;
138 meshData.SetLineData(vertex, index, Dali::Material::New("PathMat"));