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
use super::*;

pub struct Mesh {
    _vertex_count: i32,
    index_count: i32,
    ibo: GLuint,
    vao: GLuint,
}

impl Mesh {
    pub fn draw(&self) {
        unsafe{
            gl::BindVertexArray(self.vao);
            gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER,self.ibo);
            gl::DrawElements(gl::TRIANGLES, self.index_count, gl::UNSIGNED_INT, std::ptr::null());
        } 
    }
}

#[derive(Default)]
pub struct MeshBuilder {
    indices: Option<Vec<i32>>,
    vertices: Option<Vec<f32>>,
}

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

    pub fn with_vertex_data(&mut self, vertex_data: &[f32]) -> &mut Self {
        //#TODO: Abstract away OpenGL calls
        self.vertices = Some(vertex_data.to_vec());

        self
    }

    pub fn with_index_data(&mut self, index_data: &[i32]) -> &mut Self {
        self.indices = Some(index_data.to_vec());
        //#TODO: Implement in a type safe way
        self
    }

    // #TODO: Build the mesh using a Vertex Description struct
    pub fn build(&self) -> Mesh {
        // Create indices
        let (mut ibo, mut vao) = (0, 0);
        let (mut vert_count, mut ind_count) = (0,0);
        if let Some(ref data) = self.indices {
            vert_count = data.len();
            unsafe {
                gl::GenBuffers(1, &mut ibo);
                gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, ibo);
                gl::BufferData(
                    gl::ELEMENT_ARRAY_BUFFER,
                    (data.len() * std::mem::size_of::<GLint>()) as GLsizeiptr,
                    &data[0] as *const GLint as *const std::os::raw::c_void,
                    gl::STATIC_DRAW,
                );
                gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, 0);
            }
        }

        // Create VAO
        if let Some(ref data) = self.vertices {
            ind_count = data.len();

            vao = 0;
            let mut vbo = 0;
            unsafe {
                gl::GenVertexArrays(1, &mut vao);
                gl::BindVertexArray(vao);

                gl::GenBuffers(1, &mut vbo);


                gl::BindBuffer(gl::ARRAY_BUFFER, vbo);
                gl::BufferData(
                    gl::ARRAY_BUFFER,
                    (data.len() * std::mem::size_of::<GLfloat>()) as GLsizeiptr,
                    &data[0] as *const f32 as *const std::os::raw::c_void,
                    gl::STATIC_DRAW,
                );

                gl::BindVertexArray(vao);
                gl::BindBuffer(gl::ARRAY_BUFFER, vbo);

                let stride = 8 * std::mem::size_of::<GLfloat>() as GLsizei;
                gl::EnableVertexAttribArray(0);
                gl::EnableVertexAttribArray(1);
                gl::EnableVertexAttribArray(2);

                gl::VertexAttribPointer(0, 3, gl::FLOAT, gl::FALSE, stride, std::ptr::null());

                gl::VertexAttribPointer(
                    1,
                    2,
                    gl::FLOAT,
                    gl::FALSE,
                    stride,
                    (3 * std::mem::size_of::<GLfloat>()) as *const std::os::raw::c_void,
                );

                gl::VertexAttribPointer(
                    2,
                    3,
                    gl::FLOAT,
                    gl::FALSE,
                    stride,
                    (5 * std::mem::size_of::<GLfloat>()) as *const std::os::raw::c_void,
                );

                // unbind
                gl::BindBuffer(gl::ARRAY_BUFFER, 0);
                gl::BindVertexArray(0);
            }
        }

        Mesh{
            _vertex_count: vert_count as i32,
            index_count: ind_count as i32,
            ibo,
            vao,
        }
    }
}