Alpha Transition Based on FPS ?

I have 2 images I want to switch, they are img1 and img2. It’s working already, I can switch them by changing the transition with this :

glUniform1f(mix_loc, mix_value);
mix_value = mix_value >= 1.0f ? 0.0f : mix_value + 0.01f;

How do I change the transition based on fps ? for example 2x per second (thus 120fps) ?
also, I checked with mix_value + 0.001f (for very slow transition) and there’s there’s no transition after img2 back to img1.
So what I saw is : img1 --> alpha transition until reaching img2 --> img2 --> img1
Why is there no transition after the 2nd texture ?

Query the time from your OS or toolkit. If you’re using GLUT, you can use glutGet(GLUT_ELAPSED_TIME) to read the number of milliseconds since initialisation. In C++, you can use the functions in the <chrono> header. The only time function in standard C has a resolution of a second, which is too long.

[QUOTE=pixie_laluna;1292976]
also, I checked with mix_value + 0.001f (for very slow transition) and there’s there’s no transition after img2 back to img1.
So what I saw is : img1 --> alpha transition until reaching img2 --> img2 --> img1
Why is there no transition after the 2nd texture ?[/QUOTE]
Why would there be? mix_value increases from 0.0 to 1.0 in increments of 0.01, then immediately resets to 0.0. IOW, it’s a sawtooth waveform, not a triangle waveform. If you want a triangle waveform, you need to alternate between increasing and decreasing, e.g.


// initialisation
mix_value = 0.0;
delta = 0.01;
...
// update
mix_value += delta;
if (mix_value >= 1.0) { mix_value = 1.0; delta = -delta; }
if (mix_value <= 0.0) { mix_value = 0.0; delta = -delta; }

Hi, thanks ! I’m using GLFW, but I get the idea from using elapsed time. I will try with GLFW.
Also thanks for the suggestion about triangle waveform, I’ll try that as well.