summaryrefslogtreecommitdiff
path: root/engine/src/vertex.rs
blob: bb0e890dcf246f786d46bd8b103cf1019f93c409 (plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use std::mem::size_of;

use crate::color::Color;
use crate::vector::{Vec2, Vec3};

#[derive(Debug, Clone, Default)]
#[repr(C)]
pub struct Vertex
{
    pos: Vec3<f32>,
    color: Color<f32>,
    texture_coords: Vec2<f32>,
    normal: Vec3<f32>,
}

#[derive(Debug, Default)]
pub struct Builder
{
    pos: Option<Vec3<f32>>,
    color: Option<Color<f32>>,
    texture_coords: Vec2<f32>,
    normal: Option<Vec3<f32>>,
}

impl Builder
{
    #[must_use]
    pub fn new() -> Self
    {
        Self::default()
    }

    #[must_use]
    pub fn pos(mut self, pos: Vec3<f32>) -> Self
    {
        self.pos = Some(pos);

        self
    }

    #[must_use]
    pub fn color(mut self, color: Color<f32>) -> Self
    {
        self.color = Some(color);

        self
    }

    #[must_use]
    pub fn texture_coords(mut self, texture_coords: Vec2<f32>) -> Self
    {
        self.texture_coords = texture_coords;

        self
    }

    #[must_use]
    pub fn normal(mut self, normal: Vec3<f32>) -> Self
    {
        self.normal = Some(normal);

        self
    }

    #[must_use]
    pub fn build(self) -> Option<Vertex>
    {
        let pos = self.pos?;
        let color = self.color.unwrap_or_default();
        let normal = self.normal.unwrap_or_default();

        Some(Vertex {
            pos,
            color,
            texture_coords: self.texture_coords,
            normal,
        })
    }
}

impl Vertex
{
    pub(crate) fn attrs() -> &'static [Attribute]
    {
        &[
            Attribute {
                index: 0,
                component_type: AttributeComponentType::Float,
                component_cnt: AttributeComponentCnt::Three,
                component_size: size_of::<f32>(),
            },
            Attribute {
                index: 1,
                component_type: AttributeComponentType::Float,
                component_cnt: AttributeComponentCnt::Three,
                component_size: size_of::<f32>(),
            },
            Attribute {
                index: 2,
                component_type: AttributeComponentType::Float,
                component_cnt: AttributeComponentCnt::Two,
                component_size: size_of::<f32>(),
            },
            Attribute {
                index: 3,
                component_type: AttributeComponentType::Float,
                component_cnt: AttributeComponentCnt::Three,
                component_size: size_of::<f32>(),
            },
        ]
    }
}

pub(crate) struct Attribute
{
    pub(crate) index: usize,
    pub(crate) component_type: AttributeComponentType,
    pub(crate) component_cnt: AttributeComponentCnt,
    pub(crate) component_size: usize,
}

pub(crate) enum AttributeComponentType
{
    Float,
}

#[derive(Debug, Clone, Copy)]
#[repr(u32)]
#[allow(dead_code)]
pub(crate) enum AttributeComponentCnt
{
    One = 1,
    Two = 2,
    Three = 3,
    Four = 4,
}