java—glsl没有按我的预期工作的问题

eqoofvh9  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(339)

**结束。**此问题需要详细的调试信息。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。

19天前关门了。
改进这个问题
所以我想知道这是不是可以重现?
在调试java opengl项目时,我发现了一个着色器:


# version 420 core

uniform sampler2D texture1;
uniform sampler2D texture2;

in vec2 uv;

out vec4 fragColor;

void main(){
    //fragColor = texture(texture1, uv);
    fragColor = texture(texture2, uv);
}

看起来很简单,但现在当我取消//fragcolor=texture(texture1,uv)的命令并保留其余部分时,我会将texture1渲染到屏幕上。为什么?我的大脑说那是不对的,它不应该仅仅渲染纹理2,因为我覆盖了fragcolor吗?有人能解释一下吗?
更新1:我认为glsl编译有问题。
当没有纹理绑定到sampler0时,是否可以将纹理绑定到sampler1
更新2:创建纹理:在我的例子中,它只是一个带有1个样本的纹理,所以纹理是二维的,它的格式是.png,所以有4个通道,没有应用插值

texType = samples > 1 ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;

        int format;
        if (channels == 3) {
            format = GL_RGB;
        } else if (channels == 4) {
            format = GL_RGBA;
        } else {
            throw new AspectGraphicsException("textures can't be initialized with " + channels + " channels");
        }

        ID = glGenTextures();

        glBindTexture(texType, ID);

        glTexParameteri(texType,
                GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(texType,
                GL_TEXTURE_MAG_FILTER, interpolation ? GL_LINEAR : GL_NEAREST);

        glTexParameteri(texType, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(texType, GL_TEXTURE_WRAP_T, GL_REPEAT);

        if (samples > 1) {
            if (pixels == null) {
                glTexImage2DMultisample(texType, samples, format,
                        width, height, true);
            } else {
                throw new AspectGraphicsException("textures with defined with pixels can't be multisampled");
            }
        } else {
            if (pixels == null) {
                glTexImage2D(texType, 0, format, width, height,
                        0, format, GL_UNSIGNED_BYTE, NULL);
            } else {
                glTexImage2D(texType, 0, format,
                        width, height, 0, format,
                        GL_UNSIGNED_BYTE, pixels);
            }
        }
        glBindTexture(texType, 0);

绑定纹理:textype只是gl\u texture\u 2d,sampler name是“texture1”或“texture2”(请参见glsl着色器中的内容),sampler只是用于“texture1”:0和“texture2”:1

glActiveTexture(GL_TEXTURE0 + sampler);
glBindTexture(texType, ID);
shader.uniform1i(samplerName, sampler);
pgccezyw

pgccezyw1#

很可能您没有为采样器制服指定纹理单位,因此它们都被设置为指向 GL_TEXTURE0 . 可以在着色器中指定它,如下所示:


# version 420 core

layout(binding=0) uniform sampler2D texture1;
layout(binding=1) uniform sampler2D texture2;
// ...

然后将纹理绑定到:

glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, your_texture);
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, other_texture);
glDrawArrays(...);

如果这样做,你会得到正确的结果,不管什么制服被遗漏了。
请参见将纹理绑定到采样器。

相关问题