VoxelEngine2/src/primitives/cube.rs
2024-06-02 15:25:11 +02:00

130 lines
No EOL
4.9 KiB
Rust

use vulkanalia::prelude::v1_0::*;
use cgmath::{vec2, vec3, Matrix, SquareMatrix};
use crate::vertex;
use crate::scene::Scene;
#[derive(Clone, Debug)]
pub struct Cube{
pub pos: vertex::Vec3,
pub color: vertex::Vec3,
pub tex_coord: vertex::Vec2
}
impl Cube {
pub fn draw(& self, topology: &vk::PrimitiveTopology, start_index: usize, scene: &mut Scene) {
if *topology == vk::PrimitiveTopology::TRIANGLE_LIST {
// 0 top left far
scene.vertices.push(vertex::Vertex::new(
vec3(self.pos.x as f32 - 0.5, self.pos.y as f32 + 0.5, self.pos.z as f32 + 0.5),
self.color,
self.tex_coord
));
// 1 top right far
scene.vertices.push(vertex::Vertex::new(
vec3(self.pos.x as f32 + 0.5, self.pos.y as f32 + 0.5, self.pos.z as f32 + 0.5),
self.color,
self.tex_coord
));
// 2 top left near
scene.vertices.push(vertex::Vertex::new(
vec3(self.pos.x as f32 - 0.5, self.pos.y as f32 - 0.5, self.pos.z as f32 + 0.5),
self.color,
self.tex_coord
));
// 3 top right near
scene.vertices.push(vertex::Vertex::new(
vec3(self.pos.x as f32 + 0.5, self.pos.y as f32 - 0.5, self.pos.z as f32 + 0.5),
self.color,
self.tex_coord
));
// 4 bottom left far
scene.vertices.push(vertex::Vertex::new(
vec3(self.pos.x as f32 - 0.5, self.pos.y as f32 + 0.5, self.pos.z as f32 - 0.5),
self.color,
self.tex_coord
));
// 5 bottom right far
scene.vertices.push(vertex::Vertex::new(
vec3(self.pos.x as f32 + 0.5, self.pos.y as f32 + 0.5, self.pos.z as f32 - 0.5),
self.color,
self.tex_coord
));
// 6 bottom left near
scene.vertices.push(vertex::Vertex::new(
vec3(self.pos.x as f32 - 0.5, self.pos.y as f32 - 0.5, self.pos.z as f32 - 0.5),
self.color,
self.tex_coord
));
// 7 bottom right near
scene.vertices.push(vertex::Vertex::new(
vec3(self.pos.x as f32 + 0.5, self.pos.y as f32 - 0.5, self.pos.z as f32 - 0.5),
self.color,
self.tex_coord
));
// top
scene.indices.push(start_index as u32 + 3);
scene.indices.push(start_index as u32 + 0);
scene.indices.push(start_index as u32 + 2);
scene.indices.push(start_index as u32 + 3);
scene.indices.push(start_index as u32 + 1);
scene.indices.push(start_index as u32 + 0);
// bottom
scene.indices.push(start_index as u32 + 6);
scene.indices.push(start_index as u32 + 4);
scene.indices.push(start_index as u32 + 7);
scene.indices.push(start_index as u32 + 4);
scene.indices.push(start_index as u32 + 5);
scene.indices.push(start_index as u32 + 7);
// left
scene.indices.push(start_index as u32 + 0);
scene.indices.push(start_index as u32 + 4);
scene.indices.push(start_index as u32 + 2);
scene.indices.push(start_index as u32 + 6);
scene.indices.push(start_index as u32 + 2);
scene.indices.push(start_index as u32 + 4);
// right
scene.indices.push(start_index as u32 + 1);
scene.indices.push(start_index as u32 + 3);
scene.indices.push(start_index as u32 + 5);
scene.indices.push(start_index as u32 + 5);
scene.indices.push(start_index as u32 + 3);
scene.indices.push(start_index as u32 + 7);
// near
scene.indices.push(start_index as u32 + 6);
scene.indices.push(start_index as u32 + 3);
scene.indices.push(start_index as u32 + 2);
scene.indices.push(start_index as u32 + 3);
scene.indices.push(start_index as u32 + 6);
scene.indices.push(start_index as u32 + 7);
// far
scene.indices.push(start_index as u32 + 0);
scene.indices.push(start_index as u32 + 1);
scene.indices.push(start_index as u32 + 4);
scene.indices.push(start_index as u32 + 5);
scene.indices.push(start_index as u32 + 4);
scene.indices.push(start_index as u32 + 1);
}
if *topology == vk::PrimitiveTopology::POINT_LIST {
scene.vertices.push(vertex::Vertex::new(
self.pos,
self.color,
self.tex_coord
));
scene.indices.push(start_index as u32);
}
}
}