From 2e4d2f9e4476d6416489e4c63a104a278f96164a Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 25 Oct 2016 19:44:55 +0530 Subject: [PATCH] Implement char rendering --- kitty/develop_gl.py | 28 ++++++++++++++++------------ kitty/fonts.py | 4 ++++ kitty/shaders.py | 4 ++-- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/kitty/develop_gl.py b/kitty/develop_gl.py index 39744f6e1..48241c9f5 100644 --- a/kitty/develop_gl.py +++ b/kitty/develop_gl.py @@ -5,10 +5,9 @@ import glfw import OpenGL.GL as gl import sys -from PIL import Image -import numpy from kitty.shaders import ShaderProgram, array +from kitty.fonts import set_font_family, render_cell, cell_size vertex_shader = """ # version 410 @@ -143,30 +142,34 @@ textured_shaders = ( #version 410 in vec2 vertex; in vec2 texture_position; -out vec2 texture_position_out; +out vec2 texture_position_for_fs; void main() { gl_Position = vec4(vertex, 0, 1); - texture_position_out = texture_position; + texture_position_for_fs = texture_position; } - ''', +''', ''' #version 410 uniform sampler2D tex; -in vec2 texture_position_out; +in vec2 texture_position_for_fs; out vec4 final_color; +const vec3 background = vec3(0, 1, 0); +const vec3 foreground = vec3(0, 0, 1); void main() { - final_color = texture(tex, texture_position_out); + float alpha = texture(tex, texture_position_for_fs).r; + vec3 color = background * (1 - alpha) + foreground * alpha; + final_color = vec4(color, 1); } - ''') +''') def texture_data(): - img = Image.open('/home/kovid/work/calibre/resources/images/library.png') - img_data = numpy.array(list(img.getdata()), numpy.int8) - return img_data, img.size[0], img.size[1] + cell = render_cell('K')[0] + w, h = cell_size() + return cell, w, h def rectangle_texture(window): @@ -197,9 +200,10 @@ def on_error(code, msg): def main(): + glfw.glfwSetErrorCallback(on_error) if not glfw.glfwInit(): raise SystemExit('GLFW initialization failed') - glfw.glfwSetErrorCallback(on_error) + set_font_family('monospace', 144) try: _main() finally: diff --git a/kitty/fonts.py b/kitty/fonts.py index 2ed6fa617..c35b798af 100644 --- a/kitty/fonts.py +++ b/kitty/fonts.py @@ -262,6 +262,10 @@ def display_bitmap(data, w, h): img.show() +def cell_size(): + return cell_width, cell_height + + def test_rendering(text='Ping👁a', sz=144, family='monospace'): set_font_family(family, sz) cells = [] diff --git a/kitty/shaders.py b/kitty/shaders.py index 8d395f99d..e433666e0 100644 --- a/kitty/shaders.py +++ b/kitty/shaders.py @@ -87,7 +87,7 @@ class ShaderProgram: gl.glUseProgram(0) self.is_active = False - def set_2d_texture(self, var_name, data, width, height, data_type='rgba', + def set_2d_texture(self, var_name, data, width, height, data_type='red', min_filter=gl.GL_LINEAR, mag_filter=gl.GL_LINEAR, swrap=gl.GL_CLAMP_TO_EDGE, twrap=gl.GL_CLAMP_TO_EDGE): texture_id = self.texture_id = gl.glGenTextures(1) @@ -101,7 +101,7 @@ class ShaderProgram: internal_format, external_format = { 'rgba': (gl.GL_RGBA8, gl.GL_RGBA), 'rgb': (gl.GL_RGB8, gl.GL_RGB), - 'red': (gl.GL_RED, gl.GL_RED), + 'red': (gl.GL_R8, gl.GL_RED), }[data_type] gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, internal_format, width, height, 0, external_format, gl.GL_UNSIGNED_BYTE, data)