diff --git a/src/app/camera_controller.rs b/src/app/camera_controller.rs index 885241c..f049a22 100644 --- a/src/app/camera_controller.rs +++ b/src/app/camera_controller.rs @@ -43,8 +43,8 @@ impl CameraController { Self { enabled: false, movement: CameraMovement { speed: 2.0, .. CameraMovement::default()}, - rotation: CameraRotation { yaw: 45.0, sensitivity: 0.2, .. CameraRotation::default() }, - cam_position: (0.0, 0.0, 0.0).into(), + rotation: CameraRotation { yaw: 135.0, sensitivity: 0.2, .. CameraRotation::default() }, + cam_position: (-1.0, 2.0, -1.0).into(), } } diff --git a/src/app/mod.rs b/src/app/mod.rs index ab14625..f73af17 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -1,6 +1,5 @@ -use winit::event::{Event, WindowEvent, KeyboardInput, ElementState, VirtualKeyCode, MouseButton}; +use winit::event::Event; use noise::Perlin; -use std::sync::Arc; use crate::renderer::{camera::CameraTransform, Level}; @@ -44,7 +43,6 @@ impl App { GameState::Running => { self.camera_controller.set_enabled(true); } - _ => () }; // Currently we are not preventing any state changes, just return requested state diff --git a/src/app/world_gen.rs b/src/app/world_gen.rs index ef44f80..b58f979 100644 --- a/src/app/world_gen.rs +++ b/src/app/world_gen.rs @@ -1,9 +1,9 @@ -use cgmath::{Point3, InnerSpace}; +use cgmath::Point3; use noise::{Perlin, NoiseFn}; use rand::{Rng, prelude::ThreadRng}; use crate::renderer::Level; -use crate::renderer::buffers::{BLOCK_SIZE, NODE_TEX_SIZE, Block}; +use crate::renderer::buffers::BLOCK_SIZE; const SCALE: f64 = 150.0; @@ -33,6 +33,7 @@ impl WorldGen { let value = (noise.get([(x + lx) as f64 / SCALE, (z + lz) as f64 /SCALE]) + 1.0) / 2.0; let mut h = (value * 30.0) as usize; h += (rand.gen::() * 2.0) as usize; + if h == 0 { h = 1; } if h > max { max = h; } map[lx][lz] = h; } diff --git a/src/main.rs b/src/main.rs index 277a969..7093130 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,3 @@ -use std::sync::Arc; use winit::{ event::{Event, WindowEvent, KeyboardInput, ElementState, VirtualKeyCode, MouseButton}, event_loop::{ControlFlow, EventLoop}, diff --git a/src/renderer/buffers/blocks.rs b/src/renderer/buffers/blocks.rs index 5a155f3..4c6027b 100644 --- a/src/renderer/buffers/blocks.rs +++ b/src/renderer/buffers/blocks.rs @@ -36,8 +36,8 @@ impl Blocks { pub fn new(device: &wgpu::Device) -> Self { // Create texture - let BLOCK_TEX_DIM = BLOCK_TEX_SIZE * BLOCK_SIZE; - let size = wgpu::Extent3d { width: BLOCK_SIZE as u32, height: BLOCK_TEX_DIM as u32, depth: BLOCK_TEX_DIM as u32 }; + let block_tex_dim = BLOCK_TEX_SIZE * BLOCK_SIZE; + let size = wgpu::Extent3d { width: BLOCK_SIZE as u32, height: block_tex_dim as u32, depth: block_tex_dim as u32 }; let texture = device.create_texture( &wgpu::TextureDescriptor { size: size, @@ -98,7 +98,10 @@ impl Blocks { Self { blocks, texture, bind_layout, bind_group, free_block: 0 } } - pub fn write_id(&mut self, queue: &wgpu::Queue, block_id: usize) { + /* + * Writes block to gpu + */ + pub fn write(&mut self, queue: &wgpu::Queue, block_id: usize) { // Get block from id let block = &self.blocks[block_id]; @@ -123,26 +126,6 @@ impl Blocks { &mut self.blocks[block_id] } - // /* - // Write block data to gpu, should be called to apply changes - // */ - // pub fn write(&self, queue: &wgpu::Queue, block: &Block) { - // // Calculate block position in texture - // let y = (block.id%BLOCK_TEX_SIZE * BLOCK_SIZE) as u32; - // let z = (block.id/BLOCK_TEX_SIZE * BLOCK_SIZE) as u32; - // let block_origin = wgpu::Origin3d{ x:0, y, z }; - // let block_size = wgpu::Extent3d{ width: BLOCK_SIZE as u32, height: BLOCK_SIZE as u32, depth: BLOCK_SIZE as u32 }; - // let block_bytes : [u8;BLOCK_LEN] = block.data.try_into().unwrap(); - - // // Write to texture - // queue.write_texture( - // wgpu::TextureCopyView { texture: &self.texture, mip_level: 0, origin: block_origin }, - // &block_bytes, - // wgpu::TextureDataLayout { offset: 0, bytes_per_row: block_size.width, rows_per_image: block_size.height }, - // block_size - // ); - // } - /* * Allocates new block */ diff --git a/src/renderer/buffers/nodes.rs b/src/renderer/buffers/nodes.rs index a668a1f..fe30e49 100644 --- a/src/renderer/buffers/nodes.rs +++ b/src/renderer/buffers/nodes.rs @@ -1,5 +1,4 @@ use byteorder::{ByteOrder, LittleEndian}; -use rand::prelude::*; use crate::renderer::buffers::{NODE_TEX_SIZE, NODE_TEX_LEN}; @@ -70,7 +69,7 @@ impl Nodes { // Data let nodes = vec![0_u32; NODE_TEX_LEN].into_boxed_slice(); - println!("Node buffer size: {} MB", nodes.len() as f32 * 4.0 / 1024.0 / 1024.0); + println!("Nodes size: {} MB", (nodes.len() as f32 * 4.0 / 1024.0 / 1024.0 * 100.0).round() / 100.0); // Done Self { nodes, texture, size, bind_layout, bind_group } diff --git a/src/renderer/camera.rs b/src/renderer/camera.rs index 3433b89..2e4d735 100644 --- a/src/renderer/camera.rs +++ b/src/renderer/camera.rs @@ -24,9 +24,9 @@ impl Camera { } pub fn set_aspect(&mut self, aspect: f32) { self.proj_matrix = cgmath::perspective(Deg(60.0), aspect, 0.1, 100.0); } - pub fn get_projection(&self) -> Matrix4 { return self.proj_matrix; } - pub fn get_view(&self) -> Matrix4 { return self.view_matrix; } - pub fn get_position(&self) -> Point3 { return self.position; } + pub fn get_projection(&self) -> Matrix4 { self.proj_matrix } + pub fn get_view(&self) -> Matrix4 { self.view_matrix } + pub fn get_position(&self) -> Point3 { self.position } } pub trait CameraTransform { diff --git a/src/renderer/level.rs b/src/renderer/level.rs index 9629f8f..c5ccc0d 100644 --- a/src/renderer/level.rs +++ b/src/renderer/level.rs @@ -9,7 +9,13 @@ pub trait Level { impl Level for Renderer { - fn block_get(&mut self, block_pos: Point3) -> &mut buffers::Block{ + /* + * Grabs existing or allocates new block for given position + * + * block_pos: Position of the block in world, in block space + * block_id: Block identifier, equal to Block.id + */ + fn block_get(&mut self, block_pos: Point3) -> &mut buffers::Block { // Get node let node_idx = block_pos.x + buffers::NODE_TEX_SIZE * (block_pos.y + buffers::NODE_TEX_SIZE * block_pos.z); let node_value = self.buffers.node_buffer.nodes[node_idx]; @@ -25,9 +31,14 @@ impl Level for Renderer { self.buffers.brick_buffer.get((node_value-1) as usize) } - fn block_dirty(&mut self, id: usize) { - if !self.changed_bricks.contains(&id) { - self.changed_bricks.push(id); + /* + * Marks block as dirty, will be sent to gpu at the next frame + * + * block_id: Block identifier, equal to Block.id + */ + fn block_dirty(&mut self, block_id: usize) { + if !self.changed_bricks.contains(&block_id) { + self.changed_bricks.push(block_id); } } } \ No newline at end of file diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 44339b7..6f5e514 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -1,5 +1,4 @@ use winit::{window::Window, dpi::PhysicalSize}; -use cgmath::{Point3}; pub mod buffers; use buffers::Buffers; @@ -121,7 +120,7 @@ impl Renderer { if self.changed_bricks.len() > 0 { self.buffers.node_buffer.write(&self.queue); for block_id in &self.changed_bricks { - self.buffers.brick_buffer.write_id(&self.queue, *block_id); + self.buffers.brick_buffer.write(&self.queue, *block_id); } // let used = self.buffers.brick_buffer.next_block(false); diff --git a/src/shaders/raytrace/.bin/main.frag.spv b/src/shaders/raytrace/.bin/main.frag.spv index 412d9d8..6beb264 100644 Binary files a/src/shaders/raytrace/.bin/main.frag.spv and b/src/shaders/raytrace/.bin/main.frag.spv differ diff --git a/src/shaders/raytrace/main.frag b/src/shaders/raytrace/main.frag index 007b93e..d82ad3a 100644 --- a/src/shaders/raytrace/main.frag +++ b/src/shaders/raytrace/main.frag @@ -1,26 +1,22 @@ #version 450 -struct Material { - vec3 albedo; - float specular; -}; +#include "structs.cginc" -// Variables +// Input output layout(location=0) in vec2 _InUV; layout(location=0) out vec4 _OutColor; -layout(set=0,binding=0) uniform Uniforms { +// Uniform +layout(set=0, binding=0) uniform Uniform { mat4 _ViewMatrix; mat4 _ProjMatrixInv; vec3 _CamPos; vec3 _SunDir; float _Time; }; -layout(set=1,binding=0) uniform usampler3D _NodesTexture; -layout(set=2,binding=0) uniform usampler3D _BricksTexture; -layout(set=3, binding=0) uniform Materials { - Material _Materials[256]; -}; +layout(set=1, binding=0) uniform usampler3D _NodesTexture; +layout(set=2, binding=0) uniform usampler3D _BricksTexture; +layout(set=3, binding=0) uniform Materials { Material _Materials[256]; }; // Defines #define EPSILON 0.00000001 @@ -29,34 +25,12 @@ layout(set=3, binding=0) uniform Materials { #define BLOCK_TEX_SIZE 64 #define BLOCK_SIZE 32 - -// Include -#include "structs.cginc" +// Includes +#include "util.cginc" #include "shading.cginc" #include "raycasting.cginc" -float seed; -float random(float s) { - return fract(sin(seed++ + s)*43758.5453123); -} - -vec3 randomHemisphere(vec3 dir) -{ - vec3 uu = normalize(cross(dir, vec3(0.0,1.0,1.0))); - vec3 vv = cross(uu, dir); - - vec2 rv2 = vec2(random(1), random(2)); - - float ra = sqrt(rv2.y); - float rx = ra*cos(6.2831*rv2.x); - float ry = ra*sin(6.2831*rv2.x); - float rz = sqrt(1.0-rv2.y); - vec3 rr = vec3(rx*uu + ry*vv + rz*dir ); - - return normalize(rr); -} - vec3 solve() { // Calculate ray direction @@ -90,6 +64,6 @@ vec3 solve() void main() { - seed = 420 * _InUV.x + 1337 * _InUV.y + _Time * 1234; + random_seed = 420 * _InUV.x + 1337 * _InUV.y + _Time * 1234; _OutColor = vec4(solve(), 1); } \ No newline at end of file diff --git a/src/shaders/raytrace/structs.cginc b/src/shaders/raytrace/structs.cginc index 15229ad..014b3a7 100644 --- a/src/shaders/raytrace/structs.cginc +++ b/src/shaders/raytrace/structs.cginc @@ -1,4 +1,9 @@ +struct Material { + vec3 albedo; + float specular; +}; + struct Ray { vec3 origin; vec3 dir; diff --git a/src/shaders/raytrace/util.cginc b/src/shaders/raytrace/util.cginc new file mode 100644 index 0000000..5564e40 --- /dev/null +++ b/src/shaders/raytrace/util.cginc @@ -0,0 +1,21 @@ + +float random_seed; +float random(float s) { + return fract(sin(random_seed++ + s)*43758.5453123); +} + +vec3 randomHemisphere(vec3 dir) +{ + vec3 uu = normalize(cross(dir, vec3(0.0,1.0,1.0))); + vec3 vv = cross(uu, dir); + + vec2 rv2 = vec2(random(1), random(2)); + + float ra = sqrt(rv2.y); + float rx = ra*cos(6.2831*rv2.x); + float ry = ra*sin(6.2831*rv2.x); + float rz = sqrt(1.0-rv2.y); + vec3 rr = vec3(rx*uu + ry*vv + rz*dir ); + + return normalize(rr); +} \ No newline at end of file