🚧 checkpoint before adding GPU buffers

main
_ 2020-02-17 00:32:52 +00:00
parent d45b95961e
commit c415e9ed80
4 changed files with 173 additions and 3 deletions

1
Cargo.lock generated
View File

@ -190,6 +190,7 @@ dependencies = [
name = "opengl_rust" name = "opengl_rust"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"gl 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)", "gl 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
"glam 0.8.5 (registry+https://github.com/rust-lang/crates.io-index)", "glam 0.8.5 (registry+https://github.com/rust-lang/crates.io-index)",
"nom 5.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "nom 5.1.0 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -8,6 +8,8 @@ edition = "2018"
[dependencies] [dependencies]
byteorder = "1.3.2"
gl = "0.14.0" gl = "0.14.0"
glam = "0.8.5" glam = "0.8.5"

View File

@ -227,6 +227,9 @@ fn main () {
let model_data = load_small_file ("pumpking.iqm", 1024 * 1024); let model_data = load_small_file ("pumpking.iqm", 1024 * 1024);
let model = Model::from_slice (&model_data [..]); let model = Model::from_slice (&model_data [..]);
let pumpkin_verts = glezz::VertexBuffer::from_slice (model.get_vertex_slice (0));
let pumpkin_indexes = glezz::IndexBuffer::from_slice (model.get_index_slice (0));
let sky_data = load_small_file ("sky-sphere.iqm", 1024 * 1024); let sky_data = load_small_file ("sky-sphere.iqm", 1024 * 1024);
let sky_model = Model::from_slice (&sky_data [..]); let sky_model = Model::from_slice (&sky_data [..]);
@ -312,16 +315,25 @@ fn main () {
// right from RAM I can still access. Using a VBO here // right from RAM I can still access. Using a VBO here
// might actually make it easier to make it safe. // might actually make it easier to make it safe.
if false {
unsafe { unsafe {
point_to_model (&attrs, &model); point_to_model (&attrs, &model);
gl::DrawElements (gl::TRIANGLES, (model.meshes [0].num_triangles * 3) as i32, gl::UNSIGNED_INT, &model.get_index_slice (0) [0] as *const u8 as *const c_void); gl::DrawElements (gl::TRIANGLES, (model.meshes [0].num_triangles * 3) as i32, gl::UNSIGNED_INT, &model.get_index_slice (0) [0] as *const u8 as *const c_void);
} }
}
unsafe {
gl::BindBuffer (gl::ARRAY_BUFFER, 0);
gl::BindBuffer (gl::ELEMENT_ARRAY_BUFFER, 0);
}
if true { if true {
glezz::uniform_3fv (unis ["albedo"], &green); glezz::uniform_3fv (unis ["albedo"], &green);
unsafe { unsafe {
point_to_model (&attrs, &model);
gl::DrawElements (gl::TRIANGLES, (model.meshes [1].num_triangles * 3) as i32, gl::UNSIGNED_INT, &model.get_index_slice (1) [0] as *const u8 as *const c_void); gl::DrawElements (gl::TRIANGLES, (model.meshes [1].num_triangles * 3) as i32, gl::UNSIGNED_INT, &model.get_index_slice (1) [0] as *const u8 as *const c_void);
} }
} }

View File

@ -1,6 +1,14 @@
// Trivial wrappers around GLESv2 C functions that should be safe // Trivial wrappers around GLESv2 C functions that should be safe
use byteorder::{LittleEndian, ReadBytesExt};
use glam::{Mat4, Vec3, Vec4}; use glam::{Mat4, Vec3, Vec4};
use std::collections::*;
use std::convert::TryInto;
use std::cmp;
use std::io::Cursor;
use std::ffi::c_void;
use crate::iqm;
pub fn clear_color (r: f32, g: f32, b: f32, a: f32) { pub fn clear_color (r: f32, g: f32, b: f32, a: f32) {
unsafe { unsafe {
@ -60,3 +68,150 @@ pub fn uniform_matrix_4fv (uni: i32, m: &Mat4) {
gl::UniformMatrix4fv (uni, 1, FALSE_U8, m as *const Mat4 as *const f32); gl::UniformMatrix4fv (uni, 1, FALSE_U8, m as *const Mat4 as *const f32);
} }
} }
// More abstract stuff
unsafe fn vertex_attrib_pointer (id: Option <u32>, num_coords: i32, slice: &[u8]) {
const FALSE_U8: u8 = 0;
const FLOAT_SIZE: i32 = 4;
if let Some (id) = id {
gl::VertexAttribPointer (id, num_coords, gl::FLOAT, FALSE_U8, FLOAT_SIZE * num_coords, &slice [0] as *const u8 as *const c_void);
}
}
pub fn draw_model (
attrs: &HashMap <String, Option <u32>>,
model: &iqm::Model,
mesh_number: usize
) {
let index_slice: &[u8] = model.get_index_slice (mesh_number);
let num_indexes = model.meshes [mesh_number].num_triangles * 3;
unsafe {
vertex_attrib_pointer (attrs ["pos"], 3, model.get_vertex_slice (0));
vertex_attrib_pointer (attrs ["uv"], 2, model.get_vertex_slice (1));
vertex_attrib_pointer (attrs ["normal"], 3, model.get_vertex_slice (2));
gl::DrawElements (gl::TRIANGLES, (num_indexes) as i32, gl::UNSIGNED_INT, &index_slice [0] as *const u8 as *const c_void);
}
}
pub struct VertexBuffer {
id: u32,
len: usize,
}
impl VertexBuffer {
pub fn from_slice (slice: &[u8]) -> Self {
let id = {
let mut id = 0;
unsafe {
gl::GenBuffers (1, &mut id);
gl::BindBuffer (gl::ARRAY_BUFFER, id);
gl::BufferData (gl::ARRAY_BUFFER, slice.len ().try_into ().unwrap (), &slice [0] as *const u8 as *const c_void, gl::STATIC_DRAW);
}
assert! (id != 0);
id
};
Self {
id,
len: slice.len (),
}
}
pub fn bind (&self) {
unsafe {
gl::BindBuffer (gl::ARRAY_BUFFER, self.id);
}
}
}
impl Drop for VertexBuffer {
fn drop (&mut self) {
if self.id == 0 {
return;
}
unsafe {
gl::DeleteBuffers (1, &self.id);
}
self.id = 0;
}
}
pub struct IndexBuffer {
id: u32,
len: usize,
min: u32,
max: u32,
}
impl IndexBuffer {
pub fn from_slice (slice: &[u8]) -> Self {
let mut rdr = Cursor::new (slice);
let mut min = None;
let mut max = None;
assert_eq! (slice.len () % 4, 0);
for _ in 0..slice.len () / 4 {
let idx = rdr.read_u32::<LittleEndian> ().unwrap ();
min = match min {
None => Some (idx),
Some (min) => Some (cmp::min (min, idx)),
};
max = match max {
None => Some (idx),
Some (max) => Some (cmp::max (max, idx)),
};
}
let id = {
let mut id = 0;
unsafe {
gl::GenBuffers (1, &mut id);
gl::BindBuffer (gl::ELEMENT_ARRAY_BUFFER, id);
gl::BufferData (gl::ELEMENT_ARRAY_BUFFER, slice.len ().try_into ().unwrap (), &slice [0] as *const u8 as *const c_void, gl::STATIC_DRAW);
}
assert! (id != 0);
id
};
Self {
id,
len: slice.len () / 4,
min: min.unwrap (),
max: max.unwrap (),
}
}
pub fn bind (&self) {
unsafe {
gl::BindBuffer (gl::ELEMENT_ARRAY_BUFFER, self.id);
}
}
}
impl Drop for IndexBuffer {
fn drop (&mut self) {
if self.id == 0 {
return;
}
unsafe {
gl::DeleteBuffers (1, &self.id);
}
self.id = 0;
}
}