I’m working with Qt. I have multiple shaders working, however passing uniform arrays seems always to set all to zero. The expected result would be that random circles are drawn on top of the texture (or just a transparent texture due to too many circles).. What I get is a single particle circle on the lower right corner.
if (program->isLinked()) {
QVector3D hitPoints[40];
for (int k = 0; k < 40; k++) {
hitPoints[k] = QVector3D((float)rand()/(float)RAND_MAX, (float)rand()/(float)RAND_MAX, (float)rand()/(float)RAND_MAX);
}
program->setUniformValueArray("hitPoints", hitPoints, 40);
program->bind();
}
Fragment Shader:
uniform sampler2D color_texture;
uniform vec3 hitPoints[40];
void main()
{
float dist = 0.3;
vec2 texcoord = vec2(gl_TexCoord[0]);
for (int i = 0; i < 40; i++) {
float close = sqrt(pow(hitPoints[i].y - texcoord.y, 2) + pow(hitPoints[i].x - texcoord.x, 2));
if (close < dist) {
gl_FragColor = vec4(0,0,0,texture2D(color_texture, texcoord).a);
return;
}
}
gl_FragColor = texture2D(color_texture, texcoord);
}
When using plain OpenGL (without Qt), a call to
glUniformonly has effect if the corresponding program is active (usingglUseProgram). And sinceQGLShaderProgram‘ssetUniformandbindfunctions ought to be simple wrappers aroundglUniformandglUseProgramrespectively, you should change the order of function calls to:Thus always set uniforms of a program when the program is bound. But on the other hand the values of uniforms are persistent over binding and unbinding of programs, so you don’t have to set them each time you bind the program if they don’t change.
It’s sad though that the documentation doesn’t say anything about this issue. But since it names the GL functions that are wrapped by the corresponding Qt functions, I guess they just assume anyone messing with OpenGL to know about these facts anyway (in fact they are just lightweight wrappers and no new interface in itself).