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