update font rendering

This commit is contained in:
MihailRis 2024-11-12 05:58:16 +03:00
parent b3ab037115
commit edbd851d11
3 changed files with 58 additions and 35 deletions

View File

@ -45,38 +45,42 @@ int Font::calcWidth(const std::wstring& text, size_t offset, size_t length) cons
return std::min(text.length()-offset, length) * glyphInterval; return std::min(text.length()-offset, length) * glyphInterval;
} }
static inline void drawGlyph( static inline void draw_glyph(
Batch2D& batch, Batch2D& batch,
const glm::vec3& pos, const glm::vec3& pos,
const glm::vec2& offset, const glm::vec2& offset,
uint c, uint c,
int glyphSize, const glm::vec3& right,
const Camera* const glm::vec3& up,
float glyphInterval,
float lineHeight
) { ) {
batch.sprite( batch.sprite(
pos.x + offset.x, pos.x + offset.x * right.x,
pos.y + offset.y, pos.y + offset.y * right.y,
glyphSize, right.x / glyphInterval,
glyphSize, up.y,
16, 16,
c, c,
batch.getColor() batch.getColor()
); );
} }
static inline void drawGlyph( static inline void draw_glyph(
Batch3D& batch, Batch3D& batch,
const glm::vec3& pos, const glm::vec3& pos,
const glm::vec2& offset, const glm::vec2& offset,
uint c, uint c,
int glyphSize, const glm::vec3& right,
const Camera* camera const glm::vec3& up,
float glyphInterval,
float lineHeight
) { ) {
batch.sprite( batch.sprite(
pos + camera->right * offset.x + camera->up * offset.y, pos + right * offset.x + up * offset.y,
camera->up, camera->right, up, right / glyphInterval,
glyphSize * 0.5f, 0.5f,
glyphSize * 0.5f, 0.5f,
16, 16,
c, c,
glm::vec4(1.0f) glm::vec4(1.0f)
@ -89,33 +93,43 @@ static inline void draw_text(
Batch& batch, Batch& batch,
std::wstring_view text, std::wstring_view text,
const glm::vec3& pos, const glm::vec3& pos,
const glm::vec3& right,
const glm::vec3& up,
float glyphInterval, float glyphInterval,
float lineHeight, float lineHeight
const Camera* camera
) { ) {
uint page = 0; uint page = 0;
uint next = MAX_CODEPAGES; uint next = MAX_CODEPAGES;
float x = 0; int x = 0;
float y = 0; int y = 0;
do { do {
for (uint c : text){ for (uint c : text){
if (!font.isPrintableChar(c)) { if (!font.isPrintableChar(c)) {
x += glyphInterval; x++;
continue; continue;
} }
uint charpage = c >> 8; uint charpage = c >> 8;
if (charpage == page){ if (charpage == page){
batch.texture(font.getPage(charpage)); batch.texture(font.getPage(charpage));
drawGlyph(batch, pos, glm::vec2(x, y), c, lineHeight, camera); draw_glyph(
batch,
pos,
glm::vec2(x, y),
c,
right,
up,
glyphInterval,
lineHeight
);
} }
else if (charpage > page && charpage < next){ else if (charpage > page && charpage < next){
next = charpage; next = charpage;
} }
x += glyphInterval; x++;
} }
page = next; page = next;
next = MAX_CODEPAGES; next = MAX_CODEPAGES;
x = 0.0f; x = 0;
} while (page < MAX_CODEPAGES); } while (page < MAX_CODEPAGES);
} }
@ -138,26 +152,28 @@ void Font::draw(
batch, batch,
text, text,
glm::vec3(x, y, 0), glm::vec3(x, y, 0),
glyphInterval * scale, glm::vec3(glyphInterval*scale, 0, 0),
lineHeight * scale, glm::vec3(0, lineHeight*scale, 0),
nullptr glyphInterval/static_cast<float>(lineHeight),
lineHeight
); );
} }
void Font::draw( void Font::draw(
Batch3D& batch, Batch3D& batch,
const Camera& camera,
std::wstring_view text, std::wstring_view text,
const glm::vec3& pos, const glm::vec3& pos,
float scale const glm::vec3& right,
const glm::vec3& up
) const { ) const {
draw_text( draw_text(
*this, *this,
batch, batch,
text, text,
pos, pos,
glyphInterval * scale, right * static_cast<float>(glyphInterval),
lineHeight * scale, up * static_cast<float>(lineHeight),
&camera glyphInterval/static_cast<float>(lineHeight),
lineHeight
); );
} }

View File

@ -50,10 +50,10 @@ public:
void draw( void draw(
Batch3D& batch, Batch3D& batch,
const Camera& camera,
std::wstring_view text, std::wstring_view text,
const glm::vec3& pos, const glm::vec3& pos,
float scale = 1 const glm::vec3& right={1, 0, 0},
const glm::vec3& up={0, 1, 0}
) const; ) const;
const Texture* getPage(int page) const; const Texture* getPage(int page) const;

View File

@ -419,12 +419,19 @@ void WorldRenderer::renderTexts(
shader.uniformMatrix("u_apply", glm::mat4(1.0f)); shader.uniformMatrix("u_apply", glm::mat4(1.0f));
batch3d->begin(); batch3d->begin();
std::wstring string = L"Segmentation fault (core dumped)"; std::wstring string = L"Segmentation fault (core dumped)";
glm::vec3 pos(0, 100, 0);
auto zvec = camera.position - pos;
zvec.y = 0;
std::swap(zvec.x, zvec.z);
zvec.z *= -1;
zvec = glm::normalize(zvec);
font.draw( font.draw(
*batch3d, *batch3d,
camera,
string, string,
glm::vec3(0, 100, 0) - pos - zvec * (font.calcWidth(string, string.length()) * 0.5f),
camera.right * (font.calcWidth(string, string.length()) * 0.5f) zvec,
camera.up
); );
batch3d->flush(); batch3d->flush();
} }