Compare commits
No commits in common. "6b25722627f4ab53988de33c8945951d13e975e7" and "c1b505356cd2f425ab1a12bb004eceabb08cb144" have entirely different histories.
6b25722627
...
c1b505356c
|
|
@ -4,8 +4,6 @@ use bytemuck::{bytes_of, cast_slice, Pod, Zeroable};
|
|||
use glam::{Vec3, Vec4};
|
||||
use wgpu::util::DeviceExt as _;
|
||||
|
||||
use crate::scene::Face;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, Debug, Pod, Zeroable)]
|
||||
struct Vertex {
|
||||
|
|
@ -13,15 +11,6 @@ struct Vertex {
|
|||
pub normal: [f32; 3],
|
||||
}
|
||||
|
||||
impl From<crate::scene::Vertex> for Vertex {
|
||||
fn from(value: crate::scene::Vertex) -> Self {
|
||||
Vertex {
|
||||
position: value.position.into(),
|
||||
normal: value.normal.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, Pod, Zeroable)]
|
||||
struct PushConsts {
|
||||
|
|
@ -48,8 +37,27 @@ pub struct Mesh {
|
|||
}
|
||||
|
||||
impl Mesh {
|
||||
pub fn new_list(device: &wgpu::Device, attrs: Attrs, tris: Vec<Face>) -> Self {
|
||||
let data: Vec<Vertex> = tris.into_iter().flat_map(|face| face.map(Into::into)).collect();
|
||||
pub fn new_list(device: &wgpu::Device, attrs: Attrs, tris: Vec<(Vec3, Vec3, Vec3)>) -> Self {
|
||||
let data: Vec<Vertex> = tris
|
||||
.into_iter()
|
||||
.flat_map(|(a, b, c)| {
|
||||
let n = (b - a).cross(c - a).normalize();
|
||||
[
|
||||
Vertex {
|
||||
position: a.into(),
|
||||
normal: n.into(),
|
||||
},
|
||||
Vertex {
|
||||
position: b.into(),
|
||||
normal: n.into(),
|
||||
},
|
||||
Vertex {
|
||||
position: c.into(),
|
||||
normal: n.into(),
|
||||
},
|
||||
]
|
||||
})
|
||||
.collect();
|
||||
let buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("Mesh Vertex Buffer"),
|
||||
contents: cast_slice(&data),
|
||||
|
|
|
|||
|
|
@ -17,12 +17,7 @@ pub struct FancyLine {
|
|||
pub pts: Vec<Ray>,
|
||||
}
|
||||
|
||||
pub struct Vertex {
|
||||
pub position: Vec3,
|
||||
pub normal: Vec3,
|
||||
}
|
||||
|
||||
pub type Face = [Vertex; 3];
|
||||
pub type Face = (Vec3, Vec3, Vec3);
|
||||
|
||||
pub enum Mesh {
|
||||
List(Vec<Face>),
|
||||
|
|
@ -165,44 +160,29 @@ impl Renderable for Tube {
|
|||
let d = Vec2::from_angle(k as f32 * step);
|
||||
vec3(d.x, 0., d.y)
|
||||
};
|
||||
|
||||
let side = vec3(0., self.external_halflength, 0.);
|
||||
let r1 = self.inner_radius;
|
||||
let r2 = self.outer_radius;
|
||||
|
||||
let vertex = |k, r, h| {
|
||||
let n = dir(k);
|
||||
Vertex {
|
||||
position: r * n + h * side,
|
||||
normal: n,
|
||||
}
|
||||
};
|
||||
let inner = (0..sides).flat_map(|k| {
|
||||
[
|
||||
[vertex(k, -r1, -1.), vertex(k, -r1, 1.), vertex(k + 1, -r1, -1.)],
|
||||
[vertex(k + 1, -r1, -1.), vertex(k, -r1, 1.), vertex(k + 1, -r1, 1.)],
|
||||
]
|
||||
let a = r1 * dir(k);
|
||||
let b = r1 * dir(k + 1);
|
||||
[(a - side, b - side, a + side), (b - side, b + side, a + side)]
|
||||
});
|
||||
let outer = (0..sides).flat_map(|k| {
|
||||
[
|
||||
[vertex(k, r2, -1.), vertex(k + 1, r2, -1.), vertex(k, r2, 1.)],
|
||||
[vertex(k + 1, r2, -1.), vertex(k + 1, r2, 1.), vertex(k, r2, 1.)],
|
||||
]
|
||||
let a = r2 * dir(k);
|
||||
let b = r2 * dir(k + 1);
|
||||
[(a - side, b - side, a + side), (b - side, b + side, a + side)]
|
||||
});
|
||||
|
||||
let vertex = |k, r, h| {
|
||||
let n = dir(k);
|
||||
Vertex {
|
||||
position: r * n + h * side,
|
||||
normal: h * Vec3::Y,
|
||||
}
|
||||
};
|
||||
let cap = (0..sides).flat_map(|k| {
|
||||
let a = r1 * dir(k);
|
||||
let b = r1 * dir(k + 1);
|
||||
let c = r2 * dir(k + 1);
|
||||
let d = r2 * dir(k);
|
||||
[
|
||||
[vertex(k, r1, -1.), vertex(k + 1, r1, -1.), vertex(k + 1, r2, -1.)],
|
||||
[vertex(k, r1, -1.), vertex(k + 1, r2, -1.), vertex(k, r2, -1.)],
|
||||
[vertex(k, r1, 1.), vertex(k + 1, r1, 1.), vertex(k + 1, r2, 1.)],
|
||||
[vertex(k, r1, 1.), vertex(k + 1, r2, 1.), vertex(k, r2, 1.)],
|
||||
(a - side, b - side, c - side),
|
||||
(a - side, c - side, d - side),
|
||||
(a + side, b + side, c + side),
|
||||
(a + side, c + side, d + side),
|
||||
]
|
||||
});
|
||||
chain!(inner, outer, cap).collect()
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user