纹理图集在不同的计算机上呈现不同

时间:2012-03-05 05:30:44

标签: java opengl texture-mapping

我正在研究一个简单的基于2d磁贴的引擎。在我的家用电脑(Windows 7 64bit,jogl 1.1.1)上,纹理与瓷砖正确绑定,但在我的笔记本电脑上(Windows Vidta 32bit,jogl 1.1.1),它们看起来很破碎。

精灵图像为600x100(每个精灵为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

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

这是精灵文件:

enter image description here

1 个答案:

答案 0 :(得分:2)

OpenGL自动生成的mipmap导致了这个问题。将以下行更改为不允许自动mipmapping可以解决问题。

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

如果有人想评论我应该如何创建我的精灵文件以允许自动mipmapping,或者如果这应该打开,请执行:)

随着mipmapping关闭,当我移动角色时,一些瓷砖在它们下方会出现黑线。我将不得不弄清楚如何使精灵图像成为mipmap。

编辑:使文件为方形并且为2的幂。