#include "font_opengl.h" static unsigned int _pow2(unsigned int i) { register unsigned int p2; for (p2=1; p2pt; // prepare the OpenGL textures / display lists wids=(float *)malloc(sizeof(float)*255); bears=(float *)malloc(sizeof(float)*255); hoss=(float *)malloc(sizeof(float)*255); qvws=(int *)malloc(sizeof(int)*255); qvhs=(int *)malloc(sizeof(int)*255); qtws=(float *)malloc(sizeof(float)*255); qths=(float *)malloc(sizeof(float)*255); textures=(unsigned int *)malloc(sizeof(unsigned int)*255); glGenTextures(255,textures); for (register unsigned char ch=0; ch<255; ch++) { register int i,j; FT_Face face=ttf->face; FT_BitmapGlyph bitmap_glyph; FT_Glyph glyph; FT_Bitmap bitmap; unsigned int width,height; GLubyte* expanded_data; FT_Load_Glyph(face,FT_Get_Char_Index(face,ch),FT_LOAD_DEFAULT); FT_Get_Glyph(face->glyph,&glyph); FT_Glyph_To_Bitmap(&glyph,ft_render_mode_normal,0,1); bitmap_glyph=(FT_BitmapGlyph)glyph; bitmap=bitmap_glyph->bitmap; width=_pow2(bitmap.width); height=_pow2(bitmap.rows); expanded_data=(GLubyte *)malloc(sizeof(GLubyte)*width*height); for (j=0; j<(int)height;j++) { for (i=0; i<(int)width; i++) { expanded_data[i+j*width]= (i>=bitmap.width||j>=bitmap.rows)? 0:bitmap.buffer[i+bitmap.width*j]; } } glBindTexture(GL_TEXTURE_2D,textures[ch]); glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE); 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_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexImage2D(GL_TEXTURE_2D,0,GL_ALPHA,width,height, 0,GL_ALPHA,GL_UNSIGNED_BYTE,expanded_data); free((void *)expanded_data); this->wids[ch]=(float)(face->glyph->advance.x>>6); this->hoss[ch]=(float)((face->glyph->metrics.horiBearingY-face->glyph->metrics.height)>>6); this->bears[ch]=(float)(face->glyph->metrics.horiBearingX>>6); this->qvws[ch]=bitmap.width; this->qvhs[ch]=bitmap.rows; this->qtws[ch]=(float)bitmap.width/(float)width; this->qths[ch]=(float)bitmap.rows/(float)height; } color=Vector4(1.0f); } FontOpenGL::~FontOpenGL() { glDeleteTextures(255,textures); free((void *)textures); free((void *)wids); free((void *)bears); free((void *)hoss); free((void *)qvws); free((void *)qvhs); free((void *)qtws); free((void *)qths); } void FontOpenGL::Render(int x,int y,const char *text) { char *ch; glPushAttrib(GL_LIST_BIT|GL_CURRENT_BIT|GL_ENABLE_BIT|GL_TRANSFORM_BIT); glMatrixMode(GL_MODELVIEW); glDisable(GL_LIGHTING); glEnable(GL_TEXTURE_2D); glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); glColor4fv((float *)color); glPushMatrix(); glScalef(1.0f,-1.0f,1.0f); for (ch=(char *)text; *ch; ch++) { glPushMatrix(); glTranslatef(x+bears[*ch],-y-(pt-hoss[*ch]),0); glBindTexture(GL_TEXTURE_2D,textures[*ch]); glBegin(GL_QUADS); glTexCoord2f(0.0f,0.0f); glVertex2f(0.0f,(GLfloat)qvhs[*ch]); glTexCoord2f(0,(GLfloat)qths[*ch]); glVertex2f(0.0f,0.0f); glTexCoord2f((GLfloat)qtws[*ch],(GLfloat)qths[*ch]); glVertex2f((GLfloat)qvws[*ch],0.0f); glTexCoord2f((GLfloat)qtws[*ch],0.0f); glVertex2f((GLfloat)qvws[*ch],(GLfloat)qvhs[*ch]); glEnd(); glPopMatrix(); glTranslatef((GLfloat)wids[*ch],0.0f,0.0f); } glPopMatrix(); glPopAttrib(); }