diff options
Diffstat (limited to 'engine/src/mesh.rs')
| -rw-r--r-- | engine/src/mesh.rs | 76 |
1 files changed, 74 insertions, 2 deletions
diff --git a/engine/src/mesh.rs b/engine/src/mesh.rs index 82e2877..c0120a2 100644 --- a/engine/src/mesh.rs +++ b/engine/src/mesh.rs @@ -1,9 +1,11 @@ use std::alloc::Layout; -use crate::mesh::vertex_buffer::VertexLabel; +use zerocopy::IntoBytes; + +use crate::data_types::dimens::Dimens3; +use crate::mesh::vertex_buffer::{VertexAttrProperties, VertexLabel}; use crate::vector::Vec3; -pub mod cube; pub mod vertex_buffer; #[derive(Debug, Clone)] @@ -15,6 +17,76 @@ pub struct Mesh impl Mesh { + pub fn cube(size: Dimens3<f32>) -> Self + { + let half_w = size.width / 2.0; + let half_h = size.height / 2.0; + let half_d = size.depth / 2.0; + + let vertices = [ + // pos.x, pos.y, pos.z, texture_coords.x, texture_coords.y, normal.x, + // normal.y, normal.z + [half_w, half_h, half_d, 1.00, 1.00, 0.0, 1.00, 0.0], + [-half_w, half_h, half_d, 0.0, 1.00, 0.0, 1.00, 0.0], + [half_w, half_h, -half_d, 1.00, 0.0, 0.0, 1.00, 0.0], + [-half_w, half_h, -half_d, 0.0, 0.0, 0.0, 1.00, 0.0], + [half_w, -half_h, half_d, 1.00, 1.00, 0.0, -1.00, 0.0], + [-half_w, -half_h, half_d, 0.0, 1.00, 0.0, -1.00, 0.0], + [half_w, -half_h, -half_d, 1.00, 0.0, 0.0, -1.00, 0.0], + [-half_w, -half_h, -half_d, 0.0, 0.0, 0.0, -1.00, 0.0], + [-half_w, half_h, half_d, 1.00, 1.00, -1.00, 0.0, 0.0], + [-half_w, half_h, -half_d, 0.0, 1.00, -1.00, 0.0, 0.0], + [-half_w, -half_h, half_d, 1.00, 0.0, -1.00, 0.0, 0.0], + [-half_w, -half_h, -half_d, 0.0, 0.0, -1.00, 0.0, 0.0], + [half_w, half_h, half_d, 1.00, 1.00, 1.00, 0.0, 0.0], + [half_w, half_h, -half_d, 0.0, 1.00, 1.00, 0.0, 0.0], + [half_w, -half_h, half_d, 1.00, 0.0, 1.00, 0.0, 0.0], + [half_w, -half_h, -half_d, 0.0, 0.0, 1.00, 0.0, 0.0], + [half_w, half_h, -half_d, 1.00, 1.00, 0.0, 0.0, -1.00], + [-half_w, half_h, -half_d, 0.0, 1.00, 0.0, 0.0, -1.00], + [half_w, -half_h, -half_d, 1.00, 0.0, 0.0, 0.0, -1.00], + [-half_w, -half_h, -half_d, 0.0, 0.0, 0.0, 0.0, -1.00], + [half_w, half_h, half_d, 1.00, 1.00, 0.0, 0.0, 1.00], + [-half_w, half_h, half_d, 0.0, 1.00, 0.0, 0.0, 1.00], + [half_w, -half_h, half_d, 1.00, 0.0, 0.0, 0.0, 1.00], + [-half_w, -half_h, half_d, 0.0, 0.0, 0.0, 0.0, 1.00], + ]; + + let elements = [ + 0, 1, 2, 1, 3, 2, 4, 5, 6, 5, 7, 6, 8, 9, 10, 9, 11, 10, 12, 13, 14, 13, 15, + 14, 16, 17, 18, 17, 19, 18, 20, 21, 22, 21, 23, 22, + ]; + + Mesh::builder() + .vertices(unsafe { + vertex_buffer::VertexBuffer::from_raw( + &[ + VertexAttrProperties { + label: VertexLabel::Position, + ty: VertexAttrType::Float32Array { length: 3 }, + layout: Layout::new::<[f32; 3]>(), + byte_offset: 0, + }, + VertexAttrProperties { + label: VertexLabel::Uv, + ty: VertexAttrType::Float32Array { length: 2 }, + layout: Layout::new::<[f32; 2]>(), + byte_offset: size_of::<[f32; 3]>(), + }, + VertexAttrProperties { + label: VertexLabel::Normal, + ty: VertexAttrType::Float32Array { length: 3 }, + layout: Layout::new::<[f32; 3]>(), + byte_offset: size_of::<[f32; 3]>() + size_of::<[f32; 2]>(), + }, + ], + vertices.as_bytes().to_vec(), + ) + }) + .indices(elements) + .build() + } + pub fn builder() -> Builder { Builder::default() |
