Houstin, we have rendering.

This commit is contained in:
2021-02-03 22:01:21 -08:00
parent 60a950abe2
commit 80ac21e9d3
2 changed files with 190 additions and 108 deletions

View File

@@ -1,25 +1,24 @@
extern crate tobj;
extern crate winit;
use std::rc::Rc;
use std::sync::Arc;
#[cfg(not(target_arch = "wasm32"))]
use std::time::{Duration, Instant};
use bytemuck::__core::ops::Range;
use cgmath::{Matrix4, Point3};
use futures::task::LocalSpawn;
use legion::*;
use wgpu::{BindGroup, Buffer};
use wgpu_subscriber;
use winit::platform::unix::x11::ffi::Time;
use winit::{
event::{self, WindowEvent},
event_loop::{ControlFlow, EventLoop},
};
use crate::render::Renderer;
use bytemuck::__core::ops::Range;
use cgmath::{Point3, Matrix4};
use std::rc::Rc;
use wgpu::Buffer;
use winit::platform::unix::x11::ffi::Time;
use legion::*;
use std::sync::Arc;
mod framework;
mod geometry;
@@ -56,7 +55,6 @@ ECS
*/
#[cfg_attr(rustfmt, rustfmt_skip)]
#[allow(unused)]
pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(
@@ -81,15 +79,15 @@ pub enum ShaderStage {
}
/*
window: winit::window::Window,
event_loop: EventLoop<()>,
instance: wgpu::Instance,
size: winit::dpi::PhysicalSize<u32>,
surface: wgpu::Surface,
adapter: wgpu::Adapter,
device: wgpu::Device,
queue: wgpu::Queue,
*/
window: winit::window::Window,
event_loop: EventLoop<()>,
instance: wgpu::Instance,
size: winit::dpi::PhysicalSize<u32>,
surface: wgpu::Surface,
adapter: wgpu::Adapter,
device: wgpu::Device,
queue: wgpu::Queue,
*/
// a component is any type that is 'static, sized, send and sync
#[derive(Clone, Copy, Debug, PartialEq)]
@@ -125,19 +123,20 @@ pub struct RangeCopy<Idx> {
struct DirectionalLight {
color: wgpu::Color,
fov: f32,
depth: RangeCopy<f32>
depth: RangeCopy<f32>,
}
#[derive(Clone, Debug)]
struct Mesh {
pub struct Mesh {
index_buffer: Arc<Buffer>,
index_count: usize,
vertex_buffer: Arc<Buffer>,
uniform_buf: Arc<Buffer>,
uniform_buffer: Arc<Buffer>,
bind_group: Arc<BindGroup>,
}
fn main() {
// #[cfg(not(target_arch = "wasm32"))]
// {
// let chrome_tracing_dir = std::env::var("WGPU_CHROME_TRACE");
@@ -169,7 +168,7 @@ fn main() {
*/
#[cfg(not(target_arch = "wasm32"))]
let (mut pool, spawner) = {
let (mut pool, spawner) = {
let local_pool = futures::executor::LocalPool::new();
let spawner = local_pool.spawner();
(local_pool, spawner)
@@ -196,26 +195,26 @@ fn main() {
// 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);
}
{
use winit::platform::windows::WindowBuilderExtWindows;
builder = builder.with_no_redirection_bitmap(true);
}
// I think right here is where I can start pulling everything into the renderer
let window = builder.build(&event_loop).unwrap();
// Not sure why this is guarded, maybe we don't handle the event loop timing?
#[cfg(not(target_arch = "wasm32"))]
let mut last_update_inst = Instant::now();
let mut last_update_inst = Instant::now();
log::info!("Entering render loop...");
// Load up the renderer (and the resources)
let mut renderer = render::Renderer::init(window);
let (plane_vertex_buffer, plane_index_buffer, plane_uniform_buffer) = renderer.load_mesh_to_buffer("./resources/untitled.obj");
let untitled_mesh =
renderer.load_mesh_to_buffer("./resources/untitled.obj");
// This could be used for relationships between entities...???
let light_entity: Entity = world.push((
@@ -232,8 +231,11 @@ fn main() {
a: 1.0,
},
fov: 45.0,
depth: RangeCopy { start: 1.0, end: 20.0 },
}
depth: RangeCopy {
start: 1.0,
end: 20.0,
},
},
));
let mesh_entity: Entity = world.push((
@@ -241,13 +243,9 @@ fn main() {
x: -5.0,
y: 7.0,
z: 10.0,
mx: OPENGL_TO_WGPU_MATRIX
},
Mesh {
index_buffer: plane_index_buffer,
vertex_buffer: plane_vertex_buffer,
uniform_buf: plane_uniform_buffer,
mx: OPENGL_TO_WGPU_MATRIX,
},
untitled_mesh,
Color {
r: 1.0,
g: 0.5,
@@ -257,9 +255,45 @@ fn main() {
));
let entities: &[Entity] = world.extend(vec![
(Position { x: 0.0, y: 0.0, z: 0.0, mx: OPENGL_TO_WGPU_MATRIX }, Velocity { dx: 0.0, dy: 0.0, rs: 0.0 }),
(Position { x: 1.0, y: 1.0, z: 0.0, mx: OPENGL_TO_WGPU_MATRIX }, Velocity { dx: 0.0, dy: 0.0, rs: 0.0 }),
(Position { x: 2.0, y: 2.0, z: 0.0, mx: OPENGL_TO_WGPU_MATRIX }, Velocity { dx: 0.0, dy: 0.0, rs: 0.0 }),
(
Position {
x: 0.0,
y: 0.0,
z: 0.0,
mx: OPENGL_TO_WGPU_MATRIX,
},
Velocity {
dx: 0.0,
dy: 0.0,
rs: 0.0,
},
),
(
Position {
x: 1.0,
y: 1.0,
z: 0.0,
mx: OPENGL_TO_WGPU_MATRIX,
},
Velocity {
dx: 0.0,
dy: 0.0,
rs: 0.0,
},
),
(
Position {
x: 2.0,
y: 2.0,
z: 0.0,
mx: OPENGL_TO_WGPU_MATRIX,
},
Velocity {
dx: 0.0,
dy: 0.0,
rs: 0.0,
},
),
]);
// Init, this wants the references to the buffers...
@@ -278,33 +312,37 @@ fn main() {
ControlFlow::Exit
} else {
#[cfg(not(target_arch = "wasm32"))]
{
// Artificially slows the loop rate to 10 millis
// This is called after redraw events cleared
ControlFlow::WaitUntil(Instant::now() + Duration::from_millis(10))
}
{
// Artificially slows the loop rate to 10 millis
// This is called after redraw events cleared
ControlFlow::WaitUntil(Instant::now() + Duration::from_millis(10))
}
#[cfg(target_arch = "wasm32")]
{
ControlFlow::Poll
}
{
ControlFlow::Poll
}
};
match event {
event::Event::MainEventsCleared => {
#[cfg(not(target_arch = "wasm32"))]
{
// ask for a redraw every 20 millis
if last_update_inst.elapsed() > Duration::from_millis(20) {
//window.request_redraw();
resources.get_mut::<Renderer>().unwrap().window.request_redraw();
last_update_inst = Instant::now();
}
pool.run_until_stalled();
{
// ask for a redraw every 20 millis
if last_update_inst.elapsed() > Duration::from_millis(20) {
//window.request_redraw();
resources
.get_mut::<Renderer>()
.unwrap()
.window
.request_redraw();
last_update_inst = Instant::now();
}
pool.run_until_stalled();
}
#[cfg(target_arch = "wasm32")]
window.request_redraw();
window.request_redraw();
}
// Resizing will queue a request_redraw
@@ -316,18 +354,21 @@ fn main() {
let width = size.width;
let height = size.height;
resources.get_mut::<Renderer>().unwrap().resize(width, height);
resources
.get_mut::<Renderer>()
.unwrap()
.resize(width, height);
//swap_chain = device.create_swap_chain(&surface, &sc_desc);
}
event::Event::WindowEvent { event, .. } => match event {
WindowEvent::KeyboardInput {
input:
event::KeyboardInput {
virtual_keycode: Some(event::VirtualKeyCode::Escape),
state: event::ElementState::Pressed,
..
},
event::KeyboardInput {
virtual_keycode: Some(event::VirtualKeyCode::Escape),
state: event::ElementState::Pressed,
..
},
..
}
| WindowEvent::CloseRequested => {
@@ -338,15 +379,10 @@ fn main() {
}
},
event::Event::RedrawRequested(_) => {
render_schedule.execute(&mut world, &mut resources);
//resources.get_mut::<Renderer>().unwrap().render();
}
_ => {}
}
});
}