Heyho,
这个问题真的很烦人......
我正在尝试创建一个窗口并加载&使用LWJGL渲染纹理。
Window-Creation工作完美,纹理应该加载正确(经过测试,它始终是我想渲染的纹理),但它将呈现纯白色。
Initing OpenGL,加载纹理并渲染它:
private void initGL(int width, int height)
{
try
{
searchDisplayMode(width, height, true);
Display.create();
Display.setVSyncEnabled(true);
} catch (LWJGLException e)
{
e.printStackTrace();
System.exit(0);
}
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glViewport(0,0,width,height);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, width, height, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
public void loadTextures()
{
try
{
DirtID = textureManager.registerTexture("Dirt.png");
System.out.println(DirtID);
} catch (IOException e)
{
e.printStackTrace();
}
}
public void render()
{
GL11.glEnable(GL11.GL_TEXTURE_2D);
Color.white.bind();
// This was the bug! [Fixed it now]
textureManager.bindTexture(DirtID);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(0, 0);
GL11.glTexCoord2f(1, 0);
GL11.glVertex2f(0 + textureManager.getWidth(DirtID), 0);
GL11.glTexCoord2f(1, 1);
GL11.glVertex2f(0 + textureManager.getWidth(DirtID), 0 + textureManager.getHeight(DirtID));
GL11.glTexCoord2f(0, 1);
GL11.glVertex2f(0, 0 + textureManager.getHeight(DirtID));
GL11.glEnd();
}
在TextureManager.java中加载函数+其他东西:
public TextureManager()
{
textures = new ArrayList<TextureIO>();
}
public int registerTexture(String file) throws IOException
{
return registerTexture(file, false);
}
public int registerTexture(String file, boolean isFlipped) throws IOException
{
for (TextureIO t : textures)
{
if (t.getName().equals(file))
{
return t.getId();
}
}
if (isFlipped)
{
System.out.println("Register flipped Texture ~* " + file);
} else
{
System.out.println("Register Texture ~* " + file);
}
TextureIO texture = new TextureIO(file, isFlipped);
texture.init();
textures.add(texture);
return texture.getId();
}
public void bindTexture(int textureId)
{
if (boundedTexture != textureId)
{
glBindTexture(GL_TEXTURE_2D, textureId);
boundedTexture = textureId;
System.out.println("The texture " + boundedTexture + " was bound");
}
}
public void unbindTexture()
{
glBindTexture(GL_TEXTURE_2D, 0);
boundedTexture = 0;
}
public TextureIO getTexture(int textureId)
{
for (TextureIO tex: textures)
{
if (tex.getId() == textureId)
{
return tex;
}
}
return null;
}
和TextureManager使用的TextureIO.java:
public TextureIO(String ref, boolean flipped) throws IOException
{
BufferedImage image = ImageIO.read(TextureIO.class.getResourceAsStream("graphics/" + ref));
width = image.getWidth();
height = image.getHeight();
name = ref;
isFlipped = flipped;
if (flipped)
{
final AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
tx.translate(0, -height);
final AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
image = op.filter(image, null);
}
final int[] pixels = image.getRGB(0, 0, width, height, null, 0, width);
texture = BufferUtils.createIntBuffer(pixels.length);
texture.put(pixels);
texture.rewind();
}
public String getName()
{
return name;
}
public void init()
{
final IntBuffer buffer = BufferUtils.createIntBuffer(1);
glGenTextures(buffer);
id = buffer.get(0);
glBindTexture(GL_TEXTURE_2D, id);
/*
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);*/
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL12.GL_BGRA, GL_UNSIGNED_BYTE /*GL_UNSIGNED_INT_8_8_8_8_REV*/, texture);
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
}
public void bind()
{
glBindTexture(GL_TEXTURE_2D, id);
}
public void unbind()
{
glBindTexture(GL_TEXTURE_2D, 0);
}
结果:
也许你可以帮我解决这个问题。
答案 0 :(得分:1)
您在绘制时忘记启用GL_TEXTURE_2D
。因为你在纹理加载完成后就禁用了它。
在开始渲染四边形之前,你必须绑定纹理。