0fda8c4a5c3c12391eca7d63586d1f863d06a7d7
[platform/core/uifw/dali-core.git] / dali / public-api / geometry / mesh.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 // CLASS HEADER
19 #include <dali/public-api/geometry/mesh.h>
20
21 // INTERNAL INCLUDES
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>
26
27 namespace Dali
28 {
29
30 namespace
31 {
32
33 } // namespace
34
35 Mesh::Mesh()
36 {
37 }
38
39 Mesh::~Mesh()
40 {
41 }
42
43 Mesh Mesh::New( const MeshData& meshData )
44 {
45   // Convert material handle to intrusive pointer
46   Dali::Material material( meshData.GetMaterial() );
47   Internal::MaterialIPtr materialPtr( &GetImplementation( material ) );
48
49   Internal::MeshIPtr meshPtr = Internal::Mesh::New( meshData, true /* discardable */, false /* scaling not required */ );
50   return Mesh( meshPtr.Get() );
51 }
52
53 Mesh Mesh::NewPlane(const float width, const float height, const int xSteps, const int ySteps, const Rect<float>& textureCoordinates)
54 {
55   DALI_ASSERT_DEBUG( xSteps > 1 && ySteps > 1 );
56
57   const int vertexCount = xSteps * ySteps;
58
59   // vertices
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);
67   int vertexIndex = 0;
68   for( int y = 0; y < ySteps; ++y )
69   {
70     for( int x = 0; x < xSteps; ++x )
71     {
72       MeshData::Vertex& vertex = vertices[vertexIndex];
73       vertex.x = xOffset + (xSpacing * x);
74       vertex.y = yOffset + (ySpacing * y);
75       vertex.z = 0.0f;
76
77       vertex.nX = 0.0f;
78       vertex.nY = 0.0f;
79       vertex.nZ = 1.0f;
80
81       vertex.u = textureCoordinates.x + (xSpacingUV * x);
82       vertex.v = textureCoordinates.y + (ySpacingUV * y);
83       ++vertexIndex;
84     }
85   }
86
87   // faces
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;
94
95   for( int y = 0; y < ySteps - 1; ++y )
96   {
97     for( int x = 0; x < xSteps - 1; ++x )
98     {
99       index0 = (y * xSteps) + x;
100       index1 = ((y + 1) * xSteps) + x;
101       index2 = ((y + 1) * xSteps) + (x + 1);
102       *pIndex++ = index0;
103       *pIndex++ = index1;
104       *pIndex++ = index2;
105
106       index0 = ((y + 1) * xSteps) + (x + 1);
107       index1 = (y * xSteps) + (x + 1);
108       index2 = (y * xSteps) + x;
109       *pIndex++ = index0;
110       *pIndex++ = index1;
111       *pIndex++ = index2;
112     }
113   }
114
115   BoneContainer bones;
116
117   MeshData meshData;
118   meshData.SetHasNormals(true);
119   meshData.SetHasTextureCoords(true);
120   meshData.SetData( vertices, faces, bones, Material::New("PlaneMat"));
121
122   Vector4 vMin;
123   Vector4 vMax;
124   meshData.AddToBoundingVolume(vMin, vMax, Matrix::IDENTITY);
125
126   meshData.SetBoundingBoxMin(vMin);
127   meshData.SetBoundingBoxMax(vMax);
128
129   return New(meshData);
130 }
131
132 Mesh Mesh::DownCast( BaseHandle handle )
133 {
134   return Mesh( dynamic_cast<Dali::Internal::Mesh*>(handle.GetObjectPtr()) );
135 }
136
137 void Mesh::UpdateMeshData( const MeshData& meshData )
138 {
139   GetImplementation(*this).UpdateMeshData( meshData );
140 }
141
142 Mesh::Mesh(Internal::Mesh* internal)
143 : BaseHandle(internal)
144 {
145 }
146
147 } // namespace Dali