1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346

// Copyright 2017 The gltf Library Developers
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use serde;
use std;
use std::fmt;
use json::*;
use validation::{Error, Validate};

/// Helper trait for retrieving top-level objects by a universal identifier.
pub trait Get<T> {
    /// Retrieves a single value at the given index.
    fn get(&self, id: &Index<T>) -> Option<&T>;
}

/// Represents an offset into an array of type `T` owned by the root glTF object.
#[derive(Clone, Copy)]
pub struct Index<T>(u32, std::marker::PhantomData<T>);

/// The root object of a glTF 2.0 asset.
#[derive(Clone, Debug, Default, Deserialize, Validate)]
pub struct Root {
    /// An array of accessors.
    #[serde(default)]
    pub accessors: Vec<accessor::Accessor>,
    
    /// An array of keyframe animations.
    #[serde(default)]
    pub animations: Vec<animation::Animation>,

    /// Metadata about the glTF asset.
    pub asset: asset::Asset,
    
    /// An array of buffers.
    #[serde(default)]
    pub buffers: Vec<buffer::Buffer>,
    
    /// An array of buffer views.
    #[serde(default, rename = "bufferViews")]
    pub buffer_views: Vec<buffer::View>,

    /// The default scene.
    pub scene: Option<Index<scene::Scene>>,

    /// Extension specific data.
    #[serde(default)]
    pub extensions: extensions::root::Root,

    /// Optional application specific data.
    #[serde(default)]
    pub extras: Extras,
    
    /// Names of glTF extensions used somewhere in this asset.
    #[serde(default, rename = "extensionsUsed")]
    pub extensions_used: Vec<String>,

    /// Names of glTF extensions required to properly load this asset.
    #[serde(default, rename = "extensionsRequired")]
    pub extensions_required: Vec<String>,
    
    /// An array of cameras.
    #[serde(default)]
    pub cameras: Vec<camera::Camera>,
    
    /// An array of images.
    #[serde(default)]
    pub images: Vec<image::Image>,
    
    /// An array of materials.
    #[serde(default)]
    pub materials: Vec<material::Material>,
    
    /// An array of meshes.
    #[serde(default)]
    pub meshes: Vec<mesh::Mesh>,
    
    /// An array of nodes.
    #[serde(default)]
    pub nodes: Vec<scene::Node>,
    
    /// An array of samplers.
    #[serde(default)]
    pub samplers: Vec<texture::Sampler>,
    
    /// An array of scenes.
    #[serde(default)]
    pub scenes: Vec<scene::Scene>,
    
    /// An array of skins.
    #[serde(default)]
    pub skins: Vec<skin::Skin>,
    
    /// An array of textures.
    #[serde(default)]
    pub textures: Vec<texture::Texture>,
}

impl Root {
    /// Returns the accessor at the given index.
    pub fn accessor(&self, index: Index<accessor::Accessor>) -> &accessor::Accessor {
        &self.accessors[index.0 as usize]
    }

    /// Returns all accessors as a slice.
    pub fn accessors(&self) -> &[accessor::Accessor] {
        &self.accessors
    }

    /// Returns the animation at the given index.
    pub fn animation(&self, index: Index<animation::Animation>) -> &animation::Animation {
        &self.animations[index.0 as usize]
    }

    /// Returns all animations as a slice.
    pub fn animations(&self) -> &[animation::Animation] {
        &self.animations
    }

    /// Returns the metadata included with this asset.
    pub fn asset(&self) -> &asset::Asset {
        &self.asset
    }

    /// Returns the buffer at the given index.
    pub fn buffer(&self, index: Index<buffer::Buffer>) -> &buffer::Buffer {
        &self.buffers[index.0 as usize]
    }

    /// Returns all buffers as a slice.
    pub fn buffers(&self) -> &[buffer::Buffer] {
        &self.buffers
    }

    /// Returns the buffer view at the given index.
    pub fn buffer_view(&self, index: Index<buffer::View>) -> &buffer::View {
        &self.buffer_views[index.0 as usize]
    }

    /// Returns all buffer views as a slice.
    pub fn buffer_views(&self) -> &[buffer::View] {
        &self.buffer_views
    }

    /// Returns the camera at the given index.
    pub fn camera(&self, index: Index<camera::Camera>) -> &camera::Camera {
        &self.cameras[index.0 as usize]
    }

    /// Returns all cameras as a slice.
    pub fn cameras(&self) -> &[camera::Camera] {
        &self.cameras
    }

    /// Returns the extensions referenced in this .gltf file.
    pub fn extensions_used(&self) -> &[String] {
        &self.extensions_used
    }

    /// Returns the extensions required to load and render this asset.
    pub fn extensions_required(&self) -> &[String] {
        &self.extensions_required
    }

