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/main.rs | |
parent | 9ce344220ec7d03ec2902907aa41f126faf84df0 (diff) |
chore: use obj models
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 82 |
1 files changed, 34 insertions, 48 deletions
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 }, - }) -} |