use glium;
use {Screen, ScreenType};
use errors::ProcessingErr;
use shapes::{Shape, ShapeVertex, IndexType, load_colors};
pub struct Cube {
fill_buffer: glium::vertex::VertexBuffer<ShapeVertex>,
stroke_buffer: glium::vertex::VertexBuffer<ShapeVertex>,
fill_index_buffer: IndexType,
stroke_index_buffer: IndexType,
}
impl Shape for Cube {
fn fill_buffer(&self) -> Box<&glium::vertex::VertexBuffer<ShapeVertex>> {
Box::new(&self.fill_buffer)
}
fn stroke_buffer(&self) -> Box<&glium::vertex::VertexBuffer<ShapeVertex>> {
Box::new(&self.stroke_buffer)
}
fn fill_indices(&self) -> Box<&IndexType> {
Box::new(&self.fill_index_buffer)
}
fn stroke_indices(&self) -> Box<&IndexType> {
Box::new(&self.stroke_index_buffer)
}
fn get_texture(&self) -> Option<Box<&glium::texture::Texture2d>> {
None
}
}
impl Cube {
pub fn new(screen: &Screen, s: &[f64]) -> Result<Self, ProcessingErr> {
let cube_vertices = [
-1.0f32,
-1.0,
-1.0,
-1.0,
-1.0,
1.0,
-1.0,
1.0,
1.0,
1.0,
1.0,
-1.0,
-1.0,
-1.0,
-1.0,
-1.0,
1.0,
-1.0,
1.0,
-1.0,
1.0,
-1.0,
-1.0,
-1.0,
1.0,
-1.0,
-1.0,
1.0,
1.0,
-1.0,
1.0,
-1.0,
-1.0,
-1.0,
-1.0,
-1.0,
-1.0,
-1.0,
-1.0,
-1.0,
1.0,
1.0,
-1.0,
1.0,
-1.0,
1.0,
-1.0,
1.0,
-1.0,
-1.0,
1.0,
-1.0,
-1.0,
-1.0,
-1.0,
1.0,
1.0,
-1.0,
-1.0,
1.0,
1.0,
-1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
-1.0,
-1.0,
1.0,
1.0,
-1.0,
1.0,
-1.0,
-1.0,
1.0,
1.0,
1.0,
1.0,
-1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
-1.0,
-1.0,
1.0,
-1.0,
1.0,
1.0,
1.0,
-1.0,
1.0,
-1.0,
-1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
-1.0,
1.0,
1.0,
1.0,
-1.0,
1.0,
];
let mut shape = vec![];
for c in 0..s.len() {
let mut x = 0;
for _ in 0..35 {
let vertex = ShapeVertex {
position: [
cube_vertices[x] * s[c] as f32,
cube_vertices[x + 1] * s[c] as f32,
cube_vertices[x + 2] * s[c] as f32,
],
color: [0.0, 0.0, 0.0, 0.0],
texcoord: [0f32, 0.],
};
shape.push(vertex);
x += 3;
}
}
load_colors(&mut shape, &screen.fill_col);
let fill_shape_buffer = match screen.display {
ScreenType::Window(ref d) => glium::VertexBuffer::new(d, &shape)
.map_err(|e| ProcessingErr::VBNoCreate(e))?,
ScreenType::Headless(ref d) => glium::VertexBuffer::new(d, &shape)
.map_err(|e| ProcessingErr::VBNoCreate(e))?,
};
load_colors(&mut shape, &screen.stroke_col);
let stroke_shape_buffer = match screen.display {
ScreenType::Window(ref d) => glium::VertexBuffer::new(d, &shape)
.map_err(|e| ProcessingErr::VBNoCreate(e))?,
ScreenType::Headless(ref d) => glium::VertexBuffer::new(d, &shape)
.map_err(|e| ProcessingErr::VBNoCreate(e))?,
};
Ok(Cube {
fill_buffer: fill_shape_buffer,
stroke_buffer: stroke_shape_buffer,
fill_index_buffer: IndexType::NoBuffer {
ind: glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList),
},
stroke_index_buffer: IndexType::NoBuffer {
ind: glium::index::NoIndices(glium::index::PrimitiveType::LineLoop),
},
})
}
}