-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrender.cc
237 lines (209 loc) · 5.83 KB
/
render.cc
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// run: apitrace trace -o $exec.trace $exec && qapitrace $exec.trace
// run-: valgrind --leak-check=full $exec
#include <string>
#include <memory>
#include <cmath>
#include "list-fonts.hh" // lib: fontconfig
#include "distance-transform.hh"
// lib: gl
#define GL_GLEXT_PROTOTYPES
#include <GL/glcorearb.h>
// lib: glfw3
#include <GLFW/glfw3.h>
#include <iostream>
using std::cerr;
namespace font
{
/*
template <int MAGNITUDE=10>
struct atlas
{
static constexpr auto size() { return (1 << MAGNITUDE); }
atlas() : tex{make_texture(size(), size())} {}
private:
gl::texture tex;
};
*/
}
namespace
{
auto make_shader(GLenum type, std::string const& src)
{
auto s = glCreateShader(type);
auto c = src.c_str();
glShaderSource(s, 1, &c, {});
glCompileShader(s);
return s;
}
auto make_program(std::string const& vss, std::string const& fss)
{
auto p = glCreateProgram();
glAttachShader(p, make_shader(GL_VERTEX_SHADER, vss));
glAttachShader(p, make_shader(GL_FRAGMENT_SHADER, fss));
glLinkProgram(p);
return p;
}
float invalid_pattern(int x, int y, int size)
{
constexpr auto len = 8;
auto cx = x - size/2;
auto cy = y - size/2;
auto dist = (float(cx*cx + cy*cy)) / len / 35;
if (dist > 1) dist = 1;
x = (x + y) % (len * 2);
x = std::abs(x - len);
return 1 - std::min(x / (float)len, dist);
}
auto invalid_pattern(int magnitude)
{
const auto size = (1 << magnitude);
std::unique_ptr<float[]> p{new float[size*size]};
for (int y=0; y<size; y++)
for (int x=0; x<size; x++)
p[(y<<magnitude)|x] = invalid_pattern(x, y, size);
return p;
}
}
int main()
{
auto fonts = tue::list_fonts("WenQuanYi Micro Hei");
cerr << fonts.size() << " fonts available\n";
auto fa = fonts[0].render(U'猫', 1024);
fa.padding(fa.h()/8, fa.w()/8);
tue::distance_transform(fa);
fa.clerp(-20, 50, 1, 0);
fa.scale_to_height(64);
// init host
glfwInit();
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
auto win = glfwCreateWindow(16*40, 16*40, "demo", {}, {});
glfwMakeContextCurrent(win);
glfwSwapInterval(1);
// setup debugging
struct programmers_fault {};
glDebugMessageCallback([](auto, auto, auto, auto, auto, auto msg, auto) {
static std::string last;
if (last == msg) throw programmers_fault{};
printf("\e[1;31m%s\e[0m\n", msg);
last = msg;
}, {});
// init gl
glClearColor(1, 1, 1, 1);
{ // array buffer
GLuint id;
glGenBuffers(1, &id);
glBindBuffer(GL_ARRAY_BUFFER, id);
}
{ // array object
GLuint id;
glGenVertexArrays(1, &id);
glBindVertexArray(id);
}
{ // texture
GLuint id;
glGenTextures(1, &id);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
}
constexpr auto tex_mag = 8;
constexpr auto tex_size = float(1 << tex_mag);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F,
1<<tex_mag, 1<<tex_mag, 0, GL_RED, GL_FLOAT,
invalid_pattern(tex_mag).get());
glTexSubImage2D(GL_TEXTURE_2D, 0,
0, 0, fa.w(), fa.h(),
GL_RED, GL_FLOAT, fa.data());
glGenerateMipmap(GL_TEXTURE_2D);
static float data[] = {
// points
-1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0,
-0.4, -0.4, 0.4, -0.4, -0.4, 0.4, 0.4, 0.4,
// uvs
0.4, 0.4, 0.7, 0.4, 0.4, 0.7, 0.7, 0.7,
0, 0, fa.w()/tex_size, 0, 0, fa.h()/tex_size, fa.w()/tex_size, fa.h()/tex_size,
};
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, false, 0, 0);
glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, (void*)(8*2*sizeof(float)));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
// enable blending
glEnable(GL_BLEND);
glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO);
// create program
auto filler = make_program(
R"vertex(#version 330 core
layout(location=0) in vec2 pos;
layout(location=1) in vec2 _uv;
out vec2 uv_;
out vec2 x_;
out vec2 y_;
uniform float time;
uniform int rotv; // rotate vertex
uniform float scale;
void main()
{
float c = cos(time);
float s = sin(time);
x_ = vec2(c, s);
y_ = vec2(-s, c);
if (rotv == 1) gl_Position = vec4((pos.x*x_ + pos.y*y_)*scale, 0, 1);
else gl_Position = vec4(pos*scale, 0, 1);
uv_ = _uv;
}
)vertex",
R"fragment(#version 330 core
in vec2 uv_;
in vec2 x_;
in vec2 y_;
out vec4 color;
uniform sampler2D tex;
uniform int rotv; // rotate vertex
void main()
{
//vec2 p = (gl_FragCoord.x * x_ + gl_FragCoord.y * y_) / 1000;
vec2 p;
if (rotv == 1) p = uv_;
else p = uv_.x * x_ + uv_.y * y_;
float a = texture(tex, p).r;
float ina = smoothstep(0.8-fwidth(a)+0.07, 0.8+0.07, a);
float outa = smoothstep(0.8-fwidth(a), 0.8, a);
color.rgb = mix(vec3(0, 0, 0), vec3(1, 0.5, 0), ina);
color.a = outa;
}
)fragment");
glUseProgram(filler);
glUniform1i(glGetUniformLocation(filler, "tex"), 0);
int utime = glGetUniformLocation(filler, "time");
int urotv = glGetUniformLocation(filler, "rotv");
int uscale = glGetUniformLocation(filler, "scale");
// mainloop
while (!glfwWindowShouldClose(win)) {
// resize
int w, h;
glfwGetFramebufferSize(win, &w, &h);
glViewport(0, 0, w, h);
// render
glClear(GL_COLOR_BUFFER_BIT);
glUniform1f(utime, glfwGetTime()/10);
glUniform1i(urotv, false);
glUniform1f(uscale, 1);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glUniform1f(utime, glfwGetTime()/2);
glUniform1i(urotv, true);
glUniform1f(uscale, sin(glfwGetTime()/2)*6);
glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);
// event dispatching
glfwSwapBuffers(win);
glfwPollEvents();
}
glfwTerminate();
}