going to switch the lights over to using position, or maybe flesh out directional light more

This commit is contained in:
2021-02-13 23:05:45 -08:00
parent 2c9ce75b8b
commit 9665a70bac
7 changed files with 220 additions and 105 deletions

View File

@@ -157,15 +157,16 @@ impl CameraController {
(1.0 * camera.pitch.0.sin() * camera.yaw.0.cos()),
);
let yaw_offset = camera.yaw.0 - Rad(PI).0;
let right_vector = Vector3::new(
(1.0 * camera.pitch.0.sin() * yaw_offset.sin()),
(1.0 * camera.pitch.0.cos()),
(1.0 * camera.pitch.0.sin() * yaw_offset.cos()),
let offset = camera.yaw.0 + PI/2.0;
let pitch = PI/2.0;
let left_vector = Vector3::new(
(1.0 * pitch.sin() * offset.sin()),
(1.0 * pitch.cos()),
(1.0 * pitch.sin() * offset.cos()),
);
camera.position += view_vector * (self.amount_forward - self.amount_backward) * self.speed * dt;
camera.position += right_vector * (self.amount_right - self.amount_left) * self.speed * dt;
camera.position += left_vector * (self.amount_left - self.amount_right) * self.speed * dt;
// I'm not a huge fan of this
// // Move in/out (aka. "zoom")

View File

@@ -44,13 +44,7 @@ pub struct RangeCopy<Idx> {
pub end: Idx,
}
#[derive(Clone, Debug)]
pub struct DirectionalLight {
pub color: wgpu::Color,
pub fov: f32,
pub depth: RangeCopy<f32>,
pub target_view: Arc<TextureView>,
}
#[derive(Clone, Debug)]
pub struct Mesh {

View File

@@ -2,7 +2,9 @@ use bytemuck::__core::ops::Range;
use bytemuck::{Zeroable, Pod};
use cgmath::Point3;
use crate::render::OPENGL_TO_WGPU_MATRIX;
use crate::components::DirectionalLight;
use std::sync::Arc;
use wgpu::TextureView;
use crate::components::RangeCopy;
#[repr(C)]
@@ -17,6 +19,14 @@ unsafe impl Pod for LightRaw {}
unsafe impl Zeroable for LightRaw {}
#[derive(Clone, Debug)]
pub struct DirectionalLight {
pub color: wgpu::Color,
pub fov: f32,
pub depth: RangeCopy<f32>,
pub target_view: Arc<TextureView>,
}
impl DirectionalLight {
pub fn to_raw(&self, pos: Point3<f32>) -> LightRaw {
use cgmath::{Deg, EuclideanSpace, Matrix4, PerspectiveFov, Point3, Vector3};

View File

@@ -131,7 +131,7 @@ fn main() {
resources.insert(LoopState {
delta_time: Default::default(),
start_time: Instant::now(),
step_size: 0.005,
step_size: 0.01666, // 60hz
});
@@ -181,11 +181,8 @@ fn main() {
let mut current_time: f32 = elapsed_time;
event_loop.run(move |event, _, control_flow| {
// Artificially slows the loop rate to 10 millis
// This is called after redraw events cleared
//*control_flow = ControlFlow::WaitUntil(Instant::now() + Duration::from_millis(10));
*control_flow = ControlFlow::Poll;
*control_flow = ControlFlow::Poll;
match event {
event::Event::NewEvents(cause) => {
event_schedule.execute(&mut world, &mut resources);
@@ -256,15 +253,30 @@ pub fn entity_loading(world: &mut World, renderer: &mut Renderer) {
yaw: Rad(-PI),
pitch: Rad(PI/2.0)
},
Color {
r: 1.0,
g: 1.0,
b: 0.5,
a: 1.0,
},
CameraController::new(3.0, 1.0),
));
let light_mesh = renderer.load_mesh_to_buffer("./resources/light.obj");
let light_entity: Entity = world.push((
cgmath::Point3 {
x: 7.0 as f32,
y: -5.0 as f32,
z: 10.0 as f32,
x: 5.0 as f32,
y: 5.0 as f32,
z: 5.0 as f32,
},
Color {
r: 1.0,
g: 0.0,
b: 1.0,
a: 1.0,
},
light_mesh.clone(),
renderer.create_light(),
));
@@ -274,6 +286,7 @@ pub fn entity_loading(world: &mut World, renderer: &mut Renderer) {
y: 7.0 as f32,
z: 10.0 as f32,
},
light_mesh,
renderer.create_light(),
));
@@ -297,9 +310,9 @@ pub fn entity_loading(world: &mut World, renderer: &mut Renderer) {
},
monkey_mesh,
Color {
r: 1.0,
g: 0.5,
b: 0.5,
r: 0.0,
g: 1.0,
b: 0.0,
a: 1.0,
},
));

View File

@@ -21,9 +21,9 @@ use winit::platform::unix::x11::ffi::Time;
use winit::window::Window;
use crate::camera::{Camera, CameraController};
use crate::components::{Color, DirectionalLight, Mesh, Position, RangeCopy};
use crate::components::{Color, Mesh, Position, RangeCopy};
use crate::geometry::{create_plane, import_mesh, vertex, Vertex};
use crate::light::LightRaw;
use crate::light::{LightRaw, DirectionalLight};
#[cfg_attr(rustfmt, rustfmt_skip)]
#[allow(unused)]
@@ -267,8 +267,8 @@ impl Renderer {
cgmath::perspective(
cgmath::Deg(75f32),
self.size.width as f32 / self.size.height as f32,
1.0,
20.0,
0.1,
100.0,
)
}
@@ -340,46 +340,6 @@ impl Renderer {
}
}
/// I don't know why the hell this is still in here
pub fn create_plane(&self, size: f32) -> Mesh {
let vertices = [
vertex([size, -size, 0.0], [0.0, 0.0, 1.0]),
vertex([size, size, 0.0], [0.0, 0.0, 1.0]),
vertex([-size, -size, 0.0], [0.0, 0.0, 1.0]),
vertex([-size, size, 0.0], [0.0, 0.0, 1.0]),
];
let indices: &[u32] = &[0, 1, 2, 2, 1, 3];
let index_count = indices.len();
let (vertex_buf, index_buf) =
Renderer::create_buffer(&self.device, indices.to_vec(), vertices.to_vec());
let uniform_buf = Arc::new(self.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Plane Uniform Buf"),
size: mem::size_of::<EntityUniforms>() as wgpu::BufferAddress,
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
mapped_at_creation: false,
}));
let bind_group = Arc::new(self.device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &self.entity_bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::Buffer(uniform_buf.slice(..)),
}],
label: Some("Plane Bind Group"),
}));
Mesh {
index_buffer: index_buf,
index_count: index_count,
vertex_buffer: vertex_buf,
uniform_buffer: uniform_buf,
bind_group: bind_group,
}
}
pub fn load_mesh_to_buffer(&self, filepath: &str) -> Mesh {
let (vertices, indices) = import_mesh(filepath);
let index_count = indices.len();
@@ -411,7 +371,6 @@ impl Renderer {
}
pub fn init(window: &Window) -> Renderer {
log::info!("Initializing the surface...");
// Grab the GPU instance, and query its features
let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY);
@@ -428,11 +387,9 @@ impl Renderer {
});
let adapter = futures::executor::block_on(adapter).unwrap();
let optional_features = Renderer::optional_features();
let required_features = Renderer::required_features();
let adapter_features = adapter.features();
let needed_limits = wgpu::Limits::default(); //Renderer::required_limits();
// Maybe for debug tracing???
@@ -453,39 +410,6 @@ impl Renderer {
let queue = Arc::new(queue);
let device = Arc::new(device);
// This is some gross-ass web shit
/*#[cfg(target_arch = "wasm32")]
let spawner = {
use futures::{future::LocalFutureObj, task::SpawnError};
use winit::platform::web::WindowExtWebSys;
struct WebSpawner {}
impl LocalSpawn for WebSpawner {
fn spawn_local_obj(
&self,
future: LocalFutureObj<'static, ()>,
) -> Result<(), SpawnError> {
Ok(wasm_bindgen_futures::spawn_local(future))
}
}
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
// On wasm, append the canvas to the document body
web_sys::window()
.and_then(|win| win.document())
.and_then(|doc| doc.body())
.and_then(|body| {
body.append_child(&web_sys::Element::from(window.canvas()))
.ok()
})
.expect("couldn't append canvas to document body");
WebSpawner {}
};*/
log::info!("Done doing the loading part...");
let mut sc_desc = (wgpu::SwapChainDescriptor {
// Allows a texture to be a output attachment of a renderpass.
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,