loading camera transform from gltf

main
_ 2021-12-22 01:52:18 +00:00
parent c420eada1f
commit 893e01895f
2 changed files with 24 additions and 5 deletions

View File

@ -165,6 +165,7 @@ impl Graphics {
state: &GameState,
gl_state: &mut GlState,
level: &crate::LoadedLevel,
camera: &crate::Camera,
) {
use uniforms as u;
@ -178,9 +179,7 @@ impl Graphics {
let proj_mat = Mat4::perspective_rh_gl (fov.to_radians (), screen_size.0 / screen_size.1, 0.125, 200.0);
let view_mat = proj_mat *
Mat4::from_rotation_x (-63.6f32.to_radians ()) *
Mat4::from_translation ((0.0, 23.3, -12.2).into ());
let view_mat = proj_mat * camera.mat;
let world_model_mat = Mat4::IDENTITY;
self.passes [0].with (gl_state, || {

View File

@ -69,15 +69,22 @@ async fn main () -> Result <()> {
let level = LoadedLevel::from_path ("gltf/level-00.glb")?;
let scene = level.level.scenes ().next ().unwrap ();
let mut camera = None;
let mut phys_tris: Vec <opengl_rust::physics::Triangle> = Vec::new ();
for node in scene.nodes () {
if let Some (c) = node.camera () {
camera = Some (Camera {
mat: gltf_node_get_mat4 (&node).inverse (),
});
}
let mesh = match node.mesh () {
None => continue,
Some (x) => x,
};
let m = Mat4::from_cols_array_2d (&node.transform ().matrix ());
let m = gltf_node_get_mat4 (&node);
for prim in mesh.primitives () {
use gltf::Semantic;
@ -144,6 +151,11 @@ async fn main () -> Result <()> {
}
}
let camera = match camera {
None => bail! ("Couldn't find camera node in glTF file"),
Some (x) => x,
};
let mut graphics = Graphics::new ();
let mut gl_state = Default::default ();
@ -284,7 +296,7 @@ async fn main () -> Result <()> {
window.gl_make_current (&gl_ctx).unwrap ();
graphics.draw (&game_state, &mut gl_state, &level);
graphics.draw (&game_state, &mut gl_state, &level, &camera);
graphics.frames += 1;
window.gl_swap_window ();
@ -398,3 +410,11 @@ struct TriangleStream {
pub verts: gpu_buffers::VertexBuffer,
pub indices: gpu_buffers::IndexBuffer,
}
struct Camera {
mat: Mat4,
}
fn gltf_node_get_mat4 (node: &gltf::Node) -> Mat4 {
Mat4::from_cols_array_2d (&node.transform ().matrix ())
}