testing bitmap font generation and draw using stb_truetype
This commit is contained in:
@@ -3,21 +3,24 @@
|
||||
<layout id="button">
|
||||
<border width="50" margin="0 5 0 0" color=".1" thickness="2" border-color=".5"></border>
|
||||
</layout>
|
||||
<layout id="multi-button-item">
|
||||
<border height="50%" width="50%" color=".1" thickness="2" border-color=".5" />
|
||||
</layout>
|
||||
<layout id="multi-button">
|
||||
<node width="50" margin="0 5 0 0" dir="row" wrap="1">
|
||||
<border height="50%" width="50%" color=".1" thickness="2" border-color=".5" />
|
||||
<border height="50%" width="50%" color=".1" thickness="2" border-color=".5" />
|
||||
<border height="50%" width="50%" color=".1" thickness="2" border-color=".5" />
|
||||
<border height="50%" width="50%" color=".1" thickness="2" border-color=".5" />
|
||||
<ref id="multi-button-item"/>
|
||||
<ref id="multi-button-item"/>
|
||||
<ref id="multi-button-item"/>
|
||||
<ref id="multi-button-item"/>
|
||||
</node>
|
||||
</layout>
|
||||
<layout id="main">
|
||||
<node dir="col" wrap="0" width="100%" height="100%" pad="5">
|
||||
<!-- toolbar -->
|
||||
<border height="50" width="100%" pad="5" dir="row" color=".2">
|
||||
<ref id="multi-button"></ref>
|
||||
<ref id="multi-button"></ref>
|
||||
<ref id="multi-button"></ref>
|
||||
<ref id="multi-button"/>
|
||||
<ref id="multi-button"/>
|
||||
<ref id="multi-button"/>
|
||||
<ref id="button"></ref>
|
||||
<border width="50" margin="0 5 0 0" color=".1" thickness="2" border-color="1"></border>
|
||||
<border width="50" margin="0 5 0 0" color=".2" thickness="2" border-color="1"></border>
|
||||
|
||||
@@ -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,15 +97,34 @@ 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);
|
||||
glPointSize(5);
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,10 @@ class App
|
||||
Sampler sampler;
|
||||
Texture2D tex;
|
||||
LayoutManager layout;
|
||||
stbtt_fontinfo font;
|
||||
Texture2D font_tex;
|
||||
std::vector<stbtt_bakedchar> chars;
|
||||
Plane plane;
|
||||
public:
|
||||
static App I;
|
||||
float width;
|
||||
|
||||
@@ -32,5 +32,8 @@
|
||||
#include <glm/gtx/euler_angles.hpp>
|
||||
|
||||
#include <tinyxml2.h>
|
||||
#include <yoga/Yoga.h>
|
||||
#endif
|
||||
|
||||
#include <yoga/Yoga.h>
|
||||
#include <stb/stb_truetype.h>
|
||||
#include <stb/stb_image.h>
|
||||
|
||||
@@ -104,6 +104,12 @@ void Shader::u_vec4(kShaderUniform id, const glm::vec4& v)
|
||||
{
|
||||
glUniform4fv(m_umap[id], 1, glm::value_ptr(v));
|
||||
}
|
||||
|
||||
void Shader::u_vec2(kShaderUniform id, const glm::vec2& v)
|
||||
{
|
||||
glUniform2fv(m_umap[id], 1, glm::value_ptr(v));
|
||||
}
|
||||
|
||||
void Shader::u_mat4(kShaderUniform id, const glm::mat4& m)
|
||||
{
|
||||
glUniformMatrix4fv(m_umap[id], 1, GL_FALSE, glm::value_ptr(m));
|
||||
@@ -135,6 +141,11 @@ void ShaderManager::u_vec4(kShaderUniform id, const glm::vec4& v)
|
||||
m_current->u_vec4(id, v);
|
||||
}
|
||||
|
||||
void ShaderManager::u_vec2(kShaderUniform id, const glm::vec2& v)
|
||||
{
|
||||
m_current->u_vec2(id, v);
|
||||
}
|
||||
|
||||
void ShaderManager::u_mat4(kShaderUniform id, const glm::mat4& m)
|
||||
{
|
||||
m_current->u_mat4(id, m);
|
||||
|
||||
@@ -6,6 +6,8 @@ enum class kShaderUniform : uint16_t
|
||||
MVP = const_hash("mvp"),
|
||||
Tex = const_hash("tex"),
|
||||
Col = const_hash("col"),
|
||||
Tof = const_hash("texoff"),
|
||||
Tsz = const_hash("texsz"),
|
||||
};
|
||||
|
||||
class Shader
|
||||
@@ -16,6 +18,7 @@ public:
|
||||
bool create(const char* vertex, const char* fragment);
|
||||
void use();
|
||||
void u_vec4(kShaderUniform id, const glm::vec4& v);
|
||||
void u_vec2(kShaderUniform id, const glm::vec2& v);
|
||||
void u_mat4(kShaderUniform id, const glm::mat4& m);
|
||||
void u_int(kShaderUniform id, int i);
|
||||
};
|
||||
@@ -25,6 +28,7 @@ enum class kShader : uint16_t
|
||||
Color = const_hash("color"),
|
||||
Texture = const_hash("texture"),
|
||||
UVs = const_hash("uvs"),
|
||||
Font = const_hash("font"),
|
||||
};
|
||||
|
||||
class ShaderManager
|
||||
@@ -36,6 +40,7 @@ public:
|
||||
static void use(kShader id);
|
||||
static void use(const char* name);
|
||||
static void u_vec4(kShaderUniform id, const glm::vec4& v);
|
||||
static void u_vec2(kShaderUniform id, const glm::vec2& v);
|
||||
static void u_mat4(kShaderUniform id, const glm::mat4& m);
|
||||
static void u_int(kShaderUniform id, int i);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user