LWJGL:纹理呈现背景颜色

时间:2013-10-04 20:36:04

标签: java opengl textures render lwjgl

我正在研究一个我一直在计划的游戏项目。我设置了基本的东西,并希望使纹理工作。我使用自定义编写的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

但纹理全部为白色(字体内部为白色到透明渐变),背景应为蓝色到浅蓝色渐变。我知道我错过了什么,但我看不出来。有人可以帮帮我吗?

0 个答案:

没有答案