绘制 OPENGL ES 时必须使用计时器吗
我有一个名为 startAnimation 的方法:
-(void)startAnimation{
1: self.animationTimer=[NSTimer scheduledTimerWithTimeInterval:1/60
target:self selector:@selector(gameLoop)
userInfo:nil repeats:YES];
2: //[self gameLoop]
{
gameLoop 方法是这样的:
-(void)gameLoop{
[self updateModel];
[self render]
{
现在,发生了一件非常奇怪的事情。如果我在 startAnimation 方法中注释第 1 行并取消注释第 2 行,我将不会将对象渲染到屏幕上。我认为渲染可能需要不断调用 gameLoop 方法。但即使我将计时器设置为不重复(因此重复:否)也会绘制对象。这意味着只调用一次 gameLoop 方法,但从 NStimer 对象调用就足够了。但如果我手动调用 gameLoop 方法,我不会绘制对象。我尝试在执行 100 次的循环内调用该方法。这也没有帮助。 OPENGL 的计时器有什么特别之处吗?很抱歉这个问题还太不成熟。
I have a method called startAnimation:
-(void)startAnimation{
1: self.animationTimer=[NSTimer scheduledTimerWithTimeInterval:1/60
target:self selector:@selector(gameLoop)
userInfo:nil repeats:YES];
2: //[self gameLoop]
{
The gameLoop method is like this:
-(void)gameLoop{
[self updateModel];
[self render]
{
Now, a very strange thing happens. If I comment the line 1 and uncomment the line 2 in startAnimation method I will not get the objects rendered to my screen. I thought rendering might require continuously calling the gameLoop metod. But even if I set the timer to not repeat (so repats:NO) objects are drawn. It means calling the gameLoop method just once, but from an NStimer object is enough. But if I call the gameLoop method manually I do not get the objects drawn. I tried calling the method inside a loop which executes 100 times. It did not help either. Is there something special with the timers in regards with OPENGL?Sorry for the question if it's too immature.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
自从你问以来已经有一段时间了,但问题是时间间隔设置为 1/60,这是 0,因为它使用整数除法。您应该改用 1.0/60.0。
It´s been some time since you asked but the problem there is that the time interval is set to 1/60, which is 0 because it´s using the integer division. You should use 1.0/60.0 instead.
OpenGL ES 不需要计时器。对于我的绘图应用程序,每次用户触摸屏幕时都会调用渲染方法。
然而,对于游戏,大多数开发人员使用 CADisplayLink 来调用 render 或 gameloop 方法而不是 NSTimer,因为 CADisplayLink 会在每次屏幕刷新时调用 render 方法。
设置 CADisplayLink 的过程如下例所示。
那么 gameLoop 应该设置为:
A timer is not needed for OpenGL ES. For my drawing app the render method is called everytime the user touches the screen.
However, for games most developers use CADisplayLink to call the render or gameloop method instead of NSTimer, as CADisplayLink will call the render method each time the screen refreshes.
Setting up a CADisplayLink is done like the example below.
Then the gameLoop should be setup as: