Font bitmaps

Member
Posts: 306
Joined: 2009.03
Post: #1
So I am porting code from a java program that uses bitmap fonts just fine. My codebase does regular opengl images just fine. Im trying to put fonts in now and though I am getting no errors, I am seeing nothing on the screen. If anyone feels like taking a peek at what I am doing, maybe they can spot my problem. I have added in stuff that is normally not there to exclude things like projection matrix, and what not from the list of problems. My font file seems fine, its a 256x256 image containing 16x16 characters each 16x16. Its the same file I used in my java program so i know its ok.

Code:
-(void) createFontGrid
{
    //the font texture has 16 rows    
    const float rows = (float)FONT_COLUMNS;
    //The font texture has 16 columns  
    const float columns = (float)FONT_ROWS;
    const float xoffset = 1.0f / columns;  
    const float yoffset = 1.0f / rows;  
    
    //prebuild the texture coordinates array for all characters
    for(int i=0;i<FONT_ROWS*FONT_COLUMNS;i++)
    {
        const float cx = (float)(i % fontWidth) * xoffset;  
        const float cy = (float)(i / fontHeight) * yoffset;    
        const int index = i * 8;//its a square, and we need x,y per corner so 8
        m_textureCoordinates[ index ] =/*FFF*/ cx;
        m_textureCoordinates[index+1] =/*FFF*/ 1 - cy - yoffset;
        m_textureCoordinates[index+2] =/*FFF*/ cx + xoffset;
        m_textureCoordinates[index+3] =/*FFF*/ 1 - cy - yoffset;          
        m_textureCoordinates[index+4] =/*FFF*/ cx + xoffset;
        m_textureCoordinates[index+5] =/*FFF*/ 1 - cy;
        m_textureCoordinates[index+6] =/*FFF*/ cx;
        m_textureCoordinates[index+7] =/*FFF*/ 1 - cy;    
    }
    
}
//need to make the 3d and 2d common at some point
-(void) print2DAtX:(GLfloat)x y:(GLfloat)y r:(GLfloat)colorR g:(GLfloat)colorG b:(GLfloat)colorB formatString:(const char *)formatString, ...
{
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE, GL_ONE);  
    glDisable(GL_DEPTH_TEST);
    glColor4f(colorR, colorG, colorB, 1);
    //Convert the variable argument list, to a single char[] array
    //be careful, the final string length may fit into this array
    char text[256];  
    va_list ap;
    if (formatString == NULL)  
        return;
    
    va_start(ap, formatString);
    vsprintf(text, formatString, ap);
    va_end(ap);
    if(text[0]=='\0')
        return;
    
    int length = (int)strlen(text);
    
    //Set up our arrays
    const GLushort indices[]= {1, 2, 0, 3};  //triangle strip
    GLfixed vertices[8];
    [fontTexture bind];
    glVertexPointer(2, GL_FIXED, 0, vertices);  
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrthof(0, 320,0,480,-1,1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glNormal3f(0,0,1);
    glScalef(1,1,1);;
    
    
    const GLfixed fy =/*FFF*/ y;  
    const GLfixed fheight =/*FFF*/ fontHeight;  
    
    //go throught all chars int the array
    for(int i = 0;i < length;i++)
    {
        /*We have to substract 32 units, because the characters stored in  
         our font texture starts in the ASCII code 32 (the first 32 ASCII  
         code, are control codes, usually non-displayables)*/
        const int c = text[i]-32;    
        const float originX = x + (i * fontWidth);
        const float offsetX = originX + fontWidth;
        
        /*Compute the dimensions and position of the 'quad' (in reality is  
         a triangle strip with two triangles)*/
        vertices[0] = /*FFF*/originX;    
        vertices[1] = fy;            
        
        vertices[2] =/*FFF*/ offsetX;  
        vertices[3] = fy;        
        
        vertices[4] =/*FFF*/ offsetX;  
        vertices[5] = fy + fheight;
                
        vertices[6] =/*FFF*/ originX;  
        vertices[7]= fy + fheight;  
        
        /*we access to the corresponding texture coordinates in the  
         precomputed texture coordinates array*/
        glTexCoordPointer(2, GL_FIXED, 0, &m_textureCoordinates[c*FONT_ROWS]);      
        //draw the character
        glDrawElements(GL_TRIANGLE_STRIP,4,GL_UNSIGNED_SHORT,indices);
    }
}
Quote this message in a reply
Post Reply