this is jank, but a good start

master
mitchellhansen 4 years ago
parent c2b62a5f53
commit 26f2cf4836

@ -8,3 +8,12 @@ name = "ball.1"
type = "PhysicsEntity" type = "PhysicsEntity"
mesh = "ball.obj" mesh = "ball.obj"
[[entities]]
name = "camera.1"
type = "Camera"
[[entities]]
name = "light.1"
type = "Light"
mesh = "light.obj"

@ -184,7 +184,6 @@ fn main() {
// The renderer // The renderer
let mut renderer = render::state::RenderState::init(&window, &mut imgui_context); let mut renderer = render::state::RenderState::init(&window, &mut imgui_context);
entity_loading(&mut world, &mut renderer);
resources.insert(renderer); resources.insert(renderer);
resources.insert(Arc::new(Mutex::new(imgui_context))); resources.insert(Arc::new(Mutex::new(imgui_context)));
@ -360,55 +359,3 @@ pub fn setup_gamepad(event_loop: &EventLoop<OwnedEventExtension>) {
}); });
} }
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(),
));
}

@ -4,7 +4,7 @@ use std::path::PathBuf;
use std::time::Instant; use std::time::Instant;
use cgmath::{Euler, Quaternion}; use cgmath::{Euler, Quaternion};
use config::Config; use config::{Config, Value};
use config::File; use config::File;
use legion::world::SubWorld; use legion::world::SubWorld;
use legion::IntoQuery; use legion::IntoQuery;
@ -21,7 +21,8 @@ use crate::geometry::{load_obj, RawMesh};
pub struct EntityMeta { pub struct EntityMeta {
pub name: String, pub name: String,
pub ent_type: String, pub ent_type: String,
pub mesh: String, pub mesh: Option<String>,
pub position: Option<Position>
} }
pub struct RuntimeState { pub struct RuntimeState {
@ -51,10 +52,22 @@ impl RuntimeState {
let mut out = Vec::new(); let mut out = Vec::new();
for entity in self.config_db.get_array("entities").unwrap() { for entity in self.config_db.get_array("entities").unwrap() {
let table = entity.into_table().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 { out.push(EntityMeta {
name: table.get("name").unwrap().kind.to_string(), name: table.get("name").unwrap().kind.to_string(),
ent_type: table.get("type").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 out

@ -1,7 +1,7 @@
use std::path::PathBuf; use std::path::PathBuf;
use std::time::Instant; use std::time::Instant;
use cgmath::{Euler, Quaternion, Deg}; use cgmath::{Euler, Quaternion, Deg, Rad, Point3};
use imgui::FontSource; use imgui::FontSource;
use legion::*; use legion::*;
use legion::IntoQuery; use legion::IntoQuery;
@ -19,6 +19,7 @@ use crate::geometry::RawMesh;
use crate::physics::state::PhysicsState; use crate::physics::state::PhysicsState;
use crate::render::state::RenderState; use crate::render::state::RenderState;
use crate::runtime::state::RuntimeState; use crate::runtime::state::RuntimeState;
use std::f32::consts::PI;
#[system] #[system]
#[write_component(Mesh)] #[write_component(Mesh)]
@ -40,20 +41,23 @@ pub fn runtime_spawn(
for entity in runtime_state.get_configured_entities(){ for entity in runtime_state.get_configured_entities(){
match entity.ent_type.as_ref() { match entity.ent_type.as_ref() {
"Terrain" => { "PhysicsEntity" => {
let raw_mesh = match runtime_state.get_mesh(entity.mesh.as_str()) { let mesh_name = entity.mesh.unwrap();
let raw_mesh = match runtime_state.get_mesh(mesh_name.as_str()) {
None => { None => {
log::warn!("Skipping entity with invalid mesh file {:?} ", entity.mesh); log::warn!("Skipping entity with invalid mesh file {:?} ", mesh_name);
continue; continue;
} }
Some(mesh) => mesh Some(mesh) => mesh
}; };
let mut static_body = RigidBodyBuilder::new_static() let mut dynamic_body = RigidBodyBuilder::new_dynamic()
.position(Isometry3::new(Vector3::new(0.0, -8.0, 0.0), Vector::y())) .can_sleep(false)
.mass(1.0)
.translation(0.0, 0.0, 0.0)
.build(); .build();
let mesh_collider = ColliderBuilder::trimesh( let collider = ColliderBuilder::trimesh(
raw_mesh.vertices raw_mesh.vertices
.iter() .iter()
.map(|v| v.position()) .map(|v| v.position())
@ -71,46 +75,43 @@ pub fn runtime_spawn(
}) })
).unwrap(); ).unwrap();
let entity: Entity = cmd.push(( let entity: Entity = cmd.push((
Position { Position {
x: 0.0, x: 0.0,
y: -8.0, y: 20.0,
z: 0.0, z: 0.0,
rot: Euler { rot: Euler {
x: Deg(0.0), x: Deg(25.0),
y: Deg(0.0), y: Deg(45.0),
z: Deg(0.0), z: Deg(15.0),
}, },
}, },
gpu_mesh_buffer, gpu_mesh_buffer,
Physics { Physics {
rigid_body: static_body, rigid_body: dynamic_body,
rigid_body_handle: None, rigid_body_handle: None,
}, },
Collider { Collider {
collider: mesh_collider, collider: collider,
collider_handle: None, collider_handle: None,
}, },
)); ));
}, },
"PhysicsEntity" => { "Terrain" => {
let mesh_name = entity.mesh.unwrap();
let raw_mesh = match runtime_state.get_mesh(entity.mesh.as_str()) { let raw_mesh = match runtime_state.get_mesh(mesh_name.as_str()) {
None => { None => {
log::warn!("Skipping entity with invalid mesh file {:?} ", entity.mesh); log::warn!("Skipping entity with invalid mesh file {:?} ", mesh_name);
continue; continue;
} }
Some(mesh) => mesh Some(mesh) => mesh
}; };
let mut dynamic_body = RigidBodyBuilder::new_dynamic() let mut static_body = RigidBodyBuilder::new_static()
.can_sleep(false) .position(Isometry3::new(Vector3::new(0.0, -8.0, 0.0), Vector::y()))
.mass(1.0)
.translation(0.0, 35.0, 0.0)
.build(); .build();
let collider = ColliderBuilder::trimesh( let mesh_collider = ColliderBuilder::trimesh(
raw_mesh.vertices raw_mesh.vertices
.iter() .iter()
.map(|v| v.position()) .map(|v| v.position())
@ -128,28 +129,81 @@ pub fn runtime_spawn(
}) })
).unwrap(); ).unwrap();
let entity: Entity = cmd.push(( let entity: Entity = cmd.push((
Position { Position {
x: 0.0, x: 0.0,
y: 20.0, y: -8.0,
z: 0.0, z: 0.0,
rot: Euler { rot: Euler {
x: Deg(25.0), x: Deg(0.0),
y: Deg(45.0), y: Deg(0.0),
z: Deg(15.0), z: Deg(0.0),
}, },
}, },
gpu_mesh_buffer, gpu_mesh_buffer,
Physics { Physics {
rigid_body: dynamic_body, rigid_body: static_body,
rigid_body_handle: None, rigid_body_handle: None,
}, },
Collider { Collider {
collider: collider, collider: mesh_collider,
collider_handle: None, 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(),
));
} }
_ => {}, _ => {},
} }

Loading…
Cancel
Save