use crate::buffer::StructuredBufferElement;
use crate::types::{VertexBufferLayout, VertexFormat};
use bytemuck::{Pod, Zeroable};
#[derive(Debug, Clone, Copy, Pod, Zeroable)]
#[repr(C)]
pub struct VertexUv {
pub position: [f32; 2],
pub uv: [f32; 2],
}
impl VertexUv {
pub const fn new(x: f32, y: f32, u: f32, v: f32) -> Self {
Self {
position: [x, y],
uv: [u, v],
}
}
pub fn layout() -> VertexBufferLayout {
VertexBufferLayout::from_formats::<Self>(&[
VertexFormat::Float32x2, VertexFormat::Float32x2, ])
}
}
#[derive(Debug, Clone, Copy, Pod, Zeroable)]
#[repr(C)]
pub struct VertexUvTime {
pub position: [f32; 2],
pub uv: [f32; 2],
pub time: f32,
}
impl VertexUvTime {
pub const fn new(x: f32, y: f32, u: f32, v: f32, time: f32) -> Self {
Self {
position: [x, y],
uv: [u, v],
time,
}
}
pub fn layout() -> VertexBufferLayout {
VertexBufferLayout::from_formats::<Self>(&[
VertexFormat::Float32x2, VertexFormat::Float32x2, VertexFormat::Float32, ])
}
}
impl StructuredBufferElement for VertexUv {}
impl StructuredBufferElement for VertexUvTime {}
pub fn create_fullscreen_quad_with_time(time: f32) -> [VertexUvTime; 6] {
[
VertexUvTime::new(-1.0, -1.0, 0.0, 1.0, time),
VertexUvTime::new(1.0, -1.0, 1.0, 1.0, time),
VertexUvTime::new(1.0, 1.0, 1.0, 0.0, time),
VertexUvTime::new(-1.0, -1.0, 0.0, 1.0, time),
VertexUvTime::new(1.0, 1.0, 1.0, 0.0, time),
VertexUvTime::new(-1.0, 1.0, 0.0, 0.0, time),
]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_vertex_uv_layout() {
let layout = VertexUv::layout();
assert_eq!(layout.stride, 16); assert_eq!(layout.attributes.len(), 2);
assert_eq!(layout.attributes[0].offset, 0);
assert_eq!(layout.attributes[1].offset, 8);
}
#[test]
fn test_vertex_uv_time_layout() {
let layout = VertexUvTime::layout();
assert_eq!(layout.stride, 20); assert_eq!(layout.attributes.len(), 3);
assert_eq!(layout.attributes[0].offset, 0);
assert_eq!(layout.attributes[1].offset, 8);
assert_eq!(layout.attributes[2].offset, 16);
}
#[test]
fn test_create_fullscreen_quad_with_time() {
let time = 1.5;
let quad = create_fullscreen_quad_with_time(time);
assert_eq!(quad.len(), 6);
for v in &quad {
assert_eq!(v.time, time);
}
}
}