glfwGetTime
Returns the seconds since glfwInit, as a double with sub-microsecond precision.
Signature
double glfwGetTime(void);
How it works
Two uses appear in almost every lesson. Animation: pass the time to a shader as a uniform, so things move with sin(time). Frame time: subtract the time of the previous frame to get deltaTime, then multiply speeds by it, so movement is the same at 30 or 144 frames per second.
The clock uses the system's high-resolution timer. glfwSetTime can reset it, but you rarely need to.
Return value
Seconds since GLFW started (or since the last glfwSetTime). 0 if GLFW is not initialized.
Tips & pitfalls
Keep it in a double. A float has only about 7 significant digits: after some hours the frame time rounds to steps of several milliseconds and animations stutter.
Errors
When the call is invalid it does nothing and sets an error, which glGetError returns:
- GLFW_NOT_INITIALIZED
- Called before glfwInit or after glfwTerminate.
Example
1double last = glfwGetTime();
2while (!glfwWindowShouldClose(window)) {
3 double now = glfwGetTime();
4 float deltaTime = float(now - last);
5 last = now;
6 glUniform1f(timeLoc, float(now));
7 update(deltaTime);
8 render();
9 glfwSwapBuffers(window);
10 glfwPollEvents();
11}