2021-32-bit-holiday-jam/src/bin/platformer/level_loader.rs

82 lines
1.7 KiB
Rust

use anyhow::Result;
use opengl_rust::{
prelude::*,
};
pub struct LoadedLevel {
pub player_spawn: Vec3,
pub buffer: Vec <u8>,
pub level: gltf::Document,
}
impl LoadedLevel {
pub fn from_path (path: &str) -> Result <Self> {
use gltf::{
Semantic,
};
let (level, buffers, _) = gltf::import (path)?;
let buffer = match buffers.get (0) {
None => bail! ("gltf didn't load any buffers"),
Some (x) => x.0.to_vec (),
};
let scene = match level.scenes ().next () {
None => bail! ("No scenes in glTF file"),
Some (x) => x,
};
let mut player_spawn = None;
for node in scene.nodes () {
if node.name () == Some ("Player Spawn") {
let (translation, _, _) = node.transform ().decomposed ();
player_spawn = Some (Vec3::from (translation));
}
else if let Some (_camera) = node.camera () {
}
else if let Some (mesh) = node.mesh () {
for (_i, prim) in mesh.primitives ().enumerate () {
let positions = match prim.get (&Semantic::Positions) {
None => continue,
Some (x) => x,
};
let normals = match prim.get (&Semantic::Normals) {
None => continue,
Some (x) => x,
};
let indices = match prim.indices () {
None => continue,
Some (x) => x,
};
let _pos_view = match positions.view () {
None => continue,
Some (x) => x,
};
let _norm_view = match normals.view () {
None => continue,
Some (x) => x,
};
let _indices_view = match indices.view () {
None => continue,
Some (x) => x,
};
}
}
}
let player_spawn = match player_spawn {
None => bail! ("glTF file must have `Player Spawn` node"),
Some (x) => x,
};
Ok (Self {
buffer,
level,
player_spawn,
})
}
}