♻️ refactor

main
_ 2022-01-09 17:03:58 +00:00
parent 252c299c2c
commit 0dcf70649d
2 changed files with 84 additions and 77 deletions

View File

@ -0,0 +1,82 @@
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,
scene::Transform,
};
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,
})
}
}

View File

@ -19,9 +19,11 @@ use opengl_rust::{
};
mod graphics;
mod level_loader;
mod virtual_gamepad;
use graphics::Graphics;
use level_loader::LoadedLevel;
use virtual_gamepad::VirtualGamepad;
pub struct GameState {
@ -407,83 +409,6 @@ async fn main () -> Result <()> {
Ok (())
}
struct LoadedLevel {
player_spawn: Vec3,
buffer: Vec <u8>,
level: gltf::Document,
}
impl LoadedLevel {
fn from_path (path: &str) -> Result <Self> {
use gltf::{
Semantic,
scene::Transform,
};
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,
})
}
}
struct ShaderLocations {
attr_pos: u32,
attr_normal: u32,