我遇到了一个简单的基于tile的基于引擎的问题,我正在处理这个问题。在我的家用电脑上(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的相同版本,我假设有可能我的代码中存在一个错误。
希望有人更有经验的人可以帮助我解决这个问题。谢谢!
编辑:这是一张扭曲结果的图片。
沿着顶部和右侧的树木其实应该是在下面的精灵石。
这是精灵文件:
他们如何看起来坏了?你可以放一个图像吗?这将有助于诊断。 – Tim 2012-03-05 05:32:46
我编辑了一些图片! – Mick 2012-03-05 05:39:28
对不起,没有想到。如果你目前没有检查它,可能会抛出glGetError?我不知道这是否会捕捉任何东西,但有时它可以帮助您找出奇怪的方式混淆渲染的时髦问题。 – Tim 2012-03-05 05:45:04