use std::error::Error; use std::fs::read_to_string; use std::path::Path; use engine::camera::fly::{Extension as FlyCameraExtension, Options as FlyCameraOptions}; use engine::camera::Camera; 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::{AttenuationParams, GlobalLight, PointLight}; use engine::material::{Builder as MaterialBuilder, Flags as MaterialFlags}; use engine::renderer::Extension as RendererExtension; use engine::shader::Program as ShaderProgram; use engine::transform::Transform; use engine::vector::Vec3; use engine::window::{CursorMode, Extension as WindowExtension, Window}; use engine::Engine; use tracing::Level; use tracing_subscriber::FmtSubscriber; const WINDOW_SIZE: Dimens = Dimens { width: 1920, height: 1080 }; const YELLOW: Color = Color { red: 0.988235294118, green: 0.941176470588, blue: 0.0274509803922, }; const RESOURCE_DIR: &str = "res"; fn main() -> Result<(), Box> { let subscriber = FmtSubscriber::builder() .with_max_level(Level::TRACE) .finish(); tracing::subscriber::set_global_default(subscriber)?; let mut engine = Engine::new(); let transform = Transform { position: Vec3 { x: 1.6, y: 0.0, z: 0.0 }, ..Default::default() }; 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 teapot_mat = teapot_mats .into_iter() .find(|mat| Some(&mat.name) == teapot_mat_name) .ok_or("Teapot material was not found")?; engine.spawn(( teapot_obj.to_mesh()?, teapot_mat.material, transform, ShaderProgram::new()?, )); engine.spawn(( PointLight::builder() .diffuse(YELLOW) .attenuation_params(AttenuationParams { linear: 0.045, quadratic: 0.0075, ..Default::default() }) .build(), Transform { position: Vec3 { x: -6.0, y: 3.0, z: 3.0 }, ..Default::default() }, parse_obj(&read_to_string(Path::new(RESOURCE_DIR).join("cube.obj"))?)? .to_mesh()?, MaterialBuilder::new().ambient(YELLOW * 5.0).build(), ShaderProgram::new()?, MaterialFlags::builder().use_ambient_color(true).build(), )); engine.spawn((Camera { current: true, ..Default::default() },)); engine.add_sole(GlobalLight::default())?; engine.register_system(StartEvent, prepare_window); engine.add_extension(RendererExtension::default()); engine.add_extension( WindowExtension::default() .window_title("Game") .window_size(WINDOW_SIZE), ); engine.add_extension(FlyCameraExtension(FlyCameraOptions { camera_speed: 3.0, mouse_sensitivity: 0.2, })); engine.add_extension(InputExtension::default()); engine.start(); Ok(()) } fn prepare_window(window: Single) { window.set_cursor_mode(CursorMode::Disabled).unwrap(); }