Skip to content

Commit

Permalink
allow specifying subdivision on all axes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bendzae committed Oct 5, 2024
1 parent 006b83e commit 3f5d0d1
Showing 1 changed file with 40 additions and 36 deletions.
76 changes: 40 additions & 36 deletions src/smesh/primitives.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::collections::HashMap;

use glam::vec3;
use glam::{vec3, U16Vec3};

use crate::prelude::*;

pub struct Cube {
pub subdivisions: usize,
pub subdivision: U16Vec3,
}

pub struct CubeData {
Expand All @@ -14,22 +14,26 @@ pub struct CubeData {

impl Cube {
pub fn generate(self) -> SMeshResult<(SMesh, CubeData)> {
let n = self.subdivisions;
let delta = 1.0 / (n as f32);
let n_x = self.subdivision.x;
let n_y = self.subdivision.y;
let n_z = self.subdivision.z;
let delta_x = 1.0 / (n_x as f32);
let delta_y = 1.0 / (n_y as f32);
let delta_z = 1.0 / (n_z as f32);

// Construct SMesh
let mut smesh = SMesh::new();
let mut vertex_indices = HashMap::new();

// Generate vertices on the cube's surface
for x in 0..=n {
for y in 0..=n {
for z in 0..=n {
for x in 0..=n_x {
for y in 0..=n_y {
for z in 0..=n_z {
// Only add vertices on the surfaces (where x, y, or z is 0 or n)
if x == 0 || x == n || y == 0 || y == n || z == 0 || z == n {
let pos_x = -0.5 + (x as f32) * delta;
let pos_y = -0.5 + (y as f32) * delta;
let pos_z = -0.5 + (z as f32) * delta;
if x == 0 || x == n_x || y == 0 || y == n_y || z == 0 || z == n_z {
let pos_x = -0.5 + (x as f32) * delta_x;
let pos_y = -0.5 + (y as f32) * delta_y;
let pos_z = -0.5 + (z as f32) * delta_z;
let position = vec3(pos_x, pos_y, pos_z);
let vertex = smesh.add_vertex(position);
vertex_indices.insert((x, y, z), vertex);
Expand All @@ -43,19 +47,19 @@ impl Cube {

// Generate faces for each side of the cube with correct winding order
// Front face (z = n)
for x in 0..n {
for y in 0..n {
let v0 = get_vertex(x, y, n);
let v1 = get_vertex(x + 1, y, n);
let v2 = get_vertex(x + 1, y + 1, n);
let v3 = get_vertex(x, y + 1, n);
for x in 0..n_x {
for y in 0..n_y {
let v0 = get_vertex(x, y, n_z);
let v1 = get_vertex(x + 1, y, n_z);
let v2 = get_vertex(x + 1, y + 1, n_z);
let v3 = get_vertex(x, y + 1, n_z);
smesh.make_face(vec![v0, v1, v2, v3])?; // Correct winding
}
}

// Back face (z = 0)
for x in 0..n {
for y in 0..n {
for x in 0..n_x {
for y in 0..n_y {
let v0 = get_vertex(x, y, 0);
let v1 = get_vertex(x, y + 1, 0);
let v2 = get_vertex(x + 1, y + 1, 0);
Expand All @@ -65,8 +69,8 @@ impl Cube {
}

// Left face (x = 0)
for y in 0..n {
for z in 0..n {
for y in 0..n_y {
for z in 0..n_z {
let v0 = get_vertex(0, y, z);
let v1 = get_vertex(0, y, z + 1);
let v2 = get_vertex(0, y + 1, z + 1);
Expand All @@ -76,30 +80,30 @@ impl Cube {
}

// Right face (x = n)
for y in 0..n {
for z in 0..n {
let v0 = get_vertex(n, y, z);
let v1 = get_vertex(n, y + 1, z);
let v2 = get_vertex(n, y + 1, z + 1);
let v3 = get_vertex(n, y, z + 1);
for y in 0..n_y {
for z in 0..n_z {
let v0 = get_vertex(n_x, y, z);
let v1 = get_vertex(n_x, y + 1, z);
let v2 = get_vertex(n_x, y + 1, z + 1);
let v3 = get_vertex(n_x, y, z + 1);
smesh.make_face(vec![v0, v1, v2, v3])?; // Corrected winding
}
}

// Top face (y = n)
for x in 0..n {
for z in 0..n {
let v0 = get_vertex(x, n, z);
let v1 = get_vertex(x, n, z + 1);
let v2 = get_vertex(x + 1, n, z + 1);
let v3 = get_vertex(x + 1, n, z);
for x in 0..n_x {
for z in 0..n_z {
let v0 = get_vertex(x, n_y, z);
let v1 = get_vertex(x, n_y, z + 1);
let v2 = get_vertex(x + 1, n_y, z + 1);
let v3 = get_vertex(x + 1, n_y, z);
smesh.make_face(vec![v0, v1, v2, v3])?; // Corrected winding
}
}

// Bottom face (y = 0)
for x in 0..n {
for z in 0..n {
for x in 0..n_x {
for z in 0..n_z {
let v0 = get_vertex(x, 0, z);
let v1 = get_vertex(x + 1, 0, z);
let v2 = get_vertex(x + 1, 0, z + 1);
Expand All @@ -112,7 +116,7 @@ impl Cube {
Ok((
smesh,
CubeData {
front_bottom_left_vertex: get_vertex(0, 0, n),
front_bottom_left_vertex: get_vertex(0, 0, n_z),
},
))
}
Expand Down

0 comments on commit 3f5d0d1

Please sign in to comment.