gpt4 book ai didi

java - LWJGL 倾斜纹理

转载 作者:行者123 更新时间:2023-11-30 04:32:53 25 4
gpt4 key购买 nike

我试图将纹理与 LWJGL 一起使用,结果发现 RBG-png 有点倾斜。例子: Original picture/Texture

加载代码是 lwjgl wiki Space Invaders 示例的 98%。纹理.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;
}

最佳答案

我知道这是一个老问题,但我自己刚才也遇到了这个问题。假设您已经自己解决了这个问题,希望其他人也会受益。

纹理倾斜和变色的原因是它与您的 UNPACK_ALIGNMENT 没有正确对齐。据我了解,每行中的字节数必须是 UNPACK_ALIGNMENT 设置的倍数(默认为 4)。

对于 4 分量格式,这不是问题,因为每个像素由 4 个字节组成,因此任何尺寸的图像都会正确对齐。但对于其他格式,数据会被填充以保持正确对齐,这会导致这样的问题。

您可以更改图像的大小以使其正确对齐 ((width * formatComponents) % 4 == 0) 、将格式更改为 4 分量格式,或者更改UNPACK_ALIGNMENT 到不会使用以下方式填充图像的内容:

glPixelStore(GL_UNPACK_ALIGNMENT, 对齐);//对齐方式必须为 1、2、4 或 8

http://www.opengl.org/wiki/Common_Mistakes#Texture_upload_and_pixel_reads

关于java - LWJGL 倾斜纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14203226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com