Bitmap font texture coords

DJScantron

Newcomer
I have a square texture, not unlike the one here: http://en.wikipedia.org/wiki/Image:ASCII_full.svg (except mine has 2 blank rows at the bottom.)

I've tried for a while to come up with the proper tex coords for each character, but I still can't get it right.

The spacing of the coords is correct(I think), I get a whole character when I render, but It's the wrong one(or completely gray).

Here's what I have so far.(condensed)

NOTE: I'm flipping the texture vertically on load.

Code:
    int chx = 8, chy = 16;    //each characters width and height
    int imgx = 128, imgy = 128  //font texture dimensions

    //array of float pointers.
    //each element points to an array of  4 tex coords for an ascii character
    float **texCoords;


    texCoords = new float*[16*6];
    for(int i = 0; i < 16*6; ++i)
    {
        texCoords[i] = new float[2*4];
    }



    //normally these would be literals, because the font will always be 6 rows of 16 chars each 
    const int CPR = 16;  //characters per row
    const int NOR = 6;   //number of rows


    for(int y = 0; y < NOR; ++y)
    {
        for(int x = 0; x < CPR; ++x)
        {
            //top left vertex
            texCoords[(NOR * y) + x][0] = (float)x*chx / imgx;
            texCoords[(NOR * y) + x][1] = (float)y*chy / imgy;

            //bottom left vertex
            texCoords[(NOR * y) + x][2] = (float)x*chx / imgx ;
            texCoords[(NOR * y) + x][3] = ((float)y*chy - chy) / imgy;

            //top right vertex
            texCoords[(NOR * y) + x][4] = ((float)x*chx + chx) / imgx;
            texCoords[(NOR * y) + x][5] = (float)y*chy / imgy;

            //bottom right vertex
            texCoords[(NOR * y) + x][6] = ((float)x*chx + chx) / imgx;
            texCoords[(NOR * y) + x][7] = ((float)y*chy - chy) / imgy;
        }
    }
 
Your indexing is wrong. You want to index into a 1D array from 2D coordinates, which would be:
Code:
(CPR * y) + x
Alternatively, you can just keep a separate variable and increment it by 1 each iteration of the loop. It's simpler and prevents errors like that, but it means that you need to consider the nesting order of your loops (as it stands, your loops are correctly nested for a row-major array, which is what you want).

eg.
Code:
    int i = 0;
    for(int y = 0; y < NOR; ++y)
    {
        for(int x = 0; x < CPR; ++x)
        {
            //top left vertex
            texCoords[i][0] = (float)x*chx / imgx;
            texCoords[i][1] = (float)y*chy / imgy;

            //bottom left vertex
            texCoords[i][2] = (float)x*chx / imgx ;
            texCoords[i][3] = ((float)y*chy - chy) / imgy;

            //top right vertex
            texCoords[i][4] = ((float)x*chx + chx) / imgx;
            texCoords[i][5] = (float)y*chy / imgy;

            //bottom right vertex
            texCoords[i][6] = ((float)x*chx + chx) / imgx;
            texCoords[i][7] = ((float)y*chy - chy) / imgy;
            ++i;
        }
    }
 
Back
Top