c++ - Text not rendering at all with FreeType/GLFW -
i'm getting clear color; text not rendering, , shaders not giving out errors; debug output
initializing freetype version 2.4.10... opening font file freesans.ttf... loading glyph set , shaders... compiling shader textshader.vs... compiling shader textshader.fs... linking program... drawing text... 16.666667 ms/frame
here draw function
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); }
here execute draw function
void window::handleeventsandrender() { if(!isopen) { printf("must open window first render , 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 on lazy dog", -1 + 8 * sx, 1 - 50 * sy, sx, sy); glfwswapbuffers(); printfps(); } glfwterminate(); }
finally, here shaders
vertex
#version 120 attribute vec4 coord; varying vec2 texpos; void main(void) { gl_position = vec4(coord.xy, 0, 1); texpos = coord.zw; }
fragment
#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; }
can please me?
i think problem comes initialization of sx , sy
float sx = 2/1024; // sx == 0, 2 int , 1024 int. 2 / 1024 -> sx == 0 // ... test.draw("the quick brown fox jumps on lazy dog", -1 + 8 * sx, 1 - 50 * sy, sx, sy); // calling test.draw("the quick brown fox jumps on lazy dog", -1, 1, 0, 0); // in text::draw float w = g->bitmap.width * sx; -> w == 0
fix sx , sy initialization:
float sx = float(2)/float(1024); // or float sx = 2.0f / 1024.0f;
Comments
Post a Comment