opengles视图切换问题 [英] opengles view switching problem

查看:110
本文介绍了opengles视图切换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用OpenGLES制作简单的游戏. 我有两个EAGLViews(菜单和游戏视图). 每个视图都有自己的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.

推荐答案

我想您在纹理显示不正确方面遇到麻烦?

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

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

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;
    }

那么我们如何保留此上下文?我使它成为全球性的.就那么简单.当再次实例化EAGLView时,请检查全局" EAGLContext是否为nil.如果为零,则只需实例化它,否则什么也不做.除非您要退出程序,否则永远不要释放或取消分配此全局EAGLContext.

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.

顺便说一句,这是否回答了您的问题,Jenicek?

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

这篇关于opengles视图切换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