我试图在 LWJGL 中使用纹理,结果发现 RBG-png 有点歪斜。示例: 原始图片/纹理
加载代码是lwjgl wiki space入侵者示例的98%.. Texture.java:
public int target, textureID, height, width, texWidth, texHeight;
private float widthRatio, heightRatio;
public Texture(int target, int textureID) {
this.target = target;
this.textureID = textureID;
}
public void bind() {
GL11.glBindTexture(target, textureID);
}
public void setWidth(int width) {
this.width = width;
setWidth();
}
public void setHeight(int height) {
this.height = height;
setHeight();
}
public int getImageWidth() {
return width;
}
public int getImageHeight() {
return height;
}
public float getWidth() {
return widthRatio;
}
public float getHeight() {
return heightRatio;
}
public void setTextureWidth(int texWidth) {
this.texWidth = texWidth;
setWidth();
}
public void setTextureHeight(int texHeight) {
this.texHeight = texHeight;
setHeight();
}
private void setWidth() {
if (texWidth != 0)
widthRatio = ((float) width) / texWidth;
}
private void setHeight() {
if (texHeight != 0)
heightRatio = ((float) height) / texHeight;
}
TextureLoader.java:
private static HashMap<String, Texture> table = new HashMap<String, Texture>();
private static ColorModel glAlphaColorModel, glColorModel;
private static IntBuffer textureIDBuffer = BufferUtils.createIntBuffer(1);
static {
glAlphaColorModel = new ComponentColorModel(
ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8,
8, 8 }, true, false, ComponentColorModel.TRANSLUCENT,
DataBuffer.TYPE_BYTE);
glColorModel = new ComponentColorModel(
ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8,
8, 0 }, false, false, ComponentColorModel.OPAQUE,
DataBuffer.TYPE_BYTE);
}
private static int createTextureID() {
GL11.glGenTextures(textureIDBuffer);
return textureIDBuffer.get(0);
}
public static Texture getTexture(String name, BufferedImage image)
throws IOException {
Texture tex = table.get(name);
if (tex != null)
return tex;
tex = getTexture(image, GL11.GL_TEXTURE_2D, GL11.GL_RGBA,
GL11.GL_LINEAR, GL11.GL_LINEAR);
table.put(name, tex);
return tex;
}
public static Texture getTexture(BufferedImage image, int target,
int dstPixelFormat, int minFilter, int magFilter)
throws IOException {
int srcPixelFormat;
int textureID = createTextureID();
Texture texture = new Texture(target, textureID);
GL11.glBindTexture(target, textureID);
texture.setWidth(image.getWidth());
texture.setHeight(image.getHeight());
if (image.getColorModel().hasAlpha())
srcPixelFormat = GL11.GL_RGBA;
else
srcPixelFormat = GL11.GL_RGB;
ByteBuffer textureBuffer = convertImageData(image, texture);
if (target == GL11.GL_TEXTURE_2D) {
GL11.glTexParameteri(target, GL11.GL_TEXTURE_MIN_FILTER, minFilter);
GL11.glTexParameteri(target, GL11.GL_TEXTURE_MAG_FILTER, magFilter);
}
GL11.glTexImage2D(target, 0, dstPixelFormat, image.getWidth(),
image.getHeight(), 0, srcPixelFormat,
GL11.GL_UNSIGNED_BYTE, textureBuffer);
return texture;
}
private static ByteBuffer convertImageData(BufferedImage bufferedImage,
Texture texture) {
ByteBuffer imageBuffer;
WritableRaster raster;
BufferedImage texImage;
int texWidth = bufferedImage.getWidth();
int texHeight = bufferedImage.getHeight();
texture.setTextureHeight(texHeight);
texture.setTextureWidth(texWidth);
if (bufferedImage.getColorModel().hasAlpha()) {
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
texWidth, texHeight, 4, null);
texImage = new BufferedImage(glAlphaColorModel, raster, false,
new Hashtable());
} else {
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
texWidth, texHeight, 3, null);
texImage = new BufferedImage(glColorModel, raster, false,
new Hashtable());
}
Graphics g = texImage.getGraphics();
g.setColor(new Color(0f, 0f, 0f, 0f));
g.fillRect(0, 0, texWidth, texHeight);
g.drawImage(bufferedImage, 0, 0, null);
// texImage is NOT skewed at this point
byte[] data = ((DataBufferByte) texImage.getRaster().getDataBuffer())
.getData();
imageBuffer = ByteBuffer.allocateDirect(data.length);
imageBuffer.order(ByteOrder.nativeOrder());
imageBuffer.put(data, 0, data.length);
imageBuffer.flip();
return imageBuffer;
}