    /// Returns a single item from the root object.
    pub fn get<T>(&self, index: &Index<T>) -> Option<&T>
        where Self: Get<T>
    {
        (self as &Get<T>).get(index)
    }

    /// Returns the image at the given index.
    pub fn image(&self, index: Index<image::Image>) -> &image::Image {
        &self.images[index.0 as usize]
    }

    /// Returns all images as a slice.
    pub fn images(&self) -> &[image::Image] {
        &self.images
    }

    /// Returns the material at the given index.
    pub fn material(&self, index: Index<material::Material>) -> &material::Material {
        &self.materials[index.0 as usize]
    }

    /// Returns all materials as a slice.
    pub fn materials(&self) -> &[material::Material] {
        &self.materials
    }

    /// Returns the mesh at the given index.
    pub fn mesh(&self, index: Index<mesh::Mesh>) -> &mesh::Mesh {
        &self.meshes[index.0 as usize]
    }

    /// Returns all meshes as a slice.
    pub fn meshes(&self) -> &[mesh::Mesh] {
        &self.meshes
    }

    /// Returns the node at the given index.
    pub fn node(&self, index: Index<scene::Node>) -> &scene::Node {
        &self.nodes[index.0 as usize]
    }

    /// Returns all nodes as a slice.
    pub fn nodes(&self) -> &[scene::Node] {
        &self.nodes
    }

    /// Returns the sampler at the given index.
    pub fn sampler(&self, index: Index<texture::Sampler>) -> &texture::Sampler {
        &self.samplers[index.0 as usize]
    }

    /// Returns all samplers as a slice.
    pub fn samplers(&self) -> &[texture::Sampler] {
        &self.samplers
    }

    /// Returns the scene at the given index.
    pub fn scene(&self, index: Index<scene::Scene>) -> &scene::Scene {
        &self.scenes[index.0 as usize]
    }

    /// Returns all scenes as a slice.
    pub fn scenes(&self) -> &[scene::Scene] {
        &self.scenes
    }

    /// Returns the skin at the given index.
    pub fn skin(&self, index: Index<skin::Skin>) -> &skin::Skin {
        &self.skins[index.0 as usize]
    }

    /// Returns all skins as a slice.
    pub fn skins(&self) -> &[skin::Skin] {
        &self.skins
    }

    /// Returns the texture at the given index.
    pub fn texture(&self, index: Index<texture::Texture>) -> &texture::Texture {
        &self.textures[index.0 as usize]
    }

    /// Returns all textures as a slice.
    pub fn textures(&self) -> &[texture::Texture] {
        &self.textures
    }
}

impl<T> Index<T> {
    /// Creates a new `Index` representing an offset into an array containing `T`.
    fn new(value: u32) -> Self {
        Index(value, std::marker::PhantomData)
    }

    /// Returns the internal offset value.
    pub fn value(&self) -> usize {
        self.0 as usize
    }
}

impl<T> serde::Serialize for Index<T> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where S: ::serde::Serializer
    {
        serializer.serialize_u64(self.value() as u64)
    }
}

impl<'de, T> serde::Deserialize<'de> for Index<T> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: serde::Deserializer<'de>
    {
        struct Visitor<T>(std::marker::PhantomData<T>);
        impl<'de, T> serde::de::Visitor<'de> for Visitor<T> {
            type Value = Index<T>;

            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                formatter.write_str("index into child of root")
            }

            fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
                where E: serde::de::Error
            {
                Ok(Index::new(value as u32))
            }
        }
        deserializer.deserialize_u64(Visitor::<T>(std::marker::PhantomData))
    }
}

impl<T> fmt::Debug for Index<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl<T> fmt::Display for Index<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl<T: Validate> Validate for Index<T>
    where Root: Get<T>
{
    fn validate_minimally<P, R>(&self, root: &Root, path: P, mut report: &mut R)
        where P: Fn() -> Path, R: FnMut(&Fn() -> Path, Error)
    {
        if root.get(self).is_none() {
            report(&path, Error::IndexOutOfBounds);
        }
    }
}

macro_rules! impl_get {
    ($ty:ty, $field:ident) => {
        impl<'a> Get<$ty> for Root {
            fn get(&self, index: &Index<$ty>) -> Option<&$ty> {
                self.$field.get(index.value())
            }
        }
    }
}

impl_get!(accessor::Accessor, accessors);
impl_get!(animation::Animation, animations);
impl_get!(buffer::Buffer, buffers);
impl_get!(buffer::View, buffer_views);
impl_get!(camera::Camera, cameras);
impl_get!(image::Image, images);
impl_get!(material::Material, materials);
impl_get!(mesh::Mesh, meshes);
impl_get!(scene::Node, nodes);
impl_get!(texture::Sampler, samplers);
impl_get!(scene::Scene, scenes);
impl_get!(skin::Skin, skins);
impl_get!(texture::Texture, textures);