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