stbtt text testing succesful
This commit is contained in:
@@ -55,14 +55,12 @@ void App::init()
|
||||
static const char* shader_font_v =
|
||||
"#version 150\n"
|
||||
"uniform mat4 mvp;"
|
||||
"uniform vec2 texoff;"
|
||||
"uniform vec2 texsz;"
|
||||
"in vec4 pos;"
|
||||
"in vec2 pos;"
|
||||
"in vec2 uvs;"
|
||||
"out vec2 uv;"
|
||||
"void main(){"
|
||||
" uv = texoff + uvs * texsz;"
|
||||
" gl_Position = mvp * vec4(pos.xyz, 1.f);"
|
||||
" uv = uvs;"
|
||||
" gl_Position = mvp * vec4(pos, 0, 1);"
|
||||
"}";
|
||||
static const char* shader_font_f =
|
||||
"#version 150\n"
|
||||
@@ -70,7 +68,8 @@ void App::init()
|
||||
"in vec2 uv;"
|
||||
"out vec4 frag;"
|
||||
"void main(){"
|
||||
" frag = texture(tex, uv.xy);"
|
||||
//" frag = vec4(1,0,0,1);"
|
||||
" frag = texture(tex, uv);"
|
||||
"}";
|
||||
|
||||
#ifdef _WIN32
|
||||
@@ -107,21 +106,35 @@ void App::init()
|
||||
if (!tex.load("data/uvs.jpg"))
|
||||
printf("error loading image\n");
|
||||
|
||||
#if _WIN32
|
||||
FILE* font_file = fopen("C:\\Windows\\Fonts\\arial.ttf", "rb");
|
||||
#else
|
||||
FILE* font_file = fopen("/Library/Fonts/Arial.ttf", "rb");
|
||||
#endif
|
||||
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 bytes = fread(data.get(), 1, sz, font_file);
|
||||
assert(bytes==sz);
|
||||
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());
|
||||
int ret = stbtt_BakeFontBitmap(data.get(), 0, 50, bitmap.get(), w, h, start_char, num_chars, chars.data());
|
||||
font_tex.create(w, h, GL_RED, bitmap.get());
|
||||
|
||||
glGenBuffers(2, font_buffers);
|
||||
glGenVertexArrays(1, &font_array);
|
||||
|
||||
glBindVertexArray(font_array);
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, font_buffers[1]);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, font_buffers[0]);
|
||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec4), (GLvoid*)0);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec4), (GLvoid*)(sizeof(float)*2));
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
plane.create<1>(400, 400);
|
||||
|
||||
@@ -159,6 +172,7 @@ void App::update(float dt)
|
||||
layout[main_id].update(width, height);
|
||||
}
|
||||
|
||||
/*
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
glEnable(GL_SCISSOR_TEST);
|
||||
for (auto& n : layout[main_id])
|
||||
@@ -171,10 +185,65 @@ void App::update(float dt)
|
||||
}
|
||||
}
|
||||
glDisable(GL_SCISSOR_TEST);
|
||||
|
||||
*/
|
||||
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));
|
||||
|
||||
|
||||
if (chars.size())
|
||||
{
|
||||
static char str[64];
|
||||
static int counter = 0;
|
||||
counter++;
|
||||
sprintf(str, "frame %04d", counter);
|
||||
const auto len = strlen(str);
|
||||
float x = 0;
|
||||
float y = 0;
|
||||
std::vector<glm::vec4> v;
|
||||
std::vector<GLushort> idx;
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
int c = str[i] - start_char;
|
||||
stbtt_aligned_quad q;
|
||||
stbtt_GetBakedQuad(chars.data(), w, h, c, &x, &y, &q, true);
|
||||
auto n = v.size();
|
||||
v.emplace_back(q.x0, q.y1, q.s0, q.t1);
|
||||
v.emplace_back(q.x0, q.y0, q.s0, q.t0);
|
||||
v.emplace_back(q.x1, q.y0, q.s1, q.t0);
|
||||
v.emplace_back(q.x1, q.y1, q.s1, q.t1);
|
||||
idx.push_back(n+0);
|
||||
idx.push_back(n+1);
|
||||
idx.push_back(n+2);
|
||||
idx.push_back(n+0);
|
||||
idx.push_back(n+2);
|
||||
idx.push_back(n+3);
|
||||
}
|
||||
font_array_count = (int)idx.size();
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, font_buffers[1]);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, idx.size() * sizeof(GLushort), idx.data(), GL_STATIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, font_buffers[0]);
|
||||
glBufferData(GL_ARRAY_BUFFER, v.size() * sizeof(glm::vec4), v.data(), GL_STATIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// plane.draw_fill();
|
||||
glBindVertexArray(font_array);
|
||||
glDrawElements(GL_TRIANGLES, font_array_count, GL_UNSIGNED_SHORT, 0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
font_tex.unbind();
|
||||
sampler.unbind();
|
||||
|
||||
|
||||
/*
|
||||
static int i = 0;
|
||||
i = (i + 1) % 32;
|
||||
auto c = chars["hgfrr56789opk0876ryuewighfcuisdbn"[i] - 32];
|
||||
@@ -195,6 +264,7 @@ void App::update(float dt)
|
||||
//plane.draw_fill();
|
||||
font_tex.unbind();
|
||||
sampler.unbind();
|
||||
*/
|
||||
}
|
||||
|
||||
void App::resize(float w, float h)
|
||||
|
||||
@@ -14,6 +14,13 @@ class App
|
||||
Texture2D font_tex;
|
||||
std::vector<stbtt_bakedchar> chars;
|
||||
Plane plane;
|
||||
GLuint font_array;
|
||||
int font_array_count = 0;
|
||||
GLuint font_buffers[2];
|
||||
const int w = 512;
|
||||
const int h = 512;
|
||||
const int num_chars = 96;
|
||||
const int start_char = 32;
|
||||
public:
|
||||
static App I;
|
||||
float width;
|
||||
|
||||
@@ -266,10 +266,10 @@ bool LayoutManager::load(const char* path)
|
||||
}
|
||||
printf("Parsing layout: %s\n", id_str);
|
||||
uint16_t id = const_hash(id_str);
|
||||
auto& p = m_layouts.try_emplace(id);
|
||||
if (p.second)
|
||||
auto p = m_layouts.find(id);
|
||||
if (p == m_layouts.end())
|
||||
{
|
||||
auto& node = p.first->second;
|
||||
auto& node = m_layouts[id];
|
||||
node.m_manager = this;
|
||||
// try to copy the old size values
|
||||
if (old.count(id))
|
||||
|
||||
@@ -87,7 +87,7 @@ public:
|
||||
ShaderManager::u_vec4(kShaderUniform::Col, m_border_color);
|
||||
m_plane.draw_stroke();
|
||||
}
|
||||
virtual void parse_attributes(kAttribute id, const tinyxml2::XMLAttribute* attr)
|
||||
virtual void parse_attributes(kAttribute id, const tinyxml2::XMLAttribute* attr) override
|
||||
{
|
||||
switch (id)
|
||||
{
|
||||
@@ -114,6 +114,8 @@ public:
|
||||
case kAttribute::Thickness:
|
||||
m_thinkness = attr->FloatValue();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -169,7 +171,7 @@ public:
|
||||
ShaderManager::u_vec4(kShaderUniform::Col, m_border_color);
|
||||
m_shape->draw_stroke();
|
||||
}
|
||||
virtual void parse_attributes(kAttribute id, const tinyxml2::XMLAttribute* attr)
|
||||
virtual void parse_attributes(kAttribute id, const tinyxml2::XMLAttribute* attr) override
|
||||
{
|
||||
switch (id)
|
||||
{
|
||||
@@ -199,6 +201,8 @@ public:
|
||||
case kAttribute::Type:
|
||||
m_type = (kShapeType)const_hash(attr->Value());
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
#include "pch.h"
|
||||
#include "util.hpp"
|
||||
#include "util.h"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user