I'm using OpenGL ES to make a game on the iPhone. Unfortunately, I see small (and irregular) hiccups.
I use a timer with a sleep, calling the draw function every 60th of a second, in order to garantee a solid frame rate. I've tried to change the moment in time my timer awakens from its sleep, giving the draw function more time to execute. The hiccups do get less once the draw function's been given more time. With 8 milliseconds, the animations are almost fluid. My findings are:
- Apparently giving the GPU more time to execute the actual drawing, results in an (almost) perfect fluid animation.
- Drawing at the exact end of my frame results in stutter, hiccups and what not.
Now that I know that, I'm not sure how to proceed. I have two contradicting ideas about the cause of this behaviour:
- First, could it be that the OpenGL commands interfere with the drawing of the previous frame? As far as I understand this can't be the case since the commands are stored and will only execute when the draw command is given.
- Second, could the fluctuating time of the draw commands cause the timer to skip a tick?
So which explanation is more likely? Or is neither? Of course I could just try to put the draw function in a seperate thread and see if that solves my problem. But I hope to understand more of OpenGL.
This is the function being called and explains what I'm taking about:
- (void) drawView
{
// measure time with mach_absolute_time
// gameEngine update
// OpenGL commands (translate, rotate, drawArrays etc.)
// end measure time with mach_absolute_time
// usleep(animationInterval - duration - constant)
// constant is the time to start executing
// draw
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}