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;
GLint uniFactor = glGetUniformLocation(shader_program, "factor");
while (factorToSet > -0.1f) {
glUniform1f(uniFactor, factorToSet);
glGetUniformfv(shader_program, uniFactor, &setFactor);
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 :)