♻️ refactor mesh loading
parent
e4f8d3e1b3
commit
04328b3564
|
@ -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 ()))
|
||||
}
|
||||
|
||||
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 () {
|
||||
let sdl_context = sdl2::init ().unwrap ();
|
||||
let video_subsystem = sdl_context.video ().unwrap ();
|
||||
|
@ -212,20 +221,11 @@ fn main () {
|
|||
let texture = Texture::from_file ("sky.png");
|
||||
texture.bind ();
|
||||
|
||||
let model_data = load_small_file ("pumpking.iqm", 1024 * 1024);
|
||||
let model = Model::from_slice (&model_data);
|
||||
|
||||
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 mesh_pumpkin = renderable_from_iqm_file ("pumpking.iqm");
|
||||
let mesh_sky = renderable_from_iqm_file ("sky-sphere.iqm");
|
||||
let mesh_pitch = renderable_from_iqm_file ("pitch.iqm");
|
||||
|
||||
let (pitch_colors, grass_index) = {
|
||||
let silver = (255.0, 255.0, 255.0);
|
||||
let wood = (133.0, 76.0, 48.0);
|
||||
|
||||
|
@ -249,8 +249,8 @@ fn main () {
|
|||
].into_iter ());
|
||||
|
||||
let mut grass_index = None;
|
||||
let colors: Vec <_> = (0..model.meshes.len ()).map (|i| {
|
||||
let name = str::from_utf8 (model.get_mesh_name (i)).unwrap ();
|
||||
let colors: Vec <_> = (0..mesh_pitch.meshes.len ()).map (|i| {
|
||||
let name = str::from_utf8 (&mesh_pitch.meshes [i].name).unwrap ();
|
||||
if name == "Grass" {
|
||||
grass_index = Some (i);
|
||||
}
|
||||
|
@ -261,7 +261,7 @@ fn main () {
|
|||
}
|
||||
}).collect ();
|
||||
|
||||
(RenderableModel::from_iqm (&model), colors, grass_index.unwrap ())
|
||||
(colors, grass_index.unwrap ())
|
||||
};
|
||||
|
||||
glezz::enable_vertex_attrib_array (attrs ["pos"]);
|
||||
|
@ -364,20 +364,20 @@ fn main () {
|
|||
|
||||
{
|
||||
glezz::uniform_3fv (unis [&ALBEDO], &orange);
|
||||
renderable_model.draw (&attrs, 0);
|
||||
mesh_pumpkin.draw (&attrs, 0);
|
||||
|
||||
glezz::uniform_3fv (unis [&ALBEDO], &green);
|
||||
renderable_model.draw (&attrs, 1);
|
||||
mesh_pumpkin.draw (&attrs, 1);
|
||||
}
|
||||
|
||||
let mvp = view_mat * world_model_mat;
|
||||
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);
|
||||
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_1i (unis [&TEXTURE], 0);
|
||||
|
||||
renderable_sky.draw (&attrs, 0);
|
||||
mesh_sky.draw (&attrs, 0);
|
||||
}
|
||||
|
||||
glezz::enable (gl::STENCIL_TEST);
|
||||
|
@ -410,16 +410,16 @@ fn main () {
|
|||
glezz::uniform_matrix_4fv (shadow_unis [&MVP], &mvp);
|
||||
|
||||
{
|
||||
renderable_model.draw (&shadow_attrs, 0);
|
||||
renderable_model.draw (&shadow_attrs, 1);
|
||||
mesh_pumpkin.draw (&shadow_attrs, 0);
|
||||
mesh_pumpkin.draw (&shadow_attrs, 1);
|
||||
}
|
||||
|
||||
let mvp = view_mat * world_model_mat;
|
||||
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 {
|
||||
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);
|
||||
}
|
||||
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 {
|
||||
gl::StencilFunc (gl::EQUAL, 0, 1);
|
||||
gl::StencilOp (gl::KEEP, gl::KEEP, gl::KEEP);
|
||||
}
|
||||
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 ();
|
||||
|
|
|
@ -11,9 +11,10 @@ use crate::iqm;
|
|||
// 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.
|
||||
|
||||
struct RenderableMesh {
|
||||
pub struct RenderableMesh {
|
||||
first_triangle: usize,
|
||||
num_triangles: i32,
|
||||
pub name: Vec <u8>,
|
||||
}
|
||||
|
||||
pub struct RenderableModel {
|
||||
|
@ -24,7 +25,7 @@ pub struct RenderableModel {
|
|||
vertexes: VertexBuffer,
|
||||
indexes: IndexBuffer,
|
||||
|
||||
meshes: Vec <RenderableMesh>,
|
||||
pub meshes: Vec <RenderableMesh>,
|
||||
}
|
||||
|
||||
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 {
|
||||
//pub fn from_
|
||||
|
||||
pub fn from_iqm (model: &iqm::Model) -> RenderableModel {
|
||||
let pos_bytes = model.get_vertex_slice (iqm::types::POSITION);
|
||||
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 * 3 < num_normal);
|
||||
|
||||
let meshes = model.meshes.iter ()
|
||||
.map (|mesh| RenderableMesh {
|
||||
let meshes = model.meshes.iter ().enumerate ()
|
||||
.map (|(i, mesh)| RenderableMesh {
|
||||
first_triangle: mesh.first_triangle.try_into ().unwrap (),
|
||||
num_triangles: mesh.num_triangles.try_into ().unwrap (),
|
||||
name: model.get_mesh_name (i).to_owned (),
|
||||
})
|
||||
.collect ();
|
||||
|
||||
|
|
Loading…
Reference in New Issue