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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
use std::convert::TryFrom;

pub struct VertArray {
    id: u32,
    vert_attr_index: u32,
    index_buf: IndexBuf,
}

// https://stackoverflow.com/questions/26552642/when-is-what-bound-to-a-vao
impl VertArray {
    pub fn new(vert_bufs: &[&dyn VertBuffer], index_buf: IndexBuf) -> Self {
        let mut id: u32 = 0;
        gl_call!(gl::GenVertexArrays(1, &mut id));
        gl_call!(gl::BindVertexArray(id));
        index_buf.bind();
        let mut vert_attr_index = 0u32;
        for vert_buf in vert_bufs {
            vert_buf.bind();
            let layout = vert_buf.layout();
            // TODO: check if layout is empty and assert error if so.
            for attr in layout.attrs.iter() {
                match attr.attr_type {
                    VertAttrType::Mat4 => {
                        for index in 0..4 {
                            let attr_index = vert_attr_index + index;
                            gl_call!(gl::EnableVertexAttribArray(attr_index));
                            // TODO: seemingly, the component count is always 4, could hardwire it: https://stackoverflow.com/questions/23629776/setup-of-matrix-for-instance-shader/23639372#23639372
                            let component_count = 4;
                            gl_call!(gl::VertexAttribPointer(
                                attr_index,
                                component_count as i32,
                                attr.gl_data_type(),
                                if attr.normalized { gl::TRUE } else { gl::FALSE },
                                layout.stride as i32,
                                (attr.offset + attr.gl_data_type_size() * 4 * index as usize)
                                    as *const gl::types::GLvoid,
                            ));
                            // FIXME: this should only run if attr is for instancing
                            gl_call!(gl::VertexAttribDivisor(attr_index, 1));
                        }
                        vert_attr_index += 4;
                    }
                    VertAttrType::Mat3 => {
                        for index in 0..3 {
                            let attr_index = vert_attr_index + index;
                            gl_call!(gl::EnableVertexAttribArray(attr_index));
                            let component_count = 4;
                            // XXX: seems like mat3 is interpreted as literally:
                            // layout(location = 7) in vec3 in_norm_mat[0];
                            // layout(location = 8) in vec3 in_norm_mat[3];
                            // layout(location = 9) in vec3 in_norm_mat[6];
                            // so offsets needs to be [3, 3, 3], not [4, 4, 1]
                            let offset =
                                attr.offset + attr.gl_data_type_size() * 3 * index as usize;
                            gl_call!(gl::VertexAttribPointer(
                                attr_index,
                                component_count as i32,
                                attr.gl_data_type(),
                                if attr.normalized { gl::TRUE } else { gl::FALSE },
                                layout.stride as i32,
                                offset as *const gl::types::GLvoid,
                            ));
                            // FIXME: this should only run if attr is for instancing
                            gl_call!(gl::VertexAttribDivisor(attr_index, 1));
                        }
                        vert_attr_index += 3;
                    }
                    _ => {
                        gl_call!(gl::EnableVertexAttribArray(vert_attr_index));
                        gl_call!(gl::VertexAttribPointer(
                            vert_attr_index,
                            attr.count() as i32,
                            attr.gl_data_type(),
                            if attr.normalized { gl::TRUE } else { gl::FALSE },
                            layout.stride as i32,
                            attr.offset as *const gl::types::GLvoid,
                        ));
                        vert_attr_index += 1;
                    }
                }
            }
            vert_buf.unbind();
        }
        gl_call!(gl::BindVertexArray(0));
        index_buf.unbind();
        VertArray {
            id,
            vert_attr_index,
            index_buf,
        }
    }
    pub fn bind(&self) {
        gl_call!(gl::BindVertexArray(self.id));
    }
    pub fn unbind(&self) {
        gl_call!(gl::BindVertexArray(0));
    }
    pub fn index_buf(&self) -> &IndexBuf {
        &self.index_buf
    }
    pub fn index_buf_mut(&mut self) -> &mut IndexBuf {
        &mut self.index_buf
    }
    pub fn set_index_buf(&mut self, index_buf: IndexBuf) {
        self.bind();
        index_buf.bind();
        self.unbind();
        index_buf.unbind();
        self.index_buf = index_buf;
    }
    pub fn push_buf(&mut self, vert_buf: &dyn VertBuffer) {
        self.bind();
        vert_buf.bind();
        let layout = vert_buf.layout();
        for attr in layout.attrs.iter() {
            gl_call!(gl::EnableVertexAttribArray(self.vert_attr_index));
            gl_call!(gl::VertexAttribPointer(
                self.vert_attr_index,
                attr.count() as i32,
                attr.gl_data_type(),
                if attr.normalized { gl::TRUE } else { gl::FALSE },
                layout.stride as i32,
                attr.offset as *const gl::types::GLvoid,
            ));
            self.vert_attr_index += 1;
        }
        self.unbind();
        vert_buf.unbind();
    }
}

