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 vertex shader and `v_position` in fragment shader.
6pub struct Vertex{
7    pub position: (f32, f32, f32)
8}
9impl Vertex{
10    pub const fn new(x: f32, y: f32, z: f32) -> Self{
11        Vertex { position: (x, y, z) }
12    }
13}
14impl From<Vec3> for Vertex {
15    fn from(value: Vec3) -> Self {
16        Vertex { position: (value.x, value.y, value.z) }
17    }
18}
19implement_vertex!(Vertex, position);
20
21
22
23#[derive(Debug, Clone, Copy)]
24///a normal used for rendering by glium. stores vertex direction. equivelant to `normal` in 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
41
42#[derive(Clone, Copy, Debug)]
43///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.
44pub struct TextureCoords{
45    pub texture_coords: (f32, f32)
46}
47impl From<Vec2> for TextureCoords {
48    fn from(value: Vec2) -> Self {
49        Self { texture_coords: (value.x, value.y) }
50    }
51}
52impl TextureCoords{
53    pub const fn new(u: f32, v: f32) -> Self{
54        Self { texture_coords: (u, v) }
55    }
56}
57implement_vertex!(TextureCoords, texture_coords);
58
59
60
61#[derive(Clone, Copy, Debug)]
62///a vertex colour used for rendering by glium. stores vertex colour. equivelant to `colour` in vertex shader and `v_colour` in fragment shader.
63pub struct VertexColour{
64    pub colour: (f32, f32, f32, f32)
65}
66impl From<Vec4> for VertexColour{
67    fn from(value: Vec4) -> Self {
68        Self { colour: (value.x, value.y, value.z, value.w) }
69    }
70}
71impl VertexColour{
72    pub const fn new(r: f32, g: f32, b: f32, a: f32) -> Self{
73        Self { colour: (r, g, b, a) }
74    }
75}
76implement_vertex!(VertexColour, colour);
77
78
79/// create a triangle mesh from a `Display`, `u32` indices followed by anything that can be stored
80/// in a vertex buffer e.g `Vertex` and/or `TextureCoords`
81/// ```no_run
82/// use glium_types::{mesh, teapot};
83/// let (indices, vertices) = mesh!(
84///     &display, &teapot::INDICES, &teapot::VERTICES
85/// );
86/// ```
87#[macro_export]
88macro_rules! mesh {
89    ($display: expr, $indices: expr, $( $x: expr ),*) => {
90        {
91            use glium::{index::PrimitiveType, IndexBuffer, VertexBuffer};
92            let display = $display;
93            (
94                IndexBuffer::new(display, PrimitiveType::TrianglesList, $indices).unwrap(),
95                $(
96                    VertexBuffer::new(display, $x).unwrap(),
97                )*                
98            )
99        }
100    }
101}