我正在做一个我计划了很长时间的游戏项目。我设置了基本的东西并想让纹理工作。我使用自定义编写的 TextureLoader(带有您可能知道的代码片段)并将生成的纹理 id 放入哈希图中,因此我不需要重新创建新的纹理 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;
}
}
在那之后,渲染图像应该不难,但它并没有像我希望的那样渲染。我想在它上面绘制一个背景渐变和纹理,但似乎纹理正在采用背景的颜色。
主类(渲染方法):
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 类(通过 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();
}
现在我已经阅读了这段代码应该可以工作,但事实并非如此。结果如下:
http://i.imgur.com/ZlA26bt.png
但是纹理都是白色的(字体内部有白色到透明的渐变),背景应该有蓝色到浅蓝色的渐变。我知道我错过了一些东西,但我不明白是什么。有人可以帮助我吗?