2017-06-13 14:52:43 +00:00
|
|
|
use glium;
|
|
|
|
use glium::Surface;
|
|
|
|
use glium::index::PrimitiveType;
|
|
|
|
|
2017-09-13 20:51:35 +00:00
|
|
|
use std::ops::Deref;
|
|
|
|
|
2017-06-13 14:52:43 +00:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct Vertex {
|
|
|
|
position: [f32; 2],
|
|
|
|
tex_coords: [f32; 2],
|
|
|
|
}
|
|
|
|
|
|
|
|
implement_vertex!(Vertex, position, tex_coords);
|
|
|
|
|
2017-09-13 20:51:35 +00:00
|
|
|
pub struct GliumDrawer<F> {
|
|
|
|
display: F,
|
2017-06-13 14:52:43 +00:00
|
|
|
vertex_buffer: glium::VertexBuffer<Vertex>,
|
|
|
|
index_buffer: glium::IndexBuffer<u16>,
|
|
|
|
program: glium::Program,
|
|
|
|
}
|
|
|
|
|
2017-09-13 20:51:35 +00:00
|
|
|
impl<F> Deref for GliumDrawer<F> {
|
|
|
|
type Target = F;
|
|
|
|
|
|
|
|
fn deref(&self) -> &F {
|
|
|
|
&self.display
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F: glium::backend::Facade> GliumDrawer<F> {
|
|
|
|
pub fn new(display: F) -> GliumDrawer<F> {
|
2017-06-13 14:52:43 +00:00
|
|
|
// building the vertex buffer, which contains all the vertices that we will draw
|
2017-06-23 13:40:28 +00:00
|
|
|
let vertex_buffer = glium::VertexBuffer::new(
|
2017-09-13 20:51:35 +00:00
|
|
|
&display,
|
2017-06-23 13:40:28 +00:00
|
|
|
&[
|
|
|
|
Vertex {
|
|
|
|
position: [0.0, 0.0],
|
|
|
|
tex_coords: [0.0, 0.0],
|
|
|
|
},
|
|
|
|
Vertex {
|
|
|
|
position: [0.0, 1.0],
|
|
|
|
tex_coords: [0.0, 1.0],
|
|
|
|
},
|
|
|
|
Vertex {
|
|
|
|
position: [1.0, 1.0],
|
|
|
|
tex_coords: [1.0, 1.0],
|
|
|
|
},
|
|
|
|
Vertex {
|
|
|
|
position: [1.0, 0.0],
|
|
|
|
tex_coords: [1.0, 0.0],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
).unwrap();
|
2017-06-13 14:52:43 +00:00
|
|
|
|
|
|
|
// building the index buffer
|
|
|
|
let index_buffer =
|
2017-09-13 20:51:35 +00:00
|
|
|
glium::IndexBuffer::new(&display, PrimitiveType::TriangleStrip, &[1 as u16, 2, 0, 3]).unwrap();
|
2017-06-13 14:52:43 +00:00
|
|
|
|
|
|
|
// compiling shaders and linking them together
|
2017-09-13 20:51:35 +00:00
|
|
|
let program = program!(&display,
|
2017-06-13 14:52:43 +00:00
|
|
|
100 => {
|
|
|
|
vertex: "
|
|
|
|
#version 100
|
|
|
|
uniform lowp mat4 matrix;
|
|
|
|
attribute lowp vec2 position;
|
|
|
|
attribute lowp vec2 tex_coords;
|
|
|
|
varying lowp vec2 v_tex_coords;
|
|
|
|
void main() {
|
|
|
|
gl_Position = matrix * vec4(position, 0.0, 1.0);
|
|
|
|
v_tex_coords = tex_coords;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
|
|
|
|
fragment: "
|
|
|
|
#version 100
|
|
|
|
uniform lowp sampler2D tex;
|
|
|
|
varying lowp vec2 v_tex_coords;
|
|
|
|
void main() {
|
|
|
|
lowp vec4 color = texture2D(tex, v_tex_coords);
|
|
|
|
gl_FragColor.r = color.z;
|
|
|
|
gl_FragColor.g = color.y;
|
|
|
|
gl_FragColor.b = color.x;
|
|
|
|
gl_FragColor.a = color.w;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
},
|
2017-06-13 15:39:25 +00:00
|
|
|
).unwrap();
|
2017-06-13 14:52:43 +00:00
|
|
|
|
|
|
|
GliumDrawer {
|
|
|
|
display,
|
|
|
|
vertex_buffer,
|
|
|
|
index_buffer,
|
|
|
|
program,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 20:51:35 +00:00
|
|
|
pub fn render(&self, target: &mut glium::Frame, contents: &[u8], surface_dimensions: (u32, u32),
|
|
|
|
surface_location: (i32, i32), screen_size: (u32, u32)) {
|
2017-06-13 14:52:43 +00:00
|
|
|
let image = glium::texture::RawImage2d {
|
|
|
|
data: contents.into(),
|
|
|
|
width: surface_dimensions.0,
|
|
|
|
height: surface_dimensions.1,
|
2017-06-13 15:39:25 +00:00
|
|
|
format: glium::texture::ClientFormat::U8U8U8U8,
|
2017-06-13 14:52:43 +00:00
|
|
|
};
|
2017-09-13 20:51:35 +00:00
|
|
|
let opengl_texture = glium::texture::Texture2d::new(&self.display, image).unwrap();
|
2017-06-13 14:52:43 +00:00
|
|
|
|
2017-06-13 15:39:25 +00:00
|
|
|
let xscale = 2.0 * (surface_dimensions.0 as f32) / (screen_size.0 as f32);
|
|
|
|
let yscale = -2.0 * (surface_dimensions.1 as f32) / (screen_size.1 as f32);
|
2017-06-13 14:52:43 +00:00
|
|
|
|
2017-06-13 15:39:25 +00:00
|
|
|
let x = 2.0 * (surface_location.0 as f32) / (screen_size.0 as f32) - 1.0;
|
|
|
|
let y = 1.0 - 2.0 * (surface_location.1 as f32) / (screen_size.1 as f32);
|
2017-06-13 14:52:43 +00:00
|
|
|
|
2017-09-05 17:51:05 +00:00
|
|
|
let uniforms = uniform! {
|
2017-06-13 14:52:43 +00:00
|
|
|
matrix: [
|
|
|
|
[xscale, 0.0 , 0.0, 0.0],
|
|
|
|
[ 0.0 , yscale , 0.0, 0.0],
|
|
|
|
[ 0.0 , 0.0 , 1.0, 0.0],
|
|
|
|
[ x , y , 0.0, 1.0]
|
|
|
|
],
|
|
|
|
tex: &opengl_texture
|
|
|
|
};
|
|
|
|
|
2017-06-13 15:39:25 +00:00
|
|
|
target
|
2017-06-23 13:40:28 +00:00
|
|
|
.draw(
|
|
|
|
&self.vertex_buffer,
|
|
|
|
&self.index_buffer,
|
|
|
|
&self.program,
|
|
|
|
&uniforms,
|
|
|
|
&Default::default(),
|
|
|
|
)
|
2017-06-13 15:39:25 +00:00
|
|
|
.unwrap();
|
2017-06-13 14:52:43 +00:00
|
|
|
}
|
|
|
|
}
|