c++/OpenGL/GLSL, textures with “random” artifacts

风格不统一 提交于 2019-12-13 07:57:34

问题


Would like to know if someone has experienced this and knows the reason. I'm getting these strange artifacts after using "texture arrays"

http://i.imgur.com/ZfLYmQB.png

(My gpu is AMD R9 270)

  • ninja edit deleted the rest of the post for readability since it was just showing code where the problem could have been, since the project is open source now I only show the source of the problem(fragment shader)

Frag:

#version 330 core

layout (location = 0) out vec4 color;

uniform vec4 colour;
uniform vec2 light;

in DATA{
    vec4 position;
    vec2 uv;
    float tid;
    vec4 color;
}fs_in;

uniform sampler2D textures[32];

void main()
{
    float intensity = 1.0 / length(fs_in.position.xy - light);
    vec4 texColor = fs_in.color;
    if(fs_in.tid > 0.0){    
        int tid = int(fs_in.tid - 0.5);
        texColor = texture(textures[tid], fs_in.uv);
    }
    color = texColor; // * intensity;
}

Edit: github repos (sorry if It is missing some libs, having trouble linking them to github) https://github.com/PedDavid/NubDevEngineCpp

Edit: Credits to derhass for pointing out I was doing something with undefined results (accessing the array without a constant ([tid])). I have it now working with

switch(tid){
    case 0: textureColor = texture(textures[0], fs_in.uv); break;
    ...
    case 31: textureColor = texture(textures[31], fs_in.uv); break;
}

Not the prettiest, but fine for now!


回答1:


I'm getting these strange artifacts after using "texture arrays"

You are not using "texture arrays". You use arrays of texture samplers. From your fragment shader:

#version 330 core
// ...
in DATA{
    // ...
    float tid;
}fs_in;

//...

    if(fs_in.tid > 0.0){    
        int tid = int(fs_in.tid - 0.5);
        texColor = texture(textures[tid], fs_in.uv);
    }

What you try to do here is not allowed as per the GLSL 3.30 specification which states

Samplers aggregated into arrays within a shader (using square brackets [ ]) can only be indexed with integral constant expressions (see section 4.3.3 “Constant Expressions”).

Your tid is not a constant, so this will not work.

In GL 4, this constraint has been somewhat relaxed to (quote is from GLSL 4.50 spec):

When aggregated into arrays within a shader, samplers can only be indexed with a dynamically uniform integral expression, otherwise results are undefined.

Your now your input also isn't dynamically uniform either, so you will get undefined results too.

I don't know what you are trying to achieve, but maybe you can get it dobe by using array textures, which will represent a complete set of images as a single GL texture object and will not impose such constraints at accessing them.




回答2:


That looks like the shader is rendering whatever random data it finds in memory. Have you tried checking that glBindTexture(...) is called at the right time (before render) and that the value used (as returned by glGenTextures(...)) is valid?



来源:https://stackoverflow.com/questions/32261689/c-opengl-glsl-textures-with-random-artifacts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!