stbtt text testing succesful

This commit is contained in:
2017-02-05 20:15:52 +00:00
parent e2792534bc
commit 7f8cbd0981
10 changed files with 113 additions and 22 deletions

View File

@@ -22,6 +22,8 @@
AD58E0761E3421F2006ACC15 /* YGNodeList.c in Sources */ = {isa = PBXBuildFile; fileRef = AD58E0741E3421F2006ACC15 /* YGNodeList.c */; };
AD58E0771E3421F2006ACC15 /* Yoga.c in Sources */ = {isa = PBXBuildFile; fileRef = AD58E0751E3421F2006ACC15 /* Yoga.c */; };
AD58E0791E342205006ACC15 /* tinyxml2.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AD58E0781E342205006ACC15 /* tinyxml2.cpp */; };
AD95AEC61E41EDEC002DD03A /* font.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AD95AEC31E41EDEC002DD03A /* font.cpp */; };
AD95AEC71E41EDEC002DD03A /* pch.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AD95AEC51E41EDEC002DD03A /* pch.cpp */; };
ADB61C821E3D38450093280F /* util.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ADB61C801E3D38450093280F /* util.cpp */; };
/* End PBXBuildFile section */
@@ -62,6 +64,9 @@
AD58E0741E3421F2006ACC15 /* YGNodeList.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = YGNodeList.c; path = libs/yoga/yoga/YGNodeList.c; sourceTree = "<group>"; };
AD58E0751E3421F2006ACC15 /* Yoga.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = Yoga.c; path = libs/yoga/yoga/Yoga.c; sourceTree = "<group>"; };
AD58E0781E342205006ACC15 /* tinyxml2.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = tinyxml2.cpp; path = libs/tinyxml2/tinyxml2.cpp; sourceTree = "<group>"; };
AD95AEC31E41EDEC002DD03A /* font.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = font.cpp; sourceTree = "<group>"; };
AD95AEC41E41EDEC002DD03A /* font.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = font.h; sourceTree = "<group>"; };
AD95AEC51E41EDEC002DD03A /* pch.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = pch.cpp; sourceTree = "<group>"; };
ADB61C801E3D38450093280F /* util.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = util.cpp; sourceTree = "<group>"; };
ADB61C811E3D38450093280F /* util.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = util.h; sourceTree = "<group>"; };
/* End PBXFileReference section */
@@ -106,6 +111,9 @@
AD58E0511E107411006ACC15 /* engine */ = {
isa = PBXGroup;
children = (
AD95AEC31E41EDEC002DD03A /* font.cpp */,
AD95AEC41E41EDEC002DD03A /* font.h */,
AD95AEC51E41EDEC002DD03A /* pch.cpp */,
AD3B1EBE1E3B8B7600E918E3 /* layout.cpp */,
AD3B1EBF1E3B8B7600E918E3 /* layout.h */,
AD58E0631E2A76FD006ACC15 /* shader.cpp */,
@@ -200,9 +208,11 @@
AD58E06B1E2A774F006ACC15 /* texture.cpp in Sources */,
AD3B1EC01E3B8B7600E918E3 /* layout.cpp in Sources */,
AD58E0721E2A90EF006ACC15 /* app.cpp in Sources */,
AD95AEC61E41EDEC002DD03A /* font.cpp in Sources */,
AD58E0531E107411006ACC15 /* main.cpp in Sources */,
AD58E0681E2A7741006ACC15 /* image.cpp in Sources */,
AD58E0771E3421F2006ACC15 /* Yoga.c in Sources */,
AD95AEC71E41EDEC002DD03A /* pch.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@@ -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)

View File

@@ -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;

View File

@@ -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))

View File

@@ -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;
}
}
};

View File

@@ -1,3 +1,3 @@
#include "pch.h"
#include "util.hpp"
#include "util.h"