2020-02-17 03:06:52 +00:00
|
|
|
use byteorder::{ByteOrder, LittleEndian};
|
|
|
|
|
|
|
|
use std::convert::TryInto;
|
|
|
|
use std::ffi::c_void;
|
|
|
|
|
|
|
|
use crate::gpu_buffers::*;
|
|
|
|
|
|
|
|
// Takes ownership of mesh stuff in an opaque way that's abstract
|
|
|
|
// from the IQM model. IQM is zero-copy, but this is not.
|
|
|
|
// Since it's opaque, I can drop in a VBO/IBO setup when I'm not lazy.
|
|
|
|
|
2020-03-08 01:45:35 +00:00
|
|
|
pub struct RenderableMesh {
|
2020-02-17 03:06:52 +00:00
|
|
|
first_triangle: usize,
|
|
|
|
num_triangles: i32,
|
2020-03-08 01:45:35 +00:00
|
|
|
pub name: Vec <u8>,
|
2020-02-17 03:06:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RenderableModel {
|
|
|
|
num_pos: usize,
|
|
|
|
num_uv: usize,
|
|
|
|
|
|
|
|
vertexes: VertexBuffer,
|
|
|
|
indexes: IndexBuffer,
|
|
|
|
|
2020-03-08 01:45:35 +00:00
|
|
|
pub meshes: Vec <RenderableMesh>,
|
2020-02-17 03:06:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn vertex_attrib_pointer (id: Option <u32>, num_coords: i32, float_offset: usize) {
|
|
|
|
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, (float_offset * 4) as *const u8 as *const c_void);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 04:55:04 +00:00
|
|
|
pub type AttrMap = Vec <Option <u32>>;
|
2020-03-08 04:17:57 +00:00
|
|
|
|
|
|
|
pub mod attributes {
|
|
|
|
use iota::iota;
|
|
|
|
iota! {
|
|
|
|
pub const
|
|
|
|
POS: usize = iota;
|
|
|
|
, UV
|
|
|
|
, NORMAL
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 01:59:44 +00:00
|
|
|
|
2020-02-17 03:06:52 +00:00
|
|
|
impl RenderableModel {
|
2020-05-25 18:22:06 +00:00
|
|
|
pub fn from_iqm (model: &inter_quake_model::Model) -> RenderableModel {
|
|
|
|
let pos_bytes = model.get_vertex_slice (inter_quake_model::types::POSITION);
|
|
|
|
let uv_bytes = model.get_vertex_slice (inter_quake_model::types::TEXCOORD);
|
|
|
|
let normal_bytes = model.get_vertex_slice (inter_quake_model::types::NORMAL);
|
2020-02-17 03:06:52 +00:00
|
|
|
|
|
|
|
let num_pos = pos_bytes.len () / 4;
|
|
|
|
let num_uv = uv_bytes.len () / 4;
|
|
|
|
let num_normal = normal_bytes.len () / 4;
|
|
|
|
|
2020-03-07 02:23:23 +00:00
|
|
|
let num_vertexes = num_pos / 3;
|
|
|
|
assert_eq! (num_vertexes * 2, num_uv);
|
|
|
|
assert_eq! (num_vertexes * 3, num_normal);
|
|
|
|
|
2020-02-17 03:06:52 +00:00
|
|
|
let mut vertex_vec = vec! [0.0; num_pos + num_uv + num_normal];
|
|
|
|
|
|
|
|
LittleEndian::read_f32_into (pos_bytes, &mut vertex_vec [0..num_pos]);
|
|
|
|
LittleEndian::read_f32_into (uv_bytes, &mut vertex_vec [num_pos..num_pos + num_uv]);
|
|
|
|
LittleEndian::read_f32_into (normal_bytes, &mut vertex_vec [num_pos + num_uv..num_pos + num_uv + num_normal]);
|
|
|
|
|
|
|
|
let vertexes = VertexBuffer::from_slice (&vertex_vec);
|
|
|
|
|
|
|
|
let index_slice = model.get_all_indexes ();
|
2020-03-15 16:37:37 +00:00
|
|
|
let indexes = IndexBuffer::from_slice_u8 (index_slice);
|
2020-02-17 03:06:52 +00:00
|
|
|
|
|
|
|
let max_index: usize = indexes.max ().try_into ().unwrap ();
|
|
|
|
|
|
|
|
assert! (max_index * 3 < num_pos);
|
|
|
|
assert! (max_index * 2 < num_uv);
|
|
|
|
assert! (max_index * 3 < num_normal);
|
|
|
|
|
2020-03-08 01:45:35 +00:00
|
|
|
let meshes = model.meshes.iter ().enumerate ()
|
|
|
|
.map (|(i, mesh)| RenderableMesh {
|
2020-02-17 03:06:52 +00:00
|
|
|
first_triangle: mesh.first_triangle.try_into ().unwrap (),
|
|
|
|
num_triangles: mesh.num_triangles.try_into ().unwrap (),
|
2020-03-08 01:45:35 +00:00
|
|
|
name: model.get_mesh_name (i).to_owned (),
|
2020-02-17 03:06:52 +00:00
|
|
|
})
|
|
|
|
.collect ();
|
|
|
|
|
|
|
|
Self {
|
|
|
|
num_pos,
|
|
|
|
num_uv,
|
|
|
|
|
|
|
|
vertexes,
|
|
|
|
indexes,
|
|
|
|
meshes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 01:59:44 +00:00
|
|
|
pub fn draw_ranged <F> (&self,
|
|
|
|
attrs: &AttrMap, callback: F,
|
|
|
|
start: usize, end: usize
|
|
|
|
)
|
|
|
|
where F: Fn (usize) -> bool
|
2020-02-17 03:06:52 +00:00
|
|
|
{
|
|
|
|
self.vertexes.bind ();
|
|
|
|
self.indexes.bind ();
|
|
|
|
|
|
|
|
unsafe {
|
2020-03-08 04:17:57 +00:00
|
|
|
use attributes::*;
|
2020-03-08 04:55:04 +00:00
|
|
|
vertex_attrib_pointer (attrs [POS], 3, 0);
|
|
|
|
vertex_attrib_pointer (attrs [UV], 2, self.num_pos);
|
|
|
|
vertex_attrib_pointer (attrs [NORMAL], 3, self.num_pos + self.num_uv);
|
2020-03-08 01:59:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for mesh_num in start..end {
|
|
|
|
if ! callback (mesh_num) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mesh = &self.meshes [mesh_num];
|
2020-02-17 03:06:52 +00:00
|
|
|
|
2020-03-08 01:59:44 +00:00
|
|
|
unsafe {
|
|
|
|
gl::DrawRangeElements (gl::TRIANGLES, 0, self.indexes.max (), mesh.num_triangles * 3, gl::UNSIGNED_INT, (mesh.first_triangle * 3 * 4) as *const u8 as *const c_void);
|
|
|
|
}
|
2020-02-17 03:06:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-07 02:23:23 +00:00
|
|
|
|
2020-03-08 01:59:44 +00:00
|
|
|
pub fn draw_all <F> (&self, attrs: &AttrMap, callback: F)
|
|
|
|
where F: Fn (usize) -> bool
|
|
|
|
{
|
|
|
|
self.draw_ranged (attrs, callback, 0, self.meshes.len ());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn draw (&self, attrs: &AttrMap, mesh_num: usize)
|
|
|
|
{
|
|
|
|
self.draw_ranged (attrs, |_| true, mesh_num, mesh_num + 1);
|
|
|
|
}
|
|
|
|
|
2020-03-07 02:23:23 +00:00
|
|
|
pub fn num_meshes (&self) -> usize {
|
|
|
|
self.meshes.len ()
|
|
|
|
}
|
2020-02-17 03:06:52 +00:00
|
|
|
}
|
2020-03-10 12:47:21 +00:00
|
|
|
|
|
|
|
pub fn renderable_from_iqm_file <P> (filename: P) -> RenderableModel
|
|
|
|
where P: AsRef <std::path::Path>
|
|
|
|
{
|
|
|
|
let data = crate::file::load_small_file (filename, 1024 * 1024).unwrap ();
|
2020-05-25 18:22:06 +00:00
|
|
|
let model = inter_quake_model::Model::from_slice (&data).unwrap ();
|
2020-03-10 12:47:21 +00:00
|
|
|
|
|
|
|
RenderableModel::from_iqm (&model)
|
|
|
|
}
|