用lwjgl进行文本渲染

时间:2012-11-24 18:52:26

标签: java text render lwjgl

我有一个png字体,我可以正确地获取每个字母的行/列,问题是该信息是什么。每个字符是8乘8.这是我到目前为止的代码。 如果有人能给我一些帮助甚至只是一些建议,我将不胜感激。

private double xsize, ysize, x, y, rotate, xh, yh;
private int lxpos, lypos;
private Texture texture;
String text;

public TextRenderer(int fontsize, int xlocation, int ylocation,
        int rotates, String path, String Text) {

    text = Text;

    rotate = rotates;
    setXsize(fontsize);
    ysize = fontsize;
    x = xlocation;
    y = ylocation;
    setXh(getXsize() / 2);
    yh = ysize / 2;

    try {
        texture = TextureLoader.getTexture(".PNG", new FileInputStream(
                new File(path)));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    texture.bind();
    update();
}

public void update() {

    char[] c = text.toCharArray();

    int letter;
    // 16 collumns 16 rows
    for (int i = 0; i < c.length; i++) {
        letter = c[i];
        System.out.println(c[i]);
        lypos = letter % 16;
        lypos++;
        System.out.println("Collumn :" + lypos);
        lxpos = letter / 16;
        System.out.println("Row :" + lxpos);

        GL11.glEnable(GL11.GL_TEXTURE_2D);
        GL11.glPushMatrix();
        GL11.glTranslated(x, y, 0);
        GL11.glTranslatef(10.0f, 10.5f, -0.0f); // back to previous position
        GL11.glRotated(rotate, 0.0f, 0.0f, -1.0f); // rotate
        GL11.glTranslatef(-10.0f, -10.5f, 0.0f); // to the origin
        GL11.glTranslated(-x, -y, 0);
        // above is for movement/rotation of text

        // 128 px total

        lxpos = lxpos * 8;
        lxpos = lxpos * 8;

        int startx = lxpos * 8;
        int starty = lypos * 8;
        double endx = startx + 8;
        double endy = starty + 8;
        GL11.glBegin(GL11.GL_QUADS);
        GL11.glTexCoord2d(startx, starty);
        GL11.glVertex2d(x - getXh(), y - yh);
        GL11.glTexCoord2d(startx, endy);
        GL11.glVertex2d(x - getXh(), y + yh);
        GL11.glTexCoord2d(endx, endy);
        GL11.glVertex2d(x + getXh(), y + yh);

        GL11.glTexCoord2d(endx, starty);
        GL11.glVertex2d(x + getXh(), y - yh);
        GL11.glEnd();
        GL11.glPopMatrix();
        GL11.glDisable(GL11.GL_TEXTURE_2D);

1 个答案:

答案 0 :(得分:0)

这是我做的:

public class Fsc {

public static Fsc cA = new Fsc((XCOORDINSHEET), (XCOORDINSHEET), (WIDTHOFCHAR), (HEIGHTOFCHAR));
etc...

public int ix, iy, width, height;

public Fsc(int ixa, int iya, int widtha, int heighta) {

ix = ixa;

iy = iya;

width = widtha;

height = heighta;

}

public void render(int xa, int ya, float scale) {

int x2 = ix + width;

int y2 = iy + height;

glBegin(GL_QUADS);

glTexCoord2f(ix, iy);

glVertex2f(xa, ya);

glTexCoord2f(x2, iy);

glVertex2f(xa + width * scale, ya);

glTexCoord2f(x2, y2);

glVertex2f(xa + width * scale, ya + height * scale);

glTexCoord2f(ix, y2);

glVertex2f(xa, ya + height * scale);

glEnd();

}

}

public class Fontsheet {

public int sheet;

public Fontsheet(String location) {

sheet = ImagingTools.glLoadTextureLinear(location);

}

public void render(int x, int y, float scale, String text) {

glEnable(GL_TEXTURE_RECTANGLE_ARB);

glBindTexture(GL_TEXTURE_RECTANGLE_ARB, sheet);

float lwidth = 0;

int x2 = x;

for(int i = 0; i < text.length(); i++) {

char c = text.charAt(i);

x2 += lwidth;

switch(c) {

case 'A':

Fsc.cA.render(x2, y2, scale);lwidth = Fsc.cA.width * scale;

break;

case 'B': etc...

}

}

glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);

glDisable(GL_TEXTURE_RECTANGLE_ARB);

}

}

抱歉没有格式化。我知道它有效,因为我积极地使用它。复制时我可能忘记了一些代码。如果它不适合你,请告诉我。 希望它可以帮到你。