.cargo/
ombre/
ombre-derive/
reve/
src/
renderer/
shaders/
mesh.rs
9.3 KiB
scene.rs
3.2 KiB
shaders.rs
6.0 KiB
camera.rs
3.8 KiB
lib.rs
11.7 KiB
main.rs
162 B
renderer.rs
18.6 KiB
Cargo.toml
362 B
README
398 B
syntaxe/
.gitignore
34 B
Cargo.lock
8.5 KiB
Cargo.toml
523 B
README
1.0 KiB
rust-toolchain.toml
77 B
rustfmt.toml
55 B
reve/src/renderer/mesh.rs
raw
| 1 | //! Mesh objects. |
| 2 | use std::f32::consts::PI; |
| 3 | use std::{io, mem}; |
| 4 | |
| 5 | use ombre::bytes::encoding::{Decode, Encode}; |
| 6 | use ombre::derive::Packed; |
| 7 | use ombre::prelude::*; |
| 8 | |
| 9 | /// File extension for XYZW meshes. |
| 10 | #[allow(dead_code)] |
| 11 | pub const FILE_EXTENSION: &str = "xyzw"; |
| 12 | /// File header magic. |
| 13 | pub const MAGIC: [u8; 4] = [b'X', b'Y', b'Z', b'W']; |
| 14 | |
| 15 | /// Mesh vertex. |
| 16 | #[derive(Debug, Clone, PartialEq, Packed)] |
| 17 | #[repr(C)] |
| 18 | pub struct Vertex { |
| 19 | pub position: Vector3D<f32>, |
| 20 | pub color: Rgba<f32>, |
| 21 | pub normal: Vector3D<f32>, |
| 22 | pub uv: Vector2D<f32>, |
| 23 | pub opacity: f32, |
| 24 | pub polygon: u32, |
| 25 | } |
| 26 | |
| 27 | impl Vertex { |
| 28 | /// Create a new vertex. |
| 29 | pub const fn new(x: f32, y: f32, z: f32, nx: f32, ny: f32, nz: f32, u: f32, v: f32) -> Self { |
| 30 | Self { |
| 31 | position: Vector3D::new(x, y, z), |
| 32 | color: Rgba::WHITE, |
| 33 | normal: Vector3D::new(nx, ny, nz), |
| 34 | uv: Vector2D::new(u, v), |
| 35 | opacity: 1.0, |
| 36 | polygon: 0, |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /// Stride used in buffer layout calculations. |
| 41 | #[allow(dead_code)] |
| 42 | pub fn stride() -> usize { |
| 43 | mem::size_of::<Self>() |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | impl Encode for Vertex { |
| 48 | fn encode<W: io::Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> { |
| 49 | let mut n = 0; |
| 50 | |
| 51 | n += self.position.encode(writer)?; |
| 52 | n += self.color.encode(writer)?; |
| 53 | n += self.normal.encode(writer)?; |
| 54 | n += self.uv.encode(writer)?; |
| 55 | n += self.opacity.encode(writer)?; |
| 56 | n += self.polygon.encode(writer)?; |
| 57 | |
| 58 | Ok(n) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | impl Decode for Vertex { |
| 63 | fn decode<R: io::Read + ?Sized>(reader: &mut R) -> Result<Self, io::Error> { |
| 64 | Ok(Self { |
| 65 | position: Decode::decode(reader)?, |
| 66 | color: Decode::decode(reader)?, |
| 67 | normal: Decode::decode(reader)?, |
| 68 | uv: Decode::decode(reader)?, |
| 69 | opacity: Decode::decode(reader)?, |
| 70 | polygon: Decode::decode(reader)?, |
| 71 | }) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /// A mesh with vertices and optional indices. |
| 76 | /// |
| 77 | /// When encoded, uses the following format: |
| 78 | /// |
| 79 | /// .------------. |
| 80 | /// | MAGIC | "XYZW" (4 bytes) |
| 81 | /// +------------+ |
| 82 | /// | NVERTICES | (4 bytes) (Big Endian) Number of vertices |
| 83 | /// +------------+ |
| 84 | /// | NINDICES | (4 bytes) (Big Endian) Number of indices |
| 85 | /// +------------+ |
| 86 | /// | VERTICES | (NVERTICES * N bytes) Vertex data |
| 87 | /// | .... | |
| 88 | /// +------------+ |
| 89 | /// | INDICES | (NINDICES * 4 bytes) Index data |
| 90 | /// | .... | |
| 91 | /// '------------' |
| 92 | /// |
| 93 | #[derive(Debug, PartialEq)] |
| 94 | pub struct Mesh<I = u16> { |
| 95 | /// Mesh vertices. Used as vertex buffer. |
| 96 | pub vertices: Vec<Vertex>, |
| 97 | /// Mesh indices. Used as index buffer. |
| 98 | pub indices: Option<Vec<I>>, |
| 99 | } |
| 100 | |
| 101 | impl Mesh<u16> { |
| 102 | /// Create a "cube" mesh. |
| 103 | pub fn cube(size: f32) -> Self { |
| 104 | let s = size / 2.0; |
| 105 | #[rustfmt::skip] |
| 106 | let mut vertices = vec![ |
| 107 | // Front face. |
| 108 | Vertex::new(-s, -s, s, 0.0, 0.0, 1.0, 0.0, 0.0), |
| 109 | Vertex::new( s, -s, s, 0.0, 0.0, 1.0, 1.0, 0.0), |
| 110 | Vertex::new( s, s, s, 0.0, 0.0, 1.0, 1.0, 1.0), |
| 111 | Vertex::new(-s, s, s, 0.0, 0.0, 1.0, 0.0, 1.0), |
| 112 | |
| 113 | // Back face. |
| 114 | Vertex::new(-s, -s, -s, 0.0, 0.0, -1.0, 1.0, 0.0), |
| 115 | Vertex::new(-s, s, -s, 0.0, 0.0, -1.0, 1.0, 1.0), |
| 116 | Vertex::new( s, s, -s, 0.0, 0.0, -1.0, 0.0, 1.0), |
| 117 | Vertex::new( s, -s, -s, 0.0, 0.0, -1.0, 0.0, 0.0), |
| 118 | |
| 119 | // Left face. |
| 120 | Vertex::new(-s, -s, -s, -1.0, 0.0, 0.0, 0.0, 0.0), |
| 121 | Vertex::new(-s, -s, s, -1.0, 0.0, 0.0, 1.0, 0.0), |
| 122 | Vertex::new(-s, s, s, -1.0, 0.0, 0.0, 1.0, 1.0), |
| 123 | Vertex::new(-s, s, -s, -1.0, 0.0, 0.0, 0.0, 1.0), |
| 124 | |
| 125 | // Right face. |
| 126 | Vertex::new( s, -s, s, 1.0, 0.0, 0.0, 0.0, 0.0), |
| 127 | Vertex::new( s, -s, -s, 1.0, 0.0, 0.0, 1.0, 0.0), |
| 128 | Vertex::new( s, s, -s, 1.0, 0.0, 0.0, 1.0, 1.0), |
| 129 | Vertex::new( s, s, s, 1.0, 0.0, 0.0, 0.0, 1.0), |
| 130 | |
| 131 | // Top face. |
| 132 | Vertex::new(-s, s, s, 0.0, 1.0, 0.0, 0.0, 0.0), |
| 133 | Vertex::new(-s, s, -s, 0.0, 1.0, 0.0, 0.0, 1.0), |
| 134 | Vertex::new( s, s, -s, 0.0, 1.0, 0.0, 1.0, 1.0), |
| 135 | Vertex::new( s, s, s, 0.0, 1.0, 0.0, 1.0, 0.0), |
| 136 | |
| 137 | // Bottom face. |
| 138 | Vertex::new(-s, -s, -s, 0.0, -1.0, 0.0, 1.0, 1.0), |
| 139 | Vertex::new( s, -s, -s, 0.0, -1.0, 0.0, 0.0, 1.0), |
| 140 | Vertex::new( s, -s, s, 0.0, -1.0, 0.0, 0.0, 0.0), |
| 141 | Vertex::new(-s, -s, s, 0.0, -1.0, 0.0, 1.0, 0.0), |
| 142 | ]; |
| 143 | |
| 144 | // Assign polygon IDs, starting from `1`. |
| 145 | for (i, quad) in vertices.chunks_exact_mut(4).enumerate() { |
| 146 | for v in quad { |
| 147 | v.polygon = i as u32 + 1; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | Mesh { |
| 152 | vertices, |
| 153 | #[rustfmt::skip] |
| 154 | indices: Some(vec![ |
| 155 | 0, 1, 2, 0, 2, 3, |
| 156 | 4, 5, 6, 4, 6, 7, |
| 157 | 8, 9, 10, 8, 10, 11, |
| 158 | 12, 13, 14, 12, 14, 15, |
| 159 | 16, 17, 18, 16, 18, 19, |
| 160 | 20, 21, 22, 20, 22, 23, |
| 161 | ]), |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /// Create a "sphere" mesh. |
| 166 | pub fn sphere(color: Rgba8, radius: f32, segments: usize, rings: usize) -> Self { |
| 167 | let mut vertices = Vec::new(); |
| 168 | let mut indices = Vec::new(); |
| 169 | |
| 170 | // Vertices. |
| 171 | for i in 0..=rings { |
| 172 | let phi = i as f32 / rings as f32 * PI; |
| 173 | for j in 0..=segments { |
| 174 | let theta = j as f32 / segments as f32 * 2. * PI; |
| 175 | let position = Vector3D::new( |
| 176 | radius * phi.sin() * theta.cos(), |
| 177 | radius * phi.cos(), |
| 178 | radius * phi.sin() * theta.sin(), |
| 179 | ); |
| 180 | vertices.push(Vertex { |
| 181 | position, |
| 182 | color: color.into(), |
| 183 | normal: position.normalize(), |
| 184 | uv: Vector2D::default(), |
| 185 | opacity: 1., |
| 186 | polygon: 0, // TODO |
| 187 | }); |
| 188 | } |
| 189 | } |
| 190 | // Indices. |
| 191 | for i in 0..rings { |
| 192 | for j in 0..segments { |
| 193 | let first = (i * (segments + 1)) + j; |
| 194 | let second = first + segments + 1; |
| 195 | |
| 196 | // Create the first triangle (upper) |
| 197 | indices.push((first + 1) as u16); |
| 198 | indices.push(second as u16); |
| 199 | indices.push(first as u16); |
| 200 | |
| 201 | // Create the second triangle (lower) |
| 202 | indices.push((first + 1) as u16); |
| 203 | indices.push((second + 1) as u16); |
| 204 | indices.push(second as u16); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | Mesh { |
| 209 | vertices, |
| 210 | indices: Some(indices), |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /// Number of elements to render for this mesh. |
| 215 | pub fn elements(&self) -> usize { |
| 216 | self.indices.as_ref().map(|i| i.len()).unwrap_or(self.vertices.len()) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | impl<I: Encode> Encode for Mesh<I> { |
| 221 | fn encode<W: io::Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> { |
| 222 | let mut n = 0; |
| 223 | |
| 224 | n += MAGIC.encode(w)?; |
| 225 | |
| 226 | let vertices = &self.vertices; |
| 227 | let indices = &self.indices.as_deref().unwrap_or(&[]); |
| 228 | |
| 229 | assert!(vertices.len() <= u32::MAX as usize); |
| 230 | assert!(indices.len() <= u32::MAX as usize); |
| 231 | |
| 232 | n += (vertices.len() as u32).encode(w)?; |
| 233 | n += (indices.len() as u32).encode(w)?; |
| 234 | |
| 235 | for v in vertices { |
| 236 | n += v.encode(w)?; |
| 237 | } |
| 238 | for i in indices.iter() { |
| 239 | n += i.encode(w)?; |
| 240 | } |
| 241 | Ok(n) |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | impl<I: Decode> Decode for Mesh<I> { |
| 246 | fn decode<R: io::Read + ?Sized>(reader: &mut R) -> Result<Self, io::Error> { |
| 247 | let magic: [u8; 4] = Decode::decode(reader)?; |
| 248 | if magic != MAGIC { |
| 249 | return Err(io::ErrorKind::InvalidData.into()); |
| 250 | } |
| 251 | let nvertices = u32::decode(reader)? as usize; |
| 252 | let nindices = u32::decode(reader)? as usize; |
| 253 | |
| 254 | let mut vertices = Vec::with_capacity(nvertices); |
| 255 | while vertices.len() < nvertices { |
| 256 | let v = Vertex::decode(reader)?; |
| 257 | vertices.push(v); |
| 258 | } |
| 259 | |
| 260 | let mut indices = Vec::<I>::with_capacity(nindices); |
| 261 | while indices.len() < nindices { |
| 262 | let ix = I::decode(reader)?; |
| 263 | indices.push(ix); |
| 264 | } |
| 265 | |
| 266 | Ok(Self { |
| 267 | vertices, |
| 268 | indices: (!indices.is_empty()).then_some(indices), |
| 269 | }) |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | impl<I: Decode> TryFrom<&[u8]> for Mesh<I> { |
| 274 | type Error = io::Error; |
| 275 | |
| 276 | fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> { |
| 277 | let mut cursor = io::Cursor::new(bytes); |
| 278 | let mesh = Mesh::<I>::decode(&mut cursor)?; |
| 279 | |
| 280 | Ok(mesh) |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | #[cfg(test)] |
| 285 | mod tests { |
| 286 | #![allow(clippy::unwrap_used)] |
| 287 | use super::*; |
| 288 | |
| 289 | #[test] |
| 290 | fn test_encode_sphere() { |
| 291 | let sphere = Mesh::<u16>::sphere(Rgba8::GREEN, 32.0, 16, 32); |
| 292 | let mut out = Vec::new(); |
| 293 | |
| 294 | sphere.encode(&mut out).unwrap(); |
| 295 | |
| 296 | let decoded = Mesh::<u16>::try_from(out.as_slice()).unwrap(); |
| 297 | assert_eq!(sphere, decoded); |
| 298 | } |
| 299 | |
| 300 | #[test] |
| 301 | fn test_encode_cube() { |
| 302 | let cube = Mesh::<u16>::cube(16.0); |
| 303 | let mut out = Vec::new(); |
| 304 | |
| 305 | cube.encode(&mut out).unwrap(); |
| 306 | |
| 307 | let decoded = Mesh::<u16>::try_from(out.as_slice()).unwrap(); |
| 308 | assert_eq!(cube, decoded); |
| 309 | } |
| 310 | } |