diff options
author | HampusM <hampus@hampusmat.com> | 2024-05-18 15:39:55 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-05-18 15:39:55 +0200 |
commit | 9af501c99880b2e4be3458bab0891e1ea1a2e4a0 (patch) | |
tree | cb0c4d054093e5a19198b7684676c25c7ac063bf /src | |
parent | 9ce344220ec7d03ec2902907aa41f126faf84df0 (diff) |
chore: use obj models
Diffstat (limited to 'src')
-rw-r--r-- | src/cube.rs | 529 | ||||
-rw-r--r-- | src/main.rs | 82 |
2 files changed, 34 insertions, 577 deletions
diff --git a/src/cube.rs b/src/cube.rs deleted file mode 100644 index 9877b98..0000000 --- a/src/cube.rs +++ /dev/null @@ -1,529 +0,0 @@ -use engine::math::calc_triangle_surface_normal; -use engine::mesh::Mesh; -use engine::vector::Vec3; -use engine::vertex::{Builder as VertexBuilder, Vertex}; - -pub enum Side -{ - Front, - Back, - Left, - Right, - Top, - Bottom, -} - -pub enum Corner -{ - TopRight, - TopLeft, - BottomRight, - BottomLeft, -} - -pub fn create_cube_mesh( - vertex_builder_cb: impl Fn(VertexBuilder, Side, Corner) -> VertexBuilder, -) -> Mesh -{ - let mut vertices: [Option<Vertex>; VertexIndex::VARIANT_CNT] = - [(); VertexIndex::VARIANT_CNT].map(|()| None); - - let front_top_right_pos = Vec3 { x: 0.5, y: 0.5, z: -0.5 }; - let front_bottom_right_pos = Vec3 { x: 0.5, y: -0.5, z: -0.5 }; - let front_bottom_left_pos = Vec3 { x: -0.5, y: -0.5, z: -0.5 }; - let front_top_left_pos = Vec3 { x: -0.5, y: 0.5, z: -0.5 }; - - let front_normal = calc_triangle_surface_normal( - &front_top_right_pos, - &front_bottom_right_pos, - &front_top_left_pos, - ); - - vertices[VertexIndex::FrontTopRight as usize] = Some( - vertex_builder_cb( - VertexBuilder::new() - .pos(front_top_right_pos) - .normal(front_normal.clone()), - Side::Front, - Corner::TopRight, - ) - .build() - .unwrap(), - ); - - vertices[VertexIndex::FrontBottomRight as usize] = Some( - vertex_builder_cb( - VertexBuilder::new() - .pos(front_bottom_right_pos) - .normal(front_normal.clone()), - Side::Front, - Corner::BottomRight, - ) - .build() - .unwrap(), - ); - - vertices[VertexIndex::FrontBottomLeft as usize] = Some( - vertex_builder_cb( - VertexBuilder::new() - .pos(front_bottom_left_pos) - .normal(front_normal.clone()), - Side::Front, - Corner::BottomLeft, - ) - .build() - .unwrap(), - ); - - vertices[VertexIndex::FrontTopLeft as usize] = Some( - vertex_builder_cb( - VertexBuilder::new() - .pos(front_top_left_pos) - .normal(front_normal.clone()), - Side::Front, - Corner::TopLeft, - ) - .build() - .unwrap(), - ); - - let back_top_right_pos = Vec3 { x: 0.5, y: 0.5, z: 0.5 }; - let back_bottom_right_pos = Vec3 { x: 0.5, y: -0.5, z: 0.5 }; - let back_bottom_left_pos = Vec3 { x: -0.5, y: -0.5, z: 0.5 }; - let back_top_left_pos = Vec3 { x: -0.5, y: 0.5, z: 0.5 }; - - let back_normal = -calc_triangle_surface_normal( - &back_top_right_pos, - &back_bottom_right_pos, - &back_top_left_pos, - ); - - vertices[VertexIndex::BackTopRight as usize] = Some( - vertex_builder_cb( - VertexBuilder::new() - .pos(back_top_right_pos) - .normal(back_normal.clone()), - Side::Back, - Corner::TopRight, - ) - .build() - .unwrap(), - ); - - vertices[VertexIndex::BackBottomRight as usize] = Some( - vertex_builder_cb( - VertexBuilder::new() - .pos(back_bottom_right_pos) - .normal(back_normal.clone()), - Side::Back, - Corner::BottomRight, - ) - .build() - .unwrap(), - ); - - vertices[VertexIndex::BackBottomLeft as usize] = Some( - vertex_builder_cb( - VertexBuilder::new() - .pos(back_bottom_left_pos) - .normal(back_normal.clone()), - Side::Back, - Corner::BottomLeft, - ) - .build() - .unwrap(), - ); - - vertices[VertexIndex::BackTopLeft as usize] = Some( - vertex_builder_cb( - VertexBuilder::new() - .pos(back_top_left_pos) - .normal(back_normal), - Side::Back, - Corner::TopLeft, - ) - .build() - .unwrap(), - ); - - let right_back_top_pos = Vec3 { x: 0.5, y: 0.5, z: 0.5 }; - let right_back_bottom_pos = Vec3 { x: 0.5, y: -0.5, z: 0.5 }; - let right_front_top_pos = Vec3 { x: 0.5, y: 0.5, z: -0.5 }; - let right_front_bottom_pos = Vec3 { x: 0.5, y: -0.5, z: -0.5 }; - - let right_normal = calc_triangle_surface_normal( - &right_back_top_pos, - &right_back_bottom_pos, - &right_front_top_pos, - ); - - vertices[VertexIndex::RightBackTop as usize] = Some( - vertex_builder_cb( - VertexBuilder::new() - .pos(right_back_top_pos) - .normal(right_normal.clone()), - Side::Right, - Corner::TopLeft, - ) - .build() - .unwrap(), - ); - - vertices[VertexIndex::RightBackBottom as usize] = Some( - vertex_builder_cb( - VertexBuilder::new() - .pos(right_back_bottom_pos) - .normal(right_normal.clone()), - Side::Right, - Corner::BottomLeft, - ) - .build() - .unwrap(), - ); - - vertices[VertexIndex::RightFrontTop as usize] = Some( - vertex_builder_cb( - VertexBuilder::new() - .pos(right_front_top_pos) - .normal(right_normal.clone()), - Side::Right, - Corner::TopRight, - ) - .build() - .unwrap(), - ); - - vertices[VertexIndex::RightFrontBottom as usize] = Some( - vertex_builder_cb( - VertexBuilder::new() - .pos(right_front_bottom_pos) - .normal(right_normal), - Side::Right, - Corner::BottomRight, - ) - .build() - .unwrap(), - ); - - let left_back_top_pos = Vec3 { x: -0.5, y: 0.5, z: 0.5 }; - let left_back_bottom_pos = Vec3 { x: -0.5, y: -0.5, z: 0.5 }; - let left_front_top_pos = Vec3 { x: -0.5, y: 0.5, z: -0.5 }; - let left_front_bottom_pos = Vec3 { x: -0.5, y: -0.5, z: -0.5 }; - - let left_normal = -calc_triangle_surface_normal( - &left_back_top_pos, - &left_back_bottom_pos, - &left_front_top_pos, - ); - - vertices[VertexIndex::LeftBackTop as usize] = Some( - vertex_builder_cb( - VertexBuilder::new() - .pos(left_back_top_pos) - .normal(left_normal.clone()), - Side::Left, - Corner::TopRight, - ) - .build() - .unwrap(), - ); - - vertices[VertexIndex::LeftBackBottom as usize] = Some( - vertex_builder_cb( - VertexBuilder::new() - .pos(left_back_bottom_pos) - .normal(left_normal.clone()), - Side::Left, - Corner::BottomRight, - ) - .build() - .unwrap(), - ); - - vertices[VertexIndex::LeftFrontTop as usize] = Some( - vertex_builder_cb( - VertexBuilder::new() - .pos(left_front_top_pos) - .normal(left_normal.clone()), - Side::Left, - Corner::TopLeft, - ) - .build() - .unwrap(), - ); - - vertices[VertexIndex::LeftFrontBottom as usize] = Some( - vertex_builder_cb( - VertexBuilder::new() - .pos(left_front_bottom_pos) - .normal(left_normal), - Side::Left, - Corner::BottomLeft, - ) - .build() - .unwrap(), - ); - - let top_back_right_pos = Vec3 { x: 0.5, y: 0.5, z: 0.5 }; - let top_back_left_pos = Vec3 { x: -0.5, y: 0.5, z: 0.5 }; - let top_front_left_pos = Vec3 { x: -0.5, y: 0.5, z: -0.5 }; - let top_front_right_pos = Vec3 { x: 0.5, y: 0.5, z: -0.5 }; - - let top_normal = -calc_triangle_surface_normal( - &top_back_right_pos, - &top_back_left_pos, - &top_front_right_pos, - ); - - vertices[VertexIndex::TopBackRight as usize] = Some( - vertex_builder_cb( - VertexBuilder::new() - .pos(top_back_right_pos) - .normal(top_normal.clone()), - Side::Top, - Corner::TopRight, - ) - .build() - .unwrap(), - ); - - vertices[VertexIndex::TopBackLeft as usize] = Some( - vertex_builder_cb( - VertexBuilder::new() - .pos(top_back_left_pos) - .normal(top_normal.clone()), - Side::Top, - Corner::TopLeft, - ) - .build() - .unwrap(), - ); - - vertices[VertexIndex::TopFrontLeft as usize] = Some( - vertex_builder_cb( - VertexBuilder::new() - .pos(top_front_left_pos) - .normal(top_normal.clone()), - Side::Top, - Corner::BottomLeft, - ) - .build() - .unwrap(), - ); - - vertices[VertexIndex::TopFrontRight as usize] = Some( - vertex_builder_cb( - VertexBuilder::new() - .pos(top_front_right_pos) - .normal(top_normal), - Side::Top, - Corner::BottomRight, - ) - .build() - .unwrap(), - ); - - let bottom_back_right_pos = Vec3 { x: 0.5, y: -0.5, z: 0.5 }; - let bottom_back_left_pos = Vec3 { x: -0.5, y: -0.5, z: 0.5 }; - let bottom_front_right_pos = Vec3 { x: 0.5, y: -0.5, z: -0.5 }; - let bottom_front_left_pos = Vec3 { x: -0.5, y: -0.5, z: -0.5 }; - - let bottom_normal = calc_triangle_surface_normal( - &bottom_back_right_pos, - &bottom_back_left_pos, - &bottom_front_right_pos, - ); - - vertices[VertexIndex::BottomBackRight as usize] = Some( - vertex_builder_cb( - VertexBuilder::new() - .pos(bottom_back_right_pos) - .normal(bottom_normal.clone()), - Side::Bottom, - Corner::BottomRight, - ) - .build() - .unwrap(), - ); - - vertices[VertexIndex::BottomBackLeft as usize] = Some( - vertex_builder_cb( - VertexBuilder::new() - .pos(bottom_back_left_pos) - .normal(bottom_normal.clone()), - Side::Bottom, - Corner::BottomLeft, - ) - .build() - .unwrap(), - ); - - vertices[VertexIndex::BottomFrontRight as usize] = Some( - vertex_builder_cb( - VertexBuilder::new() - .pos(bottom_front_right_pos) - .normal(bottom_normal.clone()), - Side::Bottom, - Corner::TopRight, - ) - .build() - .unwrap(), - ); - - vertices[VertexIndex::BottomFrontLeft as usize] = Some( - vertex_builder_cb( - VertexBuilder::new() - .pos(bottom_front_left_pos) - .normal(bottom_normal), - Side::Bottom, - Corner::TopLeft, - ) - .build() - .unwrap(), - ); - - let front = [ - // 🮝 - VertexIndex::FrontTopRight, - VertexIndex::FrontBottomRight, - VertexIndex::FrontTopLeft, - // - // 🮟 - VertexIndex::FrontBottomRight, - VertexIndex::FrontBottomLeft, - VertexIndex::FrontTopLeft, - ]; - - let back = [ - // 🮝 - VertexIndex::BackTopRight, - VertexIndex::BackBottomRight, - VertexIndex::BackTopLeft, - // - // 🮟 - VertexIndex::BackBottomRight, - VertexIndex::BackBottomLeft, - VertexIndex::BackTopLeft, - ]; - - let right = [ - // 🮝 - VertexIndex::RightBackTop, - VertexIndex::RightBackBottom, - VertexIndex::RightFrontTop, - // - // 🮟 - VertexIndex::RightBackBottom, - VertexIndex::RightFrontBottom, - VertexIndex::RightFrontTop, - ]; - - let left = [ - // 🮝 - VertexIndex::LeftBackTop, - VertexIndex::LeftBackBottom, - VertexIndex::LeftFrontTop, - // - // 🮟 - VertexIndex::LeftBackBottom, - VertexIndex::LeftFrontBottom, - VertexIndex::LeftFrontTop, - ]; - - let top = [ - // 🮝 - VertexIndex::TopBackRight, - VertexIndex::TopBackLeft, - VertexIndex::TopFrontRight, - // - // 🮟 - VertexIndex::TopBackLeft, - VertexIndex::TopFrontLeft, - VertexIndex::TopFrontRight, - ]; - - let bottom = [ - // 🮝 - VertexIndex::BottomBackRight, - VertexIndex::BottomBackLeft, - VertexIndex::BottomFrontRight, - // - // 🮟 - VertexIndex::BottomBackLeft, - VertexIndex::BottomFrontLeft, - VertexIndex::BottomFrontRight, - ]; - - let indices = [front, back, right, left, top, bottom]; - - Mesh::new( - vertices.map(Option::unwrap).to_vec(), - Some( - indices - .into_iter() - .flatten() - .map(|index| index as u32) - .collect(), - ), - ) -} - -macro_rules! one { - ($tt: tt) => { - 1 - }; -} - -macro_rules! enum_with_variant_cnt { - ( - $(#[$attr: meta])* - enum $name: ident { - $($variant: ident,)* - } - ) => { - $(#[$attr])* - enum $name { - $($variant,)* - } - - impl $name { - const VARIANT_CNT: usize = 0 $(+ one!($variant))*; - } - }; -} - -enum_with_variant_cnt! { -#[repr(u32)] -enum VertexIndex -{ - FrontTopRight, - FrontBottomRight, - FrontBottomLeft, - FrontTopLeft, - - BackTopRight, - BackBottomRight, - BackBottomLeft, - BackTopLeft, - - RightBackTop, - RightBackBottom, - RightFrontTop, - RightFrontBottom, - - LeftBackTop, - LeftBackBottom, - LeftFrontTop, - LeftFrontBottom, - - TopBackRight, - TopBackLeft, - TopFrontRight, - TopFrontLeft, - - BottomBackRight, - BottomBackLeft, - BottomFrontRight, - BottomFrontLeft, -} -} diff --git a/src/main.rs b/src/main.rs index 358b15b..8346cbf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use std::error::Error; +use std::fs::read_to_string; use std::path::Path; use engine::camera::fly::{Extension as FlyCameraExtension, Options as FlyCameraOptions}; @@ -7,25 +8,29 @@ use engine::color::Color; use engine::data_types::dimens::Dimens; use engine::ecs::sole::Single; use engine::event::Start as StartEvent; +use engine::file_format::wavefront::mtl::parse as parse_mtl; +use engine::file_format::wavefront::obj::parse as parse_obj; use engine::input::Extension as InputExtension; use engine::lighting::{GlobalLight, LightSource}; use engine::material::Builder as MaterialBuilder; use engine::renderer::Extension as RendererExtension; use engine::shader::Program as ShaderProgram; -use engine::texture::Texture; use engine::transform::Transform; -use engine::vector::{Vec2, Vec3}; -use engine::vertex::Builder as VertexBuilder; +use engine::vector::Vec3; use engine::window::{CursorMode, Extension as WindowExtension, Window}; use engine::Engine; use tracing::Level; use tracing_subscriber::FmtSubscriber; -use crate::cube::{create_cube_mesh, Corner as CubeCorner, Side as CubeSide}; +const WINDOW_SIZE: Dimens<u32> = Dimens { width: 1920, height: 1080 }; -mod cube; +const YELLOW: Color<f32> = Color { + red: 0.988235294118, + green: 0.941176470588, + blue: 0.0274509803922, +}; -const WINDOW_SIZE: Dimens<u32> = Dimens { width: 1920, height: 1080 }; +const RESOURCE_DIR: &str = "res"; fn main() -> Result<(), Box<dyn Error>> { @@ -42,45 +47,40 @@ fn main() -> Result<(), Box<dyn Error>> ..Default::default() }; - let texture = Texture::open(Path::new("vent.png"))?; + let teapot_obj = + parse_obj(&read_to_string(Path::new(RESOURCE_DIR).join("teapot.obj"))?)?; + + let teapot_mat_name = teapot_obj + .faces + .first() + .and_then(|face| face.material_name.as_ref()); + + let teapot_mats = teapot_obj.read_and_parse_material_libs(parse_mtl)?; - let texture_id = texture.id(); + let teapot_mat = teapot_mats + .into_iter() + .find(|mat| Some(&mat.name) == teapot_mat_name) + .ok_or("Teapot material was not found")?; engine.spawn(( - create_cube_mesh(cube_vertex_builder_cb), - MaterialBuilder::new() - .textures([texture]) - .diffuse(Color::WHITE_F32) - .ambient(Color::WHITE_F32) - .specular(Color::WHITE_F32) - .ambient_map(texture_id) - .diffuse_map(texture_id) - .specular_map(texture_id) - .build(), + teapot_obj.to_mesh()?, + teapot_mat.material, transform, ShaderProgram::new()?, )); - let light_source_texture = Texture::open(Path::new("yellow.jpg"))?; - - let light_source_texture_id = light_source_texture.id(); - engine.spawn(( - LightSource::default(), + LightSource { + diffuse: YELLOW, + ..Default::default() + }, Transform { - position: Vec3 { x: -2.0, y: 3.0, z: 3.0 }, + position: Vec3 { x: -6.0, y: 3.0, z: 3.0 }, ..Default::default() }, - create_cube_mesh(cube_vertex_builder_cb), - MaterialBuilder::new() - .textures([light_source_texture]) - .diffuse(Color::WHITE_F32) - .ambient(Color::WHITE_F32 * 3.0) - .specular(Color::WHITE_F32) - .ambient_map(light_source_texture_id) - .diffuse_map(light_source_texture_id) - .specular_map(light_source_texture_id) - .build(), + parse_obj(&read_to_string(Path::new(RESOURCE_DIR).join("cube.obj"))?)? + .to_mesh()?, + MaterialBuilder::new().ambient(YELLOW * 5.0).build(), ShaderProgram::new()?, )); @@ -118,17 +118,3 @@ fn prepare_window(window: Single<Window>) { window.set_cursor_mode(CursorMode::Disabled).unwrap(); } - -fn cube_vertex_builder_cb( - vertex_builder: VertexBuilder, - _side: CubeSide, - corner: CubeCorner, -) -> VertexBuilder -{ - vertex_builder.texture_coords(match corner { - CubeCorner::TopRight => Vec2 { x: 1.0, y: 1.0 }, - CubeCorner::TopLeft => Vec2 { x: 0.0, y: 1.0 }, - CubeCorner::BottomRight => Vec2 { x: 1.0, y: 0.0 }, - CubeCorner::BottomLeft => Vec2 { x: 0.0, y: 0.0 }, - }) -} |