lots of skeleton stuff. lots of hacking. mostly hacking

This commit is contained in:
2021-02-12 00:27:11 -08:00
parent 9f4c8a856c
commit 189805dd13
8 changed files with 687 additions and 155 deletions

View File

@@ -8,13 +8,12 @@ use std::sync::Arc;
use std::time::{Duration, Instant};
use bytemuck::__core::ops::Range;
use cgmath::{Decomposed, Deg, Euler, InnerSpace, Quaternion, Rotation3, SquareMatrix};
use cgmath::{Decomposed, Deg, Euler, InnerSpace, Quaternion, Rotation3, SquareMatrix, Point3, Rad};
use futures::task::LocalSpawn;
use legion::*;
use rapier3d::dynamics::{
IntegrationParameters, JointSet, RigidBody, RigidBodyBuilder, RigidBodyHandle, RigidBodySet,
};
use rapier3d::geometry::Collider as r3dCollider;
use rapier3d::geometry::{BroadPhase, ColliderBuilder, ColliderHandle, ColliderSet, NarrowPhase};
use rapier3d::math;
use rapier3d::na::{Isometry, Isometry3, Vector, Vector3};
@@ -28,13 +27,23 @@ use winit::{
event_loop::{ControlFlow, EventLoop},
};
use gilrs::Event as GilEvent;
use crate::camera::{CameraController, Camera};
use crate::components::{Collider, Color, Physics, Position};
use crate::physics::PhysicsState;
use crate::render::Renderer;
use crate::owned_event::TrEventExtension;
use gilrs::{Gamepad, Gilrs};
use rapier3d::counters::Timer;
mod camera;
mod components;
mod geometry;
mod light;
mod physics;
mod render;
mod owned_event;
/*
@@ -58,81 +67,13 @@ mvp:
ECS
animation
render 3d
render 3d (good!)
input/io
collision / physics
entities & behaviours
collision / physics (yep!)
entities & behaviours (got the entities!)
*/
#[cfg_attr(rustfmt, rustfmt_skip)]
#[allow(unused)]
pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.0, 0.0, 0.5, 1.0,
);
// a component is any type that is 'static, sized, send and sync
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Position {
x: f32,
y: f32,
z: f32,
rot: cgmath::Quaternion<f32>,
//mx: cgmath::Matrix4<f32>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Color {
r: f32,
g: f32,
b: f32,
a: f32,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Velocity {
dx: f32,
dy: f32,
rs: f32,
}
#[derive(Clone, Default, PartialEq, Eq, Hash, Copy, Debug)]
pub struct RangeCopy<Idx> {
pub start: Idx,
pub end: Idx,
}
#[derive(Clone, Debug)]
pub struct DirectionalLight {
color: wgpu::Color,
fov: f32,
depth: RangeCopy<f32>,
target_view: Arc<TextureView>,
}
#[derive(Clone, Debug)]
pub struct Mesh {
index_buffer: Arc<Buffer>,
index_count: usize,
vertex_buffer: Arc<Buffer>,
uniform_buffer: Arc<Buffer>,
bind_group: Arc<BindGroup>,
}
#[derive(Clone, Debug)]
pub struct Physics {
rigid_body: RigidBody,
rigid_body_handle: Option<RigidBodyHandle>,
}
#[derive(Clone)]
pub struct Collider {
collider: r3dCollider,
collider_handle: Option<ColliderHandle>,
}
//log::info!("");
fn main() {
@@ -151,12 +92,13 @@ fn main() {
// TODO schedule for the update system and others
let mut update_schedule = Schedule::builder()
.add_system(physics::update_camera_system())
.add_system(physics::run_physics_system())
.add_system(physics::update_models_system())
// next system here, gamelogic update system?
.build();
let event_loop = EventLoop::new();
let event_loop = EventLoop::<TrEventExtension>::with_user_event();
let mut builder = winit::window::WindowBuilder::new();
builder = builder.with_title("MVGE");
@@ -169,8 +111,6 @@ fn main() {
let window = builder.build(&event_loop).unwrap();
let mut last_update_inst = Instant::now();
// Load up the renderer (and the resources)
let mut renderer = {
let mut renderer = render::Renderer::init(&window);
@@ -186,20 +126,94 @@ fn main() {
resources.insert(physics_state);
resources.insert(physics_pipeline);
resources.insert(CameraController::new(1.0, 1.0));
resources.insert(Instant::now());
let event_loop_proxy = event_loop.create_proxy();
std::thread::spawn(move || {
let mut gilrs = Gilrs::new().unwrap();
// Iterate over all connected gamepads
let mut gamepad: Option<Gamepad> = None;
for (_id, gamepad_) in gilrs.gamepads() {
if gamepad_.name() == "PS4" {
gamepad = Some(gamepad_);
}
println!("{} is {:?} {:?}", gamepad_.name(), gamepad_.power_info(), gamepad_.id());
}
let mut active_gamepad = None;
loop {
while let Some(GilEvent { id, event, time }) = gilrs.next_event() {
println!("{:?} New event from {}: {:?}", time, id, event);
active_gamepad = Some(id);
event_loop_proxy.send_event(TrEventExtension::GamepadEvent {
gil_event: GilEvent { id, event, time }
}).ok();
}
// // You can also use cached gamepad state
// if let Some(gamepad) = active_gamepad.map(|id| gilrs.gamepad(id)) {
// if gamepad.is_pressed(Button::South) {
// println!("Button South is pressed (XBox - A, PS - X)");
// }
// }
std::thread::sleep(std::time::Duration::from_millis(50));
}
});
let step_size: f32 = 0.005;
let mut elapsed_time: f32 = { // deltatime since last frame
let last_frame = resources.get::<Instant>().unwrap();
last_frame.elapsed()
}.as_secs_f32();
let mut delta_time: f32 = 0.0;
let mut accumulator_time: f32 = 0.0;
let mut current_time: f32 = elapsed_time;
event_loop.run(move |event, _, control_flow| {
// Artificially slows the loop rate to 10 millis
// This is called after redraw events cleared
*control_flow = ControlFlow::WaitUntil(Instant::now() + Duration::from_millis(10));
//*control_flow = ControlFlow::WaitUntil(Instant::now() + Duration::from_millis(10));
*control_flow = ControlFlow::Poll;
match event {
event::Event::MainEventsCleared => {
// ask for a redraw every 20 millis
if last_update_inst.elapsed() > Duration::from_millis(20) {
window.request_redraw();
last_update_inst = Instant::now();
elapsed_time = { // deltatime since last frame
let last_frame = resources.get::<Instant>().unwrap();
last_frame.elapsed()
}.as_secs_f32();
delta_time = elapsed_time - current_time;
current_time = elapsed_time;
if delta_time > 0.02 {
delta_time = 0.02;
}
accumulator_time += delta_time;
let dt = { // deltatime since last frame
let last_frame = resources.get::<Instant>().unwrap();
last_frame.elapsed()
};
update_schedule.execute(&mut world, &mut resources);
pool.run_until_stalled();
// ask for a redraw every 20 millis
if dt > Duration::from_millis(20) {
}
// update the world time here for next update
resources.insert(Instant::now());
render_schedule.execute(&mut world, &mut resources);
}
event::Event::DeviceEvent {
event: MouseMotion { delta },
@@ -257,7 +271,20 @@ fn main() {
pub fn entity_loading(world: &mut World, renderer: &mut Renderer) {
let monkey_mesh = renderer.load_mesh_to_buffer("./resources/monkey.obj");
// This could be used for relationships between entities...???
// let camera_ent: Entity = world.push((
// Camera {
// position: Point3 {
// x: 5.0,
// y: 5.0,
// z: 5.0
// },
// yaw: Rad(45.0),
// pitch: Rad(45.0)
// }
// ));
let light_entity: Entity = world.push((
cgmath::Point3 {
x: 7.0 as f32,