1
我目前正在使用java & lwjgl在我的主菜單中嘗試繪製背景圖像。出於某種原因,無論我使用何種紋理加載和繪圖技術,圖像都會被完全搞砸。LWJGL螺紋紋理
這是發生了什麼:
而這正是它應該是這樣的:
這是我的加載紋理代碼:
private int loadTexture(String imgName) {
try {
BufferedImage img = ImageIO.read(JarStreamLoader.load(imgName));
ByteBuffer buffer = BufferUtils.createByteBuffer(img.getWidth() * img.getHeight() * 3);
for (int x = 0; x < img.getWidth(); x++) {
for (int y = 0; y < img.getHeight(); y++) {
Color color = new Color(img.getRGB(x, y));
buffer.put((byte) color.getRed());
buffer.put((byte) color.getGreen());
buffer.put((byte) color.getBlue());
}
}
buffer.flip();
int textureId = glGenTextures();
glBindTexture(GL_TEXTURE_2D, textureId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.getWidth(), img.getHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, buffer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
return textureId;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
這就是我的渲染代碼:
public static void drawRect(int x, int y, int width, int height, Color color) {
glColor4f(color.getRed()/255, color.getGreen()/255, color.getBlue()/255, 1.0F);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2d(x, y);
glTexCoord2f(0.0f, 1.0F);
glVertex2d(x, y + height);
glTexCoord2f(1.0F, 1.0F);
glVertex2d(x + width, y + height);
glTexCoord2f(1.0F, 0.0f);
glVertex2d(x + width, y);
glEnd();
}
任何想法?
謝謝,它現在工作完美:) – Twometer