New control to load OBJ files
[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) 2015 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/devel-api/rendering/renderer.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/public-api/controls/model3d-view/model3d-view.h>
26
27
28 namespace Dali
29 {
30
31 namespace Toolkit
32 {
33
34 class ObjLoader;
35
36 namespace Internal
37 {
38 class ObjLoader
39 {
40 public:
41
42   struct TriIndex
43   {
44     int pntIndex[3];
45     int nrmIndex[3];
46     int texIndex[3];
47   };
48
49   struct Vertex
50   {
51     Vertex()
52     {}
53
54     Vertex( const Vector3& position, const Vector3& normal, const Vector2& textureCoord )
55     : position( position ), normal( normal )
56     {}
57
58     Vector3 position;
59     Vector3 normal;
60   };
61
62   struct VertexExt
63   {
64     VertexExt()
65     {}
66
67     VertexExt( const Vector3& tangent, const Vector3& binormal )
68     : tangent( tangent), bitangent (binormal)
69     {}
70
71     Vector3 tangent;
72     Vector3 bitangent;
73   };
74
75   struct BoundingVolume
76   {
77     void Init()
78     {
79       pointMin = Vector3(999999.9,999999.9,999999.9);
80       pointMax = Vector3(-999999.9,-999999.9,-999999.9);
81     }
82
83     void ConsiderNewPointInVolume(const Vector3& position)
84     {
85       pointMin.x = std::min(position.x, pointMin.x);
86       pointMin.y = std::min(position.y, pointMin.y);
87       pointMin.z = std::min(position.z, pointMin.z);
88
89       pointMax.x = std::max(position.x, pointMax.x);
90       pointMax.y = std::max(position.y, pointMax.y);
91       pointMax.z = std::max(position.z, pointMax.z);
92     }
93
94     Vector3 pointMin;
95     Vector3 pointMax;
96   };
97
98   ObjLoader();
99   virtual ~ObjLoader();
100
101   bool      IsSceneLoaded();
102   bool      IsMaterialLoaded();
103
104   bool      Load(char* objBuffer, std::streampos fileSize, std::string& materialFile);
105
106   void      LoadMaterial(char* objBuffer, std::streampos fileSize, std::string& texture0Url, std::string& texture1Url, std::string& texture2Url);
107
108   Geometry  CreateGeometry(Toolkit::Model3dView::IlluminationType illuminationType);
109
110   Vector3   GetCenter();
111   Vector3   GetSize();
112
113   void      ClearArrays();
114
115 private:
116
117   BoundingVolume mSceneAABB;
118
119   bool mSceneLoaded;
120   bool mMaterialLoaded;
121
122   Dali::Vector<Vector3> mPoints;
123   Dali::Vector<Vector2> mTextures;
124   Dali::Vector<Vector2> mTextures2;
125   Dali::Vector<Vector3> mNormals;
126   Dali::Vector<Vector3> mTangents;
127   Dali::Vector<Vector3> mBiTangents;
128   Dali::Vector<TriIndex> mTriangles;
129
130   void CalculateTangentArray(const Dali::Vector<Vector3>& vertex,
131                              const Dali::Vector<Vector2>& texcoord,
132                              Dali::Vector<TriIndex>& triangle,
133                              Dali::Vector<Vector3>& normal,
134                              Dali::Vector<Vector3>& tangent);
135
136   void CenterAndScale(bool center, Dali::Vector<Vector3>& points);
137
138
139   void CreateGeometryArray(Dali::Vector<Vertex> & vertices,
140                            Dali::Vector<Vector2> & textures,
141                            Dali::Vector<VertexExt> & verticesExt,
142                            Dali::Vector<int> & indices);
143
144 };
145
146
147
148 } // namespace Internal
149
150 } // namespace Toolkit
151
152 } // namespace Dali
153
154
155
156
157 #endif // __DALI_TOOLKIT_INTERNAL_OBJ_LOADER_H__