ios搭建OpenGL环境

前言

本篇文章介绍在ios搭建OpenGL开发环境

app的启动文章中,讲述了一个ios应用是如何启动的以及在IOS 13之后苹果公司推出的多窗口功能,通过app的启动这篇文章,我们基本能随心所欲的搭建一个app应用环境,搭建完成后的基本文件列表如下:

ViewController

接下来,我们要开始准备OpenGL环境,苹果默认提供了支持OpenGL的视图界面。

  • 首先我们需要让主界面绑定的ViewController遵循GLKViewDelegate协议

    注意:需要#import <GLKit/GLKit.h>

    复制代码
    #import <GLKit/GLKit.h>
    #import <
    @interface ViewController : 	UIViewController<GLKViewDelegate>
    @end

    GLKViewDelegate协议有一个必须遵守的接口,该接口是GLKView对象进行绘制的时候会调用的接口

    复制代码
    - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect;
  • 接下来,需要定义GLKView变量,并且要进行初始化

    复制代码
    @property (nonatomic, strong) GLKView        * glkView;
    
    -(void)glkInit
    {
        // 获取当前窗体
        UIWindow* window = [[UIApplication sharedApplication] windows].firstObject;
        // 获取显示的范围
        CGRect bound = CGRectMake(0, window.safeAreaInsets.top, self.view.frame.size.width, self.view.frame.size.height-window.safeAreaInsets.top);
        // 初始化GLKView
        _glkView = [[GLKView alloc] initWithFrame:bound];
        // 初始化EAGLContext上下文
        _glkView.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
        // 设置代理
        _glkView.delegate = self;
        _glkView.enableSetNeedsDisplay = YES;
        // 添加glkView到当前视图
        [[self view] addSubview:_glkView];
    }

    这个时候已经可以执行了,我们在-(void)glkView:(GLKView *)view drawInRect:(CGRect)rect加入代码

    复制代码
    - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
    {
    	glClearColor(1, 0, 0, 1);
    	glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
    }

    运行发现屏幕确实变红了

  • 但是,如果我们在上面绘制的方法里边打印一条日志,会发现日志不会一直在刷新打印。一般来说,对于游戏,我们需要一个帧率。而当前的绘制方法无法满足我们对帧率的需求。

    我们可以使用CADisplayLink来控制,CADisplayLink是一种定时器类型,它可以让你在每秒钟屏幕更新时执行一段代码。CADisplayLink定时器的精度非常高,因为它是和屏幕刷新频率同步的,所以可以确保动画的流畅度。另外,CADisplayLink定时器的调用方法是通过RunLoop进行的,所以它是线程安全的。

    使用CADisplayLink定时器的步骤如下:

    • 创建CADisplayLink对象。
    • 设置定时器的目标和选择器。
    • 将CADisplayLink添加到RunLoop中。

    我们在新建一个timerInit方法来使用CADisplayLink定时器,代码如下:

    复制代码
    -(void)timerInit
    {
        if(!_displayLink)
        {
            _displayLink  = [CADisplayLink displayLinkWithTarget:self selector:@selector(render:)];
            [_displayLink setPreferredFramesPerSecond:60];
            [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode: NSRunLoopCommonModes];
        }
    }
    
    - (void)render:(CADisplayLink *)displayLink
    {
        [_glkView display];
    }
  • 然后我们需要设置

    复制代码
    _glkView.enableSetNeedsDisplay = NO;

这个时候再次运行发现日志能够一直打印了。

  • 接下来就可以在实现引擎刷新的逻辑了

gitlab地址

代码已经上传到gitlab

地址:https://gitlab.com/lingyanTools/sk_ios_gl.git

相关推荐
2501_916013742 小时前
移动端网页调试实战,跨设备兼容与触控交互问题排查全流程
android·ios·小程序·https·uni-app·iphone·webview
叽哥2 小时前
flutter学习第 10 节:表单与输入
android·flutter·ios
2501_915106326 小时前
TF 上架全流程实战,从构建到 TestFlight 分发
android·ios·小程序·https·uni-app·iphone·webview
YungFan7 小时前
iOS26适配指南之UIVisualEffectView
ios·swift
Magic_ht8 小时前
UIApplicationDelegate执行说明
ios
Magic_ht8 小时前
NSThread
ios
归辞...21 小时前
「iOS」————分类与扩展
ios·分类·cocoa
Digitally1 天前
如何将联系人从 iPhone 无缝传输到 iPad?
ios·iphone·ipad
00后程序员张1 天前
Charles中文版抓包工具功能解析,提升API调试与网络性能优化
android·ios·小程序·https·uni-app·iphone·webview
2501_916013742 天前
iOS混淆工具有哪些?跨平台 App 混淆与保护的实用方案
android·ios·小程序·https·uni-app·iphone·webview