I can not display text through freetype

I can not display text through freetype

Postby albrechtmyers » 29 Jan 2020, 10:03

Hello.
{l Code}: {l Select All Code}
#include "font.hpp"

Font::Font ( const char *ttf_file )
{
  glm::clearMatrix4x4 ( &ortho[0] );
  glm::clearMatrix4x4 ( &translate[0] );
  glm::clearMatrix4x4 ( &result[0] );

  program = global.programs["sprite"];
  int width = global.width;
  int height = global.height;
  glm::ortho ( &ortho[0], 0.0f, width, 0.0f, height, 0.0f, 1.0f );
  setPos ( 0, 0 );

  FT_Init_FreeType( &ft_library );

  FT_Face face = 0;
#ifdef __ANDROID__
  FT_NewFace ( ft_library, ttf_file, 0, &face );
#else
  char *path = (char *) new char[255];
  sprintf ( path, "assets/%s", ttf_file );
  FT_New_Face ( ft_library, path, 0, &face );
  free ( path );
#endif

  FT_Set_Pixel_Sizes ( face, 0, 21 );

  const std::string estring ( "Hello Привет" );

  FT_GlyphSlot slot = face->glyph;
  int pen_x, pen_y, n;

  pen_x = 300;
  pen_y = 200;

  FT_Glyph glyph;
  for ( int i = 0; i < estring.size(); i++ ) {
    FT_ULong charcode = estring[i];
    FT_UInt glyph_index = FT_Get_Char_Index ( face, charcode );

    FT_Load_Glyph ( face, glyph_index, FT_LOAD_DEFAULT );

    FT_Render_Glyph ( face->glyph, FT_RENDER_MODE_NORMAL );
  }

  FT_Get_Glyph ( face->glyph, &glyph );

  FT_Glyph_To_Bitmap ( &glyph, FT_RENDER_MODE_NORMAL, 0, 1 );
  FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph) glyph;
  FT_Bitmap &bitmap = bitmap_glyph->bitmap;

#if 0
  width = next_p2 ( bitmap.width );
  height = next_p2 ( bitmap.rows );
#endif
  width = bitmap.width;
  height = bitmap.rows;

  GLubyte *expanded_data = new GLubyte [ width * height * 2 ];

  for ( int y = 0; y < height; y++ ) {
    for ( int x = 0; x < width; x++ ) {
      expanded_data [ 2 * ( x + y * width ) ] = expanded_data [ 2 * ( x + y * width ) + 1 ] =
        ( x >= bitmap.width || y >= bitmap.rows ) ? 0 : bitmap.buffer[ x + bitmap.width * y ];
    }
  }

  glGenTextures ( 1, &textureid );
  glBindTexture ( GL_TEXTURE_2D, textureid );
  glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, expanded_data );

 
  setSize ( width, height );
  setPos ( 100, 100 );

  delete[] expanded_data;

}
void Font::setSize ( int w, int h )
{
    vertices = new float [ 12 ];
    vertices[0] = 0;
    vertices[1] = 0;
    vertices[2] = 0;
    vertices[3] = h;
    vertices[4] = w;
    vertices[5] = 0;

    vertices[6] = w;
    vertices[7] = 0;
    vertices[8] = w;
    vertices[9] = h;
    vertices[10] = 0;
    vertices[11] = h;

    texture = new float [ 12 ];
    texture[0] = 0;
    texture[1] = 1;
    texture[2] = 0;
    texture[3] = 0;
    texture[4] = 1;
    texture[5] = 1;

    texture[6] = 1;
    texture[7] = 1;
    texture[8] = 1;
    texture[9] = 0;
    texture[10] = 0;
    texture[11] = 0;
}

void Font::setPos ( int x, int y )
{
  this->x = x;
  this->y = y;
  glm::translate ( &translate[0], x, y, 0 );
  glm::sumMatrix ( &result[0], &translate[0], &ortho[0] );
}

void Font::draw ( )
{
  glUseProgram ( program );

  sampler = glGetUniformLocation ( program, "s_texture" );

  glActiveTexture ( GL_TEXTURE0 );
  glBindTexture ( GL_TEXTURE_2D, textureid );
  glUniform1i ( sampler, 0 );

  GLint projection_location = glGetUniformLocation ( program, "transform" );
  glUniformMatrix4fv ( projection_location, 1, GL_FALSE, &result[0][0] );

  glEnableVertexAttribArray ( 0 );
  glEnableVertexAttribArray ( 1 );

  glVertexAttribPointer ( 0, 2, GL_FLOAT, GL_FALSE, 0, vertices );
  glVertexAttribPointer ( 1, 2, GL_FLOAT, GL_FALSE, 0, texture );

  glDrawArrays ( GL_TRIANGLES, 0, 12 );

  glDisableVertexAttribArray ( 0 );
  glDisableVertexAttribArray ( 1 );
}

shader code
{l Code}: {l Select All Code}
  const char *vshader =
             "#version 300 es\n"
      "layout(location = 0) in vec2 position;\n"
      "layout(location = 1) in vec2 texCoord;\n"
      "uniform mat4 transform;\n"
       "out vec2 v_texCoord;\n"
      "void main ( )\n"
      "{\n"
      " gl_Position = transform * vec4 ( position, 0.0, 1.0 );\n"
      " v_texCoord = texCoord;\n"
      "}";

    const char *fshader =
      "#version 300 es\n"
      "precision mediump float;\n"
      "in vec2 v_texCoord;\n"
      "layout(location = 0) out vec4 outColor;\n"
      "uniform sampler2D s_texture;\n"
      "void main ( )\n"
      "{\n"
      " outColor = texture ( s_texture, v_texCoord );\n"
      "}";

    GLuint program = loadProgram ( vshader, fshader );
    global.programs["sprite"] = program;
    return program;
albrechtmyers
 
Posts: 2
Joined: 29 Jan 2020, 06:42

Re: I can not display text through freetype

Postby benau » 29 Jan 2020, 13:35

This has no relation to stk-code?
Image
benau
STK Moderator
 
Posts: 183
Joined: 08 Dec 2015, 17:32

Who is online

Users browsing this forum: MSN [Bot] and 1 guest