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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use crate::{GlBind, GlObject, GlTarget, GlAttrib};
use std::ffi::c_void;

static mut CURRENT_VAO: u32 = 0;

pub trait VertexData {
    type Output;
    fn stride() -> usize {
        std::mem::size_of::<Self::Output>()
    }

    fn get_format() -> VertexFormat;
}

#[derive(Default, Debug, PartialEq, Eq, Clone)]
pub struct VertexFormat(Vec<AttribSlot>);

impl VertexFormat {
    pub fn attribs(&self) -> &Vec<AttribSlot> {
        &self.0
    }
}

#[derive(Default, Debug)]
pub struct VertexFormatBuilder {
    location: u32,
    offset: i32,
    format: VertexFormat,
}

impl VertexFormatBuilder {
    pub fn new() -> Self {
        VertexFormatBuilder::default()
    }

    pub fn push<T: GlAttrib>(&mut self, normalized: bool) -> &mut Self {
        let attrib = AttribSlot {
            location: self.location,
            type_: T::gl_enum(),
            size: T::size() as i32,
            normalized,
            offset: self.offset
        };
        self.location += 1;
        self.offset += T::stride() as i32;
        self.format.0.push(attrib);
        self
    }

    pub fn build(&self) -> VertexFormat {
        self.format.clone()
    }
}

#[derive(Default, Debug, PartialEq, Eq, Clone, Copy)]
pub struct AttribSlot {
    pub location: u32,
    pub type_: u32,
    pub size: i32,
    pub normalized: bool,
    pub offset: i32
}

impl AttribSlot {
    pub fn new<T: GlAttrib>(location: u32, normalized: bool, offset: i32) -> AttribSlot {
        AttribSlot {
            location,
            type_: T::gl_enum(),
            size: T::size() as i32,
            normalized,
            offset
        }
    }
}

#[derive(Default, Debug, PartialEq, Eq, Ord, PartialOrd)]
pub struct VertexArray(u32);

impl VertexArray {
    pub fn new() -> Result<VertexArray, String> {
        let mut handle: u32 = 0;
        unsafe { gl::GenVertexArrays(1, &mut handle) };
        Ok(VertexArray(handle))
    }

    pub fn check_bind(&self) {
        let cur_vao = Self::current_bind();
        assert_eq!(cur_vao, self.0);
    }

    pub fn setup_for<T: VertexData>(&self) {
        self.check_bind();
        let format = T::get_format();
        for a in format.0 {
            unsafe {
                gl::EnableVertexAttribArray(a.location);
                gl::VertexAttribPointer(a.location, a.size, a.type_, a.normalized as u8, T::stride() as i32, a.offset as *const c_void);
            }
            println!("{:?}", a);

        }
    }

    pub fn enable_attrib(&self, index: u32) {
        unsafe { gl::EnableVertexAttribArray(index) };
    }

    pub fn attrib_pointer<T: GlAttrib>(&self, index: u32, stride: i32, start: i32) {
        T::setup_attrib(index, false, stride, start);
    }
}

impl GlBind for VertexArray {
    fn bind(&self) {
        unsafe { CURRENT_VAO = self.0 };
        unsafe { gl::BindVertexArray(self.0) };
    }

    fn unbind(&self) {
        unsafe {
            gl::BindVertexArray(0);
            CURRENT_VAO = 0;
        }
    }
}

impl GlTarget for VertexArray {
    fn target() -> u32 { gl::NONE }
    fn binding() -> u32 { gl::VERTEX_ARRAY_BINDING }
    fn current_bind() -> u32 { unsafe { CURRENT_VAO } }
}

impl GlObject for VertexArray {
    fn get_id(&self) -> u32 {
        self.0
    }
}

impl Drop for VertexArray {
    fn drop(&mut self) {
        unsafe { gl::DeleteVertexArrays(1, &self.0) };
    }
}

#[macro_export]
macro_rules! impl_vertexdata {
    ($Name: ident, $($field: ident),+) => {
        #[repr(C)]
        #[derive(Debug, Copy, Clone)]
        pub struct $Name($($field),+);
        
        impl VertexData for $Name {
            type Output = $Name;
            fn get_format() -> VertexFormat {
                VertexFormatBuilder::new()
                    $(.push::<$field>(false))+
                    .build()
            }
        }
    };
}