Fixed rendering test.rhai

This commit is contained in:
STP
2023-11-20 00:14:00 -05:00
parent 1afb1a8e19
commit 47276e1bdc
6 changed files with 42 additions and 32 deletions

View File

@@ -2,9 +2,9 @@ use nalgebra::Point3;
use pixels::{wgpu, PixelsContext}; use pixels::{wgpu, PixelsContext};
use std::time::Instant; use std::time::Instant;
const BUFFER_PROPORTION_INIT: f32 = 0.8; const BUFFER_PROPORTION_INIT: f32 = 1.0;
const BUFFER_PROPORTION_MIN: f32 = 0.5; const BUFFER_PROPORTION_MIN: f32 = 0.5;
const BUFFER_PROPORTION_MAX: f32 = 0.9; const BUFFER_PROPORTION_MAX: f32 = 1.0;
const RAYS_INIT: i32 = 9000; const RAYS_INIT: i32 = 9000;
const RAYS_MIN: i32 = 100; const RAYS_MIN: i32 = 100;

View File

@@ -2,23 +2,23 @@ use nalgebra::{Point3, Vector3};
#[derive(Clone)] #[derive(Clone)]
pub struct Light { pub struct Light {
pub colour: Vector3<f32>,
pub position: Point3<f32>, pub position: Point3<f32>,
pub falloff: [f32; 3], pub colour: Vector3<f32>,
pub falloff: Vector3<f32>,
} }
impl Light { impl Light {
pub fn new(colour: Vector3<f32>, position: Point3<f32>, falloff: [f32; 3]) -> Self { pub fn new(position: Point3<f32>, colour: Vector3<f32>, falloff: Vector3<f32>) -> Self {
Light { Light {
colour,
position, position,
colour,
falloff, falloff,
} }
} }
pub fn white() -> Self { pub fn white() -> Self {
let colour = Vector3::new(1.0, 1.0, 1.0); let colour = Vector3::new(1.0, 1.0, 1.0);
let position = Point3::new(0.0, 0.0, 0.0); let position = Point3::new(0.0, 0.0, 0.0);
let falloff = [1.0, 0.0, 0.0]; let falloff = Vector3::new(1.0, 0.0, 0.0);
Light::new(colour, position, falloff) Light::new(position, colour, falloff)
} }
} }

View File

@@ -22,6 +22,7 @@ use nalgebra::Vector3;
fn main() { fn main() {
env_logger::init(); env_logger::init();
env::set_var("RUST_BACKTRACE", "1"); env::set_var("RUST_BACKTRACE", "1");
run(); run();
} }

View File

