Вопрос

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. 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. hTex is not recognized and querying for its location the result is -1. The GL_ARB_multitexture OpenGL extension is supported by the graphics card ( supports OpenGL version up to 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.

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

Since BUMP is not defined in your fragment shader, hTex is not used in your code, so is not an active uniform. This is expected behavior.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top