正在删除的GLSL Uniforms在switch语句中使用

时间:2015-02-10 04:32:32

标签: c++ opengl glsl shader

好的 - 我正在尝试创建一个使用多个texutre库的OpenGL程序 - 我正在使用纹理坐标的那个数字来决定使用哪个库。要做到这一点,我必须使用switch-case语句,编译器似乎认为它是无关的并删除它,导致我的glGetUniformLocation调用失败。这是我的GLSL代码:

顶点着色器:

    // define the version of GLSL to use

        #version 330 core

        // location and render order. Both defined for all verticies using a GL_ARRAY_BUFFER
        layout(location = 0) in vec2 in_location;
        layout(location = 1) in vec2 in_UV;

        // uniforms
        uniform int renderOrder;
        uniform mat4 viewMat;


        // data to be sent and interpolated to the fragment shader
        out vec2 UV;

        // entry point
        void main()
        {
            // gl_Position is a special vec3 that defines the location in space.

            // set the final position
            vec4 location = vec4(in_location, (9.f - renderOrder) / 9.f, 1.f);
            gl_Position = (viewMat * location);

            // send the data to the fragment shader and interpolates it so it has the correct value
            UV = in_UV;
        }


fragment shader:

    // define the version of GLSL to use
#version 330 core

// the texture object we can query from
uniform sampler2D tex0;
uniform sampler2D tex1;
uniform sampler2D tex2;
uniform sampler2D tex3;
uniform sampler2D tex4;
uniform sampler2D tex5;
uniform sampler2D tex6;
uniform sampler2D tex7;
uniform sampler2D tex8;
uniform sampler2D tex9;
uniform sampler2D tex10;
uniform sampler2D tex11;
uniform sampler2D tex12;
uniform sampler2D tex13;
uniform sampler2D tex14;
uniform sampler2D tex15;
uniform sampler2D tex16;

// the UV that we interpolated and sent here in the vertex shader
in vec2 UV;

// the output
out vec4 color;


void main()
{
    // use the texture2D method to get the color at the corresponding texture location using the filtering method provided.
    // http://www.arcsynthesis.org/gltut/Texturing/Tut15%20Magnification.html
    // also filps Y coordinate of UVs

    int texID = int(floor(UV.x));

    switch (texID)
    {
    case 0:
        color = texture2D(tex0, UV);
    case 1:
        color = texture2D(tex1, vec2(UV.x - texID, UV.y - texID));
    case 2:
        color = texture2D(tex2, vec2(UV.x - texID, UV.y - texID));
    case 3:
        color = texture2D(tex3, vec2(UV.x - texID, UV.y - texID));
    case 4:
        color = texture2D(tex4, vec2(UV.x - texID, UV.y - texID));
    case 5:
        color = texture2D(tex5, vec2(UV.x - texID, UV.y - texID));
    case 6:
        color = texture2D(tex6, vec2(UV.x - texID, UV.y - texID));
    case 7:
        color = texture2D(tex7, vec2(UV.x - texID, UV.y - texID));
    case 8:
        color = texture2D(tex8, vec2(UV.x - texID, UV.y - texID));
    case 9:
        color = texture2D(tex9, vec2(UV.x - texID, UV.y - texID));
    case 10:
        color = texture2D(tex10, vec2(UV.x - texID, UV.y - texID));
    case 11:
        color = texture2D(tex11, vec2(UV.x - texID, UV.y - texID));
    case 12:
        color = texture2D(tex12, vec2(UV.x - texID, UV.y - texID));
    case 13:
        color = texture2D(tex13, vec2(UV.x - texID, UV.y - texID));
    case 14:
        color = texture2D(tex14, vec2(UV.x - texID, UV.y - texID));
    case 15:
        color = texture2D(tex15, vec2(UV.x - texID, UV.y - texID));
    case 16:
        color = texture2D(tex16, vec2(UV.x - texID, UV.y - texID));

    default:
        break;
    }

}

我的GPU是NVIDIA GTX 970 驱动程序版本347.25

1 个答案:

答案 0 :(得分:0)

好的。我的问题似乎已经通过在每种情况下放置休息来解决。不知道为什么这会产生影响,但它解决了这个问题。