opengles视图切换问题

发布于 2024-07-30 13:21:43 字数 340 浏览 3 评论 0原文

我正在尝试使用 OpenGLES 制作简单的游戏。 我有两个 EAGLView(菜单和游戏视图)。 每个视图都有自己的viewController。 视图的初始化是通过 viewController 的 initWithNIBName 方法完成的。 当我想显示视图时,我只需使用主窗口的 addSubview 方法即可。 游戏视图仅在启动时初始化一次。 仅当需要时才初始化菜单视图。 问题是,当我从游戏视图转到菜单然后再返回,然后重新绘制游戏视图时,会出现问题。 (我在绘制之前在drawView方法中设置EAGLContext,所以上下文可能是正确的)。 你不知道问题出在哪里吗? 或者如果整个切换管理错误,请给我和建议。 感谢您的回复。

I´m trying to make simple game using OpenGLES.
I have two EAGLViews(menu and game view).
Each view has its own viewController. Initializing of the views is done by initWithNIBName method of the viewController.
And when I want to show the view, I simply use addSubview method of the main window.
The game view is initialized only once at the launch time. Menu view is initialized only if it´s needed. Problem is, that when I go from game view to menu and then back, and then I redraw the game view, something goes wrong. (I'm setting EAGLContext in drawView method before drawing, so the context may be right).
Don´t you know where is the problem?
Or if the whole switching is managed wrong, gimme and advice please.
Thanks for replies.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

盗心人 2024-08-06 13:21:43

我猜您遇到了纹理无法正确显示的问题?

我不知道 OpenGL 背后的真实情况,但这是我的假设:
每次返回 EAGLView 时,EAGLView 的 EAGLContext 都会更改。 (如果您已经从 OpenGLES 模板复制并粘贴)纹理只能在上下文处于正确状态后加载,否则您无法加载任何纹理。 现在,通过离开 EAGLView,然后返回,您将从 initWithCoder:(NSCoder*)coder 实例化一个新的 EAGLContext:

    context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];

    if (!context || ![EAGLContext setCurrentContext:context]) {
        [self release];
        return nil;
    }

那么我们如何保留这个上下文呢? 我让它全球化。 就那么简单。 当再次实例化 EAGLView 时,让它检查“全局”EAGLContext 是否为零。 如果它为零,则只需实例化它,否则什么都不做。 除非您想退出程序,否则永远不要释放或释放此全局 EAGLContext。

这对我有用,但同样,我上面的假设可能不正确。 如果有人知道真实情况,请教我。 我也虚心地需要指导。 我想真正知道为什么会发生这种情况以及为什么我们也必须这样做。

顺便问一下,Jenicek,这能回答你的问题吗?

I guess you are having a trouble with texture not showing correctly?

I don't know the real thing behind OpenGL, but this is my hypothesis:
Each time you came back to the EAGLView, The EAGLContext of EAGLView is changed. (if you have been copying and pasting from the OpenGLES template) The textures can only be loaded after the context is in its correct state, or else you cannot load any texture. Now, by leaving the EAGLView, and coming back, you are instantiating a new EAGLContext from initWithCoder:(NSCoder*)coder :

    context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];

    if (!context || ![EAGLContext setCurrentContext:context]) {
        [self release];
        return nil;
    }

So how can we preserve this context? I make it global. Simple as that. And when EAGLView is to be instantiated once again, have it check that whether the "global" EAGLContext is nil or not. If it's nil, just instantiate it, else do nothing. And never ever release or dealloc this global EAGLContext unless you want to quit your program.

This works for me, but again, my hypothesis above may not be correct. If anybody knows the real thing, please lecture me. I am also humbly in need of guidance. I want to truly know why this occurs and why do we have to do this as well.

And by the way, does this answer your question, Jenicek?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文