Reputation: 147
I am going through the tutorial at http://arcsynthesis.org/gltut/Positioning/Tut03%20More%20Power%20To%20The%20Shaders.html
I am currently on a lesson which creates a triangle, moves it in a circular direction and gradually change the color from white to green. The color will jump back to white after a complete cycle. I am trying to make the color move gradually from white to green and back. My conditional statement to set my color change direction appears to never evaluate to true so my change never takes effect.
Here is my fragment shader.
#version 330
out vec4 outputColor;
uniform float fragLoopDuration; //5.0f
uniform float time;
const vec4 firstColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
const vec4 secondColor = vec4(0.0f, 1.0f, 0.0f, 1.0f);
void main()
{
bool myFlag = false; //flag will indicate which direction color change will take.
float currTime = mod(time, fragLoopDuration);
float currLerp = currTime / fragLoopDuration; //currLerp range = (0.0f-1.0f)
if (currLerp == 0.9f) myFlag = !myFlag; //this flag is not geting triggered.
if (myFlag) outputColor = mix(secondColor, firstColor, currLerp * -1.0f + 1.0f); //green to white
if (!myFlag) outputColor = mix(firstColor, secondColor, currLerp); //white to green
}
Upvotes: 0
Views: 4170
Reputation: 473537
Sorry, problem is my conditional statement to set my flag appears to never evaluate to true.
What do you expect? In order for currLerp
to be exactly equal to 0.9f, currTime
and fragLoopDuration
must be very specific and very exact values. If currTime
is even slightly off, by even 0.000001, then currLerp
won't be exactly equal to 0.9f, and thus your condition will never be true.
In general, you should never test a float for equality. That is, equal to another value.
And even if it is exactly equal, it will only ever happen once. So it will be true on exactly one frame; the next frame, when currLerp
is 0.95f or something, it will be false again.
I'm guessing you mean >=
rather than ==
.
As an aside, your code is not structured the way most programmers would do it.
You have this flag that you set. Then you have two if
statements, one that will be executed if the flag is true, and one that will be executed if the flag is false. This is a waste and poor coding style. Pretty much every language with if
will also have some form of else
: the thing that happens if the condition is not true.
The standard way to do this is here:
if (myFlag)
outputColor = mix(secondColor, firstColor, currLerp * -1.0f + 1.0f);
else
outputColor = mix(firstColor, secondColor, currLerp);
And since you only use myFlag
once, there's no point in even having a variable:
if (currLerp == 0.9f) //Fix this, of course
outputColor = mix(secondColor, firstColor, currLerp * -1.0f + 1.0f);
else
outputColor = mix(firstColor, secondColor, currLerp);
Upvotes: 2