use gl;
use ToGlEnum;
use std::mem;
use backend::Facade;
pub use self::buffer::IndexBuffer;
pub use self::local::{PointsList, LinesList, LinesListAdjacency, LineStrip, LineStripAdjacency};
pub use self::local::{TrianglesList, TrianglesListAdjacency, TriangleStrip, TriangleStripAdjacency};
pub use self::local::{TriangleFan, Patches};
mod buffer;
mod local;
pub trait ToIndicesSource {
type Data: Index;
fn to_indices_source<'a>(&'a self) -> IndicesSource<'a, Self::Data>;
}
#[derive(Clone)]
pub enum IndicesSource<'a, T: 'a> {
IndexBuffer {
buffer: &'a IndexBuffer,
offset: usize,
length: usize,
},
Buffer {
pointer: &'a [T],
primitives: PrimitiveType,
offset: usize,
length: usize,
},
NoIndices {
primitives: PrimitiveType,
},
}
impl<'a, T> IndicesSource<'a, T> where T: Index {
pub fn get_primitives_type(&self) -> PrimitiveType {
match self {
&IndicesSource::IndexBuffer { ref buffer, .. } => buffer.get_primitives_type(),
&IndicesSource::Buffer { primitives, .. } => primitives,
&IndicesSource::NoIndices { primitives } => primitives,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PrimitiveType {
Points,
LinesList,
LinesListAdjacency,
LineStrip,
LineStripAdjacency,
TrianglesList,
TrianglesListAdjacency,
TriangleStrip,
TriangleStripAdjacency,
TriangleFan,
Patches {
vertices_per_patch: u16,
},
}
impl ToGlEnum for PrimitiveType {
fn to_glenum(&self) -> gl::types::GLenum {
match self {
&PrimitiveType::Points => gl::POINTS,
&PrimitiveType::LinesList => gl::LINES,
&PrimitiveType::LinesListAdjacency => gl::LINES_ADJACENCY,
&PrimitiveType::LineStrip => gl::LINE_STRIP,
&PrimitiveType::LineStripAdjacency => gl::LINE_STRIP_ADJACENCY,
&PrimitiveType::TrianglesList => gl::TRIANGLES,
&PrimitiveType::TrianglesListAdjacency => gl::TRIANGLES_ADJACENCY,
&PrimitiveType::TriangleStrip => gl::TRIANGLE_STRIP,
&PrimitiveType::TriangleStripAdjacency => gl::TRIANGLE_STRIP_ADJACENCY,
&PrimitiveType::TriangleFan => gl::TRIANGLE_FAN,
&PrimitiveType::Patches { .. } => gl::PATCHES,
}
}
}
#[derive(Copy, Clone, Debug)]
pub struct NoIndices(pub PrimitiveType);
impl ToIndicesSource for NoIndices {
type Data = u16;
fn to_indices_source(&self) -> IndicesSource<u16> { IndicesSource::NoIndices {
primitives: self.0,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)] pub enum IndexType {
U8 = gl::UNSIGNED_BYTE,
U16 = gl::UNSIGNED_SHORT,
U32 = gl::UNSIGNED_INT,
}
impl IndexType {
pub fn get_size(&self) -> usize {
match *self {
IndexType::U8 => mem::size_of::<u8>(),
IndexType::U16 => mem::size_of::<u16>(),
IndexType::U32 => mem::size_of::<u32>(),
}
}
}
impl ToGlEnum for IndexType {
fn to_glenum(&self) -> gl::types::GLenum {
*self as gl::types::GLenum
}
}
pub unsafe trait Index: Copy + Send + 'static {
fn get_type() -> IndexType;
}
unsafe impl Index for u8 {
fn get_type() -> IndexType {
IndexType::U8
}
}
unsafe impl Index for u16 {
fn get_type() -> IndexType {
IndexType::U16
}
}
unsafe impl Index for u32 {
fn get_type() -> IndexType {
IndexType::U32
}
}
pub trait IntoIndexBuffer {
fn into_index_buffer<F>(self, &F) -> IndexBuffer where F: Facade;
}