# Setting a uniform float in a fragment shader results in strange values, is this a type conversion? How can it be fixed?

First, some details:

In a fragment shader, I'm trying to make an image do a sort of "flipping mirroring" animation (lines 44-57):

#version 450 core
in vec3 Color
in vec2 Texcoord;
out vec4 outColor;
uniform sampler2D tex;
uniform float factor;
void main() {
if (Texcoord.y < factor) {
outColor = texture(tex, Texcoord) * vec4(Color, 1.0);
} else {
outColor = texture(tex, vec2(Texcoord.x, 1.0 - Texcoord.y));
}
}


When factor is 1, the image should be right-side up and has some color on it. When factor is 0, the image should be upside down and has no color added to it. When factor is 0.5, the top half should be right-side up and the bottom half should be upside down.

Currently, that is only the case if I replace factor with the number.

When I set the uniform factor with glUniform1f(), I'm getting very strange results. To illistrate, I added some debug code to lines 188-197 that sets the uniform with one number, retrieves the number from the uniform, and outputs both values to try and see what's going on.

Here's the code:

GLfloat factorToSet = 1.0f;
GLfloat setFactor = 0.0f;

while (factorToSet > -0.1f) {
glUniform1f(uniFactor, factorToSet);
printf("Factor of %.1f becomes %f\n", factorToSet, setFactor);
factorToSet -= 0.1;
}


And here are the results:

Factor of 1.0 becomes 0.000000
Factor of 0.9 becomes -2.000000
Factor of 0.8 becomes -0.000000
Factor of 0.7 becomes 2.000000
Factor of 0.6 becomes 0.000000
Factor of 0.5 becomes -0.000000
Factor of 0.4 becomes 2.000000
Factor of 0.3 becomes -36893488147419103232.000000
Factor of 0.2 becomes 0.000000
Factor of 0.1 becomes 36893488147419103232.000000
Factor of -0.0 becomes -0.000000


So with what little I understand about OpenGL and the way scalar types are stored in binary, I'm thinking that this issue is caused my GLfloat getting converted into something else on the way to the shader's uniform float. But I'm grasping at straws.

What could be causing this strange conversion between the number I send to the uniform float and the value that the uniform float becomes? What could I do to fix it if it's possible to fix?

Thanks in advanced for any help and leads, I really appreciate it :)

George Hanna provided a link to a post where someone had a similar issue. I read over the comments and someone said to use -DGL_GLEXT_PROTOTYPES as a CFLAG. So I rolled back my local code to use glUniform1f() again, added -DGL_GLEXT_PROTOTYPES to the Makefile, and everything worked! Even crazier, all the compiler warnings I had for implicit declarations of OpenGL functions were gone!

So in addition to the answer below, if you have this issue, try adding -DGL_GLEXT_PROTOTYPES to your CFLAGS.

(You can also get this affect by adding #define GL_GLEXT_PROTOTYPES before any OpenGL #includes)

• Are you checking that there isnt any errors in your program using for example: GLenum err; while((err = glGetError()) != GL_NO_ERROR) { //Process/log the error. } – George Hanna Aug 29 '17 at 7:39
• Hi @GeorgeHanna, I had this code chuck full of error checking with every OpenGL call and there were no errors. I cleaned it out to make the code easier to follow on Github. – Faison Zutavern Aug 29 '17 at 17:36

You can try solving the issue by using glUniform1fv(uniFactor, 1, &factorToSet); instead of glUniform1f as this has worked for others.

Similar problem and possible fix

• This solved the issue. I'm not sure why OpenGL is having a better time getting a reference to a value rather than a copy of the value, but I'll roll with it :) Thanks! (FYI, I can't award the bounty for another 2 hours, so hang tight for that) – Faison Zutavern Aug 29 '17 at 18:17
• Hi @GeorgeHanna, I read further in the comments of that link you provided and someone mentioned adding -DGL_GLEXT_PROTOTYPES to the CFLAGS for gcc. So I rolled back my code to use gluniform1f(uniFactor, factorToSet) and added -DGL_GLEXT_PROTOTYPES to the Makefile and it worked! I used to have a bunch of warnings for implicit declarations of OpenGL functions too, but those are gone when the flag is added. I'll add that as a note on the original question just in case anyone else has this issue in the future. – Faison Zutavern Aug 29 '17 at 18:37
• Great that it worked out. It seems to be some kind of compiler issue. – George Hanna Aug 29 '17 at 19:20

You need to either use

glProgramUniform1f(shader_program, uniFactor, factorToSet);


if you have either at least GL 4.1 or the ARB_separate_shader_objects extension or use

glUseProgram(shader_program); // CALL THIS FIRST

glUniform1f(uniFactor, factorToSet);


to select which shader you want to "work on" before you can change the uniforms.

Uniform values are conceptually stored "inside" the shader program (actual implementation may vary but act as-if the same), each individual programs have their own uniforms so you must select the shader program first or use glProgramUniform1f to set uniforms on a specific program without selecting it first.

For completeness: If you want to share uniform values between shader programs you can use Uniform Buffer Objects.

• Hi there. It's not 100% clear from my code snippets above, but if you look at the code on github, I am calling glUseProgram() on line 161. My question isn't "How do I set a uniform float?" My question is "Why are the values I send to a uniform float radically off?" If you look at the code snippet with a while loop (From lines 188-197), I'm sending a float to a uniform float, then I immediately retrieve it. Look at the results that start with "Factor of 1.0 becomes 0.000000" to see my issue. – Faison Zutavern Aug 29 '17 at 3:28