midpoint_ui/renderer/shapes/
Pyramid.rs

1use nalgebra::{Matrix4, Point3, Vector3};
2use wgpu::util::DeviceExt;
3
4use crate::renderer::{
5    core::Vertex,
6    Transform::{matrix4_to_raw_array, Transform},
7};
8
9// Vertices for a pyramid
10const VERTICES: &[Vertex] = &[
11    Vertex {
12        position: [0.0, 1.0, 0.0],
13        normal: [0.0, 0.0, 0.0],
14        tex_coords: [0.0, 0.0],
15        color: [1.0, 0.0, 0.0],
16    }, // Apex
17    Vertex {
18        position: [-1.0, -1.0, -1.0],
19        normal: [0.0, 0.0, 0.0],
20        tex_coords: [0.0, 0.0],
21        color: [0.0, 1.0, 0.0],
22    }, // Base vertices
23    Vertex {
24        position: [1.0, -1.0, -1.0],
25        normal: [0.0, 0.0, 0.0],
26        tex_coords: [0.0, 0.0],
27        color: [0.0, 0.0, 1.0],
28    },
29    Vertex {
30        position: [1.0, -1.0, 1.0],
31        normal: [0.0, 0.0, 0.0],
32        tex_coords: [0.0, 0.0],
33        color: [1.0, 1.0, 0.0],
34    },
35    Vertex {
36        position: [-1.0, -1.0, 1.0],
37        normal: [0.0, 0.0, 0.0],
38        tex_coords: [0.0, 0.0],
39        color: [0.0, 1.0, 1.0],
40    },
41];
42
43// Indices for a pyramid
44const INDICES: &[u16] = &[
45    0, 1, 2, // Side 1
46    0, 2, 3, // Side 2
47    0, 3, 4, // Side 3
48    0, 4, 1, // Side 4
49    1, 3, 2, // Base 1
50    1, 4, 3, // Base 2
51];
52
53pub struct Pyramid {
54    // position: Vector3<f32>,
55    // rotation: Vector3<f32>,
56    // scale: Vector3<f32>,
57    vertex_buffer: wgpu::Buffer,
58    index_buffer: wgpu::Buffer,
59    // uniform_buffer: wgpu::Buffer,
60    bind_group: wgpu::BindGroup,
61    transform: Transform,
62}
63
64impl Pyramid {
65    pub fn new(device: &wgpu::Device, bind_group_layout: &wgpu::BindGroupLayout) -> Self {
66        let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
67            label: Some("Pyramid Vertex Buffer"),
68            contents: bytemuck::cast_slice(VERTICES),
69            usage: wgpu::BufferUsages::VERTEX,
70        });
71
72        let index_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
73            label: Some("Pyramid Index Buffer"),
74            contents: bytemuck::cast_slice(INDICES),
75            usage: wgpu::BufferUsages::INDEX,
76        });
77
78        let empty_buffer = Matrix4::<f32>::identity();
79        let raw_matrix = matrix4_to_raw_array(&empty_buffer);
80
81        let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
82            label: Some("Pyramid Uniform Buffer"),
83            contents: bytemuck::cast_slice(&raw_matrix),
84            usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
85        });
86
87        let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
88            layout: &bind_group_layout,
89            entries: &[wgpu::BindGroupEntry {
90                binding: 0,
91                resource: uniform_buffer.as_entire_binding(),
92            }],
93            label: None,
94        });
95
96        Self {
97            // position: Vector3::new(0.0, 0.0, 0.0),
98            // rotation: Vector3::new(0.0, 0.0, 0.0),
99            // scale: Vector3::new(1.0, 1.0, 1.0),
100            vertex_buffer,
101            index_buffer,
102            // uniform_buffer,
103            bind_group,
104            transform: Transform::new(
105                Vector3::new(0.0, 0.0, 0.0),
106                Vector3::new(0.0, 0.0, 0.0),
107                Vector3::new(1.0, 1.0, 1.0),
108                uniform_buffer,
109            ),
110        }
111    }
112}