testing bitmap font generation and draw using stb_truetype

This commit is contained in:
2017-02-02 21:54:48 +00:00
parent 06b19dc596
commit d2f59c3ea1
6 changed files with 97 additions and 16 deletions

View File

@@ -52,6 +52,26 @@ void App::init()
"void main(){"
" frag = col;"
"}";
static const char* shader_font_v =
"#version 150\n"
"uniform mat4 mvp;"
"uniform vec2 texoff;"
"uniform vec2 texsz;"
"in vec4 pos;"
"in vec2 uvs;"
"out vec2 uv;"
"void main(){"
" uv = texoff + uvs * texsz;"
" gl_Position = mvp * vec4(pos.xyz, 1.f);"
"}";
static const char* shader_font_f =
"#version 150\n"
"uniform sampler2D tex;"
"in vec2 uv;"
"out vec4 frag;"
"void main(){"
" frag = texture(tex, uv.xy);"
"}";
#ifdef _WIN32
static CONSOLE_SCREEN_BUFFER_INFO info;
@@ -77,14 +97,33 @@ void App::init()
layout.load("data/layout.xml");
//layout["main"].update(width, height);
sampler.create();
sampler.create(GL_NEAREST);
ShaderManager::create(kShader::Texture, shader_v, shader_f);
ShaderManager::create(kShader::Color, shader_color_v, shader_color_f);
ShaderManager::create(kShader::UVs, shader_v, shader_uv_f);
ShaderManager::create(kShader::Font, shader_font_v, shader_font_f);
WidgetBorder::m_plane.create<1>(1, 1);
if (!tex.load("data/uvs.jpg"))
printf("error loading image\n");
FILE* font_file = fopen("C:\\Windows\\Fonts\\arial.ttf", "rb");
if (font_file)
{
fseek(font_file, 0, SEEK_END);
long sz = ftell(font_file);
auto data = std::make_unique<uint8_t[]>(sz);
fseek(font_file, 0, SEEK_SET);
int bytes = fread(data.get(), 1, sz, font_file);
int w = 512;
int h = 512;
int num_chars = 96;
auto bitmap = std::make_unique<uint8_t[]>(w*h);
chars.resize(num_chars);
int ret = stbtt_BakeFontBitmap(data.get(), 0, 50, bitmap.get(), w, h, 32, num_chars, chars.data());
font_tex.create(w, h, GL_RED, bitmap.get());
}
plane.create<1>(400, 400);
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
@@ -120,12 +159,6 @@ void App::update(float dt)
layout[main_id].update(width, height);
}
glActiveTexture(GL_TEXTURE0);
tex.bind();
sampler.bind(0);
ShaderManager::use(kShader::Texture);
ShaderManager::u_int(kShaderUniform::Tex, 0);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_SCISSOR_TEST);
for (auto& n : layout[main_id])
@@ -138,7 +171,29 @@ void App::update(float dt)
}
}
glDisable(GL_SCISSOR_TEST);
tex.unbind();
glm::mat4 proj = glm::ortho(0.f, width, height, 0.f, -1.f, 1.f);
glm::mat4 tran = glm::translate(glm::vec3(200, 200, 0));
static int i = 0;
i = (i + 1) % 32;
auto c = chars["hgfrr56789opk0876ryuewighfcuisdbn"[i] - 32];
glm::vec2 a(c.x0, c.y0);
glm::vec2 b(c.x1, c.y1);
glm::vec2 s(512, 512);
glm::vec2 tof = a / s;
glm::vec2 tsz = (b - a) / s;
glActiveTexture(GL_TEXTURE0);
font_tex.bind();
sampler.bind(0);
ShaderManager::use(kShader::Font);
ShaderManager::u_int(kShaderUniform::Tex, 0);
ShaderManager::u_mat4(kShaderUniform::MVP, proj * tran);
ShaderManager::u_vec2(kShaderUniform::Tof, tof);
ShaderManager::u_vec2(kShaderUniform::Tsz, tsz);
plane.draw_fill();
font_tex.unbind();
sampler.unbind();
}