reworked voxel; added some voxel kinds and added voxel colors

Commit 7b78288b0502 · mgecumshooter · 2026-10-21 23:52 +0300

Changeset
7b78288b0502c05af2ad501563ad307f9fdd77bd

View source at this commit

Comments

No comments yet.

Log in to comment

Diff

diff --git a/src/chunk/mod.rs b/src/chunk/mod.rs
--- a/src/chunk/mod.rs
+++ b/src/chunk/mod.rs
@@ -7,7 +7,7 @@
 
 // internal deps
 use crate::util::*;
-use crate::voxel::{VOXEL_SIZE, Voxel};
+use crate::voxel::*;
 
 // constants
 pub const CHUNK_SIZE: usize = 32;
@@ -24,7 +24,7 @@
     pub fn new(pos: Vector3) -> Self {
         let mut s = Self {
             chunk_pos: pos,
-            voxels: [Voxel::Air; CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE],
+            voxels: [Voxel::from_kind(VoxelKind::Air); CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE],
             dirty: true,
             mesh: None,
         };
@@ -32,7 +32,7 @@
             for z in 0..CHUNK_SIZE {
                 let y = (x + z).min(CHUNK_SIZE - 1);
                 let i = x + (y * CHUNK_SIZE) + (z * CHUNK_SIZE * CHUNK_SIZE);
-                s.voxels[i] = Voxel::Stone;
+                s.voxels[i] = Voxel::from_kind(VoxelKind::Stone);
             }
         }
         s
@@ -81,14 +81,25 @@
         let mut vertices: Vec<Vector3> = Vec::new();
         let mut normals: Vec<Vector3> = Vec::new();
         let mut indices: Vec<u16> = Vec::new();
+	let mut colors: Vec<u8> = Vec::new();
 
         let mut vertex_offset: i32 = 0;
         for x in 0..CHUNK_SIZE {
             for y in 0..CHUNK_SIZE {
                 for z in 0..CHUNK_SIZE {
-                    if let Some(Voxel::Air) = self.get_voxel(x, y, z) {
-                        continue;
-                    }
+		    let voxel = match self.get_voxel(x, y, z) {
+			Some(v) => {
+			    if v.kind == VoxelKind::Air {
+				continue;
+			    }
+			    v
+			}
+			None => continue,
+		    };
+		    
+                    // if let Some(Voxel::Air) = self.get_voxel(x, y, z) {
+                    //     continue;
+                    // }
                     if !self.is_exposed(x, y, z) {
                         continue;
                     }
@@ -261,6 +272,8 @@
                     indices.push(22 as u16 + vertex_offset as u16);
                     indices.push(23 as u16 + vertex_offset as u16);
 
+		    push_voxel_color(&mut colors, voxel.color);
+
                     vertex_offset += 24;
                 }
             }
@@ -279,14 +292,17 @@
             let v_size = vertex_count * std::mem::size_of::<Vector3>();
             let i_size = index_count * std::mem::size_of::<u16>();
             let n_size = normals_count * std::mem::size_of::<Vector3>();
+	    let c_size = colors.len();
 
             let v_ptr = rlb::ffi::MemAlloc(v_size as u32) as *mut Vector3;
             let i_ptr = rlb::ffi::MemAlloc(i_size as u32) as *mut u16;
             let n_ptr = rlb::ffi::MemAlloc(n_size as u32) as *mut Vector3;
+	    let c_ptr = rlb::ffi::MemAlloc(c_size as u32) as *mut u8;
 
             std::ptr::copy_nonoverlapping(vertices.as_ptr(), v_ptr, vertex_count);
             std::ptr::copy_nonoverlapping(indices.as_ptr(), i_ptr, index_count);
             std::ptr::copy_nonoverlapping(normals.as_ptr(), n_ptr, normals_count);
+	    std::ptr::copy_nonoverlapping(colors.as_ptr(), c_ptr, colors.len());
 
             let raw_mesh = rlb::ffi::Mesh {
                 vertexCount: vertex_count as i32,
@@ -296,7 +312,7 @@
                 normals: n_ptr as *mut std::ffi::c_float,
                 texcoords: std::ptr::null_mut(),
                 texcoords2: std::ptr::null_mut(),
-                colors: std::ptr::null_mut(),
+                colors: c_ptr,
                 tangents: std::ptr::null_mut(),
                 animVertices: std::ptr::null_mut(),
                 animNormals: std::ptr::null_mut(),
@@ -401,9 +417,18 @@
                 return true;
             }
 
-            if let Some(Voxel::Air) = self.get_voxel(nx as usize, ny as usize, nz as usize) {
-                return true;
-            }
+	    match self.get_voxel(nx as usize, ny as usize, nz as usize) {
+		Some(v) => {
+		    if v.kind == VoxelKind::Air {
+			return true;
+		    }
+		}
+		_ => {}
+	    }
+
+        //     if let Some(Voxel.kind::Air) = self.get_voxel(nx as usize, ny as usize, nz as usize) {
+        //         return true;
+        //     }
         }
         false
     }
