Valerie
Valerie

Reputation: 51

Unable to use two uniform variables in GLSL Fragment Shader (only the first works)

I am trying to mix the colors of two textures in a fragment shader. But unfortunately i am stuck before the real work yet.

Here is shader setup:

const char* vertex = "void main(){ \n\
                gl_Position = ftransform(); \n\
                        gl_TexCoord[0] = gl_MultiTexCoord0;\n\
                        gl_TexCoord[1] = gl_MultiTexCoord1;\n\
                     }\n";

const char* fragment = "\
                       uniform sampler2D userT;\
                       uniform sampler2D videoTex; \
                       \n\
                       void main(){\n\
                            vec4 texel = texture2D(userT, gl_TexCoord[0].st);\n\
                            gl_FragColor = texel;\n\
                        }\n";

m_vertexShader = glCreateShader(GL_VERTEX_SHADER);
m_fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

glShaderSource( m_vertexShader, 1, &vertex, NULL );
glShaderSource( m_fragmentShader, 1, &fragment, NULL );

glCompileShader( m_vertexShader );
glCompileShader( m_fragmentShader );

m_silhouetteShaderProg = glCreateProgram();
glAttachShader( m_silhouetteShaderProg, m_vertexShader );
glAttachShader( m_silhouetteShaderProg, m_fragmentShader );
glLinkProgram( m_silhouetteShaderProg );

For now the fragment shader only displays one of the txtures, this will be changed later, if i get both textures working at this place.

I did check for compilation and linking errors, and everything is fine.

And here is my display function:

 .... setup of the textures m_videoTexture and m_userPixelTexture with glTexSubImage2D calls ....

glUseProgram( m_silhouetteShaderProg );

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_videoTexture);
GLint videoTexUnif = glGetUniformLocation( m_silhouetteShaderProg, "videoTex" );
glUniform1i( videoTexUnif, 0 );

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m_userPixelTexture);
GLint userTexUnif = glGetUniformLocation( m_silhouetteShaderProg, "userT" );
glUniform1i( userTexUnif, 1 );

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

glBegin(GL_QUADS);
    glMultiTexCoord2f(GL_TEXTURE0, 1.0f, 0.0f);
    glMultiTexCoord2f(GL_TEXTURE1, 1.0f, 0.0f);
    glVertex3f(-0.5f, -0.5f, 0.0f);
    glMultiTexCoord2f(GL_TEXTURE0, 1.0f, 1.0f);
    glMultiTexCoord2f(GL_TEXTURE1, 1.0f, 1.0f*stretch);
    glVertex3f(-0.5f, 0.5f, 0.0f);
    glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 1.0f);
    glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 1.0f*stretch);
    glVertex3f(0.5f, 0.5f, 0.0f);
    glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 0.0f);
    glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 0.0f);
    glVertex3f(0.5f, -0.5f, 0.0f);
glEnd();

glUseProgram( 0 );
glActiveTexture(GL_TEXTURE0);

My Problem is that i can only access the first uniform of my fragment shader ("userT") with "glGetUniformLocation". The call: "GLint videoTexUnif = glGetUniformLocation( m_silhouetteShaderProg, "videoTex" );" results in "-1".

If i change the order of the uniforms in the fragment shader, i can access "videoTex" but not "userT".

I saw many examples using two uniforms in the fragment shader, so i strongly assume that this is possible. But what am i doing wrong here?

Upvotes: 1

Views: 2557

Answers (1)

kvark
kvark

Reputation: 5351

Your second uniform sampler was optimized out by the GLSL compiler because it's not used. In a linked GLSL program there is no such uniform, so your program logic should take that into account (any uniform can be optimized out based on your code).

Upvotes: 7

Related Questions