forked from azsn/gllabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.cpp
256 lines (220 loc) · 7.34 KB
/
demo.cpp
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* Aidan Shafran <[email protected]>, 2017.
*
* Demo code for GLLabel. Depends on GLFW3, GLEW, GLM, FreeType2, and C++11.
* Currently requires Noto Sans and Droid Sans fonts installed.
* Only tested on Arch Linux.
*/
#include "label.hpp"
#include <glfw3.h>
#include <glm/gtx/transform.hpp>
#include <codecvt>
#include <iomanip>
static uint32_t width = 1280;
static uint32_t height = 800;
static GLLabel *Label;
static bool spin = false;
static FT_Face defaultFace;
static FT_Face boldFace;
float horizontalTransform = 0.0;
float verticalTransform = 0.8;
float scale = 1;
void onKeyPress(GLFWwindow *window, int key, int scanCode, int action, int mods);
void onCharTyped(GLFWwindow *window, unsigned int codePoint, int mods);
void onScroll(GLFWwindow *window, double deltaX, double deltaY);
void onResize(GLFWwindow *window, int width, int height);
std::u32string toUTF32(const std::string &s);
static glm::vec3 pt(float pt);
int main()
{
// Create a window
if(!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW.\n");
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 8);
glfwWindowHint(GLFW_DEPTH_BITS, 0);
glfwWindowHint(GLFW_STENCIL_BITS, 0);
glfwWindowHint(GLFW_ALPHA_BITS, 8);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *window = glfwCreateWindow(width, height, "Vector-Based GPU Text Rendering", NULL, NULL);
if(!window)
{
fprintf(stderr, "Failed to create GLFW window.");
glfwTerminate();
return -1;
}
glfwSetKeyCallback(window, onKeyPress);
glfwSetCharModsCallback(window, onCharTyped);
glfwSetScrollCallback(window, onScroll);
glfwSetWindowSizeCallback(window, onResize);
// Create OpenGL context
glfwMakeContextCurrent(window);
glewExperimental = true;
if(glewInit() != GLEW_OK)
{
fprintf(stderr, "Failed to initialize GLEW.\n");
glfwDestroyWindow(window);
glfwTerminate();
return -1;
}
printf("GL Version: %s\n", glGetString(GL_VERSION));
GLuint vertexArrayId;
glGenVertexArrays(1, &vertexArrayId);
glBindVertexArray(vertexArrayId);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
// Create new label
Label = new GLLabel();
Label->ShowCaret(true);
printf("Loading font files. If this crashes, you probably don't have Droid Sans and Noto Sans installed at /usr/share/fonts.\n");
defaultFace = GLFontManager::GetFontManager()->GetDefaultFont();
boldFace = GLFontManager::GetFontManager()->GetFontFromPath("/usr/share/fonts/TTF/DroidSans-Bold.ttf");
Label->SetText(U"Welcome to vector-based GPU text rendering!\nType whatver you want!\n\nPress LEFT/RIGHT to move cursor.\nPress ESC to toggle rotate.\nScroll vertically/horizontally to move.\nScroll while holding shift to zoom.\nRight-shift for bold.\nHold ALT to type in ", 1, glm::vec4(0.5,0,0,1), defaultFace);
Label->AppendText(U"r", 1, glm::vec4(0.58, 0, 0.83, 1), defaultFace);
Label->AppendText(U"a", 1, glm::vec4(0.29, 0, 0.51, 1), defaultFace);
Label->AppendText(U"i", 1, glm::vec4(0, 0, 1, 1), defaultFace);
Label->AppendText(U"n", 1, glm::vec4(0, 1, 0, 1), defaultFace);
Label->AppendText(U"b", 1, glm::vec4(1, 1, 0, 1), defaultFace);
Label->AppendText(U"o", 1, glm::vec4(1, 0.5, 0, 1), defaultFace);
Label->AppendText(U"w", 1, glm::vec4(1, 0, 0, 1), defaultFace);
Label->AppendText(U"!\n", 1, glm::vec4(0.5,0,0,1), defaultFace);
Label->SetCaretPosition(Label->GetText().size());
GLLabel fpsLabel;
int fpsFrame = 0;
double fpsStartTime = glfwGetTime();
while(!glfwWindowShouldClose(window))
{
float time = glfwGetTime();
glClearColor(160/255.0, 169/255.0, 175/255.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
if(spin)
{
glm::mat4 mat = glm::scale(glm::mat4(),
glm::vec3(sin(time)/6000.0, cos(time)/12000.0, 1.0));
mat = glm::rotate(mat, time/3, glm::vec3(0.0,0.0,1.0));
Label->Render(time, mat);
}
else
{
glm::mat4 kS = glm::scale(glm::mat4(), pt(8));
glm::mat4 mat = glm::scale(glm::mat4(), glm::vec3(scale, scale, 1.0));
mat = glm::translate(mat,
glm::vec3(-0.9 + horizontalTransform, verticalTransform, 0));
Label->Render(time, mat*kS);
}
// Window size might change, so recalculate this (and other pt() calls)
glm::mat4 fpsTransform = glm::scale(glm::translate(glm::mat4(), glm::vec3(-1, 0.86, 0)), pt(10));
fpsLabel.Render(time, fpsTransform);
glfwPollEvents();
glfwSwapBuffers(window);
// FPS Counter
fpsFrame ++;
if(fpsFrame >= 20)
{
double endTime = glfwGetTime();
double fps = fpsFrame / (endTime - fpsStartTime);
fpsFrame = 0;
fpsStartTime = endTime;
std::ostringstream stream;
stream << "FPS: ";
stream << std::fixed << std::setprecision(1) << fps;
fpsLabel.SetText(toUTF32(stream.str()), 1, glm::vec4(0,0,0,1), defaultFace);
}
}
// Exit
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
static bool leftShift = false;
static bool rightShift = false;
void onKeyPress(GLFWwindow *window, int key, int scanCode, int action, int mods)
{
if(action == GLFW_PRESS && key == GLFW_KEY_LEFT_SHIFT)
leftShift = true;
else if(action == GLFW_RELEASE && key == GLFW_KEY_LEFT_SHIFT)
leftShift = false;
else if(action == GLFW_PRESS && key == GLFW_KEY_RIGHT_SHIFT)
rightShift = true;
else if(action == GLFW_RELEASE && key == GLFW_KEY_RIGHT_SHIFT)
rightShift = false;
if(action == GLFW_RELEASE)
return;
if(key == GLFW_KEY_BACKSPACE)
{
std::u32string text = Label->GetText();
if(text.size() > 0 && Label->GetCaretPosition() > 0)
{
Label->RemoveText(Label->GetCaretPosition()-1, 1);
Label->SetCaretPosition(Label->GetCaretPosition() - 1);
}
}
else if(key == GLFW_KEY_ENTER)
{
Label->InsertText(U"\n", Label->GetCaretPosition(), 1, glm::vec4(0,0,0,1), rightShift?boldFace:defaultFace);
Label->SetCaretPosition(Label->GetCaretPosition() + 1);
}
else if(key == GLFW_KEY_ESCAPE)
{
spin = !spin;
}
else if(key == GLFW_KEY_LEFT)
{
Label->SetCaretPosition(Label->GetCaretPosition() - 1);
}
else if(key == GLFW_KEY_RIGHT)
{
Label->SetCaretPosition(Label->GetCaretPosition() + 1);
}
}
void onCharTyped(GLFWwindow *window, unsigned int codePoint, int mods)
{
double r0 = 0, r1 = 0, r2 = 0;
if((mods & GLFW_MOD_ALT) == GLFW_MOD_ALT)
{
r0 = ((double) rand() / (RAND_MAX-1));
r1 = ((double) rand() / (RAND_MAX-1));
r2 = ((double) rand() / (RAND_MAX-1));
}
Label->InsertText(std::u32string(1, codePoint), Label->GetCaretPosition(), 1, glm::vec4(r0,r1,r2,1), rightShift?boldFace:defaultFace);
Label->SetCaretPosition(Label->GetCaretPosition() + 1);
}
void onScroll(GLFWwindow *window, double deltaX, double deltaY)
{
if(leftShift)
{
scale += 0.1*deltaY;
if(scale < 0.1)
scale = 0.1;
}
else
{
horizontalTransform += 0.1*deltaX/scale;
verticalTransform -= 0.1*deltaY/scale;
}
}
void onResize(GLFWwindow *window, int w, int h)
{
width = w;
height = h;
glViewport(0,0,w,h);
}
std::u32string toUTF32(const std::string &s)
{
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> conv;
return conv.from_bytes(s);
}
// Converts font points into a glm::vec3 scalar.
static glm::vec3 pt(float pt)
{
static const float dpiScale = 96.0 / 72.0;
static const float emUnits = 1.0/2048.0;
const float aspect = (float)height / (float)width;
float scale = emUnits * pt / 72.0;
return glm::vec3(scale * aspect, scale, 0);
}