有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java中,LWJGL/OpenGL是否有一个相当于BuffereImage的子映像来制作精灵图纸?

我正在做一个Java字体测试,基本上得到一个更大的字母表png,然后把它切成更小的部分,得到每个单独的字母作为一个单独的子图像。到目前为止,我使用的是BuffereImage和java。awt。图形

然而,我计划在未来使用LWJGL,并希望对其更加熟悉(在这次测试中,我甚至用LWJGL创建了显示器),并且想知道LWJGL的OpenGL中是否有相当于子图像的图像。我不想太油滑

以下是图片,以防万一: http://i.imgur.com/LeL58.png

这是我正在使用的代码,它可能会有所帮助

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(){
    try{
        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,
                width,
                height
            );
        }
    }
    } catch(IOException e){
        e.printStackTrace();
    }
}
}

共 (1) 个答案

  1. # 1 楼答案

    我不确定具体的方法,但这里是我支持Spritesheets的方法

    首先,我将整个图像加载到一个纹理中,然后指定几个变量,以帮助确定我要使用以下方法显示的精灵表的哪个部分:

    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
    }
    

    最后两个变量允许我使用可变单元格大小的精灵表

    然后我的精灵渲染方法如下所示

    public void draw()
        {
            glPushMatrix();
            imageData.getTexture().bind();
            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;
    
            glBegin(GL_QUADS);
            {
                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);
            }
            glEnd();
            glPopMatrix();
        }
    

    这将绘制精灵表的正确单元格