1 use cgmath::conv::array4x4;
2 use cgmath::{Matrix4, One};
4 use glium::{Surface, texture};
13 implement_vertex!(Vertex, position, tex_coords);
16 tex: texture::CompressedSrgbTexture2d,
17 vertex_buffer: glium::VertexBuffer<Vertex>,
18 index_buffer: glium::IndexBuffer<u16>,
19 program: glium::Program,
20 params: glium::DrawParameters<'a>,
25 pub fn new(display: &glium::Display) -> Text {
26 let image = image::load(Cursor::new(&include_bytes!("c64-font.png")[..]), image::PNG)
29 let dimensions = image.dimensions();
31 glium::texture::RawImage2d::from_raw_rgba_reversed(image.into_raw(), dimensions);
32 let tex = glium::texture::CompressedSrgbTexture2d::new(display, image).unwrap();
34 // building the vertex buffer, which contains all the vertices that we will draw
36 glium::VertexBuffer::new(
40 position: [-1.0, -1.0],
41 tex_coords: [0.0, 0.0],
44 position: [-1.0, 1.0],
45 tex_coords: [0.0, 1.0],
49 tex_coords: [1.0, 1.0],
52 position: [1.0, -1.0],
53 tex_coords: [1.0, 0.0],
59 let index_buffer = glium::IndexBuffer::new(
61 glium::index::PrimitiveType::TriangleStrip,
65 // compiling shaders and linking them together
66 let program = program!(display,
72 uniform mat4 perspview;
78 out vec2 v_tex_coords;
81 gl_Position = perspview * model * vec4(position, 0.0, 1.0);
83 // Characters are arranged in a 16x16 square.
84 // Texture oordinates originate in the bottom-left corner.
85 int xpos = index % 16;
86 int ypos = 15 - index / 16;
87 v_tex_coords = (tex_coords) / 16.0 + vec2(xpos / 16., ypos / 16.);
93 uniform sampler2D tex;
98 f_color = texture(tex, v_tex_coords);
103 let params = glium::DrawParameters {
104 depth: glium::Depth {
105 test: glium::draw_parameters::DepthTest::IfLess,
114 model: Matrix4::one(),
116 vertex_buffer: vertex_buffer,
117 index_buffer: index_buffer,
123 pub fn draw(&self, frame: &mut glium::Frame, perspview: &[[f32; 4]; 4]) {
124 let uniforms = uniform! {
125 model: array4x4(self.model),
126 perspview: *perspview,
127 tex: self.tex.sampled()
128 .magnify_filter(glium::uniforms::MagnifySamplerFilter::Nearest),