Removed duplicated code by creating third-party folder
[platform/core/uifw/dali-demo.git] / examples / reflection-demo / gltf-scene.h
1 #ifndef GLTF_SCENE_H
2 #define GLTF_SCENE_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
22 // EXTERNAL INCLUDES
23 #include <string>
24 #include <dali/integration-api/debug.h>
25
26 // INTERNAL INCLUDES
27 #include "third-party/pico-json.h"
28
29 #define GLTF_LOG(...)                                                                   \
30   {                                                                                     \
31     Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo, __VA_ARGS__); \
32   }
33
34 enum class glTFAttributeType
35 {
36   POSITION   = 0,
37   NORMAL     = 1,
38   TEXCOORD_0 = 2,
39   UNDEFINED,
40 };
41
42 struct glTF_Camera
43 {
44   std::string name;
45
46   // perspective setup
47   float yfov;
48   float zfar;
49   float znear;
50
51   bool isPerspective;
52 };
53
54 struct glTF_BufferView
55 {
56   uint32_t bufferIndex;
57   uint32_t byteLength;
58   uint32_t byteOffset;
59   void*    data;
60 };
61
62 struct glTF_Accessor
63 {
64   uint32_t    bufferView;
65   uint32_t    componentType;
66   uint32_t    count;
67   uint32_t    componentSize;
68   std::string type;
69 };
70
71 struct glTF_Mesh
72 {
73   std::string                                         name;
74   std::vector<std::pair<glTFAttributeType, uint32_t>> attributes;
75   uint32_t                                            indices;
76   uint32_t                                            material;
77 };
78
79 struct glTF_Texture
80 {
81   std::string uri;
82   std::string name;
83 };
84
85 struct glTF_Material
86 {
87   bool        doubleSided;
88   std::string name;
89   struct pbrMetallicRoughness
90   {
91     bool enabled{false};
92     struct baseTextureColor
93     {
94       uint32_t index;
95       uint32_t texCoord;
96     } baseTextureColor;
97   } pbrMetallicRoughness;
98 };
99
100 struct glTF_Node
101 {
102   uint32_t              index{0u};
103   std::string           name{};
104   uint32_t              meshId{0xffffffff};
105   uint32_t              cameraId{0xffffffff};
106   glTF_Node*            parent{nullptr};
107   std::vector<uint32_t> children{};
108
109   // Transform
110   float rotationQuaternion[4] = {0.0f, 0.0f, 0.0f, 1.0f};
111   float translation[3]        = {0.0f, 0.0f, 0.0f};
112   float scale[3]              = {1.0f, 1.0f, 1.0f};
113 };
114
115 using glTF_Buffer = std::vector<unsigned char>;
116
117 /**
118  * Simple glTF parser
119  *
120  * This implementation requires 2 files (it doesn't decode Base64 embedded in json)
121  */
122 struct glTF
123 {
124   glTF(const std::string& filename);
125   ~glTF() = default;
126
127   std::vector<const glTF_Mesh*> GetMeshes() const;
128
129   std::vector<const glTF_Camera*> GetCameras();
130
131   const std::vector<glTF_Material>& GetMaterials() const;
132
133   const std::vector<glTF_Texture>& GetTextures() const;
134
135   const std::vector<glTF_Node>& GetNodes() const
136   {
137     return mNodes;
138   }
139
140   /**
141    * MESH interface
142    */
143   /**
144    * Returns a copy of attribute buffer
145    * @return
146    */
147   std::vector<unsigned char> GetMeshAttributeBuffer(const glTF_Mesh& mesh, const std::vector<glTFAttributeType>& attrTypes);
148   uint32_t                   GetMeshAttributeCount(const glTF_Mesh* mesh) const;
149   const glTF_Mesh*           FindMeshByName(const std::string& name) const;
150
151   /**
152    * Returns a copy of index buffer
153    * @return
154    */
155   std::vector<uint16_t> GetMeshIndexBuffer(const glTF_Mesh* mesh) const;
156
157   const glTF_Node* FindNodeByName(const std::string& name) const;
158
159 private:
160   void LoadFromFile(const std::string& filename);
161
162   glTF_Buffer LoadFile(const std::string& filename);
163
164   bool ParseJSON();
165
166   std::vector<glTF_Mesh>       mMeshes;
167   std::vector<glTF_Camera>     mCameras;
168   std::vector<glTF_BufferView> mBufferViews;
169   std::vector<glTF_Accessor>   mAccessors;
170   std::vector<glTF_Node>       mNodes;
171   std::vector<glTF_Material>   mMaterials;
172   std::vector<glTF_Texture>    mTextures;
173   glTF_Buffer                  mBuffer;
174   glTF_Buffer                  jsonBuffer;
175
176   // json nodes
177   picojson::value jsonNode;
178 };
179
180 #endif //DALI_CMAKE_GLTF_SCENE_H