glium_types/
vert_types.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use glium::implement_vertex;
use crate::prelude::*;

#[derive(Debug, Clone, Copy)]
///a vertex used for rendering by glium. stores vertex position. equivelant to `position` in vertex shader and `v_position` in fragment shader.
pub struct Vertex{
    pub position: (f32, f32, f32)
}
impl Vertex{
    pub const fn new(x: f32, y: f32, z: f32) -> Self{
        Vertex { position: (x, y, z) }
    }
}
impl From<Vec3> for Vertex {
    fn from(value: Vec3) -> Self {
        Vertex { position: (value.x, value.y, value.z) }
    }
}
implement_vertex!(Vertex, position);



#[derive(Debug, Clone, Copy)]
///a normal used for rendering by glium. stores vertex direction. equivelant to `normal` in vertex shader and `v_normal` in fragment shader.
pub struct Normal{
    pub normal: (f32, f32, f32)
}
impl From<Vec3> for Normal{
    fn from(value: Vec3) -> Self {
        Self { normal: (value.x, value.y, value.z) }
    }
}
impl Normal{
    pub const fn new(x: f32, y: f32, z: f32) -> Self{
        Self { normal: (x, y, z) }
    }
}
implement_vertex!(Normal, normal);



#[derive(Clone, Copy, Debug)]
///a texture coordinate used for rendering by glium. also called a uv. stores uvs. equivelant to `texture_coords` in vertex shader and `uv` in fragment shader.
pub struct TextureCoords{
    pub texture_coords: (f32, f32)
}
impl From<Vec2> for TextureCoords {
    fn from(value: Vec2) -> Self {
        Self { texture_coords: (value.x, value.y) }
    }
}
impl TextureCoords{
    pub const fn new(u: f32, v: f32) -> Self{
        Self { texture_coords: (u, v) }
    }
}
implement_vertex!(TextureCoords, texture_coords);



#[derive(Clone, Copy, Debug)]
///a vertex colour used for rendering by glium. stores vertex colour. equivelant to `colour` in vertex shader and `v_colour` in fragment shader.
pub struct VertexColour{
    pub colour: (f32, f32, f32, f32)
}
impl From<Vec4> for VertexColour{
    fn from(value: Vec4) -> Self {
        Self { colour: (value.x, value.y, value.z, value.w) }
    }
}
impl VertexColour{
    pub const fn new(r: f32, g: f32, b: f32, a: f32) -> Self{
        Self { colour: (r, g, b, a) }
    }
}
implement_vertex!(VertexColour, colour);


/// create a triangle mesh from a `Display`, `u32` indices followed by anything that can be stored
/// in a vertex buffer e.g `Vertex` and/or `TextureCoords`
/// ```no_run
/// use glium_types::{mesh, teapot};
/// let (indices, vertices) = mesh!(
///     &display, &teapot::INDICES, &teapot::VERTICES
/// );
/// ```
#[macro_export]
macro_rules! mesh {
    ($display: expr, $indices: expr, $( $x: expr ),*) => {
        {
            use glium::{index::PrimitiveType, IndexBuffer, VertexBuffer};
            let display = $display;
            (
                IndexBuffer::new(display, PrimitiveType::TrianglesList, $indices).unwrap(),
                $(
                    VertexBuffer::new(display, $x).unwrap(),
                )*                
            )
        }
    }
}