♻️ refactor mesh loading

main
_ 2020-03-08 01:45:35 +00:00
parent e4f8d3e1b3
commit 04328b3564
2 changed files with 33 additions and 33 deletions

View File

@ -121,6 +121,15 @@ fn make_object_space_vec (inverse_model_mat: &Mat4, world_space_vec: &Vec3)
Vec3::from ((v4.x (), v4.y (), v4.z ())) Vec3::from ((v4.x (), v4.y (), v4.z ()))
} }
fn renderable_from_iqm_file <P> (filename: P) -> RenderableModel
where P: AsRef <std::path::Path>
{
let data = load_small_file (filename, 1024 * 1024);
let model = Model::from_slice (&data);
RenderableModel::from_iqm (&model)
}
fn main () { fn main () {
let sdl_context = sdl2::init ().unwrap (); let sdl_context = sdl2::init ().unwrap ();
let video_subsystem = sdl_context.video ().unwrap (); let video_subsystem = sdl_context.video ().unwrap ();
@ -212,20 +221,11 @@ fn main () {
let texture = Texture::from_file ("sky.png"); let texture = Texture::from_file ("sky.png");
texture.bind (); texture.bind ();
let model_data = load_small_file ("pumpking.iqm", 1024 * 1024); let mesh_pumpkin = renderable_from_iqm_file ("pumpking.iqm");
let model = Model::from_slice (&model_data); let mesh_sky = renderable_from_iqm_file ("sky-sphere.iqm");
let mesh_pitch = renderable_from_iqm_file ("pitch.iqm");
let renderable_model = RenderableModel::from_iqm (&model);
let sky_data = load_small_file ("sky-sphere.iqm", 1024 * 1024);
let sky_model = Model::from_slice (&sky_data);
let renderable_sky = RenderableModel::from_iqm (&sky_model);
let (renderable_pitch, pitch_colors, grass_index) = {
let data = load_small_file ("pitch.iqm", 1024 * 1024);
let model = Model::from_slice (&data);
let (pitch_colors, grass_index) = {
let silver = (255.0, 255.0, 255.0); let silver = (255.0, 255.0, 255.0);
let wood = (133.0, 76.0, 48.0); let wood = (133.0, 76.0, 48.0);
@ -249,8 +249,8 @@ fn main () {
].into_iter ()); ].into_iter ());
let mut grass_index = None; let mut grass_index = None;
let colors: Vec <_> = (0..model.meshes.len ()).map (|i| { let colors: Vec <_> = (0..mesh_pitch.meshes.len ()).map (|i| {
let name = str::from_utf8 (model.get_mesh_name (i)).unwrap (); let name = str::from_utf8 (&mesh_pitch.meshes [i].name).unwrap ();
if name == "Grass" { if name == "Grass" {
grass_index = Some (i); grass_index = Some (i);
} }
@ -261,7 +261,7 @@ fn main () {
} }
}).collect (); }).collect ();
(RenderableModel::from_iqm (&model), colors, grass_index.unwrap ()) (colors, grass_index.unwrap ())
}; };
glezz::enable_vertex_attrib_array (attrs ["pos"]); glezz::enable_vertex_attrib_array (attrs ["pos"]);
@ -364,20 +364,20 @@ fn main () {
{ {
glezz::uniform_3fv (unis [&ALBEDO], &orange); glezz::uniform_3fv (unis [&ALBEDO], &orange);
renderable_model.draw (&attrs, 0); mesh_pumpkin.draw (&attrs, 0);
glezz::uniform_3fv (unis [&ALBEDO], &green); glezz::uniform_3fv (unis [&ALBEDO], &green);
renderable_model.draw (&attrs, 1); mesh_pumpkin.draw (&attrs, 1);
} }
let mvp = view_mat * world_model_mat; let mvp = view_mat * world_model_mat;
glezz::uniform_matrix_4fv (unis [&MVP], &mvp); glezz::uniform_matrix_4fv (unis [&MVP], &mvp);
for (i, color) in pitch_colors.iter ().enumerate ().take (renderable_pitch.num_meshes ()) for (i, color) in pitch_colors.iter ().enumerate ().take (mesh_pitch.num_meshes ())
{ {
glezz::uniform_3fv (unis [&ALBEDO], color); glezz::uniform_3fv (unis [&ALBEDO], color);
if i != grass_index { if i != grass_index {
renderable_pitch.draw (&attrs, i); mesh_pitch.draw (&attrs, i);
} }
} }
@ -390,7 +390,7 @@ fn main () {
glezz::uniform_3fv (unis [&MIN_ALBEDO], &black); glezz::uniform_3fv (unis [&MIN_ALBEDO], &black);
glezz::uniform_1i (unis [&TEXTURE], 0); glezz::uniform_1i (unis [&TEXTURE], 0);
renderable_sky.draw (&attrs, 0); mesh_sky.draw (&attrs, 0);
} }
glezz::enable (gl::STENCIL_TEST); glezz::enable (gl::STENCIL_TEST);
@ -410,16 +410,16 @@ fn main () {
glezz::uniform_matrix_4fv (shadow_unis [&MVP], &mvp); glezz::uniform_matrix_4fv (shadow_unis [&MVP], &mvp);
{ {
renderable_model.draw (&shadow_attrs, 0); mesh_pumpkin.draw (&shadow_attrs, 0);
renderable_model.draw (&shadow_attrs, 1); mesh_pumpkin.draw (&shadow_attrs, 1);
} }
let mvp = view_mat * world_model_mat; let mvp = view_mat * world_model_mat;
glezz::uniform_matrix_4fv (shadow_unis [&MVP], &mvp); glezz::uniform_matrix_4fv (shadow_unis [&MVP], &mvp);
for i in 0..renderable_pitch.num_meshes () { for i in 0..mesh_pitch.num_meshes () {
if i != grass_index { if i != grass_index {
renderable_pitch.draw (&shadow_attrs, i); mesh_pitch.draw (&shadow_attrs, i);
} }
} }
@ -450,14 +450,14 @@ fn main () {
gl::StencilOp (gl::KEEP, gl::KEEP, gl::KEEP); gl::StencilOp (gl::KEEP, gl::KEEP, gl::KEEP);
} }
glezz::uniform_3fv (unis [&OBJECT_SPACE_LIGHT], &Vec3::from ((0.0, 0.0, 0.0))); glezz::uniform_3fv (unis [&OBJECT_SPACE_LIGHT], &Vec3::from ((0.0, 0.0, 0.0)));
renderable_pitch.draw (&attrs, grass_index); mesh_pitch.draw (&attrs, grass_index);
unsafe { unsafe {
gl::StencilFunc (gl::EQUAL, 0, 1); gl::StencilFunc (gl::EQUAL, 0, 1);
gl::StencilOp (gl::KEEP, gl::KEEP, gl::KEEP); gl::StencilOp (gl::KEEP, gl::KEEP, gl::KEEP);
} }
glezz::uniform_3fv (unis [&OBJECT_SPACE_LIGHT], &object_space_light); glezz::uniform_3fv (unis [&OBJECT_SPACE_LIGHT], &object_space_light);
renderable_pitch.draw (&attrs, grass_index); mesh_pitch.draw (&attrs, grass_index);
} }
window.gl_swap_window (); window.gl_swap_window ();