@@ -108,7 +108,8 @@ impl Scene {
engine engine
.register_type::<Scene>() .register_type::<Scene>()
.register_fn("Scene", Scene::empty) .register_fn("Scene", Scene::empty)
.register_fn("addNode", Scene::add_node); .register_fn("addNode", Scene::add_node)
.register_fn("addLight", Scene::add_light);
engine engine
.register_type::<Node>() .register_type::<Node>()

View File

@@ -43,18 +43,14 @@ pub fn run() {
//Display Surface //Display Surface
let pixels = { let pixels = {
let surface_texture = SurfaceTexture::new(window_size.width, window_size.height, &window); let surface_texture = SurfaceTexture::new(window_size.width, window_size.height, &window);
Pixels::new( Pixels::new(1, 1, surface_texture).unwrap()
window_size.width as u32,
window_size.height as u32,
surface_texture,
)
.unwrap()
}; };
//Gui //Gui
let gui = Gui::new(&window, &pixels); let gui = Gui::new(&window, &pixels);
//State //State
let mut state = State::new(window, pixels, gui); let mut state = State::new(window, pixels, gui);
state.clear();
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
// Draw the current frame // Draw the current frame
@@ -91,6 +87,9 @@ pub struct State {
rays: Vec<Ray>, rays: Vec<Ray>,
window: Window, window: Window,
buffer_width: u32,
buffer_height: u32,
pixels: Arc<Mutex<Pixels>>, pixels: Arc<Mutex<Pixels>>,
gui: Gui, gui: Gui,
index: usize, index: usize,
@@ -120,14 +119,14 @@ impl State {
scene, scene,
rays, rays,
window, window,
buffer_width: (window_size.width as f32 * gui.buffer_proportion) as u32,
buffer_height: (window_size.height as f32 * gui.buffer_proportion) as u32,
pixels: Arc::new(Mutex::new(pixels)), pixels: Arc::new(Mutex::new(pixels)),
gui, gui,
index: 0, index: 0,
} }
} }
/// Create a new `World` instance that can draw a moving box.
fn update(&mut self) -> Result<(), Box<dyn Error>> { fn update(&mut self) -> Result<(), Box<dyn Error>> {
let gui_event = self.gui.event.take(); let gui_event = self.gui.event.take();
match gui_event { match gui_event {
@@ -135,20 +134,19 @@ impl State {
GuiEvent::BufferResize | GuiEvent::CameraRelocate => { GuiEvent::BufferResize | GuiEvent::CameraRelocate => {
let pixels = &self.pixels; let pixels = &self.pixels;
let size = self.window.inner_size(); let size = self.window.inner_size();
let width_new = (size.width as f32 * self.gui.buffer_proportion) as u32; self.buffer_width = (size.width as f32 * self.gui.buffer_proportion) as u32;
let height_new = (size.height as f32 * self.gui.buffer_proportion) as u32; self.buffer_height = (size.height as f32 * self.gui.buffer_proportion) as u32;
self.clear(); self.clear();
let mut pixels = self.pixels.lock().unwrap(); let mut pixels = self.pixels.lock().unwrap();
pixels pixels
.resize_buffer(width_new, height_new) .resize_buffer(self.buffer_width, self.buffer_height)
.expect("Resize Error"); .expect("Resize Error");
self.scene.camera.set_position(self.gui.camera_eye);
self.rays = self.scene.camera.cast_rays(width_new, height_new);
} }
GuiEvent::SceneLoad(filename) => { GuiEvent::SceneLoad(filename) => {
println!("Reading {}", filename);
match self.update_scene_from_file(&filename) { match self.update_scene_from_file(&filename) {
Err(e) => { Err(e) => {
println!() println!("{}", e)
} }
Ok(()) => { Ok(()) => {
println!("Loaded file: {filename}") println!("Loaded file: {filename}")
@@ -156,7 +154,6 @@ impl State {
} }
self.clear(); self.clear();
println!("Reading {}", filename);
} }
}, },
None => {} None => {}
@@ -170,11 +167,10 @@ impl State {
println!("RESIZING!"); println!("RESIZING!");
let gui = &self.gui; let gui = &self.gui;
let mut pixels = self.pixels.lock().unwrap(); let mut pixels = self.pixels.lock().unwrap();
if let Err(err) = pixels.resize_surface(size.width, size.height) { match pixels.resize_surface(size.width, size.height) {
log_error("pixels.resize_surface", err); Err(e) => Err(Box::new(e)),
return Ok(()); _ => Ok(()),
} }
Ok(())
} }
fn keyboard_input(&mut self, key: &KeyboardInput) { fn keyboard_input(&mut self, key: &KeyboardInput) {
@@ -191,7 +187,7 @@ impl State {
/// Draw the `World` state to the frame buffer. /// Draw the `World` state to the frame buffer.
/// ///
/// Assumes the default texture format: `wgpu::TextureFormat::Rgba8UnormSrgb` /// Assumes the default texture format: `wgpu::TextureFormat::Rgba8UnormSrgb`
fn draw(&mut self) { fn draw(&mut self) -> Result<(), Box<dyn Error>> {
for i in 0..self.gui.ray_num { for i in 0..self.gui.ray_num {
let i = self.index as usize; let i = self.index as usize;
let ray_num = self.gui.ray_num; let ray_num = self.gui.ray_num;
@@ -210,6 +206,7 @@ impl State {
frame[i * 4..(i + 1) * 4].copy_from_slice(&rgba); frame[i * 4..(i + 1) * 4].copy_from_slice(&rgba);
self.index = (self.index + 1) % (frame.len() / 4); self.index = (self.index + 1) % (frame.len() / 4);
} }
Ok(())
} }
fn clear(&mut self) { fn clear(&mut self) {
@@ -220,11 +217,16 @@ impl State {
let rgba = [0x00, 0x00, 0x00, 0xff]; let rgba = [0x00, 0x00, 0x00, 0xff];
pixel.copy_from_slice(&rgba); pixel.copy_from_slice(&rgba);
} }
self.scene.camera.set_position(self.gui.camera_eye);
self.rays = self
.scene
.camera
.cast_rays(self.buffer_width, self.buffer_height);
} }
fn render(&mut self) -> Result<(), Box<dyn Error>> { fn render(&mut self) -> Result<(), Box<dyn Error>> {
self.update()?; //Update state self.update()?; //Update state
self.draw(); //Draw to pixels self.draw()?; //Draw to pixels
let pixels = self.pixels.lock().unwrap(); let pixels = self.pixels.lock().unwrap();
self.gui self.gui
.prepare(&self.window) .prepare(&self.window)

View File

@@ -7,8 +7,14 @@ let cam = Camera(eye, target, up, 70.0, 1.0);
let material = Material(V(0.5,0.5,0.5), V(0.8, 0.1, 0.2), 0.25); let material = Material(V(0.5,0.5,0.5), V(0.8, 0.1, 0.2), 0.25);
let sphere = Sphere(P(0.0,0.0,0.0), 0.5, material); let light = Light(P(2.0, 2.0, 2.0), V(0.8, 0.7, 0.3), V(1.0, 0.007, 0.0));
let light2 = Light(P(2.0, -2.0, 2.0), V(0.3, 0.4, 0.8), V(1.0, 0.007, 0.0));
scene.addLight(light);
scene.addLight(light2);
let sphere = Sphere(P(0.0,0.0,0.0), 1.0, material);
let node = Node(sphere); let node = Node(sphere);
scene.addNode(node); scene.addNode(node);
scene.render();
scene