Char grid now works. Also downgrade GL requirement to 3.3

This commit is contained in:
Kovid Goyal 2016-10-27 22:48:03 +05:30
parent 4495859164
commit 6eda70331a
2 changed files with 13 additions and 64 deletions

View File

@ -6,7 +6,7 @@ import glfw
import OpenGL.GL as gl
import sys
from kitty.shaders import ShaderProgram, GL_VERSION, ortho_matrix
from kitty.shaders import ShaderProgram, GL_VERSION
from kitty.fonts import set_font_family, cell_size
textured_shaders = (
@ -14,10 +14,9 @@ textured_shaders = (
in vec2 vertex;
in vec3 texture_position;
out vec3 texture_position_for_fs;
uniform mat4 transform;
void main() {
gl_Position = transform * vec4(vertex, 0, 1);
gl_Position = vec4(vertex, 0, 1);
texture_position_for_fs = texture_position;
}
''',
@ -27,8 +26,8 @@ uniform sampler2DArray sprites;
uniform vec3 sprite_scale;
in vec3 texture_position_for_fs;
out vec4 final_color;
const vec3 background = vec3(0, 1, 0);
const vec3 foreground = vec3(0, 0, 1);
const vec3 background = vec3(0, 0, 1);
const vec3 foreground = vec3(0, 1, 0);
void main() {
float alpha = texture(sprites, texture_position_for_fs / sprite_scale).r;
@ -76,17 +75,23 @@ class Renderer:
vertices = (gl.GLfloat * (xnum * ynum * 12))()
uv = (gl.GLfloat * (xnum * ynum * 18))()
num = 0
dx, dy = 2 * cell_width / self.w, 2 * cell_height / self.h
xmargin = (self.w - (xnum * cell_width)) / self.w
ymargin = (self.h - (ynum * cell_height)) / self.h
xstart = -1 + xmargin
ystart = 1 - ymargin
for r in range(ynum):
aoff = r * xnum * 12
uoff = r * xnum * 18
top = ystart - r * dy
for c in range(xnum):
left = xstart + c * dx
off = aoff + c * 12
vertices[off:off + 12] = rectangle_vertices(left=c, top=r, right=c + 1, bottom=r + 1)
vertices[off:off + 12] = rectangle_vertices(left=left, top=top, right=left + dx, bottom=top - dy)
sprite_pos = self.sprite_map[num % 10]
off = uoff + c * 18
uv[off:off + 18] = rectangle_uv(*sprite_pos)
num += 1
self.transform = ortho_matrix(right=xnum, bottom=ynum)
with self.program:
self.program.set_attribute_data('vertex', vertices)
self.program.set_attribute_data('texture_position', uv, items_per_attribute_value=3)
@ -94,7 +99,6 @@ class Renderer:
def render(self):
with self.program:
gl.glUniformMatrix4fv(self.program.uniform_location('transform'), 1, gl.GL_TRUE, self.transform)
gl.glDrawArrays(gl.GL_TRIANGLES, 0, self.num_vertices)

View File

@ -10,7 +10,7 @@ from OpenGL.GL.ARB.copy_image import glCopyImageSubData # only present in openg
from .fonts import render_cell, cell_size
GL_VERSION = (4, 1)
GL_VERSION = (3, 3)
VERSION = GL_VERSION[0] * 100 + GL_VERSION[1] * 10
@ -18,61 +18,6 @@ def array(*args, dtype=gl.GLfloat):
return (dtype * len(args))(*args)
def translation_matrix(x, y):
return array(
1, 0, x,
0, 1, y,
0, 0, 1
)
def scaling_matrix(x, y):
return array(
x, 0, 0,
0, y, 0,
0, 0, 1
)
def multiply(a, b):
# 0 1 2
# 3 4 5
# 6 7 8
return array(
# Row 1
a[0] * b[0] + a[1] * b[3] + a[2] * b[6],
a[0] * b[1] + a[1] * b[4] + a[2] * b[7],
a[0] * b[2] + a[1] * b[5] + a[2] * b[8],
# Row 2
a[3] * b[0] + a[4] * b[3] + a[5] * b[6],
a[3] * b[1] + a[4] * b[4] + a[5] * b[7],
a[3] * b[2] + a[4] * b[5] + a[5] * b[8],
# Row 3
a[6] * b[0] + a[7] * b[3] + a[8] * b[6],
a[6] * b[1] + a[7] * b[4] + a[8] * b[7],
a[6] * b[2] + a[7] * b[5] + a[8] * b[8]
)
def ortho_matrix(left=0, right=1, bottom=1, top=0, near=0, far=1):
# See https://www.opengl.org/sdk/docs/man2/xhtml/glOrtho.xml
def t(a, b):
return (a + b) / (a - b)
return array(
2 / (right - left), 0, 0, -t(right, left),
0, 2 / (bottom - top), 0, -t(bottom, top),
0, 0, -2 / (far - near), -t(far, near),
0, 0, 0, 1
)
def map_pos(matrix, x, y):
return (
x * matrix[0] + y * matrix[1] + matrix[3],
x * matrix[4] + y * matrix[5] + matrix[7]
)
class Sprites:
''' Maintain sprite sheets of all rendered characters on the GPU as a texture
array with each texture being a sprite sheet. '''