Add Context abstraction

Former-commit-id: 7bd77bfa85539326f688ead84edaf086c5c88985
This commit is contained in:
alteous 2017-07-09 12:42:53 +01:00
parent b450533bbe
commit 9bbd34b6f6

View File

@ -4,6 +4,7 @@ mod ffi;
use std::os::raw::*;
use std::mem;
/// Rust FFI for the MikkTSpace implementation.
const INTERFACE: ffi::SMikkTSpaceInterface = ffi::SMikkTSpaceInterface {
m_getNumFaces: faces,
m_getNumVerticesOfFace: vertices,
@ -15,7 +16,26 @@ const INTERFACE: ffi::SMikkTSpaceInterface = ffi::SMikkTSpaceInterface {
};
pub struct Context {
faces: i32,
faces: Box<ExactSizeIterator<Item = Face>>,
positions: Box<ExactSizeIterator<Item = [f32; 3]>>,
tex_coords: Box<ExactSizeIterator<Item = [f32; 2]>>,
normals: Box<ExactSizeIterator<Item = [f32; 3]>>,
}
/// Specifies whether a face is a triangle or a quad.
pub enum Face {
Triangle,
Quad,
}
impl Face {
/// Returns the number of vertices bound by this face.
pub fn vertices(&self) -> i32 {
match self {
&Face::Triangle { .. } => 3,
&Face::Quad { .. } => 4,
}
}
}
/// Returns the number of faces (triangles/quads) on the mesh to be processed.
@ -23,7 +43,7 @@ pub struct Context {
extern "C" fn faces(pContext: *const ffi::SMikkTSpaceContext) -> c_int {
unsafe {
let m: *const Context = mem::transmute(pContext);
(*m).faces as c_int
(*m).faces.len() as c_int
}
}
@ -36,7 +56,7 @@ extern "C" fn vertices(
) -> c_int {
unsafe {
let _: *const Context = mem::transmute(pContext);
unimplemented!()
3
}
}
@ -114,16 +134,19 @@ extern "C" fn set_tspace(
}
impl Context {
pub fn new() -> Self {
/// Constructor for a MikkTSpace `Context`.
pub fn new<F, P, T, N>(faces: F, positions: P, tex_coords: T, normals: N) -> Self
where
F: 'static + ExactSizeIterator<Item = Face>,
P: 'static + ExactSizeIterator<Item = [f32; 3]>,
T: 'static + ExactSizeIterator<Item = [f32; 2]>,
N: 'static + ExactSizeIterator<Item = [f32; 3]>,
{
Context {
faces: 3,
faces: Box::new(faces),
positions: Box::new(positions),
tex_coords: Box::new(tex_coords),
normals: Box::new(normals),
}
}
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
}
}