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