在Java中,LWJGL/OpenGL是否具有BuffereImage';制作雪碧床单的子图像是什么?
我正在做一个Java字体测试,基本上得到一个更大的png字母表,然后将它切成更小的部分,得到每个单独的字母作为一个单独的子图像。到目前为止,我使用的是BuffereImage和java.awt.Graphics 然而,我计划在将来使用LWJGL,并且希望对它更加熟悉(在这个测试中,我甚至用LWJGL创建了显示),并且想知道LWJGL的OpenGL中是否有等效的子图像。我不想太油滑 以下是图片,以防万一: 这是我正在使用的代码,它可能会有所帮助在Java中,LWJGL/OpenGL是否具有BuffereImage';制作雪碧床单的子图像是什么?,java,opengl,sprite,lwjgl,Java,Opengl,Sprite,Lwjgl,我正在做一个Java字体测试,基本上得到一个更大的png字母表,然后将它切成更小的部分,得到每个单独的字母作为一个单独的子图像。到目前为止,我使用的是BuffereImage和java.awt.Graphics 然而,我计划在将来使用LWJGL,并且希望对它更加熟悉(在这个测试中,我甚至用LWJGL创建了显示),并且想知道LWJGL的OpenGL中是否有等效的子图像。我不想太油滑 以下是图片,以防万一: 这是我正在使用的代码,它可能会有所帮助 package typeandscreen; *
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();
}
}
}
包装类型和屏幕;
*导入到此处,剪切以节省空间*
公共类字体{
最终整数宽度=78;
最终整数高度=12;
最终整数行=2;
最终整数cols=13;
BuffereImage字母[]=新的BuffereImage[行*列];
无效测试(){
试一试{
final BuffereImage totalImage=ImageIO.read(新文件(“ABCabcs.png”);
对于(int i=0;i
我不确定具体的方法,但下面是我支持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
}
最后两个变量允许我使用可变单元格大小的spritesheet
然后我的精灵渲染方法如下所示
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();
}
这将绘制精灵表的正确单元格。将它们复制到单个图像中是个坏主意。另外,你问了很多关于同一个问题的问题,你应该试着用一种方法来解决它,而不是尝试一堆事情,当它们不能马上解决时,你就立即放弃。如果你没有成功,请考虑拿出一笔赏金来帮助解决你的问题。OpenGL中的直接等价物是glTexSubImage2D()。顺便说一句,我将暂时搁置这些问题,只处理到目前为止给我的答案和示例。谢谢你的帮助。