|
|
@ -6,14 +6,16 @@ use bytemuck::{Pod, Zeroable};
|
|
|
|
use legion::*;
|
|
|
|
use legion::*;
|
|
|
|
use wgpu::util::DeviceExt;
|
|
|
|
use wgpu::util::DeviceExt;
|
|
|
|
use wgpu::{Buffer, Device, SwapChain, Queue, SwapChainFrame, Surface, SwapChainDescriptor, Instance};
|
|
|
|
use wgpu::{Buffer, Device, SwapChain, Queue, SwapChainFrame, Surface, SwapChainDescriptor, Instance};
|
|
|
|
use winit::dpi::{Position, PhysicalSize};
|
|
|
|
use winit::dpi::{PhysicalSize};
|
|
|
|
use winit::platform::unix::x11::ffi::Time;
|
|
|
|
use winit::platform::unix::x11::ffi::Time;
|
|
|
|
use winit::window::Window;
|
|
|
|
use winit::window::Window;
|
|
|
|
|
|
|
|
|
|
|
|
use crate::geometry::{create_plane, import_mesh, Vertex};
|
|
|
|
use crate::geometry::{create_plane, import_mesh, Vertex};
|
|
|
|
use crate::light::LightRaw;
|
|
|
|
use crate::light::LightRaw;
|
|
|
|
use crate::{Velocity, OPENGL_TO_WGPU_MATRIX};
|
|
|
|
use crate::{Velocity, OPENGL_TO_WGPU_MATRIX, Color, Mesh, Position};
|
|
|
|
use futures::executor::LocalPool;
|
|
|
|
use futures::executor::LocalPool;
|
|
|
|
|
|
|
|
use legion::world::SubWorld;
|
|
|
|
|
|
|
|
use cgmath::Point3;
|
|
|
|
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
@ -49,7 +51,7 @@ pub struct Pass {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub struct Renderer {
|
|
|
|
pub struct Renderer {
|
|
|
|
window: Window,
|
|
|
|
pub window: Window,
|
|
|
|
swapchain: SwapChain,
|
|
|
|
swapchain: SwapChain,
|
|
|
|
swapchain_description: Arc<SwapChainDescriptor>,
|
|
|
|
swapchain_description: Arc<SwapChainDescriptor>,
|
|
|
|
instance: Arc<Instance>,
|
|
|
|
instance: Arc<Instance>,
|
|
|
@ -80,7 +82,7 @@ impl Renderer {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
|
|
|
|
const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
|
|
|
|
|
|
|
|
|
|
|
|
fn generate_matrix(aspect_ratio: f32) -> cgmath::Matrix4<f32> {
|
|
|
|
pub(crate) fn generate_matrix(aspect_ratio: f32) -> cgmath::Matrix4<f32> {
|
|
|
|
let mx_projection = cgmath::perspective(cgmath::Deg(45f32), aspect_ratio, 1.0, 20.0);
|
|
|
|
let mx_projection = cgmath::perspective(cgmath::Deg(45f32), aspect_ratio, 1.0, 20.0);
|
|
|
|
let mx_view = cgmath::Matrix4::look_at(
|
|
|
|
let mx_view = cgmath::Matrix4::look_at(
|
|
|
|
cgmath::Point3::new(3.0f32, -10.0, 6.0),
|
|
|
|
cgmath::Point3::new(3.0f32, -10.0, 6.0),
|
|
|
@ -92,40 +94,38 @@ impl Renderer {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[system(for_each)]
|
|
|
|
//#[system(for_each)]
|
|
|
|
pub fn render_test(pos: &mut Position, vel: &Velocity, #[resource] renderer: &mut Renderer) {
|
|
|
|
#[system]
|
|
|
|
|
|
|
|
#[write_component(Position)]
|
|
|
|
|
|
|
|
#[write_component(Mesh)]
|
|
|
|
|
|
|
|
#[write_component(Color)]
|
|
|
|
|
|
|
|
pub fn render_test(world: &mut SubWorld, #[resource] renderer: &mut Renderer) {
|
|
|
|
|
|
|
|
|
|
|
|
let frame = renderer.get_current_frame();
|
|
|
|
let frame = renderer.get_current_frame();
|
|
|
|
//pos.x += vel.dx * time.elapsed_seconds;
|
|
|
|
|
|
|
|
//pos.y += vel.dy * time.elapsed_seconds;
|
|
|
|
let mut query = <(&mut Position, &mut Mesh, &mut Color)>::query();
|
|
|
|
|
|
|
|
|
|
|
|
// frame: &wgpu::SwapChainTexture,
|
|
|
|
|
|
|
|
// device: &wgpu::Device,
|
|
|
|
|
|
|
|
// queue: &wgpu::Queue,
|
|
|
|
|
|
|
|
// _spawner: &impl futures::task::LocalSpawn,
|
|
|
|
|
|
|
|
// )
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
// update uniforms
|
|
|
|
// update uniforms
|
|
|
|
// for entity in self.entities.iter_mut() {
|
|
|
|
for (pos, mesh, color) in query.iter_mut(world) {
|
|
|
|
//
|
|
|
|
|
|
|
|
// // Revolve the entity by the rotation speed, only if it is non-zero
|
|
|
|
// Revolve the entity by the rotation speed, only if it is non-zero
|
|
|
|
// if entity.rotation_speed != 0.0 {
|
|
|
|
// if vel.rs != 0.0 {
|
|
|
|
// let rotation = cgmath::Matrix4::from_angle_x(cgmath::Deg(entity.rotation_speed));
|
|
|
|
// let rotation = cgmath::Matrix4::from_angle_x(cgmath::Deg(vel.rs));
|
|
|
|
// entity.mx_world = entity.mx_world * rotation;
|
|
|
|
// pos.mx = pos.mx * rotation;
|
|
|
|
// }
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// let data = EntityUniforms {
|
|
|
|
|
|
|
|
// model: entity.mx_world.into(),
|
|
|
|
|
|
|
|
// color: [
|
|
|
|
|
|
|
|
// entity.color.r as f32,
|
|
|
|
|
|
|
|
// entity.color.g as f32,
|
|
|
|
|
|
|
|
// entity.color.b as f32,
|
|
|
|
|
|
|
|
// entity.color.a as f32,
|
|
|
|
|
|
|
|
// ],
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
// queue.write_buffer(&entity.uniform_buf, 0, bytemuck::bytes_of(&data));
|
|
|
|
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let data = EntityUniforms {
|
|
|
|
|
|
|
|
model: pos.mx.into(),
|
|
|
|
|
|
|
|
color: [
|
|
|
|
|
|
|
|
color.r as f32,
|
|
|
|
|
|
|
|
color.g as f32,
|
|
|
|
|
|
|
|
color.b as f32,
|
|
|
|
|
|
|
|
color.a as f32,
|
|
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
renderer.queue.write_buffer(&mesh.uniform_buf, 0, bytemuck::bytes_of(&data));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// if self.lights_are_dirty {
|
|
|
|
// if self.lights_are_dirty {
|
|
|
|
// self.lights_are_dirty = false;
|
|
|
|
// self.lights_are_dirty = false;
|
|
|
|
// for (i, light) in self.lights.iter().enumerate() {
|
|
|
|
// for (i, light) in self.lights.iter().enumerate() {
|
|
|
@ -223,7 +223,6 @@ pub fn render_test(pos: &mut Position, vel: &Velocity, #[resource] renderer: &mu
|
|
|
|
|
|
|
|
|
|
|
|
renderer.queue.submit(iter::once(encoder.finish()));
|
|
|
|
renderer.queue.submit(iter::once(encoder.finish()));
|
|
|
|
|
|
|
|
|
|
|
|
renderer.window.request_redraw();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Renderer {
|
|
|
|
impl Renderer {
|
|
|
@ -285,9 +284,17 @@ impl Renderer {
|
|
|
|
(vertex_buf, index_buf)
|
|
|
|
(vertex_buf, index_buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn load_mesh_to_buffer(&self, filepath: &str) -> (Arc<Buffer>, Arc<Buffer>) {
|
|
|
|
pub fn load_mesh_to_buffer(&self, filepath: &str) -> (Arc<Buffer>, Arc<Buffer>, Arc<Buffer>) {
|
|
|
|
let (vertices, indices) = import_mesh(filepath);
|
|
|
|
let (vertices, indices) = import_mesh(filepath);
|
|
|
|
Renderer::create_buffer(&self.device, indices, vertices)
|
|
|
|
let (vertex_buf, index_buf) = Renderer::create_buffer(&self.device, indices, vertices);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let uniform_buf = Arc::new(self.device.create_buffer(&wgpu::BufferDescriptor {
|
|
|
|
|
|
|
|
label: None,
|
|
|
|
|
|
|
|
size: mem::size_of::<EntityUniforms>() as wgpu::BufferAddress,
|
|
|
|
|
|
|
|
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
|
|
|
|
|
|
|
|
mapped_at_creation: false,
|
|
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
(vertex_buf, index_buf, uniform_buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn init(window: Window) -> Renderer {
|
|
|
|
pub fn init(window: Window) -> Renderer {
|
|
|
|