rendering is back, now to render the entities

This commit is contained in:
2021-02-02 01:15:37 -08:00
parent df13db5270
commit 60a950abe2
2 changed files with 72 additions and 45 deletions

View File

@@ -6,14 +6,16 @@ use bytemuck::{Pod, Zeroable};
use legion::*;
use wgpu::util::DeviceExt;
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::window::Window;
use crate::geometry::{create_plane, import_mesh, Vertex};
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 legion::world::SubWorld;
use cgmath::Point3;
#[repr(C)]
#[derive(Clone, Copy)]
@@ -49,7 +51,7 @@ pub struct Pass {
}
pub struct Renderer {
window: Window,
pub window: Window,
swapchain: SwapChain,
swapchain_description: Arc<SwapChainDescriptor>,
instance: Arc<Instance>,
@@ -80,7 +82,7 @@ impl Renderer {
};
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_view = cgmath::Matrix4::look_at(
cgmath::Point3::new(3.0f32, -10.0, 6.0),
@@ -92,39 +94,37 @@ impl Renderer {
}
}
#[system(for_each)]
pub fn render_test(pos: &mut Position, vel: &Velocity, #[resource] renderer: &mut Renderer) {
//#[system(for_each)]
#[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();
//pos.x += vel.dx * time.elapsed_seconds;
//pos.y += vel.dy * time.elapsed_seconds;
// frame: &wgpu::SwapChainTexture,
// device: &wgpu::Device,
// queue: &wgpu::Queue,
// _spawner: &impl futures::task::LocalSpawn,
// )
// {
let mut query = <(&mut Position, &mut Mesh, &mut Color)>::query();
// update uniforms
// for entity in self.entities.iter_mut() {
//
// // Revolve the entity by the rotation speed, only if it is non-zero
// if entity.rotation_speed != 0.0 {
// let rotation = cgmath::Matrix4::from_angle_x(cgmath::Deg(entity.rotation_speed));
// entity.mx_world = entity.mx_world * 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));
// }
for (pos, mesh, color) in query.iter_mut(world) {
// Revolve the entity by the rotation speed, only if it is non-zero
// if vel.rs != 0.0 {
// let rotation = cgmath::Matrix4::from_angle_x(cgmath::Deg(vel.rs));
// pos.mx = pos.mx * rotation;
// }
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 {
// self.lights_are_dirty = false;
@@ -223,7 +223,6 @@ pub fn render_test(pos: &mut Position, vel: &Velocity, #[resource] renderer: &mu
renderer.queue.submit(iter::once(encoder.finish()));
renderer.window.request_redraw();
}
impl Renderer {
@@ -285,9 +284,17 @@ impl Renderer {
(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);
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 {