Skip to content

Commit

Permalink
Use Root::push() in export example.
Browse files Browse the repository at this point in the history
This way, all `Index`es are guaranteed valid and the dependencies
between the glTF objects are reflected in the program's variables.

I have manually tested that the output of the example is valid glTF.
  • Loading branch information
kpreid committed Dec 18, 2023
1 parent bb53b13 commit a2508f5
Showing 1 changed file with 26 additions and 32 deletions.
58 changes: 26 additions & 32 deletions examples/export/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ fn export(output: Output) {

let (min, max) = bounding_coords(&triangle_vertices);

let mut root = gltf_json::Root::default();

let buffer_length = triangle_vertices.len() * mem::size_of::<Vertex>();
let buffer = json::Buffer {
let buffer = root.push(json::Buffer {
byte_length: USize64::from(buffer_length),
extensions: Default::default(),
extras: Default::default(),
Expand All @@ -83,19 +85,19 @@ fn export(output: Output) {
} else {
None
},
};
let buffer_view = json::buffer::View {
buffer: json::Index::new(0),
byte_length: buffer.byte_length,
});
let buffer_view = root.push(json::buffer::View {
buffer,
byte_length: USize64::from(buffer_length),
byte_offset: None,
byte_stride: Some(json::buffer::Stride(mem::size_of::<Vertex>())),
extensions: Default::default(),
extras: Default::default(),
name: None,
target: Some(Valid(json::buffer::Target::ArrayBuffer)),
};
let positions = json::Accessor {
buffer_view: Some(json::Index::new(0)),
});
let positions = root.push(json::Accessor {
buffer_view: Some(buffer_view),
byte_offset: Some(USize64(0)),
count: USize64::from(triangle_vertices.len()),
component_type: Valid(json::accessor::GenericComponentType(
Expand All @@ -109,9 +111,9 @@ fn export(output: Output) {
name: None,
normalized: false,
sparse: None,
};
let colors = json::Accessor {
buffer_view: Some(json::Index::new(0)),
});
let colors = root.push(json::Accessor {
buffer_view: Some(buffer_view),
byte_offset: Some(USize64::from(3 * mem::size_of::<f32>())),
count: USize64::from(triangle_vertices.len()),
component_type: Valid(json::accessor::GenericComponentType(
Expand All @@ -125,13 +127,13 @@ fn export(output: Output) {
name: None,
normalized: false,
sparse: None,
};
});

let primitive = json::mesh::Primitive {
attributes: {
let mut map = std::collections::BTreeMap::new();
map.insert(Valid(json::mesh::Semantic::Positions), json::Index::new(0));
map.insert(Valid(json::mesh::Semantic::Colors(0)), json::Index::new(1));
map.insert(Valid(json::mesh::Semantic::Positions), positions);
map.insert(Valid(json::mesh::Semantic::Colors(0)), colors);
map
},
extensions: Default::default(),
Expand All @@ -142,43 +144,35 @@ fn export(output: Output) {
targets: None,
};

let mesh = json::Mesh {
let mesh = root.push(json::Mesh {
extensions: Default::default(),
extras: Default::default(),
name: None,
primitives: vec![primitive],
weights: None,
};
});

let node = json::Node {
let node = root.push(json::Node {
camera: None,
children: None,
extensions: Default::default(),
extras: Default::default(),
matrix: None,
mesh: Some(json::Index::new(0)),
mesh: Some(mesh),
name: None,
rotation: None,
scale: None,
translation: None,
skin: None,
weights: None,
};
});

let root = json::Root {
accessors: vec![positions, colors],
buffers: vec![buffer],
buffer_views: vec![buffer_view],
meshes: vec![mesh],
root.push(json::Scene {
extensions: Default::default(),
extras: Default::default(),
name: None,
nodes: vec![node],
scenes: vec![json::Scene {
extensions: Default::default(),
extras: Default::default(),
name: None,
nodes: vec![json::Index::new(0)],
}],
..Default::default()
};
});

match output {
Output::Standard => {
Expand Down

0 comments on commit a2508f5

Please sign in to comment.