使用OpenGL启用alpha图层

时间:2014-05-09 22:09:30

标签: java opengl lwjgl

我在Java中使用OpenGL和Lwjgl。

我只是想用alpha图层渲染我的纹理,但实际上,它看起来像这样: http://puu.sh/8FRzn.png

我的OpenGL配置:

    glEnable(GL_TEXTURE_2D);   

    glBlendFunc (GL_ONE, GL_ONE);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_BLEND);
    GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);  


    GL11.glViewport(0, 0, screenWidth, screenHeight);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, screenWidth, screenHeight, 0, 1, -1);
    glMatrixMode(GL_MODELVIEW);

以及我如何加载我的精灵表:

BLA BLA BLA ..
public static HashMap<String, Integer> loadTexture(String path) {
    BufferedImage image = null;
    HashMap<String, Integer> hashMap = new HashMap<String, Integer>();

    try {
        image = ImageIO.read(new File(path));
    } catch (IOException e) {
        e.printStackTrace();
    }

    int w = image.getWidth();
    int h = image.getHeight();

    int[] pixels = image.getRGB(0, 0, w, h, null, 0, w);

    ByteBuffer buffer = BufferUtils.createByteBuffer(w * h *4);

    for(int x = 0; x<w; x++) {
        for(int y = 0; y<h; y++) {
            Color color = new Color(pixels[x + y * w]);

            buffer.put((byte)color.getRed());
            buffer.put((byte)color.getGreen());
            buffer.put((byte)color.getBlue());
            buffer.put((byte)color.getAlpha());
        }
    }
    buffer.flip();

    int id = glGenTextures();

    glBindTexture(GL_TEXTURE_2D, id);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

    hashMap.put("width", w);
    hashMap.put("height", h);
    hashMap.put("id", id);

    return hashMap;
}

我用常用方法渲染它:GL_QUADS

是的,我的纹理有一个Alpha图层:http://puu.sh/8FS15.png

我已经在网上搜索了答案,但我的OpenGL“配置”与每次给出的解决方案相同..

1 个答案:

答案 0 :(得分:0)

我认为这是BufferedImage未启用Alpha通道的问题。尝试先创建这样的缓冲图像。

默认情况下,当您将图像作为BufferedImage加载时,它不会加载所述图像的Alpha图层。您必须在BufferedImage构造函数中定义此参数。但问题是,通过ImageIO加载图像时,会为您创建BufferedImage,并且您无法设置此选项。通过创建具有第一个图像的宽度和高度的空图像,您可以在新图像中设置此参数。然后,您可以通过使用Graphics2D类修改该图像的像素,这将允许您绘制原始图像(通过ImageIO加载到具有alpha支持的新图像)。它可能看起来有点混乱,但下面的代码示例应该能够稍微改进一下这个解释。

BufferedImage image = new BufferedImage(int width, int height, BufferedImage.TYPE_INT_ARGB);

这将告诉它使用BufferedImage加载alpha通道。

首先需要在单独的BufferedImage中加载图像以获取宽度和高度,然后使用图形类重新绘制它。

以下是一个完整的例子:

    try {
        BufferedImage in = ImageIO.read(new FileInputStream(path));
        BufferedImage image = new BufferedImage(in.getWidth(), in.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = image.createGraphics();
        g.drawImage(in, 0, 0, null);
        g.dispose();
        this.image = image;
    } catch(IOException e) {
        e.printStackTrace();
    }

最终的this.image指的是你在课程开始时的主要形象。

相关问题