|
|
@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
extern crate imgui;
|
|
|
|
|
|
|
|
extern crate imgui_wgpu;
|
|
|
|
|
|
|
|
extern crate imgui_winit_support;
|
|
|
|
extern crate tobj;
|
|
|
|
extern crate tobj;
|
|
|
|
extern crate winit;
|
|
|
|
extern crate winit;
|
|
|
|
|
|
|
|
|
|
|
@ -8,9 +11,17 @@ use std::sync::Arc;
|
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
|
|
|
|
|
|
|
|
use bytemuck::__core::ops::Range;
|
|
|
|
use bytemuck::__core::ops::Range;
|
|
|
|
use cgmath::{Decomposed, Deg, Euler, InnerSpace, Quaternion, Rotation3, SquareMatrix, Point3, Rad};
|
|
|
|
use cgmath::{
|
|
|
|
|
|
|
|
Decomposed, Deg, Euler, InnerSpace, Point3, Quaternion, Rad, Rotation3, SquareMatrix,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
use futures::executor::block_on;
|
|
|
|
use futures::task::LocalSpawn;
|
|
|
|
use futures::task::LocalSpawn;
|
|
|
|
|
|
|
|
use gilrs::{Gamepad, Gilrs};
|
|
|
|
|
|
|
|
use gilrs::Event as GilEvent;
|
|
|
|
|
|
|
|
use imgui::*;
|
|
|
|
|
|
|
|
use imgui_wgpu::{Renderer as ImguiRenderer, RendererConfig as ImguiRendererConfig};
|
|
|
|
use legion::*;
|
|
|
|
use legion::*;
|
|
|
|
|
|
|
|
use rapier3d::counters::Timer;
|
|
|
|
use rapier3d::dynamics::{
|
|
|
|
use rapier3d::dynamics::{
|
|
|
|
IntegrationParameters, JointSet, RigidBody, RigidBodyBuilder, RigidBodyHandle, RigidBodySet,
|
|
|
|
IntegrationParameters, JointSet, RigidBody, RigidBodyBuilder, RigidBodyHandle, RigidBodySet,
|
|
|
|
};
|
|
|
|
};
|
|
|
@ -20,30 +31,27 @@ use rapier3d::na::{Isometry, Isometry3, Vector, Vector3};
|
|
|
|
use rapier3d::pipeline::PhysicsPipeline;
|
|
|
|
use rapier3d::pipeline::PhysicsPipeline;
|
|
|
|
use wgpu::{BindGroup, Buffer, TextureView};
|
|
|
|
use wgpu::{BindGroup, Buffer, TextureView};
|
|
|
|
use wgpu_subscriber;
|
|
|
|
use wgpu_subscriber;
|
|
|
|
use winit::event::DeviceEvent::MouseMotion;
|
|
|
|
|
|
|
|
use winit::platform::unix::x11::ffi::Time;
|
|
|
|
|
|
|
|
use winit::{
|
|
|
|
use winit::{
|
|
|
|
event::{self, WindowEvent},
|
|
|
|
event::{self, WindowEvent},
|
|
|
|
event_loop::{ControlFlow, EventLoop},
|
|
|
|
event_loop::{ControlFlow, EventLoop},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
use winit::event::DeviceEvent::MouseMotion;
|
|
|
|
|
|
|
|
use winit::platform::unix::x11::ffi::Time;
|
|
|
|
|
|
|
|
use winit::window::Window;
|
|
|
|
|
|
|
|
|
|
|
|
use gilrs::Event as GilEvent;
|
|
|
|
use crate::camera::{Camera, CameraController};
|
|
|
|
|
|
|
|
use crate::components::{Collider, Color, LoopState, Physics, Position};
|
|
|
|
use crate::camera::{CameraController, Camera};
|
|
|
|
use crate::owned_event::{OwnedEvent, OwnedEventExtension};
|
|
|
|
use crate::components::{Collider, Color, Physics, Position, LoopState};
|
|
|
|
|
|
|
|
use crate::physics::PhysicsState;
|
|
|
|
use crate::physics::PhysicsState;
|
|
|
|
use crate::render::Renderer;
|
|
|
|
use crate::render::Renderer;
|
|
|
|
use crate::owned_event::{OwnedEventExtension, OwnedEvent};
|
|
|
|
|
|
|
|
use gilrs::{Gamepad, Gilrs};
|
|
|
|
|
|
|
|
use rapier3d::counters::Timer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mod camera;
|
|
|
|
mod camera;
|
|
|
|
mod components;
|
|
|
|
mod components;
|
|
|
|
mod geometry;
|
|
|
|
mod geometry;
|
|
|
|
mod light;
|
|
|
|
mod light;
|
|
|
|
|
|
|
|
mod owned_event;
|
|
|
|
mod physics;
|
|
|
|
mod physics;
|
|
|
|
mod render;
|
|
|
|
mod render;
|
|
|
|
mod owned_event;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
|
@ -104,39 +112,37 @@ fn main() {
|
|
|
|
let mut builder = winit::window::WindowBuilder::new();
|
|
|
|
let mut builder = winit::window::WindowBuilder::new();
|
|
|
|
builder = builder.with_title("MVGE");
|
|
|
|
builder = builder.with_title("MVGE");
|
|
|
|
|
|
|
|
|
|
|
|
// I don't know what they are doing here
|
|
|
|
|
|
|
|
#[cfg(windows_OFF)] // TODO
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
use winit::platform::windows::WindowBuilderExtWindows;
|
|
|
|
|
|
|
|
builder = builder.with_no_redirection_bitmap(true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let window = builder.build(&event_loop).unwrap();
|
|
|
|
let window = builder.build(&event_loop).unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
// Load up the renderer (and the resources)
|
|
|
|
|
|
|
|
let mut renderer = {
|
|
|
|
|
|
|
|
let mut renderer = render::Renderer::init(&window);
|
|
|
|
|
|
|
|
entity_loading(&mut world, &mut renderer);
|
|
|
|
|
|
|
|
renderer
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut resources = Resources::default();
|
|
|
|
let mut resources = Resources::default();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Load up all the resources
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// The renderer
|
|
|
|
|
|
|
|
let mut renderer = render::Renderer::init(&window, &mut imgui_context);
|
|
|
|
|
|
|
|
entity_loading(&mut world, &mut renderer);
|
|
|
|
resources.insert(renderer);
|
|
|
|
resources.insert(renderer);
|
|
|
|
|
|
|
|
resources.insert(window);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Physics
|
|
|
|
let (physics_state, physics_pipeline) =
|
|
|
|
let (physics_state, physics_pipeline) =
|
|
|
|
PhysicsState::build(rapier3d::math::Vector::new(0.0, -9.81, 0.05));
|
|
|
|
PhysicsState::build(rapier3d::math::Vector::new(0.0, -9.81, 0.05));
|
|
|
|
resources.insert(physics_state);
|
|
|
|
resources.insert(physics_state);
|
|
|
|
resources.insert(physics_pipeline);
|
|
|
|
resources.insert(physics_pipeline);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Loop data
|
|
|
|
resources.insert(LoopState {
|
|
|
|
resources.insert(LoopState {
|
|
|
|
delta_time: Default::default(),
|
|
|
|
delta_time: Default::default(),
|
|
|
|
start_time: Instant::now(),
|
|
|
|
start_time: Instant::now(),
|
|
|
|
step_size: 0.01666, // 60hz
|
|
|
|
step_size: 0.01666, // 60hz
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// And our event stack
|
|
|
|
resources.insert(Vec::<OwnedEvent<OwnedEventExtension>>::new());
|
|
|
|
resources.insert(Vec::<OwnedEvent<OwnedEventExtension>>::new());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let event_loop_proxy = event_loop.create_proxy();
|
|
|
|
let event_loop_proxy = event_loop.create_proxy();
|
|
|
|
|
|
|
|
|
|
|
|
std::thread::spawn(move || {
|
|
|
|
std::thread::spawn(move || {
|
|
|
@ -147,7 +153,12 @@ fn main() {
|
|
|
|
if gamepad_.name() == "PS4" {
|
|
|
|
if gamepad_.name() == "PS4" {
|
|
|
|
gamepad = Some(gamepad_);
|
|
|
|
gamepad = Some(gamepad_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
println!("{} is {:?} {:?}", gamepad_.name(), gamepad_.power_info(), gamepad_.id());
|
|
|
|
println!(
|
|
|
|
|
|
|
|
"{} is {:?} {:?}",
|
|
|
|
|
|
|
|
gamepad_.name(),
|
|
|
|
|
|
|
|
gamepad_.power_info(),
|
|
|
|
|
|
|
|
gamepad_.id()
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut active_gamepad = None;
|
|
|
|
let mut active_gamepad = None;
|
|
|
|
|
|
|
|
|
|
|
@ -155,9 +166,11 @@ fn main() {
|
|
|
|
while let Some(GilEvent { id, event, time }) = gilrs.next_event() {
|
|
|
|
while let Some(GilEvent { id, event, time }) = gilrs.next_event() {
|
|
|
|
println!("{:?} New event from {}: {:?}", time, id, event);
|
|
|
|
println!("{:?} New event from {}: {:?}", time, id, event);
|
|
|
|
active_gamepad = Some(id);
|
|
|
|
active_gamepad = Some(id);
|
|
|
|
event_loop_proxy.send_event(OwnedEventExtension::GamepadEvent {
|
|
|
|
event_loop_proxy
|
|
|
|
gil_event: GilEvent { id, event, time }
|
|
|
|
.send_event(OwnedEventExtension::GamepadEvent {
|
|
|
|
}).ok();
|
|
|
|
gil_event: GilEvent { id, event, time },
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.ok();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// // You can also use cached gamepad state
|
|
|
|
// // You can also use cached gamepad state
|
|
|
@ -171,29 +184,35 @@ fn main() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
let mut elapsed_time: f32 = { // deltatime since last frame
|
|
|
|
let mut elapsed_time: f32 = {
|
|
|
|
|
|
|
|
// deltatime since last frame
|
|
|
|
let loop_state = resources.get::<LoopState>().unwrap();
|
|
|
|
let loop_state = resources.get::<LoopState>().unwrap();
|
|
|
|
loop_state.start_time.elapsed()
|
|
|
|
loop_state.start_time.elapsed()
|
|
|
|
}.as_secs_f32();
|
|
|
|
}
|
|
|
|
|
|
|
|
.as_secs_f32();
|
|
|
|
|
|
|
|
|
|
|
|
let mut delta_time: f32 = 0.0;
|
|
|
|
let mut delta_time: f32 = 0.0;
|
|
|
|
let mut accumulator_time: f32 = 0.0;
|
|
|
|
let mut accumulator_time: f32 = 0.0;
|
|
|
|
let mut current_time: f32 = elapsed_time;
|
|
|
|
let mut current_time: f32 = elapsed_time;
|
|
|
|
|
|
|
|
|
|
|
|
event_loop.run(move |event, _, control_flow| {
|
|
|
|
event_loop.run(move |event, _, control_flow| {
|
|
|
|
|
|
|
|
|
|
|
|
*control_flow = ControlFlow::Poll;
|
|
|
|
*control_flow = ControlFlow::Poll;
|
|
|
|
match event {
|
|
|
|
match event {
|
|
|
|
event::Event::NewEvents(cause) => {
|
|
|
|
event::Event::NewEvents(cause) => {
|
|
|
|
event_schedule.execute(&mut world, &mut resources);
|
|
|
|
event_schedule.execute(&mut world, &mut resources);
|
|
|
|
resources.get_mut::<Vec<OwnedEvent<OwnedEventExtension>>>()
|
|
|
|
resources
|
|
|
|
.unwrap().clear();
|
|
|
|
.get_mut::<Vec<OwnedEvent<OwnedEventExtension>>>()
|
|
|
|
|
|
|
|
.unwrap()
|
|
|
|
|
|
|
|
.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
event::Event::MainEventsCleared => {
|
|
|
|
event::Event::MainEventsCleared => {
|
|
|
|
|
|
|
|
let (step_size, elapsed_time) = {
|
|
|
|
let (step_size, elapsed_time) = { // deltatime since last frame
|
|
|
|
// deltatime since last frame
|
|
|
|
let loop_state = resources.get::<LoopState>().unwrap();
|
|
|
|
let loop_state = resources.get::<LoopState>().unwrap();
|
|
|
|
(loop_state.step_size, loop_state.start_time.elapsed().as_secs_f32())
|
|
|
|
(
|
|
|
|
|
|
|
|
loop_state.step_size,
|
|
|
|
|
|
|
|
loop_state.start_time.elapsed().as_secs_f32(),
|
|
|
|
|
|
|
|
)
|
|
|
|
};
|
|
|
|
};
|
|
|
|
delta_time = elapsed_time - current_time;
|
|
|
|
delta_time = elapsed_time - current_time;
|
|
|
|
current_time = elapsed_time;
|
|
|
|
current_time = elapsed_time;
|
|
|
@ -225,9 +244,10 @@ fn main() {
|
|
|
|
.unwrap()
|
|
|
|
.unwrap()
|
|
|
|
.resize(width, height);
|
|
|
|
.resize(width, height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
event::Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {
|
|
|
|
event::Event::WindowEvent {
|
|
|
|
*control_flow = ControlFlow::Exit
|
|
|
|
event: WindowEvent::CloseRequested,
|
|
|
|
}
|
|
|
|
..
|
|
|
|
|
|
|
|
} => *control_flow = ControlFlow::Exit,
|
|
|
|
event::Event::RedrawRequested(_) => {
|
|
|
|
event::Event::RedrawRequested(_) => {
|
|
|
|
// Call the render system
|
|
|
|
// Call the render system
|
|
|
|
render_schedule.execute(&mut world, &mut resources);
|
|
|
|
render_schedule.execute(&mut world, &mut resources);
|
|
|
@ -235,11 +255,14 @@ fn main() {
|
|
|
|
_ => {}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
resources.get_mut::<Vec<OwnedEvent<OwnedEventExtension>>>()
|
|
|
|
resources
|
|
|
|
.unwrap().push(event.into());
|
|
|
|
.get_mut::<Vec<OwnedEvent<OwnedEventExtension>>>()
|
|
|
|
|
|
|
|
.unwrap()
|
|
|
|
|
|
|
|
.push(event.into());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn entity_loading(world: &mut World, renderer: &mut Renderer) {
|
|
|
|
pub fn entity_loading(world: &mut World, renderer: &mut Renderer) {
|
|
|
|
let monkey_mesh = renderer.load_mesh_to_buffer("./resources/monkey.obj");
|
|
|
|
let monkey_mesh = renderer.load_mesh_to_buffer("./resources/monkey.obj");
|
|
|
|
|
|
|
|
|
|
|
@ -248,10 +271,10 @@ pub fn entity_loading(world: &mut World, renderer: &mut Renderer) {
|
|
|
|
position: Point3 {
|
|
|
|
position: Point3 {
|
|
|
|
x: 0.0,
|
|
|
|
x: 0.0,
|
|
|
|
y: 0.0,
|
|
|
|
y: 0.0,
|
|
|
|
z: 10.0
|
|
|
|
z: 10.0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
yaw: Rad(-PI),
|
|
|
|
yaw: Rad(-PI),
|
|
|
|
pitch: Rad(PI/2.0)
|
|
|
|
pitch: Rad(PI / 2.0),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Color {
|
|
|
|
Color {
|
|
|
|
r: 1.0,
|
|
|
|
r: 1.0,
|
|
|
@ -272,8 +295,8 @@ pub fn entity_loading(world: &mut World, renderer: &mut Renderer) {
|
|
|
|
rot: Euler {
|
|
|
|
rot: Euler {
|
|
|
|
x: Deg(0.0),
|
|
|
|
x: Deg(0.0),
|
|
|
|
y: Deg(-25.0),
|
|
|
|
y: Deg(-25.0),
|
|
|
|
z: Deg(0.0)
|
|
|
|
z: Deg(0.0),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Color {
|
|
|
|
Color {
|
|
|
|
r: 1.0,
|
|
|
|
r: 1.0,
|
|
|
@ -323,17 +346,11 @@ pub fn entity_loading(world: &mut World, renderer: &mut Renderer) {
|
|
|
|
));
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
|
|
let mut dynamic_ball_body = RigidBodyBuilder::new_dynamic()
|
|
|
|
let mut dynamic_ball_body = RigidBodyBuilder::new_dynamic()
|
|
|
|
.position(Isometry3::new(
|
|
|
|
.position(Isometry3::new(Vector3::new(0.0, 0.0, 0.0), Vector::y()))
|
|
|
|
Vector3::new(0.0, 0.0, 0.0),
|
|
|
|
|
|
|
|
Vector::y(),
|
|
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
.build();
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
|
|
let mut static_floor_body = RigidBodyBuilder::new_static()
|
|
|
|
let mut static_floor_body = RigidBodyBuilder::new_static()
|
|
|
|
.position(Isometry3::new(
|
|
|
|
.position(Isometry3::new(Vector3::new(0.0, -8.0, 0.0), Vector::y()))
|
|
|
|
Vector3::new(0.0, -8.0, 0.0),
|
|
|
|
|
|
|
|
Vector::y(),
|
|
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
.build();
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
|
|
let ball_collider = ColliderBuilder::ball(1.5).build();
|
|
|
|
let ball_collider = ColliderBuilder::ball(1.5).build();
|
|
|
|