Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Font rendering working using a quick hack. VBO renderer is still buggy.

  • Loading branch information...
commit b53c7fc22da379f0e56fc93181b14c743c1a33df 1 parent 855ff75
@sh0 authored
Showing with 132 additions and 73 deletions.
  1. +2 −2 src/ui_ticker.cpp
  2. +129 −67 src/vo_font.cpp
  3. +1 −4 src/vo_font.h
View
4 src/ui_ticker.cpp
@@ -70,7 +70,7 @@ void c_ui_ticker::draw(c_ui_rect rect)
}
// Text
- m_text_a->text_set("test");
- m_text_a->draw(100, 100);
+ m_text_a->text_set("teststring");
+ m_text_a->draw(x1, y2);
}
View
196 src/vo_font.cpp
@@ -238,17 +238,19 @@ bool c_font_face::m_ft_init()
// Check against maximum texture size
GLint max_tex_s = 0;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_tex_s);
+ /*
if (max_tex_s > 0) {
- max_tex_s = MIN(G_MAXSHORT, max_tex_s);
+ max_tex_s = MIN(G_MAXSHORT - 1, max_tex_s);
guint max_tex_x = max_tex_s / m_glyph_box_w;
guint max_tex_y = max_tex_s / m_glyph_box_h;
guint max_want = MIN(m_tex_num_x * m_tex_num_y, max_tex_x * max_tex_y);
m_tex_num_x = MIN(max_want, max_tex_x);
m_tex_num_y = MIN((max_want / m_tex_num_x) + ((max_want % m_tex_num_x) ? 1 : 0), max_tex_y);
} else {
+ */
m_tex_num_x = MIN(m_tex_num_x, G_MAXSHORT / m_glyph_box_w);
m_tex_num_y = MIN(m_tex_num_y, G_MAXSHORT / m_glyph_box_h);
- }
+ //}
// Texture size
m_tex_pic_w = m_glyph_box_w * m_tex_num_x;
@@ -398,7 +400,7 @@ c_font_face::s_glyph* c_font_face::m_ft_loadchar(guint32 id)
// Alignment
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- glPixelStorei(GL_UNPACK_ROW_LENGTH, m_tex_pic_w);
+ glPixelStorei(GL_UNPACK_ROW_LENGTH, bitmap->pitch);
// Upload glyph
guint save_x = (glyph.tex_id % m_tex_num_x) * m_glyph_box_w;
@@ -536,35 +538,39 @@ bool c_font_text::m_buf_upload()
// Advance x coordinate
if (glyph_last)
adv_x += glyph_last->adv_x;
-
- // Position
- s_vec* pos = data[m_buf_used].pos;
- gint pos_x = adv_x + glyph_cur->off_x;
- gint pos_y = adv_y + glyph_cur->off_y;
- pos[0].x = pos_x;
- pos[0].y = pos_y;
- pos[1].x = pos_x + glyph_cur->box_w;
- pos[1].y = pos_y;
- pos[2].x = pos_x + glyph_cur->box_w;
- pos[2].y = pos_y + glyph_cur->box_h;
- pos[3].x = pos_x;
- pos[3].y = pos_y + glyph_cur->box_h;
- // Texture
- s_vec* tex = data[m_buf_used].tex;
- gint tex_x = (glyph_cur->tex_id % m_face->m_tex_num_x) * m_face->m_glyph_box_w;
- gint tex_y = (glyph_cur->tex_id / m_face->m_tex_num_x) * m_face->m_glyph_box_h;
- tex[0].x = tex_x;
- tex[0].y = tex_y;
- tex[1].x = tex_x + glyph_cur->box_w;
- tex[1].y = tex_y;
- tex[2].x = tex_x + glyph_cur->box_w;
- tex[2].y = tex_y + glyph_cur->box_h;
- tex[3].x = tex_x;
- tex[3].y = tex_y + glyph_cur->box_h;
+ // Check if non-zero texture size
+ if (glyph_cur->box_w > 0 && glyph_cur->box_h > 0) {
- // Usage counter
- m_buf_used++;
+ // Position
+ s_vec* pos = data[m_buf_used].pos;
+ gint pos_x = adv_x + glyph_cur->off_x;
+ gint pos_y = adv_y - glyph_cur->off_y;
+ pos[0].x = pos_x;
+ pos[0].y = pos_y;
+ pos[1].x = pos_x + glyph_cur->box_w;
+ pos[1].y = pos_y;
+ pos[2].x = pos_x + glyph_cur->box_w;
+ pos[2].y = pos_y + glyph_cur->box_h;
+ pos[3].x = pos_x;
+ pos[3].y = pos_y + glyph_cur->box_h;
+
+ // Texture
+ s_vec* tex = data[m_buf_used].tex;
+ gint tex_x = (glyph_cur->tex_id % m_face->m_tex_num_x) * m_face->m_glyph_box_w;
+ gint tex_y = (glyph_cur->tex_id / m_face->m_tex_num_x) * m_face->m_glyph_box_h;
+ tex[0].x = tex_x;
+ tex[0].y = tex_y;
+ tex[1].x = tex_x + glyph_cur->box_w;
+ tex[1].y = tex_y;
+ tex[2].x = tex_x + glyph_cur->box_w;
+ tex[2].y = tex_y + glyph_cur->box_h;
+ tex[3].x = tex_x;
+ tex[3].y = tex_y + glyph_cur->box_h;
+
+ // Usage counter
+ m_buf_used++;
+ }
// Last glyph
glyph_last = glyph_cur;
@@ -597,29 +603,110 @@ void c_font_text::draw(gdouble v_x, gdouble v_y)
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_face->m_tex_obj);
- // Push transform
+ // Push modelview transform
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslated(v_x, v_y, 0.0);
+ // Push texture transformation
+ glMatrixMode(GL_TEXTURE);
+ glPushMatrix();
+ glLoadIdentity();
+ gdouble tex_w = static_cast<GLdouble>(m_face->m_tex_pic_w);
+ gdouble tex_h = static_cast<GLdouble>(m_face->m_tex_pic_h);
+ GLdouble mat[] = {
+ 1.0 / tex_w, 0, 0, 0,
+ 0, 1.0 / tex_h, 0, 0,
+ 0, 0, 1, 0,
+ 0, 0, 0, 1
+ };
+ glMultMatrixd(mat);
+
// Debug
- //g_warning("font draw :)");
- glColor4f(0.5, 0.5, 0.5, 1.0);
- guint x1 = 0; guint x2 = 30;
- guint y1 = 0; guint y2 = 30;
+ /*
+ glColor4f(0.0, 1.0, 0.0, 1.0);
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_COLOR);
+ guint vx1 = 0; guint vx2 = 300;
+ guint vy1 = 0; guint vy2 = 300;
+ gfloat tx1 = 0; gfloat tx2 = m_face->m_tex_pic_w;
+ gfloat ty1 = 0; gfloat ty2 = m_face->m_tex_pic_h;
glBegin(GL_QUADS);
- glVertex2i(x1, y1);
- glVertex2i(x2, y1);
- glVertex2i(x2, y2);
- glVertex2i(x1, y2);
+ glTexCoord2f(tx1, ty1);
+ glVertex2i(vx1, vy1);
+ glTexCoord2f(tx2, ty1);
+ glVertex2i(vx2, vy1);
+ glTexCoord2f(tx2, ty2);
+ glVertex2i(vx2, vy2);
+ glTexCoord2f(tx1, ty2);
+ glVertex2i(vx1, vy2);
glEnd();
glColor4f(0.0, 0.0, 1.0, 1.0);
+ */
+
+ // Debug
+ guint adv_x = 0;
+ guint adv_y = 0;
+ auto it = m_text.begin();
+ guint32 utf_cp = 0;
+ while (m_utf_conv(m_text, it, utf_cp)) {
+ auto glyph = m_face->m_ft_getchar(utf_cp);
+ if (glyph) {
+ gint pos_x = adv_x + glyph->off_x;
+ gint pos_y = adv_y - glyph->off_y;
+ gint tex_x = (glyph->tex_id % m_face->m_tex_num_x) * m_face->m_glyph_box_w;
+ gint tex_y = (glyph->tex_id / m_face->m_tex_num_x) * m_face->m_glyph_box_h;
+
+ glColor4f(1.0, 1.0, 1.0, 1.0);
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_COLOR);
+ glBegin(GL_QUADS);
+ glTexCoord2i(tex_x, tex_y);
+ glVertex2i(pos_x, pos_y);
+ glTexCoord2i(tex_x + glyph->box_w, tex_y);
+ glVertex2i(pos_x + glyph->box_w, pos_y);
+ glTexCoord2i(tex_x + glyph->box_w, tex_y + glyph->box_h);
+ glVertex2i(pos_x + glyph->box_w, pos_y + glyph->box_h);
+ glTexCoord2i(tex_x, tex_y + glyph->box_h);
+ glVertex2i(pos_x, pos_y + glyph->box_h);
+ glEnd();
+
+ adv_x += glyph->adv_x;
+ }
+ }
+
+ /*
+ // Color and blend
+ glColor4f(1.0, 1.0, 1.0, 1.0);
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_COLOR);
+
+ // Bind buffer
+ glBindBuffer(GL_ARRAY_BUFFER, m_buf_obj);
+
+ // Buffer offsets
+ glEnableClientState(GL_VERTEX_ARRAY);
+ glVertexPointer(2, GL_SHORT, sizeof(s_buf), reinterpret_cast<GLvoid*>(G_STRUCT_OFFSET(s_buf, pos)));
+ glClientActiveTexture(GL_TEXTURE0);
+ glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+ glTexCoordPointer(2, GL_SHORT, sizeof(s_buf), reinterpret_cast<GLvoid*>(G_STRUCT_OFFSET(s_buf, tex)));
+
+ // Draw
+ glDrawArrays(GL_QUADS, 0, m_buf_used);
- // Draw buffers
- m_draw_int();
+ // Disable states
+ glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+ glDisableClientState(GL_VERTEX_ARRAY);
- // Pop transform
+ // Unbind buffer
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+ */
+
+ // Pop texture transform
+ glPopMatrix();
+
+ // Pop modelview transform
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
@@ -629,31 +716,6 @@ void c_font_text::draw(gdouble v_x, gdouble v_y)
}
}
-void c_font_text::m_draw_int()
-{
- // Push texture coordinate transformation
- glMatrixMode(GL_TEXTURE);
- glPushMatrix();
- glLoadIdentity();
- glOrtho(0.0, static_cast<GLdouble>(m_face->m_tex_pic_w), 0.0, static_cast<GLdouble>(m_face->m_tex_pic_h), 0.0, 2.0);
-
- // Bind buffer
- glBindBuffer(GL_ARRAY_BUFFER, m_buf_obj);
-
- // Buffer offsets
- glVertexPointer(2, GL_SHORT, sizeof(s_buf), reinterpret_cast<GLvoid*>(G_STRUCT_OFFSET(s_buf, pos)));
- glTexCoordPointer(2, GL_SHORT, sizeof(s_buf), reinterpret_cast<GLvoid*>(G_STRUCT_OFFSET(s_buf, tex)));
-
- // Draw
- glDrawArrays(GL_QUADS, 0, m_buf_used);
-
- // Unbind buffer
- glBindBuffer(GL_ARRAY_BUFFER, 0);
-
- // Pop texture coordinate transform
- glPopMatrix();
-}
-
// UTF conversion
bool c_font_text::m_utf_conv(std::string& str, std::string::iterator& it, guint32& utf_cp)
{
View
5 src/vo_font.h
@@ -64,7 +64,7 @@ class c_font_text : c_noncopiable
// Text
std::string m_text;
- // Buffer - try to get 32 byte alignment
+ // Buffer - must be packed
struct s_vec {
guint16 x;
guint16 y;
@@ -80,9 +80,6 @@ class c_font_text : c_noncopiable
static const GLuint m_buf_size = 256;
bool m_buf_upload();
- // Rendering
- void m_draw_int();
-
// UTF conversion
bool m_utf_conv(std::string& str, std::string::iterator& it, guint32& utf_cp);
};
Please sign in to comment.
Something went wrong with that request. Please try again.