c++ - Center bitmap text to a rectangle with OpenGL -
i need center piece of text rectangle.
i found example, i'm struggling understand does.
it not hard achieve this, need know how find width , height of text after being drawn, cannot find anywhere.
to draw text, char char:
static void drawtext(std::string str, float x, float y, float z) { glrasterpos3f(x, y, z); (unsigned int = 0; < str.size(); i++) { glutbitmapcharacter(glut_bitmap_helvetica_18, str[i]); } }
not sure if best way, first program using opengl.
raster fonts awful, not work in modern opengl know - need use texture-mapped triangles implement bitmap fonts now-a-days. if starting out, legacy opengl may work you, find things raster pos not supported in opengl es , core opengl 3+.
that said can sum glutbitmapwidth (...)
across of characters in string, this:
unsigned int str_pel_width = 0; const unsigned int str_len = str.size (); // finding string length can expensive depending on implementation (e.g. in // c-string requires looping through entire string storage until // first null byte found, each , every time call this). // // string has constant-length, move out of loop better // performance! using std::string, not big issue, // did ask "best way" of doing something. (unsigned int = 0; < str_len; i++) str_pel_width += glutbitmapwidth (glut_bitmap_helvetica_18, str [i]);
now, finish discussion, should aware height of each character identical in glut bitmap font. if recall, 18 pt. helvetica 22 or 24 pixels high. distinction between pt. size , pixel size supposed dpi scaling, glut not implement this.
Comments
Post a Comment