gpt4 book ai didi

java - 纹理图集在不同计算机上呈现不同的效果

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

我正在开发的基于简单 2D 图 block 的引擎遇到问题。在我的家用计算机(Windows 7 64 位,jogl 1.1.1)上,纹理正确绑定(bind)到图 block ,但在我的笔记本电脑(Windows Vidta 32 位,jogl 1.1.1)上,它们看起来已损坏。

Sprite 图像为 600x100(每个 Sprite 为 100x100)。

这是我正在使用的textureManager 类。

public class TextureManager {

private static Texture textureAtlas;
private static Map<String, TextureCoords> locations;
private static String imageName;

public TextureManager() {

locations = new HashMap<String, TextureCoords>();
}

public static void loadAtlas(String name) {

if(textureAtlas != null) {

if(imageName == name) return;

textureAtlas.dispose();
}

imageName = name;

try {

textureAtlas = TextureIO.newTexture(new File("textures/" + name + ".png"), true);
}
catch (GLException e) {

e.printStackTrace();
}
catch (IOException e) {

e.printStackTrace();
}

setAtlasLocations();
}

private static void setAtlasLocations() {

locations.put("blank", textureAtlas.getSubImageTexCoords(0, 0, 100, 100));
locations.put("tree1", textureAtlas.getSubImageTexCoords(100, 0, 200, 100));
locations.put("tree2", textureAtlas.getSubImageTexCoords(200, 0, 300, 100));
locations.put("tree3", textureAtlas.getSubImageTexCoords(300, 0, 400, 100));
locations.put("rock", textureAtlas.getSubImageTexCoords(400, 0, 500, 100));
}

public static void bindTexture() {

textureAtlas.bind();
}

public static TextureCoords getCoords(String name) {

return locations.get(name);
}
}

这是渲染代码:

    public void draw(GL gl) {

gl.glEnable(GL.GL_TEXTURE_2D);

TextureManager.bindTexture();

int width = map.width();
int height = map.height();

float x = InterfaceManager.mapX;
float y = InterfaceManager.mapY;

gl.glTranslatef(x, y - height, 0);

gl.glBegin(GL.GL_QUADS);

gl.glNormal3f(0.0f, 0.0f, 1.0f);
gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

for(int w = 0; w < width; w++) {

for(int h = 0; h < height; h++) {

TextureCoords coords = getCoord(map.getTileType(w, h));

gl.glTexCoord2f(coords.left(), coords.bottom());
gl.glVertex3i(w, h, 0);
gl.glTexCoord2f(coords.right(), coords.bottom());
gl.glVertex3i(w+1, h, 0);
gl.glTexCoord2f(coords.right(), coords.top());
gl.glVertex3i(w+1, h+1, 0);
gl.glTexCoord2f(coords.left(), coords.top());
gl.glVertex3i(w, h+1, 0);
}
}

gl.glEnd();

gl.glTranslatef(-x, -y + height, 0);

gl.glDisable(GL.GL_TEXTURE_2D);
}

private TextureCoords getCoord(int tileType) {

if(tileType == 0) return TextureManager.getCoords("blank");
else if(tileType == 1) return TextureManager.getCoords("rock");
else if(tileType == 2) return TextureManager.getCoords("tree1");
else if(tileType == 3) return TextureManager.getCoords("tree2");
else if(tileType == 4) return TextureManager.getCoords("tree3");
else return TextureManager.getCoords("blank");
}

我知道 opengl 应该是独立于平台的,并且由于我在两者上使用相同版本的 opengl,我假设我的代码中可能存在错误。

希望有经验的人可以帮助我解决这个问题。谢谢!

编辑:这是倾斜结果的图片。

screwedTexture

顶部和右侧的树木实际上应该是下面 Sprite 中的岩石。

这是 Sprite 文件:

enter image description here

最佳答案

OpenGL 自动生成的 mipmap 导致了该问题。更改以下行以不允许自动 mipmapping 可以修复问题。

textureAtlas = TextureIO.newTexture(new File("textures/" + name + ".png"), false);

如果有人想评论我应该如何创建我的 Sprite 文件以允许自动 mipmapping,或者如果这甚至应该打开,请这样做:)

关闭 mipmapping 后,当我移动角色时,一些图 block 下方会出现一条黑线。我必须弄清楚如何使 Sprite 图像支持 mipmap。

编辑:将文件设为 2 的平方。

关于java - 纹理图集在不同计算机上呈现不同的效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9562042/

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