First, some details:
- I'm learning OpenGL from the tutorials on https://open.gl
- My computer is running Linux Mint 18.1 Xfce 64-bit
- My graphics card is a GeForce GTX 960M
- OpenGL Version: 4.5.0 NVIDIA 375.66
- GLSL Version: 4.50 NVIDIA
- Graphics Card Driver: nvidia-375 Version: 375.66-0ubuntu0.16.04.1
- CPU: Intel Core i7 6700HQ
- The code I'm working on can be found here: https://github.com/Faison/sdl2-learning/blob/8a61032d20edf91cfa60f665e1bb4d72e58f634b/phase-01-initial-setup/main.c (Makefile is located in the same directory)
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 :)
An additional note after receiving a working answer
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 #include
s)