impl Drop for VertArray {
    fn drop(&mut self) {
        gl_call!(gl::DeleteVertexArrays(1, &self.id));
    }
}

pub struct IndexBuf {
    id: u32,
    indices: Vec<u32>,
}

impl IndexBuf {
    pub fn new(indices: Vec<u32>) -> IndexBuf {
        let mut id = 0;
        gl_call!(gl::GenBuffers(1, &mut id));
        gl_call!(gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, id));
        let size = gl::types::GLsizeiptr::try_from(indices.capacity() * std::mem::size_of::<u32>())
            .unwrap();
        let ptr = indices.as_ptr() as *const gl::types::GLvoid;
        gl_call!(gl::BufferData(
            gl::ELEMENT_ARRAY_BUFFER, // target buffer type
            size,                     // size of data in bytes
            ptr,                      // pointer to data
            gl::STATIC_DRAW,          // usage hint
        ));
        gl_call!(gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, 0));
        IndexBuf { id: id, indices }
    }
    pub fn id(&self) -> u32 {
        self.id
    }
    pub fn bind(&self) {
        gl_call!(gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, self.id));
    }
    pub fn unbind(&self) {
        gl_call!(gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, 0));
    }
    pub fn len(&self) -> usize {
        self.indices.len()
    }
    pub fn indices(&self) -> &[u32] {
        &self.indices
    }
    pub fn indices_mut(&mut self) -> &mut Vec<u32> {
        &mut self.indices
    }
    pub fn set_data(&self) {
        self.bind();
        let size = gl::types::GLsizeiptr::try_from(self.indices.len() * std::mem::size_of::<u32>())
            .unwrap();
        let ptr = self.indices.as_ptr() as *const gl::types::GLvoid;
        gl_call!(gl::BufferSubData(gl::ELEMENT_ARRAY_BUFFER, 0, size, ptr));
        self.unbind();
    }
}

impl Drop for IndexBuf {
    fn drop(&mut self) {
        gl_call!(gl::DeleteBuffers(1, &self.id));
    }
}

pub trait VertBuffer {
    fn layout(&self) -> &VertLayout;
    fn bind(&self);
    fn unbind(&self);
}

impl<T: Vert> VertBuffer for VertBuf<T> {
    fn layout(&self) -> &VertLayout {
        &self.layout
    }
    fn bind(&self) {
        self.bind();
    }
    fn unbind(&self) {
        self.unbind();
    }
}

pub struct VertBuf<T: Vert> {
    id: u32,
    vertices: Vec<T>,
    layout: VertLayout,
}

