76 lines
2.2 KiB
Rust
76 lines
2.2 KiB
Rust
use macroquad::prelude as mq;
|
|
|
|
pub fn draw_cuboid(center: mq::Vec3, size: mq::Vec3, color: mq::Color) {
|
|
use mq::{Vec2, Vec3, Vertex};
|
|
let hs = size * 0.5;
|
|
let mut positions = [
|
|
Vec3::new(-hs.x, -hs.y, -hs.z),
|
|
Vec3::new(hs.x, -hs.y, -hs.z),
|
|
Vec3::new(hs.x, hs.y, -hs.z),
|
|
Vec3::new(-hs.x, hs.y, -hs.z),
|
|
Vec3::new(-hs.x, -hs.y, hs.z),
|
|
Vec3::new(hs.x, -hs.y, hs.z),
|
|
Vec3::new(hs.x, hs.y, hs.z),
|
|
Vec3::new(-hs.x, hs.y, hs.z),
|
|
];
|
|
for i in 0..positions.len() {
|
|
positions[i] += center;
|
|
}
|
|
|
|
let faces = [
|
|
([0, 1, 2, 3], Vec3::new(0.0, 0.0, -1.0)), // Back
|
|
([5, 4, 7, 6], Vec3::new(0.0, 0.0, 1.0)), // Front
|
|
([4, 0, 3, 7], Vec3::new(-1.0, 0.0, 0.0)), // Left
|
|
([1, 5, 6, 2], Vec3::new(1.0, 0.0, 0.0)), // Right
|
|
([3, 2, 6, 7], Vec3::new(0.0, 1.0, 0.0)), // Up
|
|
([4, 5, 1, 0], Vec3::new(0.0, -1.0, 0.0)), // Down
|
|
];
|
|
|
|
let uvs = [
|
|
Vec2::new(0.0, 0.0),
|
|
Vec2::new(1.0, 0.0),
|
|
Vec2::new(1.0, 1.0),
|
|
Vec2::new(0.0, 1.0),
|
|
];
|
|
|
|
let mut vertices = [Vertex {
|
|
position: mq::Vec3::ZERO,
|
|
uv: mq::Vec2::ZERO,
|
|
color: color.into(),
|
|
normal: mq::Vec4::ZERO,
|
|
}; 24];
|
|
|
|
let mut vi = 0;
|
|
for (face, normal) in faces.iter() {
|
|
for (j, &corner) in face.iter().enumerate() {
|
|
let pos = positions[corner];
|
|
vertices[vi] = Vertex {
|
|
position: mq::vec3(pos.x, pos.y, pos.z),
|
|
uv: mq::vec2(uvs[j].x, uvs[j].y),
|
|
color: color.into(),
|
|
normal: mq::vec4(normal.x, normal.y, normal.z, 0.0),
|
|
};
|
|
vi += 1;
|
|
}
|
|
}
|
|
|
|
let mut indices = [0u16; 36];
|
|
for i in 0..6 {
|
|
let base = (i * 4) as u16;
|
|
let offset = i * 6;
|
|
indices[offset + 0] = base;
|
|
indices[offset + 1] = base + 1;
|
|
indices[offset + 2] = base + 2;
|
|
indices[offset + 3] = base;
|
|
indices[offset + 4] = base + 2;
|
|
indices[offset + 5] = base + 3;
|
|
}
|
|
|
|
unsafe {
|
|
mq::get_internal_gl()
|
|
.quad_gl
|
|
.draw_mode(mq::DrawMode::Triangles);
|
|
mq::get_internal_gl().quad_gl.geometry(&vertices, &indices);
|
|
}
|
|
}
|