|
|
|
@ -1,3 +1,6 @@
|
|
|
|
|
extern crate imgui;
|
|
|
|
|
extern crate imgui_wgpu;
|
|
|
|
|
extern crate imgui_winit_support;
|
|
|
|
|
extern crate tobj;
|
|
|
|
|
extern crate winit;
|
|
|
|
|
|
|
|
|
@ -8,9 +11,17 @@ use std::sync::Arc;
|
|
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
|
|
|
|
|
|
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 gilrs::{Gamepad, Gilrs};
|
|
|
|
|
use gilrs::Event as GilEvent;
|
|
|
|
|
use imgui::*;
|
|
|
|
|
use imgui_wgpu::{Renderer as ImguiRenderer, RendererConfig as ImguiRendererConfig};
|
|
|
|
|
use legion::*;
|
|
|
|
|
use rapier3d::counters::Timer;
|
|
|
|
|
use rapier3d::dynamics::{
|
|
|
|
|
IntegrationParameters, JointSet, RigidBody, RigidBodyBuilder, RigidBodyHandle, RigidBodySet,
|
|
|
|
|
};
|
|
|
|
@ -20,30 +31,27 @@ use rapier3d::na::{Isometry, Isometry3, Vector, Vector3};
|
|
|
|
|
use rapier3d::pipeline::PhysicsPipeline;
|
|
|
|
|
use wgpu::{BindGroup, Buffer, TextureView};
|
|
|
|
|
use wgpu_subscriber;
|
|
|
|
|
use winit::event::DeviceEvent::MouseMotion;
|
|
|
|
|
use winit::platform::unix::x11::ffi::Time;
|
|
|
|
|
use winit::{
|
|
|
|
|
event::{self, WindowEvent},
|
|
|
|
|
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::{CameraController, Camera};
|
|
|
|
|
use crate::components::{Collider, Color, Physics, Position, LoopState};
|
|
|
|
|
use crate::camera::{Camera, CameraController};
|
|
|
|
|
use crate::components::{Collider, Color, LoopState, Physics, Position};
|
|
|
|
|
use crate::owned_event::{OwnedEvent, OwnedEventExtension};
|
|
|
|
|
use crate::physics::PhysicsState;
|
|
|
|
|
use crate::render::Renderer;
|
|
|
|
|
use crate::owned_event::{OwnedEventExtension, OwnedEvent};
|
|
|
|
|
use gilrs::{Gamepad, Gilrs};
|
|
|
|
|
use rapier3d::counters::Timer;
|
|
|
|
|
|
|
|
|
|
mod camera;
|
|
|
|
|
mod components;
|
|
|
|
|
mod geometry;
|
|
|
|
|
mod light;
|
|
|
|
|
mod owned_event;
|
|
|
|
|
mod physics;
|
|
|
|
|
mod render;
|
|
|
|
|
mod owned_event;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
@ -104,39 +112,37 @@ fn main() {
|
|
|
|
|
let mut builder = winit::window::WindowBuilder::new();
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
|
|
// 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(window);
|
|
|
|
|
|
|
|
|
|
// Physics
|
|
|
|
|
let (physics_state, physics_pipeline) =
|
|
|
|
|
PhysicsState::build(rapier3d::math::Vector::new(0.0, -9.81, 0.05));
|
|
|
|
|
resources.insert(physics_state);
|
|
|
|
|
resources.insert(physics_pipeline);
|
|
|
|
|
|
|
|
|
|
// Loop data
|
|
|
|
|
resources.insert(LoopState {
|
|
|
|
|
delta_time: Default::default(),
|
|
|
|
|
start_time: Instant::now(),
|
|
|
|
|
step_size: 0.01666, // 60hz
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// And our event stack
|
|
|
|
|
resources.insert(Vec::<OwnedEvent<OwnedEventExtension>>::new());
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let event_loop_proxy = event_loop.create_proxy();
|
|
|
|
|
|
|
|
|
|
std::thread::spawn(move || {
|
|
|
|
@ -147,7 +153,12 @@ fn main() {
|
|
|
|
|
if gamepad_.name() == "PS4" {
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
@ -155,9 +166,11 @@ fn main() {
|
|
|
|
|
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(OwnedEventExtension::GamepadEvent {
|
|
|
|
|
gil_event: GilEvent { id, event, time }
|
|
|
|
|
}).ok();
|
|
|
|
|
event_loop_proxy
|
|
|
|
|
.send_event(OwnedEventExtension::GamepadEvent {
|
|
|
|
|
gil_event: GilEvent { id, event, time },
|
|
|
|
|
})
|
|
|
|
|
.ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// // 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();
|
|
|
|
|
loop_state.start_time.elapsed()
|
|
|
|
|
}.as_secs_f32();
|
|
|
|
|
}
|
|
|
|
|
.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| {
|
|
|
|
|
|
|
|
|
|
*control_flow = ControlFlow::Poll;
|
|
|
|
|
match event {
|
|
|
|
|
event::Event::NewEvents(cause) => {
|
|
|
|
|
event_schedule.execute(&mut world, &mut resources);
|
|
|
|
|
resources.get_mut::<Vec<OwnedEvent<OwnedEventExtension>>>()
|
|
|
|
|
.unwrap().clear();
|
|
|
|
|
resources
|
|
|
|
|
.get_mut::<Vec<OwnedEvent<OwnedEventExtension>>>()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.clear();
|
|
|
|
|
}
|
|
|
|
|
event::Event::MainEventsCleared => {
|
|
|
|
|
|
|
|
|
|
let (step_size, elapsed_time) = { // deltatime since last frame
|
|
|
|
|
let (step_size, elapsed_time) = {
|
|
|
|
|
// deltatime since last frame
|
|
|
|
|
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;
|
|
|
|
|
current_time = elapsed_time;
|
|
|
|
@ -225,9 +244,10 @@ fn main() {
|
|
|
|
|
.unwrap()
|
|
|
|
|
.resize(width, height);
|
|
|
|
|
}
|
|
|
|
|
event::Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {
|
|
|
|
|
*control_flow = ControlFlow::Exit
|
|
|
|
|
}
|
|
|
|
|
event::Event::WindowEvent {
|
|
|
|
|
event: WindowEvent::CloseRequested,
|
|
|
|
|
..
|
|
|
|
|
} => *control_flow = ControlFlow::Exit,
|
|
|
|
|
event::Event::RedrawRequested(_) => {
|
|
|
|
|
// Call the render system
|
|
|
|
|
render_schedule.execute(&mut world, &mut resources);
|
|
|
|
@ -235,11 +255,14 @@ fn main() {
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resources.get_mut::<Vec<OwnedEvent<OwnedEventExtension>>>()
|
|
|
|
|
.unwrap().push(event.into());
|
|
|
|
|
resources
|
|
|
|
|
.get_mut::<Vec<OwnedEvent<OwnedEventExtension>>>()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.push(event.into());
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn entity_loading(world: &mut World, renderer: &mut Renderer) {
|
|
|
|
|
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 {
|
|
|
|
|
x: 0.0,
|
|
|
|
|
y: 0.0,
|
|
|
|
|
z: 10.0
|
|
|
|
|
z: 10.0,
|
|
|
|
|
},
|
|
|
|
|
yaw: Rad(-PI),
|
|
|
|
|
pitch: Rad(PI/2.0)
|
|
|
|
|
pitch: Rad(PI / 2.0),
|
|
|
|
|
},
|
|
|
|
|
Color {
|
|
|
|
|
r: 1.0,
|
|
|
|
@ -272,8 +295,8 @@ pub fn entity_loading(world: &mut World, renderer: &mut Renderer) {
|
|
|
|
|
rot: Euler {
|
|
|
|
|
x: Deg(0.0),
|
|
|
|
|
y: Deg(-25.0),
|
|
|
|
|
z: Deg(0.0)
|
|
|
|
|
}
|
|
|
|
|
z: Deg(0.0),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Color {
|
|
|
|
|
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()
|
|
|
|
|
.position(Isometry3::new(
|
|
|
|
|
Vector3::new(0.0, 0.0, 0.0),
|
|
|
|
|
Vector::y(),
|
|
|
|
|
))
|
|
|
|
|
.position(Isometry3::new(Vector3::new(0.0, 0.0, 0.0), Vector::y()))
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
let mut static_floor_body = RigidBodyBuilder::new_static()
|
|
|
|
|
.position(Isometry3::new(
|
|
|
|
|
Vector3::new(0.0, -8.0, 0.0),
|
|
|
|
|
Vector::y(),
|
|
|
|
|
))
|
|
|
|
|
.position(Isometry3::new(Vector3::new(0.0, -8.0, 0.0), Vector::y()))
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
let ball_collider = ColliderBuilder::ball(1.5).build();
|
|
|
|
|