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