绘制 OPENGL ES 时必须使用计时器吗

发布于 2024-12-07 11:36:09 字数 639 浏览 1 评论 0原文

我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江南烟雨〆相思醉 2024-12-14 11:36:09

自从你问以来已经有一段时间了,但问题是时间间隔设置为 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.

同展鸳鸯锦 2024-12-14 11:36:09

OpenGL ES 不需要计时器。对于我的绘图应用程序,每次用户触摸屏幕时都会调用渲染方法。

然而,对于游戏,大多数开发人员使用 CADisplayLink 来调用 render 或 gameloop 方法而不是 NSTimer,因为 CADisplayLink 会在每次屏幕刷新时调用 render 方法。

设置 CADisplayLink 的过程如下例所示。

- (void)setupDisplayLink {
    //this sets up the game loop to be called once each time the screen refreshes
    CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(gameLoop:)];

    [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}

那么 gameLoop 应该设置为:

- (void)gameLoop:(CADisplayLink *)displayLink {
    [self updateModel];
    [self render];
}

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.

- (void)setupDisplayLink {
    //this sets up the game loop to be called once each time the screen refreshes
    CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(gameLoop:)];

    [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}

Then the gameLoop should be setup as:

- (void)gameLoop:(CADisplayLink *)displayLink {
    [self updateModel];
    [self render];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文