wjl
wjl

Reputation: 7351

How can I tell OpenGL how often to draw stuff?

Okay, I'm going to sound like an idiot with this one. Here goes.

I've been doing iOS development for about a year now, but only tonight have I started doing anything OpenGL related. I've followed Jeff LaMarche's wonderful guide and I'm drawing a neat looking triangle, and I got it to flip around and stuff. I'm one entertained programmer.

Okay, here's the stupid question part: How can I set somewhere for OpenGL to perform glRotatef and glDrawArrays + friends continuously, or at a set frames per second? I've tried Googling it, but really can't come up with good search terms.

Thanks in advance, and get ready to field a ton more of these questions.

Upvotes: 1

Views: 339

Answers (3)

Brad Larson
Brad Larson

Reputation: 170309

While the others make good suggestions for the general case of OpenGL ES, I know that you're probably working on iOS here, Will, so there's a better platform-specific alternative. In your case, I believe you'll be better served by CADisplayLink, which fires off callbacks that are synchronized with the refresh rate of the screen. Using this, you'll get far smoother updates than with a timer or some kind of polling within a loop.

This is particularly effective when combined with Grand Central Dispatch, as I describe in my answer here. When I switched from using a loop to CADisplayLink for updates, my rendering became much smoother on all iOS devices due to fewer dropped frames. Adding GCD on top of that made things even better.

You can refer to my Molecules code for an example of this in action (see the SLSMoleculeGLViewController for how my autorotation is animated with this). Apple's OpenGL ES application template also uses CADisplayLink for updates, last I checked.

Upvotes: 2

Thomas Minor
Thomas Minor

Reputation: 657

You should read up on the concept of game loops.

http://entropyinteractive.com/2011/02/game-engine-design-the-game-loop/ is a good resource to get you started.

Upvotes: 1

oSz
oSz

Reputation: 63

Well I'm not an expert on the subject but can't you just put the rotate/draw commands in a while loop that ends when a certain button is pressed or when a specific event occurs ?

Upvotes: 0

Related Questions