Java 如何在LWGJL中绘制期间选择图像的区域?
在绘图过程中,我试图在LWJGL中选择纹理的特定区域,但我的努力没有结果。如何(如果可能)在绘图期间仅选择整个纹理的一个区域 请注意,我对Java并不陌生,我是一名经验丰富的程序员,但对于LWGJL,我却不是这样,因为我仍然完全掌握它 下面是我的代码 Sprite.javaJava 如何在LWGJL中绘制期间选择图像的区域?,java,textures,lwjgl,Java,Textures,Lwjgl,在绘图过程中,我试图在LWJGL中选择纹理的特定区域,但我的努力没有结果。如何(如果可能)在绘图期间仅选择整个纹理的一个区域 请注意,我对Java并不陌生,我是一名经验丰富的程序员,但对于LWGJL,我却不是这样,因为我仍然完全掌握它 下面是我的代码 Sprite.java public class Sprite { private Icon icon; private int width; private int height; private int offcutX; // all belo
public class Sprite
{
private Icon icon;
private int width;
private int height;
private int offcutX; // all below and this for selecting the region of the image.
private int offcutY;
private int imageX;
private int imageY;
public Sprite(TextureRegistry registry, String ref)
{
this(registry, ref, 0, 0, 0, 0);
}
public Sprite (TextureRegistry registry, String ref, int offcutX, int offcutY, int imageX, int imageY)
{
try
{
icon = registry.getIcon("net/blockbuster/resources/" + ref); // loads the icon and converts it to opengl-readable format.
width = icon.getIconWidth();
height = icon.getIconHeight();
this.offcutX = offcutX;
this.offcutY = offcutY;
this.imageX = imageX;
this.imageY = imageY;
}
catch (IOException e)
{
e.printStackTrace();
System.exit(-1);
}
}
public int getWidth()
{
return icon.getIconWidth();
}
public int getHeight()
{
return icon.getIconHeight();
}
public void draw(int x, int y) // drawing method, called on every loop.
{
glPushMatrix();
icon.bind();
glTranslatef(x, y, 0);
glBegin(GL_QUADS);
{ // I would like to be able to do this within this block.
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(0, 1);
glVertex2f(0, height);
glTexCoord2f(1, 1);
glVertex2f(width, height);
glTexCoord2f(1, 0);
glVertex2f(width, 0);
}
glEnd();
glPopMatrix();
}
}
TextureRegistry.java
public class TextureRegistry
{
private HashMap<String, Icon> table = new HashMap<String, Icon>(); // map to hold all loaded textures
public Icon getIcon(String resourceName) throws IOException
{
Icon tex = table.get(resourceName);
if (tex != null)
{
return tex;
}
tex = getIcon(resourceName, GL_TEXTURE_2D, GL_RGBA8, GL_LINEAR, GL_LINEAR);
table.put(resourceName, tex);
return tex;
}
public Icon getIcon(String resourceName, int target, int dstPixelFormat, int minFilter, int magFilter) throws IOException
{
BufferedImage image = loadImage(resourceName);
int textureID = loadTexture(image);
Icon icon = new Icon(target, textureID);
icon.setWidth(image.getWidth());
icon.setHeight(image.getHeight());
return icon;
}
public static int loadTexture(BufferedImage image)
{
int[] pixels = new int[image.getWidth() * image.getHeight()];
image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4);
for (int y = 0; y < image.getHeight(); y++)
{
for (int x = 0; x < image.getWidth(); x++)
{
int pixel = pixels[y * image.getWidth() + x];
buffer.put((byte)((pixel >> 16) & 0xFF)); // r
buffer.put((byte)((pixel >> 8) & 0xFF)); // g
buffer.put((byte)(pixel * 0xFF)); // b
buffer.put((byte)((pixel >> 24) & 0xFF)); // a
}
}
buffer.flip();
int textureID = glGenTextures();
glBindTexture(GL_TEXTURE_2D, textureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
return textureID;
}
public static BufferedImage loadImage(String ref) throws IOException
{
URL url = TextureRegistry.class.getClassLoader().getResource(ref);
if (url == null)
{
throw new IOException("Cannot find: " + ref);
}
Image img = new ImageIcon(url).getImage();
BufferedImage bufferedImage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics g = bufferedImage.getGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
return bufferedImage;
}
}
要指定要绘制的纹理区域,只需修改
texCoords
。当前,您正在使用整个纹理,例如,如果您只需要最接近原点的四分之一纹理,则可以在绘制方法中使用:
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(0, 1/2f);
glVertex2f(0, height);
glTexCoord2f(1/2f, 1/2f);
glVertex2f(width, height);
glTexCoord2f(1/2f, 0);
glVertex2f(width, 0);
这将产生与以前相同大小的图像,尽管只使用纹理左下角的四分之一。谢谢,解决了这个问题。我不知道你能用
glTexCoord2f
方法做到这一点,因为我试着把它们改成小数点,结果图片失真了,但我并没有对所有的图片都这样做。非常感谢。
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(0, 1/2f);
glVertex2f(0, height);
glTexCoord2f(1/2f, 1/2f);
glVertex2f(width, height);
glTexCoord2f(1/2f, 0);
glVertex2f(width, 0);