Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glUniform fails to set sampler value

I'm using OpenGL and GLSL to draw a texture over a simple mesh.

My problem is that when I am using glUniform1i to set the value of a sampler2D uniform, it was not set. For example in the in this code:

glUseProgram(programObject);

glUniform1i(glGetUniformLocation(programObject, "texture"), 1);
GLint val;
glGetUniformiv(programObject, 
               glGetUniformLocation(programObject, "texture"), 
               &val);
printf("Value is %d\n", val);

The value printed at command line out is 0. I have checked that the shaders are compiled correctly and the program is linked correctly. glGetUniformLocation outputs an index > 0. Furthermore, glGetError doesn't output any error at any point.

This only happens when the uniform is a sampler. As the texture is not set, quering it in the shader always returns (0, 0, 0, 1). (I have also checked, using apitrace, that my texture is correctly bound to GL_TEXTURE1, which is done inmediately after the code shown).

I have searched extensively for this problem and I have only found one instance of something similar to this here. The solution was to initialize GLEW, but this hasn't worked for me.

I'm happy to provide any extended info needed (or the trace from apitrace).

Edit

Here is the shader:

uniform sampler2D texture;

varying vec2 textureCoord;

void main() {
    gl_FragColor = texture2D(texture, textureCoord);
}

Solution

I managed to solve the problem after some work.

As it turns out, this problem wasn't due to the uniform not being set, as always returning 0 for samplers regardless of actual value appears to be a quirk of the gl implementation. This happened under Intel's driver for Sandy Bridge only. Testing the code under other implementations always returned the correct sampler value.

The problem of the texture returning (0, 0, 0, 1) always was due to a mistake I made during the generation of the texture.

Now the glGerUniformiv still returns 0, but the texture used is the correct one.

like image 813
fescrb Avatar asked Feb 21 '12 14:02

fescrb


Video Answer


1 Answers

Solution

I managed to solve the problem after some work.

As it turns out, this problem wasn't due to the uniform not being set, as always returning 0 for samplers regardless of actual value appears to be a quirk of the gl implementation. This happened under Intel's driver for Sandy Bridge only. Testing the code under other implementations always returned the correct sampler value.

The problem of the texture returning (0, 0, 0, 1) always was due to a mistake I made during the generation of the texture.

Now the glGerUniformiv still returns 0, but the texture used is the correct one.

like image 68
Tim Avatar answered Oct 13 '22 12:10

Tim