Property enum name changes in dali-core: Core changes
[platform/core/uifw/dali-core.git] / dali / public-api / geometry / mesh-factory.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include "mesh-factory.h"
19
20 #include <dali/public-api/geometry/mesh-data.h>
21 #include <dali/public-api/modeling/material.h>
22
23 namespace Dali
24 {
25 namespace MeshFactory
26 {
27
28 Dali::MeshData NewPlane(const float width, const float height, const int xSteps, const int ySteps, const Rect<float>& textureCoordinates)
29 {
30   DALI_ASSERT_DEBUG( xSteps > 1 && ySteps > 1 );
31
32   const int vertexCount = xSteps * ySteps;
33
34   // vertices
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);
42   int vertexIndex = 0;
43   for( int y = 0; y < ySteps; ++y )
44   {
45     for( int x = 0; x < xSteps; ++x )
46     {
47       MeshData::Vertex& vertex = vertices[vertexIndex];
48       vertex.x = xOffset + (xSpacing * x);
49       vertex.y = yOffset + (ySpacing * y);
50       vertex.z = 0.0f;
51
52       vertex.nX = 0.0f;
53       vertex.nY = 0.0f;
54       vertex.nZ = 1.0f;
55
56       vertex.u = textureCoordinates.x + (xSpacingUV * x);
57       vertex.v = textureCoordinates.y + (ySpacingUV * y);
58       ++vertexIndex;
59     }
60   }
61
62   // faces
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;
69
70   for( int y = 0; y < ySteps - 1; ++y )
71   {
72     for( int x = 0; x < xSteps - 1; ++x )
73     {
74       index0 = (y * xSteps) + x;
75       index1 = ((y + 1) * xSteps) + x;
76       index2 = ((y + 1) * xSteps) + (x + 1);
77       *pIndex++ = index0;
78       *pIndex++ = index1;
79       *pIndex++ = index2;
80
81       index0 = ((y + 1) * xSteps) + (x + 1);
82       index1 = (y * xSteps) + (x + 1);
83       index2 = (y * xSteps) + x;
84       *pIndex++ = index0;
85       *pIndex++ = index1;
86       *pIndex++ = index2;
87     }
88   }
89
90   BoneContainer bones;
91
92   Dali::MeshData meshData;
93
94   meshData.SetHasNormals(true);
95   meshData.SetHasTextureCoords(true);
96   meshData.SetData( vertices, faces, bones, Dali::Material::New("PlaneMat"));
97
98   Vector4 vMin;
99   Vector4 vMax;
100   meshData.AddToBoundingVolume(vMin, vMax, Matrix::IDENTITY);
101
102   meshData.SetBoundingBoxMin(vMin);
103   meshData.SetBoundingBoxMax(vMax);
104
105   return meshData;
106 }
107
108 Dali::MeshData NewPath( Dali::Path path, const unsigned int resolution )
109 {
110   MeshData meshData;
111   meshData.SetHasNormals(false);
112   meshData.SetHasTextureCoords(false);
113
114   if( resolution != 0 )
115   {
116     size_t vertexCount(resolution+1);
117     MeshData::VertexContainer vertex(vertexCount);
118
119     float sampleDelta = 1.0f/(float)resolution;
120     Vector3 tangent;
121     for( size_t i(0); i!=vertexCount; ++i )
122     {
123       //Sample path to get the vertex position
124       Vector3* vertexPosition = reinterpret_cast<Vector3*>(&vertex[i].x);
125       path.Sample( i*sampleDelta, *vertexPosition, tangent );
126     }
127
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 )
133     {
134       index[i] = nIndex;
135       index[i+1] = ++nIndex;
136     }
137
138     meshData.SetLineData(vertex, index, Dali::Material::New("PathMat"));
139   }
140
141   return meshData;
142 }
143
144 } // MeshFactory
145 } // Dali