GlassZee
GlassZee

Reputation: 1127

My original Java Font Program - Skewed when using different letters?

The output of my font program is skewed. I'm pretty sure that the problem has to do with x and y, and how printLetter() uses them. It's possibly getting confused with multiple numbers for x and y? Please let me know if you need any additional code. 'findCoords' is called from inside the gameloop.

Additional background info:

It uses LWJGL and its OpenGL. the +0.0769 and +0.25 in printLetter() is how big each tile in the main image is(the main image that holds each of the letters is 78*24, and each letter is 6*6.), in proportion to 1, and xcoords[] and ycoords[] arrays of doubles: xcoords[] holds 0/13, 1/13, 2/13, 3/13, etc, and ycoords[] holds 0/4, 1/4, 2/4, 3/4, for glTexCoord has to be in proportion to 1. Here is a visual of the image that it's taking the texture coordinates from.

enter image description here

public void findCoords(String chars){
    int numChars = chars.length();
    for(int z = 0; z < numChars; z++){

        char ch = chars.charAt(z);
        int x;
        int y;

        switch(ch){
    //LETTERS
        case 'A': x= 0; y= 0; break; case 'N': x= 0; y= 1; break;
        case 'B': x= 1; y= 0; break; case 'O': x= 1; y= 1; break;
        case 'C': x= 2; y= 0; break; case 'P': x= 2; y= 1; break;
        case 'D': x= 3; y= 0; break; case 'Q': x= 3; y= 1; break;
        case 'E': x= 4; y= 0; break; case 'R': x= 4; y= 1; break;
        case 'F': x= 5; y= 0; break; case 'S': x= 5; y= 1; break;
        case 'G': x= 6; y= 0; break; case 'T': x= 6; y= 1; break;
        case 'H': x= 7; y= 0; break; case 'U': x= 7; y= 1; break;
        case 'I': x= 8; y= 0; break; case 'V': x= 8; y= 1; break;
        case 'J': x= 9; y= 0; break; case 'W': x= 9; y= 1; break;
        case 'K': x= 10; y= 0; break; case 'X': x= 10; y= 1; break;
        case 'L': x= 11; y= 0; break; case 'Y': x= 11; y= 1; break;
        case 'M': x= 12; y= 0; break; case 'Z': x= 12; y= 1; break;
    //NUMBERS
        case '0': x= 0; y= 2; break; case '5': x= 5; y= 2; break;
        case '1': x= 1; y= 2; break; case '6': x= 6; y= 2; break;
        case '2': x= 2; y= 2; break; case '7': x= 7; y= 2; break;
        case '3': x= 3; y= 2; break; case '8': x= 8; y= 2; break;
        case '4': x= 4; y= 2; break; case '9': x= 9; y= 2; break;
    //PUNCTUATION
        case '.': x= 10; y= 2; break; case '!': x= 12; y= 2; break;
        case ',': x= 11; y= 2; break;

        case '?': x= 0; y= 3; break; case '"': x= 2; y= 3; break;
        case ':': x= 1; y= 3; break; case ' ': x= 3; y= 3; break;

        default: x= 0; y= 3;
            }

            int offs = scale*z;
            printLetter(x, y, offs);

        }
    }

.

void printLetter(int x, int y, int offs){
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
    GL11.glBegin(GL11.GL_QUADS);

    GL11.glVertex2f(0+offs, scale);
    GL11.glTexCoord2d(xcoords[x]+0.0769, ycoords[y]+0.25);

    GL11.glVertex2f(scale+offs, scale);
    GL11.glTexCoord2d(xcoords[x]+0.0769, ycoords[y]);

    GL11.glVertex2f(scale+offs, 0);
    GL11.glTexCoord2d(xcoords[x], ycoords[y]);

    GL11.glVertex2f(0+offs, 0);
    GL11.glTexCoord2d(xcoords[x], ycoords[y]+0.25);

    GL11.glEnd();
    GL11.glFlush();
}

When chars is a single letter repeated, like "AAAAA" or "BBBBBB", then there are no problems iwht the image. https://i.sstatic.net/oDwdr.png. However, when there are multiple letters in chars like "AAABBB" or "ABABAB" then the letters become skewed. The following image is what the output looks like when chars is

"AAABBB"

enter image description here


Before, the cases themselves called the print method with, for example

case 'A': printLetter(xcoord[0], ycoord[0], z)

I changed it to how it is now, to maybe make the program not as confused. Hopefully, even if it didn't fix the problem, it made it a bit easier to fix?

The problem may be because different letters will have different coordinates and that's messing up the way printLetter() is working? It's not in the cases, because I tested making two letters(A and F) have the same x and y, and made chars "AFAFAF", and there were no problems.

It also seems like, the further the letters are apart, the more screwed up they appear. Compare this "AAAZZZ" to the above "AAABBB":

enter image description here

Upvotes: 2

Views: 162

Answers (1)

Tim
Tim

Reputation: 35933

GL11.glVertex2f(0+offs, scale);
GL11.glTexCoord2d(xcoords[x]+0.0769, ycoords[y]+0.25);

GL11.glVertex2f(scale+offs, scale);
GL11.glTexCoord2d(xcoords[x]+0.0769, ycoords[y]);

GL11.glVertex2f(scale+offs, 0);
GL11.glTexCoord2d(xcoords[x], ycoords[y]);

GL11.glVertex2f(0+offs, 0);
GL11.glTexCoord2d(xcoords[x], ycoords[y]+0.25);

These calls are backward. You should call glTexCoord before glVertex, not after. glVertex call finalizes the vertex and sends it to be drawn, so each texcoord is getting associated with the next vertex instead of the one you want.

Upvotes: 2

Related Questions