Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / rendering-basic-pbr / obj-loader.h
1 #ifndef DALI_DEMO_PBR_OBJ_LOADER_H
2 #define DALI_DEMO_PBR_OBJ_LOADER_H
3
4 /*
5  * Copyright (c) 2020 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/geometry.h>
23 #include <limits>
24
25 using namespace Dali;
26
27 namespace PbrDemo
28 {
29 class ObjLoader
30 {
31 public:
32   struct TriIndex
33   {
34     int pointIndex[3];
35     int normalIndex[3];
36     int textureIndex[3];
37   };
38
39   struct BoundingVolume
40   {
41     void Init()
42     {
43       pointMin = Vector3(std::numeric_limits<float>::max(), std::numeric_limits<float>::max(), std::numeric_limits<float>::max());
44       pointMax = Vector3(std::numeric_limits<float>::min(), std::numeric_limits<float>::min(), std::numeric_limits<float>::min());
45     }
46
47     void ConsiderNewPointInVolume(const Vector3& position)
48     {
49       pointMin.x = std::min(position.x, pointMin.x);
50       pointMin.y = std::min(position.y, pointMin.y);
51       pointMin.z = std::min(position.z, pointMin.z);
52
53       pointMax.x = std::max(position.x, pointMax.x);
54       pointMax.y = std::max(position.y, pointMax.y);
55       pointMax.z = std::max(position.z, pointMax.z);
56     }
57
58     Vector3 pointMin;
59     Vector3 pointMax;
60   };
61
62   //Defines bit masks to declare which properties are needed by anyone requesting a geometry.
63   enum ObjectProperties
64   {
65     TEXTURE_COORDINATES = 1 << 0,
66     TANGENTS            = 1 << 1,
67     BINORMALS           = 1 << 2
68   };
69
70   ObjLoader();
71   virtual ~ObjLoader();
72
73   bool IsSceneLoaded();
74   bool IsMaterialLoaded();
75
76   bool LoadObject(char* objBuffer, std::streampos fileSize);
77
78   void LoadMaterial(char* objBuffer, std::streampos fileSize, std::string& diffuseTextureUrl, std::string& normalTextureUrl, std::string& glossTextureUrl);
79
80   Geometry CreateGeometry(int objectProperties, bool useSoftNormals);
81
82   Vector3 GetCenter();
83   Vector3 GetSize();
84
85   void ClearArrays();
86
87   bool IsTexturePresent();
88   bool IsDiffuseMapPresent();
89   bool IsNormalMapPresent();
90   bool IsSpecularMapPresent();
91
92 private:
93   Dali::Vector<Vector3>  mPoints;
94   Dali::Vector<Vector2>  mTextureUv;
95   Dali::Vector<Vector2>  mTextureUv2;
96   Dali::Vector<Vector3>  mNormals;
97   Dali::Vector<Vector3>  mTangents;
98   Dali::Vector<Vector3>  mBiTangents;
99   Dali::Vector<TriIndex> mTriangles;
100
101   BoundingVolume mSceneAABB;
102
103   bool mSceneLoaded;
104   bool mMaterialLoaded;
105   bool mHasTextureUv;
106
107   //Material file properties.
108   bool mHasDiffuseMap;
109   bool mHasNormalMap;
110   bool mHasSpecularMap;
111
112   /**
113    * @brief Calculates normals for each point on a per-face basis.
114    *
115    * There are multiple normals per point, each corresponding to the normal of a face connecting to the point.
116    *
117    * @param[in] vertices The vertices of the object.
118    * @param[in, out] triangles The triangles that form the faces. The normals of each triangle will be updated.
119    * @param[in, out] normals The normals to be calculated.
120    */
121   void CalculateHardFaceNormals(const Dali::Vector<Vector3>& vertices,
122                                 Dali::Vector<TriIndex>&      triangles,
123                                 Dali::Vector<Vector3>&       normals);
124
125   /**
126    * @brief Calculates smoothed normals for each point.
127    *
128    * There is one normal per point, an average of the connecting faces.
129    *
130    * @param[in] vertices The vertices of the object.
131    * @param[in, out] triangles The triangles that form the faces. The normals of each triangle will be updated.
132    * @param[in, out] normals The normals to be calculated.
133    */
134   void CalculateSoftFaceNormals(const Dali::Vector<Vector3>& vertices,
135                                 Dali::Vector<TriIndex>&      triangles,
136                                 Dali::Vector<Vector3>&       normals);
137
138   /**
139    * @brief Calculates tangents and bitangents for each point of the object.
140    *
141    * These are calculated using the object's points, texture coordinates and normals, so these must be initialised first.
142    */
143   void CalculateTangentFrame();
144
145   void CenterAndScale(bool center, Dali::Vector<Vector3>& points);
146
147   /**
148    * @brief Using the data loaded from the file, create arrays of data to be used in creating the geometry.
149    *
150    * @param[out] positions The positions of the vertices of the object.
151    * @param[out] normals The normals of the vertices of the object.
152    * @param[out] tangents The tangents of the vertices of the object.
153    * @param[out] textures The texture coordinates of the vertices of the object.
154    * @param[out] indices Indices of corresponding values to match triangles to their respective data.
155    * @param[in] useSoftNormals Indicates whether we should average the normals at each point to smooth the surface or not.
156    */
157   void CreateGeometryArray(Dali::Vector<Vector3>&        positions,
158                            Dali::Vector<Vector3>&        normals,
159                            Dali::Vector<Vector3>&        tangents,
160                            Dali::Vector<Vector2>&        textures,
161                            Dali::Vector<unsigned short>& indices,
162                            bool                          useSoftNormals);
163 };
164
165 } // namespace PbrDemo
166
167 #endif // DALI_DEMO_PBR_OBJ_LOADER_H