2016-07-05 1 views
1

현재 android ndk/opengl 프로젝트에서 작업 중이고 글꼴 렌더링 라이브러리로 freetype을 사용하려고하는데 텍스트를 화면에 렌더링 할 때 이상한 오류가 계속 발생합니다. 여기 몇 가지 샘플 텍스트에 대해 보여주는 것입니다 : (참고 : 아래 하나 "이것은"라고되어있다)Freetype Library는 세 가지 텍스처와 이상한 심볼을 제공합니다.

Text rendering oddities

설정 :

void TextRenderer::SetupGlyphs(std::string fontPath, int size){ 
    __android_log_print(ANDROID_LOG_INFO, "SetupGlyphs", "Font location: %s", fontPath.c_str()); 
    if(shadersInitialized == 0) 
     CreateShader(); 
    glUseProgram(this->shader); 

    // FreeType 
    FT_Library ft; 
    if (FT_Init_FreeType(&ft)) 
     __android_log_print(ANDROID_LOG_INFO, "SetupGlyphs", "ERROR::FREETYPE: Could not init FreeType Library."); 

    FT_Face face; 
    if (FT_New_Face(ft, fontPath.c_str(), 0, &face)) 
     __android_log_print(ANDROID_LOG_INFO, "SetupGlyphs", "ERROR::FREETYPE: Failed to load font: %s", fontPath.c_str()); 

    FT_Set_Pixel_Sizes(face, 0, size); 
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 

    for (GLubyte c = 0; c < 128; c++){ 
     if(FT_Load_Char(face, c, FT_LOAD_RENDER)){ 
      __android_log_print(ANDROID_LOG_INFO, "SetupGlyphs", "ERROR::FREETYPE: Failed to load Glyph"); 
      continue; 
     } 
     GLuint texture; 
     glGenTextures(1, &texture); 
     glBindTexture(GL_TEXTURE_2D, texture); 
     glTexImage2D(
       GL_TEXTURE_2D, 
       0, 
       GL_RGB, 
       face->glyph->bitmap.width, 
       face->glyph->bitmap.rows, 
       0, 
       GL_RGB, 
       GL_UNSIGNED_BYTE, 
       face->glyph->bitmap.buffer 
     ); 
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 

     Character character = { 
       texture, 
       ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows), 
       ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top), 
       static_cast<GLuint>(face->glyph->advance.x) 
     }; 
     characters.insert(std::pair<GLchar, Character>(c, character)); 
    } 
    glBindTexture(GL_TEXTURE_2D, 0); 
    FT_Done_Face(face); 
    FT_Done_FreeType(ft); 
} 

렌더링 :

void TextRenderer::RenderTexts() 
{ 
    if(shadersInitialized == 0) 
     CreateShader(); 
    // Activate corresponding render state 
    glUseProgram(this->shader); 

    GLuint projectionLocation = glGetUniformLocation(this->shader, "projection"); 
    glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, projectionMatrix); 

    for (int i=0; i<projects.size(); i++) { 

     ProjectLabel project = projects.at(i); 
     glUniform3f(glGetUniformLocation(this->shader, "textColor"), project.textColor.x, project.textColor.y, project.textColor.z); 
     glActiveTexture(GL_TEXTURE0); 

     GLuint vertexBuffer; 
     glGenBuffers(1, &vertexBuffer); 

     /* Set up the VBO for our vertex data */ 
     glEnableVertexAttribArray(0); 
     glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); 
     glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0); 

     // Iterate through all characters 
     std::string::const_iterator c; 
     GLuint x = project.x; 
     for (c = project.text.begin(); c != project.text.end(); c++) 
     { 

      Character ch = characters[*c]; 

      GLfloat xpos = x + ch.Bearing.x; 
      GLfloat ypos = project.y - (ch.Size.y - ch.Bearing.y); 

      GLfloat w = ch.Size.x; 
      GLfloat h = ch.Size.y; 
      // Update VBO for each character 
      GLfloat vertices[6*4] = { 
        xpos,  ypos + h, 0.0, 0.0 , 
        xpos,  ypos,  0.0, 1.0 , 
        xpos + w, ypos,  1.0, 1.0 , 

        xpos,  ypos + h, 0.0, 0.0 , 
        xpos + w, ypos,  1.0, 1.0 , 
        xpos + w, ypos + h, 1.0, 0.0 
      }; 

      glBindTexture(GL_TEXTURE_2D, ch.TextureID); 

      glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW); 
      glDrawArrays(GL_TRIANGLES, 0, 6); 

      x += (ch.Advance/64); // Bitshift by 6 to get value in pixels (2^6 = 64 (divide amount of 1/64th pixels by 64 to get amount of pixels)) 
     } 
     glDisableVertexAttribArray(0); 
    } 
    glBindTexture(GL_TEXTURE_2D, 0); 

} 

답변

2

그래서이 게시물을 발견 할 수있는 사람이라면 몇 시간 씩 웹을 터뜨리며 모든 것이 펑키 한 것처럼 보이는 이유를 알아 내려고 노력했습니다. Freetype은 GL_RGB을 통해 정렬되었습니다 (적어도 내 프로젝트에서는 아님). 대신 GL_LUMINANCE을 통해 정렬됩니다. 이러한 것들을 변경하여 glTexImage2D 위의 문제를 해결하고 또한 SIGABRT 오류가 발생했습니다.

TLDR;

glTexImage2D(
      GL_TEXTURE_2D, 
      0, 
      GL_RGB,      => GL_LUMINANCE 
      face->glyph->bitmap.width, 
      face->glyph->bitmap.rows, 
      0, 
      GL_RGB,      => GL_LUMINANCE 
      GL_UNSIGNED_BYTE, 
      face->glyph->bitmap.buffer 
    );