LWJGL:纹理使用背景颜色进行渲染

我正在研究一个我一直在计划的游戏项目。 我设置了基本的东西,并希望使纹理起作用。 我使用自定义的书写纹理加载器(带有代码片段,你可能知道)并将生成的纹理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

但纹理全部为白色(字体内部为白色至透明渐变),背景应具有蓝色到浅蓝色渐变。 我知道我错过了一些东西,但我不明白。 有人能帮助我吗?

链接地址: http://www.djcxy.com/p/34027.html

上一篇: LWJGL: Texture renders with background color

下一篇: LWJGL textures rendering as single color