我有一个混合了多个纹理的着色器。我发送纹理数作为统一的int变量。当我遍历纹理(在条件中使用此变量)时,着色器链接失败,但是当我放置确切数量的纹理时,一切正常
//this works
if(numberOfTextures==2) { //checking if this var really contains 2 - true
for(int i=0;i<2;i++) {
texturesBlend += texture2D(gSamplers[i], TexCoord0.xy);
}
texturesBlend /= numberOfTextures;
}
//this does not work
if(numberOfTextures==2) {
for(int i=0;i<numberOfTextures;i++) { //variable in condition - this causes linking error
texturesBlend += texture2D(gSamplers[i], TexCoord0.xy);
}
texturesBlend /= numberOfTextures;
}
这个问题可能是什么原因?如果我可以检查变量的值是什么,为什么我不能在条件中使用它?
EDIT 完整着色器源
#version 150
const int MAX_TEXTURES = 6;
in vec2 TexCoord0;
in vec3 Normal0;
out vec4 FragColor;
struct DirectionalLight
{
vec3 Color;
float AmbientIntensity;
float DiffuseIntensity;
vec3 Direction;
};
uniform DirectionalLight gDirectionalLight;
uniform int numberOfTextures;
uniform sampler2D gSamplers[MAX_TEXTURES];
void main()
{
vec4 AmbientColor = vec4(gDirectionalLight.Color, 1.0f) *
gDirectionalLight.AmbientIntensity;
float DiffuseFactor = dot(normalize(Normal0), -gDirectionalLight.Direction);
vec4 DiffuseColor;
if (DiffuseFactor > 0) {
DiffuseColor = vec4(gDirectionalLight.Color, 1.0f) *
gDirectionalLight.DiffuseIntensity *
DiffuseFactor;
}
else {
DiffuseColor = vec4(0, 0, 0, 0);
}
vec4 texturesBlend = vec4(0.0, 0.0, 0.0, 0.0);
int N = 1;
if(numberOfTextures<=2) {
for(int i=0;;i++) {
if(i==numberOfTextures)
break;
texturesBlend += texture2D(gSamplers[i], TexCoord0.xy);
}
texturesBlend /= numberOfTextures;
}
FragColor = texturesBlend * (AmbientColor + DiffuseColor);
};