ios - 在不使用 GLKIT 的情况下创建(从头开始)iOS 7.0 OpenGL ES 2.0 应用程序时出现问题

标签 ios objective-c opengl-es

这不是 OpenGL ES 2 without GLKit 的副本

我在 iOS 上编写 OpenGL 程序已有 4 年,在 ES2 上编写程序将近 2 年。但是随着 GLKit 和 Storyboards 的加入,以及最新的 Xcode,项目模板有所不同。并且不再有创建 EAGLView 类的 OpenGL 模板。我不想使用 GLKit。我更喜欢始终以编程方式做事。我尽量避免使用 Nib ,而且我以前从未使用过 Storyboard。但在下面的这种情况下,我将保留模板中的原样。

我的步骤:

我从一个新项目开始,然后选择单 View 应用程序。我运行它,它运行良好,但出现白屏。

接下来我转到构建阶段并添加 QuartzCore、OpenGLES。

然后我创建新文件并添加一个 Objective-C 类,使其成为 UIView 的子类并命名为 EAGLView。在我导入的 .h 和 EAGLContext *context

在 initWithFrame 的 .m 文件中,我添加了这段初始化代码...

    CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;

    eaglLayer.opaque = TRUE;


    eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking,
                                    kEAGLColorFormatRGB565, kEAGLDrawablePropertyColorFormat,

                                    nil];

    // Creates the EAGLContext and set it as the current one.
    _context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    if (!_context || ![EAGLContext setCurrentContext:_context])
    {
        NSLog(@"_context no workie!");
        return nil;
    }

在我的 ViewController.h 中,我添加了 @class EAGLView; 和一个属性 EAGLView *glView;

我在 viewDidLoad 的 ViewController.m 中添加:

CGRect rect = [[UIScreen mainScreen] applicationFrame];
int width=rect.size.width;
int height=rect.size.height;


glView = [[EAGLView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
if([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    glView.contentScaleFactor = [[UIScreen mainScreen] scale];
}
glView.contentMode = UIViewContentModeScaleToFill;
self.view = glView;

然后我运行它。我收到此错误消息:

-[CALayer setDrawableProperties:]: 无法识别的选择器发送到实例 0x8e441c0

我的 CAEAGLLayer eaglLayer 怎么没有 .drawableProperties 的选择器?我是否错过了某处步骤?

* 更新 *

我找到了这个解决方案:

在 EAGLView.m 中,我添加了以下内容:

+ (Class) layerClass
{
    return [CAEAGLLayer class];
}

现在它可以正常运行而不会崩溃。谁能告诉我为什么需要它?

最佳答案

您总是需要覆盖此方法。似乎默认图层类是 CALayer,它没有 setDrawableProperties: 方法。您可以看到 CAEAGLLayerCALayer 的子类。

在管道的某处,类似这样的东西被称为

[[[self class] layerClass] performSelector:@selector(setDrawableProperties:) withObject:properties];

它会在您不覆盖 layerClass 方法的情况下生成异常。

关于ios - 在不使用 GLKIT 的情况下创建(从头开始)iOS 7.0 OpenGL ES 2.0 应用程序时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23560153/

相关文章:

android - 用于移动和 Web 应用程序的服务器

ios - iOS 上的 char 是有符号的还是无符号的?

objective-c - 如何使一个 View Controller 在 iPhone 的纵向 View 中查看

objective-c - 调用 numberOfRowsInSection : from heightForRowAtIndexPath

ios - 基于可变数量的参数构建 NSString

ios - OpenGL : required lifetime of vertex arrays

ios - OpenGL图像查看器

iphone - Google plus API 不会在登录时返回到应用程序

objective-c - 检查sqlite列类型

android - 使用 OpenGL ES 将视觉效果应用于图像