impl<T: Vert> VertBuf<T> {
    pub fn new(vertices: Vec<T>) -> Self {
        let mut id = 0;
        gl_call!(gl::GenBuffers(1, &mut id));
        // select the buffer as an simple array
        gl_call!(gl::BindBuffer(gl::ARRAY_BUFFER, id));
        // TODO: maybe use the layout sizes for this...
        let size = gl::types::GLsizeiptr::try_from(vertices.capacity() * std::mem::size_of::<T>())
            .unwrap();
        let ptr = vertices.as_ptr() as *const gl::types::GLvoid;
        // fill selected buffer with data
        gl_call!(gl::BufferData(
            gl::ARRAY_BUFFER, // target buffer type
            size,             // size of data in bytes
            ptr,              // pointer to data
            gl::DYNAMIC_DRAW, // usage hint
        ));
        gl_call!(gl::BindBuffer(gl::ARRAY_BUFFER, 0));
        VertBuf {
            id,
            vertices,
            layout: T::layout(),
        }
    }
    pub fn id(&self) -> u32 {
        self.id
    }
    pub fn layout(&self) -> &VertLayout {
        &self.layout
    }
    pub fn bind(&self) {
        gl_call!(gl::BindBuffer(gl::ARRAY_BUFFER, self.id))
    }
    pub fn unbind(&self) {
        gl_call!(gl::BindBuffer(gl::ARRAY_BUFFER, 0));
    }
    pub fn vertices(&self) -> &[T] {
        &self.vertices
    }
    pub fn vertices_mut(&mut self) -> &mut Vec<T> {
        &mut self.vertices
    }
    pub fn set_data(&self) {
        self.bind();
        let size = gl::types::GLsizeiptr::try_from(self.vertices.len() * std::mem::size_of::<T>())
            .unwrap();
        let ptr = self.vertices.as_ptr() as *const gl::types::GLvoid;
        // fill selected buffer with data
        gl_call!(gl::BufferSubData(gl::ARRAY_BUFFER, 0, size, ptr));
        self.unbind();
        // self.vertices = vertices;
    }
}

impl<T: Vert> Drop for VertBuf<T> {
    fn drop(&mut self) {
        gl_call!(gl::DeleteBuffers(1, &self.id));
    }
}

#[derive(Debug)]
pub struct VertLayout {
    attrs: Vec<VertAttr>,
    stride: u32,
}

impl VertLayout {
    pub fn new(mut attrs: Vec<VertAttr>) -> Self {
        let mut offset: usize = 0;
        let mut stride = 0;
        let attrs = attrs
            .iter_mut()
            .map(|vert_attr| {
                // TODO: calculate offsets as Vec<u32> for sizes > 16 bytes, e.g. Mat4 = [0, 16, 32, 48] or Mat3 = [0, 12, 24]
                vert_attr.offset = offset;
                let size = vert_attr.size();
                offset += size;
                stride += size;
                *vert_attr
            })
            .collect();
        VertLayout {
            attrs,
            stride: stride as u32,
        }
    }
}

#[derive(Debug, Copy, Clone)]
pub struct VertAttr {
    attr_type: VertAttrType,
    normalized: bool,
    offset: usize,
}

impl VertAttr {
    pub fn new(attr_type: VertAttrType, normalized: bool) -> Self {
        VertAttr {
            attr_type,
            normalized,
            offset: 0,
        }
    }
    pub fn count(&self) -> usize {
        match self.attr_type {
            VertAttrType::Float2 => 2,
            VertAttrType::Float3 => 3,
            VertAttrType::Mat3 => 3 * 3,
            VertAttrType::Mat4 => 4 * 4,
        }
    }
    /// Size of attribute in bytes.
    pub fn size(&self) -> usize {
        self.gl_data_type_size() * self.count()
    }
    pub fn gl_data_type(&self) -> u32 {
        match self.attr_type {
            VertAttrType::Float2 => gl::FLOAT,
            VertAttrType::Float3 => gl::FLOAT,
            VertAttrType::Mat3 => gl::FLOAT,
            VertAttrType::Mat4 => gl::FLOAT,
        }
    }
    pub fn gl_data_type_size(&self) -> usize {
        match self.gl_data_type() {
            gl::FLOAT => 4,
            _ => panic!("unsupported data type"),
        }
    }
}

#[derive(Debug, Copy, Clone)]
pub enum VertAttrType {
    Float2,
    Float3,
    Mat3,
    Mat4,
}

pub trait Vert {
    fn layout() -> VertLayout;
}