@@ -418,10 +443,7 @@
 
         let mut bytes = Vec::with_capacity(self.voxels.len());
         for v in &self.voxels {
-            bytes.push(match v {
-                Voxel::Air => 0u8,
-                Voxel::Stone => 1u8,
-            });
+            bytes.push(v.kind as u8);
         }
 
         let _ = fs::write(path, bytes);
@@ -434,12 +456,9 @@
         );
         if let Ok(bytes) = fs::read(path) {
             if bytes.len() == CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE {
-                let mut voxels = [Voxel::Air; CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE];
+                let mut voxels = [Voxel::from_kind(VoxelKind::Air); CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE];
                 for (i, &b) in bytes.iter().enumerate() {
-                    voxels[i] = match b {
-                        1u8 => Voxel::Stone,
-                        _ => Voxel::Air,
-                    };
+                    voxels[i] = Voxel::from_byte(b);
                 }
                 return Some(Self {
                     chunk_pos: pos,
@@ -458,3 +477,12 @@
         normals.push(n);
     }
 }
+
+fn push_voxel_color(colors: &mut Vec<u8>, c: VoxelColor) {
+    for _ in 0..24 {
+	colors.push(c.0);
+	colors.push(c.1);
+	colors.push(c.2);
+	colors.push(c.3);
+    }
+}
diff --git a/src/player/mod.rs b/src/player/mod.rs
--- a/src/player/mod.rs
+++ b/src/player/mod.rs
@@ -125,11 +125,20 @@
 		for y in min_y..=max_y {
 		    for z in min_z..=max_z {
 			let (chk_x, chk_y, chk_z) = chunk.get_voxel_pos_glb(x as i32, y as i32, z as i32);
-			if let Some(Voxel::Stone) = chunk.get_voxel
-			    (chk_x as usize, chk_y as usize, chk_z as usize) {
-			    let idx = chunk.voxel_idx(chk_x as usize, chk_y as usize, chk_z as usize);
-			    obstacles.push(chunk.get_voxel_global_aabb(idx));
+			match chunk.get_voxel(chk_x as usize, chk_y as usize, chk_z as usize) {
+			    Some(v) => {
+				if v.kind != VoxelKind::Air {
+				    let idx = chunk.voxel_idx(chk_x as usize, chk_y as usize, chk_z as usize);
+				    obstacles.push(chunk.get_voxel_global_aabb(idx));
+				}
+			    }
+			    _ => {}
 			}
+			// if let Some(Voxel::Stone) = chunk.get_voxel
+			//     (chk_x as usize, chk_y as usize, chk_z as usize) {
+			//     let idx = chunk.voxel_idx(chk_x as usize, chk_y as usize, chk_z as usize);
+			//     obstacles.push(chunk.get_voxel_global_aabb(idx));
+			// }
 		    }
 		}
 	    }
diff --git a/src/voxel/mod.rs b/src/voxel/mod.rs
--- a/src/voxel/mod.rs
+++ b/src/voxel/mod.rs
@@ -1,7 +1,41 @@
+pub const VOXEL_SIZE: usize = 1;
+
 #[derive(Clone, Copy, PartialEq, Eq)]
-pub enum Voxel {
-    Air,
-    Stone,
+#[repr(u8)]
+pub enum VoxelKind {
+    Air = 0,
+    Stone = 1,
+    Dirt = 2,
+    Wood = 3,
+}
+
+#[derive(Clone, Copy)]
+pub struct VoxelColor(pub u8, pub u8, pub u8, pub u8);
+
+#[derive(Clone, Copy)]
+pub struct Voxel {
+    pub kind: VoxelKind,
+    pub color: VoxelColor,
 }
 
-pub const VOXEL_SIZE: usize = 1;
+impl Voxel {
+    pub fn from_kind(kind: VoxelKind) -> Self {
+	let color = match kind {
+	    VoxelKind::Air => VoxelColor(0, 0, 0, 0),
+	    VoxelKind::Stone => VoxelColor(128, 128, 128, 255),
+	    VoxelKind::Dirt => VoxelColor(96, 136, 67, 255),
+	    VoxelKind::Wood => VoxelColor(150, 128, 128, 255),
+	};
+	Self { kind, color }
+    }
+    pub fn from_byte(b: u8) -> Self {
+	let kind = match b {
+	    0 => VoxelKind::Air,
+	    1 => VoxelKind::Stone,
+	    2 => VoxelKind::Dirt,
+	    3 => VoxelKind::Wood,
+	    _ => VoxelKind::Air,
+	};
+	Self::from_kind(kind)
+    }
+}