简体   繁体   English

OpenGL着色器:统一变量计数不正确

[英]OpenGL shaders: uniform variables count incorrect

I want to do bump/normal/parallax mapping but for this purpose I need multitexturing - use 2 textures at a time - one for the color and one for the height map. 我想做凹凸/法线/视差贴图,但是为此,我需要多重纹理-一次使用2个纹理-一个用于颜色,一个用于高度图。 But this task accomplishment appeared absurdly problematic. 但是,这项任务的完成似乎是有问题的。

I have the following code for the vertex shader: 我有以下用于顶点着色器的代码:

    #version 330 core

    /* 0: in
     * 1: out
     * 2: uniform
     */

    // 0: in
    layout (location = 0) in vec3 v_vertexPos;
    layout (location = 1) in vec2 v_vertexTexCoord;

    // 1: out
    out vec2 f_vertexTexCoord;

    // 2: uniform
    uniform mat4 vertexMvp = mat4( 1.0f );

    void main()
    {
        f_vertexTexCoord = v_vertexTexCoord;
        gl_Position = vertexMvp * vec4( v_vertexPos, 1.0f );
    }

and the following for the fragment one: 以下是片段一:

    #version 330 core

    /* 0: in
     * 1: out
     * 2: uniform
     */

    // 0: in
    in vec2 f_vertexTexCoord;

    // 1: out
    layout (location = 0) out vec4 f_color;

    // 2: uniform
    uniform sampler2D cTex;
    uniform sampler2D hTex;

    // #define BUMP

    void main()
    {
        vec4 colorVec = texture2D( cTex, f_vertexTexCoord );

    #ifdef BUMP
        vec4 bumpVec = texture2D( hTex, f_vertexTexCoord );
        f_color = vec4( mix( bumpVec.rgb, colorVec.rgb, colorVec.a), 1.0 );
    #else
        f_color = texture2D( cTex, f_vertexTexCoord );
    #endif
    }

The shaders get compiled and attached to the shader program. 着色器被编译并附加到着色器程序。 The program is then linked and then used. 然后将程序链接并使用。 The only reported active uniform variables by glGetActiveUniform are the vertex shader's uniform vertexMvp and the fragment one's cTex. glGetActiveUniform唯一报告的活动均匀变量是顶点着色器的均匀vertexMvp和片段的cTex。 hTex is not recognized and querying for its location the result is -1. 无法识别hTex,并查询其位置结果为-1。 The GL_ARB_multitexture OpenGL extension is supported by the graphics card ( supports OpenGL version up to 4.3 ). 图形卡支持GL_ARB_multitexture OpenGL扩展(支持OpenGL版本最高为4.3)。

Tested the simple multitexturing example provided here which has only fragment shader defined, using the stock vertex one. 测试了此处提供的简单多纹理示例,该示例仅使用普通顶点一个定义了片段着色器。 This example works like a charm. 这个例子就像一个魅力。

Any suggestions? 有什么建议么?

"GLSL compilers and linkers try to be as efficient as possible. Therefore, they do their best to eliminate code that does not affect the stage outputs. Because of this, a uniform defined in a shader file does not have to be made available in the linked program. It is only available if that uniform is used by code that affects the stage output, and that the uniform itself can change the output of the stage. “ GLSL编译器和链接器尝试尽可能地提高效率。因此,它们会尽力消除不会影响阶段输出的代码。因此,不必在着色器文件中定义统一的代码。链接程序,仅在影响舞台输出的代码使用该制服并且制服本身可以更改舞台的输出时才可用。

Therefore, a uniform that is exposed by a fully linked program is called an "active" uniform; 因此,由完全链接的程序公开的制服称为“活动”制服; any other uniform specified by the original shaders is inactive. 原始着色器指定的任何其他统一无效。 Inactive uniforms cannot be used to do anything in a program." - OpenGL.org 不活动的制服不能在程序中做任何事情。”- OpenGL.org

Since BUMP is not defined in your fragment shader, hTex is not used in your code, so is not an active uniform. 由于未在片段着色器中定义BUMP,因此在代码中未使用hTex,因此不是活动制服。 This is expected behavior. 这是预期的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM