added wall

This commit is contained in:
Piotrek
2021-04-24 22:24:10 +02:00
parent d194337520
commit e390b6ed9b
5 changed files with 24 additions and 31 deletions
+14 -22
View File
@@ -39,31 +39,19 @@ impl App {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let start = std::time::Instant::now(); let start = std::time::Instant::now();
// Ground
for bx in 0..16 { for bx in 0..16 {
for bz in 0..16 { for bz in 0..16 {
let brick_id = renderer.brick_get_id(bx, 0, bz, true);
let brick_id = renderer.brick_get_id(bx, 0, bz);
renderer.brick_mark_changed(brick_id);
for x in 0..32 { for x in 0..32 {
for z in 0..32 { for z in 0..32 {
// Dirt // Dirt
for y in 0..31 { renderer.brick_set_voxel(brick_id, x, 0, z, 1);
renderer.brick_set_voxel(brick_id, x, y, z, 1);
}
// Grass // Grass
renderer.brick_set_voxel(brick_id, x, 31, z, 2); let h = (rng.gen::<f32>() * 5.0) as usize;
} for y in 1..h {
}
let brick_id = renderer.brick_get_id(bx, 1, bz);
renderer.brick_mark_changed(brick_id);
for x in 0..32 {
for z in 0..32 {
// Dirt
let h = (rng.gen::<f32>() * 4.0) as usize;
for y in 0..h {
renderer.brick_set_voxel(brick_id, x, y, z, 2); renderer.brick_set_voxel(brick_id, x, y, z, 2);
} }
} }
@@ -71,16 +59,20 @@ impl App {
} }
} }
let brick_id = renderer.brick_get_id(8, 2, 8); // Wall
for x in 0..32 { for bz in 4..8 {
for by in 0..4 {
// Brick
let brick_id = renderer.brick_get_id(8, by, bz, true);
for x in 14..18 {
for z in 0..32 { for z in 0..32 {
// White block
for y in 0..32 { for y in 0..32 {
renderer.brick_set_voxel(brick_id, x, y, z, 3); renderer.brick_set_voxel(brick_id, x, y, z, 3);
} }
} }
} }
renderer.brick_mark_changed(brick_id); }
}
println!("Generated in {}ms", start.elapsed().as_millis()); println!("Generated in {}ms", start.elapsed().as_millis());
+5 -4
View File
@@ -216,9 +216,8 @@ impl Renderer {
*/ */
#[allow(dead_code)] #[allow(dead_code)]
pub fn set_voxel(&mut self, x: usize, y: usize, z:usize, value: u8) { pub fn set_voxel(&mut self, x: usize, y: usize, z:usize, value: u8) {
let brick_id = self.brick_get_id(x/32, y/32, z/32 ); let brick_id = self.brick_get_id(x/32, y/32, z/32, true);
self.brick_set_voxel(brick_id, x%32, y%32, z%32, value); self.brick_set_voxel(brick_id, x%32, y%32, z%32, value);
self.brick_mark_changed(brick_id);
} }
/* /*
@@ -233,7 +232,7 @@ impl Renderer {
/* /*
* Gets brick id * Gets brick id
*/ */
pub fn brick_get_id(&mut self, x: usize, y: usize, z:usize) -> usize { pub fn brick_get_id(&mut self, x: usize, y: usize, z:usize, mark_changed: bool) -> usize {
// Get node // Get node
let node_idx = x + buffers::WORLD_SIZE * (y + buffers::WORLD_SIZE * z); let node_idx = x + buffers::WORLD_SIZE * (y + buffers::WORLD_SIZE * z);
let mut node_value = self.node_buffer.nodes[node_idx]; let mut node_value = self.node_buffer.nodes[node_idx];
@@ -245,7 +244,9 @@ impl Renderer {
} }
// Get brick id from node value // Get brick id from node value
(node_value - 1) as usize let brick_id = (node_value - 1) as usize;
if mark_changed { self.brick_mark_changed(brick_id); }
brick_id
} }
/* /*
Binary file not shown.
+1 -1
View File
@@ -34,7 +34,7 @@ HitResult castBricks(vec3 origin, vec3 start, vec3 dir, vec3 incAxis, uint addr)
// Normal vector (negate previous increment axis and mult by sign) // Normal vector (negate previous increment axis and mult by sign)
vec3 normal = -incAxis * raySign; vec3 normal = -incAxis * raySign;
return HitResult(brick-1, hitPoint, normal); return HitResult(brick, hitPoint, normal);
} }
// Get new closest axis to increment // Get new closest axis to increment
+1 -1
View File
@@ -13,6 +13,6 @@ vec3 applyLighting(vec3 hitPoint, vec3 view, vec3 normal, uint brick)
vec3 specular = light_color * pow(max(dot(view, reflect(-_SunDir, normal)), 0.0), 32); vec3 specular = light_color * pow(max(dot(view, reflect(-_SunDir, normal)), 0.0), 32);
// Combine colors // Combine colors
vec3 object_color = _Materials[brick].albedo.rgb;//vec3(0.0, 0.1, 0.5); vec3 object_color = _Materials[brick-1].albedo.rgb;//vec3(0.0, 0.1, 0.5);
return (ambient + diffuse + specular) * object_color; return (ambient + diffuse + specular) * object_color;
} }