-
Notifications
You must be signed in to change notification settings - Fork 11
/
shader.h
216 lines (214 loc) · 16.7 KB
/
shader.h
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* Copyright (C) 2021 Koji Takeo <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License v2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 021110-1307, USA.
*/
static const gchar *v_code =
"#version 300 es \n"
"precision highp float; \n"
" \n"
"in vec2 pv; \n"
"uniform sampler2D image; \n"
"uniform sampler2D tbl; \n"
"uniform mat3 rmat; \n"
"uniform vec2[14] gap; \n"
"uniform bool skip_stitch; \n"
"out vec4 texcoord; \n"
"out float v_y; \n"
"out float va_y; \n"
"out vec2 pos; \n"
" \n"
"#define PI 3.14159265358 \n"
" \n"
"vec2 \n"
"rot_coord(vec2 n_coord, mat3 m) \n"
"{ \n"
" vec2 s_coord; \n"
" vec2 cv, sv; \n"
" vec3 pos; \n"
" float tn, pn; \n"
" \n"
" s_coord = (n_coord + vec2(1., -1.)) * vec2(PI, -PI/2.); \n"
" cv = cos(s_coord); \n"
" sv = sin(s_coord); \n"
" pos = m * vec3(sv.y*cv.x, sv.y*sv.x, cv.y); \n"
" \n"
" tn = atan(pos.y, pos.x); \n"
" if (tn < 0.) \n"
" tn += 2.*PI; \n"
" \n"
" pn = acos(pos.z); \n"
" \n"
" return vec2(tn/(2.*PI), pn/PI); \n"
"} \n"
" \n"
"vec4 \n"
"interpolate_tbl4(sampler2D tbl, vec2 p) \n"
"{ \n"
" vec4 pp[4]; \n"
" vec2 d; \n"
" ivec2 pb; \n"
" \n"
" d = p - floor(p); \n"
" pb = ivec2(floor(p)); \n"
" \n"
" pp[0] = texelFetch(tbl, pb, 0); \n"
" pp[1] = texelFetch(tbl, pb + ivec2(1,0), 0); \n"
" pp[2] = texelFetch(tbl, pb + ivec2(0,1), 0); \n"
" pp[3] = texelFetch(tbl, pb + ivec2(1,1), 0); \n"
" \n"
" vec4 r1 = mix(pp[0], pp[1], d.x);//(1.-d.x)*pp[0] + d.x * pp[1]; \n"
" vec4 r2 = mix(pp[2], pp[3], d.x);//(1.-d.x)*pp[2] + d.x * pp[3]; \n"
" \n"
" return mix(r1, r2, d.y);//(1-d.y)*r1+d.y * r2; \n"
"} \n"
" \n"
"vec4 \n"
"interpolate_tbl(sampler2D tbl, vec2 pf, vec2 pr) \n"
"{ \n"
" vec4 af, ar; \n"
" \n"
" af = interpolate_tbl4(tbl, pf); \n"
" ar = interpolate_tbl4(tbl, pr); \n"
" \n"
" return vec4(af.xy, ar.zw); \n"
"} \n"
" \n"
"vec2 \n"
"modify_tbl(vec2 p, ivec2 sz) \n"
"{ \n"
" ivec2 node; \n"
" vec2 basegap; \n"
" float ratio; \n"
" \n"
" node = ivec2(floor(p)); \n"
" if (abs(node.y - sz.y/2) > sz.y/4) \n"
" return p; \n"
" \n"
" int blk, s, e; \n"
" blk = node.x/12; \n"
" s = node.x - (blk * 12 + 6) > 0 ? blk : blk - 1; \n"
" e = s + 1; \n"
" \n"
" float residual; \n"
" residual = (p.x - float(s * 12 + 6))/12.; \n"
" \n"
" if (s < 0) \n"
" s += 14; \n"
" if (e > 13) \n"
" e -= 14; \n"
" \n"
" basegap = mix(gap[s], gap[e], residual); \n"
" ratio = (float(node.y)-float(sz.y)/4.)*4./float(sz.y); \n"
" p += mix(vec2(0., 0.), basegap, ratio); \n"
" return p; \n"
"} \n"
" \n"
"void \n"
"main(void) \n"
"{ \n"
" vec2 p, pf, pm; \n"
" ivec2 sz; \n"
" gl_Position = vec4(pv, 0., 1.); \n"
" if (skip_stitch) { \n"
" texcoord = vec4(pv, 0., 1.); \n"
" } else { \n"
" p = rot_coord(pv, rmat); \n"
" sz = textureSize(tbl, 0) -ivec2(1,2); \n"
" pf = p *vec2(sz); \n"
" pm = modify_tbl(pf, sz); \n"
" texcoord = interpolate_tbl(tbl, pf, pm); \n"
" } \n"
" va_y = p.y*2.-1.; \n"
"} \n";
static const gchar *f_code =
"#version 300 es \n"
"precision highp float; \n"
" \n"
"in vec4 texcoord; \n"
"in float va_y; \n"
" \n"
"uniform sampler2D image; \n"
"uniform sampler2D tbl; \n"
"uniform mat3 rmat; \n"
"uniform bool skip_stitch; \n"
" \n"
"out vec4 fc; \n"
" \n"
"#define PI 3.14159265358 \n"
" \n"
"vec2 \n"
"rot_coord(vec2 n_coord, mat3 m) \n"
"{ \n"
" vec2 s_coord; \n"
" vec2 cv, sv; \n"
" vec3 pos; \n"
" float tn, pn; \n"
" \n"
" s_coord = (n_coord + vec2(1., 1.)) * vec2(PI, PI/2.); \n"
" cv = cos(s_coord); \n"
" sv = sin(s_coord); \n"
" pos = m * vec3(sv.y*cv.x, sv.y*sv.x, cv.y); \n"
" \n"
" tn = atan(pos.y, pos.x); \n"
" if (tn < 0.) \n"
" tn += 2.*PI; \n"
" \n"
" pn = acos(pos.z); \n"
" \n"
" return vec2(tn/(2.*PI), pn/PI); \n"
"} \n"
" \n"
"vec4[2] \n"
"pix(vec4 p, sampler2D s) \n"
"{ \n"
" vec4 v, r0, r1; \n"
" vec2 offset; \n"
" \n"
" v = p * vec4(0.5, 1., 0.5, 1.); \n"
" r0 = texture(s, v.xy+vec2(0.5, 0)); \n"
" r1 = texture(s, v.zw); \n"
" \n"
" return vec4[2](r0, r1); \n"
"} \n"
" \n"
"float \n"
"alpha(float y) \n"
"{ \n"
" float a; \n"
" a = 0.5 - (y / 0.02); \n"
" if (a < 0.) a = 0.; \n"
" if (a > 1.) \n"
" a = 1.; \n"
" \n"
" return a; \n"
"} \n"
" \n"
"void \n"
"main(void) \n"
"{ \n"
" float a; \n"
" vec4[2] v0; \n"
" \n"
" a = alpha(va_y); \n"
" \n"
" if (skip_stitch) { \n"
" vec2 p = rot_coord(texcoord.xy, rmat); \n"
" fc = texture(image, p); \n"
" } else { \n"
" v0 = pix(texcoord, image); \n"
" fc = mix(v0[1], v0[0], a); \n"
" } \n"
"} \n";