diff --git a/src/lib.rs b/src/lib.rs index 050038d..6a8ee87 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,3 @@ pub mod render_timer; -pub mod shader; \ No newline at end of file +pub mod shader; +pub mod vao; \ No newline at end of file diff --git a/src/vao.rs b/src/vao.rs new file mode 100644 index 0000000..3ae6494 --- /dev/null +++ b/src/vao.rs @@ -0,0 +1,104 @@ +use std::os::raw::c_void; + +pub struct EBO { + id: gl::types::GLuint, +} + +impl EBO { + pub fn new(data: &[u32]) -> Self { + let mut id = 0; + unsafe { gl::GenBuffers(1, &mut id) }; + unsafe { gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, id) }; + unsafe { + gl::BufferData( + gl::ELEMENT_ARRAY_BUFFER, + size_of_val(&data) as _, + data.as_ptr() as _, + gl::STATIC_DRAW, + ) + }; + + EBO { id: id } + } + + pub fn bind(&self) { + unsafe { gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, self.id) }; + } +} + +pub struct VBO { + id: gl::types::GLuint, +} + +impl VBO { + pub fn new(data: &[u32]) -> Self { + let mut id: u32 = 0; + unsafe { gl::GenBuffers(1, &mut id) }; + unsafe { gl::BindBuffer(gl::ARRAY_BUFFER, id) }; + unsafe { + gl::BufferData( + gl::ARRAY_BUFFER, + size_of_val(&data) as _, + data.as_ptr() as _, + gl::STATIC_DRAW, + ) + }; + + VBO { id: id } + } + + pub fn bind(&self) { + unsafe { gl::BindBuffer(gl::ARRAY_BUFFER, self.id) }; + } + + pub fn add_attrib(// TODO: 迁移到其他地方 + &self, + index: u32, + size: i32, + type_: u32, + normalized: u8, + stride: i32, + pointer: *const c_void, + ) { + self.bind(); + + unsafe { gl::VertexAttribPointer(index, size, type_, normalized, stride, pointer) }; + unsafe { gl::EnableVertexAttribArray(index) }; + } +} + +pub struct VAO { + id: gl::types::GLuint, + vbo: Option, + ebo: Option, +} + +impl VAO { + pub fn new() -> Self { + let mut id = 0; + unsafe { gl::GenVertexArrays(1, &mut id) }; + unsafe { gl::BindVertexArray(id) }; + + VAO { id: id, vbo: None, ebo: None } + } + + pub fn bind(&self) { + unsafe { gl::BindVertexArray(self.id) }; + } + + pub fn new_vbo(&mut self, data:&[u32]) { + self.bind(); + + self.vbo = Some(VBO::new(data)); + } + + pub fn new_ebo(&mut self, data:&[u32]) { + self.bind(); + + self.ebo = Some(EBO::new(data)); + } +} + +pub struct VertexAttribute { + // TODO: 定义 +} \ No newline at end of file