View File

@ -11,9 +11,10 @@ use crate::iqm;
// from the IQM model. IQM is zero-copy, but this is not. // from the IQM model. IQM is zero-copy, but this is not.
// Since it's opaque, I can drop in a VBO/IBO setup when I'm not lazy. // Since it's opaque, I can drop in a VBO/IBO setup when I'm not lazy.
struct RenderableMesh { pub struct RenderableMesh {
first_triangle: usize, first_triangle: usize,
num_triangles: i32, num_triangles: i32,
pub name: Vec <u8>,
} }
pub struct RenderableModel { pub struct RenderableModel {
@ -24,7 +25,7 @@ pub struct RenderableModel {
vertexes: VertexBuffer, vertexes: VertexBuffer,
indexes: IndexBuffer, indexes: IndexBuffer,
meshes: Vec <RenderableMesh>, pub meshes: Vec <RenderableMesh>,
} }
unsafe fn vertex_attrib_pointer (id: Option <u32>, num_coords: i32, float_offset: usize) { unsafe fn vertex_attrib_pointer (id: Option <u32>, num_coords: i32, float_offset: usize) {
@ -37,8 +38,6 @@ unsafe fn vertex_attrib_pointer (id: Option <u32>, num_coords: i32, float_offset
} }
impl RenderableModel { impl RenderableModel {
//pub fn from_
pub fn from_iqm (model: &iqm::Model) -> RenderableModel { pub fn from_iqm (model: &iqm::Model) -> RenderableModel {
let pos_bytes = model.get_vertex_slice (iqm::types::POSITION); let pos_bytes = model.get_vertex_slice (iqm::types::POSITION);
let uv_bytes = model.get_vertex_slice (iqm::types::TEXCOORD); let uv_bytes = model.get_vertex_slice (iqm::types::TEXCOORD);
@ -69,10 +68,11 @@ impl RenderableModel {
assert! (max_index * 2 < num_uv); assert! (max_index * 2 < num_uv);
assert! (max_index * 3 < num_normal); assert! (max_index * 3 < num_normal);
let meshes = model.meshes.iter () let meshes = model.meshes.iter ().enumerate ()
.map (|mesh| RenderableMesh { .map (|(i, mesh)| RenderableMesh {
first_triangle: mesh.first_triangle.try_into ().unwrap (), first_triangle: mesh.first_triangle.try_into ().unwrap (),
num_triangles: mesh.num_triangles.try_into ().unwrap (), num_triangles: mesh.num_triangles.try_into ().unwrap (),
name: model.get_mesh_name (i).to_owned (),
}) })
.collect (); .collect ();