i’m programming a little game with opengl and C.
I’ve encountered an issue:
due to clipping… my animation (actually based on glutTimerFunc) haven’t a constant fps.
A variable fps isn’t a problem, but the animation is affected, so it don’t correspond to the timing, then: fps1,pos1; fps2,pos2; fps3,pos3…
But if the fps isn’t regular, so animation doesn’t.
I would implement it like: fps1,pos1;(if fps1 isn’t drawed) fps2,pos1; (fps1 drawing ended, so now, because i skipped a frame) fps3,pos3.
how can i do it? i would use the clock() function, but it gives always 0
ps: howto force v-sync in opengl?
Whether OpenGL updates are synchronised with the vertical retrace will be communicated in an platform-specific manner since it’s related to however your platform has tied OpenGL into itself. So it’s a tick box in the interface designer on the Mac, there’s a WGL extension called WGL_EXT_swap_control over on Windows and a GLX extension in X11.
Assuming you have some means of installing a callback that is triggered upon vertical retrace (such as via a
CVDisplayLinkon the Mac; sadly I’m not up on the Windows/Linux equivalents), and you want your physics to run at a constant number of steps per second, you could write something like:That timeError stuff is to regulate against rounding problems. E.g. suppose you were running at a constant 70 frames a second and wanted physics updates 60 times a second. If you ignored the error that accumulates then you’d actually run zero physics updates a second since at each instant the code would come to the conclusion that the amount of time since the last frame draw wasn’t long enough for a physics update to have been required. And you’d also otherwise get aliasing problems if you went the other way, with any frame rate that isn’t an integer divisor of the physics update rate.
If you don’t have an explicit retrace function but instead a perpetual loop that pushes out frames but blocks on vertical retrace then you’d write the same sort of thing.
If you have no way to block on vertical retrace at all, you might install a timer at a target frame rate and pretend that’s a vertical retrace sync. You could just push frames out as fast as possible but you’ll likely get complaints with anyone with a laptop or other machine where it’s pretty obvious when the fans are coming on.