En Java, ¿LWJGL / OpenGL tiene un equivalente de la subimagen de BufferedImage para hacer hojas de sprites?

I'm doing a test for getting a font in Java, basically getting a larger png of the alphabet, then cut it into smaller parts to get each individual letter as a separate subImage. So far I was using BufferedImage and java.awt.Graphics.

However, I'm planning on using LWJGL in the future, and would like to get more familiar with it(for this test I'm even creating the display with LWJGL), and would like to know if there was an equivalent of subImages in LWJGL's OpenGL. I'd prefer not to get slick.

Here's the image, just in case it helps: http://i.imgur.com/LeL58.png

And here is the code I'm using, it could help.

package typeandscreen;

*imports go here, cut to save space*
public class Font{

    final int width = 78;
    final int height = 12;
    final int rows = 2;
    final int cols = 13;

BufferedImage letters[] = new BufferedImage[rows*cols]; 

void test(){
        final BufferedImage totalImage = ImageIO.read(new File("ABCabcs.png"));

    for(int i = 0; i < rows; i++){

        for(int j = 0; j < cols; j++){
            letters[(i * cols) + j] = totalImage.getSubimage(
                j * width,
                i * height,
    } catch(IOException e){

preguntado el 01 de febrero de 12 a las 03:02

copying them into individual images is a bad idea. Also, you asked many question regarding basically the same problem, you should try to solve it one way instead of trying a bunch of things and giving up immediately when they don't work at once. If you don't succeed consider giving out a bounty to help solve your problem. The direct equivalent in OpenGL is glTexSubImage2D() btw. -

I'll lay off the questions for a while and just work with the answers and examples that were given to me so far. Thanks for your help. -

1 Respuestas

I'm not sure about a specific method, but here is my method to support Spritesheets...

First I load the entire image into a single Texture, I then assign a few variables to help determine which part of the Sprite sheet I want to display with the following method:

public void setSpriteSheetPosition(int columns, int rows, int which_column, int which_row, int wide, int tall)
    this.columns = columns; //Number of columns the sprite sheet has
    this.rows = rows;  //Number of rows the sprite sheet has.
    this.which_column = which_column; //Which column and row I want to display on the Sprite
    this.which_row = which_row;
    this.wide = wide; //How many cells wide I want the sprite to be composed of
    this.tall = tall; //How many cells tall I want the sprite to be composed of

The last two variables allow me to have a spritesheet with variable cell sizes.

Then my Sprite rendering method looks like the following...

public void draw()
        int tx = (int)location.x;
        int ty = (int)location.y;
        glTranslatef(tx, ty, location.layer);

        float height = imageData.getTexture().getHeight();
        float width = imageData.getTexture().getWidth();

        float texture_X = ((float)which_column/(float)columns)*width;
        float texture_Y = ((float)which_row/(float)rows)*height;
        float texture_XplusWidth = ((float)(which_column+wide)/(float)columns)*width;
        float texture_YplusHeight = ((float)(which_row+tall)/(float)rows)*height;

            glTexCoord2f(texture_X, texture_Y);
            glVertex2f(0, 0);

            glTexCoord2f(texture_X, texture_YplusHeight);
            glVertex2f(0, getHeight());

            glTexCoord2f(texture_XplusWidth, texture_YplusHeight);
            glVertex2f(getWidth(), getHeight());

            glTexCoord2f(texture_XplusWidth, texture_Y);
            glVertex2f(getWidth(), 0);

Which will draw the correct cell (or cells) of the Spritesheet.

Respondido 16 Feb 12, 11:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.