LWJGL: Texture renders with background color
I'm working on a game project I've been planning for a long time. I set up the basic things and want to make the textures work. I use a custom written TextureLoader (with a code snippet you might know) and put the resulting texture id into a hash map so I don't need to recreate a new texture id.
package net.sep87x.atmos.render;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.HashMap;
import javax.imageio.ImageIO;
import net.sep87x.atmos.client.Logger;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
public class TextureLoader {
HashMap<String, Integer> textureIds;
private Logger logger;
public TextureLoader() {
this.textureIds = new HashMap<String, Integer>();
logger = Logger.getInstance();
}
public int getTextureId(String path) {
if (this.textureIds.containsKey(path)) {
return this.textureIds.get(path);
} else {
try {
return bindTexture(path);
} catch (IOException e) {
return -1;
}
}
}
public int bindTexture(String path) throws IOException {
BufferedImage img = ImageIO.read(new File(path));
int[] pixels = new int[img.getWidth() * img.getHeight()];
img.getRGB(0, 0, img.getWidth(), img.getHeight(), pixels, 0, img.getWidth());
ByteBuffer buffer = BufferUtils.createByteBuffer(img.getWidth() * img.getHeight() * 4);
for (int y = 0; y < img.getHeight(); y++) {
for (int x = 0; x < img.getWidth(); x++) {
int pixel = pixels[y * img.getWidth() + x];
buffer.put((byte) ((pixel >> 16) & 0xFF));
buffer.put((byte) ((pixel >> 8) & 0xFF));
buffer.put((byte) (pixel & 0xFF));
buffer.put((byte) ((pixel >> 24) & 0xFF));
}
}
buffer.flip();
int textureID = GL11.glGenTextures();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, img.getWidth(), img.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
this.textureIds.put(path, textureID);
logger.log("Loaded "" + path + "" as " + textureID + ".");
return textureID;
}
}
After that, it shouldn't be that difficult to render the image, but it doesn't render as I'd like it to render. I want to draw a background gradient and the texture on top of it, but it seems like the texture is taking the color of the background.
Main class (render method):
private void run() {
init();
glEnable(GL_TEXTURE_2D);
while (!Display.isCloseRequested()) {
update();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
render();
glEnd();
Display.update();
Display.sync(60);
}
Display.destroy();
}
private void render() {
currentGuiScreen.render(this);
}
GuiScreen class (which is being called through render()):
private float fadeIn = 0f;
@Override
public void render(Atmos game) {
glPushMatrix();
glBegin(GL_QUADS);
if (fadeIn < 1) fadeIn += 0.001f;
glColor3f(0f, (148f / 255f) * fadeIn, 1f * fadeIn);
glVertex2f(0, 0);
glVertex2f(1920, 0);
glColor3f(0f, (83f / 255f), (142f / 255f));
glVertex2f(1920, 1080);
glVertex2f(0, 1080);
glEnd();
glPopMatrix();
glPushMatrix();
glBegin(GL_QUADS);
glBindTexture(GL_TEXTURE_2D, game.textureLoader.getTextureId("res/img/gui/atmos.png"));
glTexCoord2f(0, 0);
glVertex2f((game.width - 512f) / 2f, (game.height - 512f) / 2f);
glTexCoord2f(1, 0);
glVertex2f((game.width - 512f) / 2f + 512, (game.height - 512f) / 2f);
glTexCoord2f(1, 1);
glVertex2f((game.width - 512f) / 2f + 512, (game.height - 512f) / 2f + 512);
glTexCoord2f(0, 1);
glVertex2f((game.width - 512f) / 2f, (game.height - 512f) / 2f + 512);
glEnd();
glPopMatrix();
}
Now as I already read this code SHOULD work, but it doesn't. The result is the following:
http://i.imgur.com/ZlA26bt.png
But the texture is all in white (with a white to transparent gradient inside the font) and the background should have a blue to lightblue gradient. I know that I'm missing something, but I don't see what. Can somebody help me?
链接地址: http://www.djcxy.com/p/34028.html上一篇: 渲染帧缓冲显示错误的结果
下一篇: LWJGL:纹理使用背景颜色进行渲染