【问题标题】:In Java, Does LWJGL/OpenGL have an equivalent of BufferedImage's subImage for making sprite sheets?在 Java 中,LWJGL/OpenGL 是否有相当于 BufferedImage 的 subImage 来制作精灵表?
【发布时间】:2012-02-16 15:26:52
【问题描述】:

我正在做一个在 Java 中获取字体的测试,基本上是获取更大的字母 png,然后将其切割成更小的部分,以将每个单独的字母作为单独的子图像。到目前为止,我使用的是 BufferedImage 和 java.awt.Graphics。

但是,我计划在未来使用 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();
    }
}
}

【问题讨论】:

  • 将它们复制到单个图像中是一个坏主意。此外,您就基本相同的问题提出了许多问题,您应该尝试以一种方式解决它,而不是尝试一堆事情并在它们一次不起作用时立即放弃。如果您没有成功,请考虑发放赏金来帮助解决您的问题。 OpenGL 中的直接等价物是 glTexSubImage2D() btw。
  • 我将暂时搁置这些问题,只使用目前提供给我的答案和示例。感谢您的帮助。

标签: java opengl sprite lwjgl


【解决方案1】:

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

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

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
}

最后两个变量允许我拥有一个具有可变单元格大小的精灵表。

那么我的Sprite渲染方法如下...

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();
    }

这将绘制 Spritesheet 的正确单元格(或多个单元格)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    • 2010-10-28
    • 1970-01-01
    • 2011-09-21
    • 2023-04-06
    • 2012-07-31
    • 1970-01-01
    相关资源
    最近更新 更多