[dali_1.1.45] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / model3d-view / obj-loader.h
1 #ifndef DALI_TOOLKIT_INTERNAL_OBJ_LOADER_H
2 #define DALI_TOOLKIT_INTERNAL_OBJ_LOADER_H
3
4 /*
5  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/rendering/renderer.h>
23 #include <limits>
24
25 namespace Dali
26 {
27
28 namespace Toolkit
29 {
30
31 class ObjLoader;
32
33 namespace Internal
34 {
35 class ObjLoader
36 {
37 public:
38
39   struct TriIndex
40   {
41     int pointIndex[3];
42     int normalIndex[3];
43     int textureIndex[3];
44   };
45
46   struct Vertex
47   {
48     Vertex()
49     {}
50
51     Vertex( const Vector3& position, const Vector3& normal, const Vector2& textureCoord )
52     : position( position ), normal( normal )
53     {}
54
55     Vector3 position;
56     Vector3 normal;
57   };
58
59   struct VertexExt
60   {
61     VertexExt()
62     {}
63
64     VertexExt( const Vector3& tangent, const Vector3& binormal )
65     : tangent( tangent), bitangent( binormal )
66     {}
67
68     Vector3 tangent;
69     Vector3 bitangent;
70   };
71
72   struct BoundingVolume
73   {
74     void Init()
75     {
76       pointMin = Vector3( std::numeric_limits<float>::max(), std::numeric_limits<float>::max(), std::numeric_limits<float>::max() );
77       pointMax = Vector3( std::numeric_limits<float>::min(), std::numeric_limits<float>::min(), std::numeric_limits<float>::min() );
78     }
79
80     void ConsiderNewPointInVolume( const Vector3& position )
81     {
82       pointMin.x = std::min( position.x, pointMin.x );
83       pointMin.y = std::min( position.y, pointMin.y );
84       pointMin.z = std::min( position.z, pointMin.z );
85
86       pointMax.x = std::max( position.x, pointMax.x );
87       pointMax.y = std::max( position.y, pointMax.y );
88       pointMax.z = std::max( position.z, pointMax.z );
89     }
90
91     Vector3 pointMin;
92     Vector3 pointMax;
93   };
94
95   //Defines bit masks to declare which properties are needed by anyone requesting a geometry.
96   enum ObjectProperties
97   {
98     TEXTURE_COORDINATES = 1 << 0,
99     TANGENTS = 1 << 1,
100     BINORMALS = 1 << 2
101   };
102
103   ObjLoader();
104   virtual ~ObjLoader();
105
106   bool      IsSceneLoaded();
107   bool      IsMaterialLoaded();
108
109   bool      LoadObject( char* objBuffer, std::streampos fileSize );
110
111   void      LoadMaterial( char* objBuffer, std::streampos fileSize, std::string& diffuseTextureUrl,
112                           std::string& normalTextureUrl, std::string& glossTextureUrl );
113
114   Geometry  CreateGeometry( int objectProperties, bool useSoftNormals );
115
116   Vector3   GetCenter();
117   Vector3   GetSize();
118
119   void      ClearArrays();
120
121   bool      IsTexturePresent();
122   bool      IsDiffuseMapPresent();
123   bool      IsNormalMapPresent();
124   bool      IsSpecularMapPresent();
125
126 private:
127
128   BoundingVolume mSceneAABB;
129
130   bool mSceneLoaded;
131   bool mMaterialLoaded;
132   bool mHasTexturePoints;
133
134   //Material file properties.
135   bool mHasDiffuseMap;
136   bool mHasNormalMap;
137   bool mHasSpecularMap;
138
139   Dali::Vector<Vector3>  mPoints;
140   Dali::Vector<Vector2>  mTextures;
141   Dali::Vector<Vector2>  mTextures2;
142   Dali::Vector<Vector3>  mNormals;
143   Dali::Vector<Vector3>  mTangents;
144   Dali::Vector<Vector3>  mBiTangents;
145   Dali::Vector<TriIndex> mTriangles;
146
147   /**
148    * @brief Calculates normals for each point on a per-face basis.
149    *
150    * There are multiple normals per point, each corresponding to the normal of a face connecting to the point.
151    *
152    * @param[in] vertices The vertices of the object.
153    * @param[in, out] triangles The triangles that form the faces. The normals of each triangle will be updated.
154    * @param[in, out] normals The normals to be calculated.
155    */
156   void CalculateHardFaceNormals( const Dali::Vector<Vector3>& vertices,
157                                  Dali::Vector<TriIndex>& triangles,
158                                  Dali::Vector<Vector3>& normals );
159
160   /**
161    * @brief Calculates smoothed normals for each point.
162    *
163    * There is one normal per point, an average of the connecting faces.
164    *
165    * @param[in] vertices The vertices of the object.
166    * @param[in, out] triangles The triangles that form the faces. The normals of each triangle will be updated.
167    * @param[in, out] normals The normals to be calculated.
168    */
169   void CalculateSoftFaceNormals( const Dali::Vector<Vector3>& vertices,
170                                  Dali::Vector<TriIndex>& triangles,
171                                  Dali::Vector<Vector3>& normals );
172
173   /**
174    * @brief Calculates tangents and bitangents for each point of the object.
175    *
176    * These are calculated using the object's points, texture coordinates and normals, so these must be initialised first.
177    */
178   void CalculateTangentFrame();
179
180   void CenterAndScale( bool center, Dali::Vector<Vector3>& points );
181
182   /**
183    * @brief Using the data loaded from the file, create arrays of data to be used in creating the geometry.
184    *
185    * @param[in] vertices The vertices of the object.
186    * @param[in] textures The texture coordinates of the object.
187    * @param[in] verticesExt Extension to vertices, storing tangents and bitangents.
188    * @param[in] indices Indices of corresponding values to match triangles to their respective data.
189    * @param[in] useSoftNormals Indicates whether we should average the normals at each point to smooth the surface or not.
190    */
191   void CreateGeometryArray( Dali::Vector<Vertex> & vertices,
192                             Dali::Vector<Vector2> & textures,
193                             Dali::Vector<VertexExt> & verticesExt,
194                             Dali::Vector<unsigned short> & indices,
195                             bool useSoftNormals );
196
197 };
198
199
200
201 } // namespace Internal
202
203 } // namespace Toolkit
204
205 } // namespace Dali
206
207
208
209
210 #endif // DALI_TOOLKIT_INTERNAL_OBJ_LOADER_H