2017-11-13 12 views
-1

글꼴 렌더링을 시도하는 OpenGL, GLM, GLAD, GLFW 및 FreeType으로 작업합니다. 렌더링 도중 OpenGL 오류가 발생하지 않았는지 확인하기 위해 단계를 밟았으며 아무것도 오류 코드를 게시하지만 아무것도 화면에 렌더링하지 않습니다. 이 장면은 초기화 후 내 그리기 루프,이 호출되는 것입니다렌더링 할 FreeType을 가져올 수 없습니다.

glm::mat4 projection = glm::ortho(0.0f, 800.0f, 0.0f, 600.0f); 

: 내가 실제로 변수를 사용하는 경우 확실하지 않다 때문에, 또한 내가 제대로 GLM의 오르 기능을 사용하고 아닌 경우 확신합니다 :

void GameLoop::Run() 
{ 
    glEnable(GL_BLEND); 
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f); 

    while (!glfwWindowShouldClose(m_game_window)) 
    { 
     // Do That thing where you draw everything 
    } 
} 

텍스처에 글꼴로드 글꼴 생성자 :

Salem::Graphics::Font::Font(std::string p_font_path) 
{ 
    FT_Library ft; 
    if (FT_Init_FreeType(&ft)) 
    { 
     std::cout << "Error loading FreeType library" << std::endl; 
    } 

    FT_Face face; 
    if (FT_New_Face(ft, p_font_path.c_str(), 0, &face)) 
    { 
     std::cout << "Error loading font;\n" << p_font_path << std::endl; 
    } 

    FT_Set_Pixel_Sizes(face, 0, 28); 
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // Disable byte-alignment restriction 

    for (GLubyte c = 0; c < 128; c++) 
    { 
     if (FT_Load_Char(face, c, FT_LOAD_RENDER)) 
     { 
      std::cout << "Failed to load a character glyph." << std::endl; 
      continue; 
     } 

     GLuint texture; 
     glGenTextures(1, &texture); 
     glBindTexture(GL_TEXTURE_2D, texture); 
     glTexImage2D(
       GL_TEXTURE_2D, 
       0, 
       GL_RED, 
       face->glyph->bitmap.width, 
       face->glyph->bitmap.rows, 
       0, 
       GL_RED, 
       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, 
       glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows), 
       glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top), 
       face->glyph->advance.x 
     }; 

     m_characters.insert(std::make_pair(c, character)); 
    } 

    FT_Done_Face(face); 
    FT_Done_FreeType(ft); 
    std::cout << "Font loaded. " << std::endl; 
} 

내 텍스트의 초기화 기능 :

void Text::Initialize() 
{ 
    glm::mat4 projection = glm::ortho(0.0f, 800.0f, 0.0f, 600.0f); 
    glGenVertexArrays(1, &m_vao); 
    glGenBuffers(1, &m_vbo); 

    glBindVertexArray(m_vao); 
    glBindBuffer(GL_ARRAY_BUFFER, m_vbo); 

    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 6 * 4, nullptr, m_draw_method); 

    glEnableVertexAttribArray(0); 
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), nullptr); 

    glBindBuffer(GL_ARRAY_BUFFER, 0); 
    glBindVertexArray(0); 
} 

그리고 내 텍스트의 그리기 기능 :

void Text::Draw() 
{ 
    glm::mat4 projection = glm::ortho(0.0f, 800.0f, 0.0f, 600.0f); 

    shader()->Use(); 
    glUniform3f(glGetUniformLocation(shader()->id(), "textColor"), m_color.x, m_color.y, m_color.z); 
    glActiveTexture(GL_TEXTURE0); 
    glBindVertexArray(m_vao); 

    for (std::string::const_iterator c = m_text.begin(); c != m_text.end(); ++c) 
    { 
     Character ch = m_font.characters()[*c]; 

     GLfloat xPosition = m_position.x + ch.Bearing.x * m_scale; 
     GLfloat yPosition = m_position.y - (ch.Size.y - ch.Bearing.y) * m_scale; 

     GLfloat width = ch.Size.x * m_scale; 
     GLfloat height = ch.Size.y * m_scale; 

     GLfloat vertices[6][4] = { 
       {xPosition,   yPosition + height, 0.0, 0.0}, 
       {xPosition,   yPosition,   0.0, 1.0}, 
       {xPosition + width, yPosition,   1.0, 1.0}, 

       {xPosition,   yPosition + height, 0.0, 0.0}, 
       {xPosition + width, yPosition,   1.0, 1.0}, 
       {xPosition + width, yPosition + height, 1.0, 0.0} 
     }; 

     glBindTexture(GL_TEXTURE_2D, ch.TextureID); 
     glBindBuffer(GL_ARRAY_BUFFER, m_vbo); 
     glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); 
     //glBindBuffer(GL_ARRAY_BUFFER, 0); 

     glDrawArrays(GL_TRIANGLES, 0, 6); 

     m_position.x += (ch.Advance >> 6) * m_scale; 
    } 

    glBindVertexArray(0); 
    glBindTexture(GL_TEXTURE_2D, 0); 
} 

이 게시물을 내 쉐이더가 필요한 경우, 알려주세요, 너무 오래되고 싶지 않았다.

+5

이것은 너무 깁니다. [mcve]를 제공하십시오 –

+0

그게 어떻습니까? 죄송합니다, 실패 할 수있는 몇 곳이있는 것 같아요. 내가 무엇을 포함해야하는지 정말로 확신하지 못했습니다. OpenGL 디버깅은 어려울 수 있습니다. –

+0

링크 된 기사를 읽습니다. 그런 다음 잃어버린 경우에는 다음을 시도하십시오. 쉐이더 및 텍스처없이 한 객체를 가장 편리한 위치에 한 번 작성하고, 실패하면 게시하고 그렇지 않은 경우 확장하십시오. –

답변

0

코드를 해독하면 문제가되는 유용한 리소스를 발견했습니다. https://learnopengl.com/#!In-Practice/Text-Rendering 프로젝트 소스에서 글꼴을 생성 할 때 필자의 직교 투영 변수가 내 셰이더 프로그램에 전달되어야한다는 것을 알았습니다. 나는이 글꼴 생성자에 다음 코드를 추가하여 이것을했다 :

glm::mat4 projection = glm::ortho(0.0f, 800.0f, 0.0f, 600.0f); 
p_shader->Use(); 
glUniformMatrix4fv(glGetUniformLocation(p_shader->id(), "projection"), 1, GL_FALSE, glm::value_ptr(projection));