c ++ / OpenGL / GLSL,具有“随机”工件 [英] c++/OpenGL/GLSL, textures with "random" artifacts
问题描述
想知道某人是否经历过这种情况,知道原因。我使用纹理数组后得到这些奇怪的工件。
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
(我的gpu是AMD R9 270)
(My gpu is AMD R9 270)
- 忍者编辑删除了其余部分的可读性,因为它只是显示出可能出现问题的代码,因为项目已打开source现在我只显示问题的来源(片段着色器)
- 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;
}
编辑:github repos(对不起,如果它缺少一些libs,他们到github) https://github.com/PedDavid/NubDevEngineCpp
github repos (sorry if It is missing some libs, having trouble linking them to github) https://github.com/PedDavid/NubDevEngineCpp
修改: derhass 指出我在做某事与未定义的结果(访问数组没有常量([tid]))。我有它现在使用
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!
推荐答案
我使用纹理数组后得到这些奇怪的工件
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);
}
根据 GLSL 3.30规范允许, p>
What you try to do here is not allowed as per the GLSL 3.30 specification which states
在着色器中聚集到数组中的采样器(使用方括号
[])只能用整数常量表达式索引$ b section 4.3.3常量表达式)。
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").
您的 tid
不是一个常数,所以这不会工作。
Your tid
is not a constant, so this will not work.
在GL 4中,这个约束有点放松了(quote是从 GLSL 4.50 spec ):
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.
我不知道你想要实现什么,但也许你可以通过使用阵列贴图,它将一组完整的图像表示为单个GL纹理对象,并且不会在访问它们时施加这样的约束。
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.
这篇关于c ++ / OpenGL / GLSL,具有“随机”工件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!