feat: 初步定义vao
This commit is contained in:
@@ -1,2 +1,3 @@
|
|||||||
pub mod render_timer;
|
pub mod render_timer;
|
||||||
pub mod shader;
|
pub mod shader;
|
||||||
|
pub mod vao;
|
||||||
104
src/vao.rs
Normal file
104
src/vao.rs
Normal file
@@ -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<VBO>,
|
||||||
|
ebo: Option<EBO>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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: 定义
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user