diff options
author | Joe Wilm <joe@jwilm.com> | 2016-02-23 20:42:58 -0800 |
---|---|---|
committer | Joe Wilm <joe@jwilm.com> | 2016-02-23 20:42:58 -0800 |
commit | 2b7caf95fd6a28d621a348e95f66dd2aee988567 (patch) | |
tree | ccfd1e7c0c4c86ecdd36c7e98d164f58b34132d0 | |
parent | e301843686c3f7ce04b7e44e82f20e8f6e3613a7 (diff) | |
download | alacritty-2b7caf95fd6a28d621a348e95f66dd2aee988567.tar.gz alacritty-2b7caf95fd6a28d621a348e95f66dd2aee988567.zip |
Render the letter J
This letter brought to you by OpenGL and freetype.
-rw-r--r-- | Cargo.lock | 395 | ||||
-rw-r--r-- | Cargo.toml | 3 | ||||
-rw-r--r-- | res/text.f.glsl | 11 | ||||
-rw-r--r-- | res/text.v.glsl | 11 | ||||
-rw-r--r-- | src/main.rs | 297 | ||||
-rw-r--r-- | src/text.rs | 12 |
6 files changed, 722 insertions, 7 deletions
@@ -2,17 +2,117 @@ name = "alacritty" version = "0.1.0" dependencies = [ + "cgmath 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "freetype-rs 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gl 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", + "glutin 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", "servo-fontconfig 0.2.0 (git+https://github.com/jwilm/rust-fontconfig)", ] [[package]] +name = "android_glue" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] name = "bitflags" version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "byteorder" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "cgl" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "gleam 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "cgmath" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "cocoa" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "core-graphics 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "objc 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "core-foundation" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "core-foundation-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "core-foundation-sys" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "core-graphics" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "core-foundation 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 0.6.14 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "crossbeam" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "dlib" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libloading 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "dwmapi-sys" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "dylib" +version = "0.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "expat-sys" version = "2.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -42,16 +142,120 @@ dependencies = [ ] [[package]] +name = "fs2" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "gcc" version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "gdi32-sys" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "gl" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "gl_generator 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "khronos_api 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "gl_generator" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "khronos_api 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "xml-rs 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "gleam" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "gl_generator 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "khronos_api 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "glutin" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "android_glue 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "cgl 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "cocoa 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", + "core-foundation 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "core-graphics 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "dwmapi-sys 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gdi32-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "gl_generator 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "kernel32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "khronos_api 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "objc 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "osmesa-sys 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "shared_library 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "shell32-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "user32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "wayland-client 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)", + "wayland-kbd 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "wayland-window 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "x11-dl 2.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "kernel32-sys" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "khronos_api" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "lazy_static" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] name = "libc" version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "libloading" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "libz-sys" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -62,16 +266,91 @@ dependencies = [ ] [[package]] +name = "log" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "make-cmd" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "malloc_buf" +version = "0.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "memmap" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "fs2 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "kernel32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "objc" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "malloc_buf 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "osmesa-sys" +version = "0.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "shared_library 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "pkg-config" version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "rand" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rustc-serialize" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "serde" +version = "0.6.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "servo-fontconfig" version = "0.2.0" source = "git+https://github.com/jwilm/rust-fontconfig#419135e5e1106ec0973dd4923bd9c70d8e438cc8" @@ -90,3 +369,119 @@ dependencies = [ "pkg-config 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "shared_library" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "shell32-sys" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "tempfile" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "user32-sys" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "wayland-client" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "crossbeam 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "wayland-scanner 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", + "wayland-sys 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "wayland-kbd" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "dlib 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", + "memmap 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "wayland-client 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "wayland-scanner" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "xml-rs 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "wayland-sys" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "dlib 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "wayland-window" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "tempfile 1.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "wayland-client 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-build" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "x11-dl" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "dylib 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "xml-rs" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + @@ -7,3 +7,6 @@ authors = ["Joe Wilm <joe@jwilm.com>"] servo-fontconfig = { git = "https://github.com/jwilm/rust-fontconfig" } freetype-rs = "0.5.0" libc = "*" +glutin = "*" +gl = "*" +cgmath = "0.7" diff --git a/res/text.f.glsl b/res/text.f.glsl new file mode 100644 index 00000000..32266b3e --- /dev/null +++ b/res/text.f.glsl @@ -0,0 +1,11 @@ +#version 330 core +in vec2 TexCoords; + +uniform sampler2D text; +uniform vec3 textColor; + +void main() +{ + vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r); + gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0) * sampled; +} diff --git a/res/text.v.glsl b/res/text.v.glsl new file mode 100644 index 00000000..89bded5c --- /dev/null +++ b/res/text.v.glsl @@ -0,0 +1,11 @@ +#version 330 core +layout (location = 0) in vec4 vertex; +out vec2 TexCoords; + +uniform mat4 projection; + +void main() +{ + gl_Position = projection * vec4(vertex.xy, 0.0, 1.0); + TexCoords = vertex.zw; +} diff --git a/src/main.rs b/src/main.rs index 7356bd36..2ba98473 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,305 @@ extern crate fontconfig; extern crate freetype; extern crate libc; +extern crate glutin; +extern crate gl; +extern crate cgmath; mod list_fonts; mod text; + +use std::ffi::CString; +use std::mem::size_of; +use std::ptr; + +use libc::c_void; + +use gl::types::*; + +use cgmath::Matrix; + +use text::RasterizedGlyph; + +static TEXT_SHADER_V: &'static str = include_str!("../res/text.v.glsl"); +static TEXT_SHADER_F: &'static str = include_str!("../res/text.f.glsl"); + fn main() { - println!("Hello, world!"); + let window = glutin::Window::new().unwrap(); + let (width, height) = window.get_inner_size_pixels().unwrap(); + unsafe { + window.make_current() + }; + + unsafe { + gl::load_with(|symbol| window.get_proc_address(symbol) as *const _); + gl::Viewport(0, 0, width as i32, height as i32); + } + + let rasterizer = text::Rasterizer::new(); + let glyph_j = rasterizer.get_glyph(180., 'J'); + + let tex = AlphaTexture::new( + glyph_j.width as i32, + glyph_j.height as i32, + glyph_j.buf.as_ptr() as *const _ + ); + + unsafe { + gl::Enable(gl::BLEND); + gl::BlendFunc(gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA); + } + + let mut vao: GLuint = 0; + let mut vbo: GLuint = 0; + unsafe { + gl::GenVertexArrays(1, &mut vao); + gl::GenBuffers(1, &mut vbo); + gl::BindVertexArray(vao); + + gl::BindBuffer(gl::ARRAY_BUFFER, vbo); + gl::BufferData( + gl::ARRAY_BUFFER, + (size_of::<f32>() * 6 * 4) as GLsizeiptr, + ptr::null(), + gl::DYNAMIC_DRAW + ); + gl::EnableVertexAttribArray(0); + gl::VertexAttribPointer(0, 4, gl::FLOAT, gl::FALSE, 4 * size_of::<f32>() as i32, + ptr::null()); + gl::BindBuffer(gl::ARRAY_BUFFER, 0); + gl::BindVertexArray(0); + } + + let program = ShaderProgram::new(width, height); + + for event in window.wait_events() { + unsafe { + gl::ClearColor(0.08, 0.08, 0.08, 1.0); + gl::Clear(gl::COLOR_BUFFER_BIT); + } + + render(&program, &glyph_j, &tex, vbo, vao); + + window.swap_buffers(); + + match event { + glutin::Event::Closed => break, + _ => () + } + } +} + +/// Render a character +/// +/// TODO use element array to describe quad instead +fn render(program: &ShaderProgram, glyph: &RasterizedGlyph, tex: &AlphaTexture, vbo: GLuint, + vao: GLuint) +{ + program.activate(); + unsafe { + // set color + gl::Uniform3f(program.color, 1., 1., 0.5); + } + + // bottom left of character + let left = 10. as f32; + let bottom = 10. as f32; + + // top right of character + let top = bottom + glyph.height as f32; + let right = left + glyph.width as f32; + let vertices: [[f32; 4]; 6] = [ + [left, top, 0., 0.], + [left, bottom, 0., 1.], + [right, bottom, 1., 1.], + + [left, top, 0., 0.], + [right, bottom, 1., 1.], + [right, top, 1., 0.], + ]; + + unsafe { + gl::ActiveTexture(gl::TEXTURE0); + gl::BindVertexArray(vao); + + gl::BindTexture(gl::TEXTURE_2D, tex.id); + gl::BindBuffer(gl::ARRAY_BUFFER, vbo); + gl::BufferSubData( + gl::ARRAY_BUFFER, + 0, + (4 * 6 * size_of::<f32>()) as isize, + vertices.as_ptr() as *const _ + ); + gl::BindBuffer(gl::ARRAY_BUFFER, 0); + + + gl::DrawArrays(gl::TRIANGLES, 0, 6); + gl::BindVertexArray(0); + gl::BindTexture(gl::TEXTURE_2D, 0); + } + + program.deactivate(); +} + +pub struct ShaderProgram { + id: GLuint, + /// uniform location for projection matrix + projection: GLint, + /// uniform location foyr textColor + color: GLint, +} + +impl ShaderProgram { + pub fn activate(&self) { + unsafe { + gl::UseProgram(self.id); + } + } + + pub fn deactivate(&self) { + unsafe { + gl::UseProgram(0); + } + } + + pub fn new(width: u32, height: u32) -> ShaderProgram { + let vertex_shader = ShaderProgram::create_vertex_shader(); + let fragment_shader = ShaderProgram::create_fragment_shader(); + let program = ShaderProgram::create_program(vertex_shader, fragment_shader); + + unsafe { + gl::DeleteShader(vertex_shader); + gl::DeleteShader(fragment_shader); + } + + // get uniform locations + let projection_str = CString::new("projection").unwrap(); + let color_str = CString::new("textColor").unwrap(); + + let (projection, color) = unsafe { + ( + gl::GetUniformLocation(program, projection_str.as_ptr()), + gl::GetUniformLocation(program, color_str.as_ptr()) + ) + }; + + assert!(projection != gl::INVALID_VALUE as i32); + assert!(projection != gl::INVALID_OPERATION as i32); + assert!(color != gl::INVALID_VALUE as i32); + assert!(color != gl::INVALID_OPERATION as i32); + + let shader = ShaderProgram { + id: program, + projection: projection, + color: color, + }; + + // set projection uniform + let ortho = cgmath::ortho(0., width as f32, 0., height as f32, -1., 1.); + let projection: [[f32; 4]; 4] = ortho.into(); + + println!("width: {}, height: {}", width, height); + + shader.activate(); + unsafe { + gl::UniformMatrix4fv(shader.projection, 1, gl::FALSE, projection.as_ptr() as *const _); + } + shader.deactivate(); + + shader + } + + fn create_program(vertex: GLuint, fragment: GLuint) -> GLuint { + + unsafe { + let program = gl::CreateProgram(); + gl::AttachShader(program, vertex); + gl::AttachShader(program, fragment); + gl::LinkProgram(program); + + let mut success: GLint = 0; + gl::GetProgramiv(program, gl::LINK_STATUS, &mut success); + + if success != (gl::TRUE as GLint) { + panic!("failed to link shader program"); + } + program + } + } + + fn create_fragment_shader() -> GLuint { + unsafe { + let fragment_shader = gl::CreateShader(gl::FRAGMENT_SHADER); + let fragment_source = CString::new(TEXT_SHADER_F).unwrap(); + gl::ShaderSource(fragment_shader, 1, &fragment_source.as_ptr(), ptr::null()); + gl::CompileShader(fragment_shader); + + let mut success: GLint = 0; + gl::GetShaderiv(fragment_shader, gl::COMPILE_STATUS, &mut success); + + if success != (gl::TRUE as GLint) { + panic!("failed to compiler fragment shader"); + } + fragment_shader + } + } + + fn create_vertex_shader() -> GLuint { + unsafe { + let vertex_shader = gl::CreateShader(gl::VERTEX_SHADER); + let vertex_source = CString::new(TEXT_SHADER_V).unwrap(); + gl::ShaderSource(vertex_shader, 1, &vertex_source.as_ptr(), ptr::null()); + gl::CompileShader(vertex_shader); + + let mut success: GLint = 0; + gl::GetShaderiv(vertex_shader, gl::COMPILE_STATUS, &mut success); + + if success != (gl::TRUE as GLint) { + panic!("failed to compiler vertex shader"); + } + vertex_shader + } + } +} + +struct AlphaTexture { + pub id: GLuint, + pub width: i32, + pub height: i32, +} + +impl AlphaTexture { + pub fn new(width: i32, height: i32, bytes: *const ::std::os::raw::c_void) -> AlphaTexture { + let mut id: GLuint = 0; + unsafe { + gl::PixelStorei(gl::UNPACK_ALIGNMENT, 1); + gl::GenTextures(1, &mut id); + gl::BindTexture(gl::TEXTURE_2D, id); + gl::TexImage2D( + gl::TEXTURE_2D, + 0, + gl::RED as i32, + width as i32, + height as i32, + 0, + gl::RED, + gl::UNSIGNED_BYTE, + bytes + ); + + gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, gl::CLAMP_TO_EDGE as i32); + gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, gl::CLAMP_TO_EDGE as i32); + gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::LINEAR as i32); + gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::LINEAR as i32); + + gl::BindTexture(gl::TEXTURE_2D, 0); + } + + AlphaTexture { + id: id, + width: width, + height: height, + } + } } diff --git a/src/text.rs b/src/text.rs index 5694e2ce..14a0cfb7 100644 --- a/src/text.rs +++ b/src/text.rs @@ -49,12 +49,12 @@ impl Rasterizer { } #[derive(Debug)] -struct RasterizedGlyph { - width: usize, - height: usize, - top: usize, - left: usize, - buf: Vec<u8>, +pub struct RasterizedGlyph { + pub width: usize, + pub height: usize, + pub top: usize, + pub left: usize, + pub buf: Vec<u8>, } |