5.OpenGL学习笔记 - 搭建iOS开发环境(不用GLKit框架)

本文介绍了如何在不使用GLKit框架的情况下,在iOS上搭建OpenGL ES的开发环境。通过自定义UIView子类,并配置layer、设置context、renderBuffer和FrameBuffer,最终实现将屏幕显示为红色的效果。详细代码可在提供的GitHub链接中查看。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文不用GLKit框架来搭建OpenGL ES在iOS上的开发环境。

需要在项目中自定义一个UIView的子类,下面所有操作均在这个子类中进行。

导入头文件

#import <OpenGLES/ES3/gl.h>

将子类view的layer改变一下:

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

添加一些用到的成员变量:

@implementation EAGLView
{
    EAGLContext *_eaglContext;
    CAEAGLLayer *_eaglLayer;
    GLuint _colorBufferRender;
    GLuint _frameBuffer;

}

设置context,选用版本3的API,context不能跨线程访问:

    //设置context
    _eaglContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
    [EAGLContext setCurrentContext:_eaglContext];

设置layer:

_eaglLayer = (CAEAGLLayer*)self.layer;
_eaglLayer.frame = self.frame;
_eaglLayer.opaque = YES;
_eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                     [NSNumber numberWithBool:YES],
                                     kEAGLDrawablePropertyRetainedBacking,
                                     kEAGLColorFormatRGBA8,
                                     kEAGLDrawablePropertyColorFormat, nil];

设置renderBuffer和FrameBuffer :

    glGenRenderbuffers(1, &_colorBufferRender);
    glBindRenderbuffer(GL_RENDERBUFFER, _colorBufferRender);
    [_eaglContext renderbufferStorage:GL_RENDERBUFFER fromDrawable:_eaglLayer];

    glGenFramebuffers(1, &_frameBuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, _frameBuffer);

    glFramebufferRenderbuffer(GL_FRAMEBUFFER,
                              GL_COLOR_ATTACHMENT0,
                              GL_RENDERBUFFER,
                              _colorBufferRender);

调用如下代码,将屏幕设置成红色:

    glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
    
    glClear(GL_COLOR_BUFFER_BIT);
    
    [_eaglContext presentRenderbuffer:GL_RENDERBUFFER];

经过如上步骤,就设置完了OpenGL在iOS上到环境。

代码地址:

https://github.com/whoyouare888/Note/tree/master/OpenGL/OpenGL%20ES%20%E6%90%AD%E5%BB%BA%E7%8E%AF%E5%A2%83

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值