-
Notifications
You must be signed in to change notification settings - Fork 1
/
misc.py
55 lines (50 loc) · 1.84 KB
/
misc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import numpy as np
vertexShader = '''
#version 450
precision highp float;
in vec2 vert;
in vec2 in_text;
out vec2 texcoord;
void main() {
gl_Position = vec4(vert, 0.0, 1.0);
texcoord = in_text;
}
'''
def fragmentShader(names, index, content):
define = '''#version 450\nout vec4 color;\nin vec2 texcoord;'''
for name in names:
define += f'''
precision highp sampler2D;
uniform sampler2D {name};
#define {name}_pos texcoord
#define {name}_tex(pos) texture({name}, pos)
#define {name}_pt pt
#define {name}_size size
#define {name}_texOff(off) {name}_tex({name}_pos + {name}_pt * vec2(off))
'''
define += '''
layout(binding = ''' + str(index) + ''') buffer data
{
vec2 pt;
vec2 size;
};
'''
return define + content + "\nvoid main() {color = hook();}"
def calculate_vbo(bound: tuple, size: tuple, vbo, vbox, padding = 8):
x1, y1, x2, y2 = bound
w, h = size
vbox.write(np.array([
# x y u v
2 * x1 / w - 1, 1 - 2 * y2 / h, x1 / w, y2 / h, # lower left
2 * x1 / w - 1, 1 - 2 * y1 / h, x1 / w, y1 / h, # upper left
2 * x2 / w - 1, 1 - 2 * y2 / h, x2 / w, y2 / h, # lower right
2 * x2 / w - 1, 1 - 2 * y1 / h, x2 / w, y1 / h, # upper right
], dtype="f4"))
x1, y1, x2, y2 = max(x1 - padding, 0), h - max(y1 - padding, 0), min(x2 + padding, w), h - min(y2 + padding, h)
vbo.write(np.array([
# x y u v
2 * x1 / w - 1, 1 - 2 * y2 / h, x1 / w, 1 - y2 / h, # lower left
2 * x1 / w - 1, 1 - 2 * y1 / h, x1 / w, 1 - y1 / h, # upper left
2 * x2 / w - 1, 1 - 2 * y2 / h, x2 / w, 1 - y2 / h, # lower right
2 * x2 / w - 1, 1 - 2 * y1 / h, x2 / w, 1 - y1 / h, # upper right
], dtype="f4"))