【问题标题】:Transparency issues with LWJGL "discard", everything is invisible?LWJGL“丢弃”的透明度问题,一切都是不可见的?
【发布时间】:2018-11-13 16:50:34
【问题描述】:

当我尝试在 LWJGL 3 中为我的纹理实现透明度时,模型似乎以完全透明的方式渲染,因此根本不可见。当我删除检查时,它工作正常。有什么线索可以解释为什么吗?

这是检查 alpha 分量的着色器代码:

#version 430

in vec2 pass_textureCoords;
in vec3 surfaceNormal;
in vec3 toLightVector[5];
in vec3 toCameraVector;
in float visibility;

out vec4 out_Color;

uniform sampler2D textureSampler;
uniform vec3 lightColour[5];
uniform vec3 attenuation[5];
uniform float shineDamper;
uniform float reflectivity;
uniform vec3 skyColour;

void main(void) {

    vec3 unitNormal = normalize(surfaceNormal);
    vec3 unitVectorToCamera = normalize(toCameraVector);

    vec3 totalDiffuse = vec3(0.0);
    vec3 totalSpecular = vec3(0.0);

    for (int i = 0; i < 5; i++) {
        float distance = length(toLightVector[i]);
        float attFactor = attenuation[i].x + (attenuation[i].y * distance) + (attenuation[i].z * distance * distance);
        vec3 unitLightVector = normalize(toLightVector[i]);
        float nDot1 = dot(unitNormal, unitLightVector);
        float brightness = max(nDot1, 0.0);
        vec3 lightDirection = -unitLightVector;
        vec3 reflectedLightDirection = reflect(lightDirection, unitNormal);
        float specularFactor = dot(reflectedLightDirection, unitVectorToCamera);
        specularFactor = max(specularFactor, 0.0);
        float dampedFactor = pow(specularFactor, shineDamper);
        totalDiffuse = totalDiffuse + (brightness * lightColour[i])/attFactor;
        totalSpecular = totalSpecular + (dampedFactor * reflectivity * lightColour[i])/attFactor;
    }
    totalDiffuse = max(totalDiffuse, 0.2);


////////////////////////////////////////////////////////////////////////////
    vec4 textureColour = texture(textureSampler, pass_textureCoords);
    if (textureColour.a < 0.5) {
        discard;
    }
////////////////////////////////////////////////////////////////////////////

    out_Color = vec4(totalDiffuse, 1.0) * textureColour + vec4(totalSpecular, 1.0);
    out_Color = mix(vec4(skyColour,1.0),out_Color, visibility);

}

我已经三重检查了所有内容都以正确的顺序加载到着色器等。但使纹理出现的唯一方法是删除此检查。

还有什么用的,请咨询。谢谢!

编辑 1:

从图像加载纹理:

protected Texture loadTexture(BufferedImage image, String info) {
    Texture texture;

    int width = image.getWidth();
    int height = image.getHeight();
    int[] pixels = image.getRGB(0, 0, width, height, null, 0, width);

    ByteBuffer buffer = BufferUtils.createByteBuffer((width * height) * 4);
    int id = glGenTextures();

    for (int i = 0; i < pixels.length; i++) {
        byte r = (byte)((pixels[i] >> 16) & 0xFF);
        byte g = (byte)((pixels[i] >> 8) & 0xFF);
        byte b = (byte)(pixels[i] & 0xFF);
        byte a = (byte)((pixels[i] >> 24) * 0xFF);
        buffer.put(r);
        buffer.put(g);
        buffer.put(b);
        buffer.put(a);
    }
    buffer.flip();
    glBindTexture(GL_TEXTURE_2D, id);
    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_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
    glBindTexture(GL_TEXTURE_2D, 0);

    texture = new Texture(id, buffer, width, height);
    if (info != "") processData(info, texture);
    return texture;
}

编辑 2:

尝试Out_Color = vec4(textureColour.aaa, 1.0);

【问题讨论】:

  • 您确定设置了 Alpha 通道吗?出于调试原因尝试out_Color = vec4(textureColour.aaa, 1.0)
  • @Rabbid76 似乎无法做到这一点。每次我尝试添加它时,它都会说找不到我的一件制服
  • @Rabbid76 查看编辑
  • @Rabbid76 似乎是这样,但我不知道为什么
  • @Rabbid76 我尝试在加载每个图像时输出a 的值,它是一个 1 的流。不太确定发生了什么...

标签: java opengl glsl shader lwjgl


【解决方案1】:

按照 Rabbid76 的建议,这是一个简单的修复方法,即在将图像读取到像素数组时,alpha 通道会丢失。相反,我现在使用:

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int pixel = image.getRGB(x, y);
            byte alpha = (byte)((pixel >> 24) & 0xff);
            byte red = (byte)((pixel >> 16) & 0xff);
            byte green = (byte)((pixel >> 8) & 0xff);
            byte blue = (byte)((pixel >> 0) & 0xff);
            buffer.put(red);
            buffer.put(green);
            buffer.put(blue);
            buffer.put(alpha);
        }
    }

效果很好!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 1970-01-01
    • 1970-01-01
    • 2022-08-04
    • 2013-01-19
    • 1970-01-01
    相关资源
    最近更新 更多