2013-05-18 22 views
4

我只是弄清楚了顏色;文本不是渲染,我的着色器沒有提供任何錯誤;這是我的調試輸出文字完全不能用FreeType/GLFW渲染

Initializing FreeType version 2.4.10... 
Opening font file FreeSans.ttf... 
Loading glyph set and shaders... 
Compiling shader textshader.vs... 

Compiling shader textshader.fs... 

Linking program... 

Drawing text... 
16.666667 ms/frame 

這裏是我的繪製函數

void text::draw(const char* text, float x, float y, float sx, float sy) { 

    const char *p; 
    FT_GlyphSlot g = face->glyph; 

    GLuint tex; 

    glActiveTexture(GL_TEXTURE0); 
    glGenTextures(1, &tex); 
    glBindTexture(GL_TEXTURE_2D, tex); 
    glUniform1i(uniform_tex, 0); 

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 

    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); 

    glEnableVertexAttribArray(attribute_coord); 
    glBindBuffer(GL_ARRAY_BUFFER, vbo); 
    glVertexAttribPointer(attribute_coord, 4, GL_FLOAT, GL_FALSE, 0, 0); 

    for(p = text; *p; p++) { 
     if(FT_Load_Char(face, *p, FT_LOAD_RENDER)) 
      continue; 

     glTexImage2D(
      GL_TEXTURE_2D, 
      0, 
      GL_ALPHA, 
      g->bitmap.width, 
      g->bitmap.rows, 
      0, 
      GL_ALPHA, 
      GL_UNSIGNED_BYTE, 
      g->bitmap.buffer 
     ); 

     float x2 = x + g->bitmap_left * sx; 
     float y2 = -y - g->bitmap_top * sy; 
     float w = g->bitmap.width * sx; 
     float h = g->bitmap.rows * sy; 

     GLfloat box[4][4] = { 
      {x2, -y2  , 0, 0}, 
      {x2 + w,-y2  , 1, 0}, 
      {x2, -y2 - h , 0, 1}, 
      {x2 + w,-y2 - h , 1, 1} 
     }; 

     glBufferData(GL_ARRAY_BUFFER, sizeof box, box, GL_DYNAMIC_DRAW); 
     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 

     x += (g->advance.x >> 6) * sx; 
     y += (g->advance.y >> 6) * sy; 
    } 

    glDisableVertexAttribArray(attribute_coord); 
    glDeleteTextures(1, &tex); 
} 

這裏是我執行繪製函數

void window::handleEventsAndRender() { 

    if(!isOpen) { 
     printf("Must open a window first to render and handle events!"); 
     return; 
    } 

    float sx = 2/1024; 
    float sy = 2/786; 

    text test("FreeSans.ttf"); 

    glUseProgram(test.textProgram); 

    GLfloat black[4] = {0, 0, 0, 1}; 
    glUniform4fv(test.uniform_color, 1, black); 

    printf("Drawing text...\n"); 

    while(glfwGetWindowParam(GLFW_OPENED)) { 

     glClearColor(1, 1, 1, 1); 
     glClear(GL_COLOR_BUFFER_BIT); 

     glDisable(GL_CULL_FACE); 
     glEnable(GL_BLEND); 
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 

     test.draw("The Quick Brown Fox Jumps Over The Lazy Dog", -1 + 8 * sx, 1 - 50 * sy, sx, sy); 

     glfwSwapBuffers(); 
     printFPS(); 
    } 

    glfwTerminate(); 
} 

最後,這裏是我的着色器

頂點

#version 120 

attribute vec4 coord; 
varying vec2 texpos; 

void main(void) { 
    gl_Position = vec4(coord.xy, 0, 1); 
    texpos = coord.zw; 
} 

片段

#version 120 

varying vec2 texpos; 
uniform sampler2d tex; 
uniform vec4 color; 

void main(void) { 
    gl_FragColor = vec4(1, 1, 1, texture2D(tex, texpos).a) * color; 
} 

有人可以幫我嗎?

+0

你得到這個工作? –

+0

@EventHorizo​​n nope。 – Tetramputechture

回答

1

我認爲你的問題來自於SX初始化和sy

float sx = 2/1024; // sx == 0, 2 is int and 1024 is int. 2/1024 -> sx == 0 
// ... 
test.draw("The Quick Brown Fox Jumps Over The Lazy Dog", -1 + 8 * sx, 1 - 50 * sy, sx, sy); 
// is actually calling like this 
test.draw("The Quick Brown Fox Jumps Over The Lazy Dog", -1, 1, 0, 0); 
// then in text::draw 
float w = g->bitmap.width * sx; -> w == 0 

修復SX和SY初始化:

float sx = float(2)/float(1024); 
// or 
float sx = 2.0f/1024.0f;