Hello glTF my old friend

This commit is contained in:
marceline-cramer 2022-02-12 20:44:57 -07:00
parent c94f7b4bdc
commit 4c4d199b05
4 changed files with 234 additions and 5 deletions

152
Cargo.lock generated
View File

@ -46,6 +46,12 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "base64"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff"
[[package]]
name = "bit-set"
version = "0.5.2"
@ -345,7 +351,8 @@ version = "0.1.0"
dependencies = [
"bytemuck",
"glam",
"image",
"gltf",
"image 0.24.0",
"pollster",
"slab",
"tobj",
@ -399,6 +406,16 @@ dependencies = [
"syn",
]
[[package]]
name = "deflate"
version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73770f8e1fe7d64df17ca66ad28994a0a623ea497fa69486e14984e715c5d174"
dependencies = [
"adler32",
"byteorder",
]
[[package]]
name = "deflate"
version = "0.9.1"
@ -533,7 +550,7 @@ dependencies = [
"cfg-if 1.0.0",
"crc32fast",
"libc",
"miniz_oxide",
"miniz_oxide 0.4.4",
]
[[package]]
@ -632,6 +649,43 @@ dependencies = [
"web-sys",
]
[[package]]
name = "gltf"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00e0a0eace786193fc83644907097285396360e9e82e30f81a21e9b1ba836a3e"
dependencies = [
"base64",
"byteorder",
"gltf-json",
"image 0.23.14",
"lazy_static",
]
[[package]]
name = "gltf-derive"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bdd53d6e284bb2bf02a6926e4cc4984978c1990914d6cd9deae4e31cf37cd113"
dependencies = [
"inflections",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "gltf-json"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9949836a9ec5e7f83f76fb9bbcbc77f254a577ebbdb0820867bc11979ef97cad"
dependencies = [
"gltf-derive",
"serde",
"serde_derive",
"serde_json",
]
[[package]]
name = "gpu-alloc"
version = "0.5.3"
@ -707,6 +761,22 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]]
name = "image"
version = "0.23.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24ffcb7e7244a9bf19d35bf2883b9c080c4ced3c07a9895572178cdb8f13f6a1"
dependencies = [
"bytemuck",
"byteorder",
"color_quant",
"jpeg-decoder 0.1.22",
"num-iter",
"num-rational 0.3.2",
"num-traits",
"png 0.16.8",
]
[[package]]
name = "image"
version = "0.24.0"
@ -720,9 +790,9 @@ dependencies = [
"gif",
"jpeg-decoder 0.2.1",
"num-iter",
"num-rational",
"num-rational 0.4.0",
"num-traits",
"png",
"png 0.17.2",
"scoped_threadpool",
"tiff",
]
@ -746,6 +816,12 @@ dependencies = [
"adler32",
]
[[package]]
name = "inflections"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a257582fdcde896fd96463bf2d40eefea0580021c0712a0e2b028b60b47a837a"
[[package]]
name = "inplace_it"
version = "0.3.3"
@ -764,6 +840,12 @@ dependencies = [
"web-sys",
]
[[package]]
name = "itoa"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35"
[[package]]
name = "jni-sys"
version = "0.3.0"
@ -903,6 +985,15 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]]
name = "miniz_oxide"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "791daaae1ed6889560f8c4359194f56648355540573244a5448a83ba1ecc7435"
dependencies = [
"adler32",
]
[[package]]
name = "miniz_oxide"
version = "0.4.4"
@ -1062,6 +1153,17 @@ dependencies = [
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12ac428b1cb17fce6f731001d307d351ec70a6d202fc2e60f7d4c5e42d8f4f07"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.4.0"
@ -1195,6 +1297,18 @@ version = "0.3.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58893f751c9b0412871a09abd62ecd2a00298c6c83befa223ef98c52aef40cbe"
[[package]]
name = "png"
version = "0.16.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c3287920cb847dee3de33d301c463fba14dda99db24214ddf93f83d3021f4c6"
dependencies = [
"bitflags",
"crc32fast",
"deflate 0.8.6",
"miniz_oxide 0.3.7",
]
[[package]]
name = "png"
version = "0.17.2"
@ -1205,7 +1319,7 @@ dependencies = [
"crc32fast",
"deflate 0.9.1",
"encoding",
"miniz_oxide",
"miniz_oxide 0.4.4",
]
[[package]]
@ -1309,6 +1423,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]]
name = "ryu"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f"
[[package]]
name = "scoped-tls"
version = "1.0.0"
@ -1333,6 +1453,28 @@ version = "1.0.136"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789"
[[package]]
name = "serde_derive"
version = "1.0.136"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e8d9fa5c3b304765ce1fd9c4c8a3de2c8db365a5b91be52f186efc675681d95"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "slab"
version = "0.4.5"

View File

@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
bytemuck = { version="1.7", features=["derive"] }
glam = "0.20"
gltf = "1.0"
image = "0.24"
pollster = "0.2"
slab = "0.4"

Binary file not shown.

View File

@ -94,3 +94,89 @@ impl ObjModel {
});
}
}
pub struct GltfModel {}
impl GltfModel {
pub fn new(ren: &mut Renderer) -> Self {
use gltf::*;
let model_data = include_bytes!("assets/DamagedHelmet.glb");
let model = Gltf::from_slice(model_data.as_slice()).unwrap();
let loader = GltfLoader::load(&model, ren);
Self {}
}
}
pub struct GltfLoader<'a> {
pub model: &'a gltf::Gltf,
pub ren: &'a mut Renderer,
}
impl<'a> GltfLoader<'a> {
pub fn load(model: &'a gltf::Gltf, ren: &'a mut Renderer) -> Self {
let mut loader = Self { model, ren };
for scene in loader.model.scenes() {
for node in scene.nodes() {
loader.load_node(node);
}
}
loader
}
pub fn load_node(&mut self, node: gltf::Node) {
if let Some(mesh) = node.mesh() {
for primitive in mesh.primitives() {
self.load_primitive(primitive);
}
}
for child in node.children() {
self.load_node(child);
}
}
pub fn load_primitive(&mut self, primitive: gltf::Primitive) {
if primitive.mode() != gltf::mesh::Mode::Triangles {
panic!("glTF primitive must be triangle list");
}
let attributes = GltfPrimitiveAttributes::new(primitive);
let positions = attributes.positions.unwrap();
let normals = attributes.normals.unwrap();
let texcoords = attributes.texcoords.unwrap();
}
}
pub struct GltfPrimitiveAttributes<'a> {
pub positions: Option<gltf::Accessor<'a>>,
pub normals: Option<gltf::Accessor<'a>>,
pub texcoords: Option<gltf::Accessor<'a>>,
}
impl<'a> GltfPrimitiveAttributes<'a> {
pub fn new(primitive: gltf::Primitive<'a>) -> Self {
let mut positions = None;
let mut normals = None;
let mut texcoords = None;
for (semantic, accessor) in primitive.attributes() {
use gltf::Semantic::*;
match semantic {
Positions => positions = Some(accessor),
Normals => normals = Some(accessor),
TexCoords(0) => texcoords = Some(accessor),
_ => println!("unused glTF attribute {:?}", semantic),
}
}
Self {
positions,
normals,
texcoords,
}
}
}