LWJGL png纹理透明度(textureColour.a白色代替黑色) [英] LWJGL png texture transparency (textureColour.a white color instead of black)

查看:94
本文介绍了LWJGL png纹理透明度(textureColour.a白色代替黑色)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有由PNG图片纹理化的草模型.我得到的是白色,而不是黑色.为什么会这样,我应该怎么做才能解决该问题?我正在使用 LWJGL 3 PNGDecoder.jar

I have grass models textured by PNG pictures. And i get white background color instead of black, which I want to. Why it is so and what should i do to fix that? I am using LWJGL 3 and PNGDecoder.jar

纹理加载器代码:

public int loadTexture(String fileName) {
    ByteBuffer buf = null;
    int tWidth = 0;
    int tHeight = 0;

    try {
        // Open the PNG file as an InputStream
        InputStream in = new FileInputStream("res/" + fileName + ".png");
        // Link the PNG decoder to this stream
        PNGDecoder decoder = new PNGDecoder(in);

        // Get the width and height of the texture
        tWidth = decoder.getWidth();
        tHeight = decoder.getHeight();

        // Decode the PNG file in a ByteBuffer
        buf = ByteBuffer.allocateDirect(
                4 * decoder.getWidth() * decoder.getHeight());
        decoder.decode(buf, decoder.getWidth() * 4, Format.RGBA);
        buf.flip();

        in.close();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(-1);
    }

    // Create a new texture object in memory and bind it
    int textureId = GL11.glGenTextures();
    GL13.glActiveTexture(textureId);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId);

    // All RGB bytes are aligned to each other and each component is 1 byte
    GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);

    // Upload the texture data and generate mip maps (for scaling)
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, tWidth, tHeight, 0,
            GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf);
    GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D);

    // Setup the ST coordinate system
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);

    // Setup what to do when the texture has to be scaled
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER,
            GL11.GL_NEAREST);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER,
            GL11.GL_LINEAR_MIPMAP_LINEAR);

    return textureId;
}

推荐答案

如果要使透明纹理看起来透明,则必须首先启用混合.将以下内容放在OpenGL初始化代码中.

If you want transparent textures to look transparent, you have to enable blending first. Put the following somewhere in your OpenGL initialization code.

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);


您还在内部以RGB而不是RGBA格式存储纹理.


You are also internally storing your texture in RGB instead of RGBA format.

GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, tWidth, tHeight, 0,
    GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf);

应该成为

GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, tWidth, tHeight, 0,
    GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf);

这篇关于LWJGL png纹理透明度(textureColour.a白色代替黑色)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