glium_types/
vert_types.rs

1use glium::implement_vertex;
2use crate::prelude::*;
3
4#[derive(Debug, Clone, Copy)]
5/// a vertex used for rendering by glium. stores vertex position. equivelant to `position` in the supplied
6/// vertex shader and `v_position` in fragment shader.
7pub struct Vertex{
8    pub position: (f32, f32, f32)
9}
10impl Vertex {
11    pub const fn new(x: f32, y: f32, z: f32) -> Self {
12        Vertex { position: (x, y, z) }
13    }
14}
15impl From<Vec3> for Vertex {
16    fn from(value: Vec3) -> Self {
17        Vertex { position: (value.x, value.y, value.z) }
18    }
19}
20implement_vertex!(Vertex, position);
21
22#[derive(Debug, Clone, Copy)]
23/// a normal used for rendering by glium. stores vertex direction. equivelant to `normal` in the supplied
24/// vertex shader and `v_normal` in fragment shader.
25pub struct Normal {
26    pub normal: (f32, f32, f32)
27}
28impl From<Vec3> for Normal {
29    fn from(value: Vec3) -> Self {
30        Self { normal: (value.x, value.y, value.z) }
31    }
32}
33impl Normal{
34    pub const fn new(x: f32, y: f32, z: f32) -> Self{
35        Self { normal: (x, y, z) }
36    }
37}
38implement_vertex!(Normal, normal);
39
40#[derive(Clone, Copy, Debug)]
41/// a texture coordinate used for rendering by glium. also called a uv. stores uvs. equivelant to
42/// `texture_coords` in the supplied vertex shader and `uv` in fragment shader.
43pub struct TextureCoords{
44    pub texture_coords: (f32, f32)
45}
46impl From<Vec2> for TextureCoords {
47    fn from(value: Vec2) -> Self {
48        Self { texture_coords: (value.x, value.y) }
49    }
50}
51impl TextureCoords{
52    pub const fn new(u: f32, v: f32) -> Self{
53        Self { texture_coords: (u, v) }
54    }
55}
56implement_vertex!(TextureCoords, texture_coords);
57
58#[derive(Clone, Copy, Debug)]
59/// a vertex colour used for rendering by glium. stores vertex colour. equivelant to `colour` in the supplied
60/// vertex shader and `v_colour` in fragment shader.
61pub struct VertexColour{
62    pub colour: (f32, f32, f32, f32)
63}
64impl From<Vec4> for VertexColour{
65    fn from(value: Vec4) -> Self {
66        Self { colour: (value.x, value.y, value.z, value.w) }
67    }
68}
69impl VertexColour{
70    pub const fn new(r: f32, g: f32, b: f32, a: f32) -> Self{
71        Self { colour: (r, g, b, a) }
72    }
73}
74implement_vertex!(VertexColour, colour);
75
76#[derive(Debug)]
77pub enum MeshError {
78    VertexErr(glium::vertex::BufferCreationError),
79    IndexErr(glium::index::BufferCreationError)
80}
81impl From<glium::vertex::BufferCreationError> for MeshError {
82    fn from(e: glium::vertex::BufferCreationError) -> Self { Self::VertexErr(e) }
83}
84impl From<glium::index::BufferCreationError> for MeshError {
85    fn from(e: glium::index::BufferCreationError) -> Self { Self::IndexErr(e) }
86}
87use glium::{index::*, vertex::*, backend::Facade};
88pub fn load_mesh<const X: usize, F, V, I>(facade: &F, vertices: [&[impl glium::Vertex + std::fmt::Debug]; X], index: &[I]) -> Result<([VertexBuffer<impl glium::Vertex>; X], IndexBuffer<I>), MeshError>
89where F: Facade + ?Sized, V: glium::Vertex + Copy, I: Index {
90    let mut vertex = Vec::new();
91    for vertices in vertices {
92        vertex.push(VertexBuffer::new(facade, vertices)?);
93    }
94    
95    let index = IndexBuffer::new(facade, PrimitiveType::TrianglesList, index)?;
96    Ok((vertex.try_into().unwrap(), index))
97}
98/// create a triangle mesh from a `Display`, `u32` indices followed by anything that can be stored
99/// in a vertex buffer e.g `Vertex` and/or `TextureCoords`.
100/// if an error occurs it returns the `MeshError` enum
101/// ```no_run
102/// use glium_types::{mesh, teapot};
103/// let (indices, vertices, normals) = mesh!(
104///     &display, &teapot::INDICES, &teapot::VERTICES, &teapot::NORMALS
105/// ).unwrap();
106/// ```
107#[macro_export]
108macro_rules! mesh {
109    ($display: expr, $indices: expr, $( $x: expr ),*) => {
110        {
111           let result: Result<_, $crate::vert_types::MeshError> = 'block: {
112                use $crate::glium::{index::PrimitiveType, IndexBuffer, VertexBuffer};
113
114                Ok((
115                    match IndexBuffer::new($display, $crate::glium::index::PrimitiveType::TrianglesList, $indices) {
116                        Err(e) => break 'block Err($crate::vert_types::MeshError::IndexErr(e)),
117                        Ok(v) => v,
118                    },
119                    $(
120                        match VertexBuffer::new($display, $x) {
121                            Err(e) => break 'block Err($crate::vert_types::MeshError::VertexErr(e)),
122                            Ok(v) => v,
123                        },
124                    )*
125                ))
126            };
127            result
128        }
129    }
130}