From 26f2cf4836640702bf831795684e6f03e9c5ebc7 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Sat, 20 Feb 2021 02:08:48 -0800 Subject: [PATCH] this is jank, but a good start --- conf/entity_spawns.toml | 9 ++++ src/main.rs | 53 ------------------- src/runtime/state.rs | 19 +++++-- src/runtime/system.rs | 112 +++++++++++++++++++++++++++++----------- 4 files changed, 108 insertions(+), 85 deletions(-) diff --git a/conf/entity_spawns.toml b/conf/entity_spawns.toml index 4ddbb59..01cb829 100644 --- a/conf/entity_spawns.toml +++ b/conf/entity_spawns.toml @@ -8,3 +8,12 @@ name = "ball.1" type = "PhysicsEntity" mesh = "ball.obj" +[[entities]] +name = "camera.1" +type = "Camera" + + +[[entities]] +name = "light.1" +type = "Light" +mesh = "light.obj" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 8f3d5d3..4976e98 100644 --- a/src/main.rs +++ b/src/main.rs @@ -184,7 +184,6 @@ fn main() { // The renderer let mut renderer = render::state::RenderState::init(&window, &mut imgui_context); - entity_loading(&mut world, &mut renderer); resources.insert(renderer); resources.insert(Arc::new(Mutex::new(imgui_context))); @@ -360,55 +359,3 @@ pub fn setup_gamepad(event_loop: &EventLoop) { }); } - - -pub fn entity_loading(world: &mut World, renderer: &mut render::state::RenderState) { - let monkey_mesh = - renderer.load_mesh_to_buffer("./resources/monkey.obj", Some(wgpu::Color::GREEN)).unwrap(); - let light_mesh = - renderer.load_mesh_to_buffer("./resources/light.obj", Some(wgpu::Color::BLACK)).unwrap(); - let ball_mesh = - renderer.load_mesh_to_buffer("./resources/ball.obj", Some(wgpu::Color::BLUE)).unwrap(); - - //load_colliding_mesh_entity(world, renderer, "./resources/test-textured.obj"); - - let camera_ent: Entity = world.push(( - Camera { - position: Point3 { - x: 0.0, - y: 0.0, - z: 10.0, - }, - yaw: Rad(-PI), - pitch: Rad(PI / 2.0), - }, - CameraController::new(3.0, 1.0), - )); - - let light_entity: Entity = world.push(( - Position { - x: 0.0, - y: 0.0, - z: 0.0, - rot: Euler { - x: Deg(0.0), - y: Deg(-25.0), - z: Deg(0.0), - }, - }, - light_mesh.clone(), - renderer.create_light(), - )); - - let light_entity: Entity = world.push(( - cgmath::Point3 { - x: -5.0 as f32, - y: 7.0 as f32, - z: 10.0 as f32, - }, - light_mesh, - renderer.create_light(), - )); - - -} diff --git a/src/runtime/state.rs b/src/runtime/state.rs index 3437c00..734c4ed 100644 --- a/src/runtime/state.rs +++ b/src/runtime/state.rs @@ -4,7 +4,7 @@ use std::path::PathBuf; use std::time::Instant; use cgmath::{Euler, Quaternion}; -use config::Config; +use config::{Config, Value}; use config::File; use legion::world::SubWorld; use legion::IntoQuery; @@ -21,7 +21,8 @@ use crate::geometry::{load_obj, RawMesh}; pub struct EntityMeta { pub name: String, pub ent_type: String, - pub mesh: String, + pub mesh: Option, + pub position: Option } pub struct RuntimeState { @@ -51,10 +52,22 @@ impl RuntimeState { let mut out = Vec::new(); for entity in self.config_db.get_array("entities").unwrap() { let table = entity.into_table().unwrap(); + + let mesh = match table.get("mesh") { + None => { None } + Some(v) => { Some(v.kind.to_string())} + }; + + // let position = match table.get("position") { + // None => { None } + // Some(v) => { Some(v.kind.to_string())} + // }; + out.push(EntityMeta { name: table.get("name").unwrap().kind.to_string(), ent_type: table.get("type").unwrap().kind.to_string(), - mesh: table.get("mesh").unwrap().kind.to_string(), + mesh: mesh, + position: None }); } out diff --git a/src/runtime/system.rs b/src/runtime/system.rs index 93d1b95..4831320 100644 --- a/src/runtime/system.rs +++ b/src/runtime/system.rs @@ -1,7 +1,7 @@ use std::path::PathBuf; use std::time::Instant; -use cgmath::{Euler, Quaternion, Deg}; +use cgmath::{Euler, Quaternion, Deg, Rad, Point3}; use imgui::FontSource; use legion::*; use legion::IntoQuery; @@ -19,6 +19,7 @@ use crate::geometry::RawMesh; use crate::physics::state::PhysicsState; use crate::render::state::RenderState; use crate::runtime::state::RuntimeState; +use std::f32::consts::PI; #[system] #[write_component(Mesh)] @@ -40,20 +41,23 @@ pub fn runtime_spawn( for entity in runtime_state.get_configured_entities(){ match entity.ent_type.as_ref() { - "Terrain" => { - let raw_mesh = match runtime_state.get_mesh(entity.mesh.as_str()) { + "PhysicsEntity" => { + let mesh_name = entity.mesh.unwrap(); + let raw_mesh = match runtime_state.get_mesh(mesh_name.as_str()) { None => { - log::warn!("Skipping entity with invalid mesh file {:?} ", entity.mesh); + log::warn!("Skipping entity with invalid mesh file {:?} ", mesh_name); continue; } Some(mesh) => mesh }; - let mut static_body = RigidBodyBuilder::new_static() - .position(Isometry3::new(Vector3::new(0.0, -8.0, 0.0), Vector::y())) + let mut dynamic_body = RigidBodyBuilder::new_dynamic() + .can_sleep(false) + .mass(1.0) + .translation(0.0, 0.0, 0.0) .build(); - let mesh_collider = ColliderBuilder::trimesh( + let collider = ColliderBuilder::trimesh( raw_mesh.vertices .iter() .map(|v| v.position()) @@ -71,46 +75,43 @@ pub fn runtime_spawn( }) ).unwrap(); - let entity: Entity = cmd.push(( Position { x: 0.0, - y: -8.0, + y: 20.0, z: 0.0, rot: Euler { - x: Deg(0.0), - y: Deg(0.0), - z: Deg(0.0), + x: Deg(25.0), + y: Deg(45.0), + z: Deg(15.0), }, }, gpu_mesh_buffer, Physics { - rigid_body: static_body, + rigid_body: dynamic_body, rigid_body_handle: None, }, Collider { - collider: mesh_collider, + collider: collider, collider_handle: None, }, )); }, - "PhysicsEntity" => { - - let raw_mesh = match runtime_state.get_mesh(entity.mesh.as_str()) { + "Terrain" => { + let mesh_name = entity.mesh.unwrap(); + let raw_mesh = match runtime_state.get_mesh(mesh_name.as_str()) { None => { - log::warn!("Skipping entity with invalid mesh file {:?} ", entity.mesh); + log::warn!("Skipping entity with invalid mesh file {:?} ", mesh_name); continue; } Some(mesh) => mesh }; - let mut dynamic_body = RigidBodyBuilder::new_dynamic() - .can_sleep(false) - .mass(1.0) - .translation(0.0, 35.0, 0.0) + let mut static_body = RigidBodyBuilder::new_static() + .position(Isometry3::new(Vector3::new(0.0, -8.0, 0.0), Vector::y())) .build(); - let collider = ColliderBuilder::trimesh( + let mesh_collider = ColliderBuilder::trimesh( raw_mesh.vertices .iter() .map(|v| v.position()) @@ -128,28 +129,81 @@ pub fn runtime_spawn( }) ).unwrap(); + let entity: Entity = cmd.push(( Position { x: 0.0, - y: 20.0, + y: -8.0, z: 0.0, rot: Euler { - x: Deg(25.0), - y: Deg(45.0), - z: Deg(15.0), + x: Deg(0.0), + y: Deg(0.0), + z: Deg(0.0), }, }, gpu_mesh_buffer, Physics { - rigid_body: dynamic_body, + rigid_body: static_body, rigid_body_handle: None, }, Collider { - collider: collider, + collider: mesh_collider, collider_handle: None, }, )); + }, + "Camera" => { + + let entity: Entity = cmd.push(( + Camera { + position: cgmath::Point3 { + x: 0.0, + y: 0.0, + z: 10.0, + }, + yaw: Rad(-PI), + pitch: Rad(PI / 2.0), + }, + CameraController::new(3.0, 1.0), + )); + }, + "Light" => { + let mesh_name = entity.mesh.unwrap(); + let raw_mesh = match runtime_state.get_mesh(mesh_name.as_str()) { + None => { + log::warn!("Skipping entity with invalid mesh file {:?} ", mesh_name); + continue; + } + Some(mesh) => mesh + }; + + let gpu_mesh_buffer = renderer.upload_mesh_to_buffer( + raw_mesh, + Some(wgpu::Color { + r: 1.0, + g: 0.7, + b: 0.3, + a: 1.0, + }) + ).unwrap(); + + let light_entity: Entity = cmd.push(( + Position { + x: 0.0, + y: 0.0, + z: 0.0, + rot: Euler { + x: Deg(0.0), + y: Deg(-25.0), + z: Deg(0.0), + }, + }, + gpu_mesh_buffer, + renderer.create_light(), + )); } + + _ => {}, }