Odd behavior of the GLSL switch statement

I want to draw a cube with a different texture on each side. I pass a Textnum parameter containing the amount of texture to use through my vertex shader for my fragment shader, and then use the switch statement to select the appropriate sampler2D based on its value:

#version 150

in float Shade;
in vec2 Textcoord;
in uint Textnum;

out vec4 outColor;

uniform sampler2D tex0;
uniform sampler2D tex1;
uniform sampler2D tex2;
uniform sampler2D tex3;
uniform sampler2D tex4;
uniform sampler2D tex5;

void main()
{
    vec4 textColor;
    switch(Textnum)
    {
        case 0u: textColor = texture(tex0, Textcoord); break;
        case 1u: textColor = texture(tex1, Textcoord); break;
        case 2u: textColor = texture(tex2, Textcoord); break;
        case 3u: textColor = texture(tex3, Textcoord); break;
        case 4u: textColor = texture(tex4, Textcoord); break;
        case 5u: textColor = texture(tex5, Textcoord); break;
    }
    outColor = textColor * vec4(Shade, Shade, Shade, 1);
}

With this code, the cube is not displayed at all. Textures load correctly, because when I do this:

void main()
{
    vec4 textColor; 
    textColor = texture(tex0, Textcoord);
    outColor = textColor * vec4(Shade, Shade, Shade, 1);
}

I see a cube with the same texture on each side. I tried this with all the probes, each time getting the expected result. What is interesting when I do:

void main()
{
    vec4 textColor;
    switch(Textnum)
    {
        case 0u: textColor = texture(tex0, Textcoord); break;
        case 1u: textColor = texture(tex1, Textcoord); break;
        case 2u: textColor = texture(tex2, Textcoord); break;
        case 3u: textColor = texture(tex3, Textcoord); break;
        case 4u: textColor = texture(tex4, Textcoord); break;
        case 5u: textColor = texture(tex5, Textcoord); break;
    }
    textColor = texture(tex0, Textcoord);
    outColor = textColor * vec4(Shade, Shade, Shade, 1);
}

I still do not see the cube, even when the statement after the switch statement should overwrite its effects. What is wrong with my code?

: , , , ( ):

void main()
{
    vec4 textColor;
    vec4 textColor0 = texture(tex0, Textcoord);
    vec4 textColor1 = texture(tex1, Textcoord);
    vec4 textColor2 = texture(tex2, Textcoord);
    vec4 textColor3 = texture(tex3, Textcoord);
    vec4 textColor4 = texture(tex4, Textcoord);
    vec4 textColor5 = texture(tex5, Textcoord);
    switch(Textnum)
    {
        case 0u: textColor = textColor0; break;
        case 1u: textColor = textColor1; break;
        case 2u: textColor = textColor2; break;
        case 3u: textColor = textColor3; break;
        case 4u: textColor = textColor4; break;
        case 5u: textColor = textColor5; break;
    }
    outColor = textColor * vec4(Shade, Shade, Shade, 1);
}

. :

void main()
{
    vec4 textColor[6];
    textColor[0] = texture(tex0, Textcoord);
    textColor[1] = texture(tex1, Textcoord);
    textColor[2] = texture(tex2, Textcoord);
    textColor[3] = texture(tex3, Textcoord);
    textColor[4] = texture(tex4, Textcoord);
    textColor[5] = texture(tex5, Textcoord);
    outColor = textColor[Textnum] * vec4(Shade, Shade, Shade, 1);
}

EDIT2: , : http://pastebin.com/R2scC0ZZ

+4
1

glsl . , . , , (mipmaps, ), .

.

: , , , . :

0(4) : error C5215: Integer varying TextNum must be flat

, : Integer , . , . , glUseProgram a GL_INVALID_ENUM, . :

flat out uint Textnum;

flat in uint Textnum;

.

+4

Source: https://habr.com/ru/post/1666994/


All